Ejemplo n.º 1
0
 /**
  * Process request data with additional logic for saving quote and creating order
  *
  * @param string $action
  * @return Mage_Adminhtml_Sales_Order_CreateController
  */
 protected function _processData($action = null)
 {
     //2011-12-20 HiepHM: Fix bug auto add product that has product_id = quote item_id
     //Bug arised because must pass $action to _processActionData(), so it could be checked in line 193 of CreateController:
     // ... && !($action == 'save'))
     if ($action != 'save') {
         if (Mage::helper('xpos')->aboveVersion('1.5')) {
             parent::_processActionData($action);
         } else {
             parent::_processData();
         }
     }
     if ($action == 'save') {
         $quote = $this->_getOrderCreateModel()->getQuote();
         if ($storeid = $this->getRequest()->getPost('store_id')) {
             $quote->setStoreId($storeid);
         } else {
             $quote->setStoreId($this->_storeid);
         }
         $data = $this->getRequest()->getPost('order');
         $couponCode = $this->_processDiscount($data);
         $shipping_same_as_billing = $this->getRequest()->getPost('shipping_same_as_billing');
         if ($data['customer_id'] != "false" && intval($data['customer_id'])) {
             $this->_getSession()->setCustomerId((int) $data['customer_id']);
         }
         $billingAddress = array('firstname' => 'Guest ' . $this->_getOrderCreateModel()->getQuote()->getId(), 'lastname' => 'Guest ' . $this->_getOrderCreateModel()->getQuote()->getId(), 'street' => array('Guest Address'), 'city' => empty($this->_billingOrigin['city']) ? 'Guest City' : $this->_billingOrigin['city'], 'country_id' => empty($this->_billingOrigin['country_id']) ? 'Guest City' : $this->_billingOrigin['country_id'], 'region' => empty($this->_billingOrigin['region']) ? 'CA' : $this->_billingOrigin['region'], 'postcode' => empty($this->_billingOrigin['postcode']) ? '95064' : $this->_billingOrigin['postcode'], 'telephone' => Mage::getStoreConfig('general/store_information/phone') == "" ? '1234567890' : Mage::getStoreConfig('general/store_information/phone'));
         if (!empty($data['customer_id']) && ($customer_id = $this->_getSession()->getCustomerId())) {
             if (Mage::getModel('customer/customer')->load($customer_id)->getDefaultBillingAddress()) {
                 $defaultBillingAddress = Mage::getModel('customer/customer')->load($customer_id)->getDefaultBillingAddress()->getData();
             }
             foreach ($billingAddress as $k => $v) {
                 if (!empty($defaultBillingAddress[$k])) {
                     $data['billing_address'][$k] = $defaultBillingAddress[$k];
                 }
             }
             if (!empty($defaultBillingAddress['region_id'])) {
                 unset($data['billing_address']['region']);
                 $data['billing_address']['region_id'] = $defaultBillingAddress['region_id'];
             }
         } else {
             foreach ($billingAddress as $k => $v) {
                 if (empty($data['billing_address'][$k])) {
                     $data['billing_address'][$k] = $v;
                 }
             }
         }
         $quote->getBillingAddress()->addData($data['billing_address']);
         if ($shipping_same_as_billing) {
             $quote->getShippingAddress()->addData($data['billing_address']);
         } else {
             $quote->getShippingAddress()->addData($data['shipping_address']);
         }
         $data['shipping_method'] = "freeshipping_freeshipping";
         $shippingAddress = $quote->getShippingAddress();
         $shippingAddress->setCollectShippingRates(true)->collectShippingRates()->setShippingMethod('freeshipping_freeshipping');
         if ($paymentData = $this->getRequest()->getPost('payment')) {
             $this->_getOrderCreateModel()->setPaymentData($paymentData);
             $quote->getPayment()->addData($paymentData);
         }
         $this->_getOrderCreateModel()->collectRates();
         $quote->collectTotals();
         $quote->save();
         // remove coupon if has
         if ($couponCode) {
             $couponCode->setIsActive(0)->save();
         }
     }
 }