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

class Zzbb_cfs_yiji_ranking extends Report {
    private $level=1;
    private $type =1;
    private $code ='code1593597365';

    public function getLevel(){
        return $this->level;
    }
    public function getType(){
        return $this->type;
    }
    public function getCode(){
        return $this->code;
    }
    public function data(){
        set_time_limit(0);
        $data = input();

        $start = date('Y-m-d 00:00:00', strtotime($data['start']));
        $end = date('Y-m-d 23:59:59', strtotime($data['end']));
        $det = db('crm_s_report_form')->alias('f')->join('crm_s_report_detail d','d.form_id=f.id')->where('d.id',$data['did'])->field('d.id,d.shop_code,d.brandclass_id,f.report_class_name')->find();

        $shop_code = $det['shop_code'];
        if(empty($shop_code)){
            return array('code'=>100,'msg'=>'报表配置有误');
        }

        
            $returnData[0]['day'] = '07-01';
            $returnData[0]['cztsycs'] = '3';
            $returnData[0]['czje'] = '10000';
            $returnData[0]['czfx'] = '1000';
            $returnData[0]['bz'] = '这里是备注';

            $returnData[0]['qd']  = '随便啥渠道';
            $returnData[0]['name'] = '不知道叫啥';
            $returnData[0]['age']  = '你猜';
            $returnData[0]['tel']  = '不告诉你';
            $returnData[0]['ytcje']  = '原套餐金额';
            $returnData[0]['sjtcje']  = '升级金额';
            $returnData[0]['cztpsl']  = '成长套拍摄量';
            $returnData[0]['dtpsl']  = '单套拍摄量';
            $returnData[0]['cpl']  = '重拍量';
            $returnData[0]['bpl']  = '补拍量';
            $returnData[0]['jfzje']  = '加服装金额';
            $returnData[0]['xddje']  = '新订单金额';
            $returnData[0]['czt']  = '成长套';
            $returnData[0]['dt']  = '单套';
            $returnData[0]['sg']  = '时光';
            $returnData[0]['jxje']  = '加选金额';
            $returnData[0]['sgje']  = '时光金额';
            $returnData[0]['xdje']  = '续订金额';
            $returnData[0]['ysje']  = '应收金额';
            $returnData[0]['qk']  = '欠款';
            $returnData[0]['hqk']  = '还欠款';
            $returnData[0]['tk']  = '退款（-）';

            $returnData[0]['现金']  = '现金';
            $returnData[0]['银行卡']  = '银行卡';
            $returnData[0]['VIP卡']  = 'VIP卡';
            $returnData[0]['活动卡']  = '活动卡';
            $returnData[0]['有赞收款']  = '有赞收款';
            $returnData[0]['报销']  = '报销';
            $returnData[0]['兴e付']  = '兴e付';
            $returnData[0]['大众点评']  = '大众点评';
            $returnData[0]['打赏']  = '打赏';
            $returnData[0]['退款减免']  = '退款减免';
            $returnData[0]['收款减免']  = '收款减免';
            $returnData[0]['积分收款']  = '积分收款';

            $returnData[0]['ssje']  = '金额';
            $returnData[0]['ddgj']  = '订单管家';
            $returnData[0]['xps']  = '选片师';
            $returnData[0]['sy']  = '摄影';
            $returnData[0]['hz']  = '化妆';
            $returnData[0]['yd']  = '引导';
            $returnData[0]['fwgj']  = '服务管家';

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

        $today = date('Ymd',strtotime($start));
        // 当日订单客资
        $oWwhere['o.targetdate'] = $today;
        $oWwhere['o.blankoutannal'] = ['<>', 1];
        $todayOrder = db('c_customerorder')->alias('o')
            ->join('c_customerinfo c', 'c.customerid=o.customerid','left')

            ->where($oWwhere)
            ->order('o.targetdate')
            ->cloumn('o.targetdate,o.orderId,c.wphone,c.wname,c.wphone,c.bbithdate,o.bargain_money','o.order');
        $sql1 = db()->getLastSql();
        $noIds = '';
        foreach ($todayOrder as $key => $value) {
            $noIds .= $value['orderId'].',';
        }
        $noIds = trim($noIds,',');
        // 当日拍照客资 排除以上
        $pWhere['photodate'] = $today;
        $todayPhoto = db('c_customerorder')->alias('o')
            ->join('c_customerphoto p','o.orderId=p.orderId','left')
            ->where($pWhere)->where('o.orderId', 'not in', $noIds)
            ->order('p.photodate')->select();
        $sql2 = db()->getLastSql();
        $noIds = $noIds.',';
        foreach ($todayPhoto as $key => $value) {
            $noIds .= $value['orderId'].',';
        }
        $noIds = trim($noIds,',');
        // 当日选片客资 排除以上
        $sWhere['selectday'] = $today;
        $todaySp = db('c_customerorder')->alias('o')
            ->join('c_customersp s','o.orderId=s.orderId','left')
            ->where($sWhere)->where('o.orderId', 'not in', $noIds)
            ->order('s.selectday')->select();
        $sql3 = db()->getLastSql();
        $noIds = $noIds.',';
        foreach ($todayPhoto as $key => $value) {
            $noIds .= $value['orderId'].',';
        }
        $noIds = trim($noIds,',');
        // 当日收款客资 排除以上
        $payWhere['paymentdate'] = $today;
        $todaySp = db('c_customerorder')->alias('o')
            ->join('c_paymentcontent p','o.orderId=p.orderId','left')
            ->where($payWhere)->where('o.orderId', 'not in', $noIds)
            ->order('p.paymentdate')->select();
        $sql4 = db()->getLastSql();
        
        $return['code'] = 0;
        $return['msg'] = '';
        $return['count'] = count($returnData);
        $return['data'] = $returnData;

        return json($return);
    }
    //  
    public function show(){
        /*
        $did = strtolower(input('param.did'));
        $info = db('crm_s_report_form')->alias('f')->join('crm_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();*/
        $menu_code = strtolower(input('param.menu_code'));
        $info = db('crm_s_report_form')->alias('f')->join('crm_s_report_detail d','d.form_id=f.id','left')->where('f.code',$menu_code)->field('d.id,d.shop_code,d.brandclass_id,f.report_class_name')->find();
        $city = db('crm_s_shop_address')->where('shop_code',$info['shop_code'])->where('type',2)->select();
        //消费类型
        $consumptionType = controller("Customer", "lib")->consumptionType(['brandclass_id'=>$info['brandclass_id']]);
        $this->assign("consumptionType", $consumptionType);
        //来源渠道
        $channel = getChannelJson($info['shop_code']);
        $this->assign("channel", json_encode($channel));

        $code = $this->getCode();
        $display = APP_PATH.'crm'.DS.'view'.DS.'report'.DS.strtolower($info['report_class_name']).'.html';
        $title = db('crm_s_report_form')->where('code', $code)->value('report_name');
        $shopInfo = controller('crm/Customer','lib')->get_shop(['shop_code'=>$info['shop_code']]);
        $this->assign('title', $shopInfo[0]['shop_name'].'-'.$title);
        $this->assign('did', $info['id']);
        // c_paymenttype
        $allPayType = db('c_paymenttype')->where("find_in_set('".$info['shop_code']."',shop_code)")->field('id,typename')->select();
        $this->assign('allPayType', $allPayType);
        $this->assign('allPayCount', count($allPayType));
        // 当前操作人的可操作店

        return $this->fetch($display);
    }
        /*
            $returnData[0]['day'] = '07-01';
            $returnData[0]['cztsycs'] = '3';
            $returnData[0]['czje'] = '10000';
            $returnData[0]['czfx'] = '1000';
            $returnData[0]['bz'] = '这里是备注';

            $returnData[0]['qd']  = '随便啥渠道';
            $returnData[0]['name'] = '不知道叫啥';
            $returnData[0]['age']  = '你猜';
            $returnData[0]['tel']  = '不告诉你';
            $returnData[0]['ytcje']  = '原套餐金额';
            $returnData[0]['sjtcje']  = '升级金额';
            $returnData[0]['cztpsl']  = '成长套拍摄量';
            $returnData[0]['dtpsl']  = '单套拍摄量';
            $returnData[0]['cpl']  = '重拍量';
            $returnData[0]['bpl']  = '补拍量';
            $returnData[0]['jfzje']  = '加服装金额';
            $returnData[0]['xddje']  = '新订单金额';
            $returnData[0]['czt']  = '成长套';
            $returnData[0]['dt']  = '单套';
            $returnData[0]['sg']  = '时光';
            $returnData[0]['jxje']  = '加选金额';
            $returnData[0]['sgje']  = '时光金额';
            $returnData[0]['xdje']  = '续订金额';
            $returnData[0]['ysje']  = '应收金额';
            $returnData[0]['qk']  = '欠款';
            $returnData[0]['hqk']  = '还欠款';
            $returnData[0]['tk']  = '退款（-）';

            $returnData[0]['现金']  = '现金';
            $returnData[0]['银行卡']  = '银行卡';
            $returnData[0]['VIP卡']  = 'VIP卡';
            $returnData[0]['活动卡']  = '活动卡';
            $returnData[0]['有赞收款']  = '有赞收款';
            $returnData[0]['报销']  = '报销';
            $returnData[0]['兴e付']  = '兴e付';
            $returnData[0]['大众点评']  = '大众点评';
            $returnData[0]['打赏']  = '打赏';
            $returnData[0]['退款减免']  = '退款减免';
            $returnData[0]['收款减免']  = '收款减免';
            $returnData[0]['积分收款']  = '积分收款';

            $returnData[0]['ssje']  = '金额';
            $returnData[0]['ddgj']  = '订单管家';
            $returnData[0]['xps']  = '选片师';
            $returnData[0]['sy']  = '摄影';
            $returnData[0]['hz']  = '化妆';
            $returnData[0]['yd']  = '引导';
            $returnData[0]['fwgj']  = '服务管家';

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