dolibarr  9.0.0
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 <http://www.gnu.org/licenses/>.
17  */
18 
30 {
34  public $element='expeditionlignebatch';
35 
36  private static $_table_element='expeditiondet_batch';
37 
38  var $sellby;
39  var $eatby;
40  var $batch;
41  var $qty;
42  var $dluo_qty; // deprecated, use qty
43  var $entrepot_id;
44  var $fk_origin_stock;
45  var $fk_expeditiondet;
46 
52  function __construct($db)
53  {
54  $this->db = $db;
55  }
56 
63  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  }
94  else
95  {
96  $this->error="Error ".$this->db->lasterror();
97  return -1;
98  }
99  }
100 
107  function create($id_line_expdet)
108  {
109  $error = 0;
110 
111  $id_line_expdet = (int) $id_line_expdet;
112 
113  $sql = "INSERT INTO ".MAIN_DB_PREFIX.self::$_table_element." (";
114  $sql.= "fk_expeditiondet";
115  $sql.= ", sellby";
116  $sql.= ", eatby";
117  $sql.= ", batch";
118  $sql.= ", qty";
119  $sql.= ", fk_origin_stock";
120  $sql.= ") VALUES (";
121  $sql.= $id_line_expdet.",";
122  $sql.= " ".(! isset($this->sellby) || dol_strlen($this->sellby)==0?'NULL':("'".$this->db->idate($this->sellby))."'").",";
123  $sql.= " ".(! isset($this->eatby) || dol_strlen($this->eatby)==0?'NULL':("'".$this->db->idate($this->eatby))."'").",";
124  $sql.= " ".(! isset($this->batch)?'NULL':("'".$this->db->escape($this->batch)."'")).",";
125  $sql.= " ".(! isset($this->qty)?((! isset($this->dluo_qty))?'NULL':$this->dluo_qty):$this->qty).","; // dluo_qty deprecated, use qty
126  $sql.= " ".(! isset($this->fk_origin_stock)?'NULL':$this->fk_origin_stock);
127  $sql.= ")";
128 
129  dol_syslog(__METHOD__, LOG_DEBUG);
130  $resql=$this->db->query($sql);
131  if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
132 
133  if (! $error)
134  {
135  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.self::$_table_element);
136  $this->fk_expeditiondet=$id_line_expdet;
137  return $this->id;
138  }
139  else
140  {
141  foreach($this->errors as $errmsg)
142  {
143  dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
144  $this->error.=($this->error?', '.$errmsg:$errmsg);
145  }
146  $this->db->rollback();
147  return -1*$error;
148  }
149  }
150 
158  static function deletefromexp($db,$id_expedition)
159  {
160  $id_expedition = (int) $id_expedition;
161 
162  $sql="DELETE FROM ".MAIN_DB_PREFIX.self::$_table_element;
163  $sql.=" WHERE fk_expeditiondet in (SELECT rowid FROM ".MAIN_DB_PREFIX."expeditiondet WHERE fk_expedition=".$id_expedition.")";
164 
165  dol_syslog(__METHOD__, LOG_DEBUG);
166  if ($db->query($sql))
167  {
168  return 1;
169  }
170  else
171  {
172  return -1;
173  }
174  }
175 
184  static function fetchAll($db, $id_line_expdet, $fk_product=0)
185  {
186  $sql="SELECT";
187  $sql.= " eb.rowid,";
188  $sql.= " eb.fk_expeditiondet,";
189  $sql.= " eb.sellby as oldsellby,"; // deprecated
190  $sql.= " eb.eatby as oldeatby,"; // deprecated
191  $sql.= " eb.batch,";
192  $sql.= " eb.qty,";
193  $sql.= " eb.fk_origin_stock";
194  if ($fk_product > 0)
195  {
196  $sql.= ", pl.sellby";
197  $sql.= ", pl.eatby";
198  }
199  $sql.= " FROM ".MAIN_DB_PREFIX.self::$_table_element." as eb";
200  if ($fk_product > 0)
201  {
202  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product_lot as pl ON pl.batch = eb.batch AND pl.fk_product = ".$fk_product;
203  }
204  $sql.= " WHERE fk_expeditiondet=".(int) $id_line_expdet;
205 
206  dol_syslog(__METHOD__ ."", LOG_DEBUG);
207  $resql=$db->query($sql);
208  if ($resql)
209  {
210  $num=$db->num_rows($resql);
211  $i=0;
212  $ret = array();
213  while ($i<$num)
214  {
215  $tmp=new self($db);
216 
217  $obj = $db->fetch_object($resql);
218 
219  $tmp->sellby = $db->jdate($obj->sellby ? $obj->sellby : $obj->oldsellby);
220  $tmp->eatby = $db->jdate($obj->eatby ? $obj->eatby : $obj->oldeatby);
221  $tmp->batch = $obj->batch;
222  $tmp->id = $obj->rowid;
223  $tmp->fk_origin_stock = $obj->fk_origin_stock;
224  $tmp->fk_expeditiondet = $obj->fk_expeditiondet;
225  $tmp->dluo_qty = $obj->qty; // dluo_qty deprecated, use qty
226  $tmp->qty = $obj->qty;
227 
228  $ret[]=$tmp;
229  $i++;
230  }
231  $db->free($resql);
232  return $ret;
233  }
234  else
235  {
236  dol_print_error($db);
237  return -1;
238  }
239  }
240 }
if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) if(! empty($conf->don->enabled) && $user->rights->societe->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) && $user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1053
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.
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
static fetchAll($db, $id_line_expdet, $fk_product=0)
Retrieve all batch number detailed information of a shipment line.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
__construct($db)
Constructor.
fetchFromStock($id_stockdluo)
Fill object based on a product-warehouse-batch&#39;s record.
static deletefromexp($db, $id_expedition)
Delete batch record attach to a shipment.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)