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