4 //include_once($path_to_root . "reporting/includes/class.pdf.inc");
5 include_once(dirname(__FILE__)."/class.pdf.inc");
6 include_once($path_to_root . "admin/db/company_db.inc");
7 include_once($path_to_root . "config.php");
8 class FrontReport extends Cpdf
41 function FrontReport($title, $filename, $size = 'A4', $fontsize = 9)
49 $this->pageHeight=842;
51 $this->bottomMargin=30;
53 $this->rightMargin=30;
57 $this->pageHeight=595;
59 $this->bottomMargin=30;
61 $this->rightMargin=30;
65 $this->pageHeight=1190;
67 $this->bottomMargin=50;
69 $this->rightMargin=40;
72 $this->pageWidth=1190;
73 $this->pageHeight=842;
75 $this->bottomMargin=50;
77 $this->rightMargin=40;
82 $this->pageHeight=792;
84 $this->bottomMargin=30;
86 $this->rightMargin=25;
88 case 'letter_landscape':
90 $this->pageHeight=612;
92 $this->bottomMargin=30;
94 $this->rightMargin=25;
98 $this->pageHeight=1008;
100 $this->bottomMargin=40;
101 $this->leftMargin=30;
102 $this->rightMargin=25;
104 case 'legal_landscape':
105 $this->pageWidth=1008;
106 $this->pageHeight=612;
108 $this->bottomMargin=40;
109 $this->leftMargin=30;
110 $this->rightMargin=25;
113 $this->size = array(0, 0, $this->pageWidth, $this->pageHeight);
114 $this->title = $title;
115 $this->filename = $filename;
116 $this->pageNumber = 0;
117 $this->endLine = $this->pageWidth - $this->rightMargin;
118 $this->companyCol = $this->endLine - 150;
119 $this->titleCol = $this->leftMargin + 100;
120 $this->lineHeight = 12;
121 $this->fontSize = $fontsize;
122 $this->oldFontSize = 0;
123 $this->row = $this->pageHeight - $this->topMargin;
124 $this->currency = '';
125 $rtl = ($_SESSION['language']->dir === 'rtl' ? 'rtl' : 'ltr');
126 $code = $_SESSION['language']->code;
127 $enc = strtoupper($_SESSION['language']->encoding);
128 // for the language array in class.pdf.inc
129 $l = array('a_meta_charset' => $enc, 'a_meta_dir' => $rtl, 'a_meta_language' => $code, 'w_page' => 'page');
130 $this->Cpdf($size, $l);
133 function Font($style = 'normal')
135 $this->selectFont("", $style);
138 function Info($params, $cols, $headers, $aligns,
139 $cols2 = null, $headers2 = null, $aligns2 = null)
141 global $app_title, $version, $power_by, $power_url, $path_to_root, $db_connections;
143 $this->addinfo('Title', $this->title);
144 $this->addinfo('Subject', $this->title);
145 $this->addinfo('Author', $app_title . ' ' . $version);
146 $this->addinfo('Creator',$power_by . ' - ' . $power_url);
147 $year = get_current_fiscalyear();
148 if ($year['closed'] == 0)
152 $this->fiscal_year = sql2date($year['begin']) . " - " . sql2date($year['end']) . " " . "(" . $how . ")";
153 $this->company = get_company_prefs();
154 $this->user = $_SESSION["wa_current_user"]->name;
155 //$this->host = $db_connections[$_SESSION["wa_current_user"]->company]["host"];
156 $this->host = $_SERVER['SERVER_NAME'];
157 $this->params = $params;
159 for ($i = 0; $i < count($this->cols); $i++)
160 $this->cols[$i] += $this->leftMargin;
161 $this->headers = $headers;
162 $this->aligns = $aligns;
163 $this->cols2 = $cols2;
164 if ($this->cols2 != null)
166 for ($i = 0; $i < count($this->cols2); $i++)
167 $this->cols2[$i] += $this->leftMargin;
169 $this->headers2 = $headers2;
170 $this->aligns2 = $aligns2;
176 if ($this->pageNumber > 1)
178 $this->row = $this->pageHeight - $this->topMargin;
180 $this->SetDrawColor(128, 128, 128);
181 $this->Line($this->row + 5, 1);
185 $this->fontSize += 4;
187 $this->Text($this->leftMargin, $this->title, $this->companyCol);
189 $this->fontSize -= 4;
190 $this->Text($this->companyCol, $this->company['coy_name']);
191 $this->row -= ($this->lineHeight + 4);
193 $str = _("Print Out Date") . ':';
194 $this->Text($this->leftMargin, $str, $this->titleCol);
195 $str = Today() . ' ' . Now();
196 $this->Text($this->titleCol, $str, $this->companyCol);
197 $this->Text($this->companyCol, $this->host);
200 $str = _("Fiscal Year") . ':';
201 $this->Text($this->leftMargin, $str, $this->titleCol);
202 $str = $this->fiscal_year;
203 $this->Text($this->titleCol, $str, $this->companyCol);
204 $this->Text($this->companyCol, $this->user);
205 for ($i = 1; $i < count($this->params); $i++)
207 if ($this->params[$i]['from'] != '')
210 $str = $this->params[$i]['text'] . ':';
211 $this->Text($this->leftMargin, $str, $this->titleCol);
212 $str = $this->params[$i]['from'];
213 if ($this->params[$i]['to'] != '')
214 $str .= " - " . $this->params[$i]['to'];
215 $this->Text($this->titleCol, $str, $this->companyCol);
218 if ($this->params[0] != '') // Comments
221 $str = _("Comments") . ':';
222 $this->Text($this->leftMargin, $str, $this->titleCol);
224 $this->Text($this->titleCol, $this->params[0], $this->endLine - 35);
227 $str = _("Page") . ' ' . $this->pageNumber;
228 $this->Text($this->endLine - 38, $str);
229 $this->Line($this->row - 5, 1);
231 $this->row -= ($this->lineHeight + 6);
232 $this->Font('italic');
233 if ($this->headers2 != null)
235 $count = count($this->headers2);
236 for ($i = 0; $i < $count; $i++)
237 $this->TextCol2($i, $i + 1, $this->headers2[$i]);
240 $count = count($this->headers);
241 for ($i = 0; $i < $count; $i++)
242 $this->TextCol($i, $i + 1, $this->headers[$i]);
244 $this->Line($this->row - 5, 1);
249 function Header2($myrow, $branch, $sales_order, $bankaccount, $doctype)
251 global $comp_path, $path_to_root, $print_as_quote, $print_invoice_no;
254 if ($this->pageNumber > 1)
257 if ($this->currency != $myrow['curr_code'])
259 include($path_to_root . "reporting/includes/doctext2.inc");
263 include($path_to_root . "reporting/includes/doctext.inc");
266 include($path_to_root . "reporting/includes/header2.inc");
271 function AddImage($logo, $x, $y, $w, $h)
273 if (strpos($logo, ".png") || strpos($logo, ".PNG"))
274 $this->addPngFromFile($logo, $x, $y, $w, $h);
276 $this->addJpegFromFile($logo, $x, $y, $w, $h);
279 function SetDrawColor($r, $g, $b)
281 $this->setStrokeColor($r, $g, $b);
284 function SetTextColor($r, $g, $b)
286 TCPDF::SetTextColor($r, $g, $b);
289 function Text($c, $txt, $n=0, $corr=0, $r=0)
292 $n = $this->pageWidth - $this->rightMargin;
294 return $this->TextWrap($c, $this->row - $r, $n - $c + $corr, $txt, 'left');
297 function TextWrap($xpos, $ypos, $len, $str, $align = 'left')
299 if ($this->fontSize != $this->oldFontSize)
301 $this->SetFontSize($this->fontSize);
302 $this->oldFontSize = $this->fontSize;
304 return $this->addTextWrap($xpos, $ypos, $len, $this->fontSize, $str, $align);
307 function TextCol($c, $n, $txt, $corr=0, $r=0)
309 return $this->TextWrap($this->cols[$c], $this->row - $r, $this->cols[$n] - $this->cols[$c] + $corr, $txt, $this->aligns[$c]);
312 function TextCol2($c, $n, $txt, $corr=0, $r=0)
314 return $this->TextWrap($this->cols2[$c], $this->row - $r, $this->cols2[$n] - $this->cols2[$c] + $corr, $txt, $this->aligns2[$c]);
317 function TextColLines($c, $n, $txt, $corr=0, $r=0)
320 $this->TextWrapLines($this->cols[$c], $this->cols[$n] - $this->cols[$c] + $corr, $txt, $this->aligns[$c]);
323 function TextWrapLines($c, $width, $txt, $align='left')
325 $str = Explode("\n", $txt);
326 for ($i = 0; $i < count($str); $i++)
331 $l = $this->TextWrap($c, $this->row , $width, $l, $align);
338 function LineTo($from, $row, $to, $row2)
340 Cpdf::line($from, $row, $to, $row2);
343 function Line($row, $height = 0)
345 $this->setLineStyle($height + 1);
346 Cpdf::line($this->pageWidth - $this->rightMargin, $row ,$this->leftMargin, $row);
349 function NewLine($l=1, $np=0)
351 $this->row -= ($l * $this->lineHeight);
352 if ($np > 0 && $this->row < $this->bottomMargin + ($np * $this->lineHeight))
356 function End($email=0, $subject=null, $myrow=null, $doctype = 0)
358 global $go_debug, $path_to_root, $comp_path;
362 $buf = $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();
379 $buf = $this->Output();
381 $dir = $comp_path.'/'.user_company(). '/pdf_files';
383 if (!file_exists($dir))
387 $fname = $dir . '/' . $this->filename;
388 $fp = fopen($fname,'w');
394 if ($this->currency != $myrow['curr_code'])
396 include("doctext2.inc");
400 include("doctext.inc");
402 require_once($path_to_root . "reporting/includes/class.mail.inc");
403 $mail = new email($this->company['coy_name'], $this->company['email']);
404 $from = $this->company['coy_name'] . " <" . $this->company['email'] . ">";
405 $to = $myrow['DebtorName'] . " <" . $myrow['email'] . ">";
406 $msg = $doc_Dear_Sirs . ",\n\n" . $doc_AttachedFile . " " . $subject .
408 if ($myrow['dimension_id'] > 0 && $doctype == 10) // helper for payment links
410 if ($myrow['dimension_id'] == 1)
412 $amt = number_format($myrow["ov_freight"] + $myrow["ov_gst"] + $myrow["ov_amount"], user_price_dec());
413 $txt = $doc_Payment_Link . " PayPal: ";
414 $nn = urlencode($this->title . " " . $myrow['reference']);
415 $url = "https://www.paypal.com/xclick/business=" . $this->company['email'] . "&item_name=" .
416 $nn . "&amount=" . $amt . "¤cy_code=" . $myrow['curr_code'];
417 $msg .= $txt . $url . "\n\n";
420 $msg .= $doc_Kindest_regards . "\n\n";
421 $sender = $this->user . "\n" . $this->company['coy_name'];
423 $mail->subject($subject);
424 $mail->text($msg . $sender);
425 $mail->attachment($fname);
426 $ret = $mail->send();
427 if (1 == 1) // just for fun and for debugging purposes!!
429 $from = str_replace("<", "(", $from);
430 $from = str_replace(">", ")", $from);
431 $to = str_replace("<", "(", $to);
432 $to = str_replace(">", ")", $to);
433 $msg2 = "<br>From: " . $from;
434 $msg2 .= "<br>To: " . $to;
435 $msg2 .= "<br>Subject: " . $subject;
436 $msg2 .= "<br>Msg: " . nl2br($msg);
437 $msg2 .= nl2br($sender) . "<br>";
438 $msg2 .= "<br>Filepath: " . $fname . "<br>Filename: " . $this->filename . "<br>";
441 $str = "<br>" . $this->title . " " . $myrow['reference'] . " " . _("sent to") . " ";
443 $str = "<br>" . $this->title . " " . $myrow['reference'] . " " . _("NOT sent to") . " ";
444 $msg2 .= $str . $myrow['DebtorName'] . " - " . $myrow['email'];
447 echo "</body></html>";
453 // <SCRIPT LANGUAGE="JavaScript"><!--
454 // function go_now () { window.location.href = "'.$fname.'"; }
457 // <body onLoad="go_now()"; >
458 // <a href="'.$fname.'">click here</a> if you are not re-directed.
461 header('Content-type: application/pdf');
462 header("Content-Disposition: inline; filename=$this->filename");
463 header('Expires: 0');
464 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
465 header('Pragma: public');
469 // also have a look through the directory, and remove the files that are older than a week
470 // rather want to save 'em
471 /*if ($d = @opendir($dir)) {
472 while (($file = readdir($d)) !== false) {
473 // then check to see if this one is too old
474 $ftime = filemtime($dir.'/'.$file);
475 if (time()-$ftime > 3600*24*7){
476 unlink($dir.'/'.$file);