<?php
/**
 * 提现管理
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author zhaoh <1070309178@qq.com>
 */
 
namespace Admin\Controller;
class MemberWithdrawalsController extends BaseController{
	public function _initialize() {
	parent::_initialize();
		$status_type=array(0=>'未处理',1=>'已处理',2=>'已关闭');
		$status_pay=array(1=>'未付款',2=>'付款中',3=>'已付款');
		$this->assign('status_type',$status_type);
		$this->assign('status_pay',$status_pay);
	}
	/**
	 * 会员提现列表
	 */
    public function index(){
        $name = 'MemberWithdrawals';
        $postdata=I('param.');
        if(!empty($postdata['status']))$map['status']=$postdata['status']-1;
        if(!empty($postdata['date_from']))$map['create_time'][]=array('EGT',strtotime($postdata['date_from']));
        if(!empty($postdata['date_to']))$map['create_time'][]=array('elt',strtotime($postdata['date_to']));
        if(!empty($postdata['keyword'])){
            $whereor['uid']=$postdata['keyword'];
            $whereor['msg']=array('like','%'.$postdata['keyword'].'%');
            $whereor['_logic'] = 'or';
            $map['_complex'] = $whereor;
        }
		$map['type']=0;
		$this->get_status($postdata['status']);
        $list = $this->lists($name,$map);
		
		//账期
		foreach($list as $k=>$v){
			$map_on['status']=0;
			$map_on['end_time']=array('elt',NOW_TIME+86400);
			$map_on['id']=$v['id'];
			$is_on=M($name)->where($map_on)->field('id')->find();
			if($is_on){
				$list[$k]['on']=1;
			}else{
				$list[$k]['on']=0;
			}
		}
		// 统计
		$field="sum(money) as money,sum(free) as fee,sum(money-free) as givemoney,count(id) as num";
		$all=M($name)->where($map)->field($field)->find();
		$this->assign('all',$all);

        $this->assign('list',$list);
		$this->assign($postdata);
        $this->display();
    }
	/**
	 * 店铺提现列表
	 */
    public function shop_index(){
        $name = 'MemberWithdrawals';
        $postdata=I('param.');
		// $map['type']=array('in','0,1,2');
		$map['type']=array('in','1,2');
        if(!empty($postdata['type']))$map['type']=$postdata['type'];
        if(!empty($postdata['status']))$map['status']=$postdata['status']-1;
        if(!empty($postdata['date_from']))$map['create_time'][]=array('EGT',strtotime($postdata['date_from']));
        if(!empty($postdata['date_to']))$map['create_time'][]=array('elt',strtotime($postdata['date_to']));
        if(!empty($postdata['keyword'])){
            $whereor['uid']=$postdata['keyword'];
            $whereor['msg']=array('like','%'.$postdata['keyword'].'%');
            $whereor['_logic'] = 'or';
            $map['_complex'] = $whereor;
        }
		$this->get_status($postdata['status']);
        $list = $this->lists($name,$map);
		
		//账期
		foreach($list as $k=>$v){
			$map_on['status']=0;
			$map_on['end_time']=array('elt',NOW_TIME+86400);
			$map_on['id']=$v['id'];
			$is_on=M($name)->where($map_on)->field('id')->find();
			if($is_on){
				$list[$k]['on']=1;
			}else{
				$list[$k]['on']=0;
			}
		}
		// 统计
		$field="sum(money) as all_money,sum(free) as all_fee,sum(money-free) as all_givemoney,count(id) as num";
		$all=M($name)->where($map)->field($field)->find();
		$this->assign('all',$all);

        $this->assign('list',$list);
		$this->assign($postdata);
        $this->display();
    }
	/**
	 * 店铺提现列表
	 */
    public function supplier_index(){
        $name = 'MemberWithdrawals';
        $postdata=I('param.');
        if(!empty($postdata['status']))$map['status']=$postdata['status']-1;
        if(!empty($postdata['date_from']))$map['create_time'][]=array('EGT',strtotime($postdata['date_from']));
        if(!empty($postdata['date_to']))$map['create_time'][]=array('elt',strtotime($postdata['date_to']));
        if(!empty($postdata['keyword'])){
            $whereor['uid']=$postdata['keyword'];
            $whereor['msg']=array('like','%'.$postdata['keyword'].'%');
            $whereor['_logic'] = 'or';
            $map['_complex'] = $whereor;
        }
		$map['type']=2;
		$this->get_status($postdata['status']);
        $list = $this->lists($name,$map);
		
		//账期
		foreach($list as $k=>$v){
			$map_on['status']=0;
			$map_on['end_time']=array('elt',NOW_TIME+86400);
			$map_on['id']=$v['id'];
			$is_on=M($name)->where($map_on)->field('id')->find();
			if($is_on){
				$list[$k]['on']=1;
			}else{
				$list[$k]['on']=0;
			}
		}
        $this->assign('list',$list);
		$this->assign($postdata);
        $this->display();
    }
	/**
	 * 提现信息
	 */
	public function edit(){
        $id=I('param.id');
       $mw_type = M('MemberWithdrawals')->where('id='.$id)->getField('type');

        $map['mw.id'] = $id;
        $map['mb.type'] = $mw_type;
        // $info=M('MemberWithdrawals')->alias('mw')->join('JOIN __MEMBER_BANK__ mb ON mw.uid=mb.uid')->where($map)->field('mw.* , mb.name,mb.bankname,mb.bankcode,mb.bankkai,mb.temp')->find();
        
        // jxr 18.09.06 start
        if($mw_type==0||$mw_type==1){	//18.11.06 +type=1 
        	$info=M('MemberWithdrawals')->find($id);
        }else{
        	$info=M('MemberWithdrawals')->alias('mw')->join('JOIN __MEMBER_BANK__ mb ON mw.uid=mb.uid')->where($map)->field('mw.* , mb.name,mb.bankname,mb.bankcode,mb.bankkai,mb.temp')->find();
        }
		// jxr 18.09.06 end
        $this->assign('info',$info);
        $this->display();
    }
	/**
	 * 处理会员提现
	 */
	public function tixian(){
        $postdata=I('param.');if(!$postdata['id'])$this->error('参数错误!');
		$map['id']=$postdata['id'];
		$map['status']=0;
		$info=M('MemberWithdrawals')->where($map)->field(true)->find();
		if(empty($info))$this->error('已处理!');
		$pay_money=$info['money']-$info['free'];
		if($postdata['status']==1){//同意提现
			$save['status']=1;
			// 解冻资金
			account_change($info['uid'],-$info['money'],2,2,'提现成功，解冻资金'.$info['money'],$postdata['adminid']);
			//提现到余额
			if($info['withdraw_status'] == 2){
				account_change($info['uid'],$info['money'],1,2,'会员'.idtoname($info['uid'],'Member','username','id',false).'提现佣金'.$info['money'].'元至余额');
			}else{
			//平台出账日志
			account_change($info['uid'],-$pay_money,1,2,'会员提现（平台出账）',$postdata['adminid'],false,3);
			//手续费入账
			if($info['free']>0)account_change($info['uid'],$info['free'],1,2,'会员提现手续费（平台入账）',$postdata['adminid'],false,4);
			}

		}else{
			$save['status']=2;
			account_change($info['uid'],-$info['money'],2,2,'提现失败，冻结资金'.-$info['money'].'元',$postdata['adminid']);
			account_change($info['uid'],$info['money'],8,2,'提现失败，返回佣金'.+$info['money'].'元',$postdata['adminid']);
		}
		$save['update_time']=NOW_TIME;
		$save['adminid']=$postdata['adminid'];
		$result=M('MemberWithdrawals')->where('id='.$postdata['id'])->save($save);
		if($result){
			action_log('do_shop_tixian', 'Admin', $postdata['id'], $postdata['adminid']);//操作日志
			$url=U('MemberWithdrawals/index');
			$this->success('处理成功！',$url);
		}else{
			$url=U('MemberWithdrawals/edit',array('id'=>$postdata['id']));
			$this->success('处理失败！',$url);
		}
		
    }
	/**
	 * 处理店铺提现
	 */
	public function shop_tixian(){
        $postdata=I('param.');if(!$postdata['id'])$this->error('参数错误!');
		$map['id']=$postdata['id'];
		$map['status']=0;
		$info=M('MemberWithdrawals')->where($map)->field(true)->find();
		if(empty($info))$this->error('已处理!');
		$pay_money=$info['money']-$info['free'];
		
		if($postdata['status']==1){//同意提现
			$save['status']=1;
			//平台出账日志
			account_change($info['uid'],-$pay_money,1,2,'店铺提现（平台出账）',$postdata['adminid'],false,3);
			//手续费入账
			if($info['free']>0)account_change($info['uid'],$info['free'],1,2,'店铺提现手续费（平台入账）',$postdata['adminid'],false,4);
		}else{
			$save['status']=2;
			shop_balance_log($info['uid'],$info['money'],1,2,'提现失败，返回资金','',UID);
			shop_balance_log($info['uid'],-$info['money'],4,2,'提现失败，解冻资金','',UID);
		}
		$save['update_time']=NOW_TIME;
		$save['adminid']=$postdata['adminid'];
		$result=M('MemberWithdrawals')->where('id='.$postdata['id'])->save($save);
		if($result){
			shop_balance_log($info['uid'],-$info['money'],4,2,'提现成功，解冻资金','',UID);
			action_log('do_shop_tixian', 'Admin', $postdata['id'], $postdata['adminid']);//操作日志
			$url=U('MemberWithdrawals/shop_index');
			$this->success('处理成功！',$url);
		}else{
			$url=U('MemberWithdrawals/edit',array('id'=>$postdata['id']));
			$this->success('处理失败！',$url);
		}
		
    }
	/**
	 * 处理供货商提现
	 */
	public function supplier_tixian(){
        $postdata=I('param.');if(!$postdata['id'])$this->error('参数错误!');
		$map['id']=$postdata['id'];
		$map['status']=0;
		$info=M('MemberWithdrawals')->where($map)->field(true)->find();
		if(empty($info))$this->error('已处理!');
		$pay_money=$info['money']-$info['free'];
		
		if($postdata['status']==1){//同意提现
			$save['status']=1;
			//平台出账日志
			account_change($info['uid'],-$pay_money,1,2,'供货商提现（平台出账）',$postdata['adminid'],false,3);
			//手续费入账
			if($info['free']>0)account_change($info['uid'],$info['free'],1,2,'供货商提现手续费（平台入账）',$postdata['adminid'],false,4);
		}else{
			$save['status']=2;
			supplier_balance_log($info['uid'],$info['money'],1,2,'提现失败，返回资金','',UID);
			supplier_balance_log($info['uid'],-$info['money'],4,2,'提现失败，解冻资金','',UID);
		}
		$save['update_time']=NOW_TIME;
		$save['adminid']=$postdata['adminid'];
		$result=M('MemberWithdrawals')->where('id='.$postdata['id'])->save($save);
		if($result){
			supplier_balance_log($info['uid'],-$info['money'],4,2,'提现成功，解冻资金','',UID);
			action_log('do_supplier_tixian', 'Admin', $postdata['id'], $postdata['adminid']);//操作日志
			$url=U('MemberWithdrawals/supplier_index');
			$this->success('处理成功！',$url);
		}else{
			$url=U('MemberWithdrawals/edit',array('id'=>$postdata['id']));
			$this->success('处理失败！',$url);
		}
		
    }
	/**
	 * 列表页提现
	 */
	public function do_tixian(){
		$return=array('status'=>0,'info'=>'');
        $postdata=I('param.');if(!$postdata['id']){$return['info']='参数错误';return $return;}
		$map['id']=$postdata['id'];
		$map['status']=0;
		$info=M('MemberWithdrawals')->where($map)->field(true)->find();
		if(empty($info)){$return['info']='已处理';return $return;}
		
		$save['status']=1;
		$save['update_time']=NOW_TIME;
		$save['adminid']=$postdata['adminid'];
		$result=M('MemberWithdrawals')->where('id='.$postdata['id'])->save($save);
		if($info['withdraw_status'] == 2){//提现至余额
			account_change($info['uid'],-$info['money'],2,2,'提现成功，解冻资金'.$info['money'],UID);
			account_change($info['uid'],$info['money'],1,2,'会员'.idtoname($info['uid'],'Member','username','id',false).'提现佣金'.$info['money'].'元至余额');
			$return['status']=1;
			$return['info']="<a href='javascript:;'>已处理</a>";
		
		}else{//提现至银行卡 
			if($result){
				$pay_money=$info['money']-$info['free'];
				if($info['type']==2){//供货商提现
					supplier_balance_log($info['uid'],-$info['money'],4,2,'提现成功，解冻资金','',UID);
					account_change($info['uid'],-$pay_money,1,2,'供货商提现（平台出账）',UID,false,3);
					if($info['free']>0)account_change($info['uid'],$info['free'],1,2,'供货商提现手续费（平台入账）',UID,false,4);
					action_log('do_supplier_tixian', 'Admin', $postdata['id'], UID);//操作日志
				}elseif($info['type']==1){//店铺提现
					shop_balance_log($info['uid'],-$info['money'],4,2,'提现成功，解冻资金','',UID);
					account_change($info['uid'],-$pay_money,1,2,'店铺提现（平台出账）',UID,false,3);
					if($info['free']>0)account_change($info['uid'],$info['free'],1,2,'店铺提现手续费（平台入账）',UID,false,4);
					action_log('do_shop_tixian', 'Admin', $postdata['id'], UID);//操作日志
				}else{//会员提现
					account_change($info['uid'],-$info['money'],2,2,'提现成功，解冻资金',UID);
					account_change($info['uid'],-$pay_money,1,2,'会员提现（平台出账）',UID,false,3);
					if($info['free']>0)account_change($info['uid'],$info['free'],1,2,'会员提现手续费（平台入账）',UID,false,4);
					action_log('do_user_tixian', 'Admin', $postdata['id'], UID);//操作日志
				}
				$return['status']=1;
				$return['info']="<a href='javascript:;'>已处理</a>";
			}else{
				$return['info']='处理失败';
			}
		}
		$this->ajaxReturn($return);
    }
	private function get_status($id){
		$status_type=array(0=>'请选择处理状态',1=>'未处理',2=>'已处理',3=>'已关闭');
		foreach($status_type as $k=>$v){
			if($id==$k){
				$status_selected.="<option selected='selected' value='$k'>$v</option>";
			}else{
				$status_selected.='<option value='.$k.'>'.$v.'</option>';
			}
		}
		$this->assign('status_selected',$status_selected);
	}



	// 会员充值
	public function member_recharge(){
        $postdata=I('param.');
		
        $map = array();$map['type']=2;
        if(!empty($postdata['date_from']))$map['addtime'][]=array('EGT',strtotime($postdata['date_from']));
        if(!empty($postdata['date_to']))$map['addtime'][]=array('elt',strtotime($postdata['date_to']));
        if(!empty($postdata['keyword'])){
            $whereor['uid']=$postdata['keyword'];
            // $whereor['msg']=array('like','%'.$postdata['keyword'].'%');
            $whereor['_logic'] = 'or';
            $map['_complex'] = $whereor;
        }

        $member_list = M('OrderInfo')->where($map)->field(true)->order('id desc')->select();

        $this->assign('member_list',$member_list);
        $this->assign($postdata);
        $this->display();
    }
    /**
	 * 会员充值信息
	 */
	public function member_edit(){
        $id=I('param.id');
        $map['id'] = $id;
        $map['type'] = 2;
        $member_info = M('OrderInfo')->where($map)->field(true)->find();
        $this->assign($member_info);
        $this->display();
    }
    // 仓库充值
	public function shops_recharge(){
        $map['type']=5;

		$postdata=I('param.');
        if(!empty($postdata['date_from']))$map['addtime'][]=array('EGT',strtotime($postdata['date_from']));
        if(!empty($postdata['date_to']))$map['addtime'][]=array('elt',strtotime($postdata['date_to']));
        if(!empty($postdata['keyword'])){
            $whereor['uid']=$postdata['keyword'];
            $whereor['msg']=array('like','%'.$postdata['keyword'].'%');
            $whereor['_logic'] = 'or';
            $map['_complex'] = $whereor;
        }

        $shops_list = M('OrderInfo')->where($map)->field(true)->select();

        $this->assign('shops_list',$shops_list);
        $this->assign($postdata);
        $this->display();
    }
    /**
	 * 仓库充值信息
	 */
	public function shops_edit(){
        $id=I('param.id');
        if($id){
	        $map['id'] = $id;
	        $map['type'] = 5;
	        $shop_info = M('OrderInfo')->where($map)->field(true)->find();
	        $this->assign($shop_info);	
        }
        $this->display();
    }
    public function recharge_delete(){exit();
    	$id=I('param.id');
    	if($id){
    		$map['id'] = $id;
    		$result = M('OrderInfo')->where($map)->delete();
    		if($result){
    			$this->success('删除成功');
    		}else{
    			$this->error('删除失败');
    		}
    	}
    }
}
?>