public static function IdByCode($strCode, $intCountry) { $objState = State::LoadByCode($strCode, $intCountry); if ($objState instanceof State) { return $objState->id; } else { return ''; } }
/** * Get state by state code. * * Returns JSON with state ID and name. */ public function actionGetStateByCode($stateCode = null, $countryId) { $state = State::LoadByCode($stateCode, $countryId); if (isset($state)) { $response = array('status' => 'success', 'state' => $state); } else { $response = array('status' => 'error', 'errors' => Yii::t('profile', 'Invalid state')); } $this->renderJSON($response); }
/** * 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); } } } } }
/** * 18 Change destination tables and map to country/state ids */ protected function actionConvertDestinationTables() { //Convert Wish List items to new formats //Ship to me //Ship to buyer //Keep in store //find and remove any destinations with an erroneous taxcode $sql = 'SELECT `id` FROM `xlsws_destination` WHERE `taxcode` NOT IN (SELECT xlsws_tax_code.lsid FROM xlsws_tax_code);'; $array = Yii::app()->db->createCommand($sql)->queryAll(); foreach ($array as $a) { Yii::log('Destination id ' . $a['id'] . 'deleted due to erroneous tax code', 'error', 'application.' . __CLASS__ . "." . __FUNCTION__); _dbx('DELETE FROM `xlsws_destination` WHERE `id`=' . $a['id'] . ';'); } $objDestinations = Destination::model()->findAll(); foreach ($objDestinations as $objDestination) { if ($objDestination->country == "*") { $objDestination->country = null; } else { if (!is_numeric($objDestination->country)) { $objC = Country::LoadByCode($objDestination->country); $objDestination->country = $objC->id; } } if ($objDestination->state == "*" || $objDestination->state == null) { $objDestination->state = null; } else { if (!is_numeric($objDestination->state)) { $objS = State::LoadByCode($objDestination->state, $objDestination->country); $objDestination->state = $objS->id; } } if (!$objDestination->save()) { return print_r($objDestination->getErrors()); } } //Need to map destinations to IDs before doing this _dbx("update `xlsws_destination` set country=null where country=0;"); _dbx("update `xlsws_destination` set state=null where state=0;"); _dbx("ALTER TABLE `xlsws_destination` CHANGE `country` `country` INT(11) UNSIGNED NULL DEFAULT NULL;"); _dbx("ALTER TABLE `xlsws_destination` CHANGE `state` `state` INT(11) UNSIGNED NULL DEFAULT NULL;"); _dbx("ALTER TABLE `xlsws_destination` CHANGE `taxcode` `taxcode` INT(11) UNSIGNED NULL DEFAULT NULL;"); _dbx("ALTER TABLE `xlsws_destination` ADD FOREIGN KEY (`state`) REFERENCES `xlsws_state` (`id`);"); _dbx("ALTER TABLE `xlsws_destination` ADD FOREIGN KEY (`country`) REFERENCES `xlsws_country` (`id`);"); _dbx("ALTER TABLE `xlsws_destination` ADD FOREIGN KEY (`taxcode`) REFERENCES `xlsws_tax_code` (`lsid`);"); _dbx("ALTER TABLE `xlsws_category` CHANGE `custom_page` `custom_page` INT(11) UNSIGNED NULL DEFAULT NULL;"); _dbx("UPDATE `xlsws_category` set `custom_page`=null where `custom_page`=0;"); _dbx("ALTER TABLE `xlsws_category` ADD FOREIGN KEY (`custom_page`) REFERENCES `xlsws_custom_page` (`id`);"); _dbx("ALTER TABLE `xlsws_country` DROP `code_a3`;"); _dbx("update `xlsws_shipping_tiers` set `class_name`='tieredshipping';"); return array('result' => "success", 'makeline' => 19, 'tag' => 'Applying database schema changes', 'total' => 50); }
/** * Match a given address to the most accurate Destination * @param string $country * @param string $state * @param string $zip * @return object :: The matching destination */ public static function LoadMatching($country, $state, $zip) { //We may get id numbers instead of text strings so convert here if (is_numeric($country)) { $country = Country::CodeById($country); } if (is_numeric($state)) { $state = State::CodeById($state); } $arrDestinations = Destination::LoadByCountry($country); if (is_array($arrDestinations) === false && $arrDestinations instanceof Traversable === false) { return null; } $objState = State::LoadByCode($state, $country); $zip = preg_replace('/[^A-Z0-9]/', '', strtoupper($zip)); foreach ($arrDestinations as $objDestination) { if ($objDestination->state == null || $objState !== null && $objState->id == $objDestination->state) { $zipStart = $objDestination->Zipcode1; $zipEnd = $objDestination->Zipcode2; if ($zipStart <= $zip && $zipEnd >= $zip || $zipStart == '' || $zipStart == '*' || $zip == '') { return $objDestination; } } } return null; }
/** * Virtual method for getting the shipping state id. * * @return integer The state country id. */ public function getShippingState() { $objShippingState = State::LoadByCode($this->shippingStateCode, $this->shippingCountry); if ($objShippingState === null) { return null; } return $objShippingState->id; }