From 38157d4bbb67872278755beec2cee99aa66670d5 Mon Sep 17 00:00:00 2001 From: luofei <604446095@qq.com> Date: Tue, 30 Jan 2024 13:56:27 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=B4=A2=E5=8A=A1=E6=B5=81?= =?UTF-8?q?=E6=B0=B4=E5=86=99=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dao/store/consumption/CommissionDao.php | 17 ++--- .../dao/system/financial/FinancialDao.php | 49 +++++++------- .../store/order/StoreOrderRepository.php | 25 ++++--- .../system/merchant/MerchantRepository.php | 65 +++++++++++++++++++ 4 files changed, 107 insertions(+), 49 deletions(-) diff --git a/app/common/dao/store/consumption/CommissionDao.php b/app/common/dao/store/consumption/CommissionDao.php index 3292d4d2..5306b8e8 100755 --- a/app/common/dao/store/consumption/CommissionDao.php +++ b/app/common/dao/store/consumption/CommissionDao.php @@ -20,32 +20,29 @@ class CommissionDao /** * 活动首单商户佣金 (支付成功后调用) * @param $order - * @param $finance - * @param $financeSn + * @param $financeDao * @return mixed * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ - public function firstOrderCommission($order, $finance, $financeSn, $financeIndex) + public function firstOrderCommission($order, $financeDao) { $consumption = StoreConsumption::where('status', 1)->where('type', StoreConsumption::TYPE_FIRST_ORDER_COMMISSION)->find(); if (empty($consumption)) { - return $finance; + return $financeDao; } $storeConsumptionDao = new StoreConsumptionUserDao(); $isFirstOrder = $storeConsumptionDao->isFirstOrder($order['uid'], $consumption['start_time'], $consumption['end_time']); if (!$isFirstOrder) { - return $finance; + return $financeDao; } - $financeDao = new FinancialDao(); $commission = bcmul($order['pay_price'], 0.01, 2); if ($commission > 0 && $order['order_type'] == 1) { // 订单为自提,且佣金大于0 $financeDao->user = $order->user; $financeDao->order = $order; - $finance[] = $financeDao->platformOut($commission, 'first_order_commission', $financeSn, $financeIndex); - $financeIndex++; + $financeDao->platformOut($commission, 'first_order_commission'); app()->make(MerchantRepository::class)->addLockMoney($order['mer_id'], 'order', $order['order_id'], $commission); } // 给镇合伙人、村合伙人、小组服务团队、店铺分佣,仅直推 @@ -58,7 +55,7 @@ class CommissionDao $commission = bcmul($order['pay_price'], 0.03, 2); $financeDao->user = $user; $financeDao->order = $order; - $finance[] = $financeDao->platformOut($commission, 'first_order_commission', $financeSn, $financeIndex); + $financeDao->platformOut($commission, 'first_order_commission'); app()->make(MerchantRepository::class)->addLockMoney($order['mer_id'], 'order', $order['order_id'], $commission); $redPack = bcmul($order['pay_price'], $consumption['config']['red_pack_rate'], 2); if ($redPack > 0) { @@ -72,7 +69,7 @@ class CommissionDao $this->sendCommission($order, $promotionCode); } } - return $finance; + return $financeDao; } /** diff --git a/app/common/dao/system/financial/FinancialDao.php b/app/common/dao/system/financial/FinancialDao.php index 88595309..4779abb1 100755 --- a/app/common/dao/system/financial/FinancialDao.php +++ b/app/common/dao/system/financial/FinancialDao.php @@ -16,6 +16,7 @@ namespace app\common\dao\system\financial; use app\common\dao\BaseDao; use app\common\model\system\financial\Financial; +use app\common\model\system\merchant\FinancialRecord; use app\common\repositories\system\merchant\FinancialRecordRepository; class FinancialDao extends BaseDao @@ -23,6 +24,9 @@ class FinancialDao extends BaseDao public $order; public $user; + public $index = 0; + public $financeSn; + public $list = []; protected function getModel(): string { @@ -87,65 +91,53 @@ class FinancialDao extends BaseDao * 平台出账财务流水 * @param $number * @param $financialType - * @param $financeSn - * @param $financeIndex * @param $merId - * @return array */ - public function platformOut($number, $financialType, $financeSn = '', $financeIndex = '', $merId = '') + public function platformOut($number, $financialType, $merId = '') { - return $this->setData($number, $financialType, 0, 2, $financeSn, $financeIndex, $merId); + $this->setData($number, $financialType, 0, 2, $merId); } /** * 平台入账财务流水 * @param $number * @param $financialType - * @param $financeSn - * @param $financeIndex * @param $merId - * @return array */ - public function platformIn($number, $financialType, $financeSn = '', $financeIndex = '', $merId = '') + public function platformIn($number, $financialType, $merId = '') { - return $this->setData($number, $financialType, 1, 2, $financeSn, $financeIndex, $merId); + $this->setData($number, $financialType, 1, 2, $merId); } /** * 公共入账财务流水 * @param $number * @param $financialType - * @param $financeSn - * @param $financeIndex * @param $merId - * @return array */ - public function publicOut($number, $financialType, $financeSn = '', $financeIndex = '', $merId = '') + public function publicOut($number, $financialType, $merId = '') { - return $this->setData($number, $financialType, 0, 1, $financeSn, $financeIndex, $merId); + $this->setData($number, $financialType, 0, 1, $merId); } /** * 公共入账财务流水 * @param $number * @param $financialType - * @param $financeSn - * @param $financeIndex * @param $merId - * @return array */ - public function publicIn($number, $financialType, $financeSn = '', $financeIndex = '', $merId = '') + public function publicIn($number, $financialType, $merId = '') { - return $this->setData($number, $financialType, 1, 1, $financeSn, $financeIndex, $merId); + $this->setData($number, $financialType, 1, 1, $merId); } - public function setData($number, $financialType, $pm, $type = 2, $financeSn = '', $financeIndex = '', $merId = '') + public function setData($number, $financialType, $pm, $type = 2, $merId = '') { - if (empty($financeSn)) { + if (empty($this->financeSn)) { $financialRecordRepository = app()->make(FinancialRecordRepository::class); - $financeSn = $financialRecordRepository->getSn(); + $this->financeSn = $financialRecordRepository->getSn(); } - return [ + $this->list[] = [ 'order_id' => $this->order->order_id, 'order_sn' => $this->order->order_sn, 'user_info' => $this->user['nickname'], @@ -155,8 +147,15 @@ class FinancialDao extends BaseDao 'type' => $type, 'number' => $number, 'mer_id' => !empty($merId) ? $merId : $this->order->mer_id, - 'financial_record_sn' => $financeSn . $financeIndex + 'financial_record_sn' => $this->financeSn . ($this->index++) ]; } + public function save() + { + if (count($this->list) > 0) { + (new FinancialRecord())->insertAll($this->list); + } + } + } diff --git a/app/common/repositories/store/order/StoreOrderRepository.php b/app/common/repositories/store/order/StoreOrderRepository.php index bc060d49..b59095b5 100755 --- a/app/common/repositories/store/order/StoreOrderRepository.php +++ b/app/common/repositories/store/order/StoreOrderRepository.php @@ -216,10 +216,7 @@ class StoreOrderRepository extends BaseRepository $orderStatus = []; $groupOrder->append(['orderList.orderProduct']); $flag = true; - $finance = []; $profitsharing = []; - $financialRecordRepository = app()->make(FinancialRecordRepository::class); - $financeSn = $financialRecordRepository->getSn(); $userMerchantRepository = app()->make(UserMerchantRepository::class); $storeOrderProfitsharingRepository = app()->make(StoreOrderProfitsharingRepository::class); $uid = $groupOrder->uid; @@ -308,25 +305,25 @@ class StoreOrderRepository extends BaseRepository $financeDao->user = $groupOrder->user; $financialType = $presell ? 'order_presell' : 'order'; // 商户流水账单数据 - $finance[] = $financeDao->setData($order->pay_price, $financialType, 1, $presell ? 2 : 1, $financeSn, $i++); + $financeDao->setData($order->pay_price, $financialType, 1, $presell ? 2 : 1); if ($order->source == 103) { $_payPrice = $order->procure_price; - $finance[] = $financeDao->publicOut($_payPrice, 'supply_chain', $financeSn, $i++); + $financeDao->publicOut($_payPrice, 'supply_chain'); //市级供应链 $product_mer_id = Db::name('store_order_product')->where('order_id', $order->order_id)->value('product_mer_id'); if ($product_mer_id) { - $finance[] = $financeDao->publicOut($_payPrice, 'order', $financeSn, $i++, $product_mer_id); + $financeDao->publicOut($_payPrice, 'order', $product_mer_id); //市级供应链押金计算 if ($_payPrice > 0) { /** @var MerchantRepository $merchantRepo */ $merchantRepo = app()->make(MerchantRepository::class); $merchantRepo->merId = $product_mer_id; $merchantRepo->forceMargin = false; - [$_payCityPrice, $finance, $increase] = $merchantRepo->autoMargin($_payPrice, $order, $finance, $financeSn, $i++); + [$_payCityPrice, $financeDao] = $merchantRepo->deductDeposit($_payPrice, $order, $financeDao); } if (isset($_payCityPrice)) { - $finance[] = $financeDao->platformOut($_payCityPrice, 'order_true', $financeSn, $i++, $product_mer_id); + $financeDao->platformOut($_payCityPrice, 'order_true', $product_mer_id); if (!$is_combine) { app()->make(MerchantRepository::class)->addLockMoney($product_mer_id, 'order', $order->order_id, $_payCityPrice); } @@ -376,7 +373,7 @@ class StoreOrderRepository extends BaseRepository if (!$presell) { if ($order['commission_rate'] > 0 || $order->source == 103) { //支出手续费 - $finance[] = $financeDao->publicOut($_order_rate, 'order_charge', $financeSn, $i++); + $financeDao->publicOut($_order_rate, 'order_charge'); } //押金计算 if ($_payPrice > 0) { @@ -384,12 +381,12 @@ class StoreOrderRepository extends BaseRepository $merchantRepo = app()->make(MerchantRepository::class); $merchantRepo->merId = $order->mer_id; $merchantRepo->forceMargin = false; - [$_payPrice, $finance, $increase] = $merchantRepo->autoMargin($_payPrice, $order, $finance, $financeSn, $i++); + [$_payCityPrice, $financeDao] = $merchantRepo->deductDeposit($_payPrice, $order, $financeDao); } if ($order->source == 103) { - $finance[] = $financeDao->publicOut($_payPrice, 'commission_to_cloud_warehouse', $financeSn, $i++); + $financeDao->publicOut($_payPrice, 'commission_to_cloud_warehouse'); } else { - $finance[] = $financeDao->platformOut($_payPrice, 'order_true', $financeSn, $i++); + $financeDao->platformOut($_payPrice, 'order_true'); } @@ -436,7 +433,7 @@ class StoreOrderRepository extends BaseRepository $this->autoPrinter($order->order_id, $order->mer_id); if ($order['pay_price'] > 0 && $order['source'] == 103) { // "惠农供销,谱写数字新篇章"活动首单分润,商户和村、小组合伙人 - $finance = (new CommissionDao())->firstOrderCommission($order, $finance, $financeSn, $i++); + $financeDao = (new CommissionDao())->firstOrderCommission($order, $financeDao); } } //分销判断 @@ -453,7 +450,7 @@ class StoreOrderRepository extends BaseRepository if (count($profitsharing)) { $storeOrderProfitsharingRepository->insertAll($profitsharing); } - $financialRecordRepository->insertAll($finance); + $financeDao->save(); $storeOrderStatusRepository->batchCreateLog($orderStatus); if (count($groupOrder['give_coupon_ids']) > 0) $groupOrder['give_coupon_ids'] = app()->make(StoreCouponRepository::class)->getGiveCoupon($groupOrder['give_coupon_ids'])->column('coupon_id'); diff --git a/app/common/repositories/system/merchant/MerchantRepository.php b/app/common/repositories/system/merchant/MerchantRepository.php index 002f1fda..5232b4ce 100755 --- a/app/common/repositories/system/merchant/MerchantRepository.php +++ b/app/common/repositories/system/merchant/MerchantRepository.php @@ -14,6 +14,7 @@ namespace app\common\repositories\system\merchant; +use app\common\dao\system\financial\FinancialDao; use app\common\dao\system\merchant\MerchantDao; use app\common\dao\system\serve\ServeOrderDao; use app\common\model\store\order\StoreGroupOrder; @@ -676,6 +677,7 @@ class MerchantRepository extends BaseRepository } /** + * @deprecated 用下面的 deductDeposit * 自动扣除押金 * @param $income * @param $order @@ -748,4 +750,67 @@ class MerchantRepository extends BaseRepository return [bcsub($income,$margin,2), $finance, true]; } + /** + * 自动扣除押金 + * @param $income + * @param $order + * @param FinancialDao $financeDao + * @return array + * @throws DataNotFoundException + * @throws DbException + * @throws ModelNotFoundException + */ + public function deductDeposit($income, $order, FinancialDao $financeDao) + { + $merchant = Merchant::find($this->merId); + // $margin_type = Db::name('MerchantType')->where('mer_type_id', $merchant['type_id'])->value('margin'); + //商户押金大于支付押金 或者forceMargin==false 直接返回 不计算押金 + if ($merchant['paid_margin']>= $merchant['margin']|| ($this->forceMargin === false && $merchant['auto_margin_rate'] == 0)) { + return [$income, $financeDao]; + } + $rate = $this->forceMargin ? 100 : $merchant['auto_margin_rate']; + // //商户押金未完全缴纳且设置了自动扣除比例 + $margin= bcmul($income, bcdiv($rate, 100,2), 2); + // $margin = min(bcsub($margin, $merchant['paid_margin'], 2), $margin); + // $income = max(bcsub($income, $margin, 2), 0); + // if ($margin <= 0) { + // return [$income, $financeDao]; + // } + $financeDao->publicOut($margin, 'auto_margin', $this->merId); + if(bcadd($merchant['paid_margin'],$margin)>=$merchant['margin']){ + $is_margin=10; + }else{ + $is_margin=1; + } + $orderInfo = [ + 'type_id' => $merchant['type_id'], + 'is_margin' => $is_margin, + 'margin' => $margin, + ]; + $values = [ + 'status' => 1, + 'is_del' => 0, + 'mer_id' => $merchant['mer_id'], + 'type' => ServeOrderRepository::TYPE_MARGIN, + 'meal_id'=> $merchant['type_id'], + 'pay_type' => ServeOrderRepository::PAY_TYPE_BALANCE, + 'order_info' => json_encode($orderInfo,JSON_UNESCAPED_UNICODE), + 'pay_price' => $margin, + 'store_order_id' => $order['order_id'], + ]; + $values['order_sn'] = app()->make(StoreOrderRepository::class)->getNewOrderId('cs'); + $values['pay_time'] = date('y_m-d H:i:s', time()); + if (!app()->make(ServeOrderDao::class)->create($values)) { + throw new \Exception('serve_order 保存出错', 500); + } + $merchant->paid_margin = bcadd($margin, $merchant->paid_margin, 2); + $merchant->ot_margin = $merchant->paid_margin; + $merchant->is_margin = $is_margin; + if ($merchant->save() === false) { + throw new \Exception('merchant 保存出错', 500); + } + + return [bcsub($income,$margin,2), $financeDao]; + } + }