调整订单退款和购物卡发货,添加商户余额支付

This commit is contained in:
luofei 2024-03-14 14:10:01 +08:00
parent 9ee6264db4
commit 4a65e3b78a
4 changed files with 118 additions and 34 deletions

View File

@ -15,6 +15,8 @@ use app\common\dao\store\order\StoreOrderDao;
use app\common\dao\system\financial\FinancialRecordDao; use app\common\dao\system\financial\FinancialRecordDao;
use app\common\model\store\order\StoreGroupOrder; use app\common\model\store\order\StoreGroupOrder;
use app\common\model\store\order\StoreOrder; use app\common\model\store\order\StoreOrder;
use app\common\model\store\order\StoreRefundOrder;
use app\common\model\system\merchant\FinancialRecord;
use app\common\model\system\merchant\Merchant; use app\common\model\system\merchant\Merchant;
use app\common\model\user\User; use app\common\model\user\User;
use app\common\repositories\BaseRepository; use app\common\repositories\BaseRepository;
@ -114,6 +116,9 @@ class StoreOrderRepository extends BaseRepository
if ($type === 'balance') { if ($type === 'balance') {
return $this->payBalance($user, $groupOrder); return $this->payBalance($user, $groupOrder);
} }
if ($type === 'merBalance') {
return $this->payBalance($user, $groupOrder);
}
if (in_array($type, ['weixin', 'alipay'], true) && $isApp) { if (in_array($type, ['weixin', 'alipay'], true) && $isApp) {
$type .= 'App'; $type .= 'App';
@ -158,6 +163,45 @@ class StoreOrderRepository extends BaseRepository
return app('json')->status('success', '余额支付成功', ['order_id' => $groupOrder['group_order_id']]); return app('json')->status('success', '余额支付成功', ['order_id' => $groupOrder['group_order_id']]);
} }
/**
* @param User $user
* @param StoreGroupOrder $groupOrder
* @return mixed
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function payMerBalance(User $user, StoreGroupOrder $groupOrder)
{
if (!systemConfig('yue_pay_status'))
throw new ValidateException('未开启余额支付');
$merchant = Merchant::where('uid', $user['uid'])->find();
if (empty($merchant['mer_money']) || $merchant['mer_money'] < $groupOrder['pay_price'])
throw new ValidateException('余额不足,请更换支付方式');
Db::startTrans();
try {
$merchant->mer_money = bcsub($merchant->mer_money, $groupOrder['pay_price'], 2);
$merchant->save();
$userBillRepository = app()->make(UserBillRepository::class);
$userBillRepository->decBill($merchant['uid'], 'mer_money', 'pay_product', [
'link_id' => $groupOrder['group_order_id'],
'status' => 1,
'title' => '购买商品',
'number' => $groupOrder['pay_price'],
'mark' => '商户余额支付支付' . floatval($groupOrder['pay_price']) . '元购买商品',
'balance' => $merchant->mer_money
]);
$this->paySuccess($groupOrder);
Db::commit();
} catch (Exception $e) {
Db::rollback();
Log::error('余额支付失败'.$e->getMessage().'。line:'.$e->getLine().'。file:'.$e->getFile());
throw new ValidateException('余额支付失败'.$e->getMessage());
}
return app('json')->status('success', '余额支付成功', ['order_id' => $groupOrder['group_order_id']]);
}
public function changePayType(StoreGroupOrder $groupOrder, int $pay_type) public function changePayType(StoreGroupOrder $groupOrder, int $pay_type)
{ {
Db::transaction(function () use ($groupOrder, $pay_type) { Db::transaction(function () use ($groupOrder, $pay_type) {
@ -203,13 +247,11 @@ class StoreOrderRepository extends BaseRepository
$orderStatus = []; $orderStatus = [];
$groupOrder->append(['orderList.orderProduct']); $groupOrder->append(['orderList.orderProduct']);
$flag = true; $flag = true;
$finance = [];
$profitsharing = []; $profitsharing = [];
$userMerchantRepository = app()->make(UserMerchantRepository::class); //商户用户表 $userMerchantRepository = app()->make(UserMerchantRepository::class); //商户用户表
$storeOrderProfitsharingRepository = app()->make(StoreOrderProfitsharingRepository::class); $storeOrderProfitsharingRepository = app()->make(StoreOrderProfitsharingRepository::class);
$storeGroupOrderRepository = app()->make(StoreGroupOrderRepository::class); $storeGroupOrderRepository = app()->make(StoreGroupOrderRepository::class);
$uid = $groupOrder->uid; $uid = $groupOrder->uid;
$i = 1;
$isVipCoupon = $storeGroupOrderRepository->isVipCoupon($groupOrder); $isVipCoupon = $storeGroupOrderRepository->isVipCoupon($groupOrder);
//验证是不是该用户的第一个订单 //验证是不是该用户的第一个订单
$groupOrder->is_first = $storeGroupOrderRepository->validateOrderIsFirst((int)$groupOrder->uid); $groupOrder->is_first = $storeGroupOrderRepository->validateOrderIsFirst((int)$groupOrder->uid);
@ -219,7 +261,7 @@ class StoreOrderRepository extends BaseRepository
$isPoints = false; $isPoints = false;
$financeDao = new FinancialRecordDao(); $financeDao = new FinancialRecordDao();
foreach ($groupOrder->orderList as $_k => $order) { foreach ($groupOrder->orderList as $_k => $order) {
$isPickupCard = $order->source == 999; $scanPay = $order->source == 999;
$order->paid = 1; $order->paid = 1;
$order->pay_time = $time; $order->pay_time = $time;
$svipDiscount = bcadd($order->svip_discount, $svipDiscount, 2); $svipDiscount = bcadd($order->svip_discount, $svipDiscount, 2);
@ -370,8 +412,8 @@ class StoreOrderRepository extends BaseRepository
//自动打印订单 //自动打印订单
$this->autoPrinter($order->order_id, $order->mer_id); $this->autoPrinter($order->order_id, $order->mer_id);
if ($order->orderProduct[0]->product->isPlatformCard() || $isPickupCard) { if ($order->orderProduct[0]->product->isPlatformCard() || $scanPay) {
//购物卡自动发货 //购物卡、线下扫码支付自动发货
$deliveryData = [ $deliveryData = [
'delivery_type' => 3, 'delivery_type' => 3,
'remark' => '', 'remark' => '',
@ -649,6 +691,7 @@ class StoreOrderRepository extends BaseRepository
*/ */
public function computed(StoreOrder $order, User $user) public function computed(StoreOrder $order, User $user)
{ {
/** @var UserBillRepository $userBillRepository */
$userBillRepository = app()->make(UserBillRepository::class); $userBillRepository = app()->make(UserBillRepository::class);
if ($order->spread_uid) { if ($order->spread_uid) {
$spreadUid = $order->spread_uid; $spreadUid = $order->spread_uid;
@ -701,6 +744,26 @@ class StoreOrderRepository extends BaseRepository
// 'number' => $order->extension_two, // 'number' => $order->extension_two,
// ], $order->mer_id); // ], $order->mer_id);
} }
// 推广佣金
$promoterCommission = FinancialRecord::where('order_id', $order['order_id'])
->where('financial_type', 'commission_to_promoter')
->field('user_id,number')
->find();
if (!empty($promoterCommission)) {
$promoter = User::where('uid', $promoterCommission['user_id'])->find();
if (!empty($promoter)) {
$balance = bcadd($promoter->now_money, $promoterCommission['number'], 2);
$promoter->save(['now_money' => $balance]);
$userBillRepository->incBill($promoterCommission['user_id'], 'brokerage', 'order_one', [
'link_id' => $order['order_id'],
'status' => 1,
'title' => '获得推广佣金',
'number' => $promoterCommission['number'],
'mark' => $order->merchant['mer_name'] . '成功销售' . floatval($order['pay_price']) . '元,奖励推广佣金' . floatval($promoterCommission['number']),
'balance' => 0
]);
}
}
} }
/** /**
@ -718,6 +781,18 @@ class StoreOrderRepository extends BaseRepository
Queue::push(SendSmsJob::class, ['tempId' => 'ADMIN_TAKE_DELIVERY_CODE', 'id' => $order->order_id]); Queue::push(SendSmsJob::class, ['tempId' => 'ADMIN_TAKE_DELIVERY_CODE', 'id' => $order->order_id]);
app()->make(MerchantRepository::class)->computedLockMoney($order); app()->make(MerchantRepository::class)->computedLockMoney($order);
$order->save(); $order->save();
if ($order->uid != $order->merchant->uid && !$order->orderProduct[0]->product->isPlatformCard()) {
$refundPrice = StoreRefundOrder::where('order_id', $order['order_id'])->where('status', '<>', -1)->sum('refund_price');
$money = bcsub($order->total_price, bcadd($order['extension_one'], $order['extension_two'], 3), 2);
$money = bcsub($money, $refundPrice, 2);
//订单确认收货,增加商户销售金额
Merchant::where('mer_id', $order->mer_id)->update(['sale_amount' => Db::raw('sale_amount+' . $money)]);
//订单确认收货,增加商户采购金额
$merId = Merchant::where('uid', $order->uid)->value('mer_id');
if (!empty($merId)) {
Merchant::where('mer_id', $merId)->update(['purchase_amount' => Db::raw('purchase_amount+' . $money)]);
}
}
}); });
} }
@ -1321,6 +1396,8 @@ class StoreOrderRepository extends BaseRepository
'change_message' => $change_message, 'change_message' => $change_message,
'change_type' => $change_type, 'change_type' => $change_type,
]; ];
$statusRepository->adminId = $order['mer_id'];
$statusRepository->adminNickname = $order->merchant['nickname'];
if ($service_id) { if ($service_id) {
$statusRepository->createServiceLog($service_id,$orderStatus); $statusRepository->createServiceLog($service_id,$orderStatus);
} else { } else {

View File

@ -100,6 +100,9 @@ class StoreOrderStatusRepository extends BaseRepository
const ORDER_DELIVERY_CITY_REFUND = 'delivery_5_10'; const ORDER_DELIVERY_CITY_REFUND = 'delivery_5_10';
const ORDER_DELIVERY_CITY_REFUNDING = 'delivery_5_9'; const ORDER_DELIVERY_CITY_REFUNDING = 'delivery_5_9';
public $adminId;
public $adminNickname;
/** /**
* StoreOrderStatusRepository constructor. * StoreOrderStatusRepository constructor.
@ -135,9 +138,15 @@ class StoreOrderStatusRepository extends BaseRepository
{ {
try{ try{
$request = request(); $request = request();
if ($request->hasMacro('userType')) {
$data['user_type'] = $request->userType(); $data['user_type'] = $request->userType();
$data['uid'] = $request->adminId(); $data['uid'] = $request->adminId();
$data['nickname'] = $request->adminInfo()->real_name; $data['nickname'] = $request->adminInfo()->real_name;
} else {
$data['user_type'] = 3;
$data['uid'] = $this->adminId;
$data['nickname'] = $this->adminNickname;
}
return $this->dao->create($data); return $this->dao->create($data);
} catch (\Exception $e) { } catch (\Exception $e) {
return $this->createSysLog($data); return $this->createSysLog($data);

View File

@ -16,6 +16,7 @@ namespace app\common\repositories\store\order;
use app\common\dao\store\coupon\StoreCouponUserDao; use app\common\dao\store\coupon\StoreCouponUserDao;
use app\common\dao\store\order\StoreRefundOrderDao; use app\common\dao\store\order\StoreRefundOrderDao;
use app\common\dao\system\financial\FinancialRecordDao;
use app\common\model\store\order\StoreOrder; use app\common\model\store\order\StoreOrder;
use app\common\model\store\order\StoreRefundOrder; use app\common\model\store\order\StoreRefundOrder;
use app\common\repositories\BaseRepository; use app\common\repositories\BaseRepository;
@ -1528,26 +1529,16 @@ class StoreRefundOrderRepository extends BaseRepository
} }
} }
} }
$financeDao = new FinancialRecordDao();
app()->make(FinancialRecordRepository::class)->inc([ $financeDao->order = $res->order->toArray();
'order_id' => $res->refund_order_id, $financeDao->order['order_id'] = $res->refund_order_id;
'order_sn' => $res->refund_order_sn, $financeDao->order['order_sn'] = $res->refund_order_sn;
'user_info' => $res->user->nickname, $financeDao->user = $res->user;
'user_id' => $res->uid, $financeDao->platformOut($refundPriceAll, 'order_refund');
'financial_type' => 'refund_true', if ($refundRate > 0) {
'number' => $refundPriceAll, $financeDao->platformOut($refundRate, 'commission_to_platform_refund');
'type' => 1, }
], $res->mer_id); $financeDao->save();
app()->make(FinancialRecordRepository::class)->inc([
'order_id' => $res->refund_order_id,
'order_sn' => $res->refund_order_sn,
'user_info' => $res->user->nickname,
'user_id' => $res->uid,
'type' => 1,
'financial_type' => 'refund_charge',
'number' => $refundRate,
], $res->mer_id);
return $res; return $res;
} }

View File

@ -16,36 +16,43 @@ class OrderDeliveryListen implements ListenerInterface
public function handle($event): void public function handle($event): void
{ {
$order = $event['order']; $order = $event['order'];
$couponTotal = 0;
foreach ($order->orderProduct as $orderProduct) { foreach ($order->orderProduct as $orderProduct) {
if (!$orderProduct->product->isPlatformCard()) { if (!$orderProduct->product->isPlatformCard()) {
continue; continue;
} }
$couponTotal = bcadd($couponTotal, $orderProduct->total_price, 2);
}
if ($couponTotal > 0) {
//发放大礼包
/** @var StoreCouponRepository $repo */ /** @var StoreCouponRepository $repo */
$repo = app()->make(StoreCouponRepository::class); $repo = app()->make(StoreCouponRepository::class);
$coupon = $repo->validCouponQuery(StoreCouponRepository::TYPE_PLATFORM_CARD, StoreCouponRepository::GET_COUPON_TYPE_BUY)->find(); $coupon = $repo->validCouponQuery(StoreCouponRepository::TYPE_PLATFORM_CARD, StoreCouponRepository::GET_COUPON_TYPE_BUY)->find();
if (empty($coupon)) { if (empty($coupon)) {
return; return;
} }
$coupon->coupon_price = $order->total_price; $coupon->coupon_price = $couponTotal;
$couponUser = $repo->sendCoupon($coupon, $order['uid'], StoreCouponUserRepository::SEND_TYPE_BUY); $couponUser = $repo->sendCoupon($coupon, $order['uid'], StoreCouponUserRepository::SEND_TYPE_BUY);
StoreCouponDetail::income($order, $couponUser['coupon_user_id'], $coupon->coupon_price, $coupon['title']); StoreCouponDetail::income($order, $couponUser['coupon_user_id'], $coupon->coupon_price, $coupon['title']);
if (!empty($orderProduct->product->give_coupon_ids) && $order->total_price >= 1000) { //发放春耕补贴优惠券
if (!empty($orderProduct->product->give_coupon_ids) && $couponTotal >= 1000) {
$giveCoupons = $repo->getGiveCoupon($orderProduct->product->give_coupon_ids); $giveCoupons = $repo->getGiveCoupon($orderProduct->product->give_coupon_ids);
foreach ($giveCoupons as $giveCoupon) { foreach ($giveCoupons as $giveCoupon) {
if ($giveCoupon['type'] != StoreCouponRepository::TYPE_STORE_COUPON) { if ($giveCoupon['type'] != StoreCouponRepository::TYPE_STORE_COUPON) {
continue; continue;
} }
try { try {
$rate = $this->getRate($order->total_price); $rate = $this->getRate($couponTotal);
$giveCoupon->coupon_price = bcmul($order->total_price, $rate, 2); $giveCoupon->coupon_price = bcmul($couponTotal, $rate, 2);
$repo->startTime = date('Y-m-d H:i:s'); $repo->startTime = date('Y-m-d H:i:s');
$repo->endTime = '2025-07-01'; $repo->endTime = '2025-07-01';
$repo->status = -1; $repo->status = -1;
$couponUser = $repo->sendCoupon($giveCoupon, $order['uid'], StoreCouponUserRepository::SEND_TYPE_BUY); $couponUser = $repo->sendCoupon($giveCoupon, $order['uid'], StoreCouponUserRepository::SEND_TYPE_BUY);
StoreCouponDetail::income($order, $couponUser['coupon_user_id'], $giveCoupon->coupon_price, $giveCoupon['title'], 0); StoreCouponDetail::income($order, $couponUser['coupon_user_id'], $giveCoupon->coupon_price, $giveCoupon['title'], 0);
} catch (\Exception $e) { } catch (\Exception $e) {
Log::info('自动发放买赠优惠券:' . $e->getMessage()); Log::error('自动发放买赠优惠券出错:' . $e->getMessage());
} }
} }
} }
@ -60,7 +67,7 @@ class OrderDeliveryListen implements ListenerInterface
$rate = 0.10; $rate = 0.10;
} elseif ($orderAmount >= 10000 && $orderAmount < 50000) { } elseif ($orderAmount >= 10000 && $orderAmount < 50000) {
$rate = 0.15; $rate = 0.15;
} elseif ($orderAmount >= 50000 && $orderAmount <= 100000) { } elseif ($orderAmount >= 50000) {
$rate = 0.20; $rate = 0.20;
} }
return $rate ?? 0; return $rate ?? 0;