diff --git a/htdocs/comm/propal/class/propal.class.php b/htdocs/comm/propal/class/propal.class.php index 3c4cc7277d2..362a93613a6 100644 --- a/htdocs/comm/propal/class/propal.class.php +++ b/htdocs/comm/propal/class/propal.class.php @@ -33,6 +33,7 @@ */ require_once(DOL_DOCUMENT_ROOT ."/core/class/commonobject.class.php"); +require_once(DOL_DOCUMENT_ROOT ."/core/class/commonobjectline.class.php"); require_once(DOL_DOCUMENT_ROOT ."/product/class/product.class.php"); require_once(DOL_DOCUMENT_ROOT ."/contact/class/contact.class.php"); @@ -524,15 +525,17 @@ class Propal extends CommonObject * \param idligne Id de la ligne detail a supprimer * \return int >0 si ok, <0 si ko */ - function delete_product($idligne) + function delete_product($lineid) { if ($this->statut == 0) { - $sql = "DELETE FROM ".MAIN_DB_PREFIX."propaldet WHERE rowid = ".$idligne; + $sql = "DELETE FROM ".MAIN_DB_PREFIX."propaldet WHERE rowid = ".$lineid; if ($this->db->query($sql) ) { $this->update_price(); + + $this->delRangOfLine($lineid, $this->element); return 1; } @@ -926,12 +929,16 @@ class Propal extends CommonObject * Lignes propales liees a un produit ou non */ $sql = "SELECT d.description, d.price, d.tva_tx, d.localtax1_tx, d.localtax2_tx, d.qty, d.fk_remise_except, d.remise_percent, d.subprice, d.fk_product,"; - $sql.= " d.info_bits, d.total_ht, d.total_tva, d.total_localtax1, d.total_localtax2, d.total_ttc, d.marge_tx, d.marque_tx, d.special_code, d.rang, d.product_type,"; - $sql.= " p.ref, p.label, p.description as product_desc"; + $sql.= " d.info_bits, d.total_ht, d.total_tva, d.total_localtax1, d.total_localtax2, d.total_ttc, d.marge_tx, d.marque_tx, d.special_code, d.product_type,"; + $sql.= " p.ref, p.label, p.description as product_desc,"; + $sql.= " r.rang"; $sql.= " FROM ".MAIN_DB_PREFIX."propaldet as d"; + $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."element_rang as r ON r.fk_parent = d.fk_propal AND r.parenttype = '".$this->element."'"; $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON d.fk_product = p.rowid"; $sql.= " WHERE d.fk_propal = ".$this->id; - $sql.= " ORDER by d.rang"; + $sql.= " AND r.fk_child = d.rowid"; + $sql.= " AND r.childtype = '".$this->element."'"; + $sql.= " ORDER by r.rang"; $result = $this->db->query($sql); if ($result) @@ -2078,22 +2085,24 @@ class Propal extends CommonObject * \brief Return an array of propal lines * \param option 0=No filter on rang, 1=filter on rang <> 0, 2=filter on rang=0 */ - function getLinesArray($option=0) + function getLinesArray() { $lines = array(); $sql = 'SELECT pt.rowid, pt.description, pt.fk_product, pt.fk_remise_except,'; $sql.= ' pt.qty, pt.tva_tx, pt.remise_percent, pt.subprice, pt.info_bits,'; $sql.= ' pt.total_ht, pt.total_tva, pt.total_ttc, pt.marge_tx, pt.marque_tx, pt.pa_ht, pt.special_code,'; - $sql.= ' pt.date_start, pt.date_end, pt.product_type, pt.rang,'; + $sql.= ' pt.date_start, pt.date_end, pt.product_type,'; $sql.= ' p.label as product_label, p.ref, p.fk_product_type, p.rowid as prodid,'; - $sql.= ' p.description as product_desc'; + $sql.= ' p.description as product_desc,'; + $sql.= " r.rang"; $sql.= ' FROM '.MAIN_DB_PREFIX.'propaldet as pt'; + $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."element_rang as r ON r.fk_parent = pt.fk_propal AND r.parenttype = '".$this->element."'"; $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON pt.fk_product=p.rowid'; $sql.= ' WHERE pt.fk_propal = '.$this->id; - if ($option == 1) $sql.= ' AND pt.rang <> 0'; - if ($option == 2) $sql.= ' AND pt.rang = 0'; - $sql.= ' ORDER BY pt.rang ASC, pt.rowid'; + $sql.= " AND r.fk_child = pt.rowid"; + $sql.= " AND r.childtype = '".$this->element."'"; + $sql.= ' ORDER BY r.rang ASC, pt.rowid'; $resql = $this->db->query($sql); if ($resql) @@ -2149,7 +2158,7 @@ class Propal extends CommonObject * \class PropaleLigne * \brief Class to manage commercial proposal lines */ -class PropaleLigne +class PropaleLigne extends CommonObjectLine { var $db; var $error; @@ -2283,7 +2292,7 @@ class PropaleLigne $sql.= ' (fk_propal, description, fk_product, product_type, fk_remise_except, qty, tva_tx, localtax1_tx, localtax2_tx,'; $sql.= ' subprice, remise_percent, '; $sql.= ' info_bits, '; - $sql.= ' total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, marge_tx, marque_tx, special_code, rang)'; + $sql.= ' total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, marge_tx, marque_tx, special_code)'; $sql.= " VALUES (".$this->fk_propal.","; $sql.= " '".addslashes($this->desc)."',"; $sql.= " ".($this->fk_product?"'".$this->fk_product."'":"null").","; @@ -2306,17 +2315,17 @@ class PropaleLigne if (isset($this->marque_tx)) $sql.= ' '.$this->marque_tx.','; else $sql.= ' null,'; if (isset($this->special_code)) $sql.= ' '.$this->special_code.','; - else $sql.= ' 0,'; - $sql.= ' '.$this->rang; + else $sql.= ' 0'; $sql.= ')'; dol_syslog("PropaleLigne::insert sql=$sql"); $resql=$this->db->query($sql); if ($resql) { - $this->rang=$rangmax; - $this->rowid=$this->db->last_insert_id(MAIN_DB_PREFIX.'propaldet'); + + $this->addRangOfLine($this->fk_propal,'propal',$this->rowid,'propal',$this->rang); + if (! $notrigger) { // Appel des triggers diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index 9cb3b09a2a9..6f3a89fc2fb 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -677,19 +677,9 @@ class CommonObject */ function line_order($renum=false) { - if (! $this->table_element_line) - { - dol_syslog("CommonObject::line_order was called on objet with property table_element_line not defined",LOG_ERR); - return -1; - } - if (! $this->fk_element) - { - dol_syslog("CommonObject::line_order was called on objet with property fk_element not defined",LOG_ERR); - return -1; - } - - $sql = 'SELECT count(rowid) FROM '.MAIN_DB_PREFIX.$this->table_element_line; - $sql.= ' WHERE '.$this->fk_element.'='.$this->id; + $sql = 'SELECT count(rowid) FROM '.MAIN_DB_PREFIX.'element_rang'; + $sql.= ' WHERE fk_parent = '.$this->id; + $sql.= ' AND parenttype = "'.$this->element.'"'; if (! $renum) $sql.= ' AND rang = 0'; if ($renum) $sql.= ' AND rang <> 0'; $resql = $this->db->query($sql); @@ -700,8 +690,9 @@ class CommonObject } if ($nl > 0) { - $sql = 'SELECT rowid FROM '.MAIN_DB_PREFIX.$this->table_element_line; - $sql.= ' WHERE '.$this->fk_element.' = '.$this->id; + $sql = 'SELECT rowid FROM '.MAIN_DB_PREFIX.'element_rang'; + $sql.= ' WHERE fk_parent = '.$this->id; + $sql.= ' AND parenttype = "'.$this->element.'"'; $sql.= ' ORDER BY rang ASC, rowid ASC'; $resql = $this->db->query($sql); if ($resql) @@ -717,7 +708,7 @@ class CommonObject } for ($i = 0 ; $i < sizeof($li) ; $i++) { - $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element_line.' SET rang = '.($i+1); + $sql = 'UPDATE '.MAIN_DB_PREFIX.'element_rang SET rang = '.($i+1); $sql.= ' WHERE rowid = '.$li[$i]; if (!$this->db->query($sql) ) { @@ -759,13 +750,47 @@ class CommonObject // Update position of line $this->updateLineDown($rowid, $rang, $max); } + + /** + * Add position of line (rang) + */ + function addRangOfLine($childid,$childtype,$rang) + { + $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'element_rang ('; + $sql.= 'fk_parent, parenttype, fk_child, childtype, rang'; + $sql.= ') VALUES ('; + $sql.= $this->id.', "'.$this->element.'", '.$childid.', "'.$childtype.'", '.$rang; + $sql.= ')'; + + if (! $this->db->query($sql) ) + { + dol_print_error($this->db); + } + } + + /** + * Delete position of line (rang) + */ + function delRangOfLine($childid,$childtype) + { + $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'element_rang'; + $sql.= ' WHERE fk_parent = '.$this->id; + $sql.= ' AND parenttype = "'.$this->element.'"'; + $sql.= ' AND fk_child = '.$childid; + $sql.= ' AND childtype = "'.$childtype.'"'; + + if (! $this->db->query($sql) ) + { + dol_print_error($this->db); + } + } /** * Update position of line (rang) */ function updateRangOfLine($rowid,$rang) { - $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element_line.' SET rang = '.$rang; + $sql = 'UPDATE '.MAIN_DB_PREFIX.'element_rang SET rang = '.$rang; $sql.= ' WHERE rowid = '.$rowid; if (! $this->db->query($sql) ) { @@ -780,13 +805,16 @@ class CommonObject { if ($rang > 1 ) { - $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element_line.' SET rang = '.$rang ; - $sql.= ' WHERE '.$this->fk_element.' = '.$this->id; + $sql = 'UPDATE '.MAIN_DB_PREFIX.'element_rang SET rang = '.$rang ; + $sql.= ' WHERE fk_parent = '.$this->id; + $sql.= ' AND parenttype = "'.$this->element.'"'; $sql.= ' AND rang = '.($rang - 1); if ($this->db->query($sql) ) { - $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element_line.' SET rang = '.($rang - 1); - $sql.= ' WHERE rowid = '.$rowid; + $sql = 'UPDATE '.MAIN_DB_PREFIX.'element_rang SET rang = '.($rang - 1); + $sql.= ' WHERE fk_parent = '.$this->id; + $sql.= ' AND parenttype = "'.$this->element.'"'; + $sql.= ' AND fk_child = '.$rowid; if (! $this->db->query($sql) ) { dol_print_error($this->db); @@ -806,13 +834,16 @@ class CommonObject { if ($rang < $max) { - $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element_line.' SET rang = '.$rang; - $sql.= ' WHERE '.$this->fk_element.' = '.$this->id; - $sql.= ' AND rang = '.($rang+1); + $sql = 'UPDATE '.MAIN_DB_PREFIX.'element_rang SET rang = '.$rang; + $sql.= ' WHERE fk_parent = '.$this->id; + $sql.= ' AND parenttype = "'.$this->element.'"'; + $sql.= ' AND rang = '.($rang + 1); if ($this->db->query($sql) ) { - $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element_line.' SET rang = '.($rang+1); - $sql.= ' WHERE rowid = '.$rowid; + $sql = 'UPDATE '.MAIN_DB_PREFIX.'element_rang SET rang = '.($rang + 1); + $sql.= ' WHERE fk_parent = '.$this->id; + $sql.= ' AND parenttype = "'.$this->element.'"'; + $sql.= ' AND fk_child = '.$rowid; if (! $this->db->query($sql) ) { dol_print_error($this->db); @@ -831,8 +862,10 @@ class CommonObject */ function getRangOfLine($rowid) { - $sql = 'SELECT rang FROM '.MAIN_DB_PREFIX.$this->table_element_line; - $sql.= ' WHERE rowid ='.$rowid; + $sql = 'SELECT rang FROM '.MAIN_DB_PREFIX.'element_rang'; + $sql.= ' WHERE fk_parent = '.$this->id; + $sql.= ' AND parenttype = "'.$this->element.'"'; + $sql.= ' AND fk_child = '.$rowid; $resql = $this->db->query($sql); if ($resql) { @@ -847,8 +880,9 @@ class CommonObject */ function getIdOfLine($rang) { - $sql = 'SELECT rowid FROM '.MAIN_DB_PREFIX.$this->table_element_line; - $sql.= ' WHERE '.$this->fk_element.' = '.$this->id; + $sql = 'SELECT rowid FROM '.MAIN_DB_PREFIX.'element_rang'; + $sql.= ' WHERE fk_parent = '.$this->id; + $sql.= ' AND parenttype = "'.$this->element.'"'; $sql.= ' AND rang = '.$rang; $resql = $this->db->query($sql); if ($resql) @@ -864,8 +898,9 @@ class CommonObject */ function line_max() { - $sql = 'SELECT max(rang) FROM '.MAIN_DB_PREFIX.$this->table_element_line; - $sql.= ' WHERE '.$this->fk_element.' = '.$this->id; + $sql = 'SELECT max(rang) FROM '.MAIN_DB_PREFIX.'element_rang'; + $sql.= ' WHERE fk_parent = '.$this->id; + $sql.= ' AND parenttype = "'.$this->element.'"'; $resql = $this->db->query($sql); if ($resql) { diff --git a/htdocs/core/class/commonobjectline.class.php b/htdocs/core/class/commonobjectline.class.php index 1ca8b33788b..534cfbb0149 100644 --- a/htdocs/core/class/commonobjectline.class.php +++ b/htdocs/core/class/commonobjectline.class.php @@ -31,6 +31,22 @@ class CommonObjectLine { + /** + * Add position of line (rang) + */ + function addRangOfLine($parentid,$parenttype,$childid,$childtype,$rang) + { + $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'element_rang ('; + $sql.= 'fk_parent, parenttype, fk_child, childtype, rang'; + $sql.= ') VALUES ('; + $sql.= $parentid.', "'.$parenttype.'", '.$childid.', "'.$childtype.'", '.$rang; + $sql.= ')'; + + if (! $this->db->query($sql) ) + { + dol_print_error($this->db); + } + } } diff --git a/htdocs/install/upgrade2.php b/htdocs/install/upgrade2.php index 0f3c3e0b996..4e711f4241c 100644 --- a/htdocs/install/upgrade2.php +++ b/htdocs/install/upgrade2.php @@ -300,6 +300,8 @@ if (GETPOST('action') && preg_match('/upgrade/i',GETPOST("action"))) if (versioncompare($versiontoarray,$afterversionarray) >= 0 && versioncompare($versiontoarray,$beforeversionarray) <= 0) { //print $langs->trans("AlreadyDone"); + + migrate_element_rang($db,$langs,$conf); // Reload menus migrate_reload_menu($db,$langs,$conf,$versionto); @@ -3052,6 +3054,87 @@ function migrate_shipping_delivery2($db,$langs,$conf) print ''; } +/* + * Migration de la gestion des rangs dans llx_element_rang + */ +function migrate_element_rang($db,$langs,$conf) +{ + dolibarr_install_syslog("upgrade2::migrate_element_rang"); + + print ''; + + print '
'; + print ''.$langs->trans('MigrationElementRang')."
\n"; + + $tables = array(); + + // llx_propaldet + $tables[] = array('name'=>'propaldet','element'=>'propal','fk_element'=>'fk_propal'); + + foreach($tables as $table) + { + $result = $db->DDLDescTable(MAIN_DB_PREFIX.$table['name'],"rang"); + $obj = $db->fetch_object($result); + if ($obj) + { + $error = 0; + + $db->begin(); + + $sql = "SELECT rowid, ".$table['fk_element'].", rang FROM ".MAIN_DB_PREFIX.$table['name']; + $resql = $db->query($sql); + if ($resql) + { + $i = 0; + $num = $db->num_rows($resql); + + if ($num) + { + while ($i < $num) + { + $obj = $db->fetch_object($resql); + + $sql = "INSERT INTO ".MAIN_DB_PREFIX."element_rang (fk_parent,parenttype,fk_child,childtype,rang)"; + $sql.= " VALUES (".$obj->fk_propal.",'".$table['element']."',".$obj->rowid.",'".$table['element']."',".$obj->rang.")"; + $resql2=$db->query($sql); + + if (!$resql2) + { + $error++; + dol_print_error($db); + } + print ". "; + $i++; + } + } + + if ($error == 0) + { + $db->commit(); + $sql = "ALTER TABLE ".MAIN_DB_PREFIX.$table['name']." DROP COLUMN rang"; + print "
".$langs->trans('FieldMigrated')."
\n"; + $db->query($sql); + } + else + { + $db->rollback(); + } + } + else + { + dol_print_error($db); + $db->rollback(); + } + } + else + { + print $langs->trans('AlreadyDone')."
\n"; + } + } + + print ''; +} + /** * Migration directory