dolibarr 23.0.4
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-2025 Frédéric France <frederic.france@free.fr>
7 * Copyright (C) 2023 Gauthier VERDOL <gauthier.verdol@atm-consulting.fr>
8 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.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
30// Put here all includes required by your class file
31require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
32//require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php';
33//require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
34
39{
43 public $element = 'productlot';
44
48 public $table_element = 'product_lot';
49
53 public $picto = 'lot';
54
58 public $stats_propale;
59
63 public $stats_commande;
64
68 public $stats_contrat;
69
73 public $stats_facture;
74
78 public $stats_commande_fournisseur;
79
83 public $stats_expedition;
84
88 public $stats_reception;
89
93 public $stats_supplier_order;
94
98 public $stats_mo;
102 public $stats_bom;
106 public $stats_mrptoconsume;
110 public $stats_mrptoproduce;
114 public $stats_facturerec;
118 public $stats_facture_fournisseur;
119
120
149 public $fields = array(
150 'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'visible' => -2, 'noteditable' => 1, 'notnull' => 1, 'index' => 1, 'position' => 1, 'comment' => 'Id', 'css' => 'left'),
151 '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'),
152 '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),
153 'entity' => array('type' => 'integer', 'label' => 'Entity', 'enabled' => 1, 'visible' => 0, 'default' => '1', 'notnull' => 1, 'index' => 1, 'position' => 20),
154 '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'),
155 'sellby' => array('type' => 'date', 'label' => 'SellByDate', 'enabled' => 'getDolGlobalInt("PRODUCT_DISABLE_SELLBY") ? 0 : 1', 'visible' => 1, 'notnull' => 0, 'position' => 60),
156 'eatby' => array('type' => 'date', 'label' => 'EatByDate', 'enabled' => 'getDolGlobalInt("PRODUCT_DISABLE_EATBY") ? 0 : 1', 'visible' => 1, 'notnull' => 0, 'position' => 62),
157 '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),
158 '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),
159 //'commissionning_date' => array('type'=>'date', 'label'=>'FirstUseDate', 'enabled'=>'getDolGlobalInt("PRODUCT_LOT_ENABLE_TRACEABILITY", 0)', 'visible'=>5, 'position'=>100),
160 '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),
161 '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),
162 'model_pdf' => array('type' => 'varchar(255)', 'label' => 'Model pdf', 'enabled' => 1, 'visible' => 0, 'position' => 215),
163 'last_main_doc' => array('type' => 'varchar(255)', 'label' => 'LastMainDoc', 'enabled' => 1, 'visible' => -2, 'position' => 310),
164 'datec' => array('type' => 'datetime', 'label' => 'DateCreation', 'enabled' => 1, 'visible' => 0, 'notnull' => 1, 'position' => 500),
165 'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'visible' => -2, 'notnull' => 1, 'position' => 501),
166 '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'),
167 'fk_user_modif' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserModif', 'enabled' => 1, 'visible' => -2, 'notnull' => -1, 'position' => 511),
168 'import_key' => array('type' => 'varchar(14)', 'label' => 'ImportId', 'enabled' => 1, 'visible' => -2, 'notnull' => -1, 'index' => 0, 'position' => 1000)
169 );
170
174 public $fk_product;
175
179 public $batch;
180
184 public $eatby = '';
185
189 public $sellby = '';
190
194 public $eol_date = '';
195
199 public $manufacturing_date = '';
200
204 public $scrapping_date = '';
205 //public $commissionning_date = '';
209 public $qc_frequency = '';
213 public $lifetime = '';
217 public $datec = '';
218
222 public $fk_user_creat;
223
227 public $fk_user_modif;
228
232 public $import_key;
233
234
240 public function __construct(DoliDB $db)
241 {
242 $this->db = $db;
243
244 $this->ismultientitymanaged = 1;
245 }
246
253 public function checkSellOrEatByMandatory($onlyFieldName = '')
254 {
255 if (getDolGlobalString('PRODUCT_DISABLE_SELLBY') && getDolGlobalString('PRODUCT_DISABLE_EATBY')) {
256 return 0;
257 }
258
259 $errorMsgArr = array();
260 if ($this->fk_product > 0) {
261 $res = $this->fetch_product();
262 $product = $this->product;
263 if ($res <= 0) {
264 $errorMsgArr[] = $product->errorsToString();
265 }
266
267 if (empty($errorMsgArr)) {
268 $errorMsgArr = self::checkSellOrEatByMandatoryFromProductAndDates($product, $this->sellby, $this->eatby, $onlyFieldName, true);
269 }
270 }
271
272 if (!empty($errorMsgArr)) {
273 $this->errors = array_merge($this->errors, $errorMsgArr);
274 return -1;
275 } else {
276 return 1;
277 }
278 }
279
289 public static function checkSellOrEatByMandatoryFromProductIdAndDates($productId, $sellBy, $eatBy, $onlyFieldName = '')
290 {
291 global $db;
292
293 if (getDolGlobalString('PRODUCT_DISABLE_SELLBY') && getDolGlobalString('PRODUCT_DISABLE_EATBY')) {
294 return null;
295 }
296
297 $errorMsgArr = array();
298 if ($productId > 0) {
299 $product = new Product($db);
300 $res = $product->fetch($productId);
301 if ($res <= 0) {
302 $errorMsgArr[] = $product->errorsToString();
303 }
304
305 if (empty($errorMsgArr)) {
306 $errorMsgArr = self::checkSellOrEatByMandatoryFromProductAndDates($product, $sellBy, $eatBy, $onlyFieldName, true);
307 }
308 }
309
310 return $errorMsgArr;
311 }
312
323 public static function checkSellOrEatByMandatoryFromProductAndDates($product, $sellBy, $eatBy, $onlyFieldName = '', $alreadyCheckConf = false)
324 {
325 global $langs;
326
327 if ($alreadyCheckConf === false && getDolGlobalString('PRODUCT_DISABLE_SELLBY') && getDolGlobalString('PRODUCT_DISABLE_EATBY')) {
328 return null;
329 }
330
331 $errorMsgArr = array();
332 $checkSellByMandatory = false;
333 $checkEatByMandatory = false;
334
335 $sellOrEatByMandatoryId = $product->sell_or_eat_by_mandatory;
336 if (!getDolGlobalString('PRODUCT_DISABLE_SELLBY') && $sellOrEatByMandatoryId == Product::SELL_OR_EAT_BY_MANDATORY_ID_SELL_BY && ($onlyFieldName == '' || $onlyFieldName == 'sellby')) {
337 $checkSellByMandatory = true;
338 } elseif (!getDolGlobalString('PRODUCT_DISABLE_EATBY') && $sellOrEatByMandatoryId == Product::SELL_OR_EAT_BY_MANDATORY_ID_EAT_BY && ($onlyFieldName == '' || $onlyFieldName == 'eatby')) {
339 $checkEatByMandatory = true;
340 } elseif ($sellOrEatByMandatoryId == Product::SELL_OR_EAT_BY_MANDATORY_ID_SELL_AND_EAT) {
341 if (!getDolGlobalString('PRODUCT_DISABLE_SELLBY') && ($onlyFieldName == '' || $onlyFieldName == 'sellby')) {
342 $checkSellByMandatory = true;
343 }
344 if (!getDolGlobalString('PRODUCT_DISABLE_EATBY') && ($onlyFieldName == '' || $onlyFieldName == 'eatby')) {
345 $checkEatByMandatory = true;
346 }
347 }
348
349 if ($checkSellByMandatory) {
350 if (!isset($sellBy) || dol_strlen((string) $sellBy) == 0) {
351 // error : sell by is mandatory
352 $errorMsgArr[] = $langs->trans('ErrorFieldRequired', $langs->transnoentities('SellByDate'));
353 }
354 }
355 if ($checkEatByMandatory) {
356 if (!isset($eatBy) || dol_strlen((string) $eatBy) == 0) {
357 // error : eat by is mandatory
358 $errorMsgArr[] = $langs->trans('ErrorFieldRequired', $langs->transnoentities('EatByDate'));
359 }
360 }
361
362 return $errorMsgArr;
363 }
364
365
373 public function create(User $user, $notrigger = 0)
374 {
375 global $conf, $langs;
376
377 dol_syslog(__METHOD__, LOG_DEBUG);
378
379 $error = 0;
380
381 // Clean parameters
382
383 if (isset($this->entity)) {
384 $this->entity = (int) $this->entity;
385 }
386 if (isset($this->fk_product)) {
387 $this->fk_product = (int) $this->fk_product;
388 }
389 if (isset($this->batch)) {
390 $this->batch = trim($this->batch);
391 }
392 if (isset($this->fk_user_creat)) {
393 $this->fk_user_creat = (int) $this->fk_user_creat;
394 }
395 if (isset($this->fk_user_modif)) {
396 $this->fk_user_modif = (int) $this->fk_user_modif;
397 }
398 if (isset($this->import_key)) {
399 $this->import_key = trim($this->import_key);
400 }
401
402 // Check parameters
403 if ($this->batch === '') {
404 $this->errors[] = $langs->trans("ErrorBadValueForBatch");
405 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
406 return -1;
407 }
408 // Put here code to add control on parameters values
409 $res = $this->checkSellOrEatByMandatory();
410 if ($res < 0) {
411 $error++;
412 }
413
414 if (!$error) {
415 // Insert request
416 $sql = 'INSERT INTO ' . $this->db->prefix() . $this->table_element . '(';
417 $sql .= 'entity,';
418 $sql .= 'fk_product,';
419 $sql .= 'batch,';
420 $sql .= 'eatby,';
421 $sql .= 'sellby,';
422 $sql .= 'eol_date,';
423 $sql .= 'manufacturing_date,';
424 $sql .= 'scrapping_date,';
425 //$sql .= 'commissionning_date,';
426 $sql .= 'qc_frequency,';
427 $sql .= 'lifetime,';
428 $sql .= 'datec,';
429 $sql .= 'fk_user_creat,';
430 $sql .= 'fk_user_modif,';
431 $sql .= 'import_key';
432 $sql .= ') VALUES (';
433 $sql .= ' ' . (!isset($this->entity) ? $conf->entity : $this->entity) . ',';
434 $sql .= ' ' . (!isset($this->fk_product) ? 'NULL' : $this->fk_product) . ',';
435 $sql .= ' ' . (!isset($this->batch) ? 'NULL' : "'" . $this->db->escape($this->batch) . "'") . ',';
436 $sql .= ' ' . (!isset($this->eatby) || dol_strlen($this->eatby) == 0 ? 'NULL' : "'" . $this->db->idate($this->eatby) . "'") . ',';
437 $sql .= ' ' . (!isset($this->sellby) || dol_strlen($this->sellby) == 0 ? 'NULL' : "'" . $this->db->idate($this->sellby) . "'") . ',';
438 $sql .= ' ' . (!isset($this->eol_date) || dol_strlen($this->eol_date) == 0 ? 'NULL' : "'" . $this->db->idate($this->eol_date) . "'") . ',';
439 $sql .= ' ' . (!isset($this->manufacturing_date) || dol_strlen($this->manufacturing_date) == 0 ? 'NULL' : "'" . $this->db->idate($this->manufacturing_date) . "'") . ',';
440 $sql .= ' ' . (!isset($this->scrapping_date) || dol_strlen($this->scrapping_date) == 0 ? 'NULL' : "'" . $this->db->idate($this->scrapping_date) . "'") . ',';
441 //$sql .= ' '.(!isset($this->commissionning_date) || dol_strlen($this->commissionning_date) == 0 ? 'NULL' : "'".$this->db->idate($this->commissionning_date)."'").',';
442 $sql .= ' '.(empty($this->qc_frequency) ? 'NULL' : $this->qc_frequency).',';
443 $sql .= ' '.(empty($this->lifetime) ? 'NULL' : $this->lifetime).',';
444 $sql .= ' ' . "'" . $this->db->idate(dol_now()) . "'" . ',';
445 $sql .= ' ' . (!isset($this->fk_user_creat) ? 'NULL' : $this->fk_user_creat) . ',';
446 $sql .= ' ' . (!isset($this->fk_user_modif) ? 'NULL' : $this->fk_user_modif) . ',';
447 $sql .= ' ' . (!isset($this->import_key) ? 'NULL' : $this->import_key);
448 $sql .= ')';
449
450 $this->db->begin();
451
452 $resql = $this->db->query($sql);
453 if (!$resql) {
454 $error++;
455 $this->errors[] = 'Error ' . $this->db->lasterror();
456 }
457
458 if (!$error) {
459 $this->id = $this->db->last_insert_id($this->db->prefix() . $this->table_element);
460
461 // Actions on extra fields
462 if (!$error) {
463 $result = $this->insertExtraFields();
464 if ($result < 0) {
465 $error++;
466 }
467 }
468
469 if (!$error && !$notrigger) {
470 // Call triggers
471 $result = $this->call_trigger('PRODUCTLOT_CREATE', $user);
472 if ($result < 0) {
473 $error++;
474 }
475 // End call triggers
476 }
477 }
478
479 // Commit or rollback
480 if ($error) {
481 $this->db->rollback();
482 } else {
483 $this->db->commit();
484 }
485 }
486
487 if ($error) {
488 dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR);
489 return -1 * $error;
490 } else {
491 return $this->id;
492 }
493 }
494
504 public function fetch($id = 0, $product_id = 0, $batch = '')
505 {
506 global $conf;
507 dol_syslog(__METHOD__, LOG_DEBUG);
508
509 $sql = "SELECT";
510 $sql .= " t.rowid,";
511 $sql .= " t.entity,";
512 $sql .= " t.fk_product,";
513 $sql .= " t.batch,";
514 $sql .= " t.eatby,";
515 $sql .= " t.sellby,";
516 $sql .= " t.eol_date,";
517 $sql .= " t.manufacturing_date,";
518 $sql .= " t.scrapping_date,";
519 //$sql .= " t.commissionning_date,";
520 $sql .= " t.qc_frequency,";
521 $sql .= " t.lifetime,";
522 $sql .= " t.model_pdf,";
523 $sql .= " t.last_main_doc,";
524 $sql .= " t.datec,";
525 $sql .= " t.tms,";
526 $sql .= " t.fk_user_creat,";
527 $sql .= " t.fk_user_modif,";
528 $sql .= " t.import_key,";
529 $sql .= " t.note_public,";
530 $sql .= " t.note_private";
531 $sql .= " FROM ".$this->db->prefix().$this->table_element." as t";
532 if ($product_id > 0 && $batch != '') {
533 $sql .= " WHERE t.batch = '".$this->db->escape($batch)."' AND t.fk_product = ".((int) $product_id);
534 } else {
535 $sql .= " WHERE t.rowid = ".((int) $id);
536 }
537
538 $resql = $this->db->query($sql);
539 if ($resql) {
540 $numrows = $this->db->num_rows($resql);
541 if ($numrows) {
542 $obj = $this->db->fetch_object($resql);
543
544 $this->id = $obj->rowid;
545 $this->ref = $obj->rowid;
546 //$this->ref = $obj->fk_product.'_'.$obj->batch;
547
548 $this->batch = $obj->batch;
549 $this->entity = (!empty($obj->entity) ? $obj->entity : $conf->entity); // Prevent "null" entity
550 $this->fk_product = $obj->fk_product;
551 $this->eatby = $this->db->jdate($obj->eatby);
552 $this->sellby = $this->db->jdate($obj->sellby);
553 $this->eol_date = $this->db->jdate($obj->eol_date);
554 $this->manufacturing_date = $this->db->jdate($obj->manufacturing_date);
555 $this->scrapping_date = $this->db->jdate($obj->scrapping_date);
556 //$this->commissionning_date = $this->db->jdate($obj->commissionning_date);
557 $this->qc_frequency = $obj->qc_frequency;
558 $this->lifetime = $obj->lifetime;
559 $this->model_pdf = $obj->model_pdf;
560 $this->last_main_doc = $obj->last_main_doc;
561
562 $this->datec = $this->db->jdate($obj->datec);
563 $this->tms = $this->db->jdate($obj->tms);
564 $this->fk_user_creat = $obj->fk_user_creat;
565 $this->fk_user_modif = $obj->fk_user_modif;
566 $this->import_key = $obj->import_key;
567 $this->note_public = $obj->note_public;
568 $this->note_private = $obj->note_private;
569
570 // Retrieve all extrafield
571 // fetch optionals attributes and labels
572 $this->fetch_optionals();
573 }
574 $this->db->free($resql);
575
576 if ($numrows) {
577 return 1;
578 } else {
579 return 0;
580 }
581 } else {
582 $this->errors[] = 'Error '.$this->db->lasterror();
583 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
584
585 return -1;
586 }
587 }
588
596 public function update(User $user, $notrigger = 0)
597 {
598 $error = 0;
599
600 dol_syslog(__METHOD__, LOG_DEBUG);
601
602 // Clean parameters
603
604 if (isset($this->entity)) {
605 $this->entity = (int) $this->entity;
606 }
607 if (isset($this->fk_product)) {
608 $this->fk_product = (int) $this->fk_product;
609 }
610 if (isset($this->batch)) {
611 $this->batch = trim($this->batch);
612 }
613 if (isset($this->fk_user_creat)) {
614 $this->fk_user_creat = (int) $this->fk_user_creat;
615 }
616 if (isset($this->fk_user_modif)) {
617 $this->fk_user_modif = (int) $this->fk_user_modif;
618 }
619 if (isset($this->import_key)) {
620 $this->import_key = trim($this->import_key);
621 }
622
623 // Check parameters
624 // Put here code to add a control on parameters values
625 $res = $this->checkSellOrEatByMandatory();
626 if ($res < 0) {
627 $error++;
628 }
629
630 // $this->oldcopy should have been set by the caller of update (here properties were already modified)
631 if (empty($this->oldcopy)) {
632 $this->oldcopy = dol_clone($this, 2);
633 }
634
635 if (!$error) {
636 // Update request
637 $sql = 'UPDATE ' . $this->db->prefix() . $this->table_element . ' SET';
638 $sql .= ' entity = ' . (isset($this->entity) ? $this->entity : "null") . ',';
639 $sql .= ' fk_product = ' . (isset($this->fk_product) ? $this->fk_product : "null") . ',';
640 $sql .= ' batch = ' . (isset($this->batch) ? "'" . $this->db->escape($this->batch) . "'" : "null") . ',';
641 $sql .= ' eatby = ' . (!isset($this->eatby) || dol_strlen($this->eatby) != 0 ? "'" . $this->db->idate($this->eatby) . "'" : 'null') . ',';
642 $sql .= ' sellby = ' . (!isset($this->sellby) || dol_strlen($this->sellby) != 0 ? "'" . $this->db->idate($this->sellby) . "'" : 'null') . ',';
643 $sql .= ' eol_date = ' . (!isset($this->eol_date) || dol_strlen($this->eol_date) != 0 ? "'" . $this->db->idate($this->eol_date) . "'" : 'null') . ',';
644 $sql .= ' manufacturing_date = ' . (!isset($this->manufacturing_date) || dol_strlen($this->manufacturing_date) != 0 ? "'" . $this->db->idate($this->manufacturing_date) . "'" : 'null') . ',';
645 $sql .= ' scrapping_date = ' . (!isset($this->scrapping_date) || dol_strlen($this->scrapping_date) != 0 ? "'" . $this->db->idate($this->scrapping_date) . "'" : 'null') . ',';
646 //$sql .= ' commissionning_date = '.(!isset($this->first_use_date) || dol_strlen($this->first_use_date) != 0 ? "'".$this->db->idate($this->first_use_date)."'" : 'null').',';
647 $sql .= ' qc_frequency = '.(!empty($this->qc_frequency) ? (int) $this->qc_frequency : 'null').',';
648 $sql .= ' lifetime = '.(!empty($this->lifetime) ? (int) $this->lifetime : 'null').',';
649 $sql .= ' datec = ' . (dol_strlen((string) $this->datec) != 0 ? "'" . $this->db->idate($this->datec) . "'" : 'null') . ',';
650 $sql .= ' tms = ' . (dol_strlen((string) $this->tms) != 0 ? "'" . $this->db->idate($this->tms) . "'" : "'" . $this->db->idate(dol_now()) . "'") . ',';
651 $sql .= ' fk_user_creat = ' . (isset($this->fk_user_creat) ? $this->fk_user_creat : "null") . ',';
652 $sql .= ' fk_user_modif = ' . (isset($this->fk_user_modif) ? $this->fk_user_modif : "null") . ',';
653 $sql .= ' import_key = ' . (isset($this->import_key) ? $this->import_key : "null");
654 $sql .= ' WHERE rowid=' . ((int) $this->id);
655
656 $this->db->begin();
657
658 $resql = $this->db->query($sql);
659 if (!$resql) {
660 $error++;
661 $this->errors[] = 'Error ' . $this->db->lasterror();
662 }
663
664 // Actions on extra fields
665 if (!$error) {
666 $result = $this->insertExtraFields();
667 if ($result < 0) {
668 $error++;
669 }
670 }
671
672 if (!$error && !$notrigger) {
673 // Call triggers
674 $result = $this->call_trigger('PRODUCTLOT_MODIFY', $user);
675 if ($result < 0) {
676 $error++;
677 }
678 // End call triggers
679 }
680
681 // Commit or rollback
682 if ($error) {
683 $this->db->rollback();
684 } else {
685 $this->db->commit();
686 }
687 }
688
689 if ($error) {
690 dol_syslog(__METHOD__ . ' ' . implode(',', $this->errors), LOG_ERR);
691 return -1 * $error;
692 } else {
693 return 1;
694 }
695 }
696
704 public function delete(User $user, $notrigger = 0)
705 {
706 global $conf;
707
708 dol_syslog(__METHOD__, LOG_DEBUG);
709
710 $error = 0;
711
712 $this->db->begin();
713
714 // Check there is no stock for this lot
715 $sql = "SELECT pb.rowid FROM ".$this->db->prefix()."product_batch as pb, ".$this->db->prefix()."product_stock as ps";
716 $sql .= " WHERE pb.fk_product_stock = ps.rowid AND pb.batch = '".$this->db->escape($this->batch)."'";
717 $sql .= " AND ps.fk_product = ".((int) $this->fk_product);
718 $sql .= $this->db->plimit(1);
719
720 $resql = $this->db->query($sql);
721 if ($resql) {
722 $obj = $this->db->fetch_object($resql);
723 if ($obj) {
724 $error++;
725 $this->errors[] = 'Error Lot is used in stock (ID = '.$obj->rowid.'). Deletion not possible.';
726 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
727 }
728 } else {
729 $error++;
730 $this->errors[] = 'Error '.$this->db->lasterror();
731 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
732 }
733
734 // Check there is no movement for this lot
735 $sql = "SELECT sm.rowid FROM ".$this->db->prefix()."stock_mouvement as sm";
736 $sql .= " WHERE sm.batch = '".$this->db->escape($this->batch)."'";
737 $sql .= " AND sm.fk_product = ".((int) $this->fk_product);
738 $sql .= $this->db->plimit(1);
739
740 $resql = $this->db->query($sql);
741 if ($resql) {
742 $obj = $this->db->fetch_object($resql);
743 if ($obj) {
744 $error++;
745 $this->errors[] = 'Error Lot was used in a stock movement (ID '.$obj->rowid.'). Deletion not possible.';
746 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
747 }
748 } else {
749 $error++;
750 $this->errors[] = 'Error '.$this->db->lasterror();
751 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
752 }
753
754 if (!$error && !$notrigger) {
755 // Call triggers
756 $result = $this->call_trigger('PRODUCTLOT_DELETE', $user);
757 if ($result < 0) {
758 $error++;
759 }
760 // End call triggers
761 }
762
763 if (!$error) {
764 $result = $this->deleteExtraFields();
765 if ($result < 0) {
766 $error++;
767 }
768 }
769
770 if (!$error) {
771 $sql = 'DELETE FROM '.$this->db->prefix().$this->table_element;
772 $sql .= ' WHERE rowid='.((int) $this->id);
773
774 $resql = $this->db->query($sql);
775 if (!$resql) {
776 $error++;
777 $this->errors[] = 'Error '.$this->db->lasterror();
778 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
779 }
780 }
781
782 // Commit or rollback
783 if ($error) {
784 $this->db->rollback();
785
786 return -1 * $error;
787 } else {
788 $this->db->commit();
789
790 // Delete the directory of the documents of the lot
791 if (!empty($conf->productbatch->multidir_output[$this->entity])) {
792 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
793 $dir = $conf->productbatch->multidir_output[$this->entity].'/'.get_exdir(0, 0, 0, 1, $this, 'product_batch');
794 if (file_exists($dir)) {
795 if (!dol_delete_dir_recursive($dir)) {
796 $this->error = 'ErrorFailToDeleteDir';
797 $this->errors[] = $this->error;
798 return -2;
799 }
800 }
801 }
802
803 return 1;
804 }
805 }
806
814 public function createFromClone(User $user, $fromid)
815 {
816 dol_syslog(__METHOD__, LOG_DEBUG);
817
818 $error = 0;
819 $object = new Productlot($this->db);
820
821 $this->db->begin();
822
823 // Load source object
824 $object->fetch($fromid);
825 // Reset object
826 $object->id = 0;
827
828 // Clear fields
829 // ...
830
831 // Create clone
832 $object->context['createfromclone'] = 'createfromclone';
833 $result = $object->create($user);
834
835 // Other options
836 if ($result < 0) {
837 $error++;
838 $this->errors = $object->errors;
839 dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
840 }
841
842 unset($object->context['createfromclone']);
843
844 // End
845 if (!$error) {
846 $this->db->commit();
847
848 return $object->id;
849 } else {
850 $this->db->rollback();
851
852 return -1;
853 }
854 }
855
862 public function loadStatsExpedition($socid = 0)
863 {
864 global $user, $hookmanager, $action;
865
866 $sql = "SELECT COUNT(DISTINCT exp.fk_soc) as nb_customers, COUNT(DISTINCT exp.rowid) as nb,";
867 $sql .= " COUNT(ed.rowid) as nb_rows, SUM(edb.qty) as qty";
868 $sql .= " FROM ".$this->db->prefix()."expeditiondet_batch as edb";
869 $sql .= " INNER JOIN ".$this->db->prefix()."expeditiondet as ed ON (ed.rowid = edb.fk_expeditiondet)";
870 $sql .= " INNER JOIN ".$this->db->prefix()."expedition as exp ON (exp.rowid = ed.fk_expedition)";
871 // $sql .= ", ".$this->db->prefix()."societe as s";
872 if (!$user->hasRight('societe', 'client', 'voir')) {
873 $sql .= ", ".$this->db->prefix()."societe_commerciaux as sc";
874 }
875 $sql .= " WHERE exp.entity IN (".getEntity('expedition').")";
876 $sql .= " AND edb.batch = '".($this->db->escape($this->batch))."'";
877 if (!$user->hasRight('societe', 'client', 'voir')) {
878 $sql .= " AND exp.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
879 }
880 //$sql.= " AND exp.fk_statut != 0";
881 if ($socid > 0) {
882 $sql .= " AND exp.fk_soc = ".((int) $socid);
883 }
884
885 $result = $this->db->query($sql);
886 if ($result) {
887 $obj = $this->db->fetch_object($result);
888 $this->stats_expedition['customers'] = $obj->nb_customers;
889 $this->stats_expedition['nb'] = $obj->nb;
890 $this->stats_expedition['rows'] = $obj->nb_rows;
891 $this->stats_expedition['qty'] = $obj->qty ? $obj->qty : 0;
892
893
894 // Virtual products can't be used with kits (see langs with key ErrorNoteAlsoThatSubProductCantBeFollowedByLot)
895
896 // if it's a virtual product, maybe it is in invoice by extension
897 // if (getDolGlobalString('PRODUCT_STATS_WITH_PARENT_PROD_IF_INCDEC')) {
898 // $TFather = $this->getFather();
899 // if (is_array($TFather) && !empty($TFather)) {
900 // foreach ($TFather as &$fatherData) {
901 // $pFather = new Product($this->db);
902 // $pFather->id = $fatherData['id'];
903 // $qtyCoef = $fatherData['qty'];
904 //
905 // if ($fatherData['incdec']) {
906 // $pFather->loadStatsExpedition($socid);
907 //
908 // $this->stats_expedition['customers'] += $pFather->stats_expedition['customers'];
909 // $this->stats_expedition['nb'] += $pFather->stats_expedition['nb'];
910 // $this->stats_expedition['rows'] += $pFather->stats_expedition['rows'];
911 // $this->stats_expedition['qty'] += $pFather->stats_expedition['qty'] * $qtyCoef;
912 // }
913 // }
914 // }
915 // }
916
917 $parameters = array('socid' => $socid);
918 $reshook = $hookmanager->executeHooks('loadStatsLotExpedition', $parameters, $this, $action);
919 if ($reshook > 0) {
920 $this->stats_expedition = $hookmanager->resArray['stats_expedition'];
921 }
922
923 return 1;
924 } else {
925 $this->error = $this->db->error();
926 return -1;
927 }
928 }
929
936 public function loadStatsSupplierOrder($socid = 0)
937 {
938 global $user, $hookmanager, $action;
939
940 $sql = "SELECT COUNT(DISTINCT cf.fk_soc) as nb_customers, COUNT(DISTINCT cf.rowid) as nb,";
941 $sql .= " COUNT(cfd.rowid) as nb_rows, SUM(cfdi.qty) as qty";
942 $sql .= " FROM ".$this->db->prefix()."receptiondet_batch as cfdi";
943 $sql .= " INNER JOIN ".$this->db->prefix()."commande_fournisseurdet as cfd ON (cfd.rowid = cfdi.fk_elementdet)";
944 $sql .= " INNER JOIN ".$this->db->prefix()."commande_fournisseur as cf ON (cf.rowid = cfd.fk_commande)";
945 // $sql .= ", ".$this->db->prefix()."societe as s";
946 if (!$user->hasRight('societe', 'client', 'voir')) {
947 $sql .= ", ".$this->db->prefix()."societe_commerciaux as sc";
948 }
949 $sql .= " WHERE cf.entity IN (".getEntity('expedition').")";
950 $sql .= " AND cfdi.batch = '".($this->db->escape($this->batch))."'";
951 $sql .= " AND cfdi.fk_product = " . (int) $this->fk_product;
952 if (!$user->hasRight('societe', 'client', 'voir')) {
953 $sql .= " AND cf.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
954 }
955 //$sql.= " AND cf.fk_statut != 0";
956 if ($socid > 0) {
957 $sql .= " AND cf.fk_soc = ".((int) $socid);
958 }
959
960 $result = $this->db->query($sql);
961 if ($result) {
962 $obj = $this->db->fetch_object($result);
963 $this->stats_supplier_order['customers'] = $obj->nb_customers;
964 $this->stats_supplier_order['nb'] = $obj->nb;
965 $this->stats_supplier_order['rows'] = $obj->nb_rows;
966 $this->stats_supplier_order['qty'] = $obj->qty ? $obj->qty : 0;
967
968
969 // Virtual products can't be used with kits (see langs with key ErrorNoteAlsoThatSubProductCantBeFollowedByLot)
970
971 // if it's a virtual product, maybe it is in invoice by extension
972 // if (getDolGlobalString('PRODUCT_STATS_WITH_PARENT_PROD_IF_INCDEC')) {
973 // $TFather = $this->getFather();
974 // if (is_array($TFather) && !empty($TFather)) {
975 // foreach ($TFather as &$fatherData) {
976 // $pFather = new Product($this->db);
977 // $pFather->id = $fatherData['id'];
978 // $qtyCoef = $fatherData['qty'];
979 //
980 // if ($fatherData['incdec']) {
981 // $pFather->stats_supplier_order($socid);
982 //
983 // $this->stats_supplier_order['customers'] += $pFather->stats_supplier_order['customers'];
984 // $this->stats_supplier_order['nb'] += $pFather->stats_supplier_order['nb'];
985 // $this->stats_supplier_order['rows'] += $pFather->stats_supplier_order['rows'];
986 // $this->stats_supplier_order['qty'] += $pFather->stats_supplier_order['qty'] * $qtyCoef;
987 // }
988 // }
989 // }
990 // }
991
992 $parameters = array('socid' => $socid);
993 $reshook = $hookmanager->executeHooks('loadStatsLotSupplierOrder', $parameters, $this, $action);
994 if ($reshook > 0) {
995 $this->stats_supplier_order = $hookmanager->resArray['stats_supplier_order'];
996 }
997
998 return 1;
999 } else {
1000 $this->error = $this->db->error();
1001 return -1;
1002 }
1003 }
1004
1011 public function loadStatsReception($socid = 0)
1012 {
1013 global $user, $hookmanager, $action;
1014
1015 $sql = "SELECT COUNT(DISTINCT recep.fk_soc) as nb_customers, COUNT(DISTINCT recep.rowid) as nb,";
1016 $sql .= " COUNT(cfdi.rowid) as nb_rows, SUM(cfdi.qty) as qty";
1017 $sql .= " FROM ".$this->db->prefix()."receptiondet_batch as cfdi";
1018 $sql .= " INNER JOIN ".$this->db->prefix()."reception as recep ON (recep.rowid = cfdi.fk_reception)";
1019 // $sql .= ", ".$this->db->prefix()."societe as s";
1020 if (!$user->hasRight('societe', 'client', 'voir')) {
1021 $sql .= ", ".$this->db->prefix()."societe_commerciaux as sc";
1022 }
1023 $sql .= " WHERE recep.entity IN (".getEntity('reception').")";
1024 $sql .= " AND cfdi.batch = '".($this->db->escape($this->batch))."'";
1025 if (!$user->hasRight('societe', 'client', 'voir')) {
1026 $sql .= " AND recep.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
1027 }
1028 //$sql.= " AND exp.fk_statut != 0";
1029 if ($socid > 0) {
1030 $sql .= " AND recep.fk_soc = ".((int) $socid);
1031 }
1032
1033 $result = $this->db->query($sql);
1034 if ($result) {
1035 $obj = $this->db->fetch_object($result);
1036 $this->stats_reception['customers'] = $obj->nb_customers;
1037 $this->stats_reception['nb'] = $obj->nb;
1038 $this->stats_reception['rows'] = $obj->nb_rows;
1039 $this->stats_reception['qty'] = $obj->qty ? $obj->qty : 0;
1040
1041
1042 // Virtual products can't be used with kits (see langs with key ErrorNoteAlsoThatSubProductCantBeFollowedByLot)
1043
1044 // if it's a virtual product, maybe it is in invoice by extension
1045 // if (getDolGlobalString('PRODUCT_STATS_WITH_PARENT_PROD_IF_INCDEC')) {
1046 // $TFather = $this->getFather();
1047 // if (is_array($TFather) && !empty($TFather)) {
1048 // foreach ($TFather as &$fatherData) {
1049 // $pFather = new Product($this->db);
1050 // $pFather->id = $fatherData['id'];
1051 // $qtyCoef = $fatherData['qty'];
1052 //
1053 // if ($fatherData['incdec']) {
1054 // $pFather->loadStatsReception($socid);
1055 //
1056 // $this->stats_expedition['customers'] += $pFather->stats_expedition['customers'];
1057 // $this->stats_expedition['nb'] += $pFather->stats_expedition['nb'];
1058 // $this->stats_expedition['rows'] += $pFather->stats_expedition['rows'];
1059 // $this->stats_expedition['qty'] += $pFather->stats_expedition['qty'] * $qtyCoef;
1060 // }
1061 // }
1062 // }
1063 // }
1064
1065 $parameters = array('socid' => $socid);
1066 $reshook = $hookmanager->executeHooks('loadStatsLotReception', $parameters, $this, $action);
1067 if ($reshook > 0) {
1068 $this->stats_expedition = $hookmanager->resArray['stats_expedition'];
1069 }
1070
1071 return 1;
1072 } else {
1073 $this->error = $this->db->error();
1074 return -1;
1075 }
1076 }
1077
1084 public function loadStatsMo($socid = 0)
1085 {
1086 global $user, $hookmanager, $action;
1087
1088 $error = 0;
1089
1090 foreach (array('toconsume', 'consumed', 'toproduce', 'produced') as $role) {
1091 $this->stats_mo['customers_'.$role] = 0;
1092 $this->stats_mo['nb_'.$role] = 0;
1093 $this->stats_mo['qty_'.$role] = 0;
1094
1095 $sql = "SELECT COUNT(DISTINCT c.fk_soc) as nb_customers, COUNT(DISTINCT c.rowid) as nb,";
1096 $sql .= " SUM(mp.qty) as qty";
1097 $sql .= " FROM ".$this->db->prefix()."mrp_mo as c";
1098 $sql .= " INNER JOIN ".$this->db->prefix()."mrp_production as mp ON mp.fk_mo=c.rowid";
1099 if (!$user->hasRight('societe', 'client', 'voir') && !$socid) {
1100 $sql .= " INNER JOIN ".$this->db->prefix()."societe_commerciaux as sc ON sc.fk_soc=c.fk_soc AND sc.fk_user = ".((int) $user->id);
1101 }
1102 $sql .= " WHERE ";
1103 $sql .= " c.entity IN (".getEntity('mo').")";
1104
1105 $sql .= " AND mp.batch = '".($this->db->escape($this->batch))."'";
1106 $sql .= " AND mp.role ='".$this->db->escape($role)."'";
1107 if ($socid > 0) {
1108 $sql .= " AND c.fk_soc = ".((int) $socid);
1109 }
1110
1111 $result = $this->db->query($sql);
1112 if ($result) {
1113 $obj = $this->db->fetch_object($result);
1114 $this->stats_mo['customers_'.$role] = $obj->nb_customers ? $obj->nb_customers : 0;
1115 $this->stats_mo['nb_'.$role] = $obj->nb ? $obj->nb : 0;
1116 $this->stats_mo['qty_'.$role] = $obj->qty ? price2num($obj->qty, 'MS') : 0; // qty may be a float due to the SUM()
1117 } else {
1118 $this->error = $this->db->error();
1119 $error++;
1120 }
1121 }
1122
1123 if (!empty($error)) {
1124 return -1;
1125 }
1126
1127 $parameters = array('socid' => $socid);
1128 $reshook = $hookmanager->executeHooks('loadStatsCustomerMO', $parameters, $this, $action);
1129 if ($reshook > 0) {
1130 $this->stats_mo = $hookmanager->resArray['stats_mo'];
1131 }
1132
1133 return 1;
1134 }
1135
1136
1143 public function getLibStatut($mode = 0)
1144 {
1145 return $this->LibStatut(0, $mode);
1146 }
1147
1148 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1156 public function LibStatut($status, $mode = 0)
1157 {
1158 // phpcs:enable
1159 //global $langs;
1160
1161 //$langs->load('stocks');
1162
1163 return '';
1164 }
1165
1166
1173 public function getTooltipContentArray($params)
1174 {
1175 global $langs;
1176
1177 $langs->loadLangs(['stocks', 'productbatch']);
1178
1179 //$option = $params['option'] ?? '';
1180
1181 $datas = [];
1182 $datas['picto'] = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Batch").'</u>';
1183 //$datas['divopen'] = '<div width="100%">';
1184 $datas['batch'] = '<br><b>'.$langs->trans('Batch').':</b> '.$this->batch;
1185 if (isDolTms($this->eatby) && !getDolGlobalString('PRODUCT_DISABLE_EATBY')) {
1186 $datas['eatby'] = '<br><b>'.$langs->trans('EatByDate').':</b> '.dol_print_date($this->eatby, 'day');
1187 }
1188 if (isDolTms($this->sellby) && !getDolGlobalString('PRODUCT_DISABLE_SELLBY')) {
1189 $datas['sellby'] = '<br><b>'.$langs->trans('SellByDate').':</b> '.dol_print_date($this->sellby, 'day');
1190 }
1191 //$datas['divclose'] = '</div>';
1192
1193 return $datas;
1194 }
1195
1208 public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $maxlen = 24, $morecss = '', $save_lastsearch_value = -1)
1209 {
1210 global $langs, $hookmanager;
1211
1212 $result = '';
1213 $params = [
1214 'id' => $this->id,
1215 'objecttype' => $this->element,
1216 'option' => $option,
1217 ];
1218 $classfortooltip = 'classfortooltip';
1219 $dataparams = '';
1220 if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
1221 $classfortooltip = 'classforajaxtooltip';
1222 $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
1223 $label = '';
1224 } else {
1225 $label = implode($this->getTooltipContentArray($params));
1226 }
1227
1228 $url = DOL_URL_ROOT.'/product/stock/productlot_card.php?id='.$this->id;
1229
1230 if ($option != 'nolink') {
1231 // Add param to save lastsearch_values or not
1232 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1233 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1234 $add_save_lastsearch_values = 1;
1235 }
1236 if ($add_save_lastsearch_values) {
1237 $url .= '&save_lastsearch_values=1';
1238 }
1239 }
1240
1241 $linkclose = '';
1242 if (empty($notooltip)) {
1243 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
1244 $label = $langs->trans("ShowMyObject");
1245 $linkclose .= ' alt="'.dolPrintHTMLForAttribute($label).'"';
1246 }
1247 $linkclose .= ($label ? ' title="'.dolPrintHTMLForAttribute($label).'"' : ' title="tocomplete"');
1248 $linkclose .= $dataparams.' class="'.$classfortooltip.($morecss ? ' '.$morecss : '').'"';
1249 } else {
1250 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
1251 }
1252
1253 if ($option == 'nolink') {
1254 $linkstart = '<span';
1255 } else {
1256 $linkstart = '<a href="'.$url.'"';
1257 }
1258 $linkstart .= $linkclose.'>';
1259 if ($option == 'nolink') {
1260 $linkend = '</span>';
1261 } else {
1262 $linkend = '</a>';
1263 }
1264
1265 $result .= $linkstart;
1266 if ($withpicto) {
1267 $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'"'), 0, 0, $notooltip ? 0 : 1);
1268 }
1269 if ($withpicto != 2) {
1270 $result .= $this->batch;
1271 }
1272 $result .= $linkend;
1273
1274 global $action;
1275 $hookmanager->initHooks(array('productlotdao'));
1276 $parameters = array('id' => $this->id, 'getnomurl' => $result);
1277 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1278 if ($reshook > 0) {
1279 $result = $hookmanager->resPrint;
1280 } else {
1281 $result .= $hookmanager->resPrint;
1282 }
1283
1284 return $result;
1285 }
1286
1287
1294 public function initAsSpecimen()
1295 {
1296 global $conf;
1297
1298 $now = dol_now();
1299
1300 // Initialise parameters
1301 $this->id = 0;
1302 $this->ref = 'SPECIMEN';
1303 $this->specimen = 1;
1304
1305 $this->entity = $conf->entity;
1306 $this->fk_product = 0;
1307 $this->batch = 'ABCD123456';
1308 $this->eatby = $now - 100000;
1309 $this->sellby = $now - 100000;
1310 $this->datec = $now - 3600;
1311 $this->tms = $now;
1312 $this->fk_user_creat = 0;
1313 $this->fk_user_modif = 0;
1314 $this->import_key = '123456';
1315
1316 return 1;
1317 }
1318
1329 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
1330 {
1331 global $langs;
1332
1333 $langs->loadLangs(array('stocks', 'productbatch', "products"));
1334 $outputlangs->loadLangs(array('stocks', 'productbatch', "products"));
1335
1336 // Positionne le modele sur le nom du modele a utiliser
1337 if (!dol_strlen($modele)) {
1338 $modele = '';
1339
1340 if (!empty($this->model_pdf)) {
1341 $modele = $this->model_pdf;
1342 } elseif (getDolGlobalString('PRODUCT_BATCH_ADDON_PDF')) {
1343 $modele = getDolGlobalString('PRODUCT_BATCH_ADDON_PDF');
1344 }
1345 }
1346
1347 $modelpath = "core/modules/product_batch/doc/";
1348
1349 return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref);
1350 }
1351
1360 public function validateField($fields, $fieldKey, $fieldValue)
1361 {
1362 // Add your own validation rules here.
1363 if ($fieldKey == 'batch') {
1364 if (preg_match('/\s/', $fieldValue)) {
1365 $this->error = 'ErrorABatchShouldNotContainsSpaces';
1366 return false;
1367 }
1368 }
1369
1370 return parent::validateField($fields, $fieldKey, $fieldValue);
1371 }
1372}
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)
Charge tableau des stats expedition pour le lot/numéro de série.
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)
Charge tableau des stats expedition pour le lot/numéro de série.
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)
Charge tableau des stats expedition pour le lot/numéro de série.
__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)
Charge tableau des stats commande fournisseur pour le lot/numéro de série.
Class to manage Dolibarr users.
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.