Ejemplo n.º 1
0
    /**
     * calculateOrderPrice
     * Calculate order price
     *
     * @access public
     * @param xPDO $order The xPDO order object
     * @return array The order amounts
     */
    public function calculateOrderPrice($order, $savePrices=true) {
    	if ($order == null) {
    		return false;	
    	}
    	
    	$orderBasket = $order->get('basket');
    	$shop = $order->getOne('Shop');
    	$modx =& $this->modx;
    	
    	$amounts = array();
    	$amounts['totalProductsPriceIn'] = 0;
    	$amounts['totalProductsPriceEx'] = 0;
    	$amounts['totalShippingPriceEx'] = 0;
    	$amounts['totalWeight'] = 0;
    	$highestTax = null;
    	
    	$this->fireEvent('vcEventCalculateOrder', 'before', array(
			'vcAction' => 'process',
			'shop' => $shop,
			'order' => $order
		));
    	
		if (is_array($orderBasket) && sizeof($orderBasket) > 0) {
	    	foreach($orderBasket as $product) {
	    		$productObject = $this->modx->getObject('vcProduct', $product['id']);
	    		
	    		if ($productObject == null) {
	    			continue;	
	    		}
	    		
	    		// Get the tax category
	    		$taxCategory = $productObject->getOne('TaxCategory'); 
	    		
	    		if ($highestTax == null) {
	    			$highestTax = $taxCategory;	
	    		} elseif ($highestTax->get('pricechange') < $taxCategory->get('pricechange')) {
	    			$highestTax = $taxCategory;	
	    		}
	    		
	    		$productPrice = $this->calculateProductPrice($productObject, true);
	    		$shippingPrice = $productObject->get('shippingprice');
	    		$amounts['totalProductsPriceIn'] += ($productPrice['in'] * $product['quantity']);
	    		$amounts['totalProductsPriceEx'] += ($productPrice['ex'] * $product['quantity']);
	    		$amounts['totalShippingPriceEx'] += ($shippingPrice * $product['quantity']);
	    		$amounts['totalWeight'] += ($productObject->get('weight') * $product['quantity']);
	    	}
	    	
	    	// Format totals
	    	$amounts['totalProductsPriceIn'] = number_format($amounts['totalProductsPriceIn'], 2, '.', '');
	    	$amounts['totalProductsPriceEx'] = number_format($amounts['totalProductsPriceEx'], 2, '.', '');
	    	$amounts['totalShippingPriceEx'] = number_format($amounts['totalShippingPriceEx'], 2, '.', '');
	    		    	
	    	// Calculate shipping taxes
	    	$taxAmount = ($amounts['totalShippingPriceEx'] / 100) * $highestTax->get('pricechange');
	    	
	    	if ((int) $this->getShopSetting('calculateShippingTaxes', $shop->get('id')) == 1) {
	    		$amounts['totalShippingPriceIn'] = number_format(($amounts['totalShippingPriceEx'] + $taxAmount), 2, '.', '');
	    	} else {
	    		$amounts['totalShippingPriceIn'] = $amounts['totalShippingPriceEx'];
	    	}
	    	
	    	// Get payment method costs
	    	if ($order->get('shippingid') != 0) {
		    	$shippingModule = $order->getOne('ShippingModule');
		    	if ($shippingModule != null) {
		    		// Get the main controller file
		    		$controller = $this->config['corePath'].'modules/shipping/'.$shippingModule->get('controller');
		    		if (is_file($controller)) {
		    			$vcAction = 'calculateOrderAmount';
		    			$returnValue = include($controller);	
		    			
		    			// Merge current shipping data with shipping data returned from module
		    			if (isset($returnValue) && is_array($returnValue)) {
		    				if (!is_array($order->get('shippingdata'))) {
		    					$order->set('shippingdata', array());
		    				}
		    				$order->set('shippingdata', array_merge($order->get('shippingdata'), $returnValue));
		    			}
		    			
		    			unset($returnValue);
		    			if (isset($module)) {
		    				unset($module);	
		    			}
		    		}
		    	}	
	    	}
	    	
	    	// Get payment method costs
	    	if ($order->get('paymentid') != 0) {
		    	$paymentModule = $order->getOne('PaymentModule');
		    	if ($paymentModule != null) {
		    		$amounts['paymentCostsEx'] = 0;
			    	$amounts['paymentCostsIn'] = 0;
			    	$amounts['paymentData'] = array();
			    	
		    		// Get the main controller file
		    		$controller = $this->config['corePath'].'modules/payment/'.$paymentModule->get('controller');
		    		if (is_file($controller)) {
		    			$vcAction = 'calculateOrderAmount';
		    			$returnValue = include($controller);	
		    			
		    			// Merge current payment data with payment data returned from module
		    			if (isset($returnValue) && is_array($returnValue)) {
		    				if (!is_array($order->get('paymentdata'))) {
		    					$order->set('paymentdata', array());
		    				}
		    				$order->set('paymentdata', array_merge($order->get('paymentdata'), $returnValue));
		    			}
		    			
		    			unset($returnValue);
		    			if (isset($module)) {
		    				unset($module);	
		    			}
		    		}
		    	}	
	    	}
	    	    
			$order->set('totalweight', $amounts['totalWeight']);	
			$order->set('totalproductamountex', $amounts['totalProductsPriceEx']);
			$order->set('totalproductamountin', $amounts['totalProductsPriceIn']);
			$order->set('totalorderamountin', ($amounts['totalProductsPriceIn'] + $order->get('shippingcostsin') + $order->get('paymentcostsin')));
			$order->set('totalorderamountex', ($amounts['totalProductsPriceEx'] + $order->get('shippingcostsex') + $order->get('paymentcostsex')));
		} else {
			$order->set('totalweight', 0);	
			$order->set('totalproductamountex', 0);
			$order->set('totalproductamountin', 0);
			$order->set('totalorderamountin', 0);
			$order->set('totalorderamountex', 0);
			$order->set('paymentcostsex', 0);
			$order->set('paymentcostsin', 0);
			$order->set('shippingcostsin', 0);
			$order->set('shippingcostsex', 0);
		}

    	if ($savePrices) {
    		$order->save();
    	}
    	
    	$this->fireEvent('vcEventCalculateOrder', 'after', array(
			'vcAction' => 'process',
			'shop' => $shop,
			'order' => $order
		));
		
    	return $order;
    }