2
0
forked from Wavyzz/dolibarr

Task # 559 : start of price by quantity management

This commit is contained in:
Maxime Kohlhaas
2012-10-22 17:47:15 +02:00
parent 94c12cfb2e
commit 8ba55f49f1
2 changed files with 392 additions and 9 deletions

View File

@@ -61,6 +61,11 @@ class Product extends CommonObject
var $multiprices_ttc=array();
var $multiprices_base_type=array();
var $multiprices_tva_tx=array();
//! Price by quantity arrays
var $price_by_qty;
var $prices_by_qty=array();
var $prices_by_qty_id=array();
var $prices_by_qty_list=array();
//! Default VAT rate of product
var $tva_tx;
//! French VAT NPR (0 or 1)
@@ -214,6 +219,10 @@ class Product extends CommonObject
if (empty($this->price)) $this->price = 0;
if (empty($this->price_min)) $this->price_min = 0;
// Price by quantity
if (empty($this->price_by_qty)) $this->price_by_qty = 0;
if (empty($this->status)) $this->status = 0;
if (empty($this->status_buy)) $this->status_buy = 0;
if (empty($this->finished)) $this->finished = 0;
@@ -802,9 +811,9 @@ class Product extends CommonObject
// Add new price
$sql = "INSERT INTO ".MAIN_DB_PREFIX."product_price(price_level,date_price,fk_product,fk_user_author,price,price_ttc,price_base_type,tosell,tva_tx,recuperableonly,";
$sql.= " localtax1_tx, localtax2_tx, price_min,price_min_ttc) ";
$sql.= " localtax1_tx, localtax2_tx, price_min,price_min_ttc,price_by_qty) ";
$sql.= " VALUES(".($level?$level:1).", '".$this->db->idate($now)."',".$this->id.",".$user->id.",".$this->price.",".$this->price_ttc.",'".$this->price_base_type."',".$this->status.",".$this->tva_tx.",".$this->tva_npr.",";
$sql.= " ".$this->localtax1_tx.",".$this->localtax2_tx.",".$this->price_min.",".$this->price_min_ttc;
$sql.= " ".$this->localtax1_tx.",".$this->localtax2_tx.",".$this->price_min.",".$this->price_min_ttc.",".$this->price_by_qty;
$sql.= ")";
dol_syslog(get_class($this)."_log_price sql=".$sql);
@@ -941,9 +950,10 @@ class Product extends CommonObject
* @param double $newminprice New price min
* @param int $level 0=standard, >0 = level if multilevel prices
* @param int $newnpr 0=Standard vat rate, 1=Special vat rate for French NPR VAT
* @param int $newpsq 1 if it has price by quantity
* @return int <0 if KO, >0 if OK
*/
function updatePrice($id, $newprice, $newpricebase, $user, $newvat='',$newminprice='', $level=0, $newnpr=0)
function updatePrice($id, $newprice, $newpricebase, $user, $newvat='',$newminprice='', $level=0, $newnpr=0, $newpsq=0)
{
global $conf,$langs;
@@ -1032,6 +1042,9 @@ class Product extends CommonObject
$this->localtax1_tx = $localtax1;
$this->localtax2_tx = $localtax2;
// Price by quantity
$this->price_by_qty = $newpsq;
$this->_log_price($user,$level);
// Appel des triggers
@@ -1166,7 +1179,7 @@ class Product extends CommonObject
for ($i=1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++)
{
$sql = "SELECT price, price_ttc, price_min, price_min_ttc,";
$sql.= " price_base_type, tva_tx, tosell";
$sql.= " price_base_type, tva_tx, tosell, price_by_qty, rowid";
$sql.= " FROM ".MAIN_DB_PREFIX."product_price";
$sql.= " WHERE price_level=".$i;
$sql.= " AND fk_product = '".$this->id."'";
@@ -1183,6 +1196,39 @@ class Product extends CommonObject
$this->multiprices_min_ttc[$i]=$result["price_min_ttc"];
$this->multiprices_base_type[$i]=$result["price_base_type"];
$this->multiprices_tva_tx[$i]=$result["tva_tx"];
// Price by quantity
$this->prices_by_qty[$i]=$result["price_by_qty"];
$this->prices_by_qty_id[$i]=$result["rowid"];
// 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,qty_max";
$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";
$resultat=array();
$resql = $this->db->query($sql) ;
if ($resql)
{
$ii=0;
while ($result= $this->db->fetch_array($resql)) {
$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]["qty_max"]= $result["qty_max"];
$ii++;
}
$this->prices_by_qty_list[$i]=$resultat;
}
else
{
dol_print_error($this->db);
return -1;
}
}
}
else
{
@@ -1190,6 +1236,57 @@ class Product extends CommonObject
return -1;
}
}
} else if (! empty($conf->global->PRODUIT_PRICE_BY_QTY))
{
$sql = "SELECT price, price_ttc, price_min, price_min_ttc,";
$sql.= " price_base_type, tva_tx, tosell, price_by_qty, rowid";
$sql.= " FROM ".MAIN_DB_PREFIX."product_price";
$sql.= " WHERE fk_product = '".$this->id."'";
$sql.= " ORDER BY date_price DESC";
$sql.= " LIMIT 1";
$resql = $this->db->query($sql);
if ($resql)
{
$result = $this->db->fetch_array($resql);
// Price by quantity
$this->prices_by_qty[0]=$result["price_by_qty"];
$this->prices_by_qty_id[0]=$result["rowid"];
// 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,qty_max";
$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";
$resultat=array();
$resql = $this->db->query($sql) ;
if ($resql)
{
$ii=0;
while ($result= $this->db->fetch_array($resql)) {
$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]["qty_max"]= $result["qty_max"];
$ii++;
}
$this->prices_by_qty_list[0]=$resultat;
}
else
{
dol_print_error($this->db);
return -1;
}
}
}
else
{
dol_print_error($this->db);
return -1;
}
}
$res=$this->load_stock();
@@ -2922,4 +3019,4 @@ class Product extends CommonObject
$this->note='This is a comment (private)';
}
}
?>
?>

View File

@@ -51,7 +51,7 @@ $object = new Product($db);
if ($action == 'update_price' && ! $_POST["cancel"] && ($user->rights->produit->creer || $user->rights->service->creer))
{
$result = $object->fetch($id);
// MultiPrix
if (! empty($conf->global->PRODUIT_MULTIPRICES))
{
@@ -70,6 +70,8 @@ if ($action == 'update_price' && ! $_POST["cancel"] && ($user->rights->produit->
$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
}
}
@@ -82,9 +84,11 @@ if ($action == 'update_price' && ! $_POST["cancel"] && ($user->rights->produit->
$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 ($object->updatePrice($object->id, $newprice, $newpricebase, $user, $newvat, $newprice_min, $level, $newnpr) > 0)
if ($object->updatePrice($object->id, $newprice, $newpricebase, $user, $newvat, $newprice_min, $level, $newnpr, $newpsq) > 0)
{
$action = '';
$mesg = '<div class="ok">'.$langs->trans("RecordSaved").'</div>';
@@ -101,6 +105,110 @@ else if ($action == 'delete' && $user->rights->produit->supprimer)
if ($result < 0) $mesg='<div class="error">'.$object->error.'</div>';
}
/*****************************************************
* Price by quantity
*****************************************************/
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');
$object->updatePrice($object->id, 0, $object->price_base_type, $user, $object->tva_tx, 0, $level, $object->tva_npr, 1);
}
if ($action == 'edit_price_by_qty') { // Edition d'un prix par quantité
$rowid = GETPOST('rowid');
}
if ($action == 'update_price_by_qty') { // Ajout / Mise à jour d'un prix par quantité
$result = $object->fetch($id);
// Récupération des variables
$rowid = GETPOST('rowid');
$priceid=GETPOST('priceid');
$newprice=price2num(GETPOST("price"),'MU');
//$newminprice=price2num(GETPOST("price_min"),'MU'); // TODO : Add min price management and discount management
$qtymin=GETPOST('qty_min');
$qtymax=GETPOST('qty_max');
// Calcul des prix (HT et TTC)
if ($newprice!='' || $newprice==0)
{
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.= " qty_max=".$qtymax;
$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,qty_max) values (";
$sql.= $priceid.','.$price.','.$price_ttc.','.$qtymin.','.$qtymax.')';
$result = $db->query($sql);
}
}
if ($action == 'delete_price_by_qty') {
$rowid = GETPOST('rowid');
$sql = "DELETE FROM ".MAIN_DB_PREFIX."product_price_by_qty";
$sql.= " WHERE rowid = ".GETPOST('rowid');
$result = $db->query($sql);
}
if ($action == 'delete_all_price_by_qty') {
$priceid=GETPOST('priceid');
$sql = "DELETE FROM ".MAIN_DB_PREFIX."product_price_by_qty";
$sql.= " WHERE fk_product_price = ".$priceid;
$result = $db->query($sql);
}
/*
* View
@@ -230,6 +338,104 @@ if (! empty($conf->global->PRODUIT_MULTIPRICES))
print price($object->multiprices_min["$i"]).' '.$langs->trans($object->multiprices_base_type["$i"]);
}
print '</td></tr>';
// Price by quantity
/*if($conf->global->PRODUIT_PRICE_BY_QTY) {
print '<tr><td>'.$langs->trans("PriceByQuantity").' '.$i.'</td><td>';
if($object->prices_by_qty[$i] == 1) {
print '<table width="50%" class="noborder">';
print '<tr class="liste_titre">';
print '<td>'.$langs->trans("PriceByQuantityRange").'</td>';
print '<td align="right">'.$langs->trans("HT").'</td>';
print '<td align="right">'.$langs->trans("TTC").'</td>';
print '<td>&nbsp;</td>';
print '</tr>';
foreach ($object->prices_by_qty_list[$i] as $j=> $prices) {
print '<tr class="'.($j % 2 == 0 ? 'pair':'impair').'">';
print '<td>'.$prices['qty_min'].' - '.$prices['qty_max'].'</td>';
print '<td align="right">'.price($prices['price']).'</td>';
print '<td align="right">'.price($prices['price_ttc']).'</td>';
print '<td align="center"><a href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=delete_price_by_qty&amp;rowid='.$prices["rowid"].'">';
print img_delete();
print '</a></td>';
print '</tr>';
}
print '</table>';
} else {
print $langs->trans("No");
}
print '</td></tr>';
}*/
// Price by quantity
if($conf->global->PRODUIT_PRICE_BY_QTY) {
print '<tr><td>'.$langs->trans("PriceByQuantity").' '.$i;
print '</td><td>';
if($object->prices_by_qty[$i] == 1) {
print '<table width="50%" class="noborder">';
print '<tr class="liste_titre">';
print '<td>'.$langs->trans("PriceByQuantityRange").' '.$i.'</td>';
print '<td align="right">'.$langs->trans("HT").'</td>';
print '<td align="right">'.$langs->trans("TTC").'</td>';
print '<td>&nbsp;</td>';
print '</tr>';
foreach ($object->prices_by_qty_list[$i] as $ii=> $prices) {
if($action == 'edit_price_by_qty' && $rowid == $prices['rowid'] && ($user->rights->produit->creer || $user->rights->service->creer)) {
print '<form action="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'" method="POST">';
print '<input type="hidden" name="action" value="update_price_by_qty">';
print '<input type="hidden" name="priceid" value="'.$object->prices_by_qty_id[$i].'">';
print '<input type="hidden" value="'.$prices['rowid'].'" name="rowid">';
print '<tr class="'.($ii % 2 == 0 ? 'pair':'impair').'">';
print '<td><input size="5" type="text" value="'.$prices['qty_min'].'" name="qty_min"> - ';
print '<input size="5" type="text" value="'.$prices['qty_max'].'" name="qty_max"></td>';
print '<td align="right"><input type="text" value="'.$prices['price'].'" name="price">&nbsp;'.$object->price_base_type.'</td>';
print '<td align="right">&nbsp;</td>';
print '<td align="center"><input type="submit" value="'.$langs->trans("Modify").'" class="button"></td>';
print '</tr>';
print '</form>';
} else {
print '<tr class="'.($ii % 2 == 0 ? 'pair':'impair').'">';
print '<td>'.$prices['qty_min'].' - '.$prices['qty_max'].'</td>';
print '<td align="right">'.price($prices['price']).'</td>';
print '<td align="right">'.price($prices['price_ttc']).'</td>';
print '<td align="center">';
if(($user->rights->produit->creer || $user->rights->service->creer)) {
print '<a href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=edit_price_by_qty&amp;rowid='.$prices["rowid"].'">';
print img_edit().'</a>';
print '<a href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=delete_price_by_qty&amp;rowid='.$prices["rowid"].'">';
print img_delete().'</a>';
} else {
print '&nbsp;';
}
print '</td>';
print '</tr>';
}
}
if($action != 'edit_price_by_qty') {
print '<form action="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'" method="POST">';
print '<input type="hidden" name="action" value="update_price_by_qty">';
print '<input type="hidden" name="priceid" value="'.$object->prices_by_qty_id[$i].'">';
print '<input type="hidden" value="0" name="rowid">';
print '<tr class="'.($ii % 2 == 0 ? 'pair':'impair').'">';
print '<td><input size="5" type="text" value="0" name="qty_min"> - ';
print '<input size="5" type="text" value="0" name="qty_max"></td>';
print '<td align="right"><input type="text" value="0.00000" name="price">&nbsp;'.$object->price_base_type.'</td>';
print '<td align="right">&nbsp;</td>';
print '<td align="center"><input type="submit" value="'.$langs->trans("Add").'" class="button"></td>';
print '</tr>';
print '</form>';
}
print '</table>';
} else {
print $langs->trans("No");
print '&nbsp;<a href="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'&action=activate_price_by_qty&level='.$i.'">('.$langs->trans("Activate").')</a>';
}
print '</td></tr>';
}
}
}
}
@@ -261,6 +467,76 @@ else
print price($object->price_min).' '.$langs->trans($object->price_base_type);
}
print '</td></tr>';
// Price by quantity
if($conf->global->PRODUIT_PRICE_BY_QTY) {
print '<tr><td>'.$langs->trans("PriceByQuantity");
if($object->prices_by_qty[0] == 0) {
print '&nbsp;<a href="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'&action=activate_price_by_qty&level=1">'.$langs->trans("Activate");
}
print '</td><td>';
if($object->prices_by_qty[0] == 1) {
print '<table width="50%" class="noborder">';
print '<tr class="liste_titre">';
print '<td>'.$langs->trans("PriceByQuantityRange").'</td>';
print '<td align="right">'.$langs->trans("HT").'</td>';
print '<td align="right">'.$langs->trans("TTC").'</td>';
print '<td>&nbsp;</td>';
print '</tr>';
foreach ($object->prices_by_qty_list[0] as $ii=> $prices) {
if($action == 'edit_price_by_qty' && $rowid == $prices['rowid'] && ($user->rights->produit->creer || $user->rights->service->creer)) {
print '<form action="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'" method="POST">';
print '<input type="hidden" name="action" value="update_price_by_qty">';
print '<input type="hidden" name="priceid" value="'.$object->prices_by_qty_id[0].'">';
print '<input type="hidden" value="'.$prices['rowid'].'" name="rowid">';
print '<tr class="'.($ii % 2 == 0 ? 'pair':'impair').'">';
print '<td><input size="5" type="text" value="'.$prices['qty_min'].'" name="qty_min"> - ';
print '<input size="5" type="text" value="'.$prices['qty_max'].'" name="qty_max"></td>';
print '<td align="right"><input type="text" value="'.$prices['price'].'" name="price">&nbsp;'.$object->price_base_type.'</td>';
print '<td align="right">&nbsp;</td>';
print '<td align="center"><input type="submit" value="'.$langs->trans("Modify").'" class="button"></td>';
print '</tr>';
print '</form>';
} else {
print '<tr class="'.($ii % 2 == 0 ? 'pair':'impair').'">';
print '<td>'.$prices['qty_min'].' - '.$prices['qty_max'].'</td>';
print '<td align="right">'.price($prices['price']).'</td>';
print '<td align="right">'.price($prices['price_ttc']).'</td>';
print '<td align="center">';
if(($user->rights->produit->creer || $user->rights->service->creer)) {
print '<a href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=edit_price_by_qty&amp;rowid='.$prices["rowid"].'">';
print img_edit().'</a>';
print '<a href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=delete_price_by_qty&amp;rowid='.$prices["rowid"].'">';
print img_delete().'</a>';
} else {
print '&nbsp;';
}
print '</td>';
print '</tr>';
}
}
if($action != 'edit_price_by_qty') {
print '<form action="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'" method="POST">';
print '<input type="hidden" name="action" value="update_price_by_qty">';
print '<input type="hidden" name="priceid" value="'.$object->prices_by_qty_id[0].'">';
print '<input type="hidden" value="0" name="rowid">';
print '<tr class="'.($ii % 2 == 0 ? 'pair':'impair').'">';
print '<td><input size="5" type="text" value="0" name="qty_min"> - ';
print '<input size="5" type="text" value="0" name="qty_max"></td>';
print '<td align="right"><input type="text" value="0.00000" name="price">&nbsp;'.$object->price_base_type.'</td>';
print '<td align="right">&nbsp;</td>';
print '<td align="center"><input type="submit" value="'.$langs->trans("Add").'" class="button"></td>';
print '</tr>';
print '</form>';
}
print '</table>';
} else {
print $langs->trans("No");
}
print '</td></tr>';
}
}
// Status (to sell)
@@ -426,7 +702,7 @@ if ($action == 'edit_price' && ($user->rights->produit->creer || $user->rights->
// Liste des evolutions du prix
$sql = "SELECT p.rowid, p.price, p.price_ttc, p.price_base_type, p.tva_tx, p.recuperableonly,";
$sql.= " p.price_level, p.price_min, p.price_min_ttc,";
$sql.= " p.price_level, p.price_min, p.price_min_ttc,p.price_by_qty,";
$sql.= " p.date_price as dp, u.rowid as user_id, u.login";
$sql.= " FROM ".MAIN_DB_PREFIX."product_price as p,";
$sql.= " ".MAIN_DB_PREFIX."user as u";
@@ -464,7 +740,11 @@ if ($result)
if (! empty($conf->global->PRODUIT_MULTIPRICES))
{
print '<td>'.$langs->trans("MultiPriceLevelsName").'</td>';
print '<td align="center">'.$langs->trans("MultiPriceLevelsName").'</td>';
}
if (! empty($conf->global->PRODUIT_PRICE_BY_QTY))
{
print '<td align="center">'.$langs->trans("Type").'</td>';
}
print '<td align="center">'.$langs->trans("PriceBase").'</td>';
@@ -492,6 +772,12 @@ if ($result)
{
print '<td align="center">'.$objp->price_level."</td>";
}
// Price by quantity
if (! empty($conf->global->PRODUIT_PRICE_BY_QTY))
{
$type = ($objp->price_by_qty == 1) ? 'PriceByQuantity' : 'Standard';
print '<td align="center">'.$langs->trans($type)."</td>";
}
print '<td align="center">'.$langs->trans($objp->price_base_type)."</td>";
print '<td align="right">'.vatrate($objp->tva_tx,true,$objp->recuperableonly)."</td>";