dolibarr  16.0.5
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
359  public function fetch_all($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, $filter = array())
360  {
361  // phpcs:enable
362  global $langs;
363 
364  if (empty($sortfield)) {
365  $sortfield = "t.rowid";
366  }
367  if (empty($sortorder)) {
368  $sortorder = "DESC";
369  }
370 
371  $sql = "SELECT";
372  $sql .= " t.rowid,";
373  $sql .= " t.entity,";
374  $sql .= " t.datec,";
375  $sql .= " t.tms,";
376  $sql .= " t.fk_product,";
377  $sql .= " t.fk_soc,";
378  $sql .= " t.ref_customer,";
379  $sql .= " t.price,";
380  $sql .= " t.price_ttc,";
381  $sql .= " t.price_min,";
382  $sql .= " t.price_min_ttc,";
383  $sql .= " t.price_base_type,";
384  $sql .= " t.default_vat_code,";
385  $sql .= " t.tva_tx,";
386  $sql .= " t.recuperableonly,";
387  $sql .= " t.localtax1_tx,";
388  $sql .= " t.localtax2_tx,";
389  $sql .= " t.localtax1_type,";
390  $sql .= " t.localtax2_type,";
391  $sql .= " t.fk_user,";
392  $sql .= " t.import_key,";
393  $sql .= " soc.nom as socname,";
394  $sql .= " prod.ref as prodref";
395  $sql .= " FROM ".$this->db->prefix()."product_customer_price as t,";
396  $sql .= " ".$this->db->prefix()."product as prod,";
397  $sql .= " ".$this->db->prefix()."societe as soc";
398  $sql .= " WHERE soc.rowid=t.fk_soc ";
399  $sql .= " AND prod.rowid=t.fk_product ";
400  $sql .= " AND prod.entity IN (".getEntity('product').")";
401  $sql .= " AND t.entity IN (".getEntity('productprice').")";
402 
403  // Manage filter
404  if (count($filter) > 0) {
405  foreach ($filter as $key => $value) {
406  if (strpos($key, 'date')) { // To allow $filter['YEAR(s.dated)']=>$year
407  $sql .= " AND ".$key." = '".$this->db->escape($value)."'";
408  } elseif ($key == 'soc.nom') {
409  $sql .= " AND ".$key." LIKE '%".$this->db->escape($value)."%'";
410  } elseif ($key == 'prod.ref' || $key == 'prod.label') {
411  $sql .= " AND ".$key." LIKE '%".$this->db->escape($value)."%'";
412  } elseif ($key == 't.price' || $key == 't.price_ttc') {
413  $sql .= " AND ".$key." LIKE '%".price2num($value)."%'";
414  } else {
415  $sql .= " AND ".$key." = ".((int) $value);
416  }
417  }
418  }
419  $sql .= $this->db->order($sortfield, $sortorder);
420  if (!empty($limit)) {
421  $sql .= $this->db->plimit($limit + 1, $offset);
422  }
423 
424  dol_syslog(get_class($this)."::fetch_all", LOG_DEBUG);
425  $resql = $this->db->query($sql);
426  if ($resql) {
427  $this->lines = array();
428  $num = $this->db->num_rows($resql);
429 
430  while ($obj = $this->db->fetch_object($resql)) {
431  $line = new PriceByCustomerLine();
432 
433  $line->id = $obj->rowid;
434 
435  $line->entity = $obj->entity;
436  $line->datec = $this->db->jdate($obj->datec);
437  $line->tms = $this->db->jdate($obj->tms);
438  $line->fk_product = $obj->fk_product;
439  $line->fk_soc = $obj->fk_soc;
440  $line->ref_customer = $obj->ref_customer;
441  $line->price = $obj->price;
442  $line->price_ttc = $obj->price_ttc;
443  $line->price_min = $obj->price_min;
444  $line->price_min_ttc = $obj->price_min_ttc;
445  $line->price_base_type = $obj->price_base_type;
446  $line->default_vat_code = $obj->default_vat_code;
447  $line->tva_tx = $obj->tva_tx;
448  $line->recuperableonly = $obj->recuperableonly;
449  $line->localtax1_tx = $obj->localtax1_tx;
450  $line->localtax2_tx = $obj->localtax2_tx;
451  $line->localtax1_type = $obj->localtax1_type;
452  $line->localtax2_type = $obj->localtax2_type;
453  $line->fk_user = $obj->fk_user;
454  $line->import_key = $obj->import_key;
455  $line->socname = $obj->socname;
456  $line->prodref = $obj->prodref;
457 
458  $this->lines[] = $line;
459  }
460  $this->db->free($resql);
461 
462  return $num;
463  } else {
464  $this->error = "Error ".$this->db->lasterror();
465  return -1;
466  }
467  }
468 
469  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
480  public function fetch_all_log($sortorder, $sortfield, $limit, $offset, $filter = array())
481  {
482  // phpcs:enable
483  global $langs;
484 
485  if (!empty($sortfield)) {
486  $sortfield = "t.rowid";
487  }
488  if (!empty($sortorder)) {
489  $sortorder = "DESC";
490  }
491 
492  $sql = "SELECT";
493  $sql .= " t.rowid,";
494  $sql .= " t.entity,";
495  $sql .= " t.datec,";
496  $sql .= " t.fk_product,";
497  $sql .= " t.fk_soc,";
498  $sql .= " t.ref_customer,";
499  $sql .= " t.price,";
500  $sql .= " t.price_ttc,";
501  $sql .= " t.price_min,";
502  $sql .= " t.price_min_ttc,";
503  $sql .= " t.price_base_type,";
504  $sql .= " t.default_vat_code,";
505  $sql .= " t.tva_tx,";
506  $sql .= " t.recuperableonly,";
507  $sql .= " t.localtax1_tx,";
508  $sql .= " t.localtax2_tx,";
509  $sql .= " t.fk_user,";
510  $sql .= " t.import_key,";
511  $sql .= " soc.nom as socname,";
512  $sql .= " prod.ref as prodref";
513  $sql .= " FROM ".$this->db->prefix()."product_customer_price_log as t";
514  $sql .= " ,".$this->db->prefix()."product as prod";
515  $sql .= " ,".$this->db->prefix()."societe as soc";
516  $sql .= " WHERE soc.rowid=t.fk_soc";
517  $sql .= " AND prod.rowid=t.fk_product ";
518  $sql .= " AND prod.entity IN (".getEntity('product').")";
519  $sql .= " AND t.entity IN (".getEntity('productprice').")";
520  // Manage filter
521  if (count($filter) > 0) {
522  foreach ($filter as $key => $value) {
523  if (strpos($key, 'date')) { // To allow $filter['YEAR(s.dated)']=>$year
524  $sql .= " AND ".$key." = '".$this->db->escape($value)."'";
525  } elseif ($key == 'soc.nom') {
526  $sql .= " AND ".$key." LIKE '%".$this->db->escape($value)."%'";
527  } else {
528  $sql .= " AND ".$key." = ".((int) $value);
529  }
530  }
531  }
532  $sql .= $this->db->order($sortfield, $sortorder);
533  if (!empty($limit)) {
534  $sql .= $this->db->plimit($limit + 1, $offset);
535  }
536 
537  dol_syslog(get_class($this)."::fetch_all_log", LOG_DEBUG);
538  $resql = $this->db->query($sql);
539  if ($resql) {
540  $this->lines = array();
541  $num = $this->db->num_rows($resql);
542 
543  while ($obj = $this->db->fetch_object($resql)) {
544  $line = new PriceByCustomerLine();
545 
546  $line->id = $obj->rowid;
547 
548  $line->entity = $obj->entity;
549  $line->datec = $this->db->jdate($obj->datec);
550  $line->tms = $this->db->jdate($obj->tms);
551  $line->fk_product = $obj->fk_product;
552  $line->fk_soc = $obj->fk_soc;
553  $line->ref_customer = $obj->ref_customer;
554  $line->price = $obj->price;
555  $line->price_ttc = $obj->price_ttc;
556  $line->price_min = $obj->price_min;
557  $line->price_min_ttc = $obj->price_min_ttc;
558  $line->price_base_type = $obj->price_base_type;
559  $line->default_vat_code = $obj->default_vat_code;
560  $line->tva_tx = $obj->tva_tx;
561  $line->recuperableonly = $obj->recuperableonly;
562  $line->localtax1_tx = $obj->localtax1_tx;
563  $line->localtax2_tx = $obj->localtax2_tx;
564  $line->fk_user = $obj->fk_user;
565  $line->import_key = $obj->import_key;
566  $line->socname = $obj->socname;
567  $line->prodref = $obj->prodref;
568 
569  $this->lines [] = $line;
570  }
571  $this->db->free($resql);
572 
573  return $num;
574  } else {
575  $this->error = "Error ".$this->db->lasterror();
576  return -1;
577  }
578  }
579 
588  public function update($user = 0, $notrigger = 0, $forceupdateaffiliate = 0)
589  {
590 
591  global $conf, $langs;
592  $error = 0;
593 
594  // Clean parameters
595 
596  if (isset($this->entity)) {
597  $this->entity = trim($this->entity);
598  }
599  if (isset($this->fk_product)) {
600  $this->fk_product = trim($this->fk_product);
601  }
602  if (isset($this->fk_soc)) {
603  $this->fk_soc = trim($this->fk_soc);
604  }
605  if (isset($this->ref_customer)) {
606  $this->ref_customer = trim($this->ref_customer);
607  }
608  if (isset($this->price)) {
609  $this->price = trim($this->price);
610  }
611  if (isset($this->price_ttc)) {
612  $this->price_ttc = trim($this->price_ttc);
613  }
614  if (isset($this->price_min)) {
615  $this->price_min = trim($this->price_min);
616  }
617  if (isset($this->price_min_ttc)) {
618  $this->price_min_ttc = trim($this->price_min_ttc);
619  }
620  if (isset($this->price_base_type)) {
621  $this->price_base_type = trim($this->price_base_type);
622  }
623  if (isset($this->tva_tx)) {
624  $this->tva_tx = (float) $this->tva_tx;
625  }
626  if (isset($this->recuperableonly)) {
627  $this->recuperableonly = trim($this->recuperableonly);
628  }
629  if (isset($this->localtax1_tx)) {
630  $this->localtax1_tx = trim($this->localtax1_tx);
631  }
632  if (isset($this->localtax2_tx)) {
633  $this->localtax2_tx = trim($this->localtax2_tx);
634  }
635  if (isset($this->fk_user)) {
636  $this->fk_user = trim($this->fk_user);
637  }
638  if (isset($this->import_key)) {
639  $this->import_key = trim($this->import_key);
640  }
641 
642  // Check parameters
643  // Put here code to add a control on parameters values
644 
645  if ($this->price != '' || $this->price == 0) {
646  if ($this->price_base_type == 'TTC') {
647  $this->price_ttc = price2num($this->price, 'MU');
648  $this->price = price2num($this->price) / (1 + ($this->tva_tx / 100));
649  $this->price = price2num($this->price, 'MU');
650 
651  if ($this->price_min != '' || $this->price_min == 0) {
652  $this->price_min_ttc = price2num($this->price_min, 'MU');
653  $this->price_min = price2num($this->price_min) / (1 + ($this->tva_tx / 100));
654  $this->price_min = price2num($this->price_min, 'MU');
655  } else {
656  $this->price_min = 0;
657  $this->price_min_ttc = 0;
658  }
659  } else {
660  $this->price = price2num($this->price, 'MU');
661  $this->price_ttc = ($this->recuperableonly != 1) ? price2num($this->price) * (1 + ($this->tva_tx / 100)) : $this->price;
662  $this->price_ttc = price2num($this->price_ttc, 'MU');
663 
664  if ($this->price_min != '' || $this->price_min == 0) {
665  $this->price_min = price2num($this->price_min, 'MU');
666  $this->price_min_ttc = price2num($this->price_min) * (1 + ($this->tva_tx / 100));
667  $this->price_min_ttc = price2num($this->price_min_ttc, 'MU');
668  // print 'X'.$newminprice.'-'.$price_min;
669  } else {
670  $this->price_min = 0;
671  $this->price_min_ttc = 0;
672  }
673  }
674  }
675 
676  // Do a copy of current record into log table
677  // Insert request
678  $sql = "INSERT INTO ".$this->db->prefix()."product_customer_price_log(";
679 
680  $sql .= "entity,";
681  $sql .= "datec,";
682  $sql .= "fk_product,";
683  $sql .= "fk_soc,";
684  $sql .= "ref_customer,";
685  $sql .= "price,";
686  $sql .= "price_ttc,";
687  $sql .= "price_min,";
688  $sql .= "price_min_ttc,";
689  $sql .= "price_base_type,";
690  $sql .= "default_vat_code,";
691  $sql .= "tva_tx,";
692  $sql .= "recuperableonly,";
693  $sql .= "localtax1_tx,";
694  $sql .= "localtax2_tx,";
695  $sql .= "localtax1_type,";
696  $sql .= "localtax2_type,";
697  $sql .= "fk_user,";
698  $sql .= "import_key";
699 
700  $sql .= ") ";
701  $sql .= "SELECT";
702 
703  $sql .= " t.entity,";
704  $sql .= " t.datec,";
705  $sql .= " t.fk_product,";
706  $sql .= " t.fk_soc,";
707  $sql .= " t.ref_customer,";
708  $sql .= " t.price,";
709  $sql .= " t.price_ttc,";
710  $sql .= " t.price_min,";
711  $sql .= " t.price_min_ttc,";
712  $sql .= " t.price_base_type,";
713  $sql .= " t.default_vat_code,";
714  $sql .= " t.tva_tx,";
715  $sql .= " t.recuperableonly,";
716  $sql .= " t.localtax1_tx,";
717  $sql .= " t.localtax2_tx,";
718  $sql .= " t.localtax1_type,";
719  $sql .= " t.localtax2_type,";
720  $sql .= " t.fk_user,";
721  $sql .= " t.import_key";
722 
723  $sql .= " FROM ".$this->db->prefix()."product_customer_price as t";
724  $sql .= " WHERE t.rowid = ".((int) $this->id);
725 
726  $this->db->begin();
727  dol_syslog(get_class($this)."::update", LOG_DEBUG);
728  $resql = $this->db->query($sql);
729  if (!$resql) {
730  $error++;
731  $this->errors [] = "Error ".$this->db->lasterror();
732  }
733 
734  // Update request
735  $sql = "UPDATE ".$this->db->prefix()."product_customer_price SET";
736 
737  $sql .= " entity=".$conf->entity.",";
738  $sql .= " datec='".$this->db->idate(dol_now())."',";
739  $sql .= " tms=".(dol_strlen($this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
740  $sql .= " fk_product=".(isset($this->fk_product) ? $this->fk_product : "null").",";
741  $sql .= " fk_soc=".(isset($this->fk_soc) ? $this->fk_soc : "null").",";
742  $sql .= " ref_customer=".(isset($this->ref_customer) ? "'".$this->db->escape($this->ref_customer)."'" : "null").",";
743  $sql .= " price=".(isset($this->price) ? $this->price : "null").",";
744  $sql .= " price_ttc=".(isset($this->price_ttc) ? $this->price_ttc : "null").",";
745  $sql .= " price_min=".(isset($this->price_min) ? $this->price_min : "null").",";
746  $sql .= " price_min_ttc=".(isset($this->price_min_ttc) ? $this->price_min_ttc : "null").",";
747  $sql .= " price_base_type=".(isset($this->price_base_type) ? "'".$this->db->escape($this->price_base_type)."'" : "null").",";
748  $sql .= " default_vat_code = ".($this->default_vat_code ? "'".$this->db->escape($this->default_vat_code)."'" : "null").",";
749  $sql .= " tva_tx=".(isset($this->tva_tx) ? (empty($this->tva_tx) ? 0 : $this->tva_tx) : "null").",";
750  $sql .= " recuperableonly=".(isset($this->recuperableonly) ? $this->recuperableonly : "null").",";
751  $sql .= " localtax1_tx=".(isset($this->localtax1_tx) ? (empty($this->localtax1_tx) ? 0 : $this->localtax1_tx) : "null").",";
752  $sql .= " localtax2_tx=".(isset($this->localtax2_tx) ? (empty($this->localtax2_tx) ? 0 : $this->localtax2_tx) : "null").",";
753  $sql .= " localtax1_type=".(!empty($this->localtax1_type) ? "'".$this->db->escape($this->localtax1_type)."'" : "'0'").",";
754  $sql .= " localtax2_type=".(!empty($this->localtax2_type) ? "'".$this->db->escape($this->localtax2_type)."'" : "'0'").",";
755  $sql .= " fk_user=".$user->id.",";
756  $sql .= " import_key=".(isset($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null")."";
757 
758  $sql .= " WHERE rowid=".((int) $this->id);
759 
760  dol_syslog(get_class($this)."::update", LOG_DEBUG);
761  $resql = $this->db->query($sql);
762  if (!$resql) {
763  $error++;
764  $this->errors [] = "Error ".$this->db->lasterror();
765  }
766 
767  if (!$error && !$notrigger) {
768  // Call trigger
769  $result = $this->call_trigger('PRODUCT_CUSTOMER_PRICE_MODIFY', $user);
770  if ($result < 0) {
771  $error++;
772  }
773  // End call triggers
774  }
775 
776  if (!$error) {
777  $result = $this->setPriceOnAffiliateThirdparty($user, $forceupdateaffiliate);
778  if ($result < 0) {
779  $error++;
780  }
781  }
782 
783  // Commit or rollback
784  if ($error) {
785  foreach ($this->errors as $errmsg) {
786  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
787  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
788  }
789  $this->db->rollback();
790  return -1 * $error;
791  } else {
792  $this->db->commit();
793  return 1;
794  }
795  }
796 
804  public function setPriceOnAffiliateThirdparty($user, $forceupdateaffiliate)
805  {
806  global $conf;
807 
808  if (!empty($conf->global->PRODUCT_DISABLE_PROPAGATE_CUSTOMER_PRICES_ON_CHILD_COMPANIES)) {
809  return 0;
810  }
811 
812  $error = 0;
813 
814  // Find all susidiaries
815  $sql = "SELECT s.rowid";
816  $sql .= " FROM ".$this->db->prefix()."societe as s";
817  $sql .= " WHERE s.parent = ".((int) $this->fk_soc);
818  $sql .= " AND s.entity IN (".getEntity('societe').")";
819 
820  dol_syslog(get_class($this)."::setPriceOnAffiliateThirdparty", LOG_DEBUG);
821  $resql = $this->db->query($sql);
822 
823  if ($resql) {
824  $this->lines = array();
825  $num = $this->db->num_rows($resql);
826 
827  while (($obj = $this->db->fetch_object($resql)) && (empty($error))) {
828  // find if there is an existing line for the product and the subsidiaries
829  $prodsocprice = new Productcustomerprice($this->db);
830 
831  $filter = array(
832  't.fk_product' => $this->fk_product, 't.fk_soc' => $obj->rowid
833  );
834 
835  $result = $prodsocprice->fetch_all('', '', 0, 0, $filter);
836  if ($result < 0) {
837  $error++;
838  $this->error = $prodsocprice->error;
839  } else {
840  // There is one line
841  if (count($prodsocprice->lines) > 0) {
842  // If force update => Update
843  if (!empty($forceupdateaffiliate)) {
844  $prodsocpriceupd = new Productcustomerprice($this->db);
845  $prodsocpriceupd->fetch($prodsocprice->lines [0]->id);
846 
847  $prodsocpriceupd->price = $this->price;
848  $prodsocpriceupd->price_min = $this->price_min;
849  $prodsocpriceupd->price_base_type = $this->price_base_type;
850  $prodsocpriceupd->tva_tx = $this->tva_tx;
851  $prodsocpriceupd->recuperableonly = $this->recuperableonly;
852 
853  $resultupd = $prodsocpriceupd->update($user, 0, $forceupdateaffiliate);
854  if ($result < 0) {
855  $error++;
856  $this->error = $prodsocpriceupd->error;
857  }
858  }
859  } else {
860  // If line do not exits then create it
861  $prodsocpricenew = new Productcustomerprice($this->db);
862  $prodsocpricenew->fk_soc = $obj->rowid;
863  $prodsocpricenew->ref_customer = $obj->ref_customer;
864  $prodsocpricenew->fk_product = $this->fk_product;
865  $prodsocpricenew->price = $this->price;
866  $prodsocpricenew->price_min = $this->price_min;
867  $prodsocpricenew->price_base_type = $this->price_base_type;
868  $prodsocpricenew->tva_tx = $this->tva_tx;
869  $prodsocpricenew->recuperableonly = $this->recuperableonly;
870 
871  $resultupd = $prodsocpricenew->create($user, 0, $forceupdateaffiliate);
872  if ($result < 0) {
873  $error++;
874  $this->error = $prodsocpricenew->error;
875  }
876  }
877  }
878  }
879  $this->db->free($resql);
880 
881  if (empty($error)) {
882  return 1;
883  } else {
884  return -1;
885  }
886  } else {
887  $this->error = "Error ".$this->db->lasterror();
888  return -1;
889  }
890  }
891 
899  public function delete($user, $notrigger = 0)
900  {
901  global $conf, $langs;
902  $error = 0;
903 
904  $this->db->begin();
905 
906  if (!$error && !$notrigger) {
907  $result = $this->call_trigger('PRODUCT_CUSTOMER_PRICE_DELETE', $user);
908  if ($result < 0) {
909  $error++;
910  }
911  }
912 
913  if (!$error) {
914  $sql = "DELETE FROM ".$this->db->prefix()."product_customer_price";
915  $sql .= " WHERE rowid=".((int) $this->id);
916 
917  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
918  $resql = $this->db->query($sql);
919  if (!$resql) {
920  $error++;
921  $this->errors [] = "Error ".$this->db->lasterror();
922  }
923  }
924 
925  // Commit or rollback
926  if ($error) {
927  foreach ($this->errors as $errmsg) {
928  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
929  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
930  }
931  $this->db->rollback();
932  return -1 * $error;
933  } else {
934  $this->db->commit();
935  return 1;
936  }
937  }
938 
946  public function createFromClone(User $user, $fromid)
947  {
948  $error = 0;
949 
950  $object = new Productcustomerprice($this->db);
951 
952  $this->db->begin();
953 
954  // Load source object
955  $object->fetch($fromid);
956  $object->id = 0;
957  $object->statut = 0;
958 
959  // Clear fields
960  // ...
961 
962  // Create clone
963  $object->context['createfromclone'] = 'createfromclone';
964  $result = $object->create($user);
965 
966  // Other options
967  if ($result < 0) {
968  $this->error = $object->error;
969  $this->errors = array_merge($this->errors, $object->errors);
970  $error++;
971  }
972 
973  if (!$error) {
974  }
975 
976  unset($object->context['createfromclone']);
977 
978  // End
979  if (!$error) {
980  $this->db->commit();
981  return $object->id;
982  } else {
983  $this->db->rollback();
984  return -1;
985  }
986  }
987 
994  public function initAsSpecimen()
995  {
996 
997  $this->id = 0;
998 
999  $this->entity = '';
1000  $this->datec = '';
1001  $this->tms = '';
1002  $this->fk_product = '';
1003  $this->fk_soc = '';
1004  $this->ref_customer = '';
1005  $this->price = '';
1006  $this->price_ttc = '';
1007  $this->price_min = '';
1008  $this->price_min_ttc = '';
1009  $this->price_base_type = '';
1010  $this->default_vat_code = '';
1011  $this->tva_tx = '';
1012  $this->recuperableonly = '';
1013  $this->localtax1_tx = '';
1014  $this->localtax2_tx = '';
1015  $this->fk_user = '';
1016  $this->import_key = '';
1017  }
1018 }
1019 
1024 {
1028  public $id;
1029 
1033  public $entity;
1034 
1035  public $datec = '';
1036  public $tms = '';
1037 
1041  public $fk_product;
1042 
1046  public $ref_customer;
1047 
1051  public $fk_soc;
1052 
1053  public $price;
1054  public $price_ttc;
1055  public $price_min;
1056  public $price_min_ttc;
1057  public $price_base_type;
1058  public $default_vat_code;
1059  public $tva_tx;
1060  public $recuperableonly;
1061  public $localtax1_tx;
1062  public $localtax2_tx;
1063 
1067  public $fk_user;
1068 
1069  public $import_key;
1070  public $socname;
1071  public $prodref;
1072 }
Productcustomerprice\fetch_all
fetch_all($sortorder='', $sortfield='', $limit=0, $offset=0, $filter=array())
Load all customer prices in memory from database.
Definition: productcustomerprice.class.php:359
db
$conf db
API class for accounts.
Definition: inc.php:41
Productcustomerprice
File of class to manage predefined price products or services by customer.
Definition: productcustomerprice.class.php:29
Productcustomerprice\update
update($user=0, $notrigger=0, $forceupdateaffiliate=0)
Update object into database.
Definition: productcustomerprice.class.php:588
Productcustomerprice\create
create($user, $notrigger=0, $forceupdateaffiliate=0)
Create object into database.
Definition: productcustomerprice.class.php:102
Productcustomerprice\setPriceOnAffiliateThirdparty
setPriceOnAffiliateThirdparty($user, $forceupdateaffiliate)
Force update price on child companies so child company has same prices than parent.
Definition: productcustomerprice.class.php:804
CommonObject
Parent class of all other business classes (invoices, contracts, proposals, orders,...
Definition: commonobject.class.php:44
Productcustomerprice\fetch
fetch($id)
Load object in memory from the database.
Definition: productcustomerprice.class.php:280
Productcustomerprice\__construct
__construct($db)
Constructor.
Definition: productcustomerprice.class.php:89
price2num
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
Definition: functions.lib.php:5661
Productcustomerprice\createFromClone
createFromClone(User $user, $fromid)
Load an object from its id and create a new one in database.
Definition: productcustomerprice.class.php:946
Productcustomerprice\initAsSpecimen
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
Definition: productcustomerprice.class.php:994
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
User
Class to manage Dolibarr users.
Definition: user.class.php:44
PriceByCustomerLine
File of class to manage predefined price products or services by customer lines.
Definition: productcustomerprice.class.php:1023
Productcustomerprice\fetch_all_log
fetch_all_log($sortorder, $sortfield, $limit, $offset, $filter=array())
Load all objects in memory from database.
Definition: productcustomerprice.class.php:480
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2845
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->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->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
price
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.
Definition: functions.lib.php:5541
CommonObject\call_trigger
call_trigger($triggerName, $user)
Call trigger based on this instance.
Definition: commonobject.class.php:5791
float
div float
Buy price without taxes.
Definition: style.css.php:809