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,";
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->eatby = $this->db->jdate($obj->eatby);
525 $this->sellby = $this->db->jdate($obj->sellby);
526 $this->description = $obj->description;
527 $this->fk_unit = $obj->fk_unit;
528 $this->rang = $obj->rang;
529 $this->fk_reception = $obj->fk_reception;
530
531 $this->fetch_optionals();
532 }
533 $this->db->free($resql);
534
535 return 1;
536 } else {
537 $this->error = "Error ".$this->db->lasterror();
538 return -1;
539 }
540 }
541
542
550 public function update($user, $notrigger = 0)
551 {
552 $error = 0;
553
554 // Clean parameters
555
556 if (isset($this->fk_element)) {
557 $this->fk_element = (int) $this->fk_element;
558 }
559 if (isset($this->fk_product)) {
560 $this->fk_product = (int) $this->fk_product;
561 }
562 if (isset($this->fk_elementdet)) {
563 $this->fk_elementdet = (int) $this->fk_elementdet;
564 }
565 if (isset($this->qty)) {
566 $this->qty = (float) $this->qty;
567 }
568 if (isset($this->fk_entrepot)) {
569 $this->fk_entrepot = (int) $this->fk_entrepot;
570 }
571 if (isset($this->fk_user)) {
572 $this->fk_user = (int) $this->fk_user;
573 }
574 if (isset($this->comment)) {
575 $this->comment = trim($this->comment);
576 }
577 if (isset($this->status)) {
578 $this->status = (int) $this->status;
579 }
580 if (isset($this->batch)) {
581 $this->batch = trim($this->batch);
582 }
583
584
585 // Check parameters
586 // Put here code to add a control on parameters values
587
588 // Update request
589 $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET";
590 $sql .= " fk_element=".(isset($this->fk_element) ? ((int) $this->fk_element) : "null").",";
591 $sql .= " fk_product=".(isset($this->fk_product) ? ((int) $this->fk_product) : "null").",";
592 $sql .= " fk_elementdet=".(isset($this->fk_elementdet) ? ((int) $this->fk_elementdet) : "null").",";
593 $sql .= " qty=".(isset($this->qty) ? ((float) $this->qty) : "null").",";
594 $sql .= " fk_entrepot=".(isset($this->fk_entrepot) ? ((int) $this->fk_entrepot) : "null").",";
595 $sql .= " fk_user=".(isset($this->fk_user) ? ((int) $this->fk_user) : "null").",";
596 $sql .= " datec=".(dol_strlen($this->datec) != 0 ? "'".$this->db->idate($this->datec)."'" : 'null').",";
597 $sql .= " comment=".(isset($this->comment) ? "'".$this->db->escape($this->comment)."'" : "null").",";
598 $sql .= " status=".(isset($this->status) ? ((int) $this->status) : "null").",";
599 $sql .= " tms=".(dol_strlen((string) $this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
600 $sql .= " batch=".(isset($this->batch) ? "'".$this->db->escape($this->batch)."'" : "null").",";
601 $sql .= " eatby=".(dol_strlen((string) $this->eatby) != 0 ? "'".$this->db->idate((int) $this->eatby)."'" : 'null').",";
602 $sql .= " sellby=".(dol_strlen((string) $this->sellby) != 0 ? "'".$this->db->idate((int) $this->sellby)."'" : 'null').",";
603 $sql .= " fk_unit = ".((int) $this->fk_unit);
604 $sql .= " WHERE rowid=".((int) $this->id);
605
606 $this->db->begin();
607
608 dol_syslog(__METHOD__);
609 $resql = $this->db->query($sql);
610 if (!$resql) {
611 $error++;
612 $this->errors[] = "Error ".$this->db->lasterror();
613 }
614
615 if (!$error) {
616 if (empty($this->id) && !empty($this->rowid)) {
617 $this->id = $this->rowid;
618 }
619 $result = $this->insertExtraFields();
620 if ($result < 0) {
621 $error++;
622 }
623
624 if (!$notrigger) {
625 // Call triggers
626 $result = $this->call_trigger('LINERECEPTION_MODIFY', $user);
627 if ($result < 0) {
628 $error++;
629 }
630 // End call triggers
631 }
632 }
633
634 // Commit or rollback
635 if ($error) {
636 foreach ($this->errors as $errmsg) {
637 dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
638 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
639 }
640 $this->db->rollback();
641 return -1 * $error;
642 } else {
643 $this->db->commit();
644 return 1;
645 }
646 }
647
648
656 public function delete($user, $notrigger = 0)
657 {
658 $error = 0;
659
660 $this->db->begin();
661
662 if (!$notrigger) {
663 // Call triggers
664 $result = $this->call_trigger('LINERECEPTION_DELETE', $user);
665 if ($result < 0) {
666 $error++;
667 }
668 // End call triggers
669 }
670
671
672 // Remove extrafields
673 if (!$error) {
674 $result = $this->deleteExtraFields();
675 if ($result < 0) {
676 $error++;
677 dol_syslog(get_class($this)."::delete error deleteExtraFields ".$this->error, LOG_ERR);
678 }
679 }
680
681 if (!$error) {
682 $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
683 $sql .= " WHERE rowid=".((int) $this->id);
684
685 dol_syslog(__METHOD__);
686 $resql = $this->db->query($sql);
687 if (!$resql) {
688 $error++;
689 $this->errors[] = "Error ".$this->db->lasterror();
690 }
691 }
692
693 // Commit or rollback
694 if ($error) {
695 foreach ($this->errors as $errmsg) {
696 dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
697 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
698 }
699 $this->db->rollback();
700 return -1 * $error;
701 } else {
702 $this->db->commit();
703 return 1;
704 }
705 }
706
707
715 public function createFromClone(User $user, $fromid)
716 {
717 $error = 0;
718
719 $object = new ReceptionLineBatch($this->db);
720
721 $this->db->begin();
722
723 // Load source object
724 $object->fetch($fromid);
725 $object->id = 0;
726 $object->status = 0;
727 $object->statut = 0;
728
729 // Clear fields
730 // ...
731
732 // Create clone
733 $object->context['createfromclone'] = 'createfromclone';
734 $result = $object->create($user);
735
736 // Other options
737 if ($result < 0) {
739 $error++;
740 }
741
742 if (!$error) {
743 }
744
745 unset($object->context['createfromclone']);
746
747 // End
748 if (!$error) {
749 $this->db->commit();
750 return $object->id;
751 } else {
752 $this->db->rollback();
753 return -1;
754 }
755 }
756
757
764 public function getLibStatut($mode = 0)
765 {
766 return $this->LibStatut($this->status, $mode);
767 }
768
769 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
777 public function LibStatut($status, $mode = 0)
778 {
779 // phpcs:enable
780 global $langs;
781 $langs->load('orders');
782
783 if ($mode == 0) {
784 return $langs->trans($this->labelStatus[$status]);
785 } elseif ($mode == 1) {
786 return $langs->trans($this->labelStatusShort[$status]);
787 } elseif ($mode == 2) {
788 return $langs->trans($this->labelStatus[$status]);
789 } elseif ($mode == 3) {
790 if ($status == 0) {
791 return img_picto($langs->trans($this->labelStatus[$status]), 'statut0');
792 } elseif ($status == 1) {
793 return img_picto($langs->trans($this->labelStatus[$status]), 'statut4');
794 } elseif ($status == 2) {
795 return img_picto($langs->trans($this->labelStatus[$status]), 'statut8');
796 }
797 } elseif ($mode == 4) {
798 if ($status == 0) {
799 return img_picto($langs->trans($this->labelStatus[$status]), 'statut0').' '.$langs->trans($this->labelStatus[$status]);
800 } elseif ($status == 1) {
801 return img_picto($langs->trans($this->labelStatus[$status]), 'statut4').' '.$langs->trans($this->labelStatus[$status]);
802 } elseif ($status == 2) {
803 return img_picto($langs->trans($this->labelStatus[$status]), 'statut8').' '.$langs->trans($this->labelStatus[$status]);
804 }
805 } elseif ($mode == 5) {
806 if ($status == 0) {
807 return '<span class="hideonsmartphone">'.$langs->trans($this->labelStatusShort[$status]).' </span>'.img_picto($langs->trans($this->labelStatus[$status]), 'statut0');
808 } elseif ($status == 1) {
809 return '<span class="hideonsmartphone">'.$langs->trans($this->labelStatusShort[$status]).' </span>'.img_picto($langs->trans($this->labelStatus[$status]), 'statut4');
810 } elseif ($status == 2) {
811 return '<span class="hideonsmartphone">'.$langs->trans($this->labelStatusShort[$status]).' </span>'.img_picto($langs->trans($this->labelStatus[$status]), 'statut8');
812 }
813 }
814 return "";
815 }
816
817
824 public function initAsSpecimen()
825 {
826 $this->id = 0;
827
828 $this->fk_element = 0;
829 $this->fk_product = 0;
830 $this->fk_elementdet = 0;
831 $this->qty = 0;
832 $this->fk_entrepot = 0;
833 $this->fk_user = 0;
834 $this->datec = '';
835 $this->comment = '';
836 $this->status = 0;
837 $this->tms = dol_now();
838 $this->batch = '';
839 $this->eatby = null;
840 $this->sellby = null;
841
842 return 1;
843 }
844
845
857 public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, $filter = '', $filtermode = 'AND')
858 {
859 dol_syslog(__METHOD__, LOG_DEBUG);
860
861 $sql = "SELECT";
862 $sql .= " t.rowid,";
863 $sql .= " t.fk_element,";
864 $sql .= " t.fk_product,";
865 $sql .= " t.fk_elementdet,";
866 $sql .= " t.qty,";
867 $sql .= " t.fk_entrepot,";
868 $sql .= " t.fk_user,";
869 $sql .= " t.datec,";
870 $sql .= " t.comment,";
871 $sql .= " t.status,";
872 $sql .= " t.tms,";
873 $sql .= " t.batch,";
874 $sql .= " t.eatby,";
875 $sql .= " t.sellby,";
876 $sql .= " t.fk_unit,";
877 $sql .= " t.description,";
878 $sql .= " t.rang";
879 $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." as t";
880
881 // Manage filter
882 if (is_array($filter)) {
883 $sqlwhere = array();
884 if (count($filter) > 0) {
885 foreach ($filter as $key => $value) {
886 if ($key == 't.comment') {
887 $sqlwhere [] = $this->db->sanitize($key)." LIKE '%".$this->db->escape($this->db->escapeforlike($value))."%'";
888 } elseif ($key == 't.datec' || $key == 't.tms' || $key == 't.eatby' || $key == 't.sellby' || $key == 't.batch') {
889 $sqlwhere [] = $this->db->sanitize($key)." = '".$this->db->escape($value)."'";
890 } elseif ($key == 'qty') {
891 $sqlwhere [] = $this->db->sanitize($key)." = ".((float) $value);
892 } else {
893 $sqlwhere [] = $this->db->sanitize($key)." = ".((int) $value);
894 }
895 }
896 }
897 if (count($sqlwhere) > 0) {
898 $sql .= ' WHERE '.implode(' '.$this->db->sanitize($filtermode).' ', $sqlwhere);
899 }
900
901 $filter = '';
902 }
903
904 // Manage filter
905 $errormessage = '';
906 $sql .= forgeSQLFromUniversalSearchCriteria($filter, $errormessage);
907 if ($errormessage) {
908 $this->errors[] = $errormessage;
909 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
910 return -1;
911 }
912
913 if (!empty($sortfield)) {
914 $sql .= $this->db->order($sortfield, $sortorder);
915 }
916 if (!empty($limit)) {
917 $sql .= $this->db->plimit($limit, $offset);
918 }
919 $this->lines = array();
920
921 $resql = $this->db->query($sql);
922 if ($resql) {
923 $num = $this->db->num_rows($resql);
924
925 while ($obj = $this->db->fetch_object($resql)) {
926 $line = new self($this->db);
927
928 $line->id = $obj->rowid;
929
930 $line->fk_element = $obj->fk_element;
931 $line->fk_product = $obj->fk_product;
932 $line->fk_elementdet = $obj->fk_elementdet;
933 $line->qty = $obj->qty;
934 $line->fk_entrepot = $obj->fk_entrepot;
935 $line->fk_user = $obj->fk_user;
936 $line->datec = $this->db->jdate($obj->datec);
937 $line->comment = $obj->comment;
938 $line->status = $obj->status;
939 $line->tms = $this->db->jdate($obj->tms);
940 $line->batch = $obj->batch;
941 $line->eatby = $this->db->jdate($obj->eatby);
942 $line->sellby = $this->db->jdate($obj->sellby);
943 $line->description = $obj->description;
944 $line->fk_unit = $obj->fk_unit;
945 $line->rang = $obj->rang;
946 $line->fetch_optionals();
947
948 $this->lines[$line->id] = $line;
949 }
950 $this->db->free($resql);
951
952 return $num;
953 } else {
954 $this->errors[] = 'Error '.$this->db->lasterror();
955 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
956
957 return -1;
958 }
959 }
960}
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.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2, $allowothertags=array())
Show picto whatever it's its name (generic function)