diff --git a/htdocs/bom/class/api_boms.class.php b/htdocs/bom/class/api_boms.class.php index 5483df0596b..7e274e9de9a 100644 --- a/htdocs/bom/class/api_boms.class.php +++ b/htdocs/bom/class/api_boms.class.php @@ -311,6 +311,51 @@ class Boms extends DolibarrApi return $result; } + /** + * Add a line to given BOM + * + * @param int $id Id of BOM to update + * @param array $request_data BOMLine data + * + * @url POST {id}/lines + * + * @return int + */ + public function postLine($id, $request_data = null) + { + if (!DolibarrApiAccess::$user->rights->bom->write) { + throw new RestException(401); + } + + $result = $this->bom->fetch($id); + if (!$result) { + throw new RestException(404, 'BOM not found'); + } + + if (!DolibarrApi::_checkAccessToResource('bom_bom', $this->bom->id)) { + throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + } + + $request_data = (object) $request_data; + + $updateRes = $this->bom->addLine( + $request_data->fk_product, + $request_data->qty, + $request_data->qty_frozen, + $request_data->disable_stock_change, + $request_data->efficiency, + $request_data->postion, + $request_data->fk_bom_child, + $request_data->import_key + ); + + if ($updateRes > 0) { + return $updateRes; + } else { + throw new RestException(400, $this->bom->error); + } + } + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore /** * Clean sensible object datas diff --git a/htdocs/bom/class/bom.class.php b/htdocs/bom/class/bom.class.php index 68cd44dcf46..f468604d5ed 100644 --- a/htdocs/bom/class/bom.class.php +++ b/htdocs/bom/class/bom.class.php @@ -502,6 +502,100 @@ class BOM extends CommonObject //return $this->deleteCommon($user, $notrigger, 1); } + /** + * Add an BOM line into database (linked to BOM) + * + * @param $fk_product + * @param $qty + * @param $qty_frozen + * @param $disable_stock_change + * @param $efficiency + * @param $fk_bom_child + * @param $import_key + * @return int + */ + public function addLine($fk_product, $qty, $qty_frozen = 0, $disable_stock_change = 0, $efficiency = 1.0, $position = -1, $fk_bom_child = null, $import_key = null){ + + global $mysoc, $conf, $langs, $user; + + $logtext = "::addLine bomid=$this->id, qty=$qty, fk_product=$fk_product, qty_frozen=$qty_frozen, disable_stock_change=$disable_stock_change, efficiency=$efficiency"; + $logtext .= ", fk_bom_child=$fk_bom_child, import_key=$import_key"; + dol_syslog(get_class($this).$logtext, LOG_DEBUG); + + if ($this->statut == self::STATUS_DRAFT){ + include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php'; + + // Clean parameters + if (empty($qty)){ + $qty = 0; + } + if (empty($qty_frozen)){ + $qty_frozen = 0; + } + if (empty($disable_stock_change)){ + $disable_stock_change = 0; + } + if (empty($efficiency)){ + $efficiency = 1.0; + } + if (empty($fk_bom_child)){ + $fk_bom_child = null; + } + if (empty($import_key)){ + $import_key = null; + } + if (empty($position)){ + $position = -1; + } + + $qty = price2num($qty); + $efficiency = price2num($efficiency); + $position = price2num($position); + + $this->db->begin(); + + // Rank to use + $rankToUse = $position; + if ($rankToUse == -1){ + $rangMax = $this->line_max(); + $rankToUse = $rangMax + 1; + } + + // Insert line + $this->line = new BOMLine($this->db); + + $this->line->context = $this->context; + + $this->line->fk_bom = $this->id; + $this->line->fk_product = $fk_product; + $this->line->qty = $qty; + $this->line->qty_frozen = $qty_frozen; + $this->line->disable_stock_change = $disable_stock_change; + $this->line->efficiency = $efficiency; + $this->line->fk_bom_child = $fk_bom_child; + $this->line->import_key = $import_key; + $this->line->position = $rankToUse; + + $result = $this->line->create($user); + + if ($result > 0){ + $this->calculateCosts(); + $this->db->commit(); + return $this->line->id; + } else { + $this->error = $this->line->error; + dol_syslog(get_class($this)."::addLine error=".$this->error, LOG_ERR); + $this->db->rollback(); + return -2; + } + + } else{ + dol_syslog(get_class($this)."::addLine status of BOM must be Draft to allow use of ->addLine()", LOG_ERR); + return -3; + } + + } + /** * Delete a line of object in database *