fournisseur.commande.dispatch.class.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. <?php
  2. /* Copyright (C) 2015 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
  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/fourn/class/fournisseur.commande.dispatch.class.php
  20. * \ingroup fournisseur stock
  21. * \brief This file is an example for a CRUD class file (Create/Read/Update/Delete)
  22. * Initialy built by build_class_from_table on 2015-02-24 10:38
  23. */
  24. // Put here all includes required by your class file
  25. require_once DOL_DOCUMENT_ROOT."/core/class/commonobject.class.php";
  26. require_once DOL_DOCUMENT_ROOT."/reception/class/reception.class.php";
  27. /**
  28. * Class to manage table commandefournisseurdispatch
  29. */
  30. class CommandeFournisseurDispatch extends CommonObjectLine
  31. {
  32. /**
  33. * @var DoliDB Database handler.
  34. */
  35. public $db;
  36. /**
  37. * @var string Error code (or message)
  38. */
  39. public $error;
  40. /**
  41. * @var string[] Error codes (or messages)
  42. */
  43. public $errors = array();
  44. /**
  45. * @var string ID to identify managed object
  46. */
  47. public $element = 'commandefournisseurdispatch';
  48. /**
  49. * @var string Name of table without prefix where object is stored
  50. */
  51. public $table_element = 'commande_fournisseur_dispatch'; //!< Name of table without prefix where object is stored
  52. public $lines = array();
  53. /**
  54. * @var int ID
  55. */
  56. public $id;
  57. /**
  58. * @var int ID
  59. */
  60. public $fk_commande;
  61. /**
  62. * @var int ID
  63. */
  64. public $fk_product;
  65. /**
  66. * @var int ID
  67. */
  68. public $fk_commandefourndet;
  69. public $qty;
  70. public $qty_asked;
  71. public $libelle;
  72. public $desc;
  73. public $tva_tx;
  74. public $vat_src_code;
  75. public $ref_supplier;
  76. /**
  77. * @var int ID
  78. */
  79. public $fk_entrepot;
  80. /**
  81. * @var int User ID
  82. */
  83. public $fk_user;
  84. public $datec = '';
  85. public $comment;
  86. /**
  87. * @var int Status
  88. */
  89. public $status;
  90. public $tms = '';
  91. public $batch;
  92. public $eatby = '';
  93. public $sellby = '';
  94. public $cost_price = 0;
  95. /**
  96. * Constructor
  97. *
  98. * @param DoliDb $db Database handler
  99. */
  100. public function __construct($db)
  101. {
  102. $this->db = $db;
  103. // List of language codes for status
  104. $this->statuts[0] = 'Received';
  105. $this->statuts[1] = 'Verified';
  106. $this->statuts[2] = 'Denied';
  107. $this->statutshort[0] = 'Received';
  108. $this->statutshort[1] = 'Verified';
  109. $this->statutshort[2] = 'Denied';
  110. }
  111. /**
  112. * Create object into database
  113. *
  114. * @param User $user User that creates
  115. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  116. * @return int <0 if KO, Id of created object if OK
  117. */
  118. public function create($user, $notrigger = 0)
  119. {
  120. global $conf, $langs, $hookmanager;
  121. $error = 0;
  122. // Clean parameters
  123. if (isset($this->fk_commande)) {
  124. $this->fk_commande = trim($this->fk_commande);
  125. }
  126. if (isset($this->fk_product)) {
  127. $this->fk_product = trim($this->fk_product);
  128. }
  129. if (isset($this->fk_commandefourndet)) {
  130. $this->fk_commandefourndet = trim($this->fk_commandefourndet);
  131. }
  132. if (isset($this->qty)) {
  133. $this->qty = trim($this->qty);
  134. }
  135. if (isset($this->fk_entrepot)) {
  136. $this->fk_entrepot = trim($this->fk_entrepot);
  137. }
  138. if (isset($this->fk_user)) {
  139. $this->fk_user = trim($this->fk_user);
  140. }
  141. if (isset($this->comment)) {
  142. $this->comment = trim($this->comment);
  143. }
  144. if (isset($this->status)) {
  145. $this->status = trim($this->status);
  146. }
  147. if (isset($this->batch)) {
  148. $this->batch = trim($this->batch);
  149. }
  150. if (empty($this->datec)) {
  151. $this->datec = dol_now();
  152. }
  153. // Check parameters
  154. // Put here code to add control on parameters values
  155. // Insert request
  156. $sql = "INSERT INTO ".MAIN_DB_PREFIX.$this->table_element."(";
  157. $sql .= "fk_commande,";
  158. $sql .= "fk_product,";
  159. $sql .= "fk_commandefourndet,";
  160. $sql .= "qty,";
  161. $sql .= "fk_entrepot,";
  162. $sql .= "fk_user,";
  163. $sql .= "datec,";
  164. $sql .= "comment,";
  165. $sql .= "status,";
  166. $sql .= "batch,";
  167. $sql .= "eatby,";
  168. $sql .= "sellby,";
  169. $sql .= "fk_reception,";
  170. $sql .= "cost_price";
  171. $sql .= ") VALUES (";
  172. $sql .= " ".(!isset($this->fk_commande) ? 'NULL' : "'".$this->db->escape($this->fk_commande)."'").",";
  173. $sql .= " ".(!isset($this->fk_product) ? 'NULL' : "'".$this->db->escape($this->fk_product)."'").",";
  174. $sql .= " ".(!isset($this->fk_commandefourndet) ? 'NULL' : "'".$this->db->escape($this->fk_commandefourndet)."'").",";
  175. $sql .= " ".(!isset($this->qty) ? 'NULL' : "'".$this->db->escape($this->qty)."'").",";
  176. $sql .= " ".(!isset($this->fk_entrepot) ? 'NULL' : "'".$this->db->escape($this->fk_entrepot)."'").",";
  177. $sql .= " ".(!isset($this->fk_user) ? 'NULL' : "'".$this->db->escape($this->fk_user)."'").",";
  178. $sql .= " ".(!isset($this->datec) || dol_strlen($this->datec) == 0 ? 'NULL' : "'".$this->db->idate($this->datec)."'").",";
  179. $sql .= " ".(!isset($this->comment) ? 'NULL' : "'".$this->db->escape($this->comment)."'").",";
  180. $sql .= " ".(!isset($this->status) ? 'NULL' : "'".$this->db->escape($this->status)."'").",";
  181. $sql .= " ".(!isset($this->batch) ? 'NULL' : "'".$this->db->escape($this->batch)."'").",";
  182. $sql .= " ".(!isset($this->eatby) || dol_strlen($this->eatby) == 0 ? 'NULL' : "'".$this->db->idate($this->eatby)."'").",";
  183. $sql .= " ".(!isset($this->sellby) || dol_strlen($this->sellby) == 0 ? 'NULL' : "'".$this->db->idate($this->sellby)."'").",";
  184. $sql .= " ".(!isset($this->fk_reception) ? 'NULL' : "'".$this->db->escape($this->fk_reception)."'").",";
  185. $sql .= " ".(!isset($this->cost_price) ? '0' : "'".$this->db->escape($this->cost_price)."'")."";
  186. $sql .= ")";
  187. $this->db->begin();
  188. dol_syslog(__METHOD__, LOG_DEBUG);
  189. $resql = $this->db->query($sql);
  190. if (!$resql) {
  191. $error++; $this->errors[] = "Error ".$this->db->lasterror();
  192. }
  193. if (!$error) {
  194. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
  195. if (!$notrigger) {
  196. // Call triggers
  197. $result=$this->call_trigger('LINERECEPTION_CREATE', $user);
  198. if ($result < 0) {
  199. $error++;
  200. }
  201. // End call triggers
  202. }
  203. }
  204. // Create extrafields
  205. if (!$error) {
  206. $result = $this->insertExtraFields();
  207. if ($result < 0) {
  208. $error++;
  209. }
  210. }
  211. // Commit or rollback
  212. if ($error) {
  213. foreach ($this->errors as $errmsg) {
  214. dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
  215. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  216. }
  217. $this->db->rollback();
  218. return -1 * $error;
  219. } else {
  220. $this->db->commit();
  221. return $this->id;
  222. }
  223. }
  224. /**
  225. * Load object in memory from the database
  226. *
  227. * @param int $id Id object
  228. * @param string $ref Ref
  229. * @return int <0 if KO, >0 if OK
  230. */
  231. public function fetch($id, $ref = '')
  232. {
  233. global $langs;
  234. $sql = "SELECT";
  235. $sql .= " t.rowid,";
  236. $sql .= " t.fk_commande,";
  237. $sql .= " t.fk_product,";
  238. $sql .= " t.fk_commandefourndet,";
  239. $sql .= " t.qty,";
  240. $sql .= " t.fk_entrepot,";
  241. $sql .= " t.fk_user,";
  242. $sql .= " t.datec,";
  243. $sql .= " t.comment,";
  244. $sql .= " t.status,";
  245. $sql .= " t.tms,";
  246. $sql .= " t.batch,";
  247. $sql .= " t.eatby,";
  248. $sql .= " t.sellby,";
  249. $sql .= " t.fk_reception";
  250. $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." as t";
  251. if ($ref) {
  252. $sql .= " WHERE t.ref = '".$this->db->escape($ref)."'";
  253. } else {
  254. $sql .= " WHERE t.rowid = ".((int) $id);
  255. }
  256. dol_syslog(get_class($this)."::fetch");
  257. $resql = $this->db->query($sql);
  258. if ($resql) {
  259. if ($this->db->num_rows($resql)) {
  260. $obj = $this->db->fetch_object($resql);
  261. $this->id = $obj->rowid;
  262. $this->fk_commande = $obj->fk_commande;
  263. $this->fk_product = $obj->fk_product;
  264. $this->fk_commandefourndet = $obj->fk_commandefourndet;
  265. $this->qty = $obj->qty;
  266. $this->fk_entrepot = $obj->fk_entrepot;
  267. $this->fk_user = $obj->fk_user;
  268. $this->datec = $this->db->jdate($obj->datec);
  269. $this->comment = $obj->comment;
  270. $this->status = $obj->status;
  271. $this->tms = $this->db->jdate($obj->tms);
  272. $this->batch = $obj->batch;
  273. $this->eatby = $this->db->jdate($obj->eatby);
  274. $this->sellby = $this->db->jdate($obj->sellby);
  275. $this->fk_reception = $obj->fk_reception;
  276. $this->fetch_optionals();
  277. }
  278. $this->db->free($resql);
  279. return 1;
  280. } else {
  281. $this->error = "Error ".$this->db->lasterror();
  282. return -1;
  283. }
  284. }
  285. /**
  286. * Update object into database
  287. *
  288. * @param User $user User that modifies
  289. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  290. * @return int <0 if KO, >0 if OK
  291. */
  292. public function update($user, $notrigger = 0)
  293. {
  294. $error = 0;
  295. // Clean parameters
  296. if (isset($this->fk_commande)) {
  297. $this->fk_commande = trim($this->fk_commande);
  298. }
  299. if (isset($this->fk_product)) {
  300. $this->fk_product = trim($this->fk_product);
  301. }
  302. if (isset($this->fk_commandefourndet)) {
  303. $this->fk_commandefourndet = trim($this->fk_commandefourndet);
  304. }
  305. if (isset($this->qty)) {
  306. $this->qty = trim($this->qty);
  307. }
  308. if (isset($this->fk_entrepot)) {
  309. $this->fk_entrepot = trim($this->fk_entrepot);
  310. }
  311. if (isset($this->fk_user)) {
  312. $this->fk_user = trim($this->fk_user);
  313. }
  314. if (isset($this->comment)) {
  315. $this->comment = trim($this->comment);
  316. }
  317. if (isset($this->status)) {
  318. $this->status = trim($this->status);
  319. }
  320. if (isset($this->batch)) {
  321. $this->batch = trim($this->batch);
  322. }
  323. // Check parameters
  324. // Put here code to add a control on parameters values
  325. // Update request
  326. $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET";
  327. $sql .= " fk_commande=".(isset($this->fk_commande) ? $this->fk_commande : "null").",";
  328. $sql .= " fk_product=".(isset($this->fk_product) ? $this->fk_product : "null").",";
  329. $sql .= " fk_commandefourndet=".(isset($this->fk_commandefourndet) ? $this->fk_commandefourndet : "null").",";
  330. $sql .= " qty=".(isset($this->qty) ? $this->qty : "null").",";
  331. $sql .= " fk_entrepot=".(isset($this->fk_entrepot) ? $this->fk_entrepot : "null").",";
  332. $sql .= " fk_user=".(isset($this->fk_user) ? $this->fk_user : "null").",";
  333. $sql .= " datec=".(dol_strlen($this->datec) != 0 ? "'".$this->db->idate($this->datec)."'" : 'null').",";
  334. $sql .= " comment=".(isset($this->comment) ? "'".$this->db->escape($this->comment)."'" : "null").",";
  335. $sql .= " status=".(isset($this->status) ? $this->status : "null").",";
  336. $sql .= " tms=".(dol_strlen($this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
  337. $sql .= " batch=".(isset($this->batch) ? "'".$this->db->escape($this->batch)."'" : "null").",";
  338. $sql .= " eatby=".(dol_strlen($this->eatby) != 0 ? "'".$this->db->idate($this->eatby)."'" : 'null').",";
  339. $sql .= " sellby=".(dol_strlen($this->sellby) != 0 ? "'".$this->db->idate($this->sellby)."'" : 'null')."";
  340. $sql .= " WHERE rowid=".((int) $this->id);
  341. $this->db->begin();
  342. dol_syslog(__METHOD__);
  343. $resql = $this->db->query($sql);
  344. if (!$resql) {
  345. $error++; $this->errors[] = "Error ".$this->db->lasterror();
  346. }
  347. if (!$error) {
  348. if (!$error) {
  349. if (empty($this->id) && !empty($this->rowid)) {
  350. $this->id = $this->rowid;
  351. }
  352. $result = $this->insertExtraFields();
  353. if ($result < 0) {
  354. $error++;
  355. }
  356. }
  357. if (!$notrigger) {
  358. // Call triggers
  359. $result = $this->call_trigger('LINERECEPTION_MODIFY', $user);
  360. if ($result < 0) {
  361. $error++;
  362. }
  363. // End call triggers
  364. }
  365. }
  366. // Commit or rollback
  367. if ($error) {
  368. foreach ($this->errors as $errmsg) {
  369. dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
  370. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  371. }
  372. $this->db->rollback();
  373. return -1 * $error;
  374. } else {
  375. $this->db->commit();
  376. return 1;
  377. }
  378. }
  379. /**
  380. * Delete object in database
  381. *
  382. * @param User $user User that deletes
  383. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  384. * @return int <0 if KO, >0 if OK
  385. */
  386. public function delete($user, $notrigger = 0)
  387. {
  388. $error = 0;
  389. $this->db->begin();
  390. if (!$error) {
  391. if (!$notrigger) {
  392. // Call triggers
  393. $result = $this->call_trigger('LINERECEPTION_DELETE', $user);
  394. if ($result < 0) {
  395. $error++;
  396. }
  397. // End call triggers
  398. }
  399. }
  400. // Remove extrafields
  401. if (!$error) {
  402. $result = $this->deleteExtraFields();
  403. if ($result < 0) {
  404. $error++;
  405. dol_syslog(get_class($this)."::delete error deleteExtraFields ".$this->error, LOG_ERR);
  406. }
  407. }
  408. if (!$error) {
  409. $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
  410. $sql .= " WHERE rowid=".((int) $this->id);
  411. dol_syslog(__METHOD__);
  412. $resql = $this->db->query($sql);
  413. if (!$resql) {
  414. $error++; $this->errors[] = "Error ".$this->db->lasterror();
  415. }
  416. }
  417. // Commit or rollback
  418. if ($error) {
  419. foreach ($this->errors as $errmsg) {
  420. dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
  421. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  422. }
  423. $this->db->rollback();
  424. return -1 * $error;
  425. } else {
  426. $this->db->commit();
  427. return 1;
  428. }
  429. }
  430. /**
  431. * Load an object from its id and create a new one in database
  432. *
  433. * @param User $user User making the clone
  434. * @param int $fromid Id of object to clone
  435. * @return int New id of clone
  436. */
  437. public function createFromClone(User $user, $fromid)
  438. {
  439. $error = 0;
  440. $object = new Commandefournisseurdispatch($this->db);
  441. $this->db->begin();
  442. // Load source object
  443. $object->fetch($fromid);
  444. $object->id = 0;
  445. $object->statut = 0;
  446. // Clear fields
  447. // ...
  448. // Create clone
  449. $object->context['createfromclone'] = 'createfromclone';
  450. $result = $object->create($user);
  451. // Other options
  452. if ($result < 0) {
  453. $this->error = $object->error;
  454. $error++;
  455. }
  456. if (!$error) {
  457. }
  458. unset($object->context['createfromclone']);
  459. // End
  460. if (!$error) {
  461. $this->db->commit();
  462. return $object->id;
  463. } else {
  464. $this->db->rollback();
  465. return -1;
  466. }
  467. }
  468. /**
  469. * Return label of the status of object
  470. *
  471. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=short label + picto
  472. * @return string Label
  473. */
  474. public function getLibStatut($mode = 0)
  475. {
  476. return $this->LibStatut($this->status, $mode);
  477. }
  478. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  479. /**
  480. * Return label of a status
  481. *
  482. * @param int $status Id status
  483. * @param int $mode 0=Long label, 1=Short label, 2=Picto + Short label, 3=Picto, 4=Picto + Long label, 5=Short label + Picto
  484. * @return string Label of status
  485. */
  486. public function LibStatut($status, $mode = 0)
  487. {
  488. // phpcs:enable
  489. global $langs;
  490. $langs->load('orders');
  491. if ($mode == 0) {
  492. return $langs->trans($this->statuts[$status]);
  493. } elseif ($mode == 1) {
  494. return $langs->trans($this->statutshort[$status]);
  495. } elseif ($mode == 2) {
  496. return $langs->trans($this->statuts[$status]);
  497. } elseif ($mode == 3) {
  498. if ($status == 0) {
  499. return img_picto($langs->trans($this->statuts[$status]), 'statut0');
  500. } elseif ($status == 1) {
  501. return img_picto($langs->trans($this->statuts[$status]), 'statut4');
  502. } elseif ($status == 2) {
  503. return img_picto($langs->trans($this->statuts[$status]), 'statut8');
  504. }
  505. } elseif ($mode == 4) {
  506. if ($status == 0) {
  507. return img_picto($langs->trans($this->statuts[$status]), 'statut0').' '.$langs->trans($this->statuts[$status]);
  508. } elseif ($status == 1) {
  509. return img_picto($langs->trans($this->statuts[$status]), 'statut4').' '.$langs->trans($this->statuts[$status]);
  510. } elseif ($status == 2) {
  511. return img_picto($langs->trans($this->statuts[$status]), 'statut8').' '.$langs->trans($this->statuts[$status]);
  512. }
  513. } elseif ($mode == 5) {
  514. if ($status == 0) {
  515. return '<span class="hideonsmartphone">'.$langs->trans($this->statutshort[$status]).' </span>'.img_picto($langs->trans($this->statuts[$status]), 'statut0');
  516. } elseif ($status == 1) {
  517. return '<span class="hideonsmartphone">'.$langs->trans($this->statutshort[$status]).' </span>'.img_picto($langs->trans($this->statuts[$status]), 'statut4');
  518. } elseif ($status == 2) {
  519. return '<span class="hideonsmartphone">'.$langs->trans($this->statutshort[$status]).' </span>'.img_picto($langs->trans($this->statuts[$status]), 'statut8');
  520. }
  521. }
  522. }
  523. /**
  524. * Initialise object with example values
  525. * Id must be 0 if object instance is a specimen
  526. *
  527. * @return void
  528. */
  529. public function initAsSpecimen()
  530. {
  531. $this->id = 0;
  532. $this->fk_commande = '';
  533. $this->fk_product = '';
  534. $this->fk_commandefourndet = '';
  535. $this->qty = '';
  536. $this->fk_entrepot = '';
  537. $this->fk_user = '';
  538. $this->datec = '';
  539. $this->comment = '';
  540. $this->status = '';
  541. $this->tms = '';
  542. $this->batch = '';
  543. $this->eatby = '';
  544. $this->sellby = '';
  545. }
  546. /**
  547. * Load object in memory from the database
  548. *
  549. * @param string $sortorder Sort Order
  550. * @param string $sortfield Sort field
  551. * @param int $limit offset limit
  552. * @param int $offset offset limit
  553. * @param array $filter filter array
  554. * @param string $filtermode filter mode (AND or OR)
  555. *
  556. * @return int <0 if KO, >0 if OK
  557. */
  558. public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
  559. {
  560. dol_syslog(__METHOD__, LOG_DEBUG);
  561. $sql = "SELECT";
  562. $sql .= " t.rowid,";
  563. $sql .= " t.fk_commande,";
  564. $sql .= " t.fk_product,";
  565. $sql .= " t.fk_commandefourndet,";
  566. $sql .= " t.qty,";
  567. $sql .= " t.fk_entrepot,";
  568. $sql .= " t.fk_user,";
  569. $sql .= " t.datec,";
  570. $sql .= " t.comment,";
  571. $sql .= " t.status,";
  572. $sql .= " t.tms,";
  573. $sql .= " t.batch,";
  574. $sql .= " t.eatby,";
  575. $sql .= " t.sellby";
  576. $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." as t";
  577. // Manage filter
  578. $sqlwhere = array();
  579. if (count($filter) > 0) {
  580. foreach ($filter as $key => $value) {
  581. if ($key == 't.comment') {
  582. $sqlwhere [] = $key." LIKE '%".$this->db->escape($value)."%'";
  583. } elseif ($key == 't.datec' || $key == 't.tms' || $key == 't.eatby' || $key == 't.sellby' || $key == 't.batch') {
  584. $sqlwhere [] = $key." = '".$this->db->escape($value)."'";
  585. } elseif ($key == 'qty') {
  586. $sqlwhere [] = $key." = ".((float) $value);
  587. } else {
  588. $sqlwhere [] = $key." = ".((int) $value);
  589. }
  590. }
  591. }
  592. if (count($sqlwhere) > 0) {
  593. $sql .= ' WHERE '.implode(' '.$this->db->escape($filtermode).' ', $sqlwhere);
  594. }
  595. if (!empty($sortfield)) {
  596. $sql .= $this->db->order($sortfield, $sortorder);
  597. }
  598. if (!empty($limit)) {
  599. $sql .= $this->db->plimit($limit, $offset);
  600. }
  601. $this->lines = array();
  602. $resql = $this->db->query($sql);
  603. if ($resql) {
  604. $num = $this->db->num_rows($resql);
  605. while ($obj = $this->db->fetch_object($resql)) {
  606. $line = new self($this->db);
  607. $line->id = $obj->rowid;
  608. $line->fk_commande = $obj->fk_commande;
  609. $line->fk_product = $obj->fk_product;
  610. $line->fk_commandefourndet = $obj->fk_commandefourndet;
  611. $line->qty = $obj->qty;
  612. $line->fk_entrepot = $obj->fk_entrepot;
  613. $line->fk_user = $obj->fk_user;
  614. $line->datec = $this->db->jdate($obj->datec);
  615. $line->comment = $obj->comment;
  616. $line->status = $obj->status;
  617. $line->tms = $this->db->jdate($obj->tms);
  618. $line->batch = $obj->batch;
  619. $line->eatby = $this->db->jdate($obj->eatby);
  620. $line->sellby = $this->db->jdate($obj->sellby);
  621. $line->fetch_optionals();
  622. $this->lines[$line->id] = $line;
  623. }
  624. $this->db->free($resql);
  625. return $num;
  626. } else {
  627. $this->errors[] = 'Error '.$this->db->lasterror();
  628. dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
  629. return -1;
  630. }
  631. }
  632. }