<?php
/**
 * 统计
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaogg <xiaogg@sina.cn>
 */
namespace Admin\Controller;
class TongjiController extends BaseController{
    //供应商统计
    public function supply(){
        $postdata=I('param.');
        $map['status']=array(array('GT',0));
        $map['is_delete']=0;
        $map_oi['shop_id']=array('neq',0);
        if($_POST){
	  //按条件搜索
	  if($postdata['shop']) $map_oi['shop_id']=$postdata['shop'];
	  //如果时间为空进入
	  if(empty($postdata['date_to'])) {
	      $date_tostr=strtotime('first day of next month');
	      $postdata['date_to']=time_format($date_tostr,'Y-m-d');
	  }
	  if(empty($postdata['date_from'])) $postdata['date_from']=time_format(strtotime('-6 month',$date_tostr),'Y-m-d');
	  $map_oi['addtime'] = array(array('GT',strtotime($postdata['date_from'])),array('lt',strtotime($postdata['date_to'])));
        }
        $map_oi['type']=1;
        $orderinfo = M('OrderInfo')->where($map_oi)->field('id,addtime')->select();
        $ordergoodsarr=array();
        $goodsarr=array();
        $i=0;
        //查询店铺列表
        foreach($orderinfo as $val){
	  $where_og['order_id']=$val['id'];
	  $ordergoodsarr[$val['id']] = M('OrderGoods')->where($where_og)->field('goods_id,shop_id,goods_number,goods_price')->select();
	  $i++;
	  foreach($ordergoodsarr as $vv){
	      foreach($vv as $v){
		$shop[]=$v['shop_id'];
	      }
	  }
        }
        $num_shop = array_count_values($shop);
        $shop_info = array_unique($shop);
        $shop_statistics=array();
        $i=0;
        //查询店铺的营业额信息
        foreach($num_shop as $k=>$val){
	  $map_i['og.shop_id']=$k;
	  $shop_statistics[$k] = M('OrderGoods')->alias('og')->where($map_i)->field('og.shop_id,sum(og.goods_number) as num,sum(og.goods_number*g.supply_price) as sup_all,sum(og.goods_price) as mon_all')->join('LEFT JOIN __GOODS__ g ON g.id=og.goods_id')->select();
	  $i++;
        }
        $this->assign('shop_statistics',$shop_statistics);
        $this->assign('shop_info',$shop_info);
        $this->assign($postdata);
        $this->display();
    }
    public function print_list(){
        $map['status']=1;
        $s_list=M('Store')->where($map)->field(true)->select();
        foreach($s_list as $k=>$v){
	  $map_o['store_id']=$v['id'];
	  $map_o['status']=3;
	  $s_list[$k]['all_amount']=M('OrderInfo')->where($map_o)->sum('order_amount');
	  $map['store_id']=$c;
	  $goods_list=M('OrderGoods')->where($map)->field(true)->select();
	  $supp_price=$suuply_price=0;
	  foreach($goods_list as $ke=>$va){
	      $supp_price = idtoname($va['goods_id'].'Goods','supply_price');
	      $s_list[$k]['supply_price'] +=$supp_price*$va['goods_number'];
	  }
	  $s_list[$k]['yingli_price']=$s_list[$k]['all_amount']-$s_list[$k]['supply_price'];
        }
        $all_amount=$all_supply=$all_yingli=0;
        foreach($s_list as $key=>$val){
	  $all_amount+=$val['all_amount'];
	  $all_supply+=$val['supply_price'];
	  $all_yingli+=$val['yingli_price'];
        }
        $this->assign('list',$s_list);
        $this->assign('all_amount',$all_amount);
        $this->assign('all_supply',$all_supply);
        $this->assign('all_yingli',$all_yingli);
        $this->display();
    }
    public function shops(){//订单明细
        $this->order_type=strtoarray(C('ORDER_TYPE'));$this->assign('order_type',$this->order_type);
        // $order_source=strtoarray(array('5'=>'PC','1'=>'微信','2'=>'移动收银','3'=>'小程序','4'=>'收银HD'));
        $order_source=strtoarray(array('1'=>'微信','2'=>'移动收银','3'=>'小程序','4'=>'收银HD'));
        $this->assign('order_source',$order_source);
        // $sale_type = array('1'=>'线下','2'=>'线上');
        $sale_type = array('2'=>'线上');
        $this->assign('sale_type',$sale_type);
        $shop_list=D('Shops')->shop_list();
        $this->assign('shop_list',$shop_list);
        $guider_list=D('Shops')->guider_list();
        $this->assign('guider_list',$guider_list);
        $cashier_list=D('Shops')->cashier_list();
        $this->assign('cashier_list',$cashier_list);
        $map=array();
        $postdata=I('param.');
        $map['pay_status']=3;
        // $map['type']=array('in','1,2,5,13,14');
        $map['type']=array('in','1,6,11,12,13,14');
        // $map['source']=array('neq',0);
        $map['is_delete']=0;
        $map['status']=array('in','1,2,3,6,8');
        if($postdata['order_sn'])$map['order_sn']=$postdata['order_sn'];

        if($postdata['shop_id'])$map['shop_id'] = $postdata['shop_id'];
        if($postdata['source']){
	  // if($postdata['source'] == 5){$map['source'] = 0;
	  // }else{$map['source'] = $postdata['source'];}
	  $map['source'] = $postdata['source'];
        }
        if($postdata['keyword']){
	  $map_m['id|username|nickname|real_name|card_sn']=$postdata['keyword'];
	  $uids=M('Member')->where($map_m)->getField('id',true);
	  $map['uid'] = array('in',$uids);
        }
        $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['addtime'][] = array('egt',$date_from);
	  $date_to = strtotime($postdata['reg_to']);
	  if($date_to){$date_to = $date_to+86399;$map['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['addtime']=array('between',array($between[0],$between[1]));
	  }else{
	      $postdata['dates']=99;
	  }
        }
        if($postdata['in_online']==1){
	  $map['in_online']=$postdata['in_online'];
        }elseif($postdata['in_online']==2){
	  $map['in_online']=0;
        }
        if($postdata['guider_id']){
	  $map['guide']=$postdata['guider_id'];
        }
        if($postdata['sa_id']){
	  $map['sa_id']=$postdata['sa_id'];
        }
        $list = $this->lists('OrderInfo',$map);
        $orderinfo = M('OrderInfo')->where($map)->field("count(id) as num,sum(discount) as act_money,sum(member_discount) as member_discount,sum(goods_allmoney) as goodsmoney,sum(order_money) as order_money,sum(profit) as profit,sum(first_money) as firstmoney,sum(delivery_fee) as deliveryfee")->find();
        $youhui = $orderinfo['act_money'];
        $ying_money = $orderinfo['goodsmoney'];
        $this->assign($orderinfo);
        $this->assign('youhui',$youhui);
        $this->assign('ying_money',$ying_money);
        $this->assign('list',$list);$this->assign($postdata);
        $this->display();
    }
    public function order($is_return=false){//订单汇总统计
        $this->order_type=strtoarray(C('ORDER_TYPE'));$this->assign('order_type',$this->order_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['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');
        if($postdata['shopid'])$map['a.shop_id'] =$map_oi['shop_id']= $postdata['shopid'];
        if($postdata['source']){
	  $map['a.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['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;
	  }
        }
        if($postdata['in_online']==1){
        	$map['a.in_online']=$map_oi['in_online']=$postdata['in_online'];
        }elseif($postdata['in_online']==2){
        	$map['a.in_online']=$map_oi['in_online']=0;
        }
        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,shop_id,addtime,(sum(order_money)/count(id)) as avg_num";
        	$list_t=M('OrderInfo')->where($map_oi)->group("DATE_FORMAT(FROM_UNIXTIME(addtime),'%k')")->field($field)->select();
        	$ordergoods = M('OrderInfo')->alias('a')->where($map)->join('__ORDER_GOODS__ g ON g.order_id=a.id')->field("sum(g.goods_number) as goods_num,a.addtime")->group("DATE_FORMAT(FROM_UNIXTIME(a.addtime),'%k')")->select();

	      $orderinfo=M('OrderInfo')->where($map_oi)->field($field)->find();
	      $orderinfo['avg_price']=$orderinfo['goods_num']=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'])]['goods_num']=$ordergoods[$k]['goods_num'];
		    $list[date('G',$v['addtime'])]['num']=$v['num'];
		    $list[date('G',$v['addtime'])]['avg_price']=format_decimal($v['order_money']/$ordergoods[$k]['goods_num']);
		    $list[date('G',$v['addtime'])]['avg_num']=format_decimal($v['avg_num']);
		    $list[date('G',$v['addtime'])]['percent_money']=number_format(($v['order_money']/$orderinfo['order_money'])*100,2).'%';
		    $orderinfo['goods_num']+=$ordergoods[$k]['goods_num'];
		    $list_time[]=date('G',$v['addtime']);
		}unset($k,$v);
		$orderinfo['avg_price']=$orderinfo['order_money']/$orderinfo['goods_num'];
		$orderinfo['avg_num']=$orderinfo['order_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]['goods_num']=0;
		    $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,shop_id,addtime,(sum(order_money)/count(id)) as avg_num";
	      $list_t=M('OrderInfo')->where($map_oi)->group("DATE_FORMAT(FROM_UNIXTIME(addtime),'%e')")->field($field)->select();
	      $ordergoods = M('OrderInfo')->alias('a')->where($map)->join('__ORDER_GOODS__ g ON g.order_id=a.id')->field("sum(g.goods_number) as goods_num,a.addtime")->group("DATE_FORMAT(FROM_UNIXTIME(a.addtime),'%e')")->select();

	      $orderinfo=M('OrderInfo')->where($map_oi)->field($field)->find();
	      $orderinfo['avg_price']=$orderinfo['goods_num']=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'])]['goods_num']=floatval($ordergoods[$k]['goods_num']);
		    $list[date('j',$v['addtime'])]['num']=floatval($v['num']);
		    $list[date('j',$v['addtime'])]['avg_price']=format_decimal($v['order_money']/$ordergoods[$k]['goods_num']);
		    $list[date('j',$v['addtime'])]['avg_num']=format_decimal($v['avg_num']);
		    $list[date('j',$v['addtime'])]['percent_money']=number_format(($v['order_money']/$orderinfo['order_money'])*100,2).'%';
		    $orderinfo['goods_num']+=$ordergoods[$k]['goods_num'];
		    $list_time[]=date('j',$v['addtime']);
		}unset($k,$v);
		$orderinfo['avg_price']=$orderinfo['order_money']/$orderinfo['goods_num'];
		$orderinfo['avg_num']=$orderinfo['order_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]['goods_num']=0;
		$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,shop_id,addtime,(sum(order_money)/count(id)) as avg_num";
	      $list_t=M('OrderInfo')->where($map_oi)->group("DATE_FORMAT(FROM_UNIXTIME(addtime),'%c')")->field($field)->select();
	      $ordergoods = M('OrderInfo')->alias('a')->where($map)->join('__ORDER_GOODS__ g ON g.order_id=a.id')->field("sum(g.goods_number) as goods_num,a.addtime")->group("DATE_FORMAT(FROM_UNIXTIME(a.addtime),'%c')")->select();
	      $orderinfo=M('OrderInfo')->where($map_oi)->field($field)->find();
	      $orderinfo['avg_price']=$orderinfo['goods_num']=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'])]['goods_num']=floatval($ordergoods[$k]['goods_num']);
		    $list[date('n',$v['addtime'])]['num']=floatval($v['num']);
		    $list[date('n',$v['addtime'])]['avg_price']=format_decimal($v['order_money']/$ordergoods[$k]['goods_num']);
		    $list[date('n',$v['addtime'])]['avg_num']=format_decimal($v['avg_num']);
		    $list[date('n',$v['addtime'])]['percent_money']=number_format(($v['order_money']/$orderinfo['order_money'])*100,2).'%';
		    $orderinfo['goods_num']+=$ordergoods[$k]['goods_num'];
		    $list_time[]=date('n',$v['addtime']);
		}unset($k,$v);
		$orderinfo['avg_price']=$orderinfo['order_money']/$orderinfo['goods_num'];
		$orderinfo['avg_num']=$orderinfo['order_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]['goods_num']=0;
		$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,shop_id,addtime,(sum(order_money)/count(id)) as avg_num";
	      $list_s=M('OrderInfo')->where($map_oi)->group("shop_id")->field($field)->select();
	      $ordergoods = M('OrderInfo')->alias('a')->where($map)->join('__ORDER_GOODS__ g ON g.order_id=a.id')->field("sum(g.goods_number) as goods_num,a.addtime,a.shop_id")->group("a.shop_id")->select();
	      $orderinfo=M('OrderInfo')->where($map_oi)->field($field)->find();
	      $orderinfo['avg_price']=$orderinfo['goods_num']=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']]['goods_num']=floatval($ordergoods[$k]['goods_num']);
		    $list[$v['shop_id']]['num']=floatval($v['num']);
		    $list[$v['shop_id']]['avg_price']=format_decimal($v['order_money']/$ordergoods[$k]['goods_num']);
		    $list[$v['shop_id']]['avg_num']=format_decimal($v['avg_num']);
		    $list[$v['shop_id']]['percent_money']=number_format(($v['order_money']/$orderinfo['order_money'])*100,2).'%';
		    $orderinfo['goods_num']+=$ordergoods[$k]['goods_num'];
		    $list_time[]=$v['shop_id'];
		}unset($k,$v);
		$orderinfo['avg_price']=$orderinfo['order_money']/$orderinfo['goods_num'];
		$orderinfo['avg_num']=$orderinfo['order_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]['goods_num']=0;
		$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_order(){
        $rest=$this->order(true);
        $data=array();
        $data=$rest['list'];
        $data[]=array('总计',$rest['info']['order_money'],floatval($rest['info']['goods_num']),$rest['info']['num'],format_decimal($rest['info']['avg_price']),format_decimal($rest['info']['avg_num']),'--');
        export_csv('订单统计.csv',$rest['title'],$data);
        exit;
    }

}