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>
|
2011-04-14 17:11:22 +02:00
|
|
|
* Copyright (C) 2004-2011 Laurent Destailleur <eldy@users.sourceforge.net>
|
2004-12-22 21:28:47 +01:00
|
|
|
* Copyright (C) 2004 Eric Seigne <eric.seigne@ryxeo.com>
|
2011-04-14 17:11:22 +02:00
|
|
|
* Copyright (C) 2005-2011 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
|
2011-08-01 01:24:38 +02:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2003-03-11 16:33:17 +01:00
|
|
|
* 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
|
2008-10-21 23:27:20 +02:00
|
|
|
*/
|
2004-08-15 17:04:24 +02:00
|
|
|
|
2011-08-27 17:40:08 +02:00
|
|
|
require_once(DOL_DOCUMENT_ROOT."/core/class/commondocgenerator.class.php");
|
2010-04-28 23:29:12 +02:00
|
|
|
require_once(DOL_DOCUMENT_ROOT."/product/class/product.class.php");
|
2010-04-25 14:43:27 +02:00
|
|
|
require_once(DOL_DOCUMENT_ROOT."/compta/bank/class/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
|
|
|
/**
|
2010-02-07 05:31:19 +01:00
|
|
|
* \class ModelePDFFactures
|
|
|
|
|
* \brief Classe mere des modeles de facture
|
2008-10-21 23:27:20 +02:00
|
|
|
*/
|
2011-08-27 17:40:08 +02:00
|
|
|
abstract class ModelePDFFactures extends CommonDocGenerator
|
2004-08-15 17:04:24 +02:00
|
|
|
{
|
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
|
|
|
/**
|
2011-05-06 15:35:18 +02:00
|
|
|
* Return list of active generation modules
|
|
|
|
|
* @param $db Database handler
|
2006-03-22 00:47:01 +01:00
|
|
|
*/
|
|
|
|
|
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();
|
2006-06-13 00:20:24 +02:00
|
|
|
|
2010-02-27 23:53:27 +01:00
|
|
|
include_once(DOL_DOCUMENT_ROOT.'/lib/functions2.lib.php');
|
|
|
|
|
$liste=getListOfModels($db,$type,'');
|
|
|
|
|
|
2006-03-22 00:47:01 +01:00
|
|
|
return $liste;
|
|
|
|
|
}
|
2004-07-27 17:15:37 +02:00
|
|
|
}
|
|
|
|
|
|
2005-11-11 20:22:57 +01:00
|
|
|
/**
|
2010-02-07 05:31:19 +01:00
|
|
|
* \class ModeleNumRefFactures
|
|
|
|
|
* \brief Classe mere des modeles de numerotation des references de facture
|
2008-10-21 23:27:20 +02:00
|
|
|
*/
|
2011-09-29 10:08:26 +02:00
|
|
|
abstract 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
|
|
|
|
2011-05-06 15:35:18 +02:00
|
|
|
/** Return if a module can be used or not
|
|
|
|
|
* @return boolean true if module can be used
|
2008-10-21 23:27:20 +02:00
|
|
|
*/
|
2008-09-05 01:44:36 +02:00
|
|
|
function isEnabled()
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-06 15:35:18 +02:00
|
|
|
/** Renvoi la description par defaut du modele de numerotation
|
|
|
|
|
* @return string Texte descripif
|
2008-10-21 23:27:20 +02:00
|
|
|
*/
|
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
|
|
|
|
2011-05-06 15:35:18 +02:00
|
|
|
/** Renvoi un exemple de numerotation
|
|
|
|
|
* @return string Example
|
2008-10-21 23:27:20 +02:00
|
|
|
*/
|
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
|
|
|
|
2011-05-06 15:35:18 +02:00
|
|
|
/** Test si les numeros deja en vigueur dans la base ne provoquent pas
|
|
|
|
|
* de conflits qui empecheraient cette numerotation de fonctionner.
|
|
|
|
|
* @return boolean false si conflit, true si ok
|
2008-10-21 23:27:20 +02:00
|
|
|
*/
|
2006-12-16 17:30:41 +01:00
|
|
|
function canBeActivated()
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2008-10-21 23:27:20 +02:00
|
|
|
|
2011-05-06 15:35:18 +02:00
|
|
|
/** Renvoi prochaine valeur attribuee
|
|
|
|
|
* @param objsoc Objet societe
|
|
|
|
|
* @param facture Objet facture
|
|
|
|
|
* @return string Valeur
|
2008-10-21 23:27:20 +02:00
|
|
|
*/
|
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
|
|
|
|
2011-05-06 15:35:18 +02:00
|
|
|
/** Renvoi version du modele de numerotation
|
|
|
|
|
* @return string Valeur
|
2008-10-21 23:27:20 +02:00
|
|
|
*/
|
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
|
|
|
/**
|
2011-09-12 19:08:02 +02:00
|
|
|
* Create a document onto disk accordign to template module.
|
|
|
|
|
*
|
|
|
|
|
* @param DoliDB $db Database handler
|
|
|
|
|
* @param Object $object Object invoice
|
|
|
|
|
* @param string $message message
|
|
|
|
|
* @param string $modele Force le modele a utiliser ('' to not force)
|
|
|
|
|
* @param Translate $outputlangs objet lang a utiliser pour traduction
|
|
|
|
|
* @param int $hidedetails Hide details of lines
|
|
|
|
|
* @param int $hidedesc Hide description
|
|
|
|
|
* @param int $hideref Hide ref
|
|
|
|
|
* @param HookManager $hookmanager Hook manager instance
|
|
|
|
|
* @return int <0 if KO, >0 if OK
|
2008-10-21 23:27:20 +02:00
|
|
|
*/
|
2011-08-10 19:56:02 +02:00
|
|
|
function facture_pdf_create($db, $object, $message, $modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0, $hookmanager=false)
|
2003-04-13 16:48:38 +02:00
|
|
|
{
|
2011-07-04 12:35:48 +02:00
|
|
|
global $conf,$user,$langs;
|
2011-08-11 01:20:55 +02:00
|
|
|
|
2006-03-22 00:47:01 +01:00
|
|
|
$langs->load("bills");
|
2005-11-11 20:22:57 +01:00
|
|
|
|
2010-12-22 21:17:14 +01:00
|
|
|
// Increase limit for PDF build
|
|
|
|
|
$err=error_reporting();
|
|
|
|
|
error_reporting(0);
|
|
|
|
|
@set_time_limit(120);
|
|
|
|
|
error_reporting($err);
|
|
|
|
|
|
2011-04-14 17:04:36 +02:00
|
|
|
$dir = "/includes/modules/facture/";
|
2011-02-20 20:04:14 +01:00
|
|
|
$srctemplatepath='';
|
2003-04-13 16:48:38 +02:00
|
|
|
|
2011-05-06 15:35:18 +02:00
|
|
|
// Positionne le modele sur le nom du modele a utiliser
|
2010-08-24 16:42:18 +02:00
|
|
|
if (! dol_strlen($modele))
|
2006-03-22 00:47:01 +01:00
|
|
|
{
|
2010-10-19 10:01:25 +02:00
|
|
|
if (! empty($conf->global->FACTURE_ADDON_PDF))
|
2006-03-22 00:47:01 +01:00
|
|
|
{
|
|
|
|
|
$modele = $conf->global->FACTURE_ADDON_PDF;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2010-04-18 23:20:34 +02:00
|
|
|
//print $langs->trans("Error")." ".$langs->trans("Error_FACTURE_ADDON_PDF_NotDefined");
|
|
|
|
|
//return 0;
|
|
|
|
|
$modele = 'crabe';
|
2006-03-22 00:47:01 +01:00
|
|
|
}
|
|
|
|
|
}
|
2003-04-13 16:48:38 +02:00
|
|
|
|
2011-02-20 20:04:14 +01:00
|
|
|
// If selected modele is a filename template (then $modele="modelname:filename")
|
|
|
|
|
$tmp=explode(':',$modele,2);
|
|
|
|
|
if (! empty($tmp[1]))
|
|
|
|
|
{
|
|
|
|
|
$modele=$tmp[0];
|
|
|
|
|
$srctemplatepath=$tmp[1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Search template file
|
|
|
|
|
$file=''; $classname=''; $filefound=0;
|
|
|
|
|
foreach(array('doc','pdf') as $prefix)
|
|
|
|
|
{
|
|
|
|
|
$file = $prefix."_".$modele.".modules.php";
|
2011-08-11 01:20:55 +02:00
|
|
|
|
2011-04-14 17:04:36 +02:00
|
|
|
// On verifie l'emplacement du modele
|
|
|
|
|
$file = dol_buildpath($dir.'doc/'.$file);
|
2011-08-11 01:20:55 +02:00
|
|
|
|
2011-04-14 17:04:36 +02:00
|
|
|
if (file_exists($file))
|
2011-02-20 20:04:14 +01:00
|
|
|
{
|
|
|
|
|
$filefound=1;
|
|
|
|
|
$classname=$prefix.'_'.$modele;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-03-22 00:47:01 +01:00
|
|
|
// Charge le modele
|
2011-02-20 20:04:14 +01:00
|
|
|
if ($filefound)
|
2006-03-22 00:47:01 +01:00
|
|
|
{
|
2011-04-14 17:04:36 +02:00
|
|
|
require_once($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;
|
2011-08-10 19:56:02 +02:00
|
|
|
if ($obj->write_file($object, $outputlangs, $srctemplatepath, $hidedetails, $hidedesc, $hideref, $hookmanager) > 0)
|
2006-03-22 00:47:01 +01:00
|
|
|
{
|
2010-09-09 16:06:15 +02:00
|
|
|
// Success in building document. We build meta file.
|
|
|
|
|
facture_meta_create($db, $object->id);
|
|
|
|
|
// et on supprime l'image correspondant au preview
|
|
|
|
|
facture_delete_preview($db, $object->id);
|
2009-01-28 16:09:37 +01:00
|
|
|
|
2008-10-28 21:05:23 +01:00
|
|
|
$outputlangs->charset_output=$sav_charset_output;
|
2011-08-11 01:20:55 +02:00
|
|
|
|
2011-07-04 12:35:48 +02:00
|
|
|
// Appel des triggers
|
|
|
|
|
include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
|
|
|
|
|
$interface=new Interfaces($db);
|
|
|
|
|
$result=$interface->run_triggers('BILL_BUILDDOC',$object,$user,$langs,$conf);
|
|
|
|
|
if ($result < 0) { $error++; $this->errors=$interface->errors; }
|
|
|
|
|
// Fin appel triggers
|
2011-08-11 01:20:55 +02:00
|
|
|
|
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
|
|
|
/**
|
2010-10-27 16:47:32 +02:00
|
|
|
* Create a meta file with document file into same directory.
|
2011-09-08 22:02:57 +02:00
|
|
|
* This should allow rgrep search
|
|
|
|
|
*
|
2010-10-27 16:47:32 +02: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;
|
|
|
|
|
|
2011-09-08 22:02:57 +02:00
|
|
|
$fac = new Facture($db);
|
2006-09-06 01:25:48 +02:00
|
|
|
$fac->fetch($facid);
|
2010-08-06 15:56:25 +02:00
|
|
|
$fac->fetch_thirdparty();
|
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
|
|
|
{
|
2011-09-17 21:49:50 +02:00
|
|
|
$nblignes = count($fac->lines);
|
2009-12-23 11:07:48 +01:00
|
|
|
$client = $fac->client->nom . " " . $fac->client->address . " " . $fac->client->cp . " " . $fac->client->ville;
|
2008-10-21 23:27:20 +02:00
|
|
|
$meta = "REFERENCE=\"" . $fac->ref . "\"
|
2009-10-24 16:33:18 +02:00
|
|
|
DATE=\"" . dol_print_date($fac->date,'') . "\"
|
|
|
|
|
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++)
|
|
|
|
|
{
|
2009-10-24 16:33:18 +02:00
|
|
|
//Pour les articles
|
2010-10-27 16:47:32 +02:00
|
|
|
$meta .= "ITEM_" . $i . "_QUANTITY=\"" . $fac->lines[$i]->qty . "\"
|
|
|
|
|
ITEM_" . $i . "_UNIT_PRICE=\"" . $fac->lines[$i]->price . "\"
|
|
|
|
|
ITEM_" . $i . "_TVA=\"" .$fac->lines[$i]->tva_tx . "\"
|
|
|
|
|
ITEM_" . $i . "_DESCRIPTION=\"" . str_replace("\r\n","",nl2br($fac->lines[$i]->desc)) . "\"
|
2009-10-24 16:33:18 +02:00
|
|
|
";
|
|
|
|
|
}
|
2008-10-21 23:27:20 +02:00
|
|
|
}
|
2011-09-20 14:56:06 +02:00
|
|
|
$fp = fopen($file,"w");
|
2008-10-21 23:27:20 +02:00
|
|
|
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));
|
2004-12-22 21:28:47 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2004-08-15 17:04:24 +02:00
|
|
|
|
2004-12-28 16:32:24 +01:00
|
|
|
/**
|
2011-05-06 15:35:18 +02:00
|
|
|
* Supprime l'image de previsualitation, pour le cas de regeneration de facture
|
2011-09-08 22:02:57 +02:00
|
|
|
*
|
2011-05-06 15:35:18 +02:00
|
|
|
* @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;
|
2011-03-09 16:34:57 +01:00
|
|
|
require_once(DOL_DOCUMENT_ROOT."/lib/files.lib.php");
|
2005-07-29 11:18:32 +02:00
|
|
|
|
2011-09-08 22:02:57 +02:00
|
|
|
$fac = new Facture($db);
|
2008-10-21 23:27:20 +02:00
|
|
|
$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
|
|
|
?>
|