* Copyright (C) 2024 Frédéric France * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program 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 this program. If not, see . * * Page to set how to autocalculate price for each level when option PRODUCT_MULTIPRICE is on. * This page is a tab in the setup of module Product if option PRODUIT_MULTIPRICES_ALLOW_AUTOCALC_PRICELEVEL is set. */ // Load Dolibarr environment require '../../main.inc.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php'; /** * @var Conf $conf * @var DoliDB $db * @var HookManager $hookmanager * @var Translate $langs * @var User $user */ // Load translation files required by the page $langs->loadLangs(array('admin', 'products')); $action = GETPOST('action', 'aZ09'); // Security check if (!$user->admin || (!isModEnabled("product") && !isModEnabled("service"))) { accessforbidden(); } $error = 0; /** * Actions */ if ($action == 'update') { $var_percent = GETPOST('var_percent', 'array'); $var_min_percent = GETPOST('var_min_percent', 'array'); $fk_level = GETPOST('fk_level', 'array'); $produit_multiprices_limit = getDolGlobalInt('PRODUIT_MULTIPRICES_LIMIT'); for ($i = 1; $i <= $produit_multiprices_limit; $i++) { $check = isset($var_min_percent[$i]); if ($i != 1) { $check = $check && isset($var_percent[$i]) && isset($fk_level[$i]); } if (!$check) { continue; } $i_var_percent = 0; // Set $i_var_percent, the percent of price for level compared to an other level if ($i != 1) { $i_var_percent = (float) price2num($var_percent[$i]); } $i_var_min_percent = (float) price2num($var_min_percent[$i]); $i_fk_level = (int) $fk_level[$i]; if ($i == 1) { $check1 = true; $check2 = $i_var_min_percent; } else { $check1 = $i_fk_level >= 1 && $i_fk_level <= $produit_multiprices_limit; $check2 = $i_var_percent && ($i_var_min_percent || (string) $i_var_min_percent === '0'); } if (empty($i_var_percent) && empty($i_var_min_percent)) { //If the level is between range but percent fields are empty, then we ensure it does not exist in DB $db->query("DELETE FROM ".MAIN_DB_PREFIX."product_pricerules WHERE level = ".((int) $i)); continue; } $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_pricerules (level, fk_level, var_percent, var_min_percent) VALUES ("; $sql .= ((int) $i).", ".$db->escape($i_fk_level).", ".$i_var_percent.", ".$i_var_min_percent.")"; if (!$db->query($sql)) { //If we could not create, then we try updating $sql = "UPDATE ".MAIN_DB_PREFIX."product_pricerules"; $sql .= " SET fk_level = ".$db->escape($i_fk_level).", var_percent = ".$i_var_percent.", var_min_percent = ".$i_var_min_percent." WHERE level = ".((int) $i); if (!$db->query($sql)) { setEventMessages($langs->trans('ErrorSavingChanges'), null, 'errors'); $error++; } } } if (!$error) { setEventMessages($langs->trans("RecordSaved"), null, 'mesgs'); } } /* * View */ $sql = "SELECT rowid, level, fk_level, var_percent, var_min_percent"; $sql .= " FROM ".MAIN_DB_PREFIX."product_pricerules"; $query = $db->query($sql); $rules = array(); while ($result = $db->fetch_object($query)) { $rules[$result->level] = $result; } $title = $langs->trans('ProductServiceSetup'); $tab = $langs->trans("ProductsAndServices"); if (!isModEnabled("product")) { $title = $langs->trans('ServiceSetup'); $tab = $langs->trans('Services'); } elseif (!isModEnabled("service")) { $title = $langs->trans('ProductSetup'); $tab = $langs->trans('Products'); } llxHeader('', $langs->trans('MultipriceRules'), '', '', 0, 0, '', '', '', 'mod-product page-admin_price_rules'); $linkback = ''.$langs->trans("BackToModuleList").''; print load_fiche_titre($title, $linkback, 'title_setup'); print '
'; print ''; print ''; $head = product_admin_prepare_head(); print dol_get_fiche_head($head, 'generator', $tab, -1, 'product'); print ''.$langs->trans("MultiPriceRuleDesc").'

'; // Array that contains the number of prices available $price_options = array(); $produit_multiprices_limit = getDolGlobalInt('PRODUIT_MULTIPRICES_LIMIT'); for ($i = 1; $i <= $produit_multiprices_limit; $i++) { $price_options[$i] = $langs->trans('SellingPrice').' '.$i; } ?>
trans('PriceLevel') ?> trans('Price') ?> trans('MinPrice') ?>
trans('SellingPrice') ?> 1 trans('PercentDiscountOver', $langs->trans('SellingPrice').' 1') ?>
trans('SellingPrice').' '.$i; // Label of price $keyforlabel = 'PRODUIT_MULTIPRICES_LABEL'.$i; if (getDolGlobalString($keyforlabel)) { print ' - '.$langs->trans(getDolGlobalString($keyforlabel)); } ?> trans('PercentVariationOver', '{s1}'); $texttoshow = str_replace('{s1}', Form::selectarray("fk_level[$i]", $return, (isset($rules[$i]) ? $rules[$i]->fk_level : null)), $texttoshow); print $texttoshow; ?> trans('PercentDiscountOver', $langs->transnoentitiesnoconv('SellingPrice').' '.$i) ?>
'; print '
'; // End of page llxFooter(); $db->close();