diff --git a/htdocs/comm/remx.php b/htdocs/comm/remx.php index 68282d15a7c..d7d8e28bb71 100644 --- a/htdocs/comm/remx.php +++ b/htdocs/comm/remx.php @@ -124,10 +124,7 @@ if ($_socid > 0) dolibarr_fiche_head($head, 'absolutediscount', $objsoc->nom); - /* - * - * - */ + print '
'; print ''; @@ -138,7 +135,7 @@ if ($_socid > 0) $sql = "SELECT SUM(rc.amount_ht) as amount, rc.fk_user"; $sql.= " FROM ".MAIN_DB_PREFIX."societe_remise_except as rc"; $sql.= " WHERE rc.fk_soc =". $objsoc->id; - $sql.= " AND fk_facture IS NULL"; + $sql.= " AND (fk_facture_line IS NULL AND fk_facture IS NULL)"; $sql.= " GROUP BY rc.fk_user"; $resql=$db->query($sql); if ($resql) @@ -181,7 +178,7 @@ if ($_socid > 0) /* - * Liste remises fixes restant en cours + * Liste remises fixes restant en cours (= liees a acune facture ni ligne de facture) */ $sql = "SELECT rc.rowid, rc.amount_ht, rc.amount_tva, rc.amount_ttc, rc.tva_tx,"; $sql.= $db->pdate("rc.datec")." as dc, rc.description,"; @@ -191,7 +188,8 @@ if ($_socid > 0) $sql.= " FROM ".MAIN_DB_PREFIX."user as u, ".MAIN_DB_PREFIX."societe_remise_except as rc"; $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."facture as fa ON rc.fk_facture_source = fa.rowid"; $sql.= " WHERE rc.fk_soc =". $objsoc->id; - $sql.= " AND u.rowid = rc.fk_user AND rc.fk_facture IS NULL"; + $sql.= " AND u.rowid = rc.fk_user"; + $sql.= " AND (rc.fk_facture_line IS NULL AND rc.fk_facture IS NULL)"; $sql.= " ORDER BY rc.datec DESC"; $resql=$db->query($sql); @@ -252,10 +250,11 @@ if ($_socid > 0) print '
'; /* - * Liste ristournes appliquées + * Liste ristournes appliquées (=liees a une ligne de facture ou facture) */ - $sql = "SELECT rc.rowid, rc.amount_ht, rc.amount_tva, rc.amount_ttc, rc.tva_tx,"; - $sql.= $db->pdate("rc.datec")." as dc, rc.description, rc.fk_facture,"; + // Remises liees a lignes de factures + $sql = "(SELECT rc.rowid, rc.amount_ht, rc.amount_tva, rc.amount_ttc, rc.tva_tx,"; + $sql.= $db->pdate("rc.datec")." as dc, rc.description, rc.fk_facture_line, rc.fk_facture,"; $sql.= " rc.fk_facture_source,"; $sql.= " u.login, u.rowid as user_id,"; $sql.= " f.rowid, f.facnumber,"; @@ -266,10 +265,26 @@ if ($_socid > 0) $sql.= " , ".MAIN_DB_PREFIX."societe_remise_except as rc"; $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."facture as fa ON rc.fk_facture_source = fa.rowid"; $sql.= " WHERE rc.fk_soc =". $objsoc->id; - $sql.= " AND rc.fk_facture = fc.rowid"; + $sql.= " AND rc.fk_facture_line = fc.rowid"; $sql.= " AND fc.fk_facture = f.rowid"; - $sql.= " AND rc.fk_user = u.rowid"; - $sql.= " ORDER BY rc.datec DESC"; + $sql.= " AND rc.fk_user = u.rowid)"; + $sql.= " UNION "; + // Remises liees a factures + $sql.= "(SELECT rc.rowid, rc.amount_ht, rc.amount_tva, rc.amount_ttc, rc.tva_tx,"; + $sql.= $db->pdate("rc.datec")." as dc, rc.description, rc.fk_facture_line, rc.fk_facture,"; + $sql.= " rc.fk_facture_source,"; + $sql.= " u.login, u.rowid as user_id,"; + $sql.= " f.rowid, f.facnumber,"; + $sql.= " fa.facnumber as ref, fa.type as type"; + $sql.= " FROM ".MAIN_DB_PREFIX."facture as f"; + $sql.= " , ".MAIN_DB_PREFIX."user as u"; + $sql.= " , ".MAIN_DB_PREFIX."societe_remise_except as rc"; + $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."facture as fa ON rc.fk_facture_source = fa.rowid"; + $sql.= " WHERE rc.fk_soc =". $objsoc->id; + $sql.= " AND rc.fk_facture = f.rowid"; + $sql.= " AND rc.fk_user = u.rowid)"; + + $sql.= " ORDER BY dc DESC"; $resql=$db->query($sql); if ($resql) diff --git a/htdocs/compta/facture.php b/htdocs/compta/facture.php index 872c21debe4..604f95c3fcf 100644 --- a/htdocs/compta/facture.php +++ b/htdocs/compta/facture.php @@ -105,27 +105,29 @@ if ($_POST['action'] == 'confirm_delete' && $_POST['confirm'] == 'yes') if (($_POST['action'] == 'confirm_deleteproductline' && $_POST['confirm'] == 'yes' && $conf->global->PRODUIT_CONFIRM_DELETE_LINE) || ($_GET['action'] == 'deleteline' && !$conf->global->PRODUIT_CONFIRM_DELETE_LINE)) { - if ($user->rights->facture->creer) - { - $fac = new Facture($db); - $fac->fetch($_GET['facid']); - $result = $fac->deleteline($_GET['rowid'], $user); - if ($result > 0) - { - if ($_REQUEST['lang_id']) - { - $outputlangs = new Translate(DOL_DOCUMENT_ROOT ."/langs",$conf); - $outputlangs->setDefaultLang($_REQUEST['lang_id']); - } - facture_pdf_create($db, $fac->id, '', $fac->modelpdf, $outputlangs); - } - else - { - print $fac->error; - } - } - Header('Location: '.$_SERVER["PHP_SELF"].'?facid='.$_GET['facid']); - exit; + if ($user->rights->facture->creer) + { + $fac = new Facture($db); + $fac->fetch($_GET['facid']); + $result = $fac->deleteline($_GET['rowid'], $user); + if ($result > 0) + { + if ($_REQUEST['lang_id']) + { + $outputlangs = new Translate(DOL_DOCUMENT_ROOT ."/langs",$conf); + $outputlangs->setDefaultLang($_REQUEST['lang_id']); + } + facture_pdf_create($db, $fac->id, '', $fac->modelpdf, $outputlangs); + + Header('Location: '.$_SERVER["PHP_SELF"].'?facid='.$_GET['facid']); + exit; + } + else + { + $mesg='
'.$fac->error.'
'; + $_GET['action']=''; + } + } } // Validation diff --git a/htdocs/facture.class.php b/htdocs/facture.class.php index e391f2bda33..4eadedb0998 100644 --- a/htdocs/facture.class.php +++ b/htdocs/facture.class.php @@ -450,7 +450,6 @@ class Facture extends CommonObject $this->ref = $obj->facnumber; $this->ref_client = $obj->ref_client; $this->type = $obj->type; - $this->datep = $obj->dp; $this->date = $obj->df; $this->amount = $obj->amount; $this->remise_percent = $obj->remise_percent; @@ -1543,25 +1542,31 @@ class Facture extends CommonObject } } - /** - * \brief Supprime une ligne facture de la base - * \param rowid Id de la ligne de facture a supprimer - */ - function deleteline($rowid, $user='') - { - global $langs, $conf; + /** + * \brief Supprime une ligne facture de la base + * \param rowid Id de la ligne de facture a supprimer + * \return int <0 if KO, >0 if OK + */ + function deleteline($rowid, $user='') + { + global $langs, $conf; - dolibarr_syslog("Facture::Deleteline rowid=".$rowid, LOG_DEBUG); + dolibarr_syslog("Facture::Deleteline rowid=".$rowid, LOG_DEBUG); - if ($this->brouillon) - { - $this->db->begin(); - + if (! $this->brouillon) + { + $this->error='ErrorBadStatus'; + return -1; + } + + $this->db->begin(); + // Libere remise liee a ligne de facture $sql = 'UPDATE '.MAIN_DB_PREFIX.'societe_remise_except'; - $sql.= ' SET fk_facture = NULL where fk_facture = '.$rowid; + $sql.= ' SET fk_facture_line = NULL where fk_facture_line = '.$rowid; + dolibarr_syslog("Facture::Deleteline sql=".$sql); $result = $this->db->query($sql); - if ($result < 0) + if (! $result) { $this->error=$this->db->error(); dolibarr_syslog("Facture::Deleteline Error ".$this->error); @@ -1571,8 +1576,9 @@ class Facture extends CommonObject // Efface ligne de facture $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'facturedet WHERE rowid = '.$rowid; + dolibarr_syslog("Facture::Deleteline sql=".$sql); $result = $this->db->query($sql); - if ($result < 0) + if (! $result) { $this->error=$this->db->error(); dolibarr_syslog("Facture::Deleteline Error ".$this->error); @@ -1591,8 +1597,7 @@ class Facture extends CommonObject $this->db->commit(); return 1; - } - } + } /** \brief Mise à jour des sommes de la facture et calculs denormalises diff --git a/htdocs/html.form.class.php b/htdocs/html.form.class.php index 7c23444845b..55663945b9a 100644 --- a/htdocs/html.form.class.php +++ b/htdocs/html.form.class.php @@ -2319,7 +2319,7 @@ class Form if (! $filter || $filter=='fk_facture_source IS NULL') print $langs->trans("CompanyHasAbsoluteDiscount",price($amount),$langs->transnoentities("Currency".$conf->monnaie)).': '; else print $langs->trans("CompanyHasCreditNote",price($amount),$langs->transnoentities("Currency".$conf->monnaie)).': '; // print $langs->trans("AvailableGlobalDiscounts").': '; - $newfilter='fk_facture IS NULL'; // Remises disponibles + $newfilter='fk_facture IS NULL AND fk_facture_line IS NULL'; // Remises disponibles if ($filter) $newfilter.=' AND '.$filter; print $this->select_remises('',$htmlname,$newfilter,$socid); print ''; diff --git a/htdocs/install/upgrade2.php b/htdocs/install/upgrade2.php index 5c61e31e27b..47c11f9b822 100644 --- a/htdocs/install/upgrade2.php +++ b/htdocs/install/upgrade2.php @@ -948,11 +948,12 @@ function migrate_price_facture($db,$langs,$conf) print ''.$langs->trans('MigrationInvoice')."
\n"; // Liste des lignes facture non a jour - $sql = "SELECT fd.rowid, fd.qty, fd.subprice, fd.remise_percent, fd.tva_taux, "; - $sql.= " f.rowid as facid, f.remise_percent as remise_percent_global"; + $sql = "SELECT fd.rowid, fd.qty, fd.subprice, fd.remise_percent, fd.tva_taux, fd.total_ttc,"; + $sql.= " f.rowid as facid, f.remise_percent as remise_percent_global, f.total_ttc as total_ttc_f"; $sql.= " FROM ".MAIN_DB_PREFIX."facturedet as fd, ".MAIN_DB_PREFIX."facture as f"; $sql.= " WHERE fd.fk_facture = f.rowid"; - $sql.= " AND ((fd.total_ttc = 0 AND fd.remise_percent != 100) or fd.total_ttc IS NULL)"; + $sql.= " AND (((fd.total_ttc = 0 AND fd.remise_percent != 100) or fd.total_ttc IS NULL) or f.total_ttc IS NULL)"; + //print $sql; $resql=$db->query($sql); if ($resql) { @@ -970,6 +971,7 @@ function migrate_price_facture($db,$langs,$conf) $txtva = $obj->tva_taux; $remise_percent = $obj->remise_percent; $remise_percent_global = $obj->remise_percent_global; + $total_ttc_f = $obj->total_ttc_f; // On met a jour les 3 nouveaux champs $facligne= new FactureLigne($db); @@ -989,28 +991,31 @@ function migrate_price_facture($db,$langs,$conf) $facligne->update_total(); - /* On touche pas a facture mere - $facture = new Facture($db); - $facture->id=$obj->facid; - - if ( $facture->fetch($facture->id) >= 0) + /* On touche a facture mere uniquement si total_ttc = 0 */ + if (! $total_ttc_f) { - if ( $facture->update_price($facture->id) > 0 ) + $facture = new Facture($db); + $facture->id=$obj->facid; + + if ( $facture->fetch($facture->id) >= 0) { - print ". "; + if ( $facture->update_price($facture->id) > 0 ) + { + print "X "; + } + else + { + print "Error id=".$facture->id; + $err++; + } } else { - print "Error id=".$facture->id; + print "Error #3"; $err++; } } - else - { - print "Error #3"; - $err++; - } - */ + $i++; } } diff --git a/htdocs/societe.class.php b/htdocs/societe.class.php index 1323211c5f8..c55260028ac 100644 --- a/htdocs/societe.class.php +++ b/htdocs/societe.class.php @@ -1036,7 +1036,7 @@ class Societe $sql.= " FROM ".MAIN_DB_PREFIX."societe_remise_except as rc"; $sql.= " WHERE rc.fk_soc =". $this->id; if (is_object($user)) $sql.= " AND rc.fk_user = ".$user->id; - $sql.= " AND rc.fk_facture IS NULL"; + $sql.= " AND (rc.fk_facture IS NULL AND rc.fk_facture_line IS NULL)"; if ($filter) $sql.=' AND '.$filter; dolibarr_syslog("Societe::getCurrentDiscount sql=".$sql,LOG_DEBUG); diff --git a/mysql/migration/2.1.0-2.2.0.sql b/mysql/migration/2.1.0-2.2.0.sql index 16fc951e830..b22be2ff904 100644 --- a/mysql/migration/2.1.0-2.2.0.sql +++ b/mysql/migration/2.1.0-2.2.0.sql @@ -1124,6 +1124,10 @@ insert into llx_c_pays (rowid,code,libelle) values (246, 'MF', 'Saint-Martin' ) ALTER TABLE llx_boxes ADD UNIQUE INDEX uk_boxes (box_id, position, fk_user); +-- Nettoyage vieux enregistrement detail pourris +delete from llx_facturedet where price = 0 and subprice = 0 and remise_percent = 0 and total_ttc = 0 and total_ht = 0; + + -- Drop constraints to allow rename ALTER TABLE llx_societe_remise_except drop foreign key fk_societe_remise_fk_facture; ALTER TABLE llx_societe_remise_except drop index idx_societe_remise_except_fk_facture; @@ -1141,5 +1145,6 @@ ALTER TABLE llx_societe_remise_except ADD CONSTRAINT fk_societe_remise_fk_factur -- Corrige statut avoir transforme en reduc ou reduc supprime apres coup -- V4.1 update llx_facture set paye=0, fk_statut=1 where paye=1 and type=2 and rowid not in (select fk_facture_source from llx_societe_remise_except); --- Corrige avoirs affectes en ligne a affectation sur facture --- V4.1 update llx_societe_remise_except as r set fk_facture_line = NULL, fk_facture = (select fk_facture from llx_facturedet where rowid = r.fk_facture_line) +-- Corrige avoirs affectes en ligne a affectation sur facture. On met total a null pour permettre recalcul par upgrade2 +-- V4.1 update llx_facture set total_ttc = NULL where rowid in (select fk_facture from llx_facturedet where description = '(CREDIT_NOTE)'); +-- V4.1 delete from llx_facturedet where description = '(CREDIT_NOTE)';