<?php
/**
* 导购员绩效统计
* @Copyright (C) 2016 汉潮 All rights reserved.
* @License http://www.hanchao9999.com
* @Author xiaogg <xiaogg@sina.cn>
*/
namespace Admin\Controller;
class GuiderController extends BaseController{
    public function index($is_return=false){
        $mapg=$guiderlist=$map=$map_oi=$list=$list_new=array();

        $guider_list=D('Shops')->guider_list();
        if($guider_list){
            $postdata=I('param.');
            $guiderlist=$guider_list;
            $map_oi['pay_status']=3;
            $map_oi['is_delete']=0;
            $map_oi['status']=3;

            if($postdata['sale_id']){
                $mapg=array();
                $mapg['id']=$map_oi['guide']= $postdata['sale_id'];
                $guiderlist=M('ShopsAccount')->where($mapg)->getField('id,name');//搜索导购员只显示搜索的的导购员
            }else{$map_oi['guide']=array('gt',0);}

            $now_time=strtotime(date('Y-m-d',NOW_TIME));
            if($postdata['reg_from'] || $postdata['reg_to']){
                $date_from = strtotime($postdata['reg_from']);
                if($date_from){$map_oi['confirm_time'][] = array('egt',$date_from);}
                $date_to = strtotime($postdata['reg_to']);
                if($date_to){$date_to = $date_to+86399;$map_oi['confirm_time'][] = array('lt',$date_to);}
                $postdata['dates']=0;
            }else{
                if(in_array($postdata['dates'],array(1,2,7,30))){
                    $between=quick_time($postdata['dates']);
                    $map_oi['confirm_time']=array('between',array($between[0],$between[1]));
                }else{
                    $postdata['dates']=99;
                }
            }
            $sales_maid=M('OrderInfo')->where($map_oi)->group("guide")->field('sum(sales_maid) as sales_maid,guide')->select();
            $totalsales_maid=M('OrderInfo')->where($map_oi)->sum('sales_maid');
            $goods=$chongzhi=$giftcard=array();
            $map_oi['type']=array('in','1,6,11,12,13,14');
            $field="count(id) as num,sum(goods_allmoney) as goods_allmoney,guide,sum(profit) as profit,sum(order_money) as order_money,sum(sales_maid) as sales_maid";
            $goods=M('OrderInfo')->where($map_oi)->group("guide")->field($field)->select();//销售提成
            $all_goods=M('OrderInfo')->where($map_oi)->field($field)->find();
            unset($map_oi['type']);
            $map_oi['type']=array('in','2,5');
            $chongzhi=M('OrderInfo')->where($map_oi)->group("guide")->field('count(id) as cnum,sum(order_amount) as order_amount,sum(sales_maid) as sales_maid,guide')->select();//充值提成

            $all_chongzhi=M('OrderInfo')->where($map_oi)->field('count(id) as cnum,sum(order_amount) as order_amount,sum(sales_maid) as sales_maid')->find();
            unset($map_oi['type']);
            $map_oi['type']=15;
            $giftcard=M('OrderInfo')->where($map_oi)->group("guide")->field('count(id) as fnum,sum(order_amount) as order_amount,sum(sales_maid) as sales_maid,guide')->select();//礼品卡提成

            $all_gift=M('OrderInfo')->where($map_oi)->field('count(id) as fnum,sum(order_amount) as order_amount,sum(sales_maid) as sales_maid')->find();
            foreach($guiderlist as $k=>$v){
                $list[$k]['name']=$v;
                if($goods){
                    foreach($goods as $kg=>$vg){
                        if($k==$vg['guide']){
                            $list[$k]['num']=$vg['num'];
                            $list[$k]['goods_allmoney']=format_decimal($vg['goods_allmoney']);
                            $list[$k]['order_money']=format_decimal($vg['order_money']);
                            $list[$k]['profit']=format_decimal($vg['profit']);
                            $list[$k]['sales_maid']=format_decimal($vg['sales_maid']);
                        }
                    }
                }
                if($chongzhi){
                    foreach($chongzhi as $kc=>$vc){
                        if($k==$vc['guide']){
                            $list[$k]['cnum']=$vc['cnum'];
                            $list[$k]['cmoney']=format_decimal($vc['order_amount']);
                            $list[$k]['csales_maid']=format_decimal($vc['sales_maid']);
                        }
                    }
                }

                if($giftcard){
                    foreach($giftcard as $kf=>$vf){
                        if($k==$vf['guide']){
                            $list[$k]['fnum']=$vf['fnum'];
                            $list[$k]['fmoney']=format_decimal($vf['order_amount']);
                            $list[$k]['fsales_maid']=format_decimal($vf['sales_maid']);
                        }
                    }
                }
                if($sales_maid){
                    foreach($sales_maid as $ka=>$va){
                        if($k==$va['guide']){
                            $list[$k]['asales_maid']=format_decimal($va['sales_maid']);
                        }
                    }
                }
            }
            $i=0;//为导出的excel做处理
            foreach($list as $kl=>$vl){
                $list_new[$i]['name']=$vl['name'];
                if(empty($vl['num'])){
                    $list_new[$i]['num']=0;
                }else{
                    $list_new[$i]['num']=$vl['num'];
                }
                if(empty($vl['goods_allmoney'])){
                    $list_new[$i]['goods_allmoney']=0;
                }else{
                    $list_new[$i]['goods_allmoney']=$vl['goods_allmoney'];
                }
                if(empty($vl['order_money'])){
                    $list_new[$i]['order_money']=0;
                }else{
                    $list_new[$i]['order_money']=$vl['order_money'];
                }
                if(empty($vl['profit'])){
                    $list_new[$i]['profit']=0;
                }else{
                    $list_new[$i]['profit']=$vl['profit'];
                }
                if(empty($vl['sales_maid'])){
                    $list_new[$i]['sales_maid']=0;
                }else{
                    $list_new[$i]['sales_maid']=$vl['sales_maid'];
                }
                if(empty($vl['cnum'])){
                    $list_new[$i]['cnum']=0;
                }else{
                    $list_new[$i]['cnum']=$vl['cnum'];
                }
                if(empty($vl['cmoney'])){
                    $list_new[$i]['cmoney']=0;
                }else{
                    $list_new[$i]['cmoney']=$vl['cmoney'];
                }
                if(empty($vl['csales_maid'])){
                    $list_new[$i]['csales_maid']=0;
                }else{
                    $list_new[$i]['csales_maid']=$vl['csales_maid'];
                }
                if(empty($vl['fnum'])){
                    $list_new[$i]['fnum']=0;
                }else{
                    $list_new[$i]['fnum']=$vl['fnum'];
                }
                if(empty($vl['fmoney'])){
                    $list_new[$i]['fmoney']=0;
                }else{
                    $list_new[$i]['fmoney']=$vl['fmoney'];
                }
                if(empty($vl['fsales_maid'])){
                    $list_new[$i]['fsales_maid']=0;
                }else{
                    $list_new[$i]['fsales_maid']=$vl['fsales_maid'];
                }
                if(empty($vl['asales_maid'])){
                    $list_new[$i]['asales_maid']=0;
                }else{
                    $list_new[$i]['asales_maid']=$vl['asales_maid'];
                }
                $i++;
            }
            //总计
            $total=array();
            $total['z']='总计';
            $total['allnum']=floatval($all_goods['num'])?$all_goods['num']:0;
            $total['allmoney']=floatval($all_goods['goods_allmoney'])?format_decimal($all_goods['goods_allmoney']):0.00;
            $total['shi_money']=floatval($all_goods['order_money'])?format_decimal($all_goods['order_money']):0.00;
            $total['allprofit']=floatval($all_goods['profit'])?format_decimal($all_goods['profit']):0.00;
            $total['allgsales_maid']=floatval($all_goods['sales_maid'])?format_decimal($all_goods['sales_maid']):0.00;
            $total['allcnum']=intval($all_chongzhi['cnum'])?intval($all_chongzhi['cnum']):0;
            $total['allcmoney']=floatval($all_chongzhi['order_amount'])?format_decimal($all_chongzhi['order_amount']):0.00;
            $total['allcsales_maid']=floatval($all_chongzhi['sales_maid'])?format_decimal($all_chongzhi['sales_maid']):0.00;
            $total['allfnum']=intval($all_gift['fnum'])?intval($all_gift['fnum']):0;
            $total['allfmoney']=floatval($all_gift['order_amount'])?format_decimal($all_gift['order_amount']):0.00;
            $total['allfsales_maid']=floatval($all_gift['sales_maid'])?format_decimal($all_gift['sales_maid']):0.00;
            $total['allsales_maid']=$totalsales_maid;
        }
        if($is_return==false){
            $this->assign('sales_list',$guider_list);
            $this->assign($postdata);
            $this->assign('list',$list_new);
            $this->assign($total);
            $this->display();
        }else{
            $return=array('data'=>$list_new,'total'=>$total);
            return $return;
        }
    }
/**
* 导购员绩效导出
*
*/
    public function export(){
        $rest=$this->index(true);
        $data=array();
        $data=$rest['data'];
        $data[]=$rest['total'];
        $title = array('导购员', '订单量', '商品总金额','实收','利润','提成金额','充值总数','充值金额','充值提成金额','礼品卡总数','礼品卡金额','礼品卡提成金额','提成总金额');
        export_csv('导购员绩效.csv',$title,$data);
        exit;
    }

}