4 include_once($path_to_root . "reporting/includes/class.pdf.inc");
5 include_once($path_to_root . "admin/db/company_db.inc");
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 $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 = $path_to_root . "themes/default/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);
345 if ($print_invoice_no == 1)
346 $this->Text($mcol, $myrow['trans_no'], $mcol + 90);
348 $this->Text($mcol, $myrow['reference'], $mcol + 90);
350 $this->Text($mcol + 90, $myrow['debtor_no'], $mcol + 180);
351 if ($doctype == 8 || $doctype == 9)
352 $this->Text($mcol + 180, sql2date($myrow['ord_date']));
354 $this->Text($mcol + 180, sql2date($myrow['tran_date']));
356 $this->fontSize -= 4;
357 $this->row = $iline1 - 5;
358 $this->Text($ccol, $doc_Charge_To, $icol);
359 $this->Text($mcol, $doc_Delivered_To);
360 $this->fontSize += 4;
366 $this->Text($ccol, $myrow['name'], $icol);
372 $this->Text($ccol, $myrow['supp_name'], $icol);
374 $this->Text($ccol, $myrow['DebtorName'], $icol);
375 $adr = explode("\n", $myrow['address']);
377 for ($i = 0; $i < count($adr); $i++)
380 $this->Text($ccol, $adr[$i], $icol);
382 if ($sales_order != NULL)
386 $this->Text($mcol, $this->company['coy_name']);
388 $this->Text($mcol, $sales_order['deliver_to']);
389 $adr = explode("\n", $sales_order['delivery_address']);
390 for ($i = 0; $i < count($adr); $i++)
393 $this->Text($mcol, $adr[$i]);
396 $this->row = $iline2 - 2 * $this->lineHeight;
397 $this->Text($ccol, $doc_Shipping_Company . ":", $ccol2);
399 $this->Text($ccol2, $myrow['shipper_name'], $mcol);
400 $this->Text($mcol, $doc_Due_Date . ":", $mcol2);
402 $this->Text($mcol2, sql2date($myrow['delivery_date']));
403 else if ($doctype != 8)
404 $this->Text($mcol2, sql2date($myrow['due_date']));
408 $this->Text($ccol, $doc_Your_Ref . ":", $ccol2);
409 $this->Text($ccol2, $branch['contact_name'], $mcol);
410 $this->Text($mcol, $doc_Our_Ref . ":", $mcol2);
412 $id = $branch['salesman'];
413 $sql = "SELECT salesman_name FROM ".TB_PREF."salesman WHERE salesman_code='$id'";
414 $result = db_query($sql,"could not get sales person");
415 $row = db_fetch($result);
417 $this->Text($mcol2, $row['salesman_name']);
420 $this->Text($ccol, $doc_Your_VAT_no . ":", $ccol2);
422 $this->Text($ccol2, $myrow['tax_id'], $mcol);
423 $this->Text($mcol, $doc_Our_VAT_no . ":", $mcol2);
424 $this->Text($mcol2, $this->company['gst_no']);
426 $this->Text($ccol, $doc_Payment_Terms . ":", $ccol2);
428 $id = $myrow['payment_terms'];
429 $sql = "SELECT terms FROM ".TB_PREF."payment_terms WHERE terms_indicator='$id'";
430 $result = db_query($sql,"could not get paymentterms");
431 $row = db_fetch($result);
433 $this->Text($ccol2, $row["terms"], $mcol);
434 $this->Text($mcol, $doc_Our_Order_No . ":", $mcol2);
435 $this->Text($mcol2, $myrow['order_']);
437 $locale = $path_to_root . "lang/" . $_SESSION['language']->code . "/locale.inc";
438 if (file_exists($locale))
440 $header2include = true;
443 $this->row = $iline3 - $this->lineHeight - 2;
445 $count = count($this->headers);
446 for ($i = 0; $i < $count; $i++)
447 $this->TextCol($i, $i + 1, $this->headers[$i], -2);
449 $temp = $this->row - 2 * $this->lineHeight;
450 $this->row = $iline5 - $this->lineHeight - 6;
451 $this->Text($ccol, $doc_Please_Quote . " - " . $myrow['curr_code']);
453 if ($branch['disable_branch'] > 0 && $doctype == 10) // payment links
455 if ($branch['disable_branch'] == 1)
457 $amt = number_format($myrow["ov_freight"] + $myrow["ov_gst"] + $myrow["ov_amount"], user_price_dec());
458 $txt = $doc_Payment_Link . " PayPal: ";
459 $name = urlencode($this->title . " " . $myrow['reference']);
460 $url = "https://www.paypal.com/xclick/business=" . $this->company['email'] . "&item_name=" .
461 $name . "&amount=" . $amt . "¤cy_code=" . $myrow['curr_code'];
462 $this->fontSize -= 2;
463 $this->Text($ccol, $txt);
465 $this->SetTextColor(0, 0, 255);
466 $this->Text($ccol, $url);
467 $this->SetTextColor(0, 0, 0);
468 $this->addLink($url, $ccol, $this->row, $this->pageWidth - $this->rightMargin, $this->row + $this->lineHeight);
469 $this->fontSize += 2;
472 if ($this->params['comments'] != '')
476 $this->Text($ccol, $this->params['comments']);
480 $this->row = $lower - 5;
481 $this->fontSize -= 4;
483 $this->Text($ccol, $doc_Address, $ccol2 + 40);
484 $this->Text($ccol2 + 30, $doc_Phone_Fax_Email, $mcol);
485 $this->Text($mcol , $doc_Bank, $mcol2);
486 $this->Text($mcol2, $doc_Bank_Account);
487 $this->fontSize += 4;
489 $adrline = $this->row;
491 $adr = explode("\n", $this->company['postal_address']);
492 for ($i = 0; $i < count($adr); $i++)
494 $this->Text($ccol, $adr[$i], $ccol2 + 40);
497 $this->row = $adrline;
498 $this->Text($ccol2 + 30, $this->company['phone'], $mcol);
500 $this->Text($ccol2 + 30, $this->company['fax'], $mcol);
502 $this->Text($ccol2 + 30, $this->company['email'], $mcol);
504 $this->row = $adrline;
505 $this->Text($mcol, $bankaccount['bank_name'], $mcol2);
506 $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 $this->Text($mcol2, $bankaccount['bank_account_name']);
516 $this->Text($mcol2, $bankaccount['bank_account_number']);
520 function AddImage($logo, $x, $y, $w, $h)
523 $x = $this->pageWidth - $x - $w;
524 if (strpos($logo, ".png") || strpos($logo, ".PNG"))
525 $this->addPngFromFile($logo, $x, $y, $w, $h);
527 $this->addJpegFromFile($logo, $x, $y, $w, $h);
530 function SetDrawColor($r, $g, $b)
532 $this->setStrokeColor($r / 255, $g / 255, $b / 255);
535 function SetTextColor($r, $g, $b)
537 $this->setColor($r / 255, $g / 255, $b / 255);
540 function Text($c, $txt, $n=0, $corr=0, $r=0)
543 $n = $this->pageWidth - $this->rightMargin;
545 return $this->TextWrap($c, $this->row - $r, $n - $c + $corr, $txt, 'left');
548 function TextWrap($xpos, $ypos, $len, $str, $align = 'left')
553 $xpos = $this->pageWidth - $xpos - $len;
554 if ($align == 'left')
556 elseif ($align == 'right')
559 return $this->addTextWrap($xpos, $ypos, $len, $this->fontSize, $str, $align);
562 function TextCol($c, $n, $txt, $corr=0, $r=0)
564 return $this->TextWrap($this->cols[$c], $this->row - $r, $this->cols[$n] - $this->cols[$c] + $corr, $txt, $this->aligns[$c]);
567 function TextCol2($c, $n, $txt, $corr=0, $r=0)
569 return $this->TextWrap($this->cols2[$c], $this->row - $r, $this->cols2[$n] - $this->cols2[$c] + $corr, $txt, $this->aligns2[$c]);
572 function TextColLines($c, $n, $txt, $corr=0)
574 $str = Explode("\n", $txt);
575 for ($i = 0; $i < count($str); $i++)
580 $l = $this->TextCol($c, $n, $l, $corr);
587 function LineTo($from, $row, $to, $row2)
589 Cpdf::line($from, $row, $to, $row2);
592 function Line($row, $height = 0)
594 $this->setLineStyle($height + 1);
595 Cpdf::line($this->pageWidth - $this->rightMargin, $row ,$this->leftMargin, $row);
598 function NewLine($l=1, $np=0)
600 $this->row -= ($l * $this->lineHeight);
601 if ($np > 0 && $this->row < $this->bottomMargin + ($np * $this->lineHeight))
605 function End($email=0, $subject=null, $myrow=null, $doctype = 0)
607 global $go_debug, $path_to_root;
609 // this is no good in IE so has been replaced, see down under
610 // but good for debugging purposes in IE
611 //session_write_close();
615 $buf = $this->output(1);
618 $pdfcode = str_replace("\n", "\n<br>", htmlspecialchars($pdfcode));
621 echo '</body></html>';
622 //header("Content-Length: $len");
623 //header("Content-Disposition: inline; filename=" . $this->filename);
624 //header('Expires: 0');
625 //header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
626 //header('Pragma: public');
628 //$this->pdf->stream();
632 $buf = $this->output();
634 $dir = './pdf_files';
636 if (!file_exists($dir))
640 $fname = $dir . '/' . $this->filename;
641 $fp = fopen($fname,'w');
647 if ($this->currency != $myrow['curr_code'])
649 include("doctext2.inc");
653 include("doctext.inc");
655 require_once($path_to_root . "reporting/includes/class.mail.inc");
656 $mail = new email($this->company['coy_name'], $this->company['email']);
657 $from = $this->company['coy_name'] . " <" . $this->company['email'] . ">";
658 $to = $myrow['DebtorName'] . " <" . $myrow['email'] . ">";
659 $msg = $doc_Dear_Sirs . ",\n\n" . $doc_AttachedFile . " " . $subject .
661 if ($myrow['dimension_id'] > 0 && $doctype == 10) // helper for payment links
663 if ($myrow['dimension_id'] == 1)
665 $amt = number_format($myrow["ov_freight"] + $myrow["ov_gst"] + $myrow["ov_amount"], user_price_dec());
666 $txt = $doc_Payment_Link . " PayPal: ";
667 $nn = urlencode($this->title . " " . $myrow['reference']);
668 $url = "https://www.paypal.com/xclick/business=" . $this->company['email'] . "&item_name=" .
669 $nn . "&amount=" . $amt . "¤cy_code=" . $myrow['curr_code'];
670 $msg .= $txt . $url . "\n\n";
673 $msg .= $doc_Kindest_regards . "\n\n";
674 $sender = $this->user . "\n" . $this->company['coy_name'];
676 $mail->subject($subject);
677 $mail->text($msg . $sender);
678 $mail->attachment($fname);
679 $ret = $mail->send();
680 if (1 == 1) // just for fun and for debugging purposes!!
682 $from = str_replace("<", "(", $from);
683 $from = str_replace(">", ")", $from);
684 $to = str_replace("<", "(", $to);
685 $to = str_replace(">", ")", $to);
686 $msg2 = "<br>From: " . $from;
687 $msg2 .= "<br>To: " . $to;
688 $msg2 .= "<br>Subject: " . $subject;
689 $msg2 .= "<br>Msg: " . nl2br($msg);
690 $msg2 .= nl2br($sender) . "<br>";
691 $msg2 .= "<br>Filepath: " . $fname . "<br>Filename: " . $this->filename . "<br>";
694 $str = "<br>" . $this->title . " " . $myrow['reference'] . " " . _("sent to") . " ";
696 $str = "<br>" . $this->title . " " . $myrow['reference'] . " " . _("NOT sent to") . " ";
697 $msg2 .= $str . $myrow['DebtorName'] . " - " . $myrow['email'];
700 echo "</body></html>";
706 // <SCRIPT LANGUAGE="JavaScript"><!--
707 // function go_now () { window.location.href = "'.$fname.'"; }
710 // <body onLoad="go_now()"; >
711 // <a href="'.$fname.'">click here</a> if you are not re-directed.
714 header('Content-type: application/pdf');
715 header("Content-Disposition: inline; filename=$this->filename");
716 header('Expires: 0');
717 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
718 header('Pragma: public');
722 // also have a look through the directory, and remove the files that are older than a week
723 // rather want to save 'em
724 /*if ($d = @opendir($dir)) {
725 while (($file = readdir($d)) !== false) {
726 // then check to see if this one is too old
727 $ftime = filemtime($dir.'/'.$file);
728 if (time()-$ftime > 3600*24*7){
729 unlink($dir.'/'.$file);