dolibarr 24.0.2
productlot.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 Juanjo Menent <jmenent@2byte.es>
4 * Copyright (C) 2015 Florian Henry <florian.henry@open-concept.pro>
5 * Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
6 * Copyright (C) 2018-2026 Frédéric France <frederic.france@free.fr>
7 * Copyright (C) 2023 Gauthier VERDOL <gauthier.verdol@atm-consulting.fr>
8 * Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
9 * Copyright (C) 2026 Charlene Benke <charlene@patas-monkey.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <https://www.gnu.org/licenses/>.
23 */
24
31// Put here all includes required by your class file
32require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
33//require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php';
34//require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
35
40{
44 public $element = 'productlot';
45
49 public $table_element = 'product_lot';
50
54 public $picto = 'lot';
55
59 public $stats_propale;
60
64 public $stats_commande;
65
69 public $stats_contrat;
70
74 public $stats_facture;
75
79 public $stats_commande_fournisseur;
80
84 public $stats_expedition;
85
89 public $stats_reception;
90
94 public $stats_supplier_order;
95
99 public $stats_mo;
103 public $stats_bom;
107 public $stats_mrptoconsume;
111 public $stats_mrptoproduce;
115 public $stats_facturerec;
119 public $stats_facture_fournisseur;
120
121
150 public $fields = array(
151 'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'visible' => -2, 'noteditable' => 1, 'notnull' => 1, 'index' => 1, 'position' => 1, 'comment' => 'Id', 'css' => 'left'),
152 'fk_product' => array('type' => 'integer:Product:product/class/product.class.php', 'label' => 'Product', 'enabled' => 1, 'visible' => 1, 'position' => 5, 'notnull' => 1, 'index' => 1, 'searchall' => 1, 'picto' => 'product', 'css' => 'maxwidth500 widthcentpercentminusxx', 'csslist' => 'tdoverflowmax250'),
153 'batch' => array('type' => 'varchar(30)', 'label' => 'Batch', 'enabled' => 1, 'visible' => 1, 'notnull' => 1, 'showoncombobox' => 1, 'index' => 1, 'position' => 10, 'comment' => 'Batch', 'searchall' => 1, 'picto' => 'lot', 'validate' => 1),
154 'entity' => array('type' => 'integer', 'label' => 'Entity', 'enabled' => 1, 'visible' => 0, 'default' => '1', 'notnull' => 1, 'index' => 1, 'position' => 20),
155 'manufacturing_date' => array('type' => 'date', 'label' => 'ManufacturingDate', 'enabled' => 'getDolGlobalInt("PRODUCT_LOT_ENABLE_TRACEABILITY") ? 1 : 0', 'visible' => 'getDolGlobalInt("PRODUCT_LOT_ENABLE_TRACEABILITY") ? 5 : 0', 'position' => 50, 'help' => 'Date of first manufacturing of the lot'),
156 'sellby' => array('type' => 'date', 'label' => 'SellByDate', 'enabled' => 'getDolGlobalInt("PRODUCT_DISABLE_SELLBY") ? 0 : 1', 'visible' => 1, 'notnull' => 0, 'position' => 60),
157 'eatby' => array('type' => 'date', 'label' => 'EatByDate', 'enabled' => 'getDolGlobalInt("PRODUCT_DISABLE_EATBY") ? 0 : 1', 'visible' => 1, 'notnull' => 0, 'position' => 62),
158 'eol_date' => array('type' => 'date', 'label' => 'EndOfLife', 'enabled' => 'getDolGlobalInt("PRODUCT_LOT_ENABLE_QUALITY_CONTROL") ? 1 : 0 ', 'visible' => 'getDolGlobalInt("PRODUCT_LOT_ENABLE_QUALITY_CONTROL") ? 5 : 0', 'position' => 70),
159 'scrapping_date' => array('type' => 'date', 'label' => 'DestructionDate', 'enabled' => 'getDolGlobalInt("PRODUCT_LOT_ENABLE_TRACEABILITY") ? 1 : 0', 'visible' => 'getDolGlobalInt("PRODUCT_LOT_ENABLE_TRACEABILITY") ? 5 : 0', 'position' => 90),
160 //'commissionning_date' => array('type'=>'date', 'label'=>'FirstUseDate', 'enabled'=>'getDolGlobalInt("PRODUCT_LOT_ENABLE_TRACEABILITY", 0)', 'visible'=>5, 'position'=>100),
161 'qc_frequency' => array('type' => 'integer', 'label' => 'QCFrequency', 'enabled' => 'getDolGlobalInt("PRODUCT_LOT_ENABLE_QUALITY_CONTROL") ? 1 : 0', 'visible' => 'getDolGlobalInt("PRODUCT_LOT_ENABLE_QUALITY_CONTROL") ? 5 : 0', 'position' => 110),
162 'lifetime' => array('type' => 'integer', 'label' => 'Lifetime', 'enabled' => 'getDolGlobalInt("PRODUCT_LOT_ENABLE_QUALITY_CONTROL") ? 1 : 0', 'visible' => 'getDolGlobalInt("PRODUCT_LOT_ENABLE_QUALITY_CONTROL") ? 5 : 0', 'position' => 110),
163 'model_pdf' => array('type' => 'varchar(255)', 'label' => 'Model pdf', 'enabled' => 1, 'visible' => 0, 'position' => 215),
164 'last_main_doc' => array('type' => 'varchar(255)', 'label' => 'LastMainDoc', 'enabled' => 1, 'visible' => -2, 'position' => 310),
165 'datec' => array('type' => 'datetime', 'label' => 'DateCreation', 'enabled' => 1, 'visible' => 0, 'notnull' => 1, 'position' => 500),
166 'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'visible' => -2, 'notnull' => 1, 'position' => 501),
167 'fk_user_creat' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserAuthor', 'enabled' => 1, 'visible' => -2, 'notnull' => 1, 'position' => 510, 'foreignkey' => 'llx_user.rowid'),
168 'fk_user_modif' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserModif', 'enabled' => 1, 'visible' => -2, 'notnull' => -1, 'position' => 511),
169 'import_key' => array('type' => 'varchar(14)', 'label' => 'ImportId', 'enabled' => 1, 'visible' => -2, 'notnull' => -1, 'index' => 0, 'position' => 1000)
170 );
171
175 public $fk_product;
176
180 public $batch;
181
185 public $eatby = '';
186
190 public $sellby = '';
191
195 public $eol_date = '';
196
200 public $manufacturing_date = '';
201
205 public $scrapping_date = '';
206 //public $commissionning_date = '';
210 public $qc_frequency = '';
214 public $lifetime = '';
218 public $datec = '';
219
223 public $fk_user_creat;
224
228 public $fk_user_modif;
229
233 public $import_key;
234
235
241 public function __construct(DoliDB $db)
242 {
243 $this->db = $db;
244
245 $this->ismultientitymanaged = 1;
246 $this->isextrafieldmanaged = 1;
247 }
248
255 public function checkSellOrEatByMandatory($onlyFieldName = '')
256 {
257 if (getDolGlobalString('PRODUCT_DISABLE_SELLBY') && getDolGlobalString('PRODUCT_DISABLE_EATBY')) {
258 return 0;
259 }
260
261 $errorMsgArr = array();
262 if ($this->fk_product > 0) {
263 $res = $this->fetch_product();
264 $product = $this->product;
265 if ($res <= 0) {
266 $errorMsgArr[] = $product->errorsToString();
267 }
268
269 if (empty($errorMsgArr)) {
270 $errorMsgArr = self::checkSellOrEatByMandatoryFromProductAndDates($product, $this->sellby, $this->eatby, $onlyFieldName, true);
271 }
272 }
273
274 if (!empty($errorMsgArr)) {
275 $this->errors = array_merge($this->errors, $errorMsgArr);
276 return -1;
277 } else {
278 return 1;
279 }
280 }
281
291 public static function checkSellOrEatByMandatoryFromProductIdAndDates($productId, $sellBy, $eatBy, $onlyFieldName = '')
292 {
293 global $db;
294
295 if (getDolGlobalString('PRODUCT_DISABLE_SELLBY') && getDolGlobalString('PRODUCT_DISABLE_EATBY')) {
296 return null;
297 }
298
299 $errorMsgArr = array();
300 if ($productId > 0) {
301 $product = new Product($db);
302 $res = $product->fetch($productId);
303 if ($res <= 0) {
304 $errorMsgArr[] = $product->errorsToString();
305 }
306
307 if (empty($errorMsgArr)) {
308 $errorMsgArr = self::checkSellOrEatByMandatoryFromProductAndDates($product, $sellBy, $eatBy, $onlyFieldName, true);
309 }
310 }
311
312 return $errorMsgArr;
313 }
314
325 public static function checkSellOrEatByMandatoryFromProductAndDates($product, $sellBy, $eatBy, $onlyFieldName = '', $alreadyCheckConf = false)
326 {
327 global $langs;
328
329 if ($alreadyCheckConf === false && getDolGlobalString('PRODUCT_DISABLE_SELLBY') && getDolGlobalString('PRODUCT_DISABLE_EATBY')) {
330 return null;
331 }
332
333 $errorMsgArr = array();
334 $checkSellByMandatory = false;
335 $checkEatByMandatory = false;
336
337 $sellOrEatByMandatoryId = $product->sell_or_eat_by_mandatory;
338 if (!getDolGlobalString('PRODUCT_DISABLE_SELLBY') && $sellOrEatByMandatoryId == Product::SELL_OR_EAT_BY_MANDATORY_ID_SELL_BY && ($onlyFieldName == '' || $onlyFieldName == 'sellby')) {
339 $checkSellByMandatory = true;
340 } elseif (!getDolGlobalString('PRODUCT_DISABLE_EATBY') && $sellOrEatByMandatoryId == Product::SELL_OR_EAT_BY_MANDATORY_ID_EAT_BY && ($onlyFieldName == '' || $onlyFieldName == 'eatby')) {
341 $checkEatByMandatory = true;
342 } elseif ($sellOrEatByMandatoryId == Product::SELL_OR_EAT_BY_MANDATORY_ID_SELL_AND_EAT) {
343 if (!getDolGlobalString('PRODUCT_DISABLE_SELLBY') && ($onlyFieldName == '' || $onlyFieldName == 'sellby')) {
344 $checkSellByMandatory = true;
345 }
346 if (!getDolGlobalString('PRODUCT_DISABLE_EATBY') && ($onlyFieldName == '' || $onlyFieldName == 'eatby')) {
347 $checkEatByMandatory = true;
348 }
349 }
350
351 if ($checkSellByMandatory) {
352 if (!isset($sellBy) || dol_strlen((string) $sellBy) == 0) {
353 // error : sell by is mandatory
354 $errorMsgArr[] = $langs->trans('ErrorFieldRequired', $langs->transnoentities('SellByDate'));
355 }
356 }
357 if ($checkEatByMandatory) {
358 if (!isset($eatBy) || dol_strlen((string) $eatBy) == 0) {
359 // error : eat by is mandatory
360 $errorMsgArr[] = $langs->trans('ErrorFieldRequired', $langs->transnoentities('EatByDate'));
361 }
362 }
363
364 return $errorMsgArr;
365 }
366
367
375 public function create(User $user, $notrigger = 0)
376 {
377 global $conf, $langs;
378
379 dol_syslog(__METHOD__, LOG_DEBUG);
380
381 $error = 0;
382
383 // Clean parameters
384
385 if (isset($this->entity)) {
386 $this->entity = (int) $this->entity;
387 }
388 if (isset($this->fk_product)) {
389 $this->fk_product = (int) $this->fk_product;
390 }
391 if (isset($this->batch)) {
392 $this->batch = trim($this->batch);
393 }
394 if (isset($this->fk_user_creat)) {
395 $this->fk_user_creat = (int) $this->fk_user_creat;
396 }
397 if (isset($this->fk_user_modif)) {
398 $this->fk_user_modif = (int) $this->fk_user_modif;
399 }
400 if (isset($this->import_key)) {
401 $this->import_key = trim($this->import_key);
402 }
403
404 // Check parameters
405 if ($this->batch === '') {
406 $this->errors[] = $langs->trans("ErrorBadValueForBatch");
407 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
408 return -1;
409 }
410 // Put here code to add control on parameters values
411 $res = $this->checkSellOrEatByMandatory();
412 if ($res < 0) {
413 $error++;
414 }
415
416 if (!$error) {
417 // Insert request
418 $sql = 'INSERT INTO ' . $this->db->prefix() . $this->table_element . '(';
419 $sql .= 'entity,';
420 $sql .= 'fk_product,';
421 $sql .= 'batch,';
422 $sql .= 'eatby,';
423 $sql .= 'sellby,';
424 $sql .= 'eol_date,';
425 $sql .= 'manufacturing_date,';
426 $sql .= 'scrapping_date,';
427 //$sql .= 'commissionning_date,';
428 $sql .= 'qc_frequency,';
429 $sql .= 'lifetime,';
430 $sql .= 'datec,';
431 $sql .= 'fk_user_creat,';
432 $sql .= 'fk_user_modif,';
433 $sql .= 'import_key';
434 $sql .= ') VALUES (';
435 $sql .= ' ' . (!isset($this->entity) ? ((int) $conf->entity) : ((int) $this->entity)) . ',';
436 $sql .= ' ' . (!isset($this->fk_product) ? 'NULL' : ((int) $this->fk_product)) . ',';
437 $sql .= ' ' . (!isset($this->batch) ? 'NULL' : "'" . $this->db->escape($this->batch) . "'") . ',';
438 $sql .= ' ' . (!isset($this->eatby) || dol_strlen($this->eatby) == 0 ? 'NULL' : "'" . $this->db->idate($this->eatby) . "'") . ',';
439 $sql .= ' ' . (!isset($this->sellby) || dol_strlen($this->sellby) == 0 ? 'NULL' : "'" . $this->db->idate($this->sellby) . "'") . ',';
440 $sql .= ' ' . (!isset($this->eol_date) || dol_strlen($this->eol_date) == 0 ? 'NULL' : "'" . $this->db->idate($this->eol_date) . "'") . ',';
441 $sql .= ' ' . (!isset($this->manufacturing_date) || dol_strlen($this->manufacturing_date) == 0 ? 'NULL' : "'" . $this->db->idate($this->manufacturing_date) . "'") . ',';
442 $sql .= ' ' . (!isset($this->scrapping_date) || dol_strlen($this->scrapping_date) == 0 ? 'NULL' : "'" . $this->db->idate($this->scrapping_date) . "'") . ',';
443 //$sql .= ' '.(!isset($this->commissionning_date) || dol_strlen($this->commissionning_date) == 0 ? 'NULL' : "'".$this->db->idate($this->commissionning_date)."'").',';
444 $sql .= ' '.(empty($this->qc_frequency) ? 'NULL' : ((int) $this->qc_frequency)).',';
445 $sql .= ' '.(empty($this->lifetime) ? 'NULL' : ((int) $this->lifetime)).',';
446 $sql .= ' ' . "'" . $this->db->idate(dol_now()) . "'" . ',';
447 $sql .= ' ' . (!isset($this->fk_user_creat) ? 'NULL' : ((int) $this->fk_user_creat)) . ',';
448 $sql .= ' ' . (!isset($this->fk_user_modif) ? 'NULL' : ((int) $this->fk_user_modif)) . ',';
449 $sql .= ' ' . (!isset($this->import_key) ? 'NULL' : "'".$this->db->escape($this->import_key)."'");
450 $sql .= ')';
451
452 $this->db->begin();
453
454 $resql = $this->db->query($sql);
455 if (!$resql) {
456 $error++;
457 $this->errors[] = 'Error ' . $this->db->lasterror();
458 }
459
460 if (!$error) {
461 $this->id = $this->db->last_insert_id($this->db->prefix() . $this->table_element);
462
463 // Actions on extra fields
464 if (!$error) {
465 $result = $this->insertExtraFields();
466 if ($result < 0) {
467 $error++;
468 }
469 }
470
471 if (!$error && !$notrigger) {
472 // Call triggers
473 $result = $this->call_trigger('PRODUCTLOT_CREATE', $user);
474 if ($result < 0) {
475 $error++;
476 }
477 // End call triggers
478 }
479 }
480
481 // Commit or rollback
482 if ($error) {
483 $this->db->rollback();
484 } else {
485 $this->db->commit();
486 }
487 }
488
489 if ($error) {
490 dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR);
491 return -1 * $error;
492 } else {
493 return $this->id;
494 }
495 }
496
506 public function fetch($id = 0, $product_id = 0, $batch = '')
507 {
508 global $conf;
509 dol_syslog(__METHOD__, LOG_DEBUG);
510
511 $sql = "SELECT";
512 $sql .= " t.rowid,";
513 $sql .= " t.entity,";
514 $sql .= " t.fk_product,";
515 $sql .= " t.batch,";
516 $sql .= " t.eatby,";
517 $sql .= " t.sellby,";
518 $sql .= " t.eol_date,";
519 $sql .= " t.manufacturing_date,";
520 $sql .= " t.scrapping_date,";
521 //$sql .= " t.commissionning_date,";
522 $sql .= " t.qc_frequency,";
523 $sql .= " t.lifetime,";
524 $sql .= " t.model_pdf,";
525 $sql .= " t.last_main_doc,";
526 $sql .= " t.datec,";
527 $sql .= " t.tms,";
528 $sql .= " t.fk_user_creat,";
529 $sql .= " t.fk_user_modif,";
530 $sql .= " t.import_key,";
531 $sql .= " t.note_public,";
532 $sql .= " t.note_private";
533 $sql .= " FROM ".$this->db->prefix().$this->table_element." as t";
534 if ($product_id > 0 && $batch != '') {
535 $sql .= " WHERE t.batch = '".$this->db->escape($batch)."' AND t.fk_product = ".((int) $product_id);
536 } else {
537 $sql .= " WHERE t.rowid = ".((int) $id);
538 }
539
540 $resql = $this->db->query($sql);
541 if ($resql) {
542 $numrows = $this->db->num_rows($resql);
543 if ($numrows) {
544 $obj = $this->db->fetch_object($resql);
545
546 $this->id = $obj->rowid;
547 $this->ref = $obj->rowid;
548 //$this->ref = $obj->fk_product.'_'.$obj->batch;
549
550 $this->batch = $obj->batch;
551 $this->entity = (!empty($obj->entity) ? $obj->entity : $conf->entity); // Prevent "null" entity
552 $this->fk_product = $obj->fk_product;
553 $this->eatby = $this->db->jdate($obj->eatby);
554 $this->sellby = $this->db->jdate($obj->sellby);
555 $this->eol_date = $this->db->jdate($obj->eol_date);
556 $this->manufacturing_date = $this->db->jdate($obj->manufacturing_date);
557 $this->scrapping_date = $this->db->jdate($obj->scrapping_date);
558 //$this->commissionning_date = $this->db->jdate($obj->commissionning_date);
559 $this->qc_frequency = $obj->qc_frequency;
560 $this->lifetime = $obj->lifetime;
561 $this->model_pdf = $obj->model_pdf;
562 $this->last_main_doc = $obj->last_main_doc;
563
564 $this->datec = $this->db->jdate($obj->datec);
565 $this->tms = $this->db->jdate($obj->tms);
566 $this->fk_user_creat = $obj->fk_user_creat;
567 $this->fk_user_modif = $obj->fk_user_modif;
568 $this->import_key = $obj->import_key;
569 $this->note_public = $obj->note_public;
570 $this->note_private = $obj->note_private;
571
572 // Retrieve all extrafield
573 // fetch optionals attributes and labels
574 $this->fetch_optionals();
575 }
576 $this->db->free($resql);
577
578 if ($numrows) {
579 return 1;
580 } else {
581 return 0;
582 }
583 } else {
584 $this->errors[] = 'Error '.$this->db->lasterror();
585 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
586
587 return -1;
588 }
589 }
590
598 public function update(User $user, $notrigger = 0)
599 {
600 $error = 0;
601
602 dol_syslog(__METHOD__, LOG_DEBUG);
603
604 // Clean parameters
605
606 if (isset($this->entity)) {
607 $this->entity = (int) $this->entity;
608 }
609 if (isset($this->fk_product)) {
610 $this->fk_product = (int) $this->fk_product;
611 }
612 if (isset($this->batch)) {
613 $this->batch = trim($this->batch);
614 }
615 if (isset($this->fk_user_creat)) {
616 $this->fk_user_creat = (int) $this->fk_user_creat;
617 }
618 if (isset($this->fk_user_modif)) {
619 $this->fk_user_modif = (int) $this->fk_user_modif;
620 }
621 if (isset($this->import_key)) {
622 $this->import_key = trim($this->import_key);
623 }
624
625 // Check parameters
626 // Put here code to add a control on parameters values
627 $res = $this->checkSellOrEatByMandatory();
628 if ($res < 0) {
629 $error++;
630 }
631
632 // $this->oldcopy should have been set by the caller of update (here properties were already modified)
633 if (empty($this->oldcopy)) {
634 $this->oldcopy = dol_clone($this, 2);
635 }
636
637 if (!$error) {
638 // Update request
639 $sql = 'UPDATE ' . $this->db->prefix() . $this->table_element . ' SET';
640 $sql .= ' entity = ' . (isset($this->entity) ? ((int) $this->entity) : "null") . ',';
641 $sql .= ' fk_product = ' . (isset($this->fk_product) ? ((int) $this->fk_product) : "null") . ',';
642 $sql .= ' batch = ' . (isset($this->batch) ? "'" . $this->db->escape($this->batch) . "'" : "null") . ',';
643 $sql .= ' eatby = ' . (!isset($this->eatby) || dol_strlen($this->eatby) != 0 ? "'" . $this->db->idate($this->eatby) . "'" : 'null') . ',';
644 $sql .= ' sellby = ' . (!isset($this->sellby) || dol_strlen($this->sellby) != 0 ? "'" . $this->db->idate($this->sellby) . "'" : 'null') . ',';
645 $sql .= ' eol_date = ' . (!isset($this->eol_date) || dol_strlen($this->eol_date) != 0 ? "'" . $this->db->idate($this->eol_date) . "'" : 'null') . ',';
646 $sql .= ' manufacturing_date = ' . (!isset($this->manufacturing_date) || dol_strlen($this->manufacturing_date) != 0 ? "'" . $this->db->idate($this->manufacturing_date) . "'" : 'null') . ',';
647 $sql .= ' scrapping_date = ' . (!isset($this->scrapping_date) || dol_strlen($this->scrapping_date) != 0 ? "'" . $this->db->idate($this->scrapping_date) . "'" : 'null') . ',';
648 //$sql .= ' commissionning_date = '.(!isset($this->first_use_date) || dol_strlen($this->first_use_date) != 0 ? "'".$this->db->idate($this->first_use_date)."'" : 'null').',';
649 $sql .= ' qc_frequency = '.(!empty($this->qc_frequency) ? (int) $this->qc_frequency : 'null').',';
650 $sql .= ' lifetime = '.(!empty($this->lifetime) ? (int) $this->lifetime : 'null').',';
651 $sql .= ' datec = ' . (dol_strlen((string) $this->datec) != 0 ? "'" . $this->db->idate($this->datec) . "'" : 'null') . ',';
652 $sql .= ' tms = ' . (dol_strlen((string) $this->tms) != 0 ? "'" . $this->db->idate($this->tms) . "'" : "'" . $this->db->idate(dol_now()) . "'") . ',';
653 $sql .= ' fk_user_creat = ' . (isset($this->fk_user_creat) ? ((int) $this->fk_user_creat) : "null") . ',';
654 $sql .= ' fk_user_modif = ' . (isset($this->fk_user_modif) ? ((int) $this->fk_user_modif) : "null") . ',';
655 $sql .= ' import_key = ' . (isset($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null");
656 $sql .= ' WHERE rowid=' . ((int) $this->id);
657
658 $this->db->begin();
659
660 $resql = $this->db->query($sql);
661 if (!$resql) {
662 $error++;
663 $this->errors[] = 'Error ' . $this->db->lasterror();
664 }
665
666 // Actions on extra fields
667 if (!$error) {
668 $result = $this->insertExtraFields();
669 if ($result < 0) {
670 $error++;
671 }
672 }
673
674 if (!$error && !$notrigger) {
675 // Call triggers
676 $result = $this->call_trigger('PRODUCTLOT_MODIFY', $user);
677 if ($result < 0) {
678 $error++;
679 }
680 // End call triggers
681 }
682
683 // Commit or rollback
684 if ($error) {
685 $this->db->rollback();
686 } else {
687 $this->db->commit();
688 }
689 }
690
691 if ($error) {
692 dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR);
693 return -1 * $error;
694 } else {
695 return 1;
696 }
697 }
698
706 public function delete(User $user, $notrigger = 0)
707 {
708 global $conf;
709
710 dol_syslog(__METHOD__, LOG_DEBUG);
711
712 $error = 0;
713
714 $this->db->begin();
715
716 // Check there is no stock for this lot
717 $sql = "SELECT pb.rowid FROM ".$this->db->prefix()."product_batch as pb, ".$this->db->prefix()."product_stock as ps";
718 $sql .= " WHERE pb.fk_product_stock = ps.rowid AND pb.batch = '".$this->db->escape($this->batch)."'";
719 $sql .= " AND ps.fk_product = ".((int) $this->fk_product);
720 $sql .= $this->db->plimit(1);
721
722 $resql = $this->db->query($sql);
723 if ($resql) {
724 $obj = $this->db->fetch_object($resql);
725 if ($obj) {
726 $error++;
727 $this->errors[] = 'Error Lot is used in stock (ID = '.$obj->rowid.'). Deletion not possible.';
728 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
729 }
730 } else {
731 $error++;
732 $this->errors[] = 'Error '.$this->db->lasterror();
733 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
734 }
735
736 // Check there is no movement for this lot
737 $sql = "SELECT sm.rowid FROM ".$this->db->prefix()."stock_mouvement as sm";
738 $sql .= " WHERE sm.batch = '".$this->db->escape($this->batch)."'";
739 $sql .= " AND sm.fk_product = ".((int) $this->fk_product);
740 $sql .= $this->db->plimit(1);
741
742 $resql = $this->db->query($sql);
743 if ($resql) {
744 $obj = $this->db->fetch_object($resql);
745 if ($obj) {
746 $error++;
747 $this->errors[] = 'Error Lot was used in a stock movement (ID '.$obj->rowid.'). Deletion not possible.';
748 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
749 }
750 } else {
751 $error++;
752 $this->errors[] = 'Error '.$this->db->lasterror();
753 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
754 }
755
756 if (!$error && !$notrigger) {
757 // Call triggers
758 $result = $this->call_trigger('PRODUCTLOT_DELETE', $user);
759 if ($result < 0) {
760 $error++;
761 }
762 // End call triggers
763 }
764
765 if (!$error) {
766 $result = $this->deleteExtraFields();
767 if ($result < 0) {
768 $error++;
769 }
770 }
771
772 if (!$error) {
773 $sql = 'DELETE FROM '.$this->db->prefix().$this->table_element;
774 $sql .= ' WHERE rowid='.((int) $this->id);
775
776 $resql = $this->db->query($sql);
777 if (!$resql) {
778 $error++;
779 $this->errors[] = 'Error '.$this->db->lasterror();
780 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
781 }
782 }
783
784 // Commit or rollback
785 if ($error) {
786 $this->db->rollback();
787
788 return -1 * $error;
789 } else {
790 $this->db->commit();
791
792 // Delete the directory of the documents of the lot
793 if (!empty($conf->productbatch->multidir_output[$this->entity])) {
794 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
795 $dir = $conf->productbatch->multidir_output[$this->entity].'/'.get_exdir(0, 0, 0, 1, $this, 'product_batch');
796 if (file_exists($dir)) {
797 if (!dol_delete_dir_recursive($dir)) {
798 $this->error = 'ErrorFailToDeleteDir';
799 $this->errors[] = $this->error;
800 return -2;
801 }
802 }
803 }
804
805 return 1;
806 }
807 }
808
816 public function createFromClone(User $user, $fromid)
817 {
818 dol_syslog(__METHOD__, LOG_DEBUG);
819
820 $error = 0;
821 $object = new Productlot($this->db);
822
823 $this->db->begin();
824
825 // Load source object
826 $object->fetch($fromid);
827 // Reset object
828 $object->id = 0;
829
830 // Clear fields
831 // ...
832
833 // Create clone
834 $object->context['createfromclone'] = 'createfromclone';
835 $result = $object->create($user);
836
837 // Other options
838 if ($result < 0) {
839 $error++;
840 $this->errors = $object->errors;
841 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
842 }
843
844 unset($object->context['createfromclone']);
845
846 // End
847 if (!$error) {
848 $this->db->commit();
849
850 return $object->id;
851 } else {
852 $this->db->rollback();
853
854 return -1;
855 }
856 }
857
864 public function loadStatsExpedition($socid = 0)
865 {
866 global $user, $hookmanager, $action;
867
868 $sql = "SELECT COUNT(DISTINCT exp.fk_soc) as nb_customers, COUNT(DISTINCT exp.rowid) as nb,";
869 $sql .= " COUNT(ed.rowid) as nb_rows, SUM(edb.qty) as qty";
870 $sql .= " FROM ".$this->db->prefix()."expeditiondet_batch as edb";
871 $sql .= " INNER JOIN ".$this->db->prefix()."expeditiondet as ed ON (ed.rowid = edb.fk_expeditiondet)";
872 $sql .= " INNER JOIN ".$this->db->prefix()."expedition as exp ON (exp.rowid = ed.fk_expedition)";
873 // $sql .= ", ".$this->db->prefix()."societe as s";
874 if (!$user->hasRight('societe', 'client', 'voir')) {
875 $sql .= ", ".$this->db->prefix()."societe_commerciaux as sc";
876 }
877 $sql .= " WHERE exp.entity IN (".getEntity('expedition').")";
878 $sql .= " AND edb.batch = '".($this->db->escape($this->batch))."'";
879 if (!$user->hasRight('societe', 'client', 'voir')) {
880 $sql .= " AND exp.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
881 }
882 //$sql.= " AND exp.fk_statut != 0";
883 if ($socid > 0) {
884 $sql .= " AND exp.fk_soc = ".((int) $socid);
885 }
886
887 $result = $this->db->query($sql);
888 if ($result) {
889 $obj = $this->db->fetch_object($result);
890 $this->stats_expedition['customers'] = $obj->nb_customers;
891 $this->stats_expedition['nb'] = $obj->nb;
892 $this->stats_expedition['rows'] = $obj->nb_rows;
893 $this->stats_expedition['qty'] = $obj->qty ? $obj->qty : 0;
894
895
896 // Virtual products can't be used with kits (see langs with key ErrorNoteAlsoThatSubProductCantBeFollowedByLot)
897
898 // if it's a virtual product, maybe it is in invoice by extension
899 // if (getDolGlobalString('PRODUCT_STATS_WITH_PARENT_PROD_IF_INCDEC')) {
900 // $TFather = $this->getFather();
901 // if (is_array($TFather) && !empty($TFather)) {
902 // foreach ($TFather as &$fatherData) {
903 // $pFather = new Product($this->db);
904 // $pFather->id = $fatherData['id'];
905 // $qtyCoef = $fatherData['qty'];
906 //
907 // if ($fatherData['incdec']) {
908 // $pFather->loadStatsExpedition($socid);
909 //
910 // $this->stats_expedition['customers'] += $pFather->stats_expedition['customers'];
911 // $this->stats_expedition['nb'] += $pFather->stats_expedition['nb'];
912 // $this->stats_expedition['rows'] += $pFather->stats_expedition['rows'];
913 // $this->stats_expedition['qty'] += $pFather->stats_expedition['qty'] * $qtyCoef;
914 // }
915 // }
916 // }
917 // }
918
919 $parameters = array('socid' => $socid);
920 $reshook = $hookmanager->executeHooks('loadStatsLotExpedition', $parameters, $this, $action);
921 if ($reshook > 0) {
922 $this->stats_expedition = $hookmanager->resArray['stats_expedition'];
923 }
924
925 return 1;
926 } else {
927 $this->error = $this->db->error();
928 return -1;
929 }
930 }
931
938 public function loadStatsSupplierOrder($socid = 0)
939 {
940 global $user, $hookmanager, $action;
941
942 $sql = "SELECT COUNT(DISTINCT cf.fk_soc) as nb_customers, COUNT(DISTINCT cf.rowid) as nb,";
943 $sql .= " COUNT(cfd.rowid) as nb_rows, SUM(cfdi.qty) as qty";
944 $sql .= " FROM ".$this->db->prefix()."receptiondet_batch as cfdi";
945 $sql .= " INNER JOIN ".$this->db->prefix()."commande_fournisseurdet as cfd ON (cfd.rowid = cfdi.fk_elementdet)";
946 $sql .= " INNER JOIN ".$this->db->prefix()."commande_fournisseur as cf ON (cf.rowid = cfd.fk_commande)";
947 // $sql .= ", ".$this->db->prefix()."societe as s";
948 if (!$user->hasRight('societe', 'client', 'voir')) {
949 $sql .= ", ".$this->db->prefix()."societe_commerciaux as sc";
950 }
951 $sql .= " WHERE cf.entity IN (".getEntity('expedition').")";
952 $sql .= " AND cfdi.batch = '".($this->db->escape($this->batch))."'";
953 $sql .= " AND cfdi.fk_product = " . (int) $this->fk_product;
954 if (!$user->hasRight('societe', 'client', 'voir')) {
955 $sql .= " AND cf.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
956 }
957 //$sql.= " AND cf.fk_statut != 0";
958 if ($socid > 0) {
959 $sql .= " AND cf.fk_soc = ".((int) $socid);
960 }
961
962 $result = $this->db->query($sql);
963 if ($result) {
964 $obj = $this->db->fetch_object($result);
965 $this->stats_supplier_order['customers'] = $obj->nb_customers;
966 $this->stats_supplier_order['nb'] = $obj->nb;
967 $this->stats_supplier_order['rows'] = $obj->nb_rows;
968 $this->stats_supplier_order['qty'] = $obj->qty ? $obj->qty : 0;
969
970
971 // Virtual products can't be used with kits (see langs with key ErrorNoteAlsoThatSubProductCantBeFollowedByLot)
972
973 // if it's a virtual product, maybe it is in invoice by extension
974 // if (getDolGlobalString('PRODUCT_STATS_WITH_PARENT_PROD_IF_INCDEC')) {
975 // $TFather = $this->getFather();
976 // if (is_array($TFather) && !empty($TFather)) {
977 // foreach ($TFather as &$fatherData) {
978 // $pFather = new Product($this->db);
979 // $pFather->id = $fatherData['id'];
980 // $qtyCoef = $fatherData['qty'];
981 //
982 // if ($fatherData['incdec']) {
983 // $pFather->stats_supplier_order($socid);
984 //
985 // $this->stats_supplier_order['customers'] += $pFather->stats_supplier_order['customers'];
986 // $this->stats_supplier_order['nb'] += $pFather->stats_supplier_order['nb'];
987 // $this->stats_supplier_order['rows'] += $pFather->stats_supplier_order['rows'];
988 // $this->stats_supplier_order['qty'] += $pFather->stats_supplier_order['qty'] * $qtyCoef;
989 // }
990 // }
991 // }
992 // }
993
994 $parameters = array('socid' => $socid);
995 $reshook = $hookmanager->executeHooks('loadStatsLotSupplierOrder', $parameters, $this, $action);
996 if ($reshook > 0) {
997 $this->stats_supplier_order = $hookmanager->resArray['stats_supplier_order'];
998 }
999
1000 return 1;
1001 } else {
1002 $this->error = $this->db->error();
1003 return -1;
1004 }
1005 }
1006
1013 public function loadStatsReception($socid = 0)
1014 {
1015 global $user, $hookmanager, $action;
1016
1017 $sql = "SELECT COUNT(DISTINCT recep.fk_soc) as nb_customers, COUNT(DISTINCT recep.rowid) as nb,";
1018 $sql .= " COUNT(cfdi.rowid) as nb_rows, SUM(cfdi.qty) as qty";
1019 $sql .= " FROM ".$this->db->prefix()."receptiondet_batch as cfdi";
1020 $sql .= " INNER JOIN ".$this->db->prefix()."reception as recep ON (recep.rowid = cfdi.fk_reception)";
1021 // $sql .= ", ".$this->db->prefix()."societe as s";
1022 if (!$user->hasRight('societe', 'client', 'voir')) {
1023 $sql .= ", ".$this->db->prefix()."societe_commerciaux as sc";
1024 }
1025 $sql .= " WHERE recep.entity IN (".getEntity('reception').")";
1026 $sql .= " AND cfdi.batch = '".($this->db->escape($this->batch))."'";
1027 if (!$user->hasRight('societe', 'client', 'voir')) {
1028 $sql .= " AND recep.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
1029 }
1030 //$sql.= " AND exp.fk_statut != 0";
1031 if ($socid > 0) {
1032 $sql .= " AND recep.fk_soc = ".((int) $socid);
1033 }
1034
1035 $result = $this->db->query($sql);
1036 if ($result) {
1037 $obj = $this->db->fetch_object($result);
1038 $this->stats_reception['customers'] = $obj->nb_customers;
1039 $this->stats_reception['nb'] = $obj->nb;
1040 $this->stats_reception['rows'] = $obj->nb_rows;
1041 $this->stats_reception['qty'] = $obj->qty ? $obj->qty : 0;
1042
1043
1044 // Virtual products can't be used with kits (see langs with key ErrorNoteAlsoThatSubProductCantBeFollowedByLot)
1045
1046 // if it's a virtual product, maybe it is in invoice by extension
1047 // if (getDolGlobalString('PRODUCT_STATS_WITH_PARENT_PROD_IF_INCDEC')) {
1048 // $TFather = $this->getFather();
1049 // if (is_array($TFather) && !empty($TFather)) {
1050 // foreach ($TFather as &$fatherData) {
1051 // $pFather = new Product($this->db);
1052 // $pFather->id = $fatherData['id'];
1053 // $qtyCoef = $fatherData['qty'];
1054 //
1055 // if ($fatherData['incdec']) {
1056 // $pFather->loadStatsReception($socid);
1057 //
1058 // $this->stats_expedition['customers'] += $pFather->stats_expedition['customers'];
1059 // $this->stats_expedition['nb'] += $pFather->stats_expedition['nb'];
1060 // $this->stats_expedition['rows'] += $pFather->stats_expedition['rows'];
1061 // $this->stats_expedition['qty'] += $pFather->stats_expedition['qty'] * $qtyCoef;
1062 // }
1063 // }
1064 // }
1065 // }
1066
1067 $parameters = array('socid' => $socid);
1068 $reshook = $hookmanager->executeHooks('loadStatsLotReception', $parameters, $this, $action);
1069 if ($reshook > 0) {
1070 $this->stats_expedition = $hookmanager->resArray['stats_expedition'];
1071 }
1072
1073 return 1;
1074 } else {
1075 $this->error = $this->db->error();
1076 return -1;
1077 }
1078 }
1079
1086 public function loadStatsMo($socid = 0)
1087 {
1088 global $user, $hookmanager, $action;
1089
1090 $error = 0;
1091
1092 foreach (array('toconsume', 'consumed', 'toproduce', 'produced') as $role) {
1093 $this->stats_mo['customers_'.$role] = 0;
1094 $this->stats_mo['nb_'.$role] = 0;
1095 $this->stats_mo['qty_'.$role] = 0;
1096
1097 $sql = "SELECT COUNT(DISTINCT c.fk_soc) as nb_customers, COUNT(DISTINCT c.rowid) as nb,";
1098 $sql .= " SUM(mp.qty) as qty";
1099 $sql .= " FROM ".$this->db->prefix()."mrp_mo as c";
1100 $sql .= " INNER JOIN ".$this->db->prefix()."mrp_production as mp ON mp.fk_mo = c.rowid";
1101 if (!$user->hasRight('societe', 'client', 'voir') && !$socid) {
1102 $sql .= " INNER JOIN ".$this->db->prefix()."societe_commerciaux as sc ON sc.fk_soc = c.fk_soc AND sc.fk_user = ".((int) $user->id);
1103 }
1104 $sql .= " WHERE ";
1105 $sql .= " c.entity IN (".getEntity('mo').")";
1106
1107 $sql .= " AND mp.batch = '".($this->db->escape($this->batch))."'";
1108 $sql .= " AND mp.role ='".$this->db->escape($role)."'";
1109 if ($socid > 0) {
1110 $sql .= " AND c.fk_soc = ".((int) $socid);
1111 }
1112
1113 $result = $this->db->query($sql);
1114 if ($result) {
1115 $obj = $this->db->fetch_object($result);
1116 $this->stats_mo['customers_'.$role] = $obj->nb_customers ? $obj->nb_customers : 0;
1117 $this->stats_mo['nb_'.$role] = $obj->nb ? $obj->nb : 0;
1118 $this->stats_mo['qty_'.$role] = $obj->qty ? price2num($obj->qty, 'MS') : 0; // qty may be a float due to the SUM()
1119 } else {
1120 $this->error = $this->db->error();
1121 $error++;
1122 }
1123 }
1124
1125 if (!empty($error)) {
1126 return -1;
1127 }
1128
1129 $parameters = array('socid' => $socid);
1130 $reshook = $hookmanager->executeHooks('loadStatsCustomerMO', $parameters, $this, $action);
1131 if ($reshook > 0) {
1132 $this->stats_mo = $hookmanager->resArray['stats_mo'];
1133 }
1134
1135 return 1;
1136 }
1137
1138
1145 public function getLibStatut($mode = 0)
1146 {
1147 return $this->LibStatut(0, $mode);
1148 }
1149
1150 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1158 public function LibStatut($status, $mode = 0)
1159 {
1160 // phpcs:enable
1161 //global $langs;
1162
1163 //$langs->load('stocks');
1164
1165 return '';
1166 }
1167
1168
1175 public function getTooltipContentArray($params)
1176 {
1177 global $langs;
1178
1179 $langs->loadLangs(['stocks', 'productbatch']);
1180
1181 //$option = $params['option'] ?? '';
1182
1183 $datas = [];
1184 $datas['picto'] = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Batch").'</u>';
1185 //$datas['divopen'] = '<div width="100%">';
1186 $datas['batch'] = '<br><b>'.$langs->trans('Batch').':</b> '.$this->batch;
1187 if (isDolTms($this->eatby) && !getDolGlobalString('PRODUCT_DISABLE_EATBY')) {
1188 $datas['eatby'] = '<br><b>'.$langs->trans('EatByDate').':</b> '.dol_print_date($this->eatby, 'day');
1189 }
1190 if (isDolTms($this->sellby) && !getDolGlobalString('PRODUCT_DISABLE_SELLBY')) {
1191 $datas['sellby'] = '<br><b>'.$langs->trans('SellByDate').':</b> '.dol_print_date($this->sellby, 'day');
1192 }
1193 //$datas['divclose'] = '</div>';
1194
1195 return $datas;
1196 }
1197
1210 public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $maxlen = 24, $morecss = '', $save_lastsearch_value = -1)
1211 {
1212 global $langs, $hookmanager;
1213
1214 $result = '';
1215 $params = [
1216 'id' => $this->id,
1217 'objecttype' => $this->element,
1218 'option' => $option,
1219 ];
1220 $classfortooltip = 'classfortooltip';
1221 $dataparams = '';
1222 if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
1223 $classfortooltip = 'classforajaxtooltip';
1224 $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
1225 $label = '';
1226 } else {
1227 $label = implode($this->getTooltipContentArray($params));
1228 }
1229
1230 $url = DOL_URL_ROOT.'/product/stock/productlot_card.php?id='.$this->id;
1231
1232 if ($option != 'nolink') {
1233 // Add param to save lastsearch_values or not
1234 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1235 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1236 $add_save_lastsearch_values = 1;
1237 }
1238 if ($add_save_lastsearch_values) {
1239 $url .= '&save_lastsearch_values=1';
1240 }
1241 }
1242
1243 $linkclose = '';
1244 if (empty($notooltip)) {
1245 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
1246 $label = $langs->trans("ShowMyObject");
1247 $linkclose .= ' alt="'.dolPrintHTMLForAttribute($label).'"';
1248 }
1249 $linkclose .= ($label ? ' title="'.dolPrintHTMLForAttribute($label).'"' : ' title="tocomplete"');
1250 $linkclose .= $dataparams.' class="'.$classfortooltip.($morecss ? ' '.$morecss : '').'"';
1251 } else {
1252 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
1253 }
1254
1255 if ($option == 'nolink') {
1256 $linkstart = '<span';
1257 } else {
1258 $linkstart = '<a href="'.$url.'"';
1259 }
1260 $linkstart .= $linkclose.'>';
1261 if ($option == 'nolink') {
1262 $linkend = '</span>';
1263 } else {
1264 $linkend = '</a>';
1265 }
1266
1267 $result .= $linkstart;
1268 if ($withpicto) {
1269 $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'"'), 0, 0, $notooltip ? 0 : 1);
1270 }
1271 if ($withpicto != 2) {
1272 $result .= $this->batch;
1273 }
1274 $result .= $linkend;
1275
1276 global $action;
1277 $hookmanager->initHooks(array('productlotdao'));
1278 $parameters = array('id' => $this->id, 'getnomurl' => $result);
1279 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1280 if ($reshook > 0) {
1281 $result = $hookmanager->resPrint;
1282 } else {
1283 $result .= $hookmanager->resPrint;
1284 }
1285
1286 return $result;
1287 }
1288
1289
1296 public function initAsSpecimen()
1297 {
1298 global $conf;
1299
1300 $now = dol_now();
1301
1302 // Initialise parameters
1303 $this->id = 0;
1304 $this->ref = 'SPECIMEN';
1305 $this->specimen = 1;
1306
1307 $this->entity = $conf->entity;
1308 $this->fk_product = 0;
1309 $this->batch = 'ABCD123456';
1310 $this->eatby = $now - 100000;
1311 $this->sellby = $now - 100000;
1312 $this->datec = $now - 3600;
1313 $this->tms = $now;
1314 $this->fk_user_creat = 0;
1315 $this->fk_user_modif = 0;
1316 $this->import_key = '123456';
1317
1318 return 1;
1319 }
1320
1331 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
1332 {
1333 global $langs;
1334
1335 $langs->loadLangs(array('stocks', 'productbatch', "products"));
1336 $outputlangs->loadLangs(array('stocks', 'productbatch', "products"));
1337
1338 // Set the model to the name of the model to use
1339 if (!dol_strlen($modele)) {
1340 $modele = '';
1341
1342 if (!empty($this->model_pdf)) {
1343 $modele = $this->model_pdf;
1344 } elseif (getDolGlobalString('PRODUCT_BATCH_ADDON_PDF')) {
1345 $modele = getDolGlobalString('PRODUCT_BATCH_ADDON_PDF');
1346 }
1347 }
1348
1349 $modelpath = "core/modules/product_batch/doc/";
1350
1351 return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref);
1352 }
1353
1362 public function validateField($fields, $fieldKey, $fieldValue)
1363 {
1364 // Add your own validation rules here.
1365 if ($fieldKey == 'batch') {
1366 if (preg_match('/\s/', $fieldValue)) {
1367 $this->error = 'ErrorABatchShouldNotContainsSpaces';
1368 return false;
1369 }
1370 }
1371
1372 return parent::validateField($fields, $fieldKey, $fieldValue);
1373 }
1374}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
$object ref
Definition info.php:90
Parent class of all other business classes (invoices, contracts, proposals, orders,...
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...
commonGenerateDocument($modelspath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams=null)
Common function for all objects extending CommonObject for generating documents.
deleteExtraFields()
Delete all extra fields values for the current object.
fetch_product()
Load the product with id $this->fk_product into this->product.
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
Class to manage Dolibarr database access.
Class to manage products or services.
Class with list of lots and properties.
update(User $user, $notrigger=0)
Update object into database.
loadStatsReception($socid=0)
Load the array of shipment stats for the lot/serial number.
getLibStatut($mode=0)
Return label of status of object.
static checkSellOrEatByMandatoryFromProductIdAndDates($productId, $sellBy, $eatBy, $onlyFieldName='')
Check sell or eat by date is mandatory from product id and sell-by and eat-by dates.
getTooltipContentArray($params)
getTooltipContentArray
fetch($id=0, $product_id=0, $batch='')
Load object in memory from the database.
loadStatsExpedition($socid=0)
Load the array of shipment stats for the lot/serial number.
LibStatut($status, $mode=0)
Return label of a given status.
getNomUrl($withpicto=0, $option='', $notooltip=0, $maxlen=24, $morecss='', $save_lastsearch_value=-1)
Return a link to the a lot card (with optionally the picto) Use this->id,this->lastname,...
generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0)
Create a document onto disk according to template module.
loadStatsMo($socid=0)
Load the array of shipment stats for the lot/serial number.
__construct(DoliDB $db)
Constructor.
create(User $user, $notrigger=0)
Create object into database.
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
checkSellOrEatByMandatory($onlyFieldName='')
Check sell or eat by date is mandatory.
static checkSellOrEatByMandatoryFromProductAndDates($product, $sellBy, $eatBy, $onlyFieldName='', $alreadyCheckConf=false)
Check sell or eat by date is mandatory from product and sell-by and eat-by dates.
validateField($fields, $fieldKey, $fieldValue)
Return validation test result for a field.
createFromClone(User $user, $fromid)
Load an object from its id and create a new one in database.
loadStatsSupplierOrder($socid=0)
Load the array of supplier order stats for the lot/serial number.
Class to manage Dolibarr users.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
dol_delete_dir_recursive($dir, $count=0, $nophperrors=0, $onlysub=0, &$countdeleted=0, $indexdatabase=1, $nolog=0, $level=0)
Remove a directory $dir and its subdirectories (or only files and subdirectories)
dol_now($mode='gmt')
Return date for now.
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)
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
isDolTms($timestamp)
isDolTms check if a timestamp is valid.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $allowothertags=array())
Show a picto called object_picto (generic function)
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_clone($srcobject, $native=2)
Create a clone of instance of object (new instance with same value for each properties) With native =...
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
get_exdir($num, $level, $alpha, $withoutslash, $object, $modulepart='')
Return a path to have a the directory according to object where files are stored.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.