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