expeditionlinebatch.class.php 7.4 KB

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