Explorar el Código

商贸区、精彩生活区分账逻辑

family hace 1 año
padre
commit
acaba6797e

+ 304 - 0
app/command/CommercialAreaOrderIndependentAccount.php

@@ -0,0 +1,304 @@
1
+<?php
2
+
3
+namespace app\command;
4
+
5
+use app\common\model\store\order\StoreOrder;
6
+use app\common\model\user\UserCertification;
7
+use app\common\repositories\merchant\order\OrderGzcRepository;
8
+use app\common\repositories\merchant\order\OrderMerchantRepository;
9
+use app\common\repositories\store\order\StoreOrderRepository;
10
+use app\common\repositories\store\order\StoreOrderStatusRepository;
11
+use app\common\repositories\user\UserBillRepository;
12
+use app\common\repositories\user\UserRepository;
13
+use Carbon\Carbon;
14
+use think\console\Command;
15
+use think\console\Input;
16
+use think\console\Output;
17
+use think\facade\Db;
18
+use think\facade\Log;
19
+
20
+class CommercialAreaOrderIndependentAccount extends Command
21
+{
22
+    protected function configure()
23
+    {
24
+        // 指令配置
25
+        $this->setName('commercial-area-order-independent-account')
26
+            ->setDescription('商贸区订单分账');
27
+    }
28
+
29
+    protected function execute(Input $input, Output $output)
30
+    {
31
+        // @todo 商贸区订单分账
32
+        $this->doCommercialAreaOrderIndependentAccount([439,491,496]);
33
+    }
34
+
35
+    /**
36
+     * 商贸区订单分账
37
+     *
38
+     * @param $excludeMerId
39
+     * @param $timeLimit
40
+     * @return void
41
+     * @throws \think\db\exception\DataNotFoundException
42
+     * @throws \think\db\exception\DbException
43
+     * @throws \think\db\exception\ModelNotFoundException
44
+     */
45
+    private function doCommercialAreaOrderIndependentAccount($excludeMerId = [], $timeLimit = 30)
46
+    {
47
+        // 商户信息
48
+        $merchants = [];
49
+//        $StoreOrderStatusRepository = app()->make(StoreOrderStatusRepository::class);
50
+//        $OrderMerchantRepository = app()->make(OrderMerchantRepository::class);
51
+//        $OrderGzcRepository = app()->make(OrderGzcRepository::class);
52
+
53
+        /** @var StoreOrderRepository $storeOrderRepository */
54
+        $storeOrderRepository = app()->make(StoreOrderRepository::class);
55
+
56
+        $time = time();
57
+        // 订单状态(0:待发货;1:待收货;2:待评价;3:已完成;4已申请退款;-1:已退款)5未支付 6已取消'
58
+        $status = [2, 3];
59
+        // 已支付、未结算
60
+        $where = ['paid' => 1, 'gzc' => 0, 'is_del' => 0, 'is_system_del' => 0];
61
+        // 普通订单 @todo 排除 特殊商户{$excludeMerId}:精彩生活区、会员区、复购区
62
+        $cursor = Db::table('rrx_store_order')->where($where)->where('pay_type', 'in', '1,2,4')->whereIn('status', $status)
63
+            ->whereNotIn('mer_id', $excludeMerId)->cursor();
64
+        foreach ($cursor as $key => $value) {
65
+            $settlement_period_time = strtotime($value['create_time']) + $timeLimit * 24 * 3600;
66
+            // @todo 下单后第{$timeLimit}天后可分账
67
+            if ($settlement_period_time >= $time) {
68
+                if (!isset($merchants[$value['mer_id']])) {
69
+                    $merchants[$value['mer_id']] = Db::name('merchant')->where('mer_id', $value['mer_id'])->find();
70
+                }
71
+                $group_order = Db::name('store_group_order')->alias('sgo')->where('sgo.group_order_id', $value['group_order_id'])->find();
72
+                // @todo 分账账号 496:表示{会员专区}账户;商贸区分账如下:成本分账到商户,利润分账到{会员专区}账户
73
+                $independentAccount = $storeOrderRepository->getIndependentAccount($value, $group_order, 496, $merchants[$value['mer_id']]['hf_member_id']);
74
+
75
+                // @todo 预分账处理
76
+                $isSuccessed = $storeOrderRepository->prepareExecuteIndependentAccount(
77
+                    $value, $group_order, $independentAccount['api_key'], $independentAccount['hf_member_id'], $independentAccount['hf_platform_member_id'], $value['cost'], $value['con_pri']
78
+                );
79
+                Db::name("log")->insert(array('data' => '商贸区-订单分账:' . json_encode($value, JSON_FORCE_OBJECT)));//日志记录
80
+
81
+//                Db::transaction(function () use ($certification, $online, $pension, $OrderGzcRepository, $OrderMerchantRepository, $StoreOrderRepository) {
82
+//                    //新增入账明细记录
83
+//                    $orderId   = $OrderMerchantRepository->getNewOrderId('IN10');
84
+//                    $create    = [
85
+//                        'uid'          => $online['uid'],
86
+//                        'platform_no'  => $orderId,
87
+//                        'account_type' => $online['pay_type'] == 1 ? 3 : 2,
88
+//                        'user_name'    => $certification['user_name'],
89
+//                        'mobile'       => $certification['mobile'],
90
+//                        'user_card'    => $certification['user_card'],
91
+//                        'pension'      => $pension,
92
+//                        'order_type'   => 2,
93
+//                        'out_trade_no' => $online['order_sn']
94
+//                    ];
95
+//                    $test_user = Db::name('user')->where('uid', $online['uid'])->value('test_user');
96
+//                    $check     = Db::name("gzc_logs")->where("out_trade_no", $online['order_sn'])->where("order_type", 2)->count();
97
+//                    if ($test_user == 0 && $check <= 0) {
98
+//                        $OrderGzcRepository->create($create);
99
+//                    }
100
+//                    $order                  = StoreOrder::getInstance()->find($online['order_id']);
101
+//                    $order->gzc             = 1;
102
+//                    $order->save();
103
+//                });
104
+            }
105
+        }
106
+
107
+        // 普通订单(红包,积分,京东,佣金,复购金,养老金,贡献值)
108
+//        $cursor = Db::table('rrx_store_order')->where('paid', 1)->where('is_settlement', 0)->where('pay_type', 'in', '0,1,2,4')->cursor();
109
+//        $StoreOrderStatusRepository = app()->make(StoreOrderStatusRepository::class);
110
+//        $OrderMerchantRepository    = app()->make(OrderMerchantRepository::class);
111
+//        $OrderGzcRepository         = app()->make(OrderGzcRepository::class);
112
+//        foreach ($cursor as $online) {
113
+//            $status = $StoreOrderStatusRepository->getWhere(['order_id' => $online['order_id'], 'change_type' => 'take']);
114
+//            if ($online['mer_id'] == 496) {
115
+//                if (!in_array($online['status'], [0, 1, 2, 3])) {
116
+//                    continue;
117
+//                }
118
+//                if (time() < strtotime($online['pay_time']) + 3600 * 24) {
119
+//                    Log::info('还没有到收货后' . $timeLimit . '天');
120
+//                    continue;
121
+//                }
122
+//            } else {
123
+//                if (!in_array($online['status'], [2, 3])) {
124
+//                    continue;
125
+//                }
126
+//                if (Carbon::now()->modify("-{$timeLimit} days")->lt($status->change_time ?? $status['change_time'])) {
127
+//                    Log::info('还没有到收货后' . $timeLimit . '天');
128
+//                    continue;
129
+//                }
130
+//            }
131
+//
132
+//            $StoreOrderRepository = app()->make(StoreOrderRepository::class);
133
+//            Db::transaction(function () use ($online, $StoreOrderRepository) {
134
+//                $order = StoreOrder::getInstance()->find($online['order_id']);
135
+//                $order->is_settlement   = 1;
136
+//                $order->settlement_time = date('Y-m-d H:i:s');
137
+//                $order->save();
138
+//
139
+//                // 结算积分
140
+//                $user_sign_score_list = Db::name('user_sign_score')
141
+//                    ->where('status', -1)
142
+//                    ->where('order_id', $online['order_id'])
143
+//                    ->whereIn('type', [1, 2])
144
+//                    ->select()
145
+//                    ->toArray();
146
+//                foreach ($user_sign_score_list as $user_sign_score) {
147
+//                    // 修改积分
148
+//                    Db::name('user')
149
+//                        ->where('uid', $user_sign_score['uid'])
150
+//                        ->inc('score', (float)$user_sign_score['reward_socre'])
151
+//                        ->update();
152
+//                    Db::name('user_sign_score')
153
+//                        ->where('status', -1)
154
+//                        ->where('id', $user_sign_score['id'])
155
+//                        ->update([
156
+//                            'status'          => 1,
157
+//                            'settlement_time' => date('Y-m-d H:i:s')
158
+//                        ]);
159
+//                }
160
+//
161
+//                // 结算红包
162
+//                $user_sign_hongbao_list = Db::name('user_sign_hongbao')
163
+//                    ->where('status', -1)
164
+//                    ->where('order_id', $online['order_id'])
165
+//                    ->whereIn('type', [1, 2])
166
+//                    ->select()
167
+//                    ->toArray();
168
+//                foreach ($user_sign_hongbao_list as $user_sign_hongbao) {
169
+//                    // 修改红包
170
+//                    Db::name('user')
171
+//                        ->where('uid', $user_sign_hongbao['uid'])
172
+//                        ->inc('hongbao', (float)$user_sign_hongbao['number'])
173
+//                        ->update();
174
+//                    Db::name('user_sign_hongbao')
175
+//                        ->where('status', -1)
176
+//                        ->where('id', $user_sign_hongbao['id'])
177
+//                        ->update([
178
+//                            'status'          => 1,
179
+//                            'settlement_time' => date('Y-m-d H:i:s')
180
+//                        ]);
181
+//                }
182
+//
183
+//
184
+//                // 结算京豆
185
+//                $user_sign_jingdou_list = Db::name('user_sign_jingdou')
186
+//                    ->where('status', -1)
187
+//                    ->where('order_id', $online['order_id'])
188
+//                    ->whereIn('type', [1, 2])
189
+//                    ->select()
190
+//                    ->toArray();
191
+//                foreach ($user_sign_jingdou_list as $user_sign_jingdou) {
192
+//                    // 修改京豆
193
+//                    Db::name('user')
194
+//                        ->where('uid', $user_sign_jingdou['uid'])
195
+//                        ->inc('jingdou', (float)$user_sign_jingdou['number'])
196
+//                        ->update();
197
+//                    Db::name('user_sign_jingdou')
198
+//                        ->where('status', -1)
199
+//                        ->where('id', $user_sign_jingdou['id'])
200
+//                        ->update([
201
+//                            'status'          => 1,
202
+//                            'settlement_time' => date('Y-m-d H:i:s')
203
+//                        ]);
204
+//                }
205
+//
206
+//
207
+//
208
+//                // 结算复购金
209
+//                $user_sign_fugou_list = Db::name('user_sign_fugou')
210
+//                    ->where('status', -1)
211
+//                    ->where('order_id', $online['order_id'])
212
+//                    ->whereIn('type', [1, 2])
213
+//                    ->select()
214
+//                    ->toArray();
215
+//                foreach ($user_sign_fugou_list as $user_sign_fugou) {
216
+//                    // 修改复购金
217
+//                    Db::name('user')
218
+//                        ->where('uid', $user_sign_fugou['uid'])
219
+//                        ->inc('fugou', (float)$user_sign_fugou['number'])
220
+//                        ->update();
221
+//                    Db::name('user_sign_fugou')
222
+//                        ->where('status', -1)
223
+//                        ->where('id', $user_sign_fugou['id'])
224
+//                        ->update([
225
+//                            'status'          => 1,
226
+//                            'settlement_time' => date('Y-m-d H:i:s')
227
+//                        ]);
228
+//                }
229
+//
230
+//
231
+//                // 结算贡献值
232
+//                $user_sign_gongxian_list = Db::name('user_sign_gongxian')
233
+//                    ->where('status', -1)
234
+//                    ->where('order_id', $online['order_id'])
235
+//                    ->whereIn('type', [1, 2])
236
+//                    ->select()
237
+//                    ->toArray();
238
+//                foreach ($user_sign_gongxian_list as $user_sign_gongxian) {
239
+//                    // 修改贡献值
240
+//                    Db::name('user')
241
+//                        ->where('uid', $user_sign_gongxian['uid'])
242
+//                        ->inc('contribute', (float)$user_sign_gongxian['number'])
243
+//                        ->update();
244
+//                    Db::name('user_sign_gongxian')
245
+//                        ->where('status', -1)
246
+//                        ->where('id', $user_sign_gongxian['id'])
247
+//                        ->update([
248
+//                            'status'          => 1,
249
+//                            'settlement_time' => date('Y-m-d H:i:s')
250
+//                        ]);
251
+//                }
252
+//
253
+//
254
+//                // 结算佣金
255
+//                $user_bill_list = Db::name('user_bill')
256
+//                    ->where('status', 0)
257
+//                    ->where('commission_type','<>', 5)
258
+//                    ->where([ 'link_id' => $online['order_id'], 'order_sn' => $online['order_sn']])
259
+//                    ->select()
260
+//                    ->toArray();
261
+//                foreach ($user_bill_list as $user_bill) {
262
+//                    if ($user_bill['commission_type'] != 5) {
263
+//                        if (in_array($user_bill['commission_type'], [7,12,19])) {
264
+//                            Db::name("merchant")
265
+//                                ->where("mer_id", $user_bill['mer_id'])
266
+//                                ->inc('brokerage_price', (float)$user_bill['number'])
267
+//                                ->update();
268
+//                        } else {
269
+//                            Db::name('user')
270
+//                                ->where('uid', $user_bill['uid'])
271
+//                                ->inc('brokerage_price', (float)$user_bill['number'])
272
+//                                ->update();
273
+//                        }
274
+//                    }
275
+//                    if ($online['pay_type'] == 0 && $user_bill['commission_type'] == 5) {
276
+//                    } else {
277
+//                        // 虚拟支付的养老金不修改不结算
278
+//                        Db::name('user_bill')
279
+//                            ->where('status', 0)
280
+//                            ->where('bill_id', $user_bill['bill_id'])
281
+//                            ->update([
282
+//                                'status'    => 1,
283
+//                                'take_time' => time()
284
+//                            ]);
285
+//                    }
286
+//                    if ($online['pay_type'] == 0 && $user_bill['commission_type'] == 5) {
287
+//                    } else {
288
+//                        // 虚拟支付的养老金不修改不结算
289
+//                        Db::name('user_bill')
290
+//                            ->where('status', 0)
291
+//                            ->where('bill_id', $user_bill['bill_id'])
292
+//                            ->update([
293
+//                                'status'    => 1,
294
+//                                'take_time' => time()
295
+//                            ]);
296
+//                    }
297
+//                }
298
+//                //按角色结算
299
+//                $StoreOrderRepository->close_order($online['order_id']);
300
+//            });
301
+//        }
302
+    }
303
+
304
+}

+ 12 - 12
app/command/GreatLifeOrderIndependentAccount.php

@@ -32,7 +32,6 @@ class GreatLifeOrderIndependentAccount extends Command
32 32
         $this->doGreatLifeOrderIndependentAccount();
33 33
     }
34 34
 
35
-
36 35
     /**
37 36
      * 精彩生活区订单分账
38 37
      *
@@ -46,18 +45,18 @@ class GreatLifeOrderIndependentAccount extends Command
46 45
     private function doGreatLifeOrderIndependentAccount($timeLimit = 30, $merId = 439)
47 46
     {
48 47
         $time = time();
49
-        // 订单状态(0:待发货;1:待收货;2:待评价;3:已完成;4已申请退款;-1:已退款)5未支付 6已取消'
50
-        $status = [2,3];
51 48
         // 已支付、未结算
52 49
         $where = [
53 50
             'paid' => 1, 'gzc' => 0, 'mer_id' => $merId, 'is_del' => 0, 'is_system_del' => 0
54 51
         ];
52
+        // 订单状态(0:待发货;1:待收货;2:待评价;3:已完成;4已申请退款;-1:已退款)5未支付 6已取消'
53
+        $status = [2,3];
55 54
         // 普通订单
56 55
         $cursor = Db::table('rrx_store_order')->where($where)->where('pay_type', 'in', '1,2,4')->whereIn('status', $status)->cursor();
57 56
 
58
-        $StoreOrderStatusRepository = app()->make(StoreOrderStatusRepository::class);
59
-        $OrderMerchantRepository    = app()->make(OrderMerchantRepository::class);
60
-        $OrderGzcRepository         = app()->make(OrderGzcRepository::class);
57
+//        $StoreOrderStatusRepository = app()->make(StoreOrderStatusRepository::class);
58
+//        $OrderMerchantRepository    = app()->make(OrderMerchantRepository::class);
59
+//        $OrderGzcRepository         = app()->make(OrderGzcRepository::class);
61 60
         // 商户信息
62 61
         $merchant = Db::name('merchant')->where('mer_id', $merId)->find();
63 62
 
@@ -68,14 +67,15 @@ class GreatLifeOrderIndependentAccount extends Command
68 67
             $settlement_period_time = strtotime($value['create_time']) + $timeLimit * 24 * 3600;
69 68
             // @todo 下单后第{$timeLimit}天后可分账
70 69
             if ($settlement_period_time >= $time) {
71
-                // 成本价
72
-                $cost = $value['cost'];
73
-                // 利润金额
74
-                $con_pri = $value['con_pri'];
75 70
                 $group_order = Db::name('store_group_order')->alias('sgo')->where('sgo.group_order_id', $value['group_order_id'])->find();
76 71
 
77
-                // @todo 预分账处理
78
-                $isSuccessed = $storeOrderRepository->prepareExecuteIndependentAccount($value, $group_order, $merchant, $value['cost'], $value['con_pri']);
72
+                // 分账账号 496:表示{会员专区}账户,精彩生活区分账到{会员专区}账户
73
+                $independentAccount = $storeOrderRepository->getIndependentAccount($value, $group_order, 496, $merchant['hf_member_id']);
74
+
75
+                // @todo 预分账处理 496:表示{会员专区}账户,精彩生活区分账到{会员专区}账户,故:hf_platform_member_id 为入账账户
76
+                $isSuccessed = $storeOrderRepository->prepareExecuteIndependentAccount(
77
+                    $value, $group_order, $independentAccount['api_key'], $independentAccount['hf_platform_member_id'], '', $value['con_pri'], 0
78
+                );
79 79
 
80 80
                 Db::name("log")->insert(array('data' => '精彩生活区-订单分账:' . json_encode($value,JSON_FORCE_OBJECT)));//日志记录
81 81
 

+ 29 - 12
app/common/repositories/store/order/StoreOrderRepository.php

@@ -10500,17 +10500,15 @@ class StoreOrderRepository extends BaseRepository
10500 10500
     }
10501 10501
 
10502 10502
     /**
10503
-     * 预分账处理
10503
+     * 获取分账账号信息
10504 10504
      *
10505 10505
      * @param $order
10506 10506
      * @param $group_order
10507
-     * @param $merchant
10508
-     * @param $merchant_profit
10509
-     * @param $platform_profit
10510
-     * @return bool
10511
-     * @throws DbException
10507
+     * @param $dst_mer_id
10508
+     * @param $alhf_member_id
10509
+     * @return array
10512 10510
      */
10513
-    public function prepareExecuteIndependentAccount($order, $group_order, $merchant, $merchant_profit, $platform_profit, $platform_mer_id = 496)
10511
+    public function getIndependentAccount($order, $group_order, $dst_mer_id, $alhf_member_id)
10514 10512
     {
10515 10513
         if ($order['pay_type'] == 1) {
10516 10514
             //微信
@@ -10525,22 +10523,41 @@ class StoreOrderRepository extends BaseRepository
10525 10523
             }
10526 10524
 
10527 10525
             // 平台特殊账户
10528
-            $hf_platform_member_id = Db::name('merchant_member')->where(['mer_id' => $platform_mer_id, 'type' => $type])->value('member_id');
10526
+            $hf_platform_member_id = Db::name('merchant_member')->where(['mer_id' => $dst_mer_id, 'type' => $type])->value('member_id');
10529 10527
         } else {
10530 10528
             //支付宝
10531
-            $hf_member_id = $merchant['hf_member_id'];
10529
+            $hf_member_id = $alhf_member_id;
10532 10530
             $api_key = 'api_live_b23537ed-64b8-45d6-832e-fb228aa9e0a9';
10533 10531
 
10534 10532
             // 平台特殊账户
10535
-            $hf_platform_member_id = Db::name('merchant_member')->where(['mer_id' => $platform_mer_id, 'type' => 1])->value('member_id');
10533
+            $hf_platform_member_id = Db::name('merchant_member')->where(['mer_id' => $dst_mer_id, 'type' => 1])->value('member_id');
10536 10534
         }
10537 10535
 
10536
+        return compact('hf_member_id',  'hf_platform_member_id', 'api_key');
10537
+    }
10538
+
10539
+    /**
10540
+     * 预分账处理
10541
+     *
10542
+     * @param $order
10543
+     * @param $group_order
10544
+     * @param $api_key
10545
+     * @param $hf_member_id
10546
+     * @param $hf_platform_member_id
10547
+     * @param $merchant_profit
10548
+     * @param $platform_profit
10549
+     * @param $dst_mer_id
10550
+     * @return bool
10551
+     * @throws DbException
10552
+     */
10553
+    public function prepareExecuteIndependentAccount($order, $group_order, $api_key, $hf_member_id, $hf_platform_member_id, $merchant_profit, $platform_profit, $dst_mer_id = 0)
10554
+    {
10538 10555
         $members[] = [
10539 10556
             'member_id' => $hf_member_id,
10540 10557
             'amount' => $merchant_profit,
10541 10558
             'fee_flag' => 'Y'
10542 10559
         ];
10543
-        if ($platform_profit > 0) {
10560
+        if (!empty($hf_platform_member_id) && ($platform_profit > 0)) {
10544 10561
             $members[] = [
10545 10562
                 'member_id' => $hf_platform_member_id,
10546 10563
                 'amount' => $platform_profit,
@@ -10559,7 +10576,7 @@ class StoreOrderRepository extends BaseRepository
10559 10576
         $params['api_key'] = $api_key;
10560 10577
 
10561 10578
         // 记录分账订单
10562
-        $this->recordIndependentAccount($order['mer_id'], $platform_mer_id, $group_order, $requestId, $merchant_profit, $platform_profit);
10579
+        $this->recordIndependentAccount($order['mer_id'], $dst_mer_id, $group_order, $requestId, $merchant_profit, $platform_profit);
10563 10580
 
10564 10581
         Db::name("log")->insert(array('data' => '汇付分账参数-----' . json_encode($params, JSON_FORCE_OBJECT)));//日志记录
10565 10582
         $res = $this->payment_confirm_create_traits($params);