예제 #1
0
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);
}
function import_csv($csv)
{
    $error_mess = array();
    $update = 0;
    $create = 0;
    $error = 0;
    while ($tab = $csv->readLine()) {
        $parentOk = false;
        if ($tab['Parent'] !== NULL) {
            $parent = \Pasteque\CategoriesService::getByName($tab['Parent']);
            $image = NULL;
            if ($parent) {
                $parentOk = true;
                $tab['Parent'] = $parent->id;
            }
        } else {
            // Category isn't subCategory
            $parentOk = true;
        }
        if ($parentOk) {
            $cat = new \Pasteque\Category($tab['Parent'], $tab['Designation'], $image, $tab['Ordre']);
            $category_exist = \Pasteque\CategoriesService::getByName($cat->label);
            //UPDATE category
            if ($category_exist) {
                $cat->id = $category_exist->id;
                if (\Pasteque\CategoriesService::updateCat($cat)) {
                    $update++;
                } else {
                    $error++;
                    $error_mess[] = \i18n("On line %d: Cannot update category: '%s'", PLUGIN_NAME, $csv->getCurrentLineNumber(), $tab['Designation']);
                }
                //CREATE category
            } else {
                $id = \Pasteque\CategoriesService::createCat($cat);
                if ($id) {
                    $create++;
                } else {
                    $error++;
                    $error_mess[] = \i18n("On line %d: Cannot create category: '%s'", PLUGIN_NAME, $csv->getCurrentLineNumber(), $tab['Designation']);
                }
            }
        } else {
            $error++;
            $error_mess[] = \i18n("On line %d: Category parent doesn't exist", PLUGIN_NAME, $csv->getCurrentLineNumber());
        }
    }
    $message = \i18n("%d line(s) inserted, %d line(s) modified, %d error(s)", PLUGIN_NAME, $create, $update, $error);
    $csv->close();
    \Pasteque\tpl_msg_box($message, $error_mess);
}