dolibarr  9.0.0
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 <http://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  function __construct($db)
85  {
86  $this->db = $db;
87  }
88 
97  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  // Uncomment this and change MYOBJECT to your own tag if you
223  // want this action calls a trigger.
224 
225  // // Call triggers
226  // include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
227  // $interface=new Interfaces($this->db);
228  // $result=$interface->run_triggers('MYOBJECT_CREATE',$this,$user,$langs,$conf);
229  // if ($result < 0) { $error++; $this->errors=$interface->errors; }
230  // // End call triggers
231  }
232  }
233 
234  if (! $error) {
235  $result = $this->setPriceOnAffiliateThirdparty($user, $forceupdateaffiliate);
236  if ($result < 0) {
237  $error ++;
238  }
239  }
240 
241  // Commit or rollback
242  if ($error) {
243  foreach ( $this->errors as $errmsg ) {
244  dol_syslog(get_class($this) . "::create " . $errmsg, LOG_ERR);
245  $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
246  }
247  $this->db->rollback();
248  return - 1 * $error;
249  } else {
250  $this->db->commit();
251  return $this->id;
252  }
253  }
254 
261  function fetch($id)
262  {
263  global $langs;
264 
265  $sql = "SELECT";
266  $sql .= " t.rowid,";
267 
268  $sql .= " t.entity,";
269  $sql .= " t.datec,";
270  $sql .= " t.tms,";
271  $sql .= " t.fk_product,";
272  $sql .= " t.fk_soc,";
273  $sql .= " t.price,";
274  $sql .= " t.price_ttc,";
275  $sql .= " t.price_min,";
276  $sql .= " t.price_min_ttc,";
277  $sql .= " t.price_base_type,";
278  $sql .= " t.default_vat_code,";
279  $sql .= " t.tva_tx,";
280  $sql .= " t.recuperableonly,";
281  $sql .= " t.localtax1_tx,";
282  $sql .= " t.localtax2_tx,";
283  $sql .= " t.fk_user,";
284  $sql .= " t.import_key";
285 
286  $sql .= " FROM " . MAIN_DB_PREFIX . "product_customer_price as t";
287  $sql .= " WHERE t.rowid = " . $id;
288 
289  dol_syslog(get_class($this) . "::fetch", LOG_DEBUG);
290  $resql = $this->db->query($sql);
291  if ($resql) {
292  if ($this->db->num_rows($resql)) {
293  $obj = $this->db->fetch_object($resql);
294 
295  $this->id = $obj->rowid;
296 
297  $this->entity = $obj->entity;
298  $this->datec = $this->db->jdate($obj->datec);
299  $this->tms = $this->db->jdate($obj->tms);
300  $this->fk_product = $obj->fk_product;
301  $this->fk_soc = $obj->fk_soc;
302  $this->price = $obj->price;
303  $this->price_ttc = $obj->price_ttc;
304  $this->price_min = $obj->price_min;
305  $this->price_min_ttc = $obj->price_min_ttc;
306  $this->price_base_type = $obj->price_base_type;
307  $this->default_vat_code = $obj->default_vat_code;
308  $this->tva_tx = $obj->tva_tx;
309  $this->recuperableonly = $obj->recuperableonly;
310  $this->localtax1_tx = $obj->localtax1_tx;
311  $this->localtax2_tx = $obj->localtax2_tx;
312  $this->fk_user = $obj->fk_user;
313  $this->import_key = $obj->import_key;
314  }
315  $this->db->free($resql);
316 
317  return 1;
318  } else {
319  $this->error = "Error " . $this->db->lasterror();
320  return - 1;
321  }
322  }
323 
324  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
335  function fetch_all($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, $filter = array())
336  {
337  // phpcs:enable
338  global $langs;
339 
340  if ( empty($sortfield)) $sortfield = "t.rowid";
341  if ( empty($sortorder)) $sortorder = "DESC";
342 
343  $sql = "SELECT";
344  $sql .= " t.rowid,";
345 
346  $sql .= " t.entity,";
347  $sql .= " t.datec,";
348  $sql .= " t.tms,";
349  $sql .= " t.fk_product,";
350  $sql .= " t.fk_soc,";
351  $sql .= " t.price,";
352  $sql .= " t.price_ttc,";
353  $sql .= " t.price_min,";
354  $sql .= " t.price_min_ttc,";
355  $sql .= " t.price_base_type,";
356  $sql .= " t.default_vat_code,";
357  $sql .= " t.tva_tx,";
358  $sql .= " t.recuperableonly,";
359  $sql .= " t.localtax1_tx,";
360  $sql .= " t.localtax2_tx,";
361  $sql .= " t.localtax1_type,";
362  $sql .= " t.localtax2_type,";
363  $sql .= " t.fk_user,";
364  $sql .= " t.import_key,";
365  $sql .= " soc.nom as socname,";
366  $sql .= " prod.ref as prodref";
367  $sql .= " FROM " . MAIN_DB_PREFIX . "product_customer_price as t ";
368  $sql .= " ," . MAIN_DB_PREFIX . "product as prod ";
369  $sql .= " ," . MAIN_DB_PREFIX . "societe as soc ";
370  $sql .= " WHERE soc.rowid=t.fk_soc ";
371  $sql .= " AND prod.rowid=t.fk_product ";
372  $sql .= " AND prod.entity IN (" . getEntity('product') . ")";
373  $sql .= " AND t.entity IN (" . getEntity('productprice') . ")";
374 
375  // Manage filter
376  if (count($filter) > 0) {
377  foreach ( $filter as $key => $value ) {
378  if (strpos($key, 'date')) // To allow $filter['YEAR(s.dated)']=>$year
379  {
380  $sql .= ' AND ' . $key . ' = \'' . $value . '\'';
381  } elseif ($key == 'soc.nom') {
382  $sql .= ' AND ' . $key . ' LIKE \'%' . $value . '%\'';
383  } elseif ($key == 'prod.ref') {
384  $sql .= ' AND ' . $key . ' LIKE \'%' . $value . '%\'';
385  } else {
386  $sql .= ' AND ' . $key . ' = ' . $value;
387  }
388  }
389  }
390  $sql.= $this->db->order($sortfield, $sortorder);
391  if (! empty($limit)) $sql .= ' ' . $this->db->plimit($limit + 1, $offset);
392 
393  dol_syslog(get_class($this) . "::fetch_all", LOG_DEBUG);
394  $resql = $this->db->query($sql);
395  if ($resql) {
396 
397  $this->lines = array ();
398  $num = $this->db->num_rows($resql);
399 
400  while ( $obj = $this->db->fetch_object($resql) ) {
401 
402  $line = new PriceByCustomerLine();
403 
404  $line->id = $obj->rowid;
405 
406  $line->entity = $obj->entity;
407  $line->datec = $this->db->jdate($obj->datec);
408  $line->tms = $this->db->jdate($obj->tms);
409  $line->fk_product = $obj->fk_product;
410  $line->fk_soc = $obj->fk_soc;
411  $line->price = $obj->price;
412  $line->price_ttc = $obj->price_ttc;
413  $line->price_min = $obj->price_min;
414  $line->price_min_ttc = $obj->price_min_ttc;
415  $line->price_base_type = $obj->price_base_type;
416  $line->default_vat_code = $obj->default_vat_code;
417  $line->tva_tx = $obj->tva_tx;
418  $line->recuperableonly = $obj->recuperableonly;
419  $line->localtax1_tx = $obj->localtax1_tx;
420  $line->localtax2_tx = $obj->localtax2_tx;
421  $line->localtax1_type = $obj->localtax1_type;
422  $line->localtax2_type = $obj->localtax2_type;
423  $line->fk_user = $obj->fk_user;
424  $line->import_key = $obj->import_key;
425  $line->socname = $obj->socname;
426  $line->prodref = $obj->prodref;
427 
428  $this->lines [] = $line;
429  }
430  $this->db->free($resql);
431 
432  return $num;
433  } else {
434  $this->error = "Error " . $this->db->lasterror();
435  return - 1;
436  }
437  }
438 
439  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
450  function fetch_all_log($sortorder, $sortfield, $limit, $offset, $filter = array())
451  {
452  // phpcs:enable
453  global $langs;
454 
455  if (! empty($sortfield)) $sortfield = "t.rowid";
456  if (! empty($sortorder)) $sortorder = "DESC";
457 
458  $sql = "SELECT";
459  $sql .= " t.rowid,";
460 
461  $sql .= " t.entity,";
462  $sql .= " t.datec,";
463  $sql .= " t.fk_product,";
464  $sql .= " t.fk_soc,";
465  $sql .= " t.price,";
466  $sql .= " t.price_ttc,";
467  $sql .= " t.price_min,";
468  $sql .= " t.price_min_ttc,";
469  $sql .= " t.price_base_type,";
470  $sql .= " t.default_vat_code,";
471  $sql .= " t.tva_tx,";
472  $sql .= " t.recuperableonly,";
473  $sql .= " t.localtax1_tx,";
474  $sql .= " t.localtax2_tx,";
475  $sql .= " t.fk_user,";
476  $sql .= " t.import_key,";
477  $sql .= " soc.nom as socname,";
478  $sql .= " prod.ref as prodref";
479  $sql .= " FROM " . MAIN_DB_PREFIX . "product_customer_price_log as t ";
480  $sql .= " ," . MAIN_DB_PREFIX . "product as prod ";
481  $sql .= " ," . MAIN_DB_PREFIX . "societe as soc ";
482  $sql .= " WHERE soc.rowid=t.fk_soc ";
483  $sql .= " AND prod.rowid=t.fk_product ";
484  $sql .= " AND prod.entity IN (" . getEntity('product') . ")";
485  $sql .= " AND t.entity IN (" . getEntity('productprice') . ")";
486 
487  // Manage filter
488  if (count($filter) > 0) {
489  foreach ( $filter as $key => $value ) {
490  if (strpos($key, 'date')) // To allow $filter['YEAR(s.dated)']=>$year
491  {
492  $sql .= ' AND ' . $key . ' = \'' . $value . '\'';
493  } elseif ($key == 'soc.nom') {
494  $sql .= ' AND ' . $key . ' LIKE \'%' . $value . '%\'';
495  } else {
496  $sql .= ' AND ' . $key . ' = ' . $value;
497  }
498  }
499  }
500 
501  $sql.= $this->db->order($sortfield, $sortorder);
502  if (! empty($limit)) $sql .= ' ' . $this->db->plimit($limit + 1, $offset);
503 
504  dol_syslog(get_class($this) . "::fetch_all_log", LOG_DEBUG);
505  $resql = $this->db->query($sql);
506  if ($resql) {
507 
508  $this->lines = array ();
509  $num = $this->db->num_rows($resql);
510 
511  while ( $obj = $this->db->fetch_object($resql) ) {
512 
513  $line = new PriceByCustomerLine();
514 
515  $line->id = $obj->rowid;
516 
517  $line->entity = $obj->entity;
518  $line->datec = $this->db->jdate($obj->datec);
519  $line->tms = $this->db->jdate($obj->tms);
520  $line->fk_product = $obj->fk_product;
521  $line->fk_soc = $obj->fk_soc;
522  $line->price = $obj->price;
523  $line->price_ttc = $obj->price_ttc;
524  $line->price_min = $obj->price_min;
525  $line->price_min_ttc = $obj->price_min_ttc;
526  $line->price_base_type = $obj->price_base_type;
527  $line->default_vat_code = $obj->default_vat_code;
528  $line->tva_tx = $obj->tva_tx;
529  $line->recuperableonly = $obj->recuperableonly;
530  $line->localtax1_tx = $obj->localtax1_tx;
531  $line->localtax2_tx = $obj->localtax2_tx;
532  $line->fk_user = $obj->fk_user;
533  $line->import_key = $obj->import_key;
534  $line->socname = $obj->socname;
535  $line->prodref = $obj->prodref;
536 
537  $this->lines [] = $line;
538  }
539  $this->db->free($resql);
540 
541  return $num;
542  } else {
543  $this->error = "Error " . $this->db->lasterror();
544  return - 1;
545  }
546  }
547 
556  function update($user = 0, $notrigger = 0, $forceupdateaffiliate = 0)
557  {
558 
559  global $conf, $langs;
560  $error = 0;
561 
562  // Clean parameters
563 
564  if (isset($this->entity))
565  $this->entity = trim($this->entity);
566  if (isset($this->fk_product))
567  $this->fk_product = trim($this->fk_product);
568  if (isset($this->fk_soc))
569  $this->fk_soc = trim($this->fk_soc);
570  if (isset($this->price))
571  $this->price = trim($this->price);
572  if (isset($this->price_ttc))
573  $this->price_ttc = trim($this->price_ttc);
574  if (isset($this->price_min))
575  $this->price_min = trim($this->price_min);
576  if (isset($this->price_min_ttc))
577  $this->price_min_ttc = trim($this->price_min_ttc);
578  if (isset($this->price_base_type))
579  $this->price_base_type = trim($this->price_base_type);
580  if (isset($this->tva_tx))
581  $this->tva_tx = trim($this->tva_tx);
582  if (isset($this->recuperableonly))
583  $this->recuperableonly = trim($this->recuperableonly);
584  if (isset($this->localtax1_tx))
585  $this->localtax1_tx = trim($this->localtax1_tx);
586  if (isset($this->localtax2_tx))
587  $this->localtax2_tx = trim($this->localtax2_tx);
588  if (isset($this->fk_user))
589  $this->fk_user = trim($this->fk_user);
590  if (isset($this->import_key))
591  $this->import_key = trim($this->import_key);
592 
593  // Check parameters
594  // Put here code to add a control on parameters values
595 
596  if ($this->price != '' || $this->price == 0) {
597  if ($this->price_base_type == 'TTC') {
598  $this->price_ttc = price2num($this->price, 'MU');
599  $this->price = price2num($this->price) / (1 + ($this->tva_tx / 100));
600  $this->price = price2num($this->price, 'MU');
601 
602  if ($this->price_min != '' || $this->price_min == 0) {
603  $this->price_min_ttc = price2num($this->price_min, 'MU');
604  $this->price_min = price2num($this->price_min) / (1 + ($this->tva_tx / 100));
605  $this->price_min = price2num($this->price_min, 'MU');
606  } else {
607  $this->price_min = 0;
608  $this->price_min_ttc = 0;
609  }
610  } else {
611  $this->price = price2num($this->price, 'MU');
612  $this->price_ttc = ($this->recuperableonly != 1) ? price2num($this->price) * (1 + ($this->tva_tx / 100)) : $this->price;
613  $this->price_ttc = price2num($this->price_ttc, 'MU');
614 
615  if ($this->price_min != '' || $this->price_min == 0) {
616  $this->price_min = price2num($this->price_min, 'MU');
617  $this->price_min_ttc = price2num($this->price_min) * (1 + ($this->tva_tx / 100));
618  $this->price_min_ttc = price2num($this->price_min_ttc, 'MU');
619  // print 'X'.$newminprice.'-'.$price_min;
620  } else {
621  $this->price_min = 0;
622  $this->price_min_ttc = 0;
623  }
624  }
625  }
626 
627  // Do a copy of current record into log table
628  // Insert request
629  $sql = "INSERT INTO " . MAIN_DB_PREFIX . "product_customer_price_log(";
630 
631  $sql .= "entity,";
632  $sql .= "datec,";
633  $sql .= "fk_product,";
634  $sql .= "fk_soc,";
635  $sql .= "price,";
636  $sql .= "price_ttc,";
637  $sql .= "price_min,";
638  $sql .= "price_min_ttc,";
639  $sql .= "price_base_type,";
640  $sql .= "default_vat_code,";
641  $sql .= "tva_tx,";
642  $sql .= "recuperableonly,";
643  $sql .= "localtax1_tx,";
644  $sql .= "localtax2_tx,";
645  $sql .= "localtax1_type,";
646  $sql .= "localtax2_type,";
647  $sql .= "fk_user,";
648  $sql .= "import_key";
649 
650  $sql .= ") ";
651  $sql .= "SELECT";
652 
653  $sql .= " t.entity,";
654  $sql .= " t.datec,";
655  $sql .= " t.fk_product,";
656  $sql .= " t.fk_soc,";
657  $sql .= " t.price,";
658  $sql .= " t.price_ttc,";
659  $sql .= " t.price_min,";
660  $sql .= " t.price_min_ttc,";
661  $sql .= " t.price_base_type,";
662  $sql .= " t.default_vat_code,";
663  $sql .= " t.tva_tx,";
664  $sql .= " t.recuperableonly,";
665  $sql .= " t.localtax1_tx,";
666  $sql .= " t.localtax2_tx,";
667  $sql .= " t.localtax1_type,";
668  $sql .= " t.localtax2_type,";
669  $sql .= " t.fk_user,";
670  $sql .= " t.import_key";
671 
672  $sql .= " FROM " . MAIN_DB_PREFIX . "product_customer_price as t";
673  $sql .= " WHERE t.rowid = " . $this->id;
674 
675  $this->db->begin();
676  dol_syslog(get_class($this) . "::update", LOG_DEBUG);
677  $resql = $this->db->query($sql);
678  if (! $resql) {
679  $error ++;
680  $this->errors [] = "Error " . $this->db->lasterror();
681  }
682 
683  // Update request
684  $sql = "UPDATE " . MAIN_DB_PREFIX . "product_customer_price SET";
685 
686  $sql .= " entity=" . $conf->entity . ",";
687  $sql .= " datec='" . $this->db->idate(dol_now()) . "',";
688  $sql .= " tms=" . (dol_strlen($this->tms) != 0 ? "'" . $this->db->idate($this->tms) . "'" : 'null') . ",";
689  $sql .= " fk_product=" . (isset($this->fk_product) ? $this->fk_product : "null") . ",";
690  $sql .= " fk_soc=" . (isset($this->fk_soc) ? $this->fk_soc : "null") . ",";
691  $sql .= " price=" . (isset($this->price) ? $this->price : "null") . ",";
692  $sql .= " price_ttc=" . (isset($this->price_ttc) ? $this->price_ttc : "null") . ",";
693  $sql .= " price_min=" . (isset($this->price_min) ? $this->price_min : "null") . ",";
694  $sql .= " price_min_ttc=" . (isset($this->price_min_ttc) ? $this->price_min_ttc : "null") . ",";
695  $sql .= " price_base_type=" . (isset($this->price_base_type) ? "'" . $this->db->escape($this->price_base_type) . "'" : "null") . ",";
696  $sql .= " default_vat_code = ".($this->default_vat_code ? "'".$this->db->escape($this->default_vat_code)."'" : "null").",";
697  $sql .= " tva_tx=" . (isset($this->tva_tx) ? (empty($this->tva_tx)?0:$this->tva_tx) : "null") . ",";
698  $sql .= " recuperableonly=" . (isset($this->recuperableonly) ? $this->recuperableonly : "null") . ",";
699  $sql .= " localtax1_tx=" . (isset($this->localtax1_tx) ? (empty($this->localtax1_tx)?0:$this->localtax1_tx) : "null") . ",";
700  $sql .= " localtax2_tx=" . (isset($this->localtax2_tx) ? (empty($this->localtax2_tx)?0:$this->localtax2_tx) : "null") . ",";
701  $sql .= " localtax1_type=" . (! empty($this->localtax1_type) ? "'".$this->db->escape($this->localtax1_type)."'": "'0'") . ",";
702  $sql .= " localtax2_type=" . (! empty($this->localtax2_type) ? "'".$this->db->escape($this->localtax2_type)."'": "'0'") . ",";
703  $sql .= " fk_user=" . $user->id . ",";
704  $sql .= " import_key=" . (isset($this->import_key) ? "'" . $this->db->escape($this->import_key) . "'" : "null") . "";
705 
706  $sql .= " WHERE rowid=" . $this->id;
707 
708  dol_syslog(get_class($this) . "::update", LOG_DEBUG);
709  $resql = $this->db->query($sql);
710  if (! $resql) {
711  $error ++;
712  $this->errors [] = "Error " . $this->db->lasterror();
713  }
714 
715  if (! $error) {
716  if (! $notrigger) {
717  // Call triggers
718  include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
719  $interface=new Interfaces($this->db);
720  $result=$interface->run_triggers('PRODUCT_CUSTOMER_PRICE_UPDATE',$this,$user,$langs,$conf);
721  if ($result < 0) { $error++; $this->errors=$interface->errors; }
722  // End call triggers
723  }
724  }
725 
726  if (! $error) {
727  $result = $this->setPriceOnAffiliateThirdparty($user, $forceupdateaffiliate);
728  if ($result < 0) {
729  $error ++;
730  }
731  }
732 
733  // Commit or rollback
734  if ($error) {
735  foreach ( $this->errors as $errmsg ) {
736  dol_syslog(get_class($this) . "::update " . $errmsg, LOG_ERR);
737  $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
738  }
739  $this->db->rollback();
740  return - 1 * $error;
741  } else {
742  $this->db->commit();
743  return 1;
744  }
745  }
746 
754  function setPriceOnAffiliateThirdparty($user, $forceupdateaffiliate)
755  {
756 
757  $error = 0;
758 
759  // Find all susidiaries
760  $sql = "SELECT s.rowid";
761  $sql .= " FROM " . MAIN_DB_PREFIX . "societe as s";
762  $sql .= " WHERE s.parent = " . $this->fk_soc;
763  $sql .= " AND s.entity IN (" . getEntity('societe') . ")";
764 
765  dol_syslog(get_class($this) . "::setPriceOnAffiliateThirdparty", LOG_DEBUG);
766  $resql = $this->db->query($sql);
767 
768  if ($resql) {
769 
770  $this->lines = array ();
771  $num = $this->db->num_rows($resql);
772 
773  while ( ($obj = $this->db->fetch_object($resql)) && (empty($error)) ) {
774 
775  // find if there is an existing line for the product and the subsidiaries
776  $prodsocprice = new Productcustomerprice($this->db);
777 
778  $filter = array (
779  't.fk_product' => $this->fk_product,'t.fk_soc' => $obj->rowid
780  );
781 
782  $result = $prodsocprice->fetch_all('', '', 0, 0, $filter);
783  if ($result < 0) {
784  $error ++;
785  $this->error = $prodsocprice->error;
786  } else {
787 
788  // There is one line
789  if (count($prodsocprice->lines) > 0) {
790  // If force update => Update
791  if (! empty($forceupdateaffiliate)) {
792 
793  $prodsocpriceupd = new Productcustomerprice($this->db);
794  $prodsocpriceupd->fetch($prodsocprice->lines [0]->id);
795 
796  $prodsocpriceupd->price = $this->price;
797  $prodsocpriceupd->price_min = $this->price_min;
798  $prodsocpriceupd->price_base_type = $this->price_base_type;
799  $prodsocpriceupd->tva_tx = $this->tva_tx;
800  $prodsocpriceupd->recuperableonly = $this->recuperableonly;
801 
802  $resultupd = $prodsocpriceupd->update($user, 0, $forceupdateaffiliate);
803  if ($result < 0) {
804  $error ++;
805  $this->error = $prodsocpriceupd->error;
806  }
807  }
808  } else {
809  // If line do not exits then create it
810  $prodsocpricenew = new Productcustomerprice($this->db);
811  $prodsocpricenew->fk_soc = $obj->rowid;
812  $prodsocpricenew->fk_product = $this->fk_product;
813  $prodsocpricenew->price = $this->price;
814  $prodsocpricenew->price_min = $this->price_min;
815  $prodsocpricenew->price_base_type = $this->price_base_type;
816  $prodsocpricenew->tva_tx = $this->tva_tx;
817  $prodsocpricenew->recuperableonly = $this->recuperableonly;
818 
819  $resultupd = $prodsocpricenew->create($user, 0, $forceupdateaffiliate);
820  if ($result < 0) {
821  $error ++;
822  $this->error = $prodsocpriceupd->error;
823  }
824  }
825  }
826  }
827  $this->db->free($resql);
828 
829  if (empty($error)) {
830  return 1;
831  } else {
832  return - 1;
833  }
834  } else {
835  $this->error = "Error " . $this->db->lasterror();
836  return - 1;
837  }
838  }
839 
847  function delete($user, $notrigger = 0)
848  {
849 
850  global $conf, $langs;
851  $error = 0;
852 
853  $this->db->begin();
854 
855  if (! $error) {
856  if (! $notrigger) {
857  // Uncomment this and change MYOBJECT to your own tag if you
858  // want this action calls a trigger.
859 
860  // // Call triggers
861  // include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
862  // $interface=new Interfaces($this->db);
863  // $result=$interface->run_triggers('MYOBJECT_DELETE',$this,$user,$langs,$conf);
864  // if ($result < 0) { $error++; $this->errors=$interface->errors; }
865  // // End call triggers
866  }
867  }
868 
869  if (! $error) {
870  $sql = "DELETE FROM " . MAIN_DB_PREFIX . "product_customer_price";
871  $sql .= " WHERE rowid=" . $this->id;
872 
873  dol_syslog(get_class($this) . "::delete", LOG_DEBUG);
874  $resql = $this->db->query($sql);
875  if (! $resql) {
876  $error ++;
877  $this->errors [] = "Error " . $this->db->lasterror();
878  }
879  }
880 
881  // Commit or rollback
882  if ($error) {
883  foreach ( $this->errors as $errmsg ) {
884  dol_syslog(get_class($this) . "::delete " . $errmsg, LOG_ERR);
885  $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
886  }
887  $this->db->rollback();
888  return - 1 * $error;
889  } else {
890  $this->db->commit();
891  return 1;
892  }
893  }
894 
901  function createFromClone($fromid)
902  {
903 
904  global $user, $langs;
905 
906  $error = 0;
907 
908  $object = new Productcustomerprice($this->db);
909 
910  $this->db->begin();
911 
912  // Load source object
913  $object->fetch($fromid);
914  $object->id = 0;
915  $object->statut = 0;
916 
917  // Clear fields
918  // ...
919 
920  // Create clone
921  $object->context['createfromclone']='createfromclone';
922  $result = $object->create($user);
923 
924  // Other options
925  if ($result < 0) {
926  $this->error = $object->error;
927  $this->errors=array_merge($this->errors, $object->errors);
928  $error++;
929  }
930 
931  if (! $error) {
932 
933  }
934 
935  unset($object->context['createfromclone']);
936 
937  // End
938  if (! $error) {
939  $this->db->commit();
940  return $object->id;
941  } else {
942  $this->db->rollback();
943  return - 1;
944  }
945  }
946 
953  function initAsSpecimen()
954  {
955 
956  $this->id = 0;
957 
958  $this->entity = '';
959  $this->datec = '';
960  $this->tms = '';
961  $this->fk_product = '';
962  $this->fk_soc = '';
963  $this->price = '';
964  $this->price_ttc = '';
965  $this->price_min = '';
966  $this->price_min_ttc = '';
967  $this->price_base_type = '';
968  $this->default_vat_code = '';
969  $this->tva_tx = '';
970  $this->recuperableonly = '';
971  $this->localtax1_tx = '';
972  $this->localtax2_tx = '';
973  $this->fk_user = '';
974  $this->import_key = '';
975  }
976 }
977 
982 {
986  public $id;
987 
991  public $entity;
992 
993  public $datec = '';
994  public $tms = '';
995 
999  public $fk_product;
1000 
1004  public $fk_soc;
1005 
1006  public $price;
1007  public $price_ttc;
1008  public $price_min;
1009  public $price_min_ttc;
1010  public $price_base_type;
1011  public $default_vat_code;
1012  public $tva_tx;
1013  public $recuperableonly;
1014  public $localtax1_tx;
1015  public $localtax2_tx;
1016 
1020  public $fk_user;
1021 
1022  public $import_key;
1023  public $socname;
1024  public $prodref;
1025 }
File of class to manage predefined price products or services by customer.
if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) if(! empty($conf->don->enabled) && $user->rights->societe->lire) if(! empty($conf->tax->enabled) && $user->rights->tax->charges->lire) if(! empty($conf->facture->enabled) &&! empty($conf->commande->enabled) && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1053
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.
update($user=0, $notrigger=0, $forceupdateaffiliate=0)
Update object into database.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
File of class to manage predefined price products or services by customer lines.
setPriceOnAffiliateThirdparty($user, $forceupdateaffiliate)
Force update price on child price.
dol_now($mode='gmt')
Return date for now.
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
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...
createFromClone($fromid)
Load an object from its id and create a new one in database.
fetch_all($sortorder='', $sortfield='', $limit=0, $offset=0, $filter=array())
Load all customer prices in memory from database.
price2num($amount, $rounding='', $alreadysqlnb=0)
Function that return a number with universal decimal format (decimal separator is &#39;...
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)
Class to manage triggers.