<?php
/**
 *  个人数据报表
 * *
 * ============================================================================
 * 版权所有 2017北京素玄科技，并保留所有权利。
 *
 * 网站地址: http://www.suxuantech.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！未经允许的情况下，您不能对本系统代码做任何修改 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * 如有修改需求，请联系素玄科技有限公司：contact@suxuantech.cn
 * ============================================================================
 * $Author: wangzhenya 2018-08-20 $
 */
namespace app\crm\lib\report;
use app\crm\lib\Report;
use think\Controller;
use think\image\Exception;

class Qqbaby_staff_data extends Report {

    private $level=1;
    private $type=1;
    private $code='code1587697093';

    public function getLevel(){
        return $this->level;
    }
    public function getType(){
        return $this->type;
    }
    public function getCode(){
        return $this->code;
    }
    public function data()
    {
/*
    	if($ipnut){
    		input();
    	}

        if(empty($data['brandclass_id'])){
            if(!in_array(session('staff_id'),explode(',',config('show_all_ids')))){
                $return = ['code' => 200, 'msg' => '没有查看集团权限', 'count' => 0, 'data' => ''];
                return json($return);
            }
        }

        set_time_limit(0);*/

        $param = input();

        //-------查询条件---------//
        
		//时间
        $where['shop_code'] = session('shop_code');
        if($param['time']){
            $month = $param['time'];
        }else{
            $month = date("Y-m", time());
        }

        $costWhere['cost_date'] = ['like','%'.$month.'%'];
        
        if($param['staffid']){//登记人
        	$costWhere['staff_id'] = $param['staffid'];
        	$infoWhere['dj_staff_id'] = $param['staffid'];
        }

        if($param['city']){//市
        	$where['city_id'] = $param['city'];
        }

		//渠道表
		 $channelData = db('s_channel')->field("id,channel_name")->where('shop_code',session('shop_code'))->select();
        //$channelData = db('s_channel')->field("id,channel_name")->select();

        //花费
        $channelCost = db('s_channel_cost')
			        ->where($where)->where($costWhere)
			        ->group('channel_name,city_name')
			        ->column('sum(cost) as cost,sum(show_num) as zxl,sum(click_num) as djl,city_name,staff_id','channel_name');
   
    	//毛客资
    	$sql = "";
    	$sql.= " sum(is_status = 1 and c_from = 1 and dj_day like '%$month%') as mkz ";
    	$sql.= ",sum(is_status=1 and c_from = 1 and is_valid = 1 and yx_day like '%$month%') as yx ";
        $sql.= ",sum(is_status=1 and c_from = 1 and is_valid = 3 and yx_day like '%$month%') as wx ";
        $sql.= ",sum(is_status=1 and c_from = 1 and c_star_day is null and dj_day like '%$month%') as dzz";
    	$sql.= ",sum(is_status=1 and is_intostore=2 and c_from = 1 and syjd_day like '%$month%') as jd ";
    	$sql.= ",sum(order_day like '%$month%' and is_status=1 and c_from = 1 and is_success = 2) as cj ";
        $sql.= ",dj_staff,dj_staff_id,city";

        $infoData = db('c_customerinfo')
		        ->where($where)
		        ->where($infoWhere)
		        ->group('customer_channel,city')
		        ->column($sql,'customer_channel');

                
        
        $trackData = db('s_tracksign')->where('shop_code',session('shop_code'))->select();

        //A客
        $tractSql = "";//(当日)
        $tractMonthSql = "";//(当月)
        $i = 0;
        foreach ($trackData as $key => $value) {
            if($value['tracksign'] == 'A' ||$value['tracksign'] == 'B' || $value['tracksign'] == 'C'){
                if($i == 0){
                    $tractSql.= " sum(sign_id = ".$value['id']." and is_status=1 and c_from = 1 and dj_day like '%$month%' ) as ".$value['tracksign'].",dj_staff,dj_staff_id,city";
                }else{
                    $tractSql.= ",sum(sign_id = ".$value['id']."  and is_status=1 and c_from = 1 and dj_day like '%$month%') as ".$value['tracksign'].",dj_staff,dj_staff_id,city";
                }
                $i++;
            }
        }

        //A、B、C客 (当日)
        if($tractSql){
            $abcData = db('c_customerinfo')
                ->where($where)
                ->where($infoWhere)
                ->group('customer_channel,city')
                ->column($tractSql,'customer_channel');
        }else{
            $abcData = [];
        }

		//实收
		$shiData = db('c_customerinfo')
					->where($where)
					->where($infoWhere)
					->where("order_day like '%$month%' ")
                    ->group('customer_channel,city')
					->column('sum(fact_money) as fact_money,city,dj_staff,dj_staff_id','customer_channel');

		$data = [];
		foreach ($channelData as $key => $vv) {
            if($infoData[$vv['channel_name']]['city'] && $infoData[$vv['channel_name']]['dj_staff_id']){
                $data[$key]['yf'] = $month;
                $data[$key]['address'] = $infoData[$vv['channel_name']]['city'];
                $data[$key]['staff_id']= $infoData[$vv['channel_name']]['dj_staff_id'];
                $data[$key]['staff']   = $infoData[$vv['channel_name']]['dj_staff'];
    			$data[$key]['qd'] = $vv['channel_name'];
    			$data[$key]['hf'] = $channelCost[$vv['channel_name']]['cost']?$channelCost[$vv['channel_name']]['cost']:0;
    			$data[$key]['zxl'] = $channelCost[$vv['channel_name']]['zxl']?$channelCost[$vv['channel_name']]['zxl']:0;
    			$data[$key]['djl'] = $channelCost[$vv['channel_name']]['djl']?$channelCost[$vv['channel_name']]['djl']:0;
    			$data[$key]['mkz'] = $infoData[$vv['channel_name']]['mkz']?$infoData[$vv['channel_name']]['mkz']:0;
                $data[$key]['yx'] = $infoData[$vv['channel_name']]['yx']?$infoData[$vv['channel_name']]['yx']:0;
                $data[$key]['a'] = $abcData[$vv['channel_name']]['A']?$abcData[$vv['channel_name']]['A']:0;
                $data[$key]['b'] = $abcData[$vv['channel_name']]['B']?$abcData[$vv['channel_name']]['B']:0;
                $data[$key]['c'] = $abcData[$vv['channel_name']]['C']?$abcData[$vv['channel_name']]['C']:0;
                $data[$key]['cj'] = $infoData[$vv['channel_name']]['cj']?$infoData[$vv['channel_name']]['cj']:0;
                $data[$key]['wx'] = $infoData[$vv['channel_name']]['wx']?$infoData[$vv['channel_name']]['wx']:0;
                $data[$key]['dzz'] = $infoData[$vv['channel_name']]['dzz']?$infoData[$vv['channel_name']]['dzz']:0;
                $data[$key]['ss'] = $shiData[$vv['channel_name']]['fact_money']?$shiData[$vv['channel_name']]['fact_money']:0;
                $data[$key]['kzcb'] = sprintf('%.2f',$data[$key]['hf']/$data[$key]['mkz']);
                $data[$key]['ddcb'] = sprintf('%.2f',$data[$key]['hf']/$data[$key]['cj']);
                $data[$key]['trccb'] = sprintf('%.2f',$data[$key]['ss']/$data[$key]['hf']);
                $data[$key]['kzyx'] = sprintf('%.2f',$data[$key]['yx']/$data[$key]['mkz']*100).'%';
                $data[$key]['zdl'] = sprintf('%.2f',$data[$key]['cj']/$data[$key]['yx']*100).'%';
            }   
		}


        $return['code'] = 0;
        $return['msg'] = '';
        $return['count'] = count($data);
        $return['data'] = $data;
        return json($return);
    }

    public function show(){
        $did = strtolower(input('param.did'));
        $info = db('s_report_form')->alias('f')->join('s_report_detail d','d.form_id=f.id')->where('d.id',$did)->field('d.id,d.shop_code,d.brandclass_id,f.report_class_name')->find();

        $shop= controller('crm/Customer','lib')->getBrands(['brandclass_id'=>$info['brandclass_id']]);

        //消费类型
        $consumptionType = controller("Customer", "lib")->consumptionType(['brandclass_id'=>$info['brandclass_id']]);
        $this->assign("consumptionType", $consumptionType);

     	//可操作店
 		$p = [
            'staff_id'=> session('staff_id'),
            'brandclass_id'=> session('brandclass_id'),
            'shop_type' => session('shop_type'),
        ];
        $djshops = getShops($p);
		$this->assign("brandclassdata",$djshops);

		// crm省    列表
        $data['type'] = 2;
        $staCity = controller("Customer", "lib")->address($data);
        $this->assign("staCity",$staCity);

        //登记人
        $mcInter = new \app\common\lib\mcInterface(['shop_type'=>session('shop_type')]);

        $staffArr = $mcInter->getStaffInfo(['shop_code'=>session('shop_code')]);
       	$this->assign('staffArr',$staffArr);

       	//渠道
       	$channelData = db('s_channel')->field("id,channel_name")->select();
       	$this->assign('channel',$channelData);

        $code = $this->getCode();
        $display = APP_PATH.'crm'.DS.'view'.DS.'report'.DS.strtolower($info['report_class_name']).'.html';
        $title = db('s_report_form')->where('code', $code)->value('report_name');
        $this->assign('title', $shop[0]['brandclass'].'-'.$title);
        $this->assign('did', $info['id']);
        $this->assign('start', date('Y-m-01'));
        $this->assign('end', date('Y-m-d'));
        $this->assign('yestoday',date('Y-m',time()));

        return $this->fetch($display);
    }



}