コード例 #1
0
 public function actionGetShippingCosts()
 {
     if (Yii::app()->request->isAjaxRequest) {
         if (isset($_POST['Shipment']) && isset($_POST['InquiryForm']) && isset($_POST['ShipmentItem'])) {
             $inquiry = new InquiryForm();
             $inquiry->setAttributes($_POST['InquiryForm']);
             $shipment = new Shipment();
             $shipment->setAttributes($_POST['Shipment']);
             $shipment_items = $_POST['ShipmentItem'];
             $items = array();
             $weight_to_count = array();
             $total_weight = 0;
             foreach ($shipment_items as $item) {
                 $shipment_item = new ShipmentItem();
                 $shipment_item->setScenario('inquiry');
                 $shipment_item->attributes = $item;
                 if ($shipment_item->validate()) {
                     array_push($weight_to_count, ShipmentItem::getStaticWeightToCount($shipment_item->package_weight, $shipment_item->package_height, $shipment_item->package_width, $shipment_item->package_length));
                 }
                 $total_weight = array_sum($weight_to_count);
             }
             switch ($shipment->service_type) {
                 case 'domestic':
                     $rate_price = RateDomestic::getRatePriceBaseOnId($inquiry->domestic_ratePrice_id, $total_weight);
                     break;
                 case 'international':
                     $rate_price = RateInternational::getRatePrice($shipment->service_id, $total_weight, $shipment->type, $inquiry->receiver_country_code);
                     break;
             }
             if (!isset($rate_price)) {
                 $rate_price = 0;
             }
             $inquiry->freight_charges = $rate_price;
             $inquiry->fuel_charges = $rate_price * 0.24;
             $inquiry->vat = ($inquiry->freight_charges + $inquiry->fuel_charges) * 0.01;
             $inquiry->shipping_charges = $inquiry->freight_charges + $inquiry->vat;
             echo CJSON::encode(array('freight_charges' => $inquiry->freight_charges, 'fuel_charges' => $inquiry->fuel_charges, 'vat' => $inquiry->vat, 'shipping_charges' => $inquiry->shipping_charges));
             Yii::app()->end();
         }
     }
 }