2004-10-20 00:24:10 +02:00
|
|
|
<?php
|
2005-03-08 11:38:12 +01:00
|
|
|
/* Copyright (C) 2003-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
|
2009-04-29 20:02:50 +02:00
|
|
|
* Copyright (C) 2004-2009 Laurent Destailleur <eldy@users.sourceforge.net>
|
2004-12-22 21:28:47 +01:00
|
|
|
* Copyright (C) 2004 Eric Seigne <eric.seigne@ryxeo.com>
|
2009-05-26 14:54:32 +02:00
|
|
|
* Copyright (C) 2005-2009 Regis Houssin <regis@dolibarr.fr>
|
2003-03-11 16:33:17 +01:00
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
* or see http://www.gnu.org/
|
|
|
|
|
*/
|
|
|
|
|
|
2005-08-22 21:39:55 +02:00
|
|
|
/**
|
2009-04-29 20:02:50 +02:00
|
|
|
* \file htdocs/includes/modules/facture/modules_facture.php
|
|
|
|
|
* \ingroup facture
|
|
|
|
|
* \brief Fichier contenant la classe mere de generation des factures en PDF
|
|
|
|
|
* et la classe mere de numerotation des factures
|
|
|
|
|
* \version $Id$
|
2008-10-21 23:27:20 +02:00
|
|
|
*/
|
2004-08-15 17:04:24 +02:00
|
|
|
|
2009-01-28 16:09:37 +01:00
|
|
|
require_once(DOL_DOCUMENT_ROOT.'/lib/pdf.lib.php');
|
2007-07-31 01:22:06 +02:00
|
|
|
require_once(DOL_DOCUMENT_ROOT.'/includes/fpdf/fpdfi/fpdi_protection.php');
|
2004-08-15 17:04:24 +02:00
|
|
|
require_once(DOL_DOCUMENT_ROOT."/product.class.php");
|
2009-01-28 16:09:37 +01:00
|
|
|
require_once(DOL_DOCUMENT_ROOT."/compta/bank/account.class.php"); // Requis car utilise dans les classes qui heritent
|
2004-08-15 17:04:24 +02:00
|
|
|
|
|
|
|
|
|
2005-08-23 21:31:25 +02:00
|
|
|
/**
|
2008-10-21 23:27:20 +02:00
|
|
|
\class ModelePDFFactures
|
2009-05-26 14:54:32 +02:00
|
|
|
\brief Classe mere des modeles de facture
|
2008-10-21 23:27:20 +02:00
|
|
|
*/
|
2004-07-27 17:15:37 +02:00
|
|
|
|
2004-08-15 17:04:24 +02:00
|
|
|
class ModelePDFFactures extends FPDF
|
|
|
|
|
{
|
2006-03-22 00:47:01 +01:00
|
|
|
var $error='';
|
2005-12-07 15:06:20 +01:00
|
|
|
|
2006-03-22 00:47:01 +01:00
|
|
|
/**
|
2009-05-26 14:54:32 +02:00
|
|
|
* \brief Renvoi le dernier message d'erreur de creation de facture
|
2006-03-22 00:47:01 +01:00
|
|
|
*/
|
|
|
|
|
function pdferror()
|
|
|
|
|
{
|
|
|
|
|
return $this->error;
|
|
|
|
|
}
|
2005-12-07 15:06:20 +01:00
|
|
|
|
2006-03-22 00:47:01 +01:00
|
|
|
/**
|
2009-05-26 14:54:32 +02:00
|
|
|
* \brief Renvoi la liste des modeles actifs
|
2006-03-22 00:47:01 +01:00
|
|
|
* \param db Handler de base
|
|
|
|
|
*/
|
|
|
|
|
function liste_modeles($db)
|
|
|
|
|
{
|
2009-05-26 14:54:32 +02:00
|
|
|
global $conf;
|
2009-10-04 19:18:09 +02:00
|
|
|
|
2008-10-21 23:27:20 +02:00
|
|
|
$type='invoice';
|
|
|
|
|
$liste=array();
|
2009-05-26 14:54:32 +02:00
|
|
|
$sql = "SELECT nom as id, nom as lib";
|
|
|
|
|
$sql.= " FROM ".MAIN_DB_PREFIX."document_model";
|
|
|
|
|
$sql.= " WHERE type = '".$type."'";
|
|
|
|
|
$sql.= " AND entity = ".$conf->entity;
|
2006-06-13 00:20:24 +02:00
|
|
|
|
2006-03-22 00:47:01 +01:00
|
|
|
$resql = $db->query($sql);
|
|
|
|
|
if ($resql)
|
|
|
|
|
{
|
|
|
|
|
$num = $db->num_rows($resql);
|
|
|
|
|
$i = 0;
|
|
|
|
|
while ($i < $num)
|
|
|
|
|
{
|
|
|
|
|
$row = $db->fetch_row($resql);
|
|
|
|
|
$liste[$row[0]]=$row[1];
|
|
|
|
|
$i++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2009-02-20 23:53:15 +01:00
|
|
|
dol_print_error($db);
|
2006-03-22 00:47:01 +01:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return $liste;
|
|
|
|
|
}
|
2005-08-23 21:31:25 +02:00
|
|
|
|
2004-07-27 17:15:37 +02:00
|
|
|
}
|
|
|
|
|
|
2005-11-11 20:22:57 +01:00
|
|
|
/**
|
2008-10-21 23:27:20 +02:00
|
|
|
\class ModeleNumRefFactures
|
2009-05-26 14:54:32 +02:00
|
|
|
\brief Classe mere des modeles de numerotation des references de facture
|
2008-10-21 23:27:20 +02:00
|
|
|
*/
|
2004-08-15 17:04:24 +02:00
|
|
|
class ModeleNumRefFactures
|
2003-03-11 16:33:17 +01:00
|
|
|
{
|
2006-12-16 17:30:41 +01:00
|
|
|
var $error='';
|
2008-10-21 23:27:20 +02:00
|
|
|
|
2008-09-05 01:44:36 +02:00
|
|
|
/** \brief Return if a module can be used or not
|
2008-10-21 23:27:20 +02:00
|
|
|
* \return boolean true if module can be used
|
|
|
|
|
*/
|
2008-09-05 01:44:36 +02:00
|
|
|
function isEnabled()
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2009-05-26 14:54:32 +02:00
|
|
|
/** \brief Renvoi la description par defaut du modele de numerotation
|
2008-10-21 23:27:20 +02:00
|
|
|
* \return string Texte descripif
|
|
|
|
|
*/
|
2006-12-16 17:30:41 +01:00
|
|
|
function info()
|
|
|
|
|
{
|
|
|
|
|
global $langs;
|
|
|
|
|
$langs->load("bills");
|
|
|
|
|
return $langs->trans("NoDescription");
|
|
|
|
|
}
|
2008-10-21 23:27:20 +02:00
|
|
|
|
2009-05-26 14:54:32 +02:00
|
|
|
/** \brief Renvoi un exemple de numerotation
|
2008-10-21 23:27:20 +02:00
|
|
|
* \return string Example
|
|
|
|
|
*/
|
2006-12-16 17:30:41 +01:00
|
|
|
function getExample()
|
|
|
|
|
{
|
|
|
|
|
global $langs;
|
|
|
|
|
$langs->load("bills");
|
|
|
|
|
return $langs->trans("NoExample");
|
|
|
|
|
}
|
2008-10-21 23:27:20 +02:00
|
|
|
|
2009-05-26 14:54:32 +02:00
|
|
|
/** \brief Test si les numeros deja en vigueur dans la base ne provoquent pas
|
|
|
|
|
* de conflits qui empecheraient cette numerotation de fonctionner.
|
2008-10-21 23:27:20 +02:00
|
|
|
* \return boolean false si conflit, true si ok
|
|
|
|
|
*/
|
2006-12-16 17:30:41 +01:00
|
|
|
function canBeActivated()
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2008-10-21 23:27:20 +02:00
|
|
|
|
2009-05-26 14:54:32 +02:00
|
|
|
/** \brief Renvoi prochaine valeur attribuee
|
2008-10-21 23:27:20 +02:00
|
|
|
* \param objsoc Objet societe
|
|
|
|
|
* \param facture Objet facture
|
|
|
|
|
* \return string Valeur
|
|
|
|
|
*/
|
2006-12-16 17:30:41 +01:00
|
|
|
function getNextValue($objsoc,$facture)
|
|
|
|
|
{
|
|
|
|
|
global $langs;
|
|
|
|
|
return $langs->trans("NotAvailable");
|
|
|
|
|
}
|
2008-10-21 23:27:20 +02:00
|
|
|
|
2007-09-11 23:55:31 +02:00
|
|
|
/** \brief Renvoi version du module numerotation
|
2008-10-21 23:27:20 +02:00
|
|
|
* \return string Valeur
|
|
|
|
|
*/
|
2007-09-11 23:55:31 +02:00
|
|
|
function getVersion()
|
|
|
|
|
{
|
|
|
|
|
global $langs;
|
|
|
|
|
$langs->load("admin");
|
|
|
|
|
|
|
|
|
|
if ($this->version == 'development') return $langs->trans("VersionDevelopment");
|
|
|
|
|
if ($this->version == 'experimental') return $langs->trans("VersionExperimental");
|
|
|
|
|
if ($this->version == 'dolibarr') return DOL_VERSION;
|
|
|
|
|
return $langs->trans("NotAvailable");
|
2008-10-21 23:27:20 +02:00
|
|
|
}
|
2003-08-02 17:26:11 +02:00
|
|
|
}
|
2003-04-13 16:48:38 +02:00
|
|
|
|
2004-07-25 15:12:15 +02:00
|
|
|
|
2004-12-28 16:32:24 +01:00
|
|
|
/**
|
2008-12-02 00:51:52 +01:00
|
|
|
* \brief Cree un facture sur disque en fonction du modele de FACTURE_ADDON_PDF
|
|
|
|
|
* \param db objet base de donnee
|
|
|
|
|
* \param id id de la facture a creer
|
|
|
|
|
* \param message message
|
|
|
|
|
* \param modele force le modele a utiliser ('' to not force)
|
|
|
|
|
* \param outputlangs objet lang a utiliser pour traduction
|
|
|
|
|
* \return int <0 if KO, >0 if OK
|
2008-10-21 23:27:20 +02:00
|
|
|
*/
|
2008-10-28 21:05:23 +01:00
|
|
|
function facture_pdf_create($db, $id, $message, $modele, $outputlangs)
|
2003-04-13 16:48:38 +02:00
|
|
|
{
|
2006-03-22 00:47:01 +01:00
|
|
|
global $conf,$langs;
|
|
|
|
|
$langs->load("bills");
|
2005-11-11 20:22:57 +01:00
|
|
|
|
2006-03-22 00:47:01 +01:00
|
|
|
$dir = DOL_DOCUMENT_ROOT . "/includes/modules/facture/";
|
2003-04-13 16:48:38 +02:00
|
|
|
|
2008-10-21 23:27:20 +02:00
|
|
|
// Positionne modele sur le nom du modele a utiliser
|
2006-03-22 00:47:01 +01:00
|
|
|
if (! strlen($modele))
|
|
|
|
|
{
|
|
|
|
|
if ($conf->global->FACTURE_ADDON_PDF)
|
|
|
|
|
{
|
|
|
|
|
$modele = $conf->global->FACTURE_ADDON_PDF;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
print $langs->trans("Error")." ".$langs->trans("Error_FACTURE_ADDON_PDF_NotDefined");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
2003-04-13 16:48:38 +02:00
|
|
|
|
2006-03-22 00:47:01 +01:00
|
|
|
// Charge le modele
|
|
|
|
|
$file = "pdf_".$modele.".modules.php";
|
|
|
|
|
if (file_exists($dir.$file))
|
|
|
|
|
{
|
|
|
|
|
$classname = "pdf_".$modele;
|
|
|
|
|
require_once($dir.$file);
|
2003-04-13 16:48:38 +02:00
|
|
|
|
2006-03-22 00:47:01 +01:00
|
|
|
$obj = new $classname($db);
|
|
|
|
|
$obj->message = $message;
|
2004-10-05 16:27:37 +02:00
|
|
|
|
2008-10-28 21:05:23 +01:00
|
|
|
// We save charset_output to restore it because write_file can change it if needed for
|
|
|
|
|
// output format that does not support UTF8.
|
|
|
|
|
$sav_charset_output=$outputlangs->charset_output;
|
2007-10-22 01:56:56 +02:00
|
|
|
if ($obj->write_file($id, $outputlangs) > 0)
|
2006-03-22 00:47:01 +01:00
|
|
|
{
|
2008-10-21 23:27:20 +02:00
|
|
|
// Success in building document. We build meta file.
|
2006-03-22 00:47:01 +01:00
|
|
|
facture_meta_create($db, $id);
|
|
|
|
|
// et on supprime l'image correspondant au preview
|
|
|
|
|
facture_delete_preview($db, $id);
|
2009-01-28 16:09:37 +01:00
|
|
|
|
2008-10-28 21:05:23 +01:00
|
|
|
$outputlangs->charset_output=$sav_charset_output;
|
2006-03-22 00:47:01 +01:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2008-10-28 21:05:23 +01:00
|
|
|
$outputlangs->charset_output=$sav_charset_output;
|
2009-02-20 23:53:15 +01:00
|
|
|
dol_print_error($db,"facture_pdf_create Error: ".$obj->error);
|
2006-04-23 16:01:42 +02:00
|
|
|
return -1;
|
2006-03-22 00:47:01 +01:00
|
|
|
}
|
2005-11-11 20:22:57 +01:00
|
|
|
|
2005-12-07 15:06:20 +01:00
|
|
|
}
|
2006-03-22 00:47:01 +01:00
|
|
|
else
|
2005-12-07 15:06:20 +01:00
|
|
|
{
|
2009-02-20 23:53:15 +01:00
|
|
|
dol_print_error('',$langs->trans("Error")." ".$langs->trans("ErrorFileDoesNotExists",$dir.$file));
|
2006-04-23 16:01:42 +02:00
|
|
|
return -1;
|
2005-12-07 15:06:20 +01:00
|
|
|
}
|
2003-04-13 16:48:38 +02:00
|
|
|
}
|
|
|
|
|
|
2004-12-23 19:28:58 +01:00
|
|
|
/**
|
2009-05-26 14:54:32 +02:00
|
|
|
* \brief Cree un meta fichier a cote de la facture sur le disque pour faciliter les recherches en texte plein.
|
|
|
|
|
* Pourquoi ? tout simplement parcequ'en fin d'exercice quand je suis avec mon comptable je n'ai pas de
|
|
|
|
|
* connexion internet "rapide" pour retrouver en 2 secondes une facture non payee ou compliquee a gerer ... avec un rgrep c'est vite fait bien fait [eric seigne]
|
2009-02-10 23:07:31 +01:00
|
|
|
* \param db Objet base de donnee
|
|
|
|
|
* \param facid Id de la facture a creer
|
|
|
|
|
* \param message Message
|
2008-10-21 23:27:20 +02:00
|
|
|
*/
|
2004-12-22 21:28:47 +01:00
|
|
|
function facture_meta_create($db, $facid, $message="")
|
|
|
|
|
{
|
2006-09-06 01:25:48 +02:00
|
|
|
global $langs,$conf;
|
|
|
|
|
|
|
|
|
|
$fac = new Facture($db,"",$facid);
|
|
|
|
|
$fac->fetch($facid);
|
|
|
|
|
$fac->fetch_client();
|
2008-10-21 23:27:20 +02:00
|
|
|
|
2006-09-06 01:25:48 +02:00
|
|
|
if ($conf->facture->dir_output)
|
2004-12-22 21:28:47 +01:00
|
|
|
{
|
2009-04-29 20:02:50 +02:00
|
|
|
$facref = dol_sanitizeFileName($fac->ref);
|
2006-09-06 01:25:48 +02:00
|
|
|
$dir = $conf->facture->dir_output . "/" . $facref ;
|
|
|
|
|
$file = $dir . "/" . $facref . ".meta";
|
2008-10-21 23:27:20 +02:00
|
|
|
|
2006-09-06 01:25:48 +02:00
|
|
|
if (! is_dir($dir))
|
|
|
|
|
{
|
|
|
|
|
create_exdir($dir);
|
|
|
|
|
}
|
|
|
|
|
|
2008-10-21 23:27:20 +02:00
|
|
|
if (is_dir($dir))
|
2006-09-06 01:25:48 +02:00
|
|
|
{
|
2008-10-21 23:27:20 +02:00
|
|
|
$nblignes = sizeof($fac->lignes);
|
|
|
|
|
$client = $fac->client->nom . " " . $fac->client->adresse . " " . $fac->client->cp . " " . $fac->client->ville;
|
|
|
|
|
$meta = "REFERENCE=\"" . $fac->ref . "\"
|
2009-02-10 23:07:31 +01:00
|
|
|
DATE=\"" . dol_print_date($fac->date,'') . "\"
|
2004-12-22 21:28:47 +01:00
|
|
|
NB_ITEMS=\"" . $nblignes . "\"
|
|
|
|
|
CLIENT=\"" . $client . "\"
|
|
|
|
|
TOTAL_HT=\"" . $fac->total_ht . "\"
|
|
|
|
|
TOTAL_TTC=\"" . $fac->total_ttc . "\"\n";
|
2006-06-13 00:20:24 +02:00
|
|
|
|
2009-10-04 19:18:09 +02:00
|
|
|
for ($i = 0 ; $i < $nblignes ; $i++)
|
|
|
|
|
{
|
2008-10-21 23:27:20 +02:00
|
|
|
//Pour les articles
|
|
|
|
|
$meta .= "ITEM_" . $i . "_QUANTITY=\"" . $fac->lignes[$i]->qty . "\"
|
2004-12-22 21:28:47 +01:00
|
|
|
ITEM_" . $i . "_UNIT_PRICE=\"" . $fac->lignes[$i]->price . "\"
|
2006-09-10 22:18:26 +02:00
|
|
|
ITEM_" . $i . "_TVA=\"" .$fac->lignes[$i]->tva_tx . "\"
|
2004-12-22 21:28:47 +01:00
|
|
|
ITEM_" . $i . "_DESCRIPTION=\"" . str_replace("\r\n","",nl2br($fac->lignes[$i]->desc)) . "\"
|
|
|
|
|
";
|
|
|
|
|
}
|
2008-10-21 23:27:20 +02:00
|
|
|
}
|
|
|
|
|
$fp = fopen ($file,"w");
|
|
|
|
|
fputs($fp,$meta);
|
|
|
|
|
fclose($fp);
|
2009-01-28 16:09:37 +01:00
|
|
|
if (! empty($conf->global->MAIN_UMASK))
|
2009-10-04 19:18:09 +02:00
|
|
|
@chmod($file, octdec($conf->global->MAIN_UMASK));
|
2009-01-28 16:09:37 +01:00
|
|
|
|
2004-12-22 21:28:47 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2004-08-15 17:04:24 +02:00
|
|
|
|
2004-12-28 16:32:24 +01:00
|
|
|
/**
|
2009-10-04 19:18:09 +02:00
|
|
|
* \brief Supprime l'image de previsualitation, pour le cas de regeneration de facture
|
|
|
|
|
* \param db objet base de donnee
|
|
|
|
|
* \param facid id de la facture a creer
|
2008-10-21 23:27:20 +02:00
|
|
|
*/
|
2005-07-29 11:18:32 +02:00
|
|
|
function facture_delete_preview($db, $facid)
|
|
|
|
|
{
|
2008-10-21 23:27:20 +02:00
|
|
|
global $langs,$conf;
|
2005-07-29 11:18:32 +02:00
|
|
|
|
2008-10-21 23:27:20 +02:00
|
|
|
$fac = new Facture($db,"",$facid);
|
|
|
|
|
$fac->fetch($facid);
|
2005-07-29 11:18:32 +02:00
|
|
|
|
2008-10-21 23:27:20 +02:00
|
|
|
if ($conf->facture->dir_output)
|
2005-12-07 15:06:20 +01:00
|
|
|
{
|
2009-04-29 20:02:50 +02:00
|
|
|
$facref = dol_sanitizeFileName($fac->ref);
|
2008-10-21 23:27:20 +02:00
|
|
|
$dir = $conf->facture->dir_output . "/" . $facref ;
|
|
|
|
|
$file = $dir . "/" . $facref . ".pdf.png";
|
|
|
|
|
|
|
|
|
|
if ( file_exists( $file ) && is_writable( $file ) )
|
|
|
|
|
{
|
2009-10-04 19:18:09 +02:00
|
|
|
if ( ! dol_delete_file($file,1) )
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2008-10-21 23:27:20 +02:00
|
|
|
}
|
2005-11-11 20:22:57 +01:00
|
|
|
}
|
2009-10-04 19:18:09 +02:00
|
|
|
|
|
|
|
|
return 1;
|
2005-07-29 11:18:32 +02:00
|
|
|
}
|
|
|
|
|
|
2009-05-26 14:54:32 +02:00
|
|
|
?>
|