[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/vtlib/Vtiger/PDF/inventory/ -> ContentViewer.php (source)

   1  <?php
   2  /*+**********************************************************************************
   3   * The contents of this file are subject to the vtiger CRM Public License Version 1.0
   4   * ("License"); You may not use this file except in compliance with the License
   5   * The Original Code is:  vtiger CRM Open Source
   6   * The Initial Developer of the Original Code is vtiger.
   7   * Portions created by vtiger are Copyright (C) vtiger.
   8   * All Rights Reserved.
   9   ************************************************************************************/
  10  
  11  include_once dirname(__FILE__) . '/../viewers/ContentViewer.php';
  12  
  13  class Vtiger_PDF_InventoryContentViewer extends Vtiger_PDF_ContentViewer {
  14  
  15      protected $headerRowHeight = 8;
  16      protected $onSummaryPage   = false;
  17  
  18  	function __construct() {
  19          // NOTE: General A4 PDF width ~ 189 (excluding margins on either side)
  20              
  21          $this->cells = array( // Name => Width
  22              'Code'    => 30,
  23              'Name'    => 55,
  24              'Quantity'=> 20,
  25              'Price'   => 20,
  26              'Discount'=> 19,
  27              'Tax'     => 16,
  28              'Total'   => 30
  29          );
  30      }
  31      
  32  	function initDisplay($parent) {
  33  
  34          $pdf = $parent->getPDF();
  35          $contentFrame = $parent->getContentFrame();
  36  
  37          $pdf->MultiCell($contentFrame->w, $contentFrame->h, "", 1, 'L', 0, 1, $contentFrame->x, $contentFrame->y);
  38          
  39          // Defer drawing the cell border later.
  40          if(!$parent->onLastPage()) {
  41              $this->displayWatermark($parent);
  42          }
  43          
  44          // Header    
  45          $offsetX = 0;
  46          $pdf->SetFont('','B');
  47          foreach($this->cells as $cellName => $cellWidth) {
  48              $cellLabel = ($this->labelModel)? $this->labelModel->get($cellName, $cellName) : $cellName;
  49              $pdf->MultiCell($cellWidth, $this->headerRowHeight, $cellLabel, 1, 'L', 0, 1, $contentFrame->x+$offsetX, $contentFrame->y);
  50              $offsetX += $cellWidth;
  51          }
  52          $pdf->SetFont('','');
  53          // Reset the y to use
  54          $contentFrame->y += $this->headerRowHeight;
  55      }
  56      
  57  	function drawCellBorder($parent, $cellHeights=False) {        
  58          $pdf = $parent->getPDF();
  59          $contentFrame = $parent->getContentFrame();
  60          
  61          if(empty($cellHeights)) $cellHeights = array();
  62  
  63          $offsetX = 0;
  64          foreach($this->cells as $cellName => $cellWidth) {
  65              $cellHeight = isset($cellHeights[$cellName])? $cellHeights[$cellName] : $contentFrame->h;
  66  
  67              $offsetY = $contentFrame->y-$this->headerRowHeight;            
  68              
  69              $pdf->MultiCell($cellWidth, $cellHeight, "", 1, 'L', 0, 1, $contentFrame->x+$offsetX, $offsetY);
  70              $offsetX += $cellWidth;
  71          }
  72      }
  73  
  74  	function display($parent) {
  75          $this->displayPreLastPage($parent);
  76          $this->displayLastPage($parent);
  77      }
  78  
  79  	function displayPreLastPage($parent) {
  80  
  81          $models = $this->contentModels;
  82  
  83          $totalModels = count($models);
  84          $pdf = $parent->getPDF();
  85  
  86          $parent->createPage();
  87          $contentFrame = $parent->getContentFrame();
  88  
  89          $contentLineX = $contentFrame->x; $contentLineY = $contentFrame->y;
  90          $overflowOffsetH = 8; // This is offset used to detect overflow to next page
  91          for ($index = 0; $index < $totalModels; ++$index) {
  92              $model = $models[$index];
  93              
  94              $contentHeight = 1;
  95              
  96              // Determine the content height to use
  97              foreach($this->cells as $cellName => $cellWidth) {
  98                  $contentString = $model->get($cellName);
  99                  if(empty($contentString)) continue;
 100                  $contentStringHeight = $pdf->GetStringHeight($contentString, $cellWidth);
 101                  if ($contentStringHeight > $contentHeight) $contentHeight = $contentStringHeight;
 102              }
 103              
 104              // Are we overshooting the height?
 105              if(ceil($contentLineY + $contentHeight) > ceil($contentFrame->h+$contentFrame->y)) {
 106              
 107                  $this->drawCellBorder($parent);
 108                  $parent->createPage();
 109  
 110                  $contentFrame = $parent->getContentFrame();
 111                  $contentLineX = $contentFrame->x; $contentLineY = $contentFrame->y;
 112              }
 113  
 114              $offsetX = 0;
 115              foreach($this->cells as $cellName => $cellWidth) {
 116                  $pdf->MultiCell($cellWidth, $contentHeight, $model->get($cellName), 0, 'L', 0, 1, $contentLineX+$offsetX, $contentLineY);
 117                  $offsetX += $cellWidth;
 118              }
 119              
 120              $contentLineY = $pdf->GetY();
 121              
 122              $commentContent = $model->get('Comment');
 123              
 124              if (!empty($commentContent)) {
 125                  $commentCellWidth = $this->cells['Name'];
 126                  $offsetX = $this->cells['Code'];
 127                  
 128                  $contentHeight = $pdf->GetStringHeight($commentContent, $commentCellWidth);            
 129                  if(ceil($contentLineY + $contentHeight + $overflowOffsetH) > ceil($contentFrame->h+$contentFrame->y)) {
 130                      
 131                      $this->drawCellBorder($parent);
 132                      $parent->createPage();
 133  
 134                      $contentFrame = $parent->getContentFrame();
 135                      $contentLineX = $contentFrame->x; $contentLineY = $contentFrame->y;
 136                  }            
 137                  $pdf->MultiCell($commentCellWidth, $contentHeight, $model->get('Comment'), 0, 'L', 0, 1, $contentLineX+$offsetX,
 138                       $contentLineY);
 139                       
 140                  $contentLineY = $pdf->GetY();
 141              }
 142          }
 143  
 144          // Summary
 145          $cellHeights = array();
 146          
 147          if ($this->contentSummaryModel) {
 148              $summaryCellKeys = $this->contentSummaryModel->keys(); $summaryCellCount = count($summaryCellKeys);
 149          
 150              $summaryCellLabelWidth = $this->cells['Quantity'] + $this->cells['Price'] + $this->cells['Discount'] + $this->cells['Tax'];
 151              $summaryCellHeight = $pdf->GetStringHeight("TEST", $summaryCellLabelWidth); // Pre-calculate cell height
 152          
 153              $summaryTotalHeight = ceil(($summaryCellHeight * $summaryCellCount));
 154      
 155              if (($contentFrame->h+$contentFrame->y) - ($contentLineY+$overflowOffsetH)  < $summaryTotalHeight) { //$overflowOffsetH is added so that last Line Item is not overlapping
 156                  $this->drawCellBorder($parent);
 157                  $parent->createPage();
 158                      
 159                  $contentFrame = $parent->getContentFrame();
 160                  $contentLineX = $contentFrame->x; $contentLineY = $contentFrame->y;
 161              }
 162                  
 163              $summaryLineX = $contentLineX + $this->cells['Code'] + $this->cells['Name'];        
 164              $summaryLineY = ($contentFrame->h+$contentFrame->y-$this->headerRowHeight)-$summaryTotalHeight;
 165          
 166              foreach($summaryCellKeys as $key) {    
 167                  $pdf->MultiCell($summaryCellLabelWidth, $summaryCellHeight, $key, 1, 'L', 0, 1, $summaryLineX, $summaryLineY);
 168                  $pdf->MultiCell($contentFrame->w-$summaryLineX+10-$summaryCellLabelWidth, $summaryCellHeight, 
 169                      $this->contentSummaryModel->get($key), 1, 'R', 0, 1, $summaryLineX+$summaryCellLabelWidth, $summaryLineY);
 170                  $summaryLineY = $pdf->GetY();
 171              }
 172          
 173              $cellIndex = 0;
 174              foreach($this->cells as $cellName=>$cellWidth) {
 175                  if ($cellIndex < 2) $cellHeights[$cellName] = $contentFrame->h;
 176                  else $cellHeights[$cellName] = $contentFrame->h - $summaryTotalHeight;
 177                  ++$cellIndex;
 178              }
 179          }
 180          $this->onSummaryPage = true;
 181          $this->drawCellBorder($parent, $cellHeights);
 182      }
 183  
 184  	function displayLastPage($parent) {
 185          // Add last page to take care of footer display
 186          if($parent->createLastPage()) {
 187              $this->onSummaryPage = false;
 188          }
 189      }
 190  
 191  	function drawStatusWaterMark($parent) {
 192          $pdf = $parent->getPDF();
 193  
 194          $waterMarkPositions=array("30","180");
 195          $waterMarkRotate=array("45","50","180");
 196  
 197          $pdf->SetFont('Arial','B',50);
 198          $pdf->SetTextColor(230,230,230);
 199          $pdf->Rotate($waterMarkRotate[0], $waterMarkRotate[1], $waterMarkRotate[2]);
 200          $pdf->Text($waterMarkPositions[0], $waterMarkPositions[1], 'created');
 201          $pdf->Rotate(0);
 202          $pdf->SetTextColor(0,0,0);
 203      }
 204  }


Generated: Fri Nov 28 20:08:37 2014 Cross-referenced by PHPXref 0.7.1