Commit 7dce65b7 authored by Julien Fastré's avatar Julien Fastré
Browse files

add parsing and validation to product import

+ weird bug about price.php loading...
parent b2110c9b
......@@ -19,7 +19,7 @@
// along with Pastèque. If not, see <http://www.gnu.org/licenses/>.
require_once('ValidationException.php');
require_once('validators/price.php');
//require_once('validators/price.php');
require_once('validators/ean.php');
require_once('validators/boolean.php');
......@@ -129,6 +129,7 @@ require_once(PT::$ABSPATH . "/inc/data/services/InventoriesService.php");
require_once(PT::$ABSPATH . "/inc/Csv.php");
require_once(PT::$ABSPATH . "/inc/catalog.php");
require_once(PT::$ABSPATH . "/inc/data/validation/load.php");
require_once(PT::$ABSPATH . "/inc/data/validation/validators/price.php");
require_once(PT::$ABSPATH . "/inc/data/parsing/load.php");
load_base_i18n(detect_preferred_language());
......
......@@ -55,13 +55,16 @@ function import_csv($csv) {
//init optionnal values
$AllKeyPossible = array_merge($csv->getKeys(), $csv->getOptionalKeys());
$tab = initArray($AllKeyPossible, $tab);
$product_errors = validateLine($tab, $csv->getCurrentLineNumber());
$error_mess = array_merge($product_errors, $error_mess);
//check
$category = \Pasteque\CategoriesService::getByName($tab['category']);
$provider = \Pasteque\ProvidersService::getByName($tab['provider']);
$taxCat = \Pasteque\TaxesService::getByName($tab['tax_cat']);
if ($taxCat && $category) {
if (count($product_errors) === 0) {
$prod = readProductLine($tab, $category, $provider, $taxCat);
$product_exist = \Pasteque\ProductsService::getByRef($prod->reference);
if ($product_exist !== null ) {
......@@ -97,16 +100,6 @@ function import_csv($csv) {
} else {
// Missing category or tax category
$error++;
if (!$category) {
$error_mess[] = \i18n("On line %d "
. "category: '%s' doesn't exist", PLUGIN_NAME,
$csv->getCurrentLineNumber(), $tab['category']);
}
if (!$taxCat) {
$error_mess[] = \i18n("On line %d: "
. "Tax category: '%s' doesn't exist", PLUGIN_NAME,
$csv->getCurrentLineNumber(), $tab['tax_cat']);
}
}
}
......@@ -115,6 +108,70 @@ function import_csv($csv) {
return array($message, $error_mess);
}
function validateLine(&$tab, $line)
{
$error_mess = array();
//check category & provider
$category = \Pasteque\CategoriesService::getByName($tab['category']);
$provider = \Pasteque\ProvidersService::getByName($tab['provider']);
$taxCat = \Pasteque\TaxesService::getByName($tab['tax_cat']);
if (!$category) {
$error_mess[] = \i18n("On line %d "
. "category: '%s' doesn't exist", PLUGIN_NAME,
$line, $tab['category']);
}
if (!$taxCat) {
$error_mess[] = \i18n("On line %d: "
. "Tax category: '%s' doesn't exist", PLUGIN_NAME,
$line, $tab['tax_cat']);
}
$fieldsToCheck = array('barcode', 'price_buy', 'scaled', 'sellVat',
'discount_enabled');
foreach($fieldsToCheck as $field) {
try {
switch ($field) {
//parsing & validation of EAN
case 'barcode':
$barcode = \Pasteque\Parsing\parseEAN($tab[$field]);
if (\Pasteque\Validation\validateEAN($barcode)) {
$tab[$field] = $barcode;
}
break;
//parsing & validation of price
case 'price_buy':
case 'sellVat':
$price = \Pasteque\Parsing\parsePrice($tab[$field]);
if (\Pasteque\Validation\validatePrice($price)) {
$tab[$field] = $price;
}
break;
case 'scaled':
case 'discount_enabled':
$boolean = \Pasteque\Parsing\parseBoolean($tab[$field]);
if (\Pasteque\Validation\validateBoolean($boolean)) {
$tab[$field] = $boolean;
}
break;
default:
continue;
}
} catch (\Pasteque\Parsing\ParsingException $ex) {
$error_mess[] = \i18n("On line %d ", PLUGIN_NAME, $line).": ".
$ex->getI18nMessage();
} catch (\Pasteque\Validation\ValidationException $ex) {
$error_mess[] = \i18n("On line %d ", PLUGIN_NAME, $line).": ".
$ex->getI18nMessage();
}
}
return $error_mess;
}
// add to product values not obligatory may be present in array
function readProductLine($line, $category, $provider, $taxCat) {
$priceSell = $line['sellVat'] / ( 1 + $taxCat->getCurrentTax()->rate);
......
......@@ -61,6 +61,7 @@
"On line %d: Cannot update update: '%s'": "On line %d: Cannot update update: '%s'",
"On line %d: Tax category: '%s' doesn't exist": "On line %d: Tax category: '%s' doesn't exist",
"On line %d category: '%s' doesn't exist": "On line %d category: '%s' doesn't exist",
"On line %d ":"On line %d ",
"Reference": "Reference",
"Quantity": "Quantity"
......
......@@ -52,6 +52,7 @@
"On line %d: Cannot update category: '%s'": "À la ligne %d: Impossible de mettre à jour la category: '%s'",
"On line %d: Cannot create product: '%s'": "À la ligne %d: Impossible de créer le produit: '%s'",
"On line %d: Category parent doesn't exist": "À la ligne %d: La categorie parent n'existe pas",
"On line %d ":"À la ligne %d",
"Import products": "Importer des produits",
"Import products from csv file": "Importer des produits à partir d'un fichier csv",
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment