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)
54 global $page_security;
55 if (!$_SESSION["wa_current_user"]->can_access_page($page_security))
57 display_error(_("The security settings on your account do not permit you to access this function"));
67 $this->pageHeight=842;
69 $this->bottomMargin=30;
71 $this->rightMargin=30;
75 $this->pageHeight=595;
77 $this->bottomMargin=30;
79 $this->rightMargin=30;
83 $this->pageHeight=1190;
85 $this->bottomMargin=50;
87 $this->rightMargin=40;
90 $this->pageWidth=1190;
91 $this->pageHeight=842;
93 $this->bottomMargin=50;
95 $this->rightMargin=40;
100 $this->pageHeight=792;
102 $this->bottomMargin=30;
103 $this->leftMargin=30;
104 $this->rightMargin=25;
106 case 'letter_landscape':
107 $this->pageWidth=792;
108 $this->pageHeight=612;
110 $this->bottomMargin=30;
111 $this->leftMargin=30;
112 $this->rightMargin=25;
115 $this->pageWidth=612;
116 $this->pageHeight=1008;
118 $this->bottomMargin=40;
119 $this->leftMargin=30;
120 $this->rightMargin=25;
122 case 'legal_landscape':
123 $this->pageWidth=1008;
124 $this->pageHeight=612;
126 $this->bottomMargin=40;
127 $this->leftMargin=30;
128 $this->rightMargin=25;
131 $this->size = array(0, 0, $this->pageWidth, $this->pageHeight);
132 $this->title = $title;
133 $this->filename = $filename.".pdf";
134 $this->pageNumber = 0;
135 $this->endLine = $this->pageWidth - $this->rightMargin;
136 $this->companyCol = $this->endLine - 150;
137 $this->titleCol = $this->leftMargin + 100;
138 $this->lineHeight = 12;
139 $this->fontSize = $fontsize;
140 $this->oldFontSize = 0;
141 $this->row = $this->pageHeight - $this->topMargin;
142 $this->currency = '';
143 $rtl = ($_SESSION['language']->dir === 'rtl' ? 'rtl' : 'ltr');
144 $code = $_SESSION['language']->code;
145 $enc = strtoupper($_SESSION['language']->encoding);
146 // for the language array in class.pdf.inc
147 $l = array('a_meta_charset' => $enc, 'a_meta_dir' => $rtl, 'a_meta_language' => $code, 'w_page' => 'page');
148 $this->Cpdf($size, $l);
151 function Font($style = 'normal')
153 $this->selectFont("", $style);
156 function Info($params, $cols, $headers, $aligns,
157 $cols2 = null, $headers2 = null, $aligns2 = null)
159 global $app_title, $version, $power_by, $power_url;
161 $this->addinfo('Title', $this->title);
162 $this->addinfo('Subject', $this->title);
163 $this->addinfo('Author', $app_title . ' ' . $version);
164 $this->addinfo('Creator',$power_by . ' - ' . $power_url);
165 $year = get_current_fiscalyear();
166 if ($year['closed'] == 0)
170 $this->fiscal_year = sql2date($year['begin']) . " - " . sql2date($year['end']) . " " . "(" . $how . ")";
171 $this->company = get_company_prefs();
172 $this->user = $_SESSION["wa_current_user"]->name;
173 $this->host = $_SERVER['SERVER_NAME'];
174 $this->params = $params;
176 for ($i = 0; $i < count($this->cols); $i++)
177 $this->cols[$i] += $this->leftMargin;
178 $this->headers = $headers;
179 $this->aligns = $aligns;
180 $this->cols2 = $cols2;
181 if ($this->cols2 != null)
183 for ($i = 0; $i < count($this->cols2); $i++)
184 $this->cols2[$i] += $this->leftMargin;
186 $this->headers2 = $headers2;
187 $this->aligns2 = $aligns2;
193 if ($this->pageNumber > 1)
195 $this->row = $this->pageHeight - $this->topMargin;
197 $this->SetDrawColor(128, 128, 128);
198 $this->Line($this->row + 5, 1);
202 $this->fontSize += 4;
204 $this->Text($this->leftMargin, $this->title, $this->companyCol);
206 $this->fontSize -= 4;
207 $this->Text($this->companyCol, $this->company['coy_name']);
208 $this->row -= ($this->lineHeight + 4);
210 $str = _("Print Out Date") . ':';
211 $this->Text($this->leftMargin, $str, $this->titleCol);
212 $str = Today() . ' ' . Now();
213 if ($this->company['time_zone'])
214 $str .= ' ' . date('O') . ' GMT';
215 $this->Text($this->titleCol, $str, $this->companyCol);
216 $this->Text($this->companyCol, $this->host);
219 $str = _("Fiscal Year") . ':';
220 $this->Text($this->leftMargin, $str, $this->titleCol);
221 $str = $this->fiscal_year;
222 $this->Text($this->titleCol, $str, $this->companyCol);
223 $this->Text($this->companyCol, $this->user);
224 for ($i = 1; $i < count($this->params); $i++)
226 if ($this->params[$i]['from'] != '')
229 $str = $this->params[$i]['text'] . ':';
230 $this->Text($this->leftMargin, $str, $this->titleCol);
231 $str = $this->params[$i]['from'];
232 if ($this->params[$i]['to'] != '')
233 $str .= " - " . $this->params[$i]['to'];
234 $this->Text($this->titleCol, $str, $this->companyCol);
237 if ($this->params[0] != '') // Comments
240 $str = _("Comments") . ':';
241 $this->Text($this->leftMargin, $str, $this->titleCol);
243 $this->Text($this->titleCol, $this->params[0], $this->endLine - 35);
246 $str = _("Page") . ' ' . $this->pageNumber;
247 $this->Text($this->endLine - 38, $str);
248 $this->Line($this->row - 5, 1);
250 $this->row -= ($this->lineHeight + 6);
251 $this->Font('italic');
252 if ($this->headers2 != null)
254 $count = count($this->headers2);
255 for ($i = 0; $i < $count; $i++)
256 $this->TextCol2($i, $i + 1, $this->headers2[$i]);
259 $count = count($this->headers);
260 for ($i = 0; $i < $count; $i++)
261 $this->TextCol($i, $i + 1, $this->headers[$i]);
263 $this->Line($this->row - 5, 1);
268 function Header2($myrow, $branch, $sales_order, $bankaccount, $doctype)
270 global $comp_path, $path_to_root, $print_as_quote, $print_invoice_no;
273 if ($this->pageNumber > 1)
276 if (isset($myrow['curr_code']) && $this->currency != $myrow['curr_code'])
278 include($path_to_root . "/reporting/includes/doctext2.inc");
282 include($path_to_root . "/reporting/includes/doctext.inc");
285 include($path_to_root . "/reporting/includes/header2.inc");
290 function AddImage($logo, $x, $y, $w, $h)
292 if (strpos($logo, ".png") || strpos($logo, ".PNG"))
293 $this->addPngFromFile($logo, $x, $y, $w, $h);
295 $this->addJpegFromFile($logo, $x, $y, $w, $h);
298 function SetDrawColor($r, $g, $b)
300 $this->setStrokeColor($r, $g, $b);
303 function SetTextColor($r, $g, $b)
305 TCPDF::SetTextColor($r, $g, $b);
308 function Text($c, $txt, $n=0, $corr=0, $r=0)
311 $n = $this->pageWidth - $this->rightMargin;
313 return $this->TextWrap($c, $this->row - $r, $n - $c + $corr, $txt, 'left');
316 function TextWrap($xpos, $ypos, $len, $str, $align = 'left')
318 if ($this->fontSize != $this->oldFontSize)
320 $this->SetFontSize($this->fontSize);
321 $this->oldFontSize = $this->fontSize;
323 return $this->addTextWrap($xpos, $ypos, $len, $this->fontSize, $str, $align);
326 function TextCol($c, $n, $txt, $corr=0, $r=0)
328 return $this->TextWrap($this->cols[$c], $this->row - $r, $this->cols[$n] - $this->cols[$c] + $corr, $txt, $this->aligns[$c]);
331 function AmountCol($c, $n, $txt, $dec=0, $corr=0, $r=0)
333 return $this->TextCol($c, $n, number_format2($txt, $dec), $corr, $r);
336 function DateCol($c, $n, $txt, $conv=false, $corr=0, $r=0)
339 $txt = sql2date($txt);
340 return $this->TextCol($c, $n, $txt, $corr, $r);
343 function TextCol2($c, $n, $txt, $corr=0, $r=0)
345 return $this->TextWrap($this->cols2[$c], $this->row - $r, $this->cols2[$n] - $this->cols2[$c] + $corr, $txt, $this->aligns2[$c]);
348 function TextColLines($c, $n, $txt, $corr=0, $r=0)
351 $this->TextWrapLines($this->cols[$c], $this->cols[$n] - $this->cols[$c] + $corr, $txt, $this->aligns[$c]);
354 function TextWrapLines($c, $width, $txt, $align='left')
356 $str = Explode("\n", $txt);
357 for ($i = 0; $i < count($str); $i++)
362 $l = $this->TextWrap($c, $this->row , $width, $l, $align);
369 function LineTo($from, $row, $to, $row2)
371 Cpdf::line($from, $row, $to, $row2);
374 function Line($row, $height = 0)
376 $this->setLineStyle($height + 1);
377 Cpdf::line($this->pageWidth - $this->rightMargin, $row ,$this->leftMargin, $row);
380 function NewLine($l=1, $np=0)
382 $this->row -= ($l * $this->lineHeight);
383 if ($this->row < $this->bottomMargin + ($np * $this->lineHeight))
387 function End($email=0, $subject=null, $myrow=null, $doctype = 0)
389 global $pdf_debug, $path_to_root, $comp_path;
393 $pdfcode = $this->Output('','S');
394 $pdfcode = str_replace("\n", "\n<br>", htmlspecialchars($pdfcode));
397 echo '</body></html>';
398 //header("Content-Length: $len");
399 //header("Content-Disposition: inline; filename=" . $this->filename);
400 //header('Expires: 0');
401 //header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
402 //header('Pragma: public');
404 //$this->pdf->stream();
409 $dir = $comp_path.'/'.user_company(). '/pdf_files';
411 if (!file_exists($dir))
415 // do not use standard filenames or your sensitive company data
416 // are world readable
417 $fname = $dir.'/'.uniqid('').'.pdf';
418 $this->Output($fname, 'F');
422 if ($this->currency != $myrow['curr_code'])
424 include("doctext2.inc");
428 include("doctext.inc");
430 require_once($path_to_root . "/reporting/includes/class.mail.inc");
431 $mail = new email($this->company['coy_name'], $this->company['email']);
432 $from = $this->company['coy_name'] . " <" . $this->company['email'] . ">";
433 if ($myrow['email'] == '') $myrow['email'] = $myrow['contact_email'];
434 $to = $myrow['DebtorName'] . " <" . $myrow['email'] . ">";
435 $msg = $doc_Dear_Sirs . " " . $myrow['DebtorName'] . ",\n\n" . $doc_AttachedFile . " " . $subject .
437 if ($myrow['dimension_id'] > 0 && $doctype == 10) // helper for payment links
439 if ($myrow['dimension_id'] == 1)
441 $amt = number_format($myrow["ov_freight"] + $myrow["ov_gst"] + $myrow["ov_amount"], user_price_dec());
442 $txt = $doc_Payment_Link . " PayPal: ";
443 $nn = urlencode($this->title . " " . $myrow['reference']);
444 $url = "https://www.paypal.com/xclick/business=" . $this->company['email'] . "&item_name=" .
445 $nn . "&amount=" . $amt . "¤cy_code=" . $myrow['curr_code'];
446 $msg .= $txt . $url . "\n\n";
449 $msg .= $doc_Kindest_regards . "\n\n";
450 $sender = $this->user . "\n" . $this->company['coy_name'] . "\n" . $this->company['postal_address'] . "\n" . $this->company['email'] . "\n" . $this->company['phone'];
452 $mail->subject($subject);
453 $mail->text($msg . $sender);
454 $mail->attachment($fname);
455 $ret = $mail->send();
456 if (1 == 1) // just for fun and for debugging purposes!!
458 $from = str_replace("<", "(", $from);
459 $from = str_replace(">", ")", $from);
460 $to = str_replace("<", "(", $to);
461 $to = str_replace(">", ")", $to);
462 $msg2 = "<br>From: " . $from;
463 $msg2 .= "<br>To: " . $to;
464 $msg2 .= "<br>Subject: " . $subject;
465 $msg2 .= "<br>Msg: " . nl2br($msg);
466 $msg2 .= nl2br($sender) . "<br>";
467 $msg2 .= "<br>Filepath: " . $fname . "<br>Filename: " . $this->filename . "<br>";
470 display_error(_("Sending document by email failed"));
472 display_notification($this->title . " " . $myrow['reference'] . " "
473 . _("has been sent by email."));
477 $printer = get_report_printer(user_print_profile(), $_POST['REP_ID']);
478 if ($printer == false) {
482 if (user_rep_popup())
483 $Ajax->popup($fname); // when embeded pdf viewer used
485 $Ajax->redirect($fname); // otherwise use faster method
489 // <SCRIPT LANGUAGE="JavaScript"><!--
490 // function go_now () { window.location.href = "'.$fname.'"; }
493 // <body onLoad="go_now()"; >
494 // <a href="'.$fname.'">click here</a> if you are not re-directed.
497 header('Content-type: application/pdf');
498 header("Content-Disposition: inline; filename=$this->filename");
499 header('Expires: 0');
500 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
501 header('Pragma: public');
505 } else { // send report to network printer
506 $prn = new remote_printer($printer['queue'],$printer['host'],
507 $printer['port'], $printer['timeout']);
508 $error = $prn->print_file($fname);
510 display_error($error);
512 display_notification(_('Report has been sent to network printer ').$printer['name']);
515 // first have a look through the directory,
516 // and remove old temporary pdfs
517 if ($d = @opendir($dir)) {
518 while (($file = readdir($d)) !== false) {
519 if (!is_file($dir.'/'.$file) || $file == 'index.php') continue;
520 // then check to see if this one is too old
521 $ftime = filemtime($dir.'/'.$file);
522 // seems 3 min is enough for any report download, isn't it?
523 if (time()-$ftime > 180){
524 unlink($dir.'/'.$file);