<?php
/**
* 代金券统计
* @Copyright (C) 2016 汉潮 All rights reserved.
* @License http://www.hanchao9999.com
* @Author xiaogg <xiaogg@sina.cn>
*/
namespace Admin\Controller;
class BonuStatisticsController extends BaseController{
    public function index($is_return=false){
        $this->order_type=strtoarray(C('ORDER_TYPE'));$this->assign('order_type',$this->order_type);
        $order_source=strtoarray(array('1'=>'微信','2'=>'移动收银','3'=>'小程序','4'=>'收银HD'));
        $this->assign('order_source',$order_source);
        $sale_type = array('1'=>'线下','2'=>'线上');
        $this->assign('sale_type',$sale_type);
        $s_map = array();$s_map['id'] = array('gt',0);$s_map['status'] = 1;
        $shop_list=D('Shops')->shop_list();
        $this->assign('shop_list',$shop_list);

        $sou_type=strtoarray(array('1'=>'按仓库','2'=>'按小时','3'=>'按日','4'=>'按月'));
        $this->assign('sou_type',$sou_type);

        $map=$map_oi=array();
        $postdata=I('param.');
        $map_oi['type']=array('in','1,6,11,13,14');
        $map_oi['pay_status']=3;
        $map_oi['is_delete']=0;
        $map_oi['status']=array('in','1,2,3,6,8');
        if($postdata['shopid'])$map_oi['shop_id']= $postdata['shopid'];
        if($postdata['source']){
            $map_oi['source']= $postdata['source'];
        }
        $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['addtime'][] = array('egt',$date_from);}
            $date_to = strtotime($postdata['reg_to']);
            if($date_to){$date_to = $date_to+86399;$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_oi['addtime']=array('between',array($between[0],$between[1]));
            }else{
                $postdata['dates']=99;
            }
        }
        if($postdata['in_online']==1){
            $map_oi['in_online']=$postdata['in_online'];
        }elseif($postdata['in_online']==2){
            $map_oi['in_online']=0;
        }
        if($postdata['id']){
            $map=array();
            $map_c['coupons_id']= $postdata['id'];
            $bonus_ids=M('CouponsGet')->where($map_c)->getField('id',true);
            if($bonus_ids){
                $map_oi['bonus_id']= array('in',$bonus_ids);
            }else{
                if($is_return){
                    $return['list']=array();
                    $return['info']=array();
                    if($postdata['sou_value']==1){
                        $return['title'] = array('仓库','总金额','优惠券金额','订单量','客单优惠占比','优惠金额占比','仓库优惠占比');
                    }else{
                        $return['title'] = array('时间','总金额','优惠券金额','订单量','客单优惠占比','优惠金额占比'.'时间优惠占比');
                    }

                    return $return;
                }else{
                    $this->assign($postdata);
                    $this->assign('list',array());
                    $this->display();exit;
                }
            }
        }
        switch($postdata['sou_value']){
            case 2://按小时
                for($i=0;$i<24;$i++){
                    $day_arr[$i] = $i; //处理时间数组
                }
                $field="count(id) as num,sum(order_money) as order_money,sum(bonus_money) as bonus_money,shop_id,addtime,(sum(bonus_money)/count(id)) as avg_num";
                $list_t=M('OrderInfo')->where($map_oi)->group("DATE_FORMAT(FROM_UNIXTIME(addtime),'%k')")->field($field)->select();
                $orderinfo=M('OrderInfo')->where($map_oi)->field($field)->find();
                $orderinfo['avg_price']=0;
                $list_time=array();
                if($list_t){
                    foreach ($list_t as $k => $v) {
                        $list[date('G',$v['addtime'])]['addtime']=date('G',$v['addtime']).'点';
                        $list[date('G',$v['addtime'])]['order_money']=format_decimal($v['order_money']);
                        $list[date('G',$v['addtime'])]['bonus_money']=$v['bonus_money'];
                        $list[date('G',$v['addtime'])]['num']=$v['num'];
                        $list[date('G',$v['addtime'])]['avg_price']=format_decimal($v['bonus_money']/$v['order_money']);
                        $list[date('G',$v['addtime'])]['avg_num']=format_decimal($v['avg_num']);
                        $list[date('G',$v['addtime'])]['percent_money']=number_format(($v['bonus_money']/$orderinfo['bonus_money'])*100,2).'%';
                        $list_time[]=date('G',$v['addtime']);
                    }unset($k,$v);
                    $orderinfo['avg_price']=format_decimal($orderinfo['bonus_money']/$orderinfo['order_money']);
                    $orderinfo['avg_num']=format_decimal($orderinfo['bonus_money']/$orderinfo['num']);
                    $new_day=array_diff($day_arr,$list_time);
                    foreach($new_day as $vt){
                        $list[$vt]['addtime']=$vt.'点';
                        $list[$vt]['order_money']=0.00;
                        $list[$vt]['bonus_money']=0.00;
                        $list[$vt]['num']=0;
                        $list[$vt]['avg_price']=0;
                        $list[$vt]['avg_num']=0;
                        $list[$vt]['percent_money']=0;
                    }
                    ksort($list);
                }
                break;
            case 3://按日
                for($i=1;$i<32;$i++){
                    $day_arr[$i] = $i; //处理时间数组
                }
                $field="count(id) as num,sum(order_money) as order_money,sum(bonus_money) as bonus_money,shop_id,addtime,(sum(bonus_money)/count(id)) as avg_num";
                $list_t=M('OrderInfo')->where($map_oi)->group("DATE_FORMAT(FROM_UNIXTIME(addtime),'%k')")->field($field)->select();
                $orderinfo=M('OrderInfo')->where($map_oi)->field($field)->find();
                $orderinfo['avg_price']=0;
                if($list_t){
                    foreach ($list_t as $k => $v) {
                        $list[date('j',$v['addtime'])]['addtime']=date('j',$v['addtime']).'日';
                        $list[date('j',$v['addtime'])]['order_money']=format_decimal($v['order_money']);
                        $list[date('j',$v['addtime'])]['bonus_money']=$v['bonus_money'];
                        $list[date('j',$v['addtime'])]['num']=$v['num'];
                        $list[date('j',$v['addtime'])]['avg_price']=format_decimal($v['bonus_money']/$v['order_money']);
                        $list[date('j',$v['addtime'])]['avg_num']=format_decimal($v['avg_num']);
                        $list[date('j',$v['addtime'])]['percent_money']=number_format(($v['bonus_money']/$orderinfo['bonus_money'])*100,2).'%';
                        $list_time[]=date('j',$v['addtime']);
                    }unset($k,$v);
                    $orderinfo['avg_price']=format_decimal($orderinfo['bonus_money']/$orderinfo['order_money']);
                    $orderinfo['avg_num']=format_decimal($orderinfo['bonus_money']/$orderinfo['num']);
                }
                $new_day=array_diff($day_arr,$list_time);
                foreach($new_day as $vt){
                    $list[$vt]['addtime']=$vt.'日';
                    $list[$vt]['order_money']=0.00;
                    $list[$vt]['bonus_money']=0.00;
                    $list[$vt]['num']=0;
                    $list[$vt]['avg_price']=0;
                    $list[$vt]['avg_num']=0;
                    $list[$vt]['percent_money']=0;
                }
                ksort($list);
                break;
            case 4://月
                for($i=1;$i<13;$i++){
                    $day_arr[$i] = $i; //处理时间数组
                }
                $field="count(id) as num,sum(order_money) as order_money,sum(bonus_money) as bonus_money,shop_id,addtime,(sum(bonus_money)/count(id)) as avg_num";
                $list_t=M('OrderInfo')->where($map_oi)->group("DATE_FORMAT(FROM_UNIXTIME(addtime),'%k')")->field($field)->select();
                $orderinfo=M('OrderInfo')->where($map_oi)->field($field)->find();
                $orderinfo['avg_price']=0;
                if($list_t){
                    foreach ($list_t as $k => $v) {
                        $list[date('n',$v['addtime'])]['addtime']=date('n',$v['addtime']).'月';
                        $list[date('n',$v['addtime'])]['order_money']=format_decimal($v['order_money']);
                        $list[date('n',$v['addtime'])]['bonus_money']=$v['bonus_money'];
                        $list[date('n',$v['addtime'])]['num']=$v['num'];
                        $list[date('n',$v['addtime'])]['avg_price']=format_decimal($v['bonus_money']/$v['order_money']);
                        $list[date('n',$v['addtime'])]['avg_num']=format_decimal($v['avg_num']);
                        $list[date('n',$v['addtime'])]['percent_money']=number_format(($v['bonus_money']/$orderinfo['bonus_money'])*100,2).'%';
                        $list_time[]=date('n',$v['addtime']);
                    }unset($k,$v);
                    $orderinfo['avg_price']=format_decimal($orderinfo['bonus_money']/$orderinfo['order_money']);
                    $orderinfo['avg_num']=format_decimal($orderinfo['bonus_money']/$orderinfo['num']);
                }
                $new_day=array_diff($day_arr,$list_time);
                foreach($new_day as $vt){
                    $list[$vt]['addtime']=$vt.'月';
                    $list[$vt]['order_money']=0.00;
                    $list[$vt]['bonus_money']=0.00;
                    $list[$vt]['num']=0;
                    $list[$vt]['avg_price']=0;
                    $list[$vt]['avg_num']=0;
                    $list[$vt]['percent_money']=0;
                }
                ksort($list);
                break;
            default://按店
                $shoplist=$shop_list;
                if($postdata['shopid']){
                    $map_s=array();
                    $map_s['id']=$postdata['shopid'];
                    $shoplist=M('Shops')->where($map_s)->getField('id,name');
                }
                $shop_ids=array_keys($shoplist);
                $postdata['sou_value']=1;
                $field="count(id) as num,sum(order_money) as order_money,sum(bonus_money) as bonus_money,shop_id,addtime,(sum(bonus_money)/count(id)) as avg_num";
                $list_s=M('OrderInfo')->where($map_oi)->group("shop_id")->field($field)->select();
                $orderinfo=M('OrderInfo')->where($map_oi)->field($field)->find();
                $orderinfo['avg_price']=0;
                if($list_s){
                    foreach ($list_s as $k => $v) {
                        $list[$v['shop_id']]['shop_name']=$shop_list[$v['shop_id']];
                        $list[$v['shop_id']]['order_money']=format_decimal($v['order_money']);
                        $list[$v['shop_id']]['bonus_money']=$v['bonus_money'];
                        $list[$v['shop_id']]['num']=$v['num'];
                        $list[$v['shop_id']]['avg_price']=format_decimal($v['bonus_money']/$v['order_money']);
                        $list[$v['shop_id']]['avg_num']=format_decimal($v['avg_num']);
                        $list[$v['shop_id']]['percent_money']=number_format(($v['bonus_money']/$orderinfo['bonus_money'])*100,2).'%';
                        $list_time[]=$v['shop_id'];
                    }unset($k,$v);
                    $orderinfo['avg_price']=format_decimal($orderinfo['bonus_money']/$orderinfo['order_money']);
                    $orderinfo['avg_num']=format_decimal($orderinfo['bonus_money']/$orderinfo['num']);
                }
                $new_day=array_diff($shop_ids,$list_time);
                foreach($new_day as $vt){
                    $list[$vt]['shop_name']=$shop_list[$vt];
                    $list[$vt]['order_money']=0.00;
                    $list[$vt]['bonus_money']=0.00;
                    $list[$vt]['num']=0;
                    $list[$vt]['avg_price']=0;
                    $list[$vt]['avg_num']=0;
                    $list[$vt]['percent_money']=0;
                }
                break;

        }

        if($is_return){
            $return['list']=$list;
            $return['info']=$orderinfo;
            if($postdata['sou_value']==1){
                $return['title'] = array('仓库','总金额','优惠券金额','订单量','客单优惠占比','优惠金额占比','仓库优惠占比');
            }else{
                $return['title'] = array('时间','总金额','优惠券金额','订单量','客单优惠占比','优惠金额占比'.'时间优惠占比');
            }

            return $return;
        }else{
            $this->assign($orderinfo);
            $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']['order_money'],floatval($rest['info']['bonus_money']),$rest['info']['num'],format_decimal($rest['info']['avg_price']),format_decimal($rest['info']['avg_num']),'--');
        export_csv('优惠券统计.csv',$rest['title'],$data);
        exit;
    }
    public function coupons_get(){
        $map=array();
        $postdata=I('param.');
        $account_status=strtoarray(array('1'=>'未使用','2'=>'已使用','3'=>'已过期','4'=>'已作废'));
        $this->assign('account_status',$account_status);
        if($postdata['uid']){
            $map['uid']=$postdata['uid'];
        }
        if($postdata['status']){
            $map['status']=$postdata['status']-1;
        }
        if($postdata['coupons_id']){
            $map['coupons_id']=$postdata['coupons_id'];
        }
        $list=$this->lists('CouponsGet',$map);
        $this->assign($postdata);
        $this->assign('list',$list);
        $this->display();
    }
}