function import_csv($csv) { $error = 0; $create = 0; $update = 0; $error_mess = array(); while ($tab = $csv->readLine()) { //init optionnal values $AllKeyPossible = array_merge($csv->getKeys(), $csv->getOptionalKeys()); $tab = initArray($AllKeyPossible, $tab); //check $category = \Pasteque\CategoriesService::getByName($tab['category']); $taxCat = \Pasteque\TaxesService::getByName($tab['tax_cat']); if ($taxCat && $category) { $prod = readProductLine($tab, $category, $taxCat); $product_exist = \Pasteque\ProductsService::getByRef($prod->reference); if ($product_exist !== null) { // update product $prod->id = $product_exist->id; $prod = mergeProduct($product_exist, $prod); //if update imposible an is occurred if (!\Pasteque\ProductsService::update($prod)) { $error++; $error_mess[] = \i18n("On line %d: " . "Cannot update product: '%s'", PLUGIN_NAME, $csv->getCurrentLineNumber(), $tab['label']); } else { // update stock_curr and stock_diary manage_stock_level($prod->id, $tab, FALSE); $update++; } } else { // create product $id = \Pasteque\ProductsService::create($prod); if ($id) { //create stock_curr and stock diary manage_stock_level($id, $tab, TRUE); $create++; } else { $error++; $error_mess[] = \i18n("On line %d: " . "Cannot create product: '%s'", PLUGIN_NAME, $csv->getCurrentLineNumber(), $tab['label']); } } } 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']); } } } $message = \i18n("%d line(s) inserted, %d line(s) modified, %d error(s)", PLUGIN_NAME, $create, $update, $error); return array($message, $error_mess); }
} } $scaled = isset($_POST['scaled']) ? 1 : 0; $visible = isset($_POST['visible']) ? 1 : 0; $discount_enabled = FALSE; $discount_rate = 0.0; if (isset($_POST['discountRate'])) { $discount_enabled = isset($_POST['discountEnabled']) ? 1 : 0; $discount_rate = $_POST['discountRate']; } $attr = null; if (isset($_POST['attributeSetId']) && $_POST['attributeSetId'] !== "") { $attr = $_POST['attributeSetId']; } $prd = \Pasteque\Product::__build($_POST['id'], $_POST['reference'], $_POST['label'], $_POST['realsell'], $catId, $provId, $disp_order, $taxCatId, $visible, $scaled, $_POST['priceBuy'], $attr, $_POST['barcode'], $img != null, $discount_enabled, $discount_rate); if (\Pasteque\ProductsService::update($prd, $img)) { $message = \i18n("Changes saved"); } else { $error = \i18n("Unable to save changes"); } } } else { if (isset($_POST['reference'])) { if (isset($_POST['reference']) && isset($_POST['label']) && isset($_POST['realsell']) && isset($_POST['categoryId']) && isset($_POST['taxCatId'])) { $catId = $_POST['categoryId']; $provId = $_POST['providerId']; $disp_order = $_POST['dispOrder'] == "" ? NULL : $_POST['dispOrder']; $taxCatId = $_POST['taxCatId']; if ($_FILES['image']['tmp_name'] !== "") { $img = file_get_contents($_FILES['image']['tmp_name']); } else {