stanley-king 2 yıl önce
ebeveyn
işleme
b408d68ac8
2 değiştirilmiş dosya ile 182 ekleme ve 7 silme
  1. 89 6
      helper/statistics/refill_balance.php
  2. 93 1
      test/TestRefillBalance.php

+ 89 - 6
helper/statistics/refill_balance.php

@@ -89,9 +89,81 @@ class refill_balance
         return intval($item['balance_id']);
     }
 
+    private function refill_time_finder($type, $cid, $start)
+    {
+        if ($start != 0) {
+            return $start;
+        }
+
+        if ($type == 'system') {
+            $mod_refill = Model('refill_order');
+            $item = $mod_refill->table('refill_order')->field('order_id,order_time')->order('order_id asc')->find();
+        } 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();
+        } 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();
+        }
+
+        $time = $item['order_time'] ?? time();
+        return $time;
+    }
+
+    private function merchant_paytime_finder($cid, $start)
+    {
+        if ($start != 0) {
+            return $start;
+        }
+
+        $mod_pay = Model();
+        if ($cid == 0)
+        {
+            $item = $mod_pay->table('refill_evidence')
+                ->field('min(add_time) as add_time')
+                ->find();
+        }
+        else
+        {
+            $item = $mod_pay->table('refill_evidence')
+                ->field('min(add_time) as add_time')
+                ->where(['mchid' => $cid])
+                ->find();
+        }
+
+        $time = $item['add_time'] ?? time();
+        return $time;
+    }
+
+    private function provider_paytime_finder($cid, $start)
+    {
+        if ($start != 0) {
+            return $start;
+        }
+
+        $mod_pay = Model();
+        if ($cid == 0)
+        {
+            $item = $mod_pay->table('provider_amount')
+                ->field('min(add_time) as add_time')
+                ->find();
+        }
+        else
+        {
+            $item = $mod_pay->table('provider_amount')
+                ->field('min(add_time) as add_time')
+                ->where(['provider_id' => $cid])
+                ->find();
+        }
+
+        $time = $item['add_time'] ?? time();
+        return $time;
+    }
+
     private function balance_data($type, $cid, $start, $end, $time_type, $parent_balance, $remark)
     {
-        $order_stat = $this->order_stat($type, $cid, $start, $end, $time_type);
+        $refill_time = $this->refill_time_finder($type, $cid, $start);
+        $order_stat = $this->order_stat($type, $cid, $refill_time, $end, $time_type);
 
         $transfer_calcer = function ($detail,$types = [])
         {
@@ -109,14 +181,19 @@ class refill_balance
         };
 
         if ($type == 'system') {
-            $transfer_detail = $this->merchant_evidence_stat(0, $start, $end);
-            $out = $this->provider_amount_stat(0, $start, $end);
+            $merchant_time = $this->merchant_paytime_finder($cid, $start);
+            $transfer_detail = $this->merchant_evidence_stat($cid, $merchant_time, $end);
+
+            $provider_time = $this->provider_paytime_finder(0, $start);
+            $out = $this->provider_amount_stat(0, $provider_time, $end);
             $cname = 'system';
 
+            $pay_time = min($merchant_time,$provider_time);
             $in = $transfer_calcer($transfer_detail);
             $balance = ncPriceFormat($in - $out);
         } elseif ($type == 'merchant') {
-            $transfer_detail = $this->merchant_evidence_stat($cid, $start, $end);
+            $pay_time = $this->merchant_paytime_finder($cid, $start);
+            $transfer_detail = $this->merchant_evidence_stat($cid, $pay_time, $end);
             $out = "0.0000";
             $cname = $this->mMerchantNames[$cid];
 
@@ -125,14 +202,20 @@ class refill_balance
         } else {
             $transfer_detail = [];
             $pid = $this->mStoreidPID[$cid];
-            $out = $this->provider_amount_stat($pid, $start, $end);
+            $pay_time = $this->provider_paytime_finder($pid, $start);
+            $out = $this->provider_amount_stat($pid, $pay_time, $end);
             $cname = $this->mProviderNames[$cid];
 
             $in = $transfer_calcer($transfer_detail);
             $balance = ncPriceFormat($in - $order_stat['channel_amounts']);
         }
 
-        $accumuter = function ($parent_id) {
+        if($start == 0) {
+            $start = min($start,$pay_time);
+        }
+
+        $accumuter = function ($parent_id)
+        {
             if ($parent_id > 0) {
                 $item = Model('refill_balance')->getBalance(['balance_id' => $parent_id]);
             } else {

+ 93 - 1
test/TestRefillBalance.php

@@ -23,7 +23,7 @@ class TestRefillBalance extends TestCase
     public function testAddBalance()
     {
         $refill_balance = new statistics\refill_balance();
-        $refill_balance->add_balance('system', 0, 0, strtotime('2021-12-01'),'order_time');
+        $refill_balance->add_balance('system', 0, 0, strtotime('2021-12-01'), 'order_time');
     }
 
     //docker-compose run -d phpcli php /var/www/html/phpunit-9.2.5.phar --filter "/(TestRefillBalance::testInitBalance)( .*)?$/" --test-suffix TestRefillBalance.php /var/www/html/test
@@ -32,4 +32,96 @@ class TestRefillBalance extends TestCase
         $refill_balance = new statistics\refill_balance();
         $refill_balance->stat_all(0, strtotime('2022-05-24'));
     }
+
+    public function testFirstDay()
+    {
+        $refill_time = function ($type, $cid, $start)
+        {
+            if ($start != 0) {
+                return $start;
+            }
+            if ($type == 'system') {
+                $mod_refill = Model('refill_order');
+                $item = $mod_refill->table('refill_order')->field('order_id,order_time')->order('order_id asc')->find();
+            } 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();
+            } 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();
+            }
+
+            $time = $item['order_time'] ?? 0;
+            return $time;
+
+//            Log::record(date('Y-m-d', $time),Log::DEBUG);
+//            return strtotime(date('Y-m-d', $time));
+        };
+
+        $order_start = $refill_time('system', 0, 0);
+        $order_start = $refill_time('merchant', 1092, 0);
+        $order_start = $refill_time('provider', 49, 0);
+    }
+
+    public function testMerchantPaytime()
+    {
+        $refill_time = function ($type, $cid, $start)
+        {
+            if ($start != 0) {
+                return $start;
+            }
+
+            $mod_pay = Model();
+            if ($type == 'system')
+            {
+                $item = $mod_pay->table('refill_evidence')
+                    ->field('min(add_time) as add_time')
+                    ->find();
+            } elseif ($type == 'merchant') {
+                $item = $mod_pay->table('refill_evidence')
+                    ->field('min(add_time) as add_time')
+                    ->where(['mchid' => $cid])
+                    ->find();
+            }
+
+            $time = $item['add_time'] ?? time();
+            Log::record(date('Y-m-d', $time),Log::DEBUG);
+            return $time;
+        };
+
+        $order_start = $refill_time('system', 0, 0);
+        $order_start = $refill_time('merchant', 1092, 0);
+    }
+
+    public function testProviderPaytime()
+    {
+        $refill_time = function ($type, $cid, $start)
+        {
+            if ($start != 0) {
+                return $start;
+            }
+
+            $mod_pay = Model();
+            if ($type == 'system')
+            {
+                $item = $mod_pay->table('provider_amount')
+                    ->field('min(add_time) as add_time')
+                    ->find();
+            } elseif ($type == 'merchant') {
+                $item = $mod_pay->table('provider_amount')
+                    ->field('min(add_time) as add_time')
+                    ->where(['provider_id' => $cid])
+                    ->find();
+            }
+
+            $time = $item['add_time'] ?? time();
+            Log::record(date('Y-m-d', $time),Log::DEBUG);
+            return $time;
+        };
+
+        $order_start = $refill_time('system', 0, 0);
+        $order_start = $refill_time('provider', 49, 0);
+    }
+
+
 }