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 '