yingzi 2 years ago
parent
commit
6626ba19fb

+ 8 - 1
Controller/Shop/Shop.Class.php

@@ -45,6 +45,7 @@ class Shop extends BaseController
         if( empty($params) ){
             $this->sendOutput('参数为空', ErrorCode::$paramError );
         }
+        $shopId  = $this->request->param('request_id');
 
         $shopData = [
             'logo' => isset($params['logo']) ? $params['logo'] : '',
@@ -99,7 +100,7 @@ class Shop extends BaseController
         
         
         
-        //推荐代理设置
+        //门店负责人设置
         if(empty($params["agentCustomerId"])){
             $shopData["agentId"]=0;
             $shopData["agentCustomerId"]=0;
@@ -116,6 +117,12 @@ class Shop extends BaseController
         }
         
         
+        //父级门店设置
+        if(!empty($params["pTwoShopId"]) && !empty($shopId)){
+            $agentTool = new AgentTools($this->onlineEnterpriseId);
+            $agentTool->pushShop($params["pTwoShopId"], $shopId);
+        }
+        
         return $shopData;
     }
 

+ 1 - 0
Dao/CommissionPartner/DNewAgentBalanceDetail.Class.php

@@ -31,6 +31,7 @@ class DNewAgentBalanceDetail extends BaseDao
             "time",
             "expand",
             "detail_id",
+            "shop_id"
         ];
         $this->_readonly = ['id'];
         parent::__construct($serviceDB);

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

@@ -32,7 +32,8 @@ class DNewAgentDetail extends BaseDao
             "mono",
             "time",
             "expand",
-            "purchaseNo"
+            "purchaseNo",
+            "shopId"
         ];
         $this->_readonly = ['id'];
         parent::__construct($serviceDB);

+ 7 - 0
Dao/CommissionPartner/DNewAgentSetting.Class.php

@@ -24,6 +24,13 @@ class DNewAgentSetting extends BaseDao
             "is_open",
             "updateTime",
             "time",
+            "oneToTwoNum",
+            "twoToThreeNum",
+            "oneonePer",
+            "oneTwoPer",
+            "twoOnePer",
+            "twoTwoPer",
+            "threePer",
         ];
         $this->_readonly = ['id'];
         parent::__construct($serviceDB);

+ 4 - 1
Dao/Shop/DShop.Class.php

@@ -48,7 +48,10 @@ class DShop extends BaseDao
             "extends",
             "modelType",
             "agentId",
-            "agentCustomerId"
+            "agentCustomerId",
+            "level",
+            "pOneShopId",
+            "pTwoShopId"
         ];
         $this->_readonly = ['id'];
         $this->_create_autofill = [

+ 394 - 0
Util/Common/AgentTools.Class.php

@@ -104,6 +104,156 @@ class AgentTools{
         return "开始执行操作";
     }
     
+    
+    
+    /**
+     * 门店推荐
+     */
+    public function pushShop($parentShopId=0,$childShopId=0){
+        if($parentShopId == 0 || $childShopId == 0 || $parentShopId == $childShopId){
+            return ["code"=>-1,"msg"=>"参数错误","data"=>null];
+        }
+        
+        //子级门店
+        $childShopData = $this->dbShop->get($childShopId);
+        if(empty($childShopData)){
+            return ["code"=>-1,"msg"=>"子门店不存在","data"=>null];
+        }
+        if($childShopData["level"]>0){
+            return ["code"=>-1,"msg"=>"当前门店不能被绑定为子级门店","data"=>null];
+        }
+        if(!empty($childShopData["pTwoShopId"])){
+            return ["code"=>-1,"msg"=>"子门店已被其他门店绑定","data"=>null];
+        }
+        
+        //父级门店
+        $parentShopData = $this->dbShop->get($parentShopId);
+        if(empty($parentShopData)){
+            return ["code"=>-1,"msg"=>"父门店不存在","data"=>null];
+        }
+        $pOneShopId = 0;
+        $pTwoShopId = $parentShopId;
+        if(!$parentShopData["pTwoShopId"]){
+            $pOneShopId = $parentShopData["pTwoShopId"];
+        }
+        
+        //更新子级门店数据
+        $save=[
+            "level"=>1,//门店级别,1为社区门店,2为代理门店,3为董事门店
+            "pOneShopId"=>$pOneShopId,//爷爷级门店id
+            "pTwoShopId"=>$pTwoShopId,//父级门店id
+        ];
+        $this->dbShop->update($save,["id"=>$childShopId]);
+        //如果父级门店没绑定过其他子级则更新为1级社区门店
+        if($parentShopData["level"]<=0){
+            $this->dbShop->update(["level"=>1],["id"=>$parentShopId]);
+        }
+        //更新父级等级
+        $this->updateShopLevel($parentShopId);
+        return ["code"=>1,"msg"=>"绑定成功","data"=>null];
+    }
+    
+    /**
+     * 更新门店等级
+     * @param type $shopId
+     */
+    public function updateShopLevel($shopId){
+        if(empty($shopId)){
+            return false;
+        }
+        $shopData = $this->dbShop->get($shopId);
+        if(empty($shopData) || $shopData["level"]==3 || $shopData["level"]==0){
+            return false;
+        }
+        //父级下的社区店数
+        $level = 1;
+        $levelCount_1 = $this->dbShop->count([["level","=",1],["pOneShopId|pTwoShopId","=",$shopId]]);
+        if($levelCount_1 && $levelCount_1>=5){
+            $level = 2;//5个升级为代理
+        }
+        //父级下的代理店数
+        $levelCount_2 = $this->dbShop->count([["level","=",2],["pOneShopId|pTwoShopId","=",$shopId]]);
+        if($levelCount_2 && $levelCount_2>=10){
+            $level = 3;//5个代理升级为董事
+        }
+        if($level<=$shopData["level"]){
+            return false;
+        }
+        //更新门店等级
+        $r = $this->dbShop->update(["level"=>$level],["id"=>$shopId]);
+        if(empty($r)){
+            return fasle;
+        }
+        return true;
+    }
+    
+    
+    /**
+     * 【新】门店推广添加任务
+     * @param type $purchaseId
+     * @param type $enterpriseId
+     * @return bool
+     */
+    public static function addTaskNew($purchaseId,$enterpriseId){
+        try{
+            if(empty($purchaseId) || empty($enterpriseId)){
+                return ["code"=>-1,"msg"=>"参数为空"];
+            }
+            //获取配置
+            $dbSetting = new DNewAgentSetting('default');
+            $settingData = $dbSetting->get(["enterpriseId"=>$enterpriseId]);
+            if(empty($settingData) || empty($settingData["is_open"]) || $settingData["oneonePer"]<=0){
+                return ["code"=>-1,"msg"=>"还未进行分佣配置"];
+            }
+            //获取采购单信息
+            $dbPurchase = new DPurchase();
+            $dbPurchase->setTable('qianniao_purchase_'.$enterpriseId);
+            $pwhere=[];
+            $pwhere["id"]=$purchaseId;
+            $pwhere["auditStatus"]=2;//审核状态
+            $pwhere["deleteStatus"]=5;//删除状态
+            $pwhere["inStatus"]=5;//入库状态
+            $pwhere["purchaseType"]=4;//采购订单
+            $purchaseData = $dbPurchase->get($pwhere);
+            if(empty($purchaseData) || empty($purchaseData["shopId"])){
+                return ["code"=>-1,"msg"=>"采购单不符合条件".$purchaseData["shopId"]];
+            }
+            //验证门店
+            $shopDb = new DShop();
+            $shopDb->setTable('qianniao_shop_1');
+            $shopData = $shopDb->get(["id"=>$purchaseData["shopId"],"enterpriseId"=>$enterpriseId]);
+            if(empty($shopData) || empty($shopData["pTwoShopId"])){
+                return ["code"=>-1,"msg"=>"当前门店没有上级门店"];
+            }
+            //验证上级门店是否存在
+            $pShopData = $this->dbShop->get($shopData["pTwoShopId"]);
+            if(empty($pShopData) || empty($pShopData["agentId"])){
+                return ["code"=>-1,"msg"=>"当前门店的上级门店数据配置错误"];
+            }
+            //添加任务
+            $dbTask = new DNewAgentTask('default');
+            $taskTableName = $dbTask->getTableName($dbTask->get_Table(), $enterpriseId, 1);
+            $dbTask->setTable($taskTableName);
+            //验证采购单是否已经添加过任务
+            $count = $dbTask->count(["purchaseId"=>$purchaseId]);
+            if($count>0){
+                return ["code"=>-1,"msg"=>"任务采购单已存在"];
+            }
+            $r = $dbTask->insert([
+                "purchaseId"=>$purchaseId,
+                "enterpriseId"=>$enterpriseId,
+                "title"=>"采购单完成计算分佣",
+                "mono"=>"",
+                "status"=>0,
+                "run_time"=>0,
+                "time"=>time(),
+            ]);
+            return ["code"=>1,"msg"=>"任务已插入[{$r}]"];
+        } catch (\Exception $e){
+            return ["code"=>-1,"msg"=>"系统错误"];
+        }
+    }
+    
     /**
      * 添加任务
      * @param type $purchaseId
@@ -198,6 +348,43 @@ class AgentTools{
         
     }
     
+    /**
+     * 【新】执行任务
+     * @return type
+     */
+    public function runTaskNew(){
+        if(empty($this->agentSettingData) || empty($this->agentSettingData["is_open"])){
+            return ["code"=>-1,"msg"=>"未开启配置"];
+        }
+        $key = "agenttaskcache".'::'.$this->enterpriseId;
+        $result = $this->cache->get($key);
+        if(!empty($result)){
+            return ["code"=>-1,"msg"=>"操作频繁"];
+        }
+        $this->cache->set($key,1,10);
+        $dbTask = new DNewAgentTask('default');
+        $taskTableName = $dbTask->getTableName($dbTask->get_Table(), $this->enterpriseId, 1);
+        $dbTask->setTable($taskTableName);
+        $data = $dbTask->select(["status"=>0],"*","id asc");
+        if(empty($data)){
+            return ["code"=>-1,"msg"=>"没有可执行任务"];
+        }
+        $nowTime=time();
+        foreach($data as $k=>$v){
+            $res = $this->runCalcMoneyDataNew($v["purchaseId"]);
+            $save=[];
+            $save=["status"=>1,"mono"=>"执行成功","run_time"=>$nowTime];
+            if(empty($res) || $res["code"]==-1){
+                $save["mono"] = empty($res["msg"])?"系统错误执行失败":$res["msg"];
+                $save["status"] = -1;
+            }
+            $dbTask->update($save, ["id"=>$v["id"]]);
+        }
+        $this->cache->set($key,null);
+        return ["code"=>1,"msg"=>"执行结束"];
+        
+    }
+    
     /**
      * 设置门店推广用户
      * @param type $parentId
@@ -450,6 +637,213 @@ class AgentTools{
         return ["code"=>1,"msg"=>"执行成功".$lms];
     }
     
+    
+    /**
+     * 【新】采购单消费计算佣金
+     * @param type $purchaseId
+     * @param type $orderMoney
+     * @param type $isPart 采购单用户如果是门店代理是否计算
+     * @return bool
+     */
+    public function runCalcMoneyDataNew($purchaseId){
+        
+        if(empty($this->agentSettingData) || empty($this->agentSettingData["is_open"])){
+            return ["code"=>-1,"msg"=>"配置信息错误"];
+        }
+        //获取采购单信息
+        if(empty($purchaseId)){
+            return ["code"=>-1,"msg"=>"采购单id错误"];
+        }
+        $pwhere=[];
+        $pwhere["id"]=$purchaseId;
+        $pwhere["auditStatus"]=2;//审核状态
+        $pwhere["deleteStatus"]=5;//删除状态
+        $pwhere["inStatus"]=5;//入库状态
+        $pwhere["purchaseType"]=4;//采购订单
+        $purchaseData = $this->dbPurchase->get($pwhere);
+        if(empty($purchaseData) || empty($purchaseData["shopId"])){
+            return ["code"=>-1,"msg"=>"采购单数据不存在或不是门店采购单".$purchaseData["shopId"]];
+        }
+        $purchaseShopId = $purchaseData["shopId"];
+        $shopData = $this->dbShop->get(["id"=>$purchaseShopId,"enterpriseId"=>$this->enterpriseId,"deleteStatus"=>5]);
+        if(empty($shopData) || empty($shopData["pTwoShopId"])){
+            return ["code"=>-1,"msg"=>"门店被删除或者没有父级门店"];
+        }
+        //开始计算
+        $purchaseAmount = $purchaseData["purchaseAmount"];//采购金额
+        $couponAmount   = $purchaseData["couponAmount"];//优惠金额
+        $otherAmount    = $purchaseData["otherAmount"];//其它金额
+//        $purchaseMoney = $purchaseAmount;
+        $purchaseMoney = $purchaseAmount + $otherAmount - $couponAmount;
+        if($purchaseMoney<=0){
+            return ["code"=>-1,"msg"=>"采购实际金额小于等于0"];
+        }
+        
+        
+        $data=[];
+        $nowTime = time();
+        $levelAr = ["xxxx","one","two"];
+        //先计算父级门店
+        $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";
+                    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(!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,
+                        ];
+                    }
+                }
+            }
+        }
+        if(empty($data)){
+            return ["code"=>-1,"msg"=>"父级绑定或者配置错误导致数据更新失败"];
+        }
+        
+
+        $num=0;
+        //开启事务
+        $this->dbNewAgentDetail->beginTransaction();
+        $lms = "";
+        foreach($data as $k=>$v){
+            $count = $this->dbNewAgentDetail->count(["purchaseId"=>$v["purchaseId"],"agentId"=>$v["agentId"]]);
+            if($count>0){
+                $lms.="当前采购单已计算佣金[{$v['purchaseId']}];";
+                continue;
+            }
+            $nid = $this->dbNewAgentDetail->insert($v);
+            if(empty($nid)){
+                $lms.="佣金明细插入失败[{$v['purchaseId']}];";
+                continue;
+            }
+            //查询门店代理账户
+            $agentItem = $this->dbNewAgent->get(["id"=>$v["agentId"]]);
+            if(empty($agentItem)){
+                $num = 0;
+                $lms.="门店代理账户不存在[{$v['purchaseId']}];";
+                break;
+            }
+            $shopItem = $this->dbShop->get($v["shopId"]);
+            if(empty($shopItem)){
+                $num = 0;
+                $lms.="门店不存在[{$v['purchaseId']}];";
+                break;
+            }
+            
+            
+            $update = [
+                'balance'    => bcadd($agentItem['balance'], $v["commission"], 2),
+                'totalMoney' => bcadd($agentItem['totalMoney'], $v["commission"], 2),
+                'updateTime' => $nowTime,
+            ];
+            //添加账户余额明细记录
+            $dres = $this->dbNewAgentBalanceDetail->insert([
+                "type"=>1,
+                "title"=>$v["title"],
+                "code"=>$v["type"]==1 ? "agent_calc" : "purchase_calc",
+                "money"=>$v["commission"],
+                "content"=>$v["title"],
+                "admin_id"=>0,
+                "customer_id"=>$v["customerId"],
+                "agent_id"=>$v["agentId"],
+                "user_center_id"=>$v["userCenterId"],
+                "shop_id"=>$v["shopId"],
+                "cash_id"=>0,
+                "old_balance"=>$agentItem["balance"],
+                "now_balance"=>$update["balance"],
+                "time"=>$nowTime,
+                "expand"=>"",
+                "detail_id"=>$nid,
+            ]);
+            if(empty($dres)){
+                $num = 0;
+                $lms.="账户余额明细插入失败[{$v['purchaseId']}];";
+                break;
+            }
+            //更新账户余额
+            $upRes = $this->dbNewAgent->update($update, ['id' => $v["agentId"]]);
+            if(empty($upRes)){
+                $num = 0;
+                $lms.="更新账户余额失败[{$v['purchaseId']}];";
+                break;
+            }
+            $num++;
+        }
+        if($num<=0){
+            $this->dbNewAgentDetail->rollBack();
+            return ["code"=>-1,"msg"=>"数据更新失败:".$lms];
+        }
+        $this->dbNewAgentDetail->commit();
+        return ["code"=>1,"msg"=>"执行成功".$lms];
+    }
+