diff --git a/app/common/dao/store/consumption/CommissionDao.php b/app/common/dao/store/consumption/CommissionDao.php index 80315cb1..acd26f4e 100755 --- a/app/common/dao/store/consumption/CommissionDao.php +++ b/app/common/dao/store/consumption/CommissionDao.php @@ -5,6 +5,7 @@ namespace app\common\dao\store\consumption; use app\common\model\store\consumption\StoreConsumption; use app\common\model\store\consumption\StoreConsumptionUser; use app\common\model\store\order\StoreOrder; +use app\common\model\system\financial\Financial; use app\common\model\system\merchant\Merchant; use app\common\model\user\User; use app\common\repositories\system\merchant\FinancialRecordRepository; @@ -25,7 +26,7 @@ class CommissionDao * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ - public function firstOrderCommission($order, $finance, $financeSn) + public function firstOrderCommission($order, $finance, $financeSn, $financeIndex) { $consumption = StoreConsumption::where('status', 1)->where('type', StoreConsumption::TYPE_FIRST_ORDER_COMMISSION)->find(); if (empty($consumption)) { @@ -44,19 +45,48 @@ class CommissionDao 'order_sn' => $order->order_sn, 'user_info' => $order->user->nickname, 'user_id' => $order['uid'], - 'financial_type' => 'first_order_commission', + 'financial_type' => 'first_order_commission', // TODO 这个类型应该是长期的 'financial_pm' => 0, 'type' => 2, 'number' => $commission, 'mer_id' => $order->mer_id, - 'financial_record_sn' => $financeSn + 'financial_record_sn' => $financeSn . $financeIndex ]; + $financeIndex++; app()->make(MerchantRepository::class)->addLockMoney($order['mer_id'], 'order', $order['order_id'], $commission); } - // TODO 商户暂时没有推广码,只给村合伙人和小组服务团队分佣 + // 给镇合伙人、村合伙人、小组服务团队、店铺分佣,仅直推 $promotionCode = User::where('uid', $order['uid'])->value('promotion_code'); if (!empty($promotionCode)) { - $this->sendCommission($order, $promotionCode); + $merchantId = substr($promotionCode, strpos($promotionCode, 'mer_') + 4); + if (strpos($promotionCode, 'mer_') !== false && $merchantId) { + $userId = Merchant::where('mer_id', $merchantId)->value('uid'); + $user = User::where('uid', $userId)->find(); + $commission = bcmul($order['pay_price'], 0.03, 2); + $finance[] = [ + 'order_id' => $order->order_id, + 'order_sn' => $order->order_sn, + 'user_info' => $user['nickname'], + 'user_id' => $user['uid'], + 'financial_type' => 'first_order_commission', + 'financial_pm' => 0, + 'type' => 2, + 'number' => $commission, + 'mer_id' => $order->mer_id, + 'financial_record_sn' => $financeSn . $financeIndex + ]; + 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) { + try { + (new StoreConsumptionUserDao())->send($consumption, $consumption['config']['red_pack_rate'], $user['uid'], $order['order_id'], $order['pay_price'], StoreConsumptionUser::STATUS_UNUSED, StoreConsumptionUser::TYPE_TWO); + } catch (\Exception $e) { + Log::error($e->getMessage()); + } + } + } else { + $this->sendCommission($order, $promotionCode); + } } return $finance; } @@ -69,7 +99,7 @@ class CommissionDao * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ - public function firstOrderBatchCommission($data) + public function firstOrderCommissionCallback($data) { $consumption = StoreConsumption::where('status', 1)->where('type', StoreConsumption::TYPE_FIRST_ORDER_COMMISSION)->find(); if (empty($consumption)) { @@ -92,7 +122,7 @@ class CommissionDao 'order_sn' => $order->order_sn, 'user_info' => $user['nickname'], 'user_id' => $user['uid'], - 'financial_type' => $user['type'] == 3 ? 'order_commission' : 'first_order_commission', + 'financial_type' => $user['type'] == 3 ? 'order_commission' : 'first_order_commission', // TODO 配送员的佣金类型需要调整 'financial_pm' => 0, 'type' => 2, 'number' => $commission, @@ -157,9 +187,9 @@ class CommissionDao public function refundByOrder($refundOrder) { $commission = bcmul($refundOrder->order['pay_price'], 0.01, 2); + $financialRecordRepository = app()->make(FinancialRecordRepository::class); + $financeSn = $financialRecordRepository->getSn(); if ($commission > 0 && $refundOrder->order['order_type'] == 1) { - $financialRecordRepository = app()->make(FinancialRecordRepository::class); - $financeSn = $financialRecordRepository->getSn(); // 订单为自提,且佣金大于0 $finance[] = [ 'order_id' => $refundOrder->order->order_id, @@ -173,9 +203,41 @@ class CommissionDao 'mer_id' => $refundOrder->order->mer_id, 'financial_record_sn' => $financeSn ]; - $financialRecordRepository->insertAll($finance); app()->make(MerchantRepository::class)->subLockMoney($refundOrder->order['mer_id'], 'order', $refundOrder->order['order_id'], $commission); } + // 是否已经退过佣金 + $refunded = Financial::where('order_id', $refundOrder->order['order_id']) + ->whereIn('financial_type', ['order_commission_refund', 'first_order_commission_refund']) + ->count(); + if ($refunded > 0) { + return; + } + + // 退佣金和红包 + $finance = []; + $financeRecord = Financial::where('order_id', $refundOrder->order['order_id']) + ->whereIn('financial_type', ['order_commission', 'first_order_commission']) + ->select()->toArray(); + $redPack = bcmul($refundOrder->order['pay_price'], 0.07, 2); + (new StoreConsumptionUserDao())->refundByCommission($refundOrder->order['uid'], $refundOrder->order->order_id, $redPack); + foreach ($financeRecord as $k => $item) { + $finance[] = [ + 'order_id' => $refundOrder->order->order_id, + 'order_sn' => $refundOrder->order->order_sn, + 'user_info' => $item['user_info'], + 'user_id' => $item['user_id'], + 'financial_type' => $item['financial_type'] . '_refund', + 'financial_pm' => 1, + 'type' => 2, + 'number' => $item['number'], + 'mer_id' => $item['mer_id'], + 'financial_record_sn' => $financeSn . ($k + 1) + ]; + } + if (count($finance) > 0) { + $financialRecordRepository->insertAll($finance); + } + $promotionCode = User::where('uid', $refundOrder['uid'])->value('promotion_code'); if ($promotionCode) { $curl = new Curl(); @@ -194,6 +256,7 @@ class CommissionDao } /** + * @deprecated 暂时弃用,后续可能会用 * 供销平台退佣金回调 * @param $data * @return array diff --git a/app/common/repositories/store/order/StoreOrderRepository.php b/app/common/repositories/store/order/StoreOrderRepository.php index e06a18f3..db2ed87d 100755 --- a/app/common/repositories/store/order/StoreOrderRepository.php +++ b/app/common/repositories/store/order/StoreOrderRepository.php @@ -508,7 +508,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++)); + $finance = (new CommissionDao())->firstOrderCommission($order, $finance, $financeSn, $i++); } } //分销判断 diff --git a/app/controller/api/Open.php b/app/controller/api/Open.php index 9935f798..03f757eb 100755 --- a/app/controller/api/Open.php +++ b/app/controller/api/Open.php @@ -27,7 +27,7 @@ class Open extends BaseController if (!empty($decrypted)) { $storeConsumptionUserDao = new CommissionDao(); // "惠农供销,谱写数字新篇章"活动首单分润 - $result = $storeConsumptionUserDao->firstOrderBatchCommission($decrypted); + $result = $storeConsumptionUserDao->firstOrderCommissionCallback($decrypted); return app('json')->success($result); } return app('json')->fail('解密失败');