<?php
/**
 * 销售统计
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaogg <xiaogg@sina.cn>
 */
namespace Admin\Controller;
class SaleMoneyController extends BaseController{
    public function index(){
        $postdata=I('param.');
        $adminid=UID;
        if($adminid !=1){
	        $data['id']=$adminid;
	        $store_id=M('Admin')->where($data)->field('store_id')->find();
	        $map['store_id']=$store_id;
        }
        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['addtime'] = array(array('GT',strtotime($postdata['date_from'])),array('lt',strtotime($postdata['date_to'])));
        $order_info = M('OrderInfo')->where($map)->field("sum(order_amount) as l_money,DATE_FORMAT(FROM_UNIXTIME(addtime),'%Y年%m月') as l_month")->group("DATE_FORMAT(FROM_UNIXTIME(addtime),'%Y年%m月')")->select();
        $months[5]["time"]=time_format(NOW_TIME,'Y年m月');
        for($i=1;$i<6;$i++){$months[(5-$i)]["time"]=time_format(strtotime('-'.$i.' month'),'Y年m月');}
        foreach($order_info  as $k=>$v){
	        foreach($months as $kk => $vv){
		        if($vv["time"] == $v["l_month"]){
			        $montharr[$vv['time']] = $v["l_money"];$moneyarr[$vv['time']] = $v["l_money"];
		        }
	        }
        }
        $money_count = array(
	        "legend"=>array('data'=>array("销售额统计")),
	        "xaxis"=>array("type"=>"category","data"=>array_keys($moneyarr)),
	        "series"=>array(
		        array("name"=>"销售额统计","type"=>"bar","stack"=>"销售额统计",'itemStyle'=>array(),"data"=>array_values($moneyarr)),
	        ),
        );
        $ec = new \Common\Vendor\Echarts();
        $this->assign('Area_count',$ec->show('Area_count', $money_count,'bar','销售额统计'));
        $this->assign($postdata);
        $this->display();
    }
    public function sale_goods(){
        $postdata=I('param.');
        $where_g = $map = array();
        $size = 20;
        $shop_list=D('Shops')->shop_list();
        $this->assign('shop_list',$shop_list);
        $supplier_list=D('Shops')->get_supplier_list();
        $this->assign('supplier_list',$supplier_list);
        if($postdata['shop_id']){
	  $shop_id = $postdata['shop_id'];
	  $map['oi.shop_id'] = $shop_id;
        }
        if($postdata['supplier_id']){
	  $map['a.suppliers_id'] = $postdata['supplier_id'];
        }
        // $sale_type = array('1'=>'线下','2'=>'线上');
          $sale_type = array('2'=>'线上');
        if (!empty($postdata['catid'])) {
	  $cat_sons = get_children($postdata['catid'], 1,5);
	  $catstr = implode(',', $cat_sons);
	  $extension_goods_id = get_extension_goods($postdata['catid'], 1);
	  $extensionstr = implode(',', $extension_goods_id);
	  $where = array();
	  if (!empty($cat_sons) && !empty($catstr)) {
	      $where['cat_id'] = array('in', $cat_sons);
	  }
	  if (!empty($extension_goods_id) && !empty($extensionstr)) {
	      $where['id'] = array('in', $extension_goods_id);
	  }
	  if (!empty($where)) {
	      $where['_logic'] = 'or';
	      $where_g['_complex'] = $where;
	  }
	  $goods_ids = M('Goods')->where($where_g)->getField('id',true);
	  if($goods_ids){
	      $map['a.goods_id'] = array('in',$goods_ids);
	  }
        }
		
        $map['oi.type'] = array('in','1,6,11,13,14');
        if($postdata['date_from'] || $postdata['date_to']){
	  $date_from = strtotime($postdata['date_from']);
	  if($date_from)$map['a.addtime'][] = array('egt',$date_from);
	  $date_to = strtotime($postdata['date_to']);
	  if($date_to){$date_to = $date_to +83699;$map['a.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']=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;
        }
        // xyy 2019.1.18 begin
        else{
            $map['oi.in_online']=0;
        }
        // xyy 2019.1.18 end
        $map['oi.status'] = array('in','1,2,3,6,8');
        $map['oi.pay_status'] = 3;

        $join = 'JOIN __ORDER_INFO__ oi ON oi.id = a.order_id';
        $field = 'a.*,a.goods_number*a.goods_price as sum_price,oi.order_sn,oi.goods_amount,oi.type,oi.in_online,oi.uid';
        if($postdata['keyword']){
	        $map['a.goods_sn|a.goods_name'] = array('like','%'.$postdata['keyword'].'%');
        }
        $map['a.status'] = array('not in','4,5');
        $order = 'a.id desc';
        $order_goods = page_list('OrderGoods',$map,$order,$field,$size,$join);
        $all_og = M('OrderGoods')->alias('a')->where($map)->join('__ORDER_INFO__ oi ON a.order_id=oi.id')->field("sum(a.goods_number) as goods_num,sum(a.goods_price) as goods_price,sum(a.goods_number*a.goods_price) as all_money,sum(a.profit) as all_profit")->find();
        $this->assign($order_goods);
        $this->assign($all_og);
        $this->add_getcateTree($postdata['catid'], '');
        $this->assign('sale_type',$sale_type);
        $this->assign($postdata);
        $this->display();
    }
    private function shop_lists($shop_id){
        $where_s=array();
        $where_s['status']=1;
        $shops_info = M('Shops')->where($where_s)->order('convert(name using gb2312) ASC')->field('id,name')->select();
        $shops_op = '';
        if (!empty($shops_info)) {
			$shops_list = array();
            foreach ($shops_info as $k => $v) {
                // if ($postdata['shop_id'] == $v['id']) {
                if ($shop_id && $shop_id == $v['id']) {
                    $shops_op .= "<option selected='selected' value='" . $v['id'] . "'>" . $v['name'] . "</option>";
                } else {
                    $shops_op .= "<option value='" . $v['id'] . "'>" . $v['name'] . "</option>";
                }
				$shops_list[$v['id']] = $v['name'];
            }
			$this->assign('shops_list', $shops_list);
        }
        $this->assign('select_shops', $shops_op);
	}
	/**
     * 得到节点树
     */
    private function add_getcateTree($pid = '', $assign = ''){
		$GoodsCategory = getcache('PurchaseCategory');
        $Tree = new \Common\Vendor\Tree();
        $select_categorys = '';
        $treemini = $Tree->treemini($GoodsCategory);
        $select_categorys .= $Tree->treeminioption($treemini, $pid);
        if ($assign) return $select_categorys; else $this->assign('select_categorys', $select_categorys);
	}
}