Example #1
0
 /**
  * TransOpenApiAct::act_openBestCarrierShipFee()
  * 开放批量最优运输方式费用接口
  * @param string $country 国家
  * @param float $weight 重量
  * @param int $shipAddId 发货地址ID
  * @param string $postCode 邮政编码
  * @param string $apiToken api调用token
  * @param string $noShipId 不参与计算的运输方式ID
  * @return array;
  */
 public function act_openBestCarrierShipFee()
 {
     $res = array();
     $data = array();
     $openFee = array();
     $noShipArr = array();
     $times = time();
     $usRate = 0;
     $exRates = array();
     $apiToken = isset($_REQUEST['apiToken']) ? post_check($_REQUEST['apiToken']) : '';
     $noShipId = isset($_REQUEST['noShipId']) ? post_check($_REQUEST['noShipId']) : '';
     $weightFlag = isset($_REQUEST['weightFlag']) ? post_check($_REQUEST['weightFlag']) : '';
     if (empty($apiToken)) {
         self::$errCode = 20001;
         self::$errMsg = 'API TOKEN 参数有误!';
         return false;
     }
     if (!empty($noShipId)) {
         if (!preg_match("/^([\\d]+,)*[\\d]+\$/", $noShipId)) {
             self::$errCode = 20002;
             self::$errMsg = '要排除的运输方式ID参数有误!';
             return false;
         } else {
             $noShipArr = explode(",", $noShipId);
         }
     }
     //检查API token 合法性
     $res = ApiCompetenceModel::getApiInfoByToken($apiToken);
     if (empty($res)) {
         self::$errCode = 20004;
         self::$errMsg = '当前API TOKEN数据不存在,请检查相关token数据!';
         return false;
     }
     $apiTokenExpire = isset($res['apiTokenExpire']) ? intval($res['apiTokenExpire']) : 0;
     $apiMaxCount = isset($res['apiMaxCount']) ? intval($res['apiMaxCount']) : 0;
     $apiName = isset($res['apiName']) ? $res['apiName'] : '';
     $apiValue = isset($res['apiValue']) ? $res['apiValue'] : '';
     $apiUid = isset($res['apiUid']) ? $res['apiUid'] : 0;
     $apiId = isset($res['id']) ? $res['id'] : 0;
     $maxCount = 0;
     if ($apiTokenExpire <= $times) {
         self::$errCode = 20003;
         self::$errMsg = '当前API TOKEN已过期,请更新API TOKEN!';
         return false;
     }
     if ($apiName !== 'openBestCarrierShipFee') {
         self::$errCode = 20004;
         self::$errMsg = '当前API调用接口名称有误,请检查!';
         return false;
     }
     if ($apiMaxCount > 0) {
         $s_time = strtotime(date('Y-m-d', $times) . " 00:00:01");
         $e_time = strtotime(date('Y-m-d', $times) . " 23:59:59");
         $res = ApiVisitStatModel::getStatByTime($apiId, $apiUid, $s_time, $e_time);
         $maxCount = isset($res['apiCount']) ? intval($res['apiCount']) : 0;
         if ($maxCount > $apiMaxCount) {
             self::$errCode = 20005;
             self::$errMsg = "当日当前API接口调用次数:{$maxCount},已超过最大次:{$apiMaxCount}!";
             return false;
         }
     }
     //API 接口调用统计
     $res = ApiVisitStatModel::updateApiVisitStat($apiId, $apiUid);
     if (!$res) {
         self::$errCode = 20006;
         self::$errMsg = "API 接口调用统计出错,请联系相关负责人处理!";
         return false;
     }
     //开放API运费计算
     $res = self::act_batchBestCarrier();
     $carriers = explode(",", $apiValue);
     foreach ($carriers as $v) {
         if (!empty($noShipArr)) {
             if (in_array($v, $noShipArr)) {
                 continue;
             }
         }
         $res = self::openFixCarrierQueryNew($v, $weightFlag);
         $totalFee = 0;
         // 开放价格 = 原价 + 开放折扣价
         $result = CarrierOpenModel::getCarrierOpenByCid($v);
         if (empty($res['totalFee'])) {
             continue;
         }
         $totalFee = ceil($res['totalFee'] + $res['totalFee'] * $result['carrierDiscount']);
         //针对运德物流的开放运费查询做美元转换
         if ($apiToken == 'e19d2feabc0eb1705f69c6ea2d9d0e1d') {
             $exRates = TransOpenApiModel::cacheExRateInfo(array('USD'), array('CNY'), 'usRate', 7200, 0);
             $usRate = round(floatval($exRates['USD/CNY']), 4);
             if ($usRate <= 0) {
                 continue;
             }
             $totalFee = ceil($totalFee / $usRate);
         }
         $openFee[] = array("carrierId" => $res['carrierId'], "channelId" => $res['channelId'], "totalFee" => $totalFee, "abb" => $result['carrierAbb'], "enName" => $result['carrierEn'], "aging" => $result['carrierAging'], "note" => $result['carrierNote']);
     }
     //对计算价格默认按照价格升序排列
     foreach ($openFee as $key => $row) {
         $fee[$key] = $row['totalFee'];
     }
     array_multisort($fee, SORT_ASC, $openFee);
     return $openFee;
 }
Example #2
0
 /**
  * ShipfeeQueryModel::cal_globalmail_shenzhen()
  * 香港globalmail运费计算
  * @param float $weight 重量
  * @param string $countryname 发往国家
  * @param $data 待定
  * @return false or array
  */
 public function cal_globalmail_shenzhen($weight, $countryname, $data)
 {
     $totalfee = 0;
     $rate = 0;
     $arealist = array();
     $arealist = TransOpenApiModel::cacheTableInfo('trans_freight_globalmail_shenzhen', '1', 'cal_globalmail_shenzhen', 86400, 0);
     if (empty($arealist)) {
         return false;
     }
     $arearow = array();
     foreach ($arealist as $value) {
         $countrys = explode(',', $value['country']);
         $countrys = array_map(array($this, 'callback_trimspace'), $countrys);
         if (in_array($countryname, $countrys)) {
             $arearow = $value;
             break;
         }
     }
     if (empty($arearow)) {
         return false;
     }
     //运费计算
     $weight_freight = $arearow['weight_freight'];
     $weight_freight_arr = explode(',', $weight_freight);
     foreach ($weight_freight_arr as $key1 => $value1) {
         $value1_arr = explode(':', $value1);
         $weight_range = explode('-', $value1_arr[0]);
         if ($weight > $weight_range[0] && $weight <= $weight_range[1]) {
             $shipfee = $value1_arr[1];
             break;
         }
     }
     if (empty($shipfee)) {
         return false;
     }
     $shipfee *= $weight;
     //油费计算
     $fuelcosts = $arearow['fuelcosts'];
     $zgTranFee = $arearow['zgTranFee'] * $weight;
     $fuelcosts_arr = explode(',', $fuelcosts);
     foreach ($fuelcosts_arr as $key2 => $value2) {
         $value2_arr = explode(':', $value2);
         $weight_range = explode('-', $value2_arr[0]);
         if ($weight > $weight_range[0] && $weight <= $weight_range[1]) {
             $fuelfee = $value2_arr[1];
             break;
         }
     }
     $exRates = TransOpenApiModel::cacheExRateInfo(array('HKD'), array('CNY'), 'hkRate', 7200, 0);
     $hkRate = round(floatval($exRates['HKD/CNY']), 4);
     if ($hkRate <= 0) {
         return array('discount' => 0, 'fee' => 0, 'totalfee' => 0, 'level' => "", 'exRate' => array("hkRate" => 0));
     }
     $shipfee += $fuelfee;
     $shipfee = $shipfee * $hkRate;
     $shipfee += $zgTranFee;
     $totalfee = $shipfee;
     $shipfee = round($shipfee, 4);
     return array('discount' => $rate, 'fee' => $shipfee, 'totalfee' => $totalfee, 'level' => "", 'exRate' => array("hkRate" => $hkRate));
 }