diff --git a/htdocs/compta/facture.php b/htdocs/compta/facture.php index f3550580985..bc71aa5c02a 100644 --- a/htdocs/compta/facture.php +++ b/htdocs/compta/facture.php @@ -124,7 +124,7 @@ if ($_POST["action"] == 'add') $facture->remise = $_POST["remise"]; $facture->remise_percent = $_POST["remise_percent"]; - if (!$_POST["propalid"] && !$_POST["commandeid"]) + if (! $_POST["propalid"] && ! $_POST["commandeid"] && ! $_POST["contratid"]) { for ($i = 1 ; $i <= $NBLINES ; $i++) { @@ -187,7 +187,7 @@ if ($_POST["action"] == 'add') } /* - * Commande + * Si création depuis commande */ if ($_POST["commandeid"]) { @@ -222,6 +222,43 @@ if ($_POST["action"] == 'add') } } + /* + * Si création depuis contrat + */ + if ($_POST["contratid"]) + { + $facture->contratid = $_POST["contratid"]; + $facid = $facture->create($user); + + if ($facid) + { + $contrat = New Contrat($db); + if ( $contrat->fetch($_POST["contratid"]) ) + { + $lines = $contrat->fetch_lignes(); + for ($i = 0 ; $i < sizeof($lines) ; $i++) + { + $result = $facture->addline($facid, + addslashes($lines[$i]->description), + $lines[$i]->subprice, + $lines[$i]->qty, + $lines[$i]->tva_tx, + $lines[$i]->product_id, + $lines[$i]->remise_percent); + } + } + else + { + print $langs->trans("UnknownError"); + } + } + else + { + dolibarr_print_error($db); + } + } + + // Fin création facture, on l'affiche if ($facid) { Header("Location: facture.php?facid=".$facid); @@ -514,33 +551,39 @@ if ($_GET["action"] == 'create') { print_titre($langs->trans("NewBill")); + $soc = new Societe($db); + if ($_GET["propalid"]) { $propal = New Propal($db); $propal->fetch($_GET["propalid"]); $societe_id = $propal->soc_id; + $soc->fetch($societe_id); } elseif ($_GET["commandeid"]) { $commande = New Commande($db); $commande->fetch($_GET["commandeid"]); $societe_id = $commande->soc_id; + $soc->fetch($societe_id); } elseif ($_GET["contratid"]) { $contrat = New Contrat($db); $contrat->fetch($_GET["contratid"]); - $societe_id = $contrat->soc_id; + $societe_id = $contrat->societe->id; + $soc=$contrat->societe; + } + else + { + $societe_id=$socidp; + $soc->fetch($societe_id); } - else $societe_id=$socidp; - $soc = new Societe($db); - $soc->fetch($societe_id); print '
'; print ''; print '' ."\n"; - print ''; print ''; @@ -576,9 +619,9 @@ if ($_GET["action"] == 'create') print ''; // Conditions de réglement - $id_condition_paiements_defaut=1; + $cond_reglement_id_defaut=1; print ""; // Mode de réglement @@ -601,8 +644,7 @@ if ($_GET["action"] == 'create') if ($_GET["propalid"] > 0) { - $amount = ($propal->price); - print ''."\n"; + print ''."\n"; print ''."\n"; print ''."\n"; print ''."\n"; @@ -611,13 +653,15 @@ if ($_GET["action"] == 'create') print ''; print ''; - print ''; + print ''; print '"; print '"; } elseif ($_GET["commandeid"] > 0) { + $commande->remise_percent=$soc->remise_client; print ''; + print ''."\n"; print ''; print ''; @@ -626,18 +670,22 @@ if ($_GET["action"] == 'create') } elseif ($_GET["contratid"] > 0) { - $amount = ($obj->price); - print ''."\n"; - print ''."\n"; - print ''."\n"; - print ''."\n"; - print ''."\n"; + // Calcul contrat->price (HT), contrat->total (TTC), contrat->tva + $contrat->remise=0; + $contrat->remise_percent=$soc->remise_client; + $contrat->update_price(); + + print ''."\n"; + print ''."\n"; + print ''."\n"; + print ''."\n"; + print ''."\n"; print ''; - print ''; - print ''; - print '"; - print '"; + print ''; + print ''; + print '"; + print '"; } else { @@ -677,7 +725,7 @@ if ($_GET["action"] == 'create') /* * Factures récurrentes */ - if ($_GET["propalid"] == 0 && $_GET["commandeid"] == 0) + if ($_GET["propalid"] == 0 && $_GET["commandeid"] == 0 && $_GET["contratid"] == 0) { $sql = "SELECT r.rowid, r.titre, r.amount FROM ".MAIN_DB_PREFIX."facture_rec as r"; $sql .= " WHERE r.fk_soc = ".$soc->id; @@ -718,7 +766,9 @@ if ($_GET["action"] == 'create') print_titre($langs->trans("ProductsAndServices")); print '
".$langs->trans("PaymentConditions").""; - $html->select_conditions_paiements($id_condition_paiements_defaut,'cond_reglement_id'); + $html->select_conditions_paiements($cond_reglement_id_defaut,'cond_reglement_id'); print "
'.$langs->trans("Proposal").''.$propal->ref.'
'.$langs->trans("GlobalDiscount").''.$propal->remise_percent.'%
'.$langs->trans("TotalHT").''.price($amount).'
'.$langs->trans("TotalHT").''.price($propal->price).'
'.$langs->trans("VAT").''.price($propal->tva)."
'.$langs->trans("TotalTTC").''.price($propal->total)."
'.$langs->trans("Order").''.$commande->ref.'
'.$langs->trans("TotalHT").''.price($commande->total_ht).'
'.$langs->trans("Contract").''.$obj->ref.'
'.$langs->trans("TotalHT").''.price($amount).'
'.$langs->trans("VAT").''.price($obj->tva)."
'.$langs->trans("TotalTTC").''.price($obj->total)."
'.$langs->trans("Contract").''.$contrat->ref.'
'.$langs->trans("TotalHT").''.price($contrat->total_ht).'
'.$langs->trans("VAT").''.price($contrat->total_tva)."
'.$langs->trans("TotalTTC").''.price($contrat->total_ttc)."
'; - print ''; + print ''; + print ''; + print ''; print ''; print ''; print ''; @@ -760,7 +810,7 @@ if ($_GET["action"] == 'create') $sql = "SELECT pt.rowid, pt.description as product, pt.tva_tx, pt.price, pt.qty, pt.remise_percent"; $sql .= " FROM ".MAIN_DB_PREFIX."propaldet as pt "; $sql .= " WHERE pt.fk_propal = ".$_GET["propalid"]; - $sql .= " AND pt.fk_product = 0"; + $sql .= " AND (pt.fk_product = 0 or pt.fk_product is null)"; $sql .= " ORDER BY pt.rowid ASC"; $result=$db->query($sql); if ($result) @@ -772,7 +822,7 @@ if ($_GET["action"] == 'create') $objp = $db->fetch_object($result); $var=!$var; print "\n"; - print ''; + print ''; print ''; print ''; print ''; @@ -792,26 +842,33 @@ if ($_GET["action"] == 'create') // Si creation depuis une commande if ($_GET["commandeid"]) { + print '
'; print_titre($langs->trans("Products")); print '
'.$langs->trans("Ref").''.$langs->trans("Description").'
'.$langs->trans("Ref").''.$langs->trans("Description").''.$langs->trans("VAT").''.$langs->trans("PriceUHT").''.$langs->trans("Qty").'
 '.$objp->product.''.dolibarr_trunc($objp->product,60).''.$objp->tva_tx.'%'.price($objp->price).''.$objp->qty.'
'; - print ''; - print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; - $sql = "SELECT pt.rowid, p.label as product, p.ref, pt.subprice, pt.qty, p.rowid as prodid, pt.remise_percent, pt.description"; - $sql .= " FROM ".MAIN_DB_PREFIX."commandedet as pt, ".MAIN_DB_PREFIX."product as p"; - $sql .= " WHERE pt.fk_product = p.rowid AND pt.fk_commande = ".$commande->id; - $sql .= " ORDER BY pt.rowid ASC"; + $sql = "SELECT pt.rowid, pt.subprice, pt.tva_tx, pt.qty, pt.remise_percent, pt.description,"; + $sql.= " p.label as product, p.ref, p.rowid as prodid"; + $sql.= " FROM ".MAIN_DB_PREFIX."commandedet as pt, ".MAIN_DB_PREFIX."product as p"; + $sql.= " WHERE pt.fk_product = p.rowid AND pt.fk_commande = ".$commande->id; + $sql.= " ORDER BY pt.rowid ASC"; $result = $db->query($sql); if ($result) { - $num = $db->num_rows(); + $num = $db->num_rows($result); $i = 0; $var=True; while ($i < $num) { - $objp = $db->fetch_object(); + $objp = $db->fetch_object($result); $var=!$var; print ''; + print ''; print ''; - print ''; - print ''; + print ''; + print ''; + print ''; $i++; } } @@ -830,10 +889,10 @@ if ($_GET["action"] == 'create') dolibarr_print_error($db); } // Lignes de commande non produits prédéfinis - $sql = "SELECT pt.rowid, pt.description as product, pt.subprice, pt.qty, pt.remise_percent"; + $sql = "SELECT pt.rowid, pt.description as product, pt.tva_tx, pt.subprice, pt.qty, pt.remise_percent"; $sql .= " FROM ".MAIN_DB_PREFIX."commandedet as pt"; $sql .= " WHERE pt.fk_commande = ".$commande->id; - $sql .= " AND pt.fk_product = 0"; + $sql .= " AND (pt.fk_product = 0 or pt.fk_product is null)"; $sql .= " ORDER BY pt.rowid ASC"; $result=$db->query($sql); @@ -846,10 +905,11 @@ if ($_GET["action"] == 'create') $objp = $db->fetch_object($result); $var=!$var; print "\n"; - print ''; + print ''; + print ''; print ''; - print ''; - print ''; + print ''; + print ''; print ''; $i++; } @@ -861,7 +921,90 @@ if ($_GET["action"] == 'create') print '
'.$langs->trans("Ref").''.$langs->trans("Product").''.$langs->trans("Price").''.$langs->trans("Discount").''.$langs->trans("Qty").'
'.$langs->trans("Ref").''.$langs->trans("Description").''.$langs->trans("VAT").''.$langs->trans("PriceUHT").''.$langs->trans("Qty").''.$langs->trans("Discount").'
'.img_object($langs->trans(""),"product")." ".$objp->ref.""; print $objp->product?' - '.$objp->product:''; @@ -819,9 +876,11 @@ if ($_GET["action"] == 'create') print ''; print dolibarr_trunc($objp->description,60); print ''.$objp->tva_tx.'%'.price($objp->subprice).''.$objp->remise_percent.'%'.$objp->qty.'
'.$objp->qty.''.$objp->remise_percent.'%
 '.$objp->product.''.dolibarr_trunc($objp->product,60).''.$objp->tva_tx.'%'.price($objp->subprice).''.$objp->remise_percent.'%'.$objp->qty.''.$objp->qty.''.$objp->remise_percent.'%
'; } - + + // Si creation depuis un contrat + if ($_GET["contratid"]) + { + print '
'; + print_titre($langs->trans("Services")); + + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + + $sql = "SELECT pt.rowid, pt.subprice, pt.tva_tx, pt.qty, pt.remise_percent, pt.description,"; + $sql.= " p.label as product, p.ref, p.rowid as prodid"; + $sql.= " FROM ".MAIN_DB_PREFIX."contratdet as pt, ".MAIN_DB_PREFIX."product as p"; + $sql.= " WHERE pt.fk_product = p.rowid AND pt.fk_contrat = ".$contrat->id; + $sql.= " ORDER BY pt.rowid ASC"; + + $result = $db->query($sql); + if ($result) + { + $num = $db->num_rows($result); + $i = 0; + $var=True; + while ($i < $num) + { + $objp = $db->fetch_object($result); + $var=!$var; + print '\n"; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + $i++; + } + } + else + { + dolibarr_print_error($db); + } + // Lignes de contrat non produits prédéfinis + $sql = "SELECT pt.rowid, pt.description as product, pt.tva_tx, pt.subprice, pt.qty, pt.remise_percent"; + $sql .= " FROM ".MAIN_DB_PREFIX."contratdet as pt"; + $sql .= " WHERE pt.fk_contrat = ".$contrat->id; + $sql .= " AND (pt.fk_product = 0 or pt.fk_product is null)"; + $sql .= " ORDER BY pt.rowid ASC"; + + $result=$db->query($sql); + if ($result) + { + $num = $db->num_rows($result); + $i = 0; + while ($i < $num) + { + $objp = $db->fetch_object($result); + $var=!$var; + print "\n"; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + $i++; + } + } + else + { + dolibarr_print_error($db); + } + + print '
'.$langs->trans("Ref").''.$langs->trans("Description").''.$langs->trans("VAT").''.$langs->trans("PriceUHT").''.$langs->trans("Qty").''.$langs->trans("Discount").'
'.img_object($langs->trans(""),"service")." ".$objp->ref.""; + print $objp->product?' - '.$objp->product:''; + print "'; + print dolibarr_trunc($objp->description,60); + print ''.$objp->tva_tx.'%'.price($objp->subprice).''.$objp->qty.''.$objp->remise_percent.'%
 '.dolibarr_trunc($objp->product,60).''.$objp->tva_tx.'%'.price($objp->subprice).''.$objp->qty.''.$objp->remise_percent.'%
'; + } + } else { diff --git a/htdocs/contrat/contrat.class.php b/htdocs/contrat/contrat.class.php index 86d62b1a65f..4bc8d9e7321 100644 --- a/htdocs/contrat/contrat.class.php +++ b/htdocs/contrat/contrat.class.php @@ -216,8 +216,8 @@ class Contrat } /** - * \brief Charge de la base les données du contrat - * \param id id du contrat à charger + * \brief Chargement depuis la base des données du contrat + * \param id Id du contrat à charger * \return int <0 si KO, >0 si OK */ function fetch($id) @@ -257,17 +257,113 @@ class Contrat $this->societe->fetch($result["fk_soc"]); $this->db->free($resql); - + return 1; } else { + dolibarr_syslog("Contrat::Fetch Erreur lecture contrat"); $this->error=$this->db->error(); return -1; } } + /** + * \brief Reinitialise le tableau lignes + */ + function fetch_lignes() + { + $this->lignes = array(); + + /* + * Lignes contrats liées à un produit + */ + $sql = "SELECT d.description, p.rowid, p.label, p.description as product_desc, p.ref,"; + $sql.= " d.price_ht, d.tva_tx, d.qty, d.remise_percent, d.subprice"; + $sql.= " FROM ".MAIN_DB_PREFIX."contratdet as d, ".MAIN_DB_PREFIX."product as p"; + $sql.= " WHERE d.fk_contrat = ".$this->id ." AND d.fk_product = p.rowid"; + $sql.= " ORDER by d.rowid ASC"; + + $result = $this->db->query($sql); + if ($result) + { + $num = $this->db->num_rows($result); + $i = 0; + + while ($i < $num) + { + $objp = $this->db->fetch_object($result); + + $ligne = new ContratLigne(); + $ligne->desc = stripslashes($objp->description); // Description ligne + $ligne->libelle = stripslashes($objp->label); // Label produit + $ligne->product_desc = stripslashes($objp->product_desc); // Description produit + $ligne->qty = $objp->qty; + $ligne->ref = $objp->ref; + $ligne->tva_tx = $objp->tva_tx; + $ligne->subprice = $objp->subprice; + $ligne->remise_percent = $objp->remise_percent; + $ligne->price = $objp->price; + $ligne->product_id = $objp->rowid; + + $this->lignes[$i] = $ligne; + //dolibarr_syslog("1 ".$ligne->desc); + //dolibarr_syslog("2 ".$ligne->product_desc); + $i++; + } + $this->db->free($result); + } + else + { + dolibarr_syslog("Contrat::Fetch Erreur lecture des lignes de contrats liées aux produits"); + return -3; + } + + /* + * Lignes contrat liées à aucun produit + */ + $sql = "SELECT d.qty, d.description, d.price_ht, d.subprice, d.tva_tx, d.rowid, d.remise_percent"; + $sql .= " FROM ".MAIN_DB_PREFIX."contratdet as d"; + $sql .= " WHERE d.fk_contrat = ".$this->id ." AND d.fk_product = 0"; + + $result = $this->db->query($sql); + if ($result) + { + $num = $this->db->num_rows($result); + $j = 0; + + while ($j < $num) + { + $objp = $this->db->fetch_object($result); + $ligne = new PropaleLigne(); + $ligne->libelle = stripslashes($objp->description); + $ligne->desc = stripslashes($objp->description); + $ligne->qty = $objp->qty; + $ligne->ref = $objp->ref; + $ligne->tva_tx = $objp->tva_tx; + $ligne->subprice = $objp->subprice; + $ligne->remise_percent = $objp->remise_percent; + $ligne->price = $objp->price; + $ligne->product_id = 0; + + $this->lignes[$i] = $ligne; + $i++; + $j++; + } + + $this->db->free($result); + } + else + { + dolibarr_syslog("Contrat::Fetch Erreur lecture des lignes de contrat non liées aux produits"); + $this->error=$this->db->error(); + return -2; + } + + return $this->lignes; + } + /** * \brief Crée un contrat vierge en base * \param user Utilisateur qui crée @@ -372,54 +468,60 @@ class Contrat { global $langs; - $qty = ereg_replace(",",".",$qty); - $pu = ereg_replace(",",".",$pu); - - dolibarr_syslog("Contrat::AddLine $desc, $pu, $qty, $txtva, $fk_product=0, $remise_percent=0, $datestart, $dateend"); + dolibarr_syslog("contrat.class.php::addline $desc, $pu, $qty, $txtva, $fk_product, $remise_percent, $datestart, $dateend"); - if ($fk_product > 0) + if ($this->statut == 0) { - $prod = new Product($this->db, $fk_product); - if ($prod->fetch($fk_product) > 0) + $qty = ereg_replace(",",".",$qty); + $pu = ereg_replace(",",".",$pu); + + if ($fk_product > 0) { - $label = $prod->libelle; - $pu = $prod->price; - $txtva = $prod->tva_tx; + $prod = new Product($this->db, $fk_product); + if ($prod->fetch($fk_product) > 0) + { + $label = $prod->libelle; + $pu = $prod->price; + $txtva = $prod->tva_tx; + } + } + + $remise = 0; + $price = ereg_replace(",",".",round($pu, 2)); + $subprice = $price; + if (strlen($remise_percent) > 0) + { + $remise = round(($pu * $remise_percent / 100), 2); + $price = $pu - $remise; + } + + // Insertion dans la base + $sql = "INSERT INTO ".MAIN_DB_PREFIX."contratdet"; + $sql.= " (fk_contrat, label, description, fk_product, price_ht, qty, tva_tx,"; + $sql.= " remise_percent, subprice, remise"; + if ($datestart > 0) { $sql.= ",date_ouverture_prevue"; } + if ($dateend > 0) { $sql.= ",date_fin_validite"; } + $sql.= ") VALUES ($this->id, '" . addslashes($label) . "','" . addslashes($desc) . "',"; + $sql.= ($fk_product>0 ? $fk_product : "null"); + $sql.= ",".ereg_replace(",",".",$price).", '$qty', $txtva, $remise_percent,'".ereg_replace(",",".",$subprice)."','".ereg_replace(",",".", $remise)."'"; + if ($datestart > 0) { $sql.= ",".$this->db->idate($datestart); } + if ($dateend > 0) { $sql.= ",".$this->db->idate($dateend); } + $sql.= ");"; + + if ( $this->db->query($sql) ) + { + $this->update_price(); + return 1; + } + else + { + $this->error=$this->db->error(); + return -1; } - } - - $remise = 0; - $price = ereg_replace(",",".",round($pu, 2)); - $subprice = $price; - if (strlen($remise_percent) > 0) - { - $remise = round(($pu * $remise_percent / 100), 2); - $price = $pu - $remise; - } - - // Insertion dans la base - $sql = "INSERT INTO ".MAIN_DB_PREFIX."contratdet"; - $sql.= " (fk_contrat, label, description, fk_product, price_ht, qty, tva_tx,"; - $sql.= " remise_percent, subprice, remise"; - if ($datestart > 0) { $sql.= ",date_ouverture_prevue"; } - if ($dateend > 0) { $sql.= ",date_fin_validite"; } - $sql.= ") VALUES ($this->id, '" . addslashes($label) . "','" . addslashes($desc) . "',"; - $sql.= ($fk_product>0 ? $fk_product : "null"); - $sql.= ",".ereg_replace(",",".",$price).", '$qty', $txtva, $remise_percent,'".ereg_replace(",",".",$subprice)."','".ereg_replace(",",".", $remise)."'"; - if ($datestart > 0) { $sql.= ",".$this->db->idate($datestart); } - if ($dateend > 0) { $sql.= ",".$this->db->idate($dateend); } - $sql.= ");"; - - // Retour - if ( $this->db->query($sql) ) - { - //$this->update_price(); - return 0; } else { - dolibarr_print_error($this->db); - return -1; + return -2; } } @@ -437,80 +539,161 @@ class Contrat */ function updateline($rowid, $desc, $pu, $qty, $remise_percent=0, $datestart='', $dateend='', $tvatx) { - // Nettoyage parametres - $qty=trim($qty); - $desc=trim($desc); - $desc=trim($desc); - $price = ereg_replace(",",".",$pu); - $tvatx = ereg_replace(",",".",$tvatx); - $subprice = $price; - $remise = 0; - if (strlen($remise_percent) > 0) + if ($this->statut == 0) { - $remise = round(($pu * $remise_percent / 100), 2); - $price = $pu - $remise; - } - else - { - $remise_percent=0; - } - - dolibarr_syslog("Contrat::UpdateLine $rowid, $desc, $pu, $qty, $remise_percent, $datestart, $dateend, $tvatx"); + // Nettoyage parametres + $qty=trim($qty); + $desc=trim($desc); + $desc=trim($desc); + $price = ereg_replace(",",".",$pu); + $tvatx = ereg_replace(",",".",$tvatx); + $subprice = $price; + $remise = 0; + if (strlen($remise_percent) > 0) + { + $remise = round(($pu * $remise_percent / 100), 2); + $price = $pu - $remise; + } + else + { + $remise_percent=0; + } - $this->db->begin(); - - $sql = "UPDATE ".MAIN_DB_PREFIX."contratdet set description='".addslashes($desc)."'"; - $sql .= ",price_ht='" . ereg_replace(",",".",$price)."'"; - $sql .= ",subprice='" . ereg_replace(",",".",$subprice)."'"; - $sql .= ",remise='" . ereg_replace(",",".",$remise)."'"; - $sql .= ",remise_percent='".ereg_replace(",",".",$remise_percent)."'"; - $sql .= ",qty='$qty'"; - $sql .= ",tva_tx='". ereg_replace(",",".",$tvatx)."'"; - - if ($datestart > 0) { $sql.= ",date_ouverture_prevue=".$this->db->idate($datestart); } - else { $sql.=",date_ouverture_prevue=null"; } - if ($dateend > 0) { $sql.= ",date_fin_validite=".$this->db->idate($dateend); } - else { $sql.=",date_fin_validite=null"; } - - $sql .= " WHERE rowid = $rowid ;"; - - $result = $this->db->query($sql); - if ($result) - { - $this->db->commit(); - - return $result; + dolibarr_syslog("Contrat::UpdateLine $rowid, $desc, $pu, $qty, $remise_percent, $datestart, $dateend, $tvatx"); + + $this->db->begin(); + + $sql = "UPDATE ".MAIN_DB_PREFIX."contratdet set description='".addslashes($desc)."'"; + $sql .= ",price_ht='" . ereg_replace(",",".",$price)."'"; + $sql .= ",subprice='" . ereg_replace(",",".",$subprice)."'"; + $sql .= ",remise='" . ereg_replace(",",".",$remise)."'"; + $sql .= ",remise_percent='".ereg_replace(",",".",$remise_percent)."'"; + $sql .= ",qty='$qty'"; + $sql .= ",tva_tx='". ereg_replace(",",".",$tvatx)."'"; + + if ($datestart > 0) { $sql.= ",date_ouverture_prevue=".$this->db->idate($datestart); } + else { $sql.=",date_ouverture_prevue=null"; } + if ($dateend > 0) { $sql.= ",date_fin_validite=".$this->db->idate($dateend); } + else { $sql.=",date_fin_validite=null"; } + + $sql .= " WHERE rowid = $rowid ;"; + + $result = $this->db->query($sql); + if ($result) + { + $this->update_price(); + + $this->db->commit(); + + return 1; + } + else + { + $this->db->rollback(); + $this->error=$this->db->error(); + dolibarr_syslog("Contrat::UpdateLigne Erreur -1"); + + return -1; + } } else { - $this->db->rollback(); + dolibarr_syslog("Contrat::UpdateLigne Erreur -2 Contrat en mode incompatible pour cette action"); + return -2; + } + } + + /** + * \brief Supprime une ligne de detail + * \param idligne Id de la ligne detail à supprimer + * \return int >0 si ok, <0 si ko + */ + function delete_line($idligne) + { + if ($this->statut == 0) + { + $sql = "DELETE FROM ".MAIN_DB_PREFIX."contratdet WHERE rowid =".$idligne; + + if ($this->db->query($sql) ) + { + $this->update_price(); + + return 1; + } + else + { + return -1; + } + } + else + { + return -2; + } + } - dolibarr_print_error($this->db); + + /** + * \brief Mets à jour le prix total du contrat + */ + + function update_price() + { + include_once DOL_DOCUMENT_ROOT . "/lib/price.lib.php"; + + /* + * Liste des produits a ajouter + */ + $sql = "SELECT price_ht, qty, tva_tx"; + $sql.= " FROM ".MAIN_DB_PREFIX."contratdet"; + $sql.= " WHERE fk_contrat = ".$this->id; + $resql=$this->db->query($sql); + if ($resql) + { + $num = $this->db->num_rows($resql); + $i = 0; + + while ($i < $num) + { + $obj = $this->db->fetch_object($resql); + $products[$i][0] = $obj->price_ht; + $products[$i][1] = $obj->qty; + $products[$i][2] = $obj->tva_tx; + $i++; + } + } + else + { + $this->error=$this->db->error(); return -1; } + $calculs = calcul_price($products, $this->remise_percent); + + $this->remise = $calculs[3]; + $this->total_ht = $calculs[0]; + $this->total_tva = $calculs[1]; + $this->total_ttc = $calculs[2]; + + /* + // Met a jour en base + $sql = "UPDATE ".MAIN_DB_PREFIX."propal SET"; + $sql .= " price='". ereg_replace(",",".",$this->total_ht)."'"; + $sql .= ", tva='". ereg_replace(",",".",$this->total_tva)."'"; + $sql .= ", total='". ereg_replace(",",".",$this->total_ttc)."'"; + $sql .= ", remise='".ereg_replace(",",".",$this->remise)."'"; + $sql .=" WHERE rowid = $this->id"; + + if ( $this->db->query($sql) ) + { + return 1; + } + else + { + $this->error=$this->db->error(); + return -1; + } + */ } - /** - * \brief Supprime une ligne de detail du contrat - * \param idligne id de la ligne detail de contrat à supprimer - */ - function delete_line($idligne) - { - - $sql = "DELETE FROM ".MAIN_DB_PREFIX."contratdet WHERE rowid =".$idligne; - - if ($this->db->query($sql) ) - { - //$this->update_price(); - - return 0; - } - else - { - return 1; - } - } - /** * \brief Classe le contrat dans un projet @@ -879,4 +1062,19 @@ class Contrat } } + + +/** + \class ContratLigne + \brief Classe permettant la gestion des lignes de contrats +*/ + +class ContratLigne +{ + function ContratLigne() + { + } +} + + ?> diff --git a/htdocs/contrat/fiche.php b/htdocs/contrat/fiche.php index d256ccd09b8..1502f9f5ceb 100644 --- a/htdocs/contrat/fiche.php +++ b/htdocs/contrat/fiche.php @@ -149,7 +149,7 @@ if ($_POST["action"] == 'addligne' && $user->rights->contrat->creer) } } -if ($_POST["action"] == 'updateligne' && $user->rights->contrat->creer) +if ($_POST["action"] == 'updateligne' && $user->rights->contrat->creer && $_POST["save"]) { $contrat = new Contrat($db,"",$_GET["id"]); if ($contrat->fetch($_GET["id"])) @@ -179,6 +179,11 @@ if ($_POST["action"] == 'updateligne' && $user->rights->contrat->creer) } } +if ($_POST["action"] == 'updateligne' && $user->rights->contrat->creer && $_POST["cancel"]) +{ + Header("Location: fiche.php?id=".$_GET["id"]); +} + if ($_GET["action"] == 'deleteline' && $user->rights->contrat->creer) { $contrat = new Contrat($db); @@ -595,10 +600,12 @@ else */ echo '
'; - $sql = "SELECT cd.statut, cd.label, cd.fk_product, cd.description, cd.price_ht, cd.qty, cd.rowid, cd.tva_tx, cd.remise_percent, cd.subprice,"; + $sql = "SELECT cd.statut, cd.label as label_det, cd.fk_product, cd.description, cd.price_ht, cd.qty, cd.rowid, cd.tva_tx, cd.remise_percent, cd.subprice,"; $sql.= " ".$db->pdate("cd.date_ouverture_prevue")." as date_debut, ".$db->pdate("cd.date_ouverture")." as date_debut_reelle,"; - $sql.= " ".$db->pdate("cd.date_fin_validite")." as date_fin, ".$db->pdate("cd.date_cloture")." as date_fin_reelle"; + $sql.= " ".$db->pdate("cd.date_fin_validite")." as date_fin, ".$db->pdate("cd.date_cloture")." as date_fin_reelle,"; + $sql.= " p.ref, p.label"; $sql.= " FROM ".MAIN_DB_PREFIX."contratdet as cd"; + $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON cd.fk_product = p.rowid"; $sql.= " WHERE cd.fk_contrat = ".$id; $sql.= " ORDER BY cd .rowid"; @@ -635,7 +642,8 @@ else { print ''; } @@ -740,10 +748,16 @@ else // Ligne carac print ""; print ''; print ''; print ''; print ''; - print ''; + print ''; // Ligne dates prévues print ""; - print '"; print ''; print ''; @@ -819,6 +837,7 @@ else $var=!$var; + // Service libre print ''; print ''; print '';
'; print ''; - print img_object($langs->trans("ShowService"),"service").' '.$objp->label.''; + print img_object($langs->trans("ShowService"),"service").' '.$objp->ref.''; + print $objp->label?' - '.$objp->label:''; if ($objp->description) print '
'.stripslashes(nl2br($objp->description)); print '
'; - print ''; - if ($objp->label) + if ($objp->fk_product) { - print img_object($langs->trans("ShowService"),"service").' '.$objp->label.'
'; + print ''; + print img_object($langs->trans("ShowService"),"service").' '.$objp->ref.''; + print $objp->label?' - '.$objp->label:''; + print '
'; + } + else + { + print $objp->label?$objp->label.'
':''; } print '
'; @@ -752,10 +766,12 @@ else print '%'; + print '
'; + print '
'; + print ''; print $langs->trans("DateStartPlanned").' '; $html->select_date($objp->date_debut,"date_start_update",0,0,($objp->date_debut>0?0:1)); print '   '.$langs->trans("DateEndPlanned").' '; @@ -792,6 +808,7 @@ else $var=false; + // Service sur produit prédéfini print ''; print ''; print ''; @@ -799,7 +816,8 @@ else print "
'; - $html->select_produits('','p_idprod','',0); + $html->select_produits('','p_idprod','',0).'
'; + print ''; print '