/**
  * Updates the cart shipping address or billing address.
  * Create address if required.
  * TODO: This function could probably have a better name and might be
  * better off on CheckoutForm.
  *
  * @param string $str
  * @return bool
  */
 public function updateAddressId($str = 'shipping')
 {
     $objAddress = null;
     $intAddressId = null;
     $objCart = Yii::app()->shoppingcart;
     // We need to validate the form. To make sure that we're not passing in an empty
     // checkoutForm or a form missing some values for the cart.
     if ($this->billingSameAsShipping == 0 && $this->validate() === false) {
         return false;
     }
     switch ($str) {
         case 'shipping':
             if (!is_null($this->intShippingAddress) && $this->intShippingAddress != 0) {
                 $intAddressId = $this->intShippingAddress;
                 $this->fillAddressFields($intAddressId, 'shipping');
             } else {
                 $attributes = array('customer_id' => $objCart->customer_id, 'first_name' => $this->shippingFirstName, 'last_name' => $this->shippingLastName, 'address1' => $this->shippingAddress1 ? $this->shippingAddress1 : null, 'address2' => $this->shippingAddress2 ? $this->shippingAddress2 : null, 'city' => $this->shippingCity ? $this->shippingCity : null, 'postal' => $this->shippingPostal ? $this->shippingPostal : null, 'country_id' => isset($this->shippingCountry) ? $this->shippingCountry : null, 'state_id' => isset($this->shippingState) ? $this->shippingState : null);
                 if (isset($this->shippingAddress1) == false) {
                     // if address1 is blank, shopper has chosen store pickup
                     $attributes['store_pickup_email'] = $this->pickupPersonEmail ? $this->pickupPersonEmail : $this->contactEmail;
                 } else {
                     $attributes['store_pickup_email'] = null;
                 }
                 Yii::log("Find or create new Shipping address\n" . print_r($attributes, true), 'info', 'application.' . __CLASS__ . "." . __FUNCTION__);
                 $objAddress = CustomerAddress::findOrCreate($attributes);
                 $objAddress->address_label = $this->shippingLabel ? $this->shippingLabel : Yii::t('global', 'Unlabeled Address');
                 $objAddress->phone = is_null($this->shippingPhone) === true ? $this->contactPhone : $this->shippingPhone;
                 $objAddress->residential = $this->shippingResidential;
             }
             break;
         case 'billing':
             if ($this->billingSameAsShipping == 1) {
                 // we should always have a shipping id before we have a billing id
                 $intAddressId = $objCart->shipaddress_id;
             } elseif (is_null($this->intBillingAddress) === false && $this->intBillingAddress != 0) {
                 $intAddressId = $this->intBillingAddress;
             } elseif ($this->billingAddress1 && $this->billingCity && $this->billingCountry) {
                 $continue = true;
                 if (empty($this->billingAddress1) === true) {
                     $this->addErrors(array('billing_address' => Yii::t('checkout', 'Billing Address cannot be blank')));
                     $continue = false;
                 }
                 if (empty($this->billingCity) === true) {
                     $this->addErrors(array('billing_city' => Yii::t('checkout', 'Billing City cannot be blank')));
                     $continue = false;
                 }
                 if ($continue === false) {
                     Yii::log('Billing address cannot be created or updated, information is missing', 'error', 'application.' . __CLASS__ . '.' . __FUNCTION__);
                     return false;
                 }
                 $attributes = array('customer_id' => $objCart->customer_id, 'address1' => $this->billingAddress1, 'address2' => $this->billingAddress2, 'city' => $this->billingCity, 'postal' => $this->billingPostal, 'country_id' => $this->billingCountry, 'state_id' => $this->billingState);
                 Yii::log("Find or create new Billing address\n" . print_r($attributes, true), 'info', 'application.' . __CLASS__ . "." . __FUNCTION__);
                 $objAddress = CustomerAddress::findOrCreate($attributes);
                 if (isset($objAddress->address_label) === false) {
                     $objAddress->address_label = $this->billingLabel ? $this->billingLabel : Yii::t('global', 'Unlabeled Address');
                 }
                 if (isset($objAddress->first_name) === false) {
                     $objAddress->first_name = $this->contactFirstName ? $this->contactFirstName : $this->shippingFirstName;
                 }
                 if (isset($objAddress->last_name) === false) {
                     $objAddress->last_name = $this->contactLastName ? $this->contactLastName : $this->shippingLastName;
                 }
                 $objAddress->residential = $this->billingResidential ? $this->billingResidential : $objAddress->residential;
             } else {
                 $objAddress = null;
                 $intAddressId = null;
             }
             break;
     }
     if ($objAddress instanceof CustomerAddress) {
         if ($this->scenario === 'StorePickup') {
             $objAddress->setScenario('StorePickup');
         }
         if ($objAddress->save() === false) {
             $this->addErrors($objAddress->getErrors());
             Yii::log("Error creating {$str} address\n" . print_r($objAddress->getErrors(), true), 'error', 'application.' . __CLASS__ . "." . __FUNCTION__);
             return false;
         }
         $intAddressId = $objAddress->id;
     }
     switch ($str) {
         case 'shipping':
             $objCart->shipaddress_id = $intAddressId;
             break;
         case 'billing':
             $objCart->billaddress_id = $intAddressId;
             $this->fillAddressFields($intAddressId);
             break;
     }
     if ($objCart->save() === false) {
         // TODO: We might want to add an error here.
         Yii::log("Error saving Cart:\n" . print_r($objCart->getErrors()), 'error', 'application.' . __CLASS__ . '.' . __FUNCTION__);
     } else {
         $objCart->recalculateAndSave();
     }
     return true;
 }
Beispiel #2
0
 /**
  * This function will run parse an order that we get from Amazon MWS.
  * It saves orders of the customers to the DB.
  * @param $response ListOrderItemsResponse Contains the orders from Amazon
  * Marketplace WebService
  * @return void
  */
 public function parseListOrders($response)
 {
     $checkDate = date("Y-m-d", strtotime($this->amazon_check_time));
     $listOrdersResult = $response->getListOrdersResult();
     if ($listOrdersResult->isSetOrders()) {
         $orders = $listOrdersResult->getOrders();
         $orderList = $orders->getOrder();
         foreach ($orderList as $order) {
             if ($order->isSetAmazonOrderId()) {
                 $strOrderId = $order->getAmazonOrderId();
                 Yii::log("Found Amazon Order " . $strOrderId, 'info', 'application.' . __CLASS__ . "." . __FUNCTION__);
                 $objCart = Cart::LoadByIdStr($strOrderId);
                 if (!$objCart instanceof Cart) {
                     //We ignore orders we've already downloaded
                     $objCart = new Cart();
                     $objCart->id_str = $strOrderId;
                     $objCart->origin = 'amazon';
                     //We mark this as just a cart, not an order, because we download the items next
                     $objCart->cart_type = CartType::cart;
                     $objOrderTotal = $order->getOrderTotal();
                     Yii::log("Order total information " . print_r($objOrderTotal, true), 'info', 'application.' . __CLASS__ . "." . __FUNCTION__);
                     $objCart->total = $objOrderTotal->getAmount();
                     $objCart->currency = $objOrderTotal->getCurrencyCode();
                     $objCart->status = OrderStatus::Requested;
                     $objCart->datetime_cre = $order->getPurchaseDate();
                     $objCart->modified = $order->getLastUpdateDate();
                     if (!$objCart->save()) {
                         Yii::log("Error saving cart " . print_r($objCart->getErrors(), true), 'error', 'application.' . __CLASS__ . "." . __FUNCTION__);
                     }
                     //Since email from is Anonymous, we probably will have to create a shell record
                     $objCustomer = Customer::LoadByEmail($order->getBuyerEmail());
                     if (!$objCustomer) {
                         $customerName = $this->_getCustomerName($order->getBuyerName());
                         $objCustomer = new Customer();
                         $objCustomer->email = $order->getBuyerEmail();
                         $objCustomer->first_name = $customerName['first_name'];
                         $objCustomer->last_name = $customerName['last_name'];
                         $objCustomer->record_type = Customer::EXTERNAL_SHELL_ACCOUNT;
                         $objCustomer->allow_login = Customer::UNAPPROVED_USER;
                         $objCustomer->save();
                     }
                     $objCart->customer_id = $objCustomer->id;
                     if (!$objCart->save()) {
                         Yii::log("Error saving cart " . print_r($objCart->getErrors(), true), 'error', 'application.' . __CLASS__ . "." . __FUNCTION__);
                     }
                     if ($order->isSetShippingAddress()) {
                         $shippingAddress = $order->getShippingAddress();
                         $countrycode = Country::IdByCode($shippingAddress->getCountryCode());
                         if ($shippingAddress->isSetStateOrRegion()) {
                             $objState = State::LoadByCode($shippingAddress->getStateOrRegion(), $countrycode);
                         }
                         $customerName = $this->_getCustomerName($shippingAddress->getName());
                         $config = array('address_label' => 'amazon', 'customer_id' => $objCustomer->id, 'first_name' => $customerName['first_name'], 'last_name' => $customerName['last_name'], 'address1' => $shippingAddress->getAddressLine1(), 'address2' => trim($shippingAddress->getAddressLine2() . " " . $shippingAddress->getAddressLine3()), 'city' => $shippingAddress->getCity(), 'state_id' => $objState->id, 'postal' => $shippingAddress->getPostalCode(), 'country_id' => $countrycode, 'phone' => $shippingAddress->getPhone());
                         $objCustAddress = CustomerAddress::findOrCreate($config);
                         $objCustomer->default_billing_id = $objCustAddress->id;
                         $objCustomer->default_shipping_id = $objCustAddress->id;
                         $objCustomer->save();
                         $objCart->shipaddress_id = $objCustAddress->id;
                         $objCart->billaddress_id = $objCustAddress->id;
                         //Amazon doesn't provide billing data, just dupe
                         if (!$objCart->save()) {
                             Yii::log("Error saving cart " . print_r($objCart->getErrors(), true), 'error', 'application.' . __CLASS__ . "." . __FUNCTION__);
                         }
                         Yii::log("Looking for destination " . $objState->country_code . " " . $objState->code . " " . $shippingAddress->getPostalCode(), 'info', 'application.' . __CLASS__ . "." . __FUNCTION__);
                         $objDestination = Destination::LoadMatching($objState->country_code, $objState->code, $shippingAddress->getPostalCode());
                         if ($objDestination === null) {
                             Yii::log("Did not find destination, using default in Web Store ", 'info', 'application.' . __CLASS__ . "." . __FUNCTION__);
                             $objDestination = Destination::getAnyAny();
                         }
                         $objCart->tax_code_id = $objDestination->taxcode;
                         $objCart->recalculateAndSave();
                     }
                     if ($order->isSetShipServiceLevel()) {
                         $strShip = $order->getShipServiceLevel();
                         //If we have a shipping object already, update it, otherwise create it
                         if (isset($objCart->shipping)) {
                             $objShipping = $objCart->shipping;
                         } else {
                             //create
                             $objShipping = new CartShipping();
                             if (!$objShipping->save()) {
                                 Yii::log("Error saving shipping info for cart " . print_r($objShipping->getErrors(), true), 'error', 'application.' . __CLASS__ . "." . __FUNCTION__);
                             }
                         }
                         if ($order->isSetShipmentServiceLevelCategory()) {
                             $strShip = $order->getShipmentServiceLevelCategory();
                         }
                         $objShipping->shipping_module = get_class($this);
                         $objShipping->shipping_data = $strShip;
                         $objShipping->shipping_method = $this->objModule->getConfig('product');
                         $objShipping->shipping_cost = 0;
                         $objShipping->shipping_sell = 0;
                         $objShipping->save();
                         $objCart->shipping_id = $objShipping->id;
                         if (!$objCart->save()) {
                             Yii::log("Error saving cart " . print_r($objCart->getErrors(), true), 'error', 'application.' . __CLASS__ . "." . __FUNCTION__);
                         }
                     }
                     //Because Amazon comes down with no payment info, just generate one here
                     $objP = new CartPayment();
                     $objP->payment_method = $this->objModule->getConfig('ls_payment_method');
                     $objP->payment_module = get_class($this);
                     $objP->payment_data = 'Amazon';
                     $objP->payment_amount = $objOrderTotal->getAmount();
                     $objP->datetime_posted = $order->getPurchaseDate();
                     if (!$objP->save()) {
                         Yii::log("Error saving payment " . print_r($objP->getErrors(), true), 'error', 'application.' . __CLASS__ . "." . __FUNCTION__);
                     }
                     $objCart->payment_id = $objP->id;
                     if (!$objCart->save()) {
                         Yii::log("Error saving cart " . print_r($objCart->getErrors(), true), 'error', 'application.' . __CLASS__ . "." . __FUNCTION__);
                     }
                     TaskQueue::CreateEvent('integration', get_class($this), 'ListOrderDetails', $objCart->id_str . "," . $checkDate);
                 }
             }
         }
     }
 }