stanley-king 2 éve
szülő
commit
054df6dcac
2 módosított fájl, 38 hozzáadás és 27 törlés
  1. 37 26
      helper/statistics/refill_balance.php
  2. 1 1
      test/TestRefillBalance.php

+ 37 - 26
helper/statistics/refill_balance.php

@@ -57,7 +57,7 @@ class refill_balance
         return [];
     }
 
-    public function stat_all($start, $end)
+    public function stat_all($end)
     {
         $mchids = function ()
         {
@@ -78,7 +78,6 @@ class refill_balance
         };
         
         $types = ['system','merchant','provider'];
-        //time types == 'order_time','notify_time'
         foreach ($types as $type)
         {
             if ($type == 'system') {
@@ -94,7 +93,7 @@ class refill_balance
                 $time_types = $this->find_timetypes($type,$cid);
                 foreach ($time_types as $time_type)
                 {
-                    $parent_id = $this->find_parent($type, $cid, $start, $time_type);
+                    [$parent_id,$start] = $this->find_parent($type, $cid, $time_type);
                     $ret = $this->add_balance($type, $cid, $start, $end, $time_type, $parent_id);
                     if(!$ret) {
                         Log::record("type=$type cid=$cid, time_type=$time_type fail",Log::ERR);
@@ -104,17 +103,17 @@ class refill_balance
         }
     }
 
-    private function find_parent($type, $cid, $start, $time_type)
+    private function find_parent($type, $cid, $time_type)
     {
         $mod = Model('refill_balance');
         $cond = ['type' => $type, 'cid' => $cid, 'time_type' => $time_type, 'confirmed' => 1];
-        $cond['end_stamp'] = ['elt', $start];
 
-        $item = $mod->field('balance_id')->where($cond)->order('end_stamp desc')->find();
-        return intval($item['balance_id']);
+        $item = $mod->field('balance_id,end_stamp')->where($cond)->order('end_stamp desc')->find();
+
+        return [intval($item['balance_id']),intval($item['end_stamp'])];
     }
 
-    private function refill_time_finder($type, $cid, $start)
+    private function refill_time_finder($type, $cid, $start,$end)
     {
         if ($start != 0) {
             return $start;
@@ -122,22 +121,28 @@ class refill_balance
 
         if ($type == 'system') {
             $mod_refill = Model('refill_order');
-            $item = $mod_refill->table('refill_order')->field('order_id,order_time')->order('order_id asc')->find();
+            $item = $mod_refill->table('refill_order')->field('order_id,order_time')
+                ->where(['order_time' => ['lt', $end]])
+                ->order('order_id asc')->find();
             $time = $item['order_time'] ?? time();
         } elseif ($type == 'merchant') {
             $mod_refill = Model('refill_order');
-            $item = $mod_refill->table('refill_order')->field('order_id,order_time')->where(['mchid' => $cid])->order('order_id asc')->find();
+            $item = $mod_refill->table('refill_order')->field('order_id,order_time')
+                ->where(['mchid' => $cid, 'order_time' => ['lt', $end]])
+                ->order('order_id asc')->find();
             $time = $item['order_time'] ?? time();
         } else {
             $mod_refill = Model('vr_order');
-            $item = $mod_refill->table('vr_order')->field('order_id,add_time')->where(['store_id' => $cid])->order('order_id asc')->find();
+            $item = $mod_refill->table('vr_order')->field('order_id,add_time')
+                ->where(['store_id' => $cid,'add_time' => ['lt', $end]])
+                ->order('order_id asc')->find();
             $time = $item['order_time'] ?? time();
         }
 
         return $time;
     }
 
-    private function merchant_paytime_finder($cid, $start)
+    private function merchant_paytime_finder($cid, $start, $end)
     {
         if ($start != 0) {
             return $start;
@@ -147,22 +152,23 @@ class refill_balance
         if ($cid == 0)
         {
             $item = $mod_pay->table('refill_evidence')
-                ->field('min(add_time) as add_time')
+                ->field('min(add_time) as madd_time')
+                ->where(['add_time' => ['lt', $end]])
                 ->find();
         }
         else
         {
             $item = $mod_pay->table('refill_evidence')
-                ->field('min(add_time) as add_time')
-                ->where(['mchid' => $cid])
+                ->field('min(add_time) as madd_time')
+                ->where(['mchid' => $cid,'add_time' => ['lt', $end]])
                 ->find();
         }
 
-        $time = $item['add_time'] ?? time();
+        $time = $item['madd_time'] ?? time();
         return $time;
     }
 
-    private function provider_paytime_finder($cid, $start)
+    private function provider_paytime_finder($cid, $start,$end)
     {
         if ($start != 0) {
             return $start;
@@ -172,24 +178,25 @@ class refill_balance
         if ($cid == 0)
         {
             $item = $mod_pay->table('provider_amount')
-                ->field('min(add_time) as add_time')
+                ->field('min(add_time) as madd_time')
+                ->where(['add_time' => ['lt', $end]])
                 ->find();
         }
         else
         {
             $item = $mod_pay->table('provider_amount')
-                ->field('min(add_time) as add_time')
-                ->where(['provider_id' => $cid])
+                ->field('min(add_time) as madd_time')
+                ->where(['provider_id' => $cid,'add_time' => ['lt', $end]])
                 ->find();
         }
 
-        $time = $item['add_time'] ?? time();
+        $time = $item['madd_time'] ?? time();
         return $time;
     }
 
     private function balance_data($type, $cid, $start, $end, $time_type, $parent_balance, $remark)
     {
-        $refill_time = $this->refill_time_finder($type, $cid, $start);
+        $refill_time = $this->refill_time_finder($type, $cid, $start,$end);
         $order_stat = $this->order_stat($type, $cid, $refill_time, $end, $time_type);
 
         $transfer_calcer = function ($detail,$types = [])
@@ -208,10 +215,10 @@ class refill_balance
         };
 
         if ($type == 'system') {
-            $merchant_time = $this->merchant_paytime_finder($cid, $start);
+            $merchant_time = $this->merchant_paytime_finder($cid, $start,$end);
             $transfer_detail = $this->merchant_evidence_stat($cid, $merchant_time, $end);
 
-            $provider_time = $this->provider_paytime_finder($cid, $start);
+            $provider_time = $this->provider_paytime_finder($cid, $start,$end);
             $out = $this->provider_amount_stat(0, $provider_time, $end);
             $cname = 'system';
 
@@ -219,7 +226,7 @@ class refill_balance
             $in = $transfer_calcer($transfer_detail);
             $balance = ncPriceFormat($in - $out);
         } elseif ($type == 'merchant') {
-            $pay_time = $this->merchant_paytime_finder($cid, $start);
+            $pay_time = $this->merchant_paytime_finder($cid, $start, $end);
             $transfer_detail = $this->merchant_evidence_stat($cid, $pay_time, $end);
             $out = "0.0000";
             $cname = $this->mMerchantNames[$cid];
@@ -229,7 +236,7 @@ class refill_balance
         } else {
             $transfer_detail = [];
             $pid = $this->mStoreidPID[$cid];
-            $pay_time = $this->provider_paytime_finder($pid, $start);
+            $pay_time = $this->provider_paytime_finder($pid, $start,$end);
             $out = $this->provider_amount_stat($pid, $pay_time, $end);
             $cname = $this->mProviderNames[$cid];
 
@@ -242,6 +249,10 @@ class refill_balance
             $start = strtotime(date('Y-m-d', $time));
         }
 
+        if($start === $end) {
+            return [];
+        }
+
         $accumuter = function ($parent_id)
         {
             if ($parent_id > 0) {

+ 1 - 1
test/TestRefillBalance.php

@@ -30,7 +30,7 @@ class TestRefillBalance extends TestCase
     public function testInitBalance()
     {
         $refill_balance = new statistics\refill_balance();
-        $refill_balance->stat_all(0, strtotime('2022-05-24'));
+        $refill_balance->stat_all(strtotime('2022-05-24'));
     }
 
     public function testFirstDay()