Bug (right to left text was done if price decimals were 3). A test sentense from...
[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                 $this->row = $this->pageHeight - $this->topMargin;
277
278                 $upper = $this->row - 2 * $this->lineHeight;
279                 $lower = $this->bottomMargin + 6 * $this->lineHeight;
280                 $iline1 = $upper - 4 * $this->lineHeight;
281                 $iline2 = $iline1 - 8 * $this->lineHeight;
282                 $iline3 = $iline2 - 8 * $this->lineHeight;
283                 $iline4 = $iline3 - 2 * $this->lineHeight;
284                 $iline5 = $lower + 5 * $this->lineHeight;
285                 $icol = $this->pageWidth / 2;
286                 $ccol = $this->cols[0] + 4;
287                 $ccol2 = $icol / 2;
288                 $mcol = $icol + 8;
289                 $mcol2 = $this->pageWidth - $ccol2;
290
291                 $this->SetDrawColor(128, 128, 128);
292                 $this->LineTo($this->pageWidth - $this->rightMargin, $upper ,$this->leftMargin, $upper);
293                 $this->LineTo($this->leftMargin, $upper ,$this->leftMargin, $lower);
294                 $this->LineTo($this->pageWidth - $this->rightMargin, $lower ,$this->leftMargin, $lower);
295                 $this->LineTo($this->pageWidth - $this->rightMargin, $lower ,$this->pageWidth - $this->rightMargin, $upper);
296                 $this->Line($iline1);
297                 $this->Line($iline2);
298                 $this->Line($iline3);
299                 $this->Line($iline4);
300                 $this->Line($iline5);
301                 $this->LineTo($icol, $upper ,$icol, $iline1);
302                 $this->NewLine();
303
304                 $this->fontSize += 4;
305                 $this->Font('bold');
306                 $this->Text($mcol, $this->title);
307                 $this->Font();
308                 $this->fontSize -= 4;
309                 if ($this->pageNumber > 1 && !strstr($this->filename, "Bulk"))
310                         $this->Text($this->endLine - 35, _("Page") . ' ' . $this->pageNumber);
311                 $this->fontSize -= 4;
312                 $this->row = $upper - 5;
313
314                 $this->Text($mcol, $doc_Invoice_no, $mcol + 90);
315                 $this->Text($mcol + 90, $doc_Cust_no, $mcol + 180);
316                 $this->Text($mcol + 180, $doc_Date);
317                 $this->fontSize += 4;
318
319                 $this->row = $upper - 2 * $this->lineHeight - 2;
320                 if ($this->company['coy_logo'] != '')
321                 {
322                         $logo = $comp_path .'/'. user_company() . "/images/" . $this->company['coy_logo'];
323                         $this->AddImage($logo, $ccol, $iline1 + 5, 250, 40);
324                 }
325                 else
326                 {
327                         $this->fontSize += 4;
328                         $this->Font('bold');
329                         $this->Text($ccol, $this->company['coy_name'], $icol);
330                         $this->Font();
331                         $this->fontSize -= 4;
332                 }
333                 if ($doctype == 8) // PO
334                         $this->Text($mcol, $myrow['order_no'], $mcol + 90);
335                 else if ($doctype == 9) // SO
336                         $this->Text($mcol, $myrow['order_no'] ." ".$myrow['customer_ref'], $mcol + 90);
337                 else if (isset($myrow['trans_no']) && isset($myrow['reference'])) // INV/CRE/STA
338                 {
339                         if ($print_invoice_no == 1)
340                                 $this->Text($mcol, $myrow['trans_no'], $mcol + 90);
341                         else
342                                 $this->Text($mcol, $myrow['reference'], $mcol + 90);
343                 }
344                 if (isset($myrow['debtor_no']))
345                         $this->Text($mcol + 90, $myrow['debtor_no'], $mcol + 180);
346                 if ($doctype == 8 || $doctype == 9)
347                         $this->Text($mcol + 180, sql2date($myrow['ord_date']));
348                 else
349                         $this->Text($mcol + 180, sql2date($myrow['tran_date']));
350
351                 $this->fontSize -= 4;
352                 $this->row = $iline1 - 5;
353                 $this->Text($ccol, $doc_Charge_To, $icol);
354                 $this->Text($mcol, $doc_Delivered_To);
355                 $this->fontSize += 4;
356
357                 $this->NewLine(2);
358                 $temp = $this->row;
359                 if ($doctype == 9)
360                 {
361                         $this->Text($ccol, $myrow['name'], $icol);
362                         $adr = array();
363                 }
364                 else
365                 {
366                         if ($doctype == 8)
367                                 $this->Text($ccol, $myrow['supp_name'], $icol);
368                         else
369                                 $this->Text($ccol, $myrow['DebtorName'], $icol);
370                         $adr = explode("\n", $myrow['address']);
371                 }
372                 for ($i = 0; $i < count($adr); $i++)
373                 {
374                         $this->NewLine();
375                         $this->Text($ccol, $adr[$i], $icol);
376                 }
377                 if ($sales_order != NULL)
378                 {
379                         $this->row = $temp;
380                         if ($doctype == 8)
381                                 $this->Text($mcol, $this->company['coy_name']);
382                         else
383                                 $this->Text($mcol, $sales_order['deliver_to']);
384                         $adr = explode("\n", $sales_order['delivery_address']);
385                         for ($i = 0; $i < count($adr); $i++)
386                         {
387                                 $this->NewLine();
388                                 $this->Text($mcol, $adr[$i]);
389                         }
390                 }
391                 $this->row = $iline2 - 2 * $this->lineHeight;
392                 $this->Text($ccol, $doc_Shipping_Company . ":", $ccol2);
393                 if ($doctype != 8 && isset($myrow['shipper_name']))
394                         $this->Text($ccol2, $myrow['shipper_name'], $mcol);
395                 $this->Text($mcol, $doc_Due_Date . ":", $mcol2);
396                 if ($doctype == 9)
397                         $this->Text($mcol2, sql2date($myrow['delivery_date']));
398                 else if ($doctype != 8 && isset($myrow['due_date']))
399                         $this->Text($mcol2, sql2date($myrow['due_date']));
400                 if ($branch != null)
401                 {
402                         $this->NewLine();
403                         $this->Text($ccol, $doc_Your_Ref . ":", $ccol2);
404                         $this->Text($ccol2, $branch['contact_name'], $mcol);
405                         $this->Text($mcol, $doc_Our_Ref . ":", $mcol2);
406
407                         $id = $branch['salesman'];
408                         $sql = "SELECT salesman_name  FROM ".TB_PREF."salesman WHERE salesman_code='$id'";
409                         $result = db_query($sql,"could not get sales person");
410                         $row = db_fetch($result);
411
412                         $this->Text($mcol2, $row['salesman_name']);
413                 }
414                 $this->NewLine();
415                 $this->Text($ccol, $doc_Your_VAT_no . ":", $ccol2);
416                 if ($doctype != 8 && isset($myrow['tax_id']))
417                         $this->Text($ccol2, $myrow['tax_id'], $mcol);
418                 $this->Text($mcol, $doc_Our_VAT_no . ":", $mcol2);
419                 $this->Text($mcol2, $this->company['gst_no']);
420                 $this->NewLine();
421                 $this->Text($ccol, $doc_Payment_Terms . ":", $ccol2);
422
423                 $id = $myrow['payment_terms'];
424                 $sql = "SELECT terms FROM ".TB_PREF."payment_terms WHERE terms_indicator='$id'";
425                 $result = db_query($sql,"could not get paymentterms");
426                 $row = db_fetch($result);
427
428                 $this->Text($ccol2, $row["terms"], $mcol);
429                 $this->Text($mcol, $doc_Our_Order_No . ":", $mcol2);
430                 if (isset($myrow['order_']))
431                         $this->Text($mcol2, $myrow['order_']);
432
433                 $locale = $path_to_root . "lang/" . $_SESSION['language']->code . "/locale.inc";
434                 if (file_exists($locale))
435                 {
436                         $header2include = true;
437                         include($locale);
438                 }
439                 $this->row = $iline3 - $this->lineHeight - 2;
440                 $this->Font('bold');
441                 $count = count($this->headers);
442                 for ($i = 0; $i < $count; $i++)
443                         $this->TextCol($i, $i + 1, $this->headers[$i], -2);
444                 $this->Font();
445                 $temp = $this->row - 2 * $this->lineHeight;
446                 $this->row = $iline5 - $this->lineHeight - 6;
447                 $this->Text($ccol, $doc_Please_Quote . " - " . $myrow['curr_code']);
448                 $this->NewLine();
449                 if ($doctype == 10 && $branch['disable_branch'] > 0) // payment links
450                 {
451                         if ($branch['disable_branch'] == 1)
452                         {
453                                 $amt = number_format($myrow["ov_freight"] + $myrow["ov_gst"] +  $myrow["ov_amount"], user_price_dec());
454                                 $txt = $doc_Payment_Link . " PayPal: ";
455                                 $name = urlencode($this->title . " " . $myrow['reference']);
456                                 $url = "https://www.paypal.com/xclick/business=" . $this->company['email'] . "&item_name=" .
457                                         $name . "&amount=" . $amt . "&currency_code=" . $myrow['curr_code'];
458                                 $this->fontSize -= 2;
459                                 $this->Text($ccol, $txt);
460                                 $this->NewLine();
461                                 $this->SetTextColor(0, 0, 255);
462                                 $this->Text($ccol, $url);
463                                 $this->SetTextColor(0, 0, 0);
464                                 $this->addLink($url, $ccol, $this->row, $this->pageWidth - $this->rightMargin, $this->row + $this->lineHeight);
465                                 $this->fontSize += 2;
466                         }
467                 }
468                 if ($this->params['comments'] != '')
469                 {
470                         $this->NewLine();
471                         $this->Font('bold');
472                         $this->Text($ccol, $this->params['comments']);
473                         $this->Font();
474                 }
475
476                 $this->row = $lower - 5;
477                 $this->fontSize -= 4;
478
479                 $this->Text($ccol, $doc_Address, $ccol2 + 40);
480                 $this->Text($ccol2 + 30, $doc_Phone_Fax_Email, $mcol);
481                 $this->Text($mcol , $doc_Bank, $mcol2);
482                 $this->Text($mcol2, $doc_Bank_Account);
483                 $this->fontSize += 4;
484                 $this->NewLine();
485                 $adrline = $this->row;
486
487                 $adr = explode("\n", $this->company['postal_address']);
488                 for ($i = 0; $i < count($adr); $i++)
489                 {
490                         $this->Text($ccol, $adr[$i], $ccol2 + 40);
491                         $this->NewLine();
492                 }
493                 $this->row = $adrline;
494                 $this->Text($ccol2 + 30, $this->company['phone'], $mcol);
495                 $this->NewLine();
496                 $this->Text($ccol2 + 30, $this->company['fax'], $mcol);
497                 $this->NewLine();
498                 $this->Text($ccol2 + 30, $this->company['email'], $mcol);
499                 // fetch this later
500                 $this->row = $adrline;
501                 if (isset($bankaccount['bank_name']))
502                         $this->Text($mcol, $bankaccount['bank_name'], $mcol2);
503                 if (isset($bankaccount['bank_address']))
504                         $adr = explode("\n", $bankaccount['bank_address']);
505                 else
506                         $adr = array();
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                 if (isset($bankaccount['bank_account_name']))
515                         $this->Text($mcol2, $bankaccount['bank_account_name']);
516                 $this->NewLine();
517                 if (isset($bankaccount['bank_account_number']))
518                         $this->Text($mcol2, $bankaccount['bank_account_number']);
519                 $this->row = $temp;
520         }
521
522         function AddImage($logo, $x, $y, $w, $h)
523         {
524                 if ($this->rtl)
525                         $x = $this->pageWidth - $x - $w;
526                 if (strpos($logo, ".png") || strpos($logo, ".PNG"))
527                         $this->addPngFromFile($logo, $x, $y, $w, $h);
528                 else
529                         $this->addJpegFromFile($logo, $x, $y, $w, $h);
530         }
531
532         function SetDrawColor($r, $g, $b)
533         {
534                 $this->setStrokeColor($r / 255, $g / 255, $b / 255);
535         }
536
537         function SetTextColor($r, $g, $b)
538         {
539                 $this->setColor($r / 255, $g / 255, $b / 255);
540         }
541
542         function Text($c, $txt, $n=0, $corr=0, $r=0)
543         {
544                 if ($n == 0)
545                         $n = $this->pageWidth - $this->rightMargin;
546
547                 return $this->TextWrap($c, $this->row - $r, $n - $c + $corr, $txt, 'left');
548         }
549
550         function TextWrap($xpos, $ypos, $len, $str, $align = 'left')
551         {
552                 if ($this->rtl)
553                 {
554                         $str = strrev($str);
555                         $xpos = $this->pageWidth - $xpos - $len;
556                         if ($align == 'left')
557                                 $align = 'right';
558                         elseif ($align == 'right')
559                                 $align = 'left';
560                 }
561                 return $this->addTextWrap($xpos, $ypos, $len, $this->fontSize, $str, $align);
562         }
563
564         function TextCol($c, $n, $txt, $corr=0, $r=0)
565         {
566                 return $this->TextWrap($this->cols[$c], $this->row - $r, $this->cols[$n] - $this->cols[$c] + $corr, $txt, $this->aligns[$c]);
567         }
568
569         function TextCol2($c, $n, $txt, $corr=0, $r=0)
570         {
571                 return $this->TextWrap($this->cols2[$c], $this->row - $r, $this->cols2[$n] - $this->cols2[$c] + $corr, $txt, $this->aligns2[$c]);
572         }
573
574         function TextColLines($c, $n, $txt, $corr=0)
575         {
576                 $str = Explode("\n", $txt);
577                 for ($i = 0; $i < count($str); $i++)
578                 {
579                         $l = $str[$i];
580                         do
581                         {
582                                 $l = $this->TextCol($c, $n, $l, $corr);
583                                 $this->NewLine();
584                         }
585                         while ($l != '');
586                 }
587         }
588
589         function LineTo($from, $row, $to, $row2)
590         {
591                 Cpdf::line($from, $row, $to, $row2);
592         }
593
594         function Line($row, $height = 0)
595         {
596                 $this->setLineStyle($height + 1);
597                 Cpdf::line($this->pageWidth - $this->rightMargin, $row ,$this->leftMargin, $row);
598         }
599
600         function NewLine($l=1, $np=0)
601         {
602                 $this->row -= ($l * $this->lineHeight);
603                 if ($np > 0 &&  $this->row < $this->bottomMargin + ($np * $this->lineHeight))
604                         $this->Header();
605         }
606
607         function End($email=0, $subject=null, $myrow=null, $doctype = 0)
608         {
609                 global $go_debug, $path_to_root, $comp_path;
610
611                 // this is no good in IE so has been replaced, see down under
612                 // but good for debugging purposes in IE
613                 //session_write_close();
614
615                 if ($go_debug == 1)
616                 {
617                         $buf = $this->output(1);
618                         $len = strlen($buf);
619                         $pdfcode = $buf;
620                         $pdfcode = str_replace("\n", "\n<br>", htmlspecialchars($pdfcode));
621                         echo '<html><body>';
622                         echo trim($pdfcode);
623                         echo '</body></html>';
624                         //header("Content-Length: $len");
625                         //header("Content-Disposition: inline; filename=" . $this->filename);
626                         //header('Expires: 0');
627                         //header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
628                         //header('Pragma: public');
629
630                         //$this->pdf->stream();
631                 }
632                 else
633                 {
634                         $buf = $this->output();
635                         $len = strlen($buf);
636                         $dir =  $comp_path.'/'.user_company(). '/pdf_files';
637                         //save the file
638                         if (!file_exists($dir))
639                         {
640                                 mkdir ($dir,0777);
641                         }
642                         $fname = $dir . '/' . $this->filename;
643                         $fp = fopen($fname,'w');
644                         fwrite($fp,$buf);
645                         fclose($fp);
646                         if ($email == 1)
647                         {
648                                 $emailtype = true;
649                                 if ($this->currency != $myrow['curr_code'])
650                                 {
651                                         include("doctext2.inc");
652                                 }
653                                 else
654                                 {
655                                         include("doctext.inc");
656                                 }
657                                 require_once($path_to_root . "reporting/includes/class.mail.inc");
658                         $mail = new email($this->company['coy_name'], $this->company['email']);
659                         $from = $this->company['coy_name'] . " <" . $this->company['email'] . ">";
660                         $to = $myrow['DebtorName'] . " <" . $myrow['email'] . ">";
661                         $msg = $doc_Dear_Sirs . ",\n\n" . $doc_AttachedFile . " " . $subject .
662                                 "\n\n";
663                                 if ($myrow['dimension_id'] > 0 && $doctype == 10) // helper for payment links
664                                 {
665                                         if ($myrow['dimension_id'] == 1)
666                                         {
667                                                 $amt = number_format($myrow["ov_freight"] + $myrow["ov_gst"] +  $myrow["ov_amount"], user_price_dec());
668                                                 $txt = $doc_Payment_Link . " PayPal: ";
669                                                 $nn = urlencode($this->title . " " . $myrow['reference']);
670                                                 $url = "https://www.paypal.com/xclick/business=" . $this->company['email'] . "&item_name=" .
671                                                         $nn . "&amount=" . $amt . "&currency_code=" . $myrow['curr_code'];
672                                                 $msg .= $txt . $url . "\n\n";
673                                         }
674                                 }
675                         $msg .= $doc_Kindest_regards . "\n\n";
676                         $sender = $this->user . "\n" . $this->company['coy_name'];
677                         $mail->to($to);
678                         $mail->subject($subject);
679                         $mail->text($msg . $sender);
680                         $mail->attachment($fname);
681                         $ret = $mail->send();
682                         if (1 == 1) // just for fun and for debugging purposes!!
683                         {
684                                 $from = str_replace("<", "(", $from);
685                                 $from = str_replace(">", ")", $from);
686                                 $to = str_replace("<", "(", $to);
687                                 $to = str_replace(">", ")", $to);
688                                 $msg2 = "<br>From: " . $from;
689                                 $msg2 .= "<br>To: " . $to;
690                                 $msg2 .= "<br>Subject: " . $subject;
691                                 $msg2 .= "<br>Msg: " . nl2br($msg);
692                                 $msg2 .= nl2br($sender) . "<br>";
693                                 $msg2 .= "<br>Filepath: " . $fname . "<br>Filename: " . $this->filename . "<br>";
694                         }
695                         if ($ret)
696                                 $str = "<br>" . $this->title . " " . $myrow['reference'] . " " . _("sent to") . " ";
697                         else
698                                 $str = "<br>" . $this->title . " " . $myrow['reference'] . " " . _("NOT sent to") . " ";
699                         $msg2 .= $str . $myrow['DebtorName'] . " - " . $myrow['email'];
700                         echo "<html><body>";
701                         echo $msg2;
702                         echo "</body></html>";
703                         }
704                         else
705                         {
706                                 //echo '<html>
707                                 //              <head>
708                                 //                <SCRIPT LANGUAGE="JavaScript"><!--
709                                 //            function go_now () { window.location.href = "'.$fname.'"; }
710                                 //            //--></SCRIPT>
711                                 //        </head>
712                                 //        <body onLoad="go_now()"; >
713                                 //          <a href="'.$fname.'">click here</a> if you are not re-directed.
714                                 //        </body>
715                                 //    </html>';
716                 header('Content-type: application/pdf');
717                 header("Content-Disposition: inline; filename=$this->filename");
718                 header('Expires: 0');
719                 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
720                 header('Pragma: public');
721                 $this->stream();
722
723                         }
724                         // also have a look through the directory, and remove the files that are older than a week
725                         // rather want to save 'em
726                         /*if ($d = @opendir($dir)) {
727                                 while (($file = readdir($d)) !== false) {
728                                         // then check to see if this one is too old
729                                         $ftime = filemtime($dir.'/'.$file);
730                                         if (time()-$ftime > 3600*24*7){
731                                                 unlink($dir.'/'.$file);
732                                         }
733                                 }
734                                 closedir($d);
735                         }*/
736                 }
737         }
738 }
739
740 ?>