diff --git a/htdocs/compta/facture.php b/htdocs/compta/facture.php index 1d3d9b433fb..3d725f5b173 100644 --- a/htdocs/compta/facture.php +++ b/htdocs/compta/facture.php @@ -44,10 +44,14 @@ if ($user->societe_id > 0) $action = ''; $socidp = $user->societe_id; } + +// Nombre de ligne pour choix de produit/service prédéfinis +$NBLINES=4; + + /* * */ - if ($_POST["action"] == 'classin') { $facture = new Facture($db); @@ -85,11 +89,21 @@ if ($_POST["action"] == 'add') if (!$_POST["propalid"] && !$_POST["commandeid"]) { - $facture->add_product($_POST["idprod1"],$_POST["qty1"],$_POST["remise_percent1"]); - $facture->add_product($_POST["idprod2"],$_POST["qty2"],$_POST["remise_percent2"]); - $facture->add_product($_POST["idprod3"],$_POST["qty3"],$_POST["remise_percent3"]); - $facture->add_product($_POST["idprod4"],$_POST["qty4"],$_POST["remise_percent4"]); - + for ($i = 1 ; $i <= $NBLINES ; $i++) + { + if ($_POST["idprod${i}"]) { + $startday=''; + $endday=''; + if ($_POST["date_start${i}year"] && $_POST["date_start${i}month"] && $_POST["date_start${i}day"]) { + $startday=$_POST["date_start${i}year"].'-'.$_POST["date_start${i}month"].'-'.$_POST["date_start${i}day"]; + } + if ($_POST["date_end${i}year"] && $_POST["date_end${i}month"] && $_POST["date_end${i}day"]) { + $endday=$_POST["date_end${i}year"].'-'.$_POST["date_end${i}month"].'-'.$_POST["date_end${i}day"]; + } + $facture->add_product($_POST["idprod${i}"],$_POST["qty${i}"],$_POST["remise_percent${i}"],$startday,$endday); + + } + } $facid = $facture->create($user); if ($facid) @@ -217,24 +231,47 @@ if ($action == 'addligne' && $user->rights->facture->creer) { $fac = new Facture($db); $fac->fetch($facid); + $datestart=''; + $dateend=''; + if ($_POST["date_startyear"] && $_POST["date_startmonth"] && $_POST["date_startday"]) { + $datestart=$_POST["date_startyear"].'-'.$_POST["date_startmonth"].'-'.$_POST["date_startday"]; + } + if ($_POST["date_endyear"] && $_POST["date_endmonth"] && $_POST["date_endday"]) { + $dateend=$_POST["date_endyear"].'-'.$_POST["date_endmonth"].'-'.$_POST["date_endday"]; + } $result = $fac->addline($facid, - $_POST["desc"], - $_POST["pu"], - $_POST["qty"], - $_POST["tva_tx"], - 0, - $_POST["remise_percent"]); + $_POST["desc"], + $_POST["pu"], + $_POST["qty"], + $_POST["tva_tx"], + 0, + $_POST["remise_percent"], + $datestart, + $dateend + ); } if ($action == 'updateligne' && $user->rights->facture->creer) { $fac = new Facture($db,"",$facid); $fac->fetch($facid); + $datestart=''; + $dateend=''; + if ($_POST["date_startyear"] && $_POST["date_startmonth"] && $_POST["date_startday"]) { + $datestart=$_POST["date_startyear"].'-'.$_POST["date_startmonth"].'-'.$_POST["date_startday"]; + } + if ($_POST["date_endyear"] && $_POST["date_endmonth"] && $_POST["date_endday"]) { + $dateend=$_POST["date_endyear"].'-'.$_POST["date_endmonth"].'-'.$_POST["date_endday"]; + } + $result = $fac->updateline($rowid, $_POST["desc"], $_POST["price"], $_POST["qty"], - $_POST["remise_percent"]); + $_POST["remise_percent"], + $datestart, + $dateend + ); } if ($action == 'deleteline' && $user->rights->facture->creer) @@ -496,12 +533,26 @@ if ($_GET["action"] == 'create') } print ''; - print ''; - for ($i = 1 ; $i < 5 ; $i++) + print ''; + if ($conf->service->enabled) { + print ''; + } + for ($i = 1 ; $i <= $NBLINES ; $i++) { print ''; print ''; - print ''; + print ''; + print ''; + // Si le module service est actif, on propose des dates de début et fin à la ligne + if ($conf->service->enabled) { + print ''; + } + print "\n"; } print '
Services/Produits prédéfinisQuan.Remise
Services/Produits prédéfinisQuan.Remise     Si produit de type service à durée limitée
%
% '; + print 'Du '; + print $html->select_date('',"date_start$i",0,0,1); + print ' au '; + print $html->select_date('',"date_end$i",0,0,1); + print '
'; @@ -542,7 +593,7 @@ if ($_GET["action"] == 'create') /* * */ - print ''; + print ''; print "\n"; print "\n"; @@ -810,10 +861,9 @@ else * Lignes de factures * */ - - $sql = "SELECT l.fk_product, l.description, l.price, l.qty, l.rowid, l.tva_taux, l.remise_percent, l.subprice"; + $sql = "SELECT l.fk_product, l.description, l.price, l.qty, l.rowid, l.tva_taux, l.remise_percent, l.subprice, ".$db->pdate("l.date_start")." as date_start, ".$db->pdate("l.date_end")." as date_end "; $sql .= " FROM ".MAIN_DB_PREFIX."facturedet as l WHERE l.fk_facture = $fac->id ORDER BY l.rowid"; - + $result = $db->query($sql); if ($result) { @@ -841,12 +891,16 @@ else print ""; if ($objp->fk_product > 0) { - print ''.stripslashes(nl2br($objp->description)).''; + print ''.stripslashes(nl2br($objp->description)).''; + if ($objp->date_start) { print " (Du ".dolibarr_print_date($objp->date_start)." au ".dolibarr_print_date($objp->date_end).")"; } + print ''; } else { - print "".stripslashes(nl2br($objp->description))."\n"; - } + print "".stripslashes(nl2br($objp->description)); + if ($objp->date_start) { print " (Du ".dolibarr_print_date($objp->date_start)." au ".dolibarr_print_date($objp->date_end).")"; } + print "\n"; + } print ''.$objp->tva_taux.' %'; print ''.price($objp->subprice)."\n"; @@ -859,7 +913,7 @@ else { print ' '; } - print ''.price($objp->subprice*$objp->qty*(100-$objp->remise_percent)/100)."\n"; + print ''.price($objp->subprice*$objp->qty*(100-$objp->remise_percent)/100)."\n"; // Icone d'edition et suppression if ($fac->statut == 0 && $user->rights->facture->creer) @@ -877,6 +931,7 @@ else } print ""; + // Update ligne de facture if ($action == 'editline' && $rowid == $objp->rowid) { print "
id\" method=\"post\">"; @@ -884,12 +939,24 @@ else print ''; print ""; print ''; - print ' '; + print ''; + //print $html->select_tva("tva_tx",$objp->tva_taux); + print "$objp->tva_taux %"; // Taux tva dépend du produit, donc on ne doit pas pouvoir le changer ici + print ''; print ''; - print ''; + print ''; print ' %'; print ''; print '' . "\n"; + if ($conf->service->enabled) { + print ""; + print 'Si produit de type service à durée limitée: Du '; + print $html->select_date($objp->date_start,"date_start",0,0,$objp->date_start?0:1); + print ' au '; + print $html->select_date($objp->date_end,"date_end",0,0,$objp->date_end?0:1); + print ''; + print '' . "\n"; + } print "
\n"; } @@ -902,7 +969,7 @@ else } else { - print $db->error(); + print "Erreur : ".$db->error()."
".$sql; } /* @@ -933,10 +1000,19 @@ else print ''; print ' %'; print ''; - + if ($conf->service->enabled) { + print ''; + print 'Si produit de type service à durée limitée: Du '; + print $html->select_date('',"date_start",0,0,1); + print ' au '; + print $html->select_date('',"date_end",0,0,1); + print ''; + } + print ''; print ""; } - print ""; + print "\n"; + /* * Fin Ajout ligne * diff --git a/htdocs/facture.class.php b/htdocs/facture.class.php index 6d99db6288f..9b186e91fde 100644 --- a/htdocs/facture.class.php +++ b/htdocs/facture.class.php @@ -55,7 +55,7 @@ class Facture { $this->db = $DB ; $this->socidp = $soc_idp; - $this->products = array(); + $this->products = array(); // Tableau de lignes de factures $this->db_table = MAIN_DB_PREFIX."facture"; $this->amount = 0; $this->remise = 0; @@ -146,7 +146,7 @@ class Facture } /* - * Produits + * Produits/services * */ for ($i = 0 ; $i < sizeof($this->products) ; $i++) @@ -160,7 +160,10 @@ class Facture $this->products_qty[$i], $prod->tva_tx, $this->products[$i], - $this->products_remise_percent[$i]); + $this->products_remise_percent[$i], + $this->products_date_start[$i], + $this->products_date_end[$i] + ); if ( $result_insert < 0) @@ -212,13 +215,12 @@ class Facture } /** - * Recupére l'objet facture - * + * Recupére l'objet facture et ses lignes de factures * */ Function fetch($rowid, $societe_id=0) { - $sql = "SELECT f.fk_soc,f.facnumber,f.amount,f.tva,f.total,f.total_ttc,f.remise,f.remise_percent,".$this->db->pdate("f.datef")."as df,f.fk_projet,".$this->db->pdate("f.date_lim_reglement")." as dlr, c.rowid as cond_regl_id, c.libelle, c.libelle_facture, f.note, f.paye, f.fk_statut, f.fk_user_author"; + $sql = "SELECT f.fk_soc,f.facnumber,f.amount,f.tva,f.total,f.total_ttc,f.remise,f.remise_percent,".$this->db->pdate("f.datef")." as df,f.fk_projet,".$this->db->pdate("f.date_lim_reglement")." as dlr, c.rowid as cond_regl_id, c.libelle, c.libelle_facture, f.note, f.paye, f.fk_statut, f.fk_user_author"; $sql .= " FROM ".MAIN_DB_PREFIX."facture as f, ".MAIN_DB_PREFIX."cond_reglement as c"; $sql .= " WHERE f.rowid=$rowid AND c.rowid = f.fk_cond_reglement"; @@ -266,7 +268,7 @@ class Facture * Lignes */ - $sql = "SELECT l.fk_product, l.description, l.price, l.qty, l.rowid, l.tva_taux, l.remise, l.remise_percent, l.subprice"; + $sql = "SELECT l.fk_product, l.description, l.price, l.qty, l.rowid, l.tva_taux, l.remise, l.remise_percent, l.subprice, ".$this->db->pdate("l.date_start")." as date_start,".$this->db->pdate("l.date_end")." as date_end"; $sql .= " FROM ".MAIN_DB_PREFIX."facturedet as l WHERE l.fk_facture = ".$this->id; $result = $this->db->query($sql); @@ -287,6 +289,8 @@ class Facture $faclig->remise = $objp->remise; $faclig->remise_percent = $objp->remise_percent; $faclig->produit_id = $objp->fk_product; + $faclig->date_start = $objp->date_start; + $faclig->date_end = $objp->date_end; $this->lignes[$i] = $faclig; $i++; } @@ -518,29 +522,33 @@ class Facture return $result; } } + /** - * Ajoute un produit dans la facture + * Ajoute un produit dans l'objet facture * */ - Function add_product($idproduct, $qty, $remise_percent) + Function add_product($idproduct, $qty, $remise_percent, $datestart='', $dateend='') { if ($idproduct > 0) { - $i = sizeof($this->products); - $this->products[$i] = $idproduct; + $i = sizeof($this->products); // On recupere nb de produit deja dans tableau products + $this->products[$i] = $idproduct; // On ajoute a la suite if (!$qty) { $qty = 1 ; } $this->products_qty[$i] = $qty; $this->products_remise_percent[$i] = $remise_percent; + if ($datestart) { $this->products_date_start[$i] = $datestart; } + if ($dateend) { $this->products_date_end[$i] = $dateend; } } } + /** - * Ajoute une ligne de facture + * Ajoute une ligne de facture (associé à aucun produit/servcie prédéfini) * */ - Function addline($facid, $desc, $pu, $qty, $txtva, $fk_product=0, $remise_percent=0) + Function addline($facid, $desc, $pu, $qty, $txtva, $fk_product=0, $remise_percent=0, $datestart='', $dateend='') { if ($this->brouillon) { @@ -557,9 +565,14 @@ class Facture $price = $pu - $remise; } - $sql = "INSERT INTO ".MAIN_DB_PREFIX."facturedet (fk_facture,description,price,qty,tva_taux, fk_product, remise_percent, subprice, remise)"; - $sql .= " VALUES ($facid, '".addslashes($desc)."', $price, $qty, $txtva, $fk_product, $remise_percent, $subprice, $remise) ;"; - + $sql = "INSERT INTO ".MAIN_DB_PREFIX."facturedet (fk_facture,description,price,qty,tva_taux, fk_product, remise_percent, subprice, remise, date_start, date_end)"; + $sql .= " VALUES ($facid, '".addslashes($desc)."', $price, $qty, $txtva, $fk_product, $remise_percent, $subprice, $remise, "; + if ($datestart) { $sql.= "'$datestart', "; } + else { $sql.=" null, "; } + if ($dateend) { $sql.= "'$dateend' "; } + else { $sql.=" null "; } + $sql.=")"; + if ( $this->db->query( $sql) ) { $this->updateprice($facid); @@ -574,9 +587,9 @@ class Facture } /** * Mets à jour une ligne de facture - * + * Retourne 0 si erreur */ - Function updateline($rowid, $desc, $pu, $qty, $remise_percent=0) + Function updateline($rowid, $desc, $pu, $qty, $remise_percent=0, $datestart='', $dateend='') { if ($this->brouillon) { @@ -597,10 +610,21 @@ class Facture $remise_percent=0; } - $sql = "UPDATE ".MAIN_DB_PREFIX."facturedet set description='$desc',price=$price,subprice=$subprice,remise=$remise,remise_percent=$remise_percent,qty=$qty WHERE rowid = $rowid ;"; + $sql = "UPDATE ".MAIN_DB_PREFIX."facturedet set description='$desc',price=$price,subprice=$subprice,remise=$remise,remise_percent=$remise_percent,qty=$qty"; + if ($datestart) { $sql.= ",date_start='$datestart'"; } + else { $sql.=",date_start=null"; } + if ($dateend) { $sql.= ",date_end='$dateend'"; } + else { $sql.=",date_end=null"; } + $sql .= " WHERE rowid = $rowid ;"; $result = $this->db->query( $sql); + if ($result) { + $this->updateprice($this->id); + } + else { + print "Erreur : ".$this->db->error()."
".$sql; + } + return $result; - $this->updateprice($this->id); } } /** diff --git a/htdocs/includes/modules/facture/pdf_crabe.modules.php b/htdocs/includes/modules/facture/pdf_crabe.modules.php index 09f8d1b4560..86bce37b1ea 100644 --- a/htdocs/includes/modules/facture/pdf_crabe.modules.php +++ b/htdocs/includes/modules/facture/pdf_crabe.modules.php @@ -84,8 +84,12 @@ Class pdf_crabe { $codeproduitservice=""; $pdf->SetXY (11, $curY ); if (defined("FACTURE_CODEPRODUITSERVICE") && FACTURE_CODEPRODUITSERVICE) { - // Affiche code produit - $codeproduitservice=" (".$fac->lignes[$i]->produit_id.")"; + // Affiche code produit si ligne associée à un code produit + $codeproduitservice=" (Code produit ".$fac->lignes[$i]->produit_id.")"; + } + if ($fac->lignes[$i]->date_start && $fac->lignes[$i]->date_end) { + // Affichage durée si il y en a une + $codeproduitservice=" (Du ".dolibarr_print_date($fac->lignes[$i]->date_start)." au ".dolibarr_print_date($fac->lignes[$i]->date_end).")"; } $pdf->MultiCell(118, 5, $fac->lignes[$i]->desc."$codeproduitservice", 0, 'J'); @@ -104,9 +108,9 @@ Class pdf_crabe { $pdf->MultiCell(10, 5, $fac->lignes[$i]->qty, 0, 'R'); // Remise sur ligne - $pdf->SetXY (161, $curY); + $pdf->SetXY (163, $curY); if ($fac->lignes[$i]->remise_percent) { - $pdf->MultiCell(10, 5, $fac->lignes[$i]->remise_percent."%", 0, 'C'); + $pdf->MultiCell(10, 5, $fac->lignes[$i]->remise_percent."%", 0, 'R'); } // Total HT