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

class Qd_zhuanhua extends Report {

    private $level=3;
    private $type=4;
    private $code='code1533897330';

    public function getLevel(){
        return $this->level;

    }
    public function getType(){
        return $this->type;
    }
    public function getCode(){
        return $this->code;
    }
    public function data()
    {
        $param = input();
        $start = $param['start'] ? date('Y-m-d', strtotime($param['start'])) : date('Y-m-d');
        $end =  $param['end'] ? date('Y-m-d 23:59:59', strtotime($param['end'])) : date('Y-m-d 23:59:59');
        $consultation_type_id = $param['consultation_type_id'] ? $param['consultation_type_id'] : HS_TYPE;
        $shop_code = $param['shop_code'];
        $staffInfo = controller('crm/Customer','lib')->get_staff_lists(['shop_code'=>$shop_code]);

        $staff = [];
        foreach($staffInfo as $k=>$v){
               $staff[$v['id']]['id'] = $v['id'];
               $staff[$v['id']]['shop_name'] = $v['shop_name'] ? $v['shop_name'] : '';
               $staff[$v['id']]['department_name'] = $v['department_name'] ? $v['department_name'] : '';
               $staff[$v['id']]['staff_name'] = $v['staffname'];
       }
        $ids = array_column($staff,'id');
        $where['is_status'] = 1;
        $where['c_from'] = 1;

        $sql = '';
        $sql .= "sum(dj_day between '$start' and '$end') as mkz,";
        $sql .= "sum(consultation_type_id = ".$consultation_type_id." and is_valid=1 and yx_day between '$start' and '$end') as valid,";//有效
        $sql .= "sum(is_valid=2 and yx_day between '$start' and '$end') as dd,";//待定
        $sql .= "sum(is_valid=3 and yx_day between '$start' and '$end') as wx,";//无效
        $sql .= "sum(consultation_type_id = ".$consultation_type_id." and sign_name='A客' and sign_time between '$start' and '$end') as a,";//A客
        $sql .= "sum(consultation_type_id = ".$consultation_type_id." and yjd_day between '$start' and '$end') as yy,";//邀约

        $sql .= "sum(consultation_type_id = ".$consultation_type_id." and is_intostore in (2,3) and syjd_day between '$start' and '$end') as jd,";//婚纱进店
        $sql .= "sum(is_intostore in (2,3) and syjd_day between '$start' and '$end') as alljd,";//总进店
        $sql .= "sum(consultation_type_id = ".$consultation_type_id." and is_success=1 and order_day between '$start' and '$end') as hsOrder,";//婚纱订单
        $sql .= "sum(is_success=1 and order_day between '$start' and '$end') as allOrder";//总订单
        // 录入客资量（分配）
        $info= db('c_customerinfo')
            ->where($where)
            ->where('add_shop_code','in',$shop_code)
            ->where('dj_staff_id','in',$ids)
            ->group('dj_staff_id')
            ->column($sql,'dj_staff_id');

        $tel= db('c_customerinfo')
            ->where($where)
            ->where('add_shop_code','in',$shop_code)
            ->where('dj_staff_id','in',$ids)
            ->group('dj_staff_id')
            ->column('sum(tel_visit_num)','dj_staff_id');

       //总成交
        $fact = db('c_customerinfo')->alias('c')->join('c_order_detail d','c.order_number=d.order_sn')
            ->where($where)
            ->where('d.pay_type',1)
            ->where('d.pay_time', 'between time', [$start, $end])
            ->where('c.add_shop_code','in',$shop_code)
            ->where('c.dj_staff_id','in',$ids)
            ->group('c.dj_staff_id')
            ->column('sum(d.order_fact)', 'c.dj_staff_id');
        //总套系
        $amount = db('c_customerinfo')->alias('c')->join('c_customer_order o','c.order_number=o.order_sn')
            ->where($where)
            ->where('c.order_start_time', 'between time', [$start, $end])
            ->where('c.add_shop_code','in',$shop_code)
            ->where('c.dj_staff_id','in',$ids)
            ->group('c.dj_staff_id')
            ->column('sum(o.order_amount)', 'c.dj_staff_id');

        $returnData = [];
        $tot = [];
        foreach($staff as $k=>$v){
            $arr = [];
            $arr['shop'] = $v['shop_name'];
            $arr['department'] = $v['department_name'];
            $arr['staffname'] = $v['staff_name'];
            $arr['mkz'] = $info[$v['id']]['mkz'] ? $info[$v['id']]['mkz'] : "";
            $arr['valid'] = $info[$v['id']]['valid'] ? $info[$v['id']]['valid'] : '';
            $arr['dd'] = $info[$v['id']]['dd'] ? $info[$v['id']]['dd'] : '';
            $arr['unvalid'] = $info[$v['id']]['wx'] ? $info[$v['id']]['wx'] : '';
            $arr['tel'] = $tel[$v['id']] ? $tel[$v['id']] : "";
            $arr['a'] = $info[$v['id']]['a'] ? $info[$v['id']]['a'] : '';
            $arr['aRate'] = intval($arr['valid'])+intval($arr['dd'])==0 ? '' : sprintf('%.2f',$arr['a']/(intval($arr['valid'])+intval($arr['dd']))*100).'%';
            $arr['yy'] = $info[$v['id']]['yy'] ? $info[$v['id']]['yy'] : '';
            $arr['yyRate']  = $arr['a']==0 ? '' : sprintf('%.2f',$arr['yy']/$arr['a']*100).'%';//邀约率  邀约/有效
            $arr['jd'] = $info[$v['id']]['jd'] ? $info[$v['id']]['jd'] : '';
            $arr['jdRate']  = $arr['yy']==0 ? '' : sprintf('%.2f',$arr['jd']/$arr['a']*100).'%';//进店率  进店/邀约
            $arr['HSsucc'] = $info[$v['id']]['hsOrder'] ? $info[$v['id']]['hsOrder'] : '';
            $arr['HSsuccRate']  = $arr['HSsucc']==0 ? '' : sprintf('%.2f',$arr['HSsucc']/$arr['jd']*100).'%';//成交率  成交/进店

            $arr['succ'] = $info[$v['id']]['allOrder'] ? $info[$v['id']]['allOrder'] : '';
            $arr['succRate']  = $arr['succ']==0 ? '' : sprintf('%.2f',$arr['succ']/$info[$v['id']]['alljd']*100).'%';//成交率  成交/进店

            $arr['fact']  = $fact[$v['id']]==0 ? '' : sprintf('%.2f',$fact[$v['id']]);//总实收
            $arr['total']  = $amount[$v['id']] == 0 ? '' : sprintf('%.2f',$amount[$v['id']]);//总套系

            array_push($returnData,$arr);
            //总计
            $tot['mkz'] += $arr['mkz'];
            $tot['valid'] += $arr['valid'];
            $tot['dd'] += $arr['dd'];
            $tot['unvalid'] += $arr['unvalid'];
            $tot['tel'] += $arr['tel'];
            $tot['a'] += $arr['a'];
            $tot['aRate'] = '';
            $tot['yy'] += $arr['yy'];
            $tot['yyRate'] = '';
            $tot['jd'] += $arr['jd'];
            $tot['alljd'] += $info[$v['id']]['alljd'];
            $tot['jdRate'] = '';
            $tot['HSsucc'] += $arr['HSsucc'];
            $tot['HSsuccRate'] = '';
            $tot['succ'] += $arr['succ'];
            $tot['succRate'] = '';
            $tot['fact'] += floatval($arr['fact']);
            $tot['total'] += floatval($arr['total']);
        }
        //总计
        $total = [];
        $total['shop'] = '';
        $total['department'] = '';
        $total['staffname'] = '总计';
        $total['mkz'] = $tot['mkz'];
        $total['valid'] = $tot['valid'];
        $total['dd'] = $tot['dd'];
        $total['unvalid'] = $tot['unvalid'];
        $total['tel'] = $tot['tel'];
        $total['a'] = $tot['a'];
        $total['aRate'] = sprintf('%.2f',$tot['a']/($tot['valid']+$tot['dd'])*100).'%';
        $total['yy'] = $tot['yy'];
        $total['yyRate'] = $total['valid']==0 ? '0%' : sprintf('%.2f',$total['yy']/$total['a']*100).'%';//邀约率  邀约/有效
        $total['jd'] = $tot['jd'];
        $total['jdRate']  = $total['yy']==0 ? '0%' : sprintf('%.2f',$total['jd']/$total['a']*100).'%';//进店率  进店/邀约
        $total['HSsucc'] += $tot['HSsucc'];
        $total['HSsuccRate']  = $total['HSsucc']==0 ? '0%' : sprintf('%.2f',$total['HSsucc']/$total['jd']*100).'%';//成交率  成交/进店
        $total['succ'] += $tot['succ'];
        $total['succRate']  = $total['succ']==0 ? '0%' : sprintf('%.2f',$total['succ']/$tot['alljd']*100).'%';//成交率  成交/进店
        $total['fact']  = sprintf('%.2f',$tot['fact']);//毛客资转换率  成交/毛客资
        $total['total']  = sprintf('%.2f',$tot['total']);//有效客资转换率  成交
        array_push($returnData,$total);
        // 导出
        if ($param['need'] == 1) {
            $title = ['店面','部门','员工','毛客资数','有效(婚纱)','待定(全部)','无效(全部)','电访量','A客量(婚纱)','A客转化率(A客/(有效+待定))','邀约(婚纱)','邀约率(邀约/A客)','进店(婚纱)','进店率(进店/A客)','成交量(婚纱)','成交率(成交量/进店)','总订单数 ','总订单率','总实收','总套系'];
            $code = $this->getCode();
            $ff = db('s_report_form')->where('code', $code)->value('report_name');
            $shop = controller('crm/Customer','lib')->get_shop(['shop_code'=>$param['shop_code']]);
            $file = $shop[0]['shop_name'].'-'.$ff.'.xlsx';
            controller("crm/Customer", "lib")->export($title, $returnData, $file);
        } else {
            $return['code'] = 0;
            $return['msg'] = '';
            $return['count'] = count($returnData);
            $return['data'] = $returnData;
            return json($return);
        }
    }

    public function show(){
        $code = $this->getCode();
        $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();
        //消费类型
        $consumptionType = controller("Customer", "lib")->consumptionType(['brandclass_id'=>$info['brandclass_id']]);
        $this->assign("consumptionType", $consumptionType);
        //店
//        $shop = controller("Customer", "lib")->get_shop(['brandclass_id'=>session('brandclass_id')]);
//        $this->assign('shop',$shop);

        $shop= controller('crm/Customer','lib')->get_shop(['shop_code'=>$info['shop_code']]);
        $this->assign('shop', $shop);


        $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]['shop_name'].'-'.$title);
        $this->assign('did', $info['id']);
        return $this->fetch($display);
    }

}