dolibarr  13.0.2
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 
59  public $price;
60  public $price_ttc;
61  public $price_min;
62  public $price_min_ttc;
63  public $price_base_type;
64  public $tva_tx;
65  public $recuperableonly;
66  public $localtax1_type;
67  public $localtax1_tx;
68  public $localtax2_type;
69  public $localtax2_tx;
70 
74  public $fk_user;
75 
76  public $lines = array();
77 
78 
84  public function __construct($db)
85  {
86  $this->db = $db;
87  }
88 
97  public function create($user, $notrigger = 0, $forceupdateaffiliate = 0)
98  {
99 
100  global $conf, $langs;
101  $error = 0;
102 
103  // Clean parameters
104 
105  if (isset($this->entity))
106  $this->entity = trim($this->entity);
107  if (isset($this->fk_product))
108  $this->fk_product = trim($this->fk_product);
109  if (isset($this->fk_soc))
110  $this->fk_soc = trim($this->fk_soc);
111  if (isset($this->price))
112  $this->price = trim($this->price);
113  if (isset($this->price_ttc))
114  $this->price_ttc = trim($this->price_ttc);
115  if (isset($this->price_min))
116  $this->price_min = trim($this->price_min);
117  if (isset($this->price_min_ttc))
118  $this->price_min_ttc = trim($this->price_min_ttc);
119  if (isset($this->price_base_type))
120  $this->price_base_type = trim($this->price_base_type);
121  if (isset($this->tva_tx))
122  $this->tva_tx = trim($this->tva_tx);
123  if (isset($this->recuperableonly))
124  $this->recuperableonly = trim($this->recuperableonly);
125  if (isset($this->localtax1_tx))
126  $this->localtax1_tx = trim($this->localtax1_tx);
127  if (isset($this->localtax2_tx))
128  $this->localtax2_tx = trim($this->localtax2_tx);
129  if (isset($this->fk_user))
130  $this->fk_user = trim($this->fk_user);
131  if (isset($this->import_key))
132  $this->import_key = trim($this->import_key);
133 
134  // Check parameters
135  // Put here code to add control on parameters values
136 
137  if ($this->price != '' || $this->price == 0) {
138  if ($this->price_base_type == 'TTC') {
139  $this->price_ttc = price2num($this->price, 'MU');
140  $this->price = price2num($this->price) / (1 + ($this->tva_tx / 100));
141  $this->price = price2num($this->price, 'MU');
142 
143  if ($this->price_min != '' || $this->price_min == 0) {
144  $this->price_min_ttc = price2num($this->price_min, 'MU');
145  $this->price_min = price2num($this->price_min) / (1 + ($this->tva_tx / 100));
146  $this->price_min = price2num($this->price_min, 'MU');
147  } else {
148  $this->price_min = 0;
149  $this->price_min_ttc = 0;
150  }
151  } else {
152  $this->price = price2num($this->price, 'MU');
153  $this->price_ttc = ($this->recuperableonly != 1) ? price2num($this->price) * (1 + ($this->tva_tx / 100)) : $this->price;
154  $this->price_ttc = price2num($this->price_ttc, 'MU');
155 
156  if ($this->price_min != '' || $this->price_min == 0) {
157  $this->price_min = price2num($this->price_min, 'MU');
158  $this->price_min_ttc = price2num($this->price_min) * (1 + ($this->tva_tx / 100));
159  $this->price_min_ttc = price2num($this->price_min_ttc, 'MU');
160  // print 'X'.$newminprice.'-'.$price_min;
161  } else {
162  $this->price_min = 0;
163  $this->price_min_ttc = 0;
164  }
165  }
166  }
167 
168  // Insert request
169  $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_customer_price(";
170  $sql .= "entity,";
171  $sql .= "datec,";
172  $sql .= "fk_product,";
173  $sql .= "fk_soc,";
174  $sql .= "price,";
175  $sql .= "price_ttc,";
176  $sql .= "price_min,";
177  $sql .= "price_min_ttc,";
178  $sql .= "price_base_type,";
179  $sql .= "default_vat_code,";
180  $sql .= "tva_tx,";
181  $sql .= "recuperableonly,";
182  $sql .= "localtax1_type,";
183  $sql .= "localtax1_tx,";
184  $sql .= "localtax2_type,";
185  $sql .= "localtax2_tx,";
186  $sql .= "fk_user,";
187  $sql .= "import_key";
188  $sql .= ") VALUES (";
189  $sql .= " ".$conf->entity.",";
190  $sql .= " '".$this->db->idate(dol_now())."',";
191  $sql .= " ".(!isset($this->fk_product) ? 'NULL' : "'".$this->db->escape($this->fk_product)."'").",";
192  $sql .= " ".(!isset($this->fk_soc) ? 'NULL' : "'".$this->db->escape($this->fk_soc)."'").",";
193  $sql .= " ".(empty($this->price) ? '0' : "'".$this->db->escape($this->price)."'").",";
194  $sql .= " ".(empty($this->price_ttc) ? '0' : "'".$this->db->escape($this->price_ttc)."'").",";
195  $sql .= " ".(empty($this->price_min) ? '0' : "'".$this->db->escape($this->price_min)."'").",";
196  $sql .= " ".(empty($this->price_min_ttc) ? '0' : "'".$this->db->escape($this->price_min_ttc)."'").",";
197  $sql .= " ".(!isset($this->price_base_type) ? 'NULL' : "'".$this->db->escape($this->price_base_type)."'").",";
198  $sql .= " ".($this->default_vat_code ? "'".$this->db->escape($this->default_vat_code)."'" : "null").",";
199  $sql .= " ".(!isset($this->tva_tx) ? 'NULL' : (empty($this->tva_tx) ? 0 : $this->tva_tx)).",";
200  $sql .= " ".(!isset($this->recuperableonly) ? 'NULL' : "'".$this->db->escape($this->recuperableonly)."'").",";
201  $sql .= " ".(empty($this->localtax1_type) ? "'0'" : "'".$this->db->escape($this->localtax1_type)."'").",";
202  $sql .= " ".(!isset($this->localtax1_tx) ? 'NULL' : (empty($this->localtax1_tx) ? 0 : $this->localtax1_tx)).",";
203  $sql .= " ".(empty($this->localtax2_type) ? "'0'" : "'".$this->db->escape($this->localtax2_type)."'").",";
204  $sql .= " ".(!isset($this->localtax2_tx) ? 'NULL' : (empty($this->localtax2_tx) ? 0 : $this->localtax2_tx)).",";
205  $sql .= " ".$user->id.",";
206  $sql .= " ".(!isset($this->import_key) ? 'NULL' : "'".$this->db->escape($this->import_key)."'")."";
207  $sql .= ")";
208 
209  $this->db->begin();
210 
211  dol_syslog(get_class($this)."::create", LOG_DEBUG);
212  $resql = $this->db->query($sql);
213  if (!$resql) {
214  $error++;
215  $this->errors [] = "Error ".$this->db->lasterror();
216  }
217 
218  if (!$error) {
219  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."product_customer_price");
220 
221  if (!$notrigger) {
222  $result = $this->call_trigger('PRODUCT_CUSTOMER_PRICE_CREATE', $user);
223  if ($result < 0) {
224  $error++;
225  }
226  }
227  }
228 
229  if (!$error) {
230  $result = $this->setPriceOnAffiliateThirdparty($user, $forceupdateaffiliate);
231  if ($result < 0) {
232  $error++;
233  }
234  }
235 
236  // Commit or rollback
237  if ($error) {
238  foreach ($this->errors as $errmsg) {
239  dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
240  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
241  }
242  $this->db->rollback();
243  return -1 * $error;
244  } else {
245  $this->db->commit();
246  return $this->id;
247  }
248  }
249 
256  public function fetch($id)
257  {
258  global $langs;
259 
260  $sql = "SELECT";
261  $sql .= " t.rowid,";
262  $sql .= " t.entity,";
263  $sql .= " t.datec,";
264  $sql .= " t.tms,";
265  $sql .= " t.fk_product,";
266  $sql .= " t.fk_soc,";
267  $sql .= " t.price,";
268  $sql .= " t.price_ttc,";
269  $sql .= " t.price_min,";
270  $sql .= " t.price_min_ttc,";
271  $sql .= " t.price_base_type,";
272  $sql .= " t.default_vat_code,";
273  $sql .= " t.tva_tx,";
274  $sql .= " t.recuperableonly,";
275  $sql .= " t.localtax1_tx,";
276  $sql .= " t.localtax2_tx,";
277  $sql .= " t.fk_user,";
278  $sql .= " t.import_key";
279 
280  $sql .= " FROM ".MAIN_DB_PREFIX."product_customer_price as t";
281  $sql .= " WHERE t.rowid = ".$id;
282 
283  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
284  $resql = $this->db->query($sql);
285  if ($resql) {
286  if ($this->db->num_rows($resql)) {
287  $obj = $this->db->fetch_object($resql);
288 
289  $this->id = $obj->rowid;
290 
291  $this->entity = $obj->entity;
292  $this->datec = $this->db->jdate($obj->datec);
293  $this->tms = $this->db->jdate($obj->tms);
294  $this->fk_product = $obj->fk_product;
295  $this->fk_soc = $obj->fk_soc;
296  $this->price = $obj->price;
297  $this->price_ttc = $obj->price_ttc;
298  $this->price_min = $obj->price_min;
299  $this->price_min_ttc = $obj->price_min_ttc;
300  $this->price_base_type = $obj->price_base_type;
301  $this->default_vat_code = $obj->default_vat_code;
302  $this->tva_tx = $obj->tva_tx;
303  $this->recuperableonly = $obj->recuperableonly;
304  $this->localtax1_tx = $obj->localtax1_tx;
305  $this->localtax2_tx = $obj->localtax2_tx;
306  $this->fk_user = $obj->fk_user;
307  $this->import_key = $obj->import_key;
308  }
309  $this->db->free($resql);
310 
311  return 1;
312  } else {
313  $this->error = "Error ".$this->db->lasterror();
314  return -1;
315  }
316  }
317 
318  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
329  public function fetch_all($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, $filter = array())
330  {
331  // phpcs:enable
332  global $langs;
333 
334  if (empty($sortfield)) $sortfield = "t.rowid";
335  if (empty($sortorder)) $sortorder = "DESC";
336 
337  $sql = "SELECT";
338  $sql .= " t.rowid,";
339  $sql .= " t.entity,";
340  $sql .= " t.datec,";
341  $sql .= " t.tms,";
342  $sql .= " t.fk_product,";
343  $sql .= " t.fk_soc,";
344  $sql .= " t.price,";
345  $sql .= " t.price_ttc,";
346  $sql .= " t.price_min,";
347  $sql .= " t.price_min_ttc,";
348  $sql .= " t.price_base_type,";
349  $sql .= " t.default_vat_code,";
350  $sql .= " t.tva_tx,";
351  $sql .= " t.recuperableonly,";
352  $sql .= " t.localtax1_tx,";
353  $sql .= " t.localtax2_tx,";
354  $sql .= " t.localtax1_type,";
355  $sql .= " t.localtax2_type,";
356  $sql .= " t.fk_user,";
357  $sql .= " t.import_key,";
358  $sql .= " soc.nom as socname,";
359  $sql .= " prod.ref as prodref";
360  $sql .= " FROM ".MAIN_DB_PREFIX."product_customer_price as t ";
361  $sql .= " ,".MAIN_DB_PREFIX."product as prod ";
362  $sql .= " ,".MAIN_DB_PREFIX."societe as soc ";
363  $sql .= " WHERE soc.rowid=t.fk_soc ";
364  $sql .= " AND prod.rowid=t.fk_product ";
365  $sql .= " AND prod.entity IN (".getEntity('product').")";
366  $sql .= " AND t.entity IN (".getEntity('productprice').")";
367 
368  // Manage filter
369  if (count($filter) > 0) {
370  foreach ($filter as $key => $value) {
371  if (strpos($key, 'date')) { // To allow $filter['YEAR(s.dated)']=>$year
372  $sql .= ' AND '.$key.' = \''.$this->db->escape($value).'\'';
373  } elseif ($key == 'soc.nom') {
374  $sql .= ' AND '.$key.' LIKE \'%'.$this->db->escape($value).'%\'';
375  } elseif ($key == 'prod.ref' || $key == 'prod.label') {
376  $sql .= ' AND '.$key.' LIKE \'%'.$this->db->escape($value).'%\'';
377  } elseif ($key == 't.price' || $key == 't.price_ttc') {
378  $sql .= ' AND '.$key.' LIKE \'%'.price2num($value).'%\'';
379  } else {
380  $sql .= ' AND '.$key.' = '.((int) $value);
381  }
382  }
383  }
384  $sql .= $this->db->order($sortfield, $sortorder);
385  if (!empty($limit)) $sql .= ' '.$this->db->plimit($limit + 1, $offset);
386 
387  dol_syslog(get_class($this)."::fetch_all", LOG_DEBUG);
388  $resql = $this->db->query($sql);
389  if ($resql) {
390  $this->lines = array();
391  $num = $this->db->num_rows($resql);
392 
393  while ($obj = $this->db->fetch_object($resql)) {
394  $line = new PriceByCustomerLine();
395 
396  $line->id = $obj->rowid;
397 
398  $line->entity = $obj->entity;
399  $line->datec = $this->db->jdate($obj->datec);
400  $line->tms = $this->db->jdate($obj->tms);
401  $line->fk_product = $obj->fk_product;
402  $line->fk_soc = $obj->fk_soc;
403  $line->price = $obj->price;
404  $line->price_ttc = $obj->price_ttc;
405  $line->price_min = $obj->price_min;
406  $line->price_min_ttc = $obj->price_min_ttc;
407  $line->price_base_type = $obj->price_base_type;
408  $line->default_vat_code = $obj->default_vat_code;
409  $line->tva_tx = $obj->tva_tx;
410  $line->recuperableonly = $obj->recuperableonly;
411  $line->localtax1_tx = $obj->localtax1_tx;
412  $line->localtax2_tx = $obj->localtax2_tx;
413  $line->localtax1_type = $obj->localtax1_type;
414  $line->localtax2_type = $obj->localtax2_type;
415  $line->fk_user = $obj->fk_user;
416  $line->import_key = $obj->import_key;
417  $line->socname = $obj->socname;
418  $line->prodref = $obj->prodref;
419 
420  $this->lines [] = $line;
421  }
422  $this->db->free($resql);
423 
424  return $num;
425  } else {
426  $this->error = "Error ".$this->db->lasterror();
427  return -1;
428  }
429  }
430 
431  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
442  public function fetch_all_log($sortorder, $sortfield, $limit, $offset, $filter = array())
443  {
444  // phpcs:enable
445  global $langs;
446 
447  if (!empty($sortfield)) $sortfield = "t.rowid";
448  if (!empty($sortorder)) $sortorder = "DESC";
449 
450  $sql = "SELECT";
451  $sql .= " t.rowid,";
452 
453  $sql .= " t.entity,";
454  $sql .= " t.datec,";
455  $sql .= " t.fk_product,";
456  $sql .= " t.fk_soc,";
457  $sql .= " t.price,";
458  $sql .= " t.price_ttc,";
459  $sql .= " t.price_min,";
460  $sql .= " t.price_min_ttc,";
461  $sql .= " t.price_base_type,";
462  $sql .= " t.default_vat_code,";
463  $sql .= " t.tva_tx,";
464  $sql .= " t.recuperableonly,";
465  $sql .= " t.localtax1_tx,";
466  $sql .= " t.localtax2_tx,";
467  $sql .= " t.fk_user,";
468  $sql .= " t.import_key,";
469  $sql .= " soc.nom as socname,";
470  $sql .= " prod.ref as prodref";
471  $sql .= " FROM ".MAIN_DB_PREFIX."product_customer_price_log as t ";
472  $sql .= " ,".MAIN_DB_PREFIX."product as prod ";
473  $sql .= " ,".MAIN_DB_PREFIX."societe as soc ";
474  $sql .= " WHERE soc.rowid=t.fk_soc ";
475  $sql .= " AND prod.rowid=t.fk_product ";
476  $sql .= " AND prod.entity IN (".getEntity('product').")";
477  $sql .= " AND t.entity IN (".getEntity('productprice').")";
478 
479  // Manage filter
480  if (count($filter) > 0) {
481  foreach ($filter as $key => $value) {
482  if (strpos($key, 'date')) // To allow $filter['YEAR(s.dated)']=>$year
483  {
484  $sql .= ' AND '.$key.' = \''.$value.'\'';
485  } elseif ($key == 'soc.nom') {
486  $sql .= ' AND '.$key.' LIKE \'%'.$value.'%\'';
487  } else {
488  $sql .= ' AND '.$key.' = '.$value;
489  }
490  }
491  }
492 
493  $sql .= $this->db->order($sortfield, $sortorder);
494  if (!empty($limit)) $sql .= ' '.$this->db->plimit($limit + 1, $offset);
495 
496  dol_syslog(get_class($this)."::fetch_all_log", LOG_DEBUG);
497  $resql = $this->db->query($sql);
498  if ($resql) {
499  $this->lines = array();
500  $num = $this->db->num_rows($resql);
501 
502  while ($obj = $this->db->fetch_object($resql)) {
503  $line = new PriceByCustomerLine();
504 
505  $line->id = $obj->rowid;
506 
507  $line->entity = $obj->entity;
508  $line->datec = $this->db->jdate($obj->datec);
509  $line->tms = $this->db->jdate($obj->tms);
510  $line->fk_product = $obj->fk_product;
511  $line->fk_soc = $obj->fk_soc;
512  $line->price = $obj->price;
513  $line->price_ttc = $obj->price_ttc;
514  $line->price_min = $obj->price_min;
515  $line->price_min_ttc = $obj->price_min_ttc;
516  $line->price_base_type = $obj->price_base_type;
517  $line->default_vat_code = $obj->default_vat_code;
518  $line->tva_tx = $obj->tva_tx;
519  $line->recuperableonly = $obj->recuperableonly;
520  $line->localtax1_tx = $obj->localtax1_tx;
521  $line->localtax2_tx = $obj->localtax2_tx;
522  $line->fk_user = $obj->fk_user;
523  $line->import_key = $obj->import_key;
524  $line->socname = $obj->socname;
525  $line->prodref = $obj->prodref;
526 
527  $this->lines [] = $line;
528  }
529  $this->db->free($resql);
530 
531  return $num;
532  } else {
533  $this->error = "Error ".$this->db->lasterror();
534  return -1;
535  }
536  }
537 
546  public function update($user = 0, $notrigger = 0, $forceupdateaffiliate = 0)
547  {
548 
549  global $conf, $langs;
550  $error = 0;
551 
552  // Clean parameters
553 
554  if (isset($this->entity))
555  $this->entity = trim($this->entity);
556  if (isset($this->fk_product))
557  $this->fk_product = trim($this->fk_product);
558  if (isset($this->fk_soc))
559  $this->fk_soc = trim($this->fk_soc);
560  if (isset($this->price))
561  $this->price = trim($this->price);
562  if (isset($this->price_ttc))
563  $this->price_ttc = trim($this->price_ttc);
564  if (isset($this->price_min))
565  $this->price_min = trim($this->price_min);
566  if (isset($this->price_min_ttc))
567  $this->price_min_ttc = trim($this->price_min_ttc);
568  if (isset($this->price_base_type))
569  $this->price_base_type = trim($this->price_base_type);
570  if (isset($this->tva_tx))
571  $this->tva_tx = trim($this->tva_tx);
572  if (isset($this->recuperableonly))
573  $this->recuperableonly = trim($this->recuperableonly);
574  if (isset($this->localtax1_tx))
575  $this->localtax1_tx = trim($this->localtax1_tx);
576  if (isset($this->localtax2_tx))
577  $this->localtax2_tx = trim($this->localtax2_tx);
578  if (isset($this->fk_user))
579  $this->fk_user = trim($this->fk_user);
580  if (isset($this->import_key))
581  $this->import_key = trim($this->import_key);
582 
583  // Check parameters
584  // Put here code to add a control on parameters values
585 
586  if ($this->price != '' || $this->price == 0) {
587  if ($this->price_base_type == 'TTC') {
588  $this->price_ttc = price2num($this->price, 'MU');
589  $this->price = price2num($this->price) / (1 + ($this->tva_tx / 100));
590  $this->price = price2num($this->price, 'MU');
591 
592  if ($this->price_min != '' || $this->price_min == 0) {
593  $this->price_min_ttc = price2num($this->price_min, 'MU');
594  $this->price_min = price2num($this->price_min) / (1 + ($this->tva_tx / 100));
595  $this->price_min = price2num($this->price_min, 'MU');
596  } else {
597  $this->price_min = 0;
598  $this->price_min_ttc = 0;
599  }
600  } else {
601  $this->price = price2num($this->price, 'MU');
602  $this->price_ttc = ($this->recuperableonly != 1) ? price2num($this->price) * (1 + ($this->tva_tx / 100)) : $this->price;
603  $this->price_ttc = price2num($this->price_ttc, 'MU');
604 
605  if ($this->price_min != '' || $this->price_min == 0) {
606  $this->price_min = price2num($this->price_min, 'MU');
607  $this->price_min_ttc = price2num($this->price_min) * (1 + ($this->tva_tx / 100));
608  $this->price_min_ttc = price2num($this->price_min_ttc, 'MU');
609  // print 'X'.$newminprice.'-'.$price_min;
610  } else {
611  $this->price_min = 0;
612  $this->price_min_ttc = 0;
613  }
614  }
615  }
616 
617  // Do a copy of current record into log table
618  // Insert request
619  $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_customer_price_log(";
620 
621  $sql .= "entity,";
622  $sql .= "datec,";
623  $sql .= "fk_product,";
624  $sql .= "fk_soc,";
625  $sql .= "price,";
626  $sql .= "price_ttc,";
627  $sql .= "price_min,";
628  $sql .= "price_min_ttc,";
629  $sql .= "price_base_type,";
630  $sql .= "default_vat_code,";
631  $sql .= "tva_tx,";
632  $sql .= "recuperableonly,";
633  $sql .= "localtax1_tx,";
634  $sql .= "localtax2_tx,";
635  $sql .= "localtax1_type,";
636  $sql .= "localtax2_type,";
637  $sql .= "fk_user,";
638  $sql .= "import_key";
639 
640  $sql .= ") ";
641  $sql .= "SELECT";
642 
643  $sql .= " t.entity,";
644  $sql .= " t.datec,";
645  $sql .= " t.fk_product,";
646  $sql .= " t.fk_soc,";
647  $sql .= " t.price,";
648  $sql .= " t.price_ttc,";
649  $sql .= " t.price_min,";
650  $sql .= " t.price_min_ttc,";
651  $sql .= " t.price_base_type,";
652  $sql .= " t.default_vat_code,";
653  $sql .= " t.tva_tx,";
654  $sql .= " t.recuperableonly,";
655  $sql .= " t.localtax1_tx,";
656  $sql .= " t.localtax2_tx,";
657  $sql .= " t.localtax1_type,";
658  $sql .= " t.localtax2_type,";
659  $sql .= " t.fk_user,";
660  $sql .= " t.import_key";
661 
662  $sql .= " FROM ".MAIN_DB_PREFIX."product_customer_price as t";
663  $sql .= " WHERE t.rowid = ".$this->id;
664 
665  $this->db->begin();
666  dol_syslog(get_class($this)."::update", LOG_DEBUG);
667  $resql = $this->db->query($sql);
668  if (!$resql) {
669  $error++;
670  $this->errors [] = "Error ".$this->db->lasterror();
671  }
672 
673  // Update request
674  $sql = "UPDATE ".MAIN_DB_PREFIX."product_customer_price SET";
675 
676  $sql .= " entity=".$conf->entity.",";
677  $sql .= " datec='".$this->db->idate(dol_now())."',";
678  $sql .= " tms=".(dol_strlen($this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
679  $sql .= " fk_product=".(isset($this->fk_product) ? $this->fk_product : "null").",";
680  $sql .= " fk_soc=".(isset($this->fk_soc) ? $this->fk_soc : "null").",";
681  $sql .= " price=".(isset($this->price) ? $this->price : "null").",";
682  $sql .= " price_ttc=".(isset($this->price_ttc) ? $this->price_ttc : "null").",";
683  $sql .= " price_min=".(isset($this->price_min) ? $this->price_min : "null").",";
684  $sql .= " price_min_ttc=".(isset($this->price_min_ttc) ? $this->price_min_ttc : "null").",";
685  $sql .= " price_base_type=".(isset($this->price_base_type) ? "'".$this->db->escape($this->price_base_type)."'" : "null").",";
686  $sql .= " default_vat_code = ".($this->default_vat_code ? "'".$this->db->escape($this->default_vat_code)."'" : "null").",";
687  $sql .= " tva_tx=".(isset($this->tva_tx) ? (empty($this->tva_tx) ? 0 : $this->tva_tx) : "null").",";
688  $sql .= " recuperableonly=".(isset($this->recuperableonly) ? $this->recuperableonly : "null").",";
689  $sql .= " localtax1_tx=".(isset($this->localtax1_tx) ? (empty($this->localtax1_tx) ? 0 : $this->localtax1_tx) : "null").",";
690  $sql .= " localtax2_tx=".(isset($this->localtax2_tx) ? (empty($this->localtax2_tx) ? 0 : $this->localtax2_tx) : "null").",";
691  $sql .= " localtax1_type=".(!empty($this->localtax1_type) ? "'".$this->db->escape($this->localtax1_type)."'" : "'0'").",";
692  $sql .= " localtax2_type=".(!empty($this->localtax2_type) ? "'".$this->db->escape($this->localtax2_type)."'" : "'0'").",";
693  $sql .= " fk_user=".$user->id.",";
694  $sql .= " import_key=".(isset($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null")."";
695 
696  $sql .= " WHERE rowid=".$this->id;
697 
698  dol_syslog(get_class($this)."::update", LOG_DEBUG);
699  $resql = $this->db->query($sql);
700  if (!$resql) {
701  $error++;
702  $this->errors [] = "Error ".$this->db->lasterror();
703  }
704 
705  if (!$error && !$notrigger)
706  {
707  // Call trigger
708  $result = $this->call_trigger('PRODUCT_CUSTOMER_PRICE_UPDATE', $user);
709  if ($result < 0) $error++;
710  // End call triggers
711  }
712 
713  if (!$error) {
714  $result = $this->setPriceOnAffiliateThirdparty($user, $forceupdateaffiliate);
715  if ($result < 0) {
716  $error++;
717  }
718  }
719 
720  // Commit or rollback
721  if ($error) {
722  foreach ($this->errors as $errmsg) {
723  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
724  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
725  }
726  $this->db->rollback();
727  return -1 * $error;
728  } else {
729  $this->db->commit();
730  return 1;
731  }
732  }
733 
741  public function setPriceOnAffiliateThirdparty($user, $forceupdateaffiliate)
742  {
743  global $conf;
744 
745  if (!empty($conf->global->PRODUCT_DISABLE_PROPAGATE_CUSTOMER_PRICES_ON_CHILD_COMPANIES)) {
746  return 0;
747  }
748 
749  $error = 0;
750 
751  // Find all susidiaries
752  $sql = "SELECT s.rowid";
753  $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
754  $sql .= " WHERE s.parent = ".$this->fk_soc;
755  $sql .= " AND s.entity IN (".getEntity('societe').")";
756 
757  dol_syslog(get_class($this)."::setPriceOnAffiliateThirdparty", LOG_DEBUG);
758  $resql = $this->db->query($sql);
759 
760  if ($resql) {
761  $this->lines = array();
762  $num = $this->db->num_rows($resql);
763 
764  while (($obj = $this->db->fetch_object($resql)) && (empty($error))) {
765  // find if there is an existing line for the product and the subsidiaries
766  $prodsocprice = new Productcustomerprice($this->db);
767 
768  $filter = array(
769  't.fk_product' => $this->fk_product, 't.fk_soc' => $obj->rowid
770  );
771 
772  $result = $prodsocprice->fetch_all('', '', 0, 0, $filter);
773  if ($result < 0) {
774  $error++;
775  $this->error = $prodsocprice->error;
776  } else {
777  // There is one line
778  if (count($prodsocprice->lines) > 0) {
779  // If force update => Update
780  if (!empty($forceupdateaffiliate)) {
781  $prodsocpriceupd = new Productcustomerprice($this->db);
782  $prodsocpriceupd->fetch($prodsocprice->lines [0]->id);
783 
784  $prodsocpriceupd->price = $this->price;
785  $prodsocpriceupd->price_min = $this->price_min;
786  $prodsocpriceupd->price_base_type = $this->price_base_type;
787  $prodsocpriceupd->tva_tx = $this->tva_tx;
788  $prodsocpriceupd->recuperableonly = $this->recuperableonly;
789 
790  $resultupd = $prodsocpriceupd->update($user, 0, $forceupdateaffiliate);
791  if ($result < 0) {
792  $error++;
793  $this->error = $prodsocpriceupd->error;
794  }
795  }
796  } else {
797  // If line do not exits then create it
798  $prodsocpricenew = new Productcustomerprice($this->db);
799  $prodsocpricenew->fk_soc = $obj->rowid;
800  $prodsocpricenew->fk_product = $this->fk_product;
801  $prodsocpricenew->price = $this->price;
802  $prodsocpricenew->price_min = $this->price_min;
803  $prodsocpricenew->price_base_type = $this->price_base_type;
804  $prodsocpricenew->tva_tx = $this->tva_tx;
805  $prodsocpricenew->recuperableonly = $this->recuperableonly;
806 
807  $resultupd = $prodsocpricenew->create($user, 0, $forceupdateaffiliate);
808  if ($result < 0) {
809  $error++;
810  $this->error = $prodsocpriceupd->error;
811  }
812  }
813  }
814  }
815  $this->db->free($resql);
816 
817  if (empty($error)) {
818  return 1;
819  } else {
820  return -1;
821  }
822  } else {
823  $this->error = "Error ".$this->db->lasterror();
824  return -1;
825  }
826  }
827 
835  public function delete($user, $notrigger = 0)
836  {
837 
838  global $conf, $langs;
839  $error = 0;
840 
841  $this->db->begin();
842 
843  if (!$error && !$notrigger) {
844  $result = $this->call_trigger('PRODUCT_CUSTOMER_PRICE_DELETE', $user);
845  if ($result < 0) {
846  $error++;
847  }
848  }
849 
850  if (!$error) {
851  $sql = "DELETE FROM ".MAIN_DB_PREFIX."product_customer_price";
852  $sql .= " WHERE rowid=".$this->id;
853 
854  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
855  $resql = $this->db->query($sql);
856  if (!$resql) {
857  $error++;
858  $this->errors [] = "Error ".$this->db->lasterror();
859  }
860  }
861 
862  // Commit or rollback
863  if ($error) {
864  foreach ($this->errors as $errmsg) {
865  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
866  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
867  }
868  $this->db->rollback();
869  return -1 * $error;
870  } else {
871  $this->db->commit();
872  return 1;
873  }
874  }
875 
883  public function createFromClone(User $user, $fromid)
884  {
885  $error = 0;
886 
887  $object = new Productcustomerprice($this->db);
888 
889  $this->db->begin();
890 
891  // Load source object
892  $object->fetch($fromid);
893  $object->id = 0;
894  $object->statut = 0;
895 
896  // Clear fields
897  // ...
898 
899  // Create clone
900  $object->context['createfromclone'] = 'createfromclone';
901  $result = $object->create($user);
902 
903  // Other options
904  if ($result < 0) {
905  $this->error = $object->error;
906  $this->errors = array_merge($this->errors, $object->errors);
907  $error++;
908  }
909 
910  if (!$error) {
911  }
912 
913  unset($object->context['createfromclone']);
914 
915  // End
916  if (!$error) {
917  $this->db->commit();
918  return $object->id;
919  } else {
920  $this->db->rollback();
921  return -1;
922  }
923  }
924 
931  public function initAsSpecimen()
932  {
933 
934  $this->id = 0;
935 
936  $this->entity = '';
937  $this->datec = '';
938  $this->tms = '';
939  $this->fk_product = '';
940  $this->fk_soc = '';
941  $this->price = '';
942  $this->price_ttc = '';
943  $this->price_min = '';
944  $this->price_min_ttc = '';
945  $this->price_base_type = '';
946  $this->default_vat_code = '';
947  $this->tva_tx = '';
948  $this->recuperableonly = '';
949  $this->localtax1_tx = '';
950  $this->localtax2_tx = '';
951  $this->fk_user = '';
952  $this->import_key = '';
953  }
954 }
955 
960 {
964  public $id;
965 
969  public $entity;
970 
971  public $datec = '';
972  public $tms = '';
973 
977  public $fk_product;
978 
982  public $fk_soc;
983 
984  public $price;
985  public $price_ttc;
986  public $price_min;
987  public $price_min_ttc;
988  public $price_base_type;
989  public $default_vat_code;
990  public $tva_tx;
991  public $recuperableonly;
992  public $localtax1_tx;
993  public $localtax2_tx;
994 
998  public $fk_user;
999 
1000  public $import_key;
1001  public $socname;
1002  public $prodref;
1003 }
File of class to manage predefined price products or services by customer.
fetch($id)
Load object in memory from the database.
fetch_all_log($sortorder, $sortfield, $limit, $offset, $filter=array())
Load all objects in memory from database.
create($user, $notrigger=0, $forceupdateaffiliate=0)
Create object into database.
dol_now($mode= 'auto')
Return date for now.
Class to manage Dolibarr users.
Definition: user.class.php:44
update($user=0, $notrigger=0, $forceupdateaffiliate=0)
Update object into database.
$conf db
API class for accounts.
Definition: inc.php:54
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...
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is &#39;...
fetch_all($sortorder= '', $sortfield= '', $limit=0, $offset=0, $filter=array())
Load all customer prices in memory from database.
File of class to manage predefined price products or services by customer lines.
setPriceOnAffiliateThirdparty($user, $forceupdateaffiliate)
Force update price on child companies so child company has same prices than parent.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
call_trigger($triggerName, $user)
Call trigger based on this instance.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->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)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
createFromClone(User $user, $fromid)
Load an object from its id and create a new one in database.
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)