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 ***********************************************************************/
14 //include_once($path_to_root . "reporting/includes/class.pdf.inc");
15 include_once(dirname(__FILE__)."/class.pdf.inc");
16 include_once(dirname(__FILE__)."/printer_class.inc");
17 include_once($path_to_root . "/admin/db/company_db.inc");
18 include_once($path_to_root . "/admin/db/printers_db.inc");
19 include_once($path_to_root . "/config.php");
20 class FrontReport extends Cpdf
53 function FrontReport($title, $filename, $size = 'A4', $fontsize = 9)
61 $this->pageHeight=842;
63 $this->bottomMargin=30;
65 $this->rightMargin=30;
69 $this->pageHeight=595;
71 $this->bottomMargin=30;
73 $this->rightMargin=30;
77 $this->pageHeight=1190;
79 $this->bottomMargin=50;
81 $this->rightMargin=40;
84 $this->pageWidth=1190;
85 $this->pageHeight=842;
87 $this->bottomMargin=50;
89 $this->rightMargin=40;
94 $this->pageHeight=792;
96 $this->bottomMargin=30;
98 $this->rightMargin=25;
100 case 'letter_landscape':
101 $this->pageWidth=792;
102 $this->pageHeight=612;
104 $this->bottomMargin=30;
105 $this->leftMargin=30;
106 $this->rightMargin=25;
109 $this->pageWidth=612;
110 $this->pageHeight=1008;
112 $this->bottomMargin=40;
113 $this->leftMargin=30;
114 $this->rightMargin=25;
116 case 'legal_landscape':
117 $this->pageWidth=1008;
118 $this->pageHeight=612;
120 $this->bottomMargin=40;
121 $this->leftMargin=30;
122 $this->rightMargin=25;
125 $this->size = array(0, 0, $this->pageWidth, $this->pageHeight);
126 $this->title = $title;
127 $this->filename = $filename.".pdf";
128 $this->pageNumber = 0;
129 $this->endLine = $this->pageWidth - $this->rightMargin;
130 $this->companyCol = $this->endLine - 150;
131 $this->titleCol = $this->leftMargin + 100;
132 $this->lineHeight = 12;
133 $this->fontSize = $fontsize;
134 $this->oldFontSize = 0;
135 $this->row = $this->pageHeight - $this->topMargin;
136 $this->currency = '';
137 $rtl = ($_SESSION['language']->dir === 'rtl' ? 'rtl' : 'ltr');
138 $code = $_SESSION['language']->code;
139 $enc = strtoupper($_SESSION['language']->encoding);
140 // for the language array in class.pdf.inc
141 $l = array('a_meta_charset' => $enc, 'a_meta_dir' => $rtl, 'a_meta_language' => $code, 'w_page' => 'page');
142 $this->Cpdf($size, $l);
145 function Font($style = 'normal')
147 $this->selectFont("", $style);
150 function Info($params, $cols, $headers, $aligns,
151 $cols2 = null, $headers2 = null, $aligns2 = null)
153 global $app_title, $version, $power_by, $power_url;
155 $this->addinfo('Title', $this->title);
156 $this->addinfo('Subject', $this->title);
157 $this->addinfo('Author', $app_title . ' ' . $version);
158 $this->addinfo('Creator',$power_by . ' - ' . $power_url);
159 $year = get_current_fiscalyear();
160 if ($year['closed'] == 0)
164 $this->fiscal_year = sql2date($year['begin']) . " - " . sql2date($year['end']) . " " . "(" . $how . ")";
165 $this->company = get_company_prefs();
166 $this->user = $_SESSION["wa_current_user"]->name;
167 $this->host = $_SERVER['SERVER_NAME'];
168 $this->params = $params;
170 for ($i = 0; $i < count($this->cols); $i++)
171 $this->cols[$i] += $this->leftMargin;
172 $this->headers = $headers;
173 $this->aligns = $aligns;
174 $this->cols2 = $cols2;
175 if ($this->cols2 != null)
177 for ($i = 0; $i < count($this->cols2); $i++)
178 $this->cols2[$i] += $this->leftMargin;
180 $this->headers2 = $headers2;
181 $this->aligns2 = $aligns2;
187 if ($this->pageNumber > 1)
189 $this->row = $this->pageHeight - $this->topMargin;
191 $this->SetDrawColor(128, 128, 128);
192 $this->Line($this->row + 5, 1);
196 $this->fontSize += 4;
198 $this->Text($this->leftMargin, $this->title, $this->companyCol);
200 $this->fontSize -= 4;
201 $this->Text($this->companyCol, $this->company['coy_name']);
202 $this->row -= ($this->lineHeight + 4);
204 $str = _("Print Out Date") . ':';
205 $this->Text($this->leftMargin, $str, $this->titleCol);
206 $str = Today() . ' ' . Now();
207 $this->Text($this->titleCol, $str, $this->companyCol);
208 $this->Text($this->companyCol, $this->host);
211 $str = _("Fiscal Year") . ':';
212 $this->Text($this->leftMargin, $str, $this->titleCol);
213 $str = $this->fiscal_year;
214 $this->Text($this->titleCol, $str, $this->companyCol);
215 $this->Text($this->companyCol, $this->user);
216 for ($i = 1; $i < count($this->params); $i++)
218 if ($this->params[$i]['from'] != '')
221 $str = $this->params[$i]['text'] . ':';
222 $this->Text($this->leftMargin, $str, $this->titleCol);
223 $str = $this->params[$i]['from'];
224 if ($this->params[$i]['to'] != '')
225 $str .= " - " . $this->params[$i]['to'];
226 $this->Text($this->titleCol, $str, $this->companyCol);
229 if ($this->params[0] != '') // Comments
232 $str = _("Comments") . ':';
233 $this->Text($this->leftMargin, $str, $this->titleCol);
235 $this->Text($this->titleCol, $this->params[0], $this->endLine - 35);
238 $str = _("Page") . ' ' . $this->pageNumber;
239 $this->Text($this->endLine - 38, $str);
240 $this->Line($this->row - 5, 1);
242 $this->row -= ($this->lineHeight + 6);
243 $this->Font('italic');
244 if ($this->headers2 != null)
246 $count = count($this->headers2);
247 for ($i = 0; $i < $count; $i++)
248 $this->TextCol2($i, $i + 1, $this->headers2[$i]);
251 $count = count($this->headers);
252 for ($i = 0; $i < $count; $i++)
253 $this->TextCol($i, $i + 1, $this->headers[$i]);
255 $this->Line($this->row - 5, 1);
260 function Header2($myrow, $branch, $sales_order, $bankaccount, $doctype)
262 global $comp_path, $path_to_root, $print_as_quote, $print_invoice_no;
265 if ($this->pageNumber > 1)
268 if ($this->currency != $myrow['curr_code'])
270 include($path_to_root . "/reporting/includes/doctext2.inc");
274 include($path_to_root . "/reporting/includes/doctext.inc");
277 include($path_to_root . "/reporting/includes/header2.inc");
282 function AddImage($logo, $x, $y, $w, $h)
284 if (strpos($logo, ".png") || strpos($logo, ".PNG"))
285 $this->addPngFromFile($logo, $x, $y, $w, $h);
287 $this->addJpegFromFile($logo, $x, $y, $w, $h);
290 function SetDrawColor($r, $g, $b)
292 $this->setStrokeColor($r, $g, $b);
295 function SetTextColor($r, $g, $b)
297 TCPDF::SetTextColor($r, $g, $b);
300 function Text($c, $txt, $n=0, $corr=0, $r=0)
303 $n = $this->pageWidth - $this->rightMargin;
305 return $this->TextWrap($c, $this->row - $r, $n - $c + $corr, $txt, 'left');
308 function TextWrap($xpos, $ypos, $len, $str, $align = 'left')
310 if ($this->fontSize != $this->oldFontSize)
312 $this->SetFontSize($this->fontSize);
313 $this->oldFontSize = $this->fontSize;
315 return $this->addTextWrap($xpos, $ypos, $len, $this->fontSize, $str, $align);
318 function TextCol($c, $n, $txt, $corr=0, $r=0)
320 return $this->TextWrap($this->cols[$c], $this->row - $r, $this->cols[$n] - $this->cols[$c] + $corr, $txt, $this->aligns[$c]);
323 function AmountCol($c, $n, $txt, $dec=0, $corr=0, $r=0)
325 return $this->TextCol($c, $n, number_format2($txt, $dec), $corr, $r);
328 function DateCol($c, $n, $txt, $conv=false, $corr=0, $r=0)
331 $txt = sql2date($txt);
332 return $this->TextCol($c, $n, $txt, $corr, $r);
335 function TextCol2($c, $n, $txt, $corr=0, $r=0)
337 return $this->TextWrap($this->cols2[$c], $this->row - $r, $this->cols2[$n] - $this->cols2[$c] + $corr, $txt, $this->aligns2[$c]);
340 function TextColLines($c, $n, $txt, $corr=0, $r=0)
343 $this->TextWrapLines($this->cols[$c], $this->cols[$n] - $this->cols[$c] + $corr, $txt, $this->aligns[$c]);
346 function TextWrapLines($c, $width, $txt, $align='left')
348 $str = Explode("\n", $txt);
349 for ($i = 0; $i < count($str); $i++)
354 $l = $this->TextWrap($c, $this->row , $width, $l, $align);
361 function LineTo($from, $row, $to, $row2)
363 Cpdf::line($from, $row, $to, $row2);
366 function Line($row, $height = 0)
368 $this->setLineStyle($height + 1);
369 Cpdf::line($this->pageWidth - $this->rightMargin, $row ,$this->leftMargin, $row);
372 function NewLine($l=1, $np=0)
374 $this->row -= ($l * $this->lineHeight);
375 if ($this->row < $this->bottomMargin + ($np * $this->lineHeight))
379 function End($email=0, $subject=null, $myrow=null, $doctype = 0)
381 global $pdf_debug, $path_to_root, $comp_path;
385 $pdfcode = $this->Output('','S');
386 $pdfcode = str_replace("\n", "\n<br>", htmlspecialchars($pdfcode));
389 echo '</body></html>';
390 //header("Content-Length: $len");
391 //header("Content-Disposition: inline; filename=" . $this->filename);
392 //header('Expires: 0');
393 //header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
394 //header('Pragma: public');
396 //$this->pdf->stream();
401 $dir = $comp_path.'/'.user_company(). '/pdf_files';
403 if (!file_exists($dir))
407 // do not use standard filenames or your sensitive company data
408 // are world readable
409 $fname = $dir.'/'.uniqid('').'.pdf';
410 $this->Output($fname, 'F');
414 if ($this->currency != $myrow['curr_code'])
416 include("doctext2.inc");
420 include("doctext.inc");
422 require_once($path_to_root . "/reporting/includes/class.mail.inc");
423 $mail = new email($this->company['coy_name'], $this->company['email']);
424 $from = $this->company['coy_name'] . " <" . $this->company['email'] . ">";
425 $to = $myrow['DebtorName'] . " <" . $myrow['email'] . ">";
426 $msg = $doc_Dear_Sirs . ",\n\n" . $doc_AttachedFile . " " . $subject .
428 if ($myrow['dimension_id'] > 0 && $doctype == 10) // helper for payment links
430 if ($myrow['dimension_id'] == 1)
432 $amt = number_format($myrow["ov_freight"] + $myrow["ov_gst"] + $myrow["ov_amount"], user_price_dec());
433 $txt = $doc_Payment_Link . " PayPal: ";
434 $nn = urlencode($this->title . " " . $myrow['reference']);
435 $url = "https://www.paypal.com/xclick/business=" . $this->company['email'] . "&item_name=" .
436 $nn . "&amount=" . $amt . "¤cy_code=" . $myrow['curr_code'];
437 $msg .= $txt . $url . "\n\n";
440 $msg .= $doc_Kindest_regards . "\n\n";
441 $sender = $this->user . "\n" . $this->company['coy_name'];
443 $mail->subject($subject);
444 $mail->text($msg . $sender);
445 $mail->attachment($fname);
446 $ret = $mail->send();
447 if (1 == 1) // just for fun and for debugging purposes!!
449 $from = str_replace("<", "(", $from);
450 $from = str_replace(">", ")", $from);
451 $to = str_replace("<", "(", $to);
452 $to = str_replace(">", ")", $to);
453 $msg2 = "<br>From: " . $from;
454 $msg2 .= "<br>To: " . $to;
455 $msg2 .= "<br>Subject: " . $subject;
456 $msg2 .= "<br>Msg: " . nl2br($msg);
457 $msg2 .= nl2br($sender) . "<br>";
458 $msg2 .= "<br>Filepath: " . $fname . "<br>Filename: " . $this->filename . "<br>";
461 display_error(_("Sending document by email failed"));
463 display_notification($this->title . " " . $myrow['reference'] . " "
464 . _("has been sent by email."));
468 $printer = get_report_printer(user_print_profile(), $_POST['REP_ID']);
469 if ($printer == false) {
473 if (user_rep_popup())
474 $Ajax->popup($fname); // when embeded pdf viewer used
476 $Ajax->redirect($fname); // otherwise use faster method
480 // <SCRIPT LANGUAGE="JavaScript"><!--
481 // function go_now () { window.location.href = "'.$fname.'"; }
484 // <body onLoad="go_now()"; >
485 // <a href="'.$fname.'">click here</a> if you are not re-directed.
488 header('Content-type: application/pdf');
489 header("Content-Disposition: inline; filename=$this->filename");
490 header('Expires: 0');
491 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
492 header('Pragma: public');
495 } else { // send report to network printer
496 $prn = new remote_printer($printer['queue'],$printer['host'],
497 $printer['port'], $printer['timeout']);
498 $error = $prn->print_file($fname);
500 display_error($error);
502 display_notification(_('Report has been sent to network printer ').$printer['name']);
505 // first have a look through the directory,
506 // and remove old temporary pdfs
507 if ($d = @opendir($dir)) {
508 while (($file = readdir($d)) !== false) {
509 if (!is_file($dir.'/'.$file) || $file == 'index.php') continue;
510 // then check to see if this one is too old
511 $ftime = filemtime($dir.'/'.$file);
512 // seems 3 min is enough for any report download, isn't it?
513 if (time()-$ftime > 180){
514 unlink($dir.'/'.$file);