dolibarr  13.0.2
expeditionbatch.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 
36  private static $_table_element = 'expeditiondet_batch';
37 
38  public $sellby;
39  public $eatby;
40  public $batch;
41  public $qty;
42  public $dluo_qty; // deprecated, use qty
43  public $entrepot_id;
44  public $fk_origin_stock;
45  public $fk_expeditiondet;
46 
52  public function __construct($db)
53  {
54  $this->db = $db;
55  }
56 
63  public function fetchFromStock($id_stockdluo)
64  {
65  $sql = "SELECT";
66  $sql .= " pb.batch,";
67  $sql .= " pl.sellby,";
68  $sql .= " pl.eatby,";
69  $sql .= " ps.fk_entrepot";
70 
71  $sql .= " FROM ".MAIN_DB_PREFIX."product_batch as pb";
72  $sql .= " JOIN ".MAIN_DB_PREFIX."product_stock as ps on pb.fk_product_stock=ps.rowid";
73  $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX."product_lot as pl on pl.batch = pb.batch AND pl.fk_product = ps.fk_product";
74  $sql .= " WHERE pb.rowid = ".(int) $id_stockdluo;
75 
76  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
77  $resql = $this->db->query($sql);
78  if ($resql)
79  {
80  if ($this->db->num_rows($resql))
81  {
82  $obj = $this->db->fetch_object($resql);
83 
84  $this->sellby = $this->db->jdate($obj->sellby);
85  $this->eatby = $this->db->jdate($obj->eatby);
86  $this->batch = $obj->batch;
87  $this->entrepot_id = $obj->fk_entrepot;
88  $this->fk_origin_stock = (int) $id_stockdluo;
89  }
90  $this->db->free($resql);
91 
92  return 1;
93  } else {
94  $this->error = "Error ".$this->db->lasterror();
95  return -1;
96  }
97  }
98 
105  public function create($id_line_expdet)
106  {
107  $error = 0;
108 
109  $id_line_expdet = (int) $id_line_expdet;
110 
111  $sql = "INSERT INTO ".MAIN_DB_PREFIX.self::$_table_element." (";
112  $sql .= "fk_expeditiondet";
113  $sql .= ", sellby";
114  $sql .= ", eatby";
115  $sql .= ", batch";
116  $sql .= ", qty";
117  $sql .= ", fk_origin_stock";
118  $sql .= ") VALUES (";
119  $sql .= $id_line_expdet.",";
120  $sql .= " ".(!isset($this->sellby) || dol_strlen($this->sellby) == 0 ? 'NULL' : ("'".$this->db->idate($this->sellby))."'").",";
121  $sql .= " ".(!isset($this->eatby) || dol_strlen($this->eatby) == 0 ? 'NULL' : ("'".$this->db->idate($this->eatby))."'").",";
122  $sql .= " ".(!isset($this->batch) ? 'NULL' : ("'".$this->db->escape($this->batch)."'")).",";
123  $sql .= " ".(!isset($this->qty) ? ((!isset($this->dluo_qty)) ? 'NULL' : $this->dluo_qty) : $this->qty).","; // dluo_qty deprecated, use qty
124  $sql .= " ".(!isset($this->fk_origin_stock) ? 'NULL' : $this->fk_origin_stock);
125  $sql .= ")";
126 
127  dol_syslog(__METHOD__, LOG_DEBUG);
128  $resql = $this->db->query($sql);
129  if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }
130 
131  if (!$error)
132  {
133  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.self::$_table_element);
134  $this->fk_expeditiondet = $id_line_expdet;
135  return $this->id;
136  } else {
137  foreach ($this->errors as $errmsg)
138  {
139  dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
140  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
141  }
142  $this->db->rollback();
143  return -1 * $error;
144  }
145  }
146 
154  public static function deletefromexp($db, $id_expedition)
155  {
156  $id_expedition = (int) $id_expedition;
157 
158  $sql = "DELETE FROM ".MAIN_DB_PREFIX.self::$_table_element;
159  $sql .= " WHERE fk_expeditiondet in (SELECT rowid FROM ".MAIN_DB_PREFIX."expeditiondet WHERE fk_expedition=".$id_expedition.")";
160 
161  dol_syslog(__METHOD__, LOG_DEBUG);
162  if ($db->query($sql))
163  {
164  return 1;
165  } else {
166  return -1;
167  }
168  }
169 
178  public static function fetchAll($db, $id_line_expdet, $fk_product = 0)
179  {
180  $sql = "SELECT";
181  $sql .= " eb.rowid,";
182  $sql .= " eb.fk_expeditiondet,";
183  $sql .= " eb.sellby as oldsellby,"; // deprecated
184  $sql .= " eb.eatby as oldeatby,"; // deprecated
185  $sql .= " eb.batch,";
186  $sql .= " eb.qty,";
187  $sql .= " eb.fk_origin_stock";
188  if ($fk_product > 0)
189  {
190  $sql .= ", pl.sellby";
191  $sql .= ", pl.eatby";
192  }
193  $sql .= " FROM ".MAIN_DB_PREFIX.self::$_table_element." as eb";
194  if ($fk_product > 0)
195  {
196  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product_lot as pl ON pl.batch = eb.batch AND pl.fk_product = ".$fk_product;
197  }
198  $sql .= " WHERE fk_expeditiondet=".(int) $id_line_expdet;
199 
200  dol_syslog(__METHOD__."", LOG_DEBUG);
201  $resql = $db->query($sql);
202  if ($resql)
203  {
204  $num = $db->num_rows($resql);
205  $i = 0;
206  $ret = array();
207  while ($i < $num)
208  {
209  $tmp = new self($db);
210 
211  $obj = $db->fetch_object($resql);
212 
213  $tmp->sellby = $db->jdate($obj->sellby ? $obj->sellby : $obj->oldsellby);
214  $tmp->eatby = $db->jdate($obj->eatby ? $obj->eatby : $obj->oldeatby);
215  $tmp->batch = $obj->batch;
216  $tmp->id = $obj->rowid;
217  $tmp->fk_origin_stock = $obj->fk_origin_stock;
218  $tmp->fk_expeditiondet = $obj->fk_expeditiondet;
219  $tmp->dluo_qty = $obj->qty; // dluo_qty deprecated, use qty
220  $tmp->qty = $obj->qty;
221 
222  $ret[] = $tmp;
223  $i++;
224  }
225  $db->free($resql);
226  return $ret;
227  } else {
228  dol_print_error($db);
229  return -1;
230  }
231  }
232 }
CRUD class for batch number management within shipment.
static $_table_element
Name of table without prefix where object is stored.
create($id_line_expdet)
Create an expeditiondet_batch DB record link to an expedtiondet record.
static fetchAll($db, $id_line_expdet, $fk_product=0)
Retrieve all batch number detailed information of a shipment line.
$conf db
API class for accounts.
Definition: inc.php:54
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.
__construct($db)
Constructor.
fetchFromStock($id_stockdluo)
Fill object based on a product-warehouse-batch&#39;s record.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
static deletefromexp($db, $id_expedition)
Delete batch record attach to a shipment.
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)