668 lines
31 KiB
PHP
668 lines
31 KiB
PHP
<?php
|
|
|
|
/**
|
|
* SaaSMall商城系统 - 团队十年电商经验汇集巨献!
|
|
* =========================================================
|
|
* Copy right 2019-2029 上海牛之云网络科技有限公司, 保留所有权利。
|
|
* ----------------------------------------------
|
|
* 官方网址: https://www.gobuysaas.com
|
|
* =========================================================
|
|
*/
|
|
|
|
namespace addon\store\model\cashier;
|
|
|
|
|
|
use addon\memberrecharge\model\RechargeOrder;
|
|
use addon\store\model\member\MemberOnceCard;
|
|
use addon\store\model\store\Store;
|
|
use app\model\BaseModel;
|
|
use app\model\member\Hongbao;
|
|
use app\model\member\Member;
|
|
use app\model\member\MemberAccount;
|
|
use app\model\member\MemberCoupon;
|
|
use app\model\member\MemberLevelOrder;
|
|
use app\model\member\Point;
|
|
use app\model\order\OrderCommon;
|
|
use app\model\order\OrderCreateTool;
|
|
use app\model\order\OrderGoods;
|
|
use app\model\site\Site;
|
|
use app\model\system\Pay;
|
|
use app\model\web\Config;
|
|
use think\facade\Cache;
|
|
use think\facade\Queue;
|
|
|
|
/**
|
|
* 收银订单计算
|
|
*
|
|
* @author Administrator
|
|
*
|
|
*/
|
|
class CashierOrderCalculate extends CashierOrderCommon
|
|
{
|
|
|
|
|
|
/**
|
|
* 计算
|
|
* @param $params
|
|
*/
|
|
public function calculate($params)
|
|
{
|
|
$site_id = $params['site_id'];
|
|
$out_trade_no = $params['out_trade_no'];
|
|
$member_id = $params['member_id'] ?? 0;
|
|
if ($member_id == 0) {
|
|
unset($params['member_id']);
|
|
}
|
|
$condition = array(
|
|
['out_trade_no', '=', $out_trade_no]
|
|
);
|
|
$cashier_order_info = model('store_cashier_order')->getInfo($condition);
|
|
if (empty($cashier_order_info))
|
|
return $this->error();
|
|
$is_calculate = isset($params['promotion']) ? true : false;
|
|
$promotion = $params['promotion'] ?? [];
|
|
$order_money = $cashier_order_info['order_money'];
|
|
$original_money = $order_money;
|
|
$cashier_order_info['original_money'] = $original_money;
|
|
$cashier_order_info = array_merge($cashier_order_info, $promotion, $params);
|
|
$order_id = $cashier_order_info['order_id'];
|
|
$order_goods_condition = array(
|
|
['order_id', '=', $order_id]
|
|
);
|
|
$cashier_order_goods_list = model('store_cashier_order_goods')->getList($order_goods_condition);
|
|
$order_goods_id_map = [];
|
|
foreach ($cashier_order_goods_list as $goods_k => $goods_info) {
|
|
$item_order_goods_id = $goods_info['order_goods_id'];
|
|
$order_goods_id_map[$goods_k] = $item_order_goods_id;
|
|
}
|
|
$cashier_order_info['order_goods_id_map'] = $order_goods_id_map;
|
|
$cashier_order_info['goods_list'] = $cashier_order_goods_list;
|
|
if ($member_id > 0) {
|
|
$member_account_model = new MemberAccount();
|
|
$member_account_condition = array(
|
|
['member_id', '=', $member_id],
|
|
['site_id', '=', $site_id]
|
|
);
|
|
$member_account_info = $member_account_model->getMemberAccount($member_account_condition)['data'] ?? [];
|
|
$cashier_order_info['member_account'] = $member_account_info;
|
|
}
|
|
$result = $this->promotionCalculate($cashier_order_info, $is_calculate);
|
|
if ($result['code'] < 0) {
|
|
return $result;
|
|
}
|
|
$result_data = $result['data'];
|
|
$result = $this->payCalculate($result_data);
|
|
if ($result['code'] < 0) {
|
|
return $result;
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* 支付计算
|
|
* @param $params
|
|
* @return array
|
|
*/
|
|
public function payCalculate($cashier_order_info)
|
|
{
|
|
$member_id = $cashier_order_info['member_id'] ?? 0;
|
|
$pay_money = $cashier_order_info['pay_money'];
|
|
$pay_type = $cashier_order_info['pay_type'] ?? '';
|
|
if ($pay_type == 'third') {
|
|
$pay_type = 'ONLINE_PAY';
|
|
}
|
|
$online_type = empty($cashier_order_info['online_type']) ? $pay_type : $cashier_order_info['online_type'];
|
|
$paid_money = 0;
|
|
$cash = $cashier_order_info['cash'] ?? 0;
|
|
$balance = $cashier_order_info['total_balance'];
|
|
$online_money = $cashier_order_info['online_money'];
|
|
switch ($online_type) {
|
|
case 'cash':
|
|
$cash = $cashier_order_info['cash'];
|
|
$paid_money += $cash;
|
|
break;
|
|
case 'online':
|
|
$online_money = $pay_money;
|
|
$paid_money += $online_money;
|
|
break;
|
|
case 'own_wechatpay':
|
|
$own_wechatpay = $pay_money;
|
|
$paid_money += $own_wechatpay;
|
|
break;
|
|
case 'own_alipay':
|
|
$own_alipay = $pay_money;
|
|
$paid_money += $own_alipay;
|
|
break;
|
|
case 'own_pos':
|
|
$own_pos = $pay_money;
|
|
$paid_money += $own_pos;
|
|
break;
|
|
}
|
|
$surplus_money = $pay_money - $paid_money;
|
|
if ($surplus_money < 0) {
|
|
$cash_change = abs($surplus_money);
|
|
}
|
|
$data = array(
|
|
'pay_money' => $pay_money,
|
|
'paid_money' => $paid_money,
|
|
'surplus_money' => $surplus_money,
|
|
'cash' => $cash,
|
|
'cash_change' => $cash_change ?? 0,
|
|
'total_balance' => $balance,//总余额
|
|
'online_money' => $online_money,
|
|
'online_type' => $online_type,
|
|
'own_wechatpay' => $own_wechatpay ?? 0,
|
|
'own_alipay' => $own_alipay ?? 0,
|
|
'own_pos' => $own_pos ?? 0,
|
|
'pay_type' => $pay_type,
|
|
);
|
|
if ($member_id > 0) {
|
|
$data['member_id'] = $member_id;
|
|
}
|
|
$data = array_merge($cashier_order_info, $data);
|
|
return $this->success($data);
|
|
}
|
|
|
|
|
|
/**
|
|
* 活动计算
|
|
* @param $cashier_order_info
|
|
*/
|
|
public function promotionCalculate($calculate, $is_calculate = true)
|
|
{
|
|
if($is_calculate){
|
|
$member_id = $calculate['member_id'] ?? 0;
|
|
$site_id = $calculate['site_id'];
|
|
if ($member_id > 0) {
|
|
$coupon_calculate = $this->couponCalculate($calculate);
|
|
if ($coupon_calculate['code'] < 0)
|
|
return $coupon_calculate;
|
|
$calculate = $coupon_calculate['data'];
|
|
}
|
|
$reduction_calculate = $this->reductionCalculate($calculate);
|
|
if ($reduction_calculate['code'] < 0)
|
|
return $reduction_calculate;
|
|
$calculate = $reduction_calculate['data'];
|
|
if ($member_id > 0) {
|
|
$hongbao_calculate = $this->hongbaoCalculate($calculate);
|
|
if ($hongbao_calculate['code'] < 0)
|
|
return $hongbao_calculate;
|
|
$calculate = $hongbao_calculate['data'];
|
|
$point_calculate = $this->pointCalculate($calculate);
|
|
if ($point_calculate['code'] < 0)
|
|
return $point_calculate;
|
|
$calculate = $point_calculate['data'];
|
|
$balance_calculate = $this->balanceCalculate($calculate);
|
|
if ($balance_calculate['code'] < 0)
|
|
return $balance_calculate;
|
|
$calculate = $balance_calculate['data'];
|
|
}
|
|
$offset = $calculate['offset'] ?? [];
|
|
$calculate['offset'] = $offset;
|
|
}
|
|
return $this->success($calculate);
|
|
}
|
|
|
|
/**
|
|
* 调价计算
|
|
* @param $cashier_order_info
|
|
*/
|
|
public function reductionCalculate($cashier_order_info)
|
|
{
|
|
$offset = $cashier_order_info['offset'] ?? [];
|
|
// $promotion = $cashier_order_info['promotion'] ?? [];
|
|
$reduction = $cashier_order_info['reduction'] ?? 0;//调整金额
|
|
$pay_money = $cashier_order_info['pay_money'];
|
|
if ($reduction > 0) {
|
|
$offset_money = $cashier_order_info['offset_money'] ?? 0;
|
|
if ($reduction > $pay_money) {
|
|
$reduction = $pay_money;
|
|
}
|
|
$pay_money -= $reduction;
|
|
$offset['reduction'] = $reduction;
|
|
$cashier_order_info['reduction'] = $reduction;
|
|
$offset_money += $reduction;
|
|
$calculate_data['offset_money'] = $offset_money;
|
|
}
|
|
// $cashier_order_info['order_money'] = $order_money;
|
|
$cashier_order_info['pay_money'] = $pay_money;
|
|
$cashier_order_info['offset'] = $offset;
|
|
return $this->success($cashier_order_info);
|
|
}
|
|
|
|
/**
|
|
* 余额计算
|
|
* @param $data
|
|
*/
|
|
public function balanceCalculate($cashier_order_info)
|
|
{
|
|
$offset = $cashier_order_info['offset'] ?? [];
|
|
$pay_money = $cashier_order_info['pay_money'];
|
|
$order_money = $cashier_order_info['order_money'];
|
|
// $promotion = $cashier_order_info['promotion'] ?? [];
|
|
$is_use_balance = $cashier_order_info['is_use_balance'] ?? 0;
|
|
$member_account = $cashier_order_info['member_account'] ?? [];
|
|
if (!empty($member_account)) {
|
|
$order_type = $cashier_order_info['order_type'];
|
|
if (in_array($order_type, ['goods', 'card'])) {
|
|
$member_balance_total = $member_account['balance_total'] ?? 0;
|
|
$offset_money = $cashier_order_info['offset_money'] ?? 0;
|
|
$balance_money = 0;
|
|
if ($member_balance_total > 0) {
|
|
if ($member_balance_total > $pay_money) {
|
|
$balance_money = $pay_money;
|
|
} else {
|
|
$balance_money = $member_balance_total;
|
|
}
|
|
}
|
|
$balance_array = array(
|
|
'balance' => $balance_money,
|
|
'balance_money' => $balance_money,
|
|
'balance_switch' => false
|
|
);
|
|
$total_balance = 0;
|
|
if ($pay_money > 0) {
|
|
if ($balance_money > 0) {
|
|
$balance_array['balance_switch'] = true;
|
|
if ($is_use_balance > 0) {
|
|
$total_balance = $balance_money;
|
|
$pay_money -= $balance_money;
|
|
}
|
|
} else {
|
|
$is_use_balance = 0;
|
|
}
|
|
} else {
|
|
$is_use_balance = 0;
|
|
}
|
|
$offset['balance'] = $balance_array;
|
|
$cashier_order_info['is_use_balance'] = $is_use_balance;
|
|
$cashier_order_info['order_money'] = $order_money;
|
|
$cashier_order_info['pay_money'] = $pay_money;
|
|
$cashier_order_info['offset'] = $offset;
|
|
$cashier_order_info['total_balance'] = $total_balance ?? 0;
|
|
$offset_money += $total_balance;
|
|
$cashier_order_info['offset_money'] = $offset_money;
|
|
}
|
|
}
|
|
return $this->success($cashier_order_info);
|
|
}
|
|
|
|
/**
|
|
* 积分计算
|
|
* @param $data
|
|
*/
|
|
public function pointCalculate($cashier_order_info)
|
|
{
|
|
$member_account = $cashier_order_info['member_account'] ?? [];
|
|
if (!empty($member_account)) {
|
|
$order_type = $cashier_order_info['order_type'];
|
|
if (in_array($order_type, ['goods', 'card'])) {
|
|
$site_id = $cashier_order_info['site_id'];
|
|
$offset = $cashier_order_info['offset'] ?? [];
|
|
$pay_money = $cashier_order_info['pay_money'];
|
|
$offset_money = $cashier_order_info['offset_money'] ?? 0;
|
|
$site_type = $cashier_order_info['site_type'] ?? '';
|
|
$point = 0;
|
|
$point_money = 0;
|
|
$use_point_money = 0;
|
|
$use_point = 0;
|
|
$is_use_point = $cashier_order_info['is_use_point'] ?? 0;//使用积分
|
|
//积分
|
|
$point_config_model = new Point();
|
|
$point_config = $point_config_model->getPointConfig($site_id, $site_type)['data'] ?? [];
|
|
$is_use = $point_config['is_use'] ?? 0;
|
|
if ($is_use > 0) {
|
|
$point_value = $point_config['value'] ?? [];
|
|
$point_exchange_rate = $point_value['point_exchange_rate'] ?? 0;//积分兑换比率 为0的话认为没有开启积分兑换
|
|
if ($point_exchange_rate > 0) {
|
|
$max_num = $point_value['point_max_num'];
|
|
$member_account_point = $member_account['point'];//会员积分
|
|
if ($member_account_point > 0) {//拥有积分大于0
|
|
$point_money = round($member_account_point / $point_exchange_rate, 2);//积分抵扣金额
|
|
if ($point_money > $pay_money) {
|
|
$point_money = $pay_money;
|
|
}
|
|
if ($max_num != 0 && $point_money > $max_num) {
|
|
$point_money = $max_num;
|
|
}
|
|
$point = ceil($point_money * $point_exchange_rate);
|
|
}
|
|
}
|
|
}
|
|
$point_array = array(
|
|
'point' => $point,
|
|
'point_money' => $point_money,
|
|
'point_switch' => false
|
|
);
|
|
if ($point > 0) {
|
|
$point_array['point_switch'] = true;
|
|
//存在可用积分且选用了积分抵扣
|
|
if ($is_use_point) {
|
|
$pay_money -= $point_money;
|
|
$use_point_money = $point_money;
|
|
$use_point = $point;
|
|
|
|
}
|
|
} else {
|
|
$is_use_point = 0;
|
|
}
|
|
$offset['point_array'] = $point_array;
|
|
$cashier_order_info['offset'] = $offset;
|
|
$cashier_order_info['is_use_point'] = $is_use_point;
|
|
$offset_money += $use_point_money;
|
|
$cashier_order_info['offset_money'] = $offset_money;
|
|
$cashier_order_info['point_money'] = $use_point_money;//积分抵扣多少金额
|
|
$cashier_order_info['point'] = $use_point;//使用多少个积分
|
|
$cashier_order_info['pay_money'] = $pay_money;
|
|
}
|
|
}
|
|
return $this->success($cashier_order_info);
|
|
}
|
|
|
|
|
|
/**
|
|
* 优惠券计算
|
|
* @param $calculate_data
|
|
*/
|
|
public function couponCalculate($calculate_data)
|
|
{
|
|
$member_id = $calculate_data['member_id'] ?? 0;
|
|
if ($member_id > 0) {
|
|
$order_type = $calculate_data['order_type'];
|
|
//只有开单和卡项可以用优惠券
|
|
if (in_array($order_type, ['goods', 'card'])) {
|
|
$pay_money = $calculate_data['pay_money'];
|
|
$order_money = $calculate_data['order_money'];
|
|
$discount_money = $calculate_data['discount_money'] ?? 0;
|
|
$site_id = $calculate_data['site_id'];
|
|
$goods_list = $calculate_data['goods_list'] ?? [];
|
|
$is_can_coupon = true;
|
|
foreach ($goods_list as $k => $v) {
|
|
$trade_type = $v['trade_type'];
|
|
if ($trade_type == 'money') {
|
|
$is_can_coupon = false;
|
|
}
|
|
}
|
|
$goods_ids = array_unique(array_column($goods_list, 'goods_id'));
|
|
$goods_money = $calculate_data['goods_money'];//优惠券现在取用的是商品价用作门槛
|
|
$real_goods_money = $calculate_data['real_goods_money'] ?? 0;
|
|
$offset = $calculate_data['offset'] ?? [];
|
|
if($order_money > 0 && $pay_money > 0){
|
|
//查询可用的优惠券
|
|
$member_coupon_model = new MemberCoupon();
|
|
$member_coupon_params = array(
|
|
'site_id' => $site_id,
|
|
'goods_id' => $goods_ids,//传入多个商品id,
|
|
'member_id' => $member_id,
|
|
);
|
|
$member_coupon_list = $member_coupon_model->getMemberCouponGoodsList($member_coupon_params)['data'] ?? [];
|
|
//查询全场可用的优惠券
|
|
$all_coupon_params = array(
|
|
'site_id' => $site_id,
|
|
'is_all' => 1,
|
|
'member_id' => $member_id,
|
|
);
|
|
$all_member_coupon_list = $member_coupon_model->getMemberCouponGoodsList($all_coupon_params)['data'] ?? [];
|
|
$member_coupon_list = array_merge($member_coupon_list, $all_member_coupon_list);
|
|
}
|
|
$default_coupon_id = 0;
|
|
$default_coupon_end_time = 0;
|
|
$default_coupon_type = '';
|
|
$default_coupon_discount = 0;
|
|
$default_coupon_offset_money = 0;
|
|
$default_coupon_money = 0;
|
|
$temp_member_coupon_list = [];
|
|
$temp_cache_coupon = [];//用以减轻I/O压力
|
|
if (!empty($member_coupon_list)) {
|
|
foreach ($member_coupon_list as $k => $v) {
|
|
$parent_id = $v['parent_id'];
|
|
$coupon_id = $v['coupon_id'];
|
|
$at_least = $v['at_least'];//最小条件
|
|
$item_type = $v['type'];//reward-满减 discount-折扣 random-随机
|
|
$is_all = $v['is_all'];
|
|
$temp_cache_coupon_item = $temp_cache_coupon[$parent_id] ?? [];
|
|
if (empty($temp_cache_coupon_item)) {
|
|
if ($is_all == 1) {
|
|
$intersect = $goods_ids;
|
|
} else {
|
|
//查询当前优惠券支持的商品
|
|
$item_condition = array(
|
|
['coupon_id', '=', $parent_id]
|
|
);
|
|
$coupon_goods_ids = $member_coupon_model->getCouponGoodsList($item_condition)['data'] ?? [];
|
|
$intersect = array_intersect($coupon_goods_ids, $goods_ids);
|
|
}
|
|
//计算这几个商品的商品总价
|
|
$goods_sum = 0;
|
|
$coupon_order_goods_ids = array();
|
|
foreach ($goods_list as $goods_k => $goods_v) {
|
|
$item_id = $goods_v['goods_id'];
|
|
if (in_array($item_id, $intersect)) {
|
|
$goods_sum += $goods_v['real_goods_money'] ?? 0;
|
|
$coupon_order_goods_ids[] = $goods_k;
|
|
}
|
|
}
|
|
$temp_cache_coupon[$parent_id] = ['goods_sum' => $goods_sum, 'coupon_order_goods_ids' => $coupon_order_goods_ids];
|
|
} else {
|
|
$goods_sum = $temp_cache_coupon_item['goods_sum'];
|
|
$coupon_order_goods_ids = $temp_cache_coupon_item['coupon_order_goods_ids'];
|
|
}
|
|
//判断它支持的商品的商品金额够不够最低金额
|
|
if ($goods_sum < $at_least) {
|
|
//移除会员优惠券
|
|
unset($member_coupon_list[$k]);
|
|
continue;
|
|
}
|
|
switch ($item_type) {
|
|
case 'reward'://满减
|
|
$item_coupon_money = $v['money'];
|
|
if ($item_coupon_money > $goods_sum) {
|
|
$item_coupon_money = $goods_sum;
|
|
}
|
|
break;
|
|
case 'discount'://折扣
|
|
$item_discount = $v['discount'];//折扣
|
|
$item_discount_limit = $v['discount_limit'];//最多抵扣
|
|
//计算折扣优惠金额
|
|
$item_coupon_money = $goods_sum * (10 - $item_discount) / 10;
|
|
$item_coupon_money = $item_coupon_money > $item_discount_limit && $item_discount_limit != 0 ? $item_discount_limit : $item_coupon_money;
|
|
$item_coupon_money = $item_coupon_money > $goods_sum ? $goods_sum : $item_coupon_money;
|
|
$item_coupon_money = round($item_coupon_money, 2);
|
|
break;
|
|
case 'random'://随机
|
|
$item_coupon_money = $v['money'];
|
|
if ($item_coupon_money > $goods_sum) {
|
|
$item_coupon_money = $goods_sum;
|
|
}
|
|
break;
|
|
}
|
|
$member_coupon_list[$k]['coupon_money'] = $item_coupon_money;
|
|
$member_coupon_list[$k]['coupon_order_goods_ids'] = $coupon_order_goods_ids;
|
|
//一个准则,折扣券不优先用
|
|
if ($item_coupon_money > $default_coupon_money) {
|
|
$default_coupon_id = $coupon_id;
|
|
$default_coupon_end_time = $v['end_time'];
|
|
$default_coupon_type = $item_type;
|
|
if ($item_type == 'discount') {
|
|
$default_coupon_discount = $v['discount'];
|
|
} else {
|
|
$default_coupon_offset_money = $v['money'];
|
|
}
|
|
$default_coupon_money = $item_coupon_money;
|
|
} else if ($item_coupon_money == $default_coupon_money) {
|
|
if ($item_type == 'discount') {
|
|
if ($default_coupon_type == $item_type) {
|
|
if ($v['discount_limit'] < $default_coupon_discount) {
|
|
$default_coupon_id = $coupon_id;
|
|
$default_coupon_end_time = $v['end_time'];
|
|
$default_coupon_type = $item_type;
|
|
$default_coupon_discount = $v['discount'];
|
|
} else if ($v['discount_limit'] == $default_coupon_discount) {
|
|
if ($v['end_time'] < $default_coupon_end_time) {
|
|
$default_coupon_id = $coupon_id;
|
|
$default_coupon_end_time = $v['end_time'];
|
|
$default_coupon_type = $item_type;
|
|
$default_coupon_discount = $v['discount'];
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
if ($default_coupon_type == $item_type) {
|
|
if ($v['money'] < $default_coupon_offset_money) {
|
|
$default_coupon_id = $coupon_id;
|
|
$default_coupon_end_time = $v['end_time'];
|
|
$default_coupon_type = $item_type;
|
|
$default_coupon_discount = $v['money'];
|
|
} else if ($v['money'] == $default_coupon_offset_money) {
|
|
if ($v['end_time'] < $default_coupon_end_time) {
|
|
$default_coupon_id = $coupon_id;
|
|
$default_coupon_end_time = $v['end_time'];
|
|
$default_coupon_type = $item_type;
|
|
$default_coupon_discount = $v['money'];
|
|
}
|
|
}
|
|
} else {
|
|
$default_coupon_id = $coupon_id;
|
|
$default_coupon_end_time = $v['end_time'];
|
|
$default_coupon_type = $item_type;
|
|
$default_coupon_offset_money = $v['money'];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
$temp_member_coupon_list = array_column($member_coupon_list, null, 'coupon_id');
|
|
}
|
|
$coupon_id = $calculate_data['coupon_id'] ?? '';
|
|
$coupon_money = 0;
|
|
$coupon_order_goods_ids = [];
|
|
//计算优惠券优惠
|
|
if (!empty($coupon_id)) {
|
|
$item_coupon_info = $temp_member_coupon_list[$coupon_id] ?? [];
|
|
//剔除非法代金券
|
|
if (empty($item_coupon_info)) {
|
|
$coupon_id = 0;
|
|
} else {
|
|
$item_coupon_money = $item_coupon_info['coupon_money'];
|
|
if ($item_coupon_money > $real_goods_money) {
|
|
$item_coupon_money = $real_goods_money;
|
|
}
|
|
$real_goods_money -= $item_coupon_money;
|
|
$coupon_money += $item_coupon_money;
|
|
$coupon_order_goods_ids = $item_coupon_info['coupon_order_goods_ids'];
|
|
}
|
|
}
|
|
if ($is_can_coupon) {
|
|
$coupon_switch = empty($member_coupon_list) ? false : true;
|
|
} else {
|
|
$coupon_switch = false;
|
|
}
|
|
$coupon_array = [
|
|
'member_coupon_list' => $member_coupon_list ?? [],
|
|
'coupon_switch' => $coupon_switch
|
|
];
|
|
$offset['coupon_array'] = $coupon_array;
|
|
$calculate_data['offset'] = $offset;
|
|
$calculate_data['real_goods_money'] = $real_goods_money;
|
|
$calculate_data['coupon_money'] = $coupon_money;
|
|
$calculate_data['coupon_id'] = $coupon_id;
|
|
$calculate_data['coupon_order_goods_ids'] = $coupon_order_goods_ids;
|
|
$pay_money -= $coupon_money;
|
|
$order_money -= $coupon_money;
|
|
$discount_money += $coupon_money;
|
|
$calculate_data['discount_money'] = $discount_money;
|
|
$calculate_data['pay_money'] = $pay_money;
|
|
$calculate_data['order_money'] = $order_money;
|
|
}
|
|
}
|
|
return $this->success($calculate_data);
|
|
}
|
|
|
|
|
|
/**
|
|
* 红包计算
|
|
* @param $calculate_data
|
|
*/
|
|
public function hongbaoCalculate($calculate_data)
|
|
{
|
|
$site_id = $calculate_data['site_id'];
|
|
$member_id = $calculate_data['member_id'] ?? 0;
|
|
if ($member_id > 0) {
|
|
$order_type = $calculate_data['order_type'];
|
|
//只有开单和卡项可以用优惠券
|
|
if (in_array($order_type, ['goods', 'card'])) {
|
|
$offset_money = $calculate_data['offset_money'] ?? 0;
|
|
$offset = $calculate_data['offset'] ?? [];
|
|
$pay_money = $calculate_data['pay_money'] ?? 0;
|
|
$goods_list = $calculate_data['goods_list'] ?? [];
|
|
$is_can_hongbao = true;
|
|
foreach ($goods_list as $k => $v) {
|
|
$trade_type = $v['trade_type'];
|
|
if ($trade_type == 'money') {
|
|
$is_can_hongbao = false;
|
|
}
|
|
}
|
|
if ($pay_money > 0) {
|
|
//查询可用的红包
|
|
$member_hongbao_model = new Hongbao();
|
|
$member_hongbao_params = array(
|
|
'site_id' => $site_id,
|
|
'member_id' => $member_id,
|
|
);
|
|
$member_hongbao_list = $member_hongbao_model->getMemberHongbaoGoodsList($member_hongbao_params)['data'] ?? [];
|
|
if (!empty($member_hongbao_list)) {
|
|
foreach ($member_hongbao_list as $k => $v) {
|
|
$item_hongbao_money = $v['money'];
|
|
if ($item_hongbao_money > $pay_money) {
|
|
$item_hongbao_money = $pay_money;
|
|
}
|
|
$member_hongbao_list[$k]['hongbao_money'] = $item_hongbao_money;
|
|
}
|
|
$temp_member_hongbao_list = array_column($member_hongbao_list, null, 'member_hongbao_id');
|
|
}
|
|
}
|
|
$hongbao_id_array = $calculate_data['hongbao_id'] ?? [];
|
|
$hongbao_money = 0;
|
|
//计算优惠券优惠
|
|
if (!empty($hongbao_id_array)) {
|
|
|
|
foreach ($hongbao_id_array as $hongbao_id_k => $hongbao_id_v) {
|
|
$item_hongbao_info = $temp_member_hongbao_list[$hongbao_id_v] ?? [];
|
|
//剔除非法的红包
|
|
if (empty($item_hongbao_info)) {
|
|
unset($hongbao_id_array[$hongbao_id_k]);
|
|
continue;
|
|
}
|
|
$item_hongbao_money = moneyFormat($item_hongbao_info['hongbao_money']);
|
|
if ($item_hongbao_money > $pay_money) {
|
|
$item_hongbao_money = $pay_money;
|
|
}
|
|
|
|
$pay_money -= $item_hongbao_money;
|
|
|
|
$hongbao_money += $item_hongbao_money;
|
|
}
|
|
}
|
|
$hongbao_money = moneyFormat($hongbao_money);
|
|
$pay_money = moneyFormat($pay_money);
|
|
if ($is_can_hongbao) {
|
|
$hongbao_switch = empty($member_hongbao_list) ? false : true;
|
|
} else {
|
|
$hongbao_switch = false;
|
|
}
|
|
$hongbao_array = [
|
|
'member_hongbao_list' => $member_hongbao_list ?? [],
|
|
'hongbao_switch' => $hongbao_switch
|
|
];
|
|
$offset['hongbao_array'] = $hongbao_array;
|
|
$calculate_data['offset'] = $offset;
|
|
$calculate_data['pay_money'] = $pay_money;
|
|
$offset_money += $hongbao_money;
|
|
$calculate_data['offset_money'] = $offset_money;
|
|
$calculate_data['hongbao_money'] = $hongbao_money;
|
|
$calculate_data['hongbao_id'] = empty($hongbao_id_array) ? '' : implode(',', $hongbao_id_array);
|
|
}
|
|
}
|
|
return $this->success($calculate_data);
|
|
}
|
|
|
|
} |