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");
276 $this->row = $this->pageHeight - $this->topMargin;
278 $upper = $this->row - 2 * $this->lineHeight;
279 $lower = $this->bottomMargin + 6 * $this->lineHeight;
280 $iline1 = $upper - 4 * $this->lineHeight;
281 $iline2 = $iline1 - 8 * $this->lineHeight;
282 $iline3 = $iline2 - 8 * $this->lineHeight;
283 $iline4 = $iline3 - 2 * $this->lineHeight;
284 $iline5 = $lower + 5 * $this->lineHeight;
285 $icol = $this->pageWidth / 2;
286 $ccol = $this->cols[0] + 4;
289 $mcol2 = $this->pageWidth - $ccol2;
291 $this->SetDrawColor(128, 128, 128);
292 $this->LineTo($this->pageWidth - $this->rightMargin, $upper ,$this->leftMargin, $upper);
293 $this->LineTo($this->leftMargin, $upper ,$this->leftMargin, $lower);
294 $this->LineTo($this->pageWidth - $this->rightMargin, $lower ,$this->leftMargin, $lower);
295 $this->LineTo($this->pageWidth - $this->rightMargin, $lower ,$this->pageWidth - $this->rightMargin, $upper);
296 $this->Line($iline1);
297 $this->Line($iline2);
298 $this->Line($iline3);
299 $this->Line($iline4);
300 $this->Line($iline5);
301 $this->LineTo($icol, $upper ,$icol, $iline1);
304 $this->fontSize += 4;
306 $this->Text($mcol, $this->title);
308 $this->fontSize -= 4;
309 if ($this->pageNumber > 1 && !strstr($this->filename, "Bulk"))
310 $this->Text($this->endLine - 35, _("Page") . ' ' . $this->pageNumber);
311 $this->fontSize -= 4;
312 $this->row = $upper - 5;
314 $this->Text($mcol, $doc_Invoice_no, $mcol + 90);
315 $this->Text($mcol + 90, $doc_Cust_no, $mcol + 180);
316 $this->Text($mcol + 180, $doc_Date);
317 $this->fontSize += 4;
319 $this->row = $upper - 2 * $this->lineHeight - 2;
320 if ($this->company['coy_logo'] != '')
322 $logo = $comp_path .'/'. user_company() . "/images/" . $this->company['coy_logo'];
323 $this->AddImage($logo, $ccol, $iline1 + 5, 250, 40);
327 $this->fontSize += 4;
329 $this->Text($ccol, $this->company['coy_name'], $icol);
331 $this->fontSize -= 4;
333 if ($doctype == 8) // PO
334 $this->Text($mcol, $myrow['order_no'], $mcol + 90);
335 else if ($doctype == 9) // SO
336 $this->Text($mcol, $myrow['order_no'] ." ".$myrow['customer_ref'], $mcol + 90);
337 else if (isset($myrow['trans_no']) && isset($myrow['reference'])) // INV/CRE/STA
339 if ($print_invoice_no == 1)
340 $this->Text($mcol, $myrow['trans_no'], $mcol + 90);
342 $this->Text($mcol, $myrow['reference'], $mcol + 90);
344 if (isset($myrow['debtor_no']))
345 $this->Text($mcol + 90, $myrow['debtor_no'], $mcol + 180);
346 if ($doctype == 8 || $doctype == 9)
347 $this->Text($mcol + 180, sql2date($myrow['ord_date']));
349 $this->Text($mcol + 180, sql2date($myrow['tran_date']));
351 $this->fontSize -= 4;
352 $this->row = $iline1 - 5;
353 $this->Text($ccol, $doc_Charge_To, $icol);
354 $this->Text($mcol, $doc_Delivered_To);
355 $this->fontSize += 4;
361 $this->Text($ccol, $myrow['name'], $icol);
367 $this->Text($ccol, $myrow['supp_name'], $icol);
369 $this->Text($ccol, $myrow['DebtorName'], $icol);
370 $adr = explode("\n", $myrow['address']);
372 for ($i = 0; $i < count($adr); $i++)
375 $this->Text($ccol, $adr[$i], $icol);
377 if ($sales_order != NULL)
381 $this->Text($mcol, $this->company['coy_name']);
383 $this->Text($mcol, $sales_order['deliver_to']);
384 $adr = explode("\n", $sales_order['delivery_address']);
385 for ($i = 0; $i < count($adr); $i++)
388 $this->Text($mcol, $adr[$i]);
391 $this->row = $iline2 - 2 * $this->lineHeight;
392 $this->Text($ccol, $doc_Shipping_Company . ":", $ccol2);
393 if ($doctype != 8 && isset($myrow['shipper_name']))
394 $this->Text($ccol2, $myrow['shipper_name'], $mcol);
395 $this->Text($mcol, $doc_Due_Date . ":", $mcol2);
397 $this->Text($mcol2, sql2date($myrow['delivery_date']));
398 else if ($doctype != 8 && isset($myrow['due_date']))
399 $this->Text($mcol2, sql2date($myrow['due_date']));
403 $this->Text($ccol, $doc_Your_Ref . ":", $ccol2);
404 $this->Text($ccol2, $branch['contact_name'], $mcol);
405 $this->Text($mcol, $doc_Our_Ref . ":", $mcol2);
407 $id = $branch['salesman'];
408 $sql = "SELECT salesman_name FROM ".TB_PREF."salesman WHERE salesman_code='$id'";
409 $result = db_query($sql,"could not get sales person");
410 $row = db_fetch($result);
412 $this->Text($mcol2, $row['salesman_name']);
415 $this->Text($ccol, $doc_Your_VAT_no . ":", $ccol2);
416 if ($doctype != 8 && isset($myrow['tax_id']))
417 $this->Text($ccol2, $myrow['tax_id'], $mcol);
418 $this->Text($mcol, $doc_Our_VAT_no . ":", $mcol2);
419 $this->Text($mcol2, $this->company['gst_no']);
421 $this->Text($ccol, $doc_Payment_Terms . ":", $ccol2);
423 $id = $myrow['payment_terms'];
424 $sql = "SELECT terms FROM ".TB_PREF."payment_terms WHERE terms_indicator='$id'";
425 $result = db_query($sql,"could not get paymentterms");
426 $row = db_fetch($result);
428 $this->Text($ccol2, $row["terms"], $mcol);
429 $this->Text($mcol, $doc_Our_Order_No . ":", $mcol2);
430 if (isset($myrow['order_']))
431 $this->Text($mcol2, $myrow['order_']);
433 $locale = $path_to_root . "lang/" . $_SESSION['language']->code . "/locale.inc";
434 if (file_exists($locale))
436 $header2include = true;
439 $this->row = $iline3 - $this->lineHeight - 2;
441 $count = count($this->headers);
442 for ($i = 0; $i < $count; $i++)
443 $this->TextCol($i, $i + 1, $this->headers[$i], -2);
445 $temp = $this->row - 2 * $this->lineHeight;
446 $this->row = $iline5 - $this->lineHeight - 6;
447 $this->Text($ccol, $doc_Please_Quote . " - " . $myrow['curr_code']);
449 if ($doctype == 10 && $branch['disable_branch'] > 0) // payment links
451 if ($branch['disable_branch'] == 1)
453 $amt = number_format($myrow["ov_freight"] + $myrow["ov_gst"] + $myrow["ov_amount"], user_price_dec());
454 $txt = $doc_Payment_Link . " PayPal: ";
455 $name = urlencode($this->title . " " . $myrow['reference']);
456 $url = "https://www.paypal.com/xclick/business=" . $this->company['email'] . "&item_name=" .
457 $name . "&amount=" . $amt . "¤cy_code=" . $myrow['curr_code'];
458 $this->fontSize -= 2;
459 $this->Text($ccol, $txt);
461 $this->SetTextColor(0, 0, 255);
462 $this->Text($ccol, $url);
463 $this->SetTextColor(0, 0, 0);
464 $this->addLink($url, $ccol, $this->row, $this->pageWidth - $this->rightMargin, $this->row + $this->lineHeight);
465 $this->fontSize += 2;
468 if ($this->params['comments'] != '')
472 $this->Text($ccol, $this->params['comments']);
476 $this->row = $lower - 5;
477 $this->fontSize -= 4;
479 $this->Text($ccol, $doc_Address, $ccol2 + 40);
480 $this->Text($ccol2 + 30, $doc_Phone_Fax_Email, $mcol);
481 $this->Text($mcol , $doc_Bank, $mcol2);
482 $this->Text($mcol2, $doc_Bank_Account);
483 $this->fontSize += 4;
485 $adrline = $this->row;
487 $adr = explode("\n", $this->company['postal_address']);
488 for ($i = 0; $i < count($adr); $i++)
490 $this->Text($ccol, $adr[$i], $ccol2 + 40);
493 $this->row = $adrline;
494 $this->Text($ccol2 + 30, $this->company['phone'], $mcol);
496 $this->Text($ccol2 + 30, $this->company['fax'], $mcol);
498 $this->Text($ccol2 + 30, $this->company['email'], $mcol);
500 $this->row = $adrline;
501 if (isset($bankaccount['bank_name']))
502 $this->Text($mcol, $bankaccount['bank_name'], $mcol2);
503 if (isset($bankaccount['bank_address']))
504 $adr = explode("\n", $bankaccount['bank_address']);
507 for ($i = 0; $i < count($adr); $i++)
510 $this->Text($mcol, $adr[$i], $mcol2);
513 $this->row = $adrline;
514 if (isset($bankaccount['bank_account_name']))
515 $this->Text($mcol2, $bankaccount['bank_account_name']);
517 if (isset($bankaccount['bank_account_number']))
518 $this->Text($mcol2, $bankaccount['bank_account_number']);
522 function AddImage($logo, $x, $y, $w, $h)
525 $x = $this->pageWidth - $x - $w;
526 if (strpos($logo, ".png") || strpos($logo, ".PNG"))
527 $this->addPngFromFile($logo, $x, $y, $w, $h);
529 $this->addJpegFromFile($logo, $x, $y, $w, $h);
532 function SetDrawColor($r, $g, $b)
534 $this->setStrokeColor($r / 255, $g / 255, $b / 255);
537 function SetTextColor($r, $g, $b)
539 $this->setColor($r / 255, $g / 255, $b / 255);
542 function Text($c, $txt, $n=0, $corr=0, $r=0)
545 $n = $this->pageWidth - $this->rightMargin;
547 return $this->TextWrap($c, $this->row - $r, $n - $c + $corr, $txt, 'left');
550 function TextWrap($xpos, $ypos, $len, $str, $align = 'left')
555 $xpos = $this->pageWidth - $xpos - $len;
556 if ($align == 'left')
558 elseif ($align == 'right')
561 return $this->addTextWrap($xpos, $ypos, $len, $this->fontSize, $str, $align);
564 function TextCol($c, $n, $txt, $corr=0, $r=0)
566 return $this->TextWrap($this->cols[$c], $this->row - $r, $this->cols[$n] - $this->cols[$c] + $corr, $txt, $this->aligns[$c]);
569 function TextCol2($c, $n, $txt, $corr=0, $r=0)
571 return $this->TextWrap($this->cols2[$c], $this->row - $r, $this->cols2[$n] - $this->cols2[$c] + $corr, $txt, $this->aligns2[$c]);
574 function TextColLines($c, $n, $txt, $corr=0)
576 $str = Explode("\n", $txt);
577 for ($i = 0; $i < count($str); $i++)
582 $l = $this->TextCol($c, $n, $l, $corr);
589 function LineTo($from, $row, $to, $row2)
591 Cpdf::line($from, $row, $to, $row2);
594 function Line($row, $height = 0)
596 $this->setLineStyle($height + 1);
597 Cpdf::line($this->pageWidth - $this->rightMargin, $row ,$this->leftMargin, $row);
600 function NewLine($l=1, $np=0)
602 $this->row -= ($l * $this->lineHeight);
603 if ($np > 0 && $this->row < $this->bottomMargin + ($np * $this->lineHeight))
607 function End($email=0, $subject=null, $myrow=null, $doctype = 0)
609 global $go_debug, $path_to_root, $comp_path;
611 // this is no good in IE so has been replaced, see down under
612 // but good for debugging purposes in IE
613 //session_write_close();
617 $buf = $this->output(1);
620 $pdfcode = str_replace("\n", "\n<br>", htmlspecialchars($pdfcode));
623 echo '</body></html>';
624 //header("Content-Length: $len");
625 //header("Content-Disposition: inline; filename=" . $this->filename);
626 //header('Expires: 0');
627 //header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
628 //header('Pragma: public');
630 //$this->pdf->stream();
634 $buf = $this->output();
636 $dir = $comp_path.'/'.user_company(). '/pdf_files';
638 if (!file_exists($dir))
642 $fname = $dir . '/' . $this->filename;
643 $fp = fopen($fname,'w');
649 if ($this->currency != $myrow['curr_code'])
651 include("doctext2.inc");
655 include("doctext.inc");
657 require_once($path_to_root . "reporting/includes/class.mail.inc");
658 $mail = new email($this->company['coy_name'], $this->company['email']);
659 $from = $this->company['coy_name'] . " <" . $this->company['email'] . ">";
660 $to = $myrow['DebtorName'] . " <" . $myrow['email'] . ">";
661 $msg = $doc_Dear_Sirs . ",\n\n" . $doc_AttachedFile . " " . $subject .
663 if ($myrow['dimension_id'] > 0 && $doctype == 10) // helper for payment links
665 if ($myrow['dimension_id'] == 1)
667 $amt = number_format($myrow["ov_freight"] + $myrow["ov_gst"] + $myrow["ov_amount"], user_price_dec());
668 $txt = $doc_Payment_Link . " PayPal: ";
669 $nn = urlencode($this->title . " " . $myrow['reference']);
670 $url = "https://www.paypal.com/xclick/business=" . $this->company['email'] . "&item_name=" .
671 $nn . "&amount=" . $amt . "¤cy_code=" . $myrow['curr_code'];
672 $msg .= $txt . $url . "\n\n";
675 $msg .= $doc_Kindest_regards . "\n\n";
676 $sender = $this->user . "\n" . $this->company['coy_name'];
678 $mail->subject($subject);
679 $mail->text($msg . $sender);
680 $mail->attachment($fname);
681 $ret = $mail->send();
682 if (1 == 1) // just for fun and for debugging purposes!!
684 $from = str_replace("<", "(", $from);
685 $from = str_replace(">", ")", $from);
686 $to = str_replace("<", "(", $to);
687 $to = str_replace(">", ")", $to);
688 $msg2 = "<br>From: " . $from;
689 $msg2 .= "<br>To: " . $to;
690 $msg2 .= "<br>Subject: " . $subject;
691 $msg2 .= "<br>Msg: " . nl2br($msg);
692 $msg2 .= nl2br($sender) . "<br>";
693 $msg2 .= "<br>Filepath: " . $fname . "<br>Filename: " . $this->filename . "<br>";
696 $str = "<br>" . $this->title . " " . $myrow['reference'] . " " . _("sent to") . " ";
698 $str = "<br>" . $this->title . " " . $myrow['reference'] . " " . _("NOT sent to") . " ";
699 $msg2 .= $str . $myrow['DebtorName'] . " - " . $myrow['email'];
702 echo "</body></html>";
708 // <SCRIPT LANGUAGE="JavaScript"><!--
709 // function go_now () { window.location.href = "'.$fname.'"; }
712 // <body onLoad="go_now()"; >
713 // <a href="'.$fname.'">click here</a> if you are not re-directed.
716 header('Content-type: application/pdf');
717 header("Content-Disposition: inline; filename=$this->filename");
718 header('Expires: 0');
719 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
720 header('Pragma: public');
724 // also have a look through the directory, and remove the files that are older than a week
725 // rather want to save 'em
726 /*if ($d = @opendir($dir)) {
727 while (($file = readdir($d)) !== false) {
728 // then check to see if this one is too old
729 $ftime = filemtime($dir.'/'.$file);
730 if (time()-$ftime > 3600*24*7){
731 unlink($dir.'/'.$file);