dolibarr/htdocs/includes/modules/facture/modules_facture.php

334 lines
8.9 KiB
PHP
Raw Normal View History

<?php
2005-03-08 11:38:12 +01:00
/* Copyright (C) 2003-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (C) 2004-2011 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2004 Eric Seigne <eric.seigne@ryxeo.com>
* Copyright (C) 2005-2011 Regis Houssin <regis@dolibarr.fr>
*
* 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/>.
* or see http://www.gnu.org/
*/
/**
* \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
*/
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
/**
* \class ModelePDFFactures
* \brief Classe mere des modeles de facture
2008-10-21 23:27:20 +02:00
*/
abstract class ModelePDFFactures extends CommonDocGenerator
{
var $error='';
2005-12-07 15:06:20 +01:00
/**
2011-05-06 15:35:18 +02:00
* Return list of active generation modules
* @param $db Database handler
*/
function liste_modeles($db)
{
2009-05-26 14:54:32 +02:00
global $conf;
2008-10-21 23:27:20 +02:00
$type='invoice';
$liste=array();
include_once(DOL_DOCUMENT_ROOT.'/lib/functions2.lib.php');
$liste=getListOfModels($db,$type,'');
return $liste;
}
2004-07-27 17:15:37 +02: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
{
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
*/
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
*/
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
*/
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
*/
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
*/
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
*/
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
}
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
*/
function facture_pdf_create($db, $object, $message, $modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0, $hookmanager=false)
{
2011-07-04 12:35:48 +02:00
global $conf,$user,$langs;
2011-08-11 01:20:55 +02:00
$langs->load("bills");
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);
$dir = "/includes/modules/facture/";
2011-02-20 20:04:14 +01:00
$srctemplatepath='';
2011-05-06 15:35:18 +02:00
// Positionne le modele sur le nom du modele a utiliser
if (! dol_strlen($modele))
{
2010-10-19 10:01:25 +02:00
if (! empty($conf->global->FACTURE_ADDON_PDF))
{
$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';
}
}
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
// On verifie l'emplacement du modele
$file = dol_buildpath($dir.'doc/'.$file);
2011-08-11 01:20:55 +02:00
if (file_exists($file))
2011-02-20 20:04:14 +01:00
{
$filefound=1;
$classname=$prefix.'_'.$modele;
break;
}
}
// Charge le modele
2011-02-20 20:04:14 +01:00
if ($filefound)
{
require_once($file);
$obj = new $classname($db);
$obj->message = $message;
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;
if ($obj->write_file($object, $outputlangs, $srctemplatepath, $hidedetails, $hidedesc, $hideref, $hookmanager) > 0)
{
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);
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
return 1;
}
else
{
2008-10-28 21:05:23 +01:00
$outputlangs->charset_output=$sav_charset_output;
dol_print_error($db,"facture_pdf_create Error: ".$obj->error);
2006-04-23 16:01:42 +02:00
return -1;
}
2005-12-07 15:06:20 +01:00
}
else
2005-12-07 15:06:20 +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
}
}
/**
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
*/
function facture_meta_create($db, $facid, $message="")
{
global $langs,$conf;
2011-09-08 22:02:57 +02:00
$fac = new Facture($db);
$fac->fetch($facid);
2010-08-06 15:56:25 +02:00
$fac->fetch_thirdparty();
2008-10-21 23:27:20 +02:00
if ($conf->facture->dir_output)
{
$facref = dol_sanitizeFileName($fac->ref);
$dir = $conf->facture->dir_output . "/" . $facref ;
$file = $dir . "/" . $facref . ".meta";
2008-10-21 23:27:20 +02:00
if (! is_dir($dir))
{
create_exdir($dir);
}
2008-10-21 23:27:20 +02:00
if (is_dir($dir))
{
2011-09-17 21:49:50 +02:00
$nblignes = count($fac->lines);
$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 . "\"
DATE=\"" . dol_print_date($fac->date,'') . "\"
NB_ITEMS=\"" . $nblignes . "\"
CLIENT=\"" . $client . "\"
TOTAL_HT=\"" . $fac->total_ht . "\"
TOTAL_TTC=\"" . $fac->total_ttc . "\"\n";
for ($i = 0 ; $i < $nblignes ; $i++)
{
//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)) . "\"
";
}
2008-10-21 23:27:20 +02:00
}
$fp = fopen($file,"w");
2008-10-21 23:27:20 +02:00
fputs($fp,$meta);
fclose($fp);
if (! empty($conf->global->MAIN_UMASK))
@chmod($file, octdec($conf->global->MAIN_UMASK));
}
}
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
*/
function facture_delete_preview($db, $facid)
{
2008-10-21 23:27:20 +02:00
global $langs,$conf;
require_once(DOL_DOCUMENT_ROOT."/lib/files.lib.php");
2011-09-08 22:02:57 +02:00
$fac = new Facture($db);
2008-10-21 23:27:20 +02:00
$fac->fetch($facid);
2008-10-21 23:27:20 +02:00
if ($conf->facture->dir_output)
2005-12-07 15:06:20 +01: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 ) )
{
if ( ! dol_delete_file($file,1) )
{
return 0;
}
2008-10-21 23:27:20 +02:00
}
}
return 1;
}
2009-05-26 14:54:32 +02:00
?>