expeditionbatch.class.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. /**
  19. * \file expedition/class/expeditionbatch.class.php
  20. * \ingroup productbatch
  21. * \brief This file implements CRUD method for managing shipment batch lines
  22. * with batch record
  23. */
  24. /**
  25. * CRUD class for batch number management within shipment
  26. */
  27. class ExpeditionLineBatch extends CommonObject
  28. {
  29. /**
  30. * @var string ID to identify managed object
  31. */
  32. public $element='expeditionlignebatch';
  33. private static $_table_element='expeditiondet_batch'; //!< Name of table without prefix where object is stored
  34. var $sellby;
  35. var $eatby;
  36. var $batch;
  37. var $dluo_qty;
  38. var $entrepot_id;
  39. var $fk_origin_stock;
  40. var $fk_expeditiondet;
  41. /**
  42. * Constructor
  43. *
  44. * @param DoliDb $db Database handler
  45. */
  46. function __construct($db)
  47. {
  48. $this->db = $db;
  49. }
  50. /**
  51. * Fill object based on a product-warehouse-batch's record
  52. *
  53. * @param int $id_stockdluo Rowid in product_batch table
  54. * @return int -1 if KO, 1 if OK
  55. */
  56. function fetchFromStock($id_stockdluo)
  57. {
  58. $sql = "SELECT";
  59. $sql.= " pb.batch,";
  60. $sql.= " pl.sellby,";
  61. $sql.= " pl.eatby,";
  62. $sql.= " ps.fk_entrepot";
  63. $sql.= " FROM ".MAIN_DB_PREFIX."product_batch as pb";
  64. $sql.= " JOIN ".MAIN_DB_PREFIX."product_stock as ps on pb.fk_product_stock=ps.rowid";
  65. $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX."product_lot as pl on pl.batch = pb.batch AND pl.fk_product = ps.fk_product";
  66. $sql.= " WHERE pb.rowid = ".(int) $id_stockdluo;
  67. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  68. $resql=$this->db->query($sql);
  69. if ($resql)
  70. {
  71. if ($this->db->num_rows($resql))
  72. {
  73. $obj = $this->db->fetch_object($resql);
  74. $this->sellby = $this->db->jdate($obj->sellby);
  75. $this->eatby = $this->db->jdate($obj->eatby);
  76. $this->batch = $obj->batch;
  77. $this->entrepot_id= $obj->fk_entrepot;
  78. $this->fk_origin_stock=(int) $id_stockdluo;
  79. }
  80. $this->db->free($resql);
  81. return 1;
  82. }
  83. else
  84. {
  85. $this->error="Error ".$this->db->lasterror();
  86. return -1;
  87. }
  88. }
  89. /**
  90. * Create an expeditiondet_batch DB record link to an expedtiondet record
  91. *
  92. * @param int $id_line_expdet rowid of expedtiondet record
  93. * @return int <0 if KO, Id of record (>0) if OK
  94. */
  95. function create($id_line_expdet)
  96. {
  97. $error = 0;
  98. $id_line_expdet = (int) $id_line_expdet;
  99. $sql = "INSERT INTO ".MAIN_DB_PREFIX.self::$_table_element." (";
  100. $sql.= "fk_expeditiondet";
  101. $sql.= ", sellby";
  102. $sql.= ", eatby";
  103. $sql.= ", batch";
  104. $sql.= ", qty";
  105. $sql.= ", fk_origin_stock";
  106. $sql.= ") VALUES (";
  107. $sql.= $id_line_expdet.",";
  108. $sql.= " ".(! isset($this->sellby) || dol_strlen($this->sellby)==0?'NULL':("'".$this->db->idate($this->sellby))."'").",";
  109. $sql.= " ".(! isset($this->eatby) || dol_strlen($this->eatby)==0?'NULL':("'".$this->db->idate($this->eatby))."'").",";
  110. $sql.= " ".(! isset($this->batch)?'NULL':("'".$this->db->escape($this->batch)."'")).",";
  111. $sql.= " ".(! isset($this->dluo_qty)?'NULL':$this->dluo_qty).",";
  112. $sql.= " ".(! isset($this->fk_origin_stock)?'NULL':$this->fk_origin_stock);
  113. $sql.= ")";
  114. dol_syslog(__METHOD__, LOG_DEBUG);
  115. $resql=$this->db->query($sql);
  116. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  117. if (! $error)
  118. {
  119. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.self::$_table_element);
  120. $this->fk_expeditiondet=$id_line_expdet;
  121. return $this->id;
  122. }
  123. else
  124. {
  125. foreach($this->errors as $errmsg)
  126. {
  127. dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
  128. $this->error.=($this->error?', '.$errmsg:$errmsg);
  129. }
  130. $this->db->rollback();
  131. return -1*$error;
  132. }
  133. }
  134. /**
  135. * Delete batch record attach to a shipment
  136. *
  137. * @param DoliDB $db Database object
  138. * @param int $id_expedition rowid of shipment
  139. * @return int -1 if KO, 1 if OK
  140. */
  141. static function deletefromexp($db,$id_expedition)
  142. {
  143. $id_expedition = (int) $id_expedition;
  144. $sql="DELETE FROM ".MAIN_DB_PREFIX.self::$_table_element;
  145. $sql.=" WHERE fk_expeditiondet in (SELECT rowid FROM ".MAIN_DB_PREFIX."expeditiondet WHERE fk_expedition=".$id_expedition.")";
  146. dol_syslog(__METHOD__, LOG_DEBUG);
  147. if ($db->query($sql))
  148. {
  149. return 1;
  150. }
  151. else
  152. {
  153. return -1;
  154. }
  155. }
  156. /**
  157. * Retrieve all batch number detailed information of a shipment line
  158. *
  159. * @param DoliDB $db Database object
  160. * @param int $id_line_expdet id of shipment line
  161. * @param int $fk_product If provided, load also detailed information of lot
  162. * @return int|array -1 if KO, array of ExpeditionLineBatch if OK
  163. */
  164. static function fetchAll($db, $id_line_expdet, $fk_product=0)
  165. {
  166. $sql="SELECT";
  167. $sql.= " eb.rowid,";
  168. $sql.= " eb.fk_expeditiondet,";
  169. $sql.= " eb.sellby as oldsellby,"; // deprecated
  170. $sql.= " eb.eatby as oldeatby,"; // deprecated
  171. $sql.= " eb.batch,";
  172. $sql.= " eb.qty,";
  173. $sql.= " eb.fk_origin_stock";
  174. if ($fk_product > 0)
  175. {
  176. $sql.= ", pl.sellby";
  177. $sql.= ", pl.eatby";
  178. }
  179. $sql.= " FROM ".MAIN_DB_PREFIX.self::$_table_element." as eb";
  180. if ($fk_product > 0)
  181. {
  182. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product_lot as pl ON pl.batch = eb.batch AND pl.fk_product = ".$fk_product;
  183. }
  184. $sql.= " WHERE fk_expeditiondet=".(int) $id_line_expdet;
  185. dol_syslog(__METHOD__ ."", LOG_DEBUG);
  186. $resql=$db->query($sql);
  187. if ($resql)
  188. {
  189. $num=$db->num_rows($resql);
  190. $i=0;
  191. $ret = array();
  192. while ($i<$num)
  193. {
  194. $tmp=new self($db);
  195. $obj = $db->fetch_object($resql);
  196. $tmp->sellby = $db->jdate($obj->sellby ? $obj->sellby : $obj->oldsellby);
  197. $tmp->eatby = $db->jdate($obj->eatby ? $obj->eatby : $obj->oldeatby);
  198. $tmp->batch = $obj->batch;
  199. $tmp->id = $obj->rowid;
  200. $tmp->fk_origin_stock = $obj->fk_origin_stock;
  201. $tmp->fk_expeditiondet = $obj->fk_expeditiondet;
  202. $tmp->dluo_qty = $obj->qty;
  203. $ret[]=$tmp;
  204. $i++;
  205. }
  206. $db->free($resql);
  207. return $ret;
  208. }
  209. else
  210. {
  211. dol_print_error($db);
  212. return -1;
  213. }
  214. }
  215. }