[ Index ] |
PHP Cross Reference of vtigercrm-6.1.0 |
[Summary view] [Print] [Text view]
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 require_once "include/Webservices/VtigerActorOperation.php"; 13 require_once "include/Webservices/LineItem/VtigerInventoryOperation.php"; 14 require_once ("include/events/include.inc"); 15 require_once 'modules/com_vtiger_workflow/VTEntityCache.inc'; 16 require_once 'data/CRMEntity.php'; 17 require_once 'include/events/SqlResultIterator.inc'; 18 require_once 'include/Webservices/LineItem/VtigerLineItemMeta.php'; 19 require_once 'include/Webservices/Retrieve.php'; 20 require_once 'include/Webservices/Update.php'; 21 require_once 'include/Webservices/Utils.php'; 22 require_once 'modules/Emails/mail.php'; 23 require_once 'include/utils/InventoryUtils.php'; 24 25 /** 26 * Description of VtigerLineItemOperation 27 */ 28 class VtigerLineItemOperation extends VtigerActorOperation { 29 private static $lineItemCache = array(); 30 private $taxType = null; 31 private $Individual = 'Individual'; 32 private $Group = 'Group'; 33 private $newId = null; 34 private $taxList = null; 35 private static $parentCache = array(); 36 37 public function __construct($webserviceObject,$user,$adb,$log) { 38 $this->user = $user; 39 $this->log = $log; 40 $this->webserviceObject = $webserviceObject; 41 $this->pearDB = $adb; 42 $this->entityTableName = $this->getActorTables(); 43 if($this->entityTableName === null){ 44 throw new WebServiceException(WebServiceErrorCode::$UNKOWNENTITY, 45 "Entity is not associated with any tables"); 46 } 47 $this->meta = new VtigerLineItemMeta($this->entityTableName,$webserviceObject,$adb,$user); 48 $this->moduleFields = null; 49 $this->taxList = array(); 50 } 51 52 protected function getNextId($elementType, $element) { 53 $sql = 'SELECT MAX('.$this->meta->getIdColumn().') as maxvalue_lineitem_id FROM '.$this->entityTableName; 54 $result = $this->pearDB->pquery($sql,array()); 55 $numOfRows = $this->pearDB->num_rows($result); 56 57 for ($i=0; $i<$numOfRows; $i++) { 58 $row = $this->pearDB->query_result($result, $i, 'maxvalue_lineitem_id'); 59 } 60 61 $id = $row + 1; 62 return $id; 63 } 64 65 public function recreate($lineItem,$parent){ 66 $components = vtws_getIdComponents($lineItem['id']); 67 $this->newId = $components[1]; 68 $elementType = 'LineItem'; 69 $this->initTax($lineItem, $parent); 70 $this->_create($elementType, $lineItem); 71 } 72 73 /** 74 * Function gives all the line items related to inventory records 75 * @param $parentId - record id or array of the inventory record id's 76 * @return <Array> - list of line items 77 * @throws WebServiceException - Database error 78 */ 79 public function getAllLineItemForParent($parentId){ 80 if(is_array($parentId)){ 81 $result = null; 82 $query = "SELECT * FROM {$this->entityTableName} WHERE id IN (". generateQuestionMarks($parentId) .")"; 83 $transactionSuccessful = vtws_runQueryAsTransaction($query,array($parentId),$result); 84 if(!$transactionSuccessful){ 85 throw new WebServiceException(WebServiceErrorCode::$DATABASEQUERYERROR, 86 "Database error while performing required operation"); 87 } 88 $lineItemList = array(); 89 if($result){ 90 $rowCount = $this->pearDB->num_rows($result); 91 for ($i = 0 ; $i < $rowCount ; ++$i) { 92 $element = $this->pearDB->query_result_rowdata($result,$i); 93 $element['parent_id'] = $parentId; 94 $lineItemList[$element['id']][] = DataTransform::filterAndSanitize($element,$this->meta); 95 } 96 } 97 return $lineItemList; 98 }else{ 99 $result = null; 100 $query = "select * from {$this->entityTableName} where id=?"; 101 $transactionSuccessful = vtws_runQueryAsTransaction($query,array($parentId),$result); 102 if(!$transactionSuccessful){ 103 throw new WebServiceException(WebServiceErrorCode::$DATABASEQUERYERROR, 104 "Database error while performing required operation"); 105 } 106 $lineItemList = array(); 107 if($result){ 108 $rowCount = $this->pearDB->num_rows($result); 109 for ($i = 0 ; $i < $rowCount ; ++$i) { 110 $element = $this->pearDB->query_result_rowdata($result,$i); 111 $element['parent_id'] = $parentId; 112 $lineItemList[] = DataTransform::filterAndSanitize($element,$this->meta); 113 } 114 } 115 return $lineItemList; 116 } 117 } 118 119 public function _create($elementType, $element){ 120 $createdElement = parent::create($elementType, $element); 121 $productId = vtws_getIdComponents($element['productid']); 122 $productId = $productId[1]; 123 124 $parentTypeHandler = vtws_getModuleHandlerFromId($element['parent_id'], $this->user); 125 $parentTypeMeta = $parentTypeHandler->getMeta(); 126 $parentType = $parentTypeMeta->getEntityName(); 127 $parent = $this->getParentById($element['parent_id']); 128 if($parentType != 'PurchaseOrder') { 129 //update the stock with existing details 130 updateStk($productId,$element['quantity'],'',array(),$parentType); 131 } 132 133 $this->initTax($element, $parent); 134 $this->updateTaxes($createdElement); 135 $createdElement['incrementondel'] = '1'; 136 if(strcasecmp($parent['hdnTaxType'], $this->Individual) ===0){ 137 $createdElement = $this->appendTaxInfo($createdElement); 138 } 139 return $createdElement; 140 } 141 142 private function appendTaxInfo($element) { 143 $meta = $this->getMeta(); 144 $moduleFields = $meta->getModuleFields(); 145 foreach ($moduleFields as $fieldName=>$field) { 146 if(preg_match('/tax\d+/', $fieldName) != 0){ 147 if(is_array($this->taxList[$fieldName])){ 148 $element[$fieldName] = $this->taxList[$fieldName]['percentage']; 149 }else{ 150 $element[$fieldName] = '0.000'; 151 } 152 } 153 } 154 return $element; 155 } 156 157 private function resetTaxInfo($element, $parent) { 158 $productTaxInfo = array(); 159 if(empty($this->taxType)){ 160 list($typeId,$recordId) = vtws_getIdComponents($element['productid']); 161 $productTaxInfo = $this->getProductTaxList($recordId); 162 } 163 if(count($productTaxInfo) == 0 && 164 strcasecmp($parent['hdnTaxType'], $this->Individual) !==0) { 165 $meta = $this->getMeta(); 166 $moduleFields = $meta->getModuleFields(); 167 foreach ($moduleFields as $fieldName=>$field) { 168 if(preg_match('/tax\d+/', $fieldName) != 0){ 169 $element[$fieldName] = '0.000'; 170 } 171 } 172 } 173 return $element; 174 } 175 176 private function updateTaxes($createdElement){ 177 if(count($this->taxList) > 0 ) { 178 $id = vtws_getIdComponents($createdElement['id']); 179 $id = $id[1]; 180 $sql = 'UPDATE vtiger_inventoryproductrel set '; 181 $sql .= implode('=?,',array_keys($this->taxList)); 182 $sql .= '=? WHERE lineitem_id = ?'; 183 $params = array(); 184 foreach ($this->taxList as $taxInfo) { 185 $params[] = $taxInfo['percentage']; 186 } 187 $params[] = $id; 188 $result = null; 189 $transactionSuccessful = vtws_runQueryAsTransaction($sql,$params,$result); 190 if(!$transactionSuccessful){ 191 throw new WebServiceException(WebServiceErrorCode::$DATABASEQUERYERROR, 192 "Database error while performing required operation"); 193 } 194 } 195 } 196 197 private function initTax($element, $parent) { 198 if (!empty($element['parent_id'])) { 199 $this->taxType = $parent['hdnTaxType']; 200 } 201 $productId = vtws_getIdComponents($element['productid']); 202 $productId = $productId[1]; 203 if (strcasecmp($parent['hdnTaxType'], $this->Individual) === 0) { 204 $found = false; 205 $meta = $this->getMeta(); 206 $moduleFields = $meta->getModuleFields(); 207 $productTaxList = $this->getProductTaxList($productId); 208 if (count($productTaxList) > 0) { 209 foreach ($moduleFields as $fieldName => $field) { 210 if (preg_match('/tax\d+/', $fieldName) != 0) { 211 if (!empty($element[$fieldName])) { 212 $found = true; 213 if (is_array($productTaxList[$fieldName])) { 214 $this->taxList[$fieldName] = array( 215 'label' => $field->getFieldLabelKey(), 216 'percentage' => $element[$fieldName] 217 ); 218 } 219 } 220 } 221 } 222 } elseif ($found == false) { 223 array_merge($this->taxList, $productTaxList); 224 } 225 } else { 226 $meta = $this->getMeta(); 227 $moduleFields = $meta->getModuleFields(); 228 $availableTaxes = getAllTaxes('available'); 229 $found = false; 230 foreach ($moduleFields as $fieldName => $field) { 231 if (preg_match('/tax\d+/', $fieldName) != 0) { 232 $found = true; 233 if (!empty($element[$fieldName])) { 234 $this->taxList[$fieldName] = array( 235 'label' => $field->getFieldLabelKey(), 236 'percentage' => $element[$fieldName] 237 ); 238 } 239 } 240 } 241 if(!$found) { 242 foreach($availableTaxes as $taxInfo){ 243 $this->taxList[$taxInfo['taxname']] = array( 244 'label' => $field->getFieldLabelKey(), 245 'percentage' => $taxInfo['percentage'] 246 ); 247 } 248 } 249 } 250 $this->taxList; 251 } 252 253 public function cleanLineItemList($parentId){ 254 $components = vtws_getIdComponents($parentId); 255 $pId = $components[1]; 256 257 $parentTypeHandler = vtws_getModuleHandlerFromId($parentId, $this->user); 258 $parentTypeMeta = $parentTypeHandler->getMeta(); 259 $parentType = $parentTypeMeta->getEntityName(); 260 261 $parentObject = CRMEntity::getInstance($parentType); 262 $parentObject->id = $pId; 263 $lineItemList = $this->getAllLineItemForParent($pId); 264 deleteInventoryProductDetails($parentObject); 265 $this->resetInventoryStockById($parentId); 266 } 267 268 public function setLineItems($elementType, $lineItemList, $parent){ 269 foreach ($lineItemList as $lineItem) { 270 $lineItem['parent_id'] = $parent['id']; 271 $this->initTax($lineItem, $parent); 272 $id = vtws_getIdComponents($lineItem['parent_id']); 273 $this->newId = $id[1]; 274 $this->create($elementType, $lineItem); 275 } 276 } 277 278 public function create($elementType, $element) { 279 $parentId = vtws_getIdComponents($element['parent_id']); 280 $parentId = $parentId[1]; 281 282 $parent = $this->getParentById($element['parent_id']); 283 if(empty($element['listprice'])){ 284 $productId = vtws_getIdComponents($element['productid']); 285 $productId = $productId[1]; 286 $element['listprice'] = $this->getProductPrice($productId); 287 } 288 $id = vtws_getIdComponents($element['parent_id']); 289 $this->newId = $id[1]; 290 $createdLineItem = $this->_create($elementType, $element); 291 $updatedLineItemList = $createdLineItem; 292 $updatedLineItemList['parent_id'] = $element['parent_id']; 293 $this->setCache($parentId, $updatedLineItemList); 294 $this->updateInventoryStock($element,$parent); 295 return $createdLineItem; 296 } 297 298 public function retrieve($id) { 299 $element = parent::retrieve($id); 300 $parent = $this->getParentById($element['parent_id']); 301 return $this->resetTaxInfo($element, $parent); 302 } 303 304 public function update($element) { 305 $parentId = vtws_getIdComponents($element['parent_id']); 306 $parentId = $parentId[1]; 307 $parentTypeHandler = vtws_getModuleHandlerFromId($element['parent_id'], $this->user); 308 $parentTypeMeta = $parentTypeHandler->getMeta(); 309 $parentType = $parentTypeMeta->getEntityName(); 310 $parentObject = CRMEntity::getInstance($parentType); 311 $parentObject->id = $parentId; 312 $lineItemList = $this->getAllLineItemForParent($parentId); 313 $parent = $this->getParentById($element['parent_id']); 314 $location = $this->getLocationById($lineItemList, $element['id']); 315 if($location === false){ 316 throw new WebserviceException('UNKOWN_CHILD','given line item is not child of parent'); 317 } 318 if(empty($element['listprice'])){ 319 $productId = vtws_getIdComponents($element['productid']); 320 $productId = $productId[1]; 321 $element['listprice'] = $this->getProductPrice($productId); 322 } 323 $lineItemList[$location] = $element; 324 deleteInventoryProductDetails($parentObject); 325 $this->resetInventoryStock($element, $parent); 326 $updatedLineItemList = array(); 327 foreach ($lineItemList as $lineItem) { 328 $id = vtws_getIdComponents($lineItem['id']); 329 $this->newId = $id[1]; 330 $updatedLineItemList[] = $this->_create($elementType, $lineItem); 331 if($element == $lineItem){ 332 $createdElement = $updatedLineItemList[count($updatedLineItemList) - 1]; 333 } 334 } 335 $this->setCache($parentId, $updatedLineItemList); 336 $this->updateInventoryStock($element,$parent); 337 $this->updateParent($element, $parent); 338 return $createdElement; 339 } 340 341 function getProductPrice($productId){ 342 $db = PearDatabase::getInstance(); 343 $sql = "select unit_price from vtiger_products where productid=?"; 344 $params = array($productId); 345 $result = null; 346 $transactionSuccessful = vtws_runQueryAsTransaction($sql,$params,$result); 347 if(!$transactionSuccessful){ 348 throw new WebServiceException(WebServiceErrorCode::$DATABASEQUERYERROR, 349 "Database error while performing required operation"); 350 } 351 $price = 0; 352 $it = new SqlResultIterator($db, $result); 353 foreach ($it as $row) { 354 $price = $row->unit_price; 355 } 356 return $price; 357 } 358 359 private function getLocationById($lineItemList, $id){ 360 foreach ($lineItemList as $index=>$lineItem) { 361 if($lineItem['id'] == $id){ 362 return $index; 363 } 364 } 365 return false; 366 } 367 368 public function delete($id){ 369 $element = vtws_retrieve($id, $this->user); 370 if(!empty($element['parent_id'])){ 371 $parent = $this->getParentById($element['parent_id']); 372 } 373 $parentId = vtws_getIdComponents($element['parent_id']); 374 $parentId = $parentId[1]; 375 $lineItemList = $this->getAllLineItemForParent($parentId); 376 $this->cleanLineItemList($element['parent_id']); 377 $this->initTax($element, $parent); 378 $result = parent::delete($id); 379 $updatedList = array(); 380 $element = null; 381 foreach ($lineItemList as $lineItem) { 382 if($id != $lineItem['id']){ 383 $updatedList[] = $lineItem; 384 }else{ 385 $element = $lineItem; 386 } 387 } 388 $this->setLineItems('LineItem', $updatedList, $parent); 389 $this->resetCacheForParent($parentId); 390 $this->updateParent($element, $parent); 391 $this->updateInventoryStock($element, $parent); 392 return $result; 393 } 394 395 private function resetCacheForParent($parentId){ 396 self::$lineItemCache[$parentId] = null; 397 } 398 399 public function updateParent($createdElement,$parent){ 400 $discount = 0; 401 $parentId = vtws_getIdComponents($parent['id']); 402 $parentId = $parentId[1]; 403 $lineItemList = $this->getAllLineItemForParent($parentId); 404 $parent['hdnSubTotal'] = 0; 405 $taxAmount = 0; 406 foreach ($lineItemList as $lineItem) { 407 $discount = 0; 408 $lineItemTotal = $lineItem['listprice'] * $lineItem['quantity']; 409 $lineItem['discount_amount'] = (float)($lineItem['discount_amount']); 410 $lineItem['discount_percent'] = (float)($lineItem['discount_percent']); 411 if(!empty($lineItem['discount_amount'])){ 412 $discount = ($lineItem['discount_amount']); 413 }elseif(!empty($lineItem['discount_percent'])) { 414 $discount = ($lineItem['discount_percent'])/100 * $lineItemTotal; 415 } 416 $this->initTax($lineItem, $parent); 417 $lineItemTotal = $lineItemTotal - $discount; 418 $parent['hdnSubTotal'] = ($parent['hdnSubTotal'] ) + $lineItemTotal; 419 if(strcasecmp($parent['hdnTaxType'], $this->Individual) ===0){ 420 foreach ($this->taxList as $taxInfo) { 421 $lineItemTaxAmount = ($taxInfo['percentage'])/100*$lineItemTotal; 422 $parent['hdnSubTotal'] += $lineItemTaxAmount; 423 } 424 } 425 } 426 427 if(!empty($parent['hdnDiscountAmount']) && ((double)$parent['hdnDiscountAmount']) > 0){ 428 $discount = ($parent['hdnDiscountAmount']); 429 } elseif(!empty($parent['hdnDiscountPercent'])){ 430 $discount = ($parent['hdnDiscountPercent']/100 * $parent['hdnSubTotal']); 431 } 432 $parent['pre_tax_total'] = $total = $parent['hdnSubTotal'] - $discount + $parent['hdnS_H_Amount']; 433 $taxTotal = $parent['hdnSubTotal'] - $discount; 434 if(strcasecmp($parent['hdnTaxType'], $this->Individual) !==0){ 435 $this->initTax($createdElement, $parent); 436 foreach ($this->taxList as $taxInfo) { 437 $taxAmount += ($taxInfo['percentage'])/100*$taxTotal; 438 } 439 } 440 $shippingTax = getAllTaxes('all', 'sh','edit',$parentId); 441 $shippingTaxInfo = array(); 442 foreach ($shippingTax as $taxInfo) { 443 $taxAmount += ($taxInfo['percentage'])/100*$parent['hdnS_H_Amount']; 444 $shippingTaxInfo[$taxInfo['taxname']] = $taxInfo['percentage']; 445 } 446 447 $parent['hdnGrandTotal'] = $total + $taxAmount + $parent['txtAdjustment']; 448 449 $parentTypeHandler = vtws_getModuleHandlerFromId($parent['id'], $this->user); 450 $parentTypeMeta = $parentTypeHandler->getMeta(); 451 $parentType = $parentTypeMeta->getEntityName(); 452 453 $parentInstance = CRMEntity::getInstance($parentType); 454 $sql = 'update '.$parentInstance->table_name.' set subtotal=?, total=?, pre_tax_total=? where '. 455 $parentInstance->tab_name_index[$parentInstance->table_name].'=?'; 456 $params = array($parent['hdnSubTotal'],$parent['hdnGrandTotal'],$parent['pre_tax_total'],$parentId); 457 $transactionSuccessful = vtws_runQueryAsTransaction($sql,$params,$result); 458 self::$parentCache[$parent['id']] = $parent; 459 if(!$transactionSuccessful){ 460 throw new WebServiceException(WebServiceErrorCode::$DATABASEQUERYERROR, 461 "Database error while performing required operation"); 462 } 463 } 464 465 public function getCollectiveTaxList(){ 466 $db = PearDatabase::getInstance(); 467 $sql = 'select * from vtiger_inventorytaxinfo where deleted=0'; 468 $params = array(); 469 $result = null; 470 $transactionSuccessful = vtws_runQueryAsTransaction($sql,$params,$result); 471 if(!$transactionSuccessful){ 472 throw new WebServiceException(WebServiceErrorCode::$DATABASEQUERYERROR, 473 "Database error while performing required operation"); 474 } 475 $it = new SqlResultIterator($db, $result); 476 $this->taxList = array(); 477 foreach ($it as $row) { 478 $this->taxList[$row->taxname] = array('label'=>$row->taxlabel, 479 'percentage'=>$row->percentage); 480 } 481 return $this->taxList; 482 } 483 484 private function getProductTaxList($productId){ 485 $db = PearDatabase::getInstance(); 486 $sql = 'select * from vtiger_producttaxrel inner join vtiger_inventorytaxinfo on 487 vtiger_producttaxrel.taxid=vtiger_inventorytaxinfo.taxid and deleted=0 488 where productid=?'; 489 $params = array($productId); 490 $result = null; 491 $transactionSuccessful = vtws_runQueryAsTransaction($sql,$params,$result); 492 if(!$transactionSuccessful){ 493 throw new WebServiceException(WebServiceErrorCode::$DATABASEQUERYERROR, 494 "Database error while performing required operation"); 495 } 496 $it = new SqlResultIterator($db, $result); 497 $this->taxList = array(); 498 foreach ($it as $row) { 499 $this->taxList[$row->taxname] = array('label'=>$row->taxlabel, 500 'percentage'=>$row->taxpercentage); 501 } 502 return $this->taxList; 503 } 504 505 private function updateInventoryStock($element, $parent){ 506 global $updateInventoryProductRel_update_product_array; 507 $updateInventoryProductRel_update_product_array = array(); 508 $entityCache = new VTEntityCache($this->user); 509 $entityData = $entityCache->forId($element['parent_id']); 510 updateInventoryProductRel($entityData); 511 } 512 513 private function resetInventoryStock($element,$parent){ 514 if(!empty($parent['id'])){ 515 $this->resetInventoryStockById($parent['id']); 516 } 517 } 518 519 private function resetInventoryStockById($parentId){ 520 if(!empty($parentId)){ 521 $entityCache = new VTEntityCache($this->user); 522 $entityData = $entityCache->forId($parentId); 523 updateInventoryProductRel($entityData); 524 } 525 } 526 527 public function getParentById($parentId){ 528 if(empty(self::$parentCache[$parentId])){ 529 return vtws_retrieve($parentId, $this->user); 530 } else { 531 return self::$parentCache[$parentId]; 532 } 533 } 534 535 function setCache($parentId, $updatedList) { 536 self::$lineItemCache[$parentId] = $updatedList; 537 } 538 539 public function __create($elementType,$element){ 540 $element['id'] = $element['parent_id']; 541 unset($element['parent_id']); 542 $success = parent::__create($elementType, $element); 543 return $success; 544 } 545 546 protected function getElement(){ 547 if(!empty($this->element['id'])) { 548 $this->element['parent_id'] = $this->element['id']; 549 } 550 return $this->element; 551 } 552 553 public function describe($elementType) { 554 $describe = parent::describe($elementType); 555 foreach ($describe['fields'] as $key => $list){ 556 if($list["name"] == 'description'){ 557 unset($describe['fields'][$key]); 558 } 559 } 560 return $describe; 561 } 562 } 563 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:08:37 2014 | Cross-referenced by PHPXref 0.7.1 |