dolibarr 21.0.0-beta
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 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;
67 public $entrepot_id;
71 public $fk_origin_stock; // rowid in llx_product_batch table (not useful)
75 public $fk_warehouse; // warehouse ID
79 public $fk_expeditiondet;
80
81
87 public function __construct($db)
88 {
89 $this->db = $db;
90 }
91
98 public function fetchFromStock($id_stockdluo)
99 {
100 $sql = "SELECT";
101 $sql .= " pb.batch,";
102 $sql .= " pl.sellby,";
103 $sql .= " pl.eatby,";
104 $sql .= " ps.fk_entrepot";
105 $sql .= " FROM ".MAIN_DB_PREFIX."product_batch as pb";
106 $sql .= " JOIN ".MAIN_DB_PREFIX."product_stock as ps on pb.fk_product_stock=ps.rowid";
107 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX."product_lot as pl on pl.batch = pb.batch AND pl.fk_product = ps.fk_product";
108 $sql .= " WHERE pb.rowid = ".(int) $id_stockdluo;
109
110 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
111
112 $resql = $this->db->query($sql);
113 if ($resql) {
114 if ($this->db->num_rows($resql)) {
115 $obj = $this->db->fetch_object($resql);
116
117 $this->sellby = $this->db->jdate($obj->sellby);
118 $this->eatby = $this->db->jdate($obj->eatby);
119 $this->batch = $obj->batch;
120 $this->entrepot_id = $obj->fk_entrepot;
121 $this->fk_origin_stock = (int) $id_stockdluo;
122 }
123 $this->db->free($resql);
124
125 return 1;
126 } else {
127 $this->error = "Error ".$this->db->lasterror();
128 return -1;
129 }
130 }
131
140 public function create($id_line_expdet, $f_user = null, $notrigger = 0)
141 {
142 global $user;
143
144 $error = 0;
145 if (!is_object($f_user)) {
146 $f_user = $user;
147 }
148
149 $id_line_expdet = (int) $id_line_expdet;
150
151 $sql = "INSERT INTO ".MAIN_DB_PREFIX.$this->table_element." (";
152 $sql .= "fk_expeditiondet";
153 $sql .= ", sellby";
154 $sql .= ", eatby";
155 $sql .= ", batch";
156 $sql .= ", qty";
157 $sql .= ", fk_origin_stock";
158 $sql .= ", fk_warehouse";
159 $sql .= ") VALUES (";
160 $sql .= $id_line_expdet;
161 $sql .= ", ".(!isset($this->sellby) || dol_strlen($this->sellby) == 0 ? 'NULL' : ("'".$this->db->idate($this->sellby))."'");
162 $sql .= ", ".(!isset($this->eatby) || dol_strlen($this->eatby) == 0 ? 'NULL' : ("'".$this->db->idate($this->eatby))."'");
163 $sql .= ", ".($this->batch == '' ? 'NULL' : ("'".$this->db->escape($this->batch)."'"));
164 $sql .= ", ".(!isset($this->qty) ? ((!isset($this->dluo_qty)) ? 'NULL' : $this->dluo_qty) : $this->qty); // dluo_qty deprecated, use qty
165 $sql .= ", ".((int) $this->fk_origin_stock);
166 $sql .= ", ".(empty($this->fk_warehouse) ? 'NULL' : $this->fk_warehouse);
167 $sql .= ")";
168
169 dol_syslog(__METHOD__, LOG_DEBUG);
170 $resql = $this->db->query($sql);
171 if (!$resql) {
172 $error++;
173 $this->errors[] = "Error ".$this->db->lasterror();
174 }
175
176 if (!$error) {
177 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
178
179 $this->fk_expeditiondet = $id_line_expdet;
180 }
181
182 if (!$error && !$notrigger) {
183 // Call trigger
184 $result = $this->call_trigger('EXPEDITIONLINEBATCH_CREATE', $f_user);
185 if ($result < 0) {
186 $error++;
187 }
188 // End call triggers
189 }
190
191 if (!$error) {
192 return $this->id;
193 } else {
194 foreach ($this->errors as $errmsg) {
195 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
196 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
197 }
198 return -1 * $error;
199 }
200 }
201
208 public function deleteFromShipment($id_expedition)
209 {
210 $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
211 $sql .= " WHERE fk_expeditiondet in (SELECT rowid FROM ".MAIN_DB_PREFIX."expeditiondet WHERE fk_expedition=".((int) $id_expedition).")";
212
213 dol_syslog(__METHOD__, LOG_DEBUG);
214 if ($this->db->query($sql)) {
215 return 1;
216 } else {
217 return -1;
218 }
219 }
220
228 public function fetchAll($id_line_expdet, $fk_product = 0)
229 {
230 $sql = "SELECT";
231 $sql .= " eb.rowid,";
232 $sql .= " eb.fk_expeditiondet,";
233 $sql .= " eb.sellby as oldsellby,"; // deprecated
234 $sql .= " eb.eatby as oldeatby,"; // deprecated
235 $sql .= " eb.batch,";
236 $sql .= " eb.qty,";
237 $sql .= " eb.fk_origin_stock,";
238 $sql .= " eb.fk_warehouse";
239 if ($fk_product > 0) {
240 $sql .= ", pl.sellby";
241 $sql .= ", pl.eatby";
242 }
243 $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." as eb";
244 if ($fk_product > 0) {
245 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product_lot as pl ON pl.batch = eb.batch AND pl.fk_product = ".((int) $fk_product);
246 }
247 $sql .= " WHERE fk_expeditiondet=".(int) $id_line_expdet;
248
249 dol_syslog(__METHOD__, LOG_DEBUG);
250 $resql = $this->db->query($sql);
251 if ($resql) {
252 $num = $this->db->num_rows($resql);
253 $i = 0;
254 $ret = array();
255 while ($i < $num) {
256 $obj = $this->db->fetch_object($resql);
257
258 $tmp = new self($this->db);
259 $tmp->sellby = $this->db->jdate(($fk_product > 0 && $obj->sellby) ? $obj->sellby : $obj->oldsellby);
260 $tmp->eatby = $this->db->jdate(($fk_product > 0 && $obj->eatby) ? $obj->eatby : $obj->oldeatby);
261 $tmp->batch = $obj->batch;
262 $tmp->id = $obj->rowid;
263 $tmp->fk_origin_stock = $obj->fk_origin_stock;
264 $tmp->fk_expeditiondet = $obj->fk_expeditiondet;
265 $tmp->fk_warehouse = $obj->fk_warehouse;
266 $tmp->dluo_qty = $obj->qty; // dluo_qty deprecated, use qty
267 $tmp->qty = $obj->qty;
268
269 $ret[] = $tmp;
270 $i++;
271 }
272
273 $this->db->free($resql);
274
275 return $ret;
276 } else {
277 dol_print_error($this->db);
278 return -1;
279 }
280 }
281}
Parent class of all other business classes (invoices, contracts, proposals, orders,...
call_trigger($triggerName, $user)
Call trigger based on this instance.
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.