调整优惠券余额扣除
This commit is contained in:
parent
9bf5deef2c
commit
01ddd9d11f
@ -16,6 +16,7 @@ namespace app\common\dao\store\coupon;
|
||||
|
||||
use app\common\dao\BaseDao;
|
||||
use app\common\model\BaseModel;
|
||||
use app\common\model\store\coupon\StoreCouponDetail;
|
||||
use app\common\model\store\coupon\StoreCouponUser;
|
||||
use app\common\repositories\store\coupon\StoreCouponRepository;
|
||||
use think\exception\ValidateException;
|
||||
@ -179,11 +180,13 @@ class StoreCouponUserDao extends BaseDao
|
||||
* 扣减优惠券余额
|
||||
* @param $id
|
||||
* @param $amount
|
||||
* @param $order
|
||||
* @return void
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function reduce($id, $amount)
|
||||
public function reduce($id, $amount, $order)
|
||||
{
|
||||
$storeCouponUser = StoreCouponUser::where('coupon_user_id', $id)->find();
|
||||
if (empty($storeCouponUser)) {
|
||||
@ -200,6 +203,16 @@ class StoreCouponUserDao extends BaseDao
|
||||
if (!$storeCouponUser->save()) {
|
||||
throw new ValidateException('优惠券余额更新出错');
|
||||
}
|
||||
$detailModel = new StoreCouponDetail();
|
||||
$detailModel->uid = $order['uid'];
|
||||
$detailModel->coupon_user_id = $id;
|
||||
$detailModel->group_order_id = $order['group_order_id'];
|
||||
$detailModel->order_id = $order['order_id'] ?? 0;
|
||||
$detailModel->type = StoreCouponDetail::TYPE_EXPEND;
|
||||
$detailModel->amount = $amount;
|
||||
$detailModel->pay_price = $order['pay_price'];
|
||||
$detailModel->create_time = time();
|
||||
$detailModel->save();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -269,15 +282,16 @@ class StoreCouponUserDao extends BaseDao
|
||||
/**
|
||||
* 批量扣除优惠券余额
|
||||
* @param $coupons
|
||||
* @param $order
|
||||
* @return void
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function reduceAll($coupons)
|
||||
public function reduceAll($coupons, $order)
|
||||
{
|
||||
foreach ($coupons as $coupon) {
|
||||
$this->reduce($coupon['coupon_user_id'], $coupon['coupon_price']);
|
||||
$this->reduce($coupon['coupon_user_id'], $coupon['coupon_price'], $order);
|
||||
}
|
||||
}
|
||||
|
||||
|
38
app/common/model/store/coupon/StoreCouponDetail.php
Normal file
38
app/common/model/store/coupon/StoreCouponDetail.php
Normal file
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model\store\coupon;
|
||||
|
||||
use app\common\model\BaseModel;
|
||||
|
||||
/**
|
||||
* Class StoreCouponDetail
|
||||
* @package app\common\model\store\coupon
|
||||
*/
|
||||
class StoreCouponDetail extends BaseModel
|
||||
{
|
||||
|
||||
const TYPE_EXPEND = 1; //支出
|
||||
const TYPE_INCOME = 2; //收入
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public static function tablePk(): string
|
||||
{
|
||||
return 'id';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public static function tableName(): string
|
||||
{
|
||||
return 'store_coupon_detail';
|
||||
}
|
||||
|
||||
public function coupon()
|
||||
{
|
||||
return $this->hasOne(StoreCoupon::class, 'coupon_id', 'coupon_id');
|
||||
}
|
||||
|
||||
}
|
@ -1190,9 +1190,6 @@ class StoreOrderCreateRepository extends StoreOrderRepository
|
||||
throw new ValidateException('库存不足');
|
||||
}
|
||||
}
|
||||
if (!empty($useCoupon[$order['mer_id']])) {
|
||||
$storeCouponUserDao->reduceAll($order['cartInfo']['order']['user_used_coupon']);
|
||||
}
|
||||
}
|
||||
|
||||
if ($orderInfo['order_type'] == 10 && !app()->make(StoreDiscountRepository::class)->decStock($orderList[0]['cartInfo']['list'][0]['source_id'])) {
|
||||
@ -1204,13 +1201,15 @@ class StoreOrderCreateRepository extends StoreOrderRepository
|
||||
'is_pay' => 1
|
||||
]);
|
||||
|
||||
//使用了平台优惠券
|
||||
if (!empty($useCoupon[0])) {
|
||||
$storeCouponUserDao->reduce($useCoupon[0][0], $orderInfo['total_platform_coupon_price']);
|
||||
}
|
||||
|
||||
//创建订单
|
||||
$groupOrder = $storeGroupOrderRepository->create($groupOrder);
|
||||
|
||||
|
||||
//使用了平台优惠券
|
||||
if (!empty($useCoupon[0])) {
|
||||
$storeCouponUserDao->reduce($useCoupon[0][0], $orderInfo['total_platform_coupon_price'], $groupOrder);
|
||||
}
|
||||
|
||||
$bills = [];
|
||||
|
||||
if ($groupOrder['integral'] > 0) {
|
||||
@ -1238,6 +1237,11 @@ class StoreOrderCreateRepository extends StoreOrderRepository
|
||||
//创建子订单
|
||||
$_order = $this->dao->create($order);
|
||||
|
||||
|
||||
if (!empty($useCoupon[$order['mer_id']])) {
|
||||
$storeCouponUserDao->reduceAll($cartInfo['order']['user_used_coupon'], $_order);
|
||||
}
|
||||
|
||||
if ($order['integral'] > 0) {
|
||||
$bills[] = [
|
||||
'uid' => $uid,
|
||||
|
Loading…
x
Reference in New Issue
Block a user