Пример #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);
}
Пример #2
0
                $img = NULL;
            }
            $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 = new \Pasteque\Product($_POST['reference'], $_POST['label'], $_POST['realsell'], $catId, $provId, $disp_order, $taxCatId, $visible, $scaled, $_POST['priceBuy'], $attr, $_POST['barcode'], $img !== null, $discount_enabled, $discount_rate);
            $id = \Pasteque\ProductsService::create($prd, $img);
            if ($id !== FALSE) {
                $message = \i18n("Product saved. <a href=\"%s\">Go to the product page</a>.", PLUGIN_NAME, \Pasteque\get_module_url_action(PLUGIN_NAME, 'product_edit', array('id' => $id)));
            } else {
                $error = \i18n("Unable to save changes");
            }
        }
    }
}
$product = NULL;
$vatprice = "";
$price = "";
if (isset($_GET['id'])) {
    $product = \Pasteque\ProductsService::get($_GET['id']);
    $taxCat = \Pasteque\TaxesService::get($product->taxCatId);
    $tax = $taxCat->getCurrentTax();