* Copyright (C) 2004 Éric Seigne
* Copyright (C) 2004 Laurent Destailleur
*
* 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/compta/facture.php
\ingroup facture
\brief Page de création d'une facture
\version $Revision$
*/
require("./pre.inc.php");
$user->getrights('facture');
$user->getrights('banque');
if (!$user->rights->facture->lire)
accessforbidden();
$langs->load("bills");
require("../facture.class.php");
require("../lib/CMailFile.class.php");
require("../paiement.class.php");
require("../project.class.php");
require("../propal.class.php");
require("./bank/account.class.php");
require("../contrat/contrat.class.php");
require("../commande/commande.class.php");
/*
* Sécurité accés client
*/
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);
$facture->fetch($_POST["facid"]);
$facture->classin($_POST["projetid"]);
}
/*
*
*/
if ($_POST["action"] == 'add')
{
$datefacture = mktime(12, 0 , 0, $_POST["remonth"], $_POST["reday"], $_POST["reyear"]);
$facture = new Facture($db, $_POST["socid"]);
$facture->number = $_POST["facnumber"];
$facture->date = $datefacture;
$facture->note = $_POST["note"];
if ($_POST["fac_rec"] > 0)
{
/*
* Facture récurrente
*/
$facture->fac_rec = $_POST["fac_rec"];
$facid = $facture->create($user);
}
else
{
$facture->projetid = $_POST["projetid"];
$facture->cond_reglement = $_POST["condid"];
$facture->amount = $_POST["amount"];
$facture->remise = $_POST["remise"];
$facture->remise_percent = $_POST["remise_percent"];
if (!$_POST["propalid"] && !$_POST["commandeid"])
{
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)
{
Header("Location: facture.php?facid=".$facid);
}
}
else
{
/*
* Propale
*/
if ($_POST["propalid"])
{
$facture->propalid = $_POST["propalid"];
$facid = $facture->create($user);
if ($facid)
{
$prop = New Propal($db);
if ( $prop->fetch($_POST["propalid"]) )
{
for ($i = 0 ; $i < sizeof($prop->lignes) ; $i++)
{
print "la propale précédente en ligne " . $prop->lignes[$i]->libelle . " avait comme prix : " . $prop->lignes[$i]->price . " ! \n";
$result = $facture->addline($facid,
addslashes($prop->lignes[$i]->libelle),
$prop->lignes[$i]->subprice,
$prop->lignes[$i]->qty,
$prop->lignes[$i]->tva_tx,
$prop->lignes[$i]->product_id,
$prop->lignes[$i]->remise_percent);
}
}
else
{
print "Erreur";
}
}
}
/*
* Commande
*/
if ($_POST["commandeid"])
{
$facture->commandeid = $_POST["commandeid"];
$facid = $facture->create($user);
if ($facid)
{
$comm = New Commande($db);
if ( $comm->fetch($_POST["commandeid"]) )
{
$lines = $comm->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 "Erreur";
}
}
else
{
print "Erreur : la facture n'a pas été créée, vérifier le numéro ! ";
print "
Retour à la propale ";
print $db->error();
}
}
if ($facid)
{
Header("Location: facture.php?facid=".$facid);
}
}
}
}
/*
*
*/
if ($_POST["action"] == 'confirm_valid' && $_POST["confirm"] == yes && $user->rights->facture->valider)
{
$fac = new Facture($db);
$fac->fetch($_GET["facid"]);
$soc = new Societe($db);
$soc->fetch($fac->socidp);
$result = $fac->set_valid($fac->id, $user, $soc);
if ($result)
{
facture_pdf_create($db, $fac->id);
}
}
if ($_GET["action"] == 'payed' && $user->rights->facture->paiement)
{
$fac = new Facture($db);
$result = $fac->set_payed($_GET["facid"]);
}
if ($_POST["action"] == 'setremise' && $user->rights->facture->creer)
{
$fac = new Facture($db);
$fac->fetch($facid);
$fac->set_remise($user, $_POST["remise"]);
}
if ($_POST["action"] == 'addligne' && $user->rights->facture->creer)
{
$fac = new Facture($db);
$fac->fetch($_POST["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($_POST["facid"],
$_POST["desc"],
$_POST["pu"],
$_POST["qty"],
$_POST["tva_tx"],
0,
$_POST["remise_percent"],
$datestart,
$dateend
);
$_GET["facid"]=$_POST["facid"]; // Pour réaffichage de la fiche en cours d'édition
}
if ($_POST["action"] == 'updateligne' && $user->rights->facture->creer)
{
$fac = new Facture($db,"",$_POST["facid"]);
$fac->fetch($_POST["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($_POST["rowid"],
$_POST["desc"],
$_POST["price"],
$_POST["qty"],
$_POST["remise_percent"],
$datestart,
$dateend
);
$_GET["facid"]=$_POST["facid"]; // Pour réaffichage de la fiche en cours d'édition
}
if ($_GET["action"] == 'deleteline' && $user->rights->facture->creer)
{
$fac = new Facture($db,"",$_GET["facid"]);
$fac->fetch($_GET["facid"]);
$result = $fac->deleteline($_GET["rowid"]);
}
if ($_POST["action"] == 'confirm_delete' && $_POST["confirm"] == yes)
{
if ($user->rights->facture->supprimer )
{
$fac = new Facture($db);
$fac->delete($_GET["facid"]);
$_GET["facid"] = 0 ;
Header("Location: facture.php");
}
}
if ($_POST["action"] == 'confirm_canceled' && $_POST["confirm"] == yes)
{
if ($user->rights->facture->supprimer )
{
$fac = new Facture($db);
$result = $fac->set_canceled($_GET["facid"]);
$_GET["facid"] = 0 ;
Header("Location: facture.php");
}
}
/*
*
*/
if ($_POST["action"] == 'send' || $_POST["action"] == 'relance')
{
$fac = new Facture($db,"",$_POST["facid"]);
if ( $fac->fetch($_POST["facid"]) )
{
$forbidden_chars=array("/","\\",":","*","?","\"","<",">","|","[","]",",",";","=");
$facref = str_replace($forbidden_chars,"_",$fac->ref);
$file = FAC_OUTPUTDIR . "/" . $facref . "/" . $facref . ".pdf";
if (is_readable($file))
{
if ($_POST["sendto"]) {
// Le destinataire a été fourni via le champ libre
$sendto = $_POST["sendto"];
$sendtoid = 0;
}
elseif ($_POST["receiver"]) {
// Le destinataire a été fourni via la liste déroulante
$soc = new Societe($db, $fac->socidp);
$sendto = $soc->contact_get_email($_POST["receiver"]);
$sendtoid = $_POST["receiver"];
}
if (strlen($sendto))
{
if ($_POST["action"] == 'send') {
$subject = $langs->trans("Bill")." $fac->ref";
$actioncode=9;
$actionmsg="Envoyée à $sendto";
$actionmsg2="Envoi Facture par mail";
}
if ($_POST["action"] == 'relance') {
$subject = "Relance facture $fac->ref";
$actioncode=10;
$actionmsg="Relance envoyée à $sendto";
$actionmsg2="Relance Facture par mail";
}
$message = $_POST["message"];
$filename = $fac->ref.".pdf";
$replyto = $_POST["replytoname"] . " <".$_POST["replytomail"] .">";
// Envoi de la facture
$mailfile = new CMailFile($subject,$sendto,$replyto,$message,array ($file),array ("application/pdf"),array ($filename));
if ( $mailfile->sendfile() )
{
$sendto = htmlentities($sendto);
$sql = "INSERT INTO ".MAIN_DB_PREFIX."actioncomm (datea,fk_action,fk_soc,note,fk_facture, fk_contact,fk_user_author, label, percent) VALUES (now(), '$actioncode' ,'$fac->socidp' ,'$actionmsg','$fac->id','$sendtoid','$user->id', '$actionmsg2',100);";
if (! $db->query($sql) )
{
dolibarr_print_error($db->error());
}
}
else
{
print $langs->trans("ErrorFailedToSendMail",$replyto,$sendto);
}
}
else
{
dolibarr_syslog("Le mail du destinataire est vide");
}
}
else
{
dolibarr_syslog("Impossible de lire :".$file);
}
}
else
{
dolibarr_syslog("Impossible de lire les données de la facture. Le fichier facture n'a peut-être pas été généré.");
}
}
/*
* Générer ou regénérer le PDF
*/
if ($_GET["action"] == 'pdf')
{
// Generation de la facture définie dans /includes/modules/facture/modules_facture.php
facture_pdf_create($db, $_GET["facid"]);
}
llxHeader('',$langs->trans("Bill"),'Facture');
$html = new Form($db);
/*********************************************************************
*
* Mode creation
*
************************************************************************/
if ($_GET["action"] == 'create')
{
print_titre("Emettre une facture");
if ($propalid)
{
$sql = "SELECT s.nom, s.prefix_comm, s.idp, p.price, p.remise, p.remise_percent, p.tva, p.total, p.ref, ".$db->pdate("p.datep")." as dp, c.id as statut, c.label as lst";
$sql .= " FROM ".MAIN_DB_PREFIX."societe as s, ".MAIN_DB_PREFIX."propal as p, ".MAIN_DB_PREFIX."c_propalst as c";
$sql .= " WHERE p.fk_soc = s.idp AND p.fk_statut = c.id";
$sql .= " AND p.rowid = $propalid";
}
else
{
$sql = "SELECT s.nom, s.prefix_comm, s.idp ";
$sql .= "FROM ".MAIN_DB_PREFIX."societe as s ";
$sql .= "WHERE s.idp = ".$_GET["socidp"];
}
if ($_GET["commandeid"])
{
$commande = New Commande($db);
$commande->fetch($_GET["commandeid"]);
$societe_id = $commande->soc_id;
}
if ( $db->query($sql) )
{
$num = $db->num_rows();
if ($num)
{
$obj = $db->fetch_object();
$soc = new Societe($db);
$soc->fetch($obj->idp);
print '
';
print ' ';
print ' ';
}
/*
* Lignes de factures
*
*/
$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)
{
$num_lignes = $db->num_rows();
$i = 0; $total = 0;
echo '';
if ($num_lignes)
{
print "";
print ''.$langs->trans("Description").' ';
print ''.$langs->trans("VAT").' ';
print 'P.U. HT ';
print ''.$langs->trans("Quantity").' ';
print 'Remise ';
print ''.$langs->trans("AmountHT").' ';
print ' ';
print " \n";
}
$var=True;
while ($i < $num_lignes)
{
$objp = $db->fetch_object();
$var=!$var;
print "";
if ($objp->fk_product > 0)
{
print ''.stripslashes(nl2br($objp->description)).' ';
if ($objp->date_start && $objp->date_end) { print " (Du ".dolibarr_print_date($objp->date_start)." au ".dolibarr_print_date($objp->date_end).")"; }
if ($objp->date_start && ! $objp->date_end) { print " (A partir du ".dolibarr_print_date($objp->date_start).")"; }
if (! $objp->date_start && $objp->date_end) { print " (Jusqu'au ".dolibarr_print_date($objp->date_end).")"; }
print ' ';
}
else
{
print "".stripslashes(nl2br($objp->description));
if ($objp->date_start && $objp->date_end) { print " (Du ".dolibarr_print_date($objp->date_start)." au ".dolibarr_print_date($objp->date_end).")"; }
if ($objp->date_start && ! $objp->date_end) { print " (A partir du ".dolibarr_print_date($objp->date_start).")"; }
if (! $objp->date_start && $objp->date_end) { print " (Jusqu'au ".dolibarr_print_date($objp->date_end).")"; }
print " \n";
}
print ''.$objp->tva_taux.' % ';
print ''.price($objp->subprice)." \n";
print ''.$objp->qty.' ';
if ($objp->remise_percent > 0)
{
print ''.$objp->remise_percent." % \n";
}
else
{
print ' ';
}
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)
{
print '';
print img_edit();
print ' ';
print '';
print img_delete();
print ' ';
}
else
{
print ' ';
}
print " ";
// Update ligne de facture
if ($_GET["action"] == 'editline' && $_GET["rowid"] == $objp->rowid)
{
print '';
print ' ';
print ' ';
print ' ';
print "";
print ''.stripslashes($objp->description).' ';
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 ' ' . "\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";
}
$total = $total + ($objp->qty * $objp->price);
$i++;
}
$db->free();
// print "
";
}
else
{
dolibarr_print_error($db->error());
}
/*
* Ajouter une ligne
*
*/
if ($fac->statut == 0 && $user->rights->facture->creer)
{
print "";
print "";
print ''.$langs->trans("Description").' ';
print ''.$langs->trans("VAT").' ';
print 'P.U. HT ';
print ''.$langs->trans("Quantity").' ';
print 'Remise ';
print ' ';
print ' ';
print ' ';
print " \n";
print ' ';
print ' ';
print ' ';
print '';
print $html->select_tva("tva_tx",$conf->defaulttx);
print ' ';
print ' ';
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 " \n";
/*
* Fin Ajout ligne
*
*/
print '';
if ($user->societe_id == 0 && $_GET["action"] <> 'valid')
{
print "";
}
print "\n";
/*
* Documents générés
*
*/
$forbidden_chars=array("/","\\",":","*","?","\"","<",">","|","[","]",",",";","=");
$facref = str_replace($forbidden_chars,"_",$fac->ref);
$file = FAC_OUTPUTDIR . "/" . $facref . "/" . $facref . ".pdf";
$filedetail = FAC_OUTPUTDIR . "/" . $facref . "/" . $facref . "-detail.pdf";
print "
";
if (file_exists($file))
{
$encfile = urlencode($file);
print_titre("Documents");
print '';
print "Facture PDF ";
print ''.$fac->ref.'.pdf ';
print ''.filesize($file). ' bytes ';
print ''.strftime("%d %b %Y %H:%M:%S",filemtime($file)).' ';
print ' ';
if (file_exists($filedetail)) // facture détaillée supplémentaire
{
$encfile = urlencode($filedetail);
print "Facture détaillée ";
print ''.$fac->ref.'-detail.pdf ';
print ''.filesize($filedetail). ' bytes ';
print ''.strftime("%d %b %Y %H:%M:%S",filemtime($filedetail)).' ';
print ' ';
}
print "
\n";
}
print ' ';
/*
* Liste des actions propres à la facture
*
*/
$sql = "SELECT id, ".$db->pdate("a.datea")." as da, a.note, code ";
$sql .= " FROM ".MAIN_DB_PREFIX."actioncomm as a, ".MAIN_DB_PREFIX."user as u ";
$sql .= " WHERE a.fk_user_author = u.rowid ";
$sql .= " AND a.fk_action in (9,10) ";
$sql .= " AND a.fk_soc = ".$fac->socidp ;
$sql .= " AND a.fk_facture = ".$fac->id;
$result = $db->query($sql);
if ($result)
{
$num = $db->num_rows();
if ($num)
{
print_titre($langs->trans("ActionsOnBill"));
$i = 0; $total = 0;
print '';
print ''.$langs->trans("Ref").' '.$langs->trans("Date").' '.$langs->trans("Action").' '.$langs->trans("By").' ';
print "\n";
$var=True;
while ($i < $num)
{
$objp = $db->fetch_object();
$var=!$var;
print "";
print ''.$objp->id.' ';
print ''.dolibarr_print_date($objp->da)." \n";
print ''.stripslashes($objp->note).' ';
print ''.$objp->code.' ';
print " \n";
$i++;
}
print "
\n";
}
}
else
{
dolibarr_print_error($db);
}
print "
";
/*
*
*
*/
if ($_GET["action"] == 'classer')
{
print "
id\">\n";
print ' ';
print ' ';
print '
';
}
/*
*
*
*/
if ($_GET["action"] == 'presend')
{
print_titre("Envoyer la facture par mail");
$langs->load("other");
$replytoname = $user->fullname;
$from_name = $replytoname;
$replytomail = $user->email;
$from_mail = $replytomail;
$form = new Form($db);
print "\n";
print ' ';
print ' ';
print ' ';
print ' ';
print ' ';
// From
print "";
print "".$langs->trans("MailFrom")." $from_name".($from_mail?" <$from_mail>":"")." ";
print "".$langs->trans("MailReply")." $replytoname".($replytomail?" <$replytomail>":"");
print " ";
print "
";
// To
print "";
print ''.$langs->trans("MailTo").' ';
$form->select_array("receiver",$soc->contact_email_array());
print " ".$langs->trans("or")." email\"> ";
// Topic + Message
$defaultmessage="Veuillez trouver ci-joint la facture $fac->ref\n\nCordialement\n\n";
$form->mail_topicmessagefile(0,1,0,$defaultmessage);
print " trans("Send")."\"> \n";
print "\n";
}
if ($_GET["action"] == 'prerelance')
{
print_titre("Envoyer une relance par mail");
$langs->load("other");
$replytoname = $user->fullname;
$from_name = $replytoname;
$replytomail = $user->email;
$from_mail = $replytomail;
$form = new Form($db);
print "\n";
print ' ';
print ' ';
print ' ';
print ' ';
print ' ';
// From
print "";
print "".$langs->trans("MailFrom")." $from_name".($from_mail?" <$from_mail>":"")." ";
print "".$langs->trans("MailReply")." $replytoname".($replytomail?" <$replytomail>":"");
print " ";
print "
";
// To
print "";
print ''.$langs->trans("MailTo").' ';
$form->select_array("receiver",$soc->contact_email_array());
print " ".$langs->trans("or")." email\"> ";
// Affiche la partie mail topic + message + file
$defaultmessage="Nous apportons à votre connaissance que la facture ".$fac->ref." ne semble toujours pas avoir été réglée. La voici donc, pour rappel, en pièce jointe.\n\nCordialement\n\n";
$form->mail_topicmessagefile(0,1,0,$defaultmessage);
print " trans("Send")."\"> \n";
print "pdate("p.datep")." as dp, p.price, p.ref, p.rowid as propalid";
$sql .= " FROM ".MAIN_DB_PREFIX."propal as p, ".MAIN_DB_PREFIX."fa_pr as fp WHERE fp.fk_propal = p.rowid AND fp.fk_facture = $fac->id";
$result = $db->query($sql);
if ($result)
{
$num = $db->num_rows();
if ($num)
{
$i = 0; $total = 0;
print "";
if ($num >1)
{
print_titre("Propositions commerciales associées");
}
else
{
print_titre("Proposition commerciale associée");
}
print '
';
print '';
print ''.$langs->trans("Ref").' ';
print ''.$langs->trans("Date").' ';
print ''.$langs->trans("Price").' ';
print " \n";
$var=True;
while ($i < $num)
{
$objp = $db->fetch_object();
$var=!$var;
print "";
print "propalid\">$objp->ref \n";
print "".strftime("%d %B %Y",$objp->dp)." \n";
print ''.price($objp->price).' ';
print " ";
$total = $total + $objp->price;
$i++;
}
print "".$langs->trans("TotalHT").": ".price($total)." ".MAIN_MONNAIE." \n";
print "
";
}
} else {
dolibarr_print_error($db);
}
}
else
{
/* Facture non trouvée */
print "Facture inexistante";
}
} else {
/***************************************************************************
* *
* Mode Liste *
* *
***************************************************************************/
$page=$_GET["page"];
$sortorder=$_GET["sortorder"];
$sortfield=$_GET["sortfield"];
$month=$_GET["month"];
$year=$_GET["year"];
if ($page == -1)
{
$page = 0 ;
}
if ($user->rights->facture->lire)
{
$limit = $conf->liste_limit;
$offset = $limit * $page ;
if ($sortorder == "")
$sortorder="DESC";
if ($sortfield == "")
$sortfield="f.datef";
$sql = "SELECT s.nom,s.idp,f.facnumber,f.total,f.total_ttc,".$db->pdate("f.datef")." as df, f.paye as paye, f.rowid as facid, f.fk_statut, sum(pf.amount) as am";
$sql .= " FROM ".MAIN_DB_PREFIX."societe as s,".MAIN_DB_PREFIX."facture as f left join ".MAIN_DB_PREFIX."paiement_facture as pf on f.rowid=pf.fk_facture WHERE f.fk_soc = s.idp";
if ($socidp)
$sql .= " AND s.idp = $socidp";
if ($month > 0)
$sql .= " AND date_format(f.datef, '%m') = $month";
if ($_GET["filtre"])
{
$filtrearr = split(",", $_GET["filtre"]);
foreach ($filtrearr as $fil)
{
$filt = split(":", $fil);
$sql .= " AND " . $filt[0] . " = " . $filt[1];
}
}
if ($year > 0)
$sql .= " AND date_format(f.datef, '%Y') = $year";
if (strlen($_POST["sf_ref"]) > 0)
{
$sql .= " AND f.facnumber like '%".$_POST["sf_ref"] . "%'";
}
$sql .= " GROUP BY f.facnumber";
$sql .= " ORDER BY ";
$listfield=split(',',$sortfield);
foreach ($listfield as $key => $value) {
$sql.="$listfield[$key] $sortorder,";
}
$sql .= " f.rowid DESC ";
$sql .= $db->plimit($limit,$offset);
$result = $db->query($sql);
}
if ($result)
{
$num = $db->num_rows();
print_barre_liste("Factures clients",$page,$_SERVER["PHP_SELF"],"&socidp=$socidp",$sortfield,$sortorder,'',$num);
$i = 0;
print '';
print '';
print '';
print_liste_field_titre($langs->trans("Ref"),$_SERVER["PHP_SELF"],"f.facnumber","","&socidp=$socidp");
print ' ';
print_liste_field_titre($langs->trans("Date"),$_SERVER["PHP_SELF"],"f.datef","","&socidp=$socidp");
print ' ';
print_liste_field_titre($langs->trans("Company"),$_SERVER["PHP_SELF"],"s.nom","","&socidp=$socidp");
print ' ';
print_liste_field_titre($langs->trans("AmountHT"),$_SERVER["PHP_SELF"],"f.total","","&socidp=$socidp");
print ' ';
print_liste_field_titre($langs->trans("AmountTTC"),$_SERVER["PHP_SELF"],"f.total_ttc","","&socidp=$socidp");
print ' ';
print_liste_field_titre("Reçu",$_SERVER["PHP_SELF"],"am","","&socidp=$socidp");
print ' ';
print_liste_field_titre($langs->trans("Status"),$_SERVER["PHP_SELF"],"fk_statut,paye","","&socidp=$socidp");
print ' ';
print " \n";
if ($num > 0)
{
$var=True;
$total=0;
$totalrecu=0;
while ($i < min($num,$limit))
{
$objp = $db->fetch_object();
$var=!$var;
print "";
if ($objp->paye)
{
$class = "normal";
}
else
{
if ($objp->fk_statut == 0)
{
$class = "normal";
}
else
{
$class = "impayee";
}
}
print ''.img_file()." \n";
print ''.$objp->facnumber." \n";
if ($objp->df > 0 )
{
print "";
$y = strftime("%Y",$objp->df);
$m = strftime("%m",$objp->df);
print strftime("%d",$objp->df)."\n";
print ' ';
print substr(strftime("%B",$objp->df),0,3)." \n";
print ' ';
print strftime("%Y",$objp->df)." \n";
}
else
{
print "!!! \n";
}
print ''.$objp->nom.' ';
print "".price($objp->total)." ";
print "".price($objp->total_ttc)." ";
print "".price($objp->am)." ";
// Affiche statut de la facture
if (! $objp->paye)
{
if ($objp->fk_statut == 0)
{
print 'brouillon ';
}
elseif ($objp->fk_statut == 3)
{
print 'abandonnée ';
}
else
{
print ''.($objp->am?"commencé":"impayée").' ';
}
}
else
{
print 'payée ';
}
print " \n";
$total+=$objp->total;
$total_ttc+=$objp->total_ttc;
$totalrecu+=$objp->am;
$i++;
}
if ($num <= $limit) {
// Print total
print "";
print "".$langs->trans("Total").": ";
print "".price($total)." ";
print "".price($total_ttc)." ";
print "".price($totalrecu)." ";
print ' ';
print " \n";
}
}
print "
";
$db->free();
}
else
{
dolibarr_print_error($db);
}
}
}
$db->close();
llxFooter("Dernière modification $Date$ révision $Revision$ ");
?>