Inserted Copyright Notice and fixed graphic items
[fa-stable.git] / reporting / includes / pdf_report.inc
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU Affero General Public License,
5         AGPL, as published by the Free Software Foundation, either version 
6         3 of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/agpl-3.0.html>.
11 ***********************************************************************/
12 /* $Revision$ */
13 $page_security = 8;
14 //include_once($path_to_root . "reporting/includes/class.pdf.inc");
15 include_once(dirname(__FILE__)."/class.pdf.inc");
16 include_once(dirname(__FILE__)."/printer_class.inc");
17 include_once($path_to_root . "admin/db/company_db.inc");
18 include_once($path_to_root . "admin/db/printers_db.inc");
19 include_once($path_to_root . "config.php");
20 class FrontReport extends Cpdf
21 {
22         var $size;
23         var $company;
24         var $user;
25         var $host;
26         var $fiscal_year;
27         var $title;
28         var $filename;
29         var $pageWidth;
30         var $pageHeight;
31         var $topMargin;
32         var $bottomMargin;
33         var $leftMargin;
34         var $rightMargin;
35         var $endLine;
36         var $lineHeight;
37         //var $rtl;
38
39         var $cols;
40         var $params;
41         var $headers;
42         var $aligns;
43         var $headers2;
44         var $aligns2;
45         var $cols2;
46         var $companyCol;
47         var $titleCol;
48         var $pageNumber;
49         var $fontSize;
50         var $oldFontSize;
51         var $currency;
52
53         function FrontReport($title, $filename, $size = 'A4', $fontsize = 9)
54         {
55                 switch ($size)
56                 {
57
58                   case 'A4':
59                   case 'a4':
60                           $this->pageWidth=595;
61                           $this->pageHeight=842;
62                           $this->topMargin=40;
63                           $this->bottomMargin=30;
64                           $this->leftMargin=40;
65                           $this->rightMargin=30;
66                           break;
67                   case 'A4_Landscape':
68                           $this->pageWidth=842;
69                           $this->pageHeight=595;
70                           $this->topMargin=30;
71                           $this->bottomMargin=30;
72                           $this->leftMargin=40;
73                           $this->rightMargin=30;
74                           break;
75                    case 'A3':
76                           $this->pageWidth=842;
77                           $this->pageHeight=1190;
78                           $this->topMargin=50;
79                           $this->bottomMargin=50;
80                           $this->leftMargin=50;
81                           $this->rightMargin=40;
82                           break;
83                    case 'A3_landscape':
84                           $this->pageWidth=1190;
85                           $this->pageHeight=842;
86                           $this->topMargin=50;
87                           $this->bottomMargin=50;
88                           $this->leftMargin=50;
89                           $this->rightMargin=40;
90                           break;
91                    case 'letter':
92                    case 'Letter':
93                           $this->pageWidth=612;
94                           $this->pageHeight=792;
95                           $this->topMargin=30;
96                           $this->bottomMargin=30;
97                           $this->leftMargin=30;
98                           $this->rightMargin=25;
99                           break;
100                    case 'letter_landscape':
101                           $this->pageWidth=792;
102                           $this->pageHeight=612;
103                           $this->topMargin=30;
104                           $this->bottomMargin=30;
105                           $this->leftMargin=30;
106                           $this->rightMargin=25;
107                           break;
108                    case 'legal':
109                           $this->pageWidth=612;
110                           $this->pageHeight=1008;
111                           $this->topMargin=50;
112                           $this->bottomMargin=40;
113                           $this->leftMargin=30;
114                           $this->rightMargin=25;
115                           break;
116                    case 'legal_landscape':
117                           $this->pageWidth=1008;
118                           $this->pageHeight=612;
119                           $this->topMargin=50;
120                           $this->bottomMargin=40;
121                           $this->leftMargin=30;
122                           $this->rightMargin=25;
123                           break;
124                 }
125                 $this->size = array(0, 0, $this->pageWidth, $this->pageHeight);
126                 $this->title = $title;
127                 $this->filename = $filename;
128                 $this->pageNumber = 0;
129                 $this->endLine = $this->pageWidth - $this->rightMargin;
130                 $this->companyCol = $this->endLine - 150;
131                 $this->titleCol = $this->leftMargin + 100;
132                 $this->lineHeight = 12;
133                 $this->fontSize = $fontsize;
134                 $this->oldFontSize = 0;
135                 $this->row = $this->pageHeight - $this->topMargin;
136                 $this->currency = '';
137                 $rtl = ($_SESSION['language']->dir === 'rtl' ? 'rtl' : 'ltr');
138                 $code = $_SESSION['language']->code;
139                 $enc = strtoupper($_SESSION['language']->encoding);
140                 // for the language array in class.pdf.inc
141                 $l = array('a_meta_charset' => $enc, 'a_meta_dir' => $rtl, 'a_meta_language' => $code, 'w_page' => 'page');
142                 $this->Cpdf($size, $l);
143         }
144
145         function Font($style = 'normal')
146         {
147                 $this->selectFont("", $style);
148         }
149
150         function Info($params, $cols, $headers, $aligns,
151                 $cols2 = null, $headers2 = null, $aligns2 = null)
152         {
153                 global $app_title, $version, $power_by, $power_url, $path_to_root,      $db_connections;
154
155                 $this->addinfo('Title', $this->title);
156                 $this->addinfo('Subject', $this->title);
157                 $this->addinfo('Author', $app_title . ' ' . $version);
158                 $this->addinfo('Creator',$power_by . ' - ' . $power_url);
159                 $year = get_current_fiscalyear();
160                 if ($year['closed'] == 0)
161                         $how = _("Active");
162                 else
163                         $how = _("Closed");
164                 $this->fiscal_year = sql2date($year['begin']) . " - " . sql2date($year['end']) . "  " . "(" . $how . ")";
165                 $this->company = get_company_prefs();
166                 $this->user = $_SESSION["wa_current_user"]->name;
167                 //$this->host = $db_connections[$_SESSION["wa_current_user"]->company]["host"];
168                 $this->host = $_SERVER['SERVER_NAME'];
169                 $this->params = $params;
170                 $this->cols = $cols;
171                 for ($i = 0; $i < count($this->cols); $i++)
172                         $this->cols[$i] += $this->leftMargin;
173                 $this->headers = $headers;
174                 $this->aligns = $aligns;
175                 $this->cols2 = $cols2;
176                 if ($this->cols2 != null)
177                 {
178                         for ($i = 0; $i < count($this->cols2); $i++)
179                                 $this->cols2[$i] += $this->leftMargin;
180                 }
181                 $this->headers2 = $headers2;
182                 $this->aligns2 = $aligns2;
183         }
184
185         function Header()
186         {
187                 $this->pageNumber++;
188                 if ($this->pageNumber > 1)
189                         $this->newPage();
190                 $this->row = $this->pageHeight - $this->topMargin;
191
192                 $this->SetDrawColor(128, 128, 128);
193                 $this->Line($this->row + 5, 1);
194
195                 $this->NewLine();
196
197                 $this->fontSize += 4;
198                 $this->Font('bold');
199                 $this->Text($this->leftMargin, $this->title, $this->companyCol);
200                 $this->Font();
201                 $this->fontSize -= 4;
202                 $this->Text($this->companyCol, $this->company['coy_name']);
203                 $this->row -= ($this->lineHeight + 4);
204
205                 $str = _("Print Out Date") . ':';
206                 $this->Text($this->leftMargin, $str, $this->titleCol);
207                 $str = Today() . '   ' . Now();
208                 $this->Text($this->titleCol, $str, $this->companyCol);
209                 $this->Text($this->companyCol, $this->host);
210
211                 $this->NewLine();
212                 $str = _("Fiscal Year") . ':';
213                 $this->Text($this->leftMargin, $str, $this->titleCol);
214                 $str = $this->fiscal_year;
215                 $this->Text($this->titleCol, $str, $this->companyCol);
216                 $this->Text($this->companyCol, $this->user);
217                 for ($i = 1; $i < count($this->params); $i++)
218                 {
219                         if ($this->params[$i]['from'] != '')
220                         {
221                                 $this->NewLine();
222                                 $str = $this->params[$i]['text'] . ':';
223                                 $this->Text($this->leftMargin, $str, $this->titleCol);
224                                 $str = $this->params[$i]['from'];
225                                 if ($this->params[$i]['to'] != '')
226                                         $str .= " - " . $this->params[$i]['to'];
227                                 $this->Text($this->titleCol, $str, $this->companyCol);
228                         }
229                 }
230                 if ($this->params[0] != '') // Comments
231                 {
232                         $this->NewLine();
233                         $str = _("Comments") . ':';
234                         $this->Text($this->leftMargin, $str, $this->titleCol);
235                         $this->Font('bold');
236                         $this->Text($this->titleCol, $this->params[0], $this->endLine - 35);
237                         $this->Font();
238                 }
239                 $str = _("Page") . ' ' . $this->pageNumber;
240                 $this->Text($this->endLine - 38, $str);
241                 $this->Line($this->row - 5, 1);
242
243                 $this->row -= ($this->lineHeight + 6);
244                 $this->Font('italic');
245                 if ($this->headers2 != null)
246                 {
247                         $count = count($this->headers2);
248                         for ($i = 0; $i < $count; $i++)
249                                 $this->TextCol2($i, $i + 1,     $this->headers2[$i]);
250                         $this->NewLine();
251                 }
252                 $count = count($this->headers);
253                 for ($i = 0; $i < $count; $i++)
254                         $this->TextCol($i, $i + 1, $this->headers[$i]);
255                 $this->Font();
256                 $this->Line($this->row - 5, 1);
257
258                 $this->NewLine(2);
259         }
260
261         function Header2($myrow, $branch, $sales_order, $bankaccount, $doctype)
262         {
263                 global $comp_path, $path_to_root, $print_as_quote, $print_invoice_no;
264
265                 $this->pageNumber++;
266                 if ($this->pageNumber > 1)
267                         $this->newPage();
268                 $header2type = true;
269                 if ($this->currency != $myrow['curr_code'])
270                 {
271                         include($path_to_root . "reporting/includes/doctext2.inc");
272                 }
273                 else
274                 {
275                         include($path_to_root . "reporting/includes/doctext.inc");
276                 }
277
278                 include($path_to_root . "reporting/includes/header2.inc");
279
280                 $this->row = $temp;
281         }
282
283         function AddImage($logo, $x, $y, $w, $h)
284         {
285                 if (strpos($logo, ".png") || strpos($logo, ".PNG"))
286                         $this->addPngFromFile($logo, $x, $y, $w, $h);
287                 else
288                         $this->addJpegFromFile($logo, $x, $y, $w, $h);
289         }
290
291         function SetDrawColor($r, $g, $b)
292         {
293                 $this->setStrokeColor($r, $g, $b);
294         }
295
296         function SetTextColor($r, $g, $b)
297         {
298                 TCPDF::SetTextColor($r, $g, $b);
299         }
300
301         function Text($c, $txt, $n=0, $corr=0, $r=0)
302         {
303                 if ($n == 0)
304                         $n = $this->pageWidth - $this->rightMargin;
305
306                 return $this->TextWrap($c, $this->row - $r, $n - $c + $corr, $txt, 'left');
307         }
308
309         function TextWrap($xpos, $ypos, $len, $str, $align = 'left')
310         {
311                 if ($this->fontSize != $this->oldFontSize)
312                 {
313                         $this->SetFontSize($this->fontSize);
314                         $this->oldFontSize = $this->fontSize;
315                 }
316                 return $this->addTextWrap($xpos, $ypos, $len, $this->fontSize, $str, $align);
317         }
318
319         function TextCol($c, $n, $txt, $corr=0, $r=0)
320         {
321                 return $this->TextWrap($this->cols[$c], $this->row - $r, $this->cols[$n] - $this->cols[$c] + $corr, $txt, $this->aligns[$c]);
322         }
323
324         function TextCol2($c, $n, $txt, $corr=0, $r=0)
325         {
326                 return $this->TextWrap($this->cols2[$c], $this->row - $r, $this->cols2[$n] - $this->cols2[$c] + $corr, $txt, $this->aligns2[$c]);
327         }
328
329         function TextColLines($c, $n, $txt, $corr=0, $r=0)
330         {
331                 $this->row -= $r;
332                 $this->TextWrapLines($this->cols[$c], $this->cols[$n] - $this->cols[$c] + $corr, $txt, $this->aligns[$c]);
333         }
334
335         function TextWrapLines($c, $width, $txt, $align='left')
336         {
337                 $str = Explode("\n", $txt);
338                 for ($i = 0; $i < count($str); $i++)
339                 {
340                         $l = $str[$i];
341                         do
342                         {
343                                 $l = $this->TextWrap($c, $this->row , $width, $l, $align);
344                                 $this->NewLine();
345                         }
346                         while ($l != '');
347                 }
348         }
349
350         function LineTo($from, $row, $to, $row2)
351         {
352                 Cpdf::line($from, $row, $to, $row2);
353         }
354
355         function Line($row, $height = 0)
356         {
357                 $this->setLineStyle($height + 1);
358                 Cpdf::line($this->pageWidth - $this->rightMargin, $row ,$this->leftMargin, $row);
359         }
360
361         function NewLine($l=1, $np=0)
362         {
363                 $this->row -= ($l * $this->lineHeight);
364                 if ($np > 0 &&  $this->row < $this->bottomMargin + ($np * $this->lineHeight))
365                         $this->Header();
366         }
367
368         function End($email=0, $subject=null, $myrow=null, $doctype = 0)
369         {
370                 global $pdf_debug, $path_to_root, $comp_path;
371
372                 if ($pdf_debug == 1)
373                 {
374                         $pdfcode = $this->Output('','S');
375                         $pdfcode = str_replace("\n", "\n<br>", htmlspecialchars($pdfcode));
376                         echo '<html><body>';
377                         echo trim($pdfcode);
378                         echo '</body></html>';
379                         //header("Content-Length: $len");
380                         //header("Content-Disposition: inline; filename=" . $this->filename);
381                         //header('Expires: 0');
382                         //header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
383                         //header('Pragma: public');
384
385                         //$this->pdf->stream();
386                 }
387                 else
388                 {
389
390                         $dir =  $comp_path.'/'.user_company(). '/pdf_files';
391                         //save the file
392                         if (!file_exists($dir))
393                         {
394                                 mkdir ($dir,0777);
395                         }
396                         // do not use standard filenames or your sensitive company data 
397                         // are world readable
398                         $fname = $dir.'/'.uniqid('').'.pdf';
399                         $this->Output($fname, 'F');
400                         if ($email == 1)
401                         {
402                                 $emailtype = true;
403                                 if ($this->currency != $myrow['curr_code'])
404                                 {
405                                         include("doctext2.inc");
406                                 }
407                                 else
408                                 {
409                                         include("doctext.inc");
410                                 }
411                                 require_once($path_to_root . "reporting/includes/class.mail.inc");
412                         $mail = new email($this->company['coy_name'], $this->company['email']);
413                         $from = $this->company['coy_name'] . " <" . $this->company['email'] . ">";
414                         $to = $myrow['DebtorName'] . " <" . $myrow['email'] . ">";
415                         $msg = $doc_Dear_Sirs . ",\n\n" . $doc_AttachedFile . " " . $subject .
416                                 "\n\n";
417                                 if ($myrow['dimension_id'] > 0 && $doctype == 10) // helper for payment links
418                                 {
419                                         if ($myrow['dimension_id'] == 1)
420                                         {
421                                                 $amt = number_format($myrow["ov_freight"] + $myrow["ov_gst"] +  $myrow["ov_amount"], user_price_dec());
422                                                 $txt = $doc_Payment_Link . " PayPal: ";
423                                                 $nn = urlencode($this->title . " " . $myrow['reference']);
424                                                 $url = "https://www.paypal.com/xclick/business=" . $this->company['email'] . "&item_name=" .
425                                                         $nn . "&amount=" . $amt . "&currency_code=" . $myrow['curr_code'];
426                                                 $msg .= $txt . $url . "\n\n";
427                                         }
428                                 }
429                         $msg .= $doc_Kindest_regards . "\n\n";
430                         $sender = $this->user . "\n" . $this->company['coy_name'];
431                         $mail->to($to);
432                         $mail->subject($subject);
433                         $mail->text($msg . $sender);
434                         $mail->attachment($fname);
435                         $ret = $mail->send();
436                         if (1 == 1) // just for fun and for debugging purposes!!
437                         {
438                                 $from = str_replace("<", "(", $from);
439                                 $from = str_replace(">", ")", $from);
440                                 $to = str_replace("<", "(", $to);
441                                 $to = str_replace(">", ")", $to);
442                                 $msg2 = "<br>From: " . $from;
443                                 $msg2 .= "<br>To: " . $to;
444                                 $msg2 .= "<br>Subject: " . $subject;
445                                 $msg2 .= "<br>Msg: " . nl2br($msg);
446                                 $msg2 .= nl2br($sender) . "<br>";
447                                 $msg2 .= "<br>Filepath: " . $fname . "<br>Filename: " . $this->filename . "<br>";
448                         }
449                         if ($ret)
450                                 $str = "<br>" . $this->title . " " . $myrow['reference'] . " " . _("sent to") . " ";
451                         else
452                                 $str = "<br>" . $this->title . " " . $myrow['reference'] . " " . _("NOT sent to") . " ";
453                         $msg2 .= $str . $myrow['DebtorName'] . " - " . $myrow['email'];
454                         echo "<html><body>";
455                         echo $msg2;
456                         echo "</body></html>";
457                         }
458                         else
459                         {
460                                 $printer = get_report_printer(user_print_profile(), $_POST['REP_ID']);
461                                 if ($printer == false) {
462                                         if(in_ajax()) {
463                                                 global $Ajax;
464
465                                                 if (user_rep_popup()) 
466                                                         $Ajax->popup($fname); // when embeded pdf viewer used
467                                                 else
468                                                         $Ajax->redirect($fname); // otherwise use faster method
469                                         } else {
470                                         //echo '<html>
471                                         //              <head>
472                                         //                <SCRIPT LANGUAGE="JavaScript"><!--
473                                         //            function go_now () { window.location.href = "'.$fname.'"; }
474                                         //            //--></SCRIPT>
475                                         //        </head>
476                                         //        <body onLoad="go_now()"; >
477                                         //          <a href="'.$fname.'">click here</a> if you are not re-directed.
478                                         //        </body>
479                                         //    </html>';
480                         header('Content-type: application/pdf');
481                     header("Content-Disposition: inline; filename=$this->filename");
482                         header('Expires: 0');
483                     header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
484                         header('Pragma: public');
485                         $this->Stream();
486                                         }
487                                 } else { // send report to network printer
488                                         $prn = new remote_printer($printer['queue'],$printer['host'],
489                                                 $printer['port'], $printer['timeout']);
490                                         $error = $prn->print_file($fname);
491                                         if ($error)
492                                                 display_error($error);
493                                         else
494                                                 display_notification(_('Report has been sent to network printer ').$printer['name']);
495                                 }
496                         }
497                         // first have a look through the directory, 
498                         // and remove old temporary pdfs
499                         if ($d = @opendir($dir)) {
500                                 while (($file = readdir($d)) !== false) {
501                                         if (!is_file($dir.'/'.$file) || $file == 'index.php') continue;
502                                 // then check to see if this one is too old
503                                         $ftime = filemtime($dir.'/'.$file);
504                                  // seems 3 min is enough for any report download, isn't it?
505                                         if (time()-$ftime > 180){
506                                                 unlink($dir.'/'.$file);
507                                         }
508                                 }
509                                 closedir($d);
510                         }
511                 }
512         }
513 }
514
515 ?>