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); }
} } else { if (isset($_POST['sendCsv'])) { $key = array('Quantity', 'Reference'); $csv = new \Pasteque\Csv($_FILES['csv']['tmp_name'], $key, array(), PLUGIN_NAME); if (!$csv->open()) { $error = $csv->getErrors(); } else { //manage empty string $csv->setEmptyStringValue("Quantity", "0"); echo "<script type=\"text/javascript\">\n"; echo "jQuery(document).ready(function() {\n"; while ($tab = $csv->readLine()) { $productOk = false; $quantityOk = false; $product = \Pasteque\ProductsService::getByRef($tab['Reference']); if ($product !== null) { $productOk = true; } else { if ($error === null) { $error = array(); } $error[] = \i18n("Unable to find product %s", PLUGIN_NAME, $tab['Reference']); } if ($tab['Quantity'] === "0" || intval($tab['Quantity']) !== 0) { $quantityOk = true; } else { if ($error === null) { $error = array(); } $error[] = \i18n("Undefined quantity for product %s", PLUGIN_NAME, $tab['Reference']);
/** Main csv import function given a csv object */ function import_csv($csv) { $error = 0; $creations = 0; $updates = 0; $error_mess = array(); $srv = new \Pasteque\TariffAreasService(); $allAreas = $srv->getAll(); $taxCats = \Pasteque\TaxesService::getAll(); $areas = array(); $areasRef = array(); while ($tab = $csv->readLine()) { //check $area = null; $ref = null; if (isset($areas[$tab['area']])) { $area = $areas[$tab['area']]; $ref = $areasRef[$tab['area']]; } else { foreach ($allAreas as $a) { if ($a->label == $tab['area']) { // Set reference for update and recreate a new to // delete non imported prices $areasRef[$tab['area']] = $a; $area = \Pasteque\TariffArea::__build($a->id, $a->label, $a->dispOrder); $areas[$tab['area']] = $area; break; } } } // Create area if not found if ($area === null) { $area = new \Pasteque\TariffArea($tab['area'], 100); $area->id = $srv->create($area); if ($area->id === false) { $error++; $error_mess[] = \i18n("Line %d: Unable to create area %s", PLUGIN_NAME, $csv->getCurrentLineNumber(), $tab['area']); continue; } $areas[$tab['area']] = $area; $areasRef[$tab['area']] = null; } $product = \Pasteque\ProductsService::getByRef($tab['reference']); if ($product === null) { $error++; $error_mess[] = \i18n("Line %d: '%s' doesn't exist", PLUGIN_NAME, $csv->getCurrentLineNumber(), $tab['reference']); continue; } $taxCat = null; foreach ($taxCats as $tc) { if ($tc->id == $product->taxCatId) { $taxCat = $tc; break; } } // Check if price should be updated $priceFound = false; $sellPrice = $tab['sellVat'] / (1 + $taxCat->getCurrentTax()->rate); if ($ref !== null) { foreach ($ref->getPrices() as $price) { if ($price->productId == $product->id) { // Update $area->addPrice($product->id, $sellPrice); $priceFound = true; $updates++; break; } } } if (!$priceFound) { // Add a price $area->addPrice($product->id, $sellPrice); $creations++; } } // Update data foreach ($areas as $name => $area) { if ($srv->update($area) === false) { $error++; $error_message[] = \i18n("Unable to save tariff area %d", PLUGIN_NAME, $name); } } $message = \i18n("%d line(s) inserted, %d line(s) modified, %d error(s)", PLUGIN_NAME, $creations, $updates, $error); return array($message, $error_mess); }