Esempio n. 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);
}
Esempio n. 2
0
//
//    Pastèque is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with Pastèque.  If not, see <http://www.gnu.org/licenses/>.
namespace ProductTariffAreas;

$message = null;
$error = null;
$srv = new \Pasteque\TariffAreasService();
if (isset($_POST['id'])) {
    // Edit the area
    $area = \Pasteque\TariffArea::__build($_POST['id'], $_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->update($area)) {
        $message = \i18n("Changes saved");
    } else {
        $error = \i18n("Unable to save changes");
    }