diff --git a/htdocs/compta/facture.php b/htdocs/compta/facture.php index 34a547ec48f..e8aa2923d37 100644 --- a/htdocs/compta/facture.php +++ b/htdocs/compta/facture.php @@ -148,6 +148,9 @@ if ($_REQUEST['action'] == 'confirm_deleteproductline' && $_REQUEST['confirm'] = $result = $fac->deleteline($_GET['rowid'], $user); if ($result > 0) { + // reorder lines + $fac->line_order(true); + // Define output language $outputlangs = $langs; $newlang=''; @@ -1767,8 +1770,11 @@ if ($_GET['action'] == 'create') $sql.= ' pt.qty, pt.tva_tx, pt.remise_percent, pt.subprice, pt.product_type, pt.info_bits,'; $sql.= ' p.label as product, p.ref, p.fk_product_type, p.rowid as prodid'; $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 = '".$object->element."'"; $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON pt.fk_product = p.rowid'; $sql.= ' WHERE pt.fk_propal = '.$object->id; + $sql.= " AND r.fk_child = pt.rowid"; + $sql.= " AND r.childtype = '".$object->element."'"; $sql.= ' ORDER BY pt.rang ASC, pt.rowid'; } // TODO deplacer dans la classe @@ -1781,8 +1787,11 @@ if ($_GET['action'] == 'create') $sql.= ' pt.date_start as date_debut_prevue, pt.date_end as date_fin_prevue,'; $sql.= ' p.label as product, p.ref, p.fk_product_type, p.rowid as prodid'; $sql.= ' FROM '.MAIN_DB_PREFIX.'commandedet as pt'; + $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."element_rang as r ON r.fk_parent = pt.fk_commande AND r.parenttype = '".$object->element."'"; $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON pt.fk_product = p.rowid'; $sql.= ' WHERE pt.fk_commande = '.$object->id; + $sql.= " AND r.fk_child = pt.rowid"; + $sql.= " AND r.childtype = '".$object->element."'"; $sql.= ' ORDER BY pt.rowid ASC'; } // TODO deplacer dans la classe @@ -2646,9 +2655,12 @@ else $sql.= ' p.ref as product_ref, p.fk_product_type, p.label as product_label,'; $sql.= ' p.description as product_desc'; $sql.= ' FROM '.MAIN_DB_PREFIX.'facturedet as l'; + $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."element_rang as r ON r.fk_parent = l.fk_facture AND r.parenttype = '".$fac->element."'"; $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'product p ON l.fk_product=p.rowid'; $sql.= ' WHERE l.fk_facture = '.$fac->id; - $sql.= ' ORDER BY l.rang ASC, l.rowid'; + $sql.= " AND r.fk_child = l.rowid"; + $sql.= " AND r.childtype = '".$fac->element."'"; + $sql.= ' ORDER BY r.rang ASC, l.rowid'; $resql = $db->query($sql); if ($resql) diff --git a/htdocs/compta/facture/class/facture.class.php b/htdocs/compta/facture/class/facture.class.php index a0e5158dbd4..110e043f2ae 100644 --- a/htdocs/compta/facture/class/facture.class.php +++ b/htdocs/compta/facture/class/facture.class.php @@ -4,7 +4,7 @@ * Copyright (C) 2004 Sebastien Di Cintio * Copyright (C) 2004 Benoit Mortier * Copyright (C) 2005 Marc Barilley / Ocebo - * Copyright (C) 2005-2009 Regis Houssin + * Copyright (C) 2005-2010 Regis Houssin * Copyright (C) 2006 Andre Cianfarani * Copyright (C) 2007 Franky Van Liedekerke * Copyright (C) 2010 Juanjo Menent @@ -32,6 +32,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 ."/societe/class/client.class.php"); @@ -673,11 +674,15 @@ class Facture extends CommonObject $sql.= ' l.localtax1_tx, l.localtax2_tx, l.remise, l.remise_percent, l.fk_remise_except, l.subprice,'; $sql.= ' l.date_start as date_start, l.date_end as date_end,'; $sql.= ' l.info_bits, l.total_ht, l.total_tva, l.total_localtax1, l.total_localtax2, l.total_ttc, l.fk_code_ventilation, l.fk_export_compta,'; - $sql.= ' p.ref as product_ref, p.fk_product_type as fk_product_type, p.label as label, p.description as product_desc'; + $sql.= ' p.ref as product_ref, p.fk_product_type as fk_product_type, p.label as label, p.description as product_desc,'; + $sql.= ' r.rang'; $sql.= ' FROM '.MAIN_DB_PREFIX.'facturedet as l'; + $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."element_rang as r ON r.fk_parent = l.fk_facture AND r.parenttype = '".$this->element."'"; $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON l.fk_product = p.rowid'; $sql.= ' WHERE l.fk_facture = '.$this->id; - $sql.= ' ORDER BY l.rang'; + $sql.= " AND r.fk_child = l.rowid"; + $sql.= " AND r.childtype = '".$this->element."'"; + $sql.= ' ORDER BY r.rang'; dol_syslog('Facture::fetch_lines sql='.$sql, LOG_DEBUG); $result = $this->db->query($sql); @@ -717,6 +722,7 @@ class Facture extends CommonObject $faclig->total_ttc = $objp->total_ttc; $faclig->export_compta = $objp->fk_export_compta; $faclig->code_ventilation = $objp->fk_code_ventilation; + $faclig->rang = $objp->rang; // Ne plus utiliser $faclig->price = $objp->price; @@ -1834,11 +1840,11 @@ class Facture extends CommonObject * \param user User object * \return int <0 if KO, >0 if OK */ - function deleteline($rowid, $user='') + function deleteline($lineid, $user='') { global $langs, $conf; - dol_syslog("Facture::Deleteline rowid=".$rowid, LOG_DEBUG); + dol_syslog("Facture::Deleteline rowid=".$lineid, LOG_DEBUG); if (! $this->brouillon) { @@ -1851,7 +1857,7 @@ class Facture extends CommonObject // Libere remise liee a ligne de facture $sql = 'UPDATE '.MAIN_DB_PREFIX.'societe_remise_except'; $sql.= ' SET fk_facture_line = NULL'; - $sql.= ' WHERE fk_facture_line = '.$rowid; + $sql.= ' WHERE fk_facture_line = '.$lineid; dol_syslog("Facture::Deleteline sql=".$sql); $result = $this->db->query($sql); @@ -1865,7 +1871,7 @@ class Facture extends CommonObject // Efface ligne de facture $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'facturedet'; - $sql.= ' WHERE rowid = '.$rowid; + $sql.= ' WHERE rowid = '.$lineid; dol_syslog("Facture::Deleteline sql=".$sql); $result = $this->db->query($sql); @@ -1878,6 +1884,8 @@ class Facture extends CommonObject } $result=$this->update_price(); + + $this->delRangOfLine($lineid, $this->element); // Appel des triggers include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php"); @@ -2895,7 +2903,7 @@ class Facture extends CommonObject * \brief Classe permettant la gestion des lignes de factures * \remarks Gere des lignes de la table llx_facturedet */ -class FactureLigne +class FactureLigne extends CommonObjectLine { var $db; var $error; @@ -2965,12 +2973,17 @@ class FactureLigne $sql = 'SELECT fd.rowid, fd.fk_facture, fd.fk_product, fd.product_type, fd.description, fd.price, fd.qty, fd.tva_tx,'; $sql.= ' fd.localtax1_tx, fd. localtax2_tx, fd.remise, fd.remise_percent, fd.fk_remise_except, fd.subprice,'; $sql.= ' fd.date_start as date_start, fd.date_end as date_end,'; - $sql.= ' fd.info_bits, fd.total_ht, fd.total_tva, fd.total_ttc, fd.rang,'; + $sql.= ' fd.info_bits, fd.total_ht, fd.total_tva, fd.total_ttc,'; $sql.= ' fd.fk_code_ventilation, fd.fk_export_compta,'; - $sql.= ' p.ref as product_ref, p.label as product_libelle, p.description as product_desc'; + $sql.= ' p.ref as product_ref, p.label as product_libelle, p.description as product_desc,'; + $sql.= ' r.rang'; $sql.= ' FROM '.MAIN_DB_PREFIX.'facturedet as fd'; + $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."element_rang as r ON r.fk_parent = fd.fk_facture AND r.parenttype = '".$this->element."'"; $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON fd.fk_product = p.rowid'; $sql.= ' WHERE fd.rowid = '.$rowid; + $sql.= " AND r.fk_child = fd.rowid"; + $sql.= " AND r.childtype = '".$this->element."'"; + $result = $this->db->query($sql); if ($result) { @@ -3049,7 +3062,6 @@ class FactureLigne $sql.= ' (fk_facture, description, qty, tva_tx, localtax1_tx, localtax2_tx,'; $sql.= ' fk_product, product_type, remise_percent, subprice, price, remise, fk_remise_except,'; $sql.= ' date_start, date_end, fk_code_ventilation, fk_export_compta, '; - $sql.= ' rang,'; $sql.= ' info_bits, total_ht, total_tva, total_localtax1, total_localtax2, total_ttc)'; $sql.= " VALUES (".$this->fk_facture.","; $sql.= " '".addslashes($this->desc)."',"; @@ -3072,7 +3084,6 @@ class FactureLigne else { $sql.='null,'; } $sql.= ' '.$this->fk_code_ventilation.','; $sql.= ' '.$this->fk_export_compta.','; - $sql.= ' '.$this->rang.','; $sql.= " '".$this->info_bits."',"; $sql.= " ".price2num($this->total_ht).","; $sql.= " ".price2num($this->total_tva).","; @@ -3086,6 +3097,8 @@ class FactureLigne if ($resql) { $this->rowid=$this->db->last_insert_id(MAIN_DB_PREFIX.'facturedet'); + + $this->addRangOfLine($this->fk_facture,'facture',$this->rowid,'facture',$this->rang); // Si fk_remise_except defini, on lie la remise a la facture // ce qui la flague comme "consommee". diff --git a/htdocs/install/upgrade2.php b/htdocs/install/upgrade2.php index 4b8e76e41d5..40a7104a5e1 100644 --- a/htdocs/install/upgrade2.php +++ b/htdocs/install/upgrade2.php @@ -3073,7 +3073,7 @@ function migrate_element_rang($db,$langs,$conf) // llx_commandedet $tables[] = array('name'=>'commandedet','element'=>'commande','fk_element'=>'fk_commande'); //llx_facturedet - //$tables[] = array('name'=>'facturedet','element'=>'facture','fk_element'=>'fk_facture'); + $tables[] = array('name'=>'facturedet','element'=>'facture','fk_element'=>'fk_facture'); foreach($tables as $table) {