dolibarr 18.0.6
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 public $qty;
45 public $dluo_qty; // deprecated, use qty
46 public $entrepot_id;
47 public $fk_origin_stock; // rowid in llx_product_batch table
48 public $fk_warehouse; // for future use in v19
49 public $fk_expeditiondet;
50
51
57 public function __construct($db)
58 {
59 $this->db = $db;
60 }
61
68 public function fetchFromStock($id_stockdluo)
69 {
70 $sql = "SELECT";
71 $sql .= " pb.batch,";
72 $sql .= " pl.sellby,";
73 $sql .= " pl.eatby,";
74 $sql .= " ps.fk_entrepot";
75
76 $sql .= " FROM ".MAIN_DB_PREFIX."product_batch as pb";
77 $sql .= " JOIN ".MAIN_DB_PREFIX."product_stock as ps on pb.fk_product_stock=ps.rowid";
78 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX."product_lot as pl on pl.batch = pb.batch AND pl.fk_product = ps.fk_product";
79 $sql .= " WHERE pb.rowid = ".(int) $id_stockdluo;
80
81 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
82
83 $resql = $this->db->query($sql);
84 if ($resql) {
85 if ($this->db->num_rows($resql)) {
86 $obj = $this->db->fetch_object($resql);
87
88 $this->sellby = $this->db->jdate($obj->sellby);
89 $this->eatby = $this->db->jdate($obj->eatby);
90 $this->batch = $obj->batch;
91 $this->entrepot_id = $obj->fk_entrepot;
92 $this->fk_origin_stock = (int) $id_stockdluo;
93 }
94 $this->db->free($resql);
95
96 return 1;
97 } else {
98 $this->error = "Error ".$this->db->lasterror();
99 return -1;
100 }
101 }
102
111 public function create($id_line_expdet, $f_user = null, $notrigger = 0)
112 {
113 global $user;
114
115 $error = 0;
116 if (!is_object($f_user)) $f_user = $user;
117
118 $id_line_expdet = (int) $id_line_expdet;
119
120 $sql = "INSERT INTO ".MAIN_DB_PREFIX.$this->table_element." (";
121 $sql .= "fk_expeditiondet";
122 $sql .= ", sellby";
123 $sql .= ", eatby";
124 $sql .= ", batch";
125 $sql .= ", qty";
126 $sql .= ", fk_origin_stock";
127 // TODO Add fk_warehouse here
128 $sql .= ") VALUES (";
129 $sql .= $id_line_expdet.",";
130 $sql .= " ".(!isset($this->sellby) || dol_strlen($this->sellby) == 0 ? 'NULL' : ("'".$this->db->idate($this->sellby))."'").",";
131 $sql .= " ".(!isset($this->eatby) || dol_strlen($this->eatby) == 0 ? 'NULL' : ("'".$this->db->idate($this->eatby))."'").",";
132 $sql .= " ".(!isset($this->batch) ? 'NULL' : ("'".$this->db->escape($this->batch)."'")).",";
133 $sql .= " ".(!isset($this->qty) ? ((!isset($this->dluo_qty)) ? 'NULL' : $this->dluo_qty) : $this->qty).","; // dluo_qty deprecated, use qty
134 $sql .= " ".(!isset($this->fk_origin_stock) ? 'NULL' : $this->fk_origin_stock);
135 $sql .= ")";
136
137 dol_syslog(__METHOD__, LOG_DEBUG);
138 $resql = $this->db->query($sql);
139 if (!$resql) {
140 $error++; $this->errors[] = "Error ".$this->db->lasterror();
141 }
142
143 if (!$error) {
144 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
145
146 $this->fk_expeditiondet = $id_line_expdet;
147 }
148
149 if (!$error && !$notrigger) {
150 // Call trigger
151 $result = $this->call_trigger('EXPEDITIONLINEBATCH_CREATE', $f_user);
152 if ($result < 0) {
153 $error++;
154 }
155 // End call triggers
156 }
157
158 if (!$error) {
159 return $this->id;
160 } else {
161 foreach ($this->errors as $errmsg) {
162 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
163 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
164 }
165 return -1 * $error;
166 }
167 }
168
175 public function deleteFromShipment($id_expedition)
176 {
177 $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
178 $sql .= " WHERE fk_expeditiondet in (SELECT rowid FROM ".MAIN_DB_PREFIX."expeditiondet WHERE fk_expedition=".((int) $id_expedition).")";
179
180 dol_syslog(__METHOD__, LOG_DEBUG);
181 if ($this->db->query($sql)) {
182 return 1;
183 } else {
184 return -1;
185 }
186 }
187
195 public function fetchAll($id_line_expdet, $fk_product = 0)
196 {
197 $sql = "SELECT";
198 $sql .= " eb.rowid,";
199 $sql .= " eb.fk_expeditiondet,";
200 $sql .= " eb.sellby as oldsellby,"; // deprecated
201 $sql .= " eb.eatby as oldeatby,"; // deprecated
202 $sql .= " eb.batch,";
203 $sql .= " eb.qty,";
204 $sql .= " eb.fk_origin_stock";
205 if ($fk_product > 0) {
206 $sql .= ", pl.sellby";
207 $sql .= ", pl.eatby";
208 }
209 $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." as eb";
210 if ($fk_product > 0) {
211 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product_lot as pl ON pl.batch = eb.batch AND pl.fk_product = ".((int) $fk_product);
212 }
213 $sql .= " WHERE fk_expeditiondet=".(int) $id_line_expdet;
214
215 dol_syslog(__METHOD__, LOG_DEBUG);
216 $resql = $this->db->query($sql);
217 if ($resql) {
218 $num = $this->db->num_rows($resql);
219 $i = 0;
220 $ret = array();
221 while ($i < $num) {
222 $obj = $this->db->fetch_object($resql);
223
224 $tmp = new self($this->db);
225 $tmp->sellby = $this->db->jdate($obj->sellby ? $obj->sellby : $obj->oldsellby);
226 $tmp->eatby = $this->db->jdate($obj->eatby ? $obj->eatby : $obj->oldeatby);
227 $tmp->batch = $obj->batch;
228 $tmp->id = $obj->rowid;
229 $tmp->fk_origin_stock = $obj->fk_origin_stock;
230 $tmp->fk_expeditiondet = $obj->fk_expeditiondet;
231 $tmp->dluo_qty = $obj->qty; // dluo_qty deprecated, use qty
232 $tmp->qty = $obj->qty;
233
234 $ret[] = $tmp;
235 $i++;
236 }
237
238 $this->db->free($resql);
239
240 return $ret;
241 } else {
242 dol_print_error($this->db);
243 return -1;
244 }
245 }
246}
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_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.