Index: branches/5.2.x/units/orders/orders_item.php =================================================================== diff -u -N -r14812 -r14884 --- branches/5.2.x/units/orders/orders_item.php (.../orders_item.php) (revision 14812) +++ branches/5.2.x/units/orders/orders_item.php (.../orders_item.php) (revision 14884) @@ -1,6 +1,6 @@ GetFieldOptions('PaymentCCExpDate'); - if( $this->GetDirtyField($options['month_field']) || $this->GetDirtyField($options['year_field']) ) - { + + if ( $this->GetDirtyField($options['month_field']) || $this->GetDirtyField($options['year_field']) ) { $this->SetDirtyField('PaymentCCExpDate', 0); $this->SetField('PaymentCCExpDate', 0); } @@ -152,37 +163,74 @@ function RecalculateTax() { $tax = $this->getTaxPercent(); - $this->SetDBField( 'VATPercent', $tax['TaxValue'] ); - $this->SetDBField( 'ShippingTaxable', $tax['ApplyToShipping']); - $this->SetDBField( 'ProcessingTaxable', $tax['ApplyToProcessing']); + $this->SetDBField('VATPercent', $tax['TaxValue']); + $this->SetDBField('ShippingTaxable', $tax['ApplyToShipping']); + $this->SetDBField('ProcessingTaxable', $tax['ApplyToProcessing']); $this->UpdateTotals(); - $subtotal = $this->GetDBField('AmountWithoutVAT'); + if ( !$this->GetDBField('VATIncluded') ) { + $subtotal = $this->GetDBField('AmountWithoutVAT'); - $query = 'SELECT SUM(Quantity * Price) FROM '.TABLE_PREFIX.'OrderItems AS oi - LEFT JOIN '.TABLE_PREFIX.'Products AS p - ON p.ProductId = oi.ProductId - WHERE p.Type = 6 AND oi.OrderId = '.$this->GetDBField('OrderId'); - $tax_exempt = $this->Conn->GetOne($query); - if ($tax_exempt) $subtotal -= $tax_exempt; + $tax_exempt = $this->getTaxExempt(); - $this->SetDBField( 'VAT', round($subtotal * $tax['TaxValue'] / 100, 2) ); - $this->UpdateTotals(); + if ( $tax_exempt ) { + $subtotal -= $tax_exempt; + } + + $this->SetDBField('VAT', round($subtotal * $tax['TaxValue'] / 100, 2)); + $this->UpdateTotals(); + } } + /** + * Returns order amount, that is excluded from tax calculations + * + * @return float + * @access protected + */ + protected function getTaxExempt() + { + $sql = 'SELECT SUM(oi.Quantity * oi.Price) + FROM ' . TABLE_PREFIX . 'OrderItems AS oi + LEFT JOIN ' . TABLE_PREFIX . 'Products AS p ON p.ProductId = oi.ProductId + WHERE p.Type = 6 AND oi.OrderId = ' . $this->GetDBField('OrderId'); + + return $this->Conn->GetOne($sql); + } + function UpdateTotals() { $total = 0; $total += $this->GetDBField('SubTotal'); - if ($this->GetDBField('ShippingTaxable')) $total += $this->GetDBField('ShippingCost'); - if ($this->GetDBField('ProcessingTaxable')) $total += $this->GetDBField('ProcessingFee'); - $this->SetDBField('AmountWithoutVAT', $total); - $total += $this->GetDBField('VAT'); + if ( $this->GetDBField('ShippingTaxable') ) { + $total += $this->GetDBField('ShippingCost'); + } - if (!$this->GetDBField('ShippingTaxable')) $total += $this->GetDBField('ShippingCost'); - if (!$this->GetDBField('ProcessingTaxable')) $total += $this->GetDBField('ProcessingFee'); + if ( $this->GetDBField('ProcessingTaxable') ) { + $total += $this->GetDBField('ProcessingFee'); + } + if ( $this->GetDBField('VATIncluded') ) { + $tax_exempt = $this->getTaxExempt(); + + $vat_percent = $this->GetDBField('VATPercent'); + $this->SetDBField('VAT', round(($total - $tax_exempt) * $vat_percent / (100 + $vat_percent), 2)); + $this->SetDBField('AmountWithoutVAT', $total - $this->GetDBField('VAT')); + } + else { + $this->SetDBField('AmountWithoutVAT', $total); + $total += $this->GetDBField('VAT'); + } + + if ( !$this->GetDBField('ShippingTaxable') ) { + $total += $this->GetDBField('ShippingCost'); + } + + if ( !$this->GetDBField('ProcessingTaxable') ) { + $total += $this->GetDBField('ProcessingFee'); + } + $total += $this->GetDBField('InsuranceFee'); $this->SetDBField('TotalAmount', $total); @@ -192,7 +240,7 @@ { return $this->GetDBField('SubTotal') + $this->GetDBField('ShippingCost') + - $this->GetDBField('VAT') + + ($this->GetDBField('VATIncluded') ? 0 : $this->GetDBField('VAT')) + $this->GetDBField('ProcessingFee') + $this->GetDBField('InsuranceFee') - $this->GetDBField('GiftCertificateDiscount');