/**
  * Returns the data model based on the primary key given in the GET variable.
  * If the data model is not found, an HTTP exception will be raised.
  * @param integer the ID of the model to be loaded
  */
 public function loadModel($id)
 {
     $model = ZoneInternational::model()->findByPk($id);
     if ($model === null) {
         throw new CHttpException(404, 'The requested page does not exist.');
     }
     return $model;
 }
 public function actionAutoCompleteLookup()
 {
     if (Yii::app()->request->isAjaxRequest && isset($_GET['q'])) {
         /* q is the default GET variable name that is used by
         		  / the autocomplete widget to pass in user input
         		 */
         $name = $_GET['q'];
         // this was set with the "max" attribute of the CAutoComplete widget
         $limit = min($_GET['limit'], 50);
         $criteria = new CDbCriteria();
         $criteria->condition = "country LIKE :sterm";
         $criteria->params = array(":sterm" => "%{$name}%");
         $criteria->limit = $limit;
         $userArray = ZoneInternational::model()->findAll($criteria);
         $returnVal = '';
         foreach ($userArray as $userAccount) {
             $returnVal .= $userAccount->getAttribute('country') . '|' . $userAccount->getAttribute('zone') . "\n";
         }
         echo $returnVal;
     }
 }
 public function actionGetRates()
 {
     $inquiry = new InquiryForm('api-rate');
     $req = Yii::app()->request;
     $inquiryMap = new CMap();
     $inquiryMap->add('shipper_country', $req->getQuery('shipper_country'));
     $inquiryMap->add('shipper_city', $req->getQuery('shipper_city'));
     $inquiryMap->add('shipper_postal', $req->getQuery('shipper_postal'));
     $inquiryMap->add('receiver_country', $req->getQuery('receiver_country'));
     $inquiryMap->add('receiver_city', $req->getQuery('receiver_city'));
     $inquiryMap->add('receiver_postal', $req->getQuery('receiver_postal'));
     $inquiryMap->add('package_weight', $req->getQuery('package_weight'));
     $inquiry->setAttributes($inquiryMap->toArray());
     if (!$inquiry->validate()) {
         echo CJSON::encode($this->statusError($inquiry->getErrors()));
         Yii::app()->end();
     }
     /**
      * cek for customer rate management
      */
     $customer_id = null;
     $allow_api = array();
     if ($this->token instanceof Token) {
         $customer_id = $this->token->customer_id;
         $customer_rates = CustomerDiscount::model()->findAllByAttributes(array('customer_id' => $this->token->customer_id, 'show_in_api' => 1));
         foreach ($customer_rates as $cus_rate) {
             array_push($allow_api, $cus_rate->service_id);
         }
     }
     /**
      * for international service
      */
     if (strtolower($inquiry->receiver_country) != 'indonesia') {
         $country = ZoneInternational::model()->findByAttributes(array('country' => strtolower($inquiry->receiver_country)));
         if (!$country instanceof ZoneInternational) {
             echo CJSON::encode($this->statusError('There is no available service for the country you\'re requested'));
             Yii::app()->end();
         }
         $rates = RateInternational::getServicesAPI($inquiry->package_weight, $country->zone, $country->transit_time, $customer_id, $allow_api);
         $product = 'International';
         if (count($rates) == 0) {
             echo CJSON::encode($this->statusError('No Available Service'));
             Yii::app()->end();
         }
         echo CJSON::encode($this->statusSuccess(array('zone' => $country->zone, 'service_type' => 'International', 'rates' => $rates)));
         Yii::app()->end();
     } else {
         $routing = IntraCityRouting::model()->findByAttributes(array('postcode' => $inquiry->receiver_postal));
         if ($routing instanceof IntraCityRouting) {
             $area = Area::getZoneID($inquiry->receiver_postal, 'postcode');
             if (!$area) {
                 echo CJSON::encode($this->statusError('No Available Service'));
                 Yii::app()->end();
             }
             $rates1 = RateCity::getCityRateAPI(ProductService::ProductCityCourier, $routing->code, $inquiry->package_weight, $customer_id, $allow_api);
             $rates2 = RateDomestic::getServiceListAPI(1, $area['district_id'], $area['zone_id'], $inquiry->package_weight, ProductService::ProductCityCourier, $customer_id, $allow_api);
             $rates = array_merge($rates1, $rates2);
             $product = 'City Courier';
             if (count($rates) == 0) {
                 echo CJSON::encode($this->statusError('No Available Service'));
                 Yii::app()->end();
             }
         } else {
             $area = Area::getZoneID($inquiry->receiver_postal, 'postcode');
             if (!$area) {
                 echo CJSON::encode($this->statusError('No Available Service'));
                 Yii::app()->end();
             }
             $rates = RateDomestic::getServiceListAPI(1, $area['district_id'], $area['zone_id'], $inquiry->package_weight, ProductService::ProductDomestic, $customer_id, $allow_api);
             $product = 'Domestic';
             if (count($rates) == 0) {
                 echo CJSON::encode($this->statusError('No Available Service'));
                 Yii::app()->end();
             }
         }
         echo CJSON::encode($this->statusSuccess(array('zone_id' => $area['zone_id'], 'service_type' => $product, 'rates' => $rates)));
         Yii::app()->end();
     }
 }
 public function actionGetRates()
 {
     if (Yii::app()->request->isAjaxRequest && isset($_GET['Cek'])) {
         $inquiry = new InquiryForm('api-rate');
         $inquiry->setAttributes($_GET['Cek']);
         $customer_id = null;
         $use_rate = array();
         $rates = array();
         if (isset($_GET['customer_id']) && is_numeric($_GET['customer_id'])) {
             $customer_id = $_GET['customer_id'];
             $customer = Customer::model()->findByPk($customer_id);
             if ($customer instanceof Customer) {
                 $customer_id = $customer->id;
                 $customer_rates = CustomerDiscount::model()->findAllByAttributes(array('customer_id' => $customer->id, 'use_rate' => 1));
                 foreach ($customer_rates as $cus_rate) {
                     array_push($use_rate, $cus_rate->service_id);
                 }
             }
         }
         if (strtolower($inquiry->receiver_country) != 'indonesia') {
             $country = ZoneInternational::model()->findByAttributes(array('country' => strtolower($inquiry->receiver_country)));
             if ($country instanceof ZoneInternational) {
                 $rates = RateInternational::getServices($inquiry->package_weight, $country->zone, $country->transit_time, $customer_id, $use_rate);
                 $product = 'International';
             }
         } else {
             $routing = IntraCityRouting::model()->findByAttributes(array('postcode' => $inquiry->receiver_postal));
             if ($routing instanceof IntraCityRouting) {
                 $rates1 = RateCity::getCityRate(ProductService::ProductCityCourier, $routing->code, $inquiry->package_weight, $customer_id, $use_rate);
                 $rates2 = array();
                 $area = Area::getZoneID($inquiry->receiver_postal, 'postcode');
                 if ($area) {
                     $rates2 = RateDomestic::getServiceList(1, $area['district_id'], $area['zone_id'], $inquiry->package_weight, ProductService::ProductCityCourier, $customer_id, $use_rate);
                     $rates = array_merge($rates1, $rates2);
                 } else {
                     $rates = $rates1;
                 }
                 $product = 'City Courier';
             } else {
                 $area = Area::getZoneID($inquiry->receiver_postal, 'postcode');
                 if ($area) {
                     $rates = RateDomestic::getServiceList(1, $area['district_id'], $area['zone_id'], $inquiry->package_weight, ProductService::ProductDomestic, $customer_id, $use_rate);
                 }
                 $product = 'Domestic';
             }
         }
         $data = new CArrayDataProvider($rates);
         //			CVarDumper::dump($rates,10,true);exit;
         $this->renderPartial('_rates', array('rates' => $data, 'product' => $product, 'customer_id' => $customer_id, 'postal' => $inquiry->receiver_postal, 'country' => $inquiry->receiver_country, 'product' => $product, 'package_weight' => $inquiry->package_weight), false, true);
         Yii::app()->end();
     }
 }