| Total Complexity | 129 |
| Total Lines | 995 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like BOM often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BOM, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | class BOM extends CommonObject |
||
| 33 | { |
||
| 34 | /** |
||
| 35 | * @var string ID to identify managed object |
||
| 36 | */ |
||
| 37 | public $element = 'bom'; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var string Name of table without prefix where object is stored |
||
| 41 | */ |
||
| 42 | public $table_element = 'bom_bom'; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var int Does bom support multicompany module ? 0=No test on entity, 1=Test with field entity, 2=Test with link by societe |
||
| 46 | */ |
||
| 47 | public $ismultientitymanaged = 1; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var int Does object support extrafields ? 0=No, 1=Yes |
||
| 51 | */ |
||
| 52 | public $isextrafieldmanaged = 1; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var string String with name of icon for bom. Must be the part after the 'object_' into object_bom.png |
||
| 56 | */ |
||
| 57 | public $picto = 'bom'; |
||
| 58 | |||
| 59 | |||
| 60 | const STATUS_DRAFT = 0; |
||
| 61 | const STATUS_VALIDATED = 1; |
||
| 62 | const STATUS_CANCELED = 9; |
||
| 63 | |||
| 64 | |||
| 65 | /** |
||
| 66 | * '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') |
||
| 67 | * Note: Filter can be a string like "(t.ref:like:'SO-%') or (t.date_creation:<:'20160101') or (t.nature:is:NULL)" |
||
| 68 | * 'label' the translation key. |
||
| 69 | * 'enabled' is a condition when the field must be managed. |
||
| 70 | * '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) |
||
| 71 | * 'noteditable' says if field is not editable (1 or 0) |
||
| 72 | * 'notnull' is set to 1 if not null in database. Set to -1 if we must set data to null if empty ('' or 0). |
||
| 73 | * 'default' is a default value for creation (can still be replaced by the global setup of default values) |
||
| 74 | * 'index' if we want an index in database. |
||
| 75 | * 'foreignkey'=>'tablename.field' if the field is a foreign key (it is recommanded to name the field fk_...). |
||
| 76 | * 'position' is the sort order of field. |
||
| 77 | * 'searchall' is 1 if we want to search in this field when making a search from the quick search button. |
||
| 78 | * '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). |
||
| 79 | * 'css' is the CSS style to use on field. For example: 'maxwidth200' |
||
| 80 | * 'help' is a string visible as a tooltip on field |
||
| 81 | * 'comment' is not used. You can store here any text of your choice. It is not used by application. |
||
| 82 | * 'showoncombobox' if value of the field must be visible into the label of the combobox that list record |
||
| 83 | * 'arraykeyval' to set list of value if type is a list of predefined values. For example: array("0"=>"Draft","1"=>"Active","-1"=>"Cancel") |
||
| 84 | */ |
||
| 85 | |||
| 86 | // BEGIN MODULEBUILDER PROPERTIES |
||
| 87 | /** |
||
| 88 | * @var array Array with all fields and their property. Do not use it as a static var. It may be modified by constructor. |
||
| 89 | */ |
||
| 90 | public $fields = array( |
||
| 91 | 'rowid' => array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-1, 'position'=>1, 'notnull'=>1, 'index'=>1, 'comment'=>"Id",), |
||
| 92 | 'entity' => array('type'=>'integer', 'label'=>'Entity', 'enabled'=>1, 'visible'=>0, 'notnull'=> 1, 'default'=>1, 'index'=>1, 'position'=>5), |
||
| 93 | 'ref' => array('type'=>'varchar(128)', 'label'=>'Ref', 'enabled'=>1, 'noteditable'=>1, 'visible'=>4, 'position'=>10, 'notnull'=>1, 'default'=>'(PROV)', 'index'=>1, 'searchall'=>1, 'comment'=>"Reference of BOM", 'showoncombobox'=>'1',), |
||
| 94 | 'label' => array('type'=>'varchar(255)', 'label'=>'Label', 'enabled'=>1, 'visible'=>1, 'position'=>30, 'notnull'=>1, 'searchall'=>1, 'showoncombobox'=>'1', 'autofocusoncreate'=>1), |
||
| 95 | 'description' => array('type'=>'text', 'label'=>'Description', 'enabled'=>1, 'visible'=>-1, 'position'=>60, 'notnull'=>-1,), |
||
| 96 | 'fk_product' => array('type'=>'integer:Product:product/class/product.class.php:1:(finished IS NULL or finished <> 0)', 'label'=>'Product', 'enabled'=>1, 'visible'=>1, 'position'=>35, 'notnull'=>1, 'index'=>1, 'help'=>'ProductBOMHelp'), |
||
| 97 | 'qty' => array('type'=>'real', 'label'=>'Quantity', 'enabled'=>1, 'visible'=>1, 'default'=>1, 'position'=>55, 'notnull'=>1, 'isameasure'=>'1', 'css'=>'maxwidth75imp'), |
||
| 98 | //'efficiency' => array('type'=>'real', 'label'=>'ManufacturingEfficiency', 'enabled'=>1, 'visible'=>-1, 'default'=>1, 'position'=>100, 'notnull'=>0, 'css'=>'maxwidth50imp', 'help'=>'ValueOfMeansLossForProductProduced'), |
||
| 99 | 'duration' => array('type'=>'duration', 'label'=>'EstimatedDuration', 'enabled'=>1, 'visible'=>-1, 'position'=>101, 'notnull'=>-1, 'css'=>'maxwidth50imp', 'help'=>'EstimatedDurationDesc'), |
||
| 100 | 'fk_warehouse' => array('type'=>'integer:Entrepot:product/stock/class/entrepot.class.php:0', 'label'=>'WarehouseForProduction', 'enabled'=>1, 'visible'=>-1, 'position'=>102), |
||
| 101 | 'note_public' => array('type'=>'html', 'label'=>'NotePublic', 'enabled'=>1, 'visible'=>-2, 'position'=>161, 'notnull'=>-1,), |
||
| 102 | 'note_private' => array('type'=>'html', 'label'=>'NotePrivate', 'enabled'=>1, 'visible'=>-2, 'position'=>162, 'notnull'=>-1,), |
||
| 103 | 'date_creation' => array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-2, 'position'=>300, 'notnull'=>1,), |
||
| 104 | 'tms' => array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-2, 'position'=>501, 'notnull'=>1,), |
||
| 105 | 'date_valid' => array('type'=>'datetime', 'label'=>'DateValidation', 'enabled'=>1, 'visible'=>-2, 'position'=>502, 'notnull'=>0,), |
||
| 106 | 'fk_user_creat' => array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserCreation', 'enabled'=>1, 'visible'=>-2, 'position'=>510, 'notnull'=>1, 'foreignkey'=>'user.rowid',), |
||
| 107 | 'fk_user_modif' => array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserModif', 'enabled'=>1, 'visible'=>-2, 'position'=>511, 'notnull'=>-1,), |
||
| 108 | 'fk_user_valid' => array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserValidation', 'enabled'=>1, 'visible'=>-2, 'position'=>512, 'notnull'=>0,), |
||
| 109 | 'import_key' => array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-2, 'position'=>1000, 'notnull'=>-1,), |
||
| 110 | 'model_pdf' =>array('type'=>'varchar(255)', 'label'=>'Model pdf', 'enabled'=>1, 'visible'=>0, 'position'=>1010), |
||
| 111 | 'status' => array('type'=>'integer', 'label'=>'Status', 'enabled'=>1, 'visible'=>2, 'position'=>1000, 'notnull'=>1, 'default'=>0, 'index'=>1, 'arrayofkeyval'=>array(0=>'Draft', 1=>'Enabled', 9=>'Disabled')), |
||
| 112 | ); |
||
| 113 | public $rowid; |
||
| 114 | public $ref; |
||
| 115 | public $label; |
||
| 116 | public $description; |
||
| 117 | public $note_public; |
||
| 118 | public $note_private; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @var integer|string date_creation |
||
| 122 | */ |
||
| 123 | public $date_creation; |
||
| 124 | |||
| 125 | |||
| 126 | public $tms; |
||
| 127 | public $fk_user_creat; |
||
| 128 | public $fk_user_modif; |
||
| 129 | public $import_key; |
||
| 130 | public $status; |
||
| 131 | public $fk_product; |
||
| 132 | public $qty; |
||
| 133 | public $efficiency; |
||
| 134 | // END MODULEBUILDER PROPERTIES |
||
| 135 | |||
| 136 | |||
| 137 | // If this object has a subtable with lines |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @var int Name of subtable line |
||
| 141 | */ |
||
| 142 | public $table_element_line = 'bom_bomline'; |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @var int Field with ID of parent key if this field has a parent |
||
| 146 | */ |
||
| 147 | public $fk_element = 'fk_bom'; |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @var int Name of subtable class that manage subtable lines |
||
| 151 | */ |
||
| 152 | public $class_element_line = 'BOMLine'; |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @var array List of child tables. To test if we can delete object. |
||
| 156 | */ |
||
| 157 | //protected $childtables=array(); |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @var array List of child tables. To know object to delete on cascade. |
||
| 161 | */ |
||
| 162 | protected $childtablesoncascade = array('bom_bomline'); |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @var BOMLine[] Array of subtable lines |
||
| 166 | */ |
||
| 167 | public $lines = array(); |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @var int Calculated cost for the BOM |
||
| 171 | */ |
||
| 172 | public $total_cost = 0; |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @var int Calculated cost for 1 unit of the product in BOM |
||
| 176 | */ |
||
| 177 | public $unit_cost = 0; |
||
| 178 | |||
| 179 | |||
| 180 | |||
| 181 | /** |
||
| 182 | * Constructor |
||
| 183 | * |
||
| 184 | * @param DoliDb $db Database handler |
||
| 185 | */ |
||
| 186 | public function __construct(DoliDB $db) |
||
| 187 | { |
||
| 188 | global $conf, $langs; |
||
| 189 | |||
| 190 | $this->db = $db; |
||
| 191 | |||
| 192 | if (empty($conf->global->MAIN_SHOW_TECHNICAL_ID) && isset($this->fields['rowid'])) $this->fields['rowid']['visible'] = 0; |
||
| 193 | if (empty($conf->multicompany->enabled) && isset($this->fields['entity'])) $this->fields['entity']['enabled'] = 0; |
||
| 194 | |||
| 195 | // Unset fields that are disabled |
||
| 196 | foreach ($this->fields as $key => $val) |
||
| 197 | { |
||
| 198 | if (isset($val['enabled']) && empty($val['enabled'])) |
||
| 199 | { |
||
| 200 | unset($this->fields[$key]); |
||
| 201 | } |
||
| 202 | } |
||
| 203 | |||
| 204 | // Translate some data of arrayofkeyval |
||
| 205 | foreach ($this->fields as $key => $val) |
||
| 206 | { |
||
| 207 | if (is_array($val['arrayofkeyval'])) |
||
| 208 | { |
||
| 209 | foreach ($val['arrayofkeyval'] as $key2 => $val2) |
||
| 210 | { |
||
| 211 | $this->fields[$key]['arrayofkeyval'][$key2] = $langs->trans($val2); |
||
| 212 | } |
||
| 213 | } |
||
| 214 | } |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Create object into database |
||
| 219 | * |
||
| 220 | * @param User $user User that creates |
||
| 221 | * @param bool $notrigger false=launch triggers after, true=disable triggers |
||
| 222 | * @return int <0 if KO, Id of created object if OK |
||
| 223 | */ |
||
| 224 | public function create(User $user, $notrigger = false) |
||
| 225 | { |
||
| 226 | if ($this->efficiency <= 0 || $this->efficiency > 1) $this->efficiency = 1; |
||
| 227 | |||
| 228 | return $this->createCommon($user, $notrigger); |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Clone an object into another one |
||
| 233 | * |
||
| 234 | * @param User $user User that creates |
||
| 235 | * @param int $fromid Id of object to clone |
||
| 236 | * @return mixed New object created, <0 if KO |
||
| 237 | */ |
||
| 238 | public function createFromClone(User $user, $fromid) |
||
| 239 | { |
||
| 240 | global $langs, $hookmanager, $extrafields; |
||
| 241 | $error = 0; |
||
| 242 | |||
| 243 | dol_syslog(__METHOD__, LOG_DEBUG); |
||
| 244 | |||
| 245 | $object = new self($this->db); |
||
| 246 | |||
| 247 | $this->db->begin(); |
||
| 248 | |||
| 249 | // Load source object |
||
| 250 | $result = $object->fetchCommon($fromid); |
||
| 251 | if ($result > 0 && !empty($object->table_element_line)) $object->fetchLines(); |
||
| 252 | |||
| 253 | // Get lines so they will be clone |
||
| 254 | //foreach ($object->lines as $line) |
||
| 255 | // $line->fetch_optionals(); |
||
| 256 | |||
| 257 | // Reset some properties |
||
| 258 | unset($object->id); |
||
| 259 | unset($object->fk_user_creat); |
||
| 260 | unset($object->import_key); |
||
| 261 | |||
| 262 | // Clear fields |
||
| 263 | $object->ref = empty($this->fields['ref']['default']) ? $langs->trans("copy_of_").$object->ref : $this->fields['ref']['default']; |
||
| 264 | $object->label = empty($this->fields['label']['default']) ? $langs->trans("CopyOf")." ".$object->label : $this->fields['label']['default']; |
||
| 265 | $object->status = self::STATUS_DRAFT; |
||
| 266 | // ... |
||
| 267 | // Clear extrafields that are unique |
||
| 268 | if (is_array($object->array_options) && count($object->array_options) > 0) |
||
| 269 | { |
||
| 270 | $extrafields->fetch_name_optionals_label($object->table_element); |
||
| 271 | foreach ($object->array_options as $key => $option) |
||
| 272 | { |
||
| 273 | $shortkey = preg_replace('/options_/', '', $key); |
||
| 274 | if (!empty($extrafields->attributes[$this->element]['unique'][$shortkey])) |
||
| 275 | { |
||
| 276 | //var_dump($key); var_dump($clonedObj->array_options[$key]); exit; |
||
| 277 | unset($object->array_options[$key]); |
||
| 278 | } |
||
| 279 | } |
||
| 280 | } |
||
| 281 | |||
| 282 | // Create clone |
||
| 283 | $object->context['createfromclone'] = 'createfromclone'; |
||
| 284 | $result = $object->createCommon($user); |
||
| 285 | if ($result < 0) { |
||
| 286 | $error++; |
||
| 287 | $this->error = $object->error; |
||
| 288 | $this->errors = $object->errors; |
||
| 289 | } |
||
| 290 | |||
| 291 | if (!$error) |
||
| 292 | { |
||
| 293 | // copy internal contacts |
||
| 294 | if ($this->copy_linked_contact($object, 'internal') < 0) |
||
| 295 | { |
||
| 296 | $error++; |
||
| 297 | } |
||
| 298 | } |
||
| 299 | |||
| 300 | if (!$error) |
||
| 301 | { |
||
| 302 | // copy external contacts if same company |
||
| 303 | if (property_exists($this, 'socid') && $this->socid == $object->socid) |
||
| 304 | { |
||
| 305 | if ($this->copy_linked_contact($object, 'external') < 0) |
||
| 306 | $error++; |
||
| 307 | } |
||
| 308 | } |
||
| 309 | |||
| 310 | // If there is lines, create lines too |
||
| 311 | |||
| 312 | |||
| 313 | |||
| 314 | unset($object->context['createfromclone']); |
||
| 315 | |||
| 316 | // End |
||
| 317 | if (!$error) { |
||
| 318 | $this->db->commit(); |
||
| 319 | return $object; |
||
| 320 | } else { |
||
| 321 | $this->db->rollback(); |
||
| 322 | return -1; |
||
| 323 | } |
||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Load object in memory from the database |
||
| 328 | * |
||
| 329 | * @param int $id Id object |
||
| 330 | * @param string $ref Ref |
||
| 331 | * @return int <0 if KO, 0 if not found, >0 if OK |
||
| 332 | */ |
||
| 333 | public function fetch($id, $ref = null) |
||
| 334 | { |
||
| 335 | $result = $this->fetchCommon($id, $ref); |
||
| 336 | |||
| 337 | if ($result > 0 && !empty($this->table_element_line)) $this->fetchLines(); |
||
| 338 | $this->calculateCosts(); |
||
| 339 | |||
| 340 | return $result; |
||
| 341 | } |
||
| 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 | |||
| 352 | $result = $this->fetchLinesCommon(); |
||
| 353 | return $result; |
||
| 354 | } |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Load list of objects in memory from the database. |
||
| 358 | * |
||
| 359 | * @param string $sortorder Sort Order |
||
| 360 | * @param string $sortfield Sort field |
||
| 361 | * @param int $limit limit |
||
| 362 | * @param int $offset Offset |
||
| 363 | * @param array $filter Filter array. Example array('field'=>'valueforlike', 'customurl'=>...) |
||
| 364 | * @param string $filtermode Filter mode (AND or OR) |
||
| 365 | * @return array|int int <0 if KO, array of pages if OK |
||
| 366 | */ |
||
| 367 | public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND') |
||
| 368 | { |
||
| 369 | global $conf; |
||
| 370 | |||
| 371 | dol_syslog(__METHOD__, LOG_DEBUG); |
||
| 372 | |||
| 373 | $records = array(); |
||
| 374 | |||
| 375 | $sql = 'SELECT '; |
||
| 376 | $sql .= $this->getFieldList(); |
||
| 377 | $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t'; |
||
| 378 | if ($this->ismultientitymanaged) $sql .= ' WHERE t.entity IN ('.getEntity($this->table_element).')'; |
||
| 379 | else $sql .= ' WHERE 1 = 1'; |
||
| 380 | // Manage filter |
||
| 381 | $sqlwhere = array(); |
||
| 382 | if (count($filter) > 0) { |
||
| 383 | foreach ($filter as $key => $value) { |
||
| 384 | if ($key == 't.rowid') { |
||
| 385 | $sqlwhere[] = $key.'='.$value; |
||
| 386 | } elseif (strpos($key, 'date') !== false) { |
||
| 387 | $sqlwhere[] = $key.' = \''.$this->db->idate($value).'\''; |
||
| 388 | } elseif ($key == 'customsql') { |
||
| 389 | $sqlwhere[] = $value; |
||
| 390 | } else { |
||
| 391 | $sqlwhere[] = $key.' LIKE \'%'.$this->db->escape($value).'%\''; |
||
| 392 | } |
||
| 393 | } |
||
| 394 | } |
||
| 395 | if (count($sqlwhere) > 0) { |
||
| 396 | $sql .= ' AND ('.implode(' '.$filtermode.' ', $sqlwhere).')'; |
||
| 397 | } |
||
| 398 | |||
| 399 | if (!empty($sortfield)) { |
||
| 400 | $sql .= $this->db->order($sortfield, $sortorder); |
||
| 401 | } |
||
| 402 | if (!empty($limit)) { |
||
| 403 | $sql .= ' '.$this->db->plimit($limit, $offset); |
||
| 404 | } |
||
| 405 | |||
| 406 | $resql = $this->db->query($sql); |
||
| 407 | if ($resql) { |
||
| 408 | $num = $this->db->num_rows($resql); |
||
| 409 | |||
| 410 | while ($obj = $this->db->fetch_object($resql)) |
||
| 411 | { |
||
| 412 | $record = new self($this->db); |
||
| 413 | $record->setVarsFromFetchObj($obj); |
||
| 414 | |||
| 415 | $records[$record->id] = $record; |
||
| 416 | } |
||
| 417 | $this->db->free($resql); |
||
| 418 | |||
| 419 | return $records; |
||
| 420 | } else { |
||
| 421 | $this->errors[] = 'Error '.$this->db->lasterror(); |
||
| 422 | dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR); |
||
| 423 | |||
| 424 | return -1; |
||
| 425 | } |
||
| 426 | } |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Update object into database |
||
| 430 | * |
||
| 431 | * @param User $user User that modifies |
||
| 432 | * @param bool $notrigger false=launch triggers after, true=disable triggers |
||
| 433 | * @return int <0 if KO, >0 if OK |
||
| 434 | */ |
||
| 435 | public function update(User $user, $notrigger = false) |
||
| 436 | { |
||
| 437 | if ($this->efficiency <= 0 || $this->efficiency > 1) $this->efficiency = 1; |
||
| 438 | |||
| 439 | return $this->updateCommon($user, $notrigger); |
||
| 440 | } |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Delete object in database |
||
| 444 | * |
||
| 445 | * @param User $user User that deletes |
||
| 446 | * @param bool $notrigger false=launch triggers after, true=disable triggers |
||
| 447 | * @return int <0 if KO, >0 if OK |
||
| 448 | */ |
||
| 449 | public function delete(User $user, $notrigger = false) |
||
| 450 | { |
||
| 451 | return $this->deleteCommon($user, $notrigger); |
||
| 452 | //return $this->deleteCommon($user, $notrigger, 1); |
||
| 453 | } |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Delete a line of object in database |
||
| 457 | * |
||
| 458 | * @param User $user User that delete |
||
| 459 | * @param int $idline Id of line to delete |
||
| 460 | * @param bool $notrigger false=launch triggers after, true=disable triggers |
||
| 461 | * @return int >0 if OK, <0 if KO |
||
| 462 | */ |
||
| 463 | public function deleteLine(User $user, $idline, $notrigger = false) |
||
| 464 | { |
||
| 465 | if ($this->status < 0) |
||
| 466 | { |
||
| 467 | $this->error = 'ErrorDeleteLineNotAllowedByObjectStatus'; |
||
| 468 | return -2; |
||
| 469 | } |
||
| 470 | |||
| 471 | return $this->deleteLineCommon($user, $idline, $notrigger); |
||
| 472 | } |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Returns the reference to the following non used BOM depending on the active numbering module |
||
| 476 | * defined into BOM_ADDON |
||
| 477 | * |
||
| 478 | * @param Product $prod Object product |
||
| 479 | * @return string BOM free reference |
||
| 480 | */ |
||
| 481 | public function getNextNumRef($prod) |
||
| 482 | { |
||
| 483 | global $langs, $conf; |
||
| 484 | $langs->load("mrp"); |
||
| 485 | |||
| 486 | if (!empty($conf->global->BOM_ADDON)) |
||
| 487 | { |
||
| 488 | $mybool = false; |
||
| 489 | |||
| 490 | $file = $conf->global->BOM_ADDON.".php"; |
||
| 491 | $classname = $conf->global->BOM_ADDON; |
||
| 492 | |||
| 493 | // Include file with class |
||
| 494 | $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']); |
||
| 495 | foreach ($dirmodels as $reldir) |
||
| 496 | { |
||
| 497 | $dir = dol_buildpath($reldir."core/modules/bom/"); |
||
| 498 | |||
| 499 | // Load file with numbering class (if found) |
||
| 500 | $mybool |= @include_once $dir.$file; |
||
| 501 | } |
||
| 502 | |||
| 503 | if ($mybool === false) |
||
| 504 | { |
||
| 505 | dol_print_error('', "Failed to include file ".$file); |
||
| 506 | return ''; |
||
| 507 | } |
||
| 508 | |||
| 509 | $obj = new $classname(); |
||
| 510 | $numref = $obj->getNextValue($prod, $this); |
||
| 511 | |||
| 512 | if ($numref != "") |
||
| 513 | { |
||
| 514 | return $numref; |
||
| 515 | } else { |
||
| 516 | $this->error = $obj->error; |
||
| 517 | //dol_print_error($this->db,get_class($this)."::getNextNumRef ".$obj->error); |
||
| 518 | return ""; |
||
| 519 | } |
||
| 520 | } else { |
||
| 521 | print $langs->trans("Error")." ".$langs->trans("Error_BOM_ADDON_NotDefined"); |
||
| 522 | return ""; |
||
| 523 | } |
||
| 524 | } |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Validate bom |
||
| 528 | * |
||
| 529 | * @param User $user User making status change |
||
| 530 | * @param int $notrigger 1=Does not execute triggers, 0= execute triggers |
||
| 531 | * @return int <=0 if OK, 0=Nothing done, >0 if KO |
||
| 532 | */ |
||
| 533 | public function validate($user, $notrigger = 0) |
||
| 534 | { |
||
| 535 | global $conf, $langs; |
||
| 536 | |||
| 537 | require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php'; |
||
| 538 | |||
| 539 | $error = 0; |
||
| 540 | |||
| 541 | // Protection |
||
| 542 | if ($this->status == self::STATUS_VALIDATED) |
||
| 543 | { |
||
| 544 | dol_syslog(get_class($this)."::validate action abandonned: already validated", LOG_WARNING); |
||
| 545 | return 0; |
||
| 546 | } |
||
| 547 | |||
| 548 | /*if (! ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->bom->create)) |
||
| 549 | || (! empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->bom->bom_advance->validate)))) |
||
| 550 | { |
||
| 551 | $this->error='NotEnoughPermissions'; |
||
| 552 | dol_syslog(get_class($this)."::valid ".$this->error, LOG_ERR); |
||
| 553 | return -1; |
||
| 554 | }*/ |
||
| 555 | |||
| 556 | $now = dol_now(); |
||
| 557 | |||
| 558 | $this->db->begin(); |
||
| 559 | |||
| 560 | // Define new ref |
||
| 561 | if (!$error && (preg_match('/^[\(]?PROV/i', $this->ref) || empty($this->ref))) // empty should not happened, but when it occurs, the test save life |
||
| 562 | { |
||
| 563 | $this->fetch_product(); |
||
| 564 | $num = $this->getNextNumRef($this->product); |
||
| 565 | } else { |
||
| 566 | $num = $this->ref; |
||
| 567 | } |
||
| 568 | $this->newref = dol_sanitizeFileName($num); |
||
| 569 | |||
| 570 | // Validate |
||
| 571 | $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element; |
||
| 572 | $sql .= " SET ref = '".$this->db->escape($num)."',"; |
||
| 573 | $sql .= " status = ".self::STATUS_VALIDATED.","; |
||
| 574 | $sql .= " date_valid='".$this->db->idate($now)."',"; |
||
| 575 | $sql .= " fk_user_valid = ".$user->id; |
||
| 576 | $sql .= " WHERE rowid = ".$this->id; |
||
| 577 | |||
| 578 | dol_syslog(get_class($this)."::validate()", LOG_DEBUG); |
||
| 579 | $resql = $this->db->query($sql); |
||
| 580 | if (!$resql) |
||
| 581 | { |
||
| 582 | dol_print_error($this->db); |
||
| 583 | $this->error = $this->db->lasterror(); |
||
| 584 | $error++; |
||
| 585 | } |
||
| 586 | |||
| 587 | if (!$error && !$notrigger) |
||
| 588 | { |
||
| 589 | // Call trigger |
||
| 590 | $result = $this->call_trigger('BOM_VALIDATE', $user); |
||
| 591 | if ($result < 0) $error++; |
||
| 592 | // End call triggers |
||
| 593 | } |
||
| 594 | |||
| 595 | if (!$error) |
||
| 596 | { |
||
| 597 | $this->oldref = $this->ref; |
||
| 598 | |||
| 599 | // Rename directory if dir was a temporary ref |
||
| 600 | if (preg_match('/^[\(]?PROV/i', $this->ref)) |
||
| 601 | { |
||
| 602 | // Now we rename also files into index |
||
| 603 | $sql = 'UPDATE '.MAIN_DB_PREFIX."ecm_files set filename = CONCAT('".$this->db->escape($this->newref)."', SUBSTR(filename, ".(strlen($this->ref) + 1).")), filepath = 'bom/".$this->db->escape($this->newref)."'"; |
||
| 604 | $sql .= " WHERE filename LIKE '".$this->db->escape($this->ref)."%' AND filepath = 'bom/".$this->db->escape($this->ref)."' and entity = ".$conf->entity; |
||
| 605 | $resql = $this->db->query($sql); |
||
| 606 | if (!$resql) { $error++; $this->error = $this->db->lasterror(); } |
||
| 607 | |||
| 608 | // We rename directory ($this->ref = old ref, $num = new ref) in order not to lose the attachments |
||
| 609 | $oldref = dol_sanitizeFileName($this->ref); |
||
| 610 | $newref = dol_sanitizeFileName($num); |
||
| 611 | $dirsource = $conf->bom->dir_output.'/'.$oldref; |
||
| 612 | $dirdest = $conf->bom->dir_output.'/'.$newref; |
||
| 613 | if (!$error && file_exists($dirsource)) |
||
| 614 | { |
||
| 615 | dol_syslog(get_class($this)."::validate() rename dir ".$dirsource." into ".$dirdest); |
||
| 616 | |||
| 617 | if (@rename($dirsource, $dirdest)) |
||
| 618 | { |
||
| 619 | dol_syslog("Rename ok"); |
||
| 620 | // Rename docs starting with $oldref with $newref |
||
| 621 | $listoffiles = dol_dir_list($conf->bom->dir_output.'/'.$newref, 'files', 1, '^'.preg_quote($oldref, '/')); |
||
| 622 | foreach ($listoffiles as $fileentry) |
||
| 623 | { |
||
| 624 | $dirsource = $fileentry['name']; |
||
| 625 | $dirdest = preg_replace('/^'.preg_quote($oldref, '/').'/', $newref, $dirsource); |
||
| 626 | $dirsource = $fileentry['path'].'/'.$dirsource; |
||
| 627 | $dirdest = $fileentry['path'].'/'.$dirdest; |
||
| 628 | @rename($dirsource, $dirdest); |
||
| 629 | } |
||
| 630 | } |
||
| 631 | } |
||
| 632 | } |
||
| 633 | } |
||
| 634 | |||
| 635 | // Set new ref and current status |
||
| 636 | if (!$error) |
||
| 637 | { |
||
| 638 | $this->ref = $num; |
||
| 639 | $this->status = self::STATUS_VALIDATED; |
||
| 640 | } |
||
| 641 | |||
| 642 | if (!$error) |
||
| 643 | { |
||
| 644 | $this->db->commit(); |
||
| 645 | return 1; |
||
| 646 | } else { |
||
| 647 | $this->db->rollback(); |
||
| 648 | return -1; |
||
| 649 | } |
||
| 650 | } |
||
| 651 | |||
| 652 | /** |
||
| 653 | * Set draft status |
||
| 654 | * |
||
| 655 | * @param User $user Object user that modify |
||
| 656 | * @param int $notrigger 1=Does not execute triggers, 0=Execute triggers |
||
| 657 | * @return int <0 if KO, >0 if OK |
||
| 658 | */ |
||
| 659 | public function setDraft($user, $notrigger = 0) |
||
| 660 | { |
||
| 661 | // Protection |
||
| 662 | if ($this->status <= self::STATUS_DRAFT) |
||
| 663 | { |
||
| 664 | return 0; |
||
| 665 | } |
||
| 666 | |||
| 667 | /*if (! ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->bom->write)) |
||
| 668 | || (! empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->bom->bom_advance->validate)))) |
||
| 669 | { |
||
| 670 | $this->error='Permission denied'; |
||
| 671 | return -1; |
||
| 672 | }*/ |
||
| 673 | |||
| 674 | return $this->setStatusCommon($user, self::STATUS_DRAFT, $notrigger, 'BOM_UNVALIDATE'); |
||
| 675 | } |
||
| 676 | |||
| 677 | /** |
||
| 678 | * Set cancel status |
||
| 679 | * |
||
| 680 | * @param User $user Object user that modify |
||
| 681 | * @param int $notrigger 1=Does not execute triggers, 0=Execute triggers |
||
| 682 | * @return int <0 if KO, 0=Nothing done, >0 if OK |
||
| 683 | */ |
||
| 684 | public function cancel($user, $notrigger = 0) |
||
| 685 | { |
||
| 686 | // Protection |
||
| 687 | if ($this->status != self::STATUS_VALIDATED) |
||
| 688 | { |
||
| 689 | return 0; |
||
| 690 | } |
||
| 691 | |||
| 692 | /*if (! ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->bom->write)) |
||
| 693 | || (! empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->bom->bom_advance->validate)))) |
||
| 694 | { |
||
| 695 | $this->error='Permission denied'; |
||
| 696 | return -1; |
||
| 697 | }*/ |
||
| 698 | |||
| 699 | return $this->setStatusCommon($user, self::STATUS_CANCELED, $notrigger, 'BOM_CLOSE'); |
||
| 700 | } |
||
| 701 | |||
| 702 | /** |
||
| 703 | * Set cancel status |
||
| 704 | * |
||
| 705 | * @param User $user Object user that modify |
||
| 706 | * @param int $notrigger 1=Does not execute triggers, 0=Execute triggers |
||
| 707 | * @return int <0 if KO, 0=Nothing done, >0 if OK |
||
| 708 | */ |
||
| 709 | public function reopen($user, $notrigger = 0) |
||
| 710 | { |
||
| 711 | // Protection |
||
| 712 | if ($this->status != self::STATUS_CANCELED) |
||
| 713 | { |
||
| 714 | return 0; |
||
| 715 | } |
||
| 716 | |||
| 717 | /*if (! ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->bom->write)) |
||
| 718 | || (! empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->bom->bom_advance->validate)))) |
||
| 719 | { |
||
| 720 | $this->error='Permission denied'; |
||
| 721 | return -1; |
||
| 722 | }*/ |
||
| 723 | |||
| 724 | return $this->setStatusCommon($user, self::STATUS_VALIDATED, $notrigger, 'BOM_REOPEN'); |
||
| 725 | } |
||
| 726 | |||
| 727 | |||
| 728 | /** |
||
| 729 | * Return a link to the object card (with optionaly the picto) |
||
| 730 | * |
||
| 731 | * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto) |
||
| 732 | * @param string $option On what the link point to ('nolink', ...) |
||
| 733 | * @param int $notooltip 1=Disable tooltip |
||
| 734 | * @param string $morecss Add more css on link |
||
| 735 | * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking |
||
| 736 | * @return string String with URL |
||
| 737 | */ |
||
| 738 | public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1) |
||
| 739 | { |
||
| 740 | global $db, $conf, $langs, $hookmanager; |
||
| 741 | global $dolibarr_main_authentication, $dolibarr_main_demo; |
||
| 742 | global $menumanager; |
||
| 743 | |||
| 744 | if (!empty($conf->dol_no_mouse_hover)) $notooltip = 1; // Force disable tooltips |
||
| 745 | |||
| 746 | $result = ''; |
||
| 747 | |||
| 748 | $label = '<u>'.$langs->trans("BillOfMaterials").'</u>'; |
||
| 749 | $label .= '<br>'; |
||
| 750 | $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref; |
||
| 751 | if (isset($this->status)) { |
||
| 752 | $label .= '<br><b>'.$langs->trans("Status").":</b> ".$this->getLibStatut(5); |
||
| 753 | } |
||
| 754 | |||
| 755 | $url = dol_buildpath('/bom/bom_card.php', 1).'?id='.$this->id; |
||
| 756 | |||
| 757 | if ($option != 'nolink') |
||
| 758 | { |
||
| 759 | // Add param to save lastsearch_values or not |
||
| 760 | $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0); |
||
| 761 | if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) $add_save_lastsearch_values = 1; |
||
| 762 | if ($add_save_lastsearch_values) $url .= '&save_lastsearch_values=1'; |
||
| 763 | } |
||
| 764 | |||
| 765 | $linkclose = ''; |
||
| 766 | if (empty($notooltip)) |
||
| 767 | { |
||
| 768 | if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) |
||
| 769 | { |
||
| 770 | $label = $langs->trans("ShowBillOfMaterials"); |
||
| 771 | $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"'; |
||
| 772 | } |
||
| 773 | $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"'; |
||
| 774 | $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"'; |
||
| 775 | |||
| 776 | /* |
||
| 777 | $hookmanager->initHooks(array('bomdao')); |
||
| 778 | $parameters=array('id'=>$this->id); |
||
| 779 | $reshook=$hookmanager->executeHooks('getnomurltooltip',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks |
||
| 780 | if ($reshook > 0) $linkclose = $hookmanager->resPrint; |
||
| 781 | */ |
||
| 782 | } else $linkclose = ($morecss ? ' class="'.$morecss.'"' : ''); |
||
| 783 | |||
| 784 | $linkstart = '<a href="'.$url.'"'; |
||
| 785 | $linkstart .= $linkclose.'>'; |
||
| 786 | $linkend = '</a>'; |
||
| 787 | |||
| 788 | $result .= $linkstart; |
||
| 789 | if ($withpicto) $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); |
||
| 790 | if ($withpicto != 2) $result .= $this->ref; |
||
| 791 | $result .= $linkend; |
||
| 792 | //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : ''); |
||
| 793 | |||
| 794 | global $action, $hookmanager; |
||
| 795 | $hookmanager->initHooks(array('bomdao')); |
||
| 796 | $parameters = array('id'=>$this->id, 'getnomurl'=>$result); |
||
| 797 | $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks |
||
| 798 | if ($reshook > 0) $result = $hookmanager->resPrint; |
||
| 799 | else $result .= $hookmanager->resPrint; |
||
| 800 | |||
| 801 | return $result; |
||
| 802 | } |
||
| 803 | |||
| 804 | /** |
||
| 805 | * Return label of the status |
||
| 806 | * |
||
| 807 | * @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 |
||
| 808 | * @return string Label of status |
||
| 809 | */ |
||
| 810 | public function getLibStatut($mode = 0) |
||
| 811 | { |
||
| 812 | return $this->LibStatut($this->status, $mode); |
||
| 813 | } |
||
| 814 | |||
| 815 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 816 | /** |
||
| 817 | * Return the status |
||
| 818 | * |
||
| 819 | * @param int $status Id status |
||
| 820 | * @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 |
||
| 821 | * @return string Label of status |
||
| 822 | */ |
||
| 823 | public function LibStatut($status, $mode = 0) |
||
| 824 | { |
||
| 825 | // phpcs:enable |
||
| 826 | if (empty($this->labelStatus)) |
||
| 827 | { |
||
| 828 | global $langs; |
||
| 829 | //$langs->load("mrp"); |
||
| 830 | $this->labelStatus[self::STATUS_DRAFT] = $langs->trans('Draft'); |
||
| 831 | $this->labelStatus[self::STATUS_VALIDATED] = $langs->trans('Enabled'); |
||
| 832 | $this->labelStatus[self::STATUS_CANCELED] = $langs->trans('Disabled'); |
||
| 833 | } |
||
| 834 | |||
| 835 | $statusType = 'status'.$status; |
||
| 836 | if ($status == self::STATUS_VALIDATED) $statusType = 'status4'; |
||
| 837 | if ($status == self::STATUS_CANCELED) $statusType = 'status6'; |
||
| 838 | |||
| 839 | return dolGetStatus($this->labelStatus[$status], $this->labelStatus[$status], '', $statusType, $mode); |
||
| 840 | } |
||
| 841 | |||
| 842 | /** |
||
| 843 | * Load the info information in the object |
||
| 844 | * |
||
| 845 | * @param int $id Id of object |
||
| 846 | * @return void |
||
| 847 | */ |
||
| 848 | public function info($id) |
||
| 849 | { |
||
| 850 | $sql = 'SELECT rowid, date_creation as datec, tms as datem,'; |
||
| 851 | $sql .= ' fk_user_creat, fk_user_modif'; |
||
| 852 | $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t'; |
||
| 853 | $sql .= ' WHERE t.rowid = '.$id; |
||
| 854 | $result = $this->db->query($sql); |
||
| 855 | if ($result) |
||
| 856 | { |
||
| 857 | if ($this->db->num_rows($result)) |
||
| 858 | { |
||
| 859 | $obj = $this->db->fetch_object($result); |
||
| 860 | $this->id = $obj->rowid; |
||
| 861 | if ($obj->fk_user_author) |
||
| 862 | { |
||
| 863 | $cuser = new User($this->db); |
||
| 864 | $cuser->fetch($obj->fk_user_author); |
||
| 865 | $this->user_creation = $cuser; |
||
| 866 | } |
||
| 867 | |||
| 868 | if ($obj->fk_user_valid) |
||
| 869 | { |
||
| 870 | $vuser = new User($this->db); |
||
| 871 | $vuser->fetch($obj->fk_user_valid); |
||
| 872 | $this->user_validation = $vuser; |
||
| 873 | } |
||
| 874 | |||
| 875 | if ($obj->fk_user_cloture) |
||
| 876 | { |
||
| 877 | $cluser = new User($this->db); |
||
| 878 | $cluser->fetch($obj->fk_user_cloture); |
||
| 879 | $this->user_cloture = $cluser; |
||
| 880 | } |
||
| 881 | |||
| 882 | $this->date_creation = $this->db->jdate($obj->datec); |
||
| 883 | $this->date_modification = $this->db->jdate($obj->datem); |
||
| 884 | $this->date_validation = $this->db->jdate($obj->datev); |
||
| 885 | } |
||
| 886 | |||
| 887 | $this->db->free($result); |
||
| 888 | } else { |
||
| 889 | dol_print_error($this->db); |
||
| 890 | } |
||
| 891 | } |
||
| 892 | |||
| 893 | /** |
||
| 894 | * Create an array of lines |
||
| 895 | * |
||
| 896 | * @return array|int array of lines if OK, <0 if KO |
||
| 897 | */ |
||
| 898 | public function getLinesArray() |
||
| 899 | { |
||
| 900 | $this->lines = array(); |
||
| 901 | |||
| 902 | $objectline = new BOMLine($this->db); |
||
| 903 | $result = $objectline->fetchAll('ASC', 'position', 0, 0, array('customsql'=>'fk_bom = '.$this->id)); |
||
| 904 | |||
| 905 | if (is_numeric($result)) |
||
| 906 | { |
||
| 907 | $this->error = $this->error; |
||
| 908 | $this->errors = $this->errors; |
||
| 909 | return $result; |
||
| 910 | } else { |
||
| 911 | $this->lines = $result; |
||
| 912 | return $this->lines; |
||
| 913 | } |
||
| 914 | } |
||
| 915 | |||
| 916 | /** |
||
| 917 | * Create a document onto disk according to template module. |
||
| 918 | * |
||
| 919 | * @param string $modele Force template to use ('' to not force) |
||
| 920 | * @param Translate $outputlangs objet lang a utiliser pour traduction |
||
| 921 | * @param int $hidedetails Hide details of lines |
||
| 922 | * @param int $hidedesc Hide description |
||
| 923 | * @param int $hideref Hide ref |
||
| 924 | * @param null|array $moreparams Array to provide more information |
||
| 925 | * @return int 0 if KO, 1 if OK |
||
| 926 | */ |
||
| 927 | public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams = null) |
||
| 928 | { |
||
| 929 | global $conf, $langs; |
||
| 930 | |||
| 931 | $langs->load("mrp"); |
||
| 932 | |||
| 933 | if (!dol_strlen($modele)) { |
||
| 934 | $modele = 'standard'; |
||
| 935 | |||
| 936 | if ($this->modelpdf) { |
||
| 937 | $modele = $this->modelpdf; |
||
| 938 | } elseif (!empty($conf->global->BOM_ADDON_PDF)) { |
||
| 939 | $modele = $conf->global->BOM_ADDON_PDF; |
||
| 940 | } |
||
| 941 | } |
||
| 942 | |||
| 943 | $modelpath = "core/modules/bom/doc/"; |
||
| 944 | |||
| 945 | return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams); |
||
| 946 | } |
||
| 947 | |||
| 948 | /** |
||
| 949 | * Initialise object with example values |
||
| 950 | * Id must be 0 if object instance is a specimen |
||
| 951 | * |
||
| 952 | * @return void |
||
| 953 | */ |
||
| 954 | public function initAsSpecimen() |
||
| 955 | { |
||
| 956 | $this->initAsSpecimenCommon(); |
||
| 957 | $this->ref = 'BOM-123'; |
||
| 958 | $this->date = $this->date_creation; |
||
| 959 | } |
||
| 960 | |||
| 961 | |||
| 962 | /** |
||
| 963 | * Action executed by scheduler |
||
| 964 | * CAN BE A CRON TASK. In such a case, parameters come from the schedule job setup field 'Parameters' |
||
| 965 | * |
||
| 966 | * @return int 0 if OK, <>0 if KO (this function is used also by cron so only 0 is OK) |
||
| 967 | */ |
||
| 968 | public function doScheduledJob() |
||
| 989 | } |
||
| 990 | |||
| 991 | /** |
||
| 992 | * BOM costs calculation based on cost_price or pmp of each BOM line |
||
| 993 | * |
||
| 994 | * @return void |
||
| 995 | */ |
||
| 996 | public function calculateCosts() |
||
| 997 | { |
||
| 998 | include_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php'; |
||
| 999 | $this->unit_cost = 0; |
||
| 1000 | $this->total_cost = 0; |
||
| 1001 | |||
| 1002 | require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php'; |
||
| 1003 | $productFournisseur = new ProductFournisseur($this->db); |
||
| 1004 | |||
| 1005 | foreach ($this->lines as &$line) { |
||
| 1006 | $tmpproduct = new Product($this->db); |
||
| 1007 | $result= $tmpproduct->fetch($line->fk_product); |
||
| 1008 | if ($result < 0) { |
||
| 1009 | $this->error=$tmpproduct->error; |
||
| 1010 | return -1; |
||
| 1442 |