<?php
/**
* 礼品卡统计
* @Copyright (C) 2016 汉潮 All rights reserved.
* @License http://www.hanchao9999.com
* @Author xiaogg <xiaogg@sina.cn>
*/
namespace Admin\Controller;
class SaleAnalysisController extends BaseController{
    public function index($is_return=false){
        $shop_list=D('Shops')->shop_list();
        $this->assign('shop_list',$shop_list);
        $sou_type=strtoarray(array('1'=>'按商品分类','2'=>'按商品品牌','3'=>'按导购员','4'=>'按收银员','5'=>'按支付方式'));
        $this->assign('sou_type',$sou_type);
        $sou_title=strtoarray(array('1'=>'商品分类','2'=>'商品品牌','3'=>'导购员','4'=>'收银员','5'=>'支付方式'));
        $this->assign('sou_title',$sou_title);
        $map=$map_oi=array();
        $postdata=I('param.');
        if($postdata['shopid']){$map['a.shop_id']=$postdata['shopid'];$map_oi['shop_id']= $postdata['shopid'];}
        $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['a.addtime'][] = array('egt',$date_from);$map_oi['addtime'][] = array('egt',$date_from);}
            $date_to = strtotime($postdata['reg_to']);
            if($date_to){$date_to = $date_to+86399;$map['a.addtime'][] = array('lt',$date_to);$map_oi['addtime'][] = array('lt',$date_to);}
            $postdata['dates']=0;
        }else{
            if(in_array($postdata['dates'],array(1,2,7,30))){
                $between=quick_time($postdata['dates']);
                $map['a.addtime']=$map_oi['addtime']=array('between',array($between[0],$between[1]));
            }else{
                $postdata['dates']=99;
            }
        }
        $map['a.type']=$map_oi['type']=array('in','1,6,11,13,14');
        $map['a.pay_status']=$map_oi['pay_status']=3;
        $map['a.is_delete']=$map_oi['is_delete']=0;
        $map['a.status']=$map_oi['status']=array('in','1,2,3,6,8');
        $list=$cat_ida=$all=array();
        $all['num']=$all['goods_number']=$all['yuan_price']=$all['goods_price']=$all['profit']=0;
        if(!$postdata['sou_value']){
            $postdata['sou_value']=1;
        }
        if(F('list'.$postdata['sou_value'].','.$postdata['shopid'].','.$postdata['dates'].','.$postdata['reg_from'].','.$postdata['reg_to'])){
            $list=F('list'.$postdata['sou_value'].','.$postdata['shopid'].','.$postdata['dates'].','.$postdata['reg_from'].','.$postdata['reg_to']);
            $all=F('all'.$postdata['sou_value'].','.$postdata['shopid'].','.$postdata['dates'].','.$postdata['reg_from'].','.$postdata['reg_to']);
        }else{
            switch($postdata['sou_value']){
                case 2://按商品品牌
                    $map_c=array();
                    $map_c['status']=1;
                    $brandids = M('Brand')->where($map_c)->field('id,brand_name')->select();
                    $orderid=M('OrderInfo')->where($map_oi)->getField('id',true);

                    if(empty($orderid)){
                        break;
                    }
                    $og_map['order_id']=array('in',$orderid);
                    $goods_oids=M('OrderGoods')->where($og_map)->field('goods_id,goods_number,attr_price*goods_number as yuan_price,goods_price*goods_number as goods_price,profit,order_id')->select();
                    if($brandids){
                        foreach($brandids as $k=>$v){
                            $g_map=array();
                            $list[$v['id']]['name']=$v['brand_name'];
                            $list[$v['id']]['profit']=$list[$v['id']]['goods_price']=$list[$v['id']]['yuan_price']=$list[$v['id']]['goods_number']=$list[$v['id']]['num']=0;
                            $g_map['brand_id']=$v['id'];
                            $goods_ids = M('Goods')->where($g_map)->getField('id',true);
                            $cat_ida[$v['id']]=$goods_ids;
                        }unset($k,$v);
                        foreach($cat_ida as $k=>$v){
                            if($v){
                                $map_n=array();
                                $map_n['a.id']=array('in',$orderid);
                                $map_n['g.goods_id']=array('in',$v);
                                $list[$k]['num']=M('OrderInfo')->alias('a')->join('__ORDER_GOODS__ g ON g.order_id=a.id')->where($map_n)->count('a.id');
                                $goods_number=$yuan_price=$goods_price=$profit=0;
                                foreach($goods_oids as $kg=>$vg){
                                    if($v && in_array($vg['goods_id'],$v)){
                                        $goods_number +=$vg['goods_number'];
                                        $yuan_price+=$vg['yuan_price'];
                                        $goods_price+=$vg['goods_price'];
                                        $profit+=$vg['profit'];
                                    }
                                }
                                $list[$k]['goods_number']=floatval($goods_number);
                                $list[$k]['yuan_price']=format_decimal($yuan_price);
                                $list[$k]['goods_price']=format_decimal($goods_price);
                                $list[$k]['profit']=format_decimal($profit);
                            }
                            $all['num']+=$list[$k]['num'];
                            $all['goods_number']+=$list[$k]['goods_number'];
                            $all['yuan_price']+=$list[$k]['yuan_price'];
                            $all['goods_price']+=$list[$k]['goods_price'];
                            $all['profit']+=$list[$k]['profit'];
                        }
                    }
                    break;
                case 3://导购员
                    $guider_list=D('Shops')->guider_list();
                    if($guider_list){
                        $guide_ids=array_keys($guider_list);
                        $map['guide']=array('in',$guide_ids);
                        $field="count(a.id) as num,sum(a.goods_allmoney) as yuan_price,sum(a.profit) as profit,sum(a.order_money) as goods_price,a.guide,sum(g.goods_number) as goods_number";
                        $list_o=M('OrderInfo')->alias('a')->where($map)->join('__ORDER_GOODS__ g ON g.order_id=a.id')->group("guide")->field($field)->select();
                        $all=M('OrderInfo')->alias('a')->where($map)->join('__ORDER_GOODS__ g ON g.order_id=a.id')->field($field)->find();
                        $list_time=array();
                        foreach ($list_o as $k => $v) {
                            $list[$v['guide']]['name']=$guider_list[$v['guide']];
                            $list[$v['guide']]['num']=$v['num'];
                            $list[$v['guide']]['goods_number']=floatval($v['goods_number']);
                            $list[$v['guide']]['yuan_price']=format_decimal($v['yuan_price']);
                            $list[$v['guide']]['goods_price']=format_decimal($v['goods_price']);
                            $list[$v['guide']]['profit']=format_decimal($v['profit']);
                            $list_time[]=$v['guide'];
                        }unset($k,$v);
                        $new_day=array_diff($guide_ids,$list_time);
                        foreach($new_day as $vt){
                            $list[$vt]['name']=$guider_list[$vt];
                            $list[$vt]['num']=0;
                            $list[$vt]['goods_number']=0;
                            $list[$vt]['yuan_price']=0;
                            $list[$vt]['goods_price']=0;
                            $list[$vt]['profit']=0;
                        }
                        if($all['num']==0){
                            $all['num']=$all['goods_number']=$all['yuan_price']=$all['goods_price']=$all['profit']=0;
                        }
                    }
                    break;
                case 4://按收银员
                    $cashier_list=D('Shops')->cashier_list();
                    $cashier_ids=array_keys($cashier_list);
                    if($cashier_list){
                        $map['sa_id']=array('in',$cashier_ids);
                        $field="count(a.id) as num,sum(a.goods_allmoney) as yuan_price,sum(a.profit) as profit,sum(a.order_money) as goods_price,a.sa_id,sum(g.goods_number) as goods_number";
                        $list_o=M('OrderInfo')->alias('a')->where($map)->join('__ORDER_GOODS__ g ON g.order_id=a.id')->group("sa_id")->field($field)->select();
                        $all=M('OrderInfo')->alias('a')->where($map)->join('__ORDER_GOODS__ g ON g.order_id=a.id')->field($field)->find();
                        $list_time=array();
                        foreach ($list_o as $k => $v) {
                            $list[$v['sa_id']]['name']=$cashier_list[$v['sa_id']];
                            $list[$v['sa_id']]['num']=$v['num'];
                            $list[$v['sa_id']]['goods_number']=floatval($v['goods_number']);
                            $list[$v['sa_id']]['yuan_price']=format_decimal($v['yuan_price']);
                            $list[$v['sa_id']]['goods_price']=format_decimal($v['goods_price']);
                            $list[$v['sa_id']]['profit']=format_decimal($v['profit']);
                            $list_time[]=$v['sa_id'];
                        }unset($k,$v);
                        $new_day=array_diff($cashier_ids,$list_time);
                        foreach($new_day as $vt){
                            $list[$vt]['name']=$cashier_list[$vt];
                            $list[$vt]['num']=0;
                            $list[$vt]['goods_number']=0;
                            $list[$vt]['yuan_price']=0;
                            $list[$vt]['goods_price']=0;
                            $list[$vt]['profit']=0;
                        }
                        if($all['num']==0){
                            $all['num']=$all['goods_number']=$all['yuan_price']=$all['goods_price']=$all['profit']=0;
                        }
                    }
                    break;
                case 5://按支付方式
                    $pay_type=array('CHINAUMSPOS'=>'银联','weixinpay'=>'微信','weixinsmall'=>'微信小程序','HANCHAO'=>'余额','HANCHAO_XIANJIN'=>'现金','HANCHAO_CARD'=>'刷卡','HANCHAO_DECCA'=>'台卡');
                    $pay_typek=array_keys($pay_type);
                    $field="count(a.id) as num,sum(a.goods_allmoney) as yuan_price,sum(a.profit) as profit,sum(a.order_money) as goods_price,a.pay_code,sum(g.goods_number) as goods_number";
                    $list_o=M('OrderInfo')->alias('a')->where($map)->join('__ORDER_GOODS__ g ON g.order_id=a.id')->group("pay_code")->field($field)->select();
                    $all=M('OrderInfo')->alias('a')->where($map)->join('__ORDER_GOODS__ g ON g.order_id=a.id')->field($field)->find();
                    $list_time=array();
                    foreach ($list_o as $k => $v) {
                        if($v['pay_code']=='CHINAUMSPOS'){
                            $list['CHINAUMSPOS']['name']=$pay_type['CHINAUMSPOS'];
                            $list['CHINAUMSPOS']['num']=$v['num'];
                            $list['CHINAUMSPOS']['goods_number']=floatval($v['goods_number']);
                            $list['CHINAUMSPOS']['yuan_price']=format_decimal($v['yuan_price']);
                            $list['CHINAUMSPOS']['goods_price']=format_decimal($v['goods_price']);
                            $list['CHINAUMSPOS']['profit']=format_decimal($v['profit']);
                            $list_time[]='CHINAUMSPOS';
                        }
                        if($v['pay_code']=='weixinpay'){
                            $list['weixinpay']['name']=$pay_type['weixinpay'];
                            $list['weixinpay']['num']=$v['num'];
                            $list['weixinpay']['goods_number']=floatval($v['goods_number']);
                            $list['weixinpay']['yuan_price']=format_decimal($v['yuan_price']);
                            $list['weixinpay']['goods_price']=format_decimal($v['goods_price']);
                            $list['weixinpay']['profit']=format_decimal($v['profit']);
                            $list_time[]='weixinpay';
                        }
                        if($v['pay_code']=='weixinsmall'){
                            $list['weixinsmall']['name']=$pay_type['weixinsmall'];
                            $list['weixinsmall']['num']=$v['num'];
                            $list['weixinsmall']['goods_number']=floatval($v['goods_number']);
                            $list['weixinsmall']['yuan_price']=format_decimal($v['yuan_price']);
                            $list['weixinsmall']['goods_price']=format_decimal($v['goods_price']);
                            $list['weixinsmall']['profit']=format_decimal($v['profit']);
                            $list_time[]='weixinsmall';
                        }
                        if(!$v['pay_code']||$v['pay_code']==''||$v['pay_code']=='Hanchao'||$v['pay_code']=='HANCHAO'){
                            $list['HANCHAO']['name']=$pay_type['HANCHAO'];
                            $list['HANCHAO']['num']+=$v['num'];
                            $list['HANCHAO']['goods_number']+=floatval($v['goods_number']);
                            $list['HANCHAO']['yuan_price']+=format_decimal($v['yuan_price']);
                            $list['HANCHAO']['goods_price']+=format_decimal($v['goods_price']);
                            $list['HANCHAO']['profit']+=format_decimal($v['profit']);
                            $list_time[]='HANCHAO';
                        }
                        if($v['pay_code']=='HANCHAO_XIANJIN'){
                            $list['HANCHAO_XIANJIN']['name']=$pay_type['HANCHAO_XIANJIN'];
                            $list['HANCHAO_XIANJIN']['num']=$v['num'];
                            $list['HANCHAO_XIANJIN']['goods_number']=floatval($v['goods_number']);
                            $list['HANCHAO_XIANJIN']['yuan_price']=format_decimal($v['yuan_price']);
                            $list['HANCHAO_XIANJIN']['goods_price']=format_decimal($v['goods_price']);
                            $list['HANCHAO_XIANJIN']['profit']=format_decimal($v['profit']);
                            $list_time[]='HANCHAO_XIANJIN';
                        }
                        if($v['pay_code']=='HANCHAO_CARD') {
                            $list['HANCHAO_CARD']['name'] = $pay_type['HANCHAO_CARD'];
                            $list['HANCHAO_CARD']['num'] = $v['num'];
                            $list['HANCHAO_CARD']['goods_number'] = floatval($v['goods_number']);
                            $list['HANCHAO_CARD']['yuan_price'] = format_decimal($v['yuan_price']);
                            $list['HANCHAO_CARD']['goods_price'] = format_decimal($v['goods_price']);
                            $list['HANCHAO_CARD']['profit'] = format_decimal($v['profit']);
                            $list_time[] = 'HANCHAO_CARD';
                        }
                        if($v['pay_code']=='HANCHAO_DECCA'){
                            $list['HANCHAO_DECCA']['name']=$pay_type['HANCHAO_DECCA'];
                            $list['HANCHAO_DECCA']['num']=$v['num'];
                            $list['HANCHAO_DECCA']['goods_number']=floatval($v['goods_number']);
                            $list['HANCHAO_DECCA']['yuan_price']=format_decimal($v['yuan_price']);
                            $list['HANCHAO_DECCA']['goods_price']=format_decimal($v['goods_price']);
                            $list['HANCHAO_DECCA']['profit']=format_decimal($v['profit']);
                            $list_time[]='HANCHAO_DECCA';
                        }

                    }unset($k,$v);
                    $list_time=array_filter(array_unique($list_time));
                    $new_day=array_diff($pay_typek,$list_time);
                    foreach($new_day as $vt){
                        $list[$vt]['name']=$pay_type[$vt];
                        $list[$vt]['num']=0;
                        $list[$vt]['goods_number']=0;
                        $list[$vt]['yuan_price']=0;
                        $list[$vt]['goods_price']=0;
                        $list[$vt]['profit']=0;
                    }
                    if($all['num']==0){
                        $all['num']=$all['goods_number']=$all['yuan_price']=$all['goods_price']=$all['profit']=0;
                    }
                    break;
                default://按商品分类
                    $postdata['sou_value']=1;
                    $map_c=array();
                    $map_c['status']=1;
                    $map_c['pid']=0;
                    $carids = M('PurchaseCategory')->where($map_c)->field('id,name')->select();
                    $orderid=M('OrderInfo')->where($map_oi)->getField('id',true);
                    $list=$cat_ida=array();
                    if(empty($orderid)){
                        break;
                    }
                    $og_map['order_id']=array('in',$orderid);
                    $goods_oids=M('OrderGoods')->where($og_map)->field('goods_id,goods_number,attr_price*goods_number as yuan_price,goods_price*goods_number as goods_price,profit,order_id')->select();
                        foreach($carids as $k=>$v){
                            $g_map=array();
                            $list[$v['id']]['name']=$v['name'];
                            $list[$v['id']]['profit']=$list[$v['id']]['goods_price']=$list[$v['id']]['yuan_price']=$list[$v['id']]['goods_number']=$list[$v['id']]['num']=0;
                            $child_catid =get_children($v['id'], 1,5);
                            if($child_catid){
                                $child_catid[]=$v['id'];
                                $g_map['cat_id']=array('in',$child_catid);
                            }else{
                                $g_map['cat_id']=$v['id'];
                            }
                            $goods_ids = M('Goods')->where($g_map)->getField('id',true);
                            $cat_ida[$v['id']]=$goods_ids;
                        }unset($k,$v);

                    foreach($cat_ida as $k=>$v){
                        if($v){
                            $map_n=array();
                            $map_n['a.id']=array('in',$orderid);
                            $map_n['g.goods_id']=array('in',$v);
                            $list[$k]['num']=M('OrderInfo')->alias('a')->join('__ORDER_GOODS__ g ON g.order_id=a.id')->where($map_n)->count('a.id');
                            $goods_number=$yuan_price=$goods_price=$profit=0;
                            foreach($goods_oids as $kg=>$vg){
                                if($v && in_array($vg['goods_id'],$v)){
                                    $goods_number +=$vg['goods_number'];
                                    $yuan_price+=$vg['yuan_price'];
                                    $goods_price+=$vg['goods_price'];
                                    $profit+=$vg['profit'];
                                }
                            }
                            $list[$k]['goods_number']=floatval($goods_number);
                            $list[$k]['yuan_price']=format_decimal($yuan_price);
                            $list[$k]['goods_price']=format_decimal($goods_price);
                            $list[$k]['profit']=format_decimal($profit);
                        }
                        $all['num']+=$list[$k]['num'];
                        $all['goods_number']+=$list[$k]['goods_number'];
                        $all['yuan_price']+=$list[$k]['yuan_price'];
                        $all['goods_price']+=$list[$k]['goods_price'];
                        $all['profit']+=$list[$k]['profit'];
                    }
                    break;

            }
            F('list'.$postdata['sou_value'].','.$postdata['shopid'].','.$postdata['dates'].','.$postdata['reg_from'].','.$postdata['reg_to'],$list);
            F('all'.$postdata['sou_value'].','.$postdata['shopid'].','.$postdata['dates'].','.$postdata['reg_from'].','.$postdata['reg_to'],$all);
        }


        if($is_return){
            $return['list']=$list;
            $return['info']=$all;
            $return['title'] = array($sou_title[$postdata['sou_value']],'销售单数','商品数量','商品原价','实收金额','利润');
            return $return;
        }else{
            $this->assign($all);
            $this->assign($postdata);
            $this->assign('list',$list);
            $this->display();
        }
    }
/**
* 销售占比分析导出
*
*/
    public function export(){
        $rest=$this->index(true);
        $data=array();
        $data=$rest['list'];
        $data[]=array('总计',$rest['info']['num'],$rest['info']['goods_number'],$rest['info']['yuan_price'],$rest['info']['goods_price'],$rest['info']['profit']);
        export_csv('销售占比分析.csv',$rest['title'],$data);
        exit;
    }

}