示例#1
0
 /**
  * TransOpenApiAct::act_getBestCarrier()
  * 固定运输方式费用接口(已废弃)
  * @param string $country 国家
  * @param float $weight 重量
  * @param int $shaddr 发货地ID(预留)
  * @param string $postcode 邮政编码
  * @param string $noShipId 邮政编码
  * @return  json string;
  */
 public function act_getBestCarrier()
 {
     $endTime = strtotime("2014-03-31 00:00:01");
     if ($endTime < time()) {
         self::$errCode = 90000;
         self::$errMsg = '此接口服务已过期,请联系物流系统负责人更换!';
         return;
     }
     $country = isset($_REQUEST['country']) ? rawurldecode(trim($_REQUEST['country'])) : '';
     $weight = isset($_REQUEST['weight']) ? abs(floatval($_REQUEST['weight'])) : 0;
     $shipaddr = isset($_REQUEST['shaddr']) ? post_check($_REQUEST['shaddr']) : '';
     $postcode = isset($_REQUEST['postcode']) ? post_check($_REQUEST['postcode']) : '';
     $noShipId = isset($_REQUEST['noshipid']) ? post_check($_REQUEST['noshipid']) : '';
     //print_r($_REQUEST);exit;
     if (empty($country) || empty($weight) || empty($shipaddr)) {
         //参数不完整
         self::$errCode = 301;
         self::$errMsg = '参数信息不完整';
         return;
     }
     $queryobj = new ShipfeeQueryModel();
     $stdc = $queryobj->translateMinorityLangToStd($country);
     //将小语种转换为标准英文
     $countrystd = '';
     if (empty($stdc)) {
         //没找到 则默认为标准的英文名
         $countrystd = $country;
     } else {
         $countrystd = $stdc['countryName'];
     }
     $data = array('postcode' => $postcode);
     /*根据发货地获取相应的发货方式列表*/
     $shiplist = $queryobj->getShipListByShipaddr($shipaddr, $noShipId);
     /* 计算每一种发货方式的运费 */
     $shipcalculateresult = array();
     //运输方式的计算结果集
     foreach ($shiplist as $shipval) {
         $result = array();
         $channel = $queryobj->getChannelInfo($shipval['id']);
         if (empty($channel)) {
             continue;
         }
         foreach ($channel as $ch) {
             $result['chname'] = $ch['channelName'];
             //渠道名
             $result['channelId'] = $ch['id'];
             //渠道ID
             $result['carrierId'] = $ch['carrierId'];
             //运输方式ID
             $result['carriername'] = $shipval['carrierNameCn'];
             //运输方式名
             $result['paname'] = '';
             //分区名称
             $carriercountryname = $queryobj->translateStdCountryNameToShipCountryName($countrystd, $shipval['id']);
             if (empty($carriercountryname)) {
                 //对照表中没有找到对应的信息 则默认为标准国家名称
                 $carriercountryname = $countrystd;
             }
             $re = $queryobj->calculateShipfee($ch['channelAlias'], $weight, $carriercountryname, $data);
             if (!$re) {
                 continue;
             }
             $result['shipfee'] = $re['fee'];
             $result['rate'] = $re['discount'];
             $shipcalculateresult[] = $result;
         }
     }
     $minship = array();
     $eubFee = array();
     $mineubship = array();
     foreach ($shipcalculateresult as $val) {
         if (empty($minship)) {
             $minship = $val;
         }
         if ($val['shipfee'] < $minship['shipfee']) {
             $minship = $val;
         }
         if ($val['carrierId'] == '6' && ($val['shipfee'] < $mineubship['shipfee'] || empty($mineubship))) {
             $mineubship = $val;
         }
     }
     if (empty($minship)) {
         self::$errCode = 303;
         self::$errMsg = '没有找到最优运输方式';
         return;
     }
     self::$errCode = 300;
     self::$errMsg = 'ok';
     // 如果运输方式为中国邮政平邮或挂号且总重量少于2KG
     if (in_array($minship['carrierId'], array(1, 2)) && $weight <= 2) {
         if (!empty($mineubship['shipfee']) && !empty($minship['shipfee']) && ($mineubship['shipfee'] - $minship['shipfee'] <= 2 || $mineubship['shipfee'] / $minship['shipfee'] - 1 <= 0.09)) {
             return array('fee' => $mineubship['shipfee'], 'carrierId' => $mineubship['carrierId'], 'channelId' => $mineubship['channelId']);
         }
     }
     return array('fee' => $minship['shipfee'], 'carrierId' => $minship['carrierId'], 'channelId' => $minship['channelId']);
 }