mo.class.php 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651
  1. <?php
  2. /* Copyright (C) 2017 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2020 Lenin Rivas <lenin@leninrivas.com>
  4. * Copyright (C) ---Put here your own copyright and developer email---
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file class/mo.class.php
  21. * \ingroup mrp
  22. * \brief This file is a CRUD class file for Mo (Create/Read/Update/Delete)
  23. */
  24. // Put here all includes required by your class file
  25. require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
  26. //require_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php';
  27. //require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
  28. /**
  29. * Class for Mo
  30. */
  31. class Mo extends CommonObject
  32. {
  33. /**
  34. * @var string ID to identify managed object
  35. */
  36. public $element = 'mo';
  37. /**
  38. * @var string Name of table without prefix where object is stored
  39. */
  40. public $table_element = 'mrp_mo';
  41. /**
  42. * @var int Does mo support multicompany module ? 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
  43. */
  44. public $ismultientitymanaged = 0;
  45. /**
  46. * @var int Does mo support extrafields ? 0=No, 1=Yes
  47. */
  48. public $isextrafieldmanaged = 1;
  49. /**
  50. * @var string String with name of icon for mo. Must be the part after the 'object_' into object_mo.png
  51. */
  52. public $picto = 'mrp';
  53. const STATUS_DRAFT = 0;
  54. const STATUS_VALIDATED = 1; // To produce
  55. const STATUS_INPROGRESS = 2;
  56. const STATUS_PRODUCED = 3;
  57. const STATUS_CANCELED = 9;
  58. /**
  59. * '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')
  60. * Note: Filter can be a string like "(t.ref:like:'SO-%') or (t.date_creation:<:'20160101') or (t.nature:is:NULL)"
  61. * 'label' the translation key.
  62. * 'picto' is code of a picto to show before value in forms
  63. * 'enabled' is a condition when the field must be managed (Example: 1 or '$conf->global->MY_SETUP_PARAM)
  64. * 'position' is the sort order of field.
  65. * 'notnull' is set to 1 if not null in database. Set to -1 if we must set data to null if empty ('' or 0).
  66. * '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)
  67. * 'noteditable' says if field is not editable (1 or 0)
  68. * '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.
  69. * 'index' if we want an index in database.
  70. * 'foreignkey'=>'tablename.field' if the field is a foreign key (it is recommanded to name the field fk_...).
  71. * 'searchall' is 1 if we want to search in this field when making a search from the quick search button.
  72. * '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).
  73. * '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'
  74. * 'help' is a 'TranslationString' to use to show a tooltip on field. You can also use 'TranslationString:keyfortooltiponlick' for a tooltip on click.
  75. * 'showoncombobox' if value of the field must be visible into the label of the combobox that list record
  76. * '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.
  77. * 'arrayofkeyval' to set list of value if type is a list of predefined values. For example: array("0"=>"Draft","1"=>"Active","-1"=>"Cancel")
  78. * 'autofocusoncreate' to have field having the focus on a create form. Only 1 field should have this property set to 1.
  79. * 'comment' is not used. You can store here any text of your choice. It is not used by application.
  80. *
  81. * Note: To have value dynamic, you can set value to 0 in definition and edit the value on the fly into the constructor.
  82. */
  83. // BEGIN MODULEBUILDER PROPERTIES
  84. /**
  85. * @var array Array with all fields and their property. Do not use it as a static var. It may be modified by constructor.
  86. */
  87. public $fields = array(
  88. 'rowid' => array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-2, 'position'=>1, 'notnull'=>1, 'index'=>1, 'comment'=>"Id",),
  89. 'entity' => array('type'=>'integer', 'label'=>'Entity', 'enabled'=>1, 'visible'=>0, 'position'=>5, 'notnull'=>1, 'default'=>'1', 'index'=>1),
  90. 'ref' => array('type'=>'varchar(128)', 'label'=>'Ref', 'enabled'=>1, 'visible'=>4, 'position'=>10, 'notnull'=>1, 'default'=>'(PROV)', 'index'=>1, 'searchall'=>1, 'comment'=>"Reference of object", 'showoncombobox'=>'1', 'noteditable'=>1),
  91. 'fk_bom' => array('type'=>'integer:Bom:bom/class/bom.class.php:0:t.status=1', 'filter'=>'active=1', 'label'=>'BOM', 'enabled'=>1, 'visible'=>1, 'position'=>33, 'notnull'=>-1, 'index'=>1, 'comment'=>"Original BOM", 'css'=>'minwidth100 maxwidth300', 'csslist'=>'nowraponall'),
  92. 'mrptype' => array('type'=>'integer', 'label'=>'Type', 'enabled'=>1, 'visible'=>1, 'position'=>34, 'notnull'=>1, 'default'=>'0', 'arrayofkeyval'=>array(0=>'Manufacturing', 1=>'Disassemble'), 'css'=>'minwidth150', 'csslist'=>'minwidth150 center'),
  93. 'fk_product' => array('type'=>'integer:Product:product/class/product.class.php:0', 'label'=>'Product', 'enabled'=>1, 'visible'=>1, 'position'=>35, 'notnull'=>1, 'index'=>1, 'comment'=>"Product to produce", 'css'=>'maxwidth300', 'csslist'=>'tdoverflowmax100', 'picto'=>'product'),
  94. 'qty' => array('type'=>'real', 'label'=>'QtyToProduce', 'enabled'=>1, 'visible'=>1, 'position'=>40, 'notnull'=>1, 'comment'=>"Qty to produce", 'css'=>'width75', 'default'=>1, 'isameasure'=>1),
  95. 'label' => array('type'=>'varchar(255)', 'label'=>'Label', 'enabled'=>1, 'visible'=>1, 'position'=>42, 'notnull'=>-1, 'searchall'=>1, 'showoncombobox'=>'2', 'css'=>'maxwidth300', 'csslist'=>'tdoverflowmax200'),
  96. 'fk_soc' => array('type'=>'integer:Societe:societe/class/societe.class.php:1', 'label'=>'ThirdParty', 'picto'=>'company', 'enabled'=>1, 'visible'=>-1, 'position'=>50, 'notnull'=>-1, 'index'=>1, 'css'=>'maxwidth400', 'csslist'=>'tdoverflowmax150'),
  97. 'fk_project' => array('type'=>'integer:Project:projet/class/project.class.php:1:fk_statut=1', 'label'=>'Project', 'picto'=>'project', 'enabled'=>1, 'visible'=>-1, 'position'=>51, 'notnull'=>-1, 'index'=>1, 'css'=>'minwidth200 maxwidth400', 'csslist'=>'tdoverflowmax100'),
  98. 'fk_warehouse' => array('type'=>'integer:Entrepot:product/stock/class/entrepot.class.php:0', 'label'=>'WarehouseForProduction', 'picto'=>'stock', 'enabled'=>1, 'visible'=>1, 'position'=>52, 'css'=>'maxwidth400', 'csslist'=>'tdoverflowmax200'),
  99. 'note_public' => array('type'=>'html', 'label'=>'NotePublic', 'enabled'=>1, 'visible'=>0, 'position'=>61, 'notnull'=>-1,),
  100. 'note_private' => array('type'=>'html', 'label'=>'NotePrivate', 'enabled'=>1, 'visible'=>0, 'position'=>62, 'notnull'=>-1,),
  101. 'date_creation' => array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-2, 'position'=>500, 'notnull'=>1,),
  102. 'tms' => array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-2, 'position'=>501, 'notnull'=>1,),
  103. 'date_valid' => array('type'=>'datetime', 'label'=>'DateValidation', 'enabled'=>1, 'visible'=>-2, 'position'=>502,),
  104. 'fk_user_creat' => array('type'=>'integer', 'label'=>'UserAuthor', 'enabled'=>1, 'visible'=>-2, 'position'=>510, 'notnull'=>1, 'foreignkey'=>'user.rowid', 'csslist'=>'tdoverflowmax100'),
  105. 'fk_user_modif' => array('type'=>'integer', 'label'=>'UserModif', 'enabled'=>1, 'visible'=>-2, 'position'=>511, 'notnull'=>-1, 'csslist'=>'tdoverflowmax100'),
  106. 'date_start_planned' => array('type'=>'datetime', 'label'=>'DateStartPlannedMo', 'enabled'=>1, 'visible'=>1, 'position'=>55, 'notnull'=>-1, 'index'=>1, 'help'=>'KeepEmptyForAsap'),
  107. 'date_end_planned' => array('type'=>'datetime', 'label'=>'DateEndPlannedMo', 'enabled'=>1, 'visible'=>1, 'position'=>56, 'notnull'=>-1, 'index'=>1,),
  108. 'import_key' => array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-2, 'position'=>1000, 'notnull'=>-1,),
  109. 'model_pdf' =>array('type'=>'varchar(255)', 'label'=>'Model pdf', 'enabled'=>1, 'visible'=>0, 'position'=>1010),
  110. 'status' => array('type'=>'integer', 'label'=>'Status', 'enabled'=>1, 'visible'=>2, 'position'=>1000, 'default'=>0, 'notnull'=>1, 'index'=>1, 'arrayofkeyval'=>array('0'=>'Draft', '1'=>'Validated', '2'=>'InProgress', '3'=>'StatusMOProduced', '9'=>'Canceled')),
  111. );
  112. public $rowid;
  113. public $entity;
  114. public $ref;
  115. public $mrptype;
  116. public $label;
  117. public $qty;
  118. public $fk_warehouse;
  119. public $fk_soc;
  120. /**
  121. * @var string public note
  122. */
  123. public $note_public;
  124. /**
  125. * @var string private note
  126. */
  127. public $note_private;
  128. /**
  129. * @var integer|string date_creation
  130. */
  131. public $date_creation;
  132. public $tms;
  133. public $fk_user_creat;
  134. public $fk_user_modif;
  135. public $import_key;
  136. public $status;
  137. public $fk_product;
  138. /**
  139. * @var integer|string date_start_planned
  140. */
  141. public $date_start_planned;
  142. /**
  143. * @var integer|string date_end_planned
  144. */
  145. public $date_end_planned;
  146. public $fk_bom;
  147. public $fk_project;
  148. // END MODULEBUILDER PROPERTIES
  149. // If this object has a subtable with lines
  150. /**
  151. * @var string Name of subtable line
  152. */
  153. public $table_element_line = 'mrp_production';
  154. /**
  155. * @var string Field with ID of parent key if this field has a parent
  156. */
  157. public $fk_element = 'fk_mo';
  158. /**
  159. * @var string Name of subtable class that manage subtable lines
  160. */
  161. public $class_element_line = 'MoLine';
  162. /**
  163. * @var array List of child tables. To test if we can delete object.
  164. */
  165. protected $childtables = array();
  166. /**
  167. * @var array List of child tables. To know object to delete on cascade.
  168. */
  169. protected $childtablesoncascade = array('mrp_production');
  170. /**
  171. * @var MoLine[] Array of subtable lines
  172. */
  173. public $lines = array();
  174. /**
  175. * Constructor
  176. *
  177. * @param DoliDb $db Database handler
  178. */
  179. public function __construct(DoliDB $db)
  180. {
  181. global $conf, $langs;
  182. $this->db = $db;
  183. if (empty($conf->global->MAIN_SHOW_TECHNICAL_ID) && isset($this->fields['rowid'])) {
  184. $this->fields['rowid']['visible'] = 0;
  185. }
  186. if (empty($conf->multicompany->enabled) && isset($this->fields['entity'])) {
  187. $this->fields['entity']['enabled'] = 0;
  188. }
  189. // Unset fields that are disabled
  190. foreach ($this->fields as $key => $val) {
  191. if (isset($val['enabled']) && empty($val['enabled'])) {
  192. unset($this->fields[$key]);
  193. }
  194. }
  195. // Translate some data of arrayofkeyval
  196. foreach ($this->fields as $key => $val) {
  197. if (!empty($val['arrayofkeyval']) && is_array($val['arrayofkeyval'])) {
  198. foreach ($val['arrayofkeyval'] as $key2 => $val2) {
  199. $this->fields[$key]['arrayofkeyval'][$key2] = $langs->trans($val2);
  200. }
  201. }
  202. }
  203. }
  204. /**
  205. * Create object into database
  206. *
  207. * @param User $user User that creates
  208. * @param bool $notrigger false=launch triggers after, true=disable triggers
  209. * @return int <=0 if KO, Id of created object if OK
  210. */
  211. public function create(User $user, $notrigger = false)
  212. {
  213. global $conf;
  214. $error = 0;
  215. $idcreated = 0;
  216. $this->db->begin();
  217. // Check that product is not a kit/virtual product
  218. if (empty($conf->global->ALLOW_USE_KITS_INTO_BOM_AND_MO) && $this->fk_product > 0) {
  219. include_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  220. $tmpproduct = new Product($this->db);
  221. $tmpproduct->fetch($this->fk_product);
  222. if ($tmpproduct->hasFatherOrChild(1) > 0) {
  223. $this->error = 'ErrorAVirtualProductCantBeUsedIntoABomOrMo';
  224. $this->errors[] = $this->error;
  225. $this->db->rollback();
  226. return -1;
  227. }
  228. }
  229. if ($this->fk_bom > 0) {
  230. // If there is a nown BOM, we force the type of MO to the type of BOM
  231. $tmpbom = new BOM($this->db);
  232. $tmpbom->fetch($this->fk_bom);
  233. $this->mrptype = $tmpbom->bomtype;
  234. }
  235. if (!$error) {
  236. $idcreated = $this->createCommon($user, $notrigger);
  237. if ($idcreated <= 0) {
  238. $error++;
  239. }
  240. }
  241. if (!$error) {
  242. $result = $this->updateProduction($user, $notrigger); // Insert lines from BOM
  243. if ($result <= 0) {
  244. $error++;
  245. }
  246. }
  247. if (!$error) {
  248. $this->db->commit();
  249. } else {
  250. $this->db->rollback();
  251. }
  252. return $idcreated;
  253. }
  254. /**
  255. * Clone an object into another one
  256. *
  257. * @param User $user User that creates
  258. * @param int $fromid Id of object to clone
  259. * @return mixed New object created, <0 if KO
  260. */
  261. public function createFromClone(User $user, $fromid)
  262. {
  263. global $langs, $extrafields;
  264. $error = 0;
  265. dol_syslog(__METHOD__, LOG_DEBUG);
  266. $object = new self($this->db);
  267. $this->db->begin();
  268. // Load source object
  269. $result = $object->fetchCommon($fromid);
  270. if ($result > 0 && !empty($object->table_element_line)) {
  271. $object->fetchLines();
  272. }
  273. // get lines so they will be clone
  274. //foreach($this->lines as $line)
  275. // $line->fetch_optionals();
  276. // Reset some properties
  277. unset($object->id);
  278. unset($object->fk_user_creat);
  279. unset($object->import_key);
  280. // Clear fields
  281. $object->ref = empty($this->fields['ref']['default']) ? "copy_of_".$object->ref : $this->fields['ref']['default'];
  282. $object->label = empty($this->fields['label']['default']) ? $langs->trans("CopyOf")." ".$object->label : $this->fields['label']['default'];
  283. $object->status = self::STATUS_DRAFT;
  284. // ...
  285. // Clear extrafields that are unique
  286. if (is_array($object->array_options) && count($object->array_options) > 0) {
  287. $extrafields->fetch_name_optionals_label($this->table_element);
  288. foreach ($object->array_options as $key => $option) {
  289. $shortkey = preg_replace('/options_/', '', $key);
  290. if (!empty($extrafields->attributes[$this->element]['unique'][$shortkey])) {
  291. //var_dump($key); var_dump($clonedObj->array_options[$key]); exit;
  292. unset($object->array_options[$key]);
  293. }
  294. }
  295. }
  296. // Create clone
  297. $object->context['createfromclone'] = 'createfromclone';
  298. $result = $object->createCommon($user);
  299. if ($result < 0) {
  300. $error++;
  301. $this->error = $object->error;
  302. $this->errors = $object->errors;
  303. }
  304. if (!$error) {
  305. // copy internal contacts
  306. if ($this->copy_linked_contact($object, 'internal') < 0) {
  307. $error++;
  308. }
  309. }
  310. if (!$error) {
  311. // copy external contacts if same company
  312. if (property_exists($this, 'socid') && $this->socid == $object->socid) {
  313. if ($this->copy_linked_contact($object, 'external') < 0) {
  314. $error++;
  315. }
  316. }
  317. }
  318. unset($object->context['createfromclone']);
  319. // End
  320. if (!$error) {
  321. $this->db->commit();
  322. return $object;
  323. } else {
  324. $this->db->rollback();
  325. return -1;
  326. }
  327. }
  328. /**
  329. * Load object in memory from the database
  330. *
  331. * @param int $id Id object
  332. * @param string $ref Ref
  333. * @return int <0 if KO, 0 if not found, >0 if OK
  334. */
  335. public function fetch($id, $ref = null)
  336. {
  337. $result = $this->fetchCommon($id, $ref);
  338. if ($result > 0 && !empty($this->table_element_line)) {
  339. $this->fetchLines();
  340. }
  341. return $result;
  342. }
  343. /**
  344. * Load object lines in memory from the database
  345. *
  346. * @return int <0 if KO, 0 if not found, >0 if OK
  347. */
  348. public function fetchLines()
  349. {
  350. $this->lines = array();
  351. $result = $this->fetchLinesCommon();
  352. return $result;
  353. }
  354. /**
  355. * Load list of objects in memory from the database.
  356. *
  357. * @param string $sortorder Sort Order
  358. * @param string $sortfield Sort field
  359. * @param int $limit limit
  360. * @param int $offset Offset
  361. * @param array $filter Filter array. Example array('field'=>'valueforlike', 'customurl'=>...)
  362. * @param string $filtermode Filter mode (AND or OR)
  363. * @return array|int int <0 if KO, array of pages if OK
  364. */
  365. public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
  366. {
  367. global $conf;
  368. dol_syslog(__METHOD__, LOG_DEBUG);
  369. $records = array();
  370. $sql = 'SELECT ';
  371. $sql .= $this->getFieldList();
  372. $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
  373. if (isset($this->ismultientitymanaged) && $this->ismultientitymanaged == 1) {
  374. $sql .= ' WHERE t.entity IN ('.getEntity($this->table_element).')';
  375. } else {
  376. $sql .= ' WHERE 1 = 1';
  377. }
  378. // Manage filter
  379. $sqlwhere = array();
  380. if (count($filter) > 0) {
  381. foreach ($filter as $key => $value) {
  382. if ($key == 't.rowid') {
  383. $sqlwhere[] = $key." = ".((int) $value);
  384. } elseif (strpos($key, 'date') !== false) {
  385. $sqlwhere[] = $key." = '".$this->db->idate($value)."'";
  386. } elseif ($key == 'customsql') {
  387. $sqlwhere[] = $value;
  388. } else {
  389. $sqlwhere[] = $key." LIKE '%".$this->db->escape($value)."%'";
  390. }
  391. }
  392. }
  393. if (count($sqlwhere) > 0) {
  394. $sql .= ' AND ('.implode(' '.$this->db->escape($filtermode).' ', $sqlwhere).')';
  395. }
  396. if (!empty($sortfield)) {
  397. $sql .= $this->db->order($sortfield, $sortorder);
  398. }
  399. if (!empty($limit)) {
  400. $sql .= $this->db->plimit($limit, $offset);
  401. }
  402. $resql = $this->db->query($sql);
  403. if ($resql) {
  404. $num = $this->db->num_rows($resql);
  405. $i = 0;
  406. while ($i < min($limit, $num)) {
  407. $obj = $this->db->fetch_object($resql);
  408. $record = new self($this->db);
  409. $record->setVarsFromFetchObj($obj);
  410. $records[$record->id] = $record;
  411. $i++;
  412. }
  413. $this->db->free($resql);
  414. return $records;
  415. } else {
  416. $this->errors[] = 'Error '.$this->db->lasterror();
  417. dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
  418. return -1;
  419. }
  420. }
  421. /**
  422. * Get list of lines linked to current line for a defined role.
  423. *
  424. * @param string $role Get lines linked to current line with the selected role ('consumed', 'produced', ...)
  425. * @param int $lineid Id of production line to filter childs
  426. * @return array Array of lines
  427. */
  428. public function fetchLinesLinked($role, $lineid = 0)
  429. {
  430. $resarray = array();
  431. $mostatic = new MoLine($this->db);
  432. $sql = 'SELECT ';
  433. $sql .= $mostatic->getFieldList();
  434. $sql .= ' FROM '.MAIN_DB_PREFIX.$mostatic->table_element.' as t';
  435. $sql .= " WHERE t.role = '".$this->db->escape($role)."'";
  436. if ($lineid > 0) {
  437. $sql .= ' AND t.fk_mrp_production = '.((int) $lineid);
  438. } else {
  439. $sql .= 'AND t.fk_mo = '.((int) $this->id);
  440. }
  441. $resql = $this->db->query($sql);
  442. if ($resql) {
  443. $num = $this->db->num_rows($resql);
  444. $i = 0;
  445. while ($i < $num) {
  446. $obj = $this->db->fetch_object($resql);
  447. if ($obj) {
  448. $resarray[] = array(
  449. 'rowid'=> $obj->rowid,
  450. 'date'=> $this->db->jdate($obj->date_creation),
  451. 'qty' => $obj->qty,
  452. 'role' => $obj->role,
  453. 'fk_product' => $obj->fk_product,
  454. 'fk_warehouse' => $obj->fk_warehouse,
  455. 'batch' => $obj->batch,
  456. 'fk_stock_movement' => $obj->fk_stock_movement
  457. );
  458. }
  459. $i++;
  460. }
  461. return $resarray;
  462. } else {
  463. $this->error = $this->db->lasterror();
  464. return array();
  465. }
  466. }
  467. /**
  468. * Count number of movement with origin of MO
  469. *
  470. * @return int Number of movements
  471. */
  472. public function countMovements()
  473. {
  474. $result = 0;
  475. $sql = 'SELECT COUNT(rowid) as nb FROM '.MAIN_DB_PREFIX.'stock_mouvement as sm';
  476. $sql .= " WHERE sm.origintype = 'mo' and sm.fk_origin = ".((int) $this->id);
  477. $resql = $this->db->query($sql);
  478. if ($resql) {
  479. $num = $this->db->num_rows($resql);
  480. $i = 0;
  481. while ($i < $num) {
  482. $obj = $this->db->fetch_object($resql);
  483. if ($obj) {
  484. $result = $obj->nb;
  485. }
  486. $i++;
  487. }
  488. } else {
  489. $this->error = $this->db->lasterror();
  490. }
  491. return $result;
  492. }
  493. /**
  494. * Update object into database
  495. *
  496. * @param User $user User that modifies
  497. * @param bool $notrigger false=launch triggers after, true=disable triggers
  498. * @return int <0 if KO, >0 if OK
  499. */
  500. public function update(User $user, $notrigger = false)
  501. {
  502. global $langs;
  503. $error = 0;
  504. $this->db->begin();
  505. $result = $this->updateCommon($user, $notrigger);
  506. if ($result <= 0) {
  507. $error++;
  508. }
  509. $result = $this->updateProduction($user, $notrigger);
  510. if ($result <= 0) {
  511. $error++;
  512. }
  513. if (!$error) {
  514. setEventMessages($langs->trans("RecordModifiedSuccessfully"), null, 'mesgs');
  515. $this->db->commit();
  516. return 1;
  517. } else {
  518. setEventMessages($this->error, $this->errors, 'errors');
  519. $this->db->rollback();
  520. return -1;
  521. }
  522. }
  523. /**
  524. * Erase and update the line to consume and to produce.
  525. *
  526. * @param User $user User that modifies
  527. * @param bool $notrigger false=launch triggers after, true=disable triggers
  528. * @return int <0 if KO, >0 if OK
  529. */
  530. public function updateProduction(User $user, $notrigger = true)
  531. {
  532. $error = 0;
  533. $role = "";
  534. if ($this->status != self::STATUS_DRAFT) {
  535. $this->error = 'BadStatus';
  536. return -1;
  537. }
  538. $this->db->begin();
  539. // Insert lines in mrp_production table from BOM data
  540. if (!$error) {
  541. // TODO Check that production has not started. If yes, we stop here.
  542. $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'mrp_production WHERE fk_mo = '.((int) $this->id);
  543. $this->db->query($sql);
  544. $moline = new MoLine($this->db);
  545. // Line to produce
  546. $moline->fk_mo = $this->id;
  547. $moline->qty = $this->qty;
  548. $moline->fk_product = $this->fk_product;
  549. $moline->position = 1;
  550. if ($this->fk_bom > 0) { // If a BOM is defined, we know what to produce.
  551. include_once DOL_DOCUMENT_ROOT.'/bom/class/bom.class.php';
  552. $bom = new Bom($this->db);
  553. $bom->fetch($this->fk_bom);
  554. if ($bom->bomtype == 1) {
  555. $role = 'toproduce';
  556. $moline->role = 'toconsume';
  557. } else {
  558. $role = 'toconsume';
  559. $moline->role = 'toproduce';
  560. }
  561. } else {
  562. if ($this->mrptype == 1) {
  563. $moline->role = 'toconsume';
  564. } else {
  565. $moline->role = 'toproduce';
  566. }
  567. }
  568. $resultline = $moline->create($user, false); // Never use triggers here
  569. if ($resultline <= 0) {
  570. $error++;
  571. $this->error = $moline->error;
  572. $this->errors = $moline->errors;
  573. dol_print_error($this->db, $moline->error, $moline->errors);
  574. }
  575. if ($this->fk_bom > 0) { // If a BOM is defined, we know what to consume.
  576. if ($bom->id > 0) {
  577. // Lines to consume
  578. if (!$error) {
  579. foreach ($bom->lines as $line) {
  580. $moline = new MoLine($this->db);
  581. $moline->fk_mo = $this->id;
  582. $moline->origin_id = $line->id;
  583. $moline->origin_type = 'bomline';
  584. if ($line->qty_frozen) {
  585. $moline->qty = $line->qty; // Qty to consume does not depends on quantity to produce
  586. } else {
  587. $moline->qty = price2num(($line->qty / ( ! empty($bom->qty) ? $bom->qty : 1 ) ) * $this->qty / ( ! empty($line->efficiency) ? $line->efficiency : 1 ), 'MS'); // Calculate with Qty to produce and more presition
  588. }
  589. if ($moline->qty <= 0) {
  590. $error++;
  591. $this->error = "BadValueForquantityToConsume";
  592. break;
  593. } else {
  594. $moline->fk_product = $line->fk_product;
  595. $moline->role = $role;
  596. $moline->position = $line->position;
  597. $moline->qty_frozen = $line->qty_frozen;
  598. $moline->disable_stock_change = $line->disable_stock_change;
  599. $resultline = $moline->create($user, false); // Never use triggers here
  600. if ($resultline <= 0) {
  601. $error++;
  602. $this->error = $moline->error;
  603. $this->errors = $moline->errors;
  604. dol_print_error($this->db, $moline->error, $moline->errors);
  605. break;
  606. }
  607. }
  608. }
  609. }
  610. }
  611. }
  612. }
  613. if (!$error) {
  614. $this->db->commit();
  615. return 1;
  616. } else {
  617. $this->db->rollback();
  618. return -1;
  619. }
  620. }
  621. /**
  622. * Delete object in database
  623. *
  624. * @param User $user User that deletes
  625. * @param bool $notrigger false=launch triggers after, true=disable triggers
  626. * @return int <0 if KO, >0 if OK
  627. */
  628. public function delete(User $user, $notrigger = false)
  629. {
  630. return $this->deleteCommon($user, $notrigger);
  631. //return $this->deleteCommon($user, $notrigger, 1);
  632. }
  633. /**
  634. * Delete a line of object in database
  635. *
  636. * @param User $user User that delete
  637. * @param int $idline Id of line to delete
  638. * @param bool $notrigger false=launch triggers after, true=disable triggers
  639. * @return int >0 if OK, <0 if KO
  640. */
  641. public function deleteLine(User $user, $idline, $notrigger = false)
  642. {
  643. if ($this->status < 0) {
  644. $this->error = 'ErrorDeleteLineNotAllowedByObjectStatus';
  645. return -2;
  646. }
  647. return $this->deleteLineCommon($user, $idline, $notrigger);
  648. }
  649. /**
  650. * Returns the reference to the following non used MO depending on the active numbering module
  651. * defined into MRP_MO_ADDON
  652. *
  653. * @param Product $prod Object product
  654. * @return string MO free reference
  655. */
  656. public function getNextNumRef($prod)
  657. {
  658. global $langs, $conf;
  659. $langs->load("mrp");
  660. if (!empty($conf->global->MRP_MO_ADDON)) {
  661. $mybool = false;
  662. $file = $conf->global->MRP_MO_ADDON.".php";
  663. $classname = $conf->global->MRP_MO_ADDON;
  664. // Include file with class
  665. $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
  666. foreach ($dirmodels as $reldir) {
  667. $dir = dol_buildpath($reldir."core/modules/mrp/");
  668. // Load file with numbering class (if found)
  669. $mybool |= @include_once $dir.$file;
  670. }
  671. if ($mybool === false) {
  672. dol_print_error('', "Failed to include file ".$file);
  673. return '';
  674. }
  675. $obj = new $classname();
  676. $numref = $obj->getNextValue($prod, $this);
  677. if ($numref != "") {
  678. return $numref;
  679. } else {
  680. $this->error = $obj->error;
  681. //dol_print_error($this->db,get_class($this)."::getNextNumRef ".$obj->error);
  682. return "";
  683. }
  684. } else {
  685. print $langs->trans("Error")." ".$langs->trans("Error_MRP_MO_ADDON_NotDefined");
  686. return "";
  687. }
  688. }
  689. /**
  690. * Validate Mo
  691. *
  692. * @param User $user User making status change
  693. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  694. * @return int <=0 if OK, 0=Nothing done, >0 if KO
  695. */
  696. public function validate($user, $notrigger = 0)
  697. {
  698. global $conf, $langs;
  699. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  700. $error = 0;
  701. // Protection
  702. if ($this->status == self::STATUS_VALIDATED) {
  703. dol_syslog(get_class($this)."::validate action abandonned: already validated", LOG_WARNING);
  704. return 0;
  705. }
  706. /*if (! ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->mrp->create))
  707. || (! empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->mrp->mrp_advance->validate))))
  708. {
  709. $this->error='NotEnoughPermissions';
  710. dol_syslog(get_class($this)."::valid ".$this->error, LOG_ERR);
  711. return -1;
  712. }*/
  713. $now = dol_now();
  714. $this->db->begin();
  715. // Define new ref
  716. if (!$error && (preg_match('/^[\(]?PROV/i', $this->ref) || empty($this->ref))) { // empty should not happened, but when it occurs, the test save life
  717. $this->fetch_product();
  718. $num = $this->getNextNumRef($this->product);
  719. } else {
  720. $num = $this->ref;
  721. }
  722. $this->newref = $num;
  723. // Validate
  724. $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element;
  725. $sql .= " SET ref = '".$this->db->escape($num)."',";
  726. $sql .= " status = ".self::STATUS_VALIDATED.",";
  727. $sql .= " date_valid='".$this->db->idate($now)."',";
  728. $sql .= " fk_user_valid = ".$user->id;
  729. $sql .= " WHERE rowid = ".((int) $this->id);
  730. dol_syslog(get_class($this)."::validate()", LOG_DEBUG);
  731. $resql = $this->db->query($sql);
  732. if (!$resql) {
  733. dol_print_error($this->db);
  734. $this->error = $this->db->lasterror();
  735. $error++;
  736. }
  737. if (!$error && !$notrigger) {
  738. // Call trigger
  739. $result = $this->call_trigger('MRP_MO_VALIDATE', $user);
  740. if ($result < 0) {
  741. $error++;
  742. }
  743. // End call triggers
  744. }
  745. if (!$error) {
  746. $this->oldref = $this->ref;
  747. // Rename directory if dir was a temporary ref
  748. if (preg_match('/^[\(]?PROV/i', $this->ref)) {
  749. // Now we rename also files into index
  750. $sql = 'UPDATE '.MAIN_DB_PREFIX."ecm_files set filename = CONCAT('".$this->db->escape($this->newref)."', SUBSTR(filename, ".(strlen($this->ref) + 1).")), filepath = 'mrp/".$this->db->escape($this->newref)."'";
  751. $sql .= " WHERE filename LIKE '".$this->db->escape($this->ref)."%' AND filepath = 'mrp/".$this->db->escape($this->ref)."' and entity = ".$conf->entity;
  752. $resql = $this->db->query($sql);
  753. if (!$resql) {
  754. $error++; $this->error = $this->db->lasterror();
  755. }
  756. // We rename directory ($this->ref = old ref, $num = new ref) in order not to lose the attachments
  757. $oldref = dol_sanitizeFileName($this->ref);
  758. $newref = dol_sanitizeFileName($num);
  759. $dirsource = $conf->mrp->dir_output.'/'.$oldref;
  760. $dirdest = $conf->mrp->dir_output.'/'.$newref;
  761. if (!$error && file_exists($dirsource)) {
  762. dol_syslog(get_class($this)."::validate() rename dir ".$dirsource." into ".$dirdest);
  763. if (@rename($dirsource, $dirdest)) {
  764. dol_syslog("Rename ok");
  765. // Rename docs starting with $oldref with $newref
  766. $listoffiles = dol_dir_list($conf->mrp->dir_output.'/'.$newref, 'files', 1, '^'.preg_quote($oldref, '/'));
  767. foreach ($listoffiles as $fileentry) {
  768. $dirsource = $fileentry['name'];
  769. $dirdest = preg_replace('/^'.preg_quote($oldref, '/').'/', $newref, $dirsource);
  770. $dirsource = $fileentry['path'].'/'.$dirsource;
  771. $dirdest = $fileentry['path'].'/'.$dirdest;
  772. @rename($dirsource, $dirdest);
  773. }
  774. }
  775. }
  776. }
  777. }
  778. // Set new ref and current status
  779. if (!$error) {
  780. $this->ref = $num;
  781. $this->status = self::STATUS_VALIDATED;
  782. }
  783. if (!$error) {
  784. $this->db->commit();
  785. return 1;
  786. } else {
  787. $this->db->rollback();
  788. return -1;
  789. }
  790. }
  791. /**
  792. * Set draft status
  793. *
  794. * @param User $user Object user that modify
  795. * @param int $notrigger 1=Does not execute triggers, 0=Execute triggers
  796. * @return int <0 if KO, >0 if OK
  797. */
  798. public function setDraft($user, $notrigger = 0)
  799. {
  800. // Protection
  801. if ($this->status <= self::STATUS_DRAFT) {
  802. return 0;
  803. }
  804. /*if (! ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->mymodule->write))
  805. || (! empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->mymodule->mymodule_advance->validate))))
  806. {
  807. $this->error='Permission denied';
  808. return -1;
  809. }*/
  810. return $this->setStatusCommon($user, self::STATUS_DRAFT, $notrigger, 'MRP_MO_UNVALIDATE');
  811. }
  812. /**
  813. * Set cancel status
  814. *
  815. * @param User $user Object user that modify
  816. * @param int $notrigger 1=Does not execute triggers, 0=Execute triggers
  817. * @return int <0 if KO, 0=Nothing done, >0 if OK
  818. */
  819. public function cancel($user, $notrigger = 0)
  820. {
  821. // Protection
  822. if ($this->status != self::STATUS_VALIDATED && $this->status != self::STATUS_INPROGRESS) {
  823. return 0;
  824. }
  825. /*if (! ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->mymodule->write))
  826. || (! empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->mymodule->mymodule_advance->validate))))
  827. {
  828. $this->error='Permission denied';
  829. return -1;
  830. }*/
  831. return $this->setStatusCommon($user, self::STATUS_CANCELED, $notrigger, 'MRP_MO_CANCEL');
  832. }
  833. /**
  834. * Set back to validated status
  835. *
  836. * @param User $user Object user that modify
  837. * @param int $notrigger 1=Does not execute triggers, 0=Execute triggers
  838. * @return int <0 if KO, 0=Nothing done, >0 if OK
  839. */
  840. public function reopen($user, $notrigger = 0)
  841. {
  842. // Protection
  843. if ($this->status != self::STATUS_PRODUCED && $this->status != self::STATUS_CANCELED) {
  844. return 0;
  845. }
  846. /*if (! ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->mymodule->write))
  847. || (! empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->mymodule->mymodule_advance->validate))))
  848. {
  849. $this->error='Permission denied';
  850. return -1;
  851. }*/
  852. return $this->setStatusCommon($user, self::STATUS_VALIDATED, $notrigger, 'MRP_MO_REOPEN');
  853. }
  854. /**
  855. * Return a link to the object card (with optionaly the picto)
  856. *
  857. * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
  858. * @param string $option On what the link point to ('nolink', '', 'production', ...)
  859. * @param int $notooltip 1=Disable tooltip
  860. * @param string $morecss Add more css on link
  861. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  862. * @return string String with URL
  863. */
  864. public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
  865. {
  866. global $conf, $langs, $hookmanager;
  867. if (!empty($conf->dol_no_mouse_hover)) {
  868. $notooltip = 1; // Force disable tooltips
  869. }
  870. $result = '';
  871. $label = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("ManufacturingOrder").'</u>';
  872. if (isset($this->status)) {
  873. $label .= ' '.$this->getLibStatut(5);
  874. }
  875. $label .= '<br>';
  876. $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
  877. if (isset($this->label)) {
  878. $label .= '<br><b>'.$langs->trans('Label').':</b> '.$this->label;
  879. }
  880. $url = DOL_URL_ROOT.'/mrp/mo_card.php?id='.$this->id;
  881. if ($option == 'production') {
  882. $url = DOL_URL_ROOT.'/mrp/mo_production.php?id='.$this->id;
  883. }
  884. if ($option != 'nolink') {
  885. // Add param to save lastsearch_values or not
  886. $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
  887. if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
  888. $add_save_lastsearch_values = 1;
  889. }
  890. if ($add_save_lastsearch_values) {
  891. $url .= '&save_lastsearch_values=1';
  892. }
  893. }
  894. $linkclose = '';
  895. if (empty($notooltip)) {
  896. if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
  897. $label = $langs->trans("ShowMo");
  898. $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
  899. }
  900. $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
  901. $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
  902. } else {
  903. $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
  904. }
  905. $linkstart = '<a href="'.$url.'"';
  906. $linkstart .= $linkclose.'>';
  907. $linkend = '</a>';
  908. $result .= $linkstart;
  909. if ($withpicto) {
  910. $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);
  911. }
  912. if ($withpicto != 2) {
  913. $result .= $this->ref;
  914. }
  915. $result .= $linkend;
  916. //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
  917. global $action, $hookmanager;
  918. $hookmanager->initHooks(array('modao'));
  919. $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
  920. $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  921. if ($reshook > 0) {
  922. $result = $hookmanager->resPrint;
  923. } else {
  924. $result .= $hookmanager->resPrint;
  925. }
  926. return $result;
  927. }
  928. /**
  929. * Return label of the status
  930. *
  931. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 6=Long label + Picto
  932. * @return string Label of status
  933. */
  934. public function getLibStatut($mode = 0)
  935. {
  936. return $this->LibStatut($this->status, $mode);
  937. }
  938. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  939. /**
  940. * Return the status
  941. *
  942. * @param int $status Id status
  943. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 6=Long label + Picto
  944. * @return string Label of status
  945. */
  946. public function LibStatut($status, $mode = 0)
  947. {
  948. // phpcs:enable
  949. if (empty($this->labelStatus)) {
  950. global $langs;
  951. //$langs->load("mrp");
  952. $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Draft');
  953. $this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('ValidatedToProduce');
  954. $this->labelStatus[self::STATUS_INPROGRESS] = $langs->transnoentitiesnoconv('InProgress');
  955. $this->labelStatus[self::STATUS_PRODUCED] = $langs->transnoentitiesnoconv('StatusMOProduced');
  956. $this->labelStatus[self::STATUS_CANCELED] = $langs->transnoentitiesnoconv('Canceled');
  957. $this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Draft');
  958. $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Validated');
  959. $this->labelStatusShort[self::STATUS_INPROGRESS] = $langs->transnoentitiesnoconv('InProgress');
  960. $this->labelStatusShort[self::STATUS_PRODUCED] = $langs->transnoentitiesnoconv('StatusMOProduced');
  961. $this->labelStatusShort[self::STATUS_CANCELED] = $langs->transnoentitiesnoconv('Canceled');
  962. }
  963. $statusType = 'status'.$status;
  964. if ($status == self::STATUS_VALIDATED) {
  965. $statusType = 'status1';
  966. }
  967. if ($status == self::STATUS_INPROGRESS) {
  968. $statusType = 'status4';
  969. }
  970. if ($status == self::STATUS_PRODUCED) {
  971. $statusType = 'status6';
  972. }
  973. if ($status == self::STATUS_CANCELED) {
  974. $statusType = 'status9';
  975. }
  976. return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
  977. }
  978. /**
  979. * Load the info information in the object
  980. *
  981. * @param int $id Id of object
  982. * @return void
  983. */
  984. public function info($id)
  985. {
  986. $sql = 'SELECT rowid, date_creation as datec, tms as datem,';
  987. $sql .= ' fk_user_creat, fk_user_modif';
  988. $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
  989. $sql .= ' WHERE t.rowid = '.((int) $id);
  990. $result = $this->db->query($sql);
  991. if ($result) {
  992. if ($this->db->num_rows($result)) {
  993. $obj = $this->db->fetch_object($result);
  994. $this->id = $obj->rowid;
  995. if ($obj->fk_user_author) {
  996. $cuser = new User($this->db);
  997. $cuser->fetch($obj->fk_user_author);
  998. $this->user_creation = $cuser;
  999. }
  1000. if ($obj->fk_user_valid) {
  1001. $vuser = new User($this->db);
  1002. $vuser->fetch($obj->fk_user_valid);
  1003. $this->user_validation = $vuser;
  1004. }
  1005. if ($obj->fk_user_cloture) {
  1006. $cluser = new User($this->db);
  1007. $cluser->fetch($obj->fk_user_cloture);
  1008. $this->user_cloture = $cluser;
  1009. }
  1010. $this->date_creation = $this->db->jdate($obj->datec);
  1011. $this->date_modification = $this->db->jdate($obj->datem);
  1012. $this->date_validation = $this->db->jdate($obj->datev);
  1013. }
  1014. $this->db->free($result);
  1015. } else {
  1016. dol_print_error($this->db);
  1017. }
  1018. }
  1019. /**
  1020. * Initialise object with example values
  1021. * Id must be 0 if object instance is a specimen
  1022. *
  1023. * @return void
  1024. */
  1025. public function initAsSpecimen()
  1026. {
  1027. $this->initAsSpecimenCommon();
  1028. $this->lines = array();
  1029. }
  1030. /**
  1031. * Create an array of lines
  1032. *
  1033. * @return array|int array of lines if OK, <0 if KO
  1034. */
  1035. public function getLinesArray()
  1036. {
  1037. $this->lines = array();
  1038. $objectline = new MoLine($this->db);
  1039. $result = $objectline->fetchAll('ASC', 'position', 0, 0, array('customsql'=>'fk_mo = '.((int) $this->id)));
  1040. if (is_numeric($result)) {
  1041. $this->error = $this->error;
  1042. $this->errors = $this->errors;
  1043. return $result;
  1044. } else {
  1045. $this->lines = $result;
  1046. return $this->lines;
  1047. }
  1048. }
  1049. /**
  1050. * Create a document onto disk according to template module.
  1051. *
  1052. * @param string $modele Force template to use ('' to not force)
  1053. * @param Translate $outputlangs objet lang a utiliser pour traduction
  1054. * @param int $hidedetails Hide details of lines
  1055. * @param int $hidedesc Hide description
  1056. * @param int $hideref Hide ref
  1057. * @param null|array $moreparams Array to provide more information
  1058. * @return int 0 if KO, 1 if OK
  1059. */
  1060. public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams = null)
  1061. {
  1062. global $conf, $langs;
  1063. $langs->load("mrp");
  1064. if (!dol_strlen($modele)) {
  1065. //$modele = 'standard';
  1066. $modele = ''; // Remove this once a pdf_standard.php exists.
  1067. if ($this->model_pdf) {
  1068. $modele = $this->model_pdf;
  1069. } elseif (!empty($conf->global->MO_ADDON_PDF)) {
  1070. $modele = $conf->global->MO_ADDON_PDF;
  1071. }
  1072. }
  1073. $modelpath = "core/modules/mrp/doc/";
  1074. if (empty($modele)) {
  1075. return 1; // Remove this once a pdf_standard.php exists.
  1076. }
  1077. return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
  1078. }
  1079. /**
  1080. * Action executed by scheduler
  1081. * CAN BE A CRON TASK. In such a case, parameters come from the schedule job setup field 'Parameters'
  1082. * Use public function doScheduledJob($param1, $param2, ...) to get parameters
  1083. *
  1084. * @return int 0 if OK, <>0 if KO (this function is used also by cron so only 0 is OK)
  1085. */
  1086. public function doScheduledJob()
  1087. {
  1088. global $conf, $langs;
  1089. //$conf->global->SYSLOG_FILE = 'DOL_DATA_ROOT/dolibarr_mydedicatedlofile.log';
  1090. $error = 0;
  1091. $this->output = '';
  1092. $this->error = '';
  1093. dol_syslog(__METHOD__, LOG_DEBUG);
  1094. $now = dol_now();
  1095. $this->db->begin();
  1096. // ...
  1097. $this->db->commit();
  1098. return $error;
  1099. }
  1100. /**
  1101. * Return HTML table table of source object lines
  1102. * TODO Move this and previous function into output html class file (htmlline.class.php).
  1103. * If lines are into a template, title must also be into a template
  1104. * But for the moment we don't know if it's possible, so we keep the method available on overloaded objects.
  1105. *
  1106. * @param string $restrictlist ''=All lines, 'services'=Restrict to services only
  1107. * @param array $selectedLines Array of lines id for selected lines
  1108. * @return void
  1109. */
  1110. public function printOriginLinesList($restrictlist = '', $selectedLines = array())
  1111. {
  1112. global $langs, $hookmanager, $conf, $form;
  1113. $langs->load('stocks');
  1114. $text_stock_options = $langs->trans("RealStockDesc").'<br>';
  1115. $text_stock_options .= $langs->trans("RealStockWillAutomaticallyWhen").'<br>';
  1116. $text_stock_options .= (!empty($conf->global->STOCK_CALCULATE_ON_SHIPMENT) || !empty($conf->global->STOCK_CALCULATE_ON_SHIPMENT_CLOSE) ? '- '.$langs->trans("DeStockOnShipment").'<br>' : '');
  1117. $text_stock_options .= (!empty($conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER) ? '- '.$langs->trans("DeStockOnValidateOrder").'<br>' : '');
  1118. $text_stock_options .= (!empty($conf->global->STOCK_CALCULATE_ON_BILL) ? '- '.$langs->trans("DeStockOnBill").'<br>' : '');
  1119. $text_stock_options .= (!empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_BILL) ? '- '.$langs->trans("ReStockOnBill").'<br>' : '');
  1120. $text_stock_options .= (!empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_VALIDATE_ORDER) ? '- '.$langs->trans("ReStockOnValidateOrder").'<br>' : '');
  1121. $text_stock_options .= (!empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_DISPATCH_ORDER) ? '- '.$langs->trans("ReStockOnDispatchOrder").'<br>' : '');
  1122. $text_stock_options .= (!empty($conf->global->STOCK_CALCULATE_ON_RECEPTION) || !empty($conf->global->STOCK_CALCULATE_ON_RECEPTION_CLOSE) ? '- '.$langs->trans("StockOnReception").'<br>' : '');
  1123. print '<tr class="liste_titre">';
  1124. print '<td>'.$langs->trans('Ref').'</td>';
  1125. print '<td class="right">'.$langs->trans('Qty');
  1126. if ($this->bom->bomtype == 0) {
  1127. print ' <span class="opacitymedium">('.$langs->trans("ForAQuantityOf", $this->bom->qty).')</span>';
  1128. } else {
  1129. print ' <span class="opacitymedium">('.$langs->trans("ForAQuantityToConsumeOf", $this->bom->qty).')</span>';
  1130. }
  1131. print '</td>';
  1132. print '<td class="center">'.$form->textwithpicto($langs->trans("PhysicalStock"), $text_stock_options, 1).'</td>';
  1133. print '<td class="center">'.$form->textwithpicto($langs->trans("VirtualStock"), $langs->trans("VirtualStockDesc")).'</td>';
  1134. print '<td class="center">'.$langs->trans('QtyFrozen').'</td>';
  1135. print '<td class="center">'.$langs->trans('DisableStockChange').'</td>';
  1136. //print '<td class="right">'.$langs->trans('Efficiency').'</td>';
  1137. //print '<td class="center">'.$form->showCheckAddButtons('checkforselect', 1).'</td>';
  1138. print '<td class="center"></td>';
  1139. print '</tr>';
  1140. $i = 0;
  1141. if (!empty($this->lines)) {
  1142. foreach ($this->lines as $line) {
  1143. $reshook = 0;
  1144. if (is_object($hookmanager)) {
  1145. $parameters = array('line'=>$line, 'i'=>$i, 'restrictlist'=>$restrictlist, 'selectedLines'=> $selectedLines);
  1146. if (!empty($line->fk_parent_line)) { $parameters['fk_parent_line'] = $line->fk_parent_line; }
  1147. $reshook = $hookmanager->executeHooks('printOriginObjectLine', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  1148. }
  1149. if (empty($reshook)) {
  1150. $this->printOriginLine($line, '', $restrictlist, '/core/tpl', $selectedLines);
  1151. }
  1152. $i++;
  1153. }
  1154. }
  1155. }
  1156. /**
  1157. * Return HTML with a line of table array of source object lines
  1158. * TODO Move this and previous function into output html class file (htmlline.class.php).
  1159. * If lines are into a template, title must also be into a template
  1160. * But for the moment we don't know if it's possible as we keep a method available on overloaded objects.
  1161. *
  1162. * @param CommonObjectLine $line Line
  1163. * @param string $var Var
  1164. * @param string $restrictlist ''=All lines, 'services'=Restrict to services only (strike line if not)
  1165. * @param string $defaulttpldir Directory where to find the template
  1166. * @param array $selectedLines Array of lines id for selected lines
  1167. * @return void
  1168. */
  1169. public function printOriginLine($line, $var, $restrictlist = '', $defaulttpldir = '/core/tpl', $selectedLines = array())
  1170. {
  1171. global $langs, $conf;
  1172. $this->tpl['id'] = $line->id;
  1173. $this->tpl['label'] = '';
  1174. if (!empty($line->fk_product)) {
  1175. $productstatic = new Product($this->db);
  1176. $productstatic->fetch($line->fk_product);
  1177. $productstatic->load_virtual_stock();
  1178. $this->tpl['label'] .= $productstatic->getNomUrl(1);
  1179. //$this->tpl['label'].= ' - '.$productstatic->label;
  1180. } else {
  1181. // If origin MRP line is not a product, but another MRP
  1182. // TODO
  1183. }
  1184. $this->tpl['qty_bom'] = 1;
  1185. if (is_object($this->bom) && $this->bom->qty > 1) {
  1186. $this->tpl['qty_bom'] = $this->bom->qty;
  1187. }
  1188. $this->tpl['stock'] = $productstatic->stock_reel;
  1189. $this->tpl['seuil_stock_alerte'] = $productstatic->seuil_stock_alerte;
  1190. $this->tpl['virtual_stock'] = $productstatic->stock_theorique;
  1191. $this->tpl['qty'] = $line->qty;
  1192. $this->tpl['qty_frozen'] = $line->qty_frozen;
  1193. $this->tpl['disable_stock_change'] = $line->disable_stock_change;
  1194. $this->tpl['efficiency'] = $line->efficiency;
  1195. $tpl = DOL_DOCUMENT_ROOT.'/mrp/tpl/originproductline.tpl.php';
  1196. $res = include $tpl;
  1197. }
  1198. /**
  1199. * Function used to replace a thirdparty id with another one.
  1200. *
  1201. * @param DoliDB $db Database handler
  1202. * @param int $origin_id Old thirdparty id
  1203. * @param int $dest_id New thirdparty id
  1204. * @return bool
  1205. */
  1206. public static function replaceThirdparty($db, $origin_id, $dest_id)
  1207. {
  1208. $tables = array('mrp_mo');
  1209. return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables);
  1210. }
  1211. }
  1212. /**
  1213. * Class MoLine. You can also remove this and generate a CRUD class for lines objects.
  1214. */
  1215. class MoLine extends CommonObjectLine
  1216. {
  1217. /**
  1218. * @var string ID to identify managed object
  1219. */
  1220. public $element = 'mrp_production';
  1221. /**
  1222. * @var string Name of table without prefix where object is stored
  1223. */
  1224. public $table_element = 'mrp_production';
  1225. /**
  1226. * @var int Does myobject support multicompany module ? 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
  1227. */
  1228. public $ismultientitymanaged = 0;
  1229. /**
  1230. * @var int Does moline support extrafields ? 0=No, 1=Yes
  1231. */
  1232. public $isextrafieldmanaged = 0;
  1233. public $fields = array(
  1234. 'rowid' =>array('type'=>'integer', 'label'=>'ID', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>10),
  1235. 'fk_mo' =>array('type'=>'integer', 'label'=>'Mo', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>15),
  1236. 'origin_id' =>array('type'=>'integer', 'label'=>'Origin', 'enabled'=>1, 'visible'=>-1, 'notnull'=>0, 'position'=>17),
  1237. 'origin_type' =>array('type'=>'varchar(10)', 'label'=>'Origin type', 'enabled'=>1, 'visible'=>-1, 'notnull'=>0, 'position'=>18),
  1238. 'position' =>array('type'=>'integer', 'label'=>'Position', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>20),
  1239. 'fk_product' =>array('type'=>'integer', 'label'=>'Product', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>25),
  1240. 'fk_warehouse' =>array('type'=>'integer', 'label'=>'Warehouse', 'enabled'=>1, 'visible'=>-1, 'position'=>30),
  1241. 'qty' =>array('type'=>'real', 'label'=>'Qty', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>35),
  1242. 'qty_frozen' => array('type'=>'smallint', 'label'=>'QuantityFrozen', 'enabled'=>1, 'visible'=>1, 'default'=>0, 'position'=>105, 'css'=>'maxwidth50imp', 'help'=>'QuantityConsumedInvariable'),
  1243. 'disable_stock_change' => array('type'=>'smallint', 'label'=>'DisableStockChange', 'enabled'=>1, 'visible'=>1, 'default'=>0, 'position'=>108, 'css'=>'maxwidth50imp', 'help'=>'DisableStockChangeHelp'),
  1244. 'batch' =>array('type'=>'varchar(30)', 'label'=>'Batch', 'enabled'=>1, 'visible'=>-1, 'position'=>140),
  1245. 'role' =>array('type'=>'varchar(10)', 'label'=>'Role', 'enabled'=>1, 'visible'=>-1, 'position'=>145),
  1246. 'fk_mrp_production' =>array('type'=>'integer', 'label'=>'Fk mrp production', 'enabled'=>1, 'visible'=>-1, 'position'=>150),
  1247. 'fk_stock_movement' =>array('type'=>'integer', 'label'=>'StockMovement', 'enabled'=>1, 'visible'=>-1, 'position'=>155),
  1248. 'date_creation' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>160),
  1249. 'tms' =>array('type'=>'timestamp', 'label'=>'Tms', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>165),
  1250. 'fk_user_creat' =>array('type'=>'integer', 'label'=>'UserCreation', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>170),
  1251. 'fk_user_modif' =>array('type'=>'integer', 'label'=>'UserModification', 'enabled'=>1, 'visible'=>-1, 'position'=>175),
  1252. 'import_key' =>array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-1, 'position'=>180),
  1253. );
  1254. public $rowid;
  1255. public $fk_mo;
  1256. public $origin_id;
  1257. public $origin_type;
  1258. public $position;
  1259. public $fk_product;
  1260. public $fk_warehouse;
  1261. public $qty;
  1262. public $qty_frozen;
  1263. public $disable_stock_change;
  1264. public $batch;
  1265. public $role;
  1266. public $fk_mrp_production;
  1267. public $fk_stock_movement;
  1268. public $date_creation;
  1269. public $tms;
  1270. public $fk_user_creat;
  1271. public $fk_user_modif;
  1272. public $import_key;
  1273. /**
  1274. * Constructor
  1275. *
  1276. * @param DoliDb $db Database handler
  1277. */
  1278. public function __construct(DoliDB $db)
  1279. {
  1280. global $conf, $langs;
  1281. $this->db = $db;
  1282. if (empty($conf->global->MAIN_SHOW_TECHNICAL_ID) && isset($this->fields['rowid'])) {
  1283. $this->fields['rowid']['visible'] = 0;
  1284. }
  1285. if (empty($conf->multicompany->enabled) && isset($this->fields['entity'])) {
  1286. $this->fields['entity']['enabled'] = 0;
  1287. }
  1288. // Unset fields that are disabled
  1289. foreach ($this->fields as $key => $val) {
  1290. if (isset($val['enabled']) && empty($val['enabled'])) {
  1291. unset($this->fields[$key]);
  1292. }
  1293. }
  1294. // Translate some data of arrayofkeyval
  1295. if (is_object($langs)) {
  1296. foreach ($this->fields as $key => $val) {
  1297. if (!empty($val['arrayofkeyval']) && is_array($val['arrayofkeyval'])) {
  1298. foreach ($val['arrayofkeyval'] as $key2 => $val2) {
  1299. $this->fields[$key]['arrayofkeyval'][$key2] = $langs->trans($val2);
  1300. }
  1301. }
  1302. }
  1303. }
  1304. }
  1305. /**
  1306. * Create object into database
  1307. *
  1308. * @param User $user User that creates
  1309. * @param bool $notrigger false=launch triggers after, true=disable triggers
  1310. * @return int <0 if KO, Id of created object if OK
  1311. */
  1312. public function create(User $user, $notrigger = false)
  1313. {
  1314. if (empty($this->qty)) {
  1315. $this->error = 'BadValueForQty';
  1316. return -1;
  1317. }
  1318. return $this->createCommon($user, $notrigger);
  1319. }
  1320. /**
  1321. * Load object in memory from the database
  1322. *
  1323. * @param int $id Id object
  1324. * @param string $ref Ref
  1325. * @return int <0 if KO, 0 if not found, >0 if OK
  1326. */
  1327. public function fetch($id, $ref = null)
  1328. {
  1329. $result = $this->fetchCommon($id, $ref);
  1330. return $result;
  1331. }
  1332. /**
  1333. * Load list of objects in memory from the database.
  1334. *
  1335. * @param string $sortorder Sort Order
  1336. * @param string $sortfield Sort field
  1337. * @param int $limit limit
  1338. * @param int $offset Offset
  1339. * @param array $filter Filter array. Example array('field'=>'valueforlike', 'customurl'=>...)
  1340. * @param string $filtermode Filter mode (AND or OR)
  1341. * @return array|int int <0 if KO, array of pages if OK
  1342. */
  1343. public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
  1344. {
  1345. global $conf;
  1346. dol_syslog(__METHOD__, LOG_DEBUG);
  1347. $records = array();
  1348. $sql = 'SELECT ';
  1349. $sql .= $this->getFieldList();
  1350. $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
  1351. if (isset($this->ismultientitymanaged) && $this->ismultientitymanaged == 1) {
  1352. $sql .= ' WHERE t.entity IN ('.getEntity($this->table_element).')';
  1353. } else {
  1354. $sql .= ' WHERE 1 = 1';
  1355. }
  1356. // Manage filter
  1357. $sqlwhere = array();
  1358. if (count($filter) > 0) {
  1359. foreach ($filter as $key => $value) {
  1360. if ($key == 't.rowid') {
  1361. $sqlwhere[] = $key." = ".((int) $value);
  1362. } elseif (strpos($key, 'date') !== false) {
  1363. $sqlwhere[] = $key." = '".$this->db->idate($value)."'";
  1364. } elseif ($key == 'customsql') {
  1365. $sqlwhere[] = $value;
  1366. } else {
  1367. $sqlwhere[] = $key." LIKE '%".$this->db->escape($value)."%'";
  1368. }
  1369. }
  1370. }
  1371. if (count($sqlwhere) > 0) {
  1372. $sql .= ' AND ('.implode(' '.$this->db->escape($filtermode).' ', $sqlwhere).')';
  1373. }
  1374. if (!empty($sortfield)) {
  1375. $sql .= $this->db->order($sortfield, $sortorder);
  1376. }
  1377. if (!empty($limit)) {
  1378. $sql .= $this->db->plimit($limit, $offset);
  1379. }
  1380. $resql = $this->db->query($sql);
  1381. if ($resql) {
  1382. $num = $this->db->num_rows($resql);
  1383. $i = 0;
  1384. while ($i < min($limit, $num)) {
  1385. $obj = $this->db->fetch_object($resql);
  1386. $record = new self($this->db);
  1387. $record->setVarsFromFetchObj($obj);
  1388. $records[$record->id] = $record;
  1389. $i++;
  1390. }
  1391. $this->db->free($resql);
  1392. return $records;
  1393. } else {
  1394. $this->errors[] = 'Error '.$this->db->lasterror();
  1395. dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
  1396. return -1;
  1397. }
  1398. }
  1399. /**
  1400. * Update object into database
  1401. *
  1402. * @param User $user User that modifies
  1403. * @param bool $notrigger false=launch triggers after, true=disable triggers
  1404. * @return int <0 if KO, >0 if OK
  1405. */
  1406. public function update(User $user, $notrigger = false)
  1407. {
  1408. return $this->updateCommon($user, $notrigger);
  1409. }
  1410. /**
  1411. * Delete object in database
  1412. *
  1413. * @param User $user User that deletes
  1414. * @param bool $notrigger false=launch triggers after, true=disable triggers
  1415. * @return int <0 if KO, >0 if OK
  1416. */
  1417. public function delete(User $user, $notrigger = false)
  1418. {
  1419. return $this->deleteCommon($user, $notrigger);
  1420. //return $this->deleteCommon($user, $notrigger, 1);
  1421. }
  1422. }