1274 lines
57 KiB
PHP
1274 lines
57 KiB
PHP
<?php
|
||
/**
|
||
* SaaSMall商城系统 - 团队十年电商经验汇集巨献!
|
||
* =========================================================
|
||
* Copy right 2019-2029 成都SAAS云科技有限公司, 保留所有权利。
|
||
* ----------------------------------------------
|
||
* 官方网址: https://www.gobuysaas.com
|
||
* =========================================================
|
||
*/
|
||
|
||
namespace addon\supply\model\order;
|
||
|
||
use addon\ali1688\model\CloudApi;
|
||
use addon\supply\model\CloudApi as CloudApiModel;
|
||
use app\model\BaseModel;
|
||
use addon\supply\model\goods\GoodsStock;
|
||
use app\model\system\Cron;
|
||
use think\facade\Cache;
|
||
use think\facade\Db;
|
||
use app\model\express\Express;
|
||
use app\model\system\Pay;
|
||
use addon\supply\model\goods\Cart as CartModel;
|
||
use app\model\express\Config as ExpressConfig;
|
||
use addon\supply\model\order\Config as OrderConfig;
|
||
use app\model\shop\SiteAddress;
|
||
use addon\supply\model\SupplyAccount;
|
||
|
||
/**
|
||
* 订单创建(批发)
|
||
* @author Administrator
|
||
*/
|
||
class OrderCreate extends BaseModel
|
||
{
|
||
public $goods_money = 0;//商品金额
|
||
public $delivery_money = 0;//配送费用
|
||
public $adjust_money = 0;//调整金额
|
||
public $invoice_money = 0;//发票费用
|
||
public $order_money = 0;//订单金额
|
||
public $pay_money = 0;//支付总价
|
||
public $is_virtual = 0; //是否是虚拟类订单
|
||
public $order_name = ''; //订单详情
|
||
public $goods_num = 0; //商品种数
|
||
public $pay_type = 'ONLINE_PAY';//支付方式
|
||
public $is_exist_not_free = false;
|
||
public $is_exist_free = false;
|
||
public $balance_money = 0;//余额
|
||
public $account_balance_money = 0;//会员账户余额(计算过程中会逐次减少)
|
||
public $error = 0; //是否有错误
|
||
public $error_msg = ''; //错误描述
|
||
public $isPay = 0; //扣款状态
|
||
public $order_ids = []; //扣款状态
|
||
|
||
/**
|
||
* 订单创建
|
||
* @param $data
|
||
* @return array|mixed
|
||
*/
|
||
public function create($data)
|
||
{
|
||
$calculate_data = $this->calculate($data);
|
||
if (isset($calculate_data['code']) && $calculate_data['code'] < 0)
|
||
return $calculate_data;
|
||
if ($this->error > 0) {
|
||
return $this->error(['error_code' => $this->error], $this->error_msg);
|
||
}
|
||
$pay = new Pay();
|
||
$out_trade_no = $pay->createOutTradeNo($data['buyer_uid']);
|
||
if ($this->is_exist_not_free) {
|
||
$order_name = [];
|
||
}
|
||
if ($this->is_exist_free) {
|
||
$free_order_name = [];
|
||
}
|
||
model("supply_order")->startTrans();
|
||
//循环生成多个订单
|
||
try {
|
||
$pay_money = 0;
|
||
$goods_stock_model = new GoodsStock();
|
||
foreach ($calculate_data['shop_goods_list'] as $k => $v) {
|
||
$item_delivery = $v['delivery'] ?? [];
|
||
$delivery_type = $item_delivery['delivery_type'] ?? '';
|
||
$delivery_type_name = Express::express_type[$delivery_type]["title"] ?? '';
|
||
//判断交易流水号
|
||
if ($v["pay_money"] == 0) {
|
||
$temp_out_trade_no = $out_trade_no;
|
||
$free_order_name[] = $v["order_name"];
|
||
} else {
|
||
$temp_out_trade_no = $out_trade_no;
|
||
$order_name[] = $v["order_name"];
|
||
}
|
||
//订单主表
|
||
$order_type = $this->orderType($v, $calculate_data);
|
||
$order_no = $this->createOrderNo($v['site_id']);
|
||
$data_order = [
|
||
'order_no' => $order_no,
|
||
'order_from' => $data['order_from'],
|
||
'order_from_name' => $data['order_from_name'],
|
||
'order_type' => $order_type['order_type_id'],
|
||
'order_type_name' => $order_type['order_type_name'],
|
||
'order_status_name' => $order_type['order_status']['name'],
|
||
'order_status_action' => json_encode($order_type['order_status'], JSON_UNESCAPED_UNICODE),
|
||
'out_trade_no' => $temp_out_trade_no,
|
||
'goods_money' => $v['goods_money'],
|
||
'delivery_money' => $v['delivery_money'],
|
||
'adjust_money' => $v['adjust_money'],
|
||
'invoice_money' => $v['invoice_money'],
|
||
'order_money' => $v['order_money'],
|
||
'balance_money' => $v['balance_money'],
|
||
'pay_money' => $v['pay_money'],
|
||
'create_time' => time(),
|
||
'is_enable_refund' => 0,
|
||
'order_name' => $v["order_name"],
|
||
'goods_num' => $v['goods_num'],
|
||
'delivery_type' => $delivery_type,
|
||
'delivery_type_name' => $delivery_type_name,
|
||
"website_id" => $v["website_id"],
|
||
'buyer_shop_id' => $calculate_data['buyer_shop_id'],//购买人所在店铺id
|
||
'buyer_shop_name' => $calculate_data['buyer_shop_name'],//购买人所在店铺
|
||
'buyer_uid' => $data['buyer_uid'],
|
||
'name' => $calculate_data['member_address']['name'] ?? '',
|
||
'mobile' => $calculate_data['member_address']['mobile'] ?? '',
|
||
'telephone' => $calculate_data['member_address']['telephone'] ?? '',
|
||
'province_id' => $calculate_data['member_address']['province_id'] ?? '',
|
||
'city_id' => $calculate_data['member_address']['city_id'] ?? '',
|
||
'district_id' => $calculate_data['member_address']['district_id'] ?? '',
|
||
'community_id' => $calculate_data['member_address']['community_id'] ?? '',
|
||
'address' => $calculate_data['member_address']['address'] ?? '',
|
||
'full_address' => $calculate_data['member_address']['full_address'] ?? '',
|
||
'longitude' => $calculate_data['member_address']['longitude'] ?? '',
|
||
'latitude' => $calculate_data['member_address']['latitude'] ?? '',
|
||
'buyer_ip' => request()->ip(),
|
||
'buyer_ask_delivery_time' => $v['buyer_ask_delivery_time'] ?? '',//定时达
|
||
"buyer_message" => $v["buyer_message"],
|
||
'site_id' => $v['site_id'],
|
||
'site_name' => $v['site_name'],
|
||
];
|
||
$order_id = model("supply_order")->add($data_order);
|
||
$order_id_arr[] = $order_id;
|
||
$pay_money += $v['pay_money'];
|
||
//订单项目表
|
||
foreach ($v['goods_list'] as $k_order_goods => $order_goods) {
|
||
$data_order_goods = array(
|
||
'order_id' => $order_id,
|
||
'order_no' => $order_no,
|
||
'sku_id' => $order_goods['sku_id'],
|
||
'sku_name' => $order_goods['sku_name'],
|
||
'sku_image' => $order_goods['sku_image'],
|
||
'sku_no' => $order_goods['sku_no'],
|
||
'is_virtual' => $order_goods['is_virtual'],
|
||
'goods_class' => $order_goods['goods_class'],
|
||
'goods_class_name' => $order_goods['goods_class_name'],
|
||
'price' => $order_goods['price'],
|
||
'cost_price' => $order_goods['cost_price'],
|
||
'num' => $order_goods['num'],
|
||
'goods_money' => $order_goods['goods_money'],
|
||
'cost_money' => $order_goods['cost_price'] * $order_goods['num'],
|
||
'commission_rate' => $order_goods['commission_rate'],
|
||
'goods_id' => $order_goods['goods_id'],
|
||
'delivery_status' => 0,
|
||
'delivery_status_name' => "未发货",
|
||
'real_goods_money' => $order_goods['real_goods_money'] ?? 0,
|
||
'buyer_shop_id' => $calculate_data['buyer_shop_id'],
|
||
'buyer_shop_name' => $calculate_data['buyer_shop_name'],
|
||
'buyer_uid' => $calculate_data['buyer_uid'],
|
||
'site_id' => $v['site_id'],
|
||
'site_name' => $v['site_name'],
|
||
);
|
||
model("supply_order_goods")->add($data_order_goods);
|
||
//库存变化
|
||
$stock_result = $goods_stock_model->decStock(["sku_id" => $order_goods['sku_id'], "num" => $order_goods['num']]);
|
||
if ($stock_result["code"] != 0) {
|
||
model("supply_order")->rollback();
|
||
return $stock_result;
|
||
}
|
||
|
||
}
|
||
}
|
||
// //扣除余额(统一扣除)
|
||
// if($calculate_data["balance_money"] > 0){
|
||
// $balance_result = $this->useBalance($calculate_data);
|
||
// if($balance_result["code"] < 0){
|
||
// model("supply_order")->rollback();
|
||
// return $balance_result;
|
||
// }
|
||
// }
|
||
//循环执行订单完成事件
|
||
foreach ($order_id_arr as $k => $v) {
|
||
$result_list = event("SupplyOrderCreate", ['order_id' => $v]);
|
||
if (!empty($result_list)) {
|
||
foreach ($result_list as $k => $v) {
|
||
if (!empty($v) && $v["code"] < 0) {
|
||
model("supply_order")->rollback();
|
||
return $v;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
//生成整体付费支付单据
|
||
if ($this->is_exist_not_free) {
|
||
$order_name_title = implode(",", $order_name);
|
||
$pay->addPay(0, $out_trade_no, $this->pay_type, $order_name_title, $order_name_title, $this->pay_money, '', 'SupplyOrderPayNotify', '');
|
||
}
|
||
//免费订单支付单据
|
||
if ($this->is_exist_free) {
|
||
$free_order_name_title = implode(",", $free_order_name);
|
||
$pay->addPay(0, $out_trade_no, $this->pay_type, $free_order_name_title, $free_order_name_title, 0, '', 'SupplyOrderPayNotify', '');
|
||
}
|
||
$this->addOrderCronClose($order_id);//增加关闭订单自动事件
|
||
$cart_ids = isset($data['cart_ids']) ? $data['cart_ids'] : '';
|
||
if (!empty($cart_ids)) {
|
||
$cart = new CartModel();
|
||
$data_cart = [
|
||
'cart_id' => $cart_ids,
|
||
'uid' => $data['buyer_uid']
|
||
];
|
||
$cart->deleteCart($data_cart);
|
||
}
|
||
Cache::tag("supply_order_create_uid_" . $data['buyer_uid'])->clear();
|
||
model("supply_order")->commit();
|
||
return $this->success($out_trade_no);
|
||
} catch (\Exception $e) {
|
||
model("supply_order")->rollback();
|
||
return $this->error('', $e->getMessage() . $e->getFile() . $e->getLine());
|
||
}
|
||
}
|
||
|
||
/***
|
||
* API订单创建
|
||
* @param $orderData
|
||
* @param $addressParam
|
||
* @param $invoiceParam
|
||
* @return void
|
||
*/
|
||
public function SupplyCreateOrder($orderData, $addressParam, $invoiceParam = [])
|
||
{
|
||
$calculate_data = $this->calculate($orderData);
|
||
if (isset($calculate_data['code']) && $calculate_data['code'] < 0) return $calculate_data;
|
||
if ($this->error > 0) {
|
||
return $this->error(['error_code' => $this->error], $this->error_msg);
|
||
}
|
||
$pay = new Pay();
|
||
$out_trade_no = $pay->createOutTradeNo($orderData['buyer_shop_id']);
|
||
if ($this->is_exist_not_free) {
|
||
$order_name = [];
|
||
}
|
||
if ($this->is_exist_free) {
|
||
$free_order_name = [];
|
||
}
|
||
model("supply_order")->startTrans();
|
||
try {
|
||
$pay_money = 0;
|
||
$goods_stock_model = new GoodsStock();
|
||
$agent_ids = [];
|
||
$order_id_arr = [];
|
||
if (isset($orderData['sku_ids_num'])) {
|
||
$agent_ids = model('site')->getColumn([['site_id', 'in', array_column($calculate_data['shop_goods_list'], 'site_id')]], 'agent_id', 'site_id');
|
||
}
|
||
foreach ($calculate_data['shop_goods_list'] as $k => $v) {
|
||
$item_delivery = $v['delivery'] ?? [];
|
||
$delivery_type = $item_delivery['delivery_type'] ?? '';
|
||
$delivery_type_name = Express::express_type[$delivery_type]["title"] ?? '';
|
||
//判断交易流水号
|
||
if ($v["pay_money"] == 0) {
|
||
$temp_out_trade_no = $out_trade_no;
|
||
$free_order_name[] = $v["order_name"];
|
||
} else {
|
||
$temp_out_trade_no = $out_trade_no;
|
||
$order_name[] = $v["order_name"];
|
||
}
|
||
//订单主表
|
||
$order_type = $this->orderType($v, $calculate_data);
|
||
$order_no = $this->createOrderNo($v['site_id']);
|
||
$data_order = [
|
||
'order_no' => $order_no,
|
||
'custom_order_no' => $orderData['outOrderId'] ?? '',
|
||
'order_from' => $orderData['order_from'] ?? 'api',
|
||
'order_from_name' => $orderData['order_from_name'] ?? 'API',
|
||
'order_type' => $order_type['order_type_id'],
|
||
'order_type_name' => $order_type['order_type_name'],
|
||
'order_status_name' => $order_type['order_status']['name'],
|
||
'order_status_action' => json_encode($order_type['order_status'], JSON_UNESCAPED_UNICODE),
|
||
'out_trade_no' => $temp_out_trade_no,
|
||
'goods_money' => $v['goods_money'],
|
||
'delivery_money' => $v['delivery_money'],
|
||
'adjust_money' => $v['adjust_money'],
|
||
'invoice_money' => $v['invoice_money'],
|
||
'order_money' => $v['order_money'],
|
||
'balance_money' => $v['balance_money'],
|
||
'pay_money' => $v['pay_money'],
|
||
'create_time' => time(),
|
||
'is_enable_refund' => 0,
|
||
'order_name' => $v["order_name"],
|
||
'goods_num' => $v['goods_num'],
|
||
'delivery_type' => $delivery_type,
|
||
'delivery_type_name' => $delivery_type_name,
|
||
"website_id" => $v["website_id"],
|
||
"shop_order_id" => $orderData["shop_order_id"]??0,
|
||
'buyer_shop_id' => $calculate_data['buyer_shop_id'],//购买人所在店铺id
|
||
'buyer_shop_name' => $calculate_data['buyer_shop_name'],//购买人所在店铺
|
||
'buyer_uid' => $orderData['buyer_uid'],
|
||
'name' => $calculate_data['member_address']['name'] ?? '',
|
||
'mobile' => $calculate_data['member_address']['mobile'] ?? '',
|
||
'telephone' => $calculate_data['member_address']['telephone'] ?? '',
|
||
'province_id' => $calculate_data['member_address']['province_id'] ?? '',
|
||
'city_id' => $calculate_data['member_address']['city_id'] ?? '',
|
||
'district_id' => $calculate_data['member_address']['district_id'] ?? '',
|
||
'community_id' => $calculate_data['member_address']['community_id'] ?? '',
|
||
'address' => $calculate_data['member_address']['address'] ?? '',
|
||
'full_address' => $calculate_data['member_address']['full_address'] ?? '',
|
||
'longitude' => $calculate_data['member_address']['longitude'] ?? '',
|
||
'latitude' => $calculate_data['member_address']['latitude'] ?? '',
|
||
'buyer_ip' => request()->ip(),
|
||
'buyer_ask_delivery_time' => $v['buyer_ask_delivery_time'] ?? '',//定时达
|
||
"buyer_message" => $v["buyer_message"],
|
||
"third_order" =>$order_no,
|
||
'site_id' => $v['site_id'],
|
||
'site_name' => $v['site_name'],
|
||
'status' => 1,
|
||
];
|
||
$order_id = model("supply_order")->add($data_order);
|
||
$order_id_arr[] = $order_id;
|
||
$pay_money += $v['pay_money'];
|
||
//订单项目表
|
||
$third_order_list = $v['third_order_list'];
|
||
$goods_list = array_column($v['goods_list'], null, 'sku_id');
|
||
if ($third_order_list) {
|
||
foreach ($third_order_list as $k_third_order_list => $third_order_list_item) {
|
||
switch ($k_third_order_list) {
|
||
case 1688:
|
||
$orderData['out_trade_no'] = $order_no;
|
||
$OrderInfo1688 = $this->getOrder1688Info($third_order_list_item, $orderData, $addressParam, $invoiceParam);
|
||
$buyOrderInfo = $this->CreateThirdOrder($agent_ids[$v['site_id']], '1688', $OrderInfo1688);
|
||
if (isset($buyOrderInfo['error_code'])) {
|
||
throw new \Exception($buyOrderInfo['error_message']);
|
||
}
|
||
$result = $buyOrderInfo['result'];
|
||
if (isset($result['failedOfferList'])) {
|
||
throw new \Exception($result['failedOfferList']['specId'] . $result['failedOfferList']['errorMessage']);
|
||
}
|
||
foreach ($third_order_list_item as $item) {
|
||
$goods_list[$item['sku_id']]['third_order_no'] = $result['orderId'];
|
||
$goods_list[$item['sku_id']]['channel_type'] = $k_third_order_list;
|
||
}
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
foreach ($goods_list as $k_order_goods => $order_goods) {
|
||
$data_order_goods = array(
|
||
'order_id' => $order_id,
|
||
"shop_order_id" => $orderData["shop_order_id"]??0,
|
||
'order_no' => $order_no,
|
||
'channel_type' => $order_goods['channel_type'],
|
||
'third_order_no' => $order_goods['third_order_no'] ?? '',
|
||
'sku_id' => $order_goods['sku_id'],
|
||
'sku_name' => $order_goods['sku_name'],
|
||
'sku_image' => $order_goods['sku_image'],
|
||
'sku_no' => $order_goods['sku_no'],
|
||
'is_virtual' => $order_goods['is_virtual'],
|
||
'goods_class' => $order_goods['goods_class'],
|
||
'goods_class_name' => $order_goods['goods_class_name'],
|
||
'price' => $order_goods['price'],
|
||
'cost_price' => $order_goods['cost_price'],
|
||
'num' => $order_goods['num'],
|
||
'goods_money' => $order_goods['goods_money'],
|
||
'cost_money' => $order_goods['cost_price'] * $order_goods['num'],
|
||
'commission_rate' => $order_goods['commission_rate'],
|
||
'goods_id' => $order_goods['goods_id'],
|
||
'delivery_status' => 0,
|
||
'delivery_status_name' => "未发货",
|
||
'real_goods_money' => $order_goods['real_goods_money'] ?? 0,
|
||
'buyer_shop_id' => $calculate_data['buyer_shop_id'],
|
||
'buyer_shop_name' => $calculate_data['buyer_shop_name'],
|
||
'buyer_uid' => $calculate_data['buyer_uid'],
|
||
'site_id' => $v['site_id'],
|
||
'site_name' => $v['site_name'],
|
||
);
|
||
model("supply_order_goods")->add($data_order_goods);
|
||
//库存变化
|
||
$stock_result = $goods_stock_model->decStock(["sku_id" => $order_goods['sku_id'], "num" => $order_goods['num']]);
|
||
if ($stock_result["code"] != 0) {
|
||
model("supply_order")->rollback();
|
||
return $stock_result;
|
||
}
|
||
}
|
||
}
|
||
//扣除余额(统一扣除)
|
||
if ($calculate_data["balance_money"] > 0) {
|
||
$calculate_data['out_trade_no'] = $out_trade_no;
|
||
$balance_result = $this->useBalance($calculate_data, $orderData['buyer_shop_id']);
|
||
if ($balance_result["code"] < 0) {
|
||
model("supply_order")->rollback();
|
||
return $balance_result;
|
||
} else {
|
||
$this->isPay = 1;
|
||
}
|
||
}
|
||
$this->order_ids = $order_id_arr;
|
||
//循环执行订单完成事件
|
||
foreach ($order_id_arr as $k => $v) {
|
||
$result_list = event("SupplyOrderCreate", ['order_id' => $v]);
|
||
if (!empty($result_list)) {
|
||
foreach ($result_list as $k => $v) {
|
||
if (!empty($v) && $v["code"] < 0) {
|
||
model("supply_order")->rollback();
|
||
return $v;
|
||
}
|
||
}
|
||
}
|
||
$this->addOrderCronClose($v);//增加关闭订单自动事件
|
||
}
|
||
//生成整体付费支付单据
|
||
if ($this->is_exist_not_free) {
|
||
$order_name_title = implode(",", $order_name);
|
||
$pay->addPay(0, $out_trade_no, $this->pay_type, $order_name_title, $order_name_title, $this->pay_money, '', 'SupplyOrderPayNotify', '');
|
||
}
|
||
//免费订单支付单据
|
||
if ($this->is_exist_free) {
|
||
$free_order_name_title = implode(",", $free_order_name);
|
||
$pay->addPay(0, $out_trade_no, $this->pay_type, $free_order_name_title, $free_order_name_title, 0, '', 'SupplyOrderPayNotify', '');
|
||
}
|
||
$cart_ids = isset($orderData['cart_ids']) ? $orderData['cart_ids'] : '';
|
||
if (!empty($cart_ids)) {
|
||
$cart = new CartModel();
|
||
$data_cart = [
|
||
'cart_id' => $cart_ids,
|
||
'uid' => $orderData['buyer_uid']
|
||
];
|
||
$cart->deleteCart($data_cart);
|
||
}
|
||
Cache::tag("supply_order_create_uid_" . $orderData['buyer_uid'])->clear();
|
||
model("supply_order")->commit();
|
||
$data = [
|
||
'QrPayUrl' => '',
|
||
'onlinePayUrl' => addon_url('supply://shop/pay/pay', ['out_trade_no' => $out_trade_no]),
|
||
'isPay' => $this->isPay,
|
||
'orderMoney' => $pay_money,
|
||
'outTradeNo' => $out_trade_no,
|
||
];
|
||
return $this->success($data);
|
||
} catch (\Exception $e) {
|
||
model("supply_order")->rollback();
|
||
return $this->error('', $e->getMessage());
|
||
}
|
||
}
|
||
|
||
public function previewOrder($data)
|
||
{
|
||
|
||
|
||
}
|
||
|
||
/***
|
||
* 创建平台商家下单
|
||
* @param $data
|
||
* @return void
|
||
*/
|
||
public function orderPay($order)
|
||
{
|
||
$orderId = (int)$order['order_id'] ?? 0;
|
||
$site_id = (int)$order['site_id'] ?? 0;
|
||
if ($orderId <= 0 || $site_id <= 0) return $this->error('订单或者店铺不存在!');
|
||
$orderGoodsList = $this->getOrderSupplyGoods($site_id, $orderId);
|
||
if (count($orderGoodsList) > 0) {
|
||
$info = model('supply_order')->getValue([['buyer_shop_id', '=', $site_id], ['custom_order_no', '=', $order['order_no']]], 'order_id');
|
||
if ($info) {
|
||
// return error(-1, '订单已存在');//防止重复下单
|
||
}
|
||
$skuIds = array_column($orderGoodsList, 'sku_id');
|
||
$skuList = model('goods_sku')->getColumn([
|
||
['sku_id', 'in', $skuIds]
|
||
], ['sku_id', 'goods_id', 'sku_no', 'cost_price', 'goods_supplier_format'], 'sku_id');
|
||
$sku_ids_num = [];//规格对应的数量信息
|
||
$sub_order_info = [];//
|
||
$channel_type_arr = [];//渠道云仓数据
|
||
$cloudParamList = [];
|
||
foreach ($orderGoodsList as $k => $v) {
|
||
$skuinfo = $skuList[$v['sku_id']];
|
||
$goods_supplier_format = json_decode($skuinfo['goods_supplier_format'], true);
|
||
$sub_order_info[$v['channel_type'] . '_' . $v['cloud_id']] = [
|
||
'site_id' => $site_id,
|
||
'shop_order_id' => $orderId,
|
||
'result_exception_reason' => '',// 请求结果:订单异常原因 1:恢复正常 2:需要换货 3:需要补发 4:地址错误 5:疫情停发
|
||
'result_express_company' => '',// 请求结果:快递公司
|
||
'result_express_number' => '',// 请求结果:快递单号
|
||
'result_express_status' => 0,// 请求结果:订单物流状态 1 待发货 2 已发货 3 已签收 4 拒收 5 已退款 6 申请退款 7 同意申请 8 拒绝申请
|
||
'result_express_time' => '',// 发货时间
|
||
'channel_type' => $v['channel_type'],// 渠道
|
||
'result_id' => $goods_supplier_format['spec_id'],
|
||
'goods_id' => $skuinfo['goods_id'] ?? '',
|
||
'sku_id' => $v['sku_id'],
|
||
'result_sub_id' => '',// 请求结果:子订单id
|
||
'result_price' => $v['cost_price'] ?? 0,// 请求结果:下单时的商品结算价格
|
||
'result_product_specs_id' => $goods_supplier_format['spec_id'] ?? '',// 请求结果:商品规格编号
|
||
'result_quantity' => $v['num'],// 请求结果:购买单品数量
|
||
'result_seller_remarks' => $order['buyer_message'] ?? '',// 请求结果:卖家备注
|
||
];
|
||
if ($v['channel_type'] != '' && $v['channel_type'] != 'own') { // 平台自营供应链
|
||
$v['spec_id'] = $goods_supplier_format['spec_id'];
|
||
$v['quantity'] = $v['num'];
|
||
$channel_type_arr[$v['channel_type']][] = $v;
|
||
$cloudParamList[$v['channel_type']][$v['cloud_id']][] = [
|
||
'sku_id' => $goods_supplier_format['spec_id'],
|
||
'quantity' => (int)$v['num'],
|
||
'outMemberId' => $order['member_id'],
|
||
'cloud_id' => $v['cloud_id'],
|
||
];
|
||
} else {
|
||
$sku_ids_num[$goods_supplier_format['spec_id']] = $v['num'];
|
||
}
|
||
}
|
||
$AppInfo = model('supply_app')->getInfo(['site_id' => $site_id]);
|
||
$invoiceParam = [];
|
||
$res = [];
|
||
if (count($channel_type_arr) > 0) { //三方云仓下单
|
||
$full_address = $order['full_address'] ?? '';
|
||
$full_address = explode('-', $full_address);
|
||
$addressParam = [
|
||
'fullName' => $order['name'] ?? '',
|
||
'mobile' => $order['mobile'] ?? '',
|
||
'phone' => $order['telephone'] ?? $order['mobile'],
|
||
'postCode' => 000000,
|
||
'provinceText' => $full_address[0] ?? '',
|
||
'cityText' => $full_address[1] ?? '',
|
||
'areaText' => $full_address[2] ?? '',
|
||
'townText' => '',
|
||
'address' => $order['address'] ?? '',
|
||
'districtCode' => $order['full_address'] ?? '',
|
||
];
|
||
foreach ($cloudParamList as $type => $vd) {
|
||
$cloud_order_arrs = $this->pushCreateOrder($type, $vd, $addressParam, $invoiceParam, $order);
|
||
foreach ($cloud_order_arrs as $k => $v1) {
|
||
$sub_order_info[$type . '_' . $v1['cloud_id']]['order_id'] = $v1['order_id'];
|
||
$sub_order_info[$type . '_' . $v1['cloud_id']]['result_sub_id'] = $v1['out_trade_no'];
|
||
}
|
||
}
|
||
}
|
||
if (count($sku_ids_num) > 0) {//自营平台下单
|
||
$orderData = array();
|
||
$orderData['buyer_shop_id'] = $site_id;
|
||
$orderData['buyer_shop_name'] = $order['site_name'];
|
||
$orderData['outOrderId'] = $order['order_no'];
|
||
$orderData['buyer_message'] = $order['buyer_message'];
|
||
$orderData['AuthPay'] = 1;
|
||
$orderData['is_balance'] = 1;
|
||
$orderData['order_from'] = 'shop';
|
||
$orderData['order_from_name'] = '平台商户';
|
||
$orderData['sku_ids_num'] = $sku_ids_num;
|
||
$orderData['appInfo'] = $AppInfo;
|
||
$orderData['buyer_uid'] = $AppInfo['id'];
|
||
$orderData['shop_order_id'] =$order['order_id'];
|
||
$addressParam = [
|
||
'name' => $order['name'] ?? '',
|
||
'mobile' => $order['mobile'] ?? '',
|
||
'telephone' => $order['telephone'] ?? '',
|
||
'province_id' => $order['province_id'] ?? '',
|
||
'city_id' => $order['city_id'] ?? '',
|
||
'district_id' => $order['district_id'] ?? '',
|
||
'community_id' => $order['community_id'] ?? '',
|
||
'address' => $order['address'] ?? '',
|
||
'full_address' => $order['full_address'] ?? '',
|
||
'longitude' => $order['longitude'] ?? '',
|
||
'latitude' => $order['latitude'] ?? '',
|
||
];
|
||
$orderData['member_address'] = $addressParam;
|
||
$res = $this->SupplyCreateOrder($orderData, $addressParam, $invoiceParam)['data'];
|
||
}
|
||
$res=model('supply_sub_order')->addList(array_values($sub_order_info));
|
||
return $this->success($res);
|
||
}
|
||
}
|
||
|
||
|
||
/***
|
||
* 三方云仓下单
|
||
* @param $type
|
||
* @param $data
|
||
* @param $addressParam
|
||
* @param $invoiceParam
|
||
* @param $order
|
||
* @return void
|
||
* @throws \Exception
|
||
*/
|
||
public function pushCreateOrder($type, $data, $addressParam, $invoiceParam = [], $order)
|
||
{
|
||
$orderData = [
|
||
'site_id' => $order['site_id'],
|
||
'shop_order_id' => $order['order_id'] ?? '',
|
||
'order_no' => $order['order_no'] ?? '',
|
||
'custom_order_no' => $order['order_no'] ?? '',
|
||
'order_from' => 'api',//订单来源
|
||
'order_from_name' => '云仓库',//来源名称
|
||
'channel_type' => $type,
|
||
'order_type' => 1,// 订单状态:-1=待请求下单,0=下单失败,1=下单成功
|
||
'status' => -1,// 订单状态:-1=待请求下单,0=下单失败,1=下单成功
|
||
'pay_status' => 0,// 是否扣款
|
||
'create_time' => time(),
|
||
'goods_money' => $order['goods_money'],
|
||
'delivery_money' => $order['delivery_money'],
|
||
'invoice_money' => $order['invoice_money'],
|
||
'order_money' => $order['order_money'],
|
||
'adjust_money' => $order['adjust_money'],
|
||
'balance_money' => $order['balance_money'],
|
||
'pay_money' => $order['pay_money'],
|
||
'buyer_shop_id' => $order['site_id'],
|
||
'order_name' => $order['order_name'],
|
||
'buyer_uid' => $order['member_id'],
|
||
'name' => $order['name'],
|
||
'mobile' => $order['mobile'] ?: $order['telephone'],
|
||
'province_id' => $order['province_id'],
|
||
'city_id' => $order['city_id'],
|
||
'district_id' => $order['district_id'],
|
||
'address' => $order['address'],
|
||
'buyer_message' => $order['buyer_message'],
|
||
'full_address' => $order['full_address'],
|
||
'goods_num' => $order['goods_num'],
|
||
'pay_time' => $order['pay_time']
|
||
];
|
||
$arr = [];
|
||
switch ($type) {
|
||
case 'third':
|
||
foreach ($data as $cloud_id => $v) {
|
||
$cloudModel = new CloudApiModel($cloud_id);
|
||
$pushData['outOrderId'] = $order['order_no'];
|
||
$pushData['AuthPay'] = 1;
|
||
$pushData['buyerMessage'] = $order['buyer_message'];
|
||
$pushData['addressParam'] = json_encode($addressParam, JSON_UNESCAPED_UNICODE);
|
||
$pushData['cargoParamList'] = json_encode($v, JSON_UNESCAPED_UNICODE);
|
||
$orderData['order_from'] = 'api';
|
||
$orderData['order_from_name'] = '云仓库|' . $cloud_id;
|
||
$res = $cloudModel->createOrderData($pushData)['data'];
|
||
$out_trade_no = '';
|
||
if ($res['code'] == 0) {
|
||
$resdata = $res['data'];
|
||
$orderData['status'] = 1;
|
||
$orderData['pay_status'] = $resdata['isPay'];
|
||
$orderData['three_pay_status'] = $resdata['isPay'];
|
||
$orderData['out_trade_no'] = $resdata['outTradeNo'];
|
||
$orderData['third_order'] = $resdata['outTradeNo'];
|
||
$orderData['cost_price'] = $resdata['orderMoney'];
|
||
$out_trade_no = $resdata['outTradeNo'];
|
||
} else {
|
||
$orderData['reason_failure'] = $res['message'];
|
||
}
|
||
$res = model('supply_order')->add($orderData);
|
||
$arr[] = [
|
||
'type' => $type,
|
||
'cloud_id' => $cloud_id,
|
||
'order_id' => $res,
|
||
'out_trade_no' => $out_trade_no
|
||
];
|
||
}
|
||
break;
|
||
case 'own3'://其他第三方
|
||
|
||
}
|
||
return $arr;
|
||
}
|
||
|
||
/***
|
||
* 获取供应链信息
|
||
* @param $orderId
|
||
* @param $field
|
||
* @return mixed
|
||
*/
|
||
public function getOrderSupplyGoods($site_id, $orderId, $field = 'og.order_goods_id,og.sku_id,og.num,og.goods_id,og.cost_price,o.buyer_message,fg.channel_type,fg.channel_type,fg.cloud_id')
|
||
{
|
||
$where = [
|
||
['og.order_id', '=', $orderId],
|
||
['fg.goods_id', '>', 0],
|
||
['fg.site_id', '=', $site_id],
|
||
];
|
||
$join = [
|
||
[
|
||
'order o',
|
||
'o.order_id=og.order_id',
|
||
'inner'
|
||
],
|
||
[
|
||
'supply_goods_warehousing fg',
|
||
'og.goods_id = fg.goods_id',
|
||
'right'
|
||
]
|
||
];
|
||
$goods_order_sku = model('order_goods')->getList($where, $field, '', 'og', $join);
|
||
// 获取订单商品信息
|
||
return $goods_order_sku;
|
||
}
|
||
|
||
|
||
/***
|
||
* 使用余额和代扣
|
||
* @param $data
|
||
* @param $site_id
|
||
* @param $from_type
|
||
* @return array
|
||
*/
|
||
public function useBalance($data, $site_id = 0, $from_type = 'buy_order')
|
||
{
|
||
$this->pay_type = 'BALANCE';
|
||
$member_account_model = new SupplyAccount();
|
||
$surplus_banance = $data['balance_money'];
|
||
$result = $member_account_model->AddAppsAccount($site_id, $data['buyer_uid'], 'usable_price', -$surplus_banance, $from_type, $data['out_trade_no'], $data['out_trade_no'] . '订单消费扣除');
|
||
return $result;
|
||
}
|
||
|
||
/**
|
||
* 订单计算
|
||
* @param $data
|
||
* @return unknown|mixed
|
||
*/
|
||
public function calculate($data)
|
||
{
|
||
$data = $this->initAddress($data);//初始化地址
|
||
$data = $this->initAccount($data);//初始化会员账户
|
||
//商品列表信息
|
||
$shop_goods_list = $this->getOrderGoodsCalculate($data);
|
||
if (empty($shop_goods_list)) {
|
||
$data['shop_goods_list'] = [];
|
||
$this->error = 1;
|
||
$this->error_msg = '找不到商品!';
|
||
} else {
|
||
foreach ($shop_goods_list as $k => $v) {
|
||
$data['shop_goods_list'][$k] = $this->shopOrderCalculate($v, $data);
|
||
}
|
||
}
|
||
//总优惠使用
|
||
$data = $this->eachShopOrder($data, true, $this);
|
||
//总结计算
|
||
$data['delivery_money'] = $this->delivery_money;
|
||
$data['adjust_money'] = $this->adjust_money;
|
||
$data['invoice_money'] = $this->invoice_money;
|
||
$data['order_money'] = $this->order_money;
|
||
$data['balance_money'] = $this->balance_money;
|
||
$data['pay_money'] = $this->pay_money;
|
||
$data['goods_money'] = $this->goods_money;
|
||
$data['goods_num'] = $this->goods_num;
|
||
$data['is_virtual'] = $this->is_virtual;
|
||
return $data;
|
||
}
|
||
|
||
/**
|
||
* 待付款订单
|
||
* @param $data
|
||
* @return unknown|mixed
|
||
*/
|
||
public function orderPayment($data)
|
||
{
|
||
$calculate_data = $this->calculate($data);
|
||
foreach ($calculate_data['shop_goods_list'] as $k => $v) {
|
||
$calculate_data['shop_goods_list'][$k] = $this->itemPayment($v, $calculate_data);
|
||
}
|
||
return $this->success($calculate_data);
|
||
}
|
||
|
||
|
||
/**
|
||
* 初始化组件 各订单
|
||
* @param $shop_item
|
||
* @param $data
|
||
* @param null $self
|
||
* @return mixed
|
||
*/
|
||
public function itemPayment($shop_item, $data)
|
||
{
|
||
|
||
$express_type = [];
|
||
if ($this->is_virtual == 0) {
|
||
if (!empty($data['member_address'])) {
|
||
//2. 查询店铺配送方式(1. 物流 2. 自提 3. 外卖)
|
||
if ($shop_item["express_config"]["is_use"] == 1) {
|
||
$express_type[] = ["title" => Express::express_type["express"]["title"], "name" => "express"];
|
||
}
|
||
// //查询店铺是否开启门店自提
|
||
// if ($shop_item["store_config"]["is_use"] == 1) {
|
||
// //根据坐标查询门店
|
||
// $store_model = new Store();
|
||
// $store_condition = array(
|
||
// ['site_id', '=', $shop_item['site_id']],
|
||
// ['is_pickup', '=', 1],
|
||
// ['status', '=', 1],
|
||
// ['is_frozen', '=', 0],
|
||
// );
|
||
//
|
||
// $latlng = array(
|
||
// 'lat' => $data['latitude'],
|
||
// 'lng' => $data['longitude'],
|
||
// );
|
||
// $store_list_result = $store_model->getLocationStoreList($store_condition, '*', $latlng);
|
||
// $store_list = $store_list_result["data"];
|
||
//
|
||
// //如果用户默认选中了门店
|
||
// $store_id = 0;
|
||
// if(!empty($store_list)){
|
||
// $store_id = $store_list[0]['store_id'];
|
||
// }
|
||
// $express_type[] = ["title" => Express::express_type["store"]["title"], "name" => "store", "store_list" => $store_list, 'store_id' => $store_id];
|
||
// }
|
||
//
|
||
// //查询店铺是否开启外卖配送
|
||
// if($shop_item["local_config"]["is_use"] == 1){
|
||
// //查询本店的通讯地址
|
||
// $express_type[] = ["title" => "外卖配送", "name" => "local"];
|
||
// }
|
||
}
|
||
}
|
||
$shop_item["express_type"] = $express_type;
|
||
return $shop_item;
|
||
}
|
||
|
||
/**
|
||
* 获取商品的计算信息
|
||
* @param unknown $data
|
||
*/
|
||
public function getOrderGoodsCalculate($data)
|
||
{
|
||
//传输购物车id组合','隔开要进行拆单
|
||
$condition = [
|
||
['is_delete', '=', 0],
|
||
];
|
||
if (!empty($data['cart_ids'])) {
|
||
$cart_condition = array(
|
||
['cart_id', 'in', $data['cart_ids']],
|
||
['shop_id', '=', $data['buyer_shop_id']]
|
||
);
|
||
$cart_list = model('supply_goods_cart')->getList($cart_condition, 'sku_id, num');
|
||
$condition[] = ['sku_id', 'in', array_column($cart_list, 'sku_id')];
|
||
$num_array = array_column($cart_list, 'num', 'sku_id');
|
||
} else if (!empty($data['sku_ids_num'])) {
|
||
$num_array = $data['sku_ids_num'];
|
||
$condition[] = ['sku_id', 'in', array_keys($data['sku_ids_num'])];
|
||
} else {
|
||
$condition[] = ['sku_id', '=', $data['sku_id']];
|
||
$num_array = [$data['sku_id'] => $data['num']];
|
||
}
|
||
$shop_goods = $this->getGoodsSkuList($condition, $num_array);
|
||
$shop_goods_list = $shop_goods;
|
||
return $shop_goods_list;
|
||
}
|
||
|
||
/**
|
||
* 获取购物车商品列表信息
|
||
* @param unknown $cart_ids
|
||
*/
|
||
public function getGoodsSkuList($condition, $num_array)
|
||
{
|
||
//组装商品列表
|
||
$field = 'price_json, sku_id,sku_name, sku_no, price, cost_price, stock, weight, volume, sku_image, site_id, site_name, website_id, is_own, goods_state, verify_state, is_virtual, is_free_shipping, shipping_template, goods_class, goods_class_name, goods_id,extend_sku_id,channel_type,goods_supplier_format';
|
||
$goods_list = model("supply_goods_sku")->getList($condition, $field);
|
||
$shop_goods_list = [];
|
||
if (!empty($goods_list)) {
|
||
foreach ($goods_list as $k => $v) {
|
||
$goods_info = model('supply_goods')->getInfo([['goods_id', '=', $v['goods_id']]], 'commission_rate,goods_image');
|
||
$v['commission_rate'] = $goods_info['commission_rate'] ?? 0;
|
||
$v['goods_image'] = $goods_info['goods_image'] ?? 0;
|
||
//用于过滤商品
|
||
if ($v['goods_state'] != 1 || $v['verify_state'] != 1) {
|
||
$this->error = 1;
|
||
$this->error_msg = '存在商品未上架或未通过审核';
|
||
}
|
||
$site_id = $v['site_id'];
|
||
$item_num = $num_array[$v['sku_id']];
|
||
$price_array = empty($v['price_json']) ? [] : json_decode($v['price_json'], true);
|
||
array_multisort(array_column($price_array, 'num'), SORT_ASC, $price_array);
|
||
$price = 0;
|
||
//按照阶梯计算商品的价格
|
||
$is_in = false;
|
||
foreach ($price_array as $price_k => $price_v) {
|
||
if ($item_num >= $price_v['num']) {
|
||
$price = $price_v['price'];
|
||
$is_in = true;
|
||
}
|
||
}
|
||
if (!$is_in) {
|
||
$this->error = 5;
|
||
$this->error_msg = $v['sku_name'] . '没有配置阶梯价格';
|
||
}
|
||
$v['num'] = $item_num;
|
||
//如果数量小于0,就提示报错
|
||
if ($v['num'] <= 0) {
|
||
$this->error = 1;
|
||
$this->error_msg = '商品购买数量不能小于等于0';
|
||
}
|
||
$v['price'] = $price;
|
||
$v['goods_money'] = $price * $item_num;
|
||
$v['real_goods_money'] = $v['goods_money'];
|
||
$v['channel_type'] = $v['channel_type'];
|
||
$v['extend_sku_id'] = $v['extend_sku_id'];
|
||
$v['goods_supplier_format'] = json_decode($v['goods_supplier_format'], true);
|
||
if (isset($shop_goods_list[$site_id])) {
|
||
$shop_goods_list[$site_id]['goods_list'][] = $v;
|
||
$shop_goods_list[$site_id]['order_name'] = string_split($shop_goods_list[$site_id]['order_name'], ",", $v['sku_name']);
|
||
$shop_goods_list[$site_id]['goods_num'] += $v['num'];
|
||
$shop_goods_list[$site_id]['goods_money'] += $v['price'] * $v['num'];
|
||
$shop_goods_list[$site_id]['goods_list_str'] = $shop_goods_list[$site_id]['goods_list_str'] . ';' . $v['sku_id'] . ':' . $v['num'];
|
||
} else {
|
||
$shop_goods_list[$site_id] = [];
|
||
$shop_goods_list[$site_id]['third_order_list'] = [];
|
||
$shop_goods_list[$site_id]['site_id'] = $site_id;
|
||
$shop_goods_list[$site_id]['site_name'] = $v['site_name'];
|
||
$shop_goods_list[$site_id]['website_id'] = $v['website_id'];
|
||
$shop_goods_list[$site_id]['goods_money'] = $v['price'] * $v['num'];
|
||
$shop_goods_list[$site_id]['goods_list_str'] = $v['sku_id'] . ':' . $v['num'];
|
||
$shop_goods_list[$site_id]['order_name'] = string_split("", ",", $v['sku_name']);
|
||
$shop_goods_list[$site_id]['goods_num'] = $v['num'];
|
||
$shop_goods_list[$site_id]['goods_list'][] = $v;
|
||
}
|
||
$goods_supplier_format = $v['goods_supplier_format'];
|
||
if ($goods_supplier_format) {
|
||
$orderDatas = [
|
||
'site_id' => $site_id,
|
||
'sku_id' => $v['sku_id'],
|
||
'extend_sku_id' => $v['extend_sku_id'],
|
||
'offerId' => $goods_supplier_format['offerId'],
|
||
'quantity' => $item_num,
|
||
'cloud_id' => $goods_supplier_format['cloud_id'] ?? $v['channel_type'],
|
||
];
|
||
$shop_goods_list[$site_id]['third_order_list'][$v['channel_type']][] = $orderDatas;
|
||
}
|
||
}
|
||
}
|
||
return $shop_goods_list;
|
||
}
|
||
|
||
/**
|
||
* 获取店铺订单计算
|
||
* @param unknown $site_id 店铺id
|
||
* @param unknown $site_name 店铺名称
|
||
* @param unknown $goods_money 商品总价
|
||
* @param unknown $goods_list 店铺商品列表
|
||
* @param unknown $data 传输生成订单数据
|
||
*/
|
||
public function shopOrderCalculate($shop_goods, $data)
|
||
{
|
||
$site_id = $shop_goods['site_id'];
|
||
//定义计算金额
|
||
$adjust_money = 0; //调整金额
|
||
$invoice_money = 0; //发票金额
|
||
//运费计算
|
||
$shop_goods = $this->delivery($shop_goods, $data);
|
||
//是否符合免邮
|
||
$is_free_delivery = $shop_goods['is_free_delivery'] ?? false;
|
||
if ($is_free_delivery) {
|
||
$shop_goods['delivery_money'] = 0;
|
||
}
|
||
$shop_goods['order_money'] = $shop_goods['goods_money'] + $shop_goods['delivery_money'] + $invoice_money;
|
||
//买家留言
|
||
if (isset($data['buyer_message']) && isset($data['buyer_message'][$site_id])) {
|
||
$item_buyer_message = $data['buyer_message'][$site_id];
|
||
$shop_goods["buyer_message"] = $item_buyer_message;
|
||
} else {
|
||
$shop_goods["buyer_message"] = '';
|
||
}
|
||
//总结计算
|
||
$shop_goods['adjust_money'] = $adjust_money;
|
||
$shop_goods['invoice_money'] = $invoice_money;
|
||
return $shop_goods;
|
||
}
|
||
|
||
|
||
/******************************************************************* 订单(不具体区分订单形式)公有操作 ******************************************************************/
|
||
|
||
/**
|
||
* 生成订单编号
|
||
*
|
||
* @param array $site_id
|
||
*/
|
||
public function createOrderNo($site_id)
|
||
{
|
||
$time_str = date('YmdHi');
|
||
$max_no = Cache::get($site_id . "_" . $time_str);
|
||
if (!isset($max_no) || empty($max_no)) {
|
||
$max_no = 1;
|
||
} else {
|
||
$max_no = $max_no + 1;
|
||
}
|
||
$order_no = $time_str . sprintf("%04d", $max_no);
|
||
Cache::set($site_id . "_" . $time_str, $max_no);
|
||
return $order_no;
|
||
}
|
||
|
||
|
||
/**
|
||
* 遍历各店铺订单 计算余额
|
||
* @param $data
|
||
* @return mixed
|
||
*/
|
||
public function eachShopOrder($data, $is_self = true, $self = null)
|
||
{
|
||
$shop_goods_list = $data['shop_goods_list'];
|
||
if ($is_self) {
|
||
$self = $this;
|
||
foreach ($shop_goods_list as $k => $v) {
|
||
$shop_goods_list[$k] = $this->eachShopOrderItem($v, $self, $is_self);
|
||
}
|
||
} else {
|
||
$shop_goods_list = $this->eachShopOrderItem($shop_goods_list, $self, $is_self);
|
||
}
|
||
$data['shop_goods_list'] = $shop_goods_list;
|
||
return $data;
|
||
}
|
||
|
||
/**
|
||
* 循环计算各个订单
|
||
* @param $item
|
||
* @param $self
|
||
* @param $is_self
|
||
* @return mixed
|
||
*/
|
||
public function eachShopOrderItem($item, $self, $is_self)
|
||
{
|
||
//余额抵扣(判断是否使用余额)
|
||
if ($self->account_balance_money > 0) {
|
||
if ($item['order_money'] <= $self->account_balance_money) {
|
||
$balance_money = $item['order_money'];
|
||
} else {
|
||
$balance_money = $self->account_balance_money;
|
||
}
|
||
} else {
|
||
$balance_money = 0;
|
||
}
|
||
$pay_money = $item['order_money'] - $balance_money;//计算出实际支付金额
|
||
//判断是否存在支付金额为0的订单
|
||
if ($is_self) {
|
||
if ($pay_money > 0) {
|
||
$self->is_exist_not_free = true;
|
||
} else {
|
||
$self->is_exist_free = true;
|
||
}
|
||
}
|
||
$self->account_balance_money -= $balance_money;//预减少账户余额
|
||
$item['pay_money'] = $pay_money;
|
||
$item['balance_money'] = $balance_money;
|
||
$self->pay_money += $pay_money;
|
||
$self->balance_money += $balance_money;//累计余额
|
||
$self->goods_money += $item['goods_money'];
|
||
$self->delivery_money += $item['delivery_money'];
|
||
$self->adjust_money += $item['adjust_money'];
|
||
$self->invoice_money += $item['invoice_money'];
|
||
$self->order_money += $item['order_money'];
|
||
$self->goods_num += $item["goods_num"];
|
||
$self->order_name = string_split($this->order_name, ",", $item["order_name"]);
|
||
return $item;
|
||
}
|
||
|
||
|
||
/**
|
||
* 初始化收货地址(用户)
|
||
* @param unknown $data
|
||
*/
|
||
public function initAddress($data)
|
||
{
|
||
//收货人地址管理
|
||
if (empty($data['member_address'])) {
|
||
//todo 店铺或个人用户的收货地址管理
|
||
$site_address_model = new SiteAddress();
|
||
$condition = array(
|
||
['site_id', '=', $data['buyer_shop_id']],
|
||
['is_default', '=', 1],
|
||
);
|
||
$address_info_result = $site_address_model->getAddressInfo($condition);
|
||
$data['member_address'] = $address_info_result['data'] ?? [];
|
||
}
|
||
return $data;
|
||
}
|
||
|
||
|
||
/**
|
||
* 初始化账户
|
||
* @param $data
|
||
* @return mixed
|
||
*/
|
||
public function initAccount($data)
|
||
{
|
||
//todo 店铺或个人用户的账户信息(余额或.....)
|
||
//余额付款
|
||
if ($data['is_balance']) {
|
||
if (isset($data["appInfo"])) {
|
||
$this->account_balance_money = Db::name('supply_app')->where(['id' => $data["appInfo"]['id']])->value('usable_price');
|
||
} else {
|
||
$this->account_balance_money = $data["account"]["balance_total"] ?? 0;
|
||
}
|
||
}
|
||
return $data;
|
||
}
|
||
|
||
/**
|
||
* 运费计算
|
||
* @param $shop_goods
|
||
* @param $data
|
||
* @param null $self
|
||
* @return mixed
|
||
*/
|
||
public function delivery($shop_goods, $data, $self = null)
|
||
{
|
||
$self = $self ?? $this;
|
||
$site_id = $shop_goods['site_id'];
|
||
//计算邮费
|
||
if ($self->is_virtual == 1) {
|
||
//虚拟订单 运费为0
|
||
$delivery_money = 0;
|
||
$shop_goods['delivery']['delivery_type'] = '';
|
||
} else {
|
||
//查询店铺是否开启快递配送
|
||
$express_config_model = new ExpressConfig();
|
||
$express_config_result = $express_config_model->getExpressConfig($site_id);
|
||
$express_config = $express_config_result["data"];
|
||
$shop_goods["express_config"] = $express_config;
|
||
$delivery_array = $data['delivery'][$site_id] ?? [];
|
||
$delivery_type = $delivery_array["delivery_type"] ?? 'express';
|
||
if (empty($data['member_address'])) {
|
||
$delivery_money = 0;
|
||
$shop_goods['delivery']['delivery_type'] = 'express';
|
||
$self->error = 1;
|
||
$self->error_msg = "未配置默认收货地址!";
|
||
} else {
|
||
if ($delivery_type == "express") {
|
||
if ($shop_goods["express_config"]["is_use"] == 1) {
|
||
//物流配送
|
||
$express = new Express();
|
||
//todo 做出兼容
|
||
$express_fee_result = $express->calculate($shop_goods, $data);
|
||
if ($express_fee_result["code"] < 0) {
|
||
$self->error = 1;
|
||
$self->error_msg = $express_fee_result["message"];
|
||
$delivery_fee = 0;
|
||
} else {
|
||
$delivery_fee = $express_fee_result['data']['delivery_fee'];
|
||
}
|
||
} else {
|
||
$self->error = 1;
|
||
$self->error_msg = "物流配送方式未开启!";
|
||
$delivery_fee = 0;
|
||
}
|
||
$delivery_money = $delivery_fee;
|
||
$shop_goods['delivery']['delivery_type'] = 'express';
|
||
}
|
||
}
|
||
}
|
||
$shop_goods['delivery_money'] = $delivery_money;
|
||
return $shop_goods;
|
||
}
|
||
|
||
|
||
/**
|
||
* 增加订单自动关闭事件
|
||
* @param $order_id
|
||
*/
|
||
public function addOrderCronClose($order_id)
|
||
{
|
||
//计算订单自动关闭时间
|
||
$config_model = new OrderConfig();
|
||
$order_config_result = $config_model->getOrderTradeConfig();
|
||
$order_config = $order_config_result["data"];
|
||
$now_time = time();
|
||
if (!empty($order_config)) {
|
||
$execute_time = $now_time + $order_config["value"]["auto_close"] * 60;//自动关闭时间
|
||
} else {
|
||
$execute_time = $now_time + 86400 * 3;//尚未配置 默认一天
|
||
}
|
||
$cron_model = new Cron();
|
||
$cron_model->addCron(1, 0, "供应商订单自动关闭", "CronSupplyOrderClose", $execute_time, $order_id);
|
||
}
|
||
|
||
/**
|
||
* 订单类型判断
|
||
* @param unknown $shop_goods
|
||
*/
|
||
public function orderType($shop_goods, $data)
|
||
{
|
||
if ($data["is_virtual"] == 1) {
|
||
// $order = new VirtualOrder();
|
||
// return [
|
||
// 'order_type_id' => 4,
|
||
// 'order_type_name' => '虚拟订单',
|
||
// 'order_status' => $order->order_status[0]
|
||
// ];
|
||
} else {
|
||
if ($shop_goods['delivery']['delivery_type'] == 'express') {
|
||
$order = new Order();
|
||
return [
|
||
'order_type_id' => 1,
|
||
'order_type_name' => '普通订单',
|
||
'order_status' => $order->order_status[0]
|
||
];
|
||
}
|
||
}
|
||
}
|
||
|
||
/***
|
||
* 获取创建1688订单信息
|
||
* @param $data
|
||
* @param $flow
|
||
* @return array
|
||
*/
|
||
public function getOrder1688Info($skuData, $orderData, $addressParam, $invoiceParam = [], $flow = 'boutiquepifa')
|
||
{
|
||
// 获取收货地址信息
|
||
$orderInfo = [
|
||
'flow' => $flow,
|
||
'outOrderId' => $orderData['out_trade_no'],
|
||
'message' => $orderData['buyer_message'],
|
||
'addressParam' => [
|
||
'fullName' => $addressParam['fullName'],//收货人姓名
|
||
'mobile' => $addressParam['mobile'], //手机
|
||
'phone' => $addressParam['phone'] ?: $addressParam['mobile'], //电话
|
||
'provinceText' => $addressParam['provinceText'] ?? '',//省份文本
|
||
'cityText' => $addressParam['cityText'] ?? '',//市文本
|
||
'areaText' => $addressParam['areaText'] ?? '', //区文本
|
||
'townText' => $addressParam['townText'] ?? '', //镇文本
|
||
'address' => $addressParam['address'], //街道地址
|
||
'postCode' => 000000,//邮编
|
||
],
|
||
'cargoParamList' => array_map(function ($skuItem) {
|
||
$data = [
|
||
'offerId' => $skuItem['offerId'] ?? '',
|
||
'specId' => $skuItem['extend_sku_id'],
|
||
'quantity' => $skuItem['quantity'],
|
||
];
|
||
if (isset($offer['openOfferId'])) {
|
||
$data['openOfferId'] = $offer['openOfferId'];
|
||
}
|
||
return $data;
|
||
}, $skuData),
|
||
];
|
||
if ($invoiceParam) {
|
||
$invoiceData = [
|
||
'invoiceType' => $invoiceParam['invoiceType'] ?? 0,
|
||
'provinceText' => $invoiceParam['provinceText'] ?? '',
|
||
'cityText' => $invoiceParam['cityText'] ?? '',
|
||
'areaText' => $invoiceParam['areaText'] ?? '',
|
||
'townText' => $invoiceParam['townText'] ?? '',
|
||
'postCode' => $invoiceParam['postCode'] ?? 000000,
|
||
'address' => $invoiceParam['address'] ?? '',
|
||
'fullName' => $invoiceParam['fullName'] ?? '',
|
||
'mobile' => $invoiceParam['mobile'] ?? '',
|
||
'phone' => $invoiceParam['phone'] ?? $invoiceParam['mobile'],
|
||
'companyName' => $invoiceParam['companyName'] ?? '',
|
||
'taxpayerIdentifier' => $invoiceParam['taxpayerIdentifier'] ?? '',
|
||
'bankAndAccount' => $invoiceParam['bankAndAccount'] ?? '',
|
||
'localInvoiceId' => $invoiceParam['localInvoiceId'] ?? '',
|
||
];
|
||
$orderInfo['invoiceParam'] = json_encode($invoiceData);
|
||
}
|
||
$orderInfo['addressParam'] = json_encode($orderInfo['addressParam']);
|
||
$orderInfo['cargoParamList'] = json_encode($orderInfo['cargoParamList']);
|
||
return $orderInfo;
|
||
}
|
||
|
||
|
||
/***
|
||
* 创建第三方订单
|
||
* @param $site_id
|
||
* @param $type
|
||
* @param $data
|
||
* @return mixed
|
||
* @throws \Exception
|
||
*/
|
||
public function CreateThirdOrder($site_id, $type, $data)
|
||
{
|
||
switch ($type) {
|
||
case '1688':
|
||
$cloudApi = new CloudApi($site_id);
|
||
$info = $cloudApi->fastCreateOrder($data);
|
||
break;
|
||
default:
|
||
$info = [];
|
||
}
|
||
return $info;
|
||
}
|
||
}
|