From 45b4122a95bd91d7b197fc8dec6880db77ee72f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Garci=CC=81a=20de=20La=20Fuente?= Date: Sat, 7 Nov 2015 12:19:48 +0100 Subject: [PATCH] NEW #3767 Allow changing multiple prices of a product at once --- htdocs/product/class/product.class.php | 11 ++ htdocs/product/price.php | 235 ++++++++++++++----------- 2 files changed, 144 insertions(+), 102 deletions(-) diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php index ba4dc432d36..1ebd94b383f 100644 --- a/htdocs/product/class/product.class.php +++ b/htdocs/product/class/product.class.php @@ -4073,4 +4073,15 @@ class Product extends CommonObject return 1; } + + public function getRights() + { + global $user; + + if ($this->isproduct()) { + return $user->rights->produit; + } else { + return $user->rights->service; + } + } } diff --git a/htdocs/product/price.php b/htdocs/product/price.php index 69802f51d57..056455a3fff 100644 --- a/htdocs/product/price.php +++ b/htdocs/product/price.php @@ -113,86 +113,115 @@ if (empty($reshook)) $action=''; } - if (($action == 'update_price') && !$cancel && ($user->rights->produit->creer || $user->rights->service->creer)) + if (($action == 'update_price') && !$cancel && $object->getRights()->creer) { - $newprice = ''; - $newprice_min = ''; - $newpricebase = ''; - $newvat = ''; - + $error = 0; + $pricestoupdate = array(); + + $psq = GETPOST('psqflag'); + $psq = empty($newpsq) ? 0 : $newpsq; $maxpricesupplier = $object->min_recommended_price(); - $object->fk_price_expression = empty($eid) ? 0 : $eid; //0 discards expression - // MultiPrix - if (!empty($conf->global->PRODUIT_MULTIPRICES)) { - - //Shall we generate prices using price rules? - $object->price_autogen = GETPOST('usePriceRules') == 'on' ? true : false; - $object->update($object->id, $user); - - for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i ++) { - if (isset($_POST ["price_".$i])) { - $level = $i; - $newprice = price2num($_POST ["price_".$i], 'MU'); - $newprice_min = price2num($_POST ["price_min_".$i], 'MU'); - $newpricebase = $_POST ["multiprices_base_type_".$i]; - $newnpr = (preg_match('/\*/', $_POST ["tva_tx_".$i]) ? 1 : 0); - $newvat = str_replace('*', '', $_POST ["tva_tx_".$i]); - $newpsq = GETPOST('psqflag'); - $newpsq = empty($newpsq) ? 0 : $newpsq; - break; // We found submited price - } - } - } else { - $level = 0; - $newprice = price2num($_POST ["price"], 'MU'); - $newprice_min = price2num($_POST ["price_min"], 'MU'); - $newpricebase = $_POST ["price_base_type"]; - $newnpr = (preg_match('/\*/', $_POST ["tva_tx"]) ? 1 : 0); - $newvat = str_replace('*', '', $_POST ["tva_tx"]); - $newpsq = GETPOST('psqflag'); - $newpsq = empty($newpsq) ? 0 : $newpsq; - } - - if (!empty($conf->global->PRODUCT_MINIMUM_RECOMMENDED_PRICE) && $newprice_min < $maxpricesupplier) { - setEventMessage($langs->trans("MinimumPriceLimit", price($maxpricesupplier, 0, '', 1, - 1, - 1, 'auto')), 'errors'); - $error ++; - $action = 'edit_price'; - } - - if ($newprice < $newprice_min && !empty($object->fk_price_expression)) { - $newprice = $newprice_min; //Set price same as min, the user will not see the - } - - $res = $object->updatePrice($newprice, $newpricebase, $user, $newvat, $newprice_min, $level, $newnpr, $newpsq); - - if ($res) { + if (!empty($conf->dynamicprices->enabled)) { + $object->fk_price_expression = empty($eid) ? 0 : $eid; //0 discards expression if ($object->fk_price_expression != 0) { //Check the expression validity by parsing it $priceparser = new PriceParser($db); - $price_result = $priceparser->parseProduct($object); - if ($price_result < 0) { //Expression is not valid + + if ($priceparser->parseProduct($object) < 0) { $error ++; - $action = 'edit_price'; setEventMessage($priceparser->translatedError(), 'errors'); } } - if (empty($error) && !empty($conf->dynamicprices->enabled)) { - $ret = $object->setPriceExpression($object->fk_price_expression); - if ($ret < 0) { - $error ++; - $action = 'edit_price'; - setEventMessage($object->error, 'errors'); + } + + // Multiprices + if (!$error && !empty($conf->global->PRODUIT_MULTIPRICES)) { + + $newprice = GETPOST('price', 'array'); + $newprice_min = GETPOST('price_min', 'array'); + $newpricebase = GETPOST('multiprices_base_type', 'array'); + $newvattx = GETPOST('tva_tx', 'array'); + + //Shall we generate prices using price rules? + $object->price_autogen = GETPOST('usePriceRules') == 'on' ? true : false; + + for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i ++) { + + if (!isset($newprice[$i])) { + continue; + } + + $newnpr = (preg_match('/\*/', $newvattx[$i] ? 1 : 0)); + $newvat = str_replace('*', '', $newvattx[$i]); + + $pricestoupdate[$i] = array( + 'price' => $newprice[$i], + 'price_min' => $newprice_min[$i], + 'price_base_type' => $newpricebase[$i], + 'vat_tx' => $newvat, + 'npr' => $newnpr + ); + + //If autogeneration is enabled, then we only set the first level + if ($object->price_autogen) { + break; } } - if (empty($error)) { - $action = ''; - setEventMessage($langs->trans("RecordSaved")); + } else { + $pricestoupdate[0] = array( + 'price' => $_POST ["price"], + 'price_min' => $_POST ["price_min"], + 'price_base_type' => $_POST ["price_base_type"], + 'vat_tx' => str_replace('*', '', $_POST ["tva_tx"]), + 'npr' => preg_match('/\*/', $_POST ["tva_tx"]) ? 1 : 0 + ); + } + + if (!$error && $object->update($object->id, $user) < 1) { + $error++; + setEventMessage($object->error, 'errors'); + $action = 'edit_price'; + } + + if (!$error) { + $db->begin(); + + foreach ($pricestoupdate as $key => $val) { + + $newprice = $val['price']; + + if ($val['price'] < $val['price_min'] && !empty($object->fk_price_expression)) { + $newprice = $val['price_min']; //Set price same as min, the user will not see the + } + + $newprice = price2num($newprice, 'MU'); + $newprice_min = price2num($val['price_min'], 'MU'); + + if (!empty($conf->global->PRODUCT_MINIMUM_RECOMMENDED_PRICE) && $newprice_min < $maxpricesupplier) { + setEventMessage($langs->trans("MinimumPriceLimit", price($maxpricesupplier, 0, '', 1, - 1, - 1, 'auto')), 'errors'); + $error ++; + break; + } + + $res = $object->updatePrice($newprice, $val['price_base_type'], $user, $val['vat_tx'], $newprice_min, $key, $val['npr'], $psq); + + if ($res < 0) { + $error ++; + setEventMessage($object->error, 'errors'); + break; + } } + } + + if (empty($error)) { + $action = ''; + setEventMessage($langs->trans("RecordSaved")); + $db->commit(); } else { $action = 'edit_price'; - setEventMessage($object->error, 'errors'); + $db->rollback(); } } @@ -444,13 +473,13 @@ if (! empty($conf->global->PRODUIT_MULTIPRICES)) // Selling price print '' . $langs->trans("SellingPrice") . ''; print ''; - if ($object->multiprices_base_type["$soc->price_level"] == 'TTC') { - print price($object->multiprices_ttc["$soc->price_level"]); + if ($object->multiprices_base_type[$soc->price_level] == 'TTC') { + print price($object->multiprices_ttc[$soc->price_level]); } else { - print price($object->multiprices["$soc->price_level"]); + print price($object->multiprices[$soc->price_level]); } - if ($object->multiprices_base_type["$soc->price_level"]) { - print ' ' . $langs->trans($object->multiprices_base_type["$soc->price_level"]); + if ($object->multiprices_base_type[$soc->price_level]) { + print ' ' . $langs->trans($object->multiprices_base_type[$soc->price_level]); } else { print ' ' . $langs->trans($object->price_base_type); } @@ -458,18 +487,18 @@ if (! empty($conf->global->PRODUIT_MULTIPRICES)) // Price min print '' . $langs->trans("MinPrice") . ''; - if ($object->multiprices_base_type["$soc->price_level"] == 'TTC') + if ($object->multiprices_base_type[$soc->price_level] == 'TTC') { - print price($object->multiprices_min_ttc["$soc->price_level"]) . ' ' . $langs->trans($object->multiprices_base_type["$soc->price_level"]); + print price($object->multiprices_min_ttc[$soc->price_level]) . ' ' . $langs->trans($object->multiprices_base_type[$soc->price_level]); } else { - print price($object->multiprices_min["$soc->price_level"]) . ' ' . $langs->trans(empty($object->multiprices_base_type["$soc->price_level"])?'HT':$object->multiprices_base_type["$soc->price_level"]); + print price($object->multiprices_min[$soc->price_level]) . ' ' . $langs->trans(empty($object->multiprices_base_type[$soc->price_level])?'HT':$object->multiprices_base_type[$soc->price_level]); } print ''; if (! empty($conf->global->PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL)) // using this option is a bug. kept for backward compatibility { // TVA - print '' . $langs->trans("VATRate") . '' . vatrate($object->multiprices_tva_tx["$soc->price_level"], true) . ''; + print '' . $langs->trans("VATRate") . '' . vatrate($object->multiprices_tva_tx[$soc->price_level], true) . ''; } else { @@ -503,28 +532,28 @@ if (! empty($conf->global->PRODUIT_MULTIPRICES)) if (! empty($conf->global->$keyforlabel)) print ' - '.$langs->trans($conf->global->$keyforlabel); print ''; - if ($object->multiprices_base_type ["$i"] == 'TTC') { - print '' . price($object->multiprices_ttc["$i"]); + if ($object->multiprices_base_type [$i] == 'TTC') { + print '' . price($object->multiprices_ttc[$i]); } else { - print '' . price($object->multiprices["$i"]); + print '' . price($object->multiprices[$i]); } - if ($object->multiprices_base_type["$i"]) { - print ' '.$langs->trans($object->multiprices_base_type ["$i"]).'';; + if ($object->multiprices_base_type[$i]) { + print ' '.$langs->trans($object->multiprices_base_type [$i]).''; } else { - print ' '.$langs->trans($object->price_base_type).'';; + print ' '.$langs->trans($object->price_base_type).''; } // Prix min print ''; - if (empty($object->multiprices_base_type["$i"])) $object->multiprices_base_type["$i"]="HT"; - if ($object->multiprices_base_type["$i"] == 'TTC') + if (empty($object->multiprices_base_type[$i])) $object->multiprices_base_type[$i]="HT"; + if ($object->multiprices_base_type[$i] == 'TTC') { - print price($object->multiprices_min_ttc["$i"]) . ' ' . $langs->trans($object->multiprices_base_type["$i"]); + print price($object->multiprices_min_ttc[$i]) . ' ' . $langs->trans($object->multiprices_base_type[$i]); } else { - print price($object->multiprices_min["$i"]) . ' ' . $langs->trans($object->multiprices_base_type["$i"]); + print price($object->multiprices_min[$i]) . ' ' . $langs->trans($object->multiprices_base_type[$i]); } print ''; @@ -894,7 +923,7 @@ if ($action == 'edit_price' && ($user->rights->produit->creer || $user->rights-> id . '" method="POST">'; + for($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i ++) { - - print '
'; - if ($i > 1) print '
'; + + if ($i > 1) print '
'; elseif (! empty($conf->global->PRODUIT_MULTIPRICES) && ! empty($conf->global->PRODUIT_MULTIPRICES_ALLOW_AUTOCALC_PRICELEVEL)) { print $langs->trans('UseMultipriceRules'). ' price_autogen ? 'checked' : '').'>

'; } @@ -927,7 +957,7 @@ if ($action == 'edit_price' && ($user->rights->produit->creer || $user->rights-> print ''; if (empty($conf->global->PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL)) { - print ''; + print ''; } print ''; @@ -936,7 +966,7 @@ if ($action == 'edit_price' && ($user->rights->produit->creer || $user->rights-> if (! empty($conf->global->PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL)) // This option is kept for backward compatibility but has no sense { print ''; } @@ -945,22 +975,22 @@ if ($action == 'edit_price' && ($user->rights->produit->creer || $user->rights-> $text = $langs->trans('SellingPrice') . ' ' . $i; print $form->textwithpicto($text, $langs->trans("PrecisionUnitIsLimitedToXDecimals", $conf->global->MAIN_MAX_DECIMALS_UNIT), 1, 1); print ''; // Min price print ''; - print ''; - print '
' . $langs->trans("VATRate") . ''; - print $form->load_tva("tva_tx_" . $i, $object->multiprices_tva_tx["$i"], $mysoc, '', $object->id); + print $form->load_tva("tva_tx[" . $i.']', $object->multiprices_tva_tx[$i], $mysoc, '', $object->id); print '
'; - if ($object->multiprices_base_type ["$i"] == 'TTC') { - print ''; + if ($object->multiprices_base_type [$i] == 'TTC') { + print ''; } else { - print ''; + print ''; } - print $form->selectPriceBaseType($object->multiprices_base_type ["$i"], "multiprices_base_type_" . $i); + print ' '.$form->selectPriceBaseType($object->multiprices_base_type [$i], "multiprices_base_type[" . $i."]"); print '
'; $text = $langs->trans('MinPrice') . ' ' . $i; print $form->textwithpicto($text, $langs->trans("PrecisionUnitIsLimitedToXDecimals", $conf->global->MAIN_MAX_DECIMALS_UNIT), 1, 1); - if ($object->multiprices_base_type ["$i"] == 'TTC') { - print ''; + if ($object->multiprices_base_type [$i] == 'TTC') { + print ''; } else { - print ''; + print ''; } if ( !empty($conf->global->PRODUCT_MINIMUM_RECOMMENDED_PRICE)) { @@ -968,15 +998,16 @@ if ($action == 'edit_price' && ($user->rights->produit->creer || $user->rights-> } print '
'; - print '   '; - print '
'; - print '
'; } - + + print '
'; + print ''; + print '   '; + print '
'; + print ''; + dol_fiche_end(); } }