<?php
/**
 * 营销报表
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaogg <xiaogg@sina.cn>
 */
 
namespace Admin\Controller;
class MarketingFormController extends BaseController {
    public function _initialize(){
        parent::_initialize();
        $this->account_type=strtoarray(C('ACCOUNT_TYPE'));
        $this->assign('account_type',$this->account_type);
        $this->account_status=strtoarray(C('ACCOUNT_STATUS'));$this->assign('account_status',$this->account_status);
        $change_types = array(0=>'增加',1=>'减少');$this->assign('change_types',$change_types);
    }
    /**
    * 余额明细列表
    */
    public function index(){
        $ac_type=$this->account_type;
        unset($ac_type['2']);unset($ac_type['4']);
        $this->assign('account_type',$ac_type);
        $postdata=I('param.');
        $this->GetList($postdata['type'],$postdata);
    }
    /**
    * 积分明细列表
    */
    public function points(){
        $postdata=I('param.');
        $this->pointslist(4,$postdata);
    }
    /**
     * 核销明细列表
     */
    public function hx(){
        $map = array();
        $postdata = I('param.');
        $uid=trim(I('param.uid'));
        if($uid)$map['uid'] = $uid;
        $shopsData = M('Shops')->where('status=1 and shop_type=1')->field('id,name')->select();
        $shopsData = format_array2select($shopsData);
        $this->assign('shopsData',$shopsData);

        if($postdata['shop_id'])$map['shop_id'] = $postdata['shop_id'];
        if(!empty($postdata['reg_from']))$map['addtime'][]=array('EGT',strtotime($postdata['reg_from']));
        if(!empty($postdata['reg_to']))$map['addtime'][]=array('elt',strtotime($postdata['reg_to']));
        $list = $this->lists('PackageHx',$map);
        $this->assign('list',$list);
        $this->assign($postdata);
        $this->display();
    }
    public function delete(){
        $mod=D('AccountLog');
        $id=$this->id?$this->id:I('id');
        if (isset($id) && is_array($id)) {
	  $ids = implode(',', $id);
	  $result=$mod->delete($ids);
        } else {
	  $id = intval($id);
	  $result=$mod->delete($id);
        }
        if($result!==false){
	  if(in_array(CONTROLLER_NAME,$this->cache_model)){savecache(CONTROLLER_NAME);}
	  $this->success(L('DO_OK'));
        }else{$this->error(L('DO_ERROR'));}
    }
    private function GetList($type='',$postdata=array()){
        $t_map=$xh_map=$c_map=$money_map=$map = array();
        if($postdata['fhid'])$map['storeid'] = $postdata['fhid']; //jxr 2019.1.28
        if($postdata['status'])$map['status'] = $postdata['status'];
        if($postdata['uid'])$t_map['uid']=$xh_map['uid']=$c_map['uid']=$money_map['id']=$map['uid'] = $postdata['uid'];
        if($type){
	       $map['type'] = array('eq',$type);
        }else{
	       $map['type'] = array('not in','4,7,3');//去掉积分，累计充值,累计消费
        }
        if($postdata['reg_from'] || $postdata['reg_to']){
            $date_from = strtotime($postdata['reg_from']);
            if($date_from){
                $map['change_time'][] = array('egt',$date_from);
                $xh_map['addtime'][]=array('egt',$date_from);
                $t_map['addtime']=array('egt',$date_from);
                $c_map['addtime'][]=array('egt',$date_from);
            }
            $date_to = strtotime($postdata['reg_to']);
            if($date_to){
                $date_to = $date_to+86399;
                $map['change_time'][] = array('lt',$date_to);
                $xh_map['addtime'][]=array('lt',$date_to);
                $t_map['addtime']=array('lt',$date_to);
                $c_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['change_time']=array('between',array($between[0],$between[1]));
                $xh_map['addtime']=array('between',array($between[0],$between[1]));
                $t_map['addtime']=array('between',array($between[0],$between[1]));
                $c_map['addtime']=array('between',array($between[0],$between[1]));
            }else{
                $postdata['dates']=99;
            }
        }
        if($postdata['change_type'] === '0' || $postdata['change_type'] == 1)$map['change_type'] = array('eq',$postdata['change_type']);
        $map['role']=0;
        $list = $this->lists('AccountLog',$map);
        foreach ($list as $key => $value) {
            if(intval($value['status'])==15)$list[$key]['status']=5;
        }
//当前余额
        $d_money = M('Member')->where($money_map)->sum('money');
//充值
        $c_map['type']=array('in','2,5');
        $chongzhi = M('OrderInfo')->where($c_map)->field('sum(order_amount) as money,count(id) as num')->find();
//消耗次数
        $xh_map['used_money']=array('gt',0);
        $xh_map['status']=array('in','1,2,3,6,7,8,9');
        $xh = M('OrderInfo')->where($xh_map)->field('sum(used_money) as used_money,count(id) as num')->find();
//退款
        $t_map['status']=1;
        $t_info=M('RefundOrder')->where($t_map)->field('sum(refund_money) as refund_money')->find();
//佣金
        unset($map['type'],$map['change_type']);
        $map['type']=8;$map['change_type']=1;
        $y_money = M('AccountLog')->where($map)->sum('change_num');
        $this->assign('d_money',$d_money);
        $this->assign('chongzhi',$chongzhi);
        $this->assign('xh',$xh);
        $this->assign('t_info',$t_info);
        $this->assign('list',$list);
        $this->assign($postdata);
        $this->display();
    }
	private function pointslist($type='',$postdata=array()){
		$account_status=strtoarray(C('ACCOUNT_STATUS'));
		unset($account_status[1]);unset($account_status[2]);
		$this->assign('account_status',$account_status);
		$dk_map=$d_map=$map = array();
		if($postdata['status'])$map['status'] = $postdata['status'];
		if($postdata['uid'])$dk_map['uid']=$d_map['id']=$map['uid'] = $postdata['uid'];
		$map['type']=4;
		if($postdata['reg_from'] || $postdata['reg_to']){
            $date_from = strtotime($postdata['reg_from']);
            if($date_from){
                $map['change_time'][] = array('egt',$date_from);
                $dk_map['addtime'][] = array('egt',$date_from);
            }
            $date_to = strtotime($postdata['reg_to']);
            if($date_to){
                $date_to = $date_to+86399;
                $map['change_time'][] = array('lt',$date_to);
                $dk_map['addtime'][] = array('egt',$date_from);
            }
            $postdata['dates']=0;
        }else{
            if(in_array($postdata['dates'],array(1,2,7,30))){
                $between=quick_time($postdata['dates']);
                $map['change_time']=array('between',array($between[0],$between[1]));
                $dk_map['addtime'] = array('between',array($between[0],$between[1]));
            }else{
                $postdata['dates']=99;
            }
        }
		if($postdata['change_type'] === '0' || $postdata['change_type'] == 1){$map['change_type'] = $postdata['change_type'];}
		$list = $this->lists('AccountLog',$map);
        foreach ($list as $key => $value) {
            if(intval($value['status'])==15)$list[$key]['status']=5;
        }
//		当前积分
		$d_points = M('Member')->where($d_map)->sum('usepoints');

//		累计获得
		unset($map['change_type']);
		$map['change_type']=0;
		$get_points = M('AccountLog')->where($map)->sum('change_num');
//		累计消耗
		unset($map['change_type']);
		$map['change_type']=1;
		$xh_points = M('AccountLog')->where($map)->sum('change_num');

//		积分抵扣
		$dk_map['status']=3;
		$dk_points=M('OrderInfo')->where($dk_map)->field('sum(used_score) as used_score')->find();

		$this->assign('d_points',$d_points);
		$this->assign('get_points',$get_points);
		$this->assign('xh_points',$xh_points);
		$this->assign('dk_points',$dk_points);
		$this->assign('list',$list);
		$this->assign($postdata);
		$this->display();
	}

    /**资金明细
    * @param bool $is_return
    */
    public function capital_flow($is_return=false){
        $postdata = I('param.');
        $map=array();
        $now_time=strtotime(date('Y-m-d',NOW_TIME));
        if($postdata['data_from'] || $postdata['data_to']){
            $date_from = strtotime($postdata['data_from']);
            if($date_from)$map['create_time'][] = array('egt',$date_from);
            $date_to = strtotime($postdata['data_to']);
            if($date_to){$date_to = $date_to+86399;$map['create_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['create_time']=array('between',array($between[0],$between[1]));
            }else{
                $postdata['dates']=99;
            }
        }
        if($postdata['in_online']==1){$map['in_online']=0;}
        elseif($postdata['in_online']==2){$map['in_online']=1;}
        // xyy 2019.1.8 begin
        else{
            $map['in_online']=0;
        }
        // xyy 2019.1.8 end
        switch($postdata['type']){
            case 2:$map['type']=$postdata['type'];
                 break;
            case 3:$map['type']=array('in','1,3,6,10,13,14,25');
                 break;
            case 5:$map['type']=$postdata['type'];
                 break;
            case 11:$map['type']=array('in','11,12');
                 break;
            case 15:$map['type']=$postdata['type'];
                 break;
            case 20:$map['type']=array('in','20,21,31');
                 break;
            default:
                $map['type']=array('neq','30');
                break;
        }
        if(!empty($postdata['sale_id'])) $map['guide']=$postdata['sale_id'];
        if(!empty($postdata['shop_id'])) $map['shop_id']=$postdata['shop_id'];
        if(!empty($postdata['cashier_name'])) $map['cashier_name']=$postdata['cashier_name'];
        $map['is_paid']=1;//已完成
        $list=$this->lists('PayLog',$map,'id desc');
        
        $field='sum(order_total_price) as goods_allmoney,sum(use_money) as all_use_money,sum(need_pay) as all_need_pay,sum(cash_money) as all_cash_money,sum(scan_money) as all_scan_money,sum(bank_money) as all_bank_money,sum(card_money) as all_card_money,sum(use_score) as all_use_score,sum(use_score_money) as all_score_money,count(id) as all_count';
        $data=M('PayLog')->where($map)->field($field)->find();
        if($list){
            foreach ($list as $key => $value) {
                $list[$key]=D('App/ShopbalanceLog')->format_cont($value);
                if(empty($value['scan_money'])){
                    $list[$key]['scan_money']=$value['need_pay'];
                    if(floatval($value['order_total_price'])==0){
                        $list[$key]['order_total_price']=$value['need_pay'];
                        $data['goods_allmoney']+=$value['need_pay'];
                        $data['all_scan_money']+=$value['need_pay'];
                    }
                }
            }
        }
        if(empty($postdata['type'])){
                $maptui=$map;
                $maptui['type']=array('in','20,21,31');
                $datatui=M('PayLog')->where($maptui)->field($field)->find();
                if($datatui){
                $data['goods_allmoney']=$data['goods_allmoney']-$datatui['goods_allmoney']*2;
                $data['all_need_pay']=$data['all_need_pay']-$datatui['all_need_pay']*2;
                $data['all_use_money']=$data['all_use_money']-$datatui['all_use_money']*2;
                $data['all_use_score']=$data['all_use_score']-$datatui['all_use_score']*2;
                $data['all_cash_money']=$data['all_cash_money']-$datatui['all_cash_money']*2;
                $data['all_scan_money']=$data['all_scan_money']-$datatui['all_scan_money']*2;
                $data['all_bank_money']=$data['all_bank_money']-$datatui['all_bank_money']*2;
                $data['all_card_money']=$data['all_card_money']-$datatui['all_card_money']*2;
                $data['all_score_money']=$data['all_score_money']-$datatui['all_score_money']*2;
                }
        }

        //导购员列表
        $sales_list=D('Shops')->guider_list();
        //仓库列表
        $shops_list=D('Shops')->shop_list();
        //收银员列表
        $map=$cashier_list_arr=array();
        $map['role_id']=1;
        $sa__id=M('ShopsRoleAccount')->where($map)->getField('account_id',true);
        $mapg['status']=1;
        $mapg['is_delete']=0;
        if($sa__id){
	        $mapg['id']=array('in',$sa__id);
	        $cashier_list_arr=M('ShopsAccount')->where($mapg)->field('id,login_name')->select();
        }
        if($is_return==false) {
            // $pay_type=array('1'=>'线上','2'=>'线下');
            $pay_type=array('1'=>'线上');
            // $order_type=array('2'=>'储值','3'=>'商品销售','5'=>'仓库充值','11'=>'服务','15'=>'礼品卡销售','20'=>'退款');
            $order_type=array('2'=>'储值','3'=>'商品销售','11'=>'服务','15'=>'礼品卡销售','20'=>'退款');
            $this->assign('pay_type',$pay_type);
            $this->assign('order_type',$order_type);
            $this->assign('sales_list',$sales_list);
            $this->assign('shops_list',$shops_list);
            $this->assign('list', $list);
            $this->assign('data', $data);
	        $this->assign('cashier_list',$cashier_list_arr);
            $this->assign($postdata);
            $this->display();
        }else{
            $list_e=M('PayLog')->where($map)->order('id desc')->select();
            $return=array('data'=>$data,'list'=>$list_e);
            return $return;
        }
    }

    /**
     * 资金明细导出
     */
    public function export_pur(){
        $guide_list=$this->capital_flow(true);
        $title = array('序号', '仓库', '收银员名称','导购员名称','订单号','总金额','余额','现金','扫码','台卡','刷卡','使用积分','积分抵扣','下单方式','类型','变更时间');
        $data = array();
        foreach ($guide_list['list'] as $k => $v) {
            $data[$k][] = $v['id'];
            $data[$k][] = idtoname($v['shop_id'],'Shops','name','id',false);
            $data[$k][] = $v['cashier_name'];
            $data[$k][] = idtoname($v['guide'],'ShopsAccount','name','id',false);
            
            if(is_numeric($v['order_id'])){
                $order_id=$v['order_id'];
            }else{
                $orderid=explode(',',$v['order_id']);
                $order_id=$orderid[1];
            }
            $data[$k][] = format_xml_num($v['order_sn']).'('.$order_id.')';
            $data[$k][] = format_price($v['order_total_price']);
            $data[$k][] = format_price($v['use_money']);
            $data[$k][] = format_price($v['cash_money']);
            $data[$k][] = format_price($v['scan_money']);
            $data[$k][] = format_price($v['bank_money']);
            $data[$k][] = format_price($v['card_money']);
            $data[$k][] = $v['use_score'];
            $data[$k][] = format_price($v['use_score_money']);
            if($v['in_online']==0)$data[$k][] = '线上';
            elseif($v['in_online']==1)$data[$k][] = '线下';
            if($v['type']==2) $data[$k][] = '充值';
            elseif($v['type']==3) $data[$k][] = '积分购买';
            elseif($v['type']==13) $data[$k][] = '收银';
            elseif($v['type']==14) $data[$k][] = '商品购买';
            elseif($v['type']==20) $data[$k][] = '取消订单';
            elseif($v['type']==21) $data[$k][] = '退货';
            $data[$k][] = time_format($v['create_time']);
        }
        $data[]=array(
            '总计：',
            '--',
            '--',
            '--',
            '--',
            format_price($guide_list['data']['goods_allmoney']),
            format_price($guide_list['data']['all_use_money']),
            format_price($guide_list['data']['all_cash_money']),
            format_price($guide_list['data']['all_scan_money']),
            format_price($guide_list['data']['all_bank_money']),
            format_price($guide_list['data']['all_card_money']),
            format_price($guide_list['data']['all_use_score']),
            format_price($guide_list['data']['all_score_money']),
            '--',
            '--',
            '共计'.$guide_list['data']['all_count'].'条',
        );
        export_csv('资金明细.csv',$title,$data);
        unset($title, $data);
        exit;
    }
    /**订货资金明细
     * @param bool $is_return
     */
    public function order_capital_flow($is_return=false){
        $postdata = I('param.');
        $map=array();
        $now_time=strtotime(date('Y-m-d',NOW_TIME));
        if($postdata['data_from'] || $postdata['data_to']){
            $date_from = strtotime($postdata['data_from']);
            if($date_from)$map['create_time'][] = array('egt',$date_from);
            $date_to = strtotime($postdata['data_to']);
            if($date_to){$date_to = $date_to+86399;$map['create_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['create_time']=array('between',array($between[0],$between[1]));
            }else{
                $postdata['dates']=99;
            }
        }
        $map['type']=30;$map['is_paid']=1;//已完成
        if(!empty($postdata['shop_id'])) $map['shop_id_in']=$postdata['shop_id'];
        if(!empty($postdata['cashier_name'])) $map['cashier_name']=$postdata['cashier_name'];
        $list=$this->lists('PayLog',$map,'id desc');
        $list_e=M('PayLog')->where($map)->order('id desc')->select();
        $field='sum(order_total_price) as goods_allmoney,sum(use_money) as all_use_money,sum(scan_money) as all_scan_money,sum(offline_money) as all_offline_money';
        $data=M('PayLog')->where($map)->field($field)->find();
        $data['all_count']=M('PayLog')->where($map)->count();
        //仓库列表
        $shops_list=D('Shops')->shop_list();
        //收银员列表
        $map=$cashier_list_arr=array();
        $map['role_id']=1;
        $sa__id=M('ShopsRoleAccount')->where($map)->getField('account_id',true);
        $mapg['status']=1;
        $mapg['is_delete']=0;
        if($sa__id){
            $mapg['id']=array('in',$sa__id);
            $cashier_list_arr=M('ShopsAccount')->where($mapg)->field('id,login_name')->select();
        }
        if($is_return==false) {
            $this->assign('shops_list',$shops_list);
            $this->assign('list', $list);
            $this->assign('data', $data);
            $this->assign('cashier_list',$cashier_list_arr);
            $this->assign($postdata);
            $this->display();
        }else{
            $return=array('data'=>$data,'list'=>$list_e);
            return $return;
        }
    }
    /**
     * 订货资金明细导出
     */
    public function order_export_pur(){
        $guide_list=$this->order_capital_flow(true);
        // $title = array('序号', '仓库','订单号','总金额','余额','扫码','线下支付','变更时间');
        $title = array('序号', '仓库','订单号','总金额','余额','扫码','变更时间');
        $data = array();
        foreach ($guide_list['list'] as $k => $v) {
            $data[$k][] = $v['id'];
            $data[$k][] = idtoname($v['shop_id_in'],'Shops','name','id',false);

            if(is_numeric($v['order_id'])){
                $order_id=$v['order_id'];
            }else{
                $orderid=explode(',',$v['order_id']);
                $order_id=$orderid[1];
            }
            $data[$k][] = format_xml_num($v['order_sn']).'('.$order_id.')';
            $data[$k][] = format_price($v['order_total_price']);
            $data[$k][] = format_price($v['use_money']);
            $data[$k][] = format_price($v['scan_money']);
            // $data[$k][] = format_price($v['offline_money']);
            $data[$k][] = time_format($v['create_time']);
        }
        $data[]=array(
            '总计：',
            '--',
            '--',
            format_price($guide_list['data']['goods_allmoney']),
            format_price($guide_list['data']['all_use_money']),
            format_price($guide_list['data']['all_scan_money']),
            // format_price($guide_list['data']['all_offline_money']),
            '共计'.$guide_list['data']['all_count'].'条',
        );
        export_csv('订货资金明细.csv',$title,$data);
        unset($title, $data);
        exit;
    }
}
?>