dolibarr 21.0.0-beta
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
70 public $datec = '';
71
75 public $fk_product;
76
80 public $fk_soc;
81
85 public $ref_customer;
86
90 public $price;
94 public $price_ttc;
98 public $price_min;
102 public $price_min_ttc;
106 public $price_base_type;
110 public $default_vat_code;
114 public $tva_tx;
118 public $recuperableonly;
122 public $localtax1_type;
126 public $localtax1_tx;
130 public $localtax2_type;
134 public $localtax2_tx;
138 public $price_label;
139
143 public $fk_user;
144
148 public $lines = array();
149
150
156 public function __construct($db)
157 {
158 $this->db = $db;
159 }
160
169 public function create($user, $notrigger = 0, $forceupdateaffiliate = 0)
170 {
171 global $conf, $langs;
172 $error = 0;
173
174 // Clean parameters
175
176 if (isset($this->entity)) {
177 $this->entity = (int) $this->entity;
178 }
179 if (isset($this->fk_product)) {
180 $this->fk_product = (int) $this->fk_product;
181 }
182 if (isset($this->fk_soc)) {
183 $this->fk_soc = (int) $this->fk_soc;
184 }
185 if (isset($this->ref_customer)) {
186 $this->ref_customer = trim($this->ref_customer);
187 }
188 if (isset($this->price)) {
189 $this->price = trim($this->price);
190 }
191 if (isset($this->price_ttc)) {
192 $this->price_ttc = trim($this->price_ttc);
193 }
194 if (isset($this->price_min)) {
195 $this->price_min = trim($this->price_min);
196 }
197 if (isset($this->price_min_ttc)) {
198 $this->price_min_ttc = trim($this->price_min_ttc);
199 }
200 if (isset($this->price_base_type)) {
201 $this->price_base_type = trim($this->price_base_type);
202 }
203 if (isset($this->tva_tx)) {
204 $this->tva_tx = trim($this->tva_tx);
205 }
206 if (isset($this->recuperableonly)) {
207 $this->recuperableonly = trim($this->recuperableonly);
208 }
209 if (isset($this->localtax1_tx)) {
210 $this->localtax1_tx = trim($this->localtax1_tx);
211 }
212 if (isset($this->localtax2_tx)) {
213 $this->localtax2_tx = trim($this->localtax2_tx);
214 }
215 if (isset($this->fk_user)) {
216 $this->fk_user = (int) $this->fk_user;
217 }
218 if (isset($this->price_label)) {
219 $this->price_label = trim($this->price_label);
220 }
221 if (isset($this->import_key)) {
222 $this->import_key = trim($this->import_key);
223 }
224
225 // Check parameters
226 // Put here code to add control on parameters values
227
228 if ($this->price != '' || $this->price == 0) {
229 $vatRate = (float) $this->tva_tx;
230 if ($this->price_base_type == 'TTC') {
231 $this->price_ttc = price2num($this->price, 'MU');
232 $this->price = (float) price2num($this->price) / (1 + ($vatRate / 100));
233 $this->price = price2num($this->price, 'MU');
234
235 if ($this->price_min != '' || $this->price_min == 0) {
236 $this->price_min_ttc = price2num($this->price_min, 'MU');
237 $this->price_min = (float) price2num($this->price_min) / (1 + ($vatRate / 100));
238 $this->price_min = price2num($this->price_min, 'MU');
239 } else {
240 $this->price_min = 0;
241 $this->price_min_ttc = 0;
242 }
243 } else {
244 $this->price = price2num($this->price, 'MU');
245 $this->price_ttc = ($this->recuperableonly != 1) ? (float) price2num($this->price) * (1 + ($vatRate / 100)) : $this->price;
246 $this->price_ttc = price2num($this->price_ttc, 'MU');
247
248 if ($this->price_min != '' || $this->price_min == 0) {
249 $this->price_min = price2num($this->price_min, 'MU');
250 $this->price_min_ttc = (float) price2num($this->price_min) * (1 + ($vatRate / 100));
251 $this->price_min_ttc = price2num($this->price_min_ttc, 'MU');
252 // print 'X'.$newminprice.'-'.$price_min;
253 } else {
254 $this->price_min = 0;
255 $this->price_min_ttc = 0;
256 }
257 }
258 }
259
260 // Insert request
261 $sql = "INSERT INTO ".$this->db->prefix()."product_customer_price(";
262 $sql .= "entity,";
263 $sql .= "datec,";
264 $sql .= "fk_product,";
265 $sql .= "fk_soc,";
266 $sql .= 'ref_customer,';
267 $sql .= "price,";
268 $sql .= "price_ttc,";
269 $sql .= "price_min,";
270 $sql .= "price_min_ttc,";
271 $sql .= "price_base_type,";
272 $sql .= "default_vat_code,";
273 $sql .= "tva_tx,";
274 $sql .= "recuperableonly,";
275 $sql .= "localtax1_type,";
276 $sql .= "localtax1_tx,";
277 $sql .= "localtax2_type,";
278 $sql .= "localtax2_tx,";
279 $sql .= "fk_user,";
280 $sql .= "price_label,";
281 $sql .= "import_key";
282 $sql .= ") VALUES (";
283 $sql .= " ".((int) $conf->entity).",";
284 $sql .= " '".$this->db->idate(dol_now())."',";
285 $sql .= " ".(!isset($this->fk_product) ? 'NULL' : "'".$this->db->escape($this->fk_product)."'").",";
286 $sql .= " ".(!isset($this->fk_soc) ? 'NULL' : "'".$this->db->escape($this->fk_soc)."'").",";
287 $sql .= " ".(!isset($this->ref_customer) ? 'NULL' : "'".$this->db->escape($this->ref_customer)."'").",";
288 $sql .= " ".(empty($this->price) ? '0' : "'".$this->db->escape($this->price)."'").",";
289 $sql .= " ".(empty($this->price_ttc) ? '0' : "'".$this->db->escape($this->price_ttc)."'").",";
290 $sql .= " ".(empty($this->price_min) ? '0' : "'".$this->db->escape($this->price_min)."'").",";
291 $sql .= " ".(empty($this->price_min_ttc) ? '0' : "'".$this->db->escape($this->price_min_ttc)."'").",";
292 $sql .= " ".(!isset($this->price_base_type) ? 'NULL' : "'".$this->db->escape($this->price_base_type)."'").",";
293 $sql .= " ".($this->default_vat_code ? "'".$this->db->escape($this->default_vat_code)."'" : "null").",";
294 $sql .= " ".(!isset($this->tva_tx) ? 'NULL' : (empty($this->tva_tx) ? 0 : $this->tva_tx)).",";
295 $sql .= " ".(!isset($this->recuperableonly) ? 'NULL' : "'".$this->db->escape($this->recuperableonly)."'").",";
296 $sql .= " ".(empty($this->localtax1_type) ? "'0'" : "'".$this->db->escape($this->localtax1_type)."'").",";
297 $sql .= " ".(!isset($this->localtax1_tx) ? 'NULL' : (empty($this->localtax1_tx) ? 0 : $this->localtax1_tx)).",";
298 $sql .= " ".(empty($this->localtax2_type) ? "'0'" : "'".$this->db->escape($this->localtax2_type)."'").",";
299 $sql .= " ".(!isset($this->localtax2_tx) ? 'NULL' : (empty($this->localtax2_tx) ? 0 : $this->localtax2_tx)).",";
300 $sql .= " ".((int) $user->id).",";
301 $sql .= " ".(!isset($this->price_label) ? 'NULL' : "'".$this->db->escape($this->price_label)."'").",";
302 $sql .= " ".(!isset($this->import_key) ? 'NULL' : "'".$this->db->escape($this->import_key)."'");
303 $sql .= ")";
304
305 $this->db->begin();
306
307 dol_syslog(get_class($this)."::create", LOG_DEBUG);
308 $resql = $this->db->query($sql);
309 if (!$resql) {
310 $error++;
311 $this->errors [] = "Error ".$this->db->lasterror();
312 }
313
314 if (!$error) {
315 $this->id = $this->db->last_insert_id($this->db->prefix()."product_customer_price");
316
317 if (!$notrigger) {
318 $result = $this->call_trigger('PRODUCT_CUSTOMER_PRICE_CREATE', $user);
319 if ($result < 0) {
320 $error++;
321 }
322 }
323 }
324
325 if (!$error) {
326 $result = $this->setPriceOnAffiliateThirdparty($user, $forceupdateaffiliate);
327 if ($result < 0) {
328 $error++;
329 }
330 }
331
332 // Commit or rollback
333 if ($error) {
334 foreach ($this->errors as $errmsg) {
335 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
336 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
337 }
338 $this->db->rollback();
339 return -1 * $error;
340 } else {
341 $this->db->commit();
342 return $this->id;
343 }
344 }
345
352 public function fetch($id)
353 {
354 global $langs;
355
356 $sql = "SELECT";
357 $sql .= " t.rowid,";
358 $sql .= " t.entity,";
359 $sql .= " t.datec,";
360 $sql .= " t.tms,";
361 $sql .= " t.fk_product,";
362 $sql .= " t.fk_soc,";
363 $sql .= " t.ref_customer,";
364 $sql .= " t.price,";
365 $sql .= " t.price_ttc,";
366 $sql .= " t.price_min,";
367 $sql .= " t.price_min_ttc,";
368 $sql .= " t.price_base_type,";
369 $sql .= " t.default_vat_code,";
370 $sql .= " t.tva_tx,";
371 $sql .= " t.recuperableonly,";
372 $sql .= " t.localtax1_tx,";
373 $sql .= " t.localtax2_tx,";
374 $sql .= " t.fk_user,";
375 $sql .= " t.price_label,";
376 $sql .= " t.import_key";
377 $sql .= " FROM ".$this->db->prefix()."product_customer_price as t";
378 $sql .= " WHERE t.rowid = ".((int) $id);
379
380 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
381 $resql = $this->db->query($sql);
382 if ($resql) {
383 if ($this->db->num_rows($resql)) {
384 $obj = $this->db->fetch_object($resql);
385
386 $this->id = $obj->rowid;
387
388 $this->entity = $obj->entity;
389 $this->datec = $this->db->jdate($obj->datec);
390 $this->tms = $this->db->jdate($obj->tms);
391 $this->fk_product = $obj->fk_product;
392 $this->fk_soc = $obj->fk_soc;
393 $this->ref_customer = $obj->ref_customer;
394 $this->price = $obj->price;
395 $this->price_ttc = $obj->price_ttc;
396 $this->price_min = $obj->price_min;
397 $this->price_min_ttc = $obj->price_min_ttc;
398 $this->price_base_type = $obj->price_base_type;
399 $this->default_vat_code = $obj->default_vat_code;
400 $this->tva_tx = $obj->tva_tx;
401 $this->recuperableonly = $obj->recuperableonly;
402 $this->localtax1_tx = $obj->localtax1_tx;
403 $this->localtax2_tx = $obj->localtax2_tx;
404 $this->fk_user = $obj->fk_user;
405 $this->price_label = $obj->price_label;
406 $this->import_key = $obj->import_key;
407
408 $this->db->free($resql);
409
410 return 1;
411 } else {
412 $this->db->free($resql);
413
414 return 0;
415 }
416 } else {
417 $this->error = "Error ".$this->db->lasterror();
418 return -1;
419 }
420 }
421
433 public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, $filter = '')
434 {
435 if (empty($sortfield)) {
436 $sortfield = "t.rowid";
437 }
438 if (empty($sortorder)) {
439 $sortorder = "DESC";
440 }
441
442 $sql = "SELECT";
443 $sql .= " t.rowid,";
444 $sql .= " t.entity,";
445 $sql .= " t.datec,";
446 $sql .= " t.tms,";
447 $sql .= " t.fk_product,";
448 $sql .= " t.fk_soc,";
449 $sql .= " t.ref_customer,";
450 $sql .= " t.price,";
451 $sql .= " t.price_ttc,";
452 $sql .= " t.price_min,";
453 $sql .= " t.price_min_ttc,";
454 $sql .= " t.price_base_type,";
455 $sql .= " t.default_vat_code,";
456 $sql .= " t.tva_tx,";
457 $sql .= " t.recuperableonly,";
458 $sql .= " t.localtax1_tx,";
459 $sql .= " t.localtax2_tx,";
460 $sql .= " t.localtax1_type,";
461 $sql .= " t.localtax2_type,";
462 $sql .= " t.fk_user,";
463 $sql .= " t.price_label,";
464 $sql .= " t.import_key,";
465 $sql .= " soc.nom as socname,";
466 $sql .= " prod.ref as prodref";
467 $sql .= " FROM ".$this->db->prefix()."product_customer_price as t,";
468 $sql .= " ".$this->db->prefix()."product as prod,";
469 $sql .= " ".$this->db->prefix()."societe as soc";
470 $sql .= " WHERE soc.rowid=t.fk_soc ";
471 $sql .= " AND prod.rowid=t.fk_product ";
472 $sql .= " AND prod.entity IN (".getEntity('product').")";
473 $sql .= " AND t.entity IN (".getEntity('productprice').")";
474
475 // Manage filter
476 if (is_array($filter)) {
477 if (count($filter) > 0) {
478 foreach ($filter as $key => $value) {
479 if (strpos($key, 'date')) { // To allow $filter['YEAR(s.dated)']=>$year
480 $sql .= " AND ".$this->db->sanitize($key)." = '".$this->db->escape($value)."'";
481 } elseif ($key == 'soc.nom') {
482 $sql .= " AND ".$this->db->sanitize($key)." LIKE '%".$this->db->escape($this->db->escapeforlike($value))."%'";
483 } elseif ($key == 'prod.ref' || $key == 'prod.label') {
484 $sql .= " AND ".$this->db->sanitize($key)." LIKE '%".$this->db->escape($this->db->escapeforlike($value))."%'";
485 } elseif ($key == 't.price' || $key == 't.price_ttc') {
486 $sql .= " AND ".$this->db->sanitize($key)." = ".((float) price2num($value));
487 } else {
488 $sql .= " AND ".$this->db->sanitize($key)." = ".((int) $value);
489 }
490 }
491 }
492
493 $filter = '';
494 }
495
496 // Manage filter
497 $errormessage = '';
498 $sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);
499 if ($errormessage) {
500 $this->errors[] = $errormessage;
501 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
502 return -1;
503 }
504
505 $sql .= $this->db->order($sortfield, $sortorder);
506 if (!empty($limit)) {
507 $sql .= $this->db->plimit($limit + 1, $offset);
508 }
509
510 dol_syslog(get_class($this)."::fetchAll", LOG_DEBUG);
511 $resql = $this->db->query($sql);
512 if ($resql) {
513 $this->lines = array();
514 $num = $this->db->num_rows($resql);
515
516 while ($obj = $this->db->fetch_object($resql)) {
517 $line = new PriceByCustomerLine($this->db);
518
519 $line->id = $obj->rowid;
520
521 $line->entity = $obj->entity;
522 $line->datec = $this->db->jdate($obj->datec);
523 $line->tms = $this->db->jdate($obj->tms);
524 $line->fk_product = $obj->fk_product;
525 $line->fk_soc = $obj->fk_soc;
526 $line->ref_customer = $obj->ref_customer;
527 $line->price = $obj->price;
528 $line->price_ttc = $obj->price_ttc;
529 $line->price_min = $obj->price_min;
530 $line->price_min_ttc = $obj->price_min_ttc;
531 $line->price_base_type = $obj->price_base_type;
532 $line->default_vat_code = $obj->default_vat_code;
533 $line->tva_tx = $obj->tva_tx;
534 $line->recuperableonly = $obj->recuperableonly;
535 $line->localtax1_tx = $obj->localtax1_tx;
536 $line->localtax2_tx = $obj->localtax2_tx;
537 $line->localtax1_type = $obj->localtax1_type;
538 $line->localtax2_type = $obj->localtax2_type;
539 $line->fk_user = $obj->fk_user;
540 $line->price_label = $obj->price_label;
541 $line->import_key = $obj->import_key;
542 $line->socname = $obj->socname;
543 $line->prodref = $obj->prodref;
544
545 $this->lines[] = $line;
546 }
547 $this->db->free($resql);
548
549 return $num;
550 } else {
551 $this->error = "Error ".$this->db->lasterror();
552 return -1;
553 }
554 }
555
566 public function fetchAllLog($sortorder, $sortfield, $limit, $offset, $filter = array())
567 {
568 if (!empty($sortfield)) {
569 $sortfield = "t.rowid";
570 }
571 if (!empty($sortorder)) {
572 $sortorder = "DESC";
573 }
574
575 $sql = "SELECT";
576 $sql .= " t.rowid,";
577 $sql .= " t.entity,";
578 $sql .= " t.datec,";
579 $sql .= " t.fk_product,";
580 $sql .= " t.fk_soc,";
581 $sql .= " t.ref_customer,";
582 $sql .= " t.price,";
583 $sql .= " t.price_ttc,";
584 $sql .= " t.price_min,";
585 $sql .= " t.price_min_ttc,";
586 $sql .= " t.price_base_type,";
587 $sql .= " t.default_vat_code,";
588 $sql .= " t.tva_tx,";
589 $sql .= " t.recuperableonly,";
590 $sql .= " t.localtax1_tx,";
591 $sql .= " t.localtax2_tx,";
592 $sql .= " t.fk_user,";
593 $sql .= " t.price_label,";
594 $sql .= " t.import_key,";
595 $sql .= " soc.nom as socname,";
596 $sql .= " prod.ref as prodref";
597 $sql .= " FROM ".$this->db->prefix()."product_customer_price_log as t";
598 $sql .= " ,".$this->db->prefix()."product as prod";
599 $sql .= " ,".$this->db->prefix()."societe as soc";
600 $sql .= " WHERE soc.rowid=t.fk_soc";
601 $sql .= " AND prod.rowid=t.fk_product ";
602 $sql .= " AND prod.entity IN (".getEntity('product').")";
603 $sql .= " AND t.entity IN (".getEntity('productprice').")";
604 // Manage filter
605 if (count($filter) > 0) {
606 foreach ($filter as $key => $value) {
607 if (strpos($key, 'date')) { // To allow $filter['YEAR(s.dated)']=>$year
608 $sql .= " AND ".$key." = '".$this->db->escape($value)."'";
609 } elseif ($key == 'soc.nom') {
610 $sql .= " AND ".$key." LIKE '%".$this->db->escape($value)."%'";
611 } else {
612 $sql .= " AND ".$key." = ".((int) $value);
613 }
614 }
615 }
616 $sql .= $this->db->order($sortfield, $sortorder);
617 if (!empty($limit)) {
618 $sql .= $this->db->plimit($limit + 1, $offset);
619 }
620
621 dol_syslog(get_class($this)."::fetchAllLog", LOG_DEBUG);
622 $resql = $this->db->query($sql);
623 if ($resql) {
624 $this->lines = array();
625 $num = $this->db->num_rows($resql);
626
627 while ($obj = $this->db->fetch_object($resql)) {
628 $line = new PriceByCustomerLine($this->db);
629
630 $line->id = $obj->rowid;
631
632 $line->entity = $obj->entity;
633 $line->datec = $this->db->jdate($obj->datec);
634 $line->tms = $this->db->jdate($obj->tms);
635 $line->fk_product = $obj->fk_product;
636 $line->fk_soc = $obj->fk_soc;
637 $line->ref_customer = $obj->ref_customer;
638 $line->price = $obj->price;
639 $line->price_ttc = $obj->price_ttc;
640 $line->price_min = $obj->price_min;
641 $line->price_min_ttc = $obj->price_min_ttc;
642 $line->price_base_type = $obj->price_base_type;
643 $line->default_vat_code = $obj->default_vat_code;
644 $line->tva_tx = $obj->tva_tx;
645 $line->recuperableonly = $obj->recuperableonly;
646 $line->localtax1_tx = $obj->localtax1_tx;
647 $line->localtax2_tx = $obj->localtax2_tx;
648 $line->fk_user = $obj->fk_user;
649 $line->price_label = $obj->price_label;
650 $line->import_key = $obj->import_key;
651 $line->socname = $obj->socname;
652 $line->prodref = $obj->prodref;
653
654 $this->lines [] = $line;
655 }
656 $this->db->free($resql);
657
658 return $num;
659 } else {
660 $this->error = "Error ".$this->db->lasterror();
661 return -1;
662 }
663 }
664
673 public function update(User $user, $notrigger = 0, $forceupdateaffiliate = 0)
674 {
675 global $conf, $langs;
676 $error = 0;
677
678 // Clean parameters
679
680 if (isset($this->entity)) {
681 $this->entity = (int) $this->entity;
682 }
683 if (isset($this->fk_product)) {
684 $this->fk_product = (int) $this->fk_product;
685 }
686 if (isset($this->fk_soc)) {
687 $this->fk_soc = (int) $this->fk_soc;
688 }
689 if (isset($this->ref_customer)) {
690 $this->ref_customer = trim($this->ref_customer);
691 }
692 if (isset($this->price)) {
693 $this->price = trim((string) $this->price);
694 }
695 if (isset($this->price_ttc)) {
696 $this->price_ttc = trim((string) $this->price_ttc);
697 }
698 if (isset($this->price_min)) {
699 $this->price_min = trim((string) $this->price_min);
700 }
701 if (isset($this->price_min_ttc)) {
702 $this->price_min_ttc = trim((string) $this->price_min_ttc);
703 }
704 if (isset($this->price_base_type)) {
705 $this->price_base_type = trim($this->price_base_type);
706 }
707 if (isset($this->tva_tx)) {
708 $this->tva_tx = trim($this->tva_tx);
709 }
710 if (isset($this->recuperableonly)) {
711 $this->recuperableonly = trim((string) $this->recuperableonly);
712 }
713 if (isset($this->localtax1_tx)) {
714 $this->localtax1_tx = trim((string) $this->localtax1_tx);
715 }
716 if (isset($this->localtax2_tx)) {
717 $this->localtax2_tx = trim((string) $this->localtax2_tx);
718 }
719 if (isset($this->fk_user)) {
720 $this->fk_user = (int) $this->fk_user;
721 }
722 if (isset($this->price_label)) {
723 $this->price_label = trim($this->price_label);
724 }
725 if (isset($this->import_key)) {
726 $this->import_key = trim($this->import_key);
727 }
728
729 // Check parameters
730 // Put here code to add a control on parameters values
731
732 if ($this->price != '' || $this->price == 0) {
733 $vatRate = (float) $this->tva_tx;
734 if ($this->price_base_type == 'TTC') {
735 $this->price_ttc = price2num($this->price, 'MU');
736 $this->price = (float) price2num($this->price) / (1 + ($vatRate / 100));
737 $this->price = price2num($this->price, 'MU');
738
739 if ($this->price_min != '' || $this->price_min == 0) {
740 $this->price_min_ttc = price2num($this->price_min, 'MU');
741 $this->price_min = (float) price2num($this->price_min) / (1 + ($vatRate / 100));
742 $this->price_min = price2num($this->price_min, 'MU');
743 } else {
744 $this->price_min = 0;
745 $this->price_min_ttc = 0;
746 }
747 } else {
748 $this->price = price2num($this->price, 'MU');
749 $this->price_ttc = ($this->recuperableonly != 1) ? (float) price2num($this->price) * (1 + ($vatRate / 100)) : $this->price;
750 $this->price_ttc = price2num($this->price_ttc, 'MU');
751
752 if ($this->price_min != '' || $this->price_min == 0) {
753 $this->price_min = price2num($this->price_min, 'MU');
754 $this->price_min_ttc = (float) price2num($this->price_min) * (1 + ($vatRate / 100));
755 $this->price_min_ttc = price2num($this->price_min_ttc, 'MU');
756 // print 'X'.$newminprice.'-'.$price_min;
757 } else {
758 $this->price_min = 0;
759 $this->price_min_ttc = 0;
760 }
761 }
762 }
763
764 // Do a copy of current record into log table
765 // Insert request
766 $sql = "INSERT INTO ".$this->db->prefix()."product_customer_price_log(";
767
768 $sql .= "entity,";
769 $sql .= "datec,";
770 $sql .= "fk_product,";
771 $sql .= "fk_soc,";
772 $sql .= "ref_customer,";
773 $sql .= "price,";
774 $sql .= "price_ttc,";
775 $sql .= "price_min,";
776 $sql .= "price_min_ttc,";
777 $sql .= "price_base_type,";
778 $sql .= "default_vat_code,";
779 $sql .= "tva_tx,";
780 $sql .= "recuperableonly,";
781 $sql .= "localtax1_tx,";
782 $sql .= "localtax2_tx,";
783 $sql .= "localtax1_type,";
784 $sql .= "localtax2_type,";
785 $sql .= "fk_user,";
786 $sql .= "price_label,";
787 $sql .= "import_key";
788
789 $sql .= ") ";
790 $sql .= "SELECT";
791
792 $sql .= " t.entity,";
793 $sql .= " t.datec,";
794 $sql .= " t.fk_product,";
795 $sql .= " t.fk_soc,";
796 $sql .= " t.ref_customer,";
797 $sql .= " t.price,";
798 $sql .= " t.price_ttc,";
799 $sql .= " t.price_min,";
800 $sql .= " t.price_min_ttc,";
801 $sql .= " t.price_base_type,";
802 $sql .= " t.default_vat_code,";
803 $sql .= " t.tva_tx,";
804 $sql .= " t.recuperableonly,";
805 $sql .= " t.localtax1_tx,";
806 $sql .= " t.localtax2_tx,";
807 $sql .= " t.localtax1_type,";
808 $sql .= " t.localtax2_type,";
809 $sql .= " t.fk_user,";
810 $sql .= " t.price_label,";
811 $sql .= " t.import_key";
812
813 $sql .= " FROM ".$this->db->prefix()."product_customer_price as t";
814 $sql .= " WHERE t.rowid = ".((int) $this->id);
815
816 $this->db->begin();
817 dol_syslog(get_class($this)."::update", LOG_DEBUG);
818 $resql = $this->db->query($sql);
819 if (!$resql) {
820 $error++;
821 $this->errors [] = "Error ".$this->db->lasterror();
822 }
823
824 // Update request
825 $sql = "UPDATE ".$this->db->prefix()."product_customer_price SET";
826
827 $sql .= " entity=".((int) $conf->entity).",";
828 $sql .= " datec='".$this->db->idate(dol_now())."',";
829 $sql .= " tms=".(dol_strlen((string) $this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
830 $sql .= " fk_product=".(isset($this->fk_product) ? $this->fk_product : "null").",";
831 $sql .= " fk_soc=".(isset($this->fk_soc) ? $this->fk_soc : "null").",";
832 $sql .= " ref_customer=".(isset($this->ref_customer) ? "'".$this->db->escape($this->ref_customer)."'" : "null").",";
833 $sql .= " price=".(isset($this->price) ? $this->price : "null").",";
834 $sql .= " price_ttc=".(isset($this->price_ttc) ? $this->price_ttc : "null").",";
835 $sql .= " price_min=".(isset($this->price_min) ? $this->price_min : "null").",";
836 $sql .= " price_min_ttc=".(isset($this->price_min_ttc) ? $this->price_min_ttc : "null").",";
837 $sql .= " price_base_type=".(isset($this->price_base_type) ? "'".$this->db->escape($this->price_base_type)."'" : "null").",";
838 $sql .= " default_vat_code = ".($this->default_vat_code ? "'".$this->db->escape($this->default_vat_code)."'" : "null").",";
839 $sql .= " tva_tx=".(isset($this->tva_tx) ? (empty($this->tva_tx) ? 0 : $this->tva_tx) : "null").",";
840 $sql .= " recuperableonly=".(isset($this->recuperableonly) ? $this->recuperableonly : "null").",";
841 $sql .= " localtax1_tx=".(isset($this->localtax1_tx) ? (empty($this->localtax1_tx) ? 0 : $this->localtax1_tx) : "null").",";
842 $sql .= " localtax2_tx=".(isset($this->localtax2_tx) ? (empty($this->localtax2_tx) ? 0 : $this->localtax2_tx) : "null").",";
843 $sql .= " localtax1_type=".(!empty($this->localtax1_type) ? "'".$this->db->escape($this->localtax1_type)."'" : "'0'").",";
844 $sql .= " localtax2_type=".(!empty($this->localtax2_type) ? "'".$this->db->escape($this->localtax2_type)."'" : "'0'").",";
845 $sql .= " fk_user=".((int) $user->id).",";
846 $sql .= " price_label=".(isset($this->price_label) ? "'".$this->db->escape($this->price_label)."'" : "null").",";
847 $sql .= " import_key=".(isset($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null");
848
849 $sql .= " WHERE rowid=".((int) $this->id);
850
851 dol_syslog(get_class($this)."::update", LOG_DEBUG);
852 $resql = $this->db->query($sql);
853 if (!$resql) {
854 $error++;
855 $this->errors [] = "Error ".$this->db->lasterror();
856 }
857
858 if (!$error && !$notrigger) {
859 // Call trigger
860 $result = $this->call_trigger('PRODUCT_CUSTOMER_PRICE_MODIFY', $user);
861 if ($result < 0) {
862 $error++;
863 }
864 // End call triggers
865 }
866
867 if (!$error) {
868 $result = $this->setPriceOnAffiliateThirdparty($user, $forceupdateaffiliate);
869 if ($result < 0) {
870 $error++;
871 }
872 }
873
874 // Commit or rollback
875 if ($error) {
876 foreach ($this->errors as $errmsg) {
877 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
878 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
879 }
880 $this->db->rollback();
881 return -1 * $error;
882 } else {
883 $this->db->commit();
884 return 1;
885 }
886 }
887
895 public function setPriceOnAffiliateThirdparty($user, $forceupdateaffiliate)
896 {
897 if (getDolGlobalString('PRODUCT_DISABLE_PROPAGATE_CUSTOMER_PRICES_ON_CHILD_COMPANIES')) {
898 return 0;
899 }
900
901 $error = 0;
902
903 // Find all susidiaries
904 $sql = "SELECT s.rowid";
905 $sql .= " FROM ".$this->db->prefix()."societe as s";
906 $sql .= " WHERE s.parent = ".((int) $this->fk_soc);
907 $sql .= " AND s.entity IN (".getEntity('societe').")";
908
909 dol_syslog(get_class($this)."::setPriceOnAffiliateThirdparty", LOG_DEBUG);
910 $resql = $this->db->query($sql);
911
912 if ($resql) {
913 $this->lines = array();
914 $num = $this->db->num_rows($resql);
915
916 while (($obj = $this->db->fetch_object($resql)) && (empty($error))) {
917 // find if there is an existing line for the product and the subsidiaries
918 $prodsocprice = new ProductCustomerPrice($this->db);
919
920 $filter = array(
921 't.fk_product' => $this->fk_product, 't.fk_soc' => $obj->rowid
922 );
923
924 $result = $prodsocprice->fetchAll('', '', 0, 0, $filter);
925 if ($result < 0) {
926 $error++;
927 $this->error = $prodsocprice->error;
928 } else {
929 // There is one line
930 if (count($prodsocprice->lines) > 0) {
931 // If force update => Update
932 if (!empty($forceupdateaffiliate)) {
933 $prodsocpriceupd = new ProductCustomerPrice($this->db);
934 $prodsocpriceupd->fetch($prodsocprice->lines [0]->id);
935
936 $prodsocpriceupd->price = $this->price;
937 $prodsocpriceupd->price_min = $this->price_min;
938 $prodsocpriceupd->price_base_type = $this->price_base_type;
939 $prodsocpriceupd->tva_tx = $this->tva_tx;
940 $prodsocpriceupd->recuperableonly = $this->recuperableonly;
941 $prodsocpriceupd->price_label = $this->price_label;
942
943 $resultupd = $prodsocpriceupd->update($user, 0, $forceupdateaffiliate);
944 if ($resultupd < 0) {
945 $error++;
946 $this->error = $prodsocpriceupd->error;
947 }
948 }
949 } else {
950 // If line do not exits then create it
951 $prodsocpricenew = new ProductCustomerPrice($this->db);
952 $prodsocpricenew->fk_soc = $obj->rowid;
953 $prodsocpricenew->ref_customer = $obj->ref_customer;
954 $prodsocpricenew->fk_product = $this->fk_product;
955 $prodsocpricenew->price = $this->price;
956 $prodsocpricenew->price_min = $this->price_min;
957 $prodsocpricenew->price_base_type = $this->price_base_type;
958 $prodsocpricenew->tva_tx = $this->tva_tx;
959 $prodsocpricenew->recuperableonly = $this->recuperableonly;
960 $prodsocpricenew->price_label = $this->price_label;
961
962 $resultupd = $prodsocpricenew->create($user, 0, $forceupdateaffiliate);
963 if ($resultupd < 0) {
964 $error++;
965 $this->error = $prodsocpricenew->error;
966 }
967 }
968 }
969 }
970 $this->db->free($resql);
971
972 if (empty($error)) {
973 return 1;
974 } else {
975 return -1;
976 }
977 } else {
978 $this->error = "Error ".$this->db->lasterror();
979 return -1;
980 }
981 }
982
990 public function delete($user, $notrigger = 0)
991 {
992 global $conf, $langs;
993 $error = 0;
994
995 $this->db->begin();
996
997 if (!$notrigger) {
998 $result = $this->call_trigger('PRODUCT_CUSTOMER_PRICE_DELETE', $user);
999 if ($result < 0) {
1000 $error++;
1001 }
1002 }
1003
1004 if (!$error) {
1005 $sql = "DELETE FROM ".$this->db->prefix()."product_customer_price";
1006 $sql .= " WHERE rowid=".((int) $this->id);
1007
1008 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
1009 $resql = $this->db->query($sql);
1010 if (!$resql) {
1011 $error++;
1012 $this->errors [] = "Error ".$this->db->lasterror();
1013 }
1014 }
1015
1016 // Commit or rollback
1017 if ($error) {
1018 foreach ($this->errors as $errmsg) {
1019 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
1020 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
1021 }
1022 $this->db->rollback();
1023 return -1 * $error;
1024 } else {
1025 $this->db->commit();
1026 return 1;
1027 }
1028 }
1029
1037 public function createFromClone(User $user, $fromid)
1038 {
1039 $error = 0;
1040
1041 $object = new ProductCustomerPrice($this->db);
1042
1043 $this->db->begin();
1044
1045 // Load source object
1046 $object->fetch($fromid);
1047 $object->id = 0;
1048 $object->statut = 0;
1049
1050 // Clear fields
1051 // ...
1052
1053 // Create clone
1054 $object->context['createfromclone'] = 'createfromclone';
1055 $result = $object->create($user);
1056
1057 // Other options
1058 if ($result < 0) {
1059 $this->error = $object->error;
1060 $this->errors = array_merge($this->errors, $object->errors);
1061 $error++;
1062 }
1063
1064 if (!$error) {
1065 }
1066
1067 unset($object->context['createfromclone']);
1068
1069 // End
1070 if (!$error) {
1071 $this->db->commit();
1072 return $object->id;
1073 } else {
1074 $this->db->rollback();
1075 return -1;
1076 }
1077 }
1078
1085 public function initAsSpecimen()
1086 {
1087 $this->id = 0;
1088
1089 $this->entity = 0;
1090 $this->datec = '';
1091 $this->tms = dol_now();
1092 $this->fk_product = 0;
1093 $this->fk_soc = 0;
1094 $this->ref_customer = '';
1095 $this->price = '';
1096 $this->price_ttc = '';
1097 $this->price_min = '';
1098 $this->price_min_ttc = '';
1099 $this->price_base_type = '';
1100 $this->default_vat_code = '';
1101 $this->tva_tx = '';
1102 $this->recuperableonly = '';
1103 $this->localtax1_tx = '';
1104 $this->localtax2_tx = '';
1105 $this->fk_user = 0;
1106 $this->price_label = '';
1107 $this->import_key = '';
1108
1109 return 1;
1110 }
1111}
1112
1117{
1121 public $id;
1122
1126 public $entity;
1127
1131 public $datec = '';
1132
1136 public $fk_product;
1137
1141 public $ref_customer;
1142
1146 public $fk_soc;
1147
1151 public $price;
1155 public $price_ttc;
1159 public $price_min;
1163 public $price_min_ttc;
1167 public $price_base_type;
1171 public $default_vat_code;
1175 public $tva_tx;
1179 public $recuperableonly;
1183 public $localtax1_tx;
1187 public $localtax2_tx;
1188
1192 public $fk_user;
1196 public $price_label;
1197
1201 public $import_key;
1205 public $socname;
1209 public $prodref;
1210}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
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 a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79