productbatch.class.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. <?php
  2. /* Copyright (C) 2007-2023 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2013-2014 Cedric GROSS <c.gross@kreiz-it.fr>
  4. * Copyright (C) 2024 Ferran Marcet <fmarcet@2byte.es>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file product/class/productbatch.class.php
  21. * \ingroup productbatch
  22. * \brief Manage record and specific data for batch number management.
  23. * Manage table llx_product_batch (should have been named product_stock_batch)
  24. */
  25. require_once DOL_DOCUMENT_ROOT."/core/class/commonobject.class.php";
  26. /**
  27. * Manage record for batch number management
  28. */
  29. class Productbatch extends CommonObject
  30. {
  31. /**
  32. * Batches rules
  33. */
  34. const BATCH_RULE_SELLBY_EATBY_DATES_FIRST = 1;
  35. /**
  36. * @var string ID to identify managed object
  37. */
  38. public $element = 'productbatch';
  39. private static $_table_element = 'product_batch'; //!< Name of table without prefix where object is stored
  40. public $tms = '';
  41. public $fk_product_stock;
  42. public $batch = '';
  43. public $qty;
  44. public $warehouseid;
  45. /**
  46. * @var int ID
  47. */
  48. public $fk_product;
  49. // Properties of the lot
  50. public $lotid; // ID in table of the details of properties of each lots
  51. /**
  52. * @var int|string
  53. * @deprecated
  54. */
  55. public $sellby = ''; // dlc
  56. /**
  57. * @var int|string
  58. * @deprecated
  59. */
  60. public $eatby = ''; // dmd/dluo
  61. /**
  62. * Constructor
  63. *
  64. * @param DoliDb $db Database handler
  65. */
  66. public function __construct($db)
  67. {
  68. $this->db = $db;
  69. }
  70. /**
  71. * Create object into database
  72. *
  73. * @param User $user User that creates
  74. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  75. * @return int Return integer <0 if KO, Id of created object if OK
  76. */
  77. public function create($user, $notrigger = 0)
  78. {
  79. $error = 0;
  80. // Clean parameters
  81. $this->cleanParam();
  82. // Check parameters
  83. // Put here code to add control on parameters values
  84. // Insert request
  85. $sql = "INSERT INTO ".$this->db->prefix()."product_batch (";
  86. $sql .= "fk_product_stock,";
  87. $sql .= "sellby,"; // no more used
  88. $sql .= "eatby,"; // no more used
  89. $sql .= "batch,";
  90. $sql .= "qty,";
  91. $sql .= "import_key";
  92. $sql .= ") VALUES (";
  93. $sql .= " ".(!isset($this->fk_product_stock) ? 'NULL' : $this->fk_product_stock).",";
  94. $sql .= " ".(!isset($this->sellby) || dol_strlen($this->sellby) == 0 ? 'NULL' : "'".$this->db->idate($this->sellby)."'").","; // no more used
  95. $sql .= " ".(!isset($this->eatby) || dol_strlen($this->eatby) == 0 ? 'NULL' : "'".$this->db->idate($this->eatby)."'").","; // no more used
  96. $sql .= " ".(!isset($this->batch) ? 'NULL' : "'".$this->db->escape($this->batch)."'").",";
  97. $sql .= " ".(!isset($this->qty) ? 'NULL' : $this->qty).",";
  98. $sql .= " ".(!isset($this->import_key) ? 'NULL' : "'".$this->db->escape($this->import_key)."'");
  99. $sql .= ")";
  100. $this->db->begin();
  101. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  102. $resql = $this->db->query($sql);
  103. if (!$resql) {
  104. $error++;
  105. $this->errors[] = "Error ".$this->db->lasterror();
  106. }
  107. if (!$error) {
  108. $this->id = $this->db->last_insert_id($this->db->prefix().self::$_table_element);
  109. }
  110. // Commit or rollback
  111. if ($error) {
  112. $this->db->rollback();
  113. return -1 * $error;
  114. } else {
  115. $this->db->commit();
  116. return $this->id;
  117. }
  118. }
  119. /**
  120. * Load object in memory from the database
  121. *
  122. * @param int $id Id object
  123. * @return int Return integer <0 if KO, >0 if OK
  124. */
  125. public function fetch($id)
  126. {
  127. $sql = "SELECT";
  128. $sql .= " t.rowid,";
  129. $sql .= " t.tms,";
  130. $sql .= " t.fk_product_stock,";
  131. $sql .= " t.sellby as oldsellby,";
  132. $sql .= " t.eatby as oldeatby,";
  133. $sql .= " t.batch,";
  134. $sql .= " t.qty,";
  135. $sql .= " t.import_key,";
  136. $sql .= " w.fk_entrepot,";
  137. $sql .= " w.fk_product,";
  138. $sql .= " pl.eatby,";
  139. $sql .= " pl.sellby";
  140. $sql .= " FROM ".$this->db->prefix()."product_batch as t";
  141. $sql .= " INNER JOIN ".$this->db->prefix()."product_stock w on t.fk_product_stock = w.rowid"; // llx_product_stock is a parent table so this link does NOT generate duplicate record
  142. $sql .= " LEFT JOIN ".$this->db->prefix()."product_lot as pl on pl.fk_product = w.fk_product and pl.batch = t.batch";
  143. $sql .= " WHERE t.rowid = ".((int) $id);
  144. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  145. $resql = $this->db->query($sql);
  146. if ($resql) {
  147. if ($this->db->num_rows($resql)) {
  148. $obj = $this->db->fetch_object($resql);
  149. $this->id = $obj->rowid;
  150. $this->tms = $this->db->jdate($obj->tms);
  151. $this->fk_product_stock = $obj->fk_product_stock;
  152. $this->sellby = $this->db->jdate($obj->sellby ? $obj->sellby : $obj->oldsellby);
  153. $this->eatby = $this->db->jdate($obj->eatby ? $obj->eatby : $obj->oldeatby);
  154. $this->batch = $obj->batch;
  155. $this->qty = $obj->qty;
  156. $this->import_key = $obj->import_key;
  157. $this->warehouseid = $obj->fk_entrepot;
  158. $this->fk_product = $obj->fk_product;
  159. }
  160. $this->db->free($resql);
  161. return 1;
  162. } else {
  163. $this->error = "Error ".$this->db->lasterror();
  164. return -1;
  165. }
  166. }
  167. /**
  168. * Update object into database
  169. *
  170. * @param User $user User that modifies
  171. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  172. * @return int Return integer <0 if KO, >0 if OK
  173. */
  174. public function update($user = null, $notrigger = 0)
  175. {
  176. $error = 0;
  177. // Clean parameters
  178. $this->cleanParam();
  179. // TODO Check qty is ok for stock move. Negative may not be allowed.
  180. if ($this->qty < 0) {
  181. }
  182. // Update request
  183. $sql = "UPDATE ".$this->db->prefix().self::$_table_element." SET";
  184. $sql .= " fk_product_stock=".(isset($this->fk_product_stock) ? $this->fk_product_stock : "null").",";
  185. $sql .= " sellby=".(dol_strlen($this->sellby) != 0 ? "'".$this->db->idate($this->sellby)."'" : 'null').",";
  186. $sql .= " eatby=".(dol_strlen($this->eatby) != 0 ? "'".$this->db->idate($this->eatby)."'" : 'null').",";
  187. $sql .= " batch=".(isset($this->batch) ? "'".$this->db->escape($this->batch)."'" : "null").",";
  188. $sql .= " qty=".(isset($this->qty) ? $this->qty : "null").",";
  189. $sql .= " import_key=".(isset($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null");
  190. $sql .= " WHERE rowid=".((int) $this->id);
  191. $this->db->begin();
  192. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  193. $resql = $this->db->query($sql);
  194. if (!$resql) {
  195. $error++;
  196. $this->errors[] = "Error ".$this->db->lasterror();
  197. }
  198. // Commit or rollback
  199. if ($error) {
  200. foreach ($this->errors as $errmsg) {
  201. dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
  202. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  203. }
  204. $this->db->rollback();
  205. return -1 * $error;
  206. } else {
  207. $this->db->commit();
  208. return 1;
  209. }
  210. }
  211. /**
  212. * Delete object in database
  213. *
  214. * @param User $user User that deletes
  215. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  216. * @return int Return integer <0 if KO, >0 if OK
  217. */
  218. public function delete($user, $notrigger = 0)
  219. {
  220. $error = 0;
  221. $this->db->begin();
  222. if (!$error) {
  223. $sql = "DELETE FROM ".$this->db->prefix().self::$_table_element;
  224. $sql .= " WHERE rowid=".((int) $this->id);
  225. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  226. $resql = $this->db->query($sql);
  227. if (!$resql) {
  228. $error++;
  229. $this->errors[] = "Error ".$this->db->lasterror();
  230. }
  231. }
  232. // Commit or rollback
  233. if ($error) {
  234. foreach ($this->errors as $errmsg) {
  235. dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
  236. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  237. }
  238. $this->db->rollback();
  239. return -1 * $error;
  240. } else {
  241. $this->db->commit();
  242. return 1;
  243. }
  244. }
  245. /**
  246. * Load an object from its id and create a new one in database
  247. *
  248. * @param User $user User making the clone
  249. * @param int $fromid Id of object to clone
  250. * @return int New id of clone
  251. */
  252. public function createFromClone(User $user, $fromid)
  253. {
  254. $error = 0;
  255. $object = new Productbatch($this->db);
  256. $this->db->begin();
  257. // Load source object
  258. $object->fetch($fromid);
  259. $object->id = 0;
  260. $object->statut = 0;
  261. // Clear fields
  262. // ...
  263. // Create clone
  264. $object->context['createfromclone'] = 'createfromclone';
  265. $result = $object->create($user);
  266. // Other options
  267. if ($result < 0) {
  268. $this->error = $object->error;
  269. $this->errors = array_merge($this->errors, $object->errors);
  270. $error++;
  271. }
  272. if (!$error) {
  273. }
  274. unset($object->context['createfromclone']);
  275. // End
  276. if (!$error) {
  277. $this->db->commit();
  278. return $object->id;
  279. } else {
  280. $this->db->rollback();
  281. return -1;
  282. }
  283. }
  284. /**
  285. * Initialise object with example values
  286. * Id must be 0 if object instance is a specimen
  287. *
  288. * @return void
  289. */
  290. public function initAsSpecimen()
  291. {
  292. $this->id = 0;
  293. $this->tms = '';
  294. $this->fk_product_stock = '';
  295. $this->sellby = '';
  296. $this->eatby = '';
  297. $this->batch = '';
  298. $this->import_key = '';
  299. }
  300. /**
  301. * Clean fields (triming)
  302. *
  303. * @return void
  304. */
  305. private function cleanParam()
  306. {
  307. if (isset($this->fk_product_stock)) {
  308. $this->fk_product_stock = (int) trim($this->fk_product_stock);
  309. }
  310. if (isset($this->batch)) {
  311. $this->batch = trim($this->batch);
  312. }
  313. if (isset($this->qty)) {
  314. $this->qty = (float) trim($this->qty);
  315. }
  316. if (isset($this->import_key)) {
  317. $this->import_key = trim($this->import_key);
  318. }
  319. }
  320. /**
  321. * Find first detail record that match eather eat-by or sell-by or batch within given warehouse
  322. *
  323. * @param int $fk_product_stock id product_stock for objet
  324. * @param integer $eatby eat-by date for object - deprecated: a search must be done on batch number
  325. * @param integer $sellby sell-by date for object - deprecated: a search must be done on batch number
  326. * @param string $batch_number batch number for object
  327. * @param int $fk_warehouse filter on warehouse (use it if you don't have $fk_product_stock)
  328. * @return int Return integer <0 if KO, >0 if OK
  329. */
  330. public function find($fk_product_stock = 0, $eatby = '', $sellby = '', $batch_number = '', $fk_warehouse = 0)
  331. {
  332. $where = array();
  333. $sql = "SELECT";
  334. $sql .= " t.rowid,";
  335. $sql .= " t.tms,";
  336. $sql .= " t.fk_product_stock,";
  337. $sql .= " t.sellby,"; // deprecated
  338. $sql .= " t.eatby,"; // deprecated
  339. $sql .= " t.batch,";
  340. $sql .= " t.qty,";
  341. $sql .= " t.import_key";
  342. $sql .= " FROM ".$this->db->prefix().self::$_table_element." as t";
  343. if ($fk_product_stock > 0 || empty($fk_warehouse)) {
  344. $sql .= " WHERE t.fk_product_stock = ".((int) $fk_product_stock);
  345. } else {
  346. $sql .= ", ".$this->db->prefix()."product_stock as ps";
  347. $sql .= " WHERE t.fk_product_stock = ps.rowid AND ps.fk_entrepot = ".((int) $fk_warehouse);
  348. }
  349. if (!empty($eatby)) {
  350. array_push($where, " eatby = '".$this->db->idate($eatby)."'"); // deprecated
  351. }
  352. if (!empty($sellby)) {
  353. array_push($where, " sellby = '".$this->db->idate($sellby)."'"); // deprecated
  354. }
  355. if (!empty($batch_number)) {
  356. $sql .= " AND batch = '".$this->db->escape($batch_number)."'";
  357. }
  358. if (!empty($where)) {
  359. $sql .= " AND (".implode(" OR ", $where).")";
  360. }
  361. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  362. $resql = $this->db->query($sql);
  363. if ($resql) {
  364. if ($this->db->num_rows($resql)) {
  365. $obj = $this->db->fetch_object($resql);
  366. $this->id = $obj->rowid;
  367. $this->tms = $this->db->jdate($obj->tms);
  368. $this->fk_product_stock = $obj->fk_product_stock;
  369. $this->sellby = $this->db->jdate($obj->sellby); // deprecated. do no tuse this data.
  370. $this->eatby = $this->db->jdate($obj->eatby); // deprecated. do not use this data.
  371. $this->batch = $obj->batch;
  372. $this->qty = $obj->qty;
  373. $this->import_key = $obj->import_key;
  374. }
  375. $this->db->free($resql);
  376. return 1;
  377. } else {
  378. $this->error = "Error ".$this->db->lasterror();
  379. return -1;
  380. }
  381. }
  382. /**
  383. * Return all batch detail records for a given product and warehouse
  384. *
  385. * @param DoliDB $dbs database object
  386. * @param int $fk_product_stock id product_stock for objet
  387. * @param int $with_qty 1 = doesn't return line with 0 quantity
  388. * @param int $fk_product If set to a product id, get eatby and sellby from table llx_product_lot
  389. * @return array|int Return integer <0 if KO, array of batch
  390. */
  391. public static function findAll($dbs, $fk_product_stock, $with_qty = 0, $fk_product = 0)
  392. {
  393. global $conf;
  394. $ret = array();
  395. $sql = "SELECT";
  396. $sql .= " t.rowid,";
  397. $sql .= " t.tms,";
  398. $sql .= " t.fk_product_stock,";
  399. $sql .= " t.sellby as oldsellby,"; // deprecated but may not be migrated into new table
  400. $sql .= " t.eatby as oldeatby,"; // deprecated but may not be migrated into new table
  401. $sql .= " t.batch,";
  402. $sql .= " t.qty,";
  403. if (getDolGlobalString('SHIPPING_DISPLAY_STOCK_ENTRY_DATE')) {
  404. $sql .= " MAX(sm.datem) as date_entree,";
  405. }
  406. $sql .= " t.import_key";
  407. if ($fk_product > 0) {
  408. $sql .= ", pl.rowid as lotid, pl.eatby as eatby, pl.sellby as sellby";
  409. // TODO May add extrafields to ?
  410. }
  411. $sql .= " FROM ".$dbs->prefix()."product_batch as t";
  412. if ($fk_product > 0) { // Add link to the table of details of a lot
  413. $sql .= " LEFT JOIN ".$dbs->prefix()."product_lot as pl ON pl.fk_product = ".((int) $fk_product)." AND pl.batch = t.batch";
  414. // TODO May add extrafields to ?
  415. }
  416. if (getDolGlobalString('SHIPPING_DISPLAY_STOCK_ENTRY_DATE')) {
  417. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product_stock AS ps ON (ps.rowid = fk_product_stock)';
  418. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'stock_mouvement AS sm ON (sm.batch = t.batch AND ps.fk_entrepot=sm.fk_entrepot AND sm.type_mouvement IN (0,3))';
  419. }
  420. $sql .= " WHERE fk_product_stock=".((int) $fk_product_stock);
  421. if ($with_qty) {
  422. $sql .= " AND t.qty <> 0";
  423. }
  424. if (getDolGlobalString('SHIPPING_DISPLAY_STOCK_ENTRY_DATE')) {
  425. $sql .= ' GROUP BY t.rowid, t.tms, t.fk_product_stock,t.sellby,t.eatby , t.batch,t.qty,t.import_key';
  426. if ($fk_product > 0) {
  427. $sql .= ', pl.rowid, pl.eatby, pl.sellby';
  428. }
  429. }
  430. $sql .= " ORDER BY ";
  431. // TODO : use product lifo and fifo when product will implement it
  432. if (getDolGlobalString('SHIPPING_DISPLAY_STOCK_ENTRY_DATE')) {
  433. $sql .= 'date_entree ASC,t.batch ASC,';
  434. }
  435. if ($fk_product > 0) {
  436. $sql .= "pl.eatby ASC, pl.sellby ASC, ";
  437. }
  438. $sql .= "t.eatby ASC, t.sellby ASC ";
  439. $sql .= ", t.qty ".(!getDolGlobalString('DO_NOT_TRY_TO_DEFRAGMENT_STOCKS_WAREHOUSE') ? 'ASC' : 'DESC'); // Note : qty ASC is important for expedition card, to avoid stock fragmentation
  440. $sql .= ", t.batch ASC";
  441. dol_syslog("productbatch::findAll", LOG_DEBUG);
  442. $resql = $dbs->query($sql);
  443. if ($resql) {
  444. $num = $dbs->num_rows($resql);
  445. $i = 0;
  446. while ($i < $num) {
  447. $obj = $dbs->fetch_object($resql);
  448. $tmp = new Productbatch($dbs);
  449. $tmp->id = $obj->rowid;
  450. $tmp->tms = $dbs->jdate($obj->tms);
  451. $tmp->fk_product_stock = $obj->fk_product_stock;
  452. $tmp->batch = $obj->batch;
  453. $tmp->qty = $obj->qty;
  454. $tmp->import_key = $obj->import_key;
  455. if (getDolGlobalString('SHIPPING_DISPLAY_STOCK_ENTRY_DATE')) {
  456. $tmp->context['stock_entry_date'] = $dbs->jdate($obj->date_entree);
  457. }
  458. if ($fk_product > 0) {
  459. // Some properties of the lot
  460. $tmp->lotid = $obj->lotid; // ID in table of the details of properties of each lots
  461. $tmp->sellby = $dbs->jdate($obj->sellby ? $obj->sellby : $obj->oldsellby);
  462. $tmp->eatby = $dbs->jdate($obj->eatby ? $obj->eatby : $obj->oldeatby);
  463. }
  464. $ret[$tmp->batch] = $tmp; // $ret is for a $fk_product_stock and unique key is on $fk_product_stock+batch
  465. $i++;
  466. }
  467. $dbs->free($resql);
  468. return $ret;
  469. } else {
  470. //$error = "Error ".$dbs->lasterror();
  471. return -1;
  472. }
  473. }
  474. /**
  475. * Return all batch known for a product and a warehouse (batch that was one day used)
  476. *
  477. * @param int $fk_product Id of product
  478. * @param int $fk_warehouse Id of warehouse
  479. * @param int $qty_min [=NULL] Minimum quantity
  480. * @param string $sortfield [=NULL] List of sort fields, separated by comma. Example: 't1.fielda,t2.fieldb'
  481. * @param string $sortorder [=NULL] Sort order, separated by comma. Example: 'ASC,DESC';
  482. * @return int|array Return integer <0 if KO, array of batch
  483. *
  484. * @throws Exception
  485. */
  486. public function findAllForProduct($fk_product, $fk_warehouse = 0, $qty_min = null, $sortfield = null, $sortorder = null)
  487. {
  488. $productBatchList = array();
  489. dol_syslog(__METHOD__.' fk_product='.$fk_product.', fk_warehouse='.$fk_warehouse.', qty_min='.$qty_min.', sortfield='.$sortfield.', sortorder='.$sortorder, LOG_DEBUG);
  490. $sql = "SELECT";
  491. $sql .= " pl.rowid";
  492. $sql .= ", pl.fk_product";
  493. $sql .= ", pl.batch";
  494. $sql .= ", pl.sellby";
  495. $sql .= ", pl.eatby";
  496. $sql .= ", pb.qty";
  497. $sql .= " FROM ".$this->db->prefix()."product_lot as pl";
  498. $sql .= " LEFT JOIN ".$this->db->prefix()."product as p ON p.rowid = pl.fk_product";
  499. $sql .= " LEFT JOIN ".$this->db->prefix()."product_batch AS pb ON pl.batch = pb.batch";
  500. $sql .= " LEFT JOIN ".$this->db->prefix()."product_stock AS ps ON ps.rowid = pb.fk_product_stock AND ps.fk_product = ".((int) $fk_product);
  501. $sql .= " WHERE p.entity IN (".getEntity('product').")";
  502. $sql .= " AND pl.fk_product = ".((int) $fk_product);
  503. if ($fk_warehouse > 0) {
  504. $sql .= " AND ps.fk_entrepot = ".((int) $fk_warehouse);
  505. }
  506. if ($qty_min !== null) {
  507. $sql .= " AND pb.qty > ".((float) price2num($qty_min, 'MS'));
  508. }
  509. $sql .= $this->db->order($sortfield, $sortorder);
  510. $resql = $this->db->query($sql);
  511. if ($resql) {
  512. while ($obj = $this->db->fetch_object($resql)) {
  513. $productBatch = new self($this->db);
  514. $productBatch->id = $obj->rowid;
  515. $productBatch->fk_product = $obj->fk_product;
  516. $productBatch->batch = $obj->batch;
  517. $productBatch->eatby = $this->db->jdate($obj->eatby);
  518. $productBatch->sellby = $this->db->jdate($obj->sellby);
  519. $productBatch->qty = $obj->qty;
  520. $productBatchList[] = $productBatch;
  521. }
  522. $this->db->free($resql);
  523. return $productBatchList;
  524. } else {
  525. dol_syslog(__METHOD__.' Error: '.$this->db->lasterror(), LOG_ERR);
  526. return -1;
  527. }
  528. }
  529. }