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, $path_to_root, $db_connections;
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 = $db_connections[$_SESSION["wa_current_user"]->company]["host"];
168 $this->host = $_SERVER['SERVER_NAME'];
169 $this->params = $params;
171 for ($i = 0; $i < count($this->cols); $i++)
172 $this->cols[$i] += $this->leftMargin;
173 $this->headers = $headers;
174 $this->aligns = $aligns;
175 $this->cols2 = $cols2;
176 if ($this->cols2 != null)
178 for ($i = 0; $i < count($this->cols2); $i++)
179 $this->cols2[$i] += $this->leftMargin;
181 $this->headers2 = $headers2;
182 $this->aligns2 = $aligns2;
188 if ($this->pageNumber > 1)
190 $this->row = $this->pageHeight - $this->topMargin;
192 $this->SetDrawColor(128, 128, 128);
193 $this->Line($this->row + 5, 1);
197 $this->fontSize += 4;
199 $this->Text($this->leftMargin, $this->title, $this->companyCol);
201 $this->fontSize -= 4;
202 $this->Text($this->companyCol, $this->company['coy_name']);
203 $this->row -= ($this->lineHeight + 4);
205 $str = _("Print Out Date") . ':';
206 $this->Text($this->leftMargin, $str, $this->titleCol);
207 $str = Today() . ' ' . Now();
208 $this->Text($this->titleCol, $str, $this->companyCol);
209 $this->Text($this->companyCol, $this->host);
212 $str = _("Fiscal Year") . ':';
213 $this->Text($this->leftMargin, $str, $this->titleCol);
214 $str = $this->fiscal_year;
215 $this->Text($this->titleCol, $str, $this->companyCol);
216 $this->Text($this->companyCol, $this->user);
217 for ($i = 1; $i < count($this->params); $i++)
219 if ($this->params[$i]['from'] != '')
222 $str = $this->params[$i]['text'] . ':';
223 $this->Text($this->leftMargin, $str, $this->titleCol);
224 $str = $this->params[$i]['from'];
225 if ($this->params[$i]['to'] != '')
226 $str .= " - " . $this->params[$i]['to'];
227 $this->Text($this->titleCol, $str, $this->companyCol);
230 if ($this->params[0] != '') // Comments
233 $str = _("Comments") . ':';
234 $this->Text($this->leftMargin, $str, $this->titleCol);
236 $this->Text($this->titleCol, $this->params[0], $this->endLine - 35);
239 $str = _("Page") . ' ' . $this->pageNumber;
240 $this->Text($this->endLine - 38, $str);
241 $this->Line($this->row - 5, 1);
243 $this->row -= ($this->lineHeight + 6);
244 $this->Font('italic');
245 if ($this->headers2 != null)
247 $count = count($this->headers2);
248 for ($i = 0; $i < $count; $i++)
249 $this->TextCol2($i, $i + 1, $this->headers2[$i]);
252 $count = count($this->headers);
253 for ($i = 0; $i < $count; $i++)
254 $this->TextCol($i, $i + 1, $this->headers[$i]);
256 $this->Line($this->row - 5, 1);
261 function Header2($myrow, $branch, $sales_order, $bankaccount, $doctype)
263 global $comp_path, $path_to_root, $print_as_quote, $print_invoice_no;
266 if ($this->pageNumber > 1)
269 if ($this->currency != $myrow['curr_code'])
271 include($path_to_root . "reporting/includes/doctext2.inc");
275 include($path_to_root . "reporting/includes/doctext.inc");
278 include($path_to_root . "reporting/includes/header2.inc");
283 function AddImage($logo, $x, $y, $w, $h)
285 if (strpos($logo, ".png") || strpos($logo, ".PNG"))
286 $this->addPngFromFile($logo, $x, $y, $w, $h);
288 $this->addJpegFromFile($logo, $x, $y, $w, $h);
291 function SetDrawColor($r, $g, $b)
293 $this->setStrokeColor($r, $g, $b);
296 function SetTextColor($r, $g, $b)
298 TCPDF::SetTextColor($r, $g, $b);
301 function Text($c, $txt, $n=0, $corr=0, $r=0)
304 $n = $this->pageWidth - $this->rightMargin;
306 return $this->TextWrap($c, $this->row - $r, $n - $c + $corr, $txt, 'left');
309 function TextWrap($xpos, $ypos, $len, $str, $align = 'left')
311 if ($this->fontSize != $this->oldFontSize)
313 $this->SetFontSize($this->fontSize);
314 $this->oldFontSize = $this->fontSize;
316 return $this->addTextWrap($xpos, $ypos, $len, $this->fontSize, $str, $align);
319 function TextCol($c, $n, $txt, $corr=0, $r=0)
321 return $this->TextWrap($this->cols[$c], $this->row - $r, $this->cols[$n] - $this->cols[$c] + $corr, $txt, $this->aligns[$c]);
324 function TextCol2($c, $n, $txt, $corr=0, $r=0)
326 return $this->TextWrap($this->cols2[$c], $this->row - $r, $this->cols2[$n] - $this->cols2[$c] + $corr, $txt, $this->aligns2[$c]);
329 function TextColLines($c, $n, $txt, $corr=0, $r=0)
332 $this->TextWrapLines($this->cols[$c], $this->cols[$n] - $this->cols[$c] + $corr, $txt, $this->aligns[$c]);
335 function TextWrapLines($c, $width, $txt, $align='left')
337 $str = Explode("\n", $txt);
338 for ($i = 0; $i < count($str); $i++)
343 $l = $this->TextWrap($c, $this->row , $width, $l, $align);
350 function LineTo($from, $row, $to, $row2)
352 Cpdf::line($from, $row, $to, $row2);
355 function Line($row, $height = 0)
357 $this->setLineStyle($height + 1);
358 Cpdf::line($this->pageWidth - $this->rightMargin, $row ,$this->leftMargin, $row);
361 function NewLine($l=1, $np=0)
363 $this->row -= ($l * $this->lineHeight);
364 if ($np > 0 && $this->row < $this->bottomMargin + ($np * $this->lineHeight))
368 function End($email=0, $subject=null, $myrow=null, $doctype = 0)
370 global $pdf_debug, $path_to_root, $comp_path;
374 $pdfcode = $this->Output('','S');
375 $pdfcode = str_replace("\n", "\n<br>", htmlspecialchars($pdfcode));
378 echo '</body></html>';
379 //header("Content-Length: $len");
380 //header("Content-Disposition: inline; filename=" . $this->filename);
381 //header('Expires: 0');
382 //header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
383 //header('Pragma: public');
385 //$this->pdf->stream();
390 $dir = $comp_path.'/'.user_company(). '/pdf_files';
392 if (!file_exists($dir))
396 // do not use standard filenames or your sensitive company data
397 // are world readable
398 $fname = $dir.'/'.uniqid('').'.pdf';
399 $this->Output($fname, 'F');
403 if ($this->currency != $myrow['curr_code'])
405 include("doctext2.inc");
409 include("doctext.inc");
411 require_once($path_to_root . "reporting/includes/class.mail.inc");
412 $mail = new email($this->company['coy_name'], $this->company['email']);
413 $from = $this->company['coy_name'] . " <" . $this->company['email'] . ">";
414 $to = $myrow['DebtorName'] . " <" . $myrow['email'] . ">";
415 $msg = $doc_Dear_Sirs . ",\n\n" . $doc_AttachedFile . " " . $subject .
417 if ($myrow['dimension_id'] > 0 && $doctype == 10) // helper for payment links
419 if ($myrow['dimension_id'] == 1)
421 $amt = number_format($myrow["ov_freight"] + $myrow["ov_gst"] + $myrow["ov_amount"], user_price_dec());
422 $txt = $doc_Payment_Link . " PayPal: ";
423 $nn = urlencode($this->title . " " . $myrow['reference']);
424 $url = "https://www.paypal.com/xclick/business=" . $this->company['email'] . "&item_name=" .
425 $nn . "&amount=" . $amt . "¤cy_code=" . $myrow['curr_code'];
426 $msg .= $txt . $url . "\n\n";
429 $msg .= $doc_Kindest_regards . "\n\n";
430 $sender = $this->user . "\n" . $this->company['coy_name'];
432 $mail->subject($subject);
433 $mail->text($msg . $sender);
434 $mail->attachment($fname);
435 $ret = $mail->send();
436 if (1 == 1) // just for fun and for debugging purposes!!
438 $from = str_replace("<", "(", $from);
439 $from = str_replace(">", ")", $from);
440 $to = str_replace("<", "(", $to);
441 $to = str_replace(">", ")", $to);
442 $msg2 = "<br>From: " . $from;
443 $msg2 .= "<br>To: " . $to;
444 $msg2 .= "<br>Subject: " . $subject;
445 $msg2 .= "<br>Msg: " . nl2br($msg);
446 $msg2 .= nl2br($sender) . "<br>";
447 $msg2 .= "<br>Filepath: " . $fname . "<br>Filename: " . $this->filename . "<br>";
450 $str = "<br>" . $this->title . " " . $myrow['reference'] . " " . _("sent to") . " ";
452 $str = "<br>" . $this->title . " " . $myrow['reference'] . " " . _("NOT sent to") . " ";
453 $msg2 .= $str . $myrow['DebtorName'] . " - " . $myrow['email'];
456 echo "</body></html>";
460 $printer = get_report_printer(user_print_profile(), $_POST['REP_ID']);
461 if ($printer == false) {
465 if (user_rep_popup())
466 $Ajax->popup($fname); // when embeded pdf viewer used
468 $Ajax->redirect($fname); // otherwise use faster method
472 // <SCRIPT LANGUAGE="JavaScript"><!--
473 // function go_now () { window.location.href = "'.$fname.'"; }
476 // <body onLoad="go_now()"; >
477 // <a href="'.$fname.'">click here</a> if you are not re-directed.
480 header('Content-type: application/pdf');
481 header("Content-Disposition: inline; filename=$this->filename");
482 header('Expires: 0');
483 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
484 header('Pragma: public');
487 } else { // send report to network printer
488 $prn = new remote_printer($printer['queue'],$printer['host'],
489 $printer['port'], $printer['timeout']);
490 $error = $prn->print_file($fname);
492 display_error($error);
494 display_notification(_('Report has been sent to network printer ').$printer['name']);
497 // first have a look through the directory,
498 // and remove old temporary pdfs
499 if ($d = @opendir($dir)) {
500 while (($file = readdir($d)) !== false) {
501 if (!is_file($dir.'/'.$file) || $file == 'index.php') continue;
502 // then check to see if this one is too old
503 $ftime = filemtime($dir.'/'.$file);
504 // seems 3 min is enough for any report download, isn't it?
505 if (time()-$ftime > 180){
506 unlink($dir.'/'.$file);