public function run() { $unit = 1; $country = $this->CheckoutForm->shippingCountryCode; $state = $this->CheckoutForm->shippingStateCode; $zipcode = $this->CheckoutForm->shippingPostal; // We can't do a lookup if the country is blank if (empty($country)) { Yii::log('DESTINATION TABLE: insufficient details for destination shipping. Shipping Country is blank.', 'info', 'application.' . __CLASS__ . '.' . __FUNCTION__); return false; } Yii::log("DESTINATION TABLE: searching for {$country}/{$state}/{$zipcode}", 'info', 'application.' . __CLASS__ . "." . __FUNCTION__); if (!isset($this->config['per'])) { Yii::log("DESTINATION TABLE: could not get destination shipping unit.", 'error', 'application.' . __CLASS__ . "." . __FUNCTION__); return false; } // Get the best matching destination $model = Destination::LoadMatching($country, $state, $zipcode); if ($model === null) { Yii::log("DESTINATION TABLE: No matching entry found for {$country} {$state} {$zipcode} .", 'error', 'application.' . __CLASS__ . "." . __FUNCTION__); return false; } if ($this->config['per'] == 'item') { $unit = $this->objCart->TotalItemCount; } elseif ($this->config['per'] == 'weight') { $unit = $this->objCart->Weight; } elseif ($this->config['per'] == 'volume') { $unit = $this->objCart->Length * $this->objCart->Width * $this->objCart->Height; } if ($unit >= $model->ship_free) { $unit -= $model->ship_free; } if ($unit < 0) { $unit = 0; } // If the Base Charge is unset or lesser than 0, don't apply this module if ($model->base_charge == '' || $model->base_charge == null) { $label = Country::CodeById($model->country) . "/" . State::CodeById($model->state); Yii::log("DESTINATION TABLE: Base charge not set for entry '" . $label . "'", 'error', 'application.' . __CLASS__ . "." . __FUNCTION__); return false; } $desc = isset($this->config['offerservices']) ? $this->config['offerservices'] : Yii::t('global', 'Standard 3-5 Business Days'); $ret[$desc] = $model->base_charge + $unit * $model->ship_rate; return $this->convertRetToDisplay($ret); }
/** * Check if a customer's default shipping address is no-tax destination. * * @return bool true if the current customer's default shipping address is in no-tax destination. */ public function defaultShippingIsNoTax() { if (isset($this->defaultShipping) === false) { // TODO: Should we return store default here instead? return false; } $objDestination = Destination::LoadMatching($this->defaultShipping->country, $this->defaultShipping->state, $this->defaultShipping->postal); if ($objDestination === null) { // TODO: Should we return store default here instead? return false; } return $objDestination->taxcode0->IsNoTax(); }
/** * 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); } } } } }
/** * Checks if the user's shipping destination is a valid one based on the * configuration by the store owner. * * @param $checkoutForm - The user's current checkout form. * @return bool - true if the shipping destination is a valid one * false if the destination is restricted to that shipping destination. */ public static function verifyUserShippingDestination($checkoutForm) { $objDestination = Destination::LoadMatching($checkoutForm->shippingCountryCode, $checkoutForm->shippingStateCode, $checkoutForm->shippingPostal); if (is_null($objDestination)) { Yii::log('Destination not matched, going with default (Any/Any)', 'info', 'application.' . __CLASS__ . "." . __FUNCTION__); $objDestination = Destination::getAnyAny(); if ($objDestination === null) { return false; } } return true; }
/** * Find the tax code associated with the provided address * * When the shipping country is empty, the Store Default tax code is used. * This is generally used before an address is entered and for store * pickup. * * If the provided address is not matched to any destination, the tax code * for ANY/ANY is used. * * @param mixed $shippingCountry The 2-letter country code for the country or the country ID. * @param mixed $shippingState The 2-letter code for the state or the state ID. * @param string $shippingPostal The postal code with all spaces removed. * @return TaxCode|null The taxcode object, or null if no corresponding tax code. * @throws CException If tax destinations are not configured. */ public static function getTaxCodeByAddress($shippingCountry, $shippingState, $shippingPostal) { if (empty($shippingCountry)) { // Without a shipping country, we use the default tax code. // This is only likely to occur for store pickup. return static::getDefault(); } // Calculate tax since that may change depending on shipping address. Yii::log(sprintf("Attempting to match with a defined Destination to Country/State/Postal %s/%s/%s", $shippingCountry, $shippingState, $shippingPostal), 'info', 'application.' . __CLASS__ . "." . __FUNCTION__); $objDestination = Destination::LoadMatching($shippingCountry, $shippingState, $shippingPostal); if ($objDestination === null) { Yii::log('Destination not matched, going with default (Any/Any)', 'info', 'application.' . __CLASS__ . "." . __FUNCTION__); $objDestination = Destination::getAnyAny(); } if ($objDestination === null) { // TODO: We shoudn't need to check for this here, since it should be a // configuration error to not have a tax destination configured. throw new CException(Yii::t('checkout', 'Website configuration error. No tax destinations have been defined by the Store Administrator. Cannot continue.')); } Yii::log('Matched Destination destination.id=' . $objDestination->id . ' to tax code destination.taxcode=' . $objDestination->taxcode, 'info', 'application.' . __CLASS__ . '.' . __FUNCTION__); return static::LoadByLS($objDestination->taxcode); }