public function actionTesRequestPickup()
 {
     $model = new Shipment();
     $model_city = new ShipmentIntracity();
     $model_domestic = new ShipmentDomestic();
     $model_international = new ShipmentInternational();
     $good_types = GoodType::model()->findAll();
     $model->type = 'parcel';
     $model->goods_type = 'B777';
     $model->delivery_instruction = 'Tes Request Pickup API';
     $model->shipper_address = 'Jl. Taman Palem Selatan';
     $model->shipper_city = 'Jakarta';
     $model->shipper_country = 'Indonesia';
     $model->shipper_name = 'Budi Darmawan';
     $model->shipper_phone = '0818334550';
     $model->shipper_province = 'DKI Jakarta';
     $model->shipper_postal = '12010';
     $model->receiver_address = 'Jl. Kesana Kemari';
     $model->receiver_country = 'Indonesia';
     $model->receiver_name = 'Darmawan Budi';
     $model->receiver_phone = '087888838';
     //		$model->receiver_city = 'Bandung';
     //		$model->receiver_province = 'Jawa Barat';
     $model->listtype;
     $model->shipment_description = 'Tes API';
     $model->receiver_postal = '42252';
     $this->render('create', array('model' => $model, 'model_city' => $model_city, 'model_domestic' => $model_domestic, 'model_international' => $model_international, 'good_types' => $good_types));
 }
 /**
  * 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 = GoodType::model()->findByPk($id);
     if ($model === null) {
         throw new CHttpException(404, 'The requested page does not exist.');
     }
     return $model;
 }
 public function actionCreate($service_type = '')
 {
     $items = array();
     $costs = array();
     $model = new Shipment();
     if ($service_type == '') {
         $this->beforeCreate($model);
     } else {
         if (!array_key_exists($service_type, $model->Service_type)) {
             throw new CHttpException(404, 'Halaman tidak ditemukan');
         }
         $model->service_type = $service_type;
         $this->performAjaxValidation($model);
         $good_types = GoodType::model()->findAll();
         $weight_to_count = array();
         $arr_cost = array();
         $total_cost = 0;
         $total_weight = 0;
         $ratePrice = 0;
         /**
          * model untuk data tambahan  
          */
         if ($model->service_type == 'domestic') {
             $model_domestic = new ShipmentDomestic();
         } elseif ($model->service_type == 'city') {
             $model_city = new ShipmentIntracity();
         } elseif ($model->service_type == 'international') {
             $model_international = new ShipmentInternational();
         }
         if (isset($_POST['Shipment'])) {
             $model->setAttributes($_POST['Shipment']);
             /**
              * set the attributs to each service type 
              */
             if ($model->service_type == 'domestic' && isset($_POST['ShipmentDomestic'])) {
                 $model_domestic->setAttributes($_POST['ShipmentDomestic']);
             } elseif ($model->service_type == 'city' && isset($_POST['ShipmentIntracity'])) {
                 $model_city->setAttributes($_POST['ShipmentIntracity']);
             } elseif ($model->service_type == 'international' && isset($_POST['ShipmentInternational'])) {
                 $model_international->setAttributes($_POST['ShipmentInternational']);
             }
             $trans = Yii::app()->db->beginTransaction();
             try {
                 if ($model->save()) {
                     /**
                      * this is for adding shipment events
                      */
                     $shipment_event = new ShipmentEvent();
                     $shipment_event->shipment_id = $model->id;
                     $shipment_event->status = $model->shipping_status;
                     $shipment_event->user_id = Yii::app()->user->id;
                     $shipment_event->save();
                     /**
                      * this is for shipment items 
                      */
                     $shipment_items = $_POST['ShipmentItem'];
                     foreach ($shipment_items as $item) {
                         $shipment_item = new ShipmentItem();
                         $shipment_item->attributes = $item;
                         $shipment_item->shipment_id = $model->id;
                         $shipment_item->save();
                         array_push($weight_to_count, $shipment_item->getWeightToCount());
                     }
                     $total_weight = array_sum($weight_to_count);
                     /**
                      * this is for shipment additional cost 
                      */
                     $shipment_costs = $_POST['ShipmentAdditionalCharge'];
                     foreach ($shipment_costs as $cost) {
                         $shipment_cost = new ShipmentAdditionalCharge();
                         $shipment_cost->attributes = $cost;
                         $shipment_cost->shipment_id = $model->id;
                         $shipment_cost->save();
                         array_push($arr_cost, $shipment_cost->cost);
                     }
                     $total_cost = array_sum($arr_cost);
                     /**
                      * Get the rate price base on service_type 
                      */
                     if ($model->service_type == 'domestic') {
                         $ratePrice = RateDomestic::getRatePrice($model->service_id, 1, $model_domestic->district_id, $model_domestic->zone_id, $total_weight);
                         $model_domestic->shipment_id = $model->id;
                         $model_domestic->save();
                     } elseif ($model->service_type == 'city') {
                         $ratePrice = IntraCityServices::getRates($model->service_id, $area_id = $model_city->area_id, $total_weight);
                         $model_city->shipment_id = $model->id;
                         $model_city->save();
                     } elseif ($model->service_type == 'international' && isset($_POST['ShipmentInternational'])) {
                         $ratePrice = RateInternational::getRatePrice($model->service_id, $total_weight, $model->type, $model_international->zone);
                         $model_international->shipment_id = $model->id;
                         $model_international->save();
                     }
                     $total_cost = $total_cost + $ratePrice;
                     $model->package_weight = $total_weight;
                     $model->charges = $total_cost;
                     $model->save();
                     $trans->commit();
                     $this->redirect(array('admin'));
                 }
             } catch (CDbException $e) {
                 $trans->rollback();
                 throw $e;
             }
         }
         $items[] = new ShipmentItem();
         $costs[] = new ShipmentAdditionalCharge();
         $data_render = array('model' => $model, 'service_type' => $service_type, 'items' => $items, 'costs' => $costs, 'good_types' => $good_types);
         if ($model->service_type == 'domestic') {
             $data_render['model_domestic'] = $model_domestic;
         } else {
             if ($model->service_type == 'city') {
                 $data_render['model_city'] = $model_city;
             } else {
                 if ($model->service_type == 'international') {
                     $data_render['model_international'] = $model_international;
                 }
             }
         }
         $this->render('create', $data_render);
     }
 }