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
00029
00030
00031
00032
00033
00034
00035 class Mage_Sales_Model_Quote_Address extends Mage_Customer_Model_Address_Abstract
00036 {
00037 const TYPE_BILLING = 'billing';
00038 const TYPE_SHIPPING = 'shipping';
00039 const RATES_FETCH = 1;
00040 const RATES_RECALCULATE = 2;
00041
00042
00043
00044
00045
00046
00047 protected $_items = null;
00048
00049
00050
00051
00052
00053
00054 protected $_quote = null;
00055
00056
00057
00058
00059
00060
00061 protected $_rates = null;
00062
00063
00064
00065
00066
00067
00068 protected $_totalModels;
00069
00070
00071
00072
00073
00074
00075 protected $_totals = array();
00076
00077
00078
00079
00080 protected function _construct()
00081 {
00082 $this->_init('sales/quote_address');
00083 }
00084
00085
00086
00087
00088
00089
00090 protected function _beforeSave()
00091 {
00092 parent::_beforeSave();
00093 if ($this->getQuote()) {
00094 $quoteId = $this->getQuote()->getId();
00095 if ($quoteId) {
00096 $this->setQuoteId($quoteId);
00097 }
00098 else {
00099 $this->_dataSaveAllowed = false;
00100 }
00101 }
00102 return $this;
00103 }
00104
00105
00106
00107
00108
00109
00110 protected function _afterSave()
00111 {
00112 parent::_afterSave();
00113 if (null !== $this->_items) {
00114 $this->getItemsCollection()->save();
00115 }
00116 if (null !== $this->_rates) {
00117 $this->getShippingRatesCollection()->save();
00118 }
00119 return $this;
00120 }
00121
00122
00123
00124
00125
00126
00127
00128 public function setQuote(Mage_Sales_Model_Quote $quote)
00129 {
00130 $this->_quote = $quote;
00131 $this->setQuoteId($quote->getId());
00132 return $this;
00133 }
00134
00135
00136
00137
00138
00139
00140 public function getQuote()
00141 {
00142 return $this->_quote;
00143 }
00144
00145
00146
00147
00148
00149
00150
00151 public function importCustomerAddress(Mage_Customer_Model_Address $address)
00152 {
00153 Mage::helper('core')->copyFieldset('customer_address', 'to_quote_address', $address, $this);
00154 $this->setEmail($address->hasEmail() ? $address->getEmail() : $address->getCustomer()->getEmail());
00155 return $this;
00156 }
00157
00158
00159
00160
00161
00162
00163 public function exportCustomerAddress()
00164 {
00165 $address = Mage::getModel('customer/address');
00166 Mage::helper('core')->copyFieldset('sales_convert_quote_address', 'to_customer_address', $this, $address);
00167 return $address;
00168 }
00169
00170
00171
00172
00173
00174
00175
00176 public function importOrderAddress(Mage_Sales_Model_Order_Address $address)
00177 {
00178 $this->setAddressType($address->getAddressType())
00179 ->setCustomerId($address->getCustomerId())
00180 ->setCustomerAddressId($address->getCustomerAddressId())
00181 ->setEmail($address->getEmail());
00182
00183 Mage::helper('core')->copyFieldset('sales_convert_order_address', 'to_quote_address', $address, $this);
00184
00185 return $this;
00186 }
00187
00188
00189
00190
00191
00192
00193
00194 public function toArray(array $arrAttributes = array())
00195 {
00196 $arr = parent::toArray();
00197 $arr['rates'] = $this->getShippingRatesCollection()->toArray($arrAttributes);
00198 $arr['items'] = $this->getItemsCollection()->toArray($arrAttributes);
00199 foreach ($this->getTotals() as $k=>$total) {
00200 $arr['totals'][$k] = $total->toArray();
00201 }
00202 return $arr;
00203 }
00204
00205
00206
00207
00208
00209
00210 public function getItemsCollection()
00211 {
00212 if (is_null($this->_items)) {
00213 $this->_items = Mage::getModel('sales/quote_address_item')->getCollection()
00214 ->setAddressFilter($this->getId());
00215
00216 if ($this->getId()) {
00217 foreach ($this->_items as $item) {
00218 $item->setAddress($this);
00219 }
00220 }
00221 }
00222 return $this->_items;
00223 }
00224
00225
00226
00227
00228
00229
00230 public function getAllItems()
00231 {
00232 $quoteItems = $this->getQuote()->getItemsCollection();
00233 $addressItems = $this->getItemsCollection();
00234
00235 $items = array();
00236 if ($this->getQuote()->getIsMultiShipping() && $addressItems->count() > 0) {
00237 foreach ($addressItems as $aItem) {
00238 if ($aItem->isDeleted()) {
00239 continue;
00240 }
00241
00242 if (!$aItem->getQuoteItemImported()) {
00243 if ($qItem = $this->getQuote()->getItemById($aItem->getQuoteItemId())) {
00244 $this->addItem($aItem);
00245 $aItem->importQuoteItem($qItem);
00246 }
00247 }
00248 $items[] = $aItem;
00249 }
00250 } else {
00251 $isQuoteVirtual = $this->getQuote()->isVirtual();
00252 foreach ($quoteItems as $qItem) {
00253 if ($qItem->isDeleted()) {
00254 continue;
00255 }
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265 if ($isQuoteVirtual) {
00266 if ($this->getAddressType() == self::TYPE_BILLING) {
00267 $items[] = $qItem;
00268 }
00269 }
00270 else {
00271 if ($this->getAddressType() == self::TYPE_SHIPPING) {
00272 $items[] = $qItem;
00273 }
00274 }
00275 }
00276 }
00277
00278 return $items;
00279 }
00280
00281
00282
00283
00284
00285
00286 public function getAllVisibleItems()
00287 {
00288 $items = array();
00289 foreach ($this->getAllItems() as $item) {
00290 if (!$item->getParentItemId()) {
00291 $items[] = $item;
00292 }
00293 }
00294 return $items;
00295 }
00296
00297
00298
00299
00300
00301
00302
00303 public function getItemQty($itemId = 0)
00304 {
00305 if ($this->hasData('item_qty')) {
00306 return $this->getData('item_qty');
00307 }
00308
00309 $qty = 0;
00310 if ($itemId == 0) {
00311 foreach ($this->getAllItems() as $item) {
00312 $qty += $item->getQty();
00313 }
00314 } else {
00315 if ($item = $this->getItemById($itemId)) {
00316 $qty = $item->getQty();
00317 }
00318 }
00319 return $qty;
00320 }
00321
00322
00323
00324
00325
00326
00327 public function hasItems()
00328 {
00329 return sizeof($this->getAllItems())>0;
00330 }
00331
00332
00333
00334
00335
00336
00337
00338 public function getItemById($itemId)
00339 {
00340 foreach ($this->getItemsCollection() as $item) {
00341 if ($item->getId()==$itemId) {
00342 return $item;
00343 }
00344 }
00345 return false;
00346 }
00347
00348
00349
00350
00351
00352
00353
00354 public function getItemByQuoteItemId($itemId)
00355 {
00356 foreach ($this->getItemsCollection() as $item) {
00357 if ($item->getQuoteItemId()==$itemId) {
00358 return $item;
00359 }
00360 }
00361 return false;
00362 }
00363
00364
00365
00366
00367
00368
00369
00370 public function removeItem($itemId)
00371 {
00372 if ($item = $this->getItemById($itemId)) {
00373 $item->isDeleted(true);
00374 }
00375 return $this;
00376 }
00377
00378
00379
00380
00381
00382
00383
00384
00385 public function addItem(Mage_Sales_Model_Quote_Item_Abstract $item, $qty=null)
00386 {
00387 if ($item instanceof Mage_Sales_Model_Quote_Item) {
00388 if ($item->getParentItemId()) {
00389 return $this;
00390 }
00391 $addressItem = Mage::getModel('sales/quote_address_item')
00392 ->setAddress($this)
00393 ->importQuoteItem($item);
00394 $this->getItemsCollection()->addItem($addressItem);
00395
00396 if ($item->getHasChildren()) {
00397 foreach ($item->getChildren() as $child) {
00398 $addressChildItem = Mage::getModel('sales/quote_address_item')
00399 ->setAddress($this)
00400 ->importQuoteItem($child)
00401 ->setParentItem($addressItem);
00402 $this->getItemsCollection()->addItem($addressChildItem);
00403 }
00404 }
00405 }
00406 else {
00407 $addressItem = $item;
00408 $addressItem->setAddress($this);
00409 if (!$addressItem->getId()) {
00410 $this->getItemsCollection()->addItem($addressItem);
00411 }
00412 }
00413
00414 if ($qty) {
00415 $addressItem->setQty($qty);
00416 }
00417 return $this;
00418 }
00419
00420
00421
00422
00423
00424
00425 public function getShippingRatesCollection()
00426 {
00427 if (is_null($this->_rates)) {
00428 $this->_rates = Mage::getModel('sales/quote_address_rate')->getCollection()
00429 ->setAddressFilter($this->getId());
00430 if ($this->getId()) {
00431 foreach ($this->_rates as $rate) {
00432 $rate->setAddress($this);
00433 }
00434 }
00435 }
00436 return $this->_rates;
00437 }
00438
00439
00440
00441
00442
00443
00444 public function getAllShippingRates()
00445 {
00446 $rates = array();
00447 foreach ($this->getShippingRatesCollection() as $rate) {
00448 if (!$rate->isDeleted()) {
00449 $rates[] = $rate;
00450 }
00451 }
00452 return $rates;
00453 }
00454
00455
00456
00457
00458
00459
00460 public function getGroupedAllShippingRates()
00461 {
00462 $rates = array();
00463 foreach ($this->getShippingRatesCollection() as $rate) {
00464 if (!$rate->isDeleted() && $rate->getCarrierInstance()) {
00465 if (!isset($rates[$rate->getCarrier()])) {
00466 $rates[$rate->getCarrier()] = array();
00467 }
00468
00469 $rates[$rate->getCarrier()][] = $rate;
00470 $rates[$rate->getCarrier()][0]->carrier_sort_order = $rate->getCarrierInstance()->getSortOrder();
00471 }
00472 }
00473 uasort($rates, array($this, '_sortRates'));
00474 return $rates;
00475 }
00476
00477
00478
00479
00480
00481
00482
00483
00484 protected function _sortRates($a, $b)
00485 {
00486 if ((int)$a[0]->carrier_sort_order < (int)$b[0]->carrier_sort_order) {
00487 return -1;
00488 }
00489 elseif ((int)$a[0]->carrier_sort_order > (int)$b[0]->carrier_sort_order) {
00490 return 1;
00491 }
00492 else {
00493 return 0;
00494 }
00495 }
00496
00497
00498
00499
00500
00501
00502
00503 public function getShippingRateById($rateId)
00504 {
00505 foreach ($this->getShippingRatesCollection() as $rate) {
00506 if ($rate->getId()==$rateId) {
00507 return $rate;
00508 }
00509 }
00510 return false;
00511 }
00512
00513
00514
00515
00516
00517
00518
00519 public function getShippingRateByCode($code)
00520 {
00521 foreach ($this->getShippingRatesCollection() as $rate) {
00522 if ($rate->getCode()==$code) {
00523 return $rate;
00524 }
00525 }
00526 return false;
00527 }
00528
00529
00530
00531
00532
00533
00534 public function removeAllShippingRates()
00535 {
00536 foreach ($this->getShippingRatesCollection() as $rate) {
00537 $rate->isDeleted(true);
00538 }
00539 return $this;
00540 }
00541
00542
00543
00544
00545
00546
00547
00548 public function addShippingRate(Mage_Sales_Model_Quote_Address_Rate $rate)
00549 {
00550 $rate->setAddress($this);
00551 $this->getShippingRatesCollection()->addItem($rate);
00552 return $this;
00553 }
00554
00555
00556
00557
00558
00559
00560 public function collectShippingRates()
00561 {
00562 if (!$this->getCollectShippingRates()) {
00563 return $this;
00564 }
00565
00566 $this->setCollectShippingRates(false);
00567
00568 $this->removeAllShippingRates();
00569
00570 if (!$this->getCountryId() && !$this->getPostcode()) {
00571 return $this;
00572 }
00573
00574 $request = Mage::getModel('shipping/rate_request');
00575 $request->setAllItems($this->getAllItems());
00576 $request->setDestCountryId($this->getCountryId());
00577 $request->setDestRegionId($this->getRegionId());
00578 $request->setDestRegionCode($this->getRegionCode());
00579
00580
00581
00582
00583 $request->setDestStreet($this->getStreet(-1));
00584 $request->setDestCity($this->getCity());
00585 $request->setDestPostcode($this->getPostcode());
00586 $request->setPackageValue($this->getBaseSubtotal());
00587 $request->setPackageValueWithDiscount($this->getBaseSubtotalWithDiscount());
00588 $request->setPackageWeight($this->getWeight());
00589 $request->setPackageQty($this->getItemQty());
00590
00591 $request->setFreeMethodWeight($this->getFreeMethodWeight());
00592
00593
00594
00595
00596
00597
00598
00599 $request->setStoreId($this->getQuote()->getStore()->getId());
00600 $request->setWebsiteId($this->getQuote()->getStore()->getWebsiteId());
00601 $request->setFreeShipping($this->getFreeShipping());
00602
00603
00604
00605 $request->setBaseCurrency($this->getQuote()->getStore()->getBaseCurrency());
00606 $request->setPackageCurrency($this->getQuote()->getStore()->getCurrentCurrency());
00607 $request->setLimitCarrier($this->getLimitCarrier());
00608
00609 $result = Mage::getModel('shipping/shipping')
00610 ->collectRates($request)
00611 ->getResult();
00612
00613 $found = false;
00614 if ($result) {
00615 $shippingRates = $result->getAllRates();
00616
00617 foreach ($shippingRates as $shippingRate) {
00618 $rate = Mage::getModel('sales/quote_address_rate')
00619 ->importShippingRate($shippingRate);
00620 $this->addShippingRate($rate);
00621
00622 if ($this->getShippingMethod()==$rate->getCode()) {
00623 $this->setShippingAmount($rate->getPrice());
00624 $found = true;
00625 }
00626 }
00627 }
00628
00629 if (!$found) {
00630 $this->setShippingAmount(0)
00631 ->setBaseShippingAmount(0)
00632 ->setShippingMethod('')
00633 ->setShippingDescription('');
00634 }
00635
00636 return $this;
00637 }
00638
00639
00640
00641
00642
00643
00644 public function getTotalModels()
00645 {
00646 if (!$this->_totalModels) {
00647 $totalsConfig = Mage::getConfig()->getNode('global/sales/quote/totals');
00648 $models = array();
00649 foreach ($totalsConfig->children() as $totalCode=>$totalConfig) {
00650 $sort = Mage::getStoreConfig('sales/totals_sort/'.$totalCode);
00651 while (isset($models[$sort])) {
00652 $sort++;
00653 }
00654 $class = $totalConfig->getClassName();
00655 if ($class && ($model = Mage::getModel($class))) {
00656 $models[$sort] = $model->setCode($totalCode);
00657 }
00658 }
00659 ksort($models);
00660 $this->_totalModels = $models;
00661 }
00662 return $this->_totalModels;
00663 }
00664
00665
00666
00667
00668
00669
00670 public function collectTotals()
00671 {
00672 foreach ($this->getTotalModels() as $model) {
00673 if (is_callable(array($model, 'collect'))) {
00674 $model->collect($this);
00675 }
00676 }
00677 return $this;
00678 }
00679
00680
00681
00682
00683
00684
00685 public function getTotals()
00686 {
00687 foreach ($this->getTotalModels() as $model) {
00688 if (is_callable(array($model, 'fetch'))) {
00689 $model->fetch($this);
00690 }
00691 }
00692 return $this->_totals;
00693 }
00694
00695
00696
00697
00698
00699
00700
00701 public function addTotal($total)
00702 {
00703 if (is_array($total)) {
00704 $totalInstance = Mage::getModel('sales/quote_address_total')
00705 ->setData($total);
00706 } elseif ($total instanceof Mage_Sales_Model_Quote_Total) {
00707 $totalInstance = $total;
00708 }
00709 $this->_totals[$totalInstance->getCode()] = $totalInstance;
00710 return $this;
00711 }
00712
00713
00714
00715
00716
00717
00718 public function __clone()
00719 {
00720 $this->setId(null);
00721 }
00722
00723
00724
00725
00726
00727
00728 public function validateMinimumAmount()
00729 {
00730 $storeId = $this->getQuote()->getStoreId();
00731 if (!Mage::getStoreConfigFlag('sales/minimum_order/active', $storeId)) {
00732 return true;
00733 }
00734
00735 if ($this->getQuote()->getIsVirtual() && $this->getAddressType() == self::TYPE_SHIPPING) {
00736 return true;
00737 }
00738 elseif (!$this->getQuote()->getIsVirtual() && $this->getAddressType() != self::TYPE_SHIPPING) {
00739 return true;
00740 }
00741
00742 $amount = Mage::getStoreConfig('sales/minimum_order/amount', $storeId);
00743 if ($this->getBaseSubtotalWithDiscount() < $amount) {
00744 return false;
00745 }
00746 return true;
00747 }
00748
00749
00750
00751
00752
00753
00754 public function getAppliedTaxes()
00755 {
00756 return unserialize($this->getData('applied_taxes'));
00757 }
00758
00759
00760
00761
00762
00763
00764
00765 public function setAppliedTaxes($data)
00766 {
00767 return $this->setData('applied_taxes', serialize($data));
00768 }
00769
00770
00771
00772
00773
00774
00775
00776
00777 public function setShippingAmount($value, $alreadyExclTax = false)
00778 {
00779 if (Mage::helper('tax')->shippingPriceIncludesTax()) {
00780 $includingTax = Mage::helper('tax')->getShippingPrice($value, true, $this, $this->getQuote()->getCustomerTaxClassId());
00781 if (!$alreadyExclTax) {
00782 $value = Mage::helper('tax')->getShippingPrice($value, false, $this, $this->getQuote()->getCustomerTaxClassId());
00783 }
00784 $this->setShippingTaxAmount($includingTax - $value);
00785 }
00786 return $this->setData('shipping_amount', $value);
00787 }
00788
00789
00790
00791
00792
00793
00794
00795
00796 public function setBaseShippingAmount($value, $alreadyExclTax = false)
00797 {
00798 if (Mage::helper('tax')->shippingPriceIncludesTax()) {
00799 $includingTax = Mage::helper('tax')->getShippingPrice($value, true, $this, $this->getQuote()->getCustomerTaxClassId());
00800 if (!$alreadyExclTax) {
00801 $value = Mage::helper('tax')->getShippingPrice($value, false, $this, $this->getQuote()->getCustomerTaxClassId());
00802 }
00803 $this->setBaseShippingTaxAmount($includingTax - $value);
00804 }
00805 return $this->setData('base_shipping_amount', $value);
00806 }
00807 }