dolibarr 25.0.0-alpha
receptionlinebatch.class.php
1<?php
2/* Copyright (C) 2015 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
4 * Copyright (C) 2024-2025 Frédéric France <frederic.france@free.fr>
5 * Copyright (C) 2024 Christophe Battarel <christophe@altairis.fr>
6 * Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
7 * Copyright (C) 2025 Nick Fragoulis
8 * Copyright (C) 2026 Jose MARTINEZ <jose.martinez@pichinov.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 */
23
31// Put here all includes required by your class file
32require_once DOL_DOCUMENT_ROOT."/core/class/commonobject.class.php";
33require_once DOL_DOCUMENT_ROOT."/reception/class/reception.class.php";
34
35
40{
45 public $TRIGGER_PREFIX = 'LINERECEPTION'; // to be overridden in child class implementations, i.e. 'BILL', 'TASK', 'PROPAL', etc.
46
50 public $db;
51
55 public $element = 'receptionlinebatch';
56
60 public $table_element = 'receptiondet_batch';
61 public $lines = array();
62
66 public $id;
67
71 public $fk_reception;
72
76 public $fk_element;
77
81 public $origin_id;
82
86 public $fk_elementdet;
87
91 public $origin_line_id;
92
96 public $element_type;
97
101 public $fk_product;
102
106 public $qty;
107
111 public $qty_asked;
112
116 public $libelle;
120 public $label;
124 public $desc;
128 public $tva_tx;
132 public $vat_src_code;
136 public $ref_supplier;
137
141 public $fk_entrepot;
142
146 public $fk_user;
147
151 public $datec = '';
155 public $comment;
156
160 public $status;
161
165 public $batch;
169 public $eatby = null;
173 public $sellby = null;
177 public $cost_price = 0;
178
182 public $ref_fourn;
186 public $rang;
187
188
194 public function __construct($db)
195 {
196 $this->db = $db;
197
198 // List of language codes for status
199 $this->labelStatus[0] = 'Received';
200 $this->labelStatus[1] = 'Verified';
201 $this->labelStatus[2] = 'Denied';
202 $this->labelStatusShort[0] = 'Received';
203 $this->labelStatusShort[1] = 'Verified';
204 $this->labelStatusShort[2] = 'Denied';
205 }
206
207
215 public function create($user, $notrigger = 0)
216 {
217 $error = 0;
218
219 // Clean parameters
220 if (isset($this->fk_element)) {
221 $this->fk_element = (int) $this->fk_element;
222 }
223 if (isset($this->fk_product)) {
224 $this->fk_product = (int) $this->fk_product;
225 }
226 if (isset($this->fk_elementdet)) {
227 $this->fk_elementdet = (int) $this->fk_elementdet;
228 }
229 if (isset($this->qty)) {
230 $this->qty = (float) $this->qty;
231 }
232 if (isset($this->fk_entrepot)) {
233 $this->fk_entrepot = (int) $this->fk_entrepot;
234 }
235 if (isset($this->fk_user)) {
236 $this->fk_user = (int) $this->fk_user;
237 }
238 if (isset($this->comment)) {
239 $this->comment = trim($this->comment);
240 }
241 if (isset($this->status)) {
242 $this->status = (int) $this->status;
243 }
244 if (isset($this->batch)) {
245 $this->batch = trim($this->batch);
246 }
247 if (empty($this->datec)) {
248 $this->datec = dol_now();
249 }
250
251 // Check parameters
252 if (empty($this->fk_product)) {
253 $this->error = 'Error, property ->fk_product must not be empty to create a line of reception';
254 return -1;
255 }
256 if (empty($this->fk_reception)) {
257 $this->error = 'Error, property ->fk_reception must not be empty to create a line of reception';
258 return -1;
259 }
260
261
262 if (empty($this->rang)) {
263 $this->rang = 0;
264 }
265
266 // Rank to use
267 $ranktouse = $this->rang;
268 if ($ranktouse == -1) {
269 $rangmax = $this->line_max($this->fk_reception);
270 $ranktouse = $rangmax + 1;
271 }
272
273 // Insert request
274 $sql = "INSERT INTO ".MAIN_DB_PREFIX.$this->table_element."(";
275 $sql .= "fk_product,";
276 $sql .= "fk_element,";
277 $sql .= "fk_elementdet,";
278 $sql .= "element_type,";
279 $sql .= "qty,";
280 $sql .= "fk_entrepot,";
281 $sql .= "fk_user,";
282 $sql .= "datec,";
283 $sql .= "comment,";
284 $sql .= "status,";
285 $sql .= "batch,";
286 $sql .= "eatby,";
287 $sql .= "sellby,";
288 $sql .= "fk_unit,";
289 $sql .= "description,";
290 $sql .= "rang,";
291 $sql .= "fk_reception,";
292 $sql .= "cost_price,";
293 $sql .= "ref_fourn";
294 $sql .= ") VALUES (";
295 $sql .= " ".(!isset($this->fk_product) ? 'NULL' : (int) $this->fk_product).",";
296 $sql .= " ".(!isset($this->fk_element) ? 'NULL' : (int) $this->fk_element).",";
297 $sql .= " ".(!isset($this->fk_elementdet) ? 'NULL' : (int) $this->fk_elementdet).",";
298 $sql .= " '".(!isset($this->element_type) ? "supplier_order" : $this->db->escape($this->element_type))."',";
299 $sql .= " ".(!isset($this->qty) ? 'NULL' : (float) $this->qty).",";
300 $sql .= " ".(!isset($this->fk_entrepot) ? 'NULL' : (int) $this->fk_entrepot).",";
301 $sql .= " ".(!isset($this->fk_user) ? 'NULL' : (int) $this->fk_user).",";
302 $sql .= " ".(!isset($this->datec) || dol_strlen($this->datec) == 0 ? 'NULL' : "'".$this->db->idate($this->datec)."'").",";
303 $sql .= " ".(!isset($this->comment) ? 'NULL' : "'".$this->db->escape($this->comment)."'").",";
304 $sql .= " ".(!isset($this->status) ? 'NULL' : (int) $this->status).",";
305 $sql .= " ".(!isset($this->batch) ? 'NULL' : "'".$this->db->escape($this->batch)."'").",";
306 $sql .= " ".(!isset($this->eatby) || dol_strlen((string) $this->eatby) == 0 ? 'NULL' : "'".$this->db->idate($this->eatby)."'").",";
307 $sql .= " ".(!isset($this->sellby) || dol_strlen((string) $this->sellby) == 0 ? 'NULL' : "'".$this->db->idate($this->sellby)."'").",";
308 $sql .= " ".((int) $this->fk_unit);
309 $sql .= ", '".(empty($this->description) ? '' : $this->db->escape($this->description))."'";
310 $sql .= ", ".((int) $ranktouse).",";
311 $sql .= " ".((int) $this->fk_reception).",";
312 $sql .= " ".(!isset($this->cost_price) ? '0' : (float) $this->cost_price).",";
313 $sql .= " ".(!isset($this->ref_fourn) ? 'NULL' : "'".$this->db->escape($this->ref_fourn)."'");
314 $sql .= ")";
315
316 $this->db->begin();
317
318 dol_syslog(__METHOD__, LOG_DEBUG);
319 $resql = $this->db->query($sql);
320 if (!$resql) {
321 $error++;
322 $this->errors[] = "Error ".$this->db->lasterror();
323 }
324
325 if (!$error) {
326 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
327
328 if (!$notrigger) {
329 // Call triggers
330 $result = $this->call_trigger('LINERECEPTION_CREATE', $user);
331 if ($result < 0) {
332 $error++;
333 }
334 // End call triggers
335 }
336 }
337
338 // Create extrafields
339 if (!$error) {
340 $result = $this->insertExtraFields();
341 if ($result < 0) {
342 $error++;
343 }
344 }
345
346 // Commit or rollback
347 if ($error) {
348 foreach ($this->errors as $errmsg) {
349 dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
350 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
351 }
352 $this->db->rollback();
353 return -1 * $error;
354 } else {
355 $this->db->commit();
356 return $this->id;
357 }
358 }
359
360
368 public function insert($user = null, $notrigger = 0)
369 {
370 global $langs;
371 $error = 0;
372
373 // Check parameters
374 $origin_id = $this->origin_id;
375 if ($origin_id > 0) {
376 if ((empty($this->fk_reception)
377 || (empty($this->fk_elementdet))
378 || !is_numeric($this->qty))) {
379 $langs->load('errors');
380 $this->errors[] = $langs->trans('ErrorMandatoryParametersNotProvided');
381 return -1;
382 }
383 } else {
384 if (empty($this->fk_reception) || !is_numeric($this->qty)) {
385 $langs->load('errors');
386 $this->errors[] = $langs->trans('ErrorMandatoryParametersNotProvided');
387 return -1;
388 }
389 }
390 $this->db->begin();
391
392 if (empty($this->rang)) {
393 $this->rang = 0;
394 }
395
396 // Rank to use
397 $ranktouse = $this->rang;
398 if ($ranktouse == -1) {
399 $rangmax = $this->line_max($this->fk_reception);
400 $ranktouse = $rangmax + 1;
401 }
402
403 $sql = "INSERT INTO ".MAIN_DB_PREFIX."receptiondet_batch (";
404 $sql .= "fk_reception";
405 $sql .= ", fk_entrepot";
406 $sql .= ", fk_elementdet";
407 $sql .= ", fk_product";
408 $sql .= ", element_type";
409 $sql .= ", qty";
410 $sql .= ", fk_unit";
411 $sql .= ", description";
412 $sql .= ", rang";
413 $sql .= ") VALUES (";
414 $sql .= ((int) $this->fk_reception);
415 $sql .= ", ".(empty($this->fk_entrepot) ? 'NULL' : ((int) $this->fk_entrepot));
416 $sql .= ", ".(empty($this->fk_elementdet) ? 'NULL' : ((int) $this->fk_elementdet));
417 $sql .= ", ".(empty($this->fk_product) ? 'NULL' : ((int) $this->fk_product));
418 $sql .= ", '".(empty($this->element_type) ? 'order' : $this->db->escape($this->element_type))."'";
419 $sql .= ", ".price2num($this->qty, 'MS');
420 $sql .= ", ".((int) $this->fk_unit);
421 $sql .= ", '".(empty($this->description) ? '' : $this->db->escape($this->description))."'";
422 $sql .= ", ".((int) $ranktouse);
423 $sql .= ")";
424
425 dol_syslog(get_class($this)."::insert", LOG_DEBUG);
426 $resql = $this->db->query($sql);
427 if ($resql) {
428 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."receptiondet_batch");
429
430
431 $result = $this->insertExtraFields();
432 if ($result < 0) {
433 $error++;
434 }
435
436
437 if (!$error && !$notrigger) {
438 // Call trigger
439 $result = $this->call_trigger('LINERECEPTION_INSERT', $user);
440 if ($result < 0) {
441 $error++;
442 }
443 // End call triggers
444 }
445
446 if ($error) {
447 foreach ($this->errors as $errmsg) {
448 dol_syslog(__METHOD__.' '.$errmsg, LOG_ERR);
449 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
450 }
451 }
452 } else {
453 $error++;
454 }
455
456 if ($error) {
457 $this->db->rollback();
458 return -1;
459 } else {
460 $this->db->commit();
461 return $this->id;
462 }
463 }
464
472 public function fetch($id, $ref = '')
473 {
474 $sql = "SELECT";
475 $sql .= " t.rowid,";
476 $sql .= " t.fk_element,";
477 $sql .= " t.fk_elementdet,";
478 $sql .= " t.element_type,";
479 $sql .= " t.fk_product,";
480 $sql .= " t.qty,";
481 $sql .= " t.fk_entrepot,";
482 $sql .= " t.fk_user,";
483 $sql .= " t.datec,";
484 $sql .= " t.comment,";
485 $sql .= " t.status,";
486 $sql .= " t.tms,";
487 $sql .= " t.batch, t.cost_price, t.ref_fourn,";
488 $sql .= " t.eatby,";
489 $sql .= " t.sellby,";
490 $sql .= " t.fk_unit,";
491 $sql .= " t.description,";
492 $sql .= " t.rang,";
493 $sql .= " t.fk_reception";
494 $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." as t";
495 if ($ref) {
496 $sql .= " WHERE t.ref = '".$this->db->escape($ref)."'";
497 } else {
498 $sql .= " WHERE t.rowid = ".((int) $id);
499 }
500
501 dol_syslog(get_class($this)."::fetch");
502 $resql = $this->db->query($sql);
503 if ($resql) {
504 if ($this->db->num_rows($resql)) {
505 $obj = $this->db->fetch_object($resql);
506
507 $this->id = $obj->rowid;
508
509 $this->fk_element = $obj->fk_element;
510 $this->origin_id = $obj->fk_element;
511 $this->fk_elementdet = $obj->fk_elementdet;
512 $this->origin_line_id = $obj->fk_elementdet;
513 $this->element_type = $obj->element_type;
514 $this->origin_type = $obj->element_type;
515 $this->fk_product = $obj->fk_product;
516 $this->qty = $obj->qty;
517 $this->fk_entrepot = $obj->fk_entrepot;
518 $this->fk_user = $obj->fk_user;
519 $this->datec = $this->db->jdate($obj->datec);
520 $this->comment = $obj->comment;
521 $this->status = $obj->status;
522 $this->tms = $this->db->jdate($obj->tms);
523 $this->batch = $obj->batch;
524 $this->cost_price = $obj->cost_price;
525 $this->ref_fourn = $obj->ref_fourn;
526 $this->eatby = $this->db->jdate($obj->eatby);
527 $this->sellby = $this->db->jdate($obj->sellby);
528 $this->description = $obj->description;
529 $this->fk_unit = $obj->fk_unit;
530 $this->rang = $obj->rang;
531 $this->fk_reception = $obj->fk_reception;
532
533 $this->fetch_optionals();
534 }
535 $this->db->free($resql);
536
537 return 1;
538 } else {
539 $this->error = "Error ".$this->db->lasterror();
540 return -1;
541 }
542 }
543
544
552 public function update($user, $notrigger = 0)
553 {
554 $error = 0;
555
556 // Clean parameters
557
558 if (isset($this->fk_element)) {
559 $this->fk_element = (int) $this->fk_element;
560 }
561 if (isset($this->fk_product)) {
562 $this->fk_product = (int) $this->fk_product;
563 }
564 if (isset($this->fk_elementdet)) {
565 $this->fk_elementdet = (int) $this->fk_elementdet;
566 }
567 if (isset($this->qty)) {
568 $this->qty = (float) $this->qty;
569 }
570 if (isset($this->fk_entrepot)) {
571 $this->fk_entrepot = (int) $this->fk_entrepot;
572 }
573 if (isset($this->fk_user)) {
574 $this->fk_user = (int) $this->fk_user;
575 }
576 if (isset($this->comment)) {
577 $this->comment = trim($this->comment);
578 }
579 if (isset($this->status)) {
580 $this->status = (int) $this->status;
581 }
582 if (isset($this->batch)) {
583 $this->batch = trim($this->batch);
584 }
585
586
587 // Check parameters
588 // Put here code to add a control on parameters values
589
590 // Update request
591 $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET";
592 $sql .= " fk_element=".(isset($this->fk_element) ? ((int) $this->fk_element) : "null").",";
593 $sql .= " fk_product=".(isset($this->fk_product) ? ((int) $this->fk_product) : "null").",";
594 $sql .= " fk_elementdet=".(isset($this->fk_elementdet) ? ((int) $this->fk_elementdet) : "null").",";
595 $sql .= " qty=".(isset($this->qty) ? ((float) $this->qty) : "null").",";
596 $sql .= " fk_entrepot=".(isset($this->fk_entrepot) ? ((int) $this->fk_entrepot) : "null").",";
597 $sql .= " fk_user=".(isset($this->fk_user) ? ((int) $this->fk_user) : "null").",";
598 $sql .= " datec=".(dol_strlen($this->datec) != 0 ? "'".$this->db->idate($this->datec)."'" : 'null').",";
599 $sql .= " comment=".(isset($this->comment) ? "'".$this->db->escape($this->comment)."'" : "null").",";
600 $sql .= " status=".(isset($this->status) ? ((int) $this->status) : "null").",";
601 $sql .= " tms=".(dol_strlen((string) $this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
602 $sql .= " batch=".(isset($this->batch) ? "'".$this->db->escape($this->batch)."'" : "null").",";
603 $sql .= " cost_price=".(isset($this->cost_price) ? ((float) $this->cost_price) : "0").",";
604 $sql .= " ref_fourn=".(isset($this->ref_fourn) ? "'".$this->db->escape($this->ref_fourn)."'" : "null").",";
605 $sql .= " eatby=".(dol_strlen((string) $this->eatby) != 0 ? "'".$this->db->idate((int) $this->eatby)."'" : 'null').",";
606 $sql .= " sellby=".(dol_strlen((string) $this->sellby) != 0 ? "'".$this->db->idate((int) $this->sellby)."'" : 'null').",";
607 $sql .= " fk_unit = ".((int) $this->fk_unit);
608 $sql .= " WHERE rowid=".((int) $this->id);
609
610 $this->db->begin();
611
612 dol_syslog(__METHOD__);
613 $resql = $this->db->query($sql);
614 if (!$resql) {
615 $error++;
616 $this->errors[] = "Error ".$this->db->lasterror();
617 }
618
619 if (!$error) {
620 if (empty($this->id) && !empty($this->rowid)) {
621 $this->id = $this->rowid;
622 }
623 $result = $this->insertExtraFields();
624 if ($result < 0) {
625 $error++;
626 }
627
628 if (!$notrigger) {
629 // Call triggers
630 $result = $this->call_trigger('LINERECEPTION_MODIFY', $user);
631 if ($result < 0) {
632 $error++;
633 }
634 // End call triggers
635 }
636 }
637
638 // Commit or rollback
639 if ($error) {
640 foreach ($this->errors as $errmsg) {
641 dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
642 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
643 }
644 $this->db->rollback();
645 return -1 * $error;
646 } else {
647 $this->db->commit();
648 return 1;
649 }
650 }
651
652
660 public function delete($user, $notrigger = 0)
661 {
662 $error = 0;
663
664 $this->db->begin();
665
666 if (!$notrigger) {
667 // Call triggers
668 $result = $this->call_trigger('LINERECEPTION_DELETE', $user);
669 if ($result < 0) {
670 $error++;
671 }
672 // End call triggers
673 }
674
675
676 // Remove extrafields
677 if (!$error) {
678 $result = $this->deleteExtraFields();
679 if ($result < 0) {
680 $error++;
681 dol_syslog(get_class($this)."::delete error deleteExtraFields ".$this->error, LOG_ERR);
682 }
683 }
684
685 if (!$error) {
686 $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
687 $sql .= " WHERE rowid=".((int) $this->id);
688
689 dol_syslog(__METHOD__);
690 $resql = $this->db->query($sql);
691 if (!$resql) {
692 $error++;
693 $this->errors[] = "Error ".$this->db->lasterror();
694 }
695 }
696
697 // Commit or rollback
698 if ($error) {
699 foreach ($this->errors as $errmsg) {
700 dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
701 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
702 }
703 $this->db->rollback();
704 return -1 * $error;
705 } else {
706 $this->db->commit();
707 return 1;
708 }
709 }
710
711
719 public function createFromClone(User $user, $fromid)
720 {
721 $error = 0;
722
723 $object = new ReceptionLineBatch($this->db);
724
725 $this->db->begin();
726
727 // Load source object
728 $object->fetch($fromid);
729 $object->id = 0;
730 $object->status = 0;
731 $object->statut = 0;
732
733 // Clear fields
734 // ...
735
736 // Create clone
737 $object->context['createfromclone'] = 'createfromclone';
738 $result = $object->create($user);
739
740 // Other options
741 if ($result < 0) {
743 $error++;
744 }
745
746 if (!$error) {
747 }
748
749 unset($object->context['createfromclone']);
750
751 // End
752 if (!$error) {
753 $this->db->commit();
754 return $object->id;
755 } else {
756 $this->db->rollback();
757 return -1;
758 }
759 }
760
761
768 public function getLibStatut($mode = 0)
769 {
770 return $this->LibStatut($this->status, $mode);
771 }
772
773 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
781 public function LibStatut($status, $mode = 0)
782 {
783 // phpcs:enable
784 global $langs;
785 $langs->load('orders');
786
787 if ($mode == 0) {
788 return $langs->trans($this->labelStatus[$status]);
789 } elseif ($mode == 1) {
790 return $langs->trans($this->labelStatusShort[$status]);
791 } elseif ($mode == 2) {
792 return $langs->trans($this->labelStatus[$status]);
793 } elseif ($mode == 3) {
794 if ($status == 0) {
795 return img_picto($langs->trans($this->labelStatus[$status]), 'statut0');
796 } elseif ($status == 1) {
797 return img_picto($langs->trans($this->labelStatus[$status]), 'statut4');
798 } elseif ($status == 2) {
799 return img_picto($langs->trans($this->labelStatus[$status]), 'statut8');
800 }
801 } elseif ($mode == 4) {
802 if ($status == 0) {
803 return img_picto($langs->trans($this->labelStatus[$status]), 'statut0').' '.$langs->trans($this->labelStatus[$status]);
804 } elseif ($status == 1) {
805 return img_picto($langs->trans($this->labelStatus[$status]), 'statut4').' '.$langs->trans($this->labelStatus[$status]);
806 } elseif ($status == 2) {
807 return img_picto($langs->trans($this->labelStatus[$status]), 'statut8').' '.$langs->trans($this->labelStatus[$status]);
808 }
809 } elseif ($mode == 5) {
810 if ($status == 0) {
811 return '<span class="hideonsmartphone">'.$langs->trans($this->labelStatusShort[$status]).' </span>'.img_picto($langs->trans($this->labelStatus[$status]), 'statut0');
812 } elseif ($status == 1) {
813 return '<span class="hideonsmartphone">'.$langs->trans($this->labelStatusShort[$status]).' </span>'.img_picto($langs->trans($this->labelStatus[$status]), 'statut4');
814 } elseif ($status == 2) {
815 return '<span class="hideonsmartphone">'.$langs->trans($this->labelStatusShort[$status]).' </span>'.img_picto($langs->trans($this->labelStatus[$status]), 'statut8');
816 }
817 }
818 return "";
819 }
820
821
828 public function initAsSpecimen()
829 {
830 $this->id = 0;
831
832 $this->fk_element = 0;
833 $this->fk_product = 0;
834 $this->fk_elementdet = 0;
835 $this->qty = 0;
836 $this->fk_entrepot = 0;
837 $this->fk_user = 0;
838 $this->datec = '';
839 $this->comment = '';
840 $this->status = 0;
841 $this->tms = dol_now();
842 $this->batch = '';
843 $this->eatby = null;
844 $this->sellby = null;
845
846 return 1;
847 }
848
849
861 public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, $filter = '', $filtermode = 'AND')
862 {
863 dol_syslog(__METHOD__, LOG_DEBUG);
864
865 $sql = "SELECT";
866 $sql .= " t.rowid,";
867 $sql .= " t.fk_element,";
868 $sql .= " t.fk_product,";
869 $sql .= " t.fk_elementdet,";
870 $sql .= " t.qty,";
871 $sql .= " t.fk_entrepot,";
872 $sql .= " t.fk_user,";
873 $sql .= " t.datec,";
874 $sql .= " t.comment,";
875 $sql .= " t.status,";
876 $sql .= " t.tms,";
877 $sql .= " t.batch,";
878 $sql .= " t.eatby,";
879 $sql .= " t.sellby,";
880 $sql .= " t.fk_unit,";
881 $sql .= " t.description,";
882 $sql .= " t.rang";
883 $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." as t";
884
885 // Manage filter
886 if (is_array($filter)) {
887 $sqlwhere = array();
888 if (count($filter) > 0) {
889 foreach ($filter as $key => $value) {
890 if ($key == 't.comment') {
891 $sqlwhere [] = $this->db->sanitize($key)." LIKE '%".$this->db->escape($this->db->escapeforlike($value))."%'";
892 } elseif ($key == 't.datec' || $key == 't.tms' || $key == 't.eatby' || $key == 't.sellby' || $key == 't.batch') {
893 $sqlwhere [] = $this->db->sanitize($key)." = '".$this->db->escape($value)."'";
894 } elseif ($key == 'qty') {
895 $sqlwhere [] = $this->db->sanitize($key)." = ".((float) $value);
896 } else {
897 $sqlwhere [] = $this->db->sanitize($key)." = ".((int) $value);
898 }
899 }
900 }
901 if (count($sqlwhere) > 0) {
902 $sql .= ' WHERE '.implode(' '.$this->db->sanitize($filtermode).' ', $sqlwhere);
903 }
904
905 $filter = '';
906 }
907
908 // Manage filter
909 $errormessage = '';
910 $sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);
911 if ($errormessage) {
912 $this->errors[] = $errormessage;
913 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
914 return -1;
915 }
916
917 if (!empty($sortfield)) {
918 $sql .= $this->db->order($sortfield, $sortorder);
919 }
920 if (!empty($limit)) {
921 $sql .= $this->db->plimit($limit, $offset);
922 }
923 $this->lines = array();
924
925 $resql = $this->db->query($sql);
926 if ($resql) {
927 $num = $this->db->num_rows($resql);
928
929 while ($obj = $this->db->fetch_object($resql)) {
930 $line = new self($this->db);
931
932 $line->id = $obj->rowid;
933
934 $line->fk_element = $obj->fk_element;
935 $line->fk_product = $obj->fk_product;
936 $line->fk_elementdet = $obj->fk_elementdet;
937 $line->qty = $obj->qty;
938 $line->fk_entrepot = $obj->fk_entrepot;
939 $line->fk_user = $obj->fk_user;
940 $line->datec = $this->db->jdate($obj->datec);
941 $line->comment = $obj->comment;
942 $line->status = $obj->status;
943 $line->tms = $this->db->jdate($obj->tms);
944 $line->batch = $obj->batch;
945 $line->eatby = $this->db->jdate($obj->eatby);
946 $line->sellby = $this->db->jdate($obj->sellby);
947 $line->description = $obj->description;
948 $line->fk_unit = $obj->fk_unit;
949 $line->rang = $obj->rang;
950 $line->fetch_optionals();
951
952 $this->lines[$line->id] = $line;
953 }
954 $this->db->free($resql);
955
956 return $num;
957 } else {
958 $this->errors[] = 'Error '.$this->db->lasterror();
959 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
960
961 return -1;
962 }
963 }
964}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
fetch_optionals($rowid=null, $optionsArray=null)
Function to get extra fields of an object into $this->array_options This method is in most cases call...
setErrorsFromObject($object)
setErrorsFromObject
deleteExtraFields()
Delete all extra fields values for the current object.
line_max($fk_parent_line=0)
Get max value used for position of line (rang)
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
Parent class for class inheritance lines of business objects This class is useless for the moment so ...
Class to manage table commandefournisseurdispatch.
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
insert($user=null, $notrigger=0)
Insert line into database.
createFromClone(User $user, $fromid)
Load an object from its id and create a new one in database.
update($user, $notrigger=0)
Update object into database.
LibStatut($status, $mode=0)
Return label of a status.
$table_element
Name of table without prefix where object is stored.
create($user, $notrigger=0)
Create object into database.
getLibStatut($mode=0)
Return label of the status of object.
fetchAll($sortorder='', $sortfield='', $limit=0, $offset=0, $filter='', $filtermode='AND')
Load object in memory from the database.
fetch($id, $ref='')
Load object in memory from the database.
Class to manage Dolibarr users.
print $langs trans("Ref").' m titre as m m statut as status
Or an array listing all the potential status of the object: array: int of the status => translated la...
Definition index.php:169
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
dol_now($mode='gmt')
Return date for now.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0, $forbiddenfields=array())
forgeSQLFromUniversalSearchCriteria
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.