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