示例#1
0
/** 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);
}
示例#2
0
        $error = \i18n("Unable to save changes");
    }
} else {
    if (isset($_POST['label'])) {
        $area = new \Pasteque\TariffArea($_POST['label'], $_POST['dispOrder']);
        foreach ($_POST as $key => $value) {
            if (strpos($key, "price-") === 0) {
                $productId = substr($key, 6);
                $product = \Pasteque\ProductsService::get($productId);
                $taxCat = \Pasteque\TaxesService::get($product->taxCatId);
                $tax = $taxCat->getCurrentTax();
                $price = $value / (1 + $tax->rate);
                $area->addPrice($productId, $price);
            }
        }
        if ($srv->create($area)) {
            $message = \i18n("Changes saved");
        } else {
            $error = \i18n("Unable to save changes");
        }
    }
}
$area = null;
if (isset($_GET['id'])) {
    $area = $srv->get($_GET['id']);
}
$categories = \Pasteque\CategoriesService::getAll();
$products = \Pasteque\ProductsService::getAll(true);
?>
<h1><?php 
\pi18n("Tariff area", PLUGIN_NAME);