yingzi 1 year ago
parent
commit
3dff08d39c
2 changed files with 64 additions and 122 deletions
  1. 7 1
      Dao/CommissionPartner/DNewAgentDetail.Class.php
  2. 57 121
      Util/Common/AgentTools.Class.php

+ 7 - 1
Dao/CommissionPartner/DNewAgentDetail.Class.php

@@ -33,7 +33,13 @@ class DNewAgentDetail extends BaseDao
             "time",
             "expand",
             "purchaseNo",
-            "shopId"
+            "shopId",
+            "level",
+            "pmoney",
+            "lastShopId",
+            "lastCommission",
+            "lastPer",
+            "sourceShopLevel"
         ];
         $this->_readonly = ['id'];
         parent::__construct($serviceDB);

+ 57 - 121
Util/Common/AgentTools.Class.php

@@ -760,7 +760,8 @@ class AgentTools{
         }
         //从最底层开始更新等级
         $pathData = array_reverse(explode(",", trim($shopData["parentPath"])));
-        $perData=[];
+        $calcData=[];
+        $directorId = 0;//第一个董事店铺的id
         for($i=0;$i<count($pathData);$i++){
             if(empty($pathData[$i])){
                 continue;
@@ -769,6 +770,10 @@ class AgentTools{
             if(empty($pData) || empty($pData["agentId"])){
                 continue;
             }
+            $pAgentData = $this->dbNewAgent->get(["id"=>$pData["agentId"]]);
+            if(empty($pAgentData)){
+                continue;
+            }
             $per = 0;
             if($pData["level"]==3){
                 $per = empty($this->agentSettingData["threePer"])?0:$this->agentSettingData["threePer"];
@@ -778,134 +783,67 @@ class AgentTools{
                     $per = empty($this->agentSettingData[$perKey]) ? 0 : $this->agentSettingData[$perKey];
                 }
             }
-            if($per>0){
-                $pmoney = bcmul($purchaseMoney,$per,2);
-                
+            //佣金本金
+            $pmoney = bcmul($purchaseMoney,$per,2);
+            if($pmoney<=0){
+                continue;
             }
-            
-            
-            
-            
-            
-            
-            
-            
-            
-            
-            
-            
-            
-        }
-        
-        
-        
-        
-        
-        
-        
-        
-        
-        
-        
-        
-        
-        
-        
-        
-        
-        
-        
-        //先计算父级门店
-        $twoShopData = $this->dbShop->get(["id"=>$shopData["pTwoShopId"],"enterpriseId"=>$this->enterpriseId,"deleteStatus"=>5]);
-        if(!empty($twoShopData) && !empty($twoShopData["agentId"])){
-            $twoAgentData = $this->dbNewAgent->get(["id"=>$twoShopData["agentId"]]);
-            if(!empty($twoAgentData)){
-                //董事
-                $twoPer=0;
-                if($twoShopData["level"]==3){
-                    $twoPer = empty($this->agentSettingData["threePer"])?0:$this->agentSettingData["threePer"];
-                }else{
-                    $twoPerKey = $levelAr[(int)$twoShopData["level"]].ucfirst($levelAr[(int)$shopData["level"]])."Per";
-                    var_dump($twoPerKey);
-                    if(!empty($twoPerKey)){
-                        $twoPer = empty($this->agentSettingData[$twoPerKey]) ? 0 : $this->agentSettingData[$twoPerKey];
-                    }
-                }
-                if(!empty($twoPer) && $twoPer>0 && $twoPer<1){
-                    //记录收益
-                    $data[]=[
-                        "purchaseMoney"=>$purchaseMoney,//采购单支付金额
-                        "calcMoney"=>$purchaseMoney,//佣金计算金额
-                        "sourceShopId"=>$purchaseShopId,//来源门店id
-                        "agentId"=>$twoAgentData["id"],//收钱门店代理账号id
-                        "shopId"=>$twoShopData["id"],//收钱门店id
-                        "customerId"=>$twoAgentData["customerId"],//收钱门店代理
-                        "userCenterId"=>$twoAgentData["userCenterId"],//收钱门店代理
-                        "commission"=>bcmul($purchaseMoney,$twoPer,2),//佣金金额
-                        "per"=>$twoPer,//佣金比例
-                        "type"=>0,
-                        "purchaseId"=>$purchaseData["id"],
-                        "purchaseNo"=>$purchaseData["no"],
-                        "status"=>0,//暂时不需要
-                        "title"=>"门店采购单分佣",
-                        "isUpgrade"=>0,
-                        "mono"=>"",
-                        "time"=>$nowTime,
-                    ];
-                }
+            if($directorId>0 && $pData["level"]==3){
+                continue;
             }
-        }
-        //再计算爷爷级门店
-        if(!empty($shopData["pOneShopId"])){
-            $oneShopData = $this->dbShop->get(["id"=>$shopData["pOneShopId"],"enterpriseId"=>$this->enterpriseId,"deleteStatus"=>5]);
-            if(!empty($oneShopData) && !empty($oneShopData["agentId"])){
-                $oneAgentData = $this->dbNewAgent->get(["id"=>$oneShopData["agentId"]]);
-                if(!empty($oneAgentData)){
-                    //董事
-                    $onePer=0;
-                    if($oneShopData["level"]==3){
-                        $onePer = empty($this->agentSettingData["threePer"])?0:$this->agentSettingData["threePer"];
-                    }else{
-                        $onePerKey = $levelAr[(int)$oneShopData["level"]].ucfirst($levelAr[(int)$shopData["level"]])."Per";
-                        if(!empty($onePerKey)){
-                            $onePer = empty($this->agentSettingData[$onePerKey]) ? 0 : $this->agentSettingData[$onePerKey];
-                        }
-                    }
-                    if(!empty($onePer) && $onePer>0 && $onePer<1){
-                        //记录收益
-                        $data[]=[
-                            "purchaseMoney"=>$purchaseMoney,//采购单支付金额
-                            "calcMoney"=>$purchaseMoney,//佣金计算金额
-                            "sourceShopId"=>$purchaseShopId,//来源门店id
-                            "agentId"=>$oneAgentData["id"],//收钱门店代理账号id
-                            "shopId"=>$oneShopData["id"],//收钱门店id
-                            "customerId"=>$oneAgentData["customerId"],//收钱门店代理
-                            "userCenterId"=>$oneAgentData["userCenterId"],//收钱门店代理
-                            "commission"=>bcmul($purchaseMoney,$onePer,2),//佣金金额
-                            "per"=>$onePer,//佣金比例
-                            "type"=>0,
-                            "purchaseId"=>$purchaseData["id"],
-                            "purchaseNo"=>$purchaseData["no"],
-                            "status"=>0,//暂时不需要
-                            "title"=>"门店采购单分佣",
-                            "isUpgrade"=>0,
-                            "mono"=>"",
-                            "time"=>$nowTime,
-                        ];
-                    }
+            $item = [
+                "purchaseMoney"=>$purchaseMoney,//采购单支付金额
+                "calcMoney"=>$purchaseMoney,//佣金计算金额
+                "sourceShopId"=>$purchaseShopId,//来源门店id
+                "shopId"=>$pData["id"],//收钱门店id
+                "agentId"=>$pAgentData["id"],//收钱门店代理账号id
+                "customerId"=>$pAgentData["customerId"],//收钱门店代理
+                "userCenterId"=>$pAgentData["userCenterId"],//收钱门店代理
+                "commission"=>$pmoney,//佣金金额
+                "per"=>$per,//佣金比例
+                "type"=>0,
+                "purchaseId"=>$purchaseData["id"],
+                "purchaseNo"=>$purchaseData["no"],
+                "status"=>0,//暂时不需要
+                "title"=>"门店采购单分佣",
+                "isUpgrade"=>0,
+                "mono"=>"",
+                "time"=>$nowTime,
+                //新字段
+                "level"=>$pData["level"],
+                "pmoney"=>$pmoney,//原佣金金额
+                "lastShopId"=>0,//上一个店铺id
+                "lastCommission"=>0,//上一个计算的佣金金额
+                "lastPer"=>0,//上一个佣金百分比
+                "sourceShopLevel"=>$shopData["level"]
+            ];
+            if($pData["level"]==3){
+                $calcData[]=$item;
+                $directorId = $pData["id"];
+                continue;
+            }
+            for($m=count($calcData)-1;$m>=0;$m--){
+                if($calcData[$m]["level"]<3){
+                    $item["commission"]     = $pmoney - $calcData[$m]["commission"];
+                    $item["lastShopId"]     = $calcData[$m]["shopId"];
+                    $item["lastCommission"] = $calcData[$m]["commission"];
+                    $item["lastPer"]        = $calcData[$m]["per"];
+                    break;
                 }
             }
+            if($item["commission"]<=0){
+                continue;
+            }
+            $calcData[]=$item;
         }
-        if(empty($data)){
-            return ["code"=>-1,"msg"=>"父级绑定或者配置错误导致数据更新失败"];
+        if(empty($calcData)){
+            return ["code"=>-1,"msg"=>"没有符合佣金条件的上级"];
         }
-        
-
         $num=0;
         //开启事务
         $this->dbNewAgentDetail->beginTransaction();
         $lms = "";
-        foreach($data as $k=>$v){
+        foreach($calcData as $k=>$v){
             $count = $this->dbNewAgentDetail->count(["purchaseId"=>$v["purchaseId"],"agentId"=>$v["agentId"]]);
             if($count>0){
                 $lms.="当前采购单已计算佣金[{$v['purchaseId']}];";
@@ -929,8 +867,6 @@ class AgentTools{
                 $lms.="门店不存在[{$v['purchaseId']}];";
                 break;
             }
-            
-            
             $update = [
                 'balance'    => bcadd($agentItem['balance'], $v["commission"], 2),
                 'totalMoney' => bcadd($agentItem['totalMoney'], $v["commission"], 2),