00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 class Mage_Weee_Model_Total_Quote_Weee extends Mage_Sales_Model_Quote_Address_Total_Tax
00029 {
00030 public function __construct(){
00031 $this->setCode('weee');
00032 }
00033
00034 public function collect(Mage_Sales_Model_Quote_Address $address)
00035 {
00036 $totalWeeeTax = 0;
00037 $baseTotalWeeeTax = 0;
00038
00039 $items = $address->getAllItems();
00040 if (!count($items)) {
00041 return $this;
00042 }
00043
00044 foreach ($items as $item) {
00045 if ($item->getParentItemId()) {
00046 continue;
00047 }
00048
00049 $this->_resetItemData($item);
00050
00051 if ($item->getHasChildren() && $item->isChildrenCalculated()) {
00052 foreach ($item->getChildren() as $child) {
00053 $this->_resetItemData($child);
00054 $this->_processItem($address, $child, true);
00055
00056 $totalWeeeTax += $child->getWeeeTaxAppliedRowAmount();
00057 $baseTotalWeeeTax += $child->getBaseWeeeTaxAppliedRowAmount();
00058 }
00059 } else {
00060 $this->_processItem($address, $item);
00061
00062 $totalWeeeTax += $item->getWeeeTaxAppliedRowAmount();
00063 $baseTotalWeeeTax += $item->getBaseWeeeTaxAppliedRowAmount();
00064 }
00065 }
00066
00067 $address->setGrandTotal($address->getGrandTotal() + $totalWeeeTax);
00068 $address->setBaseGrandTotal($address->getBaseGrandTotal() + $baseTotalWeeeTax);
00069 return $this;
00070 }
00071
00072 protected function _processItem(Mage_Sales_Model_Quote_Address $address, $item, $updateParent = false)
00073 {
00074 $custTaxClassId = $address->getQuote()->getCustomerTaxClassId();
00075 $store = $address->getQuote()->getStore();
00076
00077 $taxCalculationModel = Mage::getSingleton('tax/calculation');
00078
00079 $request = $taxCalculationModel->getRateRequest($address, $address->getQuote()->getBillingAddress(), $custTaxClassId, $store);
00080 $defaultRateRequest = $taxCalculationModel->getRateRequest(false, false, false, $store);
00081
00082 $attributes = Mage::helper('weee')->getProductWeeeAttributes(
00083 $item->getProduct(),
00084 $address,
00085 $address->getQuote()->getBillingAddress(),
00086 $store->getWebsiteId()
00087 );
00088
00089 $applied = array();
00090 $productTaxes = array();
00091
00092 foreach ($attributes as $k=>$attribute) {
00093 $baseValue = $attribute->getAmount();
00094 $value = $store->convertPrice($baseValue);
00095
00096 $rowValue = $value*$item->getQty();
00097 $baseRowValue = $baseValue*$item->getQty();
00098
00099 $title = $attribute->getName();
00100
00101 if ($item->getDiscountPercent() && Mage::helper('weee')->isDiscounted($store)) {
00102 $valueDiscount = $value/100*$item->getDiscountPercent();
00103 $baseValueDiscount = $baseValue/100*$item->getDiscountPercent();
00104
00105 $rowValueDiscount = $rowValue/100*$item->getDiscountPercent();
00106 $baseRowValueDiscount = $baseRowValue/100*$item->getDiscountPercent();
00107
00108
00109
00110
00111
00112
00113
00114
00115 $address->setDiscountAmount($address->getDiscountAmount()+$rowValueDiscount);
00116 $address->setBaseDiscountAmount($address->getBaseDiscountAmount()+$baseRowValueDiscount);
00117
00118 $address->setGrandTotal($address->getGrandTotal() - $rowValueDiscount);
00119 $address->setBaseGrandTotal($address->getBaseGrandTotal() - $baseRowValueDiscount);
00120 }
00121
00122 $oneDisposition = $baseOneDisposition = $disposition = $baseDisposition = 0;
00123
00124 if (Mage::helper('weee')->isTaxable($store)) {
00125 $currentPercent = $item->getTaxPercent();
00126 $defaultPercent = $taxCalculationModel->getRate($defaultRateRequest->setProductClassId($item->getProduct()->getTaxClassId()));
00127
00128 $valueBeforeVAT = $rowValue;
00129 $baseValueBeforeVAT = $baseRowValue;
00130
00131 $oneDisposition = $store->roundPrice($value/(100+$defaultPercent)*$currentPercent);
00132 $baseOneDisposition = $store->roundPrice($baseValue/(100+$defaultPercent)*$currentPercent);
00133
00134 $disposition = $store->roundPrice($rowValue/(100+$defaultPercent)*$currentPercent);
00135 $baseDisposition = $store->roundPrice($baseRowValue/(100+$defaultPercent)*$currentPercent);
00136
00137
00138
00139
00140 $item->setBaseTaxAmount($item->getBaseTaxAmount()+$baseDisposition);
00141 $item->setTaxAmount($item->getTaxAmount()+$disposition);
00142
00143
00144 $value -= $oneDisposition;
00145 $baseValue -= $baseOneDisposition;
00146
00147 $rowValue -= $baseDisposition;
00148 $baseRowValue -= $disposition;
00149
00150 $item->setWeeeTaxDisposition($item->getWeeeTaxDisposition() + $oneDisposition);
00151 $item->setBaseWeeeTaxDisposition($item->getBaseWeeeTaxDisposition() + $baseOneDisposition);
00152 $item->setWeeeTaxRowDisposition($item->getWeeeTaxRowDisposition() + $disposition);
00153 $item->setBaseWeeeTaxRowDisposition($item->getBaseWeeeTaxRowDisposition() + $baseDisposition);
00154
00155 $item->setTaxBeforeDiscount($item->getTaxBeforeDiscount() + $disposition);
00156 $item->setBaseTaxBeforeDiscount($item->getBaseTaxBeforeDiscount() + $baseDisposition);
00157
00158 $address->setTaxAmount($address->getTaxAmount() + $disposition);
00159 $address->setBaseTaxAmount($address->getBaseTaxAmount() + $baseDisposition);
00160
00161 $rate = $taxCalculationModel->getRate($request->setProductClassId($item->getProduct()->getTaxClassId()));
00162
00163 $this->_saveAppliedTaxes(
00164 $address,
00165 $taxCalculationModel->getAppliedRates($request),
00166 $store->roundPrice($valueBeforeVAT-$rowValue),
00167 $store->roundPrice($baseValueBeforeVAT-$baseRowValue),
00168 $rate
00169 );
00170
00171 $address->setGrandTotal($address->getGrandTotal() + $store->roundPrice($valueBeforeVAT-$rowValue));
00172 $address->setBaseGrandTotal($address->getBaseGrandTotal() + $store->roundPrice($baseValueBeforeVAT-$baseRowValue));
00173 }
00174
00175 if (Mage::helper('weee')->includeInSubtotal($store)) {
00176 $address->setSubtotal($address->getSubtotal() + $rowValue);
00177 $address->setBaseSubtotal($address->getBaseSubtotal() + $baseRowValue);
00178
00179 $address->setSubtotalWithDiscount($address->getSubtotalWithDiscount() + $rowValue);
00180 $address->setBaseSubtotalWithDiscount($address->getBaseSubtotalWithDiscount() + $baseRowValue);
00181 } else {
00182 $address->setTaxAmount($address->getTaxAmount() + $rowValue);
00183 $address->setBaseTaxAmount($address->getBaseTaxAmount() + $baseRowValue);
00184 }
00185
00186
00187 $productTaxes[] = array(
00188 'title'=>$title,
00189 'base_amount'=>$baseValue,
00190 'amount'=>$value,
00191
00192 'row_amount'=>$rowValue,
00193 'base_row_amount'=>$baseRowValue,
00194
00195 'base_amount_incl_tax'=>$baseValue+$baseOneDisposition,
00196 'amount_incl_tax'=>$value+$oneDisposition,
00197
00198 'row_amount_incl_tax'=>$rowValue+$disposition,
00199 'base_row_amount_incl_tax'=>$baseRowValue+$baseDisposition,
00200 );
00201
00202 $applied[] = array(
00203 'id'=>$attribute->getCode(),
00204 'percent'=>null,
00205 'hidden'=>Mage::helper('weee')->includeInSubtotal($store),
00206 'rates' => array(array(
00207 'amount'=>$rowValue,
00208 'base_amount'=>$baseRowValue,
00209 'base_real_amount'=>$baseRowValue,
00210 'code'=>$attribute->getCode(),
00211 'title'=>$title,
00212 'percent'=>null,
00213 'position'=>1,
00214 'priority'=>-1000+$k,
00215 ))
00216 );
00217
00218 $item->setBaseWeeeTaxAppliedAmount($item->getBaseWeeeTaxAppliedAmount() + $baseValue);
00219 $item->setBaseWeeeTaxAppliedRowAmount($item->getBaseWeeeTaxAppliedRowAmount() + $baseRowValue);
00220
00221 $item->setWeeeTaxAppliedAmount($item->getWeeeTaxAppliedAmount() + $value);
00222 $item->setWeeeTaxAppliedRowAmount($item->getWeeeTaxAppliedRowAmount() + $rowValue);
00223 }
00224
00225 Mage::helper('weee')->setApplied($item, array_merge(Mage::helper('weee')->getApplied($item), $productTaxes));
00226
00227 if ($updateParent) {
00228 $parent = $item->getParentItem();
00229
00230 $parent->setBaseWeeeTaxDisposition($parent->getBaseWeeeTaxDisposition() + $item->getBaseWeeeTaxDisposition());
00231 $parent->setWeeeTaxDisposition($parent->getWeeeTaxDisposition() + $item->getWeeeTaxDisposition());
00232
00233 $parent->setBaseWeeeTaxRowDisposition($parent->getBaseWeeeTaxRowDisposition() + $item->getBaseWeeeTaxRowDisposition());
00234 $parent->setWeeeTaxRowDisposition($parent->getWeeeTaxRowDisposition() + $item->getWeeeTaxRowDisposition());
00235
00236 $parent->setBaseWeeeTaxAppliedAmount($parent->getBaseWeeeTaxAppliedAmount() + $item->getBaseWeeeTaxAppliedAmount());
00237 $parent->setBaseWeeeTaxAppliedRowAmount($parent->getBaseWeeeTaxAppliedRowAmount() + $item->getBaseWeeeTaxAppliedRowAmount());
00238
00239 $parent->setWeeeTaxAppliedAmount($parent->getWeeeTaxAppliedAmount() + $item->getWeeeTaxAppliedAmount());
00240 $parent->setWeeeTaxAppliedRowAmount($parent->getWeeeTaxAppliedRowAmount() + $item->getWeeeTaxAppliedRowAmount());
00241 }
00242
00243 if ($applied) {
00244 $this->_saveAppliedTaxes(
00245 $address,
00246 $applied,
00247 $item->getWeeeTaxAppliedAmount(),
00248 $item->getBaseWeeeTaxAppliedAmount(),
00249 null
00250 );
00251 }
00252 }
00253
00254 protected function _resetItemData($item)
00255 {
00256 Mage::helper('weee')->setApplied($item, array());
00257
00258 $item->setBaseWeeeTaxDisposition(0);
00259 $item->setWeeeTaxDisposition(0);
00260
00261 $item->setBaseWeeeTaxRowDisposition(0);
00262 $item->setWeeeTaxRowDisposition(0);
00263
00264 $item->setBaseWeeeTaxAppliedAmount(0);
00265 $item->setBaseWeeeTaxAppliedRowAmount(0);
00266
00267 $item->setWeeeTaxAppliedAmount(0);
00268 $item->setWeeeTaxAppliedRowAmount(0);
00269 }
00270
00271 public function fetch(Mage_Sales_Model_Quote_Address $address)
00272 {
00273 return $this;
00274 }
00275 }