4 //include_once($path_to_root . "reporting/includes/class.pdf.inc");
5 include_once(dirname(__FILE__)."/class.pdf.inc");
6 include_once(dirname(__FILE__)."/printer_class.inc");
7 include_once($path_to_root . "admin/db/company_db.inc");
8 include_once($path_to_root . "admin/db/printers_db.inc");
9 include_once($path_to_root . "config.php");
10 class FrontReport extends Cpdf
43 function FrontReport($title, $filename, $size = 'A4', $fontsize = 9)
51 $this->pageHeight=842;
53 $this->bottomMargin=30;
55 $this->rightMargin=30;
59 $this->pageHeight=595;
61 $this->bottomMargin=30;
63 $this->rightMargin=30;
67 $this->pageHeight=1190;
69 $this->bottomMargin=50;
71 $this->rightMargin=40;
74 $this->pageWidth=1190;
75 $this->pageHeight=842;
77 $this->bottomMargin=50;
79 $this->rightMargin=40;
84 $this->pageHeight=792;
86 $this->bottomMargin=30;
88 $this->rightMargin=25;
90 case 'letter_landscape':
92 $this->pageHeight=612;
94 $this->bottomMargin=30;
96 $this->rightMargin=25;
100 $this->pageHeight=1008;
102 $this->bottomMargin=40;
103 $this->leftMargin=30;
104 $this->rightMargin=25;
106 case 'legal_landscape':
107 $this->pageWidth=1008;
108 $this->pageHeight=612;
110 $this->bottomMargin=40;
111 $this->leftMargin=30;
112 $this->rightMargin=25;
115 $this->size = array(0, 0, $this->pageWidth, $this->pageHeight);
116 $this->title = $title;
117 $this->filename = $filename;
118 $this->pageNumber = 0;
119 $this->endLine = $this->pageWidth - $this->rightMargin;
120 $this->companyCol = $this->endLine - 150;
121 $this->titleCol = $this->leftMargin + 100;
122 $this->lineHeight = 12;
123 $this->fontSize = $fontsize;
124 $this->oldFontSize = 0;
125 $this->row = $this->pageHeight - $this->topMargin;
126 $this->currency = '';
127 $rtl = ($_SESSION['language']->dir === 'rtl' ? 'rtl' : 'ltr');
128 $code = $_SESSION['language']->code;
129 $enc = strtoupper($_SESSION['language']->encoding);
130 // for the language array in class.pdf.inc
131 $l = array('a_meta_charset' => $enc, 'a_meta_dir' => $rtl, 'a_meta_language' => $code, 'w_page' => 'page');
132 $this->Cpdf($size, $l);
135 function Font($style = 'normal')
137 $this->selectFont("", $style);
140 function Info($params, $cols, $headers, $aligns,
141 $cols2 = null, $headers2 = null, $aligns2 = null)
143 global $app_title, $version, $power_by, $power_url, $path_to_root, $db_connections;
145 $this->addinfo('Title', $this->title);
146 $this->addinfo('Subject', $this->title);
147 $this->addinfo('Author', $app_title . ' ' . $version);
148 $this->addinfo('Creator',$power_by . ' - ' . $power_url);
149 $year = get_current_fiscalyear();
150 if ($year['closed'] == 0)
154 $this->fiscal_year = sql2date($year['begin']) . " - " . sql2date($year['end']) . " " . "(" . $how . ")";
155 $this->company = get_company_prefs();
156 $this->user = $_SESSION["wa_current_user"]->name;
157 //$this->host = $db_connections[$_SESSION["wa_current_user"]->company]["host"];
158 $this->host = $_SERVER['SERVER_NAME'];
159 $this->params = $params;
161 for ($i = 0; $i < count($this->cols); $i++)
162 $this->cols[$i] += $this->leftMargin;
163 $this->headers = $headers;
164 $this->aligns = $aligns;
165 $this->cols2 = $cols2;
166 if ($this->cols2 != null)
168 for ($i = 0; $i < count($this->cols2); $i++)
169 $this->cols2[$i] += $this->leftMargin;
171 $this->headers2 = $headers2;
172 $this->aligns2 = $aligns2;
178 if ($this->pageNumber > 1)
180 $this->row = $this->pageHeight - $this->topMargin;
182 $this->SetDrawColor(128, 128, 128);
183 $this->Line($this->row + 5, 1);
187 $this->fontSize += 4;
189 $this->Text($this->leftMargin, $this->title, $this->companyCol);
191 $this->fontSize -= 4;
192 $this->Text($this->companyCol, $this->company['coy_name']);
193 $this->row -= ($this->lineHeight + 4);
195 $str = _("Print Out Date") . ':';
196 $this->Text($this->leftMargin, $str, $this->titleCol);
197 $str = Today() . ' ' . Now();
198 $this->Text($this->titleCol, $str, $this->companyCol);
199 $this->Text($this->companyCol, $this->host);
202 $str = _("Fiscal Year") . ':';
203 $this->Text($this->leftMargin, $str, $this->titleCol);
204 $str = $this->fiscal_year;
205 $this->Text($this->titleCol, $str, $this->companyCol);
206 $this->Text($this->companyCol, $this->user);
207 for ($i = 1; $i < count($this->params); $i++)
209 if ($this->params[$i]['from'] != '')
212 $str = $this->params[$i]['text'] . ':';
213 $this->Text($this->leftMargin, $str, $this->titleCol);
214 $str = $this->params[$i]['from'];
215 if ($this->params[$i]['to'] != '')
216 $str .= " - " . $this->params[$i]['to'];
217 $this->Text($this->titleCol, $str, $this->companyCol);
220 if ($this->params[0] != '') // Comments
223 $str = _("Comments") . ':';
224 $this->Text($this->leftMargin, $str, $this->titleCol);
226 $this->Text($this->titleCol, $this->params[0], $this->endLine - 35);
229 $str = _("Page") . ' ' . $this->pageNumber;
230 $this->Text($this->endLine - 38, $str);
231 $this->Line($this->row - 5, 1);
233 $this->row -= ($this->lineHeight + 6);
234 $this->Font('italic');
235 if ($this->headers2 != null)
237 $count = count($this->headers2);
238 for ($i = 0; $i < $count; $i++)
239 $this->TextCol2($i, $i + 1, $this->headers2[$i]);
242 $count = count($this->headers);
243 for ($i = 0; $i < $count; $i++)
244 $this->TextCol($i, $i + 1, $this->headers[$i]);
246 $this->Line($this->row - 5, 1);
251 function Header2($myrow, $branch, $sales_order, $bankaccount, $doctype)
253 global $comp_path, $path_to_root, $print_as_quote, $print_invoice_no;
256 if ($this->pageNumber > 1)
259 if ($this->currency != $myrow['curr_code'])
261 include($path_to_root . "reporting/includes/doctext2.inc");
265 include($path_to_root . "reporting/includes/doctext.inc");
268 include($path_to_root . "reporting/includes/header2.inc");
273 function AddImage($logo, $x, $y, $w, $h)
275 if (strpos($logo, ".png") || strpos($logo, ".PNG"))
276 $this->addPngFromFile($logo, $x, $y, $w, $h);
278 $this->addJpegFromFile($logo, $x, $y, $w, $h);
281 function SetDrawColor($r, $g, $b)
283 $this->setStrokeColor($r, $g, $b);
286 function SetTextColor($r, $g, $b)
288 TCPDF::SetTextColor($r, $g, $b);
291 function Text($c, $txt, $n=0, $corr=0, $r=0)
294 $n = $this->pageWidth - $this->rightMargin;
296 return $this->TextWrap($c, $this->row - $r, $n - $c + $corr, $txt, 'left');
299 function TextWrap($xpos, $ypos, $len, $str, $align = 'left')
301 if ($this->fontSize != $this->oldFontSize)
303 $this->SetFontSize($this->fontSize);
304 $this->oldFontSize = $this->fontSize;
306 return $this->addTextWrap($xpos, $ypos, $len, $this->fontSize, $str, $align);
309 function TextCol($c, $n, $txt, $corr=0, $r=0)
311 return $this->TextWrap($this->cols[$c], $this->row - $r, $this->cols[$n] - $this->cols[$c] + $corr, $txt, $this->aligns[$c]);
314 function TextCol2($c, $n, $txt, $corr=0, $r=0)
316 return $this->TextWrap($this->cols2[$c], $this->row - $r, $this->cols2[$n] - $this->cols2[$c] + $corr, $txt, $this->aligns2[$c]);
319 function TextColLines($c, $n, $txt, $corr=0, $r=0)
322 $this->TextWrapLines($this->cols[$c], $this->cols[$n] - $this->cols[$c] + $corr, $txt, $this->aligns[$c]);
325 function TextWrapLines($c, $width, $txt, $align='left')
327 $str = Explode("\n", $txt);
328 for ($i = 0; $i < count($str); $i++)
333 $l = $this->TextWrap($c, $this->row , $width, $l, $align);
340 function LineTo($from, $row, $to, $row2)
342 Cpdf::line($from, $row, $to, $row2);
345 function Line($row, $height = 0)
347 $this->setLineStyle($height + 1);
348 Cpdf::line($this->pageWidth - $this->rightMargin, $row ,$this->leftMargin, $row);
351 function NewLine($l=1, $np=0)
353 $this->row -= ($l * $this->lineHeight);
354 if ($np > 0 && $this->row < $this->bottomMargin + ($np * $this->lineHeight))
358 function End($email=0, $subject=null, $myrow=null, $doctype = 0)
360 global $pdf_debug, $path_to_root, $comp_path;
364 $pdfcode = $this->Output();
365 $pdfcode = str_replace("\n", "\n<br>", htmlspecialchars($pdfcode));
368 echo '</body></html>';
369 //header("Content-Length: $len");
370 //header("Content-Disposition: inline; filename=" . $this->filename);
371 //header('Expires: 0');
372 //header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
373 //header('Pragma: public');
375 //$this->pdf->stream();
380 $buf = $this->Output();
382 $dir = $comp_path.'/'.user_company(). '/pdf_files';
384 if (!file_exists($dir))
388 // do not use standard filenames or your sensitive company data
389 // are world readable
390 $fname = tempnam($dir, 'FA');
391 rename($fname, $fname.'.pdf');
393 $fp = fopen($fname,'w');
399 if ($this->currency != $myrow['curr_code'])
401 include("doctext2.inc");
405 include("doctext.inc");
407 require_once($path_to_root . "reporting/includes/class.mail.inc");
408 $mail = new email($this->company['coy_name'], $this->company['email']);
409 $from = $this->company['coy_name'] . " <" . $this->company['email'] . ">";
410 $to = $myrow['DebtorName'] . " <" . $myrow['email'] . ">";
411 $msg = $doc_Dear_Sirs . ",\n\n" . $doc_AttachedFile . " " . $subject .
413 if ($myrow['dimension_id'] > 0 && $doctype == 10) // helper for payment links
415 if ($myrow['dimension_id'] == 1)
417 $amt = number_format($myrow["ov_freight"] + $myrow["ov_gst"] + $myrow["ov_amount"], user_price_dec());
418 $txt = $doc_Payment_Link . " PayPal: ";
419 $nn = urlencode($this->title . " " . $myrow['reference']);
420 $url = "https://www.paypal.com/xclick/business=" . $this->company['email'] . "&item_name=" .
421 $nn . "&amount=" . $amt . "¤cy_code=" . $myrow['curr_code'];
422 $msg .= $txt . $url . "\n\n";
425 $msg .= $doc_Kindest_regards . "\n\n";
426 $sender = $this->user . "\n" . $this->company['coy_name'];
428 $mail->subject($subject);
429 $mail->text($msg . $sender);
430 $mail->attachment($fname);
431 $ret = $mail->send();
432 if (1 == 1) // just for fun and for debugging purposes!!
434 $from = str_replace("<", "(", $from);
435 $from = str_replace(">", ")", $from);
436 $to = str_replace("<", "(", $to);
437 $to = str_replace(">", ")", $to);
438 $msg2 = "<br>From: " . $from;
439 $msg2 .= "<br>To: " . $to;
440 $msg2 .= "<br>Subject: " . $subject;
441 $msg2 .= "<br>Msg: " . nl2br($msg);
442 $msg2 .= nl2br($sender) . "<br>";
443 $msg2 .= "<br>Filepath: " . $fname . "<br>Filename: " . $this->filename . "<br>";
446 $str = "<br>" . $this->title . " " . $myrow['reference'] . " " . _("sent to") . " ";
448 $str = "<br>" . $this->title . " " . $myrow['reference'] . " " . _("NOT sent to") . " ";
449 $msg2 .= $str . $myrow['DebtorName'] . " - " . $myrow['email'];
452 echo "</body></html>";
456 $printer = get_report_printer(user_print_profile(), $_POST['REP_ID']);
457 if ($printer == false) {
461 if (user_rep_popup())
462 $Ajax->popup($fname); // when embeded pdf viewer used
464 $Ajax->redirect($fname); // otherwise use faster method
468 // <SCRIPT LANGUAGE="JavaScript"><!--
469 // function go_now () { window.location.href = "'.$fname.'"; }
472 // <body onLoad="go_now()"; >
473 // <a href="'.$fname.'">click here</a> if you are not re-directed.
476 header('Content-type: application/pdf');
477 header("Content-Disposition: inline; filename=$this->filename");
478 header('Expires: 0');
479 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
480 header('Pragma: public');
483 } else { // send report to network printer
484 $prn = new remote_printer($printer['queue'],$printer['host'],
485 $printer['port'], $printer['timeout']);
486 $error = $prn->print_file($fname);
488 display_error($error);
490 display_notification(_('Report has been sent to network printer ').$printer['name']);
493 // first have a look through the directory,
494 // and remove old temporary pdfs
495 if ($d = @opendir($dir)) {
496 while (($file = readdir($d)) !== false) {
497 if (!is_file($dir.'/'.$file) || $file == 'index.php') continue;
498 // then check to see if this one is too old
499 $ftime = filemtime($dir.'/'.$file);
500 // seems 3 min is enough for any report download, isn't it?
501 if (time()-$ftime > 180){
502 unlink($dir.'/'.$file);