Exemplo n.º 1
0
 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();
     }
 }