Task 559 : add use of price by qty in product selection (select and ajax search)

This commit is contained in:
Maxime Kohlhaas 2012-10-26 01:00:03 +02:00
parent 84df6bd639
commit 0f1617c31d
4 changed files with 272 additions and 146 deletions

View File

@ -1148,6 +1148,18 @@ class Form
{ {
$sql.= ", pl.label as label_translated"; $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"; $sql.= " FROM ".MAIN_DB_PREFIX."product as p";
// Multilang : we add translation // Multilang : we add translation
if (! empty($conf->global->MAIN_MULTILANGS)) if (! empty($conf->global->MAIN_MULTILANGS))
@ -1209,6 +1221,70 @@ class Form
$i = 0; $i = 0;
while ($num && $i < $num) while ($num && $i < $num)
{ {
$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++;
}
$outselect.='</select>';
$this->db->free($result);
if (empty($disableout)) print $outselect;
return $outjson;
}
else
{
dol_print_error($db);
}
}
function _construct_product_list_option(&$objp, &$opt, &$optJson, $price_level, $selected) {
global $langs,$conf,$user,$db;
$outkey=''; $outkey='';
$outval=''; $outval='';
$outref=''; $outref='';
@ -1219,8 +1295,8 @@ class Form
$outprice_ttc=''; $outprice_ttc='';
$outpricebasetype=''; $outpricebasetype='';
$outtva_tx=''; $outtva_tx='';
$outqty=1;
$objp = $this->db->fetch_object($result); $outdiscount=0;
$label=$objp->label; $label=$objp->label;
if (! empty($objp->label_translated)) $label=$objp->label_translated; if (! empty($objp->label_translated)) $label=$objp->label_translated;
@ -1234,6 +1310,7 @@ class Form
$opt = '<option value="'.$objp->rowid.'"'; $opt = '<option value="'.$objp->rowid.'"';
$opt.= ($objp->rowid == $selected)?' selected="selected"':''; $opt.= ($objp->rowid == $selected)?' selected="selected"':'';
$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 (! empty($conf->stock->enabled) && $objp->fk_product_type == 0 && isset($objp->stock))
{ {
if ($objp->stock > 0) if ($objp->stock > 0)
@ -1298,6 +1375,42 @@ class Form
} }
} }
// Price by quantity
if (!empty($objp->quantity) && $objp->quantity >= 1) {
$found = 1;
$outqty=$objp->quantity;
$outdiscount=$objp->remise_percent;
if ($objp->quantity == 1)
{
$opt.= price($objp->price).' '.$currencytext."/";
$outval.= price($objp->price).' '.$currencytextnoent."/";
$opt.= $langs->trans("Unit"); // Do not use strtolower because it breaks utf8 encoding
$outval.=$langs->transnoentities("Unit");
}
else
{
$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);
$outpricebasetype=$objp->price_base_type;
$outtva_tx=$objp->tva_tx;
}
if (!empty($objp->quantity) && $objp->quantity >= 1)
{
$opt.=" (".price($objp->unitprice).' '.$currencytext."/".$langs->trans("Unit").")"; // Do not use strtolower because it breaks utf8 encoding
$outval.=" (".price($objp->unitprice).' '.$currencytextnoent."/".$langs->transnoentities("Unit").")"; // Do not use strtolower because it breaks utf8 encoding
}
if (!empty($objp->remise_percent) && $objp->remise_percent >= 1)
{
$opt.=" - ".$langs->trans("Discount")." : ".vatrate($objp->remise_percent).' %';
$outval.=" - ".$langs->transnoentities("Discount")." : ".vatrate($objp->remise_percent).' %';
}
// If level no defined or multiprice not found, we used the default price // If level no defined or multiprice not found, we used the default price
if (! $found) if (! $found)
{ {
@ -1340,27 +1453,7 @@ class Form
} }
$opt.= "</option>\n"; $opt.= "</option>\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);
// 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));
$i++;
}
$outselect.='</select>';
$this->db->free($result);
if (empty($disableout)) print $outselect;
return $outjson;
}
else
{
dol_print_error($db);
}
} }
/** /**

View File

@ -79,7 +79,9 @@ if (! empty($conf->margin->enabled)) {
'price_ht' => 'price_ht', 'price_ht' => 'price_ht',
'origin_price_ht_cache' => 'price_ht', 'origin_price_ht_cache' => 'price_ht',
'origin_tva_tx_cache' => 'tva_tx', '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( 'update_textarea' => array(
'product_desc' => 'desc' 'product_desc' => 'desc'
@ -158,7 +160,7 @@ if (! empty($conf->margin->enabled)) {
<input type="hidden" id="origin_price_ttc_cache" name="origin_price_ttc_cache" value="" /> <input type="hidden" id="origin_price_ttc_cache" name="origin_price_ttc_cache" value="" />
</td> </td>
<td align="right"><input type="text" size="3" id="qty" name="qty" value="<?php echo (GETPOST('qty')?GETPOST('qty'):1); ?>"></td> <td align="right"><input type="text" size="3" id="qty" name="qty" value="<?php echo (GETPOST('qty')?GETPOST('qty'):1); ?>"></td>
<td align="right" nowrap="nowrap"><input type="text" size="1" value="<?php echo $buyer->remise_client; ?>" name="remise_percent">%</td> <td align="right" nowrap="nowrap"><input type="text" size="1" value="<?php echo $buyer->remise_client; ?>" id="remise_percent" name="remise_percent">%</td>
<?php <?php
$colspan = 4; $colspan = 4;
if (! empty($conf->margin->enabled)) { if (! empty($conf->margin->enabled)) {
@ -225,7 +227,9 @@ $(document).ready(function() {
$.post('<?php echo DOL_URL_ROOT; ?>/product/ajax/products.php', { $.post('<?php echo DOL_URL_ROOT; ?>/product/ajax/products.php', {
'action': 'fetch', 'action': 'fetch',
'id': $(this).val(), 'id': $(this).val(),
'price_level': <?php echo empty($buyer->price_level)?1:$buyer->price_level; ?>}, 'price_level': <?php echo empty($buyer->price_level)?1:$buyer->price_level; ?>,
'pbq': $("option:selected", this).attr('pbq')
},
function(data) { function(data) {
if (typeof data != 'undefined') { if (typeof data != 'undefined') {
$('#product_ref').val(data.ref); $('#product_ref').val(data.ref);
@ -237,6 +241,8 @@ $(document).ready(function() {
$('#origin_tva_tx_cache').val(data.tva_tx); $('#origin_tva_tx_cache').val(data.tva_tx);
$('#select_type').val(data.type).attr('disabled','disabled').trigger('change'); $('#select_type').val(data.type).attr('disabled','disabled').trigger('change');
//$('#price_base_type_area').show(); //$('#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") { if (typeof CKEDITOR == "object" && typeof CKEDITOR.instances != "undefined" && CKEDITOR.instances['product_desc'] != "undefined") {
CKEDITOR.instances['product_desc'].setData(data.desc).focus(); CKEDITOR.instances['product_desc'].setData(data.desc).focus();

View File

@ -38,9 +38,10 @@ $type=GETPOST('type','int');
$mode=GETPOST('mode','int'); $mode=GETPOST('mode','int');
$status=((GETPOST('status','int') >= 0) ? GETPOST('status','int') : -1); $status=((GETPOST('status','int') >= 0) ? GETPOST('status','int') : -1);
$outjson=(GETPOST('outjson','int') ? GETPOST('outjson','int') : 0); $outjson=(GETPOST('outjson','int') ? GETPOST('outjson','int') : 0);
$pricelevel=GETPOST('price_level','int'); $price_level=GETPOST('price_level','int');
$action=GETPOST('action', 'alpha'); $action=GETPOST('action', 'alpha');
$id=GETPOST('id', 'int'); $id=GETPOST('id', 'int');
$price_by_qty_rowid=GETPOST('pbq', 'int');
/* /*
* View * View
@ -65,11 +66,37 @@ if (! empty($action) && $action == 'fetch' && ! empty($id))
$outlabel=$object->label; $outlabel=$object->label;
$outdesc=$object->description; $outdesc=$object->description;
$outtype=$object->type; $outtype=$object->type;
$outqty=1;
$outdiscount=0;
$found=false; $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 // 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 = "SELECT price, price_ttc, price_base_type, tva_tx";
$sql.= " FROM ".MAIN_DB_PREFIX."product_price "; $sql.= " FROM ".MAIN_DB_PREFIX."product_price ";
@ -78,10 +105,10 @@ if (! empty($action) && $action == 'fetch' && ! empty($id))
$sql.= " ORDER BY date_price"; $sql.= " ORDER BY date_price";
$sql.= " DESC LIMIT 1"; $sql.= " DESC LIMIT 1";
$result = $this->db->query($sql); $result = $db->query($sql);
if ($result) if ($result)
{ {
$objp = $this->db->fetch_object($result); $objp = $db->fetch_object($result);
if ($objp) if ($objp)
{ {
$found=true; $found=true;
@ -101,7 +128,7 @@ if (! empty($action) && $action == 'fetch' && ! empty($id))
$outtva_tx=$object->tva_tx; $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); echo json_encode($outjson);

View File

@ -366,7 +366,7 @@ if (! empty($conf->global->PRODUIT_MULTIPRICES))
print '<td><input size="5" type="text" value="'.$prices['qty_min'].'" name="qty_min"></td>'; print '<td><input size="5" type="text" value="'.$prices['qty_min'].'" name="qty_min"></td>';
print '<td align="right" colspan="2"><input size="10" type="text" value="'.$prices['price'].'" name="price">&nbsp;'.$object->price_base_type.'</td>'; print '<td align="right" colspan="2"><input size="10" type="text" value="'.$prices['price'].'" name="price">&nbsp;'.$object->price_base_type.'</td>';
//print '<td align="right">&nbsp;</td>'; //print '<td align="right">&nbsp;</td>';
print '<td align="right"><input size="5" type="text" value="'.$prices['remise_percent'].'" name="remise_percent"><&nbsp;%/td>'; print '<td align="right"><input size="5" type="text" value="'.$prices['remise_percent'].'" name="remise_percent">&nbsp;%</td>';
print '<td align="center"><input type="submit" value="'.$langs->trans("Modify").'" class="button"></td>'; print '<td align="center"><input type="submit" value="'.$langs->trans("Modify").'" class="button"></td>';
print '</tr>'; print '</tr>';
print '</form>'; print '</form>';