<?php
/**
* 退换货处理
* @Copyright (C) 2016 汉潮 All rights reserved.
* @License http://www.hanchao9999.com
* @Author xiaogg <xiaogg@sina.cn>
*/
namespace Admin\Controller;
class TuiHuanController extends BaseController {
    public function _initialize(){
        $this->admin_info=session('admin_info');
        $map=array();
        $map['uid']=$this->admin_info['id'];
        $this->auth_group_id=M("AuthGroupAccess")->where($map)->getField('group_id');
        /*$is_auth=check_use_auth("WEBSHOP");
        if($is_auth['status']==0){
            $this->error('该版本没有此权限',$is_auth['url']);return;
        }*/
    }
      /**
      * 退货列表
      */
      public function index(){
	$postdata=I('param.');
        /*$goods_id = intval($postdata['goods_id']);
        if(!empty($goods_id)) {$map['goods_id'] = $goods_id;}*/
        $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);
        $this->assign('shops_list',$shops_list);
        // $tui_source=array('0'=>'全部方式','1'=>'线下直接退','2'=>'线下订单退','3'=>'线上订单退');
        $tui_source=array('0'=>'全部方式','3'=>'线上订单退');
        $this->assign('tui_source',$tui_source);
        $map=array();
		if(!empty($postdata['order_sn'])){
			$order_id=idtoname($postdata['order_sn'],'OrderInfo','id','order_sn');
			if($order_id){$map['order_id']=$order_id;}else{$map['order_id']=-1;}
		}
        if(empty($postdata['is_first'])){
            if(empty($postdata['data_from']))$postdata['data_from'] = date('Y-m-d 00:00:00', time());
            if(empty($postdata['data_to']))$postdata['data_to'] = date('Y-m-d 23:59:59', time());
        }
        if ($postdata['data_from']) {$map['add_time'][] = array('egt', strtotime($postdata['data_from']));}
        if ($postdata['data_to']) {$map['add_time'][] = array('elt', strtotime($postdata['data_to']));}
		//$map['type'] = 0;
        if($postdata['source']==1){$map['type']=0;}
        if($postdata['source']==2){$map['type']=1;}
        if($postdata['source']==3){$map['type']=2;}
        if(empty($postdata['source'])){$map['type']=2;}  //xyy 19118
		//仓库管理员
		if($this->auth_group_id==2){
			$map['shop_id']=$this->admin_info['shop_id'];
		}
        if(!empty($postdata['shop_id']))$map['shop_id']=$postdata['shop_id'];
		$list=$this->lists('ReturnGoods',$map);
		if($list){
			foreach($list as $k=>$v){
				$str='';
				$goods_info=unserialize($v['goods_info']);
				$list[$k]['goods_info']=$goods_info;
				$list[$k]['str']=idtoname($goods_info['goods_id'],'Goods','goods_name');
                if($v['rorder_id']>0)$order_type=idtoname($v['rorder_id'],'RefundOrder','order_type');
                if($v['order_id']>0) $order_type=idtoname($v['order_id'],'RefundOrder','order_type','order_id');
                if($order_type==0 || $order_type==13) $list[$k]['tui_type']='线下';
                if($order_type==14) $list[$k]['tui_type']='线上';
                if(($order_type==0 || $order_type==13) && $v['order_id']==0){$list[$k]['tui_source']='线下直接退';}
                elseif($order_type==13 && $v['order_id']>0){$list[$k]['tui_source']='线下订单退';}
                elseif($order_type==14 && $v['order_id']>0) {$list[$k]['tui_source']='线上订单退';}
                else{$list[$k]['tui_source']='线下直接退';}
			}
		}
		$this->assign('list',$list);$this->assign($postdata);
		$this->display();

	}
	/**
	 * 退货列表 外卖 服务
	 */
	public function index_sell(){
		$map=array();
		$postdata=I('param.');
		if(!empty($postdata['order_sn'])){
			$order_id=idtoname($postdata['order_sn'],'OrderInfo','id','order_sn');
			if($order_id)$map['order_id']=$order_id;
		}

		$map['type']=3;

		$map['goods_allmoney']=array('gt',0);



		$list=$this->lists('ReturnGoods',$map);
		if($list){
			foreach($list as $k=>$v){
				$str='';
				$goods_info=unserialize($v['goods_info']);
				foreach($goods_info as $ke=>$vo){
					$goods_name=idtoname($vo['goods_id'],'Goods','goods_name');
					if($ke!=0){
						$str.='&nbsp;&nbsp;|&nbsp;&nbsp;'.$goods_name;
					}else{
						$str.=$goods_name;
					}
				}
				$list[$k]['goods_info']=$goods_info;
				$list[$k]['str']=$str;
			}
		}


		$this->assign('list',$list);
		$this->assign($postdata);
		$this->display();
	}
	/**
	* 退货确认收货
	*/
	public function confirm_goods(){
		$post['id']=I('id');
		$post['admin_id']=UID;//操作日志用
		$post['type']='Admin';//操作日志用
		$return=D('Common/OrderInfo')->confirm_goods($post);
		$this->ajaxReturn($return);
	}
/**
* 同意退货
*/
    public function agree(){
        $post['id']=I('id');
        $post['type']=1;
        $post['admin_id']=UID;//操作日志用
        $post['margin_type']='Admin';//操作日志用
		$return=D('Common/OrderInfo')->tui_goods($post);
        $this->ajaxReturn($return);
    }
    public function content(){
        $id=I('id');
        $this->assign('id',$id);
        $this->display();
    }
    /**
     * 拒绝退货
     */
    public function notagree(){
        $post['id']=I('id');
        $post['type']=2;
        $post['admin_id']=UID;//操作日志用
        $post['content']=I('content');//操作日志用
        $post['margin_type']='Admin';//操作日志用
        // $return=D('Common/OrderInfo')->return_goods($post);
		$return=D('Common/OrderInfo')->tui_goods($post);
        $this->ajaxReturn($return);
    }
	/**
	* 退款列表
	*/
	public function refund(){
		$postdata=I('param.');
		if(!empty($postdata['order_sn'])){
			$order_id=idtoname($postdata['order_sn'],'OrderInfo','id','order_sn');
			if($order_id)$map['order_id']=$order_id;
		}
		$map['type']=0;
		if(!empty($postdata['time_form']))$map['addtime'][]=array('egt',strtotime($postdata['time_form']));
		if(!empty($postdata['time_to']))$map['addtime'][]=array('elt',strtotime($postdata['time_to']));
		$list=$this->lists('RefundOrder',$map);
		$this->assign('list',$list);$this->assign($postdata);
		$this->display();
	}
	/**
	 * 退款列表 外卖服务
	 */
    public function refund_sell(){
	$postdata=I('param.');
	if(!empty($postdata['order_sn'])){
		$order_id=idtoname($postdata['order_sn'],'OrderInfo','id','order_sn');
		if($order_id)$map['order_id']=$order_id;
	}
	$map['type']=3;
	if(!empty($postdata['time_form']))$map['addtime'][]=array('egt',strtotime($postdata['time_form']));
	if(!empty($postdata['time_to']))$map['addtime'][]=array('elt',strtotime($postdata['time_to']));
	$list=$this->lists('RefundOrder',$map);
	$this->assign('list',$list);$this->assign($postdata);
	$this->display();
    }
	/**
	* 编辑退款
	*/
    public function edit(){
	$id=I('id');
	$info=M('RefundOrder')->where('id='.$id)->field('id,uid,money,addtime,refund_money,refund_scores,refund_web')->find();
	$this->assign($info);
	$this->display();
    }
    /**
    * 处理退款
    */
    public function edit_refund(){
        $return=array('status'=>0,'msg'=>'');
        $postdata=I('param.');
        $id=$postdata['id'];
        //判断是否已经退款
        $info=idtoname($id,'RefundOrder','status,uid,order_id,order_goods');
        if($info['status']){$return['msg']='退款已处理';$this->ajaxReturn($return);}
        //如果是退货生成的 判断退货单是否同意了
        $org_map['uid']=$info['uid'];
        $org_map['order_id']=$info['order_id'];
        $org_map['org_id']=$info['order_goods'];
        $reg_info=M('ReturnGoods')->where($org_map)->field('id,status,shop_status')->find();
        if(!empty($reg_info)){
	  if($reg_info['status']==0 || $reg_info['shop_status']==0 ){
	      $return['msg']='请先处理退货单';
	      $return['url']=U('TuiHuan/index',array('id'=>$reg_info['id']));
	      $this->ajaxReturn($return);
	  }
        }
        $admin_id=UID;
        if($admin_id){
	  if(isset($postdata['money']))$org_save['refund_money']=$money=$postdata['money'];
	  if(isset($postdata['refund_scores']))$org_save['refund_scores']=$refund_scores=$postdata['refund_scores'];
	  if(isset($postdata['refund_money']))$org_save['refund_money']=$refund_money=$postdata['refund_money'];
	  if(isset($postdata['refund_web']))$org_save['refund_web']=$refund_web=$postdata['refund_web'];
	  if(isset($postdata['refund_card']))$org_save['refund_card']=$refund_card=$postdata['refund_card'];
	  if(isset($postdata['refund_cash']))$org_save['refund_cash']=$refund_cash=$postdata['refund_cash'];
        }
        $map['id']=$id;
        $res=M('RefundOrder')->where($map)->save($org_save);
        if($res===false){$return['msg']='修改失败';$this->ajaxReturn($return);}

        $result=D('Common/OrderInfo')->tui_kuan($id,'Admin',UID);
        $this->ajaxReturn($result);
    }
    public function aa(){
        $order_sn='20181207045211990';
        $uid=0;
        $tui_money=0.01;
        $res=D('Common/OrderInfo')->refund($uid,$tui_money,$order_sn,2);
        var_dump($res);
    }
}