From 0f1617c31d4ddf12beba0d2c460890b595161607 Mon Sep 17 00:00:00 2001 From: Maxime Kohlhaas Date: Fri, 26 Oct 2012 01:00:03 +0200 Subject: [PATCH] Task 559 : add use of price by qty in product selection (select and ajax search) --- htdocs/core/class/html.form.class.php | 367 ++++++++++++++++--------- htdocs/core/tpl/objectline_add.tpl.php | 12 +- htdocs/product/ajax/products.php | 37 ++- htdocs/product/price.php | 2 +- 4 files changed, 272 insertions(+), 146 deletions(-) diff --git a/htdocs/core/class/html.form.class.php b/htdocs/core/class/html.form.class.php index 32f76d60eeb..39f9dc167e1 100644 --- a/htdocs/core/class/html.form.class.php +++ b/htdocs/core/class/html.form.class.php @@ -1148,6 +1148,18 @@ class Form { $sql.= ", pl.label as label_translated"; } + // Price by quantity + if (! empty($conf->global->PRODUIT_PRICE_BY_QTY)) + { + $sql.= ", (SELECT pp.rowid FROM ".MAIN_DB_PREFIX."product_price as pp WHERE pp.fk_product = p.rowid"; + if ($price_level >= 1) $sql.= " AND price_level=".$price_level; + $sql.= " ORDER BY date_price"; + $sql.= " DESC LIMIT 1) as price_rowid"; + $sql.= ", (SELECT pp.price_by_qty FROM ".MAIN_DB_PREFIX."product_price as pp WHERE pp.fk_product = p.rowid"; + if ($price_level >= 1) $sql.= " AND price_level=".$price_level; + $sql.= " ORDER BY date_price"; + $sql.= " DESC LIMIT 1) as price_by_qty"; + } $sql.= " FROM ".MAIN_DB_PREFIX."product as p"; // Multilang : we add translation if (! empty($conf->global->MAIN_MULTILANGS)) @@ -1209,143 +1221,50 @@ class Form $i = 0; while ($num && $i < $num) { - $outkey=''; - $outval=''; - $outref=''; - $outlabel=''; - $outdesc=''; - $outtype=''; - $outprice_ht=''; - $outprice_ttc=''; - $outpricebasetype=''; - $outtva_tx=''; - - $objp = $this->db->fetch_object($result); - - $label=$objp->label; - if (! empty($objp->label_translated)) $label=$objp->label_translated; - if ($filterkey && $filterkey != '') $label=preg_replace('/('.preg_quote($filterkey).')/i','$1',$label,1); - - $outkey=$objp->rowid; - $outref=$objp->ref; - $outlabel=$objp->label; - $outdesc=$objp->description; - $outtype=$objp->fk_product_type; - - $opt = '\n"; - - // Add new entry - // "key" value of json key array is used by jQuery automatically as selected value - // "label" value of json key array is used by jQuery automatically as text for combo box - $outselect.=$opt; - array_push($outjson, array('key'=>$outkey, 'value'=>$outref, 'label'=>$outval, 'label2'=>$outlabel, 'desc'=>$outdesc, 'type'=>$outtype, 'price_ht'=>$outprice_ht, 'price_ttc'=>$outprice_ttc, 'pricebasetype'=>$outpricebasetype, 'tva_tx'=>$outtva_tx)); + $opt = ''; + $optJson = array(); + $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.= " FROM ".MAIN_DB_PREFIX."product_price_by_qty"; + $sql.= " WHERE fk_product_price=".$objp->price_rowid; + + dol_syslog(get_class($this)."::select_produits_do search price by qty sql=".$sql); + $result2 = $this->db->query($sql); + if ($result2) + { + $nb_prices = $this->db->num_rows($result2); + $j = 0; + while ($nb_prices && $j < $nb_prices) { + $objp2 = $this->db->fetch_object($result2); + + $objp->quantity = $objp2->qty_min; + $objp->price = $objp2->price; + $objp->unitprice = $objp2->price; + $objp->remise_percent = $objp2->remise_percent; + $objp->remise = $objp2->remise; + $objp->price_by_qty_rowid = $objp2->rowid; + + $this->_construct_product_list_option($objp, $opt, $optJson, 0, $selected); + + $j++; + + // Add new entry + // "key" value of json key array is used by jQuery automatically as selected value + // "label" value of json key array is used by jQuery automatically as text for combo box + $outselect.=$opt; + array_push($outjson, $optJson); + } + } + } else { + $this->_construct_product_list_option($objp, $opt, $optJson, $price_level, $selected); + // Add new entry + // "key" value of json key array is used by jQuery automatically as selected value + // "label" value of json key array is used by jQuery automatically as text for combo box + $outselect.=$opt; + array_push($outjson, $optJson); + } $i++; } @@ -1363,6 +1282,180 @@ class Form } } + function _construct_product_list_option(&$objp, &$opt, &$optJson, $price_level, $selected) { + global $langs,$conf,$user,$db; + + $outkey=''; + $outval=''; + $outref=''; + $outlabel=''; + $outdesc=''; + $outtype=''; + $outprice_ht=''; + $outprice_ttc=''; + $outpricebasetype=''; + $outtva_tx=''; + $outqty=1; + $outdiscount=0; + + $label=$objp->label; + if (! empty($objp->label_translated)) $label=$objp->label_translated; + if ($filterkey && $filterkey != '') $label=preg_replace('/('.preg_quote($filterkey).')/i','$1',$label,1); + + $outkey=$objp->rowid; + $outref=$objp->ref; + $outlabel=$objp->label; + $outdesc=$objp->description; + $outtype=$objp->fk_product_type; + + $opt = '\n"; + $optJson = array('key'=>$outkey, 'value'=>$outref, 'label'=>$outval, 'label2'=>$outlabel, 'desc'=>$outdesc, 'type'=>$outtype, 'price_ht'=>$outprice_ht, 'price_ttc'=>$outprice_ttc, 'pricebasetype'=>$outpricebasetype, 'tva_tx'=>$outtva_tx, 'qty'=>$outqty, 'discount'=>$outdiscount); + } + /** * Return list of products for customer (in Ajax if Ajax activated or go to select_produits_fournisseurs_do) * diff --git a/htdocs/core/tpl/objectline_add.tpl.php b/htdocs/core/tpl/objectline_add.tpl.php index d9f35ad6971..185656bf231 100644 --- a/htdocs/core/tpl/objectline_add.tpl.php +++ b/htdocs/core/tpl/objectline_add.tpl.php @@ -79,7 +79,9 @@ if (! empty($conf->margin->enabled)) { 'price_ht' => 'price_ht', 'origin_price_ht_cache' => 'price_ht', 'origin_tva_tx_cache' => 'tva_tx', - 'origin_price_ttc_cache' => 'price_ttc' + 'origin_price_ttc_cache' => 'price_ttc', + 'qty' => 'qty', + 'remise_percent' => 'discount' ), 'update_textarea' => array( 'product_desc' => 'desc' @@ -158,7 +160,7 @@ if (! empty($conf->margin->enabled)) { - % + % margin->enabled)) { @@ -225,7 +227,9 @@ $(document).ready(function() { $.post('/product/ajax/products.php', { 'action': 'fetch', 'id': $(this).val(), - 'price_level': price_level)?1:$buyer->price_level; ?>}, + 'price_level': price_level)?1:$buyer->price_level; ?>, + 'pbq': $("option:selected", this).attr('pbq') + }, function(data) { if (typeof data != 'undefined') { $('#product_ref').val(data.ref); @@ -237,6 +241,8 @@ $(document).ready(function() { $('#origin_tva_tx_cache').val(data.tva_tx); $('#select_type').val(data.type).attr('disabled','disabled').trigger('change'); //$('#price_base_type_area').show(); + $('#qty').val(data.qty); + $('#remise_percent').val(data.discount); if (typeof CKEDITOR == "object" && typeof CKEDITOR.instances != "undefined" && CKEDITOR.instances['product_desc'] != "undefined") { CKEDITOR.instances['product_desc'].setData(data.desc).focus(); diff --git a/htdocs/product/ajax/products.php b/htdocs/product/ajax/products.php index d1874d11eaa..f5702b72f8c 100644 --- a/htdocs/product/ajax/products.php +++ b/htdocs/product/ajax/products.php @@ -38,9 +38,10 @@ $type=GETPOST('type','int'); $mode=GETPOST('mode','int'); $status=((GETPOST('status','int') >= 0) ? GETPOST('status','int') : -1); $outjson=(GETPOST('outjson','int') ? GETPOST('outjson','int') : 0); -$pricelevel=GETPOST('price_level','int'); +$price_level=GETPOST('price_level','int'); $action=GETPOST('action', 'alpha'); $id=GETPOST('id', 'int'); +$price_by_qty_rowid=GETPOST('pbq', 'int'); /* * View @@ -65,11 +66,37 @@ if (! empty($action) && $action == 'fetch' && ! empty($id)) $outlabel=$object->label; $outdesc=$object->description; $outtype=$object->type; + $outqty=1; + $outdiscount=0; $found=false; + + // 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.= " FROM ".MAIN_DB_PREFIX."product_price_by_qty "; + $sql.= " WHERE rowid=".$price_by_qty_rowid.""; + + $result = $db->query($sql); + if ($result) + { + $objp = $db->fetch_object($result); + if ($objp) + { + $found=true; + $outprice_ht=price($objp->price); + $outprice_ttc=price($objp->price_ttc); + $outpricebasetype=$object->price_base_type; + $outtva_tx=$object->tva_tx; + $outqty=$objp->qty_min; + $outdiscount=$objp->remise_percent; + } + } + } // Multiprice - if (isset($price_level) && $price_level >= 1) // If we need a particular price level (from 1 to 6) + if (! $found && isset($price_level) && $price_level >= 1) // If we need a particular price level (from 1 to 6) { $sql = "SELECT price, price_ttc, price_base_type, tva_tx"; $sql.= " FROM ".MAIN_DB_PREFIX."product_price "; @@ -78,10 +105,10 @@ if (! empty($action) && $action == 'fetch' && ! empty($id)) $sql.= " ORDER BY date_price"; $sql.= " DESC LIMIT 1"; - $result = $this->db->query($sql); + $result = $db->query($sql); if ($result) { - $objp = $this->db->fetch_object($result); + $objp = $db->fetch_object($result); if ($objp) { $found=true; @@ -101,7 +128,7 @@ if (! empty($action) && $action == 'fetch' && ! empty($id)) $outtva_tx=$object->tva_tx; } - $outjson = array('ref'=>$outref, 'label'=>$outlabel, 'desc'=>$outdesc, 'type'=>$outtype, 'price_ht'=>$outprice_ht, 'price_ttc'=>$outprice_ttc, 'pricebasetype'=>$outpricebasetype, 'tva_tx'=>$outtva_tx); + $outjson = array('ref'=>$outref, 'label'=>$outlabel, 'desc'=>$outdesc, 'type'=>$outtype, 'price_ht'=>$outprice_ht, 'price_ttc'=>$outprice_ttc, 'pricebasetype'=>$outpricebasetype, 'tva_tx'=>$outtva_tx, 'qty'=>$outqty, 'discount'=>$outdiscount); } echo json_encode($outjson); diff --git a/htdocs/product/price.php b/htdocs/product/price.php index 42d6712be52..7fe5aa5b89e 100644 --- a/htdocs/product/price.php +++ b/htdocs/product/price.php @@ -366,7 +366,7 @@ if (! empty($conf->global->PRODUIT_MULTIPRICES)) print ''; print ' '.$object->price_base_type.''; //print ' '; - print '< %/td>'; + print ' %'; print ''; print ''; print '';