<?php
/**
 * 交接班统计
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaogg <xiaogg@sina.cn>
 */

namespace Admin\Controller;

class TransferController extends BaseController{
    /**
    * lss 2018-9-8修改
    * @param bool $is_return
    * @return array
    */
    public function index($is_return=false){
        $postdata=I('param.');
        $map=array();
        $now_time=strtotime(date('Y-m-d',NOW_TIME));
        if($postdata['date_from'] || $postdata['date_to']){
            $date_from = strtotime($postdata['date_from']);
            if($date_from)$map['start_time'][] = array('egt',$date_from);
            $date_to = strtotime($postdata['date_to'])+86399;
            if($date_to)$map['start_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['start_time']=array('between',array($between[0],$between[1]));
            }else{
                $postdata['dates']=99;
            }
        }
        if(!empty($postdata['shop_id'])) $map['shop_id']=$postdata['shop_id'];
        if(!empty($postdata['cashier_name'])) $map['cashier_name']=$postdata['cashier_name'];
        $list=$this->lists('TurnOver',$map,'id desc');
        $list_e=M('TurnOver')->where($map)->select();
        $data=array();
        $field='sum(all_price) as total_price,sum(all_num) as total_num,sum(re_price) as total_re_price,sum(re_num) as total_re_num,sum(recharge_price) as total_recharge_price,sum(recharge_num) as total_recharge_num,sum(on_price) as total_on_price,sum(on_num) as total_on_num,sum(yue_price) as total_yue_price,sum(card_price) as total_card_price,sum(online_price) as total_online_price,sum(weixin_price) as total_weixin_price,sum(alipay_price) as total_alipay_price,sum(cash_price) as total_cash_price,sum(re_alipay_price) as total_re_alipay_price,sum(re_cash_price) as total_re_cash_price,sum(re_weixin_price) as total_re_weixin_price,sum(re_online_price) as total_re_online_price,sum(re_yue_price) as total_re_yue_price,sum(re_card_price) as total_re_card_price,sum(pre_paid_price) as total_pre_paid_price,sum(pre_paid_num) as total_pre_paid_num';
        $data=M('TurnOver')->where($map)->field($field)->find();
        $data['count_total']=M('TurnOver')->where($map)->count();
        $map=array();
        $map['status']=1;
        $shops_list=M('Shops')->where($map)->field('id,name')->order('name')->cache(true)->select();
        $shops_list=format_array2select($shops_list);
        if($is_return==false) {
            $this->assign('shops_list', $shops_list);
            $this->assign('list', $list);
            $this->assign('data',$data);
            $this->assign($postdata);
            $this->display();
        }else{
            $return=array('data'=>$data,'list'=>$list_e);
            return $return;
        }
    }

    /**
     * lss 2018-9-8修改
     */
    public function export_pur(){
        $guide_list=$this->index(true);
        $title = array('序号', '开始时间', '结束时间','仓库','收银员','总销售金额','总订单数','总退款金额','总退款数','会员充值金额','会员充值数','网上订单总金额','网上订单总数','礼品卡订单总金额','礼品卡订单总数','余额','现金','刷卡','线上支付','微信','支付宝','现金退款','余额退款','打印次数');
        $data = array();
        foreach ($guide_list['list'] as $k => $v) {
            $data[$k][] = $v['id'];
            $data[$k][] = time_format($v['start_time']);
            $data[$k][] = time_format($v['end_time']);
            $data[$k][] = idtoname($v['shop_id'],'Shops','name','id',false);
            $data[$k][] = $v['cashier_name'];
            $data[$k][] = $v['all_price'];
            $data[$k][] = $v['all_num'];
            $data[$k][] = $v['re_price'];
            $data[$k][] = $v['re_num'];
            $data[$k][] = $v['recharge_price'];
            $data[$k][] = $v['recharge_num'];
            $data[$k][] = $v['on_price'];
            $data[$k][] = $v['on_num'];
            $data[$k][] = $v['pre_paid_price'];
            $data[$k][] = $v['pre_paid_num'];
            $data[$k][] = $v['yue_price'];
            $data[$k][] = $v['cash_price'];
            $data[$k][] = $v['card_price'];
            $data[$k][] = $v['online_price'];
            $data[$k][] = $v['weixin_price'];
            $data[$k][] = $v['alipay_price'];
            $data[$k][] = $v['re_cash_price'];
            $data[$k][] = $v['re_yue_price'];
            $data[$k][] = $v['print_num'];
        }
        $data[]=array(
            '总计：',
            '--',
            '--',
            '--',
            '--',
            $guide_list['data']['total_price'],
            $guide_list['data']['total_num'],
            $guide_list['data']['total_re_price'],
            $guide_list['data']['total_re_num'],
            $guide_list['data']['total_recharge_price'],
            $guide_list['data']['total_recharge_num'],
            $guide_list['data']['total_on_price'],
            $guide_list['data']['total_on_num'],
            $guide_list['data']['total_yue_price'],
            $guide_list['data']['total_cash_price'],
            $guide_list['data']['total_card_price'],
            $guide_list['data']['total_online_price'],
            $guide_list['data']['total_weixin_price'],
            $guide_list['data']['total_alipay_price'],
            $guide_list['data']['total_re_cash_price'],
            $guide_list['data']['total_re_yue_price'],
            '共计'.$guide_list['data']['count_total'].'条',
        );
        export_csv('交接班明细.csv',$title,$data);
        unset($title, $data);
        exit;
    }
    /**收银日报表
     *
    */
    public function cashier($is_return=false){
        $postdata=I('param.');
        $map=array();
        $now_time=strtotime(date('Y-m-d',NOW_TIME));
        if($postdata['date_from'] || $postdata['date_to']){
            $date_from = strtotime($postdata['date_from']);
            if($date_from)$map['start_time'][] = array('egt',$date_from);
            $date_to = strtotime($postdata['date_to'])+86399;
            if($date_to)$map['start_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['start_time']=array('between',array($between[0],$between[1]));
            }else{
                $postdata['dates']=99;
            }
        }
        if(!empty($postdata['shop_id'])) $map['shop_id']=$postdata['shop_id'];
        if(!empty($postdata['cashier_name'])) $map['cashier_name']=$postdata['cashier_name'];
//        $list=$this->lists('TurnOver',$map,'id desc');
        $field="sum(all_price) as total_price,sum(all_num) as total_num,sum(re_price) as total_re_price,sum(re_num) as total_re_num,sum(recharge_price) as total_recharge_price,sum(recharge_num) as total_recharge_num,sum(on_price) as total_on_price,sum(on_num) as total_on_num,sum(yue_price) as total_yue_price,sum(card_price) as total_card_price,sum(online_price) as total_online_price,sum(weixin_price) as total_weixin_price,sum(alipay_price) as total_alipay_price,sum(cash_price) as total_cash_price,sum(re_alipay_price) as total_re_alipay_price,sum(re_cash_price) as total_re_cash_price,sum(re_weixin_price) as total_re_weixin_price,sum(re_online_price) as total_re_online_price,sum(re_yue_price) as total_re_yue_price,sum(re_card_price) as total_re_card_price,sum(print_num) as total_print,start_time,end_time,shop_id";
        $list=M('TurnOver')->where($map)->field($field)->group("DATE_FORMAT(FROM_UNIXTIME(start_time),'%Y年%m月%d天')")->order('start_time desc')->select();
        $data=array();
        $field2="sum(all_price) as total_price,sum(all_num) as total_num,sum(re_price) as total_re_price,sum(re_num) as total_re_num,sum(recharge_price) as total_recharge_price,sum(recharge_num) as total_recharge_num,sum(on_price) as total_on_price,sum(on_num) as total_on_num,sum(yue_price) as total_yue_price,sum(card_price) as total_card_price,sum(online_price) as total_online_price,sum(weixin_price) as total_weixin_price,sum(alipay_price) as total_alipay_price,sum(cash_price) as total_cash_price,sum(re_alipay_price) as total_re_alipay_price,sum(re_cash_price) as total_re_cash_price,sum(re_weixin_price) as total_re_weixin_price,sum(re_online_price) as total_re_online_price,sum(re_yue_price) as total_re_yue_price,sum(re_card_price) as total_re_card_price,sum(print_num) as total_print";
        $data=M('TurnOver')->where($map)->field($field2)->find();
        $map['group']="DATE_FORMAT(FROM_UNIXTIME(start_time),'%Y年%m月%d天')";
        $order='start_time desc';
        $pagelist=page_list('TurnOver',$map,$order,$field,30);
        $data['count_total']=$pagelist['count'];
        $map=array();
        $map['status']=1;
        $shops_list=M('Shops')->where($map)->field('id,name')->order('name')->cache(true)->select();
        $shops_list=format_array2select($shops_list);
        if($is_return==false) {
            $this->assign('shops_list', $shops_list);
            $this->assign($pagelist);
            $this->assign($data);
            $this->assign($postdata);
            $this->display();
        }else{
            $return=array('data'=>$data,'list'=>$list);
            return $return;
        }
    }
    /**
     * 收银日报表导出
     */
    public function export(){
        $guide_list=$this->cashier(true);
        $title = array('序号','仓库','当班日期', '开始时间', '结束时间','总销售金额','总订单数','总退款金额','总退款数','会员充值金额','会员充值数','网上订单总金额','网上订单总数','余额','现金','刷卡','线上支付','微信','支付宝','现金退款','余额退款','打印次数');
        $data = array();
        $i=0;
        foreach ($guide_list['list'] as $k => $v) {
            $i++;
            $data[$k][] = $i;
            $data[$k][] = idtoname($v['shop_id'],'Shops');
            $data[$k][] = date('Y-m-d',$v['start_time']);
            $data[$k][] = time_format($v['start_time']);
            $data[$k][] = time_format($v['end_time']);
            $data[$k][] = $v['total_price'];
            $data[$k][] = $v['total_num'];
            $data[$k][] = $v['total_re_price'];
            $data[$k][] = $v['total_re_num'];
            $data[$k][] = $v['total_recharge_price'];
            $data[$k][] = $v['total_recharge_num'];
            $data[$k][] = $v['total_on_price'];
            $data[$k][] = $v['total_on_num'];
            $data[$k][] = $v['total_yue_price'];
            $data[$k][] = $v['total_cash_price'];
            $data[$k][] = $v['total_card_price'];
            $data[$k][] = $v['total_online_price'];
            $data[$k][] = $v['total_weixin_price'];
            $data[$k][] = $v['total_alipay_price'];
            $data[$k][] = $v['total_re_cash_price'];
            $data[$k][] = $v['total_re_yue_price'];
            $data[$k][] = $v['total_print'];
        }
        $data[]=array(
            '总计：',
            '--',
            '--',
            '--',
            '--',
            $guide_list['data']['total_price'],
            $guide_list['data']['total_num'],
            $guide_list['data']['total_re_price'],
            $guide_list['data']['total_re_num'],
            $guide_list['data']['total_recharge_price'],
            $guide_list['data']['total_recharge_num'],
            $guide_list['data']['total_on_price'],
            $guide_list['data']['total_on_num'],
            $guide_list['data']['total_yue_price'],
            $guide_list['data']['total_cash_price'],
            $guide_list['data']['total_card_price'],
            $guide_list['data']['total_online_price'],
            $guide_list['data']['total_weixin_price'],
            $guide_list['data']['total_alipay_price'],
            $guide_list['data']['total_re_cash_price'],
            $guide_list['data']['total_re_yue_price'],
            '共计'.$guide_list['data']['count_total'].'条',
        );
        export_csv('日结报表.csv',$title, $data);
        unset($title, $data);
        exit;
    }
}