fournisseur.commande.dispatch.class.php 19 KB

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