Commit 9b58ffed authored by 刘小敏's avatar 刘小敏

计算订单和创建订单接口加入积分抵扣逻辑

parent dafc0bbf
......@@ -116,7 +116,7 @@ class Order extends Common
public function calc()
{
if (!$this->request->isPost()) {
$this->error('');
$this->error('请求方式需用post');
}
$params = $this->request->param();
$this->svalidate($params, ".calc");
......
......@@ -97,6 +97,12 @@ class Order
$order = $payOper->checkAndPaid($order, 'order');
}
// 如果非积分订单使用了积分抵扣,扣除积分
if ($order->type == 'goods' && $order->deduct_score){
// 这里会自动检测订单支付状态
$order = $payOper->score($order, $order->deduct_score);
}
// 删除购物车
if ($from == 'cart') {
foreach ($items as $item) {
......
......@@ -66,14 +66,14 @@ class OrderCreate
protected $coupon_id = 0;
/**
* 用户积分总数
* 是否使用积分抵扣
*/
protected $score = 0;
protected $is_deduct_score = 0;
/**
* 积分可以抵扣的金额(元)
* 用户积分总数
*/
protected $score_deduct_amount = 0;
protected $score = 0;
/**
* 用户备注
......@@ -125,7 +125,9 @@ class OrderCreate
'dispatch_infos' => [], // 当前订单商品按照配送方式分组
'order_amount' => 0, // 订单总费用
'pay_fee' => 0, // 应支付总金额 (减去优惠之后的)
'score_generate' => 0, // 订单产生的积分
'deduct_score' => 0, // 订单扣除积分数
'score_generate' => 0, // 下单会产生的积分
'left_score' => 0, // 抵扣后剩余积分数
];
......@@ -166,7 +168,7 @@ class OrderCreate
$this->coupon_id = $params['coupon_id'] ?? 0;
$this->remark = $params['remark'] ?? '';
$this->score = $params['score'] ?? 0;
$this->is_deduct_score = $params['is_deduct_score'] ?? 0; // 是否使用积分结算 1:是 0:否
$this->money = (isset($params['money']) && $params['money'] > 0) ? $params['money'] : 0;
// 获取商品信息
......@@ -608,21 +610,51 @@ class OrderCreate
}
/**
* 计算积分的抵扣
* 获取用户积分余额
*
* @return void
*/
public function calcScore()
public function getUserScore()
{
// 获取积分余额
$score = UserModel::where('id', $this->user->id)->value('score');
if ($score) {
//计算积分抵扣金额
$this->score_deduct_amount = round($score/100, 2);
}
// 用户当前可用积分总数
$this->score = $score;
}
/**
* 计算积分的抵扣
* 抵扣规则:100积分抵扣1元
* 下单产生积分规则:订单支付金额 1元产生1积分
* @return void
*/
public function calcScore()
{
// 100积分抵扣1元
$score_rate = 100;
// 积分可换算的抵扣金额
$can_deduct_money = $this->score / $score_rate;
// 抵扣多少钱 : 实际最多只能抵扣订单金额
$deduct_money = min($can_deduct_money, $this->orderData['pay_fee']);
// 用了多少积分 : 实际消耗积分
$use_points = (int)round($deduct_money * $score_rate);
// 还要付多少钱 : 抵扣后实付金额,不能为负数
$final_pay_fee = max(round($this->orderData['pay_fee'] - $deduct_money, 2), 0);
// 剩余积分
$left_points = $this->score - $use_points;
$this->orderData['deduct_score'] = $use_points; // 订单扣除积分数
$this->orderData['left_score'] = $left_points; // 剩余积分
$this->orderData['score_generate'] = floor($final_pay_fee); // 下单会产生的积分数
$this->orderData['pay_fee'] = $final_pay_fee; // 订单需支付金额
}
/**
* 获取用户的优惠券列表,可用和不可用的做区分
......@@ -1011,13 +1043,12 @@ class OrderCreate
'dispatch_amount' => $this->orderData['dispatch_amount'],
'real_dispatch_amount' => $this->orderData['real_dispatch_amount'],
'order_amount' => $this->orderData['order_amount'],
'pay_fee' => $this->orderData['pay_fee'],
'pay_fee' => $this->orderData['pay_fee'], // 订单需要支付的金额
'temp_remain_pay_fee' => $temp_remain_pay_fee >= 0 ? $temp_remain_pay_fee : 0, // 临时剩余应支付金额,订单确认页面显示
'total_discount_fee' => $this->orderData['total_discount_fee'],
'coupon_discount_fee' => $this->orderData['coupon_discount_fee'],
'promo_discount_fee' => $this->orderData['promo_discount_fee'], // 包含包邮的运费优惠
'money' => $this->money, // 余额支付部分
'score' => $this->score_deduct_amount, // 积分抵扣部分
"invoice_amount" => $this->orderData[$this->invoiceConfig['amount_type']] // 可开票金额
];
......@@ -1032,12 +1063,15 @@ class OrderCreate
'activity_type' => $this->activity['activity']['type'] ?? null,
'activity_title' => $this->activity['activity']['title'] ?? null,
'promo_types' => array_column($this->activity['promo_infos'], 'activity_type'),
'score_amount' => $this->orderData['score_amount'], // 消耗积分
'score_deduct_amount' => $this->score_deduct_amount, // 积分抵扣金额
'score_amount' => $this->orderData['score_amount'],
'score' => $this->score, // 用户当前可用积分数
'deduct_score' => $this->orderData['deduct_score'], // 订单扣除积分数
'left_score' => $this->orderData['left_score'], // 积分抵扣后,用户剩余积分数
'score_generate' => $this->orderData['score_generate'], // 下单会产生的积分数
'goods_list' => $this->goodsList,
'promo_infos' => $this->activity['promo_infos'],
"invoice_status" => $this->invoiceConfig['status'], // 发票可开状态
"invoice_amount_type" => $this->invoiceConfig['amount_type'], // 发票金额类型
"invoice_status" => $this->invoiceConfig['status'], // 发票可开状态
"invoice_amount_type" => $this->invoiceConfig['amount_type'], // 发票金额类型
"need_address" => $this->need_address,
"offline_status" => $this->offline_status,
]);
......@@ -1047,7 +1081,7 @@ class OrderCreate
$result = array_merge($result, [
"coupon" => $this->orderData['coupon'],
"user_address" => $this->userAddress,
"score_amount" => $this->orderData['score_amount']
// "score_amount" => $this->orderData['score_amount']
]);
} else {
$result = array_merge($result, ['msg' => $this->msg]);
......@@ -1076,7 +1110,7 @@ class OrderCreate
// 计算订单各种费用
$this->calcAmount();
// 计算订单各种优惠(promo 等)
// 计算订单各种优惠(promo 等)优惠活动
if ($this->isCalcPromos()) { // 判断是否参与优惠
$this->calcDiscount();
}
......@@ -1086,17 +1120,20 @@ class OrderCreate
$this->calcCoupon();
}
// 计算积分费用
if ($this->score) {
$this->calcScore();
}
// 获取用户当前积分余额
$this->getUserScore();
// 订单应付金额
$this->orderData['order_amount'] = bcadd($this->orderData['goods_amount'], $this->orderData['dispatch_amount'], 2);
$this->orderData['total_discount_fee'] = bcadd($this->orderData['coupon_discount_fee'], $this->orderData['promo_discount_fee'], 2);
$this->orderData['pay_fee'] = bcsub($this->orderData['order_amount'], $this->orderData['total_discount_fee'], 2);
$this->orderData['order_amount'] = bcadd($this->orderData['goods_amount'], $this->orderData['dispatch_amount'], 2); // 订单总金额=商品总金额+运费总金额
$this->orderData['total_discount_fee'] = bcadd($this->orderData['coupon_discount_fee'], $this->orderData['promo_discount_fee'], 2); // 优惠总金额=优惠券优惠金额+活动优惠金额
$this->orderData['pay_fee'] = bcsub($this->orderData['order_amount'], $this->orderData['total_discount_fee'], 2); // 支付金额 = 订单总金额-优惠总金额
$this->orderData['pay_fee'] = $this->orderData['pay_fee'] < 0 ? 0 : $this->orderData['pay_fee'];
// 如果用户选择了积分抵扣(积分消耗规则:一旦选择了积分抵扣,所有积分一次性消耗完,100积分抵扣1元)
if ( $this->is_deduct_score == 1 ) {
$this->calcScore();
}
return $this->calcReturn();
}
......@@ -1158,6 +1195,8 @@ class OrderCreate
$orderData['remark'] = $this->remark;
$orderData['order_amount'] = $result['order_amount'];
$orderData['score_amount'] = $result['score_amount'];
$orderData['deduct_score'] = $result['deduct_score'];
$orderData['score_generate'] = $result['score_generate'];
$orderData['pay_fee'] = $result['pay_fee'];
$orderData['original_pay_fee'] = $result['pay_fee']; // 记录支付总金额,后面可能会订单改价
$orderData['remain_pay_fee'] = $result['pay_fee']; // 这里还是显示应支付总金额,扣除动作放到实际支付时
......
......@@ -129,8 +129,8 @@ class PayOper
$real_fee = $score; // 积分商城真实抵扣,就是积分
} else {
$log_type = 'order_pay';
// $real_fee = ; // 积分商城真实抵扣,就是积分
error_stop('缺少积分抵扣金额'); // 支持积分抵扣时补全
$real_fee = $score; // 非积分商城订单,积分抵扣
// error_stop('缺少积分抵扣金额'); // 支持积分抵扣时补全
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment