/
productbatch.class.php
608 lines (531 loc) · 18.6 KB
/
productbatch.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
<?php
/* Copyright (C) 2007-2023 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2013-2014 Cedric GROSS <c.gross@kreiz-it.fr>
* Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
* Copyright (C) 2024 Ferran Marcet <fmarcet@2byte.es>
* Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* \file product/class/productbatch.class.php
* \ingroup productbatch
* \brief Manage record and specific data for batch number management.
* Manage table llx_product_batch (should have been named product_stock_batch)
*/
require_once DOL_DOCUMENT_ROOT."/core/class/commonobject.class.php";
/**
* Manage record for batch number management
*/
class Productbatch extends CommonObject
{
/**
* Batches rules
*/
const BATCH_RULE_SELLBY_EATBY_DATES_FIRST = 1;
/**
* @var string ID to identify managed object
*/
public $element = 'productbatch';
private static $_table_element = 'product_batch'; //!< Name of table without prefix where object is stored
public $fk_product_stock;
public $batch = '';
public $qty;
public $warehouseid;
/**
* @var int ID
*/
public $fk_product;
// Properties of the lot
public $lotid; // ID in table of the details of properties of each lots
/**
* @var int|string
* @deprecated
*/
public $sellby = ''; // dlc
/**
* @var int|string
* @deprecated
*/
public $eatby = ''; // dmd/dluo
/**
* Constructor
*
* @param DoliDB $db Database handler
*/
public function __construct($db)
{
$this->db = $db;
}
/**
* Create object into database
*
* @param User $user User that creates
* @param int $notrigger 0=launch triggers after, 1=disable triggers
* @return int Return integer <0 if KO, Id of created object if OK
*/
public function create($user, $notrigger = 0)
{
$error = 0;
// Clean parameters
$this->cleanParam();
// Check parameters
// Put here code to add control on parameters values
// Insert request
$sql = "INSERT INTO ".$this->db->prefix()."product_batch (";
$sql .= "fk_product_stock,";
$sql .= "sellby,"; // no more used
$sql .= "eatby,"; // no more used
$sql .= "batch,";
$sql .= "qty,";
$sql .= "import_key";
$sql .= ") VALUES (";
$sql .= " ".(!isset($this->fk_product_stock) ? 'NULL' : $this->fk_product_stock).",";
$sql .= " ".(!isset($this->sellby) || dol_strlen($this->sellby) == 0 ? 'NULL' : "'".$this->db->idate($this->sellby)."'").","; // no more used
$sql .= " ".(!isset($this->eatby) || dol_strlen($this->eatby) == 0 ? 'NULL' : "'".$this->db->idate($this->eatby)."'").","; // no more used
$sql .= " ".(!isset($this->batch) ? 'NULL' : "'".$this->db->escape($this->batch)."'").",";
$sql .= " ".(!isset($this->qty) ? 'NULL' : $this->qty).",";
$sql .= " ".(!isset($this->import_key) ? 'NULL' : "'".$this->db->escape($this->import_key)."'");
$sql .= ")";
$this->db->begin();
dol_syslog(get_class($this)."::create", LOG_DEBUG);
$resql = $this->db->query($sql);
if (!$resql) {
$error++;
$this->errors[] = "Error ".$this->db->lasterror();
}
if (!$error) {
$this->id = $this->db->last_insert_id($this->db->prefix().self::$_table_element);
}
// Commit or rollback
if ($error) {
$this->db->rollback();
return -1 * $error;
} else {
$this->db->commit();
return $this->id;
}
}
/**
* Load object in memory from the database
*
* @param int $id Id object
* @return int Return integer <0 if KO, >0 if OK
*/
public function fetch($id)
{
$sql = "SELECT";
$sql .= " t.rowid,";
$sql .= " t.tms,";
$sql .= " t.fk_product_stock,";
$sql .= " t.sellby as oldsellby,";
$sql .= " t.eatby as oldeatby,";
$sql .= " t.batch,";
$sql .= " t.qty,";
$sql .= " t.import_key,";
$sql .= " w.fk_entrepot,";
$sql .= " w.fk_product,";
$sql .= " pl.eatby,";
$sql .= " pl.sellby";
$sql .= " FROM ".$this->db->prefix()."product_batch as t";
$sql .= " INNER JOIN ".$this->db->prefix()."product_stock w on t.fk_product_stock = w.rowid"; // llx_product_stock is a parent table so this link does NOT generate duplicate record
$sql .= " LEFT JOIN ".$this->db->prefix()."product_lot as pl on pl.fk_product = w.fk_product and pl.batch = t.batch";
$sql .= " WHERE t.rowid = ".((int) $id);
dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
$resql = $this->db->query($sql);
if ($resql) {
if ($this->db->num_rows($resql)) {
$obj = $this->db->fetch_object($resql);
$this->id = $obj->rowid;
$this->tms = $this->db->jdate($obj->tms);
$this->fk_product_stock = $obj->fk_product_stock;
$this->sellby = $this->db->jdate($obj->sellby ? $obj->sellby : $obj->oldsellby);
$this->eatby = $this->db->jdate($obj->eatby ? $obj->eatby : $obj->oldeatby);
$this->batch = $obj->batch;
$this->qty = $obj->qty;
$this->import_key = $obj->import_key;
$this->warehouseid = $obj->fk_entrepot;
$this->fk_product = $obj->fk_product;
}
$this->db->free($resql);
return 1;
} else {
$this->error = "Error ".$this->db->lasterror();
return -1;
}
}
/**
* Update object into database
*
* @param User $user User that modifies
* @param int $notrigger 0=launch triggers after, 1=disable triggers
* @return int Return integer <0 if KO, >0 if OK
*/
public function update($user = null, $notrigger = 0)
{
$error = 0;
// Clean parameters
$this->cleanParam();
// TODO Check qty is ok for stock move. Negative may not be allowed.
if ($this->qty < 0) {
}
// Update request
$sql = "UPDATE ".$this->db->prefix().self::$_table_element." SET";
$sql .= " fk_product_stock=".(isset($this->fk_product_stock) ? $this->fk_product_stock : "null").",";
$sql .= " sellby=".(dol_strlen($this->sellby) != 0 ? "'".$this->db->idate($this->sellby)."'" : 'null').",";
$sql .= " eatby=".(dol_strlen($this->eatby) != 0 ? "'".$this->db->idate($this->eatby)."'" : 'null').",";
$sql .= " batch=".(isset($this->batch) ? "'".$this->db->escape($this->batch)."'" : "null").",";
$sql .= " qty=".(isset($this->qty) ? $this->qty : "null").",";
$sql .= " import_key=".(isset($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null");
$sql .= " WHERE rowid=".((int) $this->id);
$this->db->begin();
dol_syslog(get_class($this)."::update", LOG_DEBUG);
$resql = $this->db->query($sql);
if (!$resql) {
$error++;
$this->errors[] = "Error ".$this->db->lasterror();
}
// Commit or rollback
if ($error) {
foreach ($this->errors as $errmsg) {
dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
$this->error .= ($this->error ? ', '.$errmsg : $errmsg);
}
$this->db->rollback();
return -1 * $error;
} else {
$this->db->commit();
return 1;
}
}
/**
* Delete object in database
*
* @param User $user User that deletes
* @param int $notrigger 0=launch triggers after, 1=disable triggers
* @return int Return integer <0 if KO, >0 if OK
*/
public function delete($user, $notrigger = 0)
{
$error = 0;
$this->db->begin();
if (!$error) {
$sql = "DELETE FROM ".$this->db->prefix().self::$_table_element;
$sql .= " WHERE rowid=".((int) $this->id);
dol_syslog(get_class($this)."::delete", LOG_DEBUG);
$resql = $this->db->query($sql);
if (!$resql) {
$error++;
$this->errors[] = "Error ".$this->db->lasterror();
}
}
// Commit or rollback
if ($error) {
foreach ($this->errors as $errmsg) {
dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
$this->error .= ($this->error ? ', '.$errmsg : $errmsg);
}
$this->db->rollback();
return -1 * $error;
} else {
$this->db->commit();
return 1;
}
}
/**
* Load an object from its id and create a new one in database
*
* @param User $user User making the clone
* @param int $fromid Id of object to clone
* @return int New id of clone
*/
public function createFromClone(User $user, $fromid)
{
$error = 0;
$object = new Productbatch($this->db);
$this->db->begin();
// Load source object
$object->fetch($fromid);
$object->id = 0;
$object->statut = 0;
// Clear fields
// ...
// Create clone
$object->context['createfromclone'] = 'createfromclone';
$result = $object->create($user);
// Other options
if ($result < 0) {
$this->error = $object->error;
$this->errors = array_merge($this->errors, $object->errors);
$error++;
}
if (!$error) {
}
unset($object->context['createfromclone']);
// End
if (!$error) {
$this->db->commit();
return $object->id;
} else {
$this->db->rollback();
return -1;
}
}
/**
* Initialise object with example values
* Id must be 0 if object instance is a specimen
*
* @return int
*/
public function initAsSpecimen()
{
$this->id = 0;
$this->tms = dol_now();
$this->fk_product_stock = '';
$this->sellby = '';
$this->eatby = '';
$this->batch = '';
$this->import_key = '';
return 1;
}
/**
* Clean fields (trimming)
*
* @return void
*/
private function cleanParam()
{
if (isset($this->fk_product_stock)) {
$this->fk_product_stock = (int) trim($this->fk_product_stock);
}
if (isset($this->batch)) {
$this->batch = trim($this->batch);
}
if (isset($this->qty)) {
$this->qty = (float) trim((string) $this->qty);
}
if (isset($this->import_key)) {
$this->import_key = trim($this->import_key);
}
}
/**
* Find first detailed record that match either eat-by, sell-by or batch within the warehouse
*
* @param int $fk_product_stock id product_stock for object
* @param integer $eatby eat-by date for object - deprecated: a search must be done on batch number
* @param integer $sellby sell-by date for object - deprecated: a search must be done on batch number
* @param string $batch_number batch number for object
* @param int $fk_warehouse filter on warehouse (use it if you don't have $fk_product_stock)
* @return int Return integer <0 if KO, >0 if OK
*/
public function find($fk_product_stock = 0, $eatby = null, $sellby = null, $batch_number = '', $fk_warehouse = 0)
{
$where = array();
$sql = "SELECT";
$sql .= " t.rowid,";
$sql .= " t.tms,";
$sql .= " t.fk_product_stock,";
$sql .= " t.sellby,"; // deprecated
$sql .= " t.eatby,"; // deprecated
$sql .= " t.batch,";
$sql .= " t.qty,";
$sql .= " t.import_key";
$sql .= " FROM ".$this->db->prefix().self::$_table_element." as t";
if ($fk_product_stock > 0 || empty($fk_warehouse)) {
$sql .= " WHERE t.fk_product_stock = ".((int) $fk_product_stock);
} else {
$sql .= ", ".$this->db->prefix()."product_stock as ps";
$sql .= " WHERE t.fk_product_stock = ps.rowid AND ps.fk_entrepot = ".((int) $fk_warehouse);
}
if (!empty($eatby)) {
array_push($where, " eatby = '".$this->db->idate($eatby)."'"); // deprecated
}
if (!empty($sellby)) {
array_push($where, " sellby = '".$this->db->idate($sellby)."'"); // deprecated
}
if (!empty($batch_number)) {
$sql .= " AND batch = '".$this->db->escape($batch_number)."'";
}
if (!empty($where)) {
$sql .= " AND (".$this->db->sanitize(implode(" OR ", $where), 1, 1, 1).")";
}
dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
$resql = $this->db->query($sql);
if ($resql) {
if ($this->db->num_rows($resql)) {
$obj = $this->db->fetch_object($resql);
$this->id = $obj->rowid;
$this->tms = $this->db->jdate($obj->tms);
$this->fk_product_stock = $obj->fk_product_stock;
$this->sellby = $this->db->jdate($obj->sellby); // deprecated. do no tuse this data.
$this->eatby = $this->db->jdate($obj->eatby); // deprecated. do not use this data.
$this->batch = $obj->batch;
$this->qty = $obj->qty;
$this->import_key = $obj->import_key;
}
$this->db->free($resql);
return 1;
} else {
$this->error = "Error ".$this->db->lasterror();
return -1;
}
}
/**
* Return all batch detail records for a given product and warehouse
*
* @param DoliDB $dbs database object
* @param int $fk_product_stock id product_stock for object
* @param int $with_qty 1 = doesn't return line with 0 quantity
* @param int $fk_product If set to a product id, get eatby and sellby from table llx_product_lot
* @return array|int Return integer <0 if KO, array of batch
*/
public static function findAll($dbs, $fk_product_stock, $with_qty = 0, $fk_product = 0)
{
global $conf;
$ret = array();
$sql = "SELECT";
$sql .= " t.rowid,";
$sql .= " t.tms,";
$sql .= " t.fk_product_stock,";
$sql .= " t.sellby as oldsellby,"; // deprecated but may not be migrated into new table
$sql .= " t.eatby as oldeatby,"; // deprecated but may not be migrated into new table
$sql .= " t.batch,";
$sql .= " t.qty,";
if (getDolGlobalString('SHIPPING_DISPLAY_STOCK_ENTRY_DATE')) {
$sql .= " MAX(sm.datem) as date_entree,";
}
$sql .= " t.import_key";
if ($fk_product > 0) {
$sql .= ", pl.rowid as lotid, pl.eatby as eatby, pl.sellby as sellby";
// TODO May add extrafields to ?
}
$sql .= " FROM ".$dbs->prefix()."product_batch as t";
if ($fk_product > 0) { // Add link to the table of details of a lot
$sql .= " LEFT JOIN ".$dbs->prefix()."product_lot as pl ON pl.fk_product = ".((int) $fk_product)." AND pl.batch = t.batch";
// TODO May add extrafields to ?
}
if (getDolGlobalString('SHIPPING_DISPLAY_STOCK_ENTRY_DATE')) {
$sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product_stock AS ps ON (ps.rowid = fk_product_stock)';
$sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'stock_mouvement AS sm ON (sm.batch = t.batch AND ps.fk_entrepot=sm.fk_entrepot AND sm.type_mouvement IN (0,3))';
}
$sql .= " WHERE fk_product_stock=".((int) $fk_product_stock);
if ($with_qty) {
$sql .= " AND t.qty <> 0";
}
if (getDolGlobalString('SHIPPING_DISPLAY_STOCK_ENTRY_DATE')) {
$sql .= ' GROUP BY t.rowid, t.tms, t.fk_product_stock,t.sellby,t.eatby , t.batch,t.qty,t.import_key';
if ($fk_product > 0) {
$sql .= ', pl.rowid, pl.eatby, pl.sellby';
}
}
$sql .= " ORDER BY ";
// TODO : use product lifo and fifo when product will implement it
if (getDolGlobalString('SHIPPING_DISPLAY_STOCK_ENTRY_DATE')) {
$sql .= 'date_entree ASC,t.batch ASC,';
}
if ($fk_product > 0) {
$sql .= "pl.eatby ASC, pl.sellby ASC, ";
}
$sql .= "t.eatby ASC, t.sellby ASC ";
$sql .= ", t.qty ".(!getDolGlobalString('DO_NOT_TRY_TO_DEFRAGMENT_STOCKS_WAREHOUSE') ? 'ASC' : 'DESC'); // Note : qty ASC is important for expedition card, to avoid stock fragmentation
$sql .= ", t.batch ASC";
dol_syslog("productbatch::findAll", LOG_DEBUG);
$resql = $dbs->query($sql);
if ($resql) {
$num = $dbs->num_rows($resql);
$i = 0;
while ($i < $num) {
$obj = $dbs->fetch_object($resql);
$tmp = new Productbatch($dbs);
$tmp->id = $obj->rowid;
$tmp->tms = $dbs->jdate($obj->tms);
$tmp->fk_product_stock = $obj->fk_product_stock;
$tmp->batch = $obj->batch;
$tmp->qty = $obj->qty;
$tmp->import_key = $obj->import_key;
if (getDolGlobalString('SHIPPING_DISPLAY_STOCK_ENTRY_DATE')) {
$tmp->context['stock_entry_date'] = $dbs->jdate($obj->date_entree);
}
if ($fk_product > 0) {
// Some properties of the lot
$tmp->lotid = $obj->lotid; // ID in table of the details of properties of each lots
$tmp->sellby = $dbs->jdate($obj->sellby ? $obj->sellby : $obj->oldsellby);
$tmp->eatby = $dbs->jdate($obj->eatby ? $obj->eatby : $obj->oldeatby);
}
$ret[$tmp->batch] = $tmp; // $ret is for a $fk_product_stock and unique key is on $fk_product_stock+batch
$i++;
}
$dbs->free($resql);
return $ret;
} else {
//$error = "Error ".$dbs->lasterror();
return -1;
}
}
/**
* Return all batch known for a product and a warehouse (batch that was one day used)
*
* @param int $fk_product Id of product
* @param int $fk_warehouse Id of warehouse
* @param int $qty_min [=NULL] Minimum quantity
* @param string $sortfield [=NULL] List of sort fields, separated by comma. Example: 't1.fielda,t2.fieldb'
* @param string $sortorder [=NULL] Sort order, separated by comma. Example: 'ASC,DESC';
* @return int|array Return integer <0 if KO, array of batch
*
* @throws Exception
*/
public function findAllForProduct($fk_product, $fk_warehouse = 0, $qty_min = null, $sortfield = null, $sortorder = null)
{
$productBatchList = array();
dol_syslog(__METHOD__.' fk_product='.$fk_product.', fk_warehouse='.$fk_warehouse.', qty_min='.$qty_min.', sortfield='.$sortfield.', sortorder='.$sortorder, LOG_DEBUG);
$sql = "SELECT";
$sql .= " pl.rowid";
$sql .= ", pl.fk_product";
$sql .= ", pl.batch";
$sql .= ", pl.sellby";
$sql .= ", pl.eatby";
$sql .= ", pb.qty";
$sql .= " FROM ".$this->db->prefix()."product_lot as pl";
$sql .= " LEFT JOIN ".$this->db->prefix()."product as p ON p.rowid = pl.fk_product";
$sql .= " LEFT JOIN ".$this->db->prefix()."product_batch AS pb ON pl.batch = pb.batch";
$sql .= " LEFT JOIN ".$this->db->prefix()."product_stock AS ps ON ps.rowid = pb.fk_product_stock AND ps.fk_product = ".((int) $fk_product);
$sql .= " WHERE p.entity IN (".getEntity('product').")";
$sql .= " AND pl.fk_product = ".((int) $fk_product);
if ($fk_warehouse > 0) {
$sql .= " AND ps.fk_entrepot = ".((int) $fk_warehouse);
}
if ($qty_min !== null) {
$sql .= " AND pb.qty > ".((float) price2num($qty_min, 'MS'));
}
$sql .= $this->db->order($sortfield, $sortorder);
$resql = $this->db->query($sql);
if ($resql) {
while ($obj = $this->db->fetch_object($resql)) {
$productBatch = new self($this->db);
$productBatch->id = $obj->rowid;
$productBatch->fk_product = $obj->fk_product;
$productBatch->batch = $obj->batch;
$productBatch->eatby = $this->db->jdate($obj->eatby);
$productBatch->sellby = $this->db->jdate($obj->sellby);
$productBatch->qty = $obj->qty;
$productBatchList[] = $productBatch;
}
$this->db->free($resql);
return $productBatchList;
} else {
dol_syslog(__METHOD__.' Error: '.$this->db->lasterror(), LOG_ERR);
return -1;
}
}
}