4 include_once($path_to_root . "reporting/includes/class.pdf.inc");
5 include_once($path_to_root . "admin/db/company_db.inc");
6 include_once($path_to_root . "config.php");
7 class FrontReport extends Cpdf
39 function FrontReport($title, $filename, $size = 'A4', $fontsize = 9)
47 $this->pageHeight=842;
49 $this->bottomMargin=30;
51 $this->rightMargin=30;
55 $this->pageHeight=595;
57 $this->bottomMargin=30;
59 $this->rightMargin=30;
63 $this->pageHeight=1190;
65 $this->bottomMargin=50;
67 $this->rightMargin=40;
70 $this->pageWidth=1190;
71 $this->pageHeight=842;
73 $this->bottomMargin=50;
75 $this->rightMargin=40;
80 $this->pageHeight=792;
82 $this->bottomMargin=30;
84 $this->rightMargin=25;
86 case 'letter_landscape':
88 $this->pageHeight=612;
90 $this->bottomMargin=30;
92 $this->rightMargin=25;
96 $this->pageHeight=1008;
98 $this->bottomMargin=40;
100 $this->rightMargin=25;
102 case 'legal_landscape':
103 $this->pageWidth=1008;
104 $this->pageHeight=612;
106 $this->bottomMargin=40;
107 $this->leftMargin=30;
108 $this->rightMargin=25;
111 $this->size = array(0, 0, $this->pageWidth, $this->pageHeight);
112 $this->title = $title;
113 $this->filename = $filename;
114 $this->pageNumber = 0;
115 $this->endLine = $this->pageWidth - $this->rightMargin;
116 $this->companyCol = $this->endLine - 150;
117 $this->titleCol = $this->leftMargin + 100;
118 $this->lineHeight = 12;
119 $this->fontSize = $fontsize;
120 $this->currency = '';
121 $this->rtl = ($_SESSION['language']->dir == 'rtl');
124 $this->Cpdf($this->size);
127 function Font($style = 'normal')
129 global $path_to_root;
131 $locale = $path_to_root . "lang/" . $_SESSION['language']->code . "/locale.inc";
132 if (file_exists($locale))
139 if ($style == 'italic')
140 $path = $path_to_root . 'reporting/fonts/Helvetica-Oblique.afm';
141 elseif ($style == 'bold')
142 $path = $path_to_root . 'reporting/fonts/Helvetica-Bold.afm';
143 else // even take misspelled styles
144 $path = $path_to_root . 'reporting/fonts/Helvetica.afm';
146 $this->selectFont($path, 'WinAnsiEncoding');
149 function Info($params, $cols, $headers, $aligns,
150 $cols2 = null, $headers2 = null, $aligns2 = null)
152 global $app_title, $version, $power_by, $power_url, $path_to_root, $db_connections;
154 $this->addinfo('Title', $this->title);
155 $this->addinfo('Subject', $this->title);
156 $this->addinfo('Author', $app_title . ' ' . $version);
157 $this->addinfo('Creator',$power_by . ' - ' . $power_url);
158 $year = get_current_fiscalyear();
159 if ($year['closed'] == 0)
163 $this->fiscal_year = sql2date($year['begin']) . " - " . sql2date($year['end']) . " " . "(" . $how . ")";
164 $this->company = get_company_prefs();
165 $this->user = $_SESSION["wa_current_user"]->name;
166 //$this->host = $db_connections[$_SESSION["wa_current_user"]->company]["host"];
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 - 35, $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)
285 $x = $this->pageWidth - $x - $w;
286 if (strpos($logo, ".png") || strpos($logo, ".PNG"))
287 $this->addPngFromFile($logo, $x, $y, $w, $h);
289 $this->addJpegFromFile($logo, $x, $y, $w, $h);
292 function SetDrawColor($r, $g, $b)
294 $this->setStrokeColor($r / 255, $g / 255, $b / 255);
297 function SetTextColor($r, $g, $b)
299 $this->setColor($r / 255, $g / 255, $b / 255);
302 function Text($c, $txt, $n=0, $corr=0, $r=0)
305 $n = $this->pageWidth - $this->rightMargin;
307 return $this->TextWrap($c, $this->row - $r, $n - $c + $corr, $txt, 'left');
310 function TextWrap($xpos, $ypos, $len, $str, $align = 'left')
315 $xpos = $this->pageWidth - $xpos - $len;
316 if ($align == 'left')
318 elseif ($align == 'right')
321 return $this->addTextWrap($xpos, $ypos, $len, $this->fontSize, $str, $align);
324 function TextCol($c, $n, $txt, $corr=0, $r=0)
326 return $this->TextWrap($this->cols[$c], $this->row - $r, $this->cols[$n] - $this->cols[$c] + $corr, $txt, $this->aligns[$c]);
329 function TextCol2($c, $n, $txt, $corr=0, $r=0)
331 return $this->TextWrap($this->cols2[$c], $this->row - $r, $this->cols2[$n] - $this->cols2[$c] + $corr, $txt, $this->aligns2[$c]);
334 function TextColLines($c, $n, $txt, $corr=0)
336 $str = Explode("\n", $txt);
337 for ($i = 0; $i < count($str); $i++)
342 $l = $this->TextCol($c, $n, $l, $corr);
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 ($np > 0 && $this->row < $this->bottomMargin + ($np * $this->lineHeight))
367 function End($email=0, $subject=null, $myrow=null, $doctype = 0)
369 global $go_debug, $path_to_root, $comp_path;
371 // this is no good in IE so has been replaced, see down under
372 // but good for debugging purposes in IE
373 //session_write_close();
377 $buf = $this->output(1);
380 $pdfcode = str_replace("\n", "\n<br>", htmlspecialchars($pdfcode));
383 echo '</body></html>';
384 //header("Content-Length: $len");
385 //header("Content-Disposition: inline; filename=" . $this->filename);
386 //header('Expires: 0');
387 //header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
388 //header('Pragma: public');
390 //$this->pdf->stream();
394 $buf = $this->output();
396 $dir = $comp_path.'/'.user_company(). '/pdf_files';
398 if (!file_exists($dir))
402 $fname = $dir . '/' . $this->filename;
403 $fp = fopen($fname,'w');
409 if ($this->currency != $myrow['curr_code'])
411 include("doctext2.inc");
415 include("doctext.inc");
417 require_once($path_to_root . "reporting/includes/class.mail.inc");
418 $mail = new email($this->company['coy_name'], $this->company['email']);
419 $from = $this->company['coy_name'] . " <" . $this->company['email'] . ">";
420 $to = $myrow['DebtorName'] . " <" . $myrow['email'] . ">";
421 $msg = $doc_Dear_Sirs . ",\n\n" . $doc_AttachedFile . " " . $subject .
423 if ($myrow['dimension_id'] > 0 && $doctype == 10) // helper for payment links
425 if ($myrow['dimension_id'] == 1)
427 $amt = number_format($myrow["ov_freight"] + $myrow["ov_gst"] + $myrow["ov_amount"], user_price_dec());
428 $txt = $doc_Payment_Link . " PayPal: ";
429 $nn = urlencode($this->title . " " . $myrow['reference']);
430 $url = "https://www.paypal.com/xclick/business=" . $this->company['email'] . "&item_name=" .
431 $nn . "&amount=" . $amt . "¤cy_code=" . $myrow['curr_code'];
432 $msg .= $txt . $url . "\n\n";
435 $msg .= $doc_Kindest_regards . "\n\n";
436 $sender = $this->user . "\n" . $this->company['coy_name'];
438 $mail->subject($subject);
439 $mail->text($msg . $sender);
440 $mail->attachment($fname);
441 $ret = $mail->send();
442 if (1 == 1) // just for fun and for debugging purposes!!
444 $from = str_replace("<", "(", $from);
445 $from = str_replace(">", ")", $from);
446 $to = str_replace("<", "(", $to);
447 $to = str_replace(">", ")", $to);
448 $msg2 = "<br>From: " . $from;
449 $msg2 .= "<br>To: " . $to;
450 $msg2 .= "<br>Subject: " . $subject;
451 $msg2 .= "<br>Msg: " . nl2br($msg);
452 $msg2 .= nl2br($sender) . "<br>";
453 $msg2 .= "<br>Filepath: " . $fname . "<br>Filename: " . $this->filename . "<br>";
456 $str = "<br>" . $this->title . " " . $myrow['reference'] . " " . _("sent to") . " ";
458 $str = "<br>" . $this->title . " " . $myrow['reference'] . " " . _("NOT sent to") . " ";
459 $msg2 .= $str . $myrow['DebtorName'] . " - " . $myrow['email'];
462 echo "</body></html>";
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');
484 // also have a look through the directory, and remove the files that are older than a week
485 // rather want to save 'em
486 /*if ($d = @opendir($dir)) {
487 while (($file = readdir($d)) !== false) {
488 // then check to see if this one is too old
489 $ftime = filemtime($dir.'/'.$file);
490 if (time()-$ftime > 3600*24*7){
491 unlink($dir.'/'.$file);