From 32fe761f01ee2c5f7f94394caccae0a64a218ebd Mon Sep 17 00:00:00 2001 From: Christian Humpel Date: Sun, 14 Aug 2022 12:55:48 +0200 Subject: [PATCH 1/5] NEW: Added "Get lines from BOM" at the REST Service --- htdocs/bom/class/api_boms.class.php | 31 +++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/htdocs/bom/class/api_boms.class.php b/htdocs/bom/class/api_boms.class.php index 91af888ffd8..604165868c7 100644 --- a/htdocs/bom/class/api_boms.class.php +++ b/htdocs/bom/class/api_boms.class.php @@ -2,6 +2,7 @@ /* Copyright (C) 2015 Jean-François Ferry * Copyright (C) 2019 Maxime Kohlhaas * Copyright (C) 2020 Frédéric France + * Copyright (C) 2022 Christian Humpel * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -279,6 +280,36 @@ class Boms extends DolibarrApi ); } + /** + * Get lines of an BOM + * + * @param int $id Id of BOM + * + * @url GET {id}/lines + * + * @return array + */ + public function getLines($id) + { + if (!DolibarrApiAccess::$user->rights->bom->read) { + throw new RestException(401); + } + + $result = $this->bom->fetch($id); + if (!$result) { + throw new RestException(404, 'BOM not found'); + } + + if (!DolibarrApi::_checkAccessToResource('bom_bom', $this->commande->id)) { + throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + } + $this->bom->getLinesArray(); + $result = array(); + foreach ($this->bom->lines as $line) { + array_push($result, $this->_cleanObjectDatas($line)); + } + return $result; + } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore /** From a2041650184f337bb6809a6aabc36fecbae1ca90 Mon Sep 17 00:00:00 2001 From: Christian Humpel Date: Sun, 14 Aug 2022 15:58:49 +0200 Subject: [PATCH 2/5] #21750 NEW: Added "Get lines from BOM" at the REST Service --- htdocs/bom/class/api_boms.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/bom/class/api_boms.class.php b/htdocs/bom/class/api_boms.class.php index 604165868c7..5483df0596b 100644 --- a/htdocs/bom/class/api_boms.class.php +++ b/htdocs/bom/class/api_boms.class.php @@ -300,7 +300,7 @@ class Boms extends DolibarrApi throw new RestException(404, 'BOM not found'); } - if (!DolibarrApi::_checkAccessToResource('bom_bom', $this->commande->id)) { + if (!DolibarrApi::_checkAccessToResource('bom_bom', $this->bom->id)) { throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); } $this->bom->getLinesArray(); From 0576f3159236c19b8f5301dfa4fe3c5289aa86e0 Mon Sep 17 00:00:00 2001 From: Christian Humpel Date: Mon, 15 Aug 2022 15:18:08 +0200 Subject: [PATCH 3/5] #21750 NEW: Added "Get lines and Post lines from BOM" at the REST Service --- htdocs/bom/class/api_boms.class.php | 45 ++++++++++++++ htdocs/bom/class/bom.class.php | 94 +++++++++++++++++++++++++++++ 2 files changed, 139 insertions(+) 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 * From 015f179acbbe67bdefa2164a444c9f6afc3bc746 Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Mon, 15 Aug 2022 13:19:16 +0000 Subject: [PATCH 4/5] Fixing style errors. --- htdocs/bom/class/bom.class.php | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/htdocs/bom/class/bom.class.php b/htdocs/bom/class/bom.class.php index f468604d5ed..feb7385564f 100644 --- a/htdocs/bom/class/bom.class.php +++ b/htdocs/bom/class/bom.class.php @@ -514,7 +514,8 @@ class BOM extends CommonObject * @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){ + 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; @@ -522,29 +523,29 @@ class BOM extends CommonObject $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){ + if ($this->statut == self::STATUS_DRAFT) { include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php'; // Clean parameters - if (empty($qty)){ + if (empty($qty)) { $qty = 0; } - if (empty($qty_frozen)){ + if (empty($qty_frozen)) { $qty_frozen = 0; } - if (empty($disable_stock_change)){ + if (empty($disable_stock_change)) { $disable_stock_change = 0; } - if (empty($efficiency)){ + if (empty($efficiency)) { $efficiency = 1.0; } - if (empty($fk_bom_child)){ + if (empty($fk_bom_child)) { $fk_bom_child = null; } - if (empty($import_key)){ + if (empty($import_key)) { $import_key = null; } - if (empty($position)){ + if (empty($position)) { $position = -1; } @@ -556,7 +557,7 @@ class BOM extends CommonObject // Rank to use $rankToUse = $position; - if ($rankToUse == -1){ + if ($rankToUse == -1) { $rangMax = $this->line_max(); $rankToUse = $rangMax + 1; } @@ -578,7 +579,7 @@ class BOM extends CommonObject $result = $this->line->create($user); - if ($result > 0){ + if ($result > 0) { $this->calculateCosts(); $this->db->commit(); return $this->line->id; @@ -588,12 +589,10 @@ class BOM extends CommonObject $this->db->rollback(); return -2; } - - } else{ + } else { dol_syslog(get_class($this)."::addLine status of BOM must be Draft to allow use of ->addLine()", LOG_ERR); return -3; } - } /** From 91094d48e63c893fc689be8b2dd9e6696c92db06 Mon Sep 17 00:00:00 2001 From: Christian Humpel Date: Mon, 15 Aug 2022 16:25:42 +0200 Subject: [PATCH 5/5] #21750 NEW: Added "Get lines and Post lines from BOM" at the REST Service --- htdocs/bom/class/bom.class.php | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/htdocs/bom/class/bom.class.php b/htdocs/bom/class/bom.class.php index f468604d5ed..77e8a1498b3 100644 --- a/htdocs/bom/class/bom.class.php +++ b/htdocs/bom/class/bom.class.php @@ -505,14 +505,15 @@ class BOM extends CommonObject /** * 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 + * @param int $fk_product Id of product + * @param float $qty Quantity + * @param int $qty_frozen Frozen quantity + * @param int $disable_stock_change Disable stock change on using in MO + * @param float $efficiency Efficiency in MO + * @param int $position Position of BOM-Line in BOM-Lines + * @param int $fk_bom_child Id of BOM Child + * @param string $import_key Import Key + * @return int <0 if KO, >0 if OK */ 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){