[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/include/ -> InventoryPDFController.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  
  12  include_once  'vtlib/Vtiger/PDF/models/Model.php';
  13  include_once  'vtlib/Vtiger/PDF/inventory/HeaderViewer.php';
  14  include_once  'vtlib/Vtiger/PDF/inventory/FooterViewer.php';
  15  include_once  'vtlib/Vtiger/PDF/inventory/ContentViewer.php';
  16  include_once  'vtlib/Vtiger/PDF/inventory/ContentViewer2.php';
  17  include_once  'vtlib/Vtiger/PDF/viewers/PagerViewer.php';
  18  include_once  'vtlib/Vtiger/PDF/PDFGenerator.php';
  19  include_once  'data/CRMEntity.php';
  20  
  21  class Vtiger_InventoryPDFController {
  22  
  23      protected $module;
  24      protected $focus = null;
  25  
  26  	function __construct($module) {
  27          $this->moduleName = $module;
  28      }
  29  
  30  	function loadRecord($id) {
  31          global $current_user;
  32          $this->focus = $focus = CRMEntity::getInstance($this->moduleName);
  33          $focus->retrieve_entity_info($id,$this->moduleName);
  34          $focus->apply_field_security();
  35          $focus->id = $id;
  36          $this->associated_products = getAssociatedProducts($this->moduleName,$focus);
  37      }
  38  
  39  	function getPDFGenerator() {
  40          return new Vtiger_PDF_Generator();
  41      }
  42  
  43  	function getContentViewer() {
  44          if($this->focusColumnValue('hdnTaxType') == "individual") {
  45              $contentViewer = new Vtiger_PDF_InventoryContentViewer();
  46          } else {
  47              $contentViewer = new Vtiger_PDF_InventoryTaxGroupContentViewer();
  48          }
  49          $contentViewer->setContentModels($this->buildContentModels());
  50          $contentViewer->setSummaryModel($this->buildSummaryModel());
  51          $contentViewer->setLabelModel($this->buildContentLabelModel());
  52          $contentViewer->setWatermarkModel($this->buildWatermarkModel());
  53          return $contentViewer;
  54      }
  55  
  56  	function getHeaderViewer() {
  57          $headerViewer = new Vtiger_PDF_InventoryHeaderViewer();
  58          $headerViewer->setModel($this->buildHeaderModel());
  59          return $headerViewer;
  60      }
  61  
  62  	function getFooterViewer() {
  63          $footerViewer = new Vtiger_PDF_InventoryFooterViewer();
  64          $footerViewer->setModel($this->buildFooterModel());
  65          $footerViewer->setLabelModel($this->buildFooterLabelModel());
  66          $footerViewer->setOnLastPage();
  67          return $footerViewer;
  68      }
  69  
  70  	function getPagerViewer() {
  71          $pagerViewer = new Vtiger_PDF_PagerViewer();
  72          $pagerViewer->setModel($this->buildPagermodel());
  73          return $pagerViewer;
  74      }
  75  
  76  	function Output($filename, $type) {
  77          if(is_null($this->focus)) return;
  78  
  79          $pdfgenerator = $this->getPDFGenerator();
  80  
  81          $pdfgenerator->setPagerViewer($this->getPagerViewer());
  82          $pdfgenerator->setHeaderViewer($this->getHeaderViewer());
  83          $pdfgenerator->setFooterViewer($this->getFooterViewer());
  84          $pdfgenerator->setContentViewer($this->getContentViewer());
  85  
  86          $pdfgenerator->generate($filename, $type);
  87      }
  88  
  89  
  90      // Helper methods
  91  
  92  	function buildContentModels() {
  93          $associated_products = $this->associated_products;
  94          $contentModels = array();
  95          $productLineItemIndex = 0;
  96          $totaltaxes = 0;
  97          $no_of_decimal_places = getCurrencyDecimalPlaces();
  98          foreach($associated_products as $productLineItem) {
  99              ++$productLineItemIndex;
 100  
 101              $contentModel = new Vtiger_PDF_Model();
 102  
 103              $discountPercentage  = 0.00;
 104              $total_tax_percent = 0.00;
 105              $producttotal_taxes = 0.00;
 106              $quantity = ''; $listPrice = ''; $discount = ''; $taxable_total = '';
 107              $tax_amount = ''; $producttotal = '';
 108  
 109  
 110              $quantity    = $productLineItem["qty{$productLineItemIndex}"];
 111              $listPrice    = $productLineItem["listPrice{$productLineItemIndex}"];
 112              $discount    = $productLineItem["discountTotal{$productLineItemIndex}"];
 113              $taxable_total = $quantity * $listPrice - $discount;
 114              $taxable_total = number_format($taxable_total, $no_of_decimal_places,'.','');
 115              $producttotal = $taxable_total;
 116              if($this->focus->column_fields["hdnTaxType"] == "individual") {
 117                  for($tax_count=0;$tax_count<count($productLineItem['taxes']);$tax_count++) {
 118                      $tax_percent = $productLineItem['taxes'][$tax_count]['percentage'];
 119                      $total_tax_percent += $tax_percent;
 120                      $tax_amount = (($taxable_total*$tax_percent)/100);
 121                      $producttotal_taxes += $tax_amount;
 122                  }
 123              }
 124  
 125              $producttotal_taxes = number_format($producttotal_taxes, $no_of_decimal_places,'.','');
 126              $producttotal = $taxable_total+$producttotal_taxes;
 127              $producttotal = number_format($producttotal, $no_of_decimal_places,'.','');
 128              $tax = $producttotal_taxes;
 129              $totaltaxes += $tax;
 130              $totaltaxes = number_format($totaltaxes, $no_of_decimal_places,'.','');
 131              $discountPercentage = $productLineItem["discount_percent{$productLineItemIndex}"];
 132              $productName = decode_html($productLineItem["productName{$productLineItemIndex}"]);
 133              //get the sub product
 134              $subProducts = $productLineItem["subProductArray{$productLineItemIndex}"];
 135              if($subProducts != '') {
 136                  foreach($subProducts as $subProduct) {
 137                      $productName .="\n"." - ".decode_html($subProduct);
 138                  }
 139              }
 140              $contentModel->set('Name', $productName);
 141              $contentModel->set('Code', decode_html($productLineItem["hdnProductcode{$productLineItemIndex}"]));
 142              $contentModel->set('Quantity', $quantity);
 143              $contentModel->set('Price',     $this->formatPrice($listPrice));
 144              $contentModel->set('Discount',  $this->formatPrice($discount)."\n ($discountPercentage%)");
 145              $contentModel->set('Tax',       $this->formatPrice($tax)."\n ($total_tax_percent%)");
 146              $contentModel->set('Total',     $this->formatPrice($producttotal));
 147              $contentModel->set('Comment',   decode_html($productLineItem["comment{$productLineItemIndex}"]));
 148  
 149              $contentModels[] = $contentModel;
 150          }
 151          $this->totaltaxes = $totaltaxes; //will be used to add it to the net total
 152  
 153          return $contentModels;
 154      }
 155  
 156  	function buildContentLabelModel() {
 157          $labelModel = new Vtiger_PDF_Model();
 158          $labelModel->set('Code',      getTranslatedString('Product Code',$this->moduleName));
 159          $labelModel->set('Name',      getTranslatedString('Product Name',$this->moduleName));
 160          $labelModel->set('Quantity',  getTranslatedString('Quantity',$this->moduleName));
 161          $labelModel->set('Price',     getTranslatedString('LBL_LIST_PRICE',$this->moduleName));
 162          $labelModel->set('Discount',  getTranslatedString('Discount',$this->moduleName));
 163          $labelModel->set('Tax',       getTranslatedString('Tax',$this->moduleName));
 164          $labelModel->set('Total',     getTranslatedString('Total',$this->moduleName));
 165          $labelModel->set('Comment',   getTranslatedString('Comment'),$this->moduleName);
 166          return $labelModel;
 167      }
 168  
 169  	function buildSummaryModel() {
 170          $associated_products = $this->associated_products;
 171          $final_details = $associated_products[1]['final_details'];
 172  
 173          $summaryModel = new Vtiger_PDF_Model();
 174  
 175          $netTotal = $discount = $handlingCharges =  $handlingTaxes = 0;
 176          $adjustment = $grandTotal = 0;
 177  
 178          $productLineItemIndex = 0;
 179          $sh_tax_percent = 0;
 180          foreach($associated_products as $productLineItem) {
 181              ++$productLineItemIndex;
 182              $netTotal += $productLineItem["netPrice{$productLineItemIndex}"];
 183          }
 184          $netTotal = number_format(($netTotal + $this->totaltaxes), getCurrencyDecimalPlaces(),'.', '');
 185          $summaryModel->set(getTranslatedString("Net Total", $this->moduleName), $this->formatPrice($netTotal));
 186  
 187          $discount_amount = $final_details["discount_amount_final"];
 188          $discount_percent = $final_details["discount_percentage_final"];
 189  
 190          $discount = 0.0;
 191          $discount_final_percent = '0.00';
 192          if($final_details['discount_type_final'] == 'amount') {
 193              $discount = $discount_amount;
 194          } else if($final_details['discount_type_final'] == 'percentage') {
 195              $discount_final_percent = $discount_percent;
 196              $discount = (($discount_percent*$final_details["hdnSubTotal"])/100);
 197          }
 198          $summaryModel->set(getTranslatedString("Discount", $this->moduleName)."($discount_final_percent%)", $this->formatPrice($discount));
 199  
 200          $group_total_tax_percent = '0.00';
 201          //To calculate the group tax amount
 202          if($final_details['taxtype'] == 'group') {
 203              $group_tax_details = $final_details['taxes'];
 204              for($i=0;$i<count($group_tax_details);$i++) {
 205                  $group_total_tax_percent += $group_tax_details[$i]['percentage'];
 206              }
 207              $summaryModel->set(getTranslatedString("Tax:", $this->moduleName)."($group_total_tax_percent%)", $this->formatPrice($final_details['tax_totalamount']));
 208          }
 209          //Shipping & Handling taxes
 210          $sh_tax_details = $final_details['sh_taxes'];
 211          for($i=0;$i<count($sh_tax_details);$i++) {
 212              $sh_tax_percent = $sh_tax_percent + $sh_tax_details[$i]['percentage'];
 213          }
 214          //obtain the Currency Symbol
 215          $currencySymbol = $this->buildCurrencySymbol();
 216  
 217          $summaryModel->set(getTranslatedString("Shipping & Handling Charges", $this->moduleName), $this->formatPrice($final_details['shipping_handling_charge']));
 218          $summaryModel->set(getTranslatedString("Shipping & Handling Tax:", $this->moduleName)."($sh_tax_percent%)", $this->formatPrice($final_details['shtax_totalamount']));
 219          $summaryModel->set(getTranslatedString("Adjustment", $this->moduleName), $this->formatPrice($final_details['adjustment']));
 220          $summaryModel->set(getTranslatedString("Grand Total:", $this->moduleName)."(in $currencySymbol)", $this->formatPrice($final_details['grandTotal'])); // TODO add currency string
 221  
 222          if ($this->moduleName == 'Invoice') {
 223              $receivedVal = $this->focusColumnValue("received");
 224              if (!$receivedVal) {
 225                  $this->focus->column_fields["received"] = 0;
 226              }
 227              //If Received value is exist then only Recieved, Balance details should present in PDF
 228              if ($this->formatPrice($this->focusColumnValue("received")) > 0) {
 229                  $summaryModel->set(getTranslatedString("Received", $this->moduleName), $this->formatPrice($this->focusColumnValue("received")));
 230                  $summaryModel->set(getTranslatedString("Balance", $this->moduleName), $this->formatPrice($this->focusColumnValue("balance")));
 231              }
 232          }
 233          return $summaryModel;
 234      }
 235  
 236  	function buildHeaderModel() {
 237          $headerModel = new Vtiger_PDF_Model();
 238          $headerModel->set('title', $this->buildHeaderModelTitle());
 239          $modelColumns = array($this->buildHeaderModelColumnLeft(), $this->buildHeaderModelColumnCenter(), $this->buildHeaderModelColumnRight());
 240          $headerModel->set('columns', $modelColumns);
 241  
 242          return $headerModel;
 243      }
 244  
 245  	function buildHeaderModelTitle() {
 246          return $this->moduleName;
 247      }
 248  
 249  	function buildHeaderModelColumnLeft() {
 250          global $adb;
 251  
 252          // Company information
 253          $result = $adb->pquery("SELECT * FROM vtiger_organizationdetails", array());
 254          $num_rows = $adb->num_rows($result);
 255          if($num_rows) {
 256              $resultrow = $adb->fetch_array($result);
 257  
 258              $addressValues = array();
 259              $addressValues[] = $resultrow['address'];
 260              if(!empty($resultrow['city'])) $addressValues[]= "\n".$resultrow['city'];
 261              if(!empty($resultrow['state'])) $addressValues[]= ",".$resultrow['state'];
 262              if(!empty($resultrow['code'])) $addressValues[]= $resultrow['code'];
 263              if(!empty($resultrow['country'])) $addressValues[]= "\n".$resultrow['country'];
 264  
 265              $additionalCompanyInfo = array();
 266              if(!empty($resultrow['phone']))        $additionalCompanyInfo[]= "\n".getTranslatedString("Phone: ", $this->moduleName). $resultrow['phone'];
 267              if(!empty($resultrow['fax']))        $additionalCompanyInfo[]= "\n".getTranslatedString("Fax: ", $this->moduleName). $resultrow['fax'];
 268              if(!empty($resultrow['website']))    $additionalCompanyInfo[]= "\n".getTranslatedString("Website: ", $this->moduleName). $resultrow['website'];
 269                          if(!empty($resultrow['vatid']))         $additionalCompanyInfo[]= "\n".getTranslatedString("VAT ID: ", $this->moduleName). $resultrow['vatid']; 
 270  
 271              $modelColumnLeft = array(
 272                      'logo' => "test/logo/".$resultrow['logoname'],
 273                      'summary' => decode_html($resultrow['organizationname']),
 274                      'content' => decode_html($this->joinValues($addressValues, ' '). $this->joinValues($additionalCompanyInfo, ' '))
 275              );
 276          }
 277          return $modelColumnLeft;
 278      }
 279  
 280  	function buildHeaderModelColumnCenter() {
 281          $customerName = $this->resolveReferenceLabel($this->focusColumnValue('account_id'), 'Accounts');
 282          $contactName = $this->resolveReferenceLabel($this->focusColumnValue('contact_id'), 'Contacts');
 283  
 284          $customerNameLabel = getTranslatedString('Customer Name', $this->moduleName);
 285          $contactNameLabel = getTranslatedString('Contact Name', $this->moduleName);
 286          $modelColumnCenter = array(
 287                  $customerNameLabel => $customerName,
 288                  $contactNameLabel  => $contactName,
 289          );
 290          return $modelColumnCenter;
 291      }
 292  
 293  	function buildHeaderModelColumnRight() {
 294          $issueDateLabel = getTranslatedString('Issued Date', $this->moduleName);
 295          $validDateLabel = getTranslatedString('Valid Date', $this->moduleName);
 296          $billingAddressLabel = getTranslatedString('Billing Address', $this->moduleName);
 297          $shippingAddressLabel = getTranslatedString('Shipping Address', $this->moduleName);
 298  
 299          $modelColumnRight = array(
 300                  'dates' => array(
 301                          $issueDateLabel  => $this->formatDate(date("Y-m-d")),
 302                          $validDateLabel  => $this->formatDate($this->focusColumnValue('validtill')),
 303                  ),
 304                  $billingAddressLabel  => $this->buildHeaderBillingAddress(),
 305                  $shippingAddressLabel => $this->buildHeaderShippingAddress()
 306          );
 307          return $modelColumnRight;
 308      }
 309  
 310  	function buildFooterModel() {
 311          $footerModel = new Vtiger_PDF_Model();
 312          $footerModel->set(Vtiger_PDF_InventoryFooterViewer::$DESCRIPTION_DATA_KEY, from_html($this->focusColumnValue('description')));
 313          $footerModel->set(Vtiger_PDF_InventoryFooterViewer::$TERMSANDCONDITION_DATA_KEY, from_html($this->focusColumnValue('terms_conditions')));
 314          return $footerModel;
 315      }
 316  
 317  	function buildFooterLabelModel() {
 318          $labelModel = new Vtiger_PDF_Model();
 319          $labelModel->set(Vtiger_PDF_InventoryFooterViewer::$DESCRIPTION_LABEL_KEY, getTranslatedString('Description',$this->moduleName));
 320          $labelModel->set(Vtiger_PDF_InventoryFooterViewer::$TERMSANDCONDITION_LABEL_KEY, getTranslatedString('Terms & Conditions',$this->moduleName));
 321          return $labelModel;
 322      }
 323  
 324  	function buildPagerModel() {
 325          $footerModel = new Vtiger_PDF_Model();
 326          $footerModel->set('format', '-%s-');
 327          return $footerModel;
 328      }
 329  
 330  	function getWatermarkContent() {
 331          return '';
 332      }
 333  
 334  	function buildWatermarkModel() {
 335          $watermarkModel = new Vtiger_PDF_Model();
 336          $watermarkModel->set('content', $this->getWatermarkContent());
 337          return $watermarkModel;
 338      }
 339  
 340  	function buildHeaderBillingAddress() {
 341          $billPoBox    = $this->focusColumnValues(array('bill_pobox'));
 342          $billStreet = $this->focusColumnValues(array('bill_street'));
 343          $billCity    = $this->focusColumnValues(array('bill_city'));
 344          $billState    = $this->focusColumnValues(array('bill_state'));
 345          $billCountry = $this->focusColumnValues(array('bill_country'));
 346          $billCode    =  $this->focusColumnValues(array('bill_code'));
 347          $address    = $this->joinValues(array($billPoBox, $billStreet), ' ');
 348          $address .= "\n".$this->joinValues(array($billCity, $billState), ',')." ".$billCode;
 349          $address .= "\n".$billCountry;
 350          return $address;
 351      }
 352  
 353  	function buildHeaderShippingAddress() {
 354          $shipPoBox    = $this->focusColumnValues(array('ship_pobox'));
 355          $shipStreet = $this->focusColumnValues(array('ship_street'));
 356          $shipCity    = $this->focusColumnValues(array('ship_city'));
 357          $shipState    = $this->focusColumnValues(array('ship_state'));
 358          $shipCountry = $this->focusColumnValues(array('ship_country'));
 359          $shipCode    =  $this->focusColumnValues(array('ship_code'));
 360          $address    = $this->joinValues(array($shipPoBox, $shipStreet), ' ');
 361          $address .= "\n".$this->joinValues(array($shipCity, $shipState), ',')." ".$shipCode;
 362          $address .= "\n".$shipCountry;
 363          return $address;
 364      }
 365  
 366  	function buildCurrencySymbol() {
 367          global $adb;
 368          $currencyId = $this->focus->column_fields['currency_id'];
 369          if(!empty($currencyId)) {
 370              $result = $adb->pquery("SELECT currency_symbol FROM vtiger_currency_info WHERE id=?", array($currencyId));
 371              return decode_html($adb->query_result($result,0,'currency_symbol'));
 372          }
 373          return false;
 374      }
 375  
 376  	function focusColumnValues($names, $delimeter="\n") {
 377          if(!is_array($names)) {
 378              $names = array($names);
 379          }
 380          $values = array();
 381          foreach($names as $name) {
 382              $value = $this->focusColumnValue($name, false);
 383              if($value !== false) {
 384                  $values[] = $value;
 385              }
 386          }
 387          return $this->joinValues($values, $delimeter);
 388      }
 389  
 390  	function focusColumnValue($key, $defvalue='') {
 391          $focus = $this->focus;
 392          if(isset($focus->column_fields[$key])) {
 393              return decode_html($focus->column_fields[$key]);
 394          }
 395          return $defvalue;
 396      }
 397  
 398  	function resolveReferenceLabel($id, $module=false) {
 399          if(empty($id)) {
 400              return '';
 401          }
 402          if($module === false) {
 403              $module = getSalesEntityType($id);
 404          }
 405          $label = getEntityName($module, array($id));
 406          return decode_html($label[$id]);
 407      }
 408  
 409  	function joinValues($values, $delimeter= "\n") {
 410          $valueString = '';
 411          foreach($values as $value) {
 412              if(empty($value)) continue;
 413              $valueString .= $value . $delimeter;
 414          }
 415          return rtrim($valueString, $delimeter);
 416      }
 417  
 418  	function formatNumber($value) {
 419          return number_format($value);
 420      }
 421  
 422  	function formatPrice($value, $decimal=2) {
 423          $currencyField = new CurrencyField($value);
 424          return $currencyField->getDisplayValue(null, true);
 425      }
 426  
 427  	function formatDate($value) {
 428          return DateTimeField::convertToUserFormat($value);
 429      }
 430  
 431  }
 432  ?>


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