/
Item.php
171 lines (154 loc) · 5.87 KB
/
Item.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/**
* Open Commerce LLC Commercial Extension
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Commerce LLC Commercial Extension License
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://store.opencommercellc.com/license/commercial-license
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@opencommercellc.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this package to newer
* versions in the future.
*
* @category OpenCommerce
* @package OpenCommerce_OrderEdit
* @copyright Copyright (c) 2013 Open Commerce LLC
* @license http://store.opencommercellc.com/license/commercial-license
*/
class TinyBrick_OrderEdit_Model_Order_Item extends Mage_Sales_Model_Order_Item
{
/**
* Constructs the order item model
*/
protected function _construct()
{
$this->_init('orderedit/order_item');
}
/**
* Calculates the row total for the order
* @return TinyBrick_OrderEdit_Model_Order_Item
*/
public function calcRowTotal()
{
$qty = $this->getQtyOrdered();
if ($this->getParentItem()) {
$qty = $qty*$this->getParentItem()->getQtyOrdered();
}
if ($rowTotal = $this->getRowTotalExcTax()) {
$baseTotal = $rowTotal;
$total = $this->getStore()->convertPrice($baseTotal);
}
else {
$total = $this->getCalculationPrice()*$qty;
$baseTotal = $this->getBaseCalculationPrice()*$qty;
}
$this->setRowTotal($total);
$this->setBaseRowTotal($this->getStore()->roundPrice($baseTotal));
$this->save();
return $this;
}
/**
* Gets store from order
* @return int
*/
public function getStore()
{
return $this->getOrder()->getStore();
}
/**
* Get calculation price of the order
* @return int $price
*/
public function getCalculationPrice()
{
if($this->getProductType() != 'package') {
$price = $this->getData('calculation_price');
if (is_null($price)) {
if ($this->hasCustomPrice()) {
$price = $this->getCustomPrice();
} else {
$price = $this->getOriginalPrice();
}
$this->setData('calculation_price', $price);
}
} else {
$price = $this->getPrice();
}
return $price;
}
/**
* Calculates the tax on the order
* @return TinyBrick_OrderEdit_Model_Order_Item
*/
public function calcTaxAmount()
{
$store = $this->getStore();
if (!Mage::helper('tax')->priceIncludesTax($store)) {
if (Mage::helper('tax')->applyTaxAfterDiscount($store)) {
$rowTotal = $this->getRowTotalWithDiscount();
$rowBaseTotal = $this->getBaseRowTotalWithDiscount();
} else {
$rowTotal = $this->getRowTotal();
$rowBaseTotal = $this->getBaseRowTotal();
}
$taxPercent = $this->getTaxPercent()/100;
$this->setTaxAmount($store->roundPrice($rowTotal * $taxPercent));
$this->setBaseTaxAmount($store->roundPrice($rowBaseTotal * $taxPercent));
$rowTotal = $this->getRowTotal();
$rowBaseTotal = $this->getBaseRowTotal();
$this->setTaxBeforeDiscount($store->roundPrice($rowTotal * $taxPercent));
$this->setBaseTaxBeforeDiscount($store->roundPrice($rowBaseTotal * $taxPercent));
} else {
if (Mage::helper('tax')->applyTaxAfterDiscount($store)) {
$totalBaseTax = $this->getBaseTaxAmount();
$totalTax = $this->getTaxAmount();
if ($totalTax && $totalBaseTax) {
$totalTax -= $this->getDiscountAmount()*($this->getTaxPercent()/100);
$totalBaseTax -= $this->getBaseDiscountAmount()*($this->getTaxPercent()/100);
$this->setBaseTaxAmount($store->roundPrice($totalBaseTax));
$this->setTaxAmount($store->roundPrice($totalTax));
}
}
}
if (Mage::helper('tax')->discountTax($store) && !Mage::helper('tax')->applyTaxAfterDiscount($store)) {
if ($this->getDiscountPercent()) {
$baseTaxAmount = $this->getBaseTaxBeforeDiscount();
$taxAmount = $this->getTaxBeforeDiscount();
$baseDiscountDisposition = $baseTaxAmount/100*$this->getDiscountPercent();
$discountDisposition = $taxAmount/100*$this->getDiscountPercent();
$this->setDiscountAmount($this->getDiscountAmount()+$discountDisposition);
$this->setBaseDiscountAmount($this->getBaseDiscountAmount()+$baseDiscountDisposition);
}
}
return $this;
}
/**
* Get initial calculation price
* @return int
*/
public function getBaseCalculationPrice()
{
if (!$this->hasBaseCalculationPrice()) {
if ($this->hasCustomPrice()) {
if ($price = (float) $this->getCustomPrice()) {
$rate = $this->getStore()->convertPrice($price) / $price;
$price = $price / $rate;
}
else {
$price = $this->getCustomPrice();
}
} else {
$price = $this->getPrice();
}
$this->setBaseCalculationPrice($price);
}
return $this->getData('base_calculation_price');
}
}