fdef22d2e763477de9a87dbd24214601c2bc274d
[fa-stable.git] / reporting / includes / pdf_report.inc
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 /*
13         TODO:
14         . add StartReport/EndReport handlers for better bulk report support, with
15         . email/printer destination option should be selected on class creation instead
16          of End()
17         . add/use setter function for Header2 parameters (currently passed globally)
18         . in report files pass already prepared options to SetCommonData() to avoid need for
19          selection inside FrontReport generic class.
20 */
21 include_once(dirname(__FILE__)."/class.pdf.inc");
22 include_once(dirname(__FILE__)."/printer_class.inc");
23 include_once($path_to_root . "/admin/db/company_db.inc");
24 include_once($path_to_root . "/admin/db/fiscalyears_db.inc");
25 include_once($path_to_root . "/admin/db/printers_db.inc");
26 include_once($path_to_root . "/config.php");
27
28 class FrontReport extends Cpdf
29 {
30         var $size;
31         var $company;
32         var $user;
33         var $host;
34         var $fiscal_year;
35         var $title;
36         var $filename;
37         var $pageWidth;
38         var $pageHeight;
39         var $topMargin;
40         var $bottomMargin;
41         var $leftMargin;
42         var $rightMargin;
43         var $endLine;
44         var $lineHeight;
45         //var $rtl;
46
47         var $row;
48         var $cols;
49         var $params;
50         var $headers;
51         var $aligns;
52         var $headers2;
53         var $aligns2;
54         var $cols2;
55         var $pageNumber;
56         var $fontSize;
57         var $oldFontSize;
58         var $currency;
59         var $companyLogoEnable;  // select whether to use a company logo graphic in some header templates
60         var $scaleLogoWidth;
61         var $footerEnable;  // select whether to print a page footer or not
62         var $footerText;  // store user-generated footer text
63         var $headerTmpl;  // store the name of the currently selected header template
64         var $tmplSize; // pdf header template size in pages
65
66         var $rep_id;
67         var $formData; // common data used for printing headers footers etc.
68
69         var $dest;      // destination: email or printer
70         
71         function FrontReport($title, $filename, $size = 'A4', $fontsize = 9, $orientation = 'P', $margins = NULL, $excelColWidthFactor = NULL)
72         {
73                 global $page_security;
74
75                 $this->rep_id = $_POST['REP_ID'];       // FIXME
76                 
77                 if (!$_SESSION["wa_current_user"]->can_access_page($page_security))
78                 {
79                         display_error(_("The security settings on your account do not permit you to print this report"));
80                         end_page();
81                         exit;
82                 }
83                 // Page margins - if user-specified, use those.  Otherwise, use defaults below.
84                 if (isset($margins))
85                 {
86                         $this->topMargin = $margins['top'];
87                         $this->bottomMargin = $margins['bottom'];
88                         $this->leftMargin = $margins['left'];
89                         $this->rightMargin = $margins['right'];
90                 }
91                 // Page orientation - P: portrait, L: landscape
92                 $orientation = strtoupper($orientation);
93                 // Page size name
94                 switch (strtoupper($size))
95                 {
96                         default:
97                   case 'A4':
98                           // Portrait
99                           if ($orientation == 'P')
100                           {
101                                   $this->pageWidth=595;
102                                   $this->pageHeight=842;
103                                   if (!isset($margins))
104                                   {
105                                           $this->topMargin=40;
106                                           $this->bottomMargin=30;
107                                           $this->leftMargin=40;
108                                           $this->rightMargin=30;
109                                   }
110                           }
111                           // Landscape
112                           else
113                           {
114                                   $this->pageWidth=842;
115                                   $this->pageHeight=595;
116                                   if (!isset($margins))
117                                   {
118                                           $this->topMargin=30;
119                                           $this->bottomMargin=30;
120                                           $this->leftMargin=40;
121                                           $this->rightMargin=30;
122                                   }
123                           }
124                           break;
125                    case 'A3':
126                           // Portrait
127                           if ($orientation == 'P')
128                           {
129                                   $this->pageWidth=842;
130                                   $this->pageHeight=1190;
131                                   if (!isset($margins))
132                                   {
133                                           $this->topMargin=50;
134                                           $this->bottomMargin=50;
135                                           $this->leftMargin=50;
136                                           $this->rightMargin=40;
137                                   }
138                           }
139                           // Landscape
140                           else
141                           {
142                                   $this->pageWidth=1190;
143                                   $this->pageHeight=842;
144                                   if (!isset($margins))
145                                   {
146                                           $this->topMargin=50;
147                                           $this->bottomMargin=50;
148                                           $this->leftMargin=50;
149                                           $this->rightMargin=40;
150                                   }
151                           }
152                           break;
153                    case 'LETTER':
154                           // Portrait
155                           if ($orientation == 'P')
156                           {
157                                   $this->pageWidth=612;
158                                   $this->pageHeight=792;
159                                   if (!isset($margins))
160                                   {
161                                           $this->topMargin=30;
162                                           $this->bottomMargin=30;
163                                           $this->leftMargin=30;
164                                           $this->rightMargin=25;
165                                   }
166                           }
167                           // Landscape
168                           else
169                           {
170                                   $this->pageWidth=792;
171                                   $this->pageHeight=612;
172                                   if (!isset($margins))
173                                   {
174                                           $this->topMargin=30;
175                                           $this->bottomMargin=30;
176                                           $this->leftMargin=30;
177                                           $this->rightMargin=25;
178                                   }
179                           }
180                           break;
181                    case 'LEGAL':
182                           // Portrait
183                           if ($orientation == 'P')
184                           {
185                                   $this->pageWidth=612;
186                                   $this->pageHeight=1008;
187                                   if (!isset($margins))
188                                   {
189                                           $this->topMargin=50;
190                                           $this->bottomMargin=40;
191                                           $this->leftMargin=30;
192                                           $this->rightMargin=25;
193                                   }
194                           }
195                           // Landscape
196                           else
197                           {
198                                   $this->pageWidth=1008;
199                                   $this->pageHeight=612;
200                                   if (!isset($margins))
201                                   {
202                                           $this->topMargin=50;
203                                           $this->bottomMargin=40;
204                                           $this->leftMargin=30;
205                                           $this->rightMargin=25;
206                                   }
207                           }
208                           break;
209                 }
210                 $this->size = array(0, 0, $this->pageWidth, $this->pageHeight);
211                 $this->title = $title;
212                 $this->filename = $filename.".pdf";
213                 $this->pageNumber = 0;
214                 $this->endLine = $this->pageWidth - $this->rightMargin;
215                 $this->lineHeight = 12;
216                 $this->fontSize = $fontsize;
217                 $this->oldFontSize = 0;
218                 $this->row = $this->pageHeight - $this->topMargin;
219                 $this->currency = '';
220                 $this->scaleLogoWidth = false; // if Logo, scale on width (else height).
221                 $this->SetHeaderType('Header'); // default
222
223                 $this->Cpdf($size, $_SESSION['language']->code, $orientation);
224         }
225         
226         /*
227          * Select the font and style to use for following output until
228          * it's changed again.
229          * 
230          * $style is either:
231          *   * a special case string (for backwards compatible with older code):
232          *     * bold
233          *     * italic
234          *   * or a case-insensitive string where each char represents a style choice
235          *     and you can use more than one or none at all.  Possible choices:
236          *      * empty string: regular
237      *      * B: bold
238      *      * I: italic
239      *      * U: underline
240      *      * D: line trough (aka "strike through")
241          * $fontname should be a standard PDF font (like 'times', 'helvetica' or 'courier')
242          *   or one that's been installed on your system (see TCPDF docs for details).
243          *   An empty string can also be used which will retain the font currently in use if
244          *   you just want to change the style.
245          */
246         function Font($style = '', $fontname = '')
247         {
248                 $this->selectFont($fontname, $style);
249         }
250
251         function Info($params, $cols, $headers, $aligns,
252                 $cols2 = null, $headers2 = null, $aligns2 = null,
253                 $companylogoenable = false, $footerenable = false, $footertext = '')
254         {
255                 global $app_title, $version, $power_by, $power_url;
256
257                 $this->addinfo('Title', $this->title);
258                 $this->addinfo('Subject', $this->title);
259                 $this->addinfo('Author', $app_title . ' ' . $version);
260                 $this->addinfo('Creator',$power_by . ' - ' . $power_url);
261                 $year = get_current_fiscalyear();
262                 if ($year['closed'] == 0)
263                         $how = _("Active");
264                 else
265                         $how = _("Closed");
266                 $this->fiscal_year = sql2date($year['begin']) . " - " . sql2date($year['end']) . "  " . "(" . $how . ")";
267                 $this->company = get_company_prefs();
268                 $this->user = $_SESSION["wa_current_user"]->name;
269                 $this->host = $_SERVER['SERVER_NAME'];
270                 $this->params = $params;
271                 $this->cols = $cols;
272                 for ($i = 0; $i < count($this->cols); $i++)
273                         $this->cols[$i] += $this->leftMargin;
274                 $this->headers = $headers;
275                 $this->aligns = $aligns;
276                 $this->cols2 = $cols2;
277                 if ($this->cols2 != null)
278                 {
279                         for ($i = 0; $i < count($this->cols2); $i++)
280                                 $this->cols2[$i] += $this->leftMargin;
281                 }
282                 $this->headers2 = $headers2;
283                 $this->aligns2 = $aligns2;
284
285                 // Set whether to display company logo in some header templates
286                 $this->companyLogoEnable = $companylogoenable;
287                 
288                 // Store footer settings
289                 $this->footerEnable = $footerenable;
290                 $this->footerText = $footertext;        
291         }
292         //
293         //      Header for listings
294         //
295         function Header()
296         {
297                 $companyCol = $this->endLine - 150;
298                 $titleCol = $this->leftMargin + 100;
299
300                 $this->row = $this->pageHeight - $this->topMargin;
301
302                 $this->SetDrawColor(128, 128, 128);
303                 $this->Line($this->row + 5, 1);
304
305                 $this->NewLine();
306
307                 $this->fontSize += 4;
308                 $this->Font('bold');
309                 $this->Text($this->leftMargin, $this->title, $companyCol);
310                 $this->Font();
311                 $this->fontSize -= 4;
312                 $this->Text($companyCol, $this->company['coy_name']);
313                 $this->row -= ($this->lineHeight + 4);
314
315                 $str = _("Print Out Date") . ':';
316                 $this->Text($this->leftMargin, $str, $titleCol);
317                 $str = Today() . '   ' . Now();
318                 if ($this->company['time_zone'])
319                         $str .= ' ' . date('O') . ' GMT';
320                 $this->Text($titleCol, $str, $companyCol);
321                 $this->Text($companyCol, $this->host);
322
323                 $this->NewLine();
324                 $str = _("Fiscal Year") . ':';
325                 $this->Text($this->leftMargin, $str, $titleCol);
326                 $str = $this->fiscal_year;
327                 $this->Text($titleCol, $str, $companyCol);
328                 $this->Text($companyCol, $this->user);
329                 for ($i = 1; $i < count($this->params); $i++)
330                 {
331                         if ($this->params[$i]['from'] != '')
332                         {
333                                 $this->NewLine();
334                                 $str = $this->params[$i]['text'] . ':';
335                                 $this->Text($this->leftMargin, $str, $titleCol);
336                                 $str = $this->params[$i]['from'];
337                                 if ($this->params[$i]['to'] != '')
338                                         $str .= " - " . $this->params[$i]['to'];
339                                 $this->Text($titleCol, $str, $companyCol);
340                         }
341                 }
342                 if ($this->params[0] != '') // Comments
343                 {
344                         $this->NewLine();
345                         $str = _("Comments") . ':';
346                         $this->Text($this->leftMargin, $str, $titleCol);
347                         $this->Font('bold');
348                         $this->Text($titleCol, $this->params[0], $this->endLine - 35);
349                         $this->Font();
350                 }
351                 $str = _("Page") . ' ' . $this->pageNumber;
352                 $this->Text($this->endLine - 38, $str);
353                 $this->Line($this->row - 5, 1);
354
355                 $this->row -= ($this->lineHeight + 6);
356                 $this->Font('italic');
357                 if ($this->headers2 != null)
358                 {
359                         $count = count($this->headers2);
360                         for ($i = 0; $i < $count; $i++)
361                                 $this->TextCol2($i, $i + 1,     $this->headers2[$i]);
362                         $this->NewLine();
363                 }
364                 $count = count($this->headers);
365                 for ($i = 0; $i < $count; $i++)
366                         $this->TextCol($i, $i + 1, $this->headers[$i]);
367                 $this->Font();
368                 $this->Line($this->row - 5, 1);
369
370                 $this->NewLine(2);
371         }
372         /*
373                 Transition function 
374         */
375         function SetCommonData($myrow, $branch, $sales_order, $bankaccount, $doctype)
376         {
377 //              $this->formData = array_merge($myrow, $branch, $sales_order, 
378 //                      $bank_account, array('doctype' => $doctype));
379
380                 $this->formData = array();
381                 $datnames = array( 
382                 'myrow' => array('ord_date', 'date_', 'tran_date', 
383                         'order_no','reference', 'id', 'trans_no', 'name', 'location_name',
384                         'delivery_address', 'supp_name', 'address',
385                         'DebtorName', 'supp_account_no', 'wo_ref', 'debtor_ref','type', 'trans_no', 
386                         'StockItemName', 'tax_id', 'order_', 'delivery_date', 'units_issued',
387                         'due_date', 'required_by', 'payment_terms', 'curr_code',
388                         'ov_freight', 'ov_gst', 'ov_amount', 'rep_lang'),
389                 'branch' => array('br_address', 'br_name', 'salesman', 'disable_branch', 'rep_lang'),
390                 'sales_order' => array('deliver_to', 'delivery_address', 'customer_ref'),
391                 'bankaccount' => array('bank_name', 'bank_account_number')
392                 );
393
394                 foreach($datnames as $var => $fields) {
395                         if (isset($$var)) {
396                                 foreach($fields as $locname) {
397                                         if (isset(${$var}[$locname]) && (${$var}[$locname]!==null)) {
398                                                 $this->formData[$locname] = ${$var}[$locname];
399                                         }
400                                 }
401                         }
402                 }
403                 $this->formData['doctype'] = $doctype;
404                 error_log('cust:'.var_export($myrow, true));
405                 error_log('branch:'.var_export($branch, true));
406         }
407         /*
408                 Set header handler
409         */
410         function SetHeaderType($name) {
411                 $this->headerTmpl = $name;
412         }
413         /*
414                 Header for sales/purchase documents
415         */
416         function Header2()
417         {
418                 global $path_to_root, $print_as_quote,
419                         $print_invoice_no, $packing_slip, $dflt_lang; // FIXME should be passed as params
420
421                 $doctype = $this->formData['doctype'];
422                 $header2type = true;
423                 
424                 $this->SetLang(@$this->formData['rep_lang'] ? $this->formData['rep_lang'] : $dflt_lang);
425
426                  // leave layout files names without path to enable including
427                  // modified versions from company/x/reporting directory
428                 include("includes/doctext.inc");
429                 include("includes/header2.inc");
430
431                 $this->row = $temp;
432         }
433
434         // Alternate header style which also supports a simple footer
435         function Header3()
436         {
437                 // Turn off cell padding for the main report header, restoring the current setting later
438                 $oldcMargin = $this->cMargin;
439                 $this->SetCellPadding(0);
440
441                 // Set some constants which control header item layout
442                 // only set them once or the PHP interpreter gets angry
443                 if ($this->pageNumber == 1)
444                 {
445                         define('COMPANY_WIDTH', 150);
446                         define('LOGO_HEIGHT', 50);
447                         define('LOGO_Y_POS_ADJ_FACTOR', 0.74);
448                         define('LABEL_WIDTH', 80);
449                         define('PAGE_NUM_WIDTH', 60);
450                         define('TITLE_FONT_SIZE', 14);
451                         define('HEADER1_FONT_SIZE', 10);
452                         define('HEADER2_FONT_SIZE', 9);
453                         define('FOOTER_FONT_SIZE', 10);
454                         define('FOOTER_MARGIN', 4);
455                 }
456                 // Set some variables which control header item layout
457                 $companyCol = $this->endLine - COMPANY_WIDTH;
458                 $headerFieldCol = $this->leftMargin + LABEL_WIDTH;
459                 $pageNumCol = $this->endLine - PAGE_NUM_WIDTH;
460                 $footerCol = $this->leftMargin + PAGE_NUM_WIDTH; 
461                 $footerRow = $this->bottomMargin - FOOTER_MARGIN;
462
463                 // Calling this function generates a new PDF page after the first instance
464 //              {
465 //                      // TODO: experimenting with line drawing to highlight current period
466 //                      $this->SetLineWidth(1);
467 //                      $this->LineTo($this->cols[3], 33, $this->cols[3], 534);
468 //                      $this->LineTo($this->cols[4], 33, $this->cols[4], 534);
469 //                      $this->SetLineWidth(0.1);
470                         
471 //                      $this->newPage();
472 //              }
473                 $this->row = $this->pageHeight - $this->topMargin;
474
475                 // Set the color of dividing lines we'll draw
476                 $oldDrawColor = $this->GetDrawColor();
477                 $this->SetDrawColor(128, 128, 128);
478
479                 // Tell TCPDF that we want to use its alias system to track the total number of pages
480                 $this->AliasNbPages();
481                 
482                 // Footer
483                 if ($this->footerEnable)
484                 {
485                         $this->Line($footerRow, 1);
486                         $prevFontSize = $this->fontSize;
487                         $this->fontSize = FOOTER_FONT_SIZE;
488                         $this->TextWrap($footerCol, $footerRow - ($this->fontSize + 1),
489                                 $pageNumCol - $footerCol, $this->footerText, $align = 'center',
490                                 $border = 0, $fill = 0, $link = NULL, $stretch = 1);
491                         $this->TextWrap($pageNumCol, $footerRow - ($this->fontSize + 1),
492                                 PAGE_NUM_WIDTH, _("Page") . ' ' . $this->pageNumber . '/' . $this->getAliasNbPages(),
493                                 $align = 'right', $border = 0, $fill = 0, $link = NULL, $stretch = 1);
494                         $this->fontSize = $prevFontSize;
495                 }
496
497                 //
498                 // Header
499                 //
500                 
501                 // Print gray line across the page
502                 $this->Line($this->row + 8, 1);
503
504                 $this->NewLine();
505
506                 // Print the report title nice and big
507                 $oldFontSize = $this->fontSize;
508                 $this->fontSize = TITLE_FONT_SIZE;
509                 $this->Font('B');
510                 $this->Text($this->leftMargin, $this->title, $companyCol);
511                 $this->fontSize = HEADER1_FONT_SIZE;
512
513                 // Print company logo if present and requested, or else just print company name
514                 if ($this->companyLogoEnable && ($this->company['coy_logo'] != ''))
515                 {
516                         // Build a string specifying the location of the company logo file
517                         $logo = company_path() . "/images/" . $this->company['coy_logo'];
518
519                         // Width being zero means that the image will be scaled to the specified height
520                         // keeping its aspect ratio intact.
521                         if ($this->scaleLogoWidth)
522                                 $this->AddImage($logo, $companyCol, $this->row, COMPANY_WIDTH, 0);
523                         else    
524                                 $this->AddImage($logo, $companyCol, $this->row - (LOGO_HEIGHT * LOGO_Y_POS_ADJ_FACTOR), 0, LOGO_HEIGHT);
525                 }
526                 else
527                         $this->Text($companyCol, $this->company['coy_name']);
528
529                 // Dimension 1 - optional
530                 // - only print if available and not blank
531                 if (count($this->params) > 3)
532                         if ($this->params[3]['from'] != '')
533                         {
534                                 $this->NewLine(1, 0, $this->fontSize + 2);
535                                 $str = $this->params[3]['text'] . ':';
536                                 $this->Text($this->leftMargin, $str, $headerFieldCol);
537                                 $str = $this->params[3]['from'];
538                                 $this->Text($headerFieldCol, $str, $companyCol);
539                         }
540
541                 // Dimension 2 - optional
542                 // - only print if available and not blank
543                 if (count($this->params) > 4)
544                         if ($this->params[4]['from'] != '')
545                         {
546                                 $this->NewLine(1, 0, $this->fontSize + 2);
547                                 $str = $this->params[4]['text'] . ':';
548                                 $this->Text($this->leftMargin, $str, $headerFieldCol);
549                                 $str = $this->params[4]['from'];
550                                 $this->Text($headerFieldCol, $str, $companyCol);
551                         }
552
553                 // Tags - optional
554                 // if present, it's an array of tag names
555                 if (count($this->params) > 5)
556                         if ($this->params[5]['from'] != '')
557                         {
558                                 $this->NewLine(1, 0, $this->fontSize + 2);
559                                 $str = $this->params[5]['text'] . ':';
560                                 $this->Text($this->leftMargin, $str, $headerFieldCol);
561                                 $str = '';
562                                 for ($i = 0; $i < count($this->params[5]['from']); $i++)
563                                 {
564                                         if($i != 0)
565                                                 $str .= ', ';
566                                         $str .= $this->params[5]['from'][$i];
567                                 }
568                                 $this->Text($headerFieldCol, $str, $companyCol);
569                         }
570
571                 // Report Date - time period covered
572                 // - can specify a range, or just the end date (and the report contents
573                 //   should make it obvious what the beginning date is)
574                 $this->NewLine(1, 0, $this->fontSize + 2);
575                 $str = _("Report Period") . ':';
576                 $this->Text($this->leftMargin, $str, $headerFieldCol);
577                 $str = '';
578                 if (isset($this->params[1]['from']) && $this->params[1]['from'] != '')
579                         $str = $this->params[1]['from'] . ' - ';
580                 $str .= $this->params[1]['to'];
581                 $this->Text($headerFieldCol, $str, $companyCol);
582
583                 // Turn off Bold
584                 $this->Font();
585                 
586                 $this->NewLine(1, 0, $this->fontSize + 1);
587
588                 // Make the remaining report headings a little less important
589                 $this->fontSize = HEADER2_FONT_SIZE;
590
591                 // Timestamp of when this copy of the report was generated
592                 $str = _("Generated At") . ':';
593                 $this->Text($this->leftMargin, $str, $headerFieldCol);
594                 $str = Today() . '   ' . Now();
595                 if ($this->company['time_zone'])
596                         $str .= ' ' . date('O') . ' GMT';
597                 $this->Text($headerFieldCol, $str, $companyCol);
598
599                 // Name of the user that generated this copy of the report
600                 $this->NewLine(1, 0, $this->fontSize + 1);
601                 $str = _("Generated By") . ':';
602                 $this->Text($this->leftMargin, $str, $headerFieldCol);
603                 $str = $this->user;
604                 $this->Text($headerFieldCol, $str, $companyCol);
605
606                 // Display any user-generated comments for this copy of the report
607                 if ($this->params[0] != '') // Comments
608                 {
609                         $this->NewLine(1, 0, $this->fontSize + 1);
610                         $str = _("Comments") . ':';
611                         $this->Text($this->leftMargin, $str, $headerFieldCol);
612                         $this->Font('B');
613                         $this->Text($headerFieldCol, $this->params[0], $companyCol, 0, 0, 'left', 0, 0, $link=NULL, 1);
614                         $this->Font();
615                 }
616
617                 // Add page numbering to header if footer is turned off
618                 if (!$this->footerEnable)
619                 {
620                         $str = _("Page") . ' ' . $this->pageNumber . '/' . $this->getAliasNbPages();
621                         $this->Text($pageNumCol, $str, 0, 0, 0, 'right', 0, 0, NULL, 1);
622                 }
623                 
624                 // Print gray line across the page
625                 $this->Line($this->row - 5, 1);
626
627                 // Restore font size to user-defined size
628                 $this->fontSize = $oldFontSize;
629
630                 // restore user-specified cell padding for column headers
631                 $this->SetCellPadding($oldcMargin);
632
633                 // scoot down the page a bit
634                 $oldLineHeight = $this->lineHeight;
635                 $this->lineHeight = $this->fontSize + 1;
636                 $this->row -= ($this->lineHeight + 6);
637                 $this->lineHeight = $oldLineHeight;
638
639                 // Print the column headers!
640                 $this->Font('I');
641                 if ($this->headers2 != null)
642                 {
643                         $count = count($this->headers2);
644                         for ($i = 0; $i < $count; $i++)
645                                 $this->TextCol2($i, $i + 1,     $this->headers2[$i], $corr=0, $r=0, $border=0, $fill=0, $link=NULL, $stretch=1);
646                         $this->NewLine();
647                 }
648                 $count = count($this->headers);
649                 for ($i = 0; $i < $count; $i++)
650                         $this->TextCol($i, $i + 1, $this->headers[$i], $corr=0, $r=0, $border=0, $fill=0, $link=NULL, $stretch=1);
651                 $this->Font();
652
653                 $this->NewLine(2);
654
655                 // restore user-specified draw color
656                 $this->SetDrawColor($oldDrawColor[0], $oldDrawColor[1], $oldDrawColor[2]);              
657         }
658
659         /**
660          * Format a numeric string date into something nicer looking.
661          *
662          * @param string $date Date string to be formatted.
663          * @param int $input_format Format of the input string.  Possible values are:<ul><li>0: user's default (default)</li></ul>
664          * @param int $output_format Format of the output string.  Possible values are:<ul><li>0: Month (word) Day (numeric), 4-digit Year - Example: January 1, 2000 (default)</li><li>1: Month 4-digit Year - Example: January 2000</li><li>2: Month Abbreviation 4-digit Year - Example: Jan 2000</li></ul>
665          * @access public
666          */
667         function DatePrettyPrint($date, $input_format = 0, $output_format = 0)
668         {
669                 if ($date != '')
670                 {
671                         $date = date2sql($date);
672                         $year = (int) (substr($date, 0, 4));
673                         $month = (int) (substr($date, 5, 2));
674                         $day = (int) (substr($date, 8, 2));
675                         if ($output_format == 0)
676                                 return(date('F j, Y', mktime(12, 0, 0, $month, $day, $year)));
677                         elseif ($output_format == 1)
678                                 return(date('F Y', mktime(12, 0, 0, $month, $day, $year)));
679                         elseif ($output_format == 2)
680                                 return(date('M Y', mktime(12, 0, 0, $month, $day, $year)));
681                 }
682                 else
683                         return $date;
684         }
685
686         function AddImage($logo, $x, $y, $w, $h)
687         {
688                 if (strpos($logo, ".png") || strpos($logo, ".PNG"))
689                         $this->addPngFromFile($logo, $x, $y, $w, $h);
690                 else
691                         $this->addJpegFromFile($logo, $x, $y, $w, $h);
692         }
693
694         // Get current draw color setting from TCPDF object; returns array of RGB numbers
695         function GetDrawColor()
696         {
697                 // Convert the TCPDF stored DrawColor string into an array of strings
698                 $colorFields = explode(' ', $this->DrawColor);
699
700                 // Test last value: G == grayscale, single number; RG == RGB, 3 numbers
701                 if ($colorFields[count($colorFields) - 1] == 'G')
702                         // Convert a grayscale string value to the equivalent RGB value
703                         $drawColor = array((float) $colorFields[0], (float) $colorFields[0], (float) $colorFields[0]);
704                 else
705                         // Convert RGB string values to the a numeric array
706                         $drawColor = array((float) $colorFields[0], (float) $colorFields[1], (float) $colorFields[2]);
707                 
708                 return $drawColor;
709         }
710         
711         function SetDrawColor($r, $g, $b)
712         {
713                 parent::SetDrawColor($r, $g, $b);
714         }
715
716         function SetTextColor($r, $g, $b)
717         {
718                 parent::SetTextColor($r, $g, $b);
719         }
720
721         /**
722      * Set the fill color for table cells.
723      * @see reporting/includes/TCPDF#SetFillColor($col1, $col2, $col3, $col4)
724      */
725         function SetFillColor($r, $g, $b)
726         {
727                 parent::SetFillColor($r, $g, $b);
728         }
729
730         // Get current cell padding setting from TCPDF object
731         function GetCellPadding()
732         {
733                 return $this->cMargin;
734         }
735
736         // Set desired cell padding (aka "cell margin")
737         // Seems to be just left and right margins...
738         function SetCellPadding($pad)
739         {
740                 parent::SetCellPadding($pad);
741         }
742         
743         function Text($c, $txt, $n=0, $corr=0, $r=0, $align='left', $border=0, $fill=0, $link=NULL, $stretch=1)
744         {
745                 if ($n == 0)
746                         $n = $this->pageWidth - $this->rightMargin;
747
748                 return $this->TextWrap($c, $this->row - $r, $n - $c + $corr, $txt, $align, $border, $fill, $link, $stretch);
749         }
750
751         function TextWrap($xpos, $ypos, $len, $str, $align = 'left', $border = 0, $fill = 0, $link = NULL, $stretch = 1)
752         {
753                 if ($this->fontSize != $this->oldFontSize)
754                 {
755                         $this->SetFontSize($this->fontSize);
756                         $this->oldFontSize = $this->fontSize;
757                 }
758                 return $this->addTextWrap($xpos, $ypos, $len, $this->fontSize, $str, $align, $border, $fill, $link, $stretch);
759         }
760
761         function TextCol($c, $n, $txt, $corr=0, $r=0, $border=0, $fill=0, $link=NULL, $stretch=1)
762         {
763                 return $this->TextWrap($this->cols[$c], $this->row - $r, $this->cols[$n] - $this->cols[$c] + $corr, $txt, $this->aligns[$c], $border, $fill, $link, $stretch);
764         }
765         
766         function AmountCol($c, $n, $txt, $dec=0, $corr=0, $r=0, $border=0, $fill=0, $link=NULL, $stretch=1, $color_red=false)
767         {
768                 if ($color_red && $txt < 0)
769                         $this->SetTextColor(255, 0, 0);
770                 $ret = $this->TextCol($c, $n, number_format2($txt, $dec), $corr, $r, $border, $fill, $link, $stretch);
771                 if ($color_red && $txt < 0)
772                         $this->SetTextColor(0, 0, 0);
773                 return $ret;    
774         }
775
776         function AmountCol2($c, $n, $txt, $dec=0, $corr=0, $r=0, $border=0, $fill=0, $link=NULL, $stretch=1, $color_red=false, $amount_locale = 'en_US.UTF-8', $amount_format = '%(!.2n')
777         {
778                 setlocale(LC_MONETARY, $amount_locale);
779                 if ($color_red && $txt < 0)
780                         $this->SetTextColor(255, 0, 0);
781                 $ret = $this->TextCol($c, $n, money_format($amount_format, $txt), $corr, $r, $border, $fill, $link, $stretch);
782                 if ($color_red && $txt < 0)
783                         $this->SetTextColor(0, 0, 0);
784                 return $ret;    
785         }
786         
787         function DateCol($c, $n, $txt, $conv=false, $corr=0, $r=0, $border=0, $fill=0, $link=NULL, $stretch=1)
788         {
789                 if ($conv)
790                         $txt = sql2date($txt);
791                 return $this->TextCol($c, $n, $txt, $corr, $r, $border, $fill, $link, $stretch);
792         }
793
794         function TextCol2($c, $n, $txt, $corr=0, $r=0, $border=0, $fill=0, $link=NULL, $stretch=1)
795         {
796                 return $this->TextWrap($this->cols2[$c], $this->row - $r, $this->cols2[$n] - $this->cols2[$c] + $corr, $txt, $this->aligns2[$c], $border, $fill, $link, $stretch);
797         }
798
799         function TextColLines($c, $n, $txt, $corr=0, $r=0, $border=0, $fill=0, $link=NULL, $stretch=1)
800         {
801                 $this->row -= $r;
802                 $this->TextWrapLines($this->cols[$c], $this->cols[$n] - $this->cols[$c] + $corr, $txt, $this->aligns[$c], $border, $fill, $link, $stretch);
803         }
804
805         function TextWrapLines($c, $width, $txt, $align='left', $border=0, $fill=0, $link=NULL, $stretch=1)
806         {
807                 $str = Explode("\n", $txt);
808                 for ($i = 0; $i < count($str); $i++)
809                 {
810                         $l = $str[$i];
811                         do
812                         {
813                                 $l = $this->TextWrap($c, $this->row , $width, $l, $align, $border, $fill, $link, $stretch);
814                                 $this->row -= $this->lineHeight;
815                         }
816                         while ($l != '');
817                 }
818         }
819
820         /**
821          * Expose the underlying calcTextWrap() function in this API.
822          */
823         function TextWrapCalc($txt, $width, $spacebreak=false)
824         {
825                 return $this->calcTextWrap($txt, $width, $spacebreak);
826         }
827         
828         /**
829          * Sets the line drawing style.
830          * 
831          * Takes an associative array as arg so you don't need to specify all values.
832          * 
833          * Array keys:
834          * width (float) - the thickness of the line in user units
835          * cap (string) - the type of cap to put on the line, values can be 'butt','round','square'
836          *    where the diffference between 'square' and 'butt' is that 'square' projects a flat end past the
837          *    end of the line.
838          * join (string) - can be 'miter', 'round', 'bevel'
839          * dash (mixed) - Dash pattern. Is 0 (without dash) or string with series of length values, which are the
840          *        lengths of the on and off dashes. For example: "2" represents 2 on, 2 off, 2 on, 2 off, ...;
841          *        "2,1" is 2 on, 1 off, 2 on, 1 off, ... 
842          * phase (integer) - a modifier on the dash pattern which is used to shift the point at which the pattern starts.
843          * color (array) - draw color.  Format: array(GREY), or array(R,G,B) or array(C,M,Y,K).
844          */
845         function SetLineStyle($style)
846         {
847                 parent::SetLineStyle($style);
848         }
849
850         /**
851          * Sets the line drawing width.
852          */
853         function SetLineWidth($width)
854         {
855                 parent::SetLineWidth($width);
856         }
857         
858         function LineTo($from, $row, $to, $row2)
859         {
860                 parent::line($from, $row, $to, $row2);
861         }
862
863         function Line($row, $height = 0)
864         {
865                 $oldLineWidth = $this->GetLineWidth();
866                 $this->SetLineWidth($height + 1);
867                 parent::line($this->pageWidth - $this->rightMargin, $row ,$this->leftMargin, $row);
868                 $this->SetLineWidth($oldLineWidth);
869         }
870
871         /**
872         * Underlines the contents of a cell, but not the cell padding area.
873         * Primarily useful for the last line before a "totals" line.
874         * @param int $c Column number to underline.
875         * @param int $r Print the underline(s) this number of rows below the current position.  Can be negative in order to go up.
876         * @param int $type Type of underlining to draw.  Possible values are:<ul><li>1: single underline (default)</li><li>2: double underline</li></ul>
877         * @param int $linewidth Thickness of the line to draw.  Default value of zero will use the current line width defined for this document.
878         * @param array $style Line style. Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty array).
879         * @access public
880         * @see SetLineWidth(), SetDrawColor(), SetLineStyle()
881         */
882         function UnderlineCell($c, $r = 0, $type = 1, $linewidth = 0, $style = array())
883         {
884                 // If line width was specified, save current setting so we can reset it
885                 if ($linewidth != 0)
886                 {
887                         $oldLineWidth = $this->GetLineWidth();
888                         $this->SetLineWidth($linewidth);
889                 }
890
891                 // Figure out how far down to move the line based on current font size
892                 // Calculate this because printing underline directly at $this->row goes on top
893                 // of the parts of characters that "hang down", like the bottom of commas &
894                 // lowercase letter 'g', etc.
895                 if ($this->fontSize < 10)
896                         $y_adj = 2;
897                 else
898                         $y_adj = 3; 
899                 parent::line($this->cols[$c] + $this->cMargin, $this->row - $r - $y_adj, $this->cols[$c + 1] - $this->cMargin, $this->row - $r - $y_adj, $style);
900
901                 // Double underline, far enough below the first underline so as not to overlap
902                 // the first underline (depends on current line thickness (aka "line width")
903                 if ($type == 2)
904                         parent::line($this->cols[$c] + $this->cMargin, $this->row - $r - $y_adj - ($this->GetLineWidth() + 2), $this->cols[$c + 1] - $this->cMargin, $this->row - $r - $y_adj - ($this->GetLineWidth() + 2), $style);
905
906                 // If line width was specified, reset it back to the original setting
907                 if ($linewidth != 0)
908                         $this->SetLineWidth($oldLineWidth);
909         }
910         
911         function NewLine($l=1, $np=0, $h = NULL)
912         {
913                 // If the line height wasn't specified, use the current setting
914                 if ($h == NULL)
915                         $h = $this->lineHeight;
916
917                 // Move one line down the page
918                 $this->row -= ($l * $h);
919
920                 // Check to see if we're at the bottom and should insert a page break
921                 if ($this->row < $this->bottomMargin + ($np * $h))
922                         $this->NewPage();
923         }
924
925         function NewPage() 
926         {
927                 if ($this->pageNumber==0)
928                 {
929                         // check if there is pdf header template for this report
930                         // and set if it is found
931                         $tmpl_pdf = find_custom_file("/reporting/forms/".$this->headerTmpl.".pdf");
932                         if ($tmpl_pdf) {
933                                 $this->tmplSize = $this->setSourceFile($tmpl_pdf);
934                         }
935                 }
936
937                 $this->pageNumber++;
938                 parent::newPage();
939
940                 if ($this->tmplSize) {
941                         $this->row = $this->pageHeight - $this->topMargin; // reset row
942                         $id = $this->importPage(min($this->pageNumber, $this->tmplSize));
943                         $this->useTemplate($id);
944                 }
945
946                 // include related php file if any
947                 $tmpl_php = find_custom_file("/reporting/forms/".$this->headerTmpl.".php");
948                 if ($tmpl_php) {
949                         include($tmpl_php);
950                 }
951
952                 if (method_exists($this, $this->headerTmpl))    // draw predefined page layout if any
953                         $this->{$this->headerTmpl}();
954         }
955
956         function End($email=0, $subject=null, $myrow=null, $doctype = 0)
957         {
958                 global $pdf_debug, $path_to_root;
959
960                 if ($pdf_debug == 1)
961                 {
962                         $pdfcode = $this->Output('','S');
963                         $pdfcode = str_replace("\n", "\n<br>", htmlspecialchars($pdfcode));
964                         echo '<html><body>';
965                         echo trim($pdfcode);
966                         echo '</body></html>';
967                 }
968                 else
969                 {
970
971                         $dir =  company_path(). '/pdf_files';
972                         //save the file
973                         if (!file_exists($dir))
974                         {
975                                 mkdir ($dir,0777);
976                         }
977                         // do not use standard filenames or your sensitive company data 
978                         // are world readable
979                         if ($email == 1)
980                                 $fname = $dir.'/'.$this->filename;
981                         else    
982                                 $fname = $dir.'/'.uniqid('').'.pdf';
983                         $this->Output($fname, 'F');
984
985                         if ($email == 1)
986                         {
987                                 $emailtype = true;
988                                 $this->SetLang($this->formData['rep_lang']);
989                                 include("includes/doctext.inc");
990
991                                 require_once($path_to_root . "/reporting/includes/class.mail.inc");
992                         $mail = new email(str_replace(",", "", $this->company['coy_name']), $this->company['email']);
993                                 if (!isset($myrow['email']) || $myrow['email'] == '') 
994                                         $myrow['email'] = isset($myrow['contact_email']) ? $myrow['contact_email'] : '';
995                         $to = str_replace(",", "", $myrow['DebtorName']) . " <" . $myrow['email'] . ">";
996                         $msg = $doc_Dear_Sirs . " " . $myrow['DebtorName'] . ",\n\n" . $doc_AttachedFile . " " . $subject .
997                                 "\n\n";
998                                 if (isset($myrow['dimension_id']) && $myrow['dimension_id'] > 0 && $doctype == ST_SALESINVOICE) // helper for payment links
999                                 {
1000                                         if ($myrow['dimension_id'] == 1)
1001                                         {
1002                                                 $amt = number_format($myrow["ov_freight"] + $myrow["ov_gst"] +  $myrow["ov_amount"], user_price_dec());
1003                                                 $txt = $doc_Payment_Link . " PayPal: ";
1004                                                 $nn = urlencode($this->title . " " . $myrow['reference']);
1005                                                 $url = "https://www.paypal.com/xclick/business=" . $this->company['email'] . "&item_name=" .
1006                                                         $nn . "&amount=" . $amt . "&currency_code=" . $myrow['curr_code'];
1007                                                 $msg .= $txt . $url . "\n\n";
1008                                         }
1009                                 }
1010                         $msg .= $doc_Kindest_regards . "\n\n";
1011                         $sender = $this->user . "\n" . $this->company['coy_name'] . "\n" . $this->company['postal_address'] . "\n" . $this->company['email'] . "\n" . $this->company['phone'];
1012                         $mail->to($to);
1013                         $mail->subject($subject);
1014                         $mail->text($msg . $sender);
1015                         $mail->attachment($fname);
1016                         $ret = $mail->send();
1017                                 if (!$ret)
1018                                         display_error(_("Sending document by email failed"));
1019                                 else
1020                                         display_notification($this->title . " " . $myrow['reference'] . " " 
1021                                                 . _("has been sent by email."));
1022                                 unlink($fname);         
1023                         }
1024                         else
1025                         {
1026                                 $printer = get_report_printer(user_print_profile(), $this->rep_id);
1027                                 if ($printer == false) {
1028                                         if (in_ajax()) {
1029                                                 global $Ajax;
1030
1031                                                 if (user_rep_popup()) 
1032                                                         $Ajax->popup($fname); // when embeded pdf viewer used
1033                                                 else
1034                                                         $Ajax->redirect($fname); // otherwise use faster method
1035                                         } else {
1036                                                 header('Content-type: application/pdf');
1037                                                 header('Content-Disposition: inline; filename='.$this->filename);
1038                                                 header('Expires: 0');
1039                                                 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
1040                                                 header('Pragma: public');
1041 //                                              $this->Stream(basename($fname));
1042                                                 $this->Stream($this->filename);
1043                                         }
1044                                 } else { // send report to network printer
1045                                         $prn = new remote_printer($printer['queue'],$printer['host'],
1046                                                 $printer['port'], $printer['timeout']);
1047                                         $error = $prn->print_file($fname);
1048                                         if ($error)
1049                                                 display_error($error);
1050                                         else
1051                                                 display_notification(_('Report has been sent to network printer ').$printer['name']);
1052                                 }
1053                         }
1054                         // first have a look through the directory, 
1055                         // and remove old temporary pdfs
1056                         if ($d = @opendir($dir)) {
1057                                 while (($file = readdir($d)) !== false) {
1058                                         if (!is_file($dir.'/'.$file) || $file == 'index.php') continue;
1059                                 // then check to see if this one is too old
1060                                         $ftime = filemtime($dir.'/'.$file);
1061                                  // seems 3 min is enough for any report download, isn't it?
1062                                         if (time()-$ftime > 180){
1063                                                 unlink($dir.'/'.$file);
1064                                         }
1065                                 }
1066                                 closedir($d);
1067                         }
1068                 }
1069         }
1070 }
1071
1072 ?>