<?php
/**
 * 订货退货处理
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author bxn <1090855764@qq.com>
 */
namespace Admin\Controller;
class TuiHuoController extends BaseController {
	/**
	 * 退货列表
	 */
    public function index(){
		$postdata=I('param.');
		$map['type']=1;
		if(!empty($postdata['order_sn'])){
			$order_id=idtoname($postdata['order_sn'],'OrderInfo','id','order_sn');
			if($order_id)$map['order_id']=$order_id;
		}
		if(!empty($postdata['id'])){$map['id']=$postdata['id'];}
		if(!empty($postdata['time_form']))$map['add_time'][]=array('egt',strtotime($postdata['time_form']));
		if(!empty($postdata['time_to']))$map['add_time'][]=array('elt',strtotime($postdata['time_to']));
		$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')->return_pgoods($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['margin_type']='Admin';//操作日志用
        $return=D('Common/OrderInfo')->return_goods($post);
        $this->ajaxReturn($return);
    }
	/**
	 * 退款列表
	 */
    public function refund(){
		$postdata=I('param.');
		$map['type']=1;
		if(!empty($postdata['order_sn'])){
			$order_id=idtoname($postdata['order_sn'],'OrderInfo','id','order_sn');
			if($order_id)$map['order_id']=$order_id;
		}
		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 s_refund(){
		$postdata=I('param.');
		$map['type']=2;
		if(!empty($postdata['order_sn'])){
			$order_id=idtoname($postdata['order_sn'],'OrderInfo','id','order_sn');
			if($order_id)$map['order_id']=$order_id;
		}
		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,type');
		if($info['status']){$return['msg']='退款已处理';$this->ajaxReturn($return);}
		
		//如果是退货生成的 判断退货单是否同意了
		$org_map['uid']=$info['uid'];
		
		if(strpos($info['org_id'],',')){
			$org_map['id']=array('in',$info['org_id']);
		}else{
			$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('TuiHuo/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'];
        }
        $map['id']=$id;
		$res=M('RefundOrder')->where($map)->save($org_save);
		if($res===false){$return['msg']='修改失败';$this->ajaxReturn($return);}
		
		$result=D('Common/OrderInfo')->handle_order($id,'Admin',UID,$info['type']);
		$this->ajaxReturn($result);
    }

}