expeditionlinebatch.class.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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
  44. public $fk_expeditiondet;
  45. /**
  46. * Constructor
  47. *
  48. * @param DoliDb $db Database handler
  49. */
  50. public function __construct($db)
  51. {
  52. $this->db = $db;
  53. }
  54. /**
  55. * Fill object based on a product-warehouse-batch's record
  56. *
  57. * @param int $id_stockdluo Rowid in product_batch table
  58. * @return int -1 if KO, 1 if OK
  59. */
  60. public function fetchFromStock($id_stockdluo)
  61. {
  62. $sql = "SELECT";
  63. $sql .= " pb.batch,";
  64. $sql .= " pl.sellby,";
  65. $sql .= " pl.eatby,";
  66. $sql .= " ps.fk_entrepot";
  67. $sql .= " FROM ".MAIN_DB_PREFIX."product_batch as pb";
  68. $sql .= " JOIN ".MAIN_DB_PREFIX."product_stock as ps on pb.fk_product_stock=ps.rowid";
  69. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX."product_lot as pl on pl.batch = pb.batch AND pl.fk_product = ps.fk_product";
  70. $sql .= " WHERE pb.rowid = ".(int) $id_stockdluo;
  71. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  72. $resql = $this->db->query($sql);
  73. if ($resql) {
  74. if ($this->db->num_rows($resql)) {
  75. $obj = $this->db->fetch_object($resql);
  76. $this->sellby = $this->db->jdate($obj->sellby);
  77. $this->eatby = $this->db->jdate($obj->eatby);
  78. $this->batch = $obj->batch;
  79. $this->entrepot_id = $obj->fk_entrepot;
  80. $this->fk_origin_stock = (int) $id_stockdluo;
  81. }
  82. $this->db->free($resql);
  83. return 1;
  84. } else {
  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. public function create($id_line_expdet)
  96. {
  97. $error = 0;
  98. $id_line_expdet = (int) $id_line_expdet;
  99. $sql = "INSERT INTO ".MAIN_DB_PREFIX.$this->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->qty) ? ((!isset($this->dluo_qty)) ? 'NULL' : $this->dluo_qty) : $this->qty).","; // dluo_qty deprecated, use 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) {
  117. $error++; $this->errors[] = "Error ".$this->db->lasterror();
  118. }
  119. if (!$error) {
  120. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
  121. $this->fk_expeditiondet = $id_line_expdet;
  122. return $this->id;
  123. } else {
  124. foreach ($this->errors as $errmsg) {
  125. dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
  126. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  127. }
  128. return -1 * $error;
  129. }
  130. }
  131. /**
  132. * Delete batch record attach to a shipment
  133. *
  134. * @param int $id_expedition rowid of shipment
  135. * @return int -1 if KO, 1 if OK
  136. */
  137. public function deleteFromShipment($id_expedition)
  138. {
  139. $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
  140. $sql .= " WHERE fk_expeditiondet in (SELECT rowid FROM ".MAIN_DB_PREFIX."expeditiondet WHERE fk_expedition=".((int) $id_expedition).")";
  141. dol_syslog(__METHOD__, LOG_DEBUG);
  142. if ($this->db->query($sql)) {
  143. return 1;
  144. } else {
  145. return -1;
  146. }
  147. }
  148. /**
  149. * Retrieve all batch number detailed information of a shipment line
  150. *
  151. * @param int $id_line_expdet id of shipment line
  152. * @param int $fk_product If provided, load also detailed information of lot
  153. * @return int|array -1 if KO, array of ExpeditionLineBatch if OK
  154. */
  155. public function fetchAll($id_line_expdet, $fk_product = 0)
  156. {
  157. $sql = "SELECT";
  158. $sql .= " eb.rowid,";
  159. $sql .= " eb.fk_expeditiondet,";
  160. $sql .= " eb.sellby as oldsellby,"; // deprecated
  161. $sql .= " eb.eatby as oldeatby,"; // deprecated
  162. $sql .= " eb.batch,";
  163. $sql .= " eb.qty,";
  164. $sql .= " eb.fk_origin_stock";
  165. if ($fk_product > 0) {
  166. $sql .= ", pl.sellby";
  167. $sql .= ", pl.eatby";
  168. }
  169. $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." as eb";
  170. if ($fk_product > 0) {
  171. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product_lot as pl ON pl.batch = eb.batch AND pl.fk_product = ".((int) $fk_product);
  172. }
  173. $sql .= " WHERE fk_expeditiondet=".(int) $id_line_expdet;
  174. dol_syslog(__METHOD__."", LOG_DEBUG);
  175. $resql = $this->db->query($sql);
  176. if ($resql) {
  177. $num = $this->db->num_rows($resql);
  178. $i = 0;
  179. $ret = array();
  180. while ($i < $num) {
  181. $obj = $this->db->fetch_object($resql);
  182. $tmp = new self($this->db);
  183. $tmp->sellby = $this->db->jdate($obj->sellby ? $obj->sellby : $obj->oldsellby);
  184. $tmp->eatby = $this->db->jdate($obj->eatby ? $obj->eatby : $obj->oldeatby);
  185. $tmp->batch = $obj->batch;
  186. $tmp->id = $obj->rowid;
  187. $tmp->fk_origin_stock = $obj->fk_origin_stock;
  188. $tmp->fk_expeditiondet = $obj->fk_expeditiondet;
  189. $tmp->dluo_qty = $obj->qty; // dluo_qty deprecated, use qty
  190. $tmp->qty = $obj->qty;
  191. $ret[] = $tmp;
  192. $i++;
  193. }
  194. $this->db->free($resql);
  195. return $ret;
  196. } else {
  197. dol_print_error($this->db);
  198. return -1;
  199. }
  200. }
  201. }