sunxiaobiao vor 1 Jahr
Ursprung
Commit
c645baef30
2 geänderte Dateien mit 153 neuen und 34 gelöschten Zeilen
  1. 0 29
      app/command/CommissionCronTask.php
  2. 153 5
      app/controller/api/cron/CommissionCron.php

+ 0 - 29
app/command/CommissionCronTask.php

@@ -3,39 +3,10 @@ declare (strict_types=1);
3 3
 
4 4
 namespace app\command;
5 5
 
6
-use AdaPaySdk\Drawcash;
7
-use AdaPaySdk\PaymentConfirm;
8
-use AdaPaySdk\SettleAccount;
9
-use app\common\model\merchant\order\OrderMerchant;
10
-use app\common\model\store\order\StoreOrder;
11
-use app\common\model\user\User;
12
-use app\common\model\user\UserCertification;
13
-use app\common\repositories\merchant\order\OrderGzcRepository;
14
-use app\common\repositories\merchant\order\OrderMerchantRepository;
15
-use app\common\repositories\store\order\StoreOrderStatusRepository;
16
-use app\common\repositories\store\order\StoreRefundOrderRepository;
17
-use app\common\repositories\store\product\ProductRepository;
18
-use app\common\repositories\user\UserBillRepository;
19
-use app\common\repositories\user\UserRepository;
20
-use app\common\repositories\user\UserThirdRepository;
21
-use app\controller\api\Common;
22 6
 use app\controller\api\cron\CommissionCron;
23
-use app\controller\api\merchant\sxy\Access;
24
-use app\controller\api\store\product\Jd;
25
-use app\controller\api\util\Signature;
26
-use app\traits\GongApiRequest;
27
-use app\traits\GzcApiRequest;
28
-use app\traits\ShouxinyiApiRequest;
29
-use Carbon\Carbon;
30
-use think\App;
31 7
 use think\console\Command;
32 8
 use think\console\Input;
33 9
 use think\console\Output;
34
-use think\Exception;
35
-use think\facade\Db;
36
-use think\facade\Log;
37
-use app\common\repositories\store\order\StoreOrderRepository;
38
-use think\facade\Request;
39 10
 
40 11
 /**
41 12
  * Class PensionTasks

+ 153 - 5
app/controller/api/cron/CommissionCron.php

@@ -551,34 +551,174 @@ where o.paid=1 and o.is_settlement=1 and settlement_time between '$this->startMo
551 551
         return app('json')->success('操作成功');
552 552
     }
553 553
 
554
+    //定时任务写日账单,月账单
555
+    public function cron($day,$mer_id=496)
556
+    {
557
+        //        $day=date('Y-m-d', strtotime('-1 days'));
558
+        //        $enterprise_bill_item=[];
559
+        //        $order= $order_query->select();
560
+        //        foreach ($order as $v) {
561
+        //            $arr = [];
562
+        //            $arr['mer_id'] = $mer_id;
563
+        //            $arr['transaction_time'] = $v['pay_time'];
564
+        //            $arr['money'] = $v['pay_price'];
565
+        //            $arr['uid'] = $v['uid'];
566
+        //            $arr['order_sn'] = $v['order_sn'];
567
+        //            $arr['type'] = 1;
568
+        //            $enterprise_bill_item[] = $arr;
569
+        //        }
570
+        //        if ($enterprise_bill_item) {
571
+        //            Db::name('enterprise_bill_item')->insertAll($enterprise_bill_item);
572
+        //        }
573
+
574
+        Db::name('enterprise_day_bill')->where('mer_id', $mer_id)->where('day', $day)->delete();
575
+
576
+        $day_flow_query = Db::name('enterprise_platform_income_detail')
577
+            ->where('mer_id', $mer_id)
578
+            ->when($mer_id, function ($query) use ($mer_id) {
579
+                $query->where('deal_type', -1);//没有流水
580
+            }, function ($query) use ($mer_id) {
581
+                $query->whereIn('deal_type', [3]);
582
+            })
583
+
584
+            ->where('deal_type', 4)
585
+            ->where('pm', 1);
586
+        $day_flow_query_month = clone $day_flow_query;
587
+        // 日订单流水
588
+        $day_flow = $day_flow_query->where("DATE(deal_time) = '" . $day . "'")->sum('money');
589
+
590
+        $accounting_query = Db::name('enterprise_platform_income_detail')
591
+            ->where('mer_id', $mer_id)
592
+            ->when($mer_id, function ($query) use ($mer_id) {
593
+                $query->where('deal_type', 1);
594
+            }, function ($query) use ($mer_id) {
595
+                $query->whereIn('deal_type', [6,7]);
596
+            })
597
+            ->where('pm', 1);
598
+        $accounting_query_month = clone $accounting_query;
599
+        // 入账金额
600
+        $day_accounting = $accounting_query->where("DATE(deal_time) = '" . $day . "'")->sum('money');
601
+
602
+        $expenditure_query = Db::name('enterprise_platform_income_detail')
603
+            ->when($mer_id, function ($query) use ($mer_id) {
604
+                $query->where('mer_id', $mer_id)
605
+                    ->where('deal_type', -1);//没有流水
606
+            }, function ($query) use ($mer_id) {
607
+                $query->whereIn('deal_type', [4,5]);
608
+            });
609
+        $expenditure_query_month = clone $expenditure_query;
610
+        // 日支出总额
611
+        $day_expenditure = 0;
612
+        if($mer_id==0){
613
+            $day_expenditure = $expenditure_query->where("DATE(deal_time) = '" . $day . "'")
614
+                ->whereIn('deal_type', [4,5])
615
+                ->where('mer_id',0)
616
+                ->sum('money');
617
+        }
618
+
619
+        $insert = [];
620
+        $insert['mer_id'] = $mer_id;
621
+        $insert['day'] = $day;
622
+        $insert['flow'] = $day_flow;
623
+        $insert['expenditure'] = $day_expenditure;
624
+        $insert['accounting'] = $day_accounting;
625
+
626
+        $previousDate = date('Y-m-d', strtotime($day . ' -1 day'));
627
+        $balance_zuotian = Db::name('enterprise_day_bill')->where('day', $previousDate)->where('mer_id', $mer_id)->value('balance');
628
+        $balance = $balance_zuotian ? $balance_zuotian : 0;
629
+
630
+        if($mer_id){
631
+            $tixian = Db::name('user_extract')->where("DATE(status_time) = '" . $day . "'")->where('mer_id', $mer_id)->where('status', 1)->sum('extract_price');
632
+            $service_money = Db::name('user_extract')->where("DATE(status_time) = '" . $day . "'")->where('mer_id', $mer_id)->where('status', 1)->sum('service_money');
633
+            $tixian -= $service_money;
634
+            $ylj = Db::name('user_bill')->where('mer_id', $mer_id)->where('status', 1)->where("DATE(take_time) = '" . $day . "'")->sum('number');
635
+            $insert['balance'] = $day_accounting - $tixian + $balance - $ylj;
636
+        }else{
637
+            $tixian =  Db::name('enterprise_platform_income_detail')
638
+                ->where("deal_time >'$day'")
639
+                ->where("deal_time <'$day 23:59:59'")
640
+                ->where('deal_type', 5)
641
+                ->sum('money');
642
+            $insert['balance'] = $day_accounting - $tixian + $balance ;
643
+        }
644
+
645
+
646
+
647
+        Db::name('enterprise_day_bill')->insertGetId($insert);
648
+
649
+
650
+        $month_list[] = date('Y-m', strtotime('-4 month'));
651
+        $month_list[] = date('Y-m', strtotime('-3 month'));
652
+        $month_list[] = date('Y-m', strtotime('-2 month'));
653
+        $month_list[] = date('Y-m', strtotime('-1 month'));
654
+        $month_list[] = date('Y-m');
655
+        foreach ($month_list as $month) {
656
+            Db::name('enterprise_month_bill')->where('mer_id',$mer_id)->where('month', $month)->delete();
657
+
658
+            $flow_query_month_clone = clone $day_flow_query_month;
659
+            $accounting_query_month_clone = clone $accounting_query_month;
660
+            $expenditure_query_month_clone = clone $expenditure_query_month;
661
+
662
+            $startDate = date('Y-m-01 00:00:00', strtotime($month));
663
+            $endDate = date("Y-m-d 00:00:00", strtotime('+1 month', strtotime($month)));
664
+
665
+            // 月订单流水
666
+            $money_flow = $flow_query_month_clone->whereBetween('deal_time', [$startDate, $endDate])->sum('money');
667
+
668
+            // 月入账总额
669
+            $money_accounting = $accounting_query_month_clone->whereBetween('deal_time', [$startDate, $endDate])->sum('money');
670
+
671
+            // 月支出总额
672
+            $money_expenditure = $expenditure_query_month_clone->whereBetween('deal_time', [$startDate, $endDate])->sum('money');
673
+
674
+            $insert2 = [];
675
+            $insert2['month'] = $month;
676
+            $insert2['mer_id'] = $mer_id;
677
+            $insert2['flow'] = $money_flow;
678
+            $insert2['expenditure'] = $money_expenditure;
679
+            $insert2['accounting'] = $money_accounting;
680
+            Db::name('enterprise_month_bill')->insertGetId($insert2);
681
+        }
682
+
683
+    }
684
+
554 685
     /**
555 686
      * 定时获取 交易类型 1订单入账 2佣金提现支出 3养老金(红积分兑换订单)
556 687
      */
557 688
     public function platform_income_detail_get()
558 689
     {
559 690
 
691
+        // 1、获取所有 商户 商户名称非空, 状态为正常、商户状态为开启
560 692
         $merchant_list = Db::name('merchant')
561 693
             ->where('status', 1)
562 694
             ->where('mer_state', 1)
563 695
             ->where("mer_name  != ''")
564 696
             ->column('mer_id');
565
-        $Reconciliation = app()->make(Reconciliation::class);
566 697
 
567
-        $unique_list = [];
698
+        // 2、插入日账单、月账单
699
+        // $Reconciliation = app()->make(Reconciliation::class);
700
+        $Reconciliation = $this;
568 701
 
702
+        // 3、获取 已有的 平台收支明细,并创建 唯一的 key
703
+        $unique_list = [];
569 704
         $enterprise_platform_income_detail_list = Db::name('enterprise_platform_income_detail')
570 705
             ->select()
571 706
             ->toArray();
572 707
         foreach ($enterprise_platform_income_detail_list as $item) {
708
+            // deal_type : '交易类型 1自营订单入账(商户) 2佣金提现到账(商户) 3订单支付 4商户佣金提现 5用户佣金提现 6订单入账 7 创客自营订单让利 '
709
+            // user_extra_id : 提现表id
573 710
             $unique_list[] = $item['deal_type'] . $item['uid'] . $item['order_sn'] . $item['order_id'] . $item['user_extra_id'] . $item['mer_id'];
574 711
         }
575
-        foreach ($merchant_list as $mer_id) {
576 712
 
713
+        // 4、循环所有商户ID
714
+        foreach ($merchant_list as $mer_id) {
715
+            /** @var OrderMerchantRepository $OrderMerchantRepository */
577 716
             $OrderMerchantRepository = app()->make(OrderMerchantRepository::class);
578 717
 
579 718
             $is_first = empty($enterprise_platform_income_detail_list);
580 719
 
581 720
             // 1自营订单入账金额=已结算订单金额-运费-养老金-汇付通道服务费; 订单结算的时候才统计
721
+            // 查询 该商户 所有的 平台订单(已支付、平台订单、已结算)
582 722
             $store_order_list = Db::name('store_order')
583 723
                 ->where('mer_id', $mer_id)
584 724
                 ->where('paid', 1)
@@ -590,26 +730,31 @@ where o.paid=1 and o.is_settlement=1 and settlement_time between '$this->startMo
590 730
                 ->toArray();
591 731
 
592 732
             foreach ($store_order_list as $store_order) {
733
+                // 判断该笔订单是否已 存在 平台收支明细表 的记录中
593 734
                 if (in_array('1' . $store_order['uid'] . $store_order['order_sn'] . $store_order['order_id'] . '0' . $mer_id, $unique_list)) continue;
735
+                // 支付方式,有疑问??
594 736
                 $pay_type = $store_order['pay_type'];
595 737
                 if (!in_array($pay_type, [0, 1])) continue;
596
-
738
+                // 用户订单表
597 739
                 $group_order = Db::name('store_group_order')
598 740
                     ->alias('sgo')
599 741
                     ->where('sgo.group_order_id', $store_order['group_order_id'])
600 742
                     ->find();
601 743
                 if (empty($group_order)) continue;
602
-
744
+                // 获取 店铺奖金设置
603 745
                 $shop_bonus_setting = merchantConfig($store_order['mer_id'], 'shop_bonus_setting') ?: [
604 746
                     'shop_bonus_rate' => 30,
605 747
                     'yanglao_rate'    => 5,
606 748
                 ];
749
+                // 获取这笔订单发放的养老金 TODO 这里查询应该不对,有可能查询出多列
607 750
                 $yanglao = Db::name('user_bill')
608 751
                     ->where('order_sn', $store_order['order_sn'])
609 752
                     ->where('commission_type', 5)
610 753
                     ->value('number');
611 754
                 $ru_money = 0;
755
+                // 如果pay_type 不是 0 ??
612 756
                 if ($pay_type != 0) {
757
+                    // 根据订单编号 查询 订单分账表 状态是 已分账成功的数据
613 758
                     $store_split_order = Db::name('store_split_order')
614 759
                         ->where('group_order_id', $group_order['group_order_sn'])
615 760
                         ->where('status', 1)
@@ -626,6 +771,7 @@ where o.paid=1 and o.is_settlement=1 and settlement_time between '$this->startMo
626 771
                         //支付宝
627 772
                         $api_key = 'api_live_b23537ed-64b8-45d6-832e-fb228aa9e0a9';
628 773
                     }
774
+                    // 汇付支付id
629 775
                     $params['payment_confirm_id'] = $store_split_order['hf_pay_id'];
630 776
                     $params['api_key'] = $api_key;
631 777
                     //查询支付确认对象
@@ -634,12 +780,14 @@ where o.paid=1 and o.is_settlement=1 and settlement_time between '$this->startMo
634 780
                     Db::name("log")->insert(array('data' => '汇付查询支付确认对象返回-----' . json_encode($res)));//日志记录
635 781
                     // 获取分账后一个工作日时间
636 782
                     $close_time = $this->getNextWorkday(date('Y-m-d H:i:s', strtotime('+1 days', strtotime($store_order['pay_time']))));
783
+                    // 如果成功查询到 分账信息 则更新订单的 汇付通道服务费 汇付分账成功时间
637 784
                     if (isset($res['status'])) {
638 785
                         Db::name('store_order')->where('order_id', $store_order['order_id'])->update([
639 786
                             'hf_fee_amt'    => $res['fee_amt'],
640 787
                             'hf_clear_time' => $close_time
641 788
                         ]);
642 789
                     }
790
+                    //
643 791
                     if (isset($res['status']) && time() > strtotime($close_time)) {
644 792
                         if ($store_order['mer_id'] == 496) {
645 793
                             $ru_money = $store_order['pay_price'] - $yanglao - $res['fee_amt'];