* Copyright (C) 2004-2005 Laurent Destailleur * Copyright (C) 2004 Christophe Combelles * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * $Id$ * $Source$ * */ /** \file htdocs/fourn/facture/fiche.php \ingroup facture \brief Page des la fiche facture fournisseur \version $Revision$ */ require("./pre.inc.php"); require("./paiementfourn.class.php"); $langs->load("bills"); $langs->load("companies"); /* * Sécurité accés client */ if ($user->societe_id > 0) { $action = ''; $socidp = $user->societe_id; } $html = new Form($db); $mesg=''; $action=isset($_GET["action"])?$_GET["action"]:$_POST["action"]; if ($_POST["action"] == 'confirm_valid' && $_POST["confirm"] == yes && $user->rights->fournisseur->facture->valider) { $facturefourn=new FactureFournisseur($db); $facturefourn->fetch($_GET["facid"]); $facturefourn->set_valid($user->id); Header("Location: fiche.php?facid=".$_GET["facid"]); exit; } if ($_GET["action"] == 'payed') { $facturefourn=new FactureFournisseur($db); $facturefourn->fetch($_GET["facid"]); $facturefourn->set_payed($user->id); } if($_GET["action"] == 'deletepaiement') { $facfou = new FactureFournisseur($db); $facfou->fetch($_GET["facid"]); if ($facfou->statut == 1 && $facfou->paye == 0 && $user->societe_id == 0) { $paiementfourn = new PaiementFourn($db); $paiementfourn->delete($_GET["paiement_id"]); } } if ($_POST["action"] == 'modif_libelle') { $sql = "UPDATE ".MAIN_DB_PREFIX."facture_fourn set libelle = '$form_libelle' WHERE rowid = ".$_GET["facid"]." ;"; $result = $db->query( $sql); } if ($_POST["action"] == 'update') { $datefacture = $db->idate(mktime(12, 0 , 0, $_POST["remonth"], $_POST["reday"], $_POST["reyear"])); $sql = "UPDATE ".MAIN_DB_PREFIX."facture_fourn set "; $sql .= " facnumber='".trim($_POST["facnumber"])."'"; $sql .= ", libelle='".trim($_POST["libelle"])."'"; $sql .= ", note='".$_POST["note"]."'"; $sql .= ", datef = '$datefacture'"; $sql .= " WHERE rowid = ".$_GET['facid']." ;"; $result = $db->query( $sql); } /* * Action création */ if ($_POST["action"] == 'add' && $user->rights->fournisseur->facture->creer) { if ($_POST["facnumber"]) { $datefacture = mktime(12,0,0, $_POST["remonth"], $_POST["reday"], $_POST["reyear"]); $tva = 0; $tva = ($_POST["tva_taux"] * $_POST["amount"]) / 100 ; $remise = 0; $total = $tva + $_POST["amount"] ; $db->begin(); // Creation facture $facfou = new FactureFournisseur($db); $facfou->number = $_POST["facnumber"]; $facfou->socid = $_POST["socidp"]; $facfou->libelle = $_POST["libelle"]; $facfou->date = $datefacture; $facfou->note = $_POST["note"]; $facid = $facfou->create($user); // Ajout des lignes de factures if ($facid > 0) { for ($i = 1 ; $i < 9 ; $i++) { $label = "label$i"; $amount = "amount$i"; $amountttc = "amountttc$i"; $tauxtva = "tauxtva$i"; $qty = "qty$i"; if (strlen($_POST[$label]) > 0 && !empty($_POST[$amount])) { $atleastoneline=1; $facfou->addline($_POST["$label"], $_POST["$amount"], $_POST["$tauxtva"], $_POST["$qty"], 1); } else if (strlen($_POST[$label]) > 0 && empty($_POST[$amount])) { $ht = $_POST[$amountttc] / (1 + ($_POST[$tauxtva] / 100)); $atleastoneline=1; $facfou->addline($_POST[$label], $ht, $_POST[$tauxtva], $_POST[$qty], 1); } } $db->commit(); header("Location: fiche.php?facid=$facid"); exit; } else { $db->rollback(); $mesg='
'.$facfou->error.'
'; $_GET["action"]='create'; } } else { $mesg='
'.$langs->trans("ErrorFieldRequired",$langs->trans("Ref")).'
'; $_GET["action"]='create'; } } if ($_GET["action"] == 'del_ligne') { $facfou = new FactureFournisseur($db,"",$_GET["facid"]); $facfou->deleteline($_GET["ligne_id"]); $_GET["action"] = "edit"; } if ($_GET["action"] == 'add_ligne') { $facfou = new FactureFournisseur($db,"", $_GET["facid"]); if (strlen($_POST["label"]) > 0 && $_POST["amount"] > 0) $facfou->addline($_POST["label"], $_POST["amount"], $_POST["tauxtva"], $_POST["qty"]); else { $ht = $_POST['amountttc'] / (1 + ($_POST['tauxtva'] / 100)); $facfou->addline($_POST["label"], $ht, $_POST["tauxtva"], $_POST["qty"]); } $_GET["action"] = "edit"; } /* * */ /* * * Fiche facture en mode creation * */ if ($_GET["action"] == 'create' or $_GET["action"] == 'copy') { llxHeader(); print_titre($langs->trans("NewBill")); if ($mesg) { print "$mesg
"; } if ($_GET["action"] == 'copy') { $fac_ori = new FactureFournisseur($db); $fac_ori->fetch($_GET["facid"]); } print '
'; print ''; print ''; print ''; print ''; print ''; print ''; print ''; if ($_GET["action"] == 'copy') { print ''; } else { print ''; } print ''; print ''; print "
'.$langs->trans("Company").''.$langs->trans("Comments").'
'.$langs->trans("Ref").'
'.$langs->trans("Label").'
'.$langs->trans("Label").'
'.$langs->trans("Date").''; $html->select_date(); print '
'.$langs->trans("Author").''.$user->fullname.'

"; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; for ($i = 1 ; $i < 9 ; $i++) { if ($_GET["action"] == 'copy') { $value_label = $fac_ori->lignes[$i-1][0]; $value_pu = $fac_ori->lignes[$i-1][1]; $value_qty = $fac_ori->lignes[$i-1][3]; } else { $value_qty = "1"; } print ''; print ''; print ''; print ''; print ''; print ''; } print "
 '.$langs->trans("Label").''.$langs->trans("PriceUHT").''.$langs->trans("Qty").''.$langs->trans("VATRate").''.$langs->trans("PriceUTTC").'
'.$i.''; $html->select_tva("tauxtva".$i); print '
"; print '

'; print "
"; } else { /* * Fiche facture en mode visu ou edition * */ if ($_GET["facid"] > 0) { $fac = new FactureFournisseur($db); $fac->fetch($_GET["facid"]); $societe = new Fournisseur($db); if ( $societe->fetch($fac->socidp) ) { $addons[0][0] = DOL_URL_ROOT.'/fourn/fiche.php?socid='.$fac->socidp; $addons[0][1] = $societe->nom; } llxHeader('','', $addons); if ($mesg) { print "
$mesg
"; } if ($_GET["action"] == "edit") { print_titre($langs->trans("Bill").': '.$fac->ref); print '
'; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print '"; $authorfullname=" "; if ($fac->author) { $author = new User($db, $fac->author); $author->fetch(''); $authorfullname=$author->fullname; } print ""; print '"; print ""; print "
'.$langs->trans("Company").''.stripslashes($fac->socnom).''.$langs->trans("Comments").'
'.$langs->trans("Ref").''; print ''; print '
'.$langs->trans("Label").''; print '
'.$langs->trans("AmountHT").''.price($fac->total_ht).'
'.$langs->trans("AmountTTC").''.price($fac->total_ttc).'
'.$langs->trans("Date").''; $html->select_date($fac->datep); print "
".$langs->trans("Author")."$authorfullname
'.$langs->trans("Status").''.$fac->LibStatut($fac->paye,$fac->statut)."
trans("Save")."\">
"; print "
"; /* * Lignes * */ print '
'; print "
id&action=add_ligne\" method=\"post\">"; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; for ($i = 0 ; $i < sizeof($fac->lignes) ; $i++) { print "".'"; print '"; print '"; print '"; print '"; print '"; print '"; print '"; print ''; print ''; } /* Nouvelle ligne */ print ""; print ''; print ''; print ''; print ''; print ''; print ''; print "
'.$langs->trans("Label").''.$langs->trans("PriceUHT").''.$langs->trans("PriceUTTC").''.$langs->trans("Qty").''.$langs->trans("TotalHT").''.$langs->trans("VATRate").''.$langs->trans("VAT").''.$langs->trans("TotalTTC").' 
'.$fac->lignes[$i][0]."'.price($fac->lignes[$i][1])."'.price($fac->lignes[$i][1] * (1+($fac->lignes[$i][2]/100)))."'.$fac->lignes[$i][3]."'.price($fac->lignes[$i][4])."'.$fac->lignes[$i][2]."'.price($fac->lignes[$i][5])."'.price($fac->lignes[$i][6])."'; print ''.img_delete().'
'; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print '-'; $html->select_tva("tauxtva"); print ''; print ' '; print '
"; print "
"; } else { /* * */ $h=0; $head[$h][0] = "fiche.php?facid=".$fac->id; $head[$h][1] = $langs->trans("Bill").': '.$fac->ref; $hselected = $h; $h++; dolibarr_fiche_head($head, $hselected); /* * Confirmation de la validation * */ if ($_GET["action"] == 'valid') { $html->form_confirm("fiche.php?facid=$fac->id",$langs->trans("ValidateBill"),"Etes-vous sûr de vouloir valider cette facture ?","confirm_valid"); print '
'; } print ""; print '"; print "
'; /* * Facture */ print ''; print ""; print "\n"; print ""; print '\n"; print ''; print ''; $authorfullname=" "; if ($fac->author) { $author = new User($db, $fac->author); $author->fetch(''); $authorfullname=$author->fullname; } print ""; print '"; print '"; print '"; print '"; if (strlen($fac->note)) { print ''; } print "
".$langs->trans("Company")."socidp\">$fac->socnomsocidp\">".$langs->trans("OtherBills")."
'.$langs->trans("Date").""; print dolibarr_print_date($fac->datep,"%A %e %B %Y")."
'.$langs->trans("Label").''; print $fac->libelle; print '
".$langs->trans("Author")."$authorfullname
'.$langs->trans("Status").''.$fac->LibStatut($fac->paye,$fac->statut)."
'.$langs->trans("TotalHT").''.price($fac->total_ht)."'.$langs->trans("VAT").''.price($fac->total_tva)."
'.$langs->trans("TotalTTC").''.price($fac->total_ttc)."
'.$langs->trans("Comments").''; print nl2br(stripslashes($fac->note)); print '
"; print "
"; /* * Paiements */ print ''; print '"; print "
'; $sql = "SELECT ".$db->pdate("datep")." as dp, p.amount, c.libelle as paiement_type, p.num_paiement, p.rowid"; $sql .= " FROM ".MAIN_DB_PREFIX."paiementfourn as p, ".MAIN_DB_PREFIX."c_paiement as c "; $sql .= " WHERE p.fk_facture_fourn = ".$fac->id." AND p.fk_paiement = c.id"; $result = $db->query($sql); if ($result) { $num = $db->num_rows($result); $i = 0; $total = 0; print ''; print ''; print ""; print ''; print ''; if ($fac->statut == 1 && $fac->paye == 0 && $user->societe_id == 0) { $tdsup=' colspan="2"'; } print " "; print "\n"; $var=True; while ($i < $num) { $objp = $db->fetch_object($result); $var=!$var; print ""; print "\n"; print "\n"; print "\n"; if ($fac->statut == 1 && $fac->paye == 0 && $user->societe_id == 0) { print ''; } print ""; $total = $total + $objp->amount; $i++; } print "".$conf->monnaie."\n"; if ($fac->statut > 0) { $resteapayer = abs($fac->total_ttc - $total); print ""; print '".$conf->monnaie.""; print "\n"; } print "
'.$langs->trans("Payments").' :
'.$langs->trans("Date").''.$langs->trans("Type").'".$langs->trans("AmountTTC")."
".img_object($langs->trans("Payment"),"payment").' '.dolibarr_print_date($objp->dp)."$objp->paiement_type $objp->num_paiement".price($objp->amount)."".$conf->monnaie."'; print ''; print img_delete(); print '
".$langs->trans("Total")." :".price($total)."
".$langs->trans("RemainderToPay")." :'.price($resteapayer)."
"; $db->free(); } else { dolibarr_print_error($db); } print "
"; print "
"; /* * Lignes * */ print '

'; print ''; print ''; print ''; print ''; print ''; print ''; print ''; $var=1; for ($i = 0 ; $i < sizeof($fac->lignes) ; $i++) { $var=!$var; print "".'"; print '"; print '"; print '"; print '"; print '"; print '"; print ''; } print "
'.$langs->trans("Label").''.$langs->trans("PriceUHT").''.$langs->trans("Qty").''.$langs->trans("TotalHT").''.$langs->trans("VATRate").''.$langs->trans("VAT").''.$langs->trans("TotalTTC").'
'.$fac->lignes[$i][0]."'.price($fac->lignes[$i][1])."'.$fac->lignes[$i][3]."'.price($fac->lignes[$i][4])."'.$fac->lignes[$i][2]." %'.price($fac->lignes[$i][5])."'.price($fac->lignes[$i][6])."

"; print "\n"; } /* * Boutons actions */ print "

\n"; if ($fac->statut == 0 && $user->societe_id == 0) { if ($_GET["action"] == "edit") { print ''.$langs->trans("Cancel").''; } else { print ''.$langs->trans('Edit').''; } } if ($fac->statut == 1 && $fac->paye == 0 && $user->societe_id == 0) { print ''.$langs->trans("DoPaiement").''; } if ($fac->statut == 1 && price($resteapayer) <= 0 && $fac->paye == 0 && $user->societe_id == 0) { print "id&action=payed\">".$langs->trans('ClassifyPayed').""; } if ($fac->statut == 0 && $user->rights->fournisseur->facture->valider) { if ($_GET["action"] <> "edit") print "id&action=valid\">".$langs->trans('Valid').""; } else if ($user->rights->fournisseur->facture->creer) { print "id&action=copy&socid=$fac->socidp\">".$langs->trans('Copy').""; } if ($_GET["action"] != "edit" && $fac->statut == 0 && $user->rights->fournisseur->facture->creer) { print ''.$langs->trans("Delete").''; } print "
"; } } $db->close(); llxFooter('$Date$ - $Revision$'); ?>