736 lines
36 KiB
PHP
736 lines
36 KiB
PHP
<?php
|
||
/**
|
||
* ThinkShop商城系统 - 团队十年电商经验汇集巨献!
|
||
* =========================================================
|
||
* Copy right 2019-2029 成都云之牛科技有限公司, 保留所有权利。
|
||
* ----------------------------------------------
|
||
* 官方网址: https://www.cdcloudshop.com
|
||
* 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用。
|
||
* 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
|
||
* =========================================================
|
||
*/
|
||
|
||
namespace addon\bale\model;
|
||
|
||
use app\model\order\Config;
|
||
use app\model\order\OrderCreate;
|
||
use addon\coupon\model\Coupon;
|
||
use app\model\goods\GoodsStock;
|
||
use app\model\store\Store;
|
||
use think\facade\Cache;
|
||
use app\model\express\Express;
|
||
use app\model\system\Pay;
|
||
use app\model\express\Config as ExpressConfig;
|
||
use app\model\express\Local;
|
||
|
||
/**
|
||
* 订单创建
|
||
*
|
||
* @author Administrator
|
||
*
|
||
*/
|
||
class BaleOrderCreate extends OrderCreate
|
||
{
|
||
|
||
private $goods_money = 0;//商品金额
|
||
private $delivery_money = 0;//配送费用
|
||
private $coupon_money = 0;//优惠券金额
|
||
private $adjust_money = 0;//调整金额
|
||
private $invoice_money = 0;//发票费用
|
||
private $promotion_money = 0;//优惠金额
|
||
private $order_money = 0;//订单金额
|
||
private $pay_money = 0;//支付总价
|
||
private $is_virtual = 0; //是否是虚拟类订单
|
||
private $order_name = ''; //订单详情
|
||
private $goods_num = 0; //商品种数
|
||
private $balance_money = 0;//消费券
|
||
private $member_balance_money = 0;//会员账户余额(计算过程中会逐次减少)
|
||
private $pay_type = 'ONLINE_PAY';//支付方式
|
||
private $invoice_delivery_money = 0;
|
||
private $error = 0; //是否有错误
|
||
private $error_msg = ''; //错误描述
|
||
|
||
/**
|
||
* 订单创建
|
||
* @param array $data
|
||
*/
|
||
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);
|
||
}
|
||
|
||
if (!empty($calculate_data[ 'invoice_type' ])) {
|
||
if ($calculate_data[ 'invoice_type' ] == 1 && $calculate_data[ 'invoice_full_address' ] == "") {
|
||
//物流,同城
|
||
if ($calculate_data[ 'shop_goods_list' ][ 'delivery' ][ 'delivery_type' ] == "express" || $calculate_data[ 'shop_goods_list' ][ 'delivery' ][ 'delivery_type' ] == "local") {
|
||
$calculate_data[ 'invoice_full_address' ] = $calculate_data[ 'member_address' ][ 'full_address' ] . $calculate_data[ 'member_address' ][ 'address' ];
|
||
$calculate_data[ 'shop_goods_list' ][ 'invoice_full_address' ] = $calculate_data[ 'member_address' ][ 'full_address' ] . $calculate_data[ 'member_address' ][ 'address' ];
|
||
}
|
||
//门店
|
||
if ($calculate_data[ 'shop_goods_list' ][ 'delivery' ][ 'delivery_type' ] == "store") {
|
||
$delivery_store_info = json_decode($calculate_data[ 'shop_goods_list' ][ 'delivery_store_info' ], true);
|
||
$calculate_data[ 'invoice_full_address' ] = $delivery_store_info[ 'full_address' ];
|
||
$calculate_data[ 'shop_goods_list' ][ 'invoice_full_address' ] = $delivery_store_info[ 'full_address' ];
|
||
}
|
||
}
|
||
}
|
||
|
||
$pay = new Pay();
|
||
$out_trade_no = $pay->createOutTradeNo();
|
||
model("order")->startTrans();
|
||
//循环生成多个订单
|
||
try {
|
||
$pay_money = 0;
|
||
$goods_stock_model = new GoodsStock();
|
||
$shop_goods_list = $calculate_data[ 'shop_goods_list' ];
|
||
$item_delivery = $shop_goods_list[ 'delivery' ] ?? [];
|
||
$delivery_type = $item_delivery[ 'delivery_type' ] ?? '';
|
||
$site_id = $data[ 'site_id' ];
|
||
$express_type_list = ( new \app\model\express\Config() )->getExpressTypeList($site_id);
|
||
$delivery_type_name = $express_type_list[ $delivery_type ] ?? '';
|
||
|
||
//订单主表
|
||
$order_type = $this->orderType($shop_goods_list, $calculate_data);
|
||
$order_no = $this->createOrderNo($shop_goods_list[ 'site_id' ]);
|
||
$data_order = [
|
||
'order_no' => $order_no,
|
||
'site_id' => $shop_goods_list[ 'site_id' ],
|
||
'site_name' => $shop_goods_list[ 'site_name' ],
|
||
'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' => $out_trade_no,
|
||
'member_id' => $data[ 'member_id' ],
|
||
'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(),
|
||
'goods_money' => $shop_goods_list[ 'goods_money' ],
|
||
'delivery_money' => $shop_goods_list[ 'delivery_money' ],
|
||
'coupon_id' => isset($shop_goods_list[ 'coupon_id' ]) ? $shop_goods_list[ 'coupon_id' ] : 0,
|
||
'coupon_money' => $shop_goods_list[ 'coupon_money' ] ?? 0,
|
||
'adjust_money' => $shop_goods_list[ 'adjust_money' ],
|
||
'invoice_money' => $shop_goods_list[ 'invoice_money' ],
|
||
'promotion_money' => $shop_goods_list[ 'promotion_money' ],
|
||
'order_money' => $shop_goods_list[ 'order_money' ],
|
||
'balance_money' => $shop_goods_list[ 'balance_money' ],
|
||
'pay_money' => $shop_goods_list[ 'pay_money' ],
|
||
'create_time' => time(),
|
||
'is_enable_refund' => 0,
|
||
'order_name' => $shop_goods_list[ "order_name" ],
|
||
'goods_num' => $shop_goods_list[ 'goods_num' ],
|
||
'delivery_type' => $delivery_type,
|
||
'delivery_type_name' => $delivery_type_name,
|
||
'delivery_store_id' => $shop_goods_list[ "delivery_store_id" ] ?? 0,
|
||
"delivery_store_name" => $shop_goods_list[ "delivery_store_name" ] ?? '',
|
||
"delivery_store_info" => $shop_goods_list[ "delivery_store_info" ] ?? '',
|
||
"buyer_message" => $data[ "buyer_message" ],
|
||
"promotion_type" => "bale",
|
||
"promotion_type_name" => "打包一口价",
|
||
"promotion_status_name" => "",
|
||
|
||
"invoice_delivery_money" => $shop_goods_list[ "invoice_delivery_money" ] ?? 0,
|
||
"taxpayer_number" => $shop_goods_list[ "taxpayer_number" ] ?? '',
|
||
"invoice_rate" => $shop_goods_list[ "invoice_rate" ] ?? 0,
|
||
"invoice_content" => $shop_goods_list[ "invoice_content" ] ?? '',
|
||
"invoice_full_address" => $shop_goods_list[ "invoice_full_address" ] ?? '',
|
||
"is_invoice" => $shop_goods_list[ "is_invoice" ] ?? 0,
|
||
"invoice_type" => $shop_goods_list[ "invoice_type" ] ?? 0,
|
||
"invoice_title" => $shop_goods_list[ "invoice_title" ] ?? '',
|
||
'is_tax_invoice' => $shop_goods_list[ "is_tax_invoice" ] ?? '',
|
||
'invoice_email' => $shop_goods_list[ "invoice_email" ] ?? '',
|
||
'invoice_title_type' => $shop_goods_list[ "invoice_title_type" ] ?? 0,
|
||
|
||
'buyer_ask_delivery_time' => $shop_goods_list[ 'buyer_ask_delivery_time' ] ?? '',
|
||
'store_id' => $shop_goods_list[ 'store_id' ]
|
||
];
|
||
|
||
$order_id = model("order")->add($data_order);
|
||
$pay_money += $shop_goods_list[ 'pay_money' ];
|
||
//订单项目表
|
||
foreach ($shop_goods_list[ 'goods_list' ] as $k_order_goods => $order_goods) {
|
||
$data_order_goods = array (
|
||
'order_id' => $order_id,
|
||
'site_id' => $shop_goods_list[ 'site_id' ],
|
||
'order_no' => $order_no,
|
||
'member_id' => $data[ 'member_id' ],
|
||
'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' ],
|
||
'goods_id' => $order_goods[ 'goods_id' ],
|
||
'delivery_status' => 0,
|
||
'delivery_status_name' => "未发货",
|
||
"real_goods_money" => $order_goods[ "real_goods_money" ],
|
||
'coupon_money' => $order_goods[ "coupon_money" ] ?? 0,
|
||
'promotion_money' => $order_goods[ "promotion_money" ],
|
||
|
||
'goods_name' => $order_goods[ 'goods_name' ],
|
||
'sku_spec_format' => $order_goods[ 'sku_spec_format' ],
|
||
'store_id' => $shop_goods_list[ 'store_id' ]
|
||
);
|
||
$order_goods_id = model("order_goods")->add($data_order_goods);
|
||
$calculate_data[ 'shop_goods_list' ][ 'goods_list' ][ $k_order_goods ][ 'order_goods_id' ] = $order_goods_id;
|
||
//库存变化
|
||
$stock_result = $this->skuDecStock($order_goods, $shop_goods_list[ 'store_id' ]);
|
||
if ($stock_result[ "code" ] != 0) {
|
||
model("order")->rollback();
|
||
return $stock_result;
|
||
}
|
||
}
|
||
|
||
//优惠券
|
||
if ($data_order[ 'coupon_id' ] > 0 && $data_order[ 'coupon_money' ]) {
|
||
//优惠券处理方案
|
||
$coupon_id = $shop_goods_list[ 'coupon_id' ];
|
||
$member_coupon_model = new Coupon();
|
||
$coupon_use_result = $member_coupon_model->useCoupon($coupon_id, $data[ 'member_id' ], $order_id);//使用优惠券
|
||
if ($coupon_use_result[ 'code' ] != 0) {
|
||
model("order")->rollback();
|
||
return $this->error('', "COUPON_ERROR");
|
||
}
|
||
}
|
||
|
||
$result_list = event("OrderCreate", [ 'order_id' => $order_id, 'site_id' => $shop_goods_list[ 'site_id' ], 'create_data' => $calculate_data ]);
|
||
if (!empty($result_list)) {
|
||
foreach ($result_list as $k => $v) {
|
||
if (!empty($v) && $v[ "code" ] < 0) {
|
||
model("order")->rollback();
|
||
return $v;
|
||
}
|
||
}
|
||
}
|
||
|
||
//扣除余额(统一扣除)
|
||
if ($calculate_data[ "balance_money" ] > 0) {
|
||
$this->pay_type = "BALANCE";
|
||
$calculate_data[ 'order_id' ] = $order_id;
|
||
$balance_result = $this->useBalance($calculate_data, $data[ 'site_id' ]);
|
||
if ($balance_result[ "code" ] < 0) {
|
||
model("order")->rollback();
|
||
return $balance_result;
|
||
}
|
||
}
|
||
|
||
//生成整体支付单据
|
||
$pay->addPay($shop_goods_list[ 'site_id' ], $out_trade_no, $this->pay_type, $this->order_name, $this->order_name, $this->pay_money, '', 'OrderPayNotify', '');
|
||
$this->addOrderCronClose($order_id, $shop_goods_list[ 'site_id' ]);//增加关闭订单自动事件
|
||
Cache::tag("order_create_bale_" . $data[ 'member_id' ])->clear();
|
||
model("order")->commit();
|
||
return $this->success($out_trade_no);
|
||
|
||
} catch (\Exception $e) {
|
||
model("order")->rollback();
|
||
return $this->error('', $e->getMessage() . $e->getFile() . $e->getLine());
|
||
}
|
||
|
||
}
|
||
|
||
/**
|
||
* 订单计算
|
||
* @param \app\model\order\unknown $data
|
||
* @return \app\model\order\unknown|array|mixed
|
||
*/
|
||
public function calculate($data)
|
||
{
|
||
$data = $this->initMemberAddress($data);
|
||
$data = $this->initMemberAccount($data);//初始化会员账户
|
||
//余额付款
|
||
if ($data[ 'is_balance' ] > 0) {
|
||
$this->member_balance_money = $data[ "member_account" ][ "balance_total" ] ?? 0;
|
||
}
|
||
|
||
//打包一口价id 查询订单商品数据
|
||
$bale_model = new Bale();
|
||
$bale_info_result = $bale_model->getBaleInfo([ [ "bale_id", "=", $data[ "bale_id" ] ], [ 'site_id', '=', $data[ 'site_id' ] ] ]);
|
||
$bale_info = $bale_info_result[ "data" ];
|
||
$data[ "bale_info" ] = $bale_info;//打包一口价信息
|
||
|
||
//商品数据
|
||
$sku_list_json = json_decode($data[ 'sku_list_json' ], true);
|
||
$data[ 'sku_ids' ] = array_column($sku_list_json, 'sku_id');
|
||
|
||
$sku_ids = explode(',', substr($bale_info[ 'sku_ids' ], 1, -1));
|
||
//判断商品是否在该活动中
|
||
$goods_num = 0;
|
||
$sku_list = [];
|
||
foreach ($sku_list_json as $v) {
|
||
if (in_array($v[ 'sku_id' ], $sku_ids)) {
|
||
$goods_num += $v[ 'num' ];
|
||
$sku_list[ $v[ 'sku_id' ] ] = $v[ 'num' ];
|
||
} else {
|
||
return $this->error('', '商品信息有误');
|
||
}
|
||
}
|
||
$data[ 'sku_list' ] = $sku_list;
|
||
|
||
//判断商品数量是否正确
|
||
if ($goods_num % $bale_info[ 'num' ] != 0) {
|
||
return $this->error('', '商品数量有误');
|
||
}
|
||
$data[ 'goods_num' ] = $goods_num;
|
||
$data[ 'goods_money' ] = $bale_info[ 'price' ] * ( $goods_num / $bale_info[ 'num' ] );
|
||
|
||
//商品列表信息
|
||
$shop_goods_list = $this->getOrderGoodsCalculate($data);
|
||
|
||
$data[ 'shop_goods_list' ] = $this->shopOrderCalculate($shop_goods_list, $data);
|
||
|
||
//总结计算
|
||
$data[ 'delivery_money' ] = $this->delivery_money;
|
||
$data[ 'coupon_money' ] = $this->coupon_money;
|
||
$data[ 'adjust_money' ] = $this->adjust_money;
|
||
$data[ 'invoice_money' ] = $this->invoice_money;
|
||
$data[ 'invoice_delivery_money' ] = $this->invoice_delivery_money;
|
||
$data[ 'promotion_money' ] = $this->promotion_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[ 'is_virtual' ] = $this->is_virtual;
|
||
return $data;
|
||
}
|
||
|
||
/**
|
||
* 待付款订单
|
||
* @param \app\model\order\unknown $data
|
||
* @return \app\model\order\unknown|array|mixed
|
||
*/
|
||
public function orderPayment($data)
|
||
{
|
||
$calculate_data = $this->calculate($data);
|
||
$shop_goods_list = $calculate_data[ 'shop_goods_list' ];
|
||
//1、查询会员优惠券
|
||
$coupon_list = $this->getOrderCouponList($calculate_data);
|
||
$calculate_data[ 'shop_goods_list' ][ "coupon_list" ] = $coupon_list;
|
||
$express_type = [];
|
||
if ($this->is_virtual == 0) {
|
||
|
||
//2. 查询店铺配送方式(1. 物流 2. 自提 3. 外卖)
|
||
if ($calculate_data[ 'shop_goods_list' ][ "express_config" ][ "is_use" ] == 1) {
|
||
$title = $calculate_data[ 'shop_goods_list' ][ "express_config" ][ 'value' ][ 'express_name' ];
|
||
if ($title == "") {
|
||
$title = Express::express_type[ "express" ][ "title" ];
|
||
}
|
||
$express_type[] = [ "title" => $title, "name" => "express" ];
|
||
}
|
||
//查询店铺是否开启门店自提
|
||
if ($calculate_data[ 'shop_goods_list' ][ "store_config" ][ "is_use" ] == 1) {
|
||
//根据坐标查询门店
|
||
$store_model = new Store();
|
||
$store_condition = array (
|
||
[ 'site_id', '=', $data[ '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" ];
|
||
|
||
$title = $calculate_data[ 'shop_goods_list' ][ "store_config" ][ 'value' ][ 'store_name' ];
|
||
if ($title == "") {
|
||
$title = Express::express_type[ "store" ][ "title" ];
|
||
}
|
||
$express_type[] = [ "title" => $title, "name" => "store", "store_list" => $store_list ];
|
||
}
|
||
//查询店铺是否开启外卖配送
|
||
if ($calculate_data[ 'shop_goods_list' ][ "local_config" ][ "is_use" ] == 1) {
|
||
//查询本店的通讯地址
|
||
$title = $calculate_data[ 'shop_goods_list' ][ "local_config" ][ 'value' ][ 'local_name' ];
|
||
if ($title == "") {
|
||
$title = '外卖配送';
|
||
}
|
||
$store_model = new Store();
|
||
$store_condition = array (
|
||
[ 'site_id', '=', $data[ 'site_id' ] ]
|
||
);
|
||
if (addon_is_exit('store', $data[ 'site_id' ])) {
|
||
$store_condition[] = [ 'is_o2o', '=', 1 ];
|
||
$store_condition[] = [ 'status', '=', 1 ];
|
||
$store_condition[] = [ 'is_frozen', '=', 0 ];
|
||
} else {
|
||
$store_condition[] = [ 'is_default', '=', 1 ];
|
||
}
|
||
$latlng = array (
|
||
'lat' => $data[ 'latitude' ],
|
||
'lng' => $data[ 'longitude' ],
|
||
);
|
||
$store_list_result = $store_model->getLocationStoreList($store_condition, '*', $latlng);
|
||
$store_list = $store_list_result[ 'data' ];
|
||
|
||
$express_type[] = [ "title" => $title, "name" => "local", 'store_list' => $store_list ];
|
||
}
|
||
}
|
||
|
||
$calculate_data[ 'shop_goods_list' ][ "express_type" ] = $express_type;
|
||
|
||
return $calculate_data;
|
||
|
||
}
|
||
|
||
/**
|
||
* 获取商品的计算信息
|
||
* @param unknown $data
|
||
*/
|
||
public function getOrderGoodsCalculate($data)
|
||
{
|
||
$shop_goods_list = [];
|
||
//传输打包一口价id组合','隔开要进行拆单
|
||
|
||
$goods_list = $this->getBaleGoodsList($data);
|
||
|
||
$goods_list[ 'promotion_money' ] = 0;
|
||
$shop_goods_list = $goods_list;
|
||
return $shop_goods_list;
|
||
}
|
||
|
||
/**
|
||
* 获取打包一口价商品列表信息
|
||
* @param unknown $bale_id
|
||
*/
|
||
public function getBaleGoodsList($data)
|
||
{
|
||
//组装商品列表
|
||
$field = ' ngs.sku_id, ngs.sku_name, ngs.sku_no,ngs.price, ngs.discount_price, ngs.cost_price, ngs.stock, ngs.weight, ngs.volume,
|
||
ngs.sku_image, ngs.site_id, ngs.goods_state, ngs.is_virtual, ngs.is_free_shipping, ngs.shipping_template, ngs.goods_class,
|
||
ngs.goods_class_name, ngs.goods_id, ngs.sku_spec_format,ngs.goods_name,ngs.support_trade_type,ns.site_name';
|
||
$alias = 'ngs';
|
||
$join = [
|
||
[
|
||
'site ns',
|
||
'ngs.site_id = ns.site_id',
|
||
'inner'
|
||
]
|
||
];
|
||
$goods_list = model("goods_sku")->getList([ [ 'ngs.sku_id', 'in', $data[ 'sku_ids' ] ] ], $field, '', $alias, $join);
|
||
$shop_goods_list = [];
|
||
if (!empty($goods_list)) {
|
||
$express_type_list = ( new \app\model\express\Config() )->getExpressTypeList($data[ 'site_id' ]);
|
||
foreach ($goods_list as $k => $v) {
|
||
$v[ "num" ] = $data[ 'sku_list' ][ $v[ 'sku_id' ] ];
|
||
$site_id = $v[ 'site_id' ];
|
||
$price = $v[ 'discount_price' ];
|
||
$v[ 'price' ] = $price;
|
||
$v[ 'goods_money' ] = $price * $v[ 'num' ];
|
||
$v[ 'real_goods_money' ] = $v[ 'goods_money' ];
|
||
$v[ 'coupon_money' ] = 0;//优惠券金额
|
||
$v[ 'promotion_money' ] = 0;//优惠金额
|
||
|
||
if (!empty($shop_goods_list)) {
|
||
$shop_goods_list[ 'goods_list' ][] = $v;
|
||
$shop_goods_list[ 'order_name' ] = string_split($shop_goods_list[ 'order_name' ], ",", $v[ 'sku_name' ]);
|
||
$shop_goods_list[ 'goods_money' ] += $v[ 'goods_money' ];
|
||
$shop_goods_list[ 'goods_list_str' ] = $shop_goods_list[ 'goods_list_str' ] . ';' . $v[ 'sku_id' ] . ':' . $v[ 'num' ];
|
||
$shop_goods_list[ 'goods_num' ] += $v[ 'num' ];
|
||
} else {
|
||
$shop_goods_list[ 'site_id' ] = $site_id;
|
||
$shop_goods_list[ 'site_name' ] = $v[ 'site_name' ];
|
||
$shop_goods_list[ 'goods_money' ] = $v[ 'goods_money' ];
|
||
$shop_goods_list[ 'goods_list_str' ] = $v[ 'sku_id' ] . ':' . $v[ 'num' ];
|
||
$shop_goods_list[ 'order_name' ] = string_split("", ",", $v[ 'sku_name' ]);
|
||
$shop_goods_list[ 'goods_num' ] = $v[ 'num' ];
|
||
$shop_goods_list[ 'goods_list' ][] = $v;
|
||
}
|
||
if (isset($data[ 'delivery' ][ 'delivery_type' ]) && !empty($data[ 'delivery' ][ 'delivery_type' ]) && strpos($v[ 'support_trade_type' ], $data[ 'delivery' ][ 'delivery_type' ]) === false) {
|
||
$delivery_type_name = $express_type_list[ $data[ 'delivery' ][ 'delivery_type' ] ] ?? '';
|
||
$this->error = 1;
|
||
$this->error_msg = '有商品不支持' . $delivery_type_name;
|
||
}
|
||
}
|
||
}
|
||
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' ];
|
||
|
||
//交易配置
|
||
$config_model = new Config();
|
||
$order_config_result = $config_model->getOrderEventTimeConfig($site_id);
|
||
$order_config = $order_config_result[ "data" ];
|
||
|
||
$shop_goods[ 'order_config' ] = $order_config[ 'value' ] ?? [];
|
||
|
||
//循环计算订单项商品价格(受打包一口价的影响)
|
||
$rate = $data[ 'goods_money' ] / $shop_goods[ 'goods_money' ];//计算打包一口价与原商品价格计算比率
|
||
$rate = substr(sprintf("%.5f", $rate), 0, -1);
|
||
|
||
$add_money = $data[ 'goods_money' ];
|
||
$count = count($shop_goods[ "goods_list" ]);
|
||
foreach ($shop_goods[ "goods_list" ] as $k => $v) {
|
||
if ($k == ( $count - 1 )) {
|
||
$temp_money = $add_money;
|
||
$temp_price = round($temp_money / $v[ "num" ], 3);
|
||
$temp_price = substr(sprintf("%.3f", $temp_price), 0, -1);
|
||
$temp_money = substr(sprintf("%.3f", $temp_money), 0, -1);
|
||
} else {
|
||
$temp_price = round($v[ 'discount_price' ] * $rate, 3);
|
||
$temp_money = round($v[ 'discount_price' ] * $v[ "num" ] * $rate, 3);
|
||
$temp_price = substr(sprintf("%.3f", $temp_price), 0, -1);
|
||
$temp_money = substr(sprintf("%.3f", $temp_money), 0, -1);
|
||
$add_money -= $temp_money;
|
||
}
|
||
$shop_goods[ "goods_list" ][ $k ][ 'price' ] = $temp_price;
|
||
$shop_goods[ "goods_list" ][ $k ][ 'goods_money' ] = $temp_money;
|
||
$shop_goods[ "goods_list" ][ $k ][ 'real_goods_money' ] = $temp_money;
|
||
}
|
||
$goods_money = $data[ 'goods_money' ];//直接使用打包一口价价格
|
||
$shop_goods[ 'goods_money' ] = $goods_money;
|
||
$shop_goods[ 'is_free_delivery' ] = $data[ "bale_info" ][ "shipping_fee_type" ] == 1 ? true : false;
|
||
//定义计算金额
|
||
$delivery_money = 0; //配送费用
|
||
$promotion_money = $shop_goods[ 'promotion_money' ]; //优惠费用(满减)
|
||
$coupon_money = 0; //优惠券费用
|
||
$adjust_money = 0; //调整金额
|
||
$invoice_money = 0; //发票金额
|
||
$order_money = 0; //订单金额
|
||
$balance_money = 0; //会员余额
|
||
$pay_money = 0; //应付金额
|
||
$store_id = 0;
|
||
|
||
//计算邮费
|
||
if ($this->is_virtual == 1) {
|
||
//虚拟订单 运费为0
|
||
$delivery_money = 0;
|
||
$shop_goods[ 'delivery' ][ 'delivery_type' ] = '';
|
||
} else {
|
||
$express_config_model = new ExpressConfig();
|
||
$deliver_type = $express_config_model->getDeliverTypeSort($site_id);
|
||
$deliver_type = $deliver_type[ "data" ];
|
||
$shop_goods[ "deliver_sort" ] = explode(',', $deliver_type[ 'value' ][ 'deliver_type' ]);
|
||
//查询店铺是否开启快递配送
|
||
$express_config_result = $express_config_model->getExpressConfig($site_id);
|
||
$express_config = $express_config_result[ "data" ];
|
||
$shop_goods[ "express_config" ] = $express_config;
|
||
//查询店铺是否开启门店自提
|
||
$store_config_result = $express_config_model->getStoreConfig($site_id);
|
||
$store_config = $store_config_result[ "data" ];
|
||
$shop_goods[ "store_config" ] = $store_config;
|
||
//查询店铺是否开启外卖配送
|
||
$local_config_result = $express_config_model->getLocalDeliveryConfig($site_id);
|
||
$local_config = $local_config_result[ "data" ];
|
||
$shop_goods[ "local_config" ] = $local_config;
|
||
|
||
//如果本地配送开启, 则查询出本地配送的配置
|
||
if ($shop_goods[ 'local_config' ][ 'is_use' ] == 1 && isset($data[ 'delivery' ][ 'store_id' ])) {
|
||
$local_model = new Local();
|
||
$local_info_result = $local_model->getLocalInfo([ [ 'site_id', '=', $site_id ], [ 'store_id', '=', $data[ 'delivery' ][ 'store_id' ] ] ]);
|
||
$local_info = $local_info_result[ 'data' ];
|
||
$shop_goods[ 'local_config' ][ 'info' ] = $local_info;
|
||
} else {
|
||
$shop_goods[ 'local_config' ][ 'info' ] = [];
|
||
}
|
||
$delivery_array = $data[ 'delivery' ] ?? [];
|
||
$delivery_type = $delivery_array[ "delivery_type" ] ?? 'express';
|
||
if ($delivery_type == "store") {
|
||
if (isset($data[ 'delivery' ][ "delivery_type" ]) && $data[ 'delivery' ][ "delivery_type" ] == "store") {
|
||
//门店自提
|
||
$delivery_money = 0;
|
||
$shop_goods[ 'delivery' ][ 'delivery_type' ] = 'store';
|
||
if ($shop_goods[ "store_config" ][ "is_use" ] == 0) {
|
||
$this->error = 1;
|
||
$this->error_msg = "门店自提方式未开启!";
|
||
}
|
||
if (empty($data[ 'delivery' ][ "store_id" ])) {
|
||
$this->error = 1;
|
||
$this->error_msg = "门店未选择!";
|
||
}
|
||
$shop_goods[ 'delivery' ][ 'store_id' ] = $data[ 'delivery' ][ "store_id" ];
|
||
$shop_goods[ 'buyer_ask_delivery_time' ] = $data[ 'buyer_ask_delivery_time' ];
|
||
$shop_goods = $this->storeOrderData($shop_goods, $data);
|
||
$store_id = $data[ 'delivery' ][ 'store_id' ] ?? 0;
|
||
}
|
||
} else {
|
||
if (empty($data[ 'member_address' ])) {
|
||
$delivery_money = 0;
|
||
$shop_goods[ 'delivery' ][ 'delivery_type' ] = 'express';
|
||
|
||
$this->error = 1;
|
||
$this->error_msg = "未配置默认收货地址!";
|
||
} else {
|
||
if (!isset($data[ 'delivery' ][ "delivery_type" ]) || $data[ 'delivery' ][ "delivery_type" ] == "express") {
|
||
if ($shop_goods[ "express_config" ][ "is_use" ] == 1) {
|
||
//物流配送
|
||
$express = new Express();
|
||
$express_fee_result = $express->calculate($shop_goods, $data);
|
||
if ($express_fee_result[ "code" ] < 0) {
|
||
$this->error = 1;
|
||
$this->error_msg = $express_fee_result[ "message" ];
|
||
$delivery_fee = 0;
|
||
} else {
|
||
$delivery_fee = $express_fee_result[ 'data' ][ 'delivery_fee' ];
|
||
}
|
||
} else {
|
||
$this->error = 1;
|
||
$this->error_msg = "物流配送方式未开启!";
|
||
$delivery_fee = 0;
|
||
}
|
||
$delivery_money = $delivery_fee;
|
||
$shop_goods[ 'delivery' ][ 'delivery_type' ] = 'express';
|
||
} else if ($data[ 'delivery' ][ "delivery_type" ] == "local") {
|
||
//外卖配送
|
||
$delivery_money = 0;
|
||
$shop_goods[ 'delivery' ][ 'delivery_type' ] = 'local';
|
||
if ($shop_goods[ "local_config" ][ "is_use" ] == 0) {
|
||
$this->error = 1;
|
||
$this->error_msg = "外卖配送方式未开启!";
|
||
} else {
|
||
if (empty($data[ 'delivery' ][ 'store_id' ])) {
|
||
$this->setError(1, '门店未选择!');
|
||
}
|
||
$local_delivery_time = 0;
|
||
if (!empty($data[ 'buyer_ask_delivery_time' ])) {
|
||
$buyer_ask_delivery_time_temp = explode(':', $data[ 'buyer_ask_delivery_time' ]);
|
||
$local_delivery_time = $buyer_ask_delivery_time_temp[ 0 ] * 3600 + $buyer_ask_delivery_time_temp[ 1 ] * 60;
|
||
}
|
||
$shop_goods[ 'buyer_ask_delivery_time' ] = $local_delivery_time;
|
||
|
||
$local_model = new Local();
|
||
$local_result = $local_model->calculate($shop_goods, $data);
|
||
|
||
$shop_goods[ 'delivery' ][ 'start_money' ] = 0;
|
||
if ($local_result[ 'code' ] < 0) {
|
||
$this->error = $local_result[ 'data' ][ 'code' ];
|
||
$this->error_msg = $local_result[ 'message' ];
|
||
$shop_goods[ 'delivery' ][ 'start_money' ] = $local_result[ 'data' ][ 'start_money_array' ][ 0 ] ?? 0;
|
||
} else {
|
||
$delivery_money = $local_result[ 'data' ][ 'delivery_money' ];
|
||
if (!empty($local_result[ 'data' ][ 'error_code' ])) {
|
||
$this->error = $local_result[ 'data' ][ 'code' ];
|
||
$this->error_msg = $local_result[ 'data' ][ 'error' ];
|
||
}
|
||
}
|
||
|
||
$shop_goods[ 'delivery' ][ 'error' ] = $this->error;
|
||
$shop_goods[ 'delivery' ][ 'error_msg' ] = $this->error_msg;
|
||
$store_id = $data[ 'delivery' ][ 'store_id' ] ?? 0;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
//是否符合免邮
|
||
$is_free_delivery = $shop_goods[ 'is_free_delivery' ] ?? false;
|
||
if ($data[ "bale_info" ][ "shipping_fee_type" ] != 2) {
|
||
$delivery_money = 0;
|
||
}
|
||
|
||
//发票相关
|
||
$shop_goods = $this->invoice($shop_goods, $data);
|
||
|
||
$order_money = $goods_money + $delivery_money - $promotion_money + $shop_goods[ 'invoice_money' ] + $shop_goods[ 'invoice_delivery_money' ];
|
||
$shop_goods[ 'order_money' ] = $order_money;
|
||
//优惠券活动(采用站点id:coupon_id)
|
||
//$shop_goods = $this->couponPromotion($shop_goods, $data);
|
||
$order_money = $shop_goods[ 'order_money' ];
|
||
|
||
if ($order_money < 0) {
|
||
$order_money = 0;
|
||
}
|
||
//余额抵扣(判断是否使用余额)
|
||
if ($this->member_balance_money > 0) {
|
||
if ($order_money <= $this->member_balance_money) {
|
||
$balance_money = $order_money;
|
||
} else {
|
||
$balance_money = $this->member_balance_money;
|
||
}
|
||
} else {
|
||
$balance_money = 0;
|
||
}
|
||
$pay_money = $order_money - $balance_money;//计算出实际支付金额
|
||
$this->member_balance_money -= $balance_money;//预减少账户余额
|
||
$this->balance_money += $balance_money;//累计余额
|
||
|
||
//总结计算
|
||
$shop_goods[ 'store_id' ] = $store_id;
|
||
$shop_goods[ 'goods_money' ] = $goods_money;
|
||
$shop_goods[ 'delivery_money' ] = $delivery_money;
|
||
$shop_goods[ 'coupon_money' ] = $coupon_money;
|
||
$shop_goods[ 'adjust_money' ] = $adjust_money;
|
||
// $shop_goods['invoice_money'] = $invoice_money;
|
||
$shop_goods[ 'promotion_money' ] = $promotion_money;
|
||
$shop_goods[ 'order_money' ] = $order_money;
|
||
$shop_goods[ 'balance_money' ] = $balance_money;
|
||
$shop_goods[ 'pay_money' ] = $pay_money;
|
||
|
||
$this->goods_money += $goods_money;
|
||
$this->delivery_money += $delivery_money;
|
||
$this->coupon_money += $coupon_money;
|
||
$this->adjust_money += $adjust_money;
|
||
$this->invoice_money += $shop_goods[ 'invoice_money' ];
|
||
$this->invoice_delivery_money += $shop_goods[ 'invoice_delivery_money' ];
|
||
$this->promotion_money += $promotion_money;
|
||
$this->order_money += $order_money;
|
||
$this->pay_money += $pay_money;
|
||
$this->order_name = string_split($this->order_name, ",", $shop_goods[ "order_name" ]);
|
||
return $shop_goods;
|
||
}
|
||
|
||
public function verifyArea($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);
|
||
}
|
||
if (!empty($calculate_data[ 'delivery' ][ 'delivery_type' ]) && $calculate_data[ 'delivery' ][ 'delivery_type' ] == 'store') {
|
||
if (!empty($calculate_data[ 'cart_ids' ])) {
|
||
//商品列表信息
|
||
$shop_goods_list = $this->getOrderGoodsCalculate($calculate_data);
|
||
} else {
|
||
//商品列表信息
|
||
$shop_goods_list = $this->getOrderGoodsCalculate($calculate_data);
|
||
}
|
||
$goods_lists = $shop_goods_list[ 'goods_list' ];
|
||
|
||
$res = event("GoodsSkuStock", [ 'goodslist' => $goods_lists, 'data' => $calculate_data ]);
|
||
if (!empty($res)) {
|
||
return $this->error([ 'error_code' => 11 ], '当前门店库存不足,请选择其他门店');
|
||
}
|
||
}
|
||
return $this->success(1);
|
||
}
|
||
} |