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
00036
00037
00038
00039 class Mage_Sales_Model_Quote extends Mage_Core_Model_Abstract
00040 {
00041
00042
00043
00044 const CHECKOUT_METHOD_REGISTER = 'register';
00045 const CHECKOUT_METHOD_GUEST = 'guest';
00046 const CHECKOUT_METHOD_LOGIN_IN = 'login_in';
00047
00048
00049
00050
00051
00052
00053
00054 protected $_eventPrefix = 'sales_quote';
00055 protected $_eventObject = 'quote';
00056
00057
00058
00059
00060
00061
00062 protected $_customer;
00063
00064
00065
00066
00067
00068
00069 protected $_addresses = null;
00070
00071
00072
00073
00074
00075
00076 protected $_items = null;
00077
00078
00079
00080
00081
00082
00083 protected $_payments = null;
00084
00085
00086
00087
00088 protected function _construct()
00089 {
00090 $this->_init('sales/quote');
00091 }
00092
00093
00094
00095
00096
00097
00098 public function getStoreId()
00099 {
00100 if (!$this->hasStoreId()) {
00101 return Mage::app()->getStore()->getId();
00102 }
00103 return $this->_getData('store_id');
00104 }
00105
00106
00107
00108
00109
00110
00111 public function getStore()
00112 {
00113 return Mage::app()->getStore($this->getStoreId());
00114 }
00115
00116
00117
00118
00119
00120
00121
00122 public function setStore(Mage_Core_Model_Store $store)
00123 {
00124 $this->setStoreId($store->getId());
00125 return $this;
00126 }
00127
00128
00129
00130
00131
00132
00133 public function getSharedStoreIds()
00134 {
00135 $ids = $this->_getData('shared_store_ids');
00136 if (is_null($ids) || !is_array($ids)) {
00137 if ($website = $this->getWebsite()) {
00138 return $website->getStoreIds();
00139 }
00140 return $this->getStore()->getWebsite()->getStoreIds();
00141 }
00142 return $ids;
00143 }
00144
00145
00146
00147
00148
00149
00150 protected function _beforeSave()
00151 {
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170 $globalCurrencyCode = Mage::app()->getBaseCurrencyCode();
00171 $baseCurrency = $this->getStore()->getBaseCurrency();
00172
00173 if ($this->hasForcedCurrency()){
00174 $quoteCurrency = $this->getForcedCurrency();
00175 } else {
00176 $quoteCurrency = $this->getStore()->getCurrentCurrency();
00177 }
00178
00179 $this->setGlobalCurrencyCode($globalCurrencyCode);
00180 $this->setBaseCurrencyCode($baseCurrency->getCode());
00181 $this->setStoreCurrencyCode($baseCurrency->getCode());
00182 $this->setQuoteCurrencyCode($quoteCurrency->getCode());
00183
00184
00185 $this->setStoreToBaseRate($baseCurrency->getRate($globalCurrencyCode));
00186 $this->setStoreToQuoteRate($baseCurrency->getRate($quoteCurrency));
00187
00188 $this->setBaseToGlobalRate($baseCurrency->getRate($globalCurrencyCode));
00189 $this->setBaseToQuoteRate($baseCurrency->getRate($quoteCurrency));
00190
00191 if (!$this->hasChangedFlag() || $this->getChangedFlag() == true) {
00192 $this->setIsChanged(1);
00193 } else {
00194 $this->setIsChanged(0);
00195 }
00196
00197 parent::_beforeSave();
00198 }
00199
00200
00201
00202
00203
00204
00205 protected function _afterSave()
00206 {
00207 parent::_afterSave();
00208
00209 if (null !== $this->_addresses) {
00210 $this->getAddressesCollection()->save();
00211 }
00212
00213 if (null !== $this->_items) {
00214 $this->getItemsCollection()->save();
00215 }
00216
00217 if (null !== $this->_payments) {
00218 $this->getPaymentsCollection()->save();
00219 }
00220 return $this;
00221 }
00222
00223
00224
00225
00226
00227
00228 public function loadByCustomer($customer)
00229 {
00230 if ($customer instanceof Mage_Customer_Model_Customer) {
00231 $customerId = $customer->getId();
00232 $this->setStoreId($customer->getStoreId());
00233 }
00234 else {
00235 $customerId = (int) $customer;
00236 }
00237 $this->_getResource()->loadByCustomerId($this, $customerId);
00238 $this->_afterLoad();
00239 return $this;
00240 }
00241
00242
00243
00244
00245
00246
00247
00248 public function assignCustomer(Mage_Customer_Model_Customer $customer)
00249 {
00250 if ($customer->getId()) {
00251 $this->setCustomer($customer);
00252
00253 $defaultBillingAddress = $customer->getDefaultBillingAddress();
00254 if ($defaultBillingAddress && $defaultBillingAddress->getId()) {
00255 $billingAddress = Mage::getModel('sales/quote_address')
00256 ->importCustomerAddress($defaultBillingAddress);
00257 $this->setBillingAddress($billingAddress);
00258 }
00259
00260 $defaultShippingAddress= $customer->getDefaultShippingAddress();
00261 if ($defaultShippingAddress && $defaultShippingAddress->getId()) {
00262 $shippingAddress = Mage::getModel('sales/quote_address')
00263 ->importCustomerAddress($defaultShippingAddress);
00264 }
00265 else {
00266 $shippingAddress = Mage::getModel('sales/quote_address');
00267 }
00268 $this->setShippingAddress($shippingAddress);
00269 }
00270
00271 return $this;
00272 }
00273
00274
00275
00276
00277
00278
00279
00280 public function setCustomer(Mage_Customer_Model_Customer $customer)
00281 {
00282 $this->_customer = $customer;
00283 Mage::helper('core')->copyFieldset('customer_account', 'to_quote', $customer, $this);
00284 return $this;
00285 }
00286
00287
00288
00289
00290
00291
00292 public function getCustomer()
00293 {
00294 if (is_null($this->_customer)) {
00295 $this->_customer = Mage::getModel('customer/customer');
00296 if ($customerId = $this->getCustomerId()) {
00297 $this->_customer->load($customerId);
00298 if (!$this->_customer->getId()) {
00299 $this->_customer->setCustomerId(null);
00300 }
00301 }
00302 }
00303 return $this->_customer;
00304 }
00305
00306 public function getCustomerTaxClassId()
00307 {
00308
00309
00310
00311
00312
00313 $classId = Mage::getModel('customer/group')->getTaxClassId($this->getCustomerGroupId());
00314 $this->setCustomerTaxClassId($classId);
00315
00316 return $this->getData('customer_tax_class_id');
00317 }
00318
00319
00320
00321
00322
00323
00324
00325 public function getCheckoutMethod($originalMethod = false)
00326 {
00327 if ($this->getCustomerId() && !$originalMethod) {
00328 return self::CHECKOUT_METHOD_LOGIN_IN;
00329 }
00330 return $this->_getData('checkout_method');
00331 }
00332
00333
00334
00335
00336
00337
00338 public function getAddressesCollection()
00339 {
00340 if (is_null($this->_addresses)) {
00341 $this->_addresses = Mage::getModel('sales/quote_address')->getCollection()
00342 ->setQuoteFilter($this->getId());
00343
00344 if ($this->getId()) {
00345 foreach ($this->_addresses as $address) {
00346 $address->setQuote($this);
00347 }
00348 }
00349 }
00350 return $this->_addresses;
00351 }
00352
00353
00354
00355
00356
00357
00358
00359 protected function _getAddressByType($type)
00360 {
00361 foreach ($this->getAddressesCollection() as $address) {
00362 if ($address->getAddressType() == $type && !$address->isDeleted()) {
00363 return $address;
00364 }
00365 }
00366
00367 $address = Mage::getModel('sales/quote_address')->setAddressType($type);
00368 $this->addAddress($address);
00369 return $address;
00370 }
00371
00372
00373
00374
00375
00376
00377 public function getBillingAddress()
00378 {
00379 return $this->_getAddressByType(Mage_Sales_Model_Quote_Address::TYPE_BILLING);
00380 }
00381
00382
00383
00384
00385
00386
00387 public function getShippingAddress()
00388 {
00389 return $this->_getAddressByType(Mage_Sales_Model_Quote_Address::TYPE_SHIPPING);
00390 }
00391
00392 public function getAllShippingAddresses()
00393 {
00394 $addresses = array();
00395 foreach ($this->getAddressesCollection() as $address) {
00396 if ($address->getAddressType()==Mage_Sales_Model_Quote_Address::TYPE_SHIPPING
00397 && !$address->isDeleted()) {
00398 $addresses[] = $address;
00399 }
00400 }
00401 return $addresses;
00402 }
00403
00404 public function getAllAddresses()
00405 {
00406 $addresses = array();
00407 foreach ($this->getAddressesCollection() as $address) {
00408 if (!$address->isDeleted()) {
00409 $addresses[] = $address;
00410 }
00411 }
00412 return $addresses;
00413 }
00414
00415
00416
00417
00418
00419
00420 public function getAddressById($addressId)
00421 {
00422 foreach ($this->getAddressesCollection() as $address) {
00423 if ($address->getId()==$addressId) {
00424 return $address;
00425 }
00426 }
00427 return false;
00428 }
00429
00430 public function getAddressByCustomerAddressId($addressId)
00431 {
00432 foreach ($this->getAddressesCollection() as $address) {
00433 if (!$address->isDeleted() && $address->getCustomerAddressId()==$addressId) {
00434 return $address;
00435 }
00436 }
00437 return false;
00438 }
00439
00440 public function getShippingAddressByCustomerAddressId($addressId)
00441 {
00442 foreach ($this->getAddressesCollection() as $address) {
00443 if (!$address->isDeleted() && $address->getAddressType()==Mage_Sales_Model_Quote_Address::TYPE_SHIPPING
00444 && $address->getCustomerAddressId()==$addressId) {
00445 return $address;
00446 }
00447 }
00448 return false;
00449 }
00450
00451 public function removeAddress($addressId)
00452 {
00453 foreach ($this->getAddressesCollection() as $address) {
00454 if ($address->getId()==$addressId) {
00455 $address->isDeleted(true);
00456 break;
00457 }
00458 }
00459 return $this;
00460 }
00461
00462 public function removeAllAddresses()
00463 {
00464 foreach ($this->getAddressesCollection() as $address) {
00465 $address->isDeleted(true);
00466 }
00467 return $this;
00468 }
00469
00470 public function addAddress(Mage_Sales_Model_Quote_Address $address)
00471 {
00472 $address->setQuote($this);
00473 if (!$address->getId()) {
00474 $this->getAddressesCollection()->addItem($address);
00475 }
00476 return $this;
00477 }
00478
00479
00480
00481
00482
00483
00484
00485 public function setBillingAddress(Mage_Sales_Model_Quote_Address $address)
00486 {
00487 $old = $this->getBillingAddress();
00488
00489 if (!empty($old)) {
00490 $old->addData($address->getData());
00491 } else {
00492 $this->addAddress($address->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_BILLING));
00493 }
00494 return $this;
00495 }
00496
00497
00498
00499
00500
00501
00502
00503 public function setShippingAddress(Mage_Sales_Model_Quote_Address $address)
00504 {
00505 if ($this->getIsMultiShipping()) {
00506 $this->addAddress($address->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_SHIPPING));
00507 }
00508 else {
00509 $old = $this->getShippingAddress();
00510
00511 if (!empty($old)) {
00512 $old->addData($address->getData());
00513 } else {
00514 $this->addAddress($address->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_SHIPPING));
00515 }
00516 }
00517 return $this;
00518 }
00519
00520 public function addShippingAddress(Mage_Sales_Model_Quote_Address $address)
00521 {
00522 $this->setShippingAddress($address);
00523 return $this;
00524 }
00525
00526
00527
00528
00529
00530
00531
00532 public function getItemsCollection($useCache = true)
00533 {
00534 if (is_null($this->_items)) {
00535 $this->_items = Mage::getModel('sales/quote_item')->getCollection();
00536 $this->_items->setQuote($this);
00537 }
00538 return $this->_items;
00539 }
00540
00541
00542
00543
00544
00545
00546 public function getAllItems()
00547 {
00548 $items = array();
00549 foreach ($this->getItemsCollection() as $item) {
00550 if (!$item->isDeleted()) {
00551 $items[] = $item;
00552 }
00553 }
00554 return $items;
00555 }
00556
00557
00558
00559
00560
00561
00562 public function getAllVisibleItems()
00563 {
00564 $items = array();
00565 foreach ($this->getItemsCollection() as $item) {
00566 if (!$item->isDeleted() && !$item->getParentItemId()) {
00567 $items[] = $item;
00568 }
00569 }
00570 return $items;
00571 }
00572
00573
00574
00575
00576
00577
00578 public function hasItems()
00579 {
00580 return sizeof($this->getAllItems())>0;
00581 }
00582
00583
00584
00585
00586
00587
00588 public function hasItemsWithDecimalQty()
00589 {
00590 foreach ($this->getAllItems() as $item) {
00591 if ($item->getProduct()->getStockItem()
00592 && $item->getProduct()->getStockItem()->getIsQtyDecimal()) {
00593 return true;
00594 }
00595 }
00596 return false;
00597 }
00598
00599
00600
00601
00602
00603
00604
00605 public function getItemById($itemId)
00606 {
00607 return $this->getItemsCollection()->getItemById($itemId);
00608 }
00609
00610
00611
00612
00613
00614
00615
00616 public function removeItem($itemId)
00617 {
00618 if ($item = $this->getItemById($itemId)) {
00619
00620
00621
00622 $this->setIsMultiShipping(false);
00623 $item->isDeleted(true);
00624 if ($item->getHasChildren()) {
00625 foreach ($item->getChildren() as $child) {
00626 $child->isDeleted(true);
00627 }
00628 }
00629 Mage::dispatchEvent('sales_quote_remove_item', array('quote_item' => $item));
00630 }
00631 return $this;
00632 }
00633
00634
00635
00636
00637
00638
00639
00640 public function addItem(Mage_Sales_Model_Quote_Item $item)
00641 {
00642 $item->setQuote($this);
00643 if (!$item->getId()) {
00644 $this->getItemsCollection()->addItem($item);
00645 Mage::dispatchEvent('sales_quote_add_item', array('quote_item' => $item));
00646 }
00647 return $this;
00648 }
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658 public function addProduct(Mage_Catalog_Model_Product $product, $request=null)
00659 {
00660
00661 if ($request === null) {
00662 $request = 1;
00663 }
00664 if (is_numeric($request)) {
00665 $request = new Varien_Object(array('qty'=>$request));
00666 }
00667 if (!($request instanceof Varien_Object)) {
00668 Mage::throwException(Mage::helper('sales')->__('Invalid request for adding product to quote'));
00669 }
00670
00671 $cartCandidates = $product->getTypeInstance(true)
00672 ->prepareForCart($request, $product);
00673
00674
00675
00676
00677
00678 if (is_string($cartCandidates)) {
00679 return $cartCandidates;
00680 }
00681
00682
00683
00684
00685 if (!is_array($cartCandidates)) {
00686 $cartCandidates = array($cartCandidates);
00687 }
00688
00689
00690
00691
00692 $parentItem = null;
00693 $errors = array();
00694 foreach ($cartCandidates as $candidate) {
00695 $item = $this->_addCatalogProduct($candidate, $candidate->getCartQty());
00696
00697
00698
00699
00700 if (!$parentItem) {
00701 $parentItem = $item;
00702 }
00703 if ($parentItem && $candidate->getParentProductId()) {
00704 $item->setParentItem($parentItem);
00705 }
00706
00707
00708
00709
00710 $item->addQty($candidate->getCartQty());
00711
00712
00713 if ($item->getHasError()) {
00714 $errors[] = $item->getMessage();
00715 }
00716 }
00717 if (!empty($errors)) {
00718 Mage::throwException(implode("\n", $errors));
00719 }
00720 return $item;
00721 }
00722
00723
00724
00725
00726
00727
00728
00729 protected function _addCatalogProduct(Mage_Catalog_Model_Product $product, $qty=1)
00730 {
00731
00732 $item = $this->getItemByProduct($product);
00733 if (!$item) {
00734 $item = Mage::getModel('sales/quote_item');
00735 $item->setQuote($this);
00736 }
00737
00738
00739
00740
00741 if ($item->getId() && $product->getParentProductId()) {
00742 return $item;
00743 }
00744
00745 $item->setOptions($product->getCustomOptions())
00746 ->setProduct($product);
00747
00748 $this->addItem($item);
00749
00750 return $item;
00751 }
00752
00753
00754
00755
00756
00757
00758
00759 public function getItemByProduct($product)
00760 {
00761 foreach ($this->getAllItems() as $item) {
00762 if ($item->representProduct($product)) {
00763 return $item;
00764 }
00765 }
00766 return false;
00767 }
00768
00769 public function getItemsSummaryQty()
00770 {
00771 $qty = $this->getData('all_items_qty');
00772 if (is_null($qty)) {
00773 $qty = 0;
00774 foreach ($this->getAllItems() as $item) {
00775 if ($item->getParentItem()) {
00776 continue;
00777 }
00778
00779 if (($children = $item->getChildren()) && $item->isShipSeparately()) {
00780 foreach ($children as $child) {
00781 $qty+= $child->getQty()*$item->getQty();
00782 }
00783 } else {
00784 $qty+= $item->getQty();
00785 }
00786 }
00787 $this->setData('all_items_qty', $qty);
00788 }
00789 return $qty;
00790 }
00791
00792 public function getItemVirtualQty()
00793 {
00794 $qty = $this->getData('virtual_items_qty');
00795 if (is_null($qty)) {
00796 $qty = 0;
00797 foreach ($this->getAllItems() as $item) {
00798 if ($item->getParentItem()) {
00799 continue;
00800 }
00801
00802 if (($children = $item->getChildren()) && $item->isShipSeparately()) {
00803 foreach ($children as $child) {
00804 if ($child->getProduct()->getIsVirtual()) {
00805 $qty+= $child->getQty();
00806 }
00807 }
00808 } else {
00809 if ($item->getProduct()->getIsVirtual()) {
00810 $qty+= $item->getQty();
00811 }
00812 }
00813 }
00814 $this->setData('virtual_items_qty', $qty);
00815 }
00816 return $qty;
00817 }
00818
00819
00820 public function getPaymentsCollection()
00821 {
00822 if (is_null($this->_payments)) {
00823 $this->_payments = Mage::getModel('sales/quote_payment')->getCollection()
00824 ->setQuoteFilter($this->getId());
00825
00826 if ($this->getId()) {
00827 foreach ($this->_payments as $payment) {
00828 $payment->setQuote($this);
00829 }
00830 }
00831 }
00832 return $this->_payments;
00833 }
00834
00835
00836
00837
00838 public function getPayment()
00839 {
00840 foreach ($this->getPaymentsCollection() as $payment) {
00841 if (!$payment->isDeleted()) {
00842 return $payment;
00843 }
00844 }
00845 $payment = Mage::getModel('sales/quote_payment');
00846 $this->addPayment($payment);
00847 return $payment;
00848 }
00849
00850 public function getPaymentById($paymentId)
00851 {
00852 foreach ($this->getPaymentsCollection() as $payment) {
00853 if ($payment->getId()==$paymentId) {
00854 return $payment;
00855 }
00856 }
00857 return false;
00858 }
00859
00860 public function addPayment(Mage_Sales_Model_Quote_Payment $payment)
00861 {
00862 $payment->setQuote($this);
00863 if (!$payment->getId()) {
00864 $this->getPaymentsCollection()->addItem($payment);
00865 }
00866 return $this;
00867 }
00868
00869 public function setPayment(Mage_Sales_Model_Quote_Payment $payment)
00870 {
00871 if (!$this->getIsMultiPayment() && ($old = $this->getPayment())) {
00872 $payment->setId($old->getId());
00873 }
00874 $this->addPayment($payment);
00875
00876 return $payment;
00877 }
00878
00879 public function removePayment()
00880 {
00881 $this->getPayment()->isDeleted(true);
00882 return $this;
00883 }
00884
00885
00886
00887
00888
00889
00890 public function collectTotals()
00891 {
00892 Mage::dispatchEvent(
00893 $this->_eventPrefix . '_collect_totals_before',
00894 array(
00895 $this->_eventObject=>$this
00896 )
00897 );
00898
00899 $this->setSubtotal(0);
00900 $this->setBaseSubtotal(0);
00901
00902 $this->setSubtotalWithDiscount(0);
00903 $this->setBaseSubtotalWithDiscount(0);
00904
00905 $this->setGrandTotal(0);
00906 $this->setBaseGrandTotal(0);
00907
00908 foreach ($this->getAllAddresses() as $address) {
00909 $address->setSubtotal(0);
00910 $address->setBaseSubtotal(0);
00911
00912 $address->setSubtotalWithDiscount(0);
00913 $address->setBaseSubtotalWithDiscount(0);
00914
00915 $address->setGrandTotal(0);
00916 $address->setBaseGrandTotal(0);
00917
00918 $address->collectTotals();
00919
00920 $this->setSubtotal((float) $this->getSubtotal()+$address->getSubtotal());
00921 $this->setBaseSubtotal((float) $this->getBaseSubtotal()+$address->getBaseSubtotal());
00922
00923 $this->setSubtotalWithDiscount((float) $this->getSubtotalWithDiscount()+$address->getSubtotalWithDiscount());
00924 $this->setBaseSubtotalWithDiscount((float) $this->getBaseSubtotalWithDiscount()+$address->getBaseSubtotalWithDiscount());
00925
00926 $this->setGrandTotal((float) $this->getGrandTotal()+$address->getGrandTotal());
00927 $this->setBaseGrandTotal((float) $this->getBaseGrandTotal()+$address->getBaseGrandTotal());
00928 }
00929
00930 Mage::helper('sales')->checkQuoteAmount($this, $this->getGrandTotal());
00931 Mage::helper('sales')->checkQuoteAmount($this, $this->getBaseGrandTotal());
00932
00933 $this->setItemsCount(0);
00934 $this->setItemsQty(0);
00935 $this->setVirtualItemsQty(0);
00936
00937 foreach ($this->getAllVisibleItems() as $item) {
00938 if ($item->getParentItem()) {
00939 continue;
00940 }
00941
00942 if (($children = $item->getChildren()) && $item->isShipSeparately()) {
00943 foreach ($children as $child) {
00944 if ($child->getProduct()->getIsVirtual()) {
00945 $this->setVirtualItemsQty($this->getVirtualItemsQty() + $child->getQty()*$item->getQty());
00946 }
00947 }
00948 }
00949
00950 if ($item->getProduct()->getIsVirtual()) {
00951 $this->setVirtualItemsQty($this->getVirtualItemsQty() + $item->getQty());
00952 }
00953 $this->setItemsCount($this->getItemsCount()+1);
00954 $this->setItemsQty((float) $this->getItemsQty()+$item->getQty());
00955 }
00956
00957 $this->setData('trigger_recollect', 0);
00958 $this->_validateCouponCode();
00959
00960 Mage::dispatchEvent(
00961 $this->_eventPrefix . '_collect_totals_after',
00962 array(
00963 $this->_eventObject=>$this
00964 )
00965 );
00966
00967 return $this;
00968 }
00969
00970
00971
00972
00973
00974
00975 public function getTotals()
00976 {
00977 $totals = $this->getShippingAddress()->getTotals();
00978 foreach ($this->getBillingAddress()->getTotals() as $code => $total) {
00979 if (isset($totals[$code])) {
00980 $totals[$code]->setValue($totals[$code]->getValue()+$total->getValue());
00981 }
00982 else {
00983 $totals[$code] = $total;
00984 }
00985 }
00986
00987 $sortedTotals = array();
00988 foreach ($this->getBillingAddress()->getTotalModels() as $total) {
00989
00990 if (isset($totals[$total->getCode()])) {
00991 $sortedTotals[$total->getCode()] = $totals[$total->getCode()];
00992 }
00993 }
00994 return $sortedTotals;
00995 }
00996
00997 public function addMessage($message, $index='error')
00998 {
00999 $messages = $this->getData('messages');
01000 if (is_null($messages)) {
01001 $messages = array();
01002 }
01003
01004 if (isset($messages[$index])) {
01005 return $this;
01006 }
01007
01008 if (is_string($message)) {
01009 $message = Mage::getSingleton('core/message')->error($message);
01010 }
01011
01012 $messages[$index] = $message;
01013 $this->setData('messages', $messages);
01014 return $this;
01015 }
01016
01017 public function getMessages()
01018 {
01019 $messages = $this->getData('messages');
01020 if (is_null($messages)) {
01021 $messages = array();
01022 $this->setData('messages', $messages);
01023 }
01024 return $messages;
01025 }
01026
01027 public function reserveOrderId()
01028 {
01029 if (!$this->getReservedOrderId()) {
01030 $this->setReservedOrderId($this->_getResource()->getReservedOrderId($this));
01031 } else {
01032
01033
01034 if ($this->_getResource()->isOrderIncrementIdUsed($this->getReservedOrderId())) {
01035 $this->setReservedOrderId($this->_getResource()->getReservedOrderId($this));
01036 }
01037 }
01038 return $this;
01039 }
01040
01041 public function validateMinimumAmount($multishipping = false)
01042 {
01043 $storeId = $this->getStoreId();
01044 $minOrderActive = Mage::getStoreConfigFlag('sales/minimum_order/active', $storeId);
01045 $minOrderMulti = Mage::getStoreConfigFlag('sales/minimum_order/multi_address', $storeId);
01046
01047 if (!$minOrderActive) {
01048 return true;
01049 }
01050
01051 if ($multishipping && !$minOrderMulti) {
01052 $baseTotal = 0;
01053 foreach ($this->getAllAddresses() as $address) {
01054
01055 $baseTotal += $address->getBaseSubtotalWithDiscount();
01056 }
01057
01058 if ($baseTotal < Mage::getStoreConfig('sales/minimum_order/amount', $storeId)) {
01059 return false;
01060 }
01061 }
01062 else {
01063 foreach ($this->getAllAddresses() as $address) {
01064
01065 if (!$address->validateMinimumAmount()) {
01066 return false;
01067 }
01068 }
01069 }
01070 return true;
01071 }
01072
01073
01074
01075
01076
01077
01078 public function isVirtual()
01079 {
01080 $isVirtual = true;
01081 $countItems = 0;
01082 foreach ($this->getItemsCollection() as $_item) {
01083
01084 if ($_item->isDeleted() || $_item->getParentItemId()) {
01085 continue;
01086 }
01087 $countItems ++;
01088 if (!$_item->getProduct()->getIsVirtual()) {
01089 $isVirtual = false;
01090 }
01091 }
01092 return $countItems == 0 ? false : $isVirtual;
01093 }
01094
01095
01096
01097
01098
01099
01100 public function getIsVirtual()
01101 {
01102 return intval($this->isVirtual());
01103 }
01104
01105
01106
01107
01108
01109
01110 public function hasVirtualItems()
01111 {
01112 $hasVirtual = false;
01113 foreach ($this->getItemsCollection() as $_item) {
01114 if ($_item->getParentItemId()) {
01115 continue;
01116 }
01117 if ($_item->getProduct()->isVirtual()) {
01118 $hasVirtual = true;
01119 }
01120 }
01121 return $hasVirtual;
01122 }
01123
01124
01125
01126
01127
01128
01129 public function isAllowedGuestCheckout()
01130 {
01131 return Mage::helper('checkout')->isAllowedGuestCheckout($this, $this->getStoreId());
01132 }
01133
01134
01135
01136
01137
01138
01139
01140 public function merge(Mage_Sales_Model_Quote $quote)
01141 {
01142 Mage::dispatchEvent(
01143 $this->_eventPrefix . '_merge_before',
01144 array(
01145 $this->_eventObject=>$this,
01146 'source'=>$quote
01147 )
01148 );
01149
01150 foreach ($quote->getAllVisibleItems() as $item) {
01151 $found = false;
01152 foreach ($this->getAllItems() as $quoteItem) {
01153 if ($quoteItem->compare($item)) {
01154 $quoteItem->setQty($quoteItem->getQty() + $item->getQty());
01155 $found = true;
01156 break;
01157 }
01158 }
01159
01160 if (!$found) {
01161 $newItem = clone $item;
01162 $this->addItem($newItem);
01163 if ($item->getHasChildren()) {
01164 foreach ($item->getChildren() as $child) {
01165 $newChild = clone $child;
01166 $newChild->setParentItem($newItem);
01167 $this->addItem($newChild);
01168 }
01169 }
01170 }
01171 }
01172
01173 if ($quote->getCouponCode()) {
01174 $this->setCouponCode($quote->getCouponCode());
01175 }
01176
01177 Mage::dispatchEvent(
01178 $this->_eventPrefix . '_merge_after',
01179 array(
01180 $this->_eventObject=>$this,
01181 'source'=>$quote
01182 )
01183 );
01184
01185 return $this;
01186 }
01187
01188 protected function _validateCouponCode()
01189 {
01190 $code = $this->_getData('coupon_code');
01191 if ($code) {
01192 $addressHasCoupon = false;
01193 $addresses = $this->getAllAddresses();
01194 if (count($addresses)>0) {
01195 foreach ($addresses as $address) {
01196 if ($address->hasCouponCode()) {
01197 $addressHasCoupon = true;
01198 }
01199 }
01200 if (!$addressHasCoupon) {
01201 $this->setCouponCode('');
01202 }
01203 }
01204 }
01205 return $this;
01206 }
01207
01208
01209
01210
01211
01212
01213 protected function _afterLoad()
01214 {
01215
01216 if (1 == $this->getData('trigger_recollect')) {
01217 $this->collectTotals()->save();
01218 }
01219 return parent::_afterLoad();
01220 }
01221 }