From 55aa8bc4bcfc1940e46cfe4e90451441aea5cd45 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Thu, 9 Apr 2009 21:53:53 +0000 Subject: [PATCH] Add a tool to merge all invoices in one PDF --- dev/skeletons/skeleton_script.php | 2 +- htdocs/compta/facture.php | 2 +- htdocs/compta/facture/impayees.php | 3 +- htdocs/lib/functions.lib.php | 4 +- scripts/invoices/rebuild_merge_pdf.php | 275 +++++++++++++++++++++++++ 5 files changed, 280 insertions(+), 6 deletions(-) create mode 100644 scripts/invoices/rebuild_merge_pdf.php diff --git a/dev/skeletons/skeleton_script.php b/dev/skeletons/skeleton_script.php index b397e6c0123..de05e5b5a8f 100644 --- a/dev/skeletons/skeleton_script.php +++ b/dev/skeletons/skeleton_script.php @@ -33,7 +33,7 @@ if (eregi('([^\\\/]+)$',$script_file,$reg)) $script_file=$reg[1]; $path=eregi_replace($script_file,'',$_SERVER["PHP_SELF"]); if (substr($sapi_type, 0, 3) == 'cgi') { - echo "Error: You ar usingr PH for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n"; + echo "Error: You are using PH for CGI/Web. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n"; exit; } diff --git a/htdocs/compta/facture.php b/htdocs/compta/facture.php index 39e31478c9d..a198e72d32e 100644 --- a/htdocs/compta/facture.php +++ b/htdocs/compta/facture.php @@ -3836,7 +3836,7 @@ else { // Print total print ''; - print ''.$langs->trans('Total').''; + print ''.$langs->trans('Total').''; print ''.price($total).''; print ''.price($total_ttc).''; print ''.price($totalrecu).''; diff --git a/htdocs/compta/facture/impayees.php b/htdocs/compta/facture/impayees.php index ee712008ba2..10f0af75c37 100644 --- a/htdocs/compta/facture/impayees.php +++ b/htdocs/compta/facture/impayees.php @@ -53,7 +53,6 @@ if ($_POST["action"] == "builddoc" && $user->rights->facture->lire) { if (is_array($_POST['toGenerate'])) { - $factures = dol_dir_list($conf->facture->dir_output,'all',1,implode('\.pdf|',$_POST['toGenerate']).'\.pdf','\.meta$|\.png','date',SORT_DESC) ; // liste les fichiers @@ -95,7 +94,7 @@ if ($_POST["action"] == "builddoc" && $user->rights->facture->lire) // Create output dir if not exists create_exdir($diroutputpdf); - // enregistre le fichier pdf concatene + // Save merged file $filename=strtolower(sanitizeFileName($langs->transnoentities("Unpayed"))); if ($option=='late') $filename.='_'.strtolower(sanitizeFileName($langs->transnoentities("Late"))); if ($pagecount) diff --git a/htdocs/lib/functions.lib.php b/htdocs/lib/functions.lib.php index c22aa1cf4c8..4bf4c3fe55a 100644 --- a/htdocs/lib/functions.lib.php +++ b/htdocs/lib/functions.lib.php @@ -447,8 +447,8 @@ function dol_print_date($time,$format='',$to_gmt=false,$outputlangs='',$encodeto * \param string Date in a string * YYYYMMDD * YYYYMMDDHHMMSS - * DD/MM/YY ou DD/MM/YYYY - * DD/MM/YY HH:MM:SS ou DD/MM/YYYY HH:MM:SS + * DD/MM/YY or DD/MM/YYYY (this format should not be used anymore) + * DD/MM/YY HH:MM:SS or DD/MM/YYYY HH:MM:SS (this format should not be used anymore) * \return date Date * \example 19700101020000 -> 7200 */ diff --git a/scripts/invoices/rebuild_merge_pdf.php b/scripts/invoices/rebuild_merge_pdf.php new file mode 100644 index 00000000000..140d0d79edf --- /dev/null +++ b/scripts/invoices/rebuild_merge_pdf.php @@ -0,0 +1,275 @@ +#!/usr/bin/php + + * + * 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. + */ + +/** + * \file scripts/invoices/rebuild_merge_pdf.php + * \ingroup facture + * \brief Script to rebuild PDF and merge PDF files into one + * \version $Id$ + */ + +// Test if batch mode +$sapi_type = php_sapi_name(); +$script_file=__FILE__; +if (eregi('([^\\\/]+)$',$script_file,$reg)) $script_file=$reg[1]; +$path=eregi_replace($script_file,'',$_SERVER["PHP_SELF"]); + +if (substr($sapi_type, 0, 3) == 'cgi') { + echo "Error: You are using PH for CGI/Web. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n"; + exit; +} + +// Include Dolibarr environment +require_once($path."../../htdocs/master.inc.php"); +// After this $db is an opened handler to database. We close it at end of file. +require_once(DOL_DOCUMENT_ROOT."/cron/functions_cron.lib.php"); +require_once(DOL_DOCUMENT_ROOT."/facture.class.php"); +require_once(DOL_DOCUMENT_ROOT."/includes/modules/facture/modules_facture.php"); +require_once(DOL_DOCUMENT_ROOT."/includes/fpdf/fpdfi/fpdi.php"); + + +// Load main language strings +$langs->load("main"); + +// Global variables +$version='$Revision$'; +$error=0; + + +// -------------------- START OF YOUR CODE HERE -------------------- +@set_time_limit(0); +print "***** ".$script_file." (".$version.") *****\n"; + +// Check parameters +if (! isset($argv[1])) +{ + usage(); + exit; +} + +$diroutputpdf=$conf->facture->dir_output . '/temp'; +$newmodel=''; // To force a new model +$newlangid='en_EN'; // To force a new lang id +$option=''; + +// Define options +if ($argv[1] == 'all') +{ + $option='all'; + + print 'Rebuild PDF for all invoices'."\n"; + if ($argv[2]) + { + $newlangid=$argv[2]; + print 'Use language '.$newlangid."\n"; + } + + $sql = "SELECT DISTINCT f.rowid, f.facnumber"; + $sql.= " FROM ".MAIN_DB_PREFIX."facture as f"; + $sql.= " ORDER BY f.facnumber ASC"; +} + +if ($argv[1] != 'all') +{ + $option=$argv[1].'_'.$argv[2]; + + if (! isset($argv[2])) + { + usage(); + exit; + } + + $dateafter=dol_stringtotime($argv[1]); + $datebefore=dol_stringtotime($argv[2]); + print 'Rebuild PDF for invoices with at least one payment between '.dol_print_date($dateafter,'day')." and ".dol_print_date($datebefore,'day')."\n"; + if ($argv[3]) + { + $newlangid=$argv[3]; + print 'Use language '.$newlangid."\n"; + } + + $sql = "SELECT DISTINCT f.rowid, f.facnumber"; + //$sql.= " s.nom,"; + //$sql.= " pf.amount,"; + //$sql.= " p.datep"; + $sql.= " FROM ".MAIN_DB_PREFIX."facture as f,"; + $sql.= " ".MAIN_DB_PREFIX."societe as s,"; + $sql.= " ".MAIN_DB_PREFIX."paiement_facture as pf,"; + $sql.= " ".MAIN_DB_PREFIX."paiement as p"; + $sql.= " WHERE f.fk_soc = s.rowid"; + $sql.= " AND f.rowid = pf.fk_facture"; + $sql.= " AND pf.fk_paiement = p.rowid"; + $sql.= " AND p.datep >= ".$db->idate($dateafter); + $sql.= " AND p.datep <= ".$db->idate($datebefore); + $sql.= " ORDER BY p.datep ASC"; +} + +print '--- start'."\n"; + +// Start of transaction +//$db->begin(); + +$error = 0; +$files = array() ; // liste les fichiers + +dol_syslog("scripts/invoices/rebuild_merge.php sql=".$sql); +if ( $resql=$db->query($sql) ) +{ + $num = $db->num_rows($resql); + $cpt = 0; + $oldemail = ''; + $message = ''; + $total = ''; + + if ($num) + { + // First loop on each resultset to build PDF + // ----------------------------------------- + + while ($cpt < $num) + { + $obj = $db->fetch_object($resql); + + $fac = new Facture($db); + $result=$fac->fetch($obj->rowid); + if ($result > 0) + { + $outputlangs = $langs; + if (! empty($newlangid)) + { + if ($outputlangs->defaultlang != $newlangid) + { + $outputlangs = new Translate("",$conf); + $outputlangs->setDefaultLang($newlangid); + } + } + print "Build PDF for invoice ".$obj->facnumber." - Lang = ".$outputlangs->defaultlang."\n"; + $result=facture_pdf_create($db, $fac->id, '', $newmodel?$newmodel:$fac->modelpdf, $outputlangs); + + // Add file into files array + $files[] = $conf->facture->dir_output.'/'.$fac->ref.'/'.$fac->ref.'.pdf'; + } + + if ($result <= 0) + { + print "Error: Failed to build PDF for invoice ".$fac->ref."\n"; + } + + $cpt++; + } + + + // Now, build a merged files with all files in $files array + //--------------------------------------------------------- + + // Create empty PDF + $pdf=new FPDI('P','mm','A4'); + if ($conf->global->MAIN_DISABLE_PDF_COMPRESSION) $pdf->SetCompression(false); + //$pdf->SetCompression(false); + + //$pdf->Open(); + //$pdf->AddPage(); + //$title=$langs->trans("BillsCustomersUnpayed"); + //if ($option=='late') $title=$langs->trans("BillsCustomersUnpayed"); + //$pdf->MultiCell(100, 3, $title, 0, 'J'); + + // Add all others + foreach($files as $file) + { + print "Merge PDF file for invoice ".$file."\n"; + + // Charge un document PDF depuis un fichier. + $pagecount = $pdf->setSourceFile($file); + for ($i = 1; $i <= $pagecount; $i++) + { + $tplidx = $pdf->importPage($i); + $s = $pdf->getTemplatesize($tplidx); + $pdf->AddPage($s['h'] > $s['w'] ? 'P' : 'L'); + $pdf->useTemplate($tplidx); + } + } + + // Create output dir if not exists + create_exdir($diroutputpdf); + + // Save merged file + $filename='mergedpdf'; + + if (! empty($option)) $filename.='_'.$option; + + if ($pagecount) + { + $file=$diroutputpdf.'/'.$filename.'.pdf'; + $pdf->Output($file); + if (! empty($conf->global->MAIN_UMASK)) + @chmod($file, octdec($conf->global->MAIN_UMASK)); + } + + print "Merged PDF has been built in ".$file."\n"; + } + else + { + print "No invoices with payments in this range.\n"; + } +} +else +{ + dol_print_error($db); + dol_syslog("scripts/invoices/rebuild_merge.php: Error"); +} + + +// -------------------- END OF YOUR CODE -------------------- + +if (! $error) +{ + //$db->commit(); + print '--- end ok'."\n"; +} +else +{ + print '--- end error code='.$error."\n"; + //$db->rollback(); +} + +$db->close(); + +return $error; + + +/** + * Show usage of script + * + * @return unknown + */ +function usage() +{ + global $script_file; + + print "Rebuild PDF files for some invoices and merge PDF files into one.\n"; + print "\n"; + print "To build/merge PDF for invoices with at least one payment in a range:\n"; + print "Usage: ".$script_file." datefirstpayment datelastpayment [langcode]\n"; + print "Example: ".$script_file." 20080101 20081231 fr_FR\n"; + print "\n"; + print "To build/merge PDF for all invoices:\n"; + print "Usage: ".$script_file." all [langcode]\n"; + print "Example: ".$script_file." all it_IT\n"; +} +?>