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