* Copyright (C) 2004 Éric Seigne
* Copyright (C) 2004-2005 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");
$warning_delay=31*24*60*60; // Delai affichage warning retard (si retard paiement facture > delai)
require_once "../facture.class.php";
require_once "../paiement.class.php";
if ($conf->projet->enabled) require_once "../project.class.php";
if ($conf->propal->enabled) require_once "../propal.class.php";
if ($conf->contrat->enabled) require_once "../contrat/contrat.class.php";
if ($conf->commande->enabled) require_once "../commande/commande.class.php";
require_once DOL_DOCUMENT_ROOT."/lib/CMailFile.class.php";
if ($_GET["socidp"]) { $socidp=$_GET["socidp"]; }
if (isset($_GET["msg"])) { $msg=urldecode($_GET["msg"]); }
// 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;
/*
* Actions
*/
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->mode_reglement = $_POST["mode_reglement"];
$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);
exit;
}
}
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 "
DEBUG: la propale précédente en ligne " . $prop->lignes[$i]->libelle . " avait comme prix : " . $prop->lignes[$i]->price . " !
';
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
// Génère également le fichier meta dans le m$eme répertoire (pour faciliter les recherches et indexation)
facture_pdf_create($db, $_GET["facid"]);
}
llxHeader('',$langs->trans("Bill"),'Facture');
$html = new Form($db);
/*********************************************************************
*
* Mode creation
*
**********************************************************************/
if ($_GET["action"] == 'create')
{
print_titre($langs->trans("NewBill"));
if ($_GET["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 = ".$_GET["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 '
id\">\n";
print '';
print '';
print '
';
print '
'.$langs->trans("Project").'
';
$proj = new Project($db);
$html->select_array("projetid",$proj->liste_array($soc->id));
print "
";
print '
';
}
/*
* Affiche formulaire mail
*/
if ($_GET["action"] == 'presend')
{
print ' ';
print_titre($langs->trans("SendBillByMail"));
$liste[0]=" ";
foreach ($soc->contact_email_array() as $key=>$value) {
$liste[$key]=$value;
}
// Créé l'objet formulaire mail
include_once(DOL_DOCUMENT_ROOT."/html.formmail.class.php");
$formmail = new FormMail($db);
$formmail->fromname = $user->fullname;
$formmail->frommail = $user->email;
$formmail->withfrom=1;
$formmail->withto=$liste;
$formmail->withcc=1;
$formmail->withtopic=$langs->trans("SendBillRef","__FACREF__");
$formmail->withfile=1;
$formmail->withbody=1;
// Tableau des substitutions
$formmail->substit["__FACREF__"]=$fac->ref;
// Tableau des paramètres complémentaires du post
$formmail->param["action"]="send";
$formmail->param["models"]="facture_send";
$formmail->param["facid"]=$fac->id;
$formmail->param["returnurl"]=DOL_URL_ROOT."/compta/facture.php?facid=$fac->id";
$formmail->show_form();
print ' ';
}
if ($_GET["action"] == 'prerelance')
{
print ' ';
print_titre($langs->trans("SendReminderBillByMail"));
$liste[0]=" ";
foreach ($soc->contact_email_array() as $key=>$value)
{
$liste[$key]=$value;
}
// Créé l'objet formulaire mail
include_once("../html.formmail.class.php");
$formmail = new FormMail($db);
$formmail->fromname = $user->fullname;
$formmail->frommail = $user->email;
$formmail->withfrom=1;
$formmail->withto=$liste;
$formmail->withcc=1;
$formmail->withtopic=$langs->trans("SendReminderBillRef","__FACREF__");
$formmail->withfile=1;
$formmail->withbody=1;
// Tableau des substitutions
$formmail->substit["__FACREF__"]=$fac->ref;
// Tableau des paramètres complémentaires
$formmail->param["action"]="relance";
$formmail->param["models"]="facture_relance";
$formmail->param["facid"]=$fac->id;
$formmail->param["returnurl"]=DOL_URL_ROOT."/compta/facture.php?facid=$fac->id";
$formmail->show_form();
print ' ';
}
/*
* Propales
*/
$sql = "SELECT ".$db->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";
$resql = $db->query($sql);
if ($resql)
{
$num = $db->num_rows($resql);
if ($num)
{
$i = 0; $total = 0;
print " ";
print_titre($langs->trans("RelatedCommercialProposals"));
print '