inventory.class.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  1. <?php
  2. /* Copyright (C) 2007-2019 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. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file product/inventory/class/inventory.class.php
  22. * \ingroup inventory
  23. * \brief This file is a CRUD class file for Inventory (Create/Read/Update/Delete)
  24. */
  25. // Put here all includes required by your class file
  26. require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php';
  27. require_once DOL_DOCUMENT_ROOT . '/core/class/commonobjectline.class.php';
  28. require_once DOL_DOCUMENT_ROOT . '/custom/rollerstorage/class/statuses.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 for Inventory
  33. */
  34. class Inventory extends CommonObject
  35. {
  36. /**
  37. * @var string ID to identify managed object
  38. */
  39. public $element = 'inventory';
  40. /**
  41. * @var string Name of table without prefix where object is stored
  42. */
  43. public $table_element = 'inventory';
  44. /**
  45. * @var array Does inventory support multicompany module ? 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
  46. */
  47. public $ismultientitymanaged = 1;
  48. /**
  49. * @var int Does object support extrafields ? 0=No, 1=Yes
  50. */
  51. public $isextrafieldmanaged = 0;
  52. /**
  53. * @var string String with name of icon for inventory
  54. */
  55. public $picto = 'inventory';
  56. const STATUS_DRAFT = 0; // Draft
  57. const STATUS_VALIDATED = 1; // Inventory is in process
  58. const STATUS_RECORDED = 2; // Inventory is finisged. Stock movement has been recorded.
  59. const STATUS_CANCELED = 9; // Canceled
  60. /**
  61. * 'type' field format ('integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter]]', 'sellist:TableName:LabelFieldName[:KeyFieldName[:KeyFieldParent[:Filter]]]', 'varchar(x)', 'double(24,8)', 'real', 'price', 'text', 'text:none', 'html', 'date', 'datetime', 'timestamp', 'duration', 'mail', 'phone', 'url', 'password')
  62. * Note: Filter can be a string like "(t.ref:like:'SO-%') or (t.date_creation:<:'20160101') or (t.nature:is:NULL)"
  63. * 'label' the translation key.
  64. * 'picto' is code of a picto to show before value in forms
  65. * 'enabled' is a condition when the field must be managed (Example: 1 or '$conf->global->MY_SETUP_PARAM)
  66. * 'position' is the sort order of field.
  67. * 'notnull' is set to 1 if not null in database. Set to -1 if we must set data to null if empty ('' or 0).
  68. * 'visible' says if field is visible in list (Examples: 0=Not visible, 1=Visible on list and create/update/view forms, 2=Visible on list only, 3=Visible on create/update/view form only (not list), 4=Visible on list and update/view form only (not create). 5=Visible on list and view only (not create/not update). Using a negative value means field is not shown by default on list but can be selected for viewing)
  69. * 'noteditable' says if field is not editable (1 or 0)
  70. * 'default' is a default value for creation (can still be overwrote by the Setup of Default Values if field is editable in creation form). Note: If default is set to '(PROV)' and field is 'ref', the default value will be set to '(PROVid)' where id is rowid when a new record is created.
  71. * 'index' if we want an index in database.
  72. * 'foreignkey'=>'tablename.field' if the field is a foreign key (it is recommanded to name the field fk_...).
  73. * 'searchall' is 1 if we want to search in this field when making a search from the quick search button.
  74. * 'isameasure' must be set to 1 if you want to have a total on list for this field. Field type must be summable like integer or double(24,8).
  75. * 'css' and 'cssview' and 'csslist' is the CSS style to use on field. 'css' is used in creation and update. 'cssview' is used in view mode. 'csslist' is used for columns in lists. For example: 'maxwidth200', 'wordbreak', 'tdoverflowmax200', 'minwidth300 maxwidth500 widthcentpercentminusx'
  76. * 'help' is a 'TranslationString' to use to show a tooltip on field. You can also use 'TranslationString:keyfortooltiponlick' for a tooltip on click.
  77. * 'showoncombobox' if value of the field must be visible into the label of the combobox that list record
  78. * 'disabled' is 1 if we want to have the field locked by a 'disabled' attribute. In most cases, this is never set into the definition of $fields into class, but is set dynamically by some part of code.
  79. * 'arrayofkeyval' to set a list of values if type is a list of predefined values. For example: array("0"=>"Draft","1"=>"Active","-1"=>"Cancel"). Note that type can be 'integer' or 'varchar'
  80. * 'autofocusoncreate' to have field having the focus on a create form. Only 1 field should have this property set to 1.
  81. * 'comment' is not used. You can store here any text of your choice. It is not used by application.
  82. *
  83. * Note: To have value dynamic, you can set value to 0 in definition and edit the value on the fly into the constructor.
  84. */
  85. // BEGIN MODULEBUILDER PROPERTIES
  86. /**
  87. * @var array Array with all fields and their property
  88. */
  89. public $statuses = [];
  90. public $fields = array(
  91. 'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'visible' => -1, 'enabled' => 1, 'position' => 1, 'notnull' => 1, 'index' => 1, 'comment' => 'Id',),
  92. 'ref' => array('type' => 'varchar(64)', 'label' => 'DeviceName', 'visible' => 1, 'enabled' => 1, 'position' => 10, 'notnull' => 1, 'index' => 1, 'searchall' => 1, 'comment' => 'Reference of object', 'css' => 'maxwidth200'),
  93. 'entity' => array('type' => 'integer', 'label' => 'Entity', 'visible' => 0, 'enabled' => 1, 'position' => 20, 'notnull' => 1, 'index' => 1,),
  94. 'title' => array('type' => 'varchar(255)', 'label' => 'SKU', 'visible' => 1, 'enabled' => 1, 'position' => 25, 'notnull' => 1, 'css' => 'minwidth300', 'csslist' => 'tdoverflowmax200'),
  95. 'fk_warehouse' => array('type' => 'integer:Entrepot:product/stock/class/entrepot.class.php', 'label' => 'Warehouse', 'visible' => 1, 'enabled' => 1, 'position' => 30, 'notnull' => 1, 'index' => 1, 'help' => 'InventoryForASpecificWarehouse', 'picto' => 'stock', 'css' => 'minwidth300 maxwidth500 widthcentpercentminusx', 'csslist' => 'tdoverflowmax200'),
  96. 'fk_product' => array('type' => 'integer:Product:product/class/product.class.php', 'label' => 'Product', 'get_name_url_params' => '0::0:-1:0::1', 'visible' => 0, 'enabled' => 1, 'position' => 32, 'index' => 1, 'help' => 'InventoryForASpecificProduct', 'picto' => 'product', 'css' => 'minwidth300 maxwidth500 widthcentpercentminusx', 'csslist' => 'tdoverflowmax200'),
  97. 'categories_product' => array('type' => 'chkbxlst:categorie:label:rowid::type=0:0:', 'label' => 'OrProductsWithCategories', 'visible' => 3, 'enabled' => 0, 'position' => 33, 'help' => '', 'picto' => 'category', 'css' => 'minwidth300 maxwidth500 widthcentpercentminusx'),
  98. 'date_inventory' => array('type' => 'date', 'label' => 'DateValue', 'visible' => 1, 'enabled' => '$conf->global->STOCK_INVENTORY_ADD_A_VALUE_DATE', 'position' => 35, 'csslist' => 'nowraponall'),
  99. // This date is not used so disabled by default.
  100. 'date_creation' => array('type' => 'datetime', 'label' => 'DateCreation', 'enabled' => 1, 'visible' => -2, 'notnull' => 1, 'position' => 500, 'csslist' => 'nowraponall'),
  101. 'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'visible' => -2, 'notnull' => 1, 'position' => 501, 'csslist' => 'nowraponall'),
  102. 'date_validation' => array('type' => 'datetime', 'label' => 'DateValidation', 'visible' => -2, 'enabled' => 1, 'position' => 502, 'csslist' => 'nowraponall'),
  103. 'fk_user_creat' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserAuthor', 'enabled' => 1, 'visible' => -2, 'notnull' => 1, 'position' => 510, 'foreignkey' => 'user.rowid', 'csslist' => 'tdoverflowmax200'),
  104. 'fk_user_modif' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserModif', 'enabled' => 1, 'visible' => -2, 'notnull' => -1, 'position' => 511, 'csslist' => 'tdoverflowmax200'),
  105. 'fk_user_valid' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserValidation', 'visible' => -2, 'enabled' => 1, 'position' => 512, 'csslist' => 'tdoverflowmax200'),
  106. 'import_key' => array('type' => 'varchar(14)', 'label' => 'ImportId', 'enabled' => 1, 'visible' => -2, 'notnull' => -1, 'index' => 0, 'position' => 1000),
  107. 'status' => array('type' => 'integer:Statuses:custom/rollerstorage/class/statuses.class.php', 'label' => 'Status', 'visible' => 1, 'enabled' => 1, 'position' => 1000, 'notnull' => 1, 'default' => 110, 'index' => 1,)
  108. //'status' => array('type' => 'integer', 'label' => 'Status', 'visible' => 1, 'enabled' => 1, 'position' => 1000, 'notnull' => 1, 'default' => 110, 'index' => 1,)
  109. );
  110. /**
  111. * @var int ID
  112. */
  113. public $rowid;
  114. /**
  115. * @var string Ref
  116. */
  117. public $ref;
  118. /**
  119. * @var int Entity
  120. */
  121. public $entity;
  122. /**
  123. * @var int ID
  124. */
  125. public $fk_warehouse;
  126. /**
  127. * @var int ID
  128. */
  129. public $fk_product;
  130. /**
  131. * @var string Categories id separated by comma
  132. */
  133. public $categories_product;
  134. public $date_inventory;
  135. public $title;
  136. /**
  137. * @var int Status
  138. */
  139. public $status;
  140. /**
  141. * @var integer|string date_creation
  142. */
  143. public $date_creation;
  144. /**
  145. * @var integer|string date_validation
  146. */
  147. public $date_validation;
  148. public $tms;
  149. /**
  150. * @var int ID
  151. */
  152. public $fk_user_creat;
  153. /**
  154. * @var int ID
  155. */
  156. public $fk_user_modif;
  157. /**
  158. * @var int ID
  159. */
  160. public $fk_user_valid;
  161. /**
  162. * @var string import key
  163. */
  164. public $import_key;
  165. // END MODULEBUILDER PROPERTIES
  166. // If this object has a subtable with lines
  167. /**
  168. * @var string Name of subtable line
  169. */
  170. public $table_element_line = 'inventorydet';
  171. /**
  172. * @var string Field with ID of parent key if this field has a parent
  173. */
  174. public $fk_element = 'fk_inventory';
  175. /**
  176. * @var string Name of subtable class that manage subtable lines
  177. */
  178. public $class_element_line = 'Inventoryline';
  179. /**
  180. * @var array List of child tables. To test if we can delete object.
  181. */
  182. protected $childtables = array();
  183. /**
  184. * @var array List of child tables. To know object to delete on cascade.
  185. */
  186. protected $childtablesoncascade = array('inventorydet');
  187. /**
  188. * @var InventoryLine[] Array of subtable lines
  189. */
  190. public $lines = array();
  191. /**
  192. * Constructor
  193. *
  194. * @param DoliDb $db Database handler
  195. */
  196. public function __construct(DoliDB $db)
  197. {
  198. global $conf;
  199. $this->db = $db;
  200. if (empty($conf->global->MAIN_SHOW_TECHNICAL_ID)) {
  201. $this->fields['rowid']['visible'] = 0;
  202. }
  203. if (!isModEnabled('multicompany')) {
  204. $this->fields['entity']['enabled'] = 0;
  205. }
  206. }
  207. /**
  208. * Create object into database
  209. *
  210. * @param User $user User that creates
  211. * @param bool $notrigger false=launch triggers after, true=disable triggers
  212. * @return int <0 if KO, Id of created object if OK
  213. */
  214. public function create(User $user, $notrigger = false)
  215. {
  216. $result = $this->createCommon($user, $notrigger);
  217. return $result;
  218. }
  219. /**
  220. * Validate inventory (start it)
  221. *
  222. * @param User $user User that creates
  223. * @param bool $notrigger false=launch triggers after, true=disable triggers
  224. * @return int <0 if KO, Id of created object if OK
  225. */
  226. public function validate(User $user, $notrigger = false)
  227. {
  228. global $conf;
  229. $this->db->begin();
  230. $result = 0;
  231. if ($this->status == self::STATUS_DRAFT) {
  232. // Delete inventory
  233. $sql = 'DELETE FROM ' . $this->db->prefix() . 'inventorydet WHERE fk_inventory = ' . ((int) $this->id);
  234. $resql = $this->db->query($sql);
  235. if (!$resql) {
  236. $this->error = $this->db->lasterror();
  237. $this->db->rollback();
  238. return -1;
  239. }
  240. // Scan existing stock to prefill the inventory
  241. $sql = "SELECT ps.rowid, ps.fk_entrepot as fk_warehouse, ps.fk_product, ps.reel,";
  242. $sql .= " pb.batch, pb.qty";
  243. $sql .= " FROM " . $this->db->prefix() . "product_stock as ps";
  244. $sql .= " LEFT JOIN " . $this->db->prefix() . "product_batch as pb ON pb.fk_product_stock = ps.rowid,";
  245. $sql .= " " . $this->db->prefix() . "product as p, " . $this->db->prefix() . "entrepot as e";
  246. $sql .= " WHERE p.entity IN (" . getEntity('product') . ")";
  247. $sql .= " AND ps.fk_product = p.rowid AND ps.fk_entrepot = e.rowid";
  248. if (empty($conf->global->STOCK_SUPPORTS_SERVICES)) {
  249. $sql .= " AND p.fk_product_type = 0";
  250. }
  251. if ($this->fk_product > 0) {
  252. $sql .= " AND ps.fk_product = " . ((int) $this->fk_product);
  253. }
  254. if ($this->fk_warehouse > 0) {
  255. $sql .= " AND ps.fk_entrepot = " . ((int) $this->fk_warehouse);
  256. }
  257. if (!empty($this->categories_product)) {
  258. $sql .= " AND EXISTS (";
  259. $sql .= " SELECT cp.fk_product";
  260. $sql .= " FROM " . $this->db->prefix() . "categorie_product AS cp";
  261. $sql .= " WHERE cp.fk_product = ps.fk_product";
  262. $sql .= " AND cp.fk_categorie IN (" . $this->db->sanitize($this->categories_product) . ")";
  263. $sql .= ")";
  264. }
  265. $inventoryline = new InventoryLine($this->db);
  266. $resql = $this->db->query($sql);
  267. if ($resql) {
  268. $num = $this->db->num_rows($resql);
  269. $i = 0;
  270. while ($i < $num) {
  271. $obj = $this->db->fetch_object($resql);
  272. $inventoryline->fk_inventory = $this->id;
  273. $inventoryline->fk_warehouse = $obj->fk_warehouse;
  274. $inventoryline->fk_product = $obj->fk_product;
  275. $inventoryline->batch = $obj->batch;
  276. $inventoryline->datec = dol_now();
  277. if (isModEnabled('productbatch')) {
  278. $inventoryline->qty_stock = ($obj->batch ? $obj->qty : $obj->reel); // If there is batch detail, we take qty for batch, else global qty
  279. } else {
  280. $inventoryline->qty_stock = $obj->reel;
  281. }
  282. $resultline = $inventoryline->create($user);
  283. if ($resultline <= 0) {
  284. $this->error = $inventoryline->error;
  285. $this->errors = $inventoryline->errors;
  286. $result = -1;
  287. break;
  288. }
  289. $i++;
  290. }
  291. } else {
  292. $result = -1;
  293. $this->error = $this->db->lasterror();
  294. }
  295. }
  296. if ($result >= 0) {
  297. $result = $this->setStatut($this::STATUS_VALIDATED, null, '', 'INVENTORY_VALIDATED');
  298. }
  299. if ($result > 0) {
  300. $this->db->commit();
  301. } else {
  302. $this->db->rollback();
  303. }
  304. return $result;
  305. }
  306. /**
  307. * Go back to draft
  308. *
  309. * @param User $user User that creates
  310. * @param bool $notrigger false=launch triggers after, true=disable triggers
  311. * @return int <0 if KO, Id of created object if OK
  312. */
  313. public function setDraft(User $user, $notrigger = false)
  314. {
  315. $this->db->begin();
  316. // Delete inventory
  317. $sql = 'DELETE FROM ' . $this->db->prefix() . 'inventorydet WHERE fk_inventory = ' . ((int) $this->id);
  318. $resql = $this->db->query($sql);
  319. if (!$resql) {
  320. $this->error = $this->db->lasterror();
  321. $this->db->rollback();
  322. return -1;
  323. }
  324. $result = $this->setStatut($this::STATUS_DRAFT, null, '', 'INVENTORY_DRAFT');
  325. if ($result > 0) {
  326. $this->db->commit();
  327. } else {
  328. $this->db->rollback();
  329. }
  330. return $result;
  331. }
  332. /**
  333. * Set to inventory to status "Closed". It means all stock movements were recorded.
  334. *
  335. * @param User $user User that creates
  336. * @param bool $notrigger false=launch triggers after, true=disable triggers
  337. * @return int <0 if KO, Id of created object if OK
  338. */
  339. public function setRecorded(User $user, $notrigger = false)
  340. {
  341. $this->db->begin();
  342. $result = $this->setStatut($this::STATUS_RECORDED, null, '', 'INVENTORY_RECORDED');
  343. if ($result > 0) {
  344. $this->db->commit();
  345. } else {
  346. $this->db->rollback();
  347. return -1;
  348. }
  349. return $result;
  350. }
  351. /**
  352. * Set to Canceled
  353. *
  354. * @param User $user User that creates
  355. * @param bool $notrigger false=launch triggers after, true=disable triggers
  356. * @return int <0 if KO, Id of created object if OK
  357. */
  358. public function setCanceled(User $user, $notrigger = false)
  359. {
  360. $this->db->begin();
  361. $result = $this->setStatut($this::STATUS_CANCELED, null, '', 'INVENTORY_CANCELED');
  362. if ($result > 0) {
  363. $this->db->commit();
  364. } else {
  365. $this->db->rollback();
  366. return -1;
  367. }
  368. return $result;
  369. }
  370. /**
  371. * Clone and object into another one
  372. *
  373. * @param User $user User that creates
  374. * @param int $fromid Id of object to clone
  375. * @return mixed New object created, <0 if KO
  376. */
  377. public function createFromClone(User $user, $fromid)
  378. {
  379. global $hookmanager, $langs;
  380. $error = 0;
  381. dol_syslog(__METHOD__, LOG_DEBUG);
  382. $object = new self($this->db);
  383. $this->db->begin();
  384. // Load source object
  385. $object->fetchCommon($fromid);
  386. // Reset some properties
  387. unset($object->id);
  388. unset($object->fk_user_creat);
  389. unset($object->import_key);
  390. // Clear fields
  391. $object->ref = "copy_of_" . $object->ref;
  392. $object->title = $langs->trans("CopyOf") . " " . $object->title;
  393. // ...
  394. // Create clone
  395. $object->context['createfromclone'] = 'createfromclone';
  396. $result = $object->createCommon($user);
  397. if ($result < 0) {
  398. $error++;
  399. $this->error = $object->error;
  400. $this->errors = $object->errors;
  401. }
  402. unset($object->context['createfromclone']);
  403. // End
  404. if (!$error) {
  405. $this->db->commit();
  406. return $object;
  407. } else {
  408. $this->db->rollback();
  409. return -1;
  410. }
  411. }
  412. /**
  413. * Load object in memory from the database
  414. *
  415. * @param int $id Id object
  416. * @param string $ref Ref
  417. * @return int <0 if KO, 0 if not found, >0 if OK
  418. */
  419. public function fetch($id, $ref = null)
  420. {
  421. $result = $this->fetchCommon($id, $ref);
  422. //if ($result > 0 && !empty($this->table_element_line)) $this->fetchLines();
  423. return $result;
  424. }
  425. /**
  426. * Load object lines in memory from the database
  427. *
  428. * @return int <0 if KO, 0 if not found, >0 if OK
  429. */
  430. /*public function fetchLines()
  431. {
  432. $this->lines=array();
  433. // Load lines with object MyObjectLine
  434. return count($this->lines)?1:0;
  435. }*/
  436. /**
  437. * Update object into database
  438. *
  439. * @param User $user User that modifies
  440. * @param bool $notrigger false=launch triggers after, true=disable triggers
  441. * @return int <0 if KO, >0 if OK
  442. */
  443. public function update(User $user, $notrigger = false)
  444. {
  445. return $this->updateCommon($user, $notrigger);
  446. }
  447. /**
  448. * Delete object in database
  449. *
  450. * @param User $user User that deletes
  451. * @param bool $notrigger false=launch triggers after, true=disable triggers
  452. * @return int <0 if KO, >0 if OK
  453. */
  454. public function delete(User $user, $notrigger = false)
  455. {
  456. return $this->deleteCommon($user, $notrigger);
  457. }
  458. /**
  459. * Delete a line of object in database
  460. *
  461. * @param User $user User that delete
  462. * @param int $idline Id of line to delete
  463. * @param bool $notrigger false=launch triggers after, true=disable triggers
  464. * @return int >0 if OK, <0 if KO
  465. */
  466. public function deleteLine(User $user, $idline, $notrigger = false)
  467. {
  468. if ($this->status < 0) {
  469. $this->error = 'ErrorDeleteLineNotAllowedByObjectStatus';
  470. return -2;
  471. }
  472. return $this->deleteLineCommon($user, $idline, $notrigger);
  473. }
  474. /**
  475. * Return a link to the object card (with optionaly the picto)
  476. *
  477. * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
  478. * @param string $option On what the link point to
  479. * @param int $notooltip 1=Disable tooltip
  480. * @param string $morecss Add more css on link
  481. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  482. * @return string String with URL
  483. */
  484. public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
  485. {
  486. global $db, $conf, $langs;
  487. global $dolibarr_main_authentication, $dolibarr_main_demo;
  488. global $menumanager;
  489. if (!empty($conf->dol_no_mouse_hover)) {
  490. $notooltip = 1; // Force disable tooltips
  491. }
  492. $result = '';
  493. $companylink = '';
  494. $label = '<u>' . $langs->trans("Inventory") . '</u>';
  495. $label .= '<br>';
  496. $label .= '<b>' . $langs->trans('Ref') . ':</b> ' . $this->ref;
  497. $url = dol_buildpath('/product/inventory/card.php', 1) . '?id=' . $this->id;
  498. $linkclose = '';
  499. if (empty($notooltip)) {
  500. if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
  501. $label = $langs->trans("ShowInventory");
  502. $linkclose .= ' alt="' . dol_escape_htmltag($label, 1) . '"';
  503. }
  504. $linkclose .= ' title="' . dol_escape_htmltag($label, 1) . '"';
  505. $linkclose .= ' class="classfortooltip' . ($morecss ? ' ' . $morecss : '') . '"';
  506. } else {
  507. $linkclose = ($morecss ? ' class="' . $morecss . '"' : '');
  508. }
  509. $linkstart = '<a href="' . $url . '"';
  510. $linkstart .= $linkclose . '>';
  511. $linkend = '</a>';
  512. $result .= $linkstart;
  513. if ($withpicto) {
  514. $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="' . (($withpicto != 2) ? 'paddingright ' : '') . 'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
  515. }
  516. if ($withpicto != 2) {
  517. $result .= $this->ref;
  518. }
  519. $result .= $linkend;
  520. //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
  521. return $result;
  522. }
  523. /**
  524. * Retourne le libelle du status d'un user (actif, inactif)
  525. *
  526. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  527. * @return string Label of status
  528. */
  529. public function getLibStatut($mode = 0)
  530. {
  531. return $this->LibStatut($this->status, $mode);
  532. }
  533. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  534. /**
  535. * Return the status
  536. *
  537. * @param int $status Id status
  538. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 5=Long label + Picto, 6=Long label + Picto
  539. * @return string Label of status
  540. */
  541. public static function LibStatut($status, $mode = 0)
  542. {
  543. // phpcs:enable
  544. global $langs;
  545. global $db;
  546. $statusObj = new Statuses($db);
  547. $statuses = $statusObj::getStatusids($db);
  548. $labelStatus = array();
  549. $labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Draft');
  550. $labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Validated') . ' (' . $langs->transnoentitiesnoconv('InventoryStartedShort') . ')';
  551. $labelStatus[self::STATUS_CANCELED] = $langs->transnoentitiesnoconv('Canceled');
  552. $labelStatus[self::STATUS_RECORDED] = $langs->transnoentitiesnoconv('Closed');
  553. foreach ($statuses as $key => $val) {
  554. $labelStatus[$key] = $val;
  555. $labelStatusShort[$key] = $val;
  556. }
  557. $labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Draft');
  558. $labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('InventoryStartedShort');
  559. $labelStatusShort[self::STATUS_CANCELED] = $langs->transnoentitiesnoconv('Canceled');
  560. $labelStatusShort[self::STATUS_RECORDED] = $langs->transnoentitiesnoconv('Closed');
  561. $statusType = 'status' . $status;
  562. if ($status == self::STATUS_RECORDED) {
  563. $statusType = 'status6';
  564. }
  565. return dolGetStatus($labelStatus[$status], $labelStatusShort[$status], '', $statusType, $mode);
  566. }
  567. /**
  568. * Charge les informations d'ordre info dans l'objet commande
  569. *
  570. * @param int $id Id of order
  571. * @return void
  572. */
  573. public function info($id)
  574. {
  575. $sql = "SELECT rowid, date_creation as datec, tms as datem, date_validation as datev,";
  576. $sql .= " fk_user_creat, fk_user_modif, fk_user_valid";
  577. $sql .= " FROM " . $this->db->prefix() . $this->table_element . " as t";
  578. $sql .= " WHERE t.rowid = " . ((int) $id);
  579. $result = $this->db->query($sql);
  580. if ($result) {
  581. if ($this->db->num_rows($result)) {
  582. $obj = $this->db->fetch_object($result);
  583. $this->id = $obj->rowid;
  584. if ($obj->fk_user_creat > 0) {
  585. $cuser = new User($this->db);
  586. $cuser->fetch($obj->fk_user_creat);
  587. $this->user_creation = $cuser;
  588. }
  589. if ($obj->fk_user_modif > 0) {
  590. $muser = new User($this->db);
  591. $muser->fetch($obj->fk_user_modif);
  592. $this->user_creation = $muser;
  593. }
  594. if ($obj->fk_user_valid > 0) {
  595. $vuser = new User($this->db);
  596. $vuser->fetch($obj->fk_user_valid);
  597. $this->user_validation = $vuser;
  598. }
  599. $this->date_creation = $this->db->jdate($obj->datec);
  600. $this->date_modification = $this->db->jdate($obj->datem);
  601. $this->date_validation = $this->db->jdate($obj->datev);
  602. }
  603. $this->db->free($result);
  604. } else {
  605. dol_print_error($this->db);
  606. }
  607. }
  608. /**
  609. * Initialise object with example values
  610. * Id must be 0 if object instance is a specimen
  611. *
  612. * @return void
  613. */
  614. public function initAsSpecimen()
  615. {
  616. $this->initAsSpecimenCommon();
  617. $this->title = '';
  618. }
  619. }
  620. /**
  621. * Class InventoryLine
  622. */
  623. class InventoryLine extends CommonObjectLine
  624. {
  625. /**
  626. * @var string ID to identify managed object
  627. */
  628. public $element = 'inventoryline';
  629. /**
  630. * @var string Name of table without prefix where object is stored
  631. */
  632. public $table_element = 'inventorydet';
  633. /**
  634. * @var array Does inventory support multicompany module ? 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
  635. */
  636. public $ismultientitymanaged = 0;
  637. /**
  638. * @var int Does object support extrafields ? 0=No, 1=Yes
  639. */
  640. public $isextrafieldmanaged = 0;
  641. /**
  642. * @var string String with name of icon for inventory
  643. */
  644. public $picto = 'stock';
  645. /**
  646. * 'type' if the field format.
  647. * 'label' the translation key.
  648. * 'enabled' is a condition when the field must be managed.
  649. * 'visible' says if field is visible in list (Examples: 0=Not visible, 1=Visible on list and create/update/view forms, 2=Visible on list only. Using a negative value means field is not shown by default on list but can be selected for viewing)
  650. * 'notnull' is set to 1 if not null in database. Set to -1 if we must set data to null if empty ('' or 0).
  651. * 'index' if we want an index in database.
  652. * 'foreignkey'=>'tablename.field' if the field is a foreign key (it is recommanded to name the field fk_...).
  653. * 'position' is the sort order of field.
  654. * 'searchall' is 1 if we want to search in this field when making a search from the quick search button.
  655. * 'isameasure' must be set to 1 if you want to have a total on list for this field. Field type must be summable like integer or double(24,8).
  656. * 'help' is a string visible as a tooltip on field
  657. * 'comment' is not used. You can store here any text of your choice. It is not used by application.
  658. * 'default' is a default value for creation (can still be replaced by the global setup of default values)
  659. * 'showoncombobox' if field must be shown into the label of combobox
  660. */
  661. // BEGIN MODULEBUILDER PROPERTIES
  662. /**
  663. * @var array Array with all fields and their property
  664. */
  665. public $fields = array(
  666. 'rowid' => array(
  667. 'type' => 'integer',
  668. 'label' => 'TechnicalID',
  669. 'visible' => -1,
  670. 'enabled' => 1,
  671. 'position' => 1,
  672. 'notnull' => 1,
  673. 'index' => 1,
  674. 'comment' => 'Id',
  675. ),
  676. 'fk_inventory' => array('type' => 'integer:Inventory:product/inventory/class/inventory.class.php', 'label' => 'Inventory', 'visible' => 1, 'enabled' => 1, 'position' => 30, 'index' => 1, 'help' => 'LinkToInventory'),
  677. 'fk_warehouse' => array('type' => 'integer:Entrepot:product/stock/class/entrepot.class.php', 'label' => 'Warehouse', 'visible' => 1, 'enabled' => 1, 'position' => 30, 'index' => 1, 'help' => 'LinkToThirdparty'),
  678. 'fk_product' => array('type' => 'integer:Product:product/class/product.class.php', 'label' => 'Product', 'visible' => 1, 'enabled' => 1, 'position' => 32, 'index' => 1, 'help' => 'LinkToProduct'),
  679. 'batch' => array('type' => 'string', 'label' => 'Batch', 'visible' => 1, 'enabled' => 1, 'position' => 32, 'index' => 1, 'help' => 'LinkToProduct'),
  680. 'datec' => array('type' => 'datetime', 'label' => 'DateCreation', 'enabled' => 1, 'visible' => -2, 'notnull' => 1, 'position' => 500),
  681. 'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'visible' => -2, 'notnull' => 1, 'position' => 501),
  682. 'qty_stock' => array('type' => 'double', 'label' => 'QtyFound', 'visible' => 1, 'enabled' => 1, 'position' => 32, 'index' => 1, 'help' => 'Qty we found/want (to define during draft edition)'),
  683. 'qty_view' => array('type' => 'double', 'label' => 'QtyBefore', 'visible' => 1, 'enabled' => 1, 'position' => 33, 'index' => 1, 'help' => 'Qty before (filled once movements are validated)'),
  684. 'qty_regulated' => array('type' => 'double', 'label' => 'QtyDelta', 'visible' => 1, 'enabled' => 1, 'position' => 34, 'index' => 1, 'help' => 'Qty aadded or removed (filled once movements are validated)'),
  685. 'pmp_real' => array('type' => 'double', 'label' => 'PMPReal', 'visible' => 1, 'enabled' => 1, 'position' => 35),
  686. 'pmp_expected' => array('type' => 'double', 'label' => 'PMPExpected', 'visible' => 1, 'enabled' => 1, 'position' => 36),
  687. );
  688. /**
  689. * @var int ID
  690. */
  691. public $rowid;
  692. public $fk_inventory;
  693. public $fk_warehouse;
  694. public $fk_product;
  695. public $batch;
  696. public $datec;
  697. public $tms;
  698. public $qty_stock;
  699. public $qty_view;
  700. public $qty_regulated;
  701. public $pmp_real;
  702. public $pmp_expected;
  703. /**
  704. * Create object in database
  705. *
  706. * @param User $user User that creates
  707. * @param bool $notrigger false=launch triggers after, true=disable triggers
  708. * @return int <0 if KO, >0 if OK
  709. */
  710. public function create(User $user, $notrigger = false)
  711. {
  712. return $this->createCommon($user, $notrigger);
  713. }
  714. /**
  715. * Load object in memory from the database
  716. *
  717. * @param int $id Id object
  718. * @param string $ref Ref
  719. * @return int <0 if KO, 0 if not found, >0 if OK
  720. */
  721. public function fetch($id, $ref = null)
  722. {
  723. $result = $this->fetchCommon($id, $ref);
  724. //if ($result > 0 && !empty($this->table_element_line)) $this->fetchLines();
  725. return $result;
  726. }
  727. /**
  728. * Update object into database
  729. *
  730. * @param User $user User that modifies
  731. * @param bool $notrigger false=launch triggers after, true=disable triggers
  732. * @return int <0 if KO, >0 if OK
  733. */
  734. public function update(User $user, $notrigger = false)
  735. {
  736. return $this->updateCommon($user, $notrigger);
  737. }
  738. /**
  739. * Delete object in database
  740. *
  741. * @param User $user User that deletes
  742. * @param bool $notrigger false=launch triggers after, true=disable triggers
  743. * @return int <0 if KO, >0 if OK
  744. */
  745. public function delete(User $user, $notrigger = false)
  746. {
  747. return $this->deleteCommon($user, $notrigger);
  748. //return $this->deleteCommon($user, $notrigger, 1);
  749. }
  750. }