From 95622c22dfbde3d859a096007c68b5c593af0f8a Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Mon, 10 Feb 2025 18:09:32 +0100 Subject: [PATCH] Close #32670 --- htdocs/comm/propal/class/propal.class.php | 21 ++++++----- htdocs/commande/card.php | 7 ++-- htdocs/commande/class/commande.class.php | 36 +++++++++++-------- htdocs/compta/facture/class/facture.class.php | 27 ++++++++------ .../class/fournisseur.commande.class.php | 32 ++++++++++------- .../fourn/class/fournisseur.facture.class.php | 28 +++++++++------ 6 files changed, 89 insertions(+), 62 deletions(-) diff --git a/htdocs/comm/propal/class/propal.class.php b/htdocs/comm/propal/class/propal.class.php index cbc2ec2f1d0..a215eece1c4 100644 --- a/htdocs/comm/propal/class/propal.class.php +++ b/htdocs/comm/propal/class/propal.class.php @@ -813,17 +813,22 @@ class Propal extends CommonObject $result = $this->line->insert(); if ($result > 0) { - // Reorder if child line - if (!empty($fk_parent_line)) { - $this->line_order(true, 'DESC'); - } elseif ($ranktouse > 0 && $ranktouse <= count($this->lines)) { // Update all rank of all other lines - $linecount = count($this->lines); - for ($ii = $ranktouse; $ii <= $linecount; $ii++) { - $this->updateRangOfLine($this->lines[$ii - 1]->id, $ii + 1); + if (!isset($this->context['createfromclone'])) { + if (!empty($fk_parent_line)) { + // Always reorder if child line + $this->line_order(true, 'DESC'); + } elseif ($ranktouse > 0 && $ranktouse <= count($this->lines)) { + // Update all rank of all other lines starting from the same $ranktouse + $linecount = count($this->lines); + for ($ii = $ranktouse; $ii <= $linecount; $ii++) { + $this->updateRangOfLine($this->lines[$ii - 1]->id, $ii + 1); + } } + + $this->lines[] = $this->line; } - // Mise a jour information denormalisees au niveau de la propale meme + // Update denormalized fields at the order level if (empty($noupdateafterinsertline)) { $result = $this->update_price(1, 'auto', 0, $mysoc); // This method is designed to add line from user input so total calculation must be done using 'auto' mode. } diff --git a/htdocs/commande/card.php b/htdocs/commande/card.php index e60536c9bcb..881ed15a9ee 100644 --- a/htdocs/commande/card.php +++ b/htdocs/commande/card.php @@ -218,10 +218,10 @@ if (empty($reshook)) { setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('IdThirdParty')), null, 'errors'); } else { if ($object->id > 0) { - // Because createFromClone modifies the object, we must clone it so that we can restore it later - $orig = clone $object; + // We clone object to avoid to denaturate loaded object when setting some properties for clone or if createFromClone modifies the object. + $objectutil = dol_clone($object, 1); - $result = $object->createFromClone($user, $socid); + $result = $objectutil->createFromClone($user, $socid); if ($result > 0) { $warningMsgLineList = array(); // check all product lines are to sell otherwise add a warning message for each product line is not to sell @@ -243,7 +243,6 @@ if (empty($reshook)) { exit; } else { setEventMessages($object->error, $object->errors, 'errors'); - $object = $orig; $action = ''; } } diff --git a/htdocs/commande/class/commande.class.php b/htdocs/commande/class/commande.class.php index 03359651426..640216e0d4f 100644 --- a/htdocs/commande/class/commande.class.php +++ b/htdocs/commande/class/commande.class.php @@ -1097,6 +1097,7 @@ class Commande extends CommonOrder $line->ref_ext, 1 ); + if ($result < 0) { if ($result != self::STOCK_NOT_ENOUGH_FOR_ORDER) { $this->error = $this->db->lasterror(); @@ -1536,11 +1537,11 @@ class Commande extends CommonOrder */ public function addline($desc, $pu_ht, $qty, $txtva, $txlocaltax1 = 0, $txlocaltax2 = 0, $fk_product = 0, $remise_percent = 0, $info_bits = 0, $fk_remise_except = 0, $price_base_type = 'HT', $pu_ttc = 0, $date_start = '', $date_end = '', $type = 0, $rang = -1, $special_code = 0, $fk_parent_line = 0, $fk_fournprice = null, $pa_ht = 0, $label = '', $array_options = array(), $fk_unit = null, $origin = '', $origin_id = 0, $pu_ht_devise = 0, $ref_ext = '', $noupdateafterinsertline = 0) { - global $mysoc, $conf, $langs, $user; + global $mysoc, $langs, $user; $logtext = "::addline commandeid=$this->id, desc=$desc, pu_ht=$pu_ht, qty=$qty, txtva=$txtva, fk_product=$fk_product, remise_percent=$remise_percent"; $logtext .= ", info_bits=$info_bits, fk_remise_except=$fk_remise_except, price_base_type=$price_base_type, pu_ttc=$pu_ttc, date_start=$date_start"; - $logtext .= ", date_end=$date_end, type=$type special_code=$special_code, fk_unit=$fk_unit, origin=$origin, origin_id=$origin_id, pu_ht_devise=$pu_ht_devise, ref_ext=$ref_ext"; + $logtext .= ", date_end=$date_end, type=$type special_code=$special_code, fk_unit=$fk_unit, origin=$origin, origin_id=$origin_id, pu_ht_devise=$pu_ht_devise, ref_ext=$ref_ext rang=$rang"; dol_syslog(get_class($this).$logtext, LOG_DEBUG); if ($this->statut == self::STATUS_DRAFT) { @@ -1666,6 +1667,7 @@ class Commande extends CommonOrder // Rang to use $ranktouse = $rang; + if ($ranktouse == -1) { $rangmax = $this->line_max($fk_parent_line); $ranktouse = $rangmax + 1; @@ -1738,25 +1740,29 @@ class Commande extends CommonOrder $result = $this->line->insert($user); if ($result > 0) { - // Reorder if child line - if (!empty($fk_parent_line)) { - $this->line_order(true, 'DESC'); - } elseif ($ranktouse > 0 && $ranktouse <= count($this->lines)) { // Update all rank of all other lines - $linecount = count($this->lines); - for ($ii = $ranktouse; $ii <= $linecount; $ii++) { - $this->updateRangOfLine($this->lines[$ii - 1]->id, $ii + 1); - } - } - - // Mise a jour information denormalisees au niveau de la commande meme + // Update denormalized fields at the order level if (empty($noupdateafterinsertline)) { $result = $this->update_price(1, 'auto', 0, $mysoc); // This method is designed to add line from user input so total calculation must be done using 'auto' mode. } if ($result > 0) { + if (!isset($this->context['createfromclone'])) { + if (!empty($fk_parent_line)) { + // Always reorder if child line + $this->line_order(true, 'DESC'); + } elseif ($ranktouse > 0 && $ranktouse <= count($this->lines)) { + // Update all rank of all other lines starting from the same $ranktouse + $linecount = count($this->lines); + for ($ii = $ranktouse; $ii <= $linecount; $ii++) { + $this->updateRangOfLine($this->lines[$ii - 1]->id, $ii + 1); + } + } + + $this->lines[] = $this->line; + } + $this->db->commit(); - $this->lines[] = $this->line; - return $this->line->id; + return $line->id; } else { $this->db->rollback(); return -1; diff --git a/htdocs/compta/facture/class/facture.class.php b/htdocs/compta/facture/class/facture.class.php index cd69a48e657..43859ecab23 100644 --- a/htdocs/compta/facture/class/facture.class.php +++ b/htdocs/compta/facture/class/facture.class.php @@ -4097,21 +4097,26 @@ class Facture extends CommonInvoice $result = $this->line->insert(); if ($result > 0) { - // Reorder if child line - if (!empty($fk_parent_line)) { - $this->line_order(true, 'DESC'); - } elseif ($ranktouse > 0 && $ranktouse <= count($this->lines)) { // Update all rank of all other lines - $linecount = count($this->lines); - for ($ii = $ranktouse; $ii <= $linecount; $ii++) { - $this->updateRangOfLine($this->lines[$ii - 1]->id, $ii + 1); - } - } - - // Mise a jour information denormalisees au niveau de la facture meme + // Update denormalized fields at the order level if (empty($noupdateafterinsertline)) { $result = $this->update_price(1, 'auto', 0, $mysoc); // The addline method is designed to add line from user input so total calculation with update_price must be done using 'auto' mode. } + if (!isset($this->context['createfromclone'])) { + if (!empty($fk_parent_line)) { + // Always reorder if child line + $this->line_order(true, 'DESC'); + } elseif ($ranktouse > 0 && $ranktouse <= count($this->lines)) { + // Update all rank of all other lines starting from the same $ranktouse + $linecount = count($this->lines); + for ($ii = $ranktouse; $ii <= $linecount; $ii++) { + $this->updateRangOfLine($this->lines[$ii - 1]->id, $ii + 1); + } + } + + $this->lines[] = $this->line; + } + if ($result > 0) { $this->db->commit(); return $this->line->id; diff --git a/htdocs/fourn/class/fournisseur.commande.class.php b/htdocs/fourn/class/fournisseur.commande.class.php index 957cd70fb33..7d87e6dbde6 100644 --- a/htdocs/fourn/class/fournisseur.commande.class.php +++ b/htdocs/fourn/class/fournisseur.commande.class.php @@ -1993,7 +1993,7 @@ class CommandeFournisseur extends CommonOrder * @param ?int $date_end Date end of service * @param array $array_options extrafields array * @param ?int $fk_unit Code of the unit to use. Null to use the default one - * @param int|string $pu_ht_devise Amount in currency + * @param int|string $pu_ht_devise Amount in currency * @param string $origin 'order', ... * @param int $origin_id Id of origin object * @param int $rang Rank @@ -2002,7 +2002,7 @@ class CommandeFournisseur extends CommonOrder */ public function addline($desc, $pu_ht, $qty, $txtva, $txlocaltax1 = 0.0, $txlocaltax2 = 0.0, $fk_product = 0, $fk_prod_fourn_price = 0, $ref_supplier = '', $remise_percent = 0.0, $price_base_type = 'HT', $pu_ttc = 0.0, $type = 0, $info_bits = 0, $notrigger = 0, $date_start = null, $date_end = null, $array_options = [], $fk_unit = null, $pu_ht_devise = 0, $origin = '', $origin_id = 0, $rang = -1, $special_code = 0) { - global $langs, $mysoc, $conf; + global $langs, $mysoc; dol_syslog(get_class($this)."::addline $desc, $pu_ht, $qty, $txtva, $txlocaltax1, $txlocaltax2, $fk_product, $fk_prod_fourn_price, $ref_supplier, $remise_percent, $price_base_type, $pu_ttc, $type, $info_bits, $notrigger, $date_start, $date_end, $fk_unit, $pu_ht_devise, $origin, $origin_id"); include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php'; @@ -2238,19 +2238,25 @@ class CommandeFournisseur extends CommonOrder $result = $this->line->insert($notrigger); if ($result > 0) { - // Reorder if child line - if (!empty($this->line->fk_parent_line)) { - $this->line_order(true, 'DESC'); - } elseif ($rang > 0 && $rang <= count($this->lines)) { // Update all rank of all other lines - $linecount = count($this->lines); - for ($ii = $rang; $ii <= $linecount; $ii++) { - $this->updateRangOfLine($this->lines[$ii - 1]->id, $ii + 1); - } - } - - // Mise a jour information denormalisees au niveau de la commande meme + // Update denormalized fields at the order level $result = $this->update_price(1, 'auto', 0, $this->thirdparty); // This method is designed to add line from user input so total calculation must be done using 'auto' mode. + if ($result > 0) { + if (!isset($this->context['createfromclone'])) { + if (!empty($this->line->fk_parent_line)) { + // Always reorder if child line + $this->line_order(true, 'DESC'); + } elseif ($rang > 0 && $rang <= count($this->lines)) { + // Update all rank of all other lines starting from the same $ranktouse + $linecount = count($this->lines); + for ($ii = $rang; $ii <= $linecount; $ii++) { + $this->updateRangOfLine($this->lines[$ii - 1]->id, $ii + 1); + } + } + + $this->lines[] = $this->line; + } + $this->db->commit(); return $this->line->id; } else { diff --git a/htdocs/fourn/class/fournisseur.facture.class.php b/htdocs/fourn/class/fournisseur.facture.class.php index 88331cf8bbd..d09a98e7429 100644 --- a/htdocs/fourn/class/fournisseur.facture.class.php +++ b/htdocs/fourn/class/fournisseur.facture.class.php @@ -2303,19 +2303,25 @@ class FactureFournisseur extends CommonInvoice $result = $supplierinvoiceline->insert($notrigger); if ($result > 0) { - // Reorder if child line - if (!empty($fk_parent_line)) { - $this->line_order(true, 'DESC'); - } elseif ($rang > 0 && $rang <= count($this->lines)) { // Update all rank of all other lines - $linecount = count($this->lines); - for ($ii = $rang; $ii <= $linecount; $ii++) { - $this->updateRangOfLine($this->lines[$ii - 1]->id, $ii + 1); - } - } - - // Mise a jour information denormalisees au niveau de la facture meme + // Update denormalized fields at the order level $result = $this->update_price(1, 'auto', 0, $this->thirdparty); // The addline method is designed to add line from user input so total calculation with update_price must be done using 'auto' mode. + if ($result > 0) { + if (!isset($this->context['createfromclone'])) { + if (!empty($fk_parent_line)) { + // Always reorder if child line + $this->line_order(true, 'DESC'); + } elseif ($rang > 0 && $rang <= count($this->lines)) { + // Update all rank of all other lines starting from the same $ranktouse + $linecount = count($this->lines); + for ($ii = $rang; $ii <= $linecount; $ii++) { + $this->updateRangOfLine($this->lines[$ii - 1]->id, $ii + 1); + } + } + + $this->lines[] = $supplierinvoiceline; + } + $this->db->commit(); return $supplierinvoiceline->id; } else {