productstockentrepot.class.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. <?php
  2. /* Copyright (C) 2007-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2014-2016 Juanjo Menent <jmenent@2byte.es>
  4. * Copyright (C) 2015 Florian Henry <florian.henry@open-concept.pro>
  5. * Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
  6. * Copyright (C) 2018-2019 Frédéric France <frederic.france@netlogic.fr>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. /**
  22. * \file htdocs/product/stock/class/productstockentrepot.class.php
  23. * \ingroup ProductEntrepot
  24. * \brief This file is an example for a CRUD class file (Create/Read/Update/Delete)
  25. * Put some comments here
  26. */
  27. // Put here all includes required by your class file
  28. require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
  29. //require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php';
  30. //require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
  31. /**
  32. * Class ProductStockEntrepot
  33. *
  34. * Put here description of your class
  35. *
  36. * @see CommonObject
  37. */
  38. class ProductStockEntrepot extends CommonObject
  39. {
  40. /**
  41. * @var string Id to identify managed objects
  42. */
  43. public $element = 'ProductStockEntrepot';
  44. /**
  45. * @var string Name of table without prefix where object is stored
  46. */
  47. public $table_element = 'product_warehouse_properties';
  48. public $tms = '';
  49. /**
  50. * @var int ID
  51. */
  52. public $fk_product;
  53. /**
  54. * @var int ID
  55. */
  56. public $fk_entrepot;
  57. public $seuil_stock_alerte;
  58. public $desiredstock;
  59. public $import_key;
  60. /**
  61. * Constructor
  62. *
  63. * @param DoliDb $db Database handler
  64. */
  65. public function __construct(DoliDB $db)
  66. {
  67. $this->db = $db;
  68. }
  69. /**
  70. * Create object into database
  71. *
  72. * @param User $user User that creates
  73. * @param bool $notrigger false=launch triggers after, true=disable triggers
  74. *
  75. * @return int <0 if KO, Id of created object if OK
  76. */
  77. public function create(User $user, $notrigger = false)
  78. {
  79. dol_syslog(__METHOD__, LOG_DEBUG);
  80. $error = 0;
  81. // Clean parameters
  82. if (isset($this->fk_product)) {
  83. $this->fk_product = (int) $this->fk_product;
  84. }
  85. if (isset($this->fk_entrepot)) {
  86. $this->fk_entrepot = (int) $this->fk_entrepot;
  87. }
  88. if (isset($this->seuil_stock_alerte)) {
  89. $this->seuil_stock_alerte = trim($this->seuil_stock_alerte);
  90. }
  91. if (isset($this->desiredstock)) {
  92. $this->desiredstock = trim($this->desiredstock);
  93. }
  94. if (isset($this->import_key)) {
  95. $this->import_key = trim($this->import_key);
  96. }
  97. // Check parameters
  98. // Put here code to add control on parameters values
  99. // Insert request
  100. $sql = 'INSERT INTO '.MAIN_DB_PREFIX.$this->table_element.'(';
  101. $sql .= 'fk_product,';
  102. $sql .= 'fk_entrepot,';
  103. $sql .= 'seuil_stock_alerte,';
  104. $sql .= 'desiredstock,';
  105. $sql .= 'import_key';
  106. $sql .= ') VALUES (';
  107. $sql .= ' '.(!isset($this->fk_product) ? 'NULL' : $this->fk_product).',';
  108. $sql .= ' '.(!isset($this->fk_entrepot) ? 'NULL' : $this->fk_entrepot).',';
  109. $sql .= ' '.(!isset($this->seuil_stock_alerte) ? '0' : $this->seuil_stock_alerte).',';
  110. $sql .= ' '.(!isset($this->desiredstock) ? '0' : $this->desiredstock).',';
  111. $sql .= ' '.(!isset($this->import_key) ? 'NULL' : "'".$this->db->escape($this->import_key)."'");
  112. $sql .= ')';
  113. $this->db->begin();
  114. $resql = $this->db->query($sql);
  115. if (!$resql) {
  116. $error++;
  117. $this->errors[] = 'Error '.$this->db->lasterror();
  118. dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
  119. }
  120. if (!$error) {
  121. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
  122. //if (!$notrigger) {
  123. // Uncomment this and change MYOBJECT to your own tag if you
  124. // want this action to call a trigger.
  125. //// Call triggers
  126. //$result=$this->call_trigger('MYOBJECT_CREATE',$user);
  127. //if ($result < 0) $error++;
  128. //// End call triggers
  129. //}
  130. }
  131. // Commit or rollback
  132. if ($error) {
  133. $this->db->rollback();
  134. return -1 * $error;
  135. } else {
  136. $this->db->commit();
  137. return $this->id;
  138. }
  139. }
  140. /**
  141. * Load object in memory from the database
  142. *
  143. * @param int $id Id object
  144. * @param int $fk_product Id product
  145. * @param int $fk_entrepot Id warehouse
  146. * @return int <0 if KO, 0 if not found, >0 if OK
  147. */
  148. public function fetch($id, $fk_product = 0, $fk_entrepot = 0)
  149. {
  150. if (empty($id) && (empty($fk_product) || empty($fk_entrepot))) {
  151. return -1;
  152. }
  153. dol_syslog(__METHOD__, LOG_DEBUG);
  154. $sql = 'SELECT';
  155. $sql .= ' t.rowid,';
  156. $sql .= " t.tms,";
  157. $sql .= " t.fk_product,";
  158. $sql .= " t.fk_entrepot,";
  159. $sql .= " t.seuil_stock_alerte,";
  160. $sql .= " t.desiredstock,";
  161. $sql .= " t.import_key";
  162. $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
  163. if (!empty($id)) {
  164. $sql .= ' WHERE t.rowid = '.((int) $id);
  165. } else {
  166. $sql .= ' WHERE t.fk_product = '.((int) $fk_product).' AND t.fk_entrepot = '.((int) $fk_entrepot);
  167. }
  168. $resql = $this->db->query($sql);
  169. if ($resql) {
  170. $numrows = $this->db->num_rows($resql);
  171. if ($numrows) {
  172. $obj = $this->db->fetch_object($resql);
  173. $this->id = $obj->rowid;
  174. $this->tms = $this->db->jdate($obj->tms);
  175. $this->fk_product = $obj->fk_product;
  176. $this->fk_entrepot = $obj->fk_entrepot;
  177. $this->seuil_stock_alerte = $obj->seuil_stock_alerte;
  178. $this->desiredstock = $obj->desiredstock;
  179. $this->import_key = $obj->import_key;
  180. }
  181. // Retrieve all extrafield
  182. // fetch optionals attributes and labels
  183. $this->fetch_optionals();
  184. // $this->fetch_lines();
  185. $this->db->free($resql);
  186. if ($numrows) {
  187. return 1;
  188. } else {
  189. return 0;
  190. }
  191. } else {
  192. $this->errors[] = 'Error '.$this->db->lasterror();
  193. dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
  194. return -1;
  195. }
  196. }
  197. /**
  198. * Load object in memory from the database
  199. *
  200. * @param int $fk_product Product from which we want to get limit and desired stock by warehouse
  201. * @param int $fk_entrepot Warehouse in which we want to get products limit and desired stock
  202. * @param string $sortorder Sort Order
  203. * @param string $sortfield Sort field
  204. * @param int $limit offset limit
  205. * @param int $offset offset limit
  206. * @param array $filter filter array
  207. * @param string $filtermode filter mode (AND or OR)
  208. *
  209. * @return int <0 if KO, >0 if OK
  210. */
  211. public function fetchAll($fk_product = '', $fk_entrepot = '', $sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
  212. {
  213. dol_syslog(__METHOD__, LOG_DEBUG);
  214. $sql = 'SELECT';
  215. $sql .= ' t.rowid,';
  216. $sql .= " t.tms,";
  217. $sql .= " t.fk_product,";
  218. $sql .= " t.fk_entrepot,";
  219. $sql .= " t.seuil_stock_alerte,";
  220. $sql .= " t.desiredstock,";
  221. $sql .= " t.import_key";
  222. $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
  223. $sql .= ' WHERE 1=1';
  224. // Manage filter
  225. $sqlwhere = array();
  226. if (count($filter) > 0) {
  227. foreach ($filter as $key => $value) {
  228. $sqlwhere [] = $key." LIKE '%".$this->db->escape($value)."%'";
  229. }
  230. }
  231. if (count($sqlwhere) > 0) {
  232. $sql .= ' AND '.implode(' '.$filtermode.' ', $sqlwhere);
  233. }
  234. if (!empty($fk_product) && $fk_product > 0) {
  235. $sql .= ' AND fk_product = '.((int) $fk_product);
  236. } elseif (!empty($fk_entrepot) && $fk_entrepot > 0) {
  237. $sql .= ' AND fk_entrepot = '.((int) $fk_entrepot);
  238. }
  239. // "elseif" used instead of "if" because getting list with specified fk_product and specified fk_entrepot would be the same as doing a fetch
  240. if (!empty($sortfield)) {
  241. $sql .= $this->db->order($sortfield, $sortorder);
  242. }
  243. if (!empty($limit)) {
  244. $sql .= $this->db->plimit($limit, $offset);
  245. }
  246. $lines = array();
  247. $resql = $this->db->query($sql);
  248. if ($resql) {
  249. while ($obj = $this->db->fetch_object($resql)) {
  250. $lines[$obj->rowid] = array(
  251. 'id'=>$obj->rowid
  252. ,'fk_product'=>$obj->fk_product
  253. ,'fk_entrepot'=>$obj->fk_entrepot
  254. ,'seuil_stock_alerte'=>$obj->seuil_stock_alerte
  255. ,'desiredstock'=>$obj->desiredstock
  256. );
  257. }
  258. $this->db->free($resql);
  259. return $lines;
  260. } else {
  261. $this->errors[] = 'Error '.$this->db->lasterror();
  262. dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
  263. return -1;
  264. }
  265. }
  266. /**
  267. * Update object into database
  268. *
  269. * @param User $user User that modifies
  270. * @param bool $notrigger false=launch triggers after, true=disable triggers
  271. *
  272. * @return int <0 if KO, >0 if OK
  273. */
  274. public function update(User $user, $notrigger = false)
  275. {
  276. $error = 0;
  277. dol_syslog(__METHOD__, LOG_DEBUG);
  278. // Clean parameters
  279. if (isset($this->fk_product)) {
  280. $this->fk_product = (int) $this->fk_product;
  281. }
  282. if (isset($this->fk_entrepot)) {
  283. $this->fk_entrepot = (int) $this->fk_entrepot;
  284. }
  285. if (isset($this->seuil_stock_alerte)) {
  286. $this->seuil_stock_alerte = trim($this->seuil_stock_alerte);
  287. }
  288. if (isset($this->desiredstock)) {
  289. $this->desiredstock = trim($this->desiredstock);
  290. }
  291. if (isset($this->import_key)) {
  292. $this->import_key = trim($this->import_key);
  293. }
  294. // Check parameters
  295. // Put here code to add a control on parameters values
  296. // Update request
  297. $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element.' SET';
  298. $sql .= ' tms = '.(dol_strlen($this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : "'".$this->db->idate(dol_now())."'").',';
  299. $sql .= ' fk_product = '.(isset($this->fk_product) ? $this->fk_product : "null").',';
  300. $sql .= ' fk_entrepot = '.(isset($this->fk_entrepot) ? $this->fk_entrepot : "null").',';
  301. $sql .= ' seuil_stock_alerte = '.(isset($this->seuil_stock_alerte) ? $this->seuil_stock_alerte : "null").',';
  302. $sql .= ' desiredstock = '.(isset($this->desiredstock) ? $this->desiredstock : "null").',';
  303. $sql .= ' import_key = '.(isset($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null");
  304. $sql .= ' WHERE rowid='.((int) $this->id);
  305. $this->db->begin();
  306. $resql = $this->db->query($sql);
  307. if (!$resql) {
  308. $error++;
  309. $this->errors[] = 'Error '.$this->db->lasterror();
  310. dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
  311. }
  312. //if (!$error && !$notrigger) {
  313. // Uncomment this and change MYOBJECT to your own tag if you
  314. // want this action calls a trigger.
  315. //// Call triggers
  316. //$result=$this->call_trigger('MYOBJECT_MODIFY',$user);
  317. //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
  318. //// End call triggers
  319. //}
  320. // Commit or rollback
  321. if ($error) {
  322. $this->db->rollback();
  323. return -1 * $error;
  324. } else {
  325. $this->db->commit();
  326. return 1;
  327. }
  328. }
  329. /**
  330. * Delete object in database
  331. *
  332. * @param User $user User that deletes
  333. * @param bool $notrigger false=launch triggers after, true=disable triggers
  334. *
  335. * @return int <0 if KO, >0 if OK
  336. */
  337. public function delete(User $user, $notrigger = false)
  338. {
  339. dol_syslog(__METHOD__, LOG_DEBUG);
  340. $error = 0;
  341. $this->db->begin();
  342. //if (!$error && !$notrigger) {
  343. // Uncomment this and change MYOBJECT to your own tag if you
  344. // want this action calls a trigger.
  345. //// Call triggers
  346. //$result=$this->call_trigger('MYOBJECT_DELETE',$user);
  347. //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
  348. //// End call triggers
  349. //}
  350. if (!$error) {
  351. $sql = 'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element;
  352. $sql .= ' WHERE rowid='.((int) $this->id);
  353. $resql = $this->db->query($sql);
  354. if (!$resql) {
  355. $error++;
  356. $this->errors[] = 'Error '.$this->db->lasterror();
  357. dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
  358. }
  359. }
  360. // Commit or rollback
  361. if ($error) {
  362. $this->db->rollback();
  363. return -1 * $error;
  364. } else {
  365. $this->db->commit();
  366. return 1;
  367. }
  368. }
  369. /**
  370. * Load an object from its id and create a new one in database
  371. *
  372. * @param User $user User making the clone
  373. * @param int $fromid Id of object to clone
  374. * @return int New id of clone
  375. */
  376. public function createFromClone(User $user, $fromid)
  377. {
  378. dol_syslog(__METHOD__, LOG_DEBUG);
  379. $error = 0;
  380. $object = new ProductStockEntrepot($this->db);
  381. $this->db->begin();
  382. // Load source object
  383. $object->fetch($fromid);
  384. // Reset object
  385. $object->id = 0;
  386. // Clear fields
  387. // ...
  388. // Create clone
  389. $object->context['createfromclone'] = 'createfromclone';
  390. $result = $object->create($user);
  391. // Other options
  392. if ($result < 0) {
  393. $error++;
  394. $this->errors = $object->errors;
  395. dol_syslog(__METHOD__.' '.implode(',', $this->errors), LOG_ERR);
  396. }
  397. unset($object->context['createfromclone']);
  398. // End
  399. if (!$error) {
  400. $this->db->commit();
  401. return $object->id;
  402. } else {
  403. $this->db->rollback();
  404. return -1;
  405. }
  406. }
  407. /**
  408. * Return a link to the user card (with optionaly the picto)
  409. * Use this->id,this->lastname, this->firstname
  410. *
  411. * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
  412. * @param string $option On what the link point to
  413. * @param integer $notooltip 1=Disable tooltip
  414. * @param int $maxlen Max length of visible user name
  415. * @param string $morecss Add more css on link
  416. * @return string String with URL
  417. */
  418. public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $maxlen = 24, $morecss = '')
  419. {
  420. global $langs, $conf, $db;
  421. global $dolibarr_main_authentication, $dolibarr_main_demo;
  422. global $menumanager;
  423. $result = '';
  424. $companylink = '';
  425. $label = '<u>'.$langs->trans("MyModule").'</u>';
  426. $label .= '<div width="100%">';
  427. $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
  428. $link = '<a href="'.DOL_URL_ROOT.'/ProductEntrepot/card.php?id='.$this->id.'"';
  429. $link .= ($notooltip ? '' : ' title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip'.($morecss ? ' '.$morecss : '').'"');
  430. $link .= '>';
  431. $linkend = '</a>';
  432. if ($withpicto) {
  433. $result .= ($link.img_object(($notooltip ? '' : $label), 'label', ($notooltip ? '' : 'class="classfortooltip"'), 0, 0, $notooltip ? 0 : 1).$linkend);
  434. if ($withpicto != 2) {
  435. $result .= ' ';
  436. }
  437. }
  438. $result .= $link.$this->ref.$linkend;
  439. return $result;
  440. }
  441. /**
  442. * Retourne le libelle du status d'un user (actif, inactif)
  443. *
  444. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  445. * @return string Label of status
  446. */
  447. public function getLibStatut($mode = 0)
  448. {
  449. return $this->LibStatut($this->status, $mode);
  450. }
  451. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  452. /**
  453. * Renvoi le libelle d'un status donne
  454. *
  455. * @param int $status Id status
  456. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  457. * @return string Label of status
  458. */
  459. public function LibStatut($status, $mode = 0)
  460. {
  461. // phpcs:enable
  462. global $langs;
  463. if ($mode == 0) {
  464. if ($status == 1) {
  465. return $langs->trans('Enabled');
  466. } elseif ($status == 0) {
  467. return $langs->trans('Disabled');
  468. }
  469. } elseif ($mode == 1) {
  470. if ($status == 1) {
  471. return $langs->trans('Enabled');
  472. } elseif ($status == 0) {
  473. return $langs->trans('Disabled');
  474. }
  475. } elseif ($mode == 2) {
  476. if ($status == 1) {
  477. return img_picto($langs->trans('Enabled'), 'statut4').' '.$langs->trans('Enabled');
  478. } elseif ($status == 0) {
  479. return img_picto($langs->trans('Disabled'), 'statut5').' '.$langs->trans('Disabled');
  480. }
  481. } elseif ($mode == 3) {
  482. if ($status == 1) {
  483. return img_picto($langs->trans('Enabled'), 'statut4');
  484. } elseif ($status == 0) {
  485. return img_picto($langs->trans('Disabled'), 'statut5');
  486. }
  487. } elseif ($mode == 4) {
  488. if ($status == 1) {
  489. return img_picto($langs->trans('Enabled'), 'statut4').' '.$langs->trans('Enabled');
  490. } elseif ($status == 0) {
  491. return img_picto($langs->trans('Disabled'), 'statut5').' '.$langs->trans('Disabled');
  492. }
  493. } elseif ($mode == 5) {
  494. if ($status == 1) {
  495. return $langs->trans('Enabled').' '.img_picto($langs->trans('Enabled'), 'statut4');
  496. } elseif ($status == 0) {
  497. return $langs->trans('Disabled').' '.img_picto($langs->trans('Disabled'), 'statut5');
  498. }
  499. }
  500. }
  501. /**
  502. * Initialise object with example values
  503. * Id must be 0 if object instance is a specimen
  504. *
  505. * @return void
  506. */
  507. public function initAsSpecimen()
  508. {
  509. $this->id = 0;
  510. $this->tms = '';
  511. $this->fk_product = null;
  512. $this->fk_entrepot = null;
  513. $this->seuil_stock_alerte = '';
  514. $this->desiredstock = '';
  515. $this->import_key = '';
  516. }
  517. }