diff --git a/app/common/repositories/store/order/StoreOrderRepository.php b/app/common/repositories/store/order/StoreOrderRepository.php index 591bcfb1..4c335251 100755 --- a/app/common/repositories/store/order/StoreOrderRepository.php +++ b/app/common/repositories/store/order/StoreOrderRepository.php @@ -346,7 +346,7 @@ class StoreOrderRepository extends BaseRepository $financeDao->order = $order; $financeDao->user = $groupOrder->user; $financialType = $presell ? 'order_presell' : 'order'; - // 公共收入流水账单数据 + // 平台收入流水账单数据 $financeDao->platformIn($order->total_price, $financialType); if ($order->consumption_money > 0) { // 平台支出优惠金额 @@ -793,7 +793,7 @@ class StoreOrderRepository extends BaseRepository // ], $order->mer_id); } - $promoterCommission = FinancialRecord::where('order_id', $order['id']) + $promoterCommission = FinancialRecord::where('order_id', $order['order_id']) ->where('financial_type', 'promoter_commission') ->column('user_id,number'); if (!empty($promoterCommission)) { diff --git a/app/common/repositories/store/order/StoreOtherOrderRepository.php b/app/common/repositories/store/order/StoreOtherOrderRepository.php index cc19b248..c3f808ff 100755 --- a/app/common/repositories/store/order/StoreOtherOrderRepository.php +++ b/app/common/repositories/store/order/StoreOtherOrderRepository.php @@ -12,10 +12,16 @@ namespace app\common\repositories\store\order; use app\common\dao\store\order\StoreOrderOtherDao; +use app\common\dao\system\financial\FinancialDao; +use app\common\model\store\order\StoreGroupOrder; use app\common\model\store\order\StoreGroupOrderOther; +use app\common\model\store\order\StoreOrder; use app\common\model\store\order\StoreOrderInterestOther; use app\common\model\store\order\StoreOrderOther; use app\common\model\store\order\StoreRefundOrder; +use app\common\model\store\order\StoreRefundOrderOther; +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; use app\common\repositories\store\product\ProductRepository; @@ -171,6 +177,7 @@ class StoreOtherOrderRepository extends BaseRepository //订单记录 $storeOrderStatusRepository = app()->make(StoreOrderStatusOtherRepository::class); $svipDiscount = 0; + $financeDao = new FinancialDao(); foreach ($groupOrder->orderList as $_k => $order) { if($groupOrder->order_extend){ if($order->order_extend){ @@ -205,21 +212,18 @@ class StoreOtherOrderRepository extends BaseRepository 'user_type' => $storeOrderStatusRepository::U_TYPE_USER, ]; - // 商户流水账单数据 - $finance[] = [ - 'order_id' => $order->order_id, - 'order_sn' => $order->order_sn, - 'user_info' => $groupOrder->user->nickname, - 'user_id' => $uid, - 'financial_type' => $presell ? 'order_presell' : 'order', - 'financial_pm' => 1, - 'type' => $presell ? 2 : 1, - 'number' => $order->pay_price, - 'mer_id' => $order->mer_id, - 'financial_record_sn' => $financeSn . ($i++) - ]; + $financeDao->order = $order; + $financeDao->user = $groupOrder->user; + $financialType = $presell ? 'order_presell' : 'order'; + // 平台收入流水账单数据 + $financeDao->platformIn($order->total_price, $financialType); + if ($order->consumption_money > 0) { + // 平台支出优惠金额 + $financeDao->platformOut($order->consumption_money, 'platform_consumption'); + } $_payPrice = bcsub($order->pay_price, bcadd($order['extension_one'], $order['extension_two'], 3), 2); + $orderValidAmount = bcsub($order->total_price, bcadd($order['extension_one'], $order['extension_two'], 3), 2); if ($presell) { if (isset($order->orderProduct[0]['cart_info']['presell_extension_one']) && $order->orderProduct[0]['cart_info']['presell_extension_one'] > 0) { $_payPrice = bcadd($_payPrice, $order->orderProduct[0]['cart_info']['presell_extension_one'], 2); @@ -229,72 +233,39 @@ class StoreOtherOrderRepository extends BaseRepository } } - $_order_rate = 0; + // 平台支出推广费 + $promoter = $order->merchant->promoter(); + $promoterCommission = bcmul($orderValidAmount, 0.003, 2); + if (!empty($promoter) && $promoterCommission > 0) { + $financeDao->platformOut($promoterCommission, 'commission_to_promoter'); + } - //平台手续费 - if ($order['commission_rate'] > 0) { - - $commission_rate = ($order['commission_rate'] / 100); - - $_order_rate = bcmul($_payPrice, $commission_rate, 2); - - $_payPrice = bcsub($_payPrice, $_order_rate, 2); - // 结算各镇 小组佣金 - event('order.paySuccessOrderOther', compact('order', '_order_rate')); + // 平台收入手续费 + $commission_rate = bcdiv((string)$order['commission_rate'],'100',6); + $platformCommission = bcmul($orderValidAmount, (string)$commission_rate, 2); + if ($commission_rate > 0 && $platformCommission > 0) { + $orderValidAmount = bcsub($orderValidAmount, $platformCommission, 2); + if ($promoterCommission > 0 && !empty($promoter)) { + $platformCommission = bcsub($platformCommission, $promoterCommission, 2); + } + $financeDao->platformIn($platformCommission, 'commission_to_platform', $order['mer_id']); } if (!$presell) { - - if ($order['commission_rate'] > 0) { - $finance[] = [ - 'order_id' => $order->order_id, - 'order_sn' => $order->order_sn, - 'user_info' => $groupOrder->user->nickname, - 'user_id' => $uid, - 'financial_type' => 'order_charge', - 'financial_pm' => 0, - 'type' => 1, - 'number' => $_order_rate, - 'mer_id' => $order->mer_id, - 'financial_record_sn' => $financeSn . ($i++) - ]; - } //押金计算 - if ($_payPrice > 0) { + if ($orderValidAmount > 0) { /** @var MerchantRepository $merchantRepo */ $merchantRepo = app()->make(MerchantRepository::class); - $merchantRepo->merId = $order->mer_id; + $merchantRepo->merId = $order['mer_id']; $merchantRepo->forceMargin = false; - [$_payPrice, $finance, $increase] = $merchantRepo->autoMargin($_payPrice, $order, $finance, $financeSn, $i++); + [$orderValidAmount, $financeDao] = $merchantRepo->deductDeposit($orderValidAmount, $order, $financeDao); + } + // 商户收入金额 + $financeDao->platformOut($orderValidAmount, 'merchant_order', $order['mer_id']); + if (!$is_combine) { + app()->make(MerchantRepository::class)->addLockMoney($order->mer_id, 'order', $order->order_id, $orderValidAmount); } - $finance[] = [ - 'order_id' => $order->order_id, - 'order_sn' => $order->order_sn, - 'user_info' => $groupOrder->user->nickname, - 'user_id' => $uid, - 'financial_type' => 'order_true', - 'financial_pm' => 0, - 'type' => 2, - 'number' => $_payPrice, - 'mer_id' => $order->mer_id, - 'financial_record_sn' => $financeSn . ($i++) - ]; - if ($order->platform_coupon_price > 0) { - $finance[] = [ - 'order_id' => $order->order_id, - 'order_sn' => $order->order_sn, - 'user_info' => $groupOrder->user->nickname, - 'user_id' => $uid, - 'financial_type' => $isVipCoupon ? 'order_svip_coupon' : 'order_platform_coupon', - 'financial_pm' => 0, - 'type' => 1, - 'number' => $order->platform_coupon_price, - 'mer_id' => $order->mer_id, - 'financial_record_sn' => $financeSn . ($i++) - ]; - // $_payPrice = bcadd($_payPrice, $order->platform_coupon_price, 2); - } // if (!$is_combine) { // app()->make(MerchantRepository::class)->addLockMoney($order->mer_id, 'order', $order->order_id, $_payPrice); // } @@ -554,14 +525,34 @@ class StoreOtherOrderRepository extends BaseRepository public function takeAfter(StoreOrderOther $order, ?User $user) { Db::transaction(function () use ($user, $order) { + if ($user && $order->pay_type != StoreGroupOrder::PAY_TYPE_CREDIT_BUY) { + $this->computed($order, $user); + } Queue::push(SendSmsJob::class, ['tempId' => 'ORDER_TAKE_SUCCESS', 'id' => $order->order_id]); Queue::push(SendSmsJob::class, ['tempId' => 'ADMIN_TAKE_DELIVERY_CODE', 'id' => $order->order_id]); + if ($order->pay_type != StoreGroupOrder::PAY_TYPE_CREDIT_BUY) { + app()->make(MerchantRepository::class)->computedSupplyLockMoney($order); + } if (!empty($order->interest) && $order->interest->status == StoreOrderInterestOther::STATUS_UNSETTLED) { $order->interest->start_time = date('Y-m-d H:i:s', strtotime("+{$order->interest->settle_cycle} days")); $order->interest->save(); } $order->save(); + + if ($order->uid != $order->merchant->uid && !$order->orderProduct[0]->product->isPlatformCard()) { + $refundPrice = StoreRefundOrderOther::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)]); + } + } + }); } @@ -1908,4 +1899,26 @@ class StoreOtherOrderRepository extends BaseRepository throw new Exception($e->getMessage()); } } + + public function computed(StoreOrderOther $order, User $user) + { + /** @var UserBillRepository $userBillRepository */ + $userBillRepository = app()->make(UserBillRepository::class); + $promoterCommission = FinancialRecord::where('order_id', $order['order_id']) + ->where('financial_type', 'promoter_commission') + ->column('user_id,number'); + if (!empty($promoterCommission)) { + $userBillRepository->incBill($promoterCommission['user_id'], 'brokerage', 'order_one', [ + 'link_id' => $order['order_id'], + 'status' => 0, + 'title' => '获得推广佣金', + 'number' => $promoterCommission['number'], + 'mark' => $order->merchant['mer_name'] . '成功销售' . floatval($order['pay_price']) . '元,奖励推广佣金' . floatval($promoterCommission['number']), + 'balance' => 0 + ]); + $balance = bcadd($user->now_money, $promoterCommission['number'], 2); + $user->save(['now_money' => $balance]); + } + } + } diff --git a/app/common/repositories/system/merchant/MerchantIntentionRepository.php b/app/common/repositories/system/merchant/MerchantIntentionRepository.php index 0ee93605..8b9ed3c1 100755 --- a/app/common/repositories/system/merchant/MerchantIntentionRepository.php +++ b/app/common/repositories/system/merchant/MerchantIntentionRepository.php @@ -203,7 +203,7 @@ class MerchantIntentionRepository extends BaseRepository Db::name('store_service')->insert($store_service_data); } if ($intention['type'] == 2) { - $merId = MerchantIntention::where('uid', $intention['uid'])->where('status', 1)->value('mer_id'); + $merId = Merchant::where('uid', $intention['uid'])->value('mer_id'); if (!empty($merId)) { Merchant::where('mer_id', $merId)->update(['business_status' => 2, 'mer_settlement_agree_status' => 1]); } diff --git a/app/common/repositories/system/merchant/MerchantRepository.php b/app/common/repositories/system/merchant/MerchantRepository.php index cdb95ff4..52f1b8ae 100755 --- a/app/common/repositories/system/merchant/MerchantRepository.php +++ b/app/common/repositories/system/merchant/MerchantRepository.php @@ -603,6 +603,42 @@ class MerchantRepository extends BaseRepository }); } + public function computedSupplyLockMoney($order) + { + Db::transaction(function () use ($order) { + $money = 0; + /** @var UserBillRepository $make */ + $make = app()->make(UserBillRepository::class); + $bills = $make->search(['mer_id' => $order['mer_id'], 'category' => 'mer_lock_money', 'type' => 'order', 'link_id' => '1' . $order->order_id, 'status' => 0])->select(); + foreach ($bills as $bill) { + if ($bill) { + $money = bcsub($bill->number, $make->refundMerchantMoney($bill->link_id, $bill->type, $bill->mer_id), 2); + if ($order->presellOrder) { + $presellBill = $make->search(['category' => 'mer_lock_money', 'type' => 'presell', 'link_id' => '2' . $order->presellOrder->presell_order_id, 'status' => 0])->find(); + if ($presellBill) { + $money = bcadd($money, bcsub($presellBill->number, $make->refundMerchantMoney($presellBill->link_id, $presellBill->type, $presellBill->mer_id), 2), 2); + $presellBill->status = 1; + $presellBill->save(); + } + } + $bill->status = 1; + $bill->save(); + } + if ($money > 0) { + app()->make(UserBillRepository::class)->incBill($bill->uid, 'mer_computed_money', 'order', [ + 'link_id' => $order->order_id, + 'mer_id' => $bill->mer_id, + 'status' => 0, + 'title' => '商户待解冻余额', + 'number' => $money, + 'mark' => '交易完成,商户待解冻余额' . floatval($money) . '元', + 'balance' => 0 + ]); + } + } + }); + } + public function checkMargin($merId, $typeId) { $merchant = $this->dao->get($merId);