From 7035ec42f1e047507178e68448110ededfea8a98 Mon Sep 17 00:00:00 2001 From: Maxime Kohlhaas Date: Wed, 7 Nov 2012 00:38:53 +0100 Subject: [PATCH] Task # 559 : Modifications to store unitprice as in supplier prices --- htdocs/core/class/html.form.class.php | 37 +++--- .../tables/llx_product_price_by_qty.key.sql | 2 +- .../mysql/tables/llx_product_price_by_qty.sql | 12 +- htdocs/product/ajax/products.php | 8 +- htdocs/product/class/product.class.php | 16 +-- htdocs/product/price.php | 108 +++++++----------- 6 files changed, 81 insertions(+), 102 deletions(-) diff --git a/htdocs/core/class/html.form.class.php b/htdocs/core/class/html.form.class.php index 4c78faa6690..dad4173ec60 100644 --- a/htdocs/core/class/html.form.class.php +++ b/htdocs/core/class/html.form.class.php @@ -1226,9 +1226,10 @@ class Form $objp = $this->db->fetch_object($result); if(!empty($objp->price_by_qty) && $objp->price_by_qty == 1) { // Price by quantity will return many prices for the same product - $sql = "SELECT rowid, qty_min, price, price_ttc, remise_percent, remise"; + $sql = "SELECT rowid, quantity, price, unitprice, remise_percent, remise"; $sql.= " FROM ".MAIN_DB_PREFIX."product_price_by_qty"; $sql.= " WHERE fk_product_price=".$objp->price_rowid; + $sql.= " ORDER BY quantity ASC"; dol_syslog(get_class($this)."::select_produits_do search price by qty sql=".$sql); $result2 = $this->db->query($sql); @@ -1239,9 +1240,9 @@ class Form while ($nb_prices && $j < $nb_prices) { $objp2 = $this->db->fetch_object($result2); - $objp->quantity = $objp2->qty_min; + $objp->quantity = $objp2->quantity; $objp->price = $objp2->price; - $objp->unitprice = $objp2->price; + $objp->unitprice = $objp2->unitprice; $objp->remise_percent = $objp2->remise_percent; $objp->remise = $objp2->remise; $objp->price_by_qty_rowid = $objp2->rowid; @@ -1313,14 +1314,8 @@ class Form $opt.= (!empty($objp->price_by_qty_rowid) && $objp->price_by_qty_rowid > 0)?' pbq="'.$objp->price_by_qty_rowid.'"':''; if (! empty($conf->stock->enabled) && $objp->fk_product_type == 0 && isset($objp->stock)) { - if ($objp->stock > 0) - { - $opt.= ' style="background-color:#32CD32; color:#F5F5F5;"'; - } - else if ($objp->stock <= 0) - { - $opt.= ' style="background-color:#FF0000; color:#F5F5F5;"'; - } + if ($objp->stock > 0) $opt.= ' class="product_line_stock_ok"'; + else if ($objp->stock <= 0) $opt.= ' class="product_line_stock_too_low"'; } $opt.= '>'; $opt.= $objp->ref.' - '.dol_trunc($label,32).' - '; @@ -1382,21 +1377,21 @@ class Form $outdiscount=$objp->remise_percent; if ($objp->quantity == 1) { - $opt.= price($objp->price).' '.$currencytext."/"; - $outval.= price($objp->price).' '.$currencytextnoent."/"; + $opt.= price($objp->unitprice).' '.$currencytext."/"; + $outval.= price($objp->unitprice).' '.$currencytextnoent."/"; $opt.= $langs->trans("Unit"); // Do not use strtolower because it breaks utf8 encoding $outval.=$langs->transnoentities("Unit"); } else { - $opt.= price($objp->price * $objp->quantity).' '.$currencytext."/".$objp->quantity; - $outval.= price($objp->price * $objp->quantity).' '.$currencytextnoent."/".$objp->quantity; + $opt.= price($objp->price).' '.$currencytext."/".$objp->quantity; + $outval.= price($objp->price).' '.$currencytextnoent."/".$objp->quantity; $opt.= $langs->trans("Units"); // Do not use strtolower because it breaks utf8 encoding $outval.=$langs->transnoentities("Units"); } - $outprice_ht=price($objp->price); - $outprice_ttc=price($objp->price_ttc); + $outprice_ht=price($objp->unitprice); + $outprice_ttc=price($objp->unitprice * (1 + ($objp->tva_tx / 100))); $outpricebasetype=$objp->price_base_type; $outtva_tx=$objp->tva_tx; } @@ -1573,8 +1568,12 @@ class Form $label = $objp->label; if ($filterkey && $filterkey != '') $label=preg_replace('/('.preg_quote($filterkey).')/i','$1',$label,1); - $opt.=$objp->ref.' ('.$objp->ref_fourn.') - '; - $outval.=$objRef.' ('.$objRefFourn.') - '; + $opt.=$objp->ref; + if (! empty($objp->idprodfournprice)) $opt.=' ('.$objp->ref_fourn.')'; + $opt.=' - '; + $outval.=$objRef; + if (! empty($objp->idprodfournprice)) $outval.=' ('.$objRefFourn.')'; + $outval.=' - '; $opt.=dol_trunc($objp->label,18).' - '; $outval.=dol_trunc($label,18).' - '; diff --git a/htdocs/install/mysql/tables/llx_product_price_by_qty.key.sql b/htdocs/install/mysql/tables/llx_product_price_by_qty.key.sql index e96af56e9c0..e6eec68f632 100644 --- a/htdocs/install/mysql/tables/llx_product_price_by_qty.key.sql +++ b/htdocs/install/mysql/tables/llx_product_price_by_qty.key.sql @@ -19,7 +19,7 @@ -- -- ============================================================================ -ALTER TABLE llx_product_price_by_qty ADD UNIQUE INDEX uk_product_price_by_qty_level (fk_product_price, qty_min); +ALTER TABLE llx_product_price_by_qty ADD UNIQUE INDEX uk_product_price_by_qty_level (fk_product_price, quantity); ALTER TABLE llx_product_price_by_qty ADD INDEX idx_product_price_by_qty_fk_product_price (fk_product_price); diff --git a/htdocs/install/mysql/tables/llx_product_price_by_qty.sql b/htdocs/install/mysql/tables/llx_product_price_by_qty.sql index e0585786235..556753c309d 100644 --- a/htdocs/install/mysql/tables/llx_product_price_by_qty.sql +++ b/htdocs/install/mysql/tables/llx_product_price_by_qty.sql @@ -21,12 +21,12 @@ create table llx_product_price_by_qty ( - rowid integer AUTO_INCREMENT PRIMARY KEY, + rowid integer NOT NULL AUTO_INCREMENT PRIMARY KEY, fk_product_price integer NOT NULL, - date_price timestamp, - price double (24,8) DEFAULT 0, - price_ttc double (24,8) DEFAULT 0, + date_price timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + price double(24,8) DEFAULT 0, + quantity double DEFAULT NULL, remise_percent double NOT NULL DEFAULT 0, - remise double NOT NULL DEFAULT 0, - qty_min real DEFAULT 1 + remise double NOT NULL DEFAULT 0, + unitprice double(24,8) DEFAULT 0 )ENGINE=innodb; diff --git a/htdocs/product/ajax/products.php b/htdocs/product/ajax/products.php index f5702b72f8c..9e20392fc34 100644 --- a/htdocs/product/ajax/products.php +++ b/htdocs/product/ajax/products.php @@ -74,7 +74,7 @@ if (! empty($action) && $action == 'fetch' && ! empty($id)) // Price by qty if (!empty($price_by_qty_rowid) && $price_by_qty_rowid >= 1) // If we need a particular price related to qty { - $sql = "SELECT price, price_ttc, qty_min, remise_percent"; + $sql = "SELECT price, unitprice, quantity, remise_percent"; $sql.= " FROM ".MAIN_DB_PREFIX."product_price_by_qty "; $sql.= " WHERE rowid=".$price_by_qty_rowid.""; @@ -85,11 +85,11 @@ if (! empty($action) && $action == 'fetch' && ! empty($id)) if ($objp) { $found=true; - $outprice_ht=price($objp->price); - $outprice_ttc=price($objp->price_ttc); + $outprice_ht=price($objp->unitprice); + $outprice_ttc=price($objp->unitprice * (1 + ($object->tva_tx / 100))); $outpricebasetype=$object->price_base_type; $outtva_tx=$object->tva_tx; - $outqty=$objp->qty_min; + $outqty=$objp->quantity; $outdiscount=$objp->remise_percent; } } diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php index ecedea4a10d..bf853d16e75 100644 --- a/htdocs/product/class/product.class.php +++ b/htdocs/product/class/product.class.php @@ -1203,10 +1203,10 @@ class Product extends CommonObject // Récuperation de la liste des prix selon qty si flag positionné if ($this->prices_by_qty[$i] == 1) { - $sql = "SELECT rowid,price, price_ttc, qty_min, remise_percent, remise"; + $sql = "SELECT rowid,price, unitprice, quantity, remise_percent, remise"; $sql.= " FROM ".MAIN_DB_PREFIX."product_price_by_qty"; $sql.= " WHERE fk_product_price = '".$this->prices_by_qty_id[$i]."'"; - $sql.= " ORDER BY qty_min ASC"; + $sql.= " ORDER BY quantity ASC"; $resultat=array(); $resql = $this->db->query($sql) ; if ($resql) @@ -1216,8 +1216,8 @@ class Product extends CommonObject $resultat[$ii]=array(); $resultat[$ii]["rowid"]=$result["rowid"]; $resultat[$ii]["price"]= $result["price"]; - $resultat[$ii]["price_ttc"]= $result["price_ttc"]; - $resultat[$ii]["qty_min"]= $result["qty_min"]; + $resultat[$ii]["unitprice"]= $result["unitprice"]; + $resultat[$ii]["quantity"]= $result["quantity"]; $resultat[$ii]["remise_percent"]= $result["remise_percent"]; $resultat[$ii]["remise"]= $result["remise"]; $ii++; @@ -1256,10 +1256,10 @@ class Product extends CommonObject // Récuperation de la liste des prix selon qty si flag positionné if ($this->prices_by_qty[0] == 1) { - $sql = "SELECT rowid,price, price_ttc, qty_min, remise_percent, remise"; + $sql = "SELECT rowid,price, unitprice, quantity, remise_percent, remise"; $sql.= " FROM ".MAIN_DB_PREFIX."product_price_by_qty"; $sql.= " WHERE fk_product_price = '".$this->prices_by_qty_id[0]."'"; - $sql.= " ORDER BY qty_min ASC"; + $sql.= " ORDER BY quantity ASC"; $resultat=array(); $resql = $this->db->query($sql) ; if ($resql) @@ -1269,8 +1269,8 @@ class Product extends CommonObject $resultat[$ii]=array(); $resultat[$ii]["rowid"]=$result["rowid"]; $resultat[$ii]["price"]= $result["price"]; - $resultat[$ii]["price_ttc"]= $result["price_ttc"]; - $resultat[$ii]["qty_min"]= $result["qty_min"]; + $resultat[$ii]["unitprice"]= $result["unitprice"]; + $resultat[$ii]["quantity"]= $result["quantity"]; $resultat[$ii]["remise_percent"]= $result["remise_percent"]; $resultat[$ii]["remise"]= $result["remise"]; $ii++; diff --git a/htdocs/product/price.php b/htdocs/product/price.php index 7fe5aa5b89e..14d6600779d 100644 --- a/htdocs/product/price.php +++ b/htdocs/product/price.php @@ -108,6 +108,7 @@ else if ($action == 'delete' && $user->rights->produit->supprimer) /***************************************************** * Price by quantity *****************************************************/ +$error=0; if ($action == 'activate_price_by_qty') { // Activating product price by quantity add a new price, specified as by quantity $result = $object->fetch($id); $level=GETPOST('level'); @@ -127,68 +128,47 @@ if ($action == 'update_price_by_qty') { // Ajout / Mise à jour d'un prix par qu $priceid=GETPOST('priceid'); $newprice=price2num(GETPOST("price"),'MU'); //$newminprice=price2num(GETPOST("price_min"),'MU'); // TODO : Add min price management - $qtymin=GETPOST('qty_min'); + $quantity=GETPOST('quantity'); $remise_percent=price2num(GETPOST('remise_percent')); - $remise=0; // TODO : allow dicsoun by amount when available on documents + $remise=0; // TODO : allow dicsount by amount when available on documents - // Calcul des prix (HT et TTC) - if ($newprice!='' || $newprice==0) + if (empty($quantity)) { + $error++; + $mesg='
'.$langs->trans("ErrorFieldRequired",$langs->transnoentities("Qty")).'
'; + } + if (empty($newprice)) + { + $error++; + $mesg='
'.$langs->trans("ErrorFieldRequired",$langs->transnoentities("Price")).'
'; + } + if(!$error) { + // Calcul du prix HT et du prix unitaire if ($object->price_base_type == 'TTC') { - $price_ttc = price2num($newprice,'MU'); $price = price2num($newprice) / (1 + ($object->tva_tx / 100)); - $price = price2num($price,'MU'); - - /*if ($newminprice!='' || $newminprice==0) - { - $price_min_ttc = price2num($newminprice,'MU'); - $price_min = price2num($newminprice) / (1 + ($object->tva_tx / 100)); - $price_min = price2num($price_min,'MU'); - } - else - { - $price_min=0; - $price_min_ttc=0; - }*/ } - else - { - $price = price2num($newprice,'MU'); - $price_ttc = price2num($newprice) * (1 + ($object->tva_tx / 100)); - $price_ttc = price2num($price_ttc,'MU'); - - /*if ($newminprice!='' || $newminprice==0) - { - $price_min = price2num($newminprice,'MU'); - $price_min_ttc = price2num($newminprice) * (1 + ($object->tva_tx / 100)); - $price_min_ttc = price2num($price_min_ttc,'MU'); - //print 'X'.$newminprice.'-'.$price_min; - } - else - { - $price_min=0; - $price_min_ttc=0; - }*/ - } - } - - // Ajout / mise à jour - if($rowid > 0) { - $sql = "UPDATE ".MAIN_DB_PREFIX."product_price_by_qty SET"; - $sql.= " price='".$price."',"; - $sql.= " price_ttc=".$price_ttc.","; - $sql.= " qty_min=".$qtymin.","; - $sql.= " remise_percent=".$remise_percent.","; - $sql.= " remise=".$remise; - $sql.= " WHERE rowid = ".GETPOST('rowid'); - $result = $db->query($sql); - } else { - $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_price_by_qty (fk_product_price,price,price_ttc,qty_min,remise_percent,remise) values ("; - $sql.= $priceid.','.$price.','.$price_ttc.','.$qtymin.','.$remise_percent.','.$remise.')'; - - $result = $db->query($sql); + $price = price2num($newprice,'MU'); + $unitPrice = price2num($price/$quantity,'MU'); + + // Ajout / mise à jour + if($rowid > 0) { + $sql = "UPDATE ".MAIN_DB_PREFIX."product_price_by_qty SET"; + $sql.= " price='".$price."',"; + $sql.= " unitprice=".$unitPrice.","; + $sql.= " quantity=".$quantity.","; + $sql.= " remise_percent=".$remise_percent.","; + $sql.= " remise=".$remise; + $sql.= " WHERE rowid = ".GETPOST('rowid'); + + $result = $db->query($sql); + } else { + $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_price_by_qty (fk_product_price,price,unitprice,quantity,remise_percent,remise) values ("; + $sql.= $priceid.','.$price.','.$unitPrice.','.$quantity.','.$remise_percent.','.$remise.')'; + + $result = $db->query($sql); + } } } @@ -352,7 +332,7 @@ if (! empty($conf->global->PRODUIT_MULTIPRICES)) print ''; print ''.$langs->trans("PriceByQuantityRange").' '.$i.''; print ''.$langs->trans("HT").''; - print ''.$langs->trans("TTC").''; + print ''.$langs->trans("UnitPrice").''; print ''.$langs->trans("Discount").''; print ' '; print ''; @@ -363,7 +343,7 @@ if (! empty($conf->global->PRODUIT_MULTIPRICES)) print ''; print ''; print ''; - print ''; + print ''; print ' '.$object->price_base_type.''; //print ' '; print ' %'; @@ -372,9 +352,9 @@ if (! empty($conf->global->PRODUIT_MULTIPRICES)) print ''; } else { print ''; - print ''.$prices['qty_min'].''; + print ''.$prices['quantity'].''; print ''.price($prices['price']).''; - print ''.price($prices['price_ttc']).''; + print ''.price($prices['unitprice']).''; print ''.price($prices['remise_percent']).' %'; print ''; if(($user->rights->produit->creer || $user->rights->service->creer)) { @@ -395,7 +375,7 @@ if (! empty($conf->global->PRODUIT_MULTIPRICES)) print ''; print ''; print ''; - print ''; + print ''; print ' '.$object->price_base_type.''; //print ' '; print ' %'; @@ -456,7 +436,7 @@ else print ''; print ''.$langs->trans("PriceByQuantityRange").''; print ''.$langs->trans("HT").''; - print ''.$langs->trans("TTC").''; + print ''.$langs->trans("UnitPrice").''; print ''.$langs->trans("Discount").''; print ' '; print ''; @@ -467,7 +447,7 @@ else print ''; print ''; print ''; - print ''; + print ''; print ' '.$object->price_base_type.''; //print ' '; print ' %'; @@ -476,9 +456,9 @@ else print ''; } else { print ''; - print ''.$prices['qty_min'].''; + print ''.$prices['quantity'].''; print ''.price($prices['price']).''; - print ''.price($prices['price_ttc']).''; + print ''.price($prices['unitprice']).''; print ''.price($prices['remise_percent']).' %'; print ''; if(($user->rights->produit->creer || $user->rights->service->creer)) { @@ -499,7 +479,7 @@ else print ''; print ''; print ''; - print ''; + print ''; print ' '.$object->price_base_type.''; //print ' '; print ' %';