[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/include/Webservices/LineItem/ -> VtigerInventoryOperation.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  require_once  'include/Webservices/VtigerModuleOperation.php';
  12  require_once  'include/Webservices/Utils.php';
  13  
  14  /**
  15   * Description of VtigerInventoryOperation
  16   */
  17  class VtigerInventoryOperation extends VtigerModuleOperation {
  18  
  19  	public function create($elementType, $element) {
  20          $element = $this->sanitizeInventoryForInsert($element);
  21          $element = $this->sanitizeShippingTaxes($element);
  22          $lineItems = $element['LineItems'];
  23          if (!empty($lineItems)) {
  24              $element = parent::create($elementType, $element);
  25              $handler = vtws_getModuleHandlerFromName('LineItem', $this->user);
  26              $handler->setLineItems('LineItem', $lineItems, $element);
  27              $parent = $handler->getParentById($element['id']);
  28              $handler->updateParent($lineItems, $parent);
  29              $updatedParent = $handler->getParentById($element['id']);
  30              //since subtotal and grand total is updated in the update parent api 
  31              $parent['hdnSubTotal'] = $updatedParent['hdnSubTotal'];
  32              $parent['hdnGrandTotal'] = $updatedParent['hdnGrandTotal'];
  33              $parent['pre_tax_total'] = $updatedParent['pre_tax_total'];
  34              $components = vtws_getIdComponents($element['id']);
  35              $parentId = $components[1]; 
  36              $parent['LineItems'] = $handler->getAllLineItemForParent($parentId);
  37              
  38          } else {
  39              throw new WebServiceException(WebServiceErrorCode::$MANDFIELDSMISSING, "Mandatory Fields Missing..");
  40          }
  41          return array_merge($element,$parent);
  42      }
  43  
  44  	public function update($element) {
  45          $element = $this->sanitizeInventoryForInsert($element);
  46          $element = $this->sanitizeShippingTaxes($element);
  47          $lineItemList = $element['LineItems'];
  48          $handler = vtws_getModuleHandlerFromName('LineItem', $this->user);
  49          if (!empty($lineItemList)) {
  50              $updatedElement = parent::update($element);
  51              $handler->setLineItems('LineItem', $lineItemList, $updatedElement);
  52              $parent = $handler->getParentById($element['id']);
  53              $handler->updateParent($lineItemList, $parent);
  54              $updatedParent = $handler->getParentById($element['id']);
  55              //since subtotal and grand total is updated in the update parent api 
  56              $parent['hdnSubTotal'] = $updatedParent['hdnSubTotal'];
  57              $parent['hdnGrandTotal'] = $updatedParent['hdnGrandTotal'];
  58              $parent['pre_tax_total'] = $updatedParent['pre_tax_total'];
  59              $updatedElement = array_merge($updatedElement,$parent);
  60          } else {
  61              $updatedElement = $this->revise($element);
  62          }
  63          return $updatedElement;
  64      }
  65  
  66  	public function revise($element) {
  67          $element = $this->sanitizeInventoryForInsert($element);
  68          $element = $this->sanitizeShippingTaxes($element);
  69          $handler = vtws_getModuleHandlerFromName('LineItem', $this->user);
  70          $components = vtws_getIdComponents($element['id']);
  71          $parentId = $components[1];
  72  
  73          if (!empty($element['LineItems'])) {
  74              $lineItemList = $element['LineItems'];
  75              unset($element['LineItems']);
  76  
  77              $updatedElement = parent::revise($element);
  78              $handler->setLineItems('LineItem', $lineItemList, $updatedElement);
  79              $parent = $handler->getParentById($element['id']);
  80              $handler->updateParent($lineItemList, $parent);
  81              $updatedParent = $handler->getParentById($element['id']);
  82              //since subtotal and grand total is updated in the update parent api
  83              $parent['hdnSubTotal'] = $updatedParent['hdnSubTotal'];
  84              $parent['hdnGrandTotal'] = $updatedParent['hdnGrandTotal'];
  85              $parent['pre_tax_total'] = $updatedParent['pre_tax_total'];
  86              $parent['LineItems'] = $handler->getAllLineItemForParent($parentId);
  87          } else {
  88              $prevAction = $_REQUEST['action'];
  89              // This is added as we are passing data in user format, so in the crmentity insertIntoEntity API
  90              // should convert to database format, we have added a check based on the action name there. But 
  91              // while saving Invoice and Purchase Order we are also depending on the same action file names to
  92              // not to update stock if its an ajax save. In this case also we do not want line items to change.
  93              $_REQUEST['action'] = 'FROM_WS';
  94  
  95              $parent = parent::revise($element);
  96              $_REQUEST['action'] = $prevAction;
  97          }
  98          return array_merge($element,$parent);
  99      }
 100  
 101  	public function retrieve($id) {
 102          $element = parent::retrieve($id);
 103          $skipLineItemFields = getLineItemFields();
 104          foreach ($skipLineItemFields as $key => $field) {
 105              if (array_key_exists($field, $element)) {
 106                  unset($element[$field]);
 107              }
 108          }
 109          $handler = vtws_getModuleHandlerFromName('LineItem', $this->user);
 110          $idComponents = vtws_getIdComponents($id);
 111          $lineItems = $handler->getAllLineItemForParent($idComponents[1]);
 112          $element['LineItems'] = $lineItems;
 113          return $element;
 114      }
 115  
 116  	public function delete($id) {
 117          $components = vtws_getIdComponents($id);
 118          $parentId = $components[1];
 119          $handler = vtws_getModuleHandlerFromName('LineItem', $this->user);
 120          $handler->cleanLineItemList($id);
 121          $result = parent::delete($id);
 122          return $result;
 123      }
 124      /**
 125       * function to display discounts,taxes and adjustments
 126       * @param type $element
 127       * @return type
 128       */
 129  	protected function sanitizeInventoryForInsert($element) {
 130          $meta = $this->getMeta();
 131          if (!empty($element['hdnTaxType'])) {
 132              $_REQUEST['taxtype'] = $element['hdnTaxType'];
 133          }
 134          if (!empty($element['hdnSubTotal'])) {
 135              $_REQUEST['subtotal'] = $element['hdnSubTotal'];
 136          }
 137  
 138          if (($element['hdnDiscountAmount'])) {
 139              $_REQUEST['discount_type_final'] = 'amount';
 140              $_REQUEST['discount_amount_final'] = $element['hdnDiscountAmount'];
 141          } elseif (($element['hdnDiscountPercent'])) {
 142              $_REQUEST['discount_type_final'] = 'percentage';
 143              $_REQUEST['discount_percentage_final'] = $element['hdnDiscountPercent'];
 144          } else {
 145              $_REQUEST['discount_type_final'] = '';
 146              $_REQUEST['discount_percentage_final'] = '';
 147          }
 148          
 149  
 150          if (($element['txtAdjustment'])) {
 151              $_REQUEST['adjustmentType'] = ((int) $element['txtAdjustment'] < 0) ? '-' : '+';
 152              $_REQUEST['adjustment'] = abs($element['txtAdjustment']);
 153          }else {
 154              $_REQUEST['adjustmentType'] = '';
 155              $_REQUEST['adjustment'] = '';
 156          }
 157          if (!empty($element['hdnGrandTotal'])) {
 158              $_REQUEST['total'] = $element['hdnGrandTotal'];
 159          }
 160          
 161          return $element;
 162      }
 163      
 164  	public function sanitizeShippingTaxes($element){
 165          $_REQUEST['shipping_handling_charge'] = $element['hdnS_H_Amount'];
 166          $taxDetails = getAllTaxes('all', 'sh');
 167          foreach ($taxDetails as $taxInfo) {
 168              //removing previous taxes
 169              unset($_REQUEST[$taxInfo['taxname'] . '_sh_percent']);
 170              if ($taxInfo['deleted'] == '0' || $taxInfo['deleted'] === 0) {
 171                  if(isset($element['hdnS_H_Percent']) && $element['hdnS_H_Percent'] != 0){
 172                      $_REQUEST[$taxInfo['taxname'] . '_sh_percent'] = $element['hdnS_H_Percent'];
 173                      break;
 174                  } else {
 175                      if(isset($element[$taxInfo['taxname'] . '_sh_percent'])){
 176                          $_REQUEST[$taxInfo['taxname'] . '_sh_percent'] = $element[$taxInfo['taxname'] . '_sh_percent'];
 177                      }
 178                      //if there is Shipping Amount and shipping taxes is provided with 0 
 179                      elseif($element['hdnS_H_Amount'] > 0 && $element['hdnS_H_Percent'] === 0){
 180                          $_REQUEST[$taxInfo['taxname'] . '_sh_percent'] = 0;
 181                      }else{
 182                          $_REQUEST[$taxInfo['taxname'] . '_sh_percent'] = $taxInfo['percentage'];
 183                      }
 184                  }
 185              }
 186          }
 187          return $element;        
 188      }
 189      /* NOTE: Special case to pull the default setting of TermsAndCondition */
 190  
 191      public function describe($elementType) {
 192          $describe = parent::describe($elementType);
 193          $tandc = getTermsAndConditions();
 194          foreach ($describe['fields'] as $key => $list){
 195              if($list["name"] == 'terms_conditions'){
 196                  $describe['fields'][$key]['default'] = $tandc;
 197              }
 198          }
 199          return $describe;
 200      }
 201  }
 202  
 203  ?>


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