dolibarr  17.0.4
productcustomerprice.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2007-2012 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2014 Florian Henry <florian.henry@open-concept.pro>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  */
18 
24 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
25 
30 {
34  public $element = 'product_customer_price';
35 
39  public $table_element = 'product_customer_price';
40 
44  public $entity;
45 
46  public $datec = '';
47  public $tms = '';
48 
52  public $fk_product;
53 
57  public $fk_soc;
58 
62  public $ref_customer;
63 
64  public $price;
65  public $price_ttc;
66  public $price_min;
67  public $price_min_ttc;
68  public $price_base_type;
69  public $tva_tx;
70  public $recuperableonly;
71  public $localtax1_type;
72  public $localtax1_tx;
73  public $localtax2_type;
74  public $localtax2_tx;
75 
79  public $fk_user;
80 
81  public $lines = array();
82 
83 
89  public function __construct($db)
90  {
91  $this->db = $db;
92  }
93 
102  public function create($user, $notrigger = 0, $forceupdateaffiliate = 0)
103  {
104 
105  global $conf, $langs;
106  $error = 0;
107 
108  // Clean parameters
109 
110  if (isset($this->entity)) {
111  $this->entity = trim($this->entity);
112  }
113  if (isset($this->fk_product)) {
114  $this->fk_product = trim($this->fk_product);
115  }
116  if (isset($this->fk_soc)) {
117  $this->fk_soc = trim($this->fk_soc);
118  }
119  if (isset($this->ref_customer)) {
120  $this->ref_customer = trim($this->ref_customer);
121  }
122  if (isset($this->price)) {
123  $this->price = trim($this->price);
124  }
125  if (isset($this->price_ttc)) {
126  $this->price_ttc = trim($this->price_ttc);
127  }
128  if (isset($this->price_min)) {
129  $this->price_min = trim($this->price_min);
130  }
131  if (isset($this->price_min_ttc)) {
132  $this->price_min_ttc = trim($this->price_min_ttc);
133  }
134  if (isset($this->price_base_type)) {
135  $this->price_base_type = trim($this->price_base_type);
136  }
137  if (isset($this->tva_tx)) {
138  $this->tva_tx = (float) $this->tva_tx;
139  }
140  if (isset($this->recuperableonly)) {
141  $this->recuperableonly = trim($this->recuperableonly);
142  }
143  if (isset($this->localtax1_tx)) {
144  $this->localtax1_tx = trim($this->localtax1_tx);
145  }
146  if (isset($this->localtax2_tx)) {
147  $this->localtax2_tx = trim($this->localtax2_tx);
148  }
149  if (isset($this->fk_user)) {
150  $this->fk_user = trim($this->fk_user);
151  }
152  if (isset($this->import_key)) {
153  $this->import_key = trim($this->import_key);
154  }
155 
156  // Check parameters
157  // Put here code to add control on parameters values
158 
159  if ($this->price != '' || $this->price == 0) {
160  if ($this->price_base_type == 'TTC') {
161  $this->price_ttc = price2num($this->price, 'MU');
162  $this->price = price2num($this->price) / (1 + ($this->tva_tx / 100));
163  $this->price = price2num($this->price, 'MU');
164 
165  if ($this->price_min != '' || $this->price_min == 0) {
166  $this->price_min_ttc = price2num($this->price_min, 'MU');
167  $this->price_min = price2num($this->price_min) / (1 + ($this->tva_tx / 100));
168  $this->price_min = price2num($this->price_min, 'MU');
169  } else {
170  $this->price_min = 0;
171  $this->price_min_ttc = 0;
172  }
173  } else {
174  $this->price = price2num($this->price, 'MU');
175  $this->price_ttc = ($this->recuperableonly != 1) ? price2num($this->price) * (1 + ($this->tva_tx / 100)) : $this->price;
176  $this->price_ttc = price2num($this->price_ttc, 'MU');
177 
178  if ($this->price_min != '' || $this->price_min == 0) {
179  $this->price_min = price2num($this->price_min, 'MU');
180  $this->price_min_ttc = price2num($this->price_min) * (1 + ($this->tva_tx / 100));
181  $this->price_min_ttc = price2num($this->price_min_ttc, 'MU');
182  // print 'X'.$newminprice.'-'.$price_min;
183  } else {
184  $this->price_min = 0;
185  $this->price_min_ttc = 0;
186  }
187  }
188  }
189 
190  // Insert request
191  $sql = "INSERT INTO ".$this->db->prefix()."product_customer_price(";
192  $sql .= "entity,";
193  $sql .= "datec,";
194  $sql .= "fk_product,";
195  $sql .= "fk_soc,";
196  $sql .= 'ref_customer,';
197  $sql .= "price,";
198  $sql .= "price_ttc,";
199  $sql .= "price_min,";
200  $sql .= "price_min_ttc,";
201  $sql .= "price_base_type,";
202  $sql .= "default_vat_code,";
203  $sql .= "tva_tx,";
204  $sql .= "recuperableonly,";
205  $sql .= "localtax1_type,";
206  $sql .= "localtax1_tx,";
207  $sql .= "localtax2_type,";
208  $sql .= "localtax2_tx,";
209  $sql .= "fk_user,";
210  $sql .= "import_key";
211  $sql .= ") VALUES (";
212  $sql .= " ".((int) $conf->entity).",";
213  $sql .= " '".$this->db->idate(dol_now())."',";
214  $sql .= " ".(!isset($this->fk_product) ? 'NULL' : "'".$this->db->escape($this->fk_product)."'").",";
215  $sql .= " ".(!isset($this->fk_soc) ? 'NULL' : "'".$this->db->escape($this->fk_soc)."'").",";
216  $sql .= " ".(!isset($this->ref_customer) ? 'NULL' : "'".$this->db->escape($this->ref_customer)."'").",";
217  $sql .= " ".(empty($this->price) ? '0' : "'".$this->db->escape($this->price)."'").",";
218  $sql .= " ".(empty($this->price_ttc) ? '0' : "'".$this->db->escape($this->price_ttc)."'").",";
219  $sql .= " ".(empty($this->price_min) ? '0' : "'".$this->db->escape($this->price_min)."'").",";
220  $sql .= " ".(empty($this->price_min_ttc) ? '0' : "'".$this->db->escape($this->price_min_ttc)."'").",";
221  $sql .= " ".(!isset($this->price_base_type) ? 'NULL' : "'".$this->db->escape($this->price_base_type)."'").",";
222  $sql .= " ".($this->default_vat_code ? "'".$this->db->escape($this->default_vat_code)."'" : "null").",";
223  $sql .= " ".(!isset($this->tva_tx) ? 'NULL' : (empty($this->tva_tx) ? 0 : $this->tva_tx)).",";
224  $sql .= " ".(!isset($this->recuperableonly) ? 'NULL' : "'".$this->db->escape($this->recuperableonly)."'").",";
225  $sql .= " ".(empty($this->localtax1_type) ? "'0'" : "'".$this->db->escape($this->localtax1_type)."'").",";
226  $sql .= " ".(!isset($this->localtax1_tx) ? 'NULL' : (empty($this->localtax1_tx) ? 0 : $this->localtax1_tx)).",";
227  $sql .= " ".(empty($this->localtax2_type) ? "'0'" : "'".$this->db->escape($this->localtax2_type)."'").",";
228  $sql .= " ".(!isset($this->localtax2_tx) ? 'NULL' : (empty($this->localtax2_tx) ? 0 : $this->localtax2_tx)).",";
229  $sql .= " ".((int) $user->id).",";
230  $sql .= " ".(!isset($this->import_key) ? 'NULL' : "'".$this->db->escape($this->import_key)."'")."";
231  $sql .= ")";
232 
233  $this->db->begin();
234 
235  dol_syslog(get_class($this)."::create", LOG_DEBUG);
236  $resql = $this->db->query($sql);
237  if (!$resql) {
238  $error++;
239  $this->errors [] = "Error ".$this->db->lasterror();
240  }
241 
242  if (!$error) {
243  $this->id = $this->db->last_insert_id($this->db->prefix()."product_customer_price");
244 
245  if (!$notrigger) {
246  $result = $this->call_trigger('PRODUCT_CUSTOMER_PRICE_CREATE', $user);
247  if ($result < 0) {
248  $error++;
249  }
250  }
251  }
252 
253  if (!$error) {
254  $result = $this->setPriceOnAffiliateThirdparty($user, $forceupdateaffiliate);
255  if ($result < 0) {
256  $error++;
257  }
258  }
259 
260  // Commit or rollback
261  if ($error) {
262  foreach ($this->errors as $errmsg) {
263  dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
264  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
265  }
266  $this->db->rollback();
267  return -1 * $error;
268  } else {
269  $this->db->commit();
270  return $this->id;
271  }
272  }
273 
280  public function fetch($id)
281  {
282  global $langs;
283 
284  $sql = "SELECT";
285  $sql .= " t.rowid,";
286  $sql .= " t.entity,";
287  $sql .= " t.datec,";
288  $sql .= " t.tms,";
289  $sql .= " t.fk_product,";
290  $sql .= " t.fk_soc,";
291  $sql .= " t.ref_customer,";
292  $sql .= " t.price,";
293  $sql .= " t.price_ttc,";
294  $sql .= " t.price_min,";
295  $sql .= " t.price_min_ttc,";
296  $sql .= " t.price_base_type,";
297  $sql .= " t.default_vat_code,";
298  $sql .= " t.tva_tx,";
299  $sql .= " t.recuperableonly,";
300  $sql .= " t.localtax1_tx,";
301  $sql .= " t.localtax2_tx,";
302  $sql .= " t.fk_user,";
303  $sql .= " t.import_key";
304  $sql .= " FROM ".$this->db->prefix()."product_customer_price as t";
305  $sql .= " WHERE t.rowid = ".((int) $id);
306 
307  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
308  $resql = $this->db->query($sql);
309  if ($resql) {
310  if ($this->db->num_rows($resql)) {
311  $obj = $this->db->fetch_object($resql);
312 
313  $this->id = $obj->rowid;
314 
315  $this->entity = $obj->entity;
316  $this->datec = $this->db->jdate($obj->datec);
317  $this->tms = $this->db->jdate($obj->tms);
318  $this->fk_product = $obj->fk_product;
319  $this->fk_soc = $obj->fk_soc;
320  $this->ref_customer = $obj->ref_customer;
321  $this->price = $obj->price;
322  $this->price_ttc = $obj->price_ttc;
323  $this->price_min = $obj->price_min;
324  $this->price_min_ttc = $obj->price_min_ttc;
325  $this->price_base_type = $obj->price_base_type;
326  $this->default_vat_code = $obj->default_vat_code;
327  $this->tva_tx = $obj->tva_tx;
328  $this->recuperableonly = $obj->recuperableonly;
329  $this->localtax1_tx = $obj->localtax1_tx;
330  $this->localtax2_tx = $obj->localtax2_tx;
331  $this->fk_user = $obj->fk_user;
332  $this->import_key = $obj->import_key;
333 
334  $this->db->free($resql);
335 
336  return 1;
337  } else {
338  $this->db->free($resql);
339 
340  return 0;
341  }
342  } else {
343  $this->error = "Error ".$this->db->lasterror();
344  return -1;
345  }
346  }
347 
348  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
360  public function fetch_all($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, $filter = array())
361  {
362  // phpcs:enable
363 
364  dol_syslog(get_class($this)."::fetch_all is deprecated, use fetchAll instead", LOG_NOTICE);
365 
366  return $this->fetchAll($sortorder, $sortfield, $limit, $offset, $filter);
367  }
368 
380  public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, $filter = array())
381  {
382  global $langs;
383 
384  if (empty($sortfield)) {
385  $sortfield = "t.rowid";
386  }
387  if (empty($sortorder)) {
388  $sortorder = "DESC";
389  }
390 
391  $sql = "SELECT";
392  $sql .= " t.rowid,";
393  $sql .= " t.entity,";
394  $sql .= " t.datec,";
395  $sql .= " t.tms,";
396  $sql .= " t.fk_product,";
397  $sql .= " t.fk_soc,";
398  $sql .= " t.ref_customer,";
399  $sql .= " t.price,";
400  $sql .= " t.price_ttc,";
401  $sql .= " t.price_min,";
402  $sql .= " t.price_min_ttc,";
403  $sql .= " t.price_base_type,";
404  $sql .= " t.default_vat_code,";
405  $sql .= " t.tva_tx,";
406  $sql .= " t.recuperableonly,";
407  $sql .= " t.localtax1_tx,";
408  $sql .= " t.localtax2_tx,";
409  $sql .= " t.localtax1_type,";
410  $sql .= " t.localtax2_type,";
411  $sql .= " t.fk_user,";
412  $sql .= " t.import_key,";
413  $sql .= " soc.nom as socname,";
414  $sql .= " prod.ref as prodref";
415  $sql .= " FROM ".$this->db->prefix()."product_customer_price as t,";
416  $sql .= " ".$this->db->prefix()."product as prod,";
417  $sql .= " ".$this->db->prefix()."societe as soc";
418  $sql .= " WHERE soc.rowid=t.fk_soc ";
419  $sql .= " AND prod.rowid=t.fk_product ";
420  $sql .= " AND prod.entity IN (".getEntity('product').")";
421  $sql .= " AND t.entity IN (".getEntity('productprice').")";
422 
423  // Manage filter
424  if (count($filter) > 0) {
425  foreach ($filter as $key => $value) {
426  if (strpos($key, 'date')) { // To allow $filter['YEAR(s.dated)']=>$year
427  $sql .= " AND ".$key." = '".$this->db->escape($value)."'";
428  } elseif ($key == 'soc.nom') {
429  $sql .= " AND ".$key." LIKE '%".$this->db->escape($value)."%'";
430  } elseif ($key == 'prod.ref' || $key == 'prod.label') {
431  $sql .= " AND ".$key." LIKE '%".$this->db->escape($value)."%'";
432  } elseif ($key == 't.price' || $key == 't.price_ttc') {
433  $sql .= " AND ".$key." LIKE '%".price2num($value)."%'";
434  } else {
435  $sql .= " AND ".$key." = ".((int) $value);
436  }
437  }
438  }
439  $sql .= $this->db->order($sortfield, $sortorder);
440  if (!empty($limit)) {
441  $sql .= $this->db->plimit($limit + 1, $offset);
442  }
443 
444  dol_syslog(get_class($this)."::fetchAll", LOG_DEBUG);
445  $resql = $this->db->query($sql);
446  if ($resql) {
447  $this->lines = array();
448  $num = $this->db->num_rows($resql);
449 
450  while ($obj = $this->db->fetch_object($resql)) {
451  $line = new PriceByCustomerLine();
452 
453  $line->id = $obj->rowid;
454 
455  $line->entity = $obj->entity;
456  $line->datec = $this->db->jdate($obj->datec);
457  $line->tms = $this->db->jdate($obj->tms);
458  $line->fk_product = $obj->fk_product;
459  $line->fk_soc = $obj->fk_soc;
460  $line->ref_customer = $obj->ref_customer;
461  $line->price = $obj->price;
462  $line->price_ttc = $obj->price_ttc;
463  $line->price_min = $obj->price_min;
464  $line->price_min_ttc = $obj->price_min_ttc;
465  $line->price_base_type = $obj->price_base_type;
466  $line->default_vat_code = $obj->default_vat_code;
467  $line->tva_tx = $obj->tva_tx;
468  $line->recuperableonly = $obj->recuperableonly;
469  $line->localtax1_tx = $obj->localtax1_tx;
470  $line->localtax2_tx = $obj->localtax2_tx;
471  $line->localtax1_type = $obj->localtax1_type;
472  $line->localtax2_type = $obj->localtax2_type;
473  $line->fk_user = $obj->fk_user;
474  $line->import_key = $obj->import_key;
475  $line->socname = $obj->socname;
476  $line->prodref = $obj->prodref;
477 
478  $this->lines[] = $line;
479  }
480  $this->db->free($resql);
481 
482  return $num;
483  } else {
484  $this->error = "Error ".$this->db->lasterror();
485  return -1;
486  }
487  }
488 
489  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
500  public function fetch_all_log($sortorder, $sortfield, $limit, $offset, $filter = array())
501  {
502  // phpcs:enable
503  global $langs;
504 
505  if (!empty($sortfield)) {
506  $sortfield = "t.rowid";
507  }
508  if (!empty($sortorder)) {
509  $sortorder = "DESC";
510  }
511 
512  $sql = "SELECT";
513  $sql .= " t.rowid,";
514  $sql .= " t.entity,";
515  $sql .= " t.datec,";
516  $sql .= " t.fk_product,";
517  $sql .= " t.fk_soc,";
518  $sql .= " t.ref_customer,";
519  $sql .= " t.price,";
520  $sql .= " t.price_ttc,";
521  $sql .= " t.price_min,";
522  $sql .= " t.price_min_ttc,";
523  $sql .= " t.price_base_type,";
524  $sql .= " t.default_vat_code,";
525  $sql .= " t.tva_tx,";
526  $sql .= " t.recuperableonly,";
527  $sql .= " t.localtax1_tx,";
528  $sql .= " t.localtax2_tx,";
529  $sql .= " t.fk_user,";
530  $sql .= " t.import_key,";
531  $sql .= " soc.nom as socname,";
532  $sql .= " prod.ref as prodref";
533  $sql .= " FROM ".$this->db->prefix()."product_customer_price_log as t";
534  $sql .= " ,".$this->db->prefix()."product as prod";
535  $sql .= " ,".$this->db->prefix()."societe as soc";
536  $sql .= " WHERE soc.rowid=t.fk_soc";
537  $sql .= " AND prod.rowid=t.fk_product ";
538  $sql .= " AND prod.entity IN (".getEntity('product').")";
539  $sql .= " AND t.entity IN (".getEntity('productprice').")";
540  // Manage filter
541  if (count($filter) > 0) {
542  foreach ($filter as $key => $value) {
543  if (strpos($key, 'date')) { // To allow $filter['YEAR(s.dated)']=>$year
544  $sql .= " AND ".$key." = '".$this->db->escape($value)."'";
545  } elseif ($key == 'soc.nom') {
546  $sql .= " AND ".$key." LIKE '%".$this->db->escape($value)."%'";
547  } else {
548  $sql .= " AND ".$key." = ".((int) $value);
549  }
550  }
551  }
552  $sql .= $this->db->order($sortfield, $sortorder);
553  if (!empty($limit)) {
554  $sql .= $this->db->plimit($limit + 1, $offset);
555  }
556 
557  dol_syslog(get_class($this)."::fetch_all_log", LOG_DEBUG);
558  $resql = $this->db->query($sql);
559  if ($resql) {
560  $this->lines = array();
561  $num = $this->db->num_rows($resql);
562 
563  while ($obj = $this->db->fetch_object($resql)) {
564  $line = new PriceByCustomerLine();
565 
566  $line->id = $obj->rowid;
567 
568  $line->entity = $obj->entity;
569  $line->datec = $this->db->jdate($obj->datec);
570  $line->tms = $this->db->jdate($obj->tms);
571  $line->fk_product = $obj->fk_product;
572  $line->fk_soc = $obj->fk_soc;
573  $line->ref_customer = $obj->ref_customer;
574  $line->price = $obj->price;
575  $line->price_ttc = $obj->price_ttc;
576  $line->price_min = $obj->price_min;
577  $line->price_min_ttc = $obj->price_min_ttc;
578  $line->price_base_type = $obj->price_base_type;
579  $line->default_vat_code = $obj->default_vat_code;
580  $line->tva_tx = $obj->tva_tx;
581  $line->recuperableonly = $obj->recuperableonly;
582  $line->localtax1_tx = $obj->localtax1_tx;
583  $line->localtax2_tx = $obj->localtax2_tx;
584  $line->fk_user = $obj->fk_user;
585  $line->import_key = $obj->import_key;
586  $line->socname = $obj->socname;
587  $line->prodref = $obj->prodref;
588 
589  $this->lines [] = $line;
590  }
591  $this->db->free($resql);
592 
593  return $num;
594  } else {
595  $this->error = "Error ".$this->db->lasterror();
596  return -1;
597  }
598  }
599 
608  public function update($user = 0, $notrigger = 0, $forceupdateaffiliate = 0)
609  {
610 
611  global $conf, $langs;
612  $error = 0;
613 
614  // Clean parameters
615 
616  if (isset($this->entity)) {
617  $this->entity = trim($this->entity);
618  }
619  if (isset($this->fk_product)) {
620  $this->fk_product = trim($this->fk_product);
621  }
622  if (isset($this->fk_soc)) {
623  $this->fk_soc = trim($this->fk_soc);
624  }
625  if (isset($this->ref_customer)) {
626  $this->ref_customer = trim($this->ref_customer);
627  }
628  if (isset($this->price)) {
629  $this->price = trim($this->price);
630  }
631  if (isset($this->price_ttc)) {
632  $this->price_ttc = trim($this->price_ttc);
633  }
634  if (isset($this->price_min)) {
635  $this->price_min = trim($this->price_min);
636  }
637  if (isset($this->price_min_ttc)) {
638  $this->price_min_ttc = trim($this->price_min_ttc);
639  }
640  if (isset($this->price_base_type)) {
641  $this->price_base_type = trim($this->price_base_type);
642  }
643  if (isset($this->tva_tx)) {
644  $this->tva_tx = (float) $this->tva_tx;
645  }
646  if (isset($this->recuperableonly)) {
647  $this->recuperableonly = trim($this->recuperableonly);
648  }
649  if (isset($this->localtax1_tx)) {
650  $this->localtax1_tx = trim($this->localtax1_tx);
651  }
652  if (isset($this->localtax2_tx)) {
653  $this->localtax2_tx = trim($this->localtax2_tx);
654  }
655  if (isset($this->fk_user)) {
656  $this->fk_user = trim($this->fk_user);
657  }
658  if (isset($this->import_key)) {
659  $this->import_key = trim($this->import_key);
660  }
661 
662  // Check parameters
663  // Put here code to add a control on parameters values
664 
665  if ($this->price != '' || $this->price == 0) {
666  if ($this->price_base_type == 'TTC') {
667  $this->price_ttc = price2num($this->price, 'MU');
668  $this->price = price2num($this->price) / (1 + ($this->tva_tx / 100));
669  $this->price = price2num($this->price, 'MU');
670 
671  if ($this->price_min != '' || $this->price_min == 0) {
672  $this->price_min_ttc = price2num($this->price_min, 'MU');
673  $this->price_min = price2num($this->price_min) / (1 + ($this->tva_tx / 100));
674  $this->price_min = price2num($this->price_min, 'MU');
675  } else {
676  $this->price_min = 0;
677  $this->price_min_ttc = 0;
678  }
679  } else {
680  $this->price = price2num($this->price, 'MU');
681  $this->price_ttc = ($this->recuperableonly != 1) ? price2num($this->price) * (1 + ($this->tva_tx / 100)) : $this->price;
682  $this->price_ttc = price2num($this->price_ttc, 'MU');
683 
684  if ($this->price_min != '' || $this->price_min == 0) {
685  $this->price_min = price2num($this->price_min, 'MU');
686  $this->price_min_ttc = price2num($this->price_min) * (1 + ($this->tva_tx / 100));
687  $this->price_min_ttc = price2num($this->price_min_ttc, 'MU');
688  // print 'X'.$newminprice.'-'.$price_min;
689  } else {
690  $this->price_min = 0;
691  $this->price_min_ttc = 0;
692  }
693  }
694  }
695 
696  // Do a copy of current record into log table
697  // Insert request
698  $sql = "INSERT INTO ".$this->db->prefix()."product_customer_price_log(";
699 
700  $sql .= "entity,";
701  $sql .= "datec,";
702  $sql .= "fk_product,";
703  $sql .= "fk_soc,";
704  $sql .= "ref_customer,";
705  $sql .= "price,";
706  $sql .= "price_ttc,";
707  $sql .= "price_min,";
708  $sql .= "price_min_ttc,";
709  $sql .= "price_base_type,";
710  $sql .= "default_vat_code,";
711  $sql .= "tva_tx,";
712  $sql .= "recuperableonly,";
713  $sql .= "localtax1_tx,";
714  $sql .= "localtax2_tx,";
715  $sql .= "localtax1_type,";
716  $sql .= "localtax2_type,";
717  $sql .= "fk_user,";
718  $sql .= "import_key";
719 
720  $sql .= ") ";
721  $sql .= "SELECT";
722 
723  $sql .= " t.entity,";
724  $sql .= " t.datec,";
725  $sql .= " t.fk_product,";
726  $sql .= " t.fk_soc,";
727  $sql .= " t.ref_customer,";
728  $sql .= " t.price,";
729  $sql .= " t.price_ttc,";
730  $sql .= " t.price_min,";
731  $sql .= " t.price_min_ttc,";
732  $sql .= " t.price_base_type,";
733  $sql .= " t.default_vat_code,";
734  $sql .= " t.tva_tx,";
735  $sql .= " t.recuperableonly,";
736  $sql .= " t.localtax1_tx,";
737  $sql .= " t.localtax2_tx,";
738  $sql .= " t.localtax1_type,";
739  $sql .= " t.localtax2_type,";
740  $sql .= " t.fk_user,";
741  $sql .= " t.import_key";
742 
743  $sql .= " FROM ".$this->db->prefix()."product_customer_price as t";
744  $sql .= " WHERE t.rowid = ".((int) $this->id);
745 
746  $this->db->begin();
747  dol_syslog(get_class($this)."::update", LOG_DEBUG);
748  $resql = $this->db->query($sql);
749  if (!$resql) {
750  $error++;
751  $this->errors [] = "Error ".$this->db->lasterror();
752  }
753 
754  // Update request
755  $sql = "UPDATE ".$this->db->prefix()."product_customer_price SET";
756 
757  $sql .= " entity=".$conf->entity.",";
758  $sql .= " datec='".$this->db->idate(dol_now())."',";
759  $sql .= " tms=".(dol_strlen($this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
760  $sql .= " fk_product=".(isset($this->fk_product) ? $this->fk_product : "null").",";
761  $sql .= " fk_soc=".(isset($this->fk_soc) ? $this->fk_soc : "null").",";
762  $sql .= " ref_customer=".(isset($this->ref_customer) ? "'".$this->db->escape($this->ref_customer)."'" : "null").",";
763  $sql .= " price=".(isset($this->price) ? $this->price : "null").",";
764  $sql .= " price_ttc=".(isset($this->price_ttc) ? $this->price_ttc : "null").",";
765  $sql .= " price_min=".(isset($this->price_min) ? $this->price_min : "null").",";
766  $sql .= " price_min_ttc=".(isset($this->price_min_ttc) ? $this->price_min_ttc : "null").",";
767  $sql .= " price_base_type=".(isset($this->price_base_type) ? "'".$this->db->escape($this->price_base_type)."'" : "null").",";
768  $sql .= " default_vat_code = ".($this->default_vat_code ? "'".$this->db->escape($this->default_vat_code)."'" : "null").",";
769  $sql .= " tva_tx=".(isset($this->tva_tx) ? (empty($this->tva_tx) ? 0 : $this->tva_tx) : "null").",";
770  $sql .= " recuperableonly=".(isset($this->recuperableonly) ? $this->recuperableonly : "null").",";
771  $sql .= " localtax1_tx=".(isset($this->localtax1_tx) ? (empty($this->localtax1_tx) ? 0 : $this->localtax1_tx) : "null").",";
772  $sql .= " localtax2_tx=".(isset($this->localtax2_tx) ? (empty($this->localtax2_tx) ? 0 : $this->localtax2_tx) : "null").",";
773  $sql .= " localtax1_type=".(!empty($this->localtax1_type) ? "'".$this->db->escape($this->localtax1_type)."'" : "'0'").",";
774  $sql .= " localtax2_type=".(!empty($this->localtax2_type) ? "'".$this->db->escape($this->localtax2_type)."'" : "'0'").",";
775  $sql .= " fk_user=".$user->id.",";
776  $sql .= " import_key=".(isset($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null")."";
777 
778  $sql .= " WHERE rowid=".((int) $this->id);
779 
780  dol_syslog(get_class($this)."::update", LOG_DEBUG);
781  $resql = $this->db->query($sql);
782  if (!$resql) {
783  $error++;
784  $this->errors [] = "Error ".$this->db->lasterror();
785  }
786 
787  if (!$error && !$notrigger) {
788  // Call trigger
789  $result = $this->call_trigger('PRODUCT_CUSTOMER_PRICE_MODIFY', $user);
790  if ($result < 0) {
791  $error++;
792  }
793  // End call triggers
794  }
795 
796  if (!$error) {
797  $result = $this->setPriceOnAffiliateThirdparty($user, $forceupdateaffiliate);
798  if ($result < 0) {
799  $error++;
800  }
801  }
802 
803  // Commit or rollback
804  if ($error) {
805  foreach ($this->errors as $errmsg) {
806  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
807  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
808  }
809  $this->db->rollback();
810  return -1 * $error;
811  } else {
812  $this->db->commit();
813  return 1;
814  }
815  }
816 
824  public function setPriceOnAffiliateThirdparty($user, $forceupdateaffiliate)
825  {
826  global $conf;
827 
828  if (!empty($conf->global->PRODUCT_DISABLE_PROPAGATE_CUSTOMER_PRICES_ON_CHILD_COMPANIES)) {
829  return 0;
830  }
831 
832  $error = 0;
833 
834  // Find all susidiaries
835  $sql = "SELECT s.rowid";
836  $sql .= " FROM ".$this->db->prefix()."societe as s";
837  $sql .= " WHERE s.parent = ".((int) $this->fk_soc);
838  $sql .= " AND s.entity IN (".getEntity('societe').")";
839 
840  dol_syslog(get_class($this)."::setPriceOnAffiliateThirdparty", LOG_DEBUG);
841  $resql = $this->db->query($sql);
842 
843  if ($resql) {
844  $this->lines = array();
845  $num = $this->db->num_rows($resql);
846 
847  while (($obj = $this->db->fetch_object($resql)) && (empty($error))) {
848  // find if there is an existing line for the product and the subsidiaries
849  $prodsocprice = new Productcustomerprice($this->db);
850 
851  $filter = array(
852  't.fk_product' => $this->fk_product, 't.fk_soc' => $obj->rowid
853  );
854 
855  $result = $prodsocprice->fetchAll('', '', 0, 0, $filter);
856  if ($result < 0) {
857  $error++;
858  $this->error = $prodsocprice->error;
859  } else {
860  // There is one line
861  if (count($prodsocprice->lines) > 0) {
862  // If force update => Update
863  if (!empty($forceupdateaffiliate)) {
864  $prodsocpriceupd = new Productcustomerprice($this->db);
865  $prodsocpriceupd->fetch($prodsocprice->lines [0]->id);
866 
867  $prodsocpriceupd->price = $this->price;
868  $prodsocpriceupd->price_min = $this->price_min;
869  $prodsocpriceupd->price_base_type = $this->price_base_type;
870  $prodsocpriceupd->tva_tx = $this->tva_tx;
871  $prodsocpriceupd->recuperableonly = $this->recuperableonly;
872 
873  $resultupd = $prodsocpriceupd->update($user, 0, $forceupdateaffiliate);
874  if ($result < 0) {
875  $error++;
876  $this->error = $prodsocpriceupd->error;
877  }
878  }
879  } else {
880  // If line do not exits then create it
881  $prodsocpricenew = new Productcustomerprice($this->db);
882  $prodsocpricenew->fk_soc = $obj->rowid;
883  $prodsocpricenew->ref_customer = $obj->ref_customer;
884  $prodsocpricenew->fk_product = $this->fk_product;
885  $prodsocpricenew->price = $this->price;
886  $prodsocpricenew->price_min = $this->price_min;
887  $prodsocpricenew->price_base_type = $this->price_base_type;
888  $prodsocpricenew->tva_tx = $this->tva_tx;
889  $prodsocpricenew->recuperableonly = $this->recuperableonly;
890 
891  $resultupd = $prodsocpricenew->create($user, 0, $forceupdateaffiliate);
892  if ($result < 0) {
893  $error++;
894  $this->error = $prodsocpricenew->error;
895  }
896  }
897  }
898  }
899  $this->db->free($resql);
900 
901  if (empty($error)) {
902  return 1;
903  } else {
904  return -1;
905  }
906  } else {
907  $this->error = "Error ".$this->db->lasterror();
908  return -1;
909  }
910  }
911 
919  public function delete($user, $notrigger = 0)
920  {
921  global $conf, $langs;
922  $error = 0;
923 
924  $this->db->begin();
925 
926  if (!$error && !$notrigger) {
927  $result = $this->call_trigger('PRODUCT_CUSTOMER_PRICE_DELETE', $user);
928  if ($result < 0) {
929  $error++;
930  }
931  }
932 
933  if (!$error) {
934  $sql = "DELETE FROM ".$this->db->prefix()."product_customer_price";
935  $sql .= " WHERE rowid=".((int) $this->id);
936 
937  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
938  $resql = $this->db->query($sql);
939  if (!$resql) {
940  $error++;
941  $this->errors [] = "Error ".$this->db->lasterror();
942  }
943  }
944 
945  // Commit or rollback
946  if ($error) {
947  foreach ($this->errors as $errmsg) {
948  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
949  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
950  }
951  $this->db->rollback();
952  return -1 * $error;
953  } else {
954  $this->db->commit();
955  return 1;
956  }
957  }
958 
966  public function createFromClone(User $user, $fromid)
967  {
968  $error = 0;
969 
970  $object = new Productcustomerprice($this->db);
971 
972  $this->db->begin();
973 
974  // Load source object
975  $object->fetch($fromid);
976  $object->id = 0;
977  $object->statut = 0;
978 
979  // Clear fields
980  // ...
981 
982  // Create clone
983  $object->context['createfromclone'] = 'createfromclone';
984  $result = $object->create($user);
985 
986  // Other options
987  if ($result < 0) {
988  $this->error = $object->error;
989  $this->errors = array_merge($this->errors, $object->errors);
990  $error++;
991  }
992 
993  if (!$error) {
994  }
995 
996  unset($object->context['createfromclone']);
997 
998  // End
999  if (!$error) {
1000  $this->db->commit();
1001  return $object->id;
1002  } else {
1003  $this->db->rollback();
1004  return -1;
1005  }
1006  }
1007 
1014  public function initAsSpecimen()
1015  {
1016 
1017  $this->id = 0;
1018 
1019  $this->entity = '';
1020  $this->datec = '';
1021  $this->tms = '';
1022  $this->fk_product = '';
1023  $this->fk_soc = '';
1024  $this->ref_customer = '';
1025  $this->price = '';
1026  $this->price_ttc = '';
1027  $this->price_min = '';
1028  $this->price_min_ttc = '';
1029  $this->price_base_type = '';
1030  $this->default_vat_code = '';
1031  $this->tva_tx = '';
1032  $this->recuperableonly = '';
1033  $this->localtax1_tx = '';
1034  $this->localtax2_tx = '';
1035  $this->fk_user = '';
1036  $this->import_key = '';
1037  }
1038 }
1039 
1044 {
1048  public $id;
1049 
1053  public $entity;
1054 
1055  public $datec = '';
1056  public $tms = '';
1057 
1061  public $fk_product;
1062 
1066  public $ref_customer;
1067 
1071  public $fk_soc;
1072 
1073  public $price;
1074  public $price_ttc;
1075  public $price_min;
1076  public $price_min_ttc;
1077  public $price_base_type;
1078  public $default_vat_code;
1079  public $tva_tx;
1080  public $recuperableonly;
1081  public $localtax1_tx;
1082  public $localtax2_tx;
1083 
1087  public $fk_user;
1088 
1089  public $import_key;
1090  public $socname;
1091  public $prodref;
1092 }
Parent class of all other business classes (invoices, contracts, proposals, orders,...
call_trigger($triggerName, $user)
Call trigger based on this instance.
File of class to manage predefined price products or services by customer lines.
File of class to manage predefined price products or services by customer.
fetch_all($sortorder='', $sortfield='', $limit=0, $offset=0, $filter=array())
Load all customer prices in memory from database.
fetch($id)
Load object in memory from the database.
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
createFromClone(User $user, $fromid)
Load an object from its id and create a new one in database.
update($user=0, $notrigger=0, $forceupdateaffiliate=0)
Update object into database.
fetchAll($sortorder='', $sortfield='', $limit=0, $offset=0, $filter=array())
Load all customer prices in memory from database.
setPriceOnAffiliateThirdparty($user, $forceupdateaffiliate)
Force update price on child companies so child company has same prices than parent.
create($user, $notrigger=0, $forceupdateaffiliate=0)
Create object into database.
fetch_all_log($sortorder, $sortfield, $limit, $offset, $filter=array())
Load all objects in memory from database.
Class to manage Dolibarr users.
Definition: user.class.php:47
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:745
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
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.
dol_now($mode='auto')
Return date for now.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
div float
Buy price without taxes.
Definition: style.css.php:913
$conf db
API class for accounts.
Definition: inc.php:41