309 行
14 KiB
PHP
可执行文件
309 行
14 KiB
PHP
可执行文件
<?php
|
||
|
||
namespace Api\Controller;
|
||
|
||
use Common\Business\Sell;
|
||
use Common\Business\SerialNumberGoods;
|
||
use Common\Business\StockRecord;
|
||
use Common\Model\RetailBillOnlineModel;
|
||
use LittleGift\Business\Order;
|
||
use Think\Controller;
|
||
//[1.8.50]
|
||
|
||
/**
|
||
* 发货及物流信息接口
|
||
* Class ExpressController
|
||
* @package Api\Controller
|
||
*/
|
||
class ExpressController extends Controller
|
||
{
|
||
private $url = "http://poll.kuaidi100.com/poll/query.do?";//请求地址
|
||
|
||
/**
|
||
* 修改订单表状态 添加物流发货记录
|
||
* @param $item
|
||
* @return array
|
||
* platform_type 商户平台类型 1默认系统 2微礼平台
|
||
* $item['type'] 1电子面单 2直接填写单号
|
||
* $item['express_no'] 物流单号
|
||
* $item['express_name']物流名称
|
||
* $item['bill_id'] 订单bill_order表id
|
||
* $item['merchant_id'] 商户id
|
||
* $item['store_id'] 门店id
|
||
* $item['bill_order'] 门店id
|
||
*/
|
||
public function toSendGoods($item)
|
||
{
|
||
$platform_type = $item['platform_type'] ? $item['platform_type'] : 1;
|
||
$ExpressOrderModel = D('ExpressOrder');
|
||
$time = time();
|
||
$addItem = array(
|
||
'merchant_id' => $item['merchant_id'], //int(11) DEFAULT NULL COMMENT '商户id',
|
||
'store_id' => $item['store_id'], //int(11) DEFAULT NULL COMMENT '门店id ',
|
||
'bill_id' => $item['bill_id'], //int(11) DEFAULT NULL COMMENT '订单表bill_order表id',
|
||
'express_no' => $item['express_no'], //varchar(50) DEFAULT NULL COMMENT '物流单号',
|
||
'express_name' => $item['express_name'], //varchar(50) DEFAULT NULL COMMENT '物流名称',
|
||
'express_code' => $item['express_code'], //varchar(50) DEFAULT NULL COMMENT '物流编码',
|
||
'order_type' => $item['order_type'] ? $item['order_type'] : 1, //tinyint(2) DEFAULT '1' COMMENT '1购买商品订单 2大转盘订单 其他预留',
|
||
'add_time' => $time, //varchar(20) DEFAULT NULL COMMENT '添加时间',
|
||
'status' => 1, //tinyint(1) DEFAULT '1' COMMENT '1正常 0取消',
|
||
'type' => $item['type'], //tinyint(1) DEFAULT '1' COMMENT '1电子面单 2直接填写单号',
|
||
'logistics_id' => $item['logistics_id'] ? $item['logistics_id'] : 0,//int(11) DEFAULT '0' COMMENT '菜鸟电子面单记录表id',
|
||
);
|
||
if ($platform_type == 2 && $item['bill_order']['order_type'] == 1) {
|
||
//第三方平台购买订单
|
||
$ThirdOrderClass = new Order();
|
||
$thirdResult = $ThirdOrderClass->orderExecute([
|
||
'merchant_id' => $item['merchant_id'],
|
||
'third_order'=>$item['bill_order']['third_order']
|
||
]);
|
||
if($thirdResult['status'] == 0){
|
||
$res['status'] = 0;
|
||
$res['msg'] = '第三方平台发货失败'.$thirdResult['message'];
|
||
return $res;
|
||
}
|
||
}
|
||
$add = $ExpressOrderModel->addOrder($addItem);
|
||
if ($add) {
|
||
$BillOnlineModel = D('RetailBillOnline');
|
||
$map = array(
|
||
$BillOnlineModel::BILL_ID => $item['bill_id']
|
||
);
|
||
$save = array(
|
||
$BillOnlineModel::DELIVERY_TYPE => 4,
|
||
$BillOnlineModel::STATUS => 2,
|
||
$BillOnlineModel::DELIVERY_TIME => $time,
|
||
);
|
||
$update = $BillOnlineModel->updateOrder($map, $save);
|
||
$res['status'] = 1;//快递发货成功
|
||
//购买商品订单发货通知
|
||
if ($update) {
|
||
$bill_info = $item['bill_order']; //订单信息
|
||
if ($bill_info['order_type'] == 3) {
|
||
//换货订单减少商家库存
|
||
$param = array(
|
||
'bill_order' => $bill_info
|
||
);
|
||
A("Api/Inventory")->CustomerStockDec($param);
|
||
} else if ($bill_info['order_type'] == 1) {
|
||
//商品信息验证
|
||
$SellClass = new Sell();
|
||
$goodsVerify = $SellClass->distributionGoodsVerify([
|
||
'merchant_id' => $item['merchant_id'],
|
||
'store_id' => $item['store_id'],
|
||
'order_id' => $item['bill_id'],
|
||
'type' => 1
|
||
]);
|
||
if ($goodsVerify['status'] != 1) {
|
||
$goodsVerify['msg'] = $goodsVerify['message'];
|
||
return $goodsVerify;
|
||
}
|
||
$goods_serial_list = $goodsVerify['data']; //串号商品的信息
|
||
//购买订单串号仓库减少
|
||
if (!empty($goods_serial_list)) {
|
||
$SerialClass = new SerialNumberGoods();
|
||
$serialResult = $SerialClass->basisNumberOperation([
|
||
'store_id' => $item['store_id'],
|
||
'goods_list' => $goods_serial_list,
|
||
'type' => 2
|
||
]);
|
||
//仓库减少新增出库记录
|
||
if ($serialResult['status'] == 1) {
|
||
$sell_id_arr = array_column($goods_serial_list, 'id');
|
||
if (!empty($sell_id_arr)) {
|
||
$StockRecordClass = new StockRecord();
|
||
$outsideResult = $StockRecordClass->basisSellStockRecord([
|
||
'store_id' => $item['store_id'],
|
||
'sell_id' => $sell_id_arr
|
||
]);
|
||
$outside_id_arr = $outsideResult['data'];
|
||
if (!empty($outside_id_arr)) {
|
||
$new_outside_id_arr = array_column($outside_id_arr, null, 'stock_out_id');
|
||
$OnlineModel = new RetailBillOnlineModel();
|
||
$order_genre = $OnlineModel->getFindField([
|
||
"bill_id" => $item['bill_id'],
|
||
], 'order_genre');
|
||
switch ($order_genre) {
|
||
case 1:
|
||
$stock_type = 8;
|
||
break;
|
||
case 2:
|
||
$stock_type = 4;
|
||
break;
|
||
case 3:
|
||
$stock_type = 5;
|
||
break;
|
||
case 4:
|
||
$stock_type = 11;
|
||
break;
|
||
}
|
||
foreach ($goods_serial_list as $val) {
|
||
if ($val['serial_number_state'] == 1 && !empty($val['serial_list'])) {
|
||
$SerialClass->stockSerial([
|
||
'user_type' => 4,
|
||
'merchant_id' => $item['merchant_id'],
|
||
'store_id' => $item['store_id'],
|
||
'outside_id' => $new_outside_id_arr[$val['id']]['id'],
|
||
'type' => $stock_type,
|
||
'goods_id' => $val['goods_id'],
|
||
'sku_id' => $val['sku_id'] ? $val['sku_id'] : 0,
|
||
'serial_list' => $val['serial_list']
|
||
]);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
$openid = M('retail_xcxvip')->where(array('id' => $item['uid']))->getField("openid");
|
||
$store_info = D("RetailStore")->getFind(array('id' => $item['store_id']), "storetag,detail_address,phone");
|
||
$bill_info = M('retail_bill_order as b')
|
||
->join('ls_retail_bill_online as o on b.id=o.bill_id')
|
||
->where(array('b.id' => $item['bill_id']))
|
||
->field("b.order_no,b.all_money,b.add_time,o.user_address,o.user_phone,o.user_name,o.remark,o.order_genre")
|
||
->find();
|
||
$SellModel = D('RetailSell');
|
||
$sMap = array(
|
||
$SellModel::BILL_ID => $item['bill_id']
|
||
);
|
||
$sFields = array(
|
||
$SellModel::GOODS_NAME
|
||
);
|
||
$sellOrder = $SellModel->getSelect($sMap, $sFields, '', '1,2');
|
||
if (count($sellOrder) > 1) {
|
||
$goods_name = $sellOrder[0][$SellModel::GOODS_NAME] . '等商品';
|
||
} else {
|
||
$goods_name = $sellOrder[0][$SellModel::GOODS_NAME];
|
||
}
|
||
switch ($bill_info['order_genre']) {
|
||
case '1':
|
||
$order_type = '普通订单';
|
||
break;
|
||
case '2':
|
||
$order_type = '拼团订单';
|
||
break;
|
||
case '3':
|
||
$order_type = '秒杀订单';
|
||
break;
|
||
default:
|
||
$order_type = '普通订单';
|
||
}
|
||
$tempSend = array(
|
||
'merchant_id' => $item['merchant_id'],
|
||
'openid' => $openid,
|
||
'type' => 2,//'1自提通知 2发货通知',
|
||
'sendData' => array(
|
||
'store_name' => $store_info['storetag'],
|
||
'delivery_type' => $item['express_name'] ? $item['express_name'] : "快递",
|
||
'delivery_time' => date('Y-m-d H:i:s'),
|
||
'order_no' => $bill_info['order_no'],
|
||
'user_address' => $bill_info['user_address'],
|
||
'user_phone' => $bill_info['user_phone'],
|
||
'username' => $bill_info['user_name'],
|
||
'price' => $bill_info['all_money'],
|
||
'add_time' => date('Y-m-d H:i:s', $bill_info['add_time']),
|
||
'goods_name' => $goods_name,
|
||
'store_address' => $store_info['detail_address'] ? $store_info['detail_address'] : '暂无地址',
|
||
'store_phone' => $store_info['phone'] ? $store_info['phone'] : '暂无手机号',
|
||
'user_remark' => $bill_info['remark'] ? $bill_info['remark'] : '暂无备注',
|
||
'order_type' => $order_type,
|
||
'page' => 'pages/order/myOrderDetail/myOrderDetail?order_no=' . $bill_info['order_no']
|
||
),
|
||
);
|
||
A('Api/XcxMark')->sendMark($tempSend);
|
||
$res['msg'] = '发货成功';
|
||
//操作日志记录
|
||
if ($item['type'] == 1) {
|
||
$msg = '电子面单发货';
|
||
} else {
|
||
$msg = '直接填写订单号发货';
|
||
}
|
||
$source = 0;//请求来源: 0pc系统 1小助手
|
||
$type = 4;//平台:1合伙人端 2代理商端 3商户端 4门店端
|
||
$request_data = json_encode(I('post.'));//请求参数 json数据
|
||
$desc = $order_type . ':' . $msg;//操作行为描述
|
||
action_log($source, $type, $request_data, $desc);
|
||
//end
|
||
} else {
|
||
$res['msg'] = '发货成功,线上订单发货状态更新失败';
|
||
}
|
||
} else {
|
||
$res['status'] = 0;
|
||
$res['msg'] = '数据库发生错误,请稍后重试';
|
||
}
|
||
return $res;
|
||
}
|
||
|
||
public function getExpressInfo($bill_id, $order_type = 1)
|
||
{
|
||
$ExpressOrderModel = D('ExpressOrder');
|
||
$eMap = array(
|
||
'bill_id' => $bill_id,
|
||
'status' => 1,
|
||
'order_type' => $order_type,
|
||
);
|
||
$express = $ExpressOrderModel->getFind($eMap);
|
||
if (empty($express)) {
|
||
$res['status'] = 0;
|
||
$res['msg'] = '未查询到物流或订单已取消发货';
|
||
return $res;
|
||
}
|
||
$param = array(
|
||
'com' => $express['express_code'], //快递公司编码
|
||
'num' => $express['express_no'],//快递单号
|
||
'resultv2' => '1', //开启行政区域解析
|
||
);
|
||
//请求参数
|
||
$post_data = array();
|
||
$post_data["customer"] = C('KD100_CUSTOMER'); //查询公司编号
|
||
$post_data["param"] = json_encode($param);
|
||
$post_data['sign'] = $this->sign($post_data);
|
||
$params = "";
|
||
foreach ($post_data as $k => $v) {
|
||
$params .= "$k=" . urlencode($v) . "&"; //默认UTF-8编码格式
|
||
}
|
||
$post_data = substr($params, 0, -1);
|
||
|
||
$result = $this->url($post_data);
|
||
$data = str_replace("\"", '"', $result);
|
||
$data = json_decode($data, true);
|
||
$data['express_name'] = $express['express_name'];
|
||
$data['nu'] = $express['express_no'];
|
||
if ($data && $data['status'] == '200') {
|
||
//data['state'] 0在途,1揽收,2疑难,3签收,4退签,5派件,6退回等7个状态
|
||
return array(
|
||
'status' => 1,
|
||
'msg' => '查询成功',
|
||
'data' => $data,
|
||
);
|
||
} else {
|
||
return array(
|
||
'status' => 0,
|
||
'msg' => $data['message'] ? $data['message'] : '暂无物流信息',
|
||
'data' => $data,
|
||
);
|
||
}
|
||
}
|
||
|
||
private function url($data)
|
||
{
|
||
//发送post请求
|
||
$ch = curl_init();
|
||
curl_setopt($ch, CURLOPT_POST, 1);
|
||
curl_setopt($ch, CURLOPT_HEADER, 0);
|
||
curl_setopt($ch, CURLOPT_URL, $this->url);
|
||
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
|
||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||
$result = curl_exec($ch);
|
||
return $result;
|
||
}
|
||
|
||
private function sign($data)
|
||
{
|
||
$string = $data["param"] . C('KD100_KEY') . $data["customer"];
|
||
$signs = md5($string);
|
||
$sign = strtoupper($signs);
|
||
return $sign;
|
||
}
|
||
} |