<?php
/**
 * 订单管理
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author zhaoh <zhao@qq.com>
 */
namespace Supplier\Controller;
use Think\Controller;
class OrderController extends BaseController {
	/**
	 * 订单管理
	 */
    public function index(){
		$postdata=I('param.');
		$tabid=$postdata['tabid'];
		if(empty($tabid)){
			$all_order=D('Common/Suppliers')->supplier_all_order($postdata,'','',10);
		}elseif($tabid=='no_pay'){
			$all_order=D('Common/Suppliers')->supplier_all_order($postdata,'no_pay');
		}elseif($tabid=='no_ship'){
			$all_order=D('Common/Suppliers')->supplier_all_order($postdata,'no_ship');
		}elseif($tabid=='yes_ship'){
			$all_order=D('Common/Suppliers')->supplier_all_order($postdata,'yes_ship');
		}elseif($tabid=='no_comment'){
			$all_order=D('Common/Suppliers')->supplier_all_order($postdata,'no_comment');
		}elseif($tabid=='no_group'){
			$all_order=D('Common/Suppliers')->supplier_all_order($postdata,'no_group');
		}
		$tabid=$tabid?$tabid:'index';
		$this->assign($all_order);
		$this->assign($postdata);
        $this->display($tabid);
    }
	/**
	 * 发货弹窗
	 */
	public function ship_info(){
		$order_id=I('id');
		$order_info=M('OrderInfo')->where('id='.$order_id)->field('order_sn,id')->find();
		$this->assign($order_info);
		$shipping_list=M('Shipping')->where('status=1')->field('code,name')->select();
        foreach($shipping_list as $k=>$v){
            $shipping_info[$v['code']]=$v['name'];
        }
        $this->assign('shipping',$shipping_info);
		$this->display();
	}
	/**
	 * 供货商发货
	 */
	public function ship(){
		$postdata=I('param.');
		$postdata['admin_id']=MID;
		$postdata['type']='Supplier';//供货商发货日志
		$postdata['order_type']=10;
		$return=D('Common/OrderInfo')->ship_goods($postdata);
		$this->ajaxReturn($return);
	}
	/**
	 * 修改运费
	 */
	public function change_ship_free(){
		$id=I('param.id');
		//订单信息
		$info=M('OrderInfo')->where('id='.$id)->field('id,order_sn,goods_amount,order_amount,delivery_fee')->find();
		//付款单信息
		$where['is_delete'] = 0;
		$where['order_id'] = array("like", "%," . $id . ",%");
		$need_pay = M("PayLog")->where($where)->getField('need_pay');
		$info['order_amount']=$info['order_amount']>$need_pay?$need_pay:$info['order_amount'];
		$this->assign($info);
		$this->display();
	}
	/**
	 * 修改运费
	 */
	public function change_ship(){
		$post=I('param.');
		$post['admin_id']=MID;
		$post['type']='Shop';//供货商修改运费日志
		$return=D('Common/OrderInfo')->change_ship_free($post);
		$this->ajaxReturn($return);
	}
	/**
	 * 配送方式
	 */
    public function shipping(){
		$postdata=I('param.');
		$tabid=$postdata['tabid'];
		if(empty($tabid)){$list=D('Shipping')->ship_list();$this->assign($list);}
		$tabid=$tabid?$tabid:'shipping';
        $this->display($tabid);
    }
	/**
	 * 编辑配送方式
	 */
    public function edit_shipping(){
		$postdata=I('param.');
		$tabid=$postdata['tabid'];
		if(!$postdata['id']){$this->redirect('Order/shipping');}
		$ship_info=D('Shipping')->ship_info($postdata);
		$this->assign($ship_info);
		
		$tabid=$tabid?$tabid:'edit_shipping';
		$this->assign('menu_action_current','shipping');
        $this->display($tabid);
    }
	/**
	 * 添加配送方式
	 */
    public function add_ship(){
		$postdata=I('param.');
		$return=D('Shipping')->add_ship($postdata);
		if($return['status']){$return['url']=U('Shop/Order/shipping');}
        $this->ajaxReturn($return);
    }
	/**
	 * 修改配送方式
	 */
    public function update_ship(){
		$postdata=I('param.');
		$return=D('Shipping')->update_ship($postdata);
		if($return['status']){$return['url']=U('Shop/Order/shipping');}
        $this->ajaxReturn($return);
    }
	/**
	 * 删除配送方式
	 */
    public function del_ship(){
		$postdata=I('param.');
		$return=D('Shipping')->del_ship($postdata);
		if($return['status']){$return['url']=U('Shop/Order/shipping');}
        $this->ajaxReturn($return);
    }
	/**
	 * 退款退货
	 */
    public function back(){
		$postdata=I('param.');
		$tabid=$postdata['tabid'];
		if(empty($tabid)){
			$return=D('ReturnGoods')->return_list();$this->assign($return);
		}elseif($tabid=='refund'){
			$return=D('ReturnGoods')->refund_list();$this->assign($return);
		}
		$tabid=$tabid?$tabid:'back';
        $this->display($tabid);
    }
	/**
	 * 退货的确认收货
	 */
    public function confirm_goods(){
		$post['id']=I('id');
		$post['admin_id']=MID;//操作日志用
		$post['type']='Shop';//操作日志用
		$return=D('Common/OrderInfo')->confirm_goods($post);
        $this->ajaxReturn($return);
    }
    /**
     * 订单详情
     */
    public function info(){
        $id = I('param.id');
        $postdata=I('param.');
        $tabid=$postdata['tabid'];
        if(empty($id)){$this->redirect('Order/index');}
        $map['shop_id']=$this->supplier_info['id'];
        $map['id']=$id;
        $order_info = M('OrderInfo')->where($map)->field(true)->find();
        if($order_info) {
            $order_info['end_time'] = $order_info['addtime'] + C('CANCEL_ORDER_TIME') * 86400;//取消订单倒计时
            $order_info['ship_time'] = M('ShipOrder')->where('order_id=' . $id)->getField('ship_time');//发货时间
            $order_info['get_time'] = M('ShipOrder')->where('order_id=' . $id)->getField('get_time');//收货时间
            $order_goods = M('OrderGoods')->where('order_id=' . $id)->field(true)->select();
			$ordershipping=M('ShipOrder')->where('order_id='.$id)->field(true)->select();
			$ordership=array();
			foreach($ordershipping as $v){
				$goodsid=explode(',',$v['goods_id']);
				foreach($goodsid as $val){
					$ordership[$val]=$v;
				}
			}
			foreach ($order_goods as $k => $v) {
				if(!empty($v['goods_attr'])){//获取属性和货号
					$attr = unserialize($v['goods_attr']);
					$order_goods[$k]['attr_name'] = $attr['name'];
					$attr_map['attr_key']=$attr['key'];
					$attr_map['goods_id']=$v['goods_id'];
					$attr_sn=M('goods_attr_stock')->where($attr_map)->getField('goods_attr_sn');
					$order_goods[$k]['attr_sn'] = $attr_sn;
				}
				$order_goods[$k]['img'] = idtoname($v['goods_id'], 'Goods', 'img');
				$order_goods[$k]['goods_integral'] = $v['goods_integral'];
				$order_goods[$k]['shipping']=$ordership[$v['goods_id']];
			}
            $invoice = unserialize($order_info['invoice_info']);//print_r($invoice);
            $this->assign($invoice);
            $this->assign($order_info);
            $this->assign('order_goods', $order_goods);
        }else{
            $this->error('无此操作权限');
        }
        $this->assign('menu_action_current', 'index');
        $tabid=$tabid?$tabid:'info';
        $this->display($tabid);
    }
    /*
    *订货单（仓库订货）
    */
    public function library(){
    	$postdata=I('param.');
		$tabid=$postdata['tabid'];
		if(empty($tabid)){
			$all_order=D('Common/Suppliers')->suppliers_all_order($postdata,1,'',10);
		}
		$tabid=$tabid?$tabid:'library';
		$this->assign($all_order);
		$this->assign($postdata);
        $this->display($tabid);
    }
    /**
	 * 发货弹窗
	 */
	public function fa(){
		$return = array('status' => 0, 'msg'=>'发货失败！' );
		$order_id=I('id');
		$out_stock = D('Admin/Storage')->all_out_stock($order_id);
        if($out_stock['status'] == 0){
            $return['msg'] = $out_stock['msg'];
			$this->ajaxReturn($return);
        } 
        $return['msg'] = '发货成功！';
        $this->ajaxReturn($return);
	}
	//打印
    public function order_print(){
       /* $id = I('get.id');//订单id
        $this->assign('id',$id);
        $order_info = M('library_info')->where('id='.$id)->field(true)->find();
		$template = '';
        switch ( $order_info['order_status'] ) {
            case  1 :
                $order_status = '订货单';
				$template = 'supp_template';
                break;                    
            case  2 :
                $order_status = '发货单';
                break;
            case  3 :
                $order_status = '配货单';
                break;
            case  4 :
                $order_status = '调拨单';
                break;
            case  5 :
                $order_status = '退货单';
                break;                    
            case  6 :
                $order_status = '入库单';
                break;
            default:
                $order_status = '';
        }
        $this->assign('order_status',$order_status);
        $where = array();
        $where['libinfo_id']=$id;
        $goods = M('library_goods')->where($where)->select();
        foreach ($goods as $k => $v) {
            $attr_key = explode('-', $v['goods_attr_sn']);
            if(!empty($attr_key[1])) {
                $attr_name = attr_key2name($attr_key[1],$attr_key[0]);
                $goods[$k]['goods_attr_sn'] = $attr_name;
            }else{
                $goods[$k]['goods_attr_sn'] = '';
            }
        }
        $this->assign('goods',$goods);
        
		$supplier_info = M('Suppliers')->where('id = '.MID)->find();
        $time = date('Y-m-d H:i:s',NOW_TIME);
        $this->assign('supplier_info',$supplier_info);
        $this->assign('print_time',$time);
        $this->assign('order_info',$order_info);
		
		if($template){
			$map = array();
			$map['code'] = $template;       
			$content = M('PrintTemplate')->where($map)->getField('content');
			$content1 = $this->fetch('',htmlspecialchars_decode($content));
			$this->assign('info',$content1);
		}
        $this->display();*/
    }
}
?>