4ccd6b4b75b9e1a7ebe7a6db79a4318a823e3a9c
[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
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 ($this->rtl) // this is good for presentation only
138                 if (!$own)
139                 {
140                         if (user_price_dec() == 3) // only for presentation of farsi
141                         {
142                                 $this->rtl = true;
143                                 $path = $path_to_root . 'reporting/fonts/farsi_1.afm';
144                         }
145                         elseif ($style == 'italic')
146                                 $path = $path_to_root . 'reporting/fonts/Helvetica-Oblique.afm';
147                         elseif ($style == 'bold')
148                                 $path = $path_to_root . 'reporting/fonts/Helvetica-Bold.afm';
149                         else // even take misspelled styles
150                                 $path = $path_to_root . 'reporting/fonts/Helvetica.afm';
151                 }
152                 $this->selectFont($path, 'WinAnsiEncoding');
153         }
154
155         function Info($params, $cols, $headers, $aligns,
156                 $cols2 = null, $headers2 = null, $aligns2 = null)
157         {
158                 global $app_title, $version, $power_by, $power_url, $path_to_root,      $db_connections;
159
160                 $this->addinfo('Title', $this->title);
161                 $this->addinfo('Subject', $this->title);
162                 $this->addinfo('Author', $app_title . ' ' . $version);
163                 $this->addinfo('Creator',$power_by . ' - ' . $power_url);
164                 $year = get_current_fiscalyear();
165                 if ($year['closed'] == 0)
166                         $how = _("Active");
167                 else
168                         $how = _("Closed");
169                 $this->fiscal_year = sql2date($year['begin']) . " - " . sql2date($year['end']) . "  " . "(" . $how . ")";
170                 $this->company = get_company_prefs();
171                 $this->user = $_SESSION["wa_current_user"]->name;
172                 //$this->host = $db_connections[$_SESSION["wa_current_user"]->company]["host"];
173                 $this->host = $_SERVER['SERVER_NAME'];
174                 $this->params = $params;
175                 $this->cols = $cols;
176                 for ($i = 0; $i < count($this->cols); $i++)
177                         $this->cols[$i] += $this->leftMargin;
178                 $this->headers = $headers;
179                 $this->aligns = $aligns;
180                 $this->cols2 = $cols2;
181                 if ($this->cols2 != null)
182                 {
183                         for ($i = 0; $i < count($this->cols2); $i++)
184                                 $this->cols2[$i] += $this->leftMargin;
185                 }
186                 $this->headers2 = $headers2;
187                 $this->aligns2 = $aligns2;
188         }
189
190         function Header()
191         {
192                 $this->pageNumber++;
193                 if ($this->pageNumber > 1)
194                         $this->newPage();
195                 $this->row = $this->pageHeight - $this->topMargin;
196
197                 $this->SetDrawColor(128, 128, 128);
198                 $this->Line($this->row + 5, 1);
199
200                 $this->NewLine();
201
202                 $this->fontSize += 4;
203                 $this->Font('bold');
204                 $this->Text($this->leftMargin, $this->title, $this->companyCol);
205                 $this->Font();
206                 $this->fontSize -= 4;
207                 $this->Text($this->companyCol, $this->company['coy_name']);
208                 $this->row -= ($this->lineHeight + 4);
209
210                 $str = _("Print Out Date") . ':';
211                 $this->Text($this->leftMargin, $str, $this->titleCol);
212                 $str = Today() . '   ' . Now();
213                 $this->Text($this->titleCol, $str, $this->companyCol);
214                 $this->Text($this->companyCol, $this->host);
215
216                 $this->NewLine();
217                 $str = _("Fiscal Year") . ':';
218                 $this->Text($this->leftMargin, $str, $this->titleCol);
219                 $str = $this->fiscal_year;
220                 $this->Text($this->titleCol, $str, $this->companyCol);
221                 $this->Text($this->companyCol, $this->user);
222                 for ($i = 1; $i < count($this->params); $i++)
223                 {
224                         if ($this->params[$i]['from'] != '')
225                         {
226                                 $this->NewLine();
227                                 $str = $this->params[$i]['text'] . ':';
228                                 $this->Text($this->leftMargin, $str, $this->titleCol);
229                                 $str = $this->params[$i]['from'];
230                                 if ($this->params[$i]['to'] != '')
231                                         $str .= " - " . $this->params[$i]['to'];
232                                 $this->Text($this->titleCol, $str, $this->companyCol);
233                         }
234                 }
235                 if ($this->params[0] != '') // Comments
236                 {
237                         $this->NewLine();
238                         $str = _("Comments") . ':';
239                         $this->Text($this->leftMargin, $str, $this->titleCol);
240                         $this->Font('bold');
241                         $this->Text($this->titleCol, $this->params[0], $this->endLine - 35);
242                         $this->Font();
243                 }
244                 $str = _("Page") . ' ' . $this->pageNumber;
245                 $this->Text($this->endLine - 35, $str);
246                 $this->Line($this->row - 5, 1);
247
248                 $this->row -= ($this->lineHeight + 6);
249                 $this->Font('italic');
250                 if ($this->headers2 != null)
251                 {
252                         $count = count($this->headers2);
253                         for ($i = 0; $i < $count; $i++)
254                                 $this->TextCol2($i, $i + 1,     $this->headers2[$i]);
255                         $this->NewLine();
256                 }
257                 $count = count($this->headers);
258                 for ($i = 0; $i < $count; $i++)
259                         $this->TextCol($i, $i + 1, $this->headers[$i]);
260                 $this->Font();
261                 $this->Line($this->row - 5, 1);
262
263                 $this->NewLine(2);
264         }
265
266         function Header2($myrow, $branch, $sales_order, $bankaccount, $doctype)
267         {
268                 global $path_to_root, $print_as_quote, $print_invoice_no;
269
270                 $this->pageNumber++;
271                 if ($this->pageNumber > 1)
272                         $this->newPage();
273                 $header2type = true;
274                 if ($this->currency != $myrow['curr_code'])
275                 {
276                         include($path_to_root . "reporting/includes/doctext2.inc");
277                 }
278                 else
279                 {
280                         include($path_to_root . "reporting/includes/doctext.inc");
281                 }
282                 $this->row = $this->pageHeight - $this->topMargin;
283
284                 $upper = $this->row - 2 * $this->lineHeight;
285                 $lower = $this->bottomMargin + 6 * $this->lineHeight;
286                 $iline1 = $upper - 4 * $this->lineHeight;
287                 $iline2 = $iline1 - 8 * $this->lineHeight;
288                 $iline3 = $iline2 - 8 * $this->lineHeight;
289                 $iline4 = $iline3 - 2 * $this->lineHeight;
290                 $iline5 = $lower + 5 * $this->lineHeight;
291                 $icol = $this->pageWidth / 2;
292                 $ccol = $this->cols[0] + 4;
293                 $ccol2 = $icol / 2;
294                 $mcol = $icol + 8;
295                 $mcol2 = $this->pageWidth - $ccol2;
296
297                 $this->SetDrawColor(128, 128, 128);
298                 $this->LineTo($this->pageWidth - $this->rightMargin, $upper ,$this->leftMargin, $upper);
299                 $this->LineTo($this->leftMargin, $upper ,$this->leftMargin, $lower);
300                 $this->LineTo($this->pageWidth - $this->rightMargin, $lower ,$this->leftMargin, $lower);
301                 $this->LineTo($this->pageWidth - $this->rightMargin, $lower ,$this->pageWidth - $this->rightMargin, $upper);
302                 $this->Line($iline1);
303                 $this->Line($iline2);
304                 $this->Line($iline3);
305                 $this->Line($iline4);
306                 $this->Line($iline5);
307                 $this->LineTo($icol, $upper ,$icol, $iline1);
308                 $this->NewLine();
309
310                 $this->fontSize += 4;
311                 $this->Font('bold');
312                 $this->Text($mcol, $this->title);
313                 $this->Font();
314                 $this->fontSize -= 4;
315                 if ($this->pageNumber > 1 && !strstr($this->filename, "Bulk"))
316                         $this->Text($this->endLine - 35, _("Page") . ' ' . $this->pageNumber);
317                 $this->fontSize -= 4;
318                 $this->row = $upper - 5;
319
320                 $this->Text($mcol, $doc_Invoice_no, $mcol + 90);
321                 $this->Text($mcol + 90, $doc_Cust_no, $mcol + 180);
322                 $this->Text($mcol + 180, $doc_Date);
323                 $this->fontSize += 4;
324
325                 $this->row = $upper - 2 * $this->lineHeight - 2;
326                 if ($this->company['coy_logo'] != '')
327                 {
328                         $logo = $path_to_root . "themes/default/images/" . $this->company['coy_logo'];
329                         $this->AddImage($logo, $ccol, $iline1 + 5, 250, 40);
330                 }
331                 else
332                 {
333                         $this->fontSize += 4;
334                         $this->Font('bold');
335                         $this->Text($ccol, $this->company['coy_name'], $icol);
336                         $this->Font();
337                         $this->fontSize -= 4;
338                 }
339                 if ($doctype == 8) // PO
340                         $this->Text($mcol, $myrow['order_no'], $mcol + 90);
341                 else if ($doctype == 9) // SO
342                         $this->Text($mcol, $myrow['order_no'] ." ".$myrow['customer_ref'], $mcol + 90);
343                 else // INV/CRE/STA
344                 {
345                         if ($print_invoice_no == 1)
346                                 $this->Text($mcol, $myrow['trans_no'], $mcol + 90);
347                         else
348                                 $this->Text($mcol, $myrow['reference'], $mcol + 90);
349                 }
350                 $this->Text($mcol + 90, $myrow['debtor_no'], $mcol + 180);
351                 if ($doctype == 8 || $doctype == 9)
352                         $this->Text($mcol + 180, sql2date($myrow['ord_date']));
353                 else
354                         $this->Text($mcol + 180, sql2date($myrow['tran_date']));
355
356                 $this->fontSize -= 4;
357                 $this->row = $iline1 - 5;
358                 $this->Text($ccol, $doc_Charge_To, $icol);
359                 $this->Text($mcol, $doc_Delivered_To);
360                 $this->fontSize += 4;
361
362                 $this->NewLine(2);
363                 $temp = $this->row;
364                 if ($doctype == 9)
365                 {
366                         $this->Text($ccol, $myrow['name'], $icol);
367                         //$adr = ?;
368                 }
369                 else
370                 {
371                         if ($doctype == 8)
372                                 $this->Text($ccol, $myrow['supp_name'], $icol);
373                         else
374                                 $this->Text($ccol, $myrow['DebtorName'], $icol);
375                         $adr = explode("\n", $myrow['address']);
376                 }
377                 for ($i = 0; $i < count($adr); $i++)
378                 {
379                         $this->NewLine();
380                         $this->Text($ccol, $adr[$i], $icol);
381                 }
382                 if ($sales_order != NULL)
383                 {
384                         $this->row = $temp;
385                         if ($doctype == 8)
386                                 $this->Text($mcol, $this->company['coy_name']);
387                         else
388                                 $this->Text($mcol, $sales_order['deliver_to']);
389                         $adr = explode("\n", $sales_order['delivery_address']);
390                         for ($i = 0; $i < count($adr); $i++)
391                         {
392                                 $this->NewLine();
393                                 $this->Text($mcol, $adr[$i]);
394                         }
395                 }
396                 $this->row = $iline2 - 2 * $this->lineHeight;
397                 $this->Text($ccol, $doc_Shipping_Company . ":", $ccol2);
398                 if ($doctype != 8)
399                         $this->Text($ccol2, $myrow['shipper_name'], $mcol);
400                 $this->Text($mcol, $doc_Due_Date . ":", $mcol2);
401                 if ($doctype == 9)
402                         $this->Text($mcol2, sql2date($myrow['delivery_date']));
403                 else if ($doctype != 8)
404                         $this->Text($mcol2, sql2date($myrow['due_date']));
405                 if ($branch != null)
406                 {
407                         $this->NewLine();
408                         $this->Text($ccol, $doc_Your_Ref . ":", $ccol2);
409                         $this->Text($ccol2, $branch['contact_name'], $mcol);
410                         $this->Text($mcol, $doc_Our_Ref . ":", $mcol2);
411
412                         $id = $branch['salesman'];
413                         $sql = "SELECT salesman_name  FROM ".TB_PREF."salesman WHERE salesman_code='$id'";
414                         $result = db_query($sql,"could not get sales person");
415                         $row = db_fetch($result);
416
417                         $this->Text($mcol2, $row['salesman_name']);
418                 }
419                 $this->NewLine();
420                 $this->Text($ccol, $doc_Your_VAT_no . ":", $ccol2);
421                 if ($doctype != 8)
422                         $this->Text($ccol2, $myrow['tax_id'], $mcol);
423                 $this->Text($mcol, $doc_Our_VAT_no . ":", $mcol2);
424                 $this->Text($mcol2, $this->company['gst_no']);
425                 $this->NewLine();
426                 $this->Text($ccol, $doc_Payment_Terms . ":", $ccol2);
427
428                 $id = $myrow['payment_terms'];
429                 $sql = "SELECT terms FROM ".TB_PREF."payment_terms WHERE terms_indicator='$id'";
430                 $result = db_query($sql,"could not get paymentterms");
431                 $row = db_fetch($result);
432
433                 $this->Text($ccol2, $row["terms"], $mcol);
434                 $this->Text($mcol, $doc_Our_Order_No . ":", $mcol2);
435                 $this->Text($mcol2, $myrow['order_']);
436
437                 $locale = $path_to_root . "lang/" . $_SESSION['language']->code . "/locale.inc";
438                 if (file_exists($locale))
439                 {
440                         $header2include = true;
441                         include($locale);
442                 }
443                 $this->row = $iline3 - $this->lineHeight - 2;
444                 $this->Font('bold');
445                 $count = count($this->headers);
446                 for ($i = 0; $i < $count; $i++)
447                         $this->TextCol($i, $i + 1, $this->headers[$i], -2);
448                 $this->Font();
449                 $temp = $this->row - 2 * $this->lineHeight;
450                 $this->row = $iline5 - $this->lineHeight - 6;
451                 $this->Text($ccol, $doc_Please_Quote . " - " . $myrow['curr_code']);
452                 $this->NewLine();
453                 if ($branch['disable_branch'] > 0 && $doctype == 10) // payment links
454                 {
455                         if ($branch['disable_branch'] == 1)
456                         {
457                                 $amt = number_format($myrow["ov_freight"] + $myrow["ov_gst"] +  $myrow["ov_amount"], user_price_dec());
458                                 $txt = $doc_Payment_Link . " PayPal: ";
459                                 $name = urlencode($this->title . " " . $myrow['reference']);
460                                 $url = "https://www.paypal.com/xclick/business=" . $this->company['email'] . "&item_name=" .
461                                         $name . "&amount=" . $amt . "&currency_code=" . $myrow['curr_code'];
462                                 $this->fontSize -= 2;
463                                 $this->Text($ccol, $txt);
464                                 $this->NewLine();
465                                 $this->SetTextColor(0, 0, 255);
466                                 $this->Text($ccol, $url);
467                                 $this->SetTextColor(0, 0, 0);
468                                 $this->addLink($url, $ccol, $this->row, $this->pageWidth - $this->rightMargin, $this->row + $this->lineHeight);
469                                 $this->fontSize += 2;
470                         }
471                 }
472                 if ($this->params['comments'] != '')
473                 {
474                         $this->NewLine();
475                         $this->Font('bold');
476                         $this->Text($ccol, $this->params['comments']);
477                         $this->Font();
478                 }
479
480                 $this->row = $lower - 5;
481                 $this->fontSize -= 4;
482
483                 $this->Text($ccol, $doc_Address, $ccol2 + 40);
484                 $this->Text($ccol2 + 30, $doc_Phone_Fax_Email, $mcol);
485                 $this->Text($mcol , $doc_Bank, $mcol2);
486                 $this->Text($mcol2, $doc_Bank_Account);
487                 $this->fontSize += 4;
488                 $this->NewLine();
489                 $adrline = $this->row;
490
491                 $adr = explode("\n", $this->company['postal_address']);
492                 for ($i = 0; $i < count($adr); $i++)
493                 {
494                         $this->Text($ccol, $adr[$i], $ccol2 + 40);
495                         $this->NewLine();
496                 }
497                 $this->row = $adrline;
498                 $this->Text($ccol2 + 30, $this->company['phone'], $mcol);
499                 $this->NewLine();
500                 $this->Text($ccol2 + 30, $this->company['fax'], $mcol);
501                 $this->NewLine();
502                 $this->Text($ccol2 + 30, $this->company['email'], $mcol);
503                 // fetch this later
504                 $this->row = $adrline;
505                 $this->Text($mcol, $bankaccount['bank_name'], $mcol2);
506                 $adr = explode("\n", $bankaccount['bank_address']);
507                 for ($i = 0; $i < count($adr); $i++)
508                 {
509                         $this->NewLine();
510                         $this->Text($mcol, $adr[$i], $mcol2);
511                 }
512
513                 $this->row = $adrline;
514                 $this->Text($mcol2, $bankaccount['bank_account_name']);
515                 $this->NewLine();
516                 $this->Text($mcol2, $bankaccount['bank_account_number']);
517                 $this->row = $temp;
518         }
519
520         function AddImage($logo, $x, $y, $w, $h)
521         {
522                 if ($this->rtl)
523                         $x = $this->pageWidth - $x - $w;
524                 if (strpos($logo, ".png") || strpos($logo, ".PNG"))
525                         $this->addPngFromFile($logo, $x, $y, $w, $h);
526                 else
527                         $this->addJpegFromFile($logo, $x, $y, $w, $h);
528         }
529
530         function SetDrawColor($r, $g, $b)
531         {
532                 $this->setStrokeColor($r / 255, $g / 255, $b / 255);
533         }
534
535         function SetTextColor($r, $g, $b)
536         {
537                 $this->setColor($r / 255, $g / 255, $b / 255);
538         }
539
540         function Text($c, $txt, $n=0, $corr=0, $r=0)
541         {
542                 if ($n == 0)
543                         $n = $this->pageWidth - $this->rightMargin;
544
545                 return $this->TextWrap($c, $this->row - $r, $n - $c + $corr, $txt, 'left');
546         }
547
548         function TextWrap($xpos, $ypos, $len, $str, $align = 'left')
549         {
550                 if ($this->rtl)
551                 {
552                         $str = strrev($str);
553                         $xpos = $this->pageWidth - $xpos - $len;
554                         if ($align == 'left')
555                                 $align = 'right';
556                         elseif ($align == 'right')
557                                 $align = 'left';
558                 }
559                 return $this->addTextWrap($xpos, $ypos, $len, $this->fontSize, $str, $align);
560         }
561
562         function TextCol($c, $n, $txt, $corr=0, $r=0)
563         {
564                 return $this->TextWrap($this->cols[$c], $this->row - $r, $this->cols[$n] - $this->cols[$c] + $corr, $txt, $this->aligns[$c]);
565         }
566
567         function TextCol2($c, $n, $txt, $corr=0, $r=0)
568         {
569                 return $this->TextWrap($this->cols2[$c], $this->row - $r, $this->cols2[$n] - $this->cols2[$c] + $corr, $txt, $this->aligns2[$c]);
570         }
571
572         function TextColLines($c, $n, $txt, $corr=0)
573         {
574                 $str = Explode("\n", $txt);
575                 for ($i = 0; $i < count($str); $i++)
576                 {
577                         $l = $str[$i];
578                         do
579                         {
580                                 $l = $this->TextCol($c, $n, $l, $corr);
581                                 $this->NewLine();
582                         }
583                         while ($l != '');
584                 }
585         }
586
587         function LineTo($from, $row, $to, $row2)
588         {
589                 Cpdf::line($from, $row, $to, $row2);
590         }
591
592         function Line($row, $height = 0)
593         {
594                 $this->setLineStyle($height + 1);
595                 Cpdf::line($this->pageWidth - $this->rightMargin, $row ,$this->leftMargin, $row);
596         }
597
598         function NewLine($l=1, $np=0)
599         {
600                 $this->row -= ($l * $this->lineHeight);
601                 if ($np > 0 &&  $this->row < $this->bottomMargin + ($np * $this->lineHeight))
602                         $this->Header();
603         }
604
605         function End($email=0, $subject=null, $myrow=null, $doctype = 0)
606         {
607                 global $go_debug, $path_to_root;
608
609                 // this is no good in IE so has been replaced, see down under
610                 // but good for debugging purposes in IE
611                 //session_write_close();
612
613                 if ($go_debug == 1)
614                 {
615                         $buf = $this->output(1);
616                         $len = strlen($buf);
617                         $pdfcode = $buf;
618                         $pdfcode = str_replace("\n", "\n<br>", htmlspecialchars($pdfcode));
619                         echo '<html><body>';
620                         echo trim($pdfcode);
621                         echo '</body></html>';
622                         //header("Content-Length: $len");
623                         //header("Content-Disposition: inline; filename=" . $this->filename);
624                         //header('Expires: 0');
625                         //header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
626                         //header('Pragma: public');
627
628                         //$this->pdf->stream();
629                 }
630                 else
631                 {
632                         $buf = $this->output();
633                         $len = strlen($buf);
634                         $dir = './pdf_files';
635                         //save the file
636                         if (!file_exists($dir))
637                         {
638                                 mkdir ($dir,0777);
639                         }
640                         $fname = $dir . '/' . $this->filename;
641                         $fp = fopen($fname,'w');
642                         fwrite($fp,$buf);
643                         fclose($fp);
644                         if ($email == 1)
645                         {
646                                 $emailtype = true;
647                                 if ($this->currency != $myrow['curr_code'])
648                                 {
649                                         include("doctext2.inc");
650                                 }
651                                 else
652                                 {
653                                         include("doctext.inc");
654                                 }
655                                 require_once($path_to_root . "reporting/includes/class.mail.inc");
656                         $mail = new email($this->company['coy_name'], $this->company['email']);
657                         $from = $this->company['coy_name'] . " <" . $this->company['email'] . ">";
658                         $to = $myrow['DebtorName'] . " <" . $myrow['email'] . ">";
659                         $msg = $doc_Dear_Sirs . ",\n\n" . $doc_AttachedFile . " " . $subject .
660                                 "\n\n";
661                                 if ($myrow['dimension_id'] > 0 && $doctype == 10) // helper for payment links
662                                 {
663                                         if ($myrow['dimension_id'] == 1)
664                                         {
665                                                 $amt = number_format($myrow["ov_freight"] + $myrow["ov_gst"] +  $myrow["ov_amount"], user_price_dec());
666                                                 $txt = $doc_Payment_Link . " PayPal: ";
667                                                 $nn = urlencode($this->title . " " . $myrow['reference']);
668                                                 $url = "https://www.paypal.com/xclick/business=" . $this->company['email'] . "&item_name=" .
669                                                         $nn . "&amount=" . $amt . "&currency_code=" . $myrow['curr_code'];
670                                                 $msg .= $txt . $url . "\n\n";
671                                         }
672                                 }
673                         $msg .= $doc_Kindest_regards . "\n\n";
674                         $sender = $this->user . "\n" . $this->company['coy_name'];
675                         $mail->to($to);
676                         $mail->subject($subject);
677                         $mail->text($msg . $sender);
678                         $mail->attachment($fname);
679                         $ret = $mail->send();
680                         if (1 == 1) // just for fun and for debugging purposes!!
681                         {
682                                 $from = str_replace("<", "(", $from);
683                                 $from = str_replace(">", ")", $from);
684                                 $to = str_replace("<", "(", $to);
685                                 $to = str_replace(">", ")", $to);
686                                 $msg2 = "<br>From: " . $from;
687                                 $msg2 .= "<br>To: " . $to;
688                                 $msg2 .= "<br>Subject: " . $subject;
689                                 $msg2 .= "<br>Msg: " . nl2br($msg);
690                                 $msg2 .= nl2br($sender) . "<br>";
691                                 $msg2 .= "<br>Filepath: " . $fname . "<br>Filename: " . $this->filename . "<br>";
692                         }
693                         if ($ret)
694                                 $str = "<br>" . $this->title . " " . $myrow['reference'] . " " . _("sent to") . " ";
695                         else
696                                 $str = "<br>" . $this->title . " " . $myrow['reference'] . " " . _("NOT sent to") . " ";
697                         $msg2 .= $str . $myrow['DebtorName'] . " - " . $myrow['email'];
698                         echo "<html><body>";
699                         echo $msg2;
700                         echo "</body></html>";
701                         }
702                         else
703                         {
704                                 //echo '<html>
705                                 //              <head>
706                                 //                <SCRIPT LANGUAGE="JavaScript"><!--
707                                 //            function go_now () { window.location.href = "'.$fname.'"; }
708                                 //            //--></SCRIPT>
709                                 //        </head>
710                                 //        <body onLoad="go_now()"; >
711                                 //          <a href="'.$fname.'">click here</a> if you are not re-directed.
712                                 //        </body>
713                                 //    </html>';
714                 header('Content-type: application/pdf');
715                 header("Content-Disposition: inline; filename=$this->filename");
716                 header('Expires: 0');
717                 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
718                 header('Pragma: public');
719                 $this->stream();
720
721                         }
722                         // also have a look through the directory, and remove the files that are older than a week
723                         // rather want to save 'em
724                         /*if ($d = @opendir($dir)) {
725                                 while (($file = readdir($d)) !== false) {
726                                         // then check to see if this one is too old
727                                         $ftime = filemtime($dir.'/'.$file);
728                                         if (time()-$ftime > 3600*24*7){
729                                                 unlink($dir.'/'.$file);
730                                         }
731                                 }
732                                 closedir($d);
733                         }*/
734                 }
735         }
736 }
737
738 ?>