From ecd673399f5ae7fa8fac777c07b948f7eb8033f3 Mon Sep 17 00:00:00 2001 From: Regis Houssin Date: Sat, 9 Apr 2011 15:59:45 +0000 Subject: [PATCH] Fix: correct reorder line with fk_parent_line Fix: problem with create order from proposals with fk_parent_line --- htdocs/comm/propal/class/propal.class.php | 5 ++- htdocs/commande/class/commande.class.php | 23 +++++++++--- htdocs/core/class/commonobject.class.php | 45 ++++++++++++++++------- 3 files changed, 53 insertions(+), 20 deletions(-) diff --git a/htdocs/comm/propal/class/propal.class.php b/htdocs/comm/propal/class/propal.class.php index f2598222e2a..a6a8d78443b 100644 --- a/htdocs/comm/propal/class/propal.class.php +++ b/htdocs/comm/propal/class/propal.class.php @@ -350,7 +350,7 @@ class Propal extends CommonObject $rangtouse = $rang; if ($rangtouse == -1) { - $rangmax = $this->line_max(); + $rangmax = $this->line_max($fk_parent_line); $rangtouse = $rangmax + 1; } @@ -399,6 +399,9 @@ 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'); + // Mise a jour informations denormalisees au niveau de la propale meme $result=$this->update_price(1); if ($result > 0) diff --git a/htdocs/commande/class/commande.class.php b/htdocs/commande/class/commande.class.php index d4620e47b80..76cae62813b 100644 --- a/htdocs/commande/class/commande.class.php +++ b/htdocs/commande/class/commande.class.php @@ -592,11 +592,19 @@ class Commande extends CommonObject if ($this->id) { + $fk_parent_line=0; + $num=sizeof($this->lines); + /* * Insertion du detail des produits dans la base */ - for ($i = 0 ; $i < sizeof($this->lines) ; $i++) + for ($i=0;$i<$num;$i++) { + // Reset fk_parent_line for no child products and special product + if (($this->lines[$i]->product_type != 9 && empty($this->lines[$i]->fk_parent_line)) || $this->lines[$i]->product_type == 9) { + $fk_parent_line = 0; + } + $result = $this->addline( $this->id, $this->lines[$i]->desc, @@ -615,7 +623,8 @@ class Commande extends CommonObject $this->lines[$i]->date_end, $this->lines[$i]->product_type, $this->lines[$i]->rang, - $this->lines[$i]->special_code + $this->lines[$i]->special_code, + $fk_parent_line ); if ($result < 0) { @@ -624,6 +633,10 @@ class Commande extends CommonObject $this->db->rollback(); return -1; } + // Defined the new fk_parent_line + if ($result > 0 && $this->lines[$i]->product_type == 9) { + $fk_parent_line = $result; + } } // Mise a jour ref @@ -985,8 +998,8 @@ class Commande extends CommonObject $this->line->price=$price; $this->line->remise=$remise; - $result=$this->line->insert(); - if ($result > 0) + $resInsert=$this->line->insert(); + if ($resInsert > 0) { // Mise a jour informations denormalisees au niveau de la commande meme $this->id=$commandeid; // TODO A virer @@ -994,7 +1007,7 @@ class Commande extends CommonObject if ($result > 0) { $this->db->commit(); - return 1; + return $resInsert; } else { diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index 974b2e79920..cc5b5769fa2 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -700,7 +700,7 @@ class CommonObject * Stocke un numero de rang pour toutes les lignes de detail d'un element qui n'en ont pas. * @param renum true to renum all already ordered lines, false to renum only not already ordered lines. */ - function line_order($renum=false) + function line_order($renum=false, $rowidorder='ASC') { if (! $this->table_element_line) { @@ -727,7 +727,7 @@ class CommonObject { $sql = 'SELECT rowid FROM '.MAIN_DB_PREFIX.$this->table_element_line; $sql.= ' WHERE '.$this->fk_element.' = '.$this->id; - $sql.= ' ORDER BY rang ASC, rowid ASC'; + $sql.= ' ORDER BY rang ASC, rowid '.$rowidorder; $resql = $this->db->query($sql); if ($resql) { @@ -736,14 +736,10 @@ class CommonObject while ($i < $num) { $row = $this->db->fetch_row($resql); - $li[$i] = $row[0]; + $this->updateRangOfLine($row[0], ($i+1)); $i++; } } - for ($i = 0 ; $i < sizeof($li) ; $i++) - { - $this->updateRangOfLine($li[$i], ($i+1)); - } } } @@ -896,15 +892,36 @@ class CommonObject * Get max value used for position of line (rang) * @result int Max value of rang in table of lines */ - function line_max() + function line_max($fk_parent_line=0) { - $sql = 'SELECT max(rang) FROM '.MAIN_DB_PREFIX.$this->table_element_line; - $sql.= ' WHERE '.$this->fk_element.' = '.$this->id; - $resql = $this->db->query($sql); - if ($resql) + // Search the last rang with fk_parent_line + if ($fk_parent_line) { - $row = $this->db->fetch_row($resql); - return $row[0]; + $sql = 'SELECT max(rang) FROM '.MAIN_DB_PREFIX.$this->table_element_line; + $sql.= ' WHERE '.$this->fk_element.' = '.$this->id; + $sql.= ' AND fk_parent_line = '.$fk_parent_line; + $resql = $this->db->query($sql); + if ($resql) + { + $row = $this->db->fetch_row($resql); + if (! empty($row[0])) { + return $row[0]; + } else { + return $this->getRangOfLine($fk_parent_line); + } + } + } + // If not, search the last rang of element + else + { + $sql = 'SELECT max(rang) FROM '.MAIN_DB_PREFIX.$this->table_element_line; + $sql.= ' WHERE '.$this->fk_element.' = '.$this->id; + $resql = $this->db->query($sql); + if ($resql) + { + $row = $this->db->fetch_row($resql); + return $row[0]; + } } }