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 ***********************************************************************/
13 //include_once($path_to_root . "reporting/includes/class.pdf.inc");
14 include_once(dirname(__FILE__)."/class.pdf.inc");
15 include_once(dirname(__FILE__)."/printer_class.inc");
16 include_once($path_to_root . "/admin/db/company_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
52 function FrontReport($title, $filename, $size = 'A4', $fontsize = 9)
60 $this->pageHeight=842;
62 $this->bottomMargin=30;
64 $this->rightMargin=30;
68 $this->pageHeight=595;
70 $this->bottomMargin=30;
72 $this->rightMargin=30;
76 $this->pageHeight=1190;
78 $this->bottomMargin=50;
80 $this->rightMargin=40;
83 $this->pageWidth=1190;
84 $this->pageHeight=842;
86 $this->bottomMargin=50;
88 $this->rightMargin=40;
93 $this->pageHeight=792;
95 $this->bottomMargin=30;
97 $this->rightMargin=25;
99 case 'letter_landscape':
100 $this->pageWidth=792;
101 $this->pageHeight=612;
103 $this->bottomMargin=30;
104 $this->leftMargin=30;
105 $this->rightMargin=25;
108 $this->pageWidth=612;
109 $this->pageHeight=1008;
111 $this->bottomMargin=40;
112 $this->leftMargin=30;
113 $this->rightMargin=25;
115 case 'legal_landscape':
116 $this->pageWidth=1008;
117 $this->pageHeight=612;
119 $this->bottomMargin=40;
120 $this->leftMargin=30;
121 $this->rightMargin=25;
124 $this->size = array(0, 0, $this->pageWidth, $this->pageHeight);
125 $this->title = $title;
126 $this->filename = $filename.".pdf";
127 $this->pageNumber = 0;
128 $this->endLine = $this->pageWidth - $this->rightMargin;
129 $this->companyCol = $this->endLine - 150;
130 $this->titleCol = $this->leftMargin + 100;
131 $this->lineHeight = 12;
132 $this->fontSize = $fontsize;
133 $this->oldFontSize = 0;
134 $this->row = $this->pageHeight - $this->topMargin;
135 $this->currency = '';
136 $rtl = ($_SESSION['language']->dir === 'rtl' ? 'rtl' : 'ltr');
137 $code = $_SESSION['language']->code;
138 $enc = strtoupper($_SESSION['language']->encoding);
139 // for the language array in class.pdf.inc
140 $l = array('a_meta_charset' => $enc, 'a_meta_dir' => $rtl, 'a_meta_language' => $code, 'w_page' => 'page');
141 $this->Cpdf($size, $l);
144 function Font($style = 'normal')
146 $this->selectFont("", $style);
149 function Info($params, $cols, $headers, $aligns,
150 $cols2 = null, $headers2 = null, $aligns2 = null)
152 global $app_title, $version, $power_by, $power_url;
154 $this->addinfo('Title', $this->title);
155 $this->addinfo('Subject', $this->title);
156 $this->addinfo('Author', $app_title . ' ' . $version);
157 $this->addinfo('Creator',$power_by . ' - ' . $power_url);
158 $year = get_current_fiscalyear();
159 if ($year['closed'] == 0)
163 $this->fiscal_year = sql2date($year['begin']) . " - " . sql2date($year['end']) . " " . "(" . $how . ")";
164 $this->company = get_company_prefs();
165 $this->user = $_SESSION["wa_current_user"]->name;
166 $this->host = $_SERVER['SERVER_NAME'];
167 $this->params = $params;
169 for ($i = 0; $i < count($this->cols); $i++)
170 $this->cols[$i] += $this->leftMargin;
171 $this->headers = $headers;
172 $this->aligns = $aligns;
173 $this->cols2 = $cols2;
174 if ($this->cols2 != null)
176 for ($i = 0; $i < count($this->cols2); $i++)
177 $this->cols2[$i] += $this->leftMargin;
179 $this->headers2 = $headers2;
180 $this->aligns2 = $aligns2;
186 if ($this->pageNumber > 1)
188 $this->row = $this->pageHeight - $this->topMargin;
190 $this->SetDrawColor(128, 128, 128);
191 $this->Line($this->row + 5, 1);
195 $this->fontSize += 4;
197 $this->Text($this->leftMargin, $this->title, $this->companyCol);
199 $this->fontSize -= 4;
200 $this->Text($this->companyCol, $this->company['coy_name']);
201 $this->row -= ($this->lineHeight + 4);
203 $str = _("Print Out Date") . ':';
204 $this->Text($this->leftMargin, $str, $this->titleCol);
205 $str = Today() . ' ' . Now();
206 $this->Text($this->titleCol, $str, $this->companyCol);
207 $this->Text($this->companyCol, $this->host);
210 $str = _("Fiscal Year") . ':';
211 $this->Text($this->leftMargin, $str, $this->titleCol);
212 $str = $this->fiscal_year;
213 $this->Text($this->titleCol, $str, $this->companyCol);
214 $this->Text($this->companyCol, $this->user);
215 for ($i = 1; $i < count($this->params); $i++)
217 if ($this->params[$i]['from'] != '')
220 $str = $this->params[$i]['text'] . ':';
221 $this->Text($this->leftMargin, $str, $this->titleCol);
222 $str = $this->params[$i]['from'];
223 if ($this->params[$i]['to'] != '')
224 $str .= " - " . $this->params[$i]['to'];
225 $this->Text($this->titleCol, $str, $this->companyCol);
228 if ($this->params[0] != '') // Comments
231 $str = _("Comments") . ':';
232 $this->Text($this->leftMargin, $str, $this->titleCol);
234 $this->Text($this->titleCol, $this->params[0], $this->endLine - 35);
237 $str = _("Page") . ' ' . $this->pageNumber;
238 $this->Text($this->endLine - 38, $str);
239 $this->Line($this->row - 5, 1);
241 $this->row -= ($this->lineHeight + 6);
242 $this->Font('italic');
243 if ($this->headers2 != null)
245 $count = count($this->headers2);
246 for ($i = 0; $i < $count; $i++)
247 $this->TextCol2($i, $i + 1, $this->headers2[$i]);
250 $count = count($this->headers);
251 for ($i = 0; $i < $count; $i++)
252 $this->TextCol($i, $i + 1, $this->headers[$i]);
254 $this->Line($this->row - 5, 1);
259 function Header2($myrow, $branch, $sales_order, $bankaccount, $doctype)
261 global $comp_path, $path_to_root, $print_as_quote, $print_invoice_no;
264 if ($this->pageNumber > 1)
267 if ($this->currency != $myrow['curr_code'])
269 include($path_to_root . "/reporting/includes/doctext2.inc");
273 include($path_to_root . "/reporting/includes/doctext.inc");
276 include($path_to_root . "/reporting/includes/header2.inc");
281 function AddImage($logo, $x, $y, $w, $h)
283 if (strpos($logo, ".png") || strpos($logo, ".PNG"))
284 $this->addPngFromFile($logo, $x, $y, $w, $h);
286 $this->addJpegFromFile($logo, $x, $y, $w, $h);
289 function SetDrawColor($r, $g, $b)
291 $this->setStrokeColor($r, $g, $b);
294 function SetTextColor($r, $g, $b)
296 TCPDF::SetTextColor($r, $g, $b);
299 function Text($c, $txt, $n=0, $corr=0, $r=0)
302 $n = $this->pageWidth - $this->rightMargin;
304 return $this->TextWrap($c, $this->row - $r, $n - $c + $corr, $txt, 'left');
307 function TextWrap($xpos, $ypos, $len, $str, $align = 'left')
309 if ($this->fontSize != $this->oldFontSize)
311 $this->SetFontSize($this->fontSize);
312 $this->oldFontSize = $this->fontSize;
314 return $this->addTextWrap($xpos, $ypos, $len, $this->fontSize, $str, $align);
317 function TextCol($c, $n, $txt, $corr=0, $r=0)
319 return $this->TextWrap($this->cols[$c], $this->row - $r, $this->cols[$n] - $this->cols[$c] + $corr, $txt, $this->aligns[$c]);
322 function AmountCol($c, $n, $txt, $dec=0, $corr=0, $r=0)
324 return $this->TextCol($c, $n, number_format2($txt, $dec), $corr, $r);
327 function DateCol($c, $n, $txt, $conv=false, $corr=0, $r=0)
330 $txt = sql2date($txt);
331 return $this->TextCol($c, $n, $txt, $corr, $r);
334 function TextCol2($c, $n, $txt, $corr=0, $r=0)
336 return $this->TextWrap($this->cols2[$c], $this->row - $r, $this->cols2[$n] - $this->cols2[$c] + $corr, $txt, $this->aligns2[$c]);
339 function TextColLines($c, $n, $txt, $corr=0, $r=0)
342 $this->TextWrapLines($this->cols[$c], $this->cols[$n] - $this->cols[$c] + $corr, $txt, $this->aligns[$c]);
345 function TextWrapLines($c, $width, $txt, $align='left')
347 $str = Explode("\n", $txt);
348 for ($i = 0; $i < count($str); $i++)
353 $l = $this->TextWrap($c, $this->row , $width, $l, $align);
360 function LineTo($from, $row, $to, $row2)
362 Cpdf::line($from, $row, $to, $row2);
365 function Line($row, $height = 0)
367 $this->setLineStyle($height + 1);
368 Cpdf::line($this->pageWidth - $this->rightMargin, $row ,$this->leftMargin, $row);
371 function NewLine($l=1, $np=0)
373 $this->row -= ($l * $this->lineHeight);
374 if ($this->row < $this->bottomMargin + ($np * $this->lineHeight))
378 function End($email=0, $subject=null, $myrow=null, $doctype = 0)
380 global $pdf_debug, $path_to_root, $comp_path;
384 $pdfcode = $this->Output('','S');
385 $pdfcode = str_replace("\n", "\n<br>", htmlspecialchars($pdfcode));
388 echo '</body></html>';
389 //header("Content-Length: $len");
390 //header("Content-Disposition: inline; filename=" . $this->filename);
391 //header('Expires: 0');
392 //header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
393 //header('Pragma: public');
395 //$this->pdf->stream();
400 $dir = $comp_path.'/'.user_company(). '/pdf_files';
402 if (!file_exists($dir))
406 // do not use standard filenames or your sensitive company data
407 // are world readable
408 $fname = $dir.'/'.uniqid('').'.pdf';
409 $this->Output($fname, 'F');
413 if ($this->currency != $myrow['curr_code'])
415 include("doctext2.inc");
419 include("doctext.inc");
421 require_once($path_to_root . "/reporting/includes/class.mail.inc");
422 $mail = new email($this->company['coy_name'], $this->company['email']);
423 $from = $this->company['coy_name'] . " <" . $this->company['email'] . ">";
424 $to = $myrow['DebtorName'] . " <" . $myrow['email'] . ">";
425 $msg = $doc_Dear_Sirs . ",\n\n" . $doc_AttachedFile . " " . $subject .
427 if ($myrow['dimension_id'] > 0 && $doctype == 10) // helper for payment links
429 if ($myrow['dimension_id'] == 1)
431 $amt = number_format($myrow["ov_freight"] + $myrow["ov_gst"] + $myrow["ov_amount"], user_price_dec());
432 $txt = $doc_Payment_Link . " PayPal: ";
433 $nn = urlencode($this->title . " " . $myrow['reference']);
434 $url = "https://www.paypal.com/xclick/business=" . $this->company['email'] . "&item_name=" .
435 $nn . "&amount=" . $amt . "¤cy_code=" . $myrow['curr_code'];
436 $msg .= $txt . $url . "\n\n";
439 $msg .= $doc_Kindest_regards . "\n\n";
440 $sender = $this->user . "\n" . $this->company['coy_name'];
442 $mail->subject($subject);
443 $mail->text($msg . $sender);
444 $mail->attachment($fname);
445 $ret = $mail->send();
446 if (1 == 1) // just for fun and for debugging purposes!!
448 $from = str_replace("<", "(", $from);
449 $from = str_replace(">", ")", $from);
450 $to = str_replace("<", "(", $to);
451 $to = str_replace(">", ")", $to);
452 $msg2 = "<br>From: " . $from;
453 $msg2 .= "<br>To: " . $to;
454 $msg2 .= "<br>Subject: " . $subject;
455 $msg2 .= "<br>Msg: " . nl2br($msg);
456 $msg2 .= nl2br($sender) . "<br>";
457 $msg2 .= "<br>Filepath: " . $fname . "<br>Filename: " . $this->filename . "<br>";
460 display_error(_("Sending document by email failed"));
462 display_notification($this->title . " " . $myrow['reference'] . " "
463 . _("has been sent by email."));
467 $printer = get_report_printer(user_print_profile(), $_POST['REP_ID']);
468 if ($printer == false) {
472 if (user_rep_popup())
473 $Ajax->popup($fname); // when embeded pdf viewer used
475 $Ajax->redirect($fname); // otherwise use faster method
479 // <SCRIPT LANGUAGE="JavaScript"><!--
480 // function go_now () { window.location.href = "'.$fname.'"; }
483 // <body onLoad="go_now()"; >
484 // <a href="'.$fname.'">click here</a> if you are not re-directed.
487 header('Content-type: application/pdf');
488 header("Content-Disposition: inline; filename=$this->filename");
489 header('Expires: 0');
490 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
491 header('Pragma: public');
494 } else { // send report to network printer
495 $prn = new remote_printer($printer['queue'],$printer['host'],
496 $printer['port'], $printer['timeout']);
497 $error = $prn->print_file($fname);
499 display_error($error);
501 display_notification(_('Report has been sent to network printer ').$printer['name']);
504 // first have a look through the directory,
505 // and remove old temporary pdfs
506 if ($d = @opendir($dir)) {
507 while (($file = readdir($d)) !== false) {
508 if (!is_file($dir.'/'.$file) || $file == 'index.php') continue;
509 // then check to see if this one is too old
510 $ftime = filemtime($dir.'/'.$file);
511 // seems 3 min is enough for any report download, isn't it?
512 if (time()-$ftime > 180){
513 unlink($dir.'/'.$file);