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))
137 //if ($this->rtl) // this is good for presentation only
140 if (user_price_dec() == 3) // only for presentation of farsi
143 $path = $path_to_root . 'reporting/fonts/farsi_1.afm';
145 elseif ($style == 'italic')
146 $path = $path_to_root . 'reporting/fonts/Helvetica-Oblique.afm';
147 elseif ($style == 'bold')
148 $path = $path_to_root . 'reporting/fonts/Helvetica-Bold.afm';
149 else // even take misspelled styles
150 $path = $path_to_root . 'reporting/fonts/Helvetica.afm';
152 $this->selectFont($path, 'WinAnsiEncoding');
155 function Info($params, $cols, $headers, $aligns,
156 $cols2 = null, $headers2 = null, $aligns2 = null)
158 global $app_title, $version, $power_by, $power_url, $path_to_root, $db_connections;
160 $this->addinfo('Title', $this->title);
161 $this->addinfo('Subject', $this->title);
162 $this->addinfo('Author', $app_title . ' ' . $version);
163 $this->addinfo('Creator',$power_by . ' - ' . $power_url);
164 $year = get_current_fiscalyear();
165 if ($year['closed'] == 0)
169 $this->fiscal_year = sql2date($year['begin']) . " - " . sql2date($year['end']) . " " . "(" . $how . ")";
170 $this->company = get_company_prefs();
171 $this->user = $_SESSION["wa_current_user"]->name;
172 //$this->host = $db_connections[$_SESSION["wa_current_user"]->company]["host"];
173 $this->host = $_SERVER['SERVER_NAME'];
174 $this->params = $params;
176 for ($i = 0; $i < count($this->cols); $i++)
177 $this->cols[$i] += $this->leftMargin;
178 $this->headers = $headers;
179 $this->aligns = $aligns;
180 $this->cols2 = $cols2;
181 if ($this->cols2 != null)
183 for ($i = 0; $i < count($this->cols2); $i++)
184 $this->cols2[$i] += $this->leftMargin;
186 $this->headers2 = $headers2;
187 $this->aligns2 = $aligns2;
193 if ($this->pageNumber > 1)
195 $this->row = $this->pageHeight - $this->topMargin;
197 $this->SetDrawColor(128, 128, 128);
198 $this->Line($this->row + 5, 1);
202 $this->fontSize += 4;
204 $this->Text($this->leftMargin, $this->title, $this->companyCol);
206 $this->fontSize -= 4;
207 $this->Text($this->companyCol, $this->company['coy_name']);
208 $this->row -= ($this->lineHeight + 4);
210 $str = _("Print Out Date") . ':';
211 $this->Text($this->leftMargin, $str, $this->titleCol);
212 $str = Today() . ' ' . Now();
213 $this->Text($this->titleCol, $str, $this->companyCol);
214 $this->Text($this->companyCol, $this->host);
217 $str = _("Fiscal Year") . ':';
218 $this->Text($this->leftMargin, $str, $this->titleCol);
219 $str = $this->fiscal_year;
220 $this->Text($this->titleCol, $str, $this->companyCol);
221 $this->Text($this->companyCol, $this->user);
222 for ($i = 1; $i < count($this->params); $i++)
224 if ($this->params[$i]['from'] != '')
227 $str = $this->params[$i]['text'] . ':';
228 $this->Text($this->leftMargin, $str, $this->titleCol);
229 $str = $this->params[$i]['from'];
230 if ($this->params[$i]['to'] != '')
231 $str .= " - " . $this->params[$i]['to'];
232 $this->Text($this->titleCol, $str, $this->companyCol);
235 if ($this->params[0] != '') // Comments
238 $str = _("Comments") . ':';
239 $this->Text($this->leftMargin, $str, $this->titleCol);
241 $this->Text($this->titleCol, $this->params[0], $this->endLine - 35);
244 $str = _("Page") . ' ' . $this->pageNumber;
245 $this->Text($this->endLine - 35, $str);
246 $this->Line($this->row - 5, 1);
248 $this->row -= ($this->lineHeight + 6);
249 $this->Font('italic');
250 if ($this->headers2 != null)
252 $count = count($this->headers2);
253 for ($i = 0; $i < $count; $i++)
254 $this->TextCol2($i, $i + 1, $this->headers2[$i]);
257 $count = count($this->headers);
258 for ($i = 0; $i < $count; $i++)
259 $this->TextCol($i, $i + 1, $this->headers[$i]);
261 $this->Line($this->row - 5, 1);
266 function Header2($myrow, $branch, $sales_order, $bankaccount, $doctype)
268 global $comp_path, $path_to_root, $print_as_quote, $print_invoice_no;
271 if ($this->pageNumber > 1)
274 if ($this->currency != $myrow['curr_code'])
276 include($path_to_root . "reporting/includes/doctext2.inc");
280 include($path_to_root . "reporting/includes/doctext.inc");
282 $this->row = $this->pageHeight - $this->topMargin;
284 $upper = $this->row - 2 * $this->lineHeight;
285 $lower = $this->bottomMargin + 6 * $this->lineHeight;
286 $iline1 = $upper - 4 * $this->lineHeight;
287 $iline2 = $iline1 - 8 * $this->lineHeight;
288 $iline3 = $iline2 - 8 * $this->lineHeight;
289 $iline4 = $iline3 - 2 * $this->lineHeight;
290 $iline5 = $lower + 5 * $this->lineHeight;
291 $icol = $this->pageWidth / 2;
292 $ccol = $this->cols[0] + 4;
295 $mcol2 = $this->pageWidth - $ccol2;
297 $this->SetDrawColor(128, 128, 128);
298 $this->LineTo($this->pageWidth - $this->rightMargin, $upper ,$this->leftMargin, $upper);
299 $this->LineTo($this->leftMargin, $upper ,$this->leftMargin, $lower);
300 $this->LineTo($this->pageWidth - $this->rightMargin, $lower ,$this->leftMargin, $lower);
301 $this->LineTo($this->pageWidth - $this->rightMargin, $lower ,$this->pageWidth - $this->rightMargin, $upper);
302 $this->Line($iline1);
303 $this->Line($iline2);
304 $this->Line($iline3);
305 $this->Line($iline4);
306 $this->Line($iline5);
307 $this->LineTo($icol, $upper ,$icol, $iline1);
310 $this->fontSize += 4;
312 $this->Text($mcol, $this->title);
314 $this->fontSize -= 4;
315 if ($this->pageNumber > 1 && !strstr($this->filename, "Bulk"))
316 $this->Text($this->endLine - 35, _("Page") . ' ' . $this->pageNumber);
317 $this->fontSize -= 4;
318 $this->row = $upper - 5;
320 $this->Text($mcol, $doc_Invoice_no, $mcol + 90);
321 $this->Text($mcol + 90, $doc_Cust_no, $mcol + 180);
322 $this->Text($mcol + 180, $doc_Date);
323 $this->fontSize += 4;
325 $this->row = $upper - 2 * $this->lineHeight - 2;
326 if ($this->company['coy_logo'] != '')
328 $logo = $comp_path .'/'. user_company() . "/images/" . $this->company['coy_logo'];
329 $this->AddImage($logo, $ccol, $iline1 + 5, 250, 40);
333 $this->fontSize += 4;
335 $this->Text($ccol, $this->company['coy_name'], $icol);
337 $this->fontSize -= 4;
339 if ($doctype == 8) // PO
340 $this->Text($mcol, $myrow['order_no'], $mcol + 90);
341 else if ($doctype == 9) // SO
342 $this->Text($mcol, $myrow['order_no'] ." ".$myrow['customer_ref'], $mcol + 90);
343 else if (isset($myrow['trans_no']) && isset($myrow['reference'])) // INV/CRE/STA
345 if ($print_invoice_no == 1)
346 $this->Text($mcol, $myrow['trans_no'], $mcol + 90);
348 $this->Text($mcol, $myrow['reference'], $mcol + 90);
350 if (isset($myrow['debtor_no']))
351 $this->Text($mcol + 90, $myrow['debtor_no'], $mcol + 180);
352 if ($doctype == 8 || $doctype == 9)
353 $this->Text($mcol + 180, sql2date($myrow['ord_date']));
355 $this->Text($mcol + 180, sql2date($myrow['tran_date']));
357 $this->fontSize -= 4;
358 $this->row = $iline1 - 5;
359 $this->Text($ccol, $doc_Charge_To, $icol);
360 $this->Text($mcol, $doc_Delivered_To);
361 $this->fontSize += 4;
367 $this->Text($ccol, $myrow['name'], $icol);
373 $this->Text($ccol, $myrow['supp_name'], $icol);
375 $this->Text($ccol, $myrow['DebtorName'], $icol);
376 $adr = explode("\n", $myrow['address']);
378 for ($i = 0; $i < count($adr); $i++)
381 $this->Text($ccol, $adr[$i], $icol);
383 if ($sales_order != NULL)
387 $this->Text($mcol, $this->company['coy_name']);
389 $this->Text($mcol, $sales_order['deliver_to']);
390 $adr = explode("\n", $sales_order['delivery_address']);
391 for ($i = 0; $i < count($adr); $i++)
394 $this->Text($mcol, $adr[$i]);
397 $this->row = $iline2 - 2 * $this->lineHeight;
398 $this->Text($ccol, $doc_Shipping_Company . ":", $ccol2);
399 if ($doctype != 8 && isset($myrow['shipper_name']))
400 $this->Text($ccol2, $myrow['shipper_name'], $mcol);
401 $this->Text($mcol, $doc_Due_Date . ":", $mcol2);
403 $this->Text($mcol2, sql2date($myrow['delivery_date']));
404 else if ($doctype != 8 && isset($myrow['due_date']))
405 $this->Text($mcol2, sql2date($myrow['due_date']));
409 $this->Text($ccol, $doc_Your_Ref . ":", $ccol2);
410 $this->Text($ccol2, $branch['contact_name'], $mcol);
411 $this->Text($mcol, $doc_Our_Ref . ":", $mcol2);
413 $id = $branch['salesman'];
414 $sql = "SELECT salesman_name FROM ".TB_PREF."salesman WHERE salesman_code='$id'";
415 $result = db_query($sql,"could not get sales person");
416 $row = db_fetch($result);
418 $this->Text($mcol2, $row['salesman_name']);
421 $this->Text($ccol, $doc_Your_VAT_no . ":", $ccol2);
422 if ($doctype != 8 && isset($myrow['tax_id']))
423 $this->Text($ccol2, $myrow['tax_id'], $mcol);
424 $this->Text($mcol, $doc_Our_VAT_no . ":", $mcol2);
425 $this->Text($mcol2, $this->company['gst_no']);
427 $this->Text($ccol, $doc_Payment_Terms . ":", $ccol2);
429 $id = $myrow['payment_terms'];
430 $sql = "SELECT terms FROM ".TB_PREF."payment_terms WHERE terms_indicator='$id'";
431 $result = db_query($sql,"could not get paymentterms");
432 $row = db_fetch($result);
434 $this->Text($ccol2, $row["terms"], $mcol);
435 $this->Text($mcol, $doc_Our_Order_No . ":", $mcol2);
436 if (isset($myrow['order_']))
437 $this->Text($mcol2, $myrow['order_']);
439 $locale = $path_to_root . "lang/" . $_SESSION['language']->code . "/locale.inc";
440 if (file_exists($locale))
442 $header2include = true;
445 $this->row = $iline3 - $this->lineHeight - 2;
447 $count = count($this->headers);
448 for ($i = 0; $i < $count; $i++)
449 $this->TextCol($i, $i + 1, $this->headers[$i], -2);
451 $temp = $this->row - 2 * $this->lineHeight;
452 $this->row = $iline5 - $this->lineHeight - 6;
453 $this->Text($ccol, $doc_Please_Quote . " - " . $myrow['curr_code']);
455 if ($doctype == 10 && $branch['disable_branch'] > 0) // payment links
457 if ($branch['disable_branch'] == 1)
459 $amt = number_format($myrow["ov_freight"] + $myrow["ov_gst"] + $myrow["ov_amount"], user_price_dec());
460 $txt = $doc_Payment_Link . " PayPal: ";
461 $name = urlencode($this->title . " " . $myrow['reference']);
462 $url = "https://www.paypal.com/xclick/business=" . $this->company['email'] . "&item_name=" .
463 $name . "&amount=" . $amt . "¤cy_code=" . $myrow['curr_code'];
464 $this->fontSize -= 2;
465 $this->Text($ccol, $txt);
467 $this->SetTextColor(0, 0, 255);
468 $this->Text($ccol, $url);
469 $this->SetTextColor(0, 0, 0);
470 $this->addLink($url, $ccol, $this->row, $this->pageWidth - $this->rightMargin, $this->row + $this->lineHeight);
471 $this->fontSize += 2;
474 if ($this->params['comments'] != '')
478 $this->Text($ccol, $this->params['comments']);
482 $this->row = $lower - 5;
483 $this->fontSize -= 4;
485 $this->Text($ccol, $doc_Address, $ccol2 + 40);
486 $this->Text($ccol2 + 30, $doc_Phone_Fax_Email, $mcol);
487 $this->Text($mcol , $doc_Bank, $mcol2);
488 $this->Text($mcol2, $doc_Bank_Account);
489 $this->fontSize += 4;
491 $adrline = $this->row;
493 $adr = explode("\n", $this->company['postal_address']);
494 for ($i = 0; $i < count($adr); $i++)
496 $this->Text($ccol, $adr[$i], $ccol2 + 40);
499 $this->row = $adrline;
500 $this->Text($ccol2 + 30, $this->company['phone'], $mcol);
502 $this->Text($ccol2 + 30, $this->company['fax'], $mcol);
504 $this->Text($ccol2 + 30, $this->company['email'], $mcol);
506 $this->row = $adrline;
507 if (isset($bankaccount['bank_name']))
508 $this->Text($mcol, $bankaccount['bank_name'], $mcol2);
509 if (isset($bankaccount['bank_address']))
510 $adr = explode("\n", $bankaccount['bank_address']);
513 for ($i = 0; $i < count($adr); $i++)
516 $this->Text($mcol, $adr[$i], $mcol2);
519 $this->row = $adrline;
520 if (isset($bankaccount['bank_account_name']))
521 $this->Text($mcol2, $bankaccount['bank_account_name']);
523 if (isset($bankaccount['bank_account_number']))
524 $this->Text($mcol2, $bankaccount['bank_account_number']);
528 function AddImage($logo, $x, $y, $w, $h)
531 $x = $this->pageWidth - $x - $w;
532 if (strpos($logo, ".png") || strpos($logo, ".PNG"))
533 $this->addPngFromFile($logo, $x, $y, $w, $h);
535 $this->addJpegFromFile($logo, $x, $y, $w, $h);
538 function SetDrawColor($r, $g, $b)
540 $this->setStrokeColor($r / 255, $g / 255, $b / 255);
543 function SetTextColor($r, $g, $b)
545 $this->setColor($r / 255, $g / 255, $b / 255);
548 function Text($c, $txt, $n=0, $corr=0, $r=0)
551 $n = $this->pageWidth - $this->rightMargin;
553 return $this->TextWrap($c, $this->row - $r, $n - $c + $corr, $txt, 'left');
556 function TextWrap($xpos, $ypos, $len, $str, $align = 'left')
561 $xpos = $this->pageWidth - $xpos - $len;
562 if ($align == 'left')
564 elseif ($align == 'right')
567 return $this->addTextWrap($xpos, $ypos, $len, $this->fontSize, $str, $align);
570 function TextCol($c, $n, $txt, $corr=0, $r=0)
572 return $this->TextWrap($this->cols[$c], $this->row - $r, $this->cols[$n] - $this->cols[$c] + $corr, $txt, $this->aligns[$c]);
575 function TextCol2($c, $n, $txt, $corr=0, $r=0)
577 return $this->TextWrap($this->cols2[$c], $this->row - $r, $this->cols2[$n] - $this->cols2[$c] + $corr, $txt, $this->aligns2[$c]);
580 function TextColLines($c, $n, $txt, $corr=0)
582 $str = Explode("\n", $txt);
583 for ($i = 0; $i < count($str); $i++)
588 $l = $this->TextCol($c, $n, $l, $corr);
595 function LineTo($from, $row, $to, $row2)
597 Cpdf::line($from, $row, $to, $row2);
600 function Line($row, $height = 0)
602 $this->setLineStyle($height + 1);
603 Cpdf::line($this->pageWidth - $this->rightMargin, $row ,$this->leftMargin, $row);
606 function NewLine($l=1, $np=0)
608 $this->row -= ($l * $this->lineHeight);
609 if ($np > 0 && $this->row < $this->bottomMargin + ($np * $this->lineHeight))
613 function End($email=0, $subject=null, $myrow=null, $doctype = 0)
615 global $go_debug, $path_to_root, $comp_path;
617 // this is no good in IE so has been replaced, see down under
618 // but good for debugging purposes in IE
619 //session_write_close();
623 $buf = $this->output(1);
626 $pdfcode = str_replace("\n", "\n<br>", htmlspecialchars($pdfcode));
629 echo '</body></html>';
630 //header("Content-Length: $len");
631 //header("Content-Disposition: inline; filename=" . $this->filename);
632 //header('Expires: 0');
633 //header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
634 //header('Pragma: public');
636 //$this->pdf->stream();
640 $buf = $this->output();
642 $dir = $comp_path.'/'.user_company(). '/pdf_files';
644 if (!file_exists($dir))
648 $fname = $dir . '/' . $this->filename;
649 $fp = fopen($fname,'w');
655 if ($this->currency != $myrow['curr_code'])
657 include("doctext2.inc");
661 include("doctext.inc");
663 require_once($path_to_root . "reporting/includes/class.mail.inc");
664 $mail = new email($this->company['coy_name'], $this->company['email']);
665 $from = $this->company['coy_name'] . " <" . $this->company['email'] . ">";
666 $to = $myrow['DebtorName'] . " <" . $myrow['email'] . ">";
667 $msg = $doc_Dear_Sirs . ",\n\n" . $doc_AttachedFile . " " . $subject .
669 if ($myrow['dimension_id'] > 0 && $doctype == 10) // helper for payment links
671 if ($myrow['dimension_id'] == 1)
673 $amt = number_format($myrow["ov_freight"] + $myrow["ov_gst"] + $myrow["ov_amount"], user_price_dec());
674 $txt = $doc_Payment_Link . " PayPal: ";
675 $nn = urlencode($this->title . " " . $myrow['reference']);
676 $url = "https://www.paypal.com/xclick/business=" . $this->company['email'] . "&item_name=" .
677 $nn . "&amount=" . $amt . "¤cy_code=" . $myrow['curr_code'];
678 $msg .= $txt . $url . "\n\n";
681 $msg .= $doc_Kindest_regards . "\n\n";
682 $sender = $this->user . "\n" . $this->company['coy_name'];
684 $mail->subject($subject);
685 $mail->text($msg . $sender);
686 $mail->attachment($fname);
687 $ret = $mail->send();
688 if (1 == 1) // just for fun and for debugging purposes!!
690 $from = str_replace("<", "(", $from);
691 $from = str_replace(">", ")", $from);
692 $to = str_replace("<", "(", $to);
693 $to = str_replace(">", ")", $to);
694 $msg2 = "<br>From: " . $from;
695 $msg2 .= "<br>To: " . $to;
696 $msg2 .= "<br>Subject: " . $subject;
697 $msg2 .= "<br>Msg: " . nl2br($msg);
698 $msg2 .= nl2br($sender) . "<br>";
699 $msg2 .= "<br>Filepath: " . $fname . "<br>Filename: " . $this->filename . "<br>";
702 $str = "<br>" . $this->title . " " . $myrow['reference'] . " " . _("sent to") . " ";
704 $str = "<br>" . $this->title . " " . $myrow['reference'] . " " . _("NOT sent to") . " ";
705 $msg2 .= $str . $myrow['DebtorName'] . " - " . $myrow['email'];
708 echo "</body></html>";
714 // <SCRIPT LANGUAGE="JavaScript"><!--
715 // function go_now () { window.location.href = "'.$fname.'"; }
718 // <body onLoad="go_now()"; >
719 // <a href="'.$fname.'">click here</a> if you are not re-directed.
722 header('Content-type: application/pdf');
723 header("Content-Disposition: inline; filename=$this->filename");
724 header('Expires: 0');
725 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
726 header('Pragma: public');
730 // also have a look through the directory, and remove the files that are older than a week
731 // rather want to save 'em
732 /*if ($d = @opendir($dir)) {
733 while (($file = readdir($d)) !== false) {
734 // then check to see if this one is too old
735 $ftime = filemtime($dir.'/'.$file);
736 if (time()-$ftime > 3600*24*7){
737 unlink($dir.'/'.$file);