diff --git a/htdocs/bom/class/api_boms.class.php b/htdocs/bom/class/api_boms.class.php index 7e274e9de9a..3c06a658b96 100644 --- a/htdocs/bom/class/api_boms.class.php +++ b/htdocs/bom/class/api_boms.class.php @@ -344,7 +344,7 @@ class Boms extends DolibarrApi $request_data->qty_frozen, $request_data->disable_stock_change, $request_data->efficiency, - $request_data->postion, + $request_data->position, $request_data->fk_bom_child, $request_data->import_key ); @@ -356,6 +356,103 @@ class Boms extends DolibarrApi } } + /** + * Update a line to given BOM + * + * @param int $id Id of BOM to update + * @param int $lineid Id of line to update + * @param array $request_data BOMLine data + * + * @url PUT {id}/lines/{lineid} + * + * @return array|bool + */ + public function putLine($id, $lineid, $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->updateLine( + $lineid, + $request_data->qty, + $request_data->qty_frozen, + $request_data->disable_stock_change, + $request_data->efficiency, + $request_data->position, + $request_data->fk_bom_child, + $request_data->import_key + ); + + if ($updateRes > 0) { + $result = $this->get($id); + unset($result->line); + return $this->_cleanObjectDatas($result); + } + return false; + } + + /** + * Delete a line to given BOM + * + * + * @param int $id Id of BOM to update + * @param int $lineid Id of line to delete + * + * @url DELETE {id}/lines/{lineid} + * + * @return int + * + * @throws RestException 401 + * @throws RestException 404 + * @throws RestException 500 + */ + public function deleteLine($id, $lineid) + { + 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); + } + + //Check the rowid is a line of current bom object + $lineIdIsFromObject = false; + foreach ($this->bom->lines as $bl) { + if ($bl->id == $lineid) { + $lineIdIsFromObject = true; + break; + } + } + if (!$lineIdIsFromObject) { + throw new RestException(500, 'Line to delete (rowid: '.$lineid.') is not a line of BOM (id: '.$this->bom->id.')'); + } + + $updateRes = $this->bom->deleteline(DolibarrApiAccess::$user, $lineid); + if ($updateRes > 0) { + return $this->get($id); + } else { + throw new RestException(405, $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 42ced4a2f0e..e774e4ddacf 100644 --- a/htdocs/bom/class/bom.class.php +++ b/htdocs/bom/class/bom.class.php @@ -513,7 +513,7 @@ class BOM extends CommonObject * @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 + * @return int <0 if KO, Id of created object 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) { @@ -557,10 +557,17 @@ class BOM extends CommonObject $this->db->begin(); // Rank to use + $rangMax = $this->line_max(); $rankToUse = $position; - if ($rankToUse == -1) { - $rangMax = $this->line_max(); + if ($rankToUse <= 0 or $rankToUse > $rangMax) { // New line after existing lines $rankToUse = $rangMax + 1; + } else { // New line between the existing lines + foreach ($this->lines as $bl) { + if ($bl->position >= $rankToUse) { + $bl->position++; + $bl->update($user); + } + } } // Insert line @@ -583,7 +590,114 @@ class BOM extends CommonObject if ($result > 0) { $this->calculateCosts(); $this->db->commit(); - return $this->line->id; + return $result; + } 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; + } + } + + /** + * Update an BOM line into database + * + * @param int $rowid Id of line to update + * @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, Id of updated BOM-Line if OK + */ + public function updateLine($rowid, $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 = "::updateLine bomid=$this->id, qty=$qty, 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(); + + //Fetch current line from the database and then clone the object and set it in $oldline property + $line = new BOMLine($this->db); + $line->fetch($rowid); + $line->fetch_optionals(); + + $staticLine = clone $line; + $line->oldcopy = $staticLine; + $this->line = $line; + $this->line->context = $this->context; + + // Rank to use + $rankToUse = (int) $position; + if ($rankToUse != $line->oldcopy->position) { // check if position have a new value + foreach ($this->lines as $bl) { + if ($bl->position >= $rankToUse AND $bl->position < ($line->oldcopy->position + 1)) { // move rank up + $bl->position++; + $bl->update($user); + } + if ($bl->position <= $rankToUse AND $bl->position > ($line->oldcopy->position)) { // move rank down + $bl->position--; + $bl->update($user); + } + } + } + + + $this->line->fk_bom = $this->id; + $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->update($user); + + if ($result > 0) { + $this->calculateCosts(); + $this->db->commit(); + return $result; } else { $this->error = $this->line->error; dol_syslog(get_class($this)."::addLine error=".$this->error, LOG_ERR); @@ -611,7 +725,38 @@ class BOM extends CommonObject return -2; } - return $this->deleteLineCommon($user, $idline, $notrigger); + $this->db->begin(); + + //Fetch current line from the database and then clone the object and set it in $oldline property + $line = new BOMLine($this->db); + $line->fetch($idline); + $line->fetch_optionals(); + + $staticLine = clone $line; + $line->oldcopy = $staticLine; + $this->line = $line; + $this->line->context = $this->context; + + $result = $this->line->delete($user, $notrigger); + + //Positions (rank) reordering + foreach ($this->lines as $bl) { + if ($bl->position > ($line->oldcopy->position)) { // move rank down + $bl->position--; + $bl->update($user); + } + } + + if ($result > 0) { + $this->calculateCosts(); + $this->db->commit(); + return $result; + } else { + $this->error = $this->line->error; + dol_syslog(get_class($this)."::addLine error=".$this->error, LOG_ERR); + $this->db->rollback(); + return -2; + } } /** diff --git a/htdocs/install/doctemplates/websites/website_template-style03.zip b/htdocs/install/doctemplates/websites/website_template-style03.zip index 1d6af1e04f4..f831b789e34 100644 Binary files a/htdocs/install/doctemplates/websites/website_template-style03.zip and b/htdocs/install/doctemplates/websites/website_template-style03.zip differ diff --git a/htdocs/install/mysql/migration/16.0.0-17.0.0.sql b/htdocs/install/mysql/migration/16.0.0-17.0.0.sql index fc6fd86420a..9aeae78de02 100644 --- a/htdocs/install/mysql/migration/16.0.0-17.0.0.sql +++ b/htdocs/install/mysql/migration/16.0.0-17.0.0.sql @@ -84,3 +84,5 @@ ALTER TABLE llx_societe ADD last_main_doc VARCHAR(255) NULL AFTER model_pdf; ALTER TABLE llx_ticket ADD COLUMN ip varchar(250); ALTER TABLE llx_ticket ADD email_date datetime after email_msgid; + +ALTER TABLE llx_cronjob ADD COLUMN pid integer; diff --git a/htdocs/install/mysql/tables/llx_cronjob.sql b/htdocs/install/mysql/tables/llx_cronjob.sql index 27e8a31cc9a..012801c28bb 100644 --- a/htdocs/install/mysql/tables/llx_cronjob.sql +++ b/htdocs/install/mysql/tables/llx_cronjob.sql @@ -47,6 +47,7 @@ CREATE TABLE llx_cronjob autodelete integer DEFAULT 0, -- 0=Job is kept unchanged once nbrun > maxrun or date > dateend, 2=Job must be archived (archive = status 2) once nbrun > maxrun or date > dateend status integer NOT NULL DEFAULT 1, -- 0=disabled, 1=enabled, 2=archived processing integer NOT NULL DEFAULT 0, -- 1=process currently running + pid integer, -- The cronjob PID, NULL if not in processing test varchar(255) DEFAULT '1', fk_user_author integer DEFAULT NULL, fk_user_mod integer DEFAULT NULL, diff --git a/htdocs/product/stock/replenish.php b/htdocs/product/stock/replenish.php index 91833e95271..c8d10444711 100644 --- a/htdocs/product/stock/replenish.php +++ b/htdocs/product/stock/replenish.php @@ -467,7 +467,7 @@ if ($usevirtualstock) { $sqlProductionToConsume .= " AND mp5.role IN ('toconsume', 'consummed')"; $sqlProductionToConsume .= " AND mm5.status IN (1,2))"; - $sqlProductionToProduce = "(SELECT GREATEST(0, ".$db->ifsql("SUM(".$db->ifsql("mp5.role = 'toproduce'", 'mp5.qty', '- mp5.qty').") IS NULL", "0", "SUM(".$db->ifsql("mp5.role = 'toconsume'", 'mp5.qty', '- mp5.qty').")").") as qty"; // We need the ifsql because if result is 0 for product p.rowid, we must return 0 and not NULL + $sqlProductionToProduce = "(SELECT GREATEST(0, ".$db->ifsql("SUM(".$db->ifsql("mp5.role = 'toproduce'", 'mp5.qty', '- mp5.qty').") IS NULL", "0", "SUM(".$db->ifsql("mp5.role = 'toproduce'", 'mp5.qty', '- mp5.qty').")").") as qty"; // We need the ifsql because if result is 0 for product p.rowid, we must return 0 and not NULL $sqlProductionToProduce .= " FROM ".MAIN_DB_PREFIX."mrp_mo as mm5,"; $sqlProductionToProduce .= " ".MAIN_DB_PREFIX."mrp_production as mp5"; $sqlProductionToProduce .= " WHERE mm5.rowid = mp5.fk_mo AND mm5.entity IN (".getEntity(!empty($conf->global->STOCK_CALCULATE_VIRTUAL_STOCK_TRANSVERSE_MODE) ? 'stock' : 'mo').")";