inventory.class.php 31 KB

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