dolibarr 20.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 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
5 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
26require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
27require_once DOL_DOCUMENT_ROOT.'/core/class/commonobjectline.class.php';
28
33{
37 public $fields = array(
38 'ref' => array('type' => 'varchar(128)', 'label' => 'Ref', 'enabled' => 1, 'visible' => 4, 'position' => 10, 'notnull' => 1, 'default' => '(PROV)', 'index' => 1, 'searchall' => 1, 'comment' => "Reference of object", 'showoncombobox' => 1, 'noteditable' => 1),
39 'fk_product' => array('type' => 'integer:Product:product/class/product.class.php:0', 'label' => 'Product', 'enabled' => '$conf->product->enabled', 'visible' => 1, 'position' => 35, 'notnull' => 1, 'index' => 1, 'comment' => "Product to produce", 'css' => 'maxwidth300', 'csslist' => 'tdoverflowmax100', 'picto' => 'product'),
40 'ref_customer' => array('type' => 'varchar(128)', 'label' => 'RefCustomer', 'enabled' => 1, 'visible' => 4, 'position' => 10, 'notnull' => 1,),
41 'datec' => array('type' => 'datetime', 'label' => 'AppliedPricesFrom', 'enabled' => 1, 'visible' => 1, 'position' => 500, 'notnull' => 1,),
42 'price_base_type' => array('type' => 'varchar(255)', 'label' => 'PriceBase', 'enabled' => 1, 'visible' => 1, 'position' => 11, 'notnull' => -1, 'comment' => 'Price Base Type'),
43 'tva_tx' => array('type' => 'decimal(20,6)', 'label' => 'VAT', 'enabled' => 1, 'visible' => 1, 'position' => 12, 'notnull' => -1, 'comment' => 'TVA Tax Rate'),
44 'price' => array('type' => 'decimal(20,6)', 'label' => 'HT', 'enabled' => 1, 'visible' => 1, 'position' => 8, 'notnull' => -1, 'comment' => 'Price HT'),
45 'price_ttc' => array('type' => 'decimal(20,6)', 'label' => 'TTC', 'enabled' => 1, 'visible' => 1, 'position' => 8, 'notnull' => -1, 'comment' => 'Price TTC'),
46 'price_min' => array('type' => 'decimal(20,6)', 'label' => 'MinPriceHT', 'enabled' => 1, 'visible' => 1, 'position' => 9, 'notnull' => -1, 'comment' => 'Minimum Price'),
47 'price_min_ttc' => array('type' => 'decimal(20,6)', 'label' => 'MinPriceTTC', 'enabled' => 1, 'visible' => 1, 'position' => 10, 'notnull' => -1, 'comment' => 'Minimum Price TTC'),
48 'price_label' => array('type' => 'varchar(255)', 'label' => 'PriceLabel', 'enabled' => 1, 'visible' => 1, 'position' => 20, 'notnull' => -1, 'comment' => 'Price Label'),
49 'fk_user' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserModif', 'enabled' => 1, 'visible' => 1, 'position' => 510, 'notnull' => 1, 'foreignkey' => 'user.rowid', 'csslist' => 'tdoverflowmax100'),
50 );
51
55 public $element = 'product_customer_price';
56
60 public $table_element = 'product_customer_price';
61
65 public $entity;
66
67 public $datec = '';
68
72 public $fk_product;
73
77 public $fk_soc;
78
82 public $ref_customer;
83
84 public $price;
85 public $price_ttc;
86 public $price_min;
87 public $price_min_ttc;
88 public $price_base_type;
89 public $default_vat_code;
90 public $tva_tx;
91 public $recuperableonly;
92 public $localtax1_type;
93 public $localtax1_tx;
94 public $localtax2_type;
95 public $localtax2_tx;
96 public $price_label;
97
101 public $fk_user;
102
106 public $lines = array();
107
108
114 public function __construct($db)
115 {
116 $this->db = $db;
117 }
118
127 public function create($user, $notrigger = 0, $forceupdateaffiliate = 0)
128 {
129 global $conf, $langs;
130 $error = 0;
131
132 // Clean parameters
133
134 if (isset($this->entity)) {
135 $this->entity = (int) $this->entity;
136 }
137 if (isset($this->fk_product)) {
138 $this->fk_product = (int) $this->fk_product;
139 }
140 if (isset($this->fk_soc)) {
141 $this->fk_soc = (int) $this->fk_soc;
142 }
143 if (isset($this->ref_customer)) {
144 $this->ref_customer = trim($this->ref_customer);
145 }
146 if (isset($this->price)) {
147 $this->price = trim($this->price);
148 }
149 if (isset($this->price_ttc)) {
150 $this->price_ttc = trim($this->price_ttc);
151 }
152 if (isset($this->price_min)) {
153 $this->price_min = trim($this->price_min);
154 }
155 if (isset($this->price_min_ttc)) {
156 $this->price_min_ttc = trim($this->price_min_ttc);
157 }
158 if (isset($this->price_base_type)) {
159 $this->price_base_type = trim($this->price_base_type);
160 }
161 if (isset($this->tva_tx)) {
162 $this->tva_tx = (float) $this->tva_tx;
163 }
164 if (isset($this->recuperableonly)) {
165 $this->recuperableonly = trim($this->recuperableonly);
166 }
167 if (isset($this->localtax1_tx)) {
168 $this->localtax1_tx = trim($this->localtax1_tx);
169 }
170 if (isset($this->localtax2_tx)) {
171 $this->localtax2_tx = trim($this->localtax2_tx);
172 }
173 if (isset($this->fk_user)) {
174 $this->fk_user = (int) $this->fk_user;
175 }
176 if (isset($this->price_label)) {
177 $this->price_label = trim($this->price_label);
178 }
179 if (isset($this->import_key)) {
180 $this->import_key = trim($this->import_key);
181 }
182
183 // Check parameters
184 // Put here code to add control on parameters values
185
186 if ($this->price != '' || $this->price == 0) {
187 if ($this->price_base_type == 'TTC') {
188 $this->price_ttc = price2num($this->price, 'MU');
189 $this->price = price2num($this->price) / (1 + ($this->tva_tx / 100));
190 $this->price = price2num($this->price, 'MU');
191
192 if ($this->price_min != '' || $this->price_min == 0) {
193 $this->price_min_ttc = price2num($this->price_min, 'MU');
194 $this->price_min = price2num($this->price_min) / (1 + ($this->tva_tx / 100));
195 $this->price_min = price2num($this->price_min, 'MU');
196 } else {
197 $this->price_min = 0;
198 $this->price_min_ttc = 0;
199 }
200 } else {
201 $this->price = price2num($this->price, 'MU');
202 $this->price_ttc = ($this->recuperableonly != 1) ? price2num($this->price) * (1 + ($this->tva_tx / 100)) : $this->price;
203 $this->price_ttc = price2num($this->price_ttc, 'MU');
204
205 if ($this->price_min != '' || $this->price_min == 0) {
206 $this->price_min = price2num($this->price_min, 'MU');
207 $this->price_min_ttc = price2num($this->price_min) * (1 + ($this->tva_tx / 100));
208 $this->price_min_ttc = price2num($this->price_min_ttc, 'MU');
209 // print 'X'.$newminprice.'-'.$price_min;
210 } else {
211 $this->price_min = 0;
212 $this->price_min_ttc = 0;
213 }
214 }
215 }
216
217 // Insert request
218 $sql = "INSERT INTO ".$this->db->prefix()."product_customer_price(";
219 $sql .= "entity,";
220 $sql .= "datec,";
221 $sql .= "fk_product,";
222 $sql .= "fk_soc,";
223 $sql .= 'ref_customer,';
224 $sql .= "price,";
225 $sql .= "price_ttc,";
226 $sql .= "price_min,";
227 $sql .= "price_min_ttc,";
228 $sql .= "price_base_type,";
229 $sql .= "default_vat_code,";
230 $sql .= "tva_tx,";
231 $sql .= "recuperableonly,";
232 $sql .= "localtax1_type,";
233 $sql .= "localtax1_tx,";
234 $sql .= "localtax2_type,";
235 $sql .= "localtax2_tx,";
236 $sql .= "fk_user,";
237 $sql .= "price_label,";
238 $sql .= "import_key";
239 $sql .= ") VALUES (";
240 $sql .= " ".((int) $conf->entity).",";
241 $sql .= " '".$this->db->idate(dol_now())."',";
242 $sql .= " ".(!isset($this->fk_product) ? 'NULL' : "'".$this->db->escape($this->fk_product)."'").",";
243 $sql .= " ".(!isset($this->fk_soc) ? 'NULL' : "'".$this->db->escape($this->fk_soc)."'").",";
244 $sql .= " ".(!isset($this->ref_customer) ? 'NULL' : "'".$this->db->escape($this->ref_customer)."'").",";
245 $sql .= " ".(empty($this->price) ? '0' : "'".$this->db->escape($this->price)."'").",";
246 $sql .= " ".(empty($this->price_ttc) ? '0' : "'".$this->db->escape($this->price_ttc)."'").",";
247 $sql .= " ".(empty($this->price_min) ? '0' : "'".$this->db->escape($this->price_min)."'").",";
248 $sql .= " ".(empty($this->price_min_ttc) ? '0' : "'".$this->db->escape($this->price_min_ttc)."'").",";
249 $sql .= " ".(!isset($this->price_base_type) ? 'NULL' : "'".$this->db->escape($this->price_base_type)."'").",";
250 $sql .= " ".($this->default_vat_code ? "'".$this->db->escape($this->default_vat_code)."'" : "null").",";
251 $sql .= " ".(!isset($this->tva_tx) ? 'NULL' : (empty($this->tva_tx) ? 0 : $this->tva_tx)).",";
252 $sql .= " ".(!isset($this->recuperableonly) ? 'NULL' : "'".$this->db->escape($this->recuperableonly)."'").",";
253 $sql .= " ".(empty($this->localtax1_type) ? "'0'" : "'".$this->db->escape($this->localtax1_type)."'").",";
254 $sql .= " ".(!isset($this->localtax1_tx) ? 'NULL' : (empty($this->localtax1_tx) ? 0 : $this->localtax1_tx)).",";
255 $sql .= " ".(empty($this->localtax2_type) ? "'0'" : "'".$this->db->escape($this->localtax2_type)."'").",";
256 $sql .= " ".(!isset($this->localtax2_tx) ? 'NULL' : (empty($this->localtax2_tx) ? 0 : $this->localtax2_tx)).",";
257 $sql .= " ".((int) $user->id).",";
258 $sql .= " ".(!isset($this->price_label) ? 'NULL' : "'".$this->db->escape($this->price_label)."'").",";
259 $sql .= " ".(!isset($this->import_key) ? 'NULL' : "'".$this->db->escape($this->import_key)."'");
260 $sql .= ")";
261
262 $this->db->begin();
263
264 dol_syslog(get_class($this)."::create", LOG_DEBUG);
265 $resql = $this->db->query($sql);
266 if (!$resql) {
267 $error++;
268 $this->errors [] = "Error ".$this->db->lasterror();
269 }
270
271 if (!$error) {
272 $this->id = $this->db->last_insert_id($this->db->prefix()."product_customer_price");
273
274 if (!$notrigger) {
275 $result = $this->call_trigger('PRODUCT_CUSTOMER_PRICE_CREATE', $user);
276 if ($result < 0) {
277 $error++;
278 }
279 }
280 }
281
282 if (!$error) {
283 $result = $this->setPriceOnAffiliateThirdparty($user, $forceupdateaffiliate);
284 if ($result < 0) {
285 $error++;
286 }
287 }
288
289 // Commit or rollback
290 if ($error) {
291 foreach ($this->errors as $errmsg) {
292 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
293 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
294 }
295 $this->db->rollback();
296 return -1 * $error;
297 } else {
298 $this->db->commit();
299 return $this->id;
300 }
301 }
302
309 public function fetch($id)
310 {
311 global $langs;
312
313 $sql = "SELECT";
314 $sql .= " t.rowid,";
315 $sql .= " t.entity,";
316 $sql .= " t.datec,";
317 $sql .= " t.tms,";
318 $sql .= " t.fk_product,";
319 $sql .= " t.fk_soc,";
320 $sql .= " t.ref_customer,";
321 $sql .= " t.price,";
322 $sql .= " t.price_ttc,";
323 $sql .= " t.price_min,";
324 $sql .= " t.price_min_ttc,";
325 $sql .= " t.price_base_type,";
326 $sql .= " t.default_vat_code,";
327 $sql .= " t.tva_tx,";
328 $sql .= " t.recuperableonly,";
329 $sql .= " t.localtax1_tx,";
330 $sql .= " t.localtax2_tx,";
331 $sql .= " t.fk_user,";
332 $sql .= " t.price_label,";
333 $sql .= " t.import_key";
334 $sql .= " FROM ".$this->db->prefix()."product_customer_price as t";
335 $sql .= " WHERE t.rowid = ".((int) $id);
336
337 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
338 $resql = $this->db->query($sql);
339 if ($resql) {
340 if ($this->db->num_rows($resql)) {
341 $obj = $this->db->fetch_object($resql);
342
343 $this->id = $obj->rowid;
344
345 $this->entity = $obj->entity;
346 $this->datec = $this->db->jdate($obj->datec);
347 $this->tms = $this->db->jdate($obj->tms);
348 $this->fk_product = $obj->fk_product;
349 $this->fk_soc = $obj->fk_soc;
350 $this->ref_customer = $obj->ref_customer;
351 $this->price = $obj->price;
352 $this->price_ttc = $obj->price_ttc;
353 $this->price_min = $obj->price_min;
354 $this->price_min_ttc = $obj->price_min_ttc;
355 $this->price_base_type = $obj->price_base_type;
356 $this->default_vat_code = $obj->default_vat_code;
357 $this->tva_tx = $obj->tva_tx;
358 $this->recuperableonly = $obj->recuperableonly;
359 $this->localtax1_tx = $obj->localtax1_tx;
360 $this->localtax2_tx = $obj->localtax2_tx;
361 $this->fk_user = $obj->fk_user;
362 $this->price_label = $obj->price_label;
363 $this->import_key = $obj->import_key;
364
365 $this->db->free($resql);
366
367 return 1;
368 } else {
369 $this->db->free($resql);
370
371 return 0;
372 }
373 } else {
374 $this->error = "Error ".$this->db->lasterror();
375 return -1;
376 }
377 }
378
390 public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, $filter = '')
391 {
392 if (empty($sortfield)) {
393 $sortfield = "t.rowid";
394 }
395 if (empty($sortorder)) {
396 $sortorder = "DESC";
397 }
398
399 $sql = "SELECT";
400 $sql .= " t.rowid,";
401 $sql .= " t.entity,";
402 $sql .= " t.datec,";
403 $sql .= " t.tms,";
404 $sql .= " t.fk_product,";
405 $sql .= " t.fk_soc,";
406 $sql .= " t.ref_customer,";
407 $sql .= " t.price,";
408 $sql .= " t.price_ttc,";
409 $sql .= " t.price_min,";
410 $sql .= " t.price_min_ttc,";
411 $sql .= " t.price_base_type,";
412 $sql .= " t.default_vat_code,";
413 $sql .= " t.tva_tx,";
414 $sql .= " t.recuperableonly,";
415 $sql .= " t.localtax1_tx,";
416 $sql .= " t.localtax2_tx,";
417 $sql .= " t.localtax1_type,";
418 $sql .= " t.localtax2_type,";
419 $sql .= " t.fk_user,";
420 $sql .= " t.price_label,";
421 $sql .= " t.import_key,";
422 $sql .= " soc.nom as socname,";
423 $sql .= " prod.ref as prodref";
424 $sql .= " FROM ".$this->db->prefix()."product_customer_price as t,";
425 $sql .= " ".$this->db->prefix()."product as prod,";
426 $sql .= " ".$this->db->prefix()."societe as soc";
427 $sql .= " WHERE soc.rowid=t.fk_soc ";
428 $sql .= " AND prod.rowid=t.fk_product ";
429 $sql .= " AND prod.entity IN (".getEntity('product').")";
430 $sql .= " AND t.entity IN (".getEntity('productprice').")";
431
432 // Manage filter
433 if (is_array($filter)) {
434 if (count($filter) > 0) {
435 foreach ($filter as $key => $value) {
436 if (strpos($key, 'date')) { // To allow $filter['YEAR(s.dated)']=>$year
437 $sql .= " AND ".$this->db->sanitize($key)." = '".$this->db->escape($value)."'";
438 } elseif ($key == 'soc.nom') {
439 $sql .= " AND ".$this->db->sanitize($key)." LIKE '%".$this->db->escape($this->db->escapeforlike($value))."%'";
440 } elseif ($key == 'prod.ref' || $key == 'prod.label') {
441 $sql .= " AND ".$this->db->sanitize($key)." LIKE '%".$this->db->escape($this->db->escapeforlike($value))."%'";
442 } elseif ($key == 't.price' || $key == 't.price_ttc') {
443 $sql .= " AND ".$this->db->sanitize($key)." = ".((float) price2num($value));
444 } else {
445 $sql .= " AND ".$this->db->sanitize($key)." = ".((int) $value);
446 }
447 }
448 }
449
450 $filter = '';
451 }
452
453 // Manage filter
454 $errormessage = '';
455 $sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);
456 if ($errormessage) {
457 $this->errors[] = $errormessage;
458 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
459 return -1;
460 }
461
462 $sql .= $this->db->order($sortfield, $sortorder);
463 if (!empty($limit)) {
464 $sql .= $this->db->plimit($limit + 1, $offset);
465 }
466
467 dol_syslog(get_class($this)."::fetchAll", LOG_DEBUG);
468 $resql = $this->db->query($sql);
469 if ($resql) {
470 $this->lines = array();
471 $num = $this->db->num_rows($resql);
472
473 while ($obj = $this->db->fetch_object($resql)) {
474 $line = new PriceByCustomerLine($this->db);
475
476 $line->id = $obj->rowid;
477
478 $line->entity = $obj->entity;
479 $line->datec = $this->db->jdate($obj->datec);
480 $line->tms = $this->db->jdate($obj->tms);
481 $line->fk_product = $obj->fk_product;
482 $line->fk_soc = $obj->fk_soc;
483 $line->ref_customer = $obj->ref_customer;
484 $line->price = $obj->price;
485 $line->price_ttc = $obj->price_ttc;
486 $line->price_min = $obj->price_min;
487 $line->price_min_ttc = $obj->price_min_ttc;
488 $line->price_base_type = $obj->price_base_type;
489 $line->default_vat_code = $obj->default_vat_code;
490 $line->tva_tx = $obj->tva_tx;
491 $line->recuperableonly = $obj->recuperableonly;
492 $line->localtax1_tx = $obj->localtax1_tx;
493 $line->localtax2_tx = $obj->localtax2_tx;
494 $line->localtax1_type = $obj->localtax1_type;
495 $line->localtax2_type = $obj->localtax2_type;
496 $line->fk_user = $obj->fk_user;
497 $line->price_label = $obj->price_label;
498 $line->import_key = $obj->import_key;
499 $line->socname = $obj->socname;
500 $line->prodref = $obj->prodref;
501
502 $this->lines[] = $line;
503 }
504 $this->db->free($resql);
505
506 return $num;
507 } else {
508 $this->error = "Error ".$this->db->lasterror();
509 return -1;
510 }
511 }
512
523 public function fetchAllLog($sortorder, $sortfield, $limit, $offset, $filter = array())
524 {
525 if (!empty($sortfield)) {
526 $sortfield = "t.rowid";
527 }
528 if (!empty($sortorder)) {
529 $sortorder = "DESC";
530 }
531
532 $sql = "SELECT";
533 $sql .= " t.rowid,";
534 $sql .= " t.entity,";
535 $sql .= " t.datec,";
536 $sql .= " t.fk_product,";
537 $sql .= " t.fk_soc,";
538 $sql .= " t.ref_customer,";
539 $sql .= " t.price,";
540 $sql .= " t.price_ttc,";
541 $sql .= " t.price_min,";
542 $sql .= " t.price_min_ttc,";
543 $sql .= " t.price_base_type,";
544 $sql .= " t.default_vat_code,";
545 $sql .= " t.tva_tx,";
546 $sql .= " t.recuperableonly,";
547 $sql .= " t.localtax1_tx,";
548 $sql .= " t.localtax2_tx,";
549 $sql .= " t.fk_user,";
550 $sql .= " t.price_label,";
551 $sql .= " t.import_key,";
552 $sql .= " soc.nom as socname,";
553 $sql .= " prod.ref as prodref";
554 $sql .= " FROM ".$this->db->prefix()."product_customer_price_log as t";
555 $sql .= " ,".$this->db->prefix()."product as prod";
556 $sql .= " ,".$this->db->prefix()."societe as soc";
557 $sql .= " WHERE soc.rowid=t.fk_soc";
558 $sql .= " AND prod.rowid=t.fk_product ";
559 $sql .= " AND prod.entity IN (".getEntity('product').")";
560 $sql .= " AND t.entity IN (".getEntity('productprice').")";
561 // Manage filter
562 if (count($filter) > 0) {
563 foreach ($filter as $key => $value) {
564 if (strpos($key, 'date')) { // To allow $filter['YEAR(s.dated)']=>$year
565 $sql .= " AND ".$key." = '".$this->db->escape($value)."'";
566 } elseif ($key == 'soc.nom') {
567 $sql .= " AND ".$key." LIKE '%".$this->db->escape($value)."%'";
568 } else {
569 $sql .= " AND ".$key." = ".((int) $value);
570 }
571 }
572 }
573 $sql .= $this->db->order($sortfield, $sortorder);
574 if (!empty($limit)) {
575 $sql .= $this->db->plimit($limit + 1, $offset);
576 }
577
578 dol_syslog(get_class($this)."::fetchAllLog", LOG_DEBUG);
579 $resql = $this->db->query($sql);
580 if ($resql) {
581 $this->lines = array();
582 $num = $this->db->num_rows($resql);
583
584 while ($obj = $this->db->fetch_object($resql)) {
585 $line = new PriceByCustomerLine($this->db);
586
587 $line->id = $obj->rowid;
588
589 $line->entity = $obj->entity;
590 $line->datec = $this->db->jdate($obj->datec);
591 $line->tms = $this->db->jdate($obj->tms);
592 $line->fk_product = $obj->fk_product;
593 $line->fk_soc = $obj->fk_soc;
594 $line->ref_customer = $obj->ref_customer;
595 $line->price = $obj->price;
596 $line->price_ttc = $obj->price_ttc;
597 $line->price_min = $obj->price_min;
598 $line->price_min_ttc = $obj->price_min_ttc;
599 $line->price_base_type = $obj->price_base_type;
600 $line->default_vat_code = $obj->default_vat_code;
601 $line->tva_tx = $obj->tva_tx;
602 $line->recuperableonly = $obj->recuperableonly;
603 $line->localtax1_tx = $obj->localtax1_tx;
604 $line->localtax2_tx = $obj->localtax2_tx;
605 $line->fk_user = $obj->fk_user;
606 $line->price_label = $obj->price_label;
607 $line->import_key = $obj->import_key;
608 $line->socname = $obj->socname;
609 $line->prodref = $obj->prodref;
610
611 $this->lines [] = $line;
612 }
613 $this->db->free($resql);
614
615 return $num;
616 } else {
617 $this->error = "Error ".$this->db->lasterror();
618 return -1;
619 }
620 }
621
630 public function update(User $user, $notrigger = 0, $forceupdateaffiliate = 0)
631 {
632 global $conf, $langs;
633 $error = 0;
634
635 // Clean parameters
636
637 if (isset($this->entity)) {
638 $this->entity = (int) $this->entity;
639 }
640 if (isset($this->fk_product)) {
641 $this->fk_product = (int) $this->fk_product;
642 }
643 if (isset($this->fk_soc)) {
644 $this->fk_soc = (int) $this->fk_soc;
645 }
646 if (isset($this->ref_customer)) {
647 $this->ref_customer = trim($this->ref_customer);
648 }
649 if (isset($this->price)) {
650 $this->price = trim($this->price);
651 }
652 if (isset($this->price_ttc)) {
653 $this->price_ttc = trim($this->price_ttc);
654 }
655 if (isset($this->price_min)) {
656 $this->price_min = trim($this->price_min);
657 }
658 if (isset($this->price_min_ttc)) {
659 $this->price_min_ttc = trim($this->price_min_ttc);
660 }
661 if (isset($this->price_base_type)) {
662 $this->price_base_type = trim($this->price_base_type);
663 }
664 if (isset($this->tva_tx)) {
665 $this->tva_tx = (float) $this->tva_tx;
666 }
667 if (isset($this->recuperableonly)) {
668 $this->recuperableonly = trim($this->recuperableonly);
669 }
670 if (isset($this->localtax1_tx)) {
671 $this->localtax1_tx = trim($this->localtax1_tx);
672 }
673 if (isset($this->localtax2_tx)) {
674 $this->localtax2_tx = trim($this->localtax2_tx);
675 }
676 if (isset($this->fk_user)) {
677 $this->fk_user = (int) $this->fk_user;
678 }
679 if (isset($this->price_label)) {
680 $this->price_label = trim($this->price_label);
681 }
682 if (isset($this->import_key)) {
683 $this->import_key = trim($this->import_key);
684 }
685
686 // Check parameters
687 // Put here code to add a control on parameters values
688
689 if ($this->price != '' || $this->price == 0) {
690 if ($this->price_base_type == 'TTC') {
691 $this->price_ttc = price2num($this->price, 'MU');
692 $this->price = price2num($this->price) / (1 + ($this->tva_tx / 100));
693 $this->price = price2num($this->price, 'MU');
694
695 if ($this->price_min != '' || $this->price_min == 0) {
696 $this->price_min_ttc = price2num($this->price_min, 'MU');
697 $this->price_min = price2num($this->price_min) / (1 + ($this->tva_tx / 100));
698 $this->price_min = price2num($this->price_min, 'MU');
699 } else {
700 $this->price_min = 0;
701 $this->price_min_ttc = 0;
702 }
703 } else {
704 $this->price = price2num($this->price, 'MU');
705 $this->price_ttc = ($this->recuperableonly != 1) ? price2num($this->price) * (1 + ($this->tva_tx / 100)) : $this->price;
706 $this->price_ttc = price2num($this->price_ttc, 'MU');
707
708 if ($this->price_min != '' || $this->price_min == 0) {
709 $this->price_min = price2num($this->price_min, 'MU');
710 $this->price_min_ttc = price2num($this->price_min) * (1 + ($this->tva_tx / 100));
711 $this->price_min_ttc = price2num($this->price_min_ttc, 'MU');
712 // print 'X'.$newminprice.'-'.$price_min;
713 } else {
714 $this->price_min = 0;
715 $this->price_min_ttc = 0;
716 }
717 }
718 }
719
720 // Do a copy of current record into log table
721 // Insert request
722 $sql = "INSERT INTO ".$this->db->prefix()."product_customer_price_log(";
723
724 $sql .= "entity,";
725 $sql .= "datec,";
726 $sql .= "fk_product,";
727 $sql .= "fk_soc,";
728 $sql .= "ref_customer,";
729 $sql .= "price,";
730 $sql .= "price_ttc,";
731 $sql .= "price_min,";
732 $sql .= "price_min_ttc,";
733 $sql .= "price_base_type,";
734 $sql .= "default_vat_code,";
735 $sql .= "tva_tx,";
736 $sql .= "recuperableonly,";
737 $sql .= "localtax1_tx,";
738 $sql .= "localtax2_tx,";
739 $sql .= "localtax1_type,";
740 $sql .= "localtax2_type,";
741 $sql .= "fk_user,";
742 $sql .= "price_label,";
743 $sql .= "import_key";
744
745 $sql .= ") ";
746 $sql .= "SELECT";
747
748 $sql .= " t.entity,";
749 $sql .= " t.datec,";
750 $sql .= " t.fk_product,";
751 $sql .= " t.fk_soc,";
752 $sql .= " t.ref_customer,";
753 $sql .= " t.price,";
754 $sql .= " t.price_ttc,";
755 $sql .= " t.price_min,";
756 $sql .= " t.price_min_ttc,";
757 $sql .= " t.price_base_type,";
758 $sql .= " t.default_vat_code,";
759 $sql .= " t.tva_tx,";
760 $sql .= " t.recuperableonly,";
761 $sql .= " t.localtax1_tx,";
762 $sql .= " t.localtax2_tx,";
763 $sql .= " t.localtax1_type,";
764 $sql .= " t.localtax2_type,";
765 $sql .= " t.fk_user,";
766 $sql .= " t.price_label,";
767 $sql .= " t.import_key";
768
769 $sql .= " FROM ".$this->db->prefix()."product_customer_price as t";
770 $sql .= " WHERE t.rowid = ".((int) $this->id);
771
772 $this->db->begin();
773 dol_syslog(get_class($this)."::update", LOG_DEBUG);
774 $resql = $this->db->query($sql);
775 if (!$resql) {
776 $error++;
777 $this->errors [] = "Error ".$this->db->lasterror();
778 }
779
780 // Update request
781 $sql = "UPDATE ".$this->db->prefix()."product_customer_price SET";
782
783 $sql .= " entity=".$conf->entity.",";
784 $sql .= " datec='".$this->db->idate(dol_now())."',";
785 $sql .= " tms=".(dol_strlen($this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
786 $sql .= " fk_product=".(isset($this->fk_product) ? $this->fk_product : "null").",";
787 $sql .= " fk_soc=".(isset($this->fk_soc) ? $this->fk_soc : "null").",";
788 $sql .= " ref_customer=".(isset($this->ref_customer) ? "'".$this->db->escape($this->ref_customer)."'" : "null").",";
789 $sql .= " price=".(isset($this->price) ? $this->price : "null").",";
790 $sql .= " price_ttc=".(isset($this->price_ttc) ? $this->price_ttc : "null").",";
791 $sql .= " price_min=".(isset($this->price_min) ? $this->price_min : "null").",";
792 $sql .= " price_min_ttc=".(isset($this->price_min_ttc) ? $this->price_min_ttc : "null").",";
793 $sql .= " price_base_type=".(isset($this->price_base_type) ? "'".$this->db->escape($this->price_base_type)."'" : "null").",";
794 $sql .= " default_vat_code = ".($this->default_vat_code ? "'".$this->db->escape($this->default_vat_code)."'" : "null").",";
795 $sql .= " tva_tx=".(isset($this->tva_tx) ? (empty($this->tva_tx) ? 0 : $this->tva_tx) : "null").",";
796 $sql .= " recuperableonly=".(isset($this->recuperableonly) ? $this->recuperableonly : "null").",";
797 $sql .= " localtax1_tx=".(isset($this->localtax1_tx) ? (empty($this->localtax1_tx) ? 0 : $this->localtax1_tx) : "null").",";
798 $sql .= " localtax2_tx=".(isset($this->localtax2_tx) ? (empty($this->localtax2_tx) ? 0 : $this->localtax2_tx) : "null").",";
799 $sql .= " localtax1_type=".(!empty($this->localtax1_type) ? "'".$this->db->escape($this->localtax1_type)."'" : "'0'").",";
800 $sql .= " localtax2_type=".(!empty($this->localtax2_type) ? "'".$this->db->escape($this->localtax2_type)."'" : "'0'").",";
801 $sql .= " fk_user=".$user->id.",";
802 $sql .= " price_label=".(isset($this->price_label) ? "'".$this->db->escape($this->price_label)."'" : "null").",";
803 $sql .= " import_key=".(isset($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null");
804
805 $sql .= " WHERE rowid=".((int) $this->id);
806
807 dol_syslog(get_class($this)."::update", LOG_DEBUG);
808 $resql = $this->db->query($sql);
809 if (!$resql) {
810 $error++;
811 $this->errors [] = "Error ".$this->db->lasterror();
812 }
813
814 if (!$error && !$notrigger) {
815 // Call trigger
816 $result = $this->call_trigger('PRODUCT_CUSTOMER_PRICE_MODIFY', $user);
817 if ($result < 0) {
818 $error++;
819 }
820 // End call triggers
821 }
822
823 if (!$error) {
824 $result = $this->setPriceOnAffiliateThirdparty($user, $forceupdateaffiliate);
825 if ($result < 0) {
826 $error++;
827 }
828 }
829
830 // Commit or rollback
831 if ($error) {
832 foreach ($this->errors as $errmsg) {
833 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
834 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
835 }
836 $this->db->rollback();
837 return -1 * $error;
838 } else {
839 $this->db->commit();
840 return 1;
841 }
842 }
843
851 public function setPriceOnAffiliateThirdparty($user, $forceupdateaffiliate)
852 {
853 global $conf;
854
855 if (getDolGlobalString('PRODUCT_DISABLE_PROPAGATE_CUSTOMER_PRICES_ON_CHILD_COMPANIES')) {
856 return 0;
857 }
858
859 $error = 0;
860
861 // Find all susidiaries
862 $sql = "SELECT s.rowid";
863 $sql .= " FROM ".$this->db->prefix()."societe as s";
864 $sql .= " WHERE s.parent = ".((int) $this->fk_soc);
865 $sql .= " AND s.entity IN (".getEntity('societe').")";
866
867 dol_syslog(get_class($this)."::setPriceOnAffiliateThirdparty", LOG_DEBUG);
868 $resql = $this->db->query($sql);
869
870 if ($resql) {
871 $this->lines = array();
872 $num = $this->db->num_rows($resql);
873
874 while (($obj = $this->db->fetch_object($resql)) && (empty($error))) {
875 // find if there is an existing line for the product and the subsidiaries
876 $prodsocprice = new ProductCustomerPrice($this->db);
877
878 $filter = array(
879 't.fk_product' => $this->fk_product, 't.fk_soc' => $obj->rowid
880 );
881
882 $result = $prodsocprice->fetchAll('', '', 0, 0, $filter);
883 if ($result < 0) {
884 $error++;
885 $this->error = $prodsocprice->error;
886 } else {
887 // There is one line
888 if (count($prodsocprice->lines) > 0) {
889 // If force update => Update
890 if (!empty($forceupdateaffiliate)) {
891 $prodsocpriceupd = new ProductCustomerPrice($this->db);
892 $prodsocpriceupd->fetch($prodsocprice->lines [0]->id);
893
894 $prodsocpriceupd->price = $this->price;
895 $prodsocpriceupd->price_min = $this->price_min;
896 $prodsocpriceupd->price_base_type = $this->price_base_type;
897 $prodsocpriceupd->tva_tx = $this->tva_tx;
898 $prodsocpriceupd->recuperableonly = $this->recuperableonly;
899 $prodsocpriceupd->price_label = $this->price_label;
900
901 $resultupd = $prodsocpriceupd->update($user, 0, $forceupdateaffiliate);
902 if ($resultupd < 0) {
903 $error++;
904 $this->error = $prodsocpriceupd->error;
905 }
906 }
907 } else {
908 // If line do not exits then create it
909 $prodsocpricenew = new ProductCustomerPrice($this->db);
910 $prodsocpricenew->fk_soc = $obj->rowid;
911 $prodsocpricenew->ref_customer = $obj->ref_customer;
912 $prodsocpricenew->fk_product = $this->fk_product;
913 $prodsocpricenew->price = $this->price;
914 $prodsocpricenew->price_min = $this->price_min;
915 $prodsocpricenew->price_base_type = $this->price_base_type;
916 $prodsocpricenew->tva_tx = $this->tva_tx;
917 $prodsocpricenew->recuperableonly = $this->recuperableonly;
918 $prodsocpricenew->price_label = $this->price_label;
919
920 $resultupd = $prodsocpricenew->create($user, 0, $forceupdateaffiliate);
921 if ($resultupd < 0) {
922 $error++;
923 $this->error = $prodsocpricenew->error;
924 }
925 }
926 }
927 }
928 $this->db->free($resql);
929
930 if (empty($error)) {
931 return 1;
932 } else {
933 return -1;
934 }
935 } else {
936 $this->error = "Error ".$this->db->lasterror();
937 return -1;
938 }
939 }
940
948 public function delete($user, $notrigger = 0)
949 {
950 global $conf, $langs;
951 $error = 0;
952
953 $this->db->begin();
954
955 if (!$notrigger) {
956 $result = $this->call_trigger('PRODUCT_CUSTOMER_PRICE_DELETE', $user);
957 if ($result < 0) {
958 $error++;
959 }
960 }
961
962 if (!$error) {
963 $sql = "DELETE FROM ".$this->db->prefix()."product_customer_price";
964 $sql .= " WHERE rowid=".((int) $this->id);
965
966 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
967 $resql = $this->db->query($sql);
968 if (!$resql) {
969 $error++;
970 $this->errors [] = "Error ".$this->db->lasterror();
971 }
972 }
973
974 // Commit or rollback
975 if ($error) {
976 foreach ($this->errors as $errmsg) {
977 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
978 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
979 }
980 $this->db->rollback();
981 return -1 * $error;
982 } else {
983 $this->db->commit();
984 return 1;
985 }
986 }
987
995 public function createFromClone(User $user, $fromid)
996 {
997 $error = 0;
998
999 $object = new ProductCustomerPrice($this->db);
1000
1001 $this->db->begin();
1002
1003 // Load source object
1004 $object->fetch($fromid);
1005 $object->id = 0;
1006 $object->statut = 0;
1007
1008 // Clear fields
1009 // ...
1010
1011 // Create clone
1012 $object->context['createfromclone'] = 'createfromclone';
1013 $result = $object->create($user);
1014
1015 // Other options
1016 if ($result < 0) {
1017 $this->error = $object->error;
1018 $this->errors = array_merge($this->errors, $object->errors);
1019 $error++;
1020 }
1021
1022 if (!$error) {
1023 }
1024
1025 unset($object->context['createfromclone']);
1026
1027 // End
1028 if (!$error) {
1029 $this->db->commit();
1030 return $object->id;
1031 } else {
1032 $this->db->rollback();
1033 return -1;
1034 }
1035 }
1036
1043 public function initAsSpecimen()
1044 {
1045 $this->id = 0;
1046
1047 $this->entity = 0;
1048 $this->datec = '';
1049 $this->tms = dol_now();
1050 $this->fk_product = 0;
1051 $this->fk_soc = 0;
1052 $this->ref_customer = '';
1053 $this->price = '';
1054 $this->price_ttc = '';
1055 $this->price_min = '';
1056 $this->price_min_ttc = '';
1057 $this->price_base_type = '';
1058 $this->default_vat_code = '';
1059 $this->tva_tx = '';
1060 $this->recuperableonly = '';
1061 $this->localtax1_tx = '';
1062 $this->localtax2_tx = '';
1063 $this->fk_user = 0;
1064 $this->price_label = '';
1065 $this->import_key = '';
1066
1067 return 1;
1068 }
1069}
1070
1075{
1079 public $id;
1080
1084 public $entity;
1085
1086 public $datec = '';
1087
1091 public $fk_product;
1092
1096 public $ref_customer;
1097
1101 public $fk_soc;
1102
1103 public $price;
1104 public $price_ttc;
1105 public $price_min;
1106 public $price_min_ttc;
1107 public $price_base_type;
1108 public $default_vat_code;
1109 public $tva_tx;
1110 public $recuperableonly;
1111 public $localtax1_tx;
1112 public $localtax2_tx;
1113
1117 public $fk_user;
1118 public $price_label;
1119
1120 public $import_key;
1121 public $socname;
1122 public $prodref;
1123}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
Parent class of all other business classes (invoices, contracts, proposals, orders,...
call_trigger($triggerName, $user)
Call trigger based on this instance.
Parent class for class inheritance lines of business objects This class is useless for the moment so ...
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.
create($user, $notrigger=0, $forceupdateaffiliate=0)
Create object into 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.
fetchAllLog($sortorder, $sortfield, $limit, $offset, $filter=array())
Load all objects in memory from database.
update(User $user, $notrigger=0, $forceupdateaffiliate=0)
Update object into database.
createFromClone(User $user, $fromid)
Load an object from its id and create a new one in database.
fetchAll($sortorder='', $sortfield='', $limit=0, $offset=0, $filter='')
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.
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.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
dol_now($mode='auto')
Return date for now.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.