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