dolibarr  7.0.0-beta
fournisseur.product.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2006-2011 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2009-2014 Regis Houssin <regis.houssin@capnetworks.com>
5  * Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
6  * Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.fr>
7  * Copyright (C) 2015 Marcos GarcĂ­a <marcosgdf@gmail.com>
8  * Copyright (C) 2016 Charlie Benke <charlie@patas-monkey.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
30 require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
31 require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.class.php';
32 require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php';
33 
34 
39 {
40  var $db;
41  var $error;
42 
43  var $product_fourn_price_id; // id of ligne product-supplier
44 
45  var $id; // product id
51  var $delivery_time_days;
52  var $ref_supplier; // ref supplier (can be set by get_buyprice)
53  var $vatrate_supplier; // default vat rate for this supplier/qty/product (can be set by get_buyprice)
54 
55  var $fourn_id; //supplier id
56  var $fourn_qty; // quantity for price (can be set by get_buyprice)
57  var $fourn_pu; // unit price for quantity (can be set by get_buyprice)
58 
59  var $fourn_price; // price for quantity
60  var $fourn_remise_percent; // discount for quantity (percent)
61  var $fourn_remise; // discount for quantity (amount)
62  var $product_fourn_id; // supplier id
63  var $fk_availability; // availability delay - visible/used if option FOURN_PRODUCT_AVAILABILITY is on (duplicate information compared to delivery delay)
64  var $fourn_unitprice;
65  var $fourn_tva_tx;
66  var $fourn_tva_npr;
70  var $fourn_unitcharges; // old version used a buggy system to calculate margin of a charge field on supplier price. Now margin is on pmp, best supplier price or cost price.
71 
72  var $fk_supplier_price_expression;
73  var $supplier_reputation; // reputation of supplier
74  var $reputations=array(); // list of available supplier reputations
75 
81  function __construct($db)
82  {
83  global $langs;
84 
85  $this->db = $db;
86  $langs->load("suppliers");
87  $this->reputations= array('-1'=>'', 'FAVORITE'=>$langs->trans('Favorite'),'NOTTHGOOD'=>$langs->trans('NotTheGoodQualitySupplier'), 'DONOTORDER'=>$langs->trans('DoNotOrderThisProductToThisSupplier'));
88  }
89 
90 
91 
98  function remove_fournisseur($id_fourn)
99  {
100  $ok=1;
101 
102  $this->db->begin();
103 
104  $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price";
105  $sql.= " WHERE fk_product = ".$this->id." AND fk_soc = ".$id_fourn;
106 
107  dol_syslog(get_class($this)."::remove_fournisseur", LOG_DEBUG);
108  $resql2=$this->db->query($sql);
109  if (! $resql2)
110  {
111  $this->error=$this->db->lasterror();
112  $ok=0;
113  }
114 
115  if ($ok)
116  {
117  $this->db->commit();
118  return 1;
119  }
120  else
121  {
122  $this->db->rollback();
123  return -1;
124  }
125  }
126 
127 
135  {
136  global $conf, $user;
137 
138  $error=0;
139 
140  $this->db->begin();
141 
142  // Call trigger
143  $result=$this->call_trigger('SUPPLIER_PRODUCT_BUYPRICE_DELETE',$user);
144  if ($result < 0) $error++;
145  // End call triggers
146 
147  if (empty($error))
148  {
149 
150  $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_fournisseur_price";
151  $sql.= " WHERE rowid = ".$rowid;
152 
153  dol_syslog(get_class($this)."::remove_product_fournisseur_price", LOG_DEBUG);
154  $resql = $this->db->query($sql);
155  if (!$resql)
156  {
157  $this->error=$this->db->lasterror();
158  $error++;
159  }
160  }
161 
162  if (empty($error)){
163  $this->db->commit();
164  return 1;
165  }else{
166  $this->db->rollback();
167  return -1;
168  }
169 
170  }
171 
172 
194  function update_buyprice($qty, $buyprice, $user, $price_base_type, $fourn, $availability, $ref_fourn, $tva_tx, $charges=0, $remise_percent=0, $remise=0, $newnpr=0, $delivery_time_days=0, $supplier_reputation='', $localtaxes_array=array(), $newdefaultvatcode='')
195  {
196  global $conf, $langs;
197  //global $mysoc;
198 
199  // Clean parameter
200  if (empty($qty)) $qty=0;
201  if (empty($buyprice)) $buyprice=0;
202  if (empty($charges)) $charges=0;
203  if (empty($availability)) $availability=0;
204  if (empty($remise_percent)) $remise_percent=0;
205  if (empty($supplier_reputation) || $supplier_reputation == -1) $supplier_reputation='';
206  if ($delivery_time_days != '' && ! is_numeric($delivery_time_days)) $delivery_time_days = '';
207  if ($price_base_type == 'TTC')
208  {
209  $ttx = $tva_tx;
210  $buyprice = $buyprice/(1+($ttx/100));
211  }
213  $charges=price2num($charges,'MU');
214  $qty=price2num($qty);
215  $error=0;
216 
217  $unitBuyPrice = price2num($buyprice/$qty,'MU');
218  $unitCharges = price2num($charges/$qty,'MU');
219 
220  $now=dol_now();
221 
222  $newvat = $tva_tx;
223 
224  if (count($localtaxes_array) > 0)
225  {
226  $localtaxtype1=$localtaxes_array['0'];
227  $localtax1=$localtaxes_array['1'];
228  $localtaxtype2=$localtaxes_array['2'];
229  $localtax2=$localtaxes_array['3'];
230  }
231  else // old method. deprecated because ot can't retreive type
232  {
233  $localtaxtype1='0';
234  $localtax1=get_localtax($newvat,1);
235  $localtaxtype2='0';
236  $localtax2=get_localtax($newvat,2);
237  }
238  if (empty($localtax1)) $localtax1=0; // If = '' then = 0
239  if (empty($localtax2)) $localtax2=0; // If = '' then = 0
240 
241  $this->db->begin();
242 
243  if ($this->product_fourn_price_id > 0)
244  {
245  $sql = "UPDATE ".MAIN_DB_PREFIX."product_fournisseur_price";
246  $sql.= " SET fk_user = " . $user->id." ,";
247  $sql.= " ref_fourn = '" . $this->db->escape($ref_fourn) . "',";
248  $sql.= " price = ".price2num($buyprice).",";
249  $sql.= " quantity = ".$qty.",";
250  $sql.= " remise_percent = ".$remise_percent.",";
251  $sql.= " remise = ".$remise.",";
252  $sql.= " unitprice = ".$unitBuyPrice.",";
253  $sql.= " unitcharges = ".$unitCharges.","; // deprecated
254  $sql.= " fk_availability = ".$availability.",";
255  $sql.= " entity = ".$conf->entity.",";
256  $sql.= " tva_tx = ".price2num($tva_tx).",";
257  // TODO Add localtax1 and localtax2
258  //$sql.= " localtax1_tx=".($localtax1>=0?$localtax1:'NULL').",";
259  //$sql.= " localtax2_tx=".($localtax2>=0?$localtax2:'NULL').",";
260  //$sql.= " localtax1_type=".($localtaxtype1!=''?"'".$localtaxtype1."'":"'0'").",";
261  //$sql.= " localtax2_type=".($localtaxtype2!=''?"'".$localtaxtype2."'":"'0'").",";
262  $sql.= " default_vat_code=".($newdefaultvatcode?"'".$this->db->escape($newdefaultvatcode)."'":"null").",";
263  $sql.= " info_bits = ".$newnpr.",";
264  $sql.= " charges = ".$charges.","; // deprecated
265  $sql.= " delivery_time_days = ".($delivery_time_days != '' ? $delivery_time_days : 'null').",";
266  $sql.= " supplier_reputation = ".(empty($supplier_reputation) ? 'NULL' : "'".$this->db->escape($supplier_reputation)."'");
267  $sql.= " WHERE rowid = ".$this->product_fourn_price_id;
268  // TODO Add price_base_type and price_ttc
269 
270  dol_syslog(get_class($this).'::update_buyprice update knowing id of line = product_fourn_price_id = '.$this->product_fourn_price_id, LOG_DEBUG);
271  $resql = $this->db->query($sql);
272  if ($resql)
273  {
274  // Call trigger
275  $result=$this->call_trigger('SUPPLIER_PRODUCT_BUYPRICE_UPDATE',$user);
276  if ($result < 0) $error++;
277  // End call triggers
278 
279  if (empty($error))
280  {
281  $this->db->commit();
282  return $this->product_fourn_price_id;
283  }
284  else
285  {
286  $this->db->rollback();
287  return -1;
288  }
289  }
290  else
291  {
292  $this->error=$this->db->error()." sql=".$sql;
293  $this->db->rollback();
294  return -2;
295  }
296  }
297 
298  else
299  {
300  dol_syslog(get_class($this) . '::update_buyprice without knowing id of line, so we delete from company, quantity and supplier_ref and insert again', LOG_DEBUG);
301 
302  // Delete price for this quantity
303  $sql = "DELETE FROM " . MAIN_DB_PREFIX . "product_fournisseur_price";
304  $sql .= " WHERE fk_soc = " . $fourn->id . " AND ref_fourn = '" . $this->db->escape($ref_fourn) . "' AND quantity = " . $qty . " AND entity = " . $conf->entity;
305  $resql = $this->db->query($sql);
306  if ($resql) {
307  // Add price for this quantity to supplier
308  $sql = "INSERT INTO " . MAIN_DB_PREFIX . "product_fournisseur_price(";
309  $sql .= "datec, fk_product, fk_soc, ref_fourn, fk_user, price, quantity, remise_percent, remise, unitprice, tva_tx, charges, unitcharges, fk_availability, default_vat_code, info_bits, entity, delivery_time_days, supplier_reputation)";
310  $sql .= " values('" . $this->db->idate($now) . "',";
311  $sql .= " " . $this->id . ",";
312  $sql .= " " . $fourn->id . ",";
313  $sql .= " '" . $this->db->escape($ref_fourn) . "',";
314  $sql .= " " . $user->id . ",";
315  $sql .= " " . $buyprice . ",";
316  $sql .= " " . $qty . ",";
317  $sql .= " " . $remise_percent . ",";
318  $sql .= " " . $remise . ",";
319  $sql .= " " . $unitBuyPrice . ",";
320  $sql .= " " . $tva_tx . ",";
321  $sql .= " " . $charges . ",";
322  $sql .= " " . $unitCharges . ",";
323  $sql .= " " . $availability . ",";
324  $sql .= " ".($newdefaultvatcode?"'".$this->db->escape($newdefaultvatcode)."'":"null").",";
325  $sql .= " " . $newnpr . ",";
326  $sql .= $conf->entity . ",";
327  $sql .= $delivery_time_days . ",";
328  $sql .= (empty($supplier_reputation) ? 'NULL' : "'" . $this->db->escape($supplier_reputation) . "'");
329  $sql .= ")";
330 
331  $idinserted = 0;
332 
333  $resql = $this->db->query($sql);
334  if ($resql) {
335  $idinserted = $this->db->last_insert_id(MAIN_DB_PREFIX . "product_fournisseur_price");
336  }
337  else {
338  $error++;
339  }
340 
341  if (! $error && empty($conf->global->PRODUCT_PRICE_SUPPLIER_NO_LOG)) {
342  // Add record into log table
343  $sql = "INSERT INTO " . MAIN_DB_PREFIX . "product_fournisseur_price_log(";
344  $sql .= "datec, fk_product_fournisseur,fk_user,price,quantity)";
345  $sql .= "values('" . $this->db->idate($now) . "',";
346  $sql .= " " . $this->product_fourn_id . ",";
347  $sql .= " " . $user->id . ",";
348  $sql .= " " . price2num($buyprice) . ",";
349  $sql .= " " . $qty;
350  $sql .= ")";
351 
352  $resql = $this->db->query($sql);
353  if (! $resql) {
354  $error++;
355  }
356  }
357 
358  if (! $error) {
359  // Call trigger
360  $result = $this->call_trigger('SUPPLIER_PRODUCT_BUYPRICE_CREATE', $user);
361  if ($result < 0)
362  $error++;
363  // End call triggers
364 
365  if (empty($error)) {
366  $this->db->commit();
367  return $idinserted;
368  } else {
369  $this->db->rollback();
370  return -1;
371  }
372  } else {
373  $this->error = $this->db->lasterror() . " sql=" . $sql;
374  $this->db->rollback();
375  return -2;
376  }
377  } else {
378  $this->error = $this->db->lasterror() . " sql=" . $sql;
379  $this->db->rollback();
380  return - 1;
381  }
382  }
383  }
384 
392  function fetch_product_fournisseur_price($rowid, $ignore_expression = 0)
393  {
394  global $conf;
395  $sql = "SELECT pfp.rowid, pfp.price, pfp.quantity, pfp.unitprice, pfp.remise_percent, pfp.remise, pfp.tva_tx, pfp.default_vat_code, pfp.fk_availability,";
396  $sql.= " pfp.fk_soc, pfp.ref_fourn, pfp.fk_product, pfp.charges, pfp.unitcharges, pfp.fk_supplier_price_expression, pfp.delivery_time_days,"; // , pfp.recuperableonly as fourn_tva_npr"; FIXME this field not exist in llx_product_fournisseur_price
397  $sql.= " pfp.supplier_reputation";
398  $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp";
399  $sql.= " WHERE pfp.rowid = ".$rowid;
400 
401  dol_syslog(get_class($this)."::fetch_product_fournisseur_price", LOG_DEBUG);
402  $resql = $this->db->query($sql);
403  if ($resql)
404  {
405  $obj = $this->db->fetch_object($resql);
406  if ($obj)
407  {
408  $this->product_fourn_price_id = $rowid;
409  $this->id = $obj->fk_product;
410  $this->fk_product = $obj->fk_product;
411  $this->product_id = $obj->fk_product; // deprecated
412  $this->fourn_id = $obj->fk_soc;
413  $this->fourn_ref = $obj->ref_fourn; // deprecated
414  $this->ref_supplier = $obj->ref_fourn;
415  $this->fourn_price = $obj->price;
416  $this->fourn_charges = $obj->charges; // deprecated
417  $this->fourn_qty = $obj->quantity;
418  $this->fourn_remise_percent = $obj->remise_percent;
419  $this->fourn_remise = $obj->remise;
420  $this->fourn_unitprice = $obj->unitprice;
421  $this->fourn_unitcharges = $obj->unitcharges; // deprecated
422  $this->fourn_tva_tx = $obj->tva_tx;
423  // TODO
424  // $this->fourn_tva_npr = $obj->fourn_tva_npr; // TODO this field not exist in llx_product_fournisseur_price. We should add it ?
425  // Add also localtaxes
426  $this->fk_availability = $obj->fk_availability;
427  $this->delivery_time_days = $obj->delivery_time_days;
428  $this->fk_supplier_price_expression = $obj->fk_supplier_price_expression;
429  $this->supplier_reputation = $obj->supplier_reputation;
430  $this->default_vat_code = $obj->default_vat_code;
431 
432  if (empty($ignore_expression) && !empty($this->fk_supplier_price_expression))
433  {
434  $priceparser = new PriceParser($this->db);
435  $price_result = $priceparser->parseProductSupplier($this);
436  if ($price_result >= 0) {
437  $this->fourn_price = $price_result;
438  //recalculation of unitprice, as probably the price changed...
439  if ($this->fourn_qty!=0)
440  {
441  $this->fourn_unitprice = price2num($this->fourn_price/$this->fourn_qty,'MU');
442  }
443  else
444  {
445  $this->fourn_unitprice="";
446  }
447  }
448  }
449 
450  return 1;
451  }
452  else
453  {
454  return 0;
455  }
456  }
457  else
458  {
459  $this->error=$this->db->error();
460  return -1;
461  }
462  }
463 
464 
473  function list_product_fournisseur_price($prodid, $sortfield='', $sortorder='')
474  {
475  global $conf;
476 
477  $sql = "SELECT s.nom as supplier_name, s.rowid as fourn_id,";
478  $sql.= " pfp.rowid as product_fourn_pri_id, pfp.ref_fourn, pfp.fk_product as product_fourn_id, pfp.fk_supplier_price_expression,";
479  $sql.= " pfp.price, pfp.quantity, pfp.unitprice, pfp.remise_percent, pfp.remise, pfp.tva_tx, pfp.fk_availability, pfp.charges, pfp.unitcharges, pfp.info_bits, pfp.delivery_time_days, pfp.supplier_reputation";
480  $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp";
481  $sql.= ", ".MAIN_DB_PREFIX."societe as s";
482  $sql.= " WHERE pfp.entity IN (".getEntity('productprice').")";
483  $sql.= " AND pfp.fk_soc = s.rowid";
484  $sql.= " AND s.status=1"; // only enabled company selected
485  $sql.= " AND pfp.fk_product = ".$prodid;
486  if (empty($sortfield)) $sql.= " ORDER BY s.nom, pfp.quantity, pfp.price";
487  else $sql.= $this->db->order($sortfield,$sortorder);
488  dol_syslog(get_class($this)."::list_product_fournisseur_price", LOG_DEBUG);
489 
490  $resql = $this->db->query($sql);
491  if ($resql)
492  {
493  $retarray = array();
494 
495  while ($record = $this->db->fetch_array($resql))
496  {
497  //define base attribute
498  $prodfourn = new ProductFournisseur($this->db);
499 
500  $prodfourn->product_fourn_price_id = $record["product_fourn_pri_id"];
501  $prodfourn->product_fourn_id = $record["product_fourn_id"];
502  $prodfourn->fourn_ref = $record["ref_fourn"];
503  $prodfourn->ref_supplier = $record["ref_fourn"];
504  $prodfourn->fourn_price = $record["price"];
505  $prodfourn->fourn_qty = $record["quantity"];
506  $prodfourn->fourn_remise_percent = $record["remise_percent"];
507  $prodfourn->fourn_remise = $record["remise"];
508  $prodfourn->fourn_unitprice = $record["unitprice"];
509  $prodfourn->fourn_charges = $record["charges"]; // deprecated
510  $prodfourn->fourn_unitcharges = $record["unitcharges"]; // deprecated
511  $prodfourn->fourn_tva_tx = $record["tva_tx"];
512  $prodfourn->fourn_id = $record["fourn_id"];
513  $prodfourn->fourn_name = $record["supplier_name"];
514  $prodfourn->fk_availability = $record["fk_availability"];
515  $prodfourn->delivery_time_days = $record["delivery_time_days"];
516  $prodfourn->id = $prodid;
517  $prodfourn->fourn_tva_npr = $record["info_bits"];
518  $prodfourn->fk_supplier_price_expression = $record["fk_supplier_price_expression"];
519  $prodfourn->supplier_reputation = $record["supplier_reputation"];
520 
521  if (!empty($conf->dynamicprices->enabled) && !empty($prodfourn->fk_supplier_price_expression)) {
522  $priceparser = new PriceParser($this->db);
523  $price_result = $priceparser->parseProductSupplier($prodfourn);
524  if ($price_result >= 0) {
525  $prodfourn->fourn_price = $price_result;
526  $prodfourn->fourn_unitprice = null; //force recalculation of unitprice, as probably the price changed...
527  }
528  }
529 
530  if (!isset($prodfourn->fourn_unitprice))
531  {
532  if ($prodfourn->fourn_qty!=0)
533  {
534  $prodfourn->fourn_unitprice = price2num($prodfourn->fourn_price/$prodfourn->fourn_qty,'MU');
535  }
536  else
537  {
538  $prodfourn->fourn_unitprice="";
539  }
540  }
541 
542  $retarray[]=$prodfourn;
543  }
544 
545  $this->db->free($resql);
546  return $retarray;
547  }
548  else
549  {
550  $this->error=$this->db->error();
551  return -1;
552  }
553  }
554 
563  function find_min_price_product_fournisseur($prodid, $qty=0, $socid=0)
564  {
565  global $conf;
566 
567  if (empty($prodid))
568  {
569  dol_syslog("Warning function find_min_price_product_fournisseur were called with prodid empty. May be a bug.", LOG_WARNING);
570  return 0;
571  }
572 
573  $this->product_fourn_price_id = '';
574  $this->product_fourn_id = '';
575  $this->fourn_ref = '';
576  $this->fourn_price = '';
577  $this->fourn_qty = '';
578  $this->fourn_remise_percent = '';
579  $this->fourn_remise = '';
580  $this->fourn_unitprice = '';
581  $this->fourn_id = '';
582  $this->fourn_name = '';
583  $this->delivery_time_days = '';
584  $this->id = '';
585 
586  $sql = "SELECT s.nom as supplier_name, s.rowid as fourn_id,";
587  $sql.= " pfp.rowid as product_fourn_price_id, pfp.ref_fourn,";
588  $sql.= " pfp.price, pfp.quantity, pfp.unitprice, pfp.tva_tx, pfp.charges, pfp.unitcharges, ";
589  $sql.= " pfp.remise, pfp.remise_percent, pfp.fk_supplier_price_expression, pfp.delivery_time_days";
590  $sql.= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."product_fournisseur_price as pfp";
591  $sql.= " WHERE s.entity IN (".getEntity('societe').")";
592  $sql.= " AND pfp.fk_product = ".$prodid;
593  $sql.= " AND pfp.fk_soc = s.rowid";
594  $sql.= " AND s.status = 1"; // only enabled society
595  if ($qty > 0) $sql.= " AND pfp.quantity <= ".$qty;
596  if ($socid > 0) $sql.= ' AND pfp.fk_soc = '.$socid;
597 
598  dol_syslog(get_class($this)."::find_min_price_product_fournisseur", LOG_DEBUG);
599 
600  $resql = $this->db->query($sql);
601  if ($resql)
602  {
603  $record_array = array();
604 
605  //Store each record to array for later search of min
606  while ($record = $this->db->fetch_array($resql))
607  {
608  $record_array[]=$record;
609  }
610 
611  if (count($record_array) == 0)
612  {
613  $this->db->free($resql);
614  return 0;
615  }
616  else
617  {
618  $min = -1;
619  foreach($record_array as $record)
620  {
621  $fourn_price = $record["price"];
622  // discount calculated buy price
623  $fourn_unitprice = $record["unitprice"] * (1 - $record["remise_percent"] / 100) + $record["unitcharges"] - $record["remise"];
624  if (!empty($conf->dynamicprices->enabled) && !empty($record["fk_supplier_price_expression"])) {
625  $prod_supplier = new ProductFournisseur($this->db);
626  $prod_supplier->product_fourn_price_id = $record["product_fourn_price_id"];
627  $prod_supplier->id = $prodid;
628  $prod_supplier->fourn_qty = $record["quantity"];
629  $prod_supplier->fourn_tva_tx = $record["tva_tx"];
630  $prod_supplier->fk_supplier_price_expression = $record["fk_supplier_price_expression"];
631  $priceparser = new PriceParser($this->db);
632  $price_result = $priceparser->parseProductSupplier($prod_supplier);
633  if ($price_result >= 0) {
634  $fourn_price = price2num($price_result,'MU');
635  if ($record["quantity"] != 0)
636  {
637  $fourn_unitprice = price2num($fourn_price/$record["quantity"],'MU');
638  }
639  else
640  {
641  $fourn_unitprice = $fourn_price;
642  }
643  }
644  }
645  if ($fourn_unitprice < $min || $min == -1)
646  {
647  $this->product_fourn_price_id = $record["product_fourn_price_id"];
648  $this->ref_supplier = $record["ref_fourn"];
649  $this->ref_fourn = $record["ref_fourn"]; // deprecated
650  $this->fourn_ref = $record["ref_fourn"]; // deprecated
651  $this->fourn_price = $fourn_price;
652  $this->fourn_qty = $record["quantity"];
653  $this->fourn_remise_percent = $record["remise_percent"];
654  $this->fourn_remise = $record["remise"];
655  $this->fourn_unitprice = $record["unitprice"];
656  $this->fourn_charges = $record["charges"]; // deprecated
657  $this->fourn_unitcharges = $record["unitcharges"]; // deprecated
658  $this->fourn_tva_tx = $record["tva_tx"];
659  $this->fourn_id = $record["fourn_id"];
660  $this->fourn_name = $record["supplier_name"];
661  $this->delivery_time_days = $record["delivery_time_days"];
662  $this->fk_supplier_price_expression = $record["fk_supplier_price_expression"];
663  $this->id = $prodid;
664  $min = $fourn_unitprice;
665  }
666  }
667  }
668 
669  $this->db->free($resql);
670  return 1;
671  }
672  else
673  {
674  $this->error=$this->db->error();
675  return -1;
676  }
677  }
678 
685  function setSupplierPriceExpression($expression_id)
686  {
687  global $conf;
688 
689  // Clean parameters
690  $this->db->begin();
691  $expression_id = $expression_id != 0 ? $expression_id : 'NULL';
692 
693  $sql = "UPDATE ".MAIN_DB_PREFIX."product_fournisseur_price";
694  $sql.= " SET fk_supplier_price_expression = ".$expression_id;
695  $sql.= " WHERE rowid = ".$this->product_fourn_price_id;
696 
697  dol_syslog(get_class($this)."::setSupplierPriceExpression", LOG_DEBUG);
698 
699  $resql = $this->db->query($sql);
700  if ($resql)
701  {
702  $this->db->commit();
703  return 1;
704  }
705  else
706  {
707  $this->error=$this->db->error()." sql=".$sql;
708  $this->db->rollback();
709  return -1;
710  }
711  }
712 
723  function getSocNomUrl($withpicto=0,$option='supplier',$maxlen=0,$notooltip=0)
724  {
725  $thirdparty = new Fournisseur($this->db);
726  $thirdparty->fetch($this->fourn_id);
727 
728  return $thirdparty->getNomUrl($withpicto,$option,$maxlen,$notooltip);
729  }
730 
742  function display_price_product_fournisseur($showunitprice=1,$showsuptitle=1,$maxlen=0,$notooltip=0, $productFournList=array())
743  {
744  global $langs;
745 
746  $out = '';
747  $langs->load("suppliers");
748  if (count($productFournList) > 0) {
749  $out .= '<table class="nobordernopadding" width="100%">';
750  $out .= '<tr><td class="liste_titre" align="right">'.($showunitprice?$langs->trans("Price").' '.$langs->trans("HT"):'').'</td>';
751  $out .= '<td class="liste_titre" align="right">'.($showunitprice?$langs->trans("QtyMin"):'').'</td>';
752  $out .= '<td class="liste_titre">'.$langs->trans("Supplier").'</td>';
753  $out .= '<td class="liste_titre">'.$langs->trans("SupplierRef").'</td></tr>';
754  foreach ($productFournList as $productFourn) {
755  $out.= '<tr><td align="right">'.($showunitprice?price($productFourn->fourn_unitprice * (1 -$productFourn->fourn_remise_percent/100) + $productFourn->fourn_unitcharges - $productFourn->fourn_remise):'').'</td>';
756  $out.= '<td align="right">'.($showunitprice?$productFourn->fourn_qty:'').'</td>';
757  $out.= '<td>'.$productFourn->getSocNomUrl(1, 'supplier', $maxlen, $notooltip).'</td>';
758  $out.= '<td>'.$productFourn->fourn_ref.'<td></tr>';
759  }
760  $out .= '</table>';
761  } else {
762  $out=($showunitprice?price($this->fourn_unitprice * (1 - $this->fourn_remise_percent/100) + $this->fourn_unitcharges - $this->fourn_remise).' '.$langs->trans("HT").' &nbsp; (':'').($showsuptitle?$langs->trans("Supplier").': ':'').$this->getSocNomUrl(1, 'supplier', $maxlen, $notooltip).' / '.$langs->trans("SupplierRef").': '.$this->fourn_ref.($showunitprice?')':'');
763  }
764  return $out;
765  }
766 
775  public static function replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
776  {
777  $tables = array(
778  'product_fournisseur_price'
779  );
780 
781  return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables);
782  }
783 
784 }
785 
Class to parse product price expressions.
getSocNomUrl($withpicto=0, $option='supplier', $maxlen=0, $notooltip=0)
Display supplier of product.
Class to manage products or services.
Class to manage Dolibarr database access.
update_buyprice($qty, $buyprice, $user, $price_base_type, $fourn, $availability, $ref_fourn, $tva_tx, $charges=0, $remise_percent=0, $remise=0, $newnpr=0, $delivery_time_days=0, $supplier_reputation='', $localtaxes_array=array(), $newdefaultvatcode='')
Modify the purchase price for a supplier.
setSupplierPriceExpression($expression_id)
Sets the supplier price expression.
get_localtax($vatrate, $local, $thirdparty_buyer="", $thirdparty_seller="", $vatnpr=0)
Return localtax rate for a particular vat, when selling a product with vat $vatrate, from a $thirdparty_buyer to a $thirdparty_seller Note: This function applies same rules than get_default_tva.
Class to manage suppliers.
find_min_price_product_fournisseur($prodid, $qty=0, $socid=0)
Load properties for minimum price.
remove_fournisseur($id_fourn)
Remove all prices for this couple supplier-product.
$tva_tx
Default VAT rate of product.
remove_product_fournisseur_price($rowid)
Remove a price for a couple supplier-product.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
static commonReplaceThirdparty(DoliDB $db, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a thirdparty id with another one.
dol_now($mode='gmt')
Return date for now.
display_price_product_fournisseur($showunitprice=1, $showsuptitle=1, $maxlen=0, $notooltip=0, $productFournList=array())
Display price of product.
static replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages...
list_product_fournisseur_price($prodid, $sortfield='', $sortorder='')
List all supplier prices of a product.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if(!empty($conf->fournisseur->enabled)&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->societe->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if(!empty($conf->fournisseur->enabled)&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1013
fetch_product_fournisseur_price($rowid, $ignore_expression=0)
Loads the price information of a provider.
call_trigger($trigger_name, $user)
Call trigger based on this instance.
price2num($amount, $rounding='', $alreadysqlnb=0)
Function that return a number with universal decimal format (decimal separator is '...
Class to manage predefined suppliers products.