Improving Quotes - Create PDF

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Improving Quotes - Create PDF

tbannister
Hi, I'm not sure if this is the correct place to send this, but I've
done a little work fixing up the CreatePDF section of the Quotes
module.  I've included a patch file of the changes.  Most of my changes
were aimed at either making the output more professional or increasing
the modularity of the code.

--
Tyler Bannister
NetAccess Systems Inc.
905-524-2001 xt432

--- CreatePDF.php.orig 2005-10-17 13:51:06.155957432 -0400
+++ CreatePDF.php 2005-10-17 13:23:44.506526080 -0400
@@ -1,29 +1,40 @@
 <?php
-/*********************************************************************************
-** The contents of this file are subject to the vtiger CRM Public License Version 1.0
- * ("License"); You may not use this file except in compliance with the License
+/****************************************************************************
+ * The contents of this file are subject to the vtiger CRM Public License
+ * Version 1.0 ("License"); You may not use this file except in compliance
+ * with the License
+ *
  * The Original Code is:  vtiger CRM Open Source
  * The Initial Developer of the Original Code is vtiger.
  * Portions created by vtiger are Copyright (C) vtiger.
  * All Rights Reserved.
-*
- ********************************************************************************/
+ ****************************************************************************/
+
 require('include/fpdf/fpdf.php');
 require_once('modules/Quotes/Quote.php');
 
+define('QUOTE_BILLING_HEADER', $app_strings['QUOTE_BILLING_HEADER']);
+define('QUOTE_SERVICE_HEADER', $app_strings['QUOTE_SERVICE_HEADER']);
+define('QUOTE_TITLE',          $app_strings['QUOTE_TITLE']);
+define('QUOTE_SERVICE_NUMBER', $app_strings['QUOTE_SERVICE_NUMBER']);
+define('QUOTE_DATE',           $app_strings['QUOTE_DATE']);
+define('QUOTE_SALESPERSON',    $app_strings['QUOTE_SALESPERSON']);
+
 $id = $_REQUEST['record'];
 global $adb;
 //retreiving the invoice info
 $focus = new Quote();
 $focus->retrieve_entity_info($_REQUEST['record'],"Quotes");
 $account_name = getAccountName($focus->column_fields[account_id]);
-$iData[] = $account_name;
+$iData = array($account_name);
 $iData[] = $id;
 $iData[] = date('Y-m-d');
 
 //setting the Customer Data
-$iCustData[] = $account_name;
+$iCustData = array($account_name);
 
+$title = $focus->column_fields['subject'];
+$salesperson = $current_user->first_name .' '. $current_user->last_name;
 if($focus->column_fields["duedate"] != '')
 {
  $due_date = $focus->column_fields["validtill"];
@@ -35,7 +46,7 @@
 $iCustData[] = $due_date;
 
 //setting the billing address
-$bdata[] = $account_name;
+$bdata = array('', $account_name);
 if($focus->column_fields["bill_street"] != '')
 {
         $bill_street = $focus->column_fields["bill_street"];
@@ -70,16 +81,16 @@
  $bdata[] = $bill_country;
 }
 
-for($i =0; $i <5; $i++)
+for($i =0; $i <10; $i++)
 {
- if(sizeof($bdata) < 6)
+ if(sizeof($bdata) < 11)
  {
  $bdata[] = ' ';
  }
 }
 
 //setting the shipping address
-$sdata[] = $account_name;
+$sdata = array('', $account_name);
 if($focus->column_fields["ship_street"] != '')
 {
         $ship_street = $focus->column_fields["ship_street"];
@@ -113,9 +124,9 @@
  $sdata[] = $ship_country;
 }
 
-for($i =0; $i <5; $i++)
+for($i =0; $i <10; $i++)
 {
- if(sizeof($sdata) < 6)
+ if(sizeof($sdata) < 11)
  {
  $sdata[] = ' ';
  }
@@ -143,14 +154,14 @@
 
 if($num_rows == 1)
 {
- $org_name = $adb->query_result($result,0,"organizationame");
+ $org_name    = $adb->query_result($result,0,"organizationame");
  $org_address = $adb->query_result($result,0,"address");
- $org_city = $adb->query_result($result,0,"city");
- $org_state = $adb->query_result($result,0,"state");
+ $org_city    = $adb->query_result($result,0,"city");
+ $org_state   = $adb->query_result($result,0,"state");
  $org_country = $adb->query_result($result,0,"country");
- $org_code = $adb->query_result($result,0,"code");
- $org_phone = $adb->query_result($result,0,"phone");
- $org_fax = $adb->query_result($result,0,"fax");
+ $org_code    = $adb->query_result($result,0,"code");
+ $org_phone   = $adb->query_result($result,0,"phone");
+ $org_fax     = $adb->query_result($result,0,"fax");
  $org_website = $adb->query_result($result,0,"website");
 
  if($org_name != '')
@@ -159,19 +170,22 @@
  }
  if($org_address != '' || $org_city != '' || $org_state != '')
  {
- $companyaddress[] = $org_address.' '.$org_city.' '.$org_state;
+ $companyaddress[] = $org_address;
+ $companyaddress[] = $org_city .', '.$org_state;
  }
  if($org_country != '' || $org_code!= '')
  {
  $companyaddress[] = $org_country.' '.$org_code;
  }
+ $companyaddress[] = '';
  if($org_phone != '' || $org_fax != '')
  {
- $companyaddress[] = $org_phone.' '.$org_fax;
+ $companyaddress[] = $app_strings['Phone_Short'] .': '. $org_phone;
+ $companyaddress[] = $app_strings['Fax'] .': '. $org_fax;
  }
  if($org_website != '')
  {
- $companyaddress[] = $org_website;
+ $companyaddress[] = $app_strings['Website_Short']. ': '. $org_website;
  }
 
  for($i =0; $i < 4; $i++)
@@ -219,113 +233,177 @@
 
 class PDF extends FPDF
 {
-
-// Invoice Title
-function setInvoiceTitle($title,$logo_name,$caddress)
-{
- if($title != "")
- {
- if(isset($logo_name) && $logo_name != '')
- {
- $this->Image('test/logo/'.$logo_name,10,10,0,0);
- }
- else
- {
- //$this->Image('themes/Aqua/images/blank.jpg',10,10,0,0);
- }
- for($i=0;$i<count($caddress);$i++)
- {
-
- $this->Ln();
- $this->Cell(40);
- $this->SetFont('','',10);
- $this->Cell(0,5,$caddress[$i],0,0,'L',0);
- }
- $this->Ln();
- $this->SetFillColor(224,235,255);
-     $this->SetTextColor(0);
-     $this->SetFont('','B',18);
-     $this->Cell(0,10,$title,0,0,'C',0);
-
- }
+  var $table_fill_color    = array(224,235,255);
+  var $table_text_color    = array(  0,  0,  0);
+  var $table_border_color  = array(  0,  0,  0);
+  var $header_fill_color   = array(  0,  0,  0);
+  var $header_text_color   = array(255,255,255);
+  var $other_fill_color    = array(233,241,253);
+  var $other_text_color    = array(  0,  0,  0);
+  var $title_text_color    = array(  0,  0,  0);
+  var $divider_color       = array(  0,  0,  0);
+  var $pdf_font            = 'Arial';
+  var $header_image        = '';
+  var $header_title        = '';
+  var $header_address      = '';
+
+function PDF($font = 'Arial', $orientation = 'P',
+             $unit = 'mm', $format = 'A4') {
+  $this->FPDF($orientation,$unit,$format);
+  $this->pdf_font = $font;
+  $this->SetFont($this->pdf_font,'',10);
+}
+
+function Header() {
+  if ($this->header_image != '') {
+    $this->Image($this->header_image,10,10,0,0);
+  } else {
+//  $this->Image('themes/Aqua/images/blank.jpg',10,10,0,0);
+  }
+  if ($this->header_title != '') {
+    $this->SetFillColor($this->pdf_fill_color[0],
+                        $this->pdf_fill_color[1],
+                        $this->pdf_fill_color[2]);
+    $this->SetTextColor($this->title_text_color[0],
+                        $this->title_text_color[1],
+                        $this->title_text_color[2]);
+    $this->Cell(80);
+    $this->SetFont($this->pdf_font,'B',18);
+    $this->Cell(30,10,$this->header_title,0,0,'C',0);
+  }
+  $this->SetY(5);
+  if (is_array($this->header_address)) {
+    for($i=0;$i<count($this->header_address);$i++) {
+      $this->Cell(150);
+      $this->SetFont($this->pdf_font,'',8);
+      $this->Cell(0,5,$this->header_address[$i],0,0,'L',0);
+      $this->Ln(3.2);
+    }
+  }
 }
-//Invoice Address
-function setAddress($billing="",$shipping="")
-{
-
- $this->Ln();
- $this->SetFillColor(224,235,255);
- $this->SetTextColor(0);
-     $this->SetFont('','B',10);
- $this->Cell(130,10,"Bill To:",0,0,'L',0);
- $this->Cell(0,10,"Ship To:",0,0,'L',0);
- for($i=0;$i<count($billing);$i++)
- {
- $this->Cell(17);
- $this->SetFont('','',10);
- $this->Cell(130,5,$billing[$i],0,0,'L',0);
- $this->Cell(0,5,$shipping[$i],0,0,'L',0);
- $this->Ln();
- }
 
-}
-//Invoice from
-function setInvoiceDetails($iHeader,$iData)
-{
-    $this->Ln();
-    $this->SetFillColor(162,200,243);
-    $this->SetTextColor(0);
-    $this->SetDrawColor(61,121,206);
-    //$this->SetLineWidth(.3);
-    $this->SetFont('Arial','B',10);
-    //Header
-    $this->Cell(15);
-    foreach($iHeader as $value)
-    {
-        $this->Cell(40,7,$value,1,0,'L',1);
+function Footer() {
+  $this->SetY(-20);
+  $this->Cell(0,8,"Terms & Conditions: ",0,0,'C',0);
+  $this->Ln();
+  $this->Cell(0,8,$conditions,0,0,'L',0);
+}
+
+function drawHeader($header,$width=40,$height=7) {
+  $this->SetFillColor($this->header_fill_color[0],
+                      $this->header_fill_color[1],
+                      $this->header_fill_color[2]);
+  $this->SetTextColor($this->header_text_color[0],
+                      $this->header_text_color[1],
+                      $this->header_text_color[2]);
+  $this->SetDrawColor($this->table_border_color[0],
+                      $this->table_border_color[1],
+                      $this->table_border_color[2]);
+  $this->SetFont($this->pdf_font,'B',10);
+  $this->Cell($width,$height,$header,1,0,'C',1);
+  $this->Ln();
+}
+
+function drawCell($data,$width=40,$height=7) {
+  $this->SetFont($this->pdf_font,'',8);
+  $this->SetFillColor($this->table_fill_color[0],
+                      $this->table_fill_color[1],
+                      $this->table_fill_color[2]);
+  $this->SetTextColor($this->table_text_color[0],
+                      $this->table_text_color[1],
+                      $this->table_text_color[2]);
+  $this->Cell($width,$height,$data,'',0,'C',1);
+  $this->Ln($height);
+}
+
+// $headers should be a one-dimensional array of headers
+// $data should be a two-dimensional array of data
+// $col_size should be a one-dimensional array of column widths
+function drawTable($headers, $data, $col_size = '', $x = -1, $y = -1) {
+  if ($x != -1) {
+    $this->SetLeftMargin($x);
+    $this->SetX($x);
+  }
+  if ($y != -1) {
+    $this->SetY($y);
+  }
+  if ($col_size == '') {
+    $col_size = array_fill(0,sizeof($headers)-1, 190 / sizeof($headers));
+  }
+  $this->SetFillColor($this->header_fill_color[0],
+                      $this->header_fill_color[1],
+                      $this->header_fill_color[2]);
+  $this->SetTextColor($this->header_text_color[0],
+                      $this->header_text_color[1],
+                      $this->header_text_color[2]);
+  $this->SetDrawColor($this->table_border_color[0],
+                      $this->table_border_color[1],
+                      $this->table_border_color[2]);
+  $this->SetFont($this->pdf_font,'B',10);
+  foreach ($headers as $num => $cell) {
+    $this->Cell($col_size[$num],7,$cell,1,0,'C',1);
+  }
+  $this->Ln();
+
+  $size = 3.2;
+  $this->SetFont($this->pdf_font,'',8);
+  $this->SetFillColor($this->table_fill_color[0],
+                      $this->table_fill_color[1],
+                      $this->table_fill_color[2]);
+  $this->SetTextColor($this->table_text_color[0],
+                      $this->table_text_color[1],
+                      $this->table_text_color[2]);
+  $borders = 'LR';
+  foreach ($data as $row => $set) {
+    if ($row == sizeof($data) - 1) {
+      $borders = 'LRB';
     }
-    $this->Ln();
-    $this->SetFillColor(233,241,253);
-    $this->SetTextColor(0);
-    $this->SetFont('');
-    //Data
-    $this->Cell(15);
-    $fill=0;
-    foreach($iData as $value)
-    {
- $this->Cell(40,6,$value,1,0,'L',0);
+    if (is_array($set)) {
+      foreach($set as $num => $cell) {
+        $this->Cell($col_size[$num],$size,$cell,$borders,0,'L',1);
+      }
+    } else {
+        $this->Cell($col_size[$num],$size,$set,$borders,0,'L',1);
     }
-    $this->Ln();
+    $this->Ln($size);
+  }
+  if ($x != -1) {
+    $this->SetLeftMargin(10);
+    $this->SetX(10);
+  }
 }
 
-//customer Details
-function setCustomerDetails($iCHeader,$iCData)
-{
-    $this->Ln();
-    //$this->Cell(0);
-    $this->SetFillColor(162,200,243);
-    $this->SetTextColor(0);
-    $this->SetDrawColor(61,121,206);
-    //$this->SetLineWidth(.3);
-    $this->SetFont('Arial','B',10);
-    //Header
-    //$this->Cell(15);
-    foreach($iCHeader as $value)
-    {
-        $this->Cell(63,7,$value,1,0,'L',1);
-    }
-    $this->Ln();
-    $this->SetFillColor(233,241,253);
-    $this->SetTextColor(0);
-    $this->SetFont('');
-    //Data
-    //$this->Cell(15);
-    $fill=0;
-    foreach($iCData as $value)
-    {
- $this->Cell(63,6,$value,1,0,'L',0);
+// Invoice Title
+function setInvoiceTitle($title,$logo_name,$caddress) {
+  if($title != '') {
+    if(isset($logo_name) && $logo_name != '') {
+      $this->header_image = 'test/logo/'.$logo_name;
     }
-    $this->Ln();
+    $this->header_address = $caddress;    
+    $this->header_title   = $title;
+  }
+}
+
+// Invoice Address
+// Both $billing and $shipping should be arrays
+function setAddress($billing=array(),$shipping=array()) {
+  $this->drawTable(array(QUOTE_BILLING_HEADER),$billing, array(65),10,35);
+  $this->drawTable(array(QUOTE_SERVICE_HEADER),$shipping,array(65),85,35);
+}
+
+function setInvoiceSummary($serviceid, $salesperson = '') {
+  $this->SetLeftMargin(160);
+  $this->SetX(160);
+  $this->SetY(35);
+  $date = date('F j, Y');
+  $this->drawHeader(QUOTE_SERVICE_NUMBER);
+  $this->drawCell($serviceid);
+  $this->drawHeader(QUOTE_DATE);
+  $this->drawCell($date);
+  $this->drawHeader(QUOTE_SALESPERSON);
+  $this->drawCell($salesperson);
+  $this->SetLeftMargin(10);
+  $this->SetX(10);
 }
 
 //Product Details
@@ -333,46 +411,31 @@
 {
     $this->Ln();
     $this->Ln();
-    $this->SetFillColor(162,200,243);
-    $this->SetTextColor(0);
-    $this->SetDrawColor(61,121,206);
-    $this->SetLineWidth(.3);
-    $this->SetFont('Arial','B',10);
-    //Header
-    foreach($ivHeader as $value)
-    {
-        $this->Cell(38,7,$value,0,0,'L',0);
+    $data = array();
+    $col_sizes = array(20,110,20,20,20);
+    foreach($ivData as $key=>$value) {
+      $data[] = array($value['qty'], $value['productname'], $value['listprice'],
+                   $value['unitprice'], $value['total']);
     }
-    $this->Ln();
-    $this->SetDrawColor(0,0,0);
-    $this->SetLineWidth(.5);
-    $this->line(10,140,200,140);
-    $this->SetFillColor(233,241,253);
-    $this->SetTextColor(0);
-    $this->SetFont('');
-    //Data
-    $fill=0;
-     foreach($ivData as $key=>$value)
- {
-     $this->Cell(38,6,$value['productname'],0,0,'L',0);
- $this->Cell(38,6,$value['qty'],0,0,'L',0);
- $this->Cell(38,6,$value['unitprice'],0,0,'L',0);
- $this->Cell(38,6,$value['listprice'],0,0,'L',0);
- $this->Cell(38,6,$value['total'],0,0,'R',0);
- $this->Ln();
- }
+    $this->drawTable($ivHeader, $data, $col_sizes);
     $this->Ln();
 }
 
 function setTotal($price_total="",$conditions="")
 {
  $this->Ln();
- $this->SetDrawColor(0,0,0);
+ $this->SetDrawColor($this->divider_color[0],
+                            $this->divider_color[1],
+                            $this->divider_color[2]);
  $this->SetLineWidth(.3);
 // $this->line(10,200,200,200);
- $this->SetFillColor(224,235,255);
- $this->SetTextColor(0);
-     $this->SetFont('','B',10);
+ $this->SetFillColor($this->table_fill_color[0],
+                            $this->table_fill_color[1],
+                            $this->table_fill_color[2]);
+ $this->SetTextColor($this->table_text_color[0],
+                            $this->table_text_color[1],
+                            $this->table_text_color[2]);
+     $this->SetFont($this->pdf_font,'B',10);
  $this->Cell(140,6,"Sub Total: ",0,0,'R',0);
  $this->Cell(0,6,$price_total[0],1,0,'R',0);
      $this->Ln(4);
@@ -389,9 +452,6 @@
  $this->Cell(0,6,$price_total[3],1,0,'R',0);
  $this->Ln();
  $this->Ln();
- $this->Cell(0,8,"Terms & Conditions: ",0,0,'L',0);
- $this->Ln();
- $this->Cell(0,8,$conditions,0,0,'L',0);
 }
 }
 //$bdata = array("aaaaaaaaa","48/1,Katcherry Street","Rasipuram","Namakkal (D.T)");
@@ -399,16 +459,14 @@
 $iHead = array("Company","Quote No.","Date");
 $iCustHeadDtls = array("Customer Name","Valid Till");
 //$iCustData = array("Nortel Networks","usc-107565","26-05-2005");
-$iHeadDtls = array("Product Name","Quantity","List Price","Unit Price","Total");
+$iHeadDtls = array("Quantity", "Product Name","List Price","Unit Price","Total");
 
-$pdf = new PDF('P','mm','A4');
-$pdf->SetFont('Arial','',10);
+$pdf = new PDF();
+$pdf->setInvoiceTitle($title,$logo_name,$companyaddress);
 $pdf->AddPage();
-$pdf->setInvoiceTitle("Quotes",$logo_name,$companyaddress);
 $pdf->Ln();
-$pdf->setInvoiceDetails($iHead,$iData);
 $pdf->setAddress($bdata,$sdata);
-$pdf->setCustomerDetails($iCustHeadDtls,$iCustData);
+$pdf->setInvoiceSummary($id,$salesperson);
 $pdf->setProductDetails($iHeadDtls,$iDataDtls);
 $pdf->setTotal($price_total,$conditions);
 $pdf->Output();



-------------------------------------------------------
This SF.Net email is sponsored by:
Power Architecture Resource Center: Free content, downloads, discussions,
and more. http://solutions.newsforge.com/ibmarch.tmpl
_______________________________________________
vtigercrm-contributors mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/vtigercrm-contributors
Loading...