dolibarr 23.0.3
expeditionlinebatch.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2007-2015 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2013-2014 Cedric GROSS <c.gross@kreiz-it.fr>
4 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
31{
35 public $element = 'expeditionlignebatch';
36
40 public $table_element = 'expeditiondet_batch';
41
45 public $sellby;
49 public $eatby;
53 public $batch;
54
58 public $qty;
63 public $dluo_qty;
68 public $entrepot_id;
72 public $fk_origin_stock; // rowid in llx_product_batch table (not useful)
76 public $fk_warehouse; // warehouse ID
80 public $fk_expeditiondet;
81
82
88 public function __construct($db)
89 {
90 $this->db = $db;
91 }
92
99 public function fetchFromStock($id_stockdluo)
100 {
101 $sql = "SELECT";
102 $sql .= " pb.batch,";
103 $sql .= " pl.sellby,";
104 $sql .= " pl.eatby,";
105 $sql .= " ps.fk_entrepot";
106 $sql .= " FROM ".MAIN_DB_PREFIX."product_batch as pb";
107 $sql .= " JOIN ".MAIN_DB_PREFIX."product_stock as ps on pb.fk_product_stock=ps.rowid";
108 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX."product_lot as pl on pl.batch = pb.batch AND pl.fk_product = ps.fk_product";
109 $sql .= " WHERE pb.rowid = ".(int) $id_stockdluo;
110
111 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
112
113 $resql = $this->db->query($sql);
114 if ($resql) {
115 if ($this->db->num_rows($resql)) {
116 $obj = $this->db->fetch_object($resql);
117
118 $this->sellby = $this->db->jdate($obj->sellby);
119 $this->eatby = $this->db->jdate($obj->eatby);
120 $this->batch = $obj->batch;
121 $this->entrepot_id = $obj->fk_entrepot; // deprecated use fk_warehouse
122 $this->fk_warehouse = $obj->fk_entrepot;
123 $this->fk_origin_stock = (int) $id_stockdluo;
124 }
125 $this->db->free($resql);
126
127 return 1;
128 } else {
129 $this->error = "Error ".$this->db->lasterror();
130 return -1;
131 }
132 }
133
142 public function create($id_line_expdet, $f_user = null, $notrigger = 0)
143 {
144 global $user;
145
146 $error = 0;
147 if (!is_object($f_user)) {
148 $f_user = $user;
149 }
150
151 $id_line_expdet = (int) $id_line_expdet;
152
153 $sql = "INSERT INTO ".MAIN_DB_PREFIX.$this->table_element." (";
154 $sql .= "fk_expeditiondet";
155 $sql .= ", sellby";
156 $sql .= ", eatby";
157 $sql .= ", batch";
158 $sql .= ", qty";
159 $sql .= ", fk_origin_stock";
160 $sql .= ", fk_warehouse";
161 $sql .= ") VALUES (";
162 $sql .= $id_line_expdet;
163 $sql .= ", ".(!isset($this->sellby) || dol_strlen($this->sellby) == 0 ? 'NULL' : ("'".$this->db->idate($this->sellby))."'");
164 $sql .= ", ".(!isset($this->eatby) || dol_strlen($this->eatby) == 0 ? 'NULL' : ("'".$this->db->idate($this->eatby))."'");
165 $sql .= ", ".($this->batch == '' ? 'NULL' : ("'".$this->db->escape((string) $this->batch)."'"));
166 $sql .= ", ".(!isset($this->qty) ? ((!isset($this->dluo_qty)) ? 'NULL' : $this->dluo_qty) : $this->qty); // dluo_qty deprecated, use qty
167 $sql .= ", ".((int) $this->fk_origin_stock);
168 $sql .= ", ".(empty($this->fk_warehouse) ? 'NULL' : $this->fk_warehouse);
169 $sql .= ")";
170
171 dol_syslog(__METHOD__, LOG_DEBUG);
172 $resql = $this->db->query($sql);
173 if (!$resql) {
174 $error++;
175 $this->errors[] = "Error ".$this->db->lasterror();
176 }
177
178 if (!$error) {
179 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
180
181 $this->fk_expeditiondet = $id_line_expdet;
182 }
183
184 if (!$error && !$notrigger) {
185 // Call trigger
186 $result = $this->call_trigger('EXPEDITIONLINEBATCH_CREATE', $f_user);
187 if ($result < 0) {
188 $error++;
189 }
190 // End call triggers
191 }
192
193 if (!$error) {
194 return $this->id;
195 } else {
196 foreach ($this->errors as $errmsg) {
197 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
198 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
199 }
200 return -1 * $error;
201 }
202 }
203
210 public function deleteFromShipment($id_expedition)
211 {
212 $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
213 $sql .= " WHERE fk_expeditiondet in (SELECT rowid FROM ".MAIN_DB_PREFIX."expeditiondet WHERE fk_expedition=".((int) $id_expedition).")";
214
215 dol_syslog(__METHOD__, LOG_DEBUG);
216 if ($this->db->query($sql)) {
217 return 1;
218 } else {
219 return -1;
220 }
221 }
222
230 public function fetchAll($id_line_expdet, $fk_product = 0)
231 {
232 $sql = "SELECT";
233 $sql .= " eb.rowid,";
234 $sql .= " eb.fk_expeditiondet,";
235 $sql .= " eb.sellby as oldsellby,"; // deprecated
236 $sql .= " eb.eatby as oldeatby,"; // deprecated
237 $sql .= " eb.batch,";
238 $sql .= " eb.qty,";
239 $sql .= " eb.fk_origin_stock,";
240 $sql .= " eb.fk_warehouse";
241 if ($fk_product > 0) {
242 $sql .= ", pl.sellby";
243 $sql .= ", pl.eatby";
244 }
245 $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." as eb";
246 if ($fk_product > 0) {
247 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product_lot as pl ON pl.batch = eb.batch AND pl.fk_product = ".((int) $fk_product);
248 }
249 $sql .= " WHERE fk_expeditiondet=".(int) $id_line_expdet;
250
251 dol_syslog(__METHOD__, LOG_DEBUG);
252 $resql = $this->db->query($sql);
253 if ($resql) {
254 $num = $this->db->num_rows($resql);
255 $i = 0;
256 $ret = array();
257 while ($i < $num) {
258 $obj = $this->db->fetch_object($resql);
259
260 $tmp = new self($this->db);
261 $tmp->sellby = $this->db->jdate(($fk_product > 0 && $obj->sellby) ? $obj->sellby : $obj->oldsellby);
262 $tmp->eatby = $this->db->jdate(($fk_product > 0 && $obj->eatby) ? $obj->eatby : $obj->oldeatby);
263 $tmp->batch = $obj->batch;
264 $tmp->id = $obj->rowid;
265 $tmp->fk_origin_stock = $obj->fk_origin_stock;
266 $tmp->fk_expeditiondet = $obj->fk_expeditiondet;
267 $tmp->fk_warehouse = $obj->fk_warehouse;
268 $tmp->dluo_qty = $obj->qty; // dluo_qty deprecated, use qty
269 $tmp->qty = $obj->qty;
270
271 $ret[] = $tmp;
272 $i++;
273 }
274
275 $this->db->free($resql);
276
277 return $ret;
278 } else {
279 dol_print_error($this->db);
280 return -1;
281 }
282 }
283}
Parent class of all other business classes (invoices, contracts, proposals, orders,...
CRUD class for batch number management within shipment.
fetchFromStock($id_stockdluo)
Fill object based on a product-warehouse-batch's record.
deleteFromShipment($id_expedition)
Delete batch record attach to a shipment.
create($id_line_expdet, $f_user=null, $notrigger=0)
Create an expeditiondet_batch DB record link to an expedtiondet record.
fetchAll($id_line_expdet, $fk_product=0)
Retrieve all batch number detailed information of a shipment line.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.