dolibarr  7.0.0-beta
invoice2.lib.php
Go to the documentation of this file.
1 <?php
2 /*
3  * Copyright (C) 2009-2013 Laurent Destailleur <eldy@users.sourceforge.net>
4 *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  * or see http://www.gnu.org/
18  */
19 
26 require_once(DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php');
27 require_once(DOL_DOCUMENT_ROOT.'/core/lib/pdf.lib.php');
28 
29 
51 function rebuild_merge_pdf($db, $langs, $conf, $diroutputpdf, $newlangid, $filter, $dateafterdate, $datebeforedate, $paymentdateafter, $paymentdatebefore, $usestdout, $regenerate=0, $filesuffix='', $paymentbankid='', $thirdpartiesid='', $fileprefix='mergedpdf')
52 {
53  $sql = "SELECT DISTINCT f.rowid, f.facnumber";
54  $sql.= " FROM ".MAIN_DB_PREFIX."facture as f";
55  $sqlwhere='';
56  $sqlorder='';
57  if (in_array('all',$filter))
58  {
59  $sqlorder = " ORDER BY f.facnumber ASC";
60  }
61  if (in_array('date',$filter))
62  {
63  if (empty($sqlwhere)) $sqlwhere=' WHERE ';
64  else $sqlwhere.=" AND";
65  $sqlwhere.= " f.fk_statut > 0";
66  $sqlwhere.= " AND f.datef >= '".$db->idate($dateafterdate)."'";
67  $sqlwhere.= " AND f.datef <= '".$db->idate($datebeforedate)."'";
68  $sqlorder = " ORDER BY f.datef ASC";
69  }
70  if (in_array('nopayment',$filter))
71  {
72  $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON f.rowid = pf.fk_facture";
73  if (empty($sqlwhere)) $sqlwhere=' WHERE ';
74  else $sqlwhere.=" AND";
75  $sqlwhere.= " f.fk_statut > 0";
76  $sqlwhere.= " AND pf.fk_paiement IS NULL";
77  }
78  if (in_array('payments',$filter) || in_array('bank',$filter))
79  {
80  $sql.= ", ".MAIN_DB_PREFIX."paiement_facture as pf, ".MAIN_DB_PREFIX."paiement as p";
81  if (in_array('bank',$filter)) $sql.= ", ".MAIN_DB_PREFIX."bank as b";
82  if (empty($sqlwhere)) $sqlwhere=' WHERE ';
83  else $sqlwhere.=" AND";
84  $sqlwhere.= " f.fk_statut > 0";
85  $sqlwhere.= " AND f.rowid = pf.fk_facture";
86  $sqlwhere.= " AND pf.fk_paiement = p.rowid";
87  if (in_array('payments',$filter))
88  {
89  $sqlwhere.= " AND p.datep >= '".$db->idate($paymentdateafter)."'";
90  $sqlwhere.= " AND p.datep <= '".$db->idate($paymentdatebefore)."'";
91  }
92  if (in_array('bank',$filter))
93  {
94  $sqlwhere.= " AND p.fk_bank = b.rowid";
95  $sqlwhere.= " AND b.fk_account = ".$paymentbankid;
96  }
97  $sqlorder = " ORDER BY p.datep ASC";
98  }
99  if (in_array('nodeposit',$filter))
100  {
101  if (empty($sqlwhere)) $sqlwhere=' WHERE ';
102  else $sqlwhere.=" AND";
103  $sqlwhere.=' type <> 3';
104  }
105  if (in_array('noreplacement',$filter))
106  {
107  if (empty($sqlwhere)) $sqlwhere=' WHERE ';
108  else $sqlwhere.=" AND";
109  $sqlwhere.=' type <> 1';
110  }
111  if (in_array('nocreditnote',$filter))
112  {
113  if (empty($sqlwhere)) $sqlwhere=' WHERE ';
114  else $sqlwhere.=" AND";
115  $sqlwhere.=' type <> 2';
116  }
117  if (in_array('excludethirdparties',$filter) && is_array($thirdpartiesid))
118  {
119  if (empty($sqlwhere)) $sqlwhere=' WHERE ';
120  else $sqlwhere.=" AND";
121  $sqlwhere.=' f.fk_soc NOT IN ('.join(',',$thirdpartiesid).')';
122  }
123  if (in_array('onlythirdparties',$filter) && is_array($thirdpartiesid))
124  {
125  if (empty($sqlwhere)) $sqlwhere=' WHERE ';
126  else $sqlwhere.=" AND";
127  $sqlwhere.=' f.fk_soc IN ('.join(',',$thirdpartiesid).')';
128  }
129  if ($sqlwhere) $sql.=$sqlwhere;
130  if ($sqlorder) $sql.=$sqlorder;
131 
132  //print $sql; exit;
133  dol_syslog("scripts/invoices/rebuild_merge.php:", LOG_DEBUG);
134 
135  if ($usestdout) print '--- start'."\n";
136 
137  // Start of transaction
138  //$db->begin();
139 
140  $error = 0;
141  $result = 0;
142  $files = array() ; // liste les fichiers
143 
144  dol_syslog("scripts/invoices/rebuild_merge.php", LOG_DEBUG);
145  if ( $resql=$db->query($sql) )
146  {
147  $num = $db->num_rows($resql);
148  $cpt = 0;
149  $oldemail = '';
150  $message = '';
151  $total = '';
152 
153  if ($num)
154  {
155  // First loop on each resultset to build PDF
156  // -----------------------------------------
157 
158  while ($cpt < $num)
159  {
160  $obj = $db->fetch_object($resql);
161 
162  $fac = new Facture($db);
163  $result=$fac->fetch($obj->rowid);
164  if ($result > 0)
165  {
166  $outputlangs = $langs;
167  if (! empty($newlangid))
168  {
169  if ($outputlangs->defaultlang != $newlangid)
170  {
171  $outputlangs = new Translate("",$conf);
172  $outputlangs->setDefaultLang($newlangid);
173  }
174  }
175  $filename=$conf->facture->dir_output.'/'.$fac->ref.'/'.$fac->ref.'.pdf';
176  if ($regenerate || ! dol_is_file($filename))
177  {
178  if ($usestdout) print "Build PDF for invoice ".$obj->facnumber." - Lang = ".$outputlangs->defaultlang."\n";
179  $result= $fac->generateDocument($regenerate?$regenerate:$fac->modelpdf, $outputlangs);
180  }
181  else {
182  if ($usestdout) print "PDF for invoice ".$obj->facnumber." already exists\n";
183  }
184 
185  // Add file into files array
186  $files[] = $filename;
187  }
188 
189  if ($result <= 0)
190  {
191  $error++;
192  if ($usestdout) print "Error: Failed to build PDF for invoice ".($fac->ref?$fac->ref:' id '.$obj->rowid)."\n";
193  else dol_syslog("Failed to build PDF for invoice ".($fac->ref?$fac->ref:' id '.$obj->rowid), LOG_ERR);
194  }
195 
196  $cpt++;
197  }
198 
199 
200  // Define format of output PDF
201  $formatarray=pdf_getFormat($langs);
202  $page_largeur = $formatarray['width'];
203  $page_hauteur = $formatarray['height'];
204  $format = array($page_largeur,$page_hauteur);
205 
206  if ($usestdout) print "Using output PDF format ".join('x',$format)."\n";
207  else dol_syslog("Using output PDF format ".join('x',$format), LOG_ERR);
208 
209 
210  // Now, build a merged files with all files in $files array
211  //---------------------------------------------------------
212 
213  // Create empty PDF
214  $pdf=pdf_getInstance($format);
215  if (class_exists('TCPDF'))
216  {
217  $pdf->setPrintHeader(false);
218  $pdf->setPrintFooter(false);
219  }
220  $pdf->SetFont(pdf_getPDFFont($langs));
221 
222  if ($conf->global->MAIN_DISABLE_PDF_COMPRESSION) $pdf->SetCompression(false);
223  //$pdf->SetCompression(false);
224 
225  // Add all others
226  foreach($files as $file)
227  {
228  if ($usestdout) print "Merge PDF file for invoice ".$file."\n";
229  else dol_syslog("Merge PDF file for invoice ".$file);
230 
231  // Charge un document PDF depuis un fichier.
232  $pagecount = $pdf->setSourceFile($file);
233  for ($i = 1; $i <= $pagecount; $i++)
234  {
235  $tplidx = $pdf->importPage($i);
236  $s = $pdf->getTemplatesize($tplidx);
237  $pdf->AddPage($s['h'] > $s['w'] ? 'P' : 'L');
238  $pdf->useTemplate($tplidx);
239  }
240  }
241 
242  // Create output dir if not exists
243  dol_mkdir($diroutputpdf);
244 
245  // Save merged file
246  $filename=$fileprefix;
247  if (empty($filename)) $filename='mergedpdf';
248  if (! empty($filesuffix)) $filename.='_'.$filesuffix;
249  $file=$diroutputpdf.'/'.$filename.'.pdf';
250 
251  if (! $error && $pagecount)
252  {
253  $pdf->Output($file,'F');
254  if (! empty($conf->global->MAIN_UMASK))
255  @chmod($file, octdec($conf->global->MAIN_UMASK));
256  }
257 
258  if ($usestdout)
259  {
260  if (! $error) print "Merged PDF has been built in ".$file."\n";
261  else print "Can't build PDF ".$file."\n";
262  }
263 
264  $result = 1;
265  }
266  else
267  {
268  if ($usestdout) print "No invoices found for criteria.\n";
269  else dol_syslog("No invoices found for criteria");
270  $result = 0;
271  }
272  }
273  else
274  {
275  dol_print_error($db);
276  dol_syslog("scripts/invoices/rebuild_merge.php: Error");
277  $error++;
278  }
279 
280  if ($error) return -1;
281  else return $result;
282 }
283 
pdf_getInstance($format='', $metric='mm', $pagetype='P')
Return a PDF instance object.
Definition: pdf.lib.php:82
rebuild_merge_pdf($db, $langs, $conf, $diroutputpdf, $newlangid, $filter, $dateafterdate, $datebeforedate, $paymentdateafter, $paymentdatebefore, $usestdout, $regenerate=0, $filesuffix='', $paymentbankid='', $thirdpartiesid='', $fileprefix='mergedpdf')
Function to build a compiled PDF.
pdf_getFormat(Translate $outputlangs=null)
Return array with format properties of default PDF format.
Definition: pdf.lib.php:42
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
pdf_getPDFFont($outputlangs)
Return font name to use for PDF generation.
Definition: pdf.lib.php:233
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
Class to manage translations.
dol_is_file($pathoffile)
Return if path is a file.
Definition: files.lib.php:427
print
Draft customers invoices.
Definition: index.php:91
dol_mkdir($dir, $dataroot='', $newmask=null)
Creation of a directory (this can create recursive subdir)
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if(!empty($conf->fournisseur->enabled)&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->societe->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if(!empty($conf->fournisseur->enabled)&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1013
Class to manage invoices.