dolibarr  18.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  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_expeditiondet;
49 
50 
56  public function __construct($db)
57  {
58  $this->db = $db;
59  }
60 
67  public function fetchFromStock($id_stockdluo)
68  {
69  $sql = "SELECT";
70  $sql .= " pb.batch,";
71  $sql .= " pl.sellby,";
72  $sql .= " pl.eatby,";
73  $sql .= " ps.fk_entrepot";
74 
75  $sql .= " FROM ".MAIN_DB_PREFIX."product_batch as pb";
76  $sql .= " JOIN ".MAIN_DB_PREFIX."product_stock as ps on pb.fk_product_stock=ps.rowid";
77  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX."product_lot as pl on pl.batch = pb.batch AND pl.fk_product = ps.fk_product";
78  $sql .= " WHERE pb.rowid = ".(int) $id_stockdluo;
79 
80  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
81 
82  $resql = $this->db->query($sql);
83  if ($resql) {
84  if ($this->db->num_rows($resql)) {
85  $obj = $this->db->fetch_object($resql);
86 
87  $this->sellby = $this->db->jdate($obj->sellby);
88  $this->eatby = $this->db->jdate($obj->eatby);
89  $this->batch = $obj->batch;
90  $this->entrepot_id = $obj->fk_entrepot;
91  $this->fk_origin_stock = (int) $id_stockdluo;
92  }
93  $this->db->free($resql);
94 
95  return 1;
96  } else {
97  $this->error = "Error ".$this->db->lasterror();
98  return -1;
99  }
100  }
101 
110  public function create($id_line_expdet, $f_user = null, $notrigger = 0)
111  {
112  global $user;
113 
114  $error = 0;
115  if (!is_object($f_user)) $f_user = $user;
116 
117  $id_line_expdet = (int) $id_line_expdet;
118 
119  $sql = "INSERT INTO ".MAIN_DB_PREFIX.$this->table_element." (";
120  $sql .= "fk_expeditiondet";
121  $sql .= ", sellby";
122  $sql .= ", eatby";
123  $sql .= ", batch";
124  $sql .= ", qty";
125  $sql .= ", fk_origin_stock";
126  $sql .= ") VALUES (";
127  $sql .= $id_line_expdet.",";
128  $sql .= " ".(!isset($this->sellby) || dol_strlen($this->sellby) == 0 ? 'NULL' : ("'".$this->db->idate($this->sellby))."'").",";
129  $sql .= " ".(!isset($this->eatby) || dol_strlen($this->eatby) == 0 ? 'NULL' : ("'".$this->db->idate($this->eatby))."'").",";
130  $sql .= " ".(!isset($this->batch) ? 'NULL' : ("'".$this->db->escape($this->batch)."'")).",";
131  $sql .= " ".(!isset($this->qty) ? ((!isset($this->dluo_qty)) ? 'NULL' : $this->dluo_qty) : $this->qty).","; // dluo_qty deprecated, use qty
132  $sql .= " ".(!isset($this->fk_origin_stock) ? 'NULL' : $this->fk_origin_stock);
133  $sql .= ")";
134 
135  dol_syslog(__METHOD__, LOG_DEBUG);
136  $resql = $this->db->query($sql);
137  if (!$resql) {
138  $error++; $this->errors[] = "Error ".$this->db->lasterror();
139  }
140 
141  if (!$error) {
142  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
143 
144  $this->fk_expeditiondet = $id_line_expdet;
145  }
146 
147  if (!$error && !$notrigger) {
148  // Call trigger
149  $result = $this->call_trigger('EXPEDITIONLINEBATCH_CREATE', $f_user);
150  if ($result < 0) {
151  $error++;
152  }
153  // End call triggers
154  }
155 
156  if (!$error) {
157  return $this->id;
158  } else {
159  foreach ($this->errors as $errmsg) {
160  dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
161  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
162  }
163  return -1 * $error;
164  }
165  }
166 
173  public function deleteFromShipment($id_expedition)
174  {
175  $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
176  $sql .= " WHERE fk_expeditiondet in (SELECT rowid FROM ".MAIN_DB_PREFIX."expeditiondet WHERE fk_expedition=".((int) $id_expedition).")";
177 
178  dol_syslog(__METHOD__, LOG_DEBUG);
179  if ($this->db->query($sql)) {
180  return 1;
181  } else {
182  return -1;
183  }
184  }
185 
193  public function fetchAll($id_line_expdet, $fk_product = 0)
194  {
195  $sql = "SELECT";
196  $sql .= " eb.rowid,";
197  $sql .= " eb.fk_expeditiondet,";
198  $sql .= " eb.sellby as oldsellby,"; // deprecated
199  $sql .= " eb.eatby as oldeatby,"; // deprecated
200  $sql .= " eb.batch,";
201  $sql .= " eb.qty,";
202  $sql .= " eb.fk_origin_stock";
203  if ($fk_product > 0) {
204  $sql .= ", pl.sellby";
205  $sql .= ", pl.eatby";
206  }
207  $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." as eb";
208  if ($fk_product > 0) {
209  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product_lot as pl ON pl.batch = eb.batch AND pl.fk_product = ".((int) $fk_product);
210  }
211  $sql .= " WHERE fk_expeditiondet=".(int) $id_line_expdet;
212 
213  dol_syslog(__METHOD__, LOG_DEBUG);
214  $resql = $this->db->query($sql);
215  if ($resql) {
216  $num = $this->db->num_rows($resql);
217  $i = 0;
218  $ret = array();
219  while ($i < $num) {
220  $obj = $this->db->fetch_object($resql);
221 
222  $tmp = new self($this->db);
223  $tmp->sellby = $this->db->jdate($obj->sellby ? $obj->sellby : $obj->oldsellby);
224  $tmp->eatby = $this->db->jdate($obj->eatby ? $obj->eatby : $obj->oldeatby);
225  $tmp->batch = $obj->batch;
226  $tmp->id = $obj->rowid;
227  $tmp->fk_origin_stock = $obj->fk_origin_stock;
228  $tmp->fk_expeditiondet = $obj->fk_expeditiondet;
229  $tmp->dluo_qty = $obj->qty; // dluo_qty deprecated, use qty
230  $tmp->qty = $obj->qty;
231 
232  $ret[] = $tmp;
233  $i++;
234  }
235 
236  $this->db->free($resql);
237 
238  return $ret;
239  } else {
240  dol_print_error($this->db);
241  return -1;
242  }
243  }
244 }
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.
if(isModEnabled('facture') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $sql
Social contributions to pay.
Definition: index.php:746
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.