dolibarr 20.0.0
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 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
30{
34 public $element = 'expeditionlignebatch';
35
39 public $table_element = 'expeditiondet_batch';
40
41 public $sellby;
42 public $eatby;
43 public $batch;
44
48 public $qty;
49 public $dluo_qty; // deprecated, use qty
50 public $entrepot_id;
51 public $fk_origin_stock; // rowid in llx_product_batch table (not useful)
52 public $fk_warehouse; // warehouse ID
53 public $fk_expeditiondet;
54
55
61 public function __construct($db)
62 {
63 $this->db = $db;
64 }
65
72 public function fetchFromStock($id_stockdluo)
73 {
74 $sql = "SELECT";
75 $sql .= " pb.batch,";
76 $sql .= " pl.sellby,";
77 $sql .= " pl.eatby,";
78 $sql .= " ps.fk_entrepot";
79 $sql .= " FROM ".MAIN_DB_PREFIX."product_batch as pb";
80 $sql .= " JOIN ".MAIN_DB_PREFIX."product_stock as ps on pb.fk_product_stock=ps.rowid";
81 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX."product_lot as pl on pl.batch = pb.batch AND pl.fk_product = ps.fk_product";
82 $sql .= " WHERE pb.rowid = ".(int) $id_stockdluo;
83
84 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
85
86 $resql = $this->db->query($sql);
87 if ($resql) {
88 if ($this->db->num_rows($resql)) {
89 $obj = $this->db->fetch_object($resql);
90
91 $this->sellby = $this->db->jdate($obj->sellby);
92 $this->eatby = $this->db->jdate($obj->eatby);
93 $this->batch = $obj->batch;
94 $this->entrepot_id = $obj->fk_entrepot;
95 $this->fk_origin_stock = (int) $id_stockdluo;
96 }
97 $this->db->free($resql);
98
99 return 1;
100 } else {
101 $this->error = "Error ".$this->db->lasterror();
102 return -1;
103 }
104 }
105
114 public function create($id_line_expdet, $f_user = null, $notrigger = 0)
115 {
116 global $user;
117
118 $error = 0;
119 if (!is_object($f_user)) {
120 $f_user = $user;
121 }
122
123 $id_line_expdet = (int) $id_line_expdet;
124
125 $sql = "INSERT INTO ".MAIN_DB_PREFIX.$this->table_element." (";
126 $sql .= "fk_expeditiondet";
127 $sql .= ", sellby";
128 $sql .= ", eatby";
129 $sql .= ", batch";
130 $sql .= ", qty";
131 $sql .= ", fk_origin_stock";
132 $sql .= ", fk_warehouse";
133 $sql .= ") VALUES (";
134 $sql .= $id_line_expdet;
135 $sql .= ", ".(!isset($this->sellby) || dol_strlen($this->sellby) == 0 ? 'NULL' : ("'".$this->db->idate($this->sellby))."'");
136 $sql .= ", ".(!isset($this->eatby) || dol_strlen($this->eatby) == 0 ? 'NULL' : ("'".$this->db->idate($this->eatby))."'");
137 $sql .= ", ".(!isset($this->batch) ? 'NULL' : ("'".$this->db->escape($this->batch)."'"));
138 $sql .= ", ".(!isset($this->qty) ? ((!isset($this->dluo_qty)) ? 'NULL' : $this->dluo_qty) : $this->qty); // dluo_qty deprecated, use qty
139 $sql .= ", ".(!isset($this->fk_origin_stock) ? 'NULL' : $this->fk_origin_stock);
140 $sql .= ", ".(!isset($this->fk_warehouse) ? 'NULL' : $this->fk_warehouse);
141 $sql .= ")";
142
143 dol_syslog(__METHOD__, LOG_DEBUG);
144 $resql = $this->db->query($sql);
145 if (!$resql) {
146 $error++;
147 $this->errors[] = "Error ".$this->db->lasterror();
148 }
149
150 if (!$error) {
151 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
152
153 $this->fk_expeditiondet = $id_line_expdet;
154 }
155
156 if (!$error && !$notrigger) {
157 // Call trigger
158 $result = $this->call_trigger('EXPEDITIONLINEBATCH_CREATE', $f_user);
159 if ($result < 0) {
160 $error++;
161 }
162 // End call triggers
163 }
164
165 if (!$error) {
166 return $this->id;
167 } else {
168 foreach ($this->errors as $errmsg) {
169 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
170 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
171 }
172 return -1 * $error;
173 }
174 }
175
182 public function deleteFromShipment($id_expedition)
183 {
184 $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
185 $sql .= " WHERE fk_expeditiondet in (SELECT rowid FROM ".MAIN_DB_PREFIX."expeditiondet WHERE fk_expedition=".((int) $id_expedition).")";
186
187 dol_syslog(__METHOD__, LOG_DEBUG);
188 if ($this->db->query($sql)) {
189 return 1;
190 } else {
191 return -1;
192 }
193 }
194
202 public function fetchAll($id_line_expdet, $fk_product = 0)
203 {
204 $sql = "SELECT";
205 $sql .= " eb.rowid,";
206 $sql .= " eb.fk_expeditiondet,";
207 $sql .= " eb.sellby as oldsellby,"; // deprecated
208 $sql .= " eb.eatby as oldeatby,"; // deprecated
209 $sql .= " eb.batch,";
210 $sql .= " eb.qty,";
211 $sql .= " eb.fk_origin_stock,";
212 $sql .= " eb.fk_warehouse";
213 if ($fk_product > 0) {
214 $sql .= ", pl.sellby";
215 $sql .= ", pl.eatby";
216 }
217 $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." as eb";
218 if ($fk_product > 0) {
219 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product_lot as pl ON pl.batch = eb.batch AND pl.fk_product = ".((int) $fk_product);
220 }
221 $sql .= " WHERE fk_expeditiondet=".(int) $id_line_expdet;
222
223 dol_syslog(__METHOD__, LOG_DEBUG);
224 $resql = $this->db->query($sql);
225 if ($resql) {
226 $num = $this->db->num_rows($resql);
227 $i = 0;
228 $ret = array();
229 while ($i < $num) {
230 $obj = $this->db->fetch_object($resql);
231
232 $tmp = new self($this->db);
233 $tmp->sellby = $this->db->jdate(($fk_product > 0 && $obj->sellby) ? $obj->sellby : $obj->oldsellby);
234 $tmp->eatby = $this->db->jdate(($fk_product > 0 && $obj->eatby) ? $obj->eatby : $obj->oldeatby);
235 $tmp->batch = $obj->batch;
236 $tmp->id = $obj->rowid;
237 $tmp->fk_origin_stock = $obj->fk_origin_stock;
238 $tmp->fk_expeditiondet = $obj->fk_expeditiondet;
239 $tmp->fk_warehouse = $obj->fk_warehouse;
240 $tmp->dluo_qty = $obj->qty; // dluo_qty deprecated, use qty
241 $tmp->qty = $obj->qty;
242
243 $ret[] = $tmp;
244 $i++;
245 }
246
247 $this->db->free($resql);
248
249 return $ret;
250 } else {
251 dol_print_error($this->db);
252 return -1;
253 }
254 }
255}
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.