fournisseur.commande.dispatch.class.php 20 KB

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