2 /**********************************************************************
3 Copyright (C) FrontAccounting, LLC.
4 Released under the terms of the GNU Affero General Public License,
5 AGPL, as published by the Free Software Foundation, either version
6 3 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/agpl-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;
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 TextCol2($c, $n, $txt, $corr=0, $r=0)
325 return $this->TextWrap($this->cols2[$c], $this->row - $r, $this->cols2[$n] - $this->cols2[$c] + $corr, $txt, $this->aligns2[$c]);
328 function TextColLines($c, $n, $txt, $corr=0, $r=0)
331 $this->TextWrapLines($this->cols[$c], $this->cols[$n] - $this->cols[$c] + $corr, $txt, $this->aligns[$c]);
334 function TextWrapLines($c, $width, $txt, $align='left')
336 $str = Explode("\n", $txt);
337 for ($i = 0; $i < count($str); $i++)
342 $l = $this->TextWrap($c, $this->row , $width, $l, $align);
349 function LineTo($from, $row, $to, $row2)
351 Cpdf::line($from, $row, $to, $row2);
354 function Line($row, $height = 0)
356 $this->setLineStyle($height + 1);
357 Cpdf::line($this->pageWidth - $this->rightMargin, $row ,$this->leftMargin, $row);
360 function NewLine($l=1, $np=0)
362 $this->row -= ($l * $this->lineHeight);
363 if ($this->row < $this->bottomMargin + ($np * $this->lineHeight))
367 function End($email=0, $subject=null, $myrow=null, $doctype = 0)
369 global $pdf_debug, $path_to_root, $comp_path;
373 $pdfcode = $this->Output('','S');
374 $pdfcode = str_replace("\n", "\n<br>", htmlspecialchars($pdfcode));
377 echo '</body></html>';
378 //header("Content-Length: $len");
379 //header("Content-Disposition: inline; filename=" . $this->filename);
380 //header('Expires: 0');
381 //header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
382 //header('Pragma: public');
384 //$this->pdf->stream();
389 $dir = $comp_path.'/'.user_company(). '/pdf_files';
391 if (!file_exists($dir))
395 // do not use standard filenames or your sensitive company data
396 // are world readable
397 $fname = $dir.'/'.uniqid('').'.pdf';
398 $this->Output($fname, 'F');
402 if ($this->currency != $myrow['curr_code'])
404 include("doctext2.inc");
408 include("doctext.inc");
410 require_once($path_to_root . "reporting/includes/class.mail.inc");
411 $mail = new email($this->company['coy_name'], $this->company['email']);
412 $from = $this->company['coy_name'] . " <" . $this->company['email'] . ">";
413 $to = $myrow['DebtorName'] . " <" . $myrow['email'] . ">";
414 $msg = $doc_Dear_Sirs . ",\n\n" . $doc_AttachedFile . " " . $subject .
416 if ($myrow['dimension_id'] > 0 && $doctype == 10) // helper for payment links
418 if ($myrow['dimension_id'] == 1)
420 $amt = number_format($myrow["ov_freight"] + $myrow["ov_gst"] + $myrow["ov_amount"], user_price_dec());
421 $txt = $doc_Payment_Link . " PayPal: ";
422 $nn = urlencode($this->title . " " . $myrow['reference']);
423 $url = "https://www.paypal.com/xclick/business=" . $this->company['email'] . "&item_name=" .
424 $nn . "&amount=" . $amt . "¤cy_code=" . $myrow['curr_code'];
425 $msg .= $txt . $url . "\n\n";
428 $msg .= $doc_Kindest_regards . "\n\n";
429 $sender = $this->user . "\n" . $this->company['coy_name'];
431 $mail->subject($subject);
432 $mail->text($msg . $sender);
433 $mail->attachment($fname);
434 $ret = $mail->send();
435 if (1 == 1) // just for fun and for debugging purposes!!
437 $from = str_replace("<", "(", $from);
438 $from = str_replace(">", ")", $from);
439 $to = str_replace("<", "(", $to);
440 $to = str_replace(">", ")", $to);
441 $msg2 = "<br>From: " . $from;
442 $msg2 .= "<br>To: " . $to;
443 $msg2 .= "<br>Subject: " . $subject;
444 $msg2 .= "<br>Msg: " . nl2br($msg);
445 $msg2 .= nl2br($sender) . "<br>";
446 $msg2 .= "<br>Filepath: " . $fname . "<br>Filename: " . $this->filename . "<br>";
449 $str = "<br>" . $this->title . " " . $myrow['reference'] . " " . _("sent to") . " ";
451 $str = "<br>" . $this->title . " " . $myrow['reference'] . " " . _("NOT sent to") . " ";
452 $msg2 .= $str . $myrow['DebtorName'] . " - " . $myrow['email'];
455 echo "</body></html>";
459 $printer = get_report_printer(user_print_profile(), $_POST['REP_ID']);
460 if ($printer == false) {
464 if (user_rep_popup())
465 $Ajax->popup($fname); // when embeded pdf viewer used
467 $Ajax->redirect($fname); // otherwise use faster method
471 // <SCRIPT LANGUAGE="JavaScript"><!--
472 // function go_now () { window.location.href = "'.$fname.'"; }
475 // <body onLoad="go_now()"; >
476 // <a href="'.$fname.'">click here</a> if you are not re-directed.
479 header('Content-type: application/pdf');
480 header("Content-Disposition: inline; filename=$this->filename");
481 header('Expires: 0');
482 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
483 header('Pragma: public');
486 } else { // send report to network printer
487 $prn = new remote_printer($printer['queue'],$printer['host'],
488 $printer['port'], $printer['timeout']);
489 $error = $prn->print_file($fname);
491 display_error($error);
493 display_notification(_('Report has been sent to network printer ').$printer['name']);
496 // first have a look through the directory,
497 // and remove old temporary pdfs
498 if ($d = @opendir($dir)) {
499 while (($file = readdir($d)) !== false) {
500 if (!is_file($dir.'/'.$file) || $file == 'index.php') continue;
501 // then check to see if this one is too old
502 $ftime = filemtime($dir.'/'.$file);
503 // seems 3 min is enough for any report download, isn't it?
504 if (time()-$ftime > 180){
505 unlink($dir.'/'.$file);