productlot.class.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. <?php
  2. /* Copyright (C) 2007-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2014 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 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 product/stock/class/productlot.class.php
  23. * \ingroup stock
  24. * \brief This is CRUD class file to manage table productlot (Create/Read/Update/Delete)
  25. */
  26. // Put here all includes required by your class file
  27. require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
  28. //require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php';
  29. //require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
  30. /**
  31. * Class with list of lots and properties
  32. */
  33. class Productlot extends CommonObject
  34. {
  35. /**
  36. * @var string Id to identify managed objects
  37. */
  38. public $element = 'productlot';
  39. /**
  40. * @var string Name of table without prefix where object is stored
  41. */
  42. public $table_element = 'product_lot';
  43. /**
  44. * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
  45. */
  46. public $picto = 'lot';
  47. /**
  48. * 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
  49. * @var int
  50. */
  51. public $ismultientitymanaged = 1;
  52. /**
  53. * 'type' if the field format ('integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter]]', 'varchar(x)', 'double(24,8)', 'real', 'price', 'text', 'html', 'date', 'datetime', 'timestamp', 'duration', 'mail', 'phone', 'url', 'password')
  54. * Note: Filter can be a string like "(t.ref:like:'SO-%') or (t.date_creation:<:'20160101') or (t.nature:is:NULL)"
  55. * 'label' the translation key.
  56. * 'enabled' is a condition when the field must be managed (Example: 1 or '$conf->global->MY_SETUP_PARAM)
  57. * 'position' is the sort order of field.
  58. * 'notnull' is set to 1 if not null in database. Set to -1 if we must set data to null if empty ('' or 0).
  59. * '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)
  60. * 'noteditable' says if field is not editable (1 or 0)
  61. * '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.
  62. * 'index' if we want an index in database.
  63. * 'foreignkey'=>'tablename.field' if the field is a foreign key (it is recommanded to name the field fk_...).
  64. * 'searchall' is 1 if we want to search in this field when making a search from the quick search button.
  65. * '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).
  66. * 'css' is the CSS style to use on field. For example: 'maxwidth200'
  67. * 'help' is a string visible as a tooltip on field
  68. * 'showoncombobox' if value of the field must be visible into the label of the combobox that list record
  69. * '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.
  70. * 'arrayofkeyval' to set list of value if type is a list of predefined values. For example: array("0"=>"Draft","1"=>"Active","-1"=>"Cancel")
  71. * 'autofocusoncreate' to have field having the focus on a create form. Only 1 field should have this property set to 1.
  72. * 'comment' is not used. You can store here any text of your choice. It is not used by application.
  73. *
  74. * Note: To have value dynamic, you can set value to 0 in definition and edit the value on the fly into the constructor.
  75. */
  76. /**
  77. * @var array Array with all fields and their property. Do not use it as a static var. It may be modified by constructor.
  78. */
  79. public $fields = array(
  80. 'rowid' => array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-2, 'noteditable'=>1, 'notnull'=> 1, 'index'=>1, 'position'=>1, 'comment'=>'Id', 'css'=>'left'),
  81. 'fk_product' => array('type'=>'integer:Product:product/class/product.class.php', 'label'=>'Product', 'enabled'=>1, 'visible'=>1, 'position'=>5, 'notnull'=>1, 'index'=>1, 'searchall'=>1),
  82. 'batch' => array('type'=>'varchar(30)', 'label'=>'Batch', 'enabled'=>1, 'visible'=>1, 'notnull'=>0, 'showoncombobox'=>1, 'index'=>1, 'position'=>10, 'comment'=>'Batch', 'searchall'=>1),
  83. 'entity' => array('type'=>'integer', 'label'=>'Entity', 'enabled'=>1, 'visible'=>0, 'default'=>1, 'notnull'=>1, 'index'=>1, 'position'=>20),
  84. 'sellby' => array('type'=>'date', 'label'=>'SellByDate', 'enabled'=>'empty($conf->global->PRODUCT_DISABLE_SELLBY)?1:0', 'visible'=>5, 'position'=>60),
  85. 'eol_date' => array('type'=>'date', 'label'=>'EndOfLife', 'enabled'=>'empty($conf->global->PRODUCT_ENABLE_TRACEABILITY)?0:1', 'visible'=>5, 'position'=>70),
  86. 'manufacturing_date' => array('type'=>'date', 'label'=>'ManufacturingDate', 'enabled'=>'empty($conf->global->PRODUCT_ENABLE_TRACEABILITY)?0:1', 'visible'=>5, 'position'=>80),
  87. 'scrapping_date' => array('type'=>'date', 'label'=>'DestructionDate', 'enabled'=>'empty($conf->global->PRODUCT_ENABLE_TRACEABILITY)?0:1', 'visible'=>5, 'position'=>90),
  88. //'commissionning_date' => array('type'=>'date', 'label'=>'FirstUseDate', 'enabled'=>'empty($conf->global->PRODUCT_ENABLE_TRACEABILITY)?0:1', 'visible'=>5, 'position'=>100),
  89. //'qc_frequency' => array('type'=>'varchar(6)', 'label'=>'QCFrequency', 'enabled'=>'empty($conf->global->PRODUCT_ENABLE_QUALITYCONTROL)?1:0', 'visible'=>5, 'position'=>110),
  90. 'eatby' => array('type'=>'date', 'label'=>'EatByDate', 'enabled'=>'empty($conf->global->PRODUCT_DISABLE_EATBY)?1:0', 'visible'=>5, 'position'=>62),
  91. 'datec' => array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'position'=>500),
  92. 'tms' => array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-2, 'notnull'=>1, 'position'=>501),
  93. 'fk_user_creat' => array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserAuthor', 'enabled'=>1, 'visible'=>-2, 'notnull'=>1, 'position'=>510, 'foreignkey'=>'llx_user.rowid'),
  94. 'fk_user_modif' => array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserModif', 'enabled'=>1, 'visible'=>-2, 'notnull'=>-1, 'position'=>511),
  95. 'import_key' => array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-2, 'notnull'=>-1, 'index'=>0, 'position'=>1000)
  96. );
  97. /**
  98. * @var int Entity
  99. */
  100. public $entity;
  101. /**
  102. * @var int Product ID
  103. */
  104. public $fk_product;
  105. /**
  106. * @var string batch ref
  107. */
  108. public $batch;
  109. public $eatby = '';
  110. public $sellby = '';
  111. public $eol_date = '';
  112. public $manufacturing_date = '';
  113. public $scrapping_date = '';
  114. //public $commissionning_date = '';
  115. //public $qc_frequency = '';
  116. public $datec = '';
  117. public $tms = '';
  118. /**
  119. * @var int user ID
  120. */
  121. public $fk_user_creat;
  122. /**
  123. * @var int user ID
  124. */
  125. public $fk_user_modif;
  126. /**
  127. * @var string import key
  128. */
  129. public $import_key;
  130. /**
  131. * Constructor
  132. *
  133. * @param DoliDb $db Database handler
  134. */
  135. public function __construct(DoliDB $db)
  136. {
  137. $this->db = $db;
  138. }
  139. /**
  140. * Create object into database
  141. *
  142. * @param User $user User that creates
  143. * @param bool $notrigger false=launch triggers after, true=disable triggers
  144. *
  145. * @return int <0 if KO, Id of created object if OK
  146. */
  147. public function create(User $user, $notrigger = false)
  148. {
  149. global $conf;
  150. dol_syslog(__METHOD__, LOG_DEBUG);
  151. $error = 0;
  152. // Clean parameters
  153. if (isset($this->entity)) {
  154. $this->entity = (int) $this->entity;
  155. }
  156. if (isset($this->fk_product)) {
  157. $this->fk_product = (int) $this->fk_product;
  158. }
  159. if (isset($this->batch)) {
  160. $this->batch = trim($this->batch);
  161. }
  162. if (isset($this->fk_user_creat)) {
  163. $this->fk_user_creat = (int) $this->fk_user_creat;
  164. }
  165. if (isset($this->fk_user_modif)) {
  166. $this->fk_user_modif = (int) $this->fk_user_modif;
  167. }
  168. if (isset($this->import_key)) {
  169. $this->import_key = trim($this->import_key);
  170. }
  171. // Check parameters
  172. // Put here code to add control on parameters values
  173. // Insert request
  174. $sql = 'INSERT INTO '.$this->db->prefix().$this->table_element.'(';
  175. $sql .= 'entity,';
  176. $sql .= 'fk_product,';
  177. $sql .= 'batch,';
  178. $sql .= 'eatby,';
  179. $sql .= 'sellby,';
  180. $sql .= 'eol_date,';
  181. $sql .= 'manufacturing_date,';
  182. $sql .= 'scrapping_date,';
  183. //$sql .= 'commissionning_date,';
  184. //$sql .= 'qc_frequency,';
  185. $sql .= 'datec,';
  186. $sql .= 'fk_user_creat,';
  187. $sql .= 'fk_user_modif,';
  188. $sql .= 'import_key';
  189. $sql .= ') VALUES (';
  190. $sql .= ' '.(!isset($this->entity) ? $conf->entity : $this->entity).',';
  191. $sql .= ' '.(!isset($this->fk_product) ? 'NULL' : $this->fk_product).',';
  192. $sql .= ' '.(!isset($this->batch) ? 'NULL' : "'".$this->db->escape($this->batch)."'").',';
  193. $sql .= ' '.(!isset($this->eatby) || dol_strlen($this->eatby) == 0 ? 'NULL' : "'".$this->db->idate($this->eatby)."'").',';
  194. $sql .= ' '.(!isset($this->sellby) || dol_strlen($this->sellby) == 0 ? 'NULL' : "'".$this->db->idate($this->sellby)."'").',';
  195. $sql .= ' '.(!isset($this->eol_date) || dol_strlen($this->eol_date) == 0 ? 'NULL' : "'".$this->db->idate($this->eol_date)."'").',';
  196. $sql .= ' '.(!isset($this->manufacturing_date) || dol_strlen($this->manufacturing_date) == 0 ? 'NULL' : "'".$this->db->idate($this->manufacturing_date)."'").',';
  197. $sql .= ' '.(!isset($this->scrapping_date) || dol_strlen($this->scrapping_date) == 0 ? 'NULL' : "'".$this->db->idate($this->scrapping_date)."'").',';
  198. //$sql .= ' '.(!isset($this->commissionning_date) || dol_strlen($this->commissionning_date) == 0 ? 'NULL' : "'".$this->db->idate($this->commissionning_date)."'").',';
  199. //$sql .= ' '.(!isset($this->qc_frequency) ? 'NULL' : $this->qc_frequency).',';
  200. $sql .= ' '."'".$this->db->idate(dol_now())."'".',';
  201. $sql .= ' '.(!isset($this->fk_user_creat) ? 'NULL' : $this->fk_user_creat).',';
  202. $sql .= ' '.(!isset($this->fk_user_modif) ? 'NULL' : $this->fk_user_modif).',';
  203. $sql .= ' '.(!isset($this->import_key) ? 'NULL' : $this->import_key);
  204. $sql .= ')';
  205. $this->db->begin();
  206. $resql = $this->db->query($sql);
  207. if (!$resql) {
  208. $error++;
  209. $this->errors[] = 'Error '.$this->db->lasterror();
  210. dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
  211. }
  212. if (!$error) {
  213. $this->id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
  214. // Actions on extra fields
  215. if (!$error) {
  216. $result = $this->insertExtraFields();
  217. if ($result < 0) {
  218. $error++;
  219. }
  220. }
  221. if (!$error && !$notrigger) {
  222. // Call triggers
  223. $result = $this->call_trigger('PRODUCTLOT_CREATE', $user);
  224. if ($result < 0) {
  225. $error++;
  226. }
  227. // End call triggers
  228. }
  229. }
  230. // Commit or rollback
  231. if ($error) {
  232. $this->db->rollback();
  233. return -1 * $error;
  234. } else {
  235. $this->db->commit();
  236. return $this->id;
  237. }
  238. }
  239. /**
  240. * Load object in memory from the database
  241. *
  242. * @param int $id Id of lot/batch
  243. * @param int $product_id Id of product, batch number parameter required
  244. * @param string $batch batch number
  245. *
  246. * @return int <0 if KO, 0 if not found, >0 if OK
  247. */
  248. public function fetch($id = 0, $product_id = 0, $batch = '')
  249. {
  250. global $conf;
  251. dol_syslog(__METHOD__, LOG_DEBUG);
  252. $sql = "SELECT";
  253. $sql .= " t.rowid,";
  254. $sql .= " t.entity,";
  255. $sql .= " t.fk_product,";
  256. $sql .= " t.batch,";
  257. $sql .= " t.eatby,";
  258. $sql .= " t.sellby,";
  259. $sql .= " t.eol_date,";
  260. $sql .= " t.manufacturing_date,";
  261. $sql .= " t.scrapping_date,";
  262. //$sql .= " t.commissionning_date,";
  263. //$sql .= " t.qc_frequency,";
  264. $sql .= " t.datec,";
  265. $sql .= " t.tms,";
  266. $sql .= " t.fk_user_creat,";
  267. $sql .= " t.fk_user_modif,";
  268. $sql .= " t.import_key,";
  269. $sql .= " t.note_public,";
  270. $sql .= " t.note_private";
  271. $sql .= " FROM ".$this->db->prefix().$this->table_element." as t";
  272. if ($product_id > 0 && $batch != '') {
  273. $sql .= " WHERE t.batch = '".$this->db->escape($batch)."' AND t.fk_product = ".((int) $product_id);
  274. } else {
  275. $sql .= " WHERE t.rowid = ".((int) $id);
  276. }
  277. $resql = $this->db->query($sql);
  278. if ($resql) {
  279. $numrows = $this->db->num_rows($resql);
  280. if ($numrows) {
  281. $obj = $this->db->fetch_object($resql);
  282. $this->id = $obj->rowid;
  283. $this->ref = $obj->rowid;
  284. //$this->ref = $obj->fk_product.'_'.$obj->batch;
  285. $this->batch = $obj->batch;
  286. $this->entity = (!empty($obj->entity) ? $obj->entity : $conf->entity); // Prevent "null" entity
  287. $this->fk_product = $obj->fk_product;
  288. $this->eatby = $this->db->jdate($obj->eatby);
  289. $this->sellby = $this->db->jdate($obj->sellby);
  290. $this->eol_date = $this->db->jdate($obj->eol_date);
  291. $this->manufacturing_date = $this->db->jdate($obj->manufacturing_date);
  292. $this->scrapping_date = $this->db->jdate($obj->scrapping_date);
  293. //$this->commissionning_date = $this->db->jdate($obj->commissionning_date);
  294. //$this->qc_frequency = $obj->qc_frequency;
  295. $this->datec = $this->db->jdate($obj->datec);
  296. $this->tms = $this->db->jdate($obj->tms);
  297. $this->fk_user_creat = $obj->fk_user_creat;
  298. $this->fk_user_modif = $obj->fk_user_modif;
  299. $this->import_key = $obj->import_key;
  300. $this->note_public = $obj->note_public;
  301. $this->note_private = $obj->note_private;
  302. // Retrieve all extrafield
  303. // fetch optionals attributes and labels
  304. $this->fetch_optionals();
  305. }
  306. $this->db->free($resql);
  307. if ($numrows) {
  308. return 1;
  309. } else {
  310. return 0;
  311. }
  312. } else {
  313. $this->errors[] = 'Error '.$this->db->lasterror();
  314. dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
  315. return -1;
  316. }
  317. }
  318. /**
  319. * Update object into database
  320. *
  321. * @param User $user User that modifies
  322. * @param bool $notrigger false=launch triggers after, true=disable triggers
  323. *
  324. * @return int <0 if KO, >0 if OK
  325. */
  326. public function update(User $user, $notrigger = false)
  327. {
  328. $error = 0;
  329. dol_syslog(__METHOD__, LOG_DEBUG);
  330. // Clean parameters
  331. if (isset($this->entity)) {
  332. $this->entity = (int) $this->entity;
  333. }
  334. if (isset($this->fk_product)) {
  335. $this->fk_product = (int) $this->fk_product;
  336. }
  337. if (isset($this->batch)) {
  338. $this->batch = trim($this->batch);
  339. }
  340. if (isset($this->fk_user_creat)) {
  341. $this->fk_user_creat = (int) $this->fk_user_creat;
  342. }
  343. if (isset($this->fk_user_modif)) {
  344. $this->fk_user_modif = (int) $this->fk_user_modif;
  345. }
  346. if (isset($this->import_key)) {
  347. $this->import_key = trim($this->import_key);
  348. }
  349. // $this->oldcopy should have been set by the caller of update (here properties were already modified)
  350. if (empty($this->oldcopy)) {
  351. $this->oldcopy = dol_clone($this);
  352. }
  353. // Update request
  354. $sql = 'UPDATE '.$this->db->prefix().$this->table_element.' SET';
  355. $sql .= ' entity = '.(isset($this->entity) ? $this->entity : "null").',';
  356. $sql .= ' fk_product = '.(isset($this->fk_product) ? $this->fk_product : "null").',';
  357. $sql .= ' batch = '.(isset($this->batch) ? "'".$this->db->escape($this->batch)."'" : "null").',';
  358. $sql .= ' eatby = '.(!isset($this->eatby) || dol_strlen($this->eatby) != 0 ? "'".$this->db->idate($this->eatby)."'" : 'null').',';
  359. $sql .= ' sellby = '.(!isset($this->sellby) || dol_strlen($this->sellby) != 0 ? "'".$this->db->idate($this->sellby)."'" : 'null').',';
  360. $sql .= ' eol_date = '.(!isset($this->eol_date) || dol_strlen($this->eol_date) != 0 ? "'".$this->db->idate($this->eol_date)."'" : 'null').',';
  361. $sql .= ' manufacturing_date = '.(!isset($this->manufacturing_date) || dol_strlen($this->manufacturing_date) != 0 ? "'".$this->db->idate($this->manufacturing_date)."'" : 'null').',';
  362. $sql .= ' scrapping_date = '.(!isset($this->scrapping_date) || dol_strlen($this->scrapping_date) != 0 ? "'".$this->db->idate($this->scrapping_date)."'" : 'null').',';
  363. //$sql .= ' commissionning_date = '.(!isset($this->first_use_date) || dol_strlen($this->first_use_date) != 0 ? "'".$this->db->idate($this->first_use_date)."'" : 'null').',';
  364. //$sql .= ' qc_frequency = '.(!isset($this->qc_frequency) || dol_strlen($this->qc_frequency) != 0 ? "'".$this->db->escape($this->qc_frequency)."'" : 'null').',';
  365. $sql .= ' datec = '.(!isset($this->datec) || dol_strlen($this->datec) != 0 ? "'".$this->db->idate($this->datec)."'" : 'null').',';
  366. $sql .= ' tms = '.(dol_strlen($this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : "'".$this->db->idate(dol_now())."'").',';
  367. $sql .= ' fk_user_creat = '.(isset($this->fk_user_creat) ? $this->fk_user_creat : "null").',';
  368. $sql .= ' fk_user_modif = '.(isset($this->fk_user_modif) ? $this->fk_user_modif : "null").',';
  369. $sql .= ' import_key = '.(isset($this->import_key) ? $this->import_key : "null");
  370. $sql .= ' WHERE rowid='.((int) $this->id);
  371. $this->db->begin();
  372. $resql = $this->db->query($sql);
  373. if (!$resql) {
  374. $error++;
  375. $this->errors[] = 'Error '.$this->db->lasterror();
  376. dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
  377. }
  378. // Actions on extra fields
  379. if (!$error) {
  380. $result = $this->insertExtraFields();
  381. if ($result < 0) {
  382. $error++;
  383. }
  384. }
  385. if (!$error && !$notrigger) {
  386. // Call triggers
  387. $result = $this->call_trigger('PRODUCTLOT_MODIFY', $user);
  388. if ($result < 0) {
  389. $error++;
  390. }
  391. // End call triggers
  392. }
  393. // Commit or rollback
  394. if ($error) {
  395. $this->db->rollback();
  396. return -1 * $error;
  397. } else {
  398. $this->db->commit();
  399. return 1;
  400. }
  401. }
  402. /**
  403. * Delete object in database
  404. *
  405. * @param User $user User that deletes
  406. * @param bool $notrigger false=launch triggers after, true=disable triggers
  407. *
  408. * @return int <0 if KO, >0 if OK
  409. */
  410. public function delete(User $user, $notrigger = false)
  411. {
  412. dol_syslog(__METHOD__, LOG_DEBUG);
  413. $error = 0;
  414. $this->db->begin();
  415. //if (!$error) {
  416. //if (!$notrigger) {
  417. // Uncomment this and change PRODUCTLOT to your own tag if you
  418. // want this action calls a trigger.
  419. //// Call triggers
  420. //$result=$this->call_trigger('PRODUCTLOT_DELETE',$user);
  421. //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
  422. //// End call triggers
  423. //}
  424. //}
  425. if (!$error) {
  426. $sql = 'DELETE FROM '.$this->db->prefix().$this->table_element;
  427. $sql .= ' WHERE rowid='.((int) $this->id);
  428. $resql = $this->db->query($sql);
  429. if (!$resql) {
  430. $error++;
  431. $this->errors[] = 'Error '.$this->db->lasterror();
  432. dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
  433. }
  434. }
  435. // Commit or rollback
  436. if ($error) {
  437. $this->db->rollback();
  438. return -1 * $error;
  439. } else {
  440. $this->db->commit();
  441. return 1;
  442. }
  443. }
  444. /**
  445. * Load an object from its id and create a new one in database
  446. *
  447. * @param User $user User making the clone
  448. * @param int $fromid Id of object to clone
  449. * @return int New id of clone
  450. */
  451. public function createFromClone(User $user, $fromid)
  452. {
  453. dol_syslog(__METHOD__, LOG_DEBUG);
  454. $error = 0;
  455. $object = new Productlot($this->db);
  456. $this->db->begin();
  457. // Load source object
  458. $object->fetch($fromid);
  459. // Reset object
  460. $object->id = 0;
  461. // Clear fields
  462. // ...
  463. // Create clone
  464. $object->context['createfromclone'] = 'createfromclone';
  465. $result = $object->create($user);
  466. // Other options
  467. if ($result < 0) {
  468. $error++;
  469. $this->errors = $object->errors;
  470. dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
  471. }
  472. unset($object->context['createfromclone']);
  473. // End
  474. if (!$error) {
  475. $this->db->commit();
  476. return $object->id;
  477. } else {
  478. $this->db->rollback();
  479. return -1;
  480. }
  481. }
  482. /**
  483. * Return label of status of object
  484. *
  485. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto
  486. * @return string Label of status
  487. */
  488. public function getLibStatut($mode = 0)
  489. {
  490. return $this->LibStatut(0, $mode);
  491. }
  492. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  493. /**
  494. * Return label of a given status
  495. *
  496. * @param int $status Status
  497. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto
  498. * @return string Label of status
  499. */
  500. public function LibStatut($status, $mode = 0)
  501. {
  502. // phpcs:enable
  503. global $langs;
  504. //$langs->load('stocks');
  505. return '';
  506. }
  507. /**
  508. * Return a link to the a lot card (with optionaly the picto)
  509. * Use this->id,this->lastname, this->firstname
  510. *
  511. * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
  512. * @param string $option On what the link point to
  513. * @param integer $notooltip 1=Disable tooltip
  514. * @param int $maxlen Max length of visible user name
  515. * @param string $morecss Add more css on link
  516. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  517. * @return string String with URL
  518. */
  519. public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $maxlen = 24, $morecss = '', $save_lastsearch_value = -1)
  520. {
  521. global $langs, $conf, $db;
  522. global $dolibarr_main_authentication, $dolibarr_main_demo;
  523. global $menumanager;
  524. $result = '';
  525. $label = img_picto('', $this->picto).' <u>'.$langs->trans("Batch").'</u>';
  526. $label .= '<div width="100%">';
  527. $label .= '<b>'.$langs->trans('Batch').':</b> '.$this->batch;
  528. if ($this->eatby && empty($conf->global->PRODUCT_DISABLE_EATBY)) {
  529. $label .= '<br><b>'.$langs->trans('EatByDate').':</b> '.dol_print_date($this->eatby, 'day');
  530. }
  531. if ($this->sellby && empty($conf->global->PRODUCT_DISABLE_SELLBY)) {
  532. $label .= '<br><b>'.$langs->trans('SellByDate').':</b> '.dol_print_date($this->sellby, 'day');
  533. }
  534. $url = DOL_URL_ROOT.'/product/stock/productlot_card.php?id='.$this->id;
  535. if ($option != 'nolink') {
  536. // Add param to save lastsearch_values or not
  537. $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
  538. if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
  539. $add_save_lastsearch_values = 1;
  540. }
  541. if ($add_save_lastsearch_values) {
  542. $url .= '&save_lastsearch_values=1';
  543. }
  544. }
  545. $linkclose = '';
  546. if (empty($notooltip)) {
  547. if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
  548. $label = $langs->trans("ShowMyObject");
  549. $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
  550. }
  551. $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
  552. $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
  553. } else {
  554. $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
  555. }
  556. if ($option == 'nolink') {
  557. $linkstart = '<span';
  558. } else {
  559. $linkstart = '<a href="'.$url.'"';
  560. }
  561. $linkstart .= $linkclose.'>';
  562. if ($option == 'nolink') {
  563. $linkend = '</span>';
  564. } else {
  565. $linkend = '</a>';
  566. }
  567. $result .= $linkstart;
  568. if ($withpicto) {
  569. $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);
  570. }
  571. if ($withpicto != 2) {
  572. $result .= $this->batch;
  573. }
  574. $result .= $linkend;
  575. return $result;
  576. }
  577. /**
  578. * Initialise object with example values
  579. * Id must be 0 if object instance is a specimen
  580. *
  581. * @return void
  582. */
  583. public function initAsSpecimen()
  584. {
  585. $this->id = 0;
  586. $this->entity = null;
  587. $this->fk_product = null;
  588. $this->batch = '';
  589. $this->eatby = '';
  590. $this->sellby = '';
  591. $this->datec = '';
  592. $this->tms = '';
  593. $this->fk_user_creat = null;
  594. $this->fk_user_modif = null;
  595. $this->import_key = '';
  596. }
  597. }