dolibarr 19.0.4
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 $fields = array(
35 '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),
36 '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'),
37 'ref_customer' => array('type'=>'varchar(128)', 'label'=>'RefCustomer', 'enabled'=>1, 'visible'=>4, 'position'=>10, 'notnull'=>1,),
38 'datec' => array('type'=>'datetime', 'label'=>'AppliedPricesFrom', 'enabled'=>1, 'visible'=>1, 'position'=>500, 'notnull'=>1,),
39 'price_base_type' => array('type' => 'varchar(255)', 'label' => 'PriceBase', 'enabled' => 1, 'visible' => 1, 'position' => 11, 'notnull' => -1, 'comment' => 'Price Base Type'),
40 'tva_tx' => array('type' => 'decimal(20,6)', 'label' => 'VAT', 'enabled' => 1, 'visible' => 1, 'position' => 12, 'notnull' => -1, 'comment' => 'TVA Tax Rate'),
41 'price' => array('type' => 'decimal(20,6)', 'label' => 'HT', 'enabled' => 1, 'visible' => 1, 'position' => 8, 'notnull' => -1, 'comment' => 'Price HT'),
42 'price_ttc' => array('type' => 'decimal(20,6)', 'label' => 'TTC', 'enabled' => 1, 'visible' => 1, 'position' => 8, 'notnull' => -1, 'comment' => 'Price TTC'),
43 'price_min' => array('type' => 'decimal(20,6)', 'label' => 'MinPriceHT', 'enabled' => 1, 'visible' => 1, 'position' => 9, 'notnull' => -1, 'comment' => 'Minimum Price'),
44 'price_min_ttc' => array('type' => 'decimal(20,6)', 'label' => 'MinPriceTTC', 'enabled' => 1, 'visible' => 1, 'position' => 10, 'notnull' => -1, 'comment' => 'Minimum Price TTC'),
45 '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'),
46 );
47
51 public $element = 'product_customer_price';
52
56 public $table_element = 'product_customer_price';
57
61 public $entity;
62
63 public $datec = '';
64 public $tms = '';
65
69 public $fk_product;
70
74 public $fk_soc;
75
79 public $ref_customer;
80
81 public $price;
82 public $price_ttc;
83 public $price_min;
84 public $price_min_ttc;
85 public $price_base_type;
86 public $default_vat_code;
87 public $tva_tx;
88 public $recuperableonly;
89 public $localtax1_type;
90 public $localtax1_tx;
91 public $localtax2_type;
92 public $localtax2_tx;
93
97 public $fk_user;
98
102 public $lines = array();
103
104
110 public function __construct($db)
111 {
112 $this->db = $db;
113 }
114
123 public function create($user, $notrigger = 0, $forceupdateaffiliate = 0)
124 {
125 global $conf, $langs;
126 $error = 0;
127
128 // Clean parameters
129
130 if (isset($this->entity)) {
131 $this->entity = trim($this->entity);
132 }
133 if (isset($this->fk_product)) {
134 $this->fk_product = trim($this->fk_product);
135 }
136 if (isset($this->fk_soc)) {
137 $this->fk_soc = trim($this->fk_soc);
138 }
139 if (isset($this->ref_customer)) {
140 $this->ref_customer = trim($this->ref_customer);
141 }
142 if (isset($this->price)) {
143 $this->price = trim($this->price);
144 }
145 if (isset($this->price_ttc)) {
146 $this->price_ttc = trim($this->price_ttc);
147 }
148 if (isset($this->price_min)) {
149 $this->price_min = trim($this->price_min);
150 }
151 if (isset($this->price_min_ttc)) {
152 $this->price_min_ttc = trim($this->price_min_ttc);
153 }
154 if (isset($this->price_base_type)) {
155 $this->price_base_type = trim($this->price_base_type);
156 }
157 if (isset($this->tva_tx)) {
158 $this->tva_tx = (float) $this->tva_tx;
159 }
160 if (isset($this->recuperableonly)) {
161 $this->recuperableonly = trim($this->recuperableonly);
162 }
163 if (isset($this->localtax1_tx)) {
164 $this->localtax1_tx = trim($this->localtax1_tx);
165 }
166 if (isset($this->localtax2_tx)) {
167 $this->localtax2_tx = trim($this->localtax2_tx);
168 }
169 if (isset($this->fk_user)) {
170 $this->fk_user = trim($this->fk_user);
171 }
172 if (isset($this->import_key)) {
173 $this->import_key = trim($this->import_key);
174 }
175
176 // Check parameters
177 // Put here code to add control on parameters values
178
179 if ($this->price != '' || $this->price == 0) {
180 if ($this->price_base_type == 'TTC') {
181 $this->price_ttc = price2num($this->price, 'MU');
182 $this->price = price2num($this->price) / (1 + ($this->tva_tx / 100));
183 $this->price = price2num($this->price, 'MU');
184
185 if ($this->price_min != '' || $this->price_min == 0) {
186 $this->price_min_ttc = price2num($this->price_min, 'MU');
187 $this->price_min = price2num($this->price_min) / (1 + ($this->tva_tx / 100));
188 $this->price_min = price2num($this->price_min, 'MU');
189 } else {
190 $this->price_min = 0;
191 $this->price_min_ttc = 0;
192 }
193 } else {
194 $this->price = price2num($this->price, 'MU');
195 $this->price_ttc = ($this->recuperableonly != 1) ? price2num($this->price) * (1 + ($this->tva_tx / 100)) : $this->price;
196 $this->price_ttc = price2num($this->price_ttc, 'MU');
197
198 if ($this->price_min != '' || $this->price_min == 0) {
199 $this->price_min = price2num($this->price_min, 'MU');
200 $this->price_min_ttc = price2num($this->price_min) * (1 + ($this->tva_tx / 100));
201 $this->price_min_ttc = price2num($this->price_min_ttc, 'MU');
202 // print 'X'.$newminprice.'-'.$price_min;
203 } else {
204 $this->price_min = 0;
205 $this->price_min_ttc = 0;
206 }
207 }
208 }
209
210 // Insert request
211 $sql = "INSERT INTO ".$this->db->prefix()."product_customer_price(";
212 $sql .= "entity,";
213 $sql .= "datec,";
214 $sql .= "fk_product,";
215 $sql .= "fk_soc,";
216 $sql .= 'ref_customer,';
217 $sql .= "price,";
218 $sql .= "price_ttc,";
219 $sql .= "price_min,";
220 $sql .= "price_min_ttc,";
221 $sql .= "price_base_type,";
222 $sql .= "default_vat_code,";
223 $sql .= "tva_tx,";
224 $sql .= "recuperableonly,";
225 $sql .= "localtax1_type,";
226 $sql .= "localtax1_tx,";
227 $sql .= "localtax2_type,";
228 $sql .= "localtax2_tx,";
229 $sql .= "fk_user,";
230 $sql .= "import_key";
231 $sql .= ") VALUES (";
232 $sql .= " ".((int) $conf->entity).",";
233 $sql .= " '".$this->db->idate(dol_now())."',";
234 $sql .= " ".(!isset($this->fk_product) ? 'NULL' : "'".$this->db->escape($this->fk_product)."'").",";
235 $sql .= " ".(!isset($this->fk_soc) ? 'NULL' : "'".$this->db->escape($this->fk_soc)."'").",";
236 $sql .= " ".(!isset($this->ref_customer) ? 'NULL' : "'".$this->db->escape($this->ref_customer)."'").",";
237 $sql .= " ".(empty($this->price) ? '0' : "'".$this->db->escape($this->price)."'").",";
238 $sql .= " ".(empty($this->price_ttc) ? '0' : "'".$this->db->escape($this->price_ttc)."'").",";
239 $sql .= " ".(empty($this->price_min) ? '0' : "'".$this->db->escape($this->price_min)."'").",";
240 $sql .= " ".(empty($this->price_min_ttc) ? '0' : "'".$this->db->escape($this->price_min_ttc)."'").",";
241 $sql .= " ".(!isset($this->price_base_type) ? 'NULL' : "'".$this->db->escape($this->price_base_type)."'").",";
242 $sql .= " ".($this->default_vat_code ? "'".$this->db->escape($this->default_vat_code)."'" : "null").",";
243 $sql .= " ".(!isset($this->tva_tx) ? 'NULL' : (empty($this->tva_tx) ? 0 : $this->tva_tx)).",";
244 $sql .= " ".(!isset($this->recuperableonly) ? 'NULL' : "'".$this->db->escape($this->recuperableonly)."'").",";
245 $sql .= " ".(empty($this->localtax1_type) ? "'0'" : "'".$this->db->escape($this->localtax1_type)."'").",";
246 $sql .= " ".(!isset($this->localtax1_tx) ? 'NULL' : (empty($this->localtax1_tx) ? 0 : $this->localtax1_tx)).",";
247 $sql .= " ".(empty($this->localtax2_type) ? "'0'" : "'".$this->db->escape($this->localtax2_type)."'").",";
248 $sql .= " ".(!isset($this->localtax2_tx) ? 'NULL' : (empty($this->localtax2_tx) ? 0 : $this->localtax2_tx)).",";
249 $sql .= " ".((int) $user->id).",";
250 $sql .= " ".(!isset($this->import_key) ? 'NULL' : "'".$this->db->escape($this->import_key)."'");
251 $sql .= ")";
252
253 $this->db->begin();
254
255 dol_syslog(get_class($this)."::create", LOG_DEBUG);
256 $resql = $this->db->query($sql);
257 if (!$resql) {
258 $error++;
259 $this->errors [] = "Error ".$this->db->lasterror();
260 }
261
262 if (!$error) {
263 $this->id = $this->db->last_insert_id($this->db->prefix()."product_customer_price");
264
265 if (!$notrigger) {
266 $result = $this->call_trigger('PRODUCT_CUSTOMER_PRICE_CREATE', $user);
267 if ($result < 0) {
268 $error++;
269 }
270 }
271 }
272
273 if (!$error) {
274 $result = $this->setPriceOnAffiliateThirdparty($user, $forceupdateaffiliate);
275 if ($result < 0) {
276 $error++;
277 }
278 }
279
280 // Commit or rollback
281 if ($error) {
282 foreach ($this->errors as $errmsg) {
283 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
284 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
285 }
286 $this->db->rollback();
287 return -1 * $error;
288 } else {
289 $this->db->commit();
290 return $this->id;
291 }
292 }
293
300 public function fetch($id)
301 {
302 global $langs;
303
304 $sql = "SELECT";
305 $sql .= " t.rowid,";
306 $sql .= " t.entity,";
307 $sql .= " t.datec,";
308 $sql .= " t.tms,";
309 $sql .= " t.fk_product,";
310 $sql .= " t.fk_soc,";
311 $sql .= " t.ref_customer,";
312 $sql .= " t.price,";
313 $sql .= " t.price_ttc,";
314 $sql .= " t.price_min,";
315 $sql .= " t.price_min_ttc,";
316 $sql .= " t.price_base_type,";
317 $sql .= " t.default_vat_code,";
318 $sql .= " t.tva_tx,";
319 $sql .= " t.recuperableonly,";
320 $sql .= " t.localtax1_tx,";
321 $sql .= " t.localtax2_tx,";
322 $sql .= " t.fk_user,";
323 $sql .= " t.import_key";
324 $sql .= " FROM ".$this->db->prefix()."product_customer_price as t";
325 $sql .= " WHERE t.rowid = ".((int) $id);
326
327 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
328 $resql = $this->db->query($sql);
329 if ($resql) {
330 if ($this->db->num_rows($resql)) {
331 $obj = $this->db->fetch_object($resql);
332
333 $this->id = $obj->rowid;
334
335 $this->entity = $obj->entity;
336 $this->datec = $this->db->jdate($obj->datec);
337 $this->tms = $this->db->jdate($obj->tms);
338 $this->fk_product = $obj->fk_product;
339 $this->fk_soc = $obj->fk_soc;
340 $this->ref_customer = $obj->ref_customer;
341 $this->price = $obj->price;
342 $this->price_ttc = $obj->price_ttc;
343 $this->price_min = $obj->price_min;
344 $this->price_min_ttc = $obj->price_min_ttc;
345 $this->price_base_type = $obj->price_base_type;
346 $this->default_vat_code = $obj->default_vat_code;
347 $this->tva_tx = $obj->tva_tx;
348 $this->recuperableonly = $obj->recuperableonly;
349 $this->localtax1_tx = $obj->localtax1_tx;
350 $this->localtax2_tx = $obj->localtax2_tx;
351 $this->fk_user = $obj->fk_user;
352 $this->import_key = $obj->import_key;
353
354 $this->db->free($resql);
355
356 return 1;
357 } else {
358 $this->db->free($resql);
359
360 return 0;
361 }
362 } else {
363 $this->error = "Error ".$this->db->lasterror();
364 return -1;
365 }
366 }
367
379 public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, $filter = array())
380 {
381 if (empty($sortfield)) {
382 $sortfield = "t.rowid";
383 }
384 if (empty($sortorder)) {
385 $sortorder = "DESC";
386 }
387
388 $sql = "SELECT";
389 $sql .= " t.rowid,";
390 $sql .= " t.entity,";
391 $sql .= " t.datec,";
392 $sql .= " t.tms,";
393 $sql .= " t.fk_product,";
394 $sql .= " t.fk_soc,";
395 $sql .= " t.ref_customer,";
396 $sql .= " t.price,";
397 $sql .= " t.price_ttc,";
398 $sql .= " t.price_min,";
399 $sql .= " t.price_min_ttc,";
400 $sql .= " t.price_base_type,";
401 $sql .= " t.default_vat_code,";
402 $sql .= " t.tva_tx,";
403 $sql .= " t.recuperableonly,";
404 $sql .= " t.localtax1_tx,";
405 $sql .= " t.localtax2_tx,";
406 $sql .= " t.localtax1_type,";
407 $sql .= " t.localtax2_type,";
408 $sql .= " t.fk_user,";
409 $sql .= " t.import_key,";
410 $sql .= " soc.nom as socname,";
411 $sql .= " prod.ref as prodref";
412 $sql .= " FROM ".$this->db->prefix()."product_customer_price as t,";
413 $sql .= " ".$this->db->prefix()."product as prod,";
414 $sql .= " ".$this->db->prefix()."societe as soc";
415 $sql .= " WHERE soc.rowid=t.fk_soc ";
416 $sql .= " AND prod.rowid=t.fk_product ";
417 $sql .= " AND prod.entity IN (".getEntity('product').")";
418 $sql .= " AND t.entity IN (".getEntity('productprice').")";
419
420 // Manage filter
421 if (count($filter) > 0) {
422 foreach ($filter as $key => $value) {
423 if (strpos($key, 'date')) { // To allow $filter['YEAR(s.dated)']=>$year
424 $sql .= " AND ".$key." = '".$this->db->escape($value)."'";
425 } elseif ($key == 'soc.nom') {
426 $sql .= " AND ".$key." LIKE '%".$this->db->escape($value)."%'";
427 } elseif ($key == 'prod.ref' || $key == 'prod.label') {
428 $sql .= " AND ".$key." LIKE '%".$this->db->escape($value)."%'";
429 } elseif ($key == 't.price' || $key == 't.price_ttc') {
430 $sql .= " AND ".$key." LIKE '%".price2num($value)."%'";
431 } else {
432 $sql .= " AND ".$key." = ".((int) $value);
433 }
434 }
435 }
436 $sql .= $this->db->order($sortfield, $sortorder);
437 if (!empty($limit)) {
438 $sql .= $this->db->plimit($limit + 1, $offset);
439 }
440
441 dol_syslog(get_class($this)."::fetchAll", LOG_DEBUG);
442 $resql = $this->db->query($sql);
443 if ($resql) {
444 $this->lines = array();
445 $num = $this->db->num_rows($resql);
446
447 while ($obj = $this->db->fetch_object($resql)) {
448 $line = new PriceByCustomerLine();
449
450 $line->id = $obj->rowid;
451
452 $line->entity = $obj->entity;
453 $line->datec = $this->db->jdate($obj->datec);
454 $line->tms = $this->db->jdate($obj->tms);
455 $line->fk_product = $obj->fk_product;
456 $line->fk_soc = $obj->fk_soc;
457 $line->ref_customer = $obj->ref_customer;
458 $line->price = $obj->price;
459 $line->price_ttc = $obj->price_ttc;
460 $line->price_min = $obj->price_min;
461 $line->price_min_ttc = $obj->price_min_ttc;
462 $line->price_base_type = $obj->price_base_type;
463 $line->default_vat_code = $obj->default_vat_code;
464 $line->tva_tx = $obj->tva_tx;
465 $line->recuperableonly = $obj->recuperableonly;
466 $line->localtax1_tx = $obj->localtax1_tx;
467 $line->localtax2_tx = $obj->localtax2_tx;
468 $line->localtax1_type = $obj->localtax1_type;
469 $line->localtax2_type = $obj->localtax2_type;
470 $line->fk_user = $obj->fk_user;
471 $line->import_key = $obj->import_key;
472 $line->socname = $obj->socname;
473 $line->prodref = $obj->prodref;
474
475 $this->lines[] = $line;
476 }
477 $this->db->free($resql);
478
479 return $num;
480 } else {
481 $this->error = "Error ".$this->db->lasterror();
482 return -1;
483 }
484 }
485
496 public function fetchAllLog($sortorder, $sortfield, $limit, $offset, $filter = array())
497 {
498 if (!empty($sortfield)) {
499 $sortfield = "t.rowid";
500 }
501 if (!empty($sortorder)) {
502 $sortorder = "DESC";
503 }
504
505 $sql = "SELECT";
506 $sql .= " t.rowid,";
507 $sql .= " t.entity,";
508 $sql .= " t.datec,";
509 $sql .= " t.fk_product,";
510 $sql .= " t.fk_soc,";
511 $sql .= " t.ref_customer,";
512 $sql .= " t.price,";
513 $sql .= " t.price_ttc,";
514 $sql .= " t.price_min,";
515 $sql .= " t.price_min_ttc,";
516 $sql .= " t.price_base_type,";
517 $sql .= " t.default_vat_code,";
518 $sql .= " t.tva_tx,";
519 $sql .= " t.recuperableonly,";
520 $sql .= " t.localtax1_tx,";
521 $sql .= " t.localtax2_tx,";
522 $sql .= " t.fk_user,";
523 $sql .= " t.import_key,";
524 $sql .= " soc.nom as socname,";
525 $sql .= " prod.ref as prodref";
526 $sql .= " FROM ".$this->db->prefix()."product_customer_price_log as t";
527 $sql .= " ,".$this->db->prefix()."product as prod";
528 $sql .= " ,".$this->db->prefix()."societe as soc";
529 $sql .= " WHERE soc.rowid=t.fk_soc";
530 $sql .= " AND prod.rowid=t.fk_product ";
531 $sql .= " AND prod.entity IN (".getEntity('product').")";
532 $sql .= " AND t.entity IN (".getEntity('productprice').")";
533 // Manage filter
534 if (count($filter) > 0) {
535 foreach ($filter as $key => $value) {
536 if (strpos($key, 'date')) { // To allow $filter['YEAR(s.dated)']=>$year
537 $sql .= " AND ".$key." = '".$this->db->escape($value)."'";
538 } elseif ($key == 'soc.nom') {
539 $sql .= " AND ".$key." LIKE '%".$this->db->escape($value)."%'";
540 } else {
541 $sql .= " AND ".$key." = ".((int) $value);
542 }
543 }
544 }
545 $sql .= $this->db->order($sortfield, $sortorder);
546 if (!empty($limit)) {
547 $sql .= $this->db->plimit($limit + 1, $offset);
548 }
549
550 dol_syslog(get_class($this)."::fetchAllLog", LOG_DEBUG);
551 $resql = $this->db->query($sql);
552 if ($resql) {
553 $this->lines = array();
554 $num = $this->db->num_rows($resql);
555
556 while ($obj = $this->db->fetch_object($resql)) {
557 $line = new PriceByCustomerLine();
558
559 $line->id = $obj->rowid;
560
561 $line->entity = $obj->entity;
562 $line->datec = $this->db->jdate($obj->datec);
563 $line->tms = $this->db->jdate($obj->tms);
564 $line->fk_product = $obj->fk_product;
565 $line->fk_soc = $obj->fk_soc;
566 $line->ref_customer = $obj->ref_customer;
567 $line->price = $obj->price;
568 $line->price_ttc = $obj->price_ttc;
569 $line->price_min = $obj->price_min;
570 $line->price_min_ttc = $obj->price_min_ttc;
571 $line->price_base_type = $obj->price_base_type;
572 $line->default_vat_code = $obj->default_vat_code;
573 $line->tva_tx = $obj->tva_tx;
574 $line->recuperableonly = $obj->recuperableonly;
575 $line->localtax1_tx = $obj->localtax1_tx;
576 $line->localtax2_tx = $obj->localtax2_tx;
577 $line->fk_user = $obj->fk_user;
578 $line->import_key = $obj->import_key;
579 $line->socname = $obj->socname;
580 $line->prodref = $obj->prodref;
581
582 $this->lines [] = $line;
583 }
584 $this->db->free($resql);
585
586 return $num;
587 } else {
588 $this->error = "Error ".$this->db->lasterror();
589 return -1;
590 }
591 }
592
601 public function update($user = 0, $notrigger = 0, $forceupdateaffiliate = 0)
602 {
603 global $conf, $langs;
604 $error = 0;
605
606 // Clean parameters
607
608 if (isset($this->entity)) {
609 $this->entity = trim($this->entity);
610 }
611 if (isset($this->fk_product)) {
612 $this->fk_product = trim($this->fk_product);
613 }
614 if (isset($this->fk_soc)) {
615 $this->fk_soc = trim($this->fk_soc);
616 }
617 if (isset($this->ref_customer)) {
618 $this->ref_customer = trim($this->ref_customer);
619 }
620 if (isset($this->price)) {
621 $this->price = trim($this->price);
622 }
623 if (isset($this->price_ttc)) {
624 $this->price_ttc = trim($this->price_ttc);
625 }
626 if (isset($this->price_min)) {
627 $this->price_min = trim($this->price_min);
628 }
629 if (isset($this->price_min_ttc)) {
630 $this->price_min_ttc = trim($this->price_min_ttc);
631 }
632 if (isset($this->price_base_type)) {
633 $this->price_base_type = trim($this->price_base_type);
634 }
635 if (isset($this->tva_tx)) {
636 $this->tva_tx = (float) $this->tva_tx;
637 }
638 if (isset($this->recuperableonly)) {
639 $this->recuperableonly = trim($this->recuperableonly);
640 }
641 if (isset($this->localtax1_tx)) {
642 $this->localtax1_tx = trim($this->localtax1_tx);
643 }
644 if (isset($this->localtax2_tx)) {
645 $this->localtax2_tx = trim($this->localtax2_tx);
646 }
647 if (isset($this->fk_user)) {
648 $this->fk_user = trim($this->fk_user);
649 }
650 if (isset($this->import_key)) {
651 $this->import_key = trim($this->import_key);
652 }
653
654 // Check parameters
655 // Put here code to add a control on parameters values
656
657 if ($this->price != '' || $this->price == 0) {
658 if ($this->price_base_type == 'TTC') {
659 $this->price_ttc = price2num($this->price, 'MU');
660 $this->price = price2num($this->price) / (1 + ($this->tva_tx / 100));
661 $this->price = price2num($this->price, 'MU');
662
663 if ($this->price_min != '' || $this->price_min == 0) {
664 $this->price_min_ttc = price2num($this->price_min, 'MU');
665 $this->price_min = price2num($this->price_min) / (1 + ($this->tva_tx / 100));
666 $this->price_min = price2num($this->price_min, 'MU');
667 } else {
668 $this->price_min = 0;
669 $this->price_min_ttc = 0;
670 }
671 } else {
672 $this->price = price2num($this->price, 'MU');
673 $this->price_ttc = ($this->recuperableonly != 1) ? price2num($this->price) * (1 + ($this->tva_tx / 100)) : $this->price;
674 $this->price_ttc = price2num($this->price_ttc, 'MU');
675
676 if ($this->price_min != '' || $this->price_min == 0) {
677 $this->price_min = price2num($this->price_min, 'MU');
678 $this->price_min_ttc = price2num($this->price_min) * (1 + ($this->tva_tx / 100));
679 $this->price_min_ttc = price2num($this->price_min_ttc, 'MU');
680 // print 'X'.$newminprice.'-'.$price_min;
681 } else {
682 $this->price_min = 0;
683 $this->price_min_ttc = 0;
684 }
685 }
686 }
687
688 // Do a copy of current record into log table
689 // Insert request
690 $sql = "INSERT INTO ".$this->db->prefix()."product_customer_price_log(";
691
692 $sql .= "entity,";
693 $sql .= "datec,";
694 $sql .= "fk_product,";
695 $sql .= "fk_soc,";
696 $sql .= "ref_customer,";
697 $sql .= "price,";
698 $sql .= "price_ttc,";
699 $sql .= "price_min,";
700 $sql .= "price_min_ttc,";
701 $sql .= "price_base_type,";
702 $sql .= "default_vat_code,";
703 $sql .= "tva_tx,";
704 $sql .= "recuperableonly,";
705 $sql .= "localtax1_tx,";
706 $sql .= "localtax2_tx,";
707 $sql .= "localtax1_type,";
708 $sql .= "localtax2_type,";
709 $sql .= "fk_user,";
710 $sql .= "import_key";
711
712 $sql .= ") ";
713 $sql .= "SELECT";
714
715 $sql .= " t.entity,";
716 $sql .= " t.datec,";
717 $sql .= " t.fk_product,";
718 $sql .= " t.fk_soc,";
719 $sql .= " t.ref_customer,";
720 $sql .= " t.price,";
721 $sql .= " t.price_ttc,";
722 $sql .= " t.price_min,";
723 $sql .= " t.price_min_ttc,";
724 $sql .= " t.price_base_type,";
725 $sql .= " t.default_vat_code,";
726 $sql .= " t.tva_tx,";
727 $sql .= " t.recuperableonly,";
728 $sql .= " t.localtax1_tx,";
729 $sql .= " t.localtax2_tx,";
730 $sql .= " t.localtax1_type,";
731 $sql .= " t.localtax2_type,";
732 $sql .= " t.fk_user,";
733 $sql .= " t.import_key";
734
735 $sql .= " FROM ".$this->db->prefix()."product_customer_price as t";
736 $sql .= " WHERE t.rowid = ".((int) $this->id);
737
738 $this->db->begin();
739 dol_syslog(get_class($this)."::update", LOG_DEBUG);
740 $resql = $this->db->query($sql);
741 if (!$resql) {
742 $error++;
743 $this->errors [] = "Error ".$this->db->lasterror();
744 }
745
746 // Update request
747 $sql = "UPDATE ".$this->db->prefix()."product_customer_price SET";
748
749 $sql .= " entity=".$conf->entity.",";
750 $sql .= " datec='".$this->db->idate(dol_now())."',";
751 $sql .= " tms=".(dol_strlen($this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
752 $sql .= " fk_product=".(isset($this->fk_product) ? $this->fk_product : "null").",";
753 $sql .= " fk_soc=".(isset($this->fk_soc) ? $this->fk_soc : "null").",";
754 $sql .= " ref_customer=".(isset($this->ref_customer) ? "'".$this->db->escape($this->ref_customer)."'" : "null").",";
755 $sql .= " price=".(isset($this->price) ? $this->price : "null").",";
756 $sql .= " price_ttc=".(isset($this->price_ttc) ? $this->price_ttc : "null").",";
757 $sql .= " price_min=".(isset($this->price_min) ? $this->price_min : "null").",";
758 $sql .= " price_min_ttc=".(isset($this->price_min_ttc) ? $this->price_min_ttc : "null").",";
759 $sql .= " price_base_type=".(isset($this->price_base_type) ? "'".$this->db->escape($this->price_base_type)."'" : "null").",";
760 $sql .= " default_vat_code = ".($this->default_vat_code ? "'".$this->db->escape($this->default_vat_code)."'" : "null").",";
761 $sql .= " tva_tx=".(isset($this->tva_tx) ? (empty($this->tva_tx) ? 0 : $this->tva_tx) : "null").",";
762 $sql .= " recuperableonly=".(isset($this->recuperableonly) ? $this->recuperableonly : "null").",";
763 $sql .= " localtax1_tx=".(isset($this->localtax1_tx) ? (empty($this->localtax1_tx) ? 0 : $this->localtax1_tx) : "null").",";
764 $sql .= " localtax2_tx=".(isset($this->localtax2_tx) ? (empty($this->localtax2_tx) ? 0 : $this->localtax2_tx) : "null").",";
765 $sql .= " localtax1_type=".(!empty($this->localtax1_type) ? "'".$this->db->escape($this->localtax1_type)."'" : "'0'").",";
766 $sql .= " localtax2_type=".(!empty($this->localtax2_type) ? "'".$this->db->escape($this->localtax2_type)."'" : "'0'").",";
767 $sql .= " fk_user=".$user->id.",";
768 $sql .= " import_key=".(isset($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null");
769
770 $sql .= " WHERE rowid=".((int) $this->id);
771
772 dol_syslog(get_class($this)."::update", LOG_DEBUG);
773 $resql = $this->db->query($sql);
774 if (!$resql) {
775 $error++;
776 $this->errors [] = "Error ".$this->db->lasterror();
777 }
778
779 if (!$error && !$notrigger) {
780 // Call trigger
781 $result = $this->call_trigger('PRODUCT_CUSTOMER_PRICE_MODIFY', $user);
782 if ($result < 0) {
783 $error++;
784 }
785 // End call triggers
786 }
787
788 if (!$error) {
789 $result = $this->setPriceOnAffiliateThirdparty($user, $forceupdateaffiliate);
790 if ($result < 0) {
791 $error++;
792 }
793 }
794
795 // Commit or rollback
796 if ($error) {
797 foreach ($this->errors as $errmsg) {
798 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
799 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
800 }
801 $this->db->rollback();
802 return -1 * $error;
803 } else {
804 $this->db->commit();
805 return 1;
806 }
807 }
808
816 public function setPriceOnAffiliateThirdparty($user, $forceupdateaffiliate)
817 {
818 global $conf;
819
820 if (getDolGlobalString('PRODUCT_DISABLE_PROPAGATE_CUSTOMER_PRICES_ON_CHILD_COMPANIES')) {
821 return 0;
822 }
823
824 $error = 0;
825
826 // Find all susidiaries
827 $sql = "SELECT s.rowid";
828 $sql .= " FROM ".$this->db->prefix()."societe as s";
829 $sql .= " WHERE s.parent = ".((int) $this->fk_soc);
830 $sql .= " AND s.entity IN (".getEntity('societe').")";
831
832 dol_syslog(get_class($this)."::setPriceOnAffiliateThirdparty", LOG_DEBUG);
833 $resql = $this->db->query($sql);
834
835 if ($resql) {
836 $this->lines = array();
837 $num = $this->db->num_rows($resql);
838
839 while (($obj = $this->db->fetch_object($resql)) && (empty($error))) {
840 // find if there is an existing line for the product and the subsidiaries
841 $prodsocprice = new ProductCustomerPrice($this->db);
842
843 $filter = array(
844 't.fk_product' => $this->fk_product, 't.fk_soc' => $obj->rowid
845 );
846
847 $result = $prodsocprice->fetchAll('', '', 0, 0, $filter);
848 if ($result < 0) {
849 $error++;
850 $this->error = $prodsocprice->error;
851 } else {
852 // There is one line
853 if (count($prodsocprice->lines) > 0) {
854 // If force update => Update
855 if (!empty($forceupdateaffiliate)) {
856 $prodsocpriceupd = new ProductCustomerPrice($this->db);
857 $prodsocpriceupd->fetch($prodsocprice->lines [0]->id);
858
859 $prodsocpriceupd->price = $this->price;
860 $prodsocpriceupd->price_min = $this->price_min;
861 $prodsocpriceupd->price_base_type = $this->price_base_type;
862 $prodsocpriceupd->tva_tx = $this->tva_tx;
863 $prodsocpriceupd->recuperableonly = $this->recuperableonly;
864
865 $resultupd = $prodsocpriceupd->update($user, 0, $forceupdateaffiliate);
866 if ($resultupd < 0) {
867 $error++;
868 $this->error = $prodsocpriceupd->error;
869 }
870 }
871 } else {
872 // If line do not exits then create it
873 $prodsocpricenew = new ProductCustomerPrice($this->db);
874 $prodsocpricenew->fk_soc = $obj->rowid;
875 $prodsocpricenew->ref_customer = $obj->ref_customer;
876 $prodsocpricenew->fk_product = $this->fk_product;
877 $prodsocpricenew->price = $this->price;
878 $prodsocpricenew->price_min = $this->price_min;
879 $prodsocpricenew->price_base_type = $this->price_base_type;
880 $prodsocpricenew->tva_tx = $this->tva_tx;
881 $prodsocpricenew->recuperableonly = $this->recuperableonly;
882
883 $resultupd = $prodsocpricenew->create($user, 0, $forceupdateaffiliate);
884 if ($resultupd < 0) {
885 $error++;
886 $this->error = $prodsocpricenew->error;
887 }
888 }
889 }
890 }
891 $this->db->free($resql);
892
893 if (empty($error)) {
894 return 1;
895 } else {
896 return -1;
897 }
898 } else {
899 $this->error = "Error ".$this->db->lasterror();
900 return -1;
901 }
902 }
903
911 public function delete($user, $notrigger = 0)
912 {
913 global $conf, $langs;
914 $error = 0;
915
916 $this->db->begin();
917
918 if (!$notrigger) {
919 $result = $this->call_trigger('PRODUCT_CUSTOMER_PRICE_DELETE', $user);
920 if ($result < 0) {
921 $error++;
922 }
923 }
924
925 if (!$error) {
926 $sql = "DELETE FROM ".$this->db->prefix()."product_customer_price";
927 $sql .= " WHERE rowid=".((int) $this->id);
928
929 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
930 $resql = $this->db->query($sql);
931 if (!$resql) {
932 $error++;
933 $this->errors [] = "Error ".$this->db->lasterror();
934 }
935 }
936
937 // Commit or rollback
938 if ($error) {
939 foreach ($this->errors as $errmsg) {
940 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
941 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
942 }
943 $this->db->rollback();
944 return -1 * $error;
945 } else {
946 $this->db->commit();
947 return 1;
948 }
949 }
950
958 public function createFromClone(User $user, $fromid)
959 {
960 $error = 0;
961
962 $object = new ProductCustomerPrice($this->db);
963
964 $this->db->begin();
965
966 // Load source object
967 $object->fetch($fromid);
968 $object->id = 0;
969 $object->statut = 0;
970
971 // Clear fields
972 // ...
973
974 // Create clone
975 $object->context['createfromclone'] = 'createfromclone';
976 $result = $object->create($user);
977
978 // Other options
979 if ($result < 0) {
980 $this->error = $object->error;
981 $this->errors = array_merge($this->errors, $object->errors);
982 $error++;
983 }
984
985 if (!$error) {
986 }
987
988 unset($object->context['createfromclone']);
989
990 // End
991 if (!$error) {
992 $this->db->commit();
993 return $object->id;
994 } else {
995 $this->db->rollback();
996 return -1;
997 }
998 }
999
1006 public function initAsSpecimen()
1007 {
1008 $this->id = 0;
1009
1010 $this->entity = '';
1011 $this->datec = '';
1012 $this->tms = '';
1013 $this->fk_product = '';
1014 $this->fk_soc = '';
1015 $this->ref_customer = '';
1016 $this->price = '';
1017 $this->price_ttc = '';
1018 $this->price_min = '';
1019 $this->price_min_ttc = '';
1020 $this->price_base_type = '';
1021 $this->default_vat_code = '';
1022 $this->tva_tx = '';
1023 $this->recuperableonly = '';
1024 $this->localtax1_tx = '';
1025 $this->localtax2_tx = '';
1026 $this->fk_user = '';
1027 $this->import_key = '';
1028 }
1029}
1030
1035{
1039 public $id;
1040
1044 public $entity;
1045
1046 public $datec = '';
1047 public $tms = '';
1048
1052 public $fk_product;
1053
1057 public $ref_customer;
1058
1062 public $fk_soc;
1063
1064 public $price;
1065 public $price_ttc;
1066 public $price_min;
1067 public $price_min_ttc;
1068 public $price_base_type;
1069 public $default_vat_code;
1070 public $tva_tx;
1071 public $recuperableonly;
1072 public $localtax1_tx;
1073 public $localtax2_tx;
1074
1078 public $fk_user;
1079
1080 public $import_key;
1081 public $socname;
1082 public $prodref;
1083}
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.
fetchAll($sortorder='', $sortfield='', $limit=0, $offset=0, $filter=array())
Load all customer prices in memory from database.
update($user=0, $notrigger=0, $forceupdateaffiliate=0)
Update object into database.
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.
createFromClone(User $user, $fromid)
Load an object from its id and create a new one in 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.
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.