From 4a65e3b78ab3cd9a17aebc081013b464cca33d2a Mon Sep 17 00:00:00 2001 From: luofei <604446095@qq.com> Date: Thu, 14 Mar 2024 14:10:01 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E8=AE=A2=E5=8D=95=E9=80=80?= =?UTF-8?q?=E6=AC=BE=E5=92=8C=E8=B4=AD=E7=89=A9=E5=8D=A1=E5=8F=91=E8=B4=A7?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0=E5=95=86=E6=88=B7=E4=BD=99=E9=A2=9D?= =?UTF-8?q?=E6=94=AF=E4=BB=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../store/order/StoreOrderRepository.php | 87 +++++++++++++++++-- .../order/StoreOrderStatusRepository.php | 15 +++- .../order/StoreRefundOrderRepository.php | 31 +++---- crmeb/listens/OrderDeliveryListen.php | 19 ++-- 4 files changed, 118 insertions(+), 34 deletions(-) diff --git a/app/common/repositories/store/order/StoreOrderRepository.php b/app/common/repositories/store/order/StoreOrderRepository.php index 5ea2353c..d5a6206b 100644 --- a/app/common/repositories/store/order/StoreOrderRepository.php +++ b/app/common/repositories/store/order/StoreOrderRepository.php @@ -15,6 +15,8 @@ use app\common\dao\store\order\StoreOrderDao; use app\common\dao\system\financial\FinancialRecordDao; use app\common\model\store\order\StoreGroupOrder; 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\user\User; use app\common\repositories\BaseRepository; @@ -114,6 +116,9 @@ class StoreOrderRepository extends BaseRepository if ($type === 'balance') { return $this->payBalance($user, $groupOrder); } + if ($type === 'merBalance') { + return $this->payBalance($user, $groupOrder); + } if (in_array($type, ['weixin', 'alipay'], true) && $isApp) { $type .= 'App'; @@ -158,6 +163,45 @@ class StoreOrderRepository extends BaseRepository 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) { Db::transaction(function () use ($groupOrder, $pay_type) { @@ -203,13 +247,11 @@ class StoreOrderRepository extends BaseRepository $orderStatus = []; $groupOrder->append(['orderList.orderProduct']); $flag = true; - $finance = []; $profitsharing = []; $userMerchantRepository = app()->make(UserMerchantRepository::class); //商户用户表 $storeOrderProfitsharingRepository = app()->make(StoreOrderProfitsharingRepository::class); $storeGroupOrderRepository = app()->make(StoreGroupOrderRepository::class); $uid = $groupOrder->uid; - $i = 1; $isVipCoupon = $storeGroupOrderRepository->isVipCoupon($groupOrder); //验证是不是该用户的第一个订单 $groupOrder->is_first = $storeGroupOrderRepository->validateOrderIsFirst((int)$groupOrder->uid); @@ -219,7 +261,7 @@ class StoreOrderRepository extends BaseRepository $isPoints = false; $financeDao = new FinancialRecordDao(); foreach ($groupOrder->orderList as $_k => $order) { - $isPickupCard = $order->source == 999; + $scanPay = $order->source == 999; $order->paid = 1; $order->pay_time = $time; $svipDiscount = bcadd($order->svip_discount, $svipDiscount, 2); @@ -370,8 +412,8 @@ class StoreOrderRepository extends BaseRepository //自动打印订单 $this->autoPrinter($order->order_id, $order->mer_id); - if ($order->orderProduct[0]->product->isPlatformCard() || $isPickupCard) { - //购物卡自动发货 + if ($order->orderProduct[0]->product->isPlatformCard() || $scanPay) { + //购物卡、线下扫码支付自动发货 $deliveryData = [ 'delivery_type' => 3, 'remark' => '', @@ -649,6 +691,7 @@ class StoreOrderRepository extends BaseRepository */ public function computed(StoreOrder $order, User $user) { + /** @var UserBillRepository $userBillRepository */ $userBillRepository = app()->make(UserBillRepository::class); if ($order->spread_uid) { $spreadUid = $order->spread_uid; @@ -701,6 +744,26 @@ class StoreOrderRepository extends BaseRepository // 'number' => $order->extension_two, // ], $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]); app()->make(MerchantRepository::class)->computedLockMoney($order); $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_type' => $change_type, ]; + $statusRepository->adminId = $order['mer_id']; + $statusRepository->adminNickname = $order->merchant['nickname']; if ($service_id) { $statusRepository->createServiceLog($service_id,$orderStatus); } else { diff --git a/app/common/repositories/store/order/StoreOrderStatusRepository.php b/app/common/repositories/store/order/StoreOrderStatusRepository.php index 2fe32e60..1dfc6c4b 100644 --- a/app/common/repositories/store/order/StoreOrderStatusRepository.php +++ b/app/common/repositories/store/order/StoreOrderStatusRepository.php @@ -100,6 +100,9 @@ class StoreOrderStatusRepository extends BaseRepository const ORDER_DELIVERY_CITY_REFUND = 'delivery_5_10'; const ORDER_DELIVERY_CITY_REFUNDING = 'delivery_5_9'; + public $adminId; + public $adminNickname; + /** * StoreOrderStatusRepository constructor. @@ -135,9 +138,15 @@ class StoreOrderStatusRepository extends BaseRepository { try{ $request = request(); - $data['user_type'] = $request->userType(); - $data['uid'] = $request->adminId(); - $data['nickname'] = $request->adminInfo()->real_name; + if ($request->hasMacro('userType')) { + $data['user_type'] = $request->userType(); + $data['uid'] = $request->adminId(); + $data['nickname'] = $request->adminInfo()->real_name; + } else { + $data['user_type'] = 3; + $data['uid'] = $this->adminId; + $data['nickname'] = $this->adminNickname; + } return $this->dao->create($data); } catch (\Exception $e) { return $this->createSysLog($data); diff --git a/app/common/repositories/store/order/StoreRefundOrderRepository.php b/app/common/repositories/store/order/StoreRefundOrderRepository.php index d5fe483e..7dc9d787 100644 --- a/app/common/repositories/store/order/StoreRefundOrderRepository.php +++ b/app/common/repositories/store/order/StoreRefundOrderRepository.php @@ -16,6 +16,7 @@ namespace app\common\repositories\store\order; use app\common\dao\store\coupon\StoreCouponUserDao; 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\StoreRefundOrder; use app\common\repositories\BaseRepository; @@ -1528,26 +1529,16 @@ class StoreRefundOrderRepository extends BaseRepository } } } - - 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, - 'financial_type' => 'refund_true', - 'number' => $refundPriceAll, - 'type' => 1, - ], $res->mer_id); - - 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); + $financeDao = new FinancialRecordDao(); + $financeDao->order = $res->order->toArray(); + $financeDao->order['order_id'] = $res->refund_order_id; + $financeDao->order['order_sn'] = $res->refund_order_sn; + $financeDao->user = $res->user; + $financeDao->platformOut($refundPriceAll, 'order_refund'); + if ($refundRate > 0) { + $financeDao->platformOut($refundRate, 'commission_to_platform_refund'); + } + $financeDao->save(); return $res; } diff --git a/crmeb/listens/OrderDeliveryListen.php b/crmeb/listens/OrderDeliveryListen.php index 5959baf4..7dac4b00 100644 --- a/crmeb/listens/OrderDeliveryListen.php +++ b/crmeb/listens/OrderDeliveryListen.php @@ -16,36 +16,43 @@ class OrderDeliveryListen implements ListenerInterface public function handle($event): void { $order = $event['order']; + $couponTotal = 0; foreach ($order->orderProduct as $orderProduct) { if (!$orderProduct->product->isPlatformCard()) { continue; } + $couponTotal = bcadd($couponTotal, $orderProduct->total_price, 2); + } + + if ($couponTotal > 0) { + //发放大礼包 /** @var StoreCouponRepository $repo */ $repo = app()->make(StoreCouponRepository::class); $coupon = $repo->validCouponQuery(StoreCouponRepository::TYPE_PLATFORM_CARD, StoreCouponRepository::GET_COUPON_TYPE_BUY)->find(); if (empty($coupon)) { return; } - $coupon->coupon_price = $order->total_price; + $coupon->coupon_price = $couponTotal; $couponUser = $repo->sendCoupon($coupon, $order['uid'], StoreCouponUserRepository::SEND_TYPE_BUY); 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); foreach ($giveCoupons as $giveCoupon) { if ($giveCoupon['type'] != StoreCouponRepository::TYPE_STORE_COUPON) { continue; } try { - $rate = $this->getRate($order->total_price); - $giveCoupon->coupon_price = bcmul($order->total_price, $rate, 2); + $rate = $this->getRate($couponTotal); + $giveCoupon->coupon_price = bcmul($couponTotal, $rate, 2); $repo->startTime = date('Y-m-d H:i:s'); $repo->endTime = '2025-07-01'; $repo->status = -1; $couponUser = $repo->sendCoupon($giveCoupon, $order['uid'], StoreCouponUserRepository::SEND_TYPE_BUY); StoreCouponDetail::income($order, $couponUser['coupon_user_id'], $giveCoupon->coupon_price, $giveCoupon['title'], 0); } catch (\Exception $e) { - Log::info('自动发放买赠优惠券:' . $e->getMessage()); + Log::error('自动发放买赠优惠券出错:' . $e->getMessage()); } } } @@ -60,7 +67,7 @@ class OrderDeliveryListen implements ListenerInterface $rate = 0.10; } elseif ($orderAmount >= 10000 && $orderAmount < 50000) { $rate = 0.15; - } elseif ($orderAmount >= 50000 && $orderAmount <= 100000) { + } elseif ($orderAmount >= 50000) { $rate = 0.20; } return $rate ?? 0;