<?php
/**
 * 订单管理
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author zhaoh <zhao@qq.com>
 */
namespace Shop\Controller;
use Think\Controller;
class OrderController extends BaseController {
	public function _initialize()
    {
        parent::_initialize();
        $shops_info=session('shops_info');
        if(empty($shops_info['id']))$this->error('身份过期，请重新登录',U('Shop/Register/shop_login'));
		$this->mendian();
		$this->order_status=strtoarray(C('ORDER_STATUS'));$this->assign('order_status',$this->order_status);
		$this->order_pay=strtoarray(C('ORDER_PAY'));$this->assign('order_pay',$this->order_pay);
		$wm_order_type=strtoarray(array('13'=>'线下','14'=>'线上','3'=>'积分商城'));$this->assign('wm_order_type',$wm_order_type);
		 $send_type_status=strtoarray(array('2'=>'同城配送','3'=>'快递发货'));$this->assign('send_type_status',$send_type_status);
    }
	/**
	 * 订单管理
	 */
    public function index(){
		$postdata=I('param.');
		$tabid=$postdata['tabid'];
		if(empty($tabid)){
			$all_order=D('Common/Shops')->shop_all_order($postdata,'','',10);
		}elseif($tabid=='no_pay'){
			$all_order=D('Common/Shops')->shop_all_order($postdata,'no_pay');
		}elseif($tabid=='no_ship'){
			$all_order=D('Common/Shops')->shop_all_order($postdata,'no_ship');
		}elseif($tabid=='yes_ship'){
			$all_order=D('Common/Shops')->shop_all_order($postdata,'yes_ship');
		}elseif($tabid=='no_comment'){
			$all_order=D('Common/Shops')->shop_all_order($postdata,'no_comment');
		}elseif($tabid=='no_group'){
			$all_order=D('Common/Shops')->shop_all_order($postdata,'no_group');
		}elseif($tabid=='no_ship'){
			$all_order=D('Common/Shops')->shop_all_order($postdata,'no_back');
		}elseif($tabid=='yes_ship'){
			$all_order=D('Common/Shops')->shop_all_order($postdata,'back');
		}
		$tabid = $tabid ? $tabid : 'index';
		$this->assign($all_order);$this->assign($postdata);
        $this->display($tabid);
    }
	/**
	 * 订单管理
	 */
    public function cashier(){
		$postdata=I('param.');
		$tabid=$postdata['tabid'];
		if(empty($tabid)){
			$all_order=D('Common/Shops')->shop_cashier_order($postdata,'','',10);
		}elseif($tabid=='no_pay'){
			$all_order=D('Common/Shops')->shop_cashier_order($postdata,'no_pay');
		}elseif($tabid=='no_ship'){
			$all_order=D('Common/Shops')->shop_cashier_order($postdata,'no_ship');
		}elseif($tabid=='yes_ship'){
			$all_order=D('Common/Shops')->shop_cashier_order($postdata,'yes_ship');
		}elseif($tabid=='no_comment'){
			$all_order=D('Common/Shops')->shop_cashier_order($postdata,'no_comment');
		}elseif($tabid=='no_group'){
			$all_order=D('Common/Shops')->shop_cashier_order($postdata,'no_group');
		}
		$tabid = $tabid ? $tabid : 'cashier';
		$this->assign($all_order);$this->assign($postdata);
        $this->display($tabid);
    }
	/**
	 * 订单管理
	 */
    public function service_order(){
    	$auth_code = 'serviceorder_list';
        $ret=D('Shop/Shops')->shop_account_option($auth_code);
        if($ret['status']!=1) {$this->error($ret['msg']);}
		$postdata=I('param.');
		$tabid=$postdata['tabid'];
		if(empty($tabid)){
			$all_order=D('Common/Shops')->shop_service_order($postdata,'','',10);
			$this->assign('title_ass','订单');
		}elseif($tabid=='no_pay'){
			$all_order=D('Common/Shops')->shop_service_order($postdata,'no_pay');
			$this->assign('title_ass','待付款订单');
		}elseif($tabid=='no_ship'){
			$all_order=D('Common/Shops')->shop_service_order($postdata,'no_ship');
			$this->assign('title_ass','未发货订单');
		}elseif($tabid=='yes_ship'){
			$all_order=D('Common/Shops')->shop_service_order($postdata,'yes_ship');
			$this->assign('title_ass','待使用订单');
		}elseif($tabid=='no_comment'){
			$all_order=D('Common/Shops')->shop_service_order($postdata,'no_comment');
			$this->assign('title_ass','待评论订单');
		}elseif($tabid=='no_group'){
			$all_order=D('Common/Shops')->shop_service_order($postdata,'no_group');
			$this->assign('title_ass','待成团订单');
		}
		// echo "<pre>";
		// print_r($all_order);exit;
		$tabid = $tabid ? $tabid : 'service_order';
		$ys=idtoname(MID,'Shops','is_hide_phone');
		foreach ($all_order['list'] as $k => $val) {
			$username=show_user_name($val['uid']);
			if(strlen($username)==11 && $ys){
				$all_order['list'][$k]['username_mobile']=str_cutmobile($username);
			}else{
				$all_order['list'][$k]['username_mobile']=$username;
			}
		}
		$this->assign($all_order);$this->assign($postdata);
        $this->display($tabid);
    }
    /**
     * 外卖订单管理
     */
    public function to_order(){
        $postdata=I('param.');
        $tabid=$postdata['tabid'];
        if($tabid=='to_info'){
            $id = intval($postdata['id']);
            $order_sn = M('OrderInfo')->where('id='.$postdata['id'])->field('order_sn,accept_time,finish_time,distribute')->find();
            if(C("DIS_TRIBUTE") == 1){
                $reqStatus = D('Common/Distribute')->details_order($order_sn);
                $this->assign('reqstatus',$reqStatus);
            }
            $this->get_info($id,14,'Order/to_order');
            if($order_sn['accept_time'])$this->assign('accept_time',$order_sn["accept_time"]);
            if($order_sn['finish_time'])$this->assign('finish_time',$order_sn["finish_time"]);
        }else{
            $this->get_order(4,$postdata);
            $tabid='to_order';
        }
        $map_pl['order_id']=array('like','%,'.$id.',%');
        $use_score_money=M('PayLog')->where($map_pl)->getfield('use_score_money');
        $this->assign('use_score_money',$use_score_money);
        $this->assign('title_ass','订单');
        $this->display($tabid);
    }
    /**
     * 获取订单详情
     * @param $id
     * @param int $type
     * @param string $url
     */
    private function get_info($id,$type=1,$url='Order/index'){
        if(empty($id)){$this->redirect($url);}
        $map['shop_id']=MID;
        $map['id']=$id;
        $map['type']=$type;
        $order_info = M('OrderInfo')->where($map)->field(true)->find();
        if($order_info) {
            $order_info['end_time'] = $order_info['addtime'] + C('CANCEL_ORDER_TIME') * 86400;//取消订单倒计时
            $other_map=array();$other_map['order_id']=$id;
            if(in_array($type,array(1,3))) {
                $ship_order=M('ShipOrder')->where($other_map)->field('ship_time,get_time')->find();
                $order_info['ship_time'] = $ship_order['ship_time'];//发货时间
                $order_info['get_time'] = $ship_order['get_time'];//收货时间
            }elseif($type==14){
                $ta_map=array();$ta_map['order_id']=$order_info['order_sn'];
                $takeaway_order=M('TakeawayOrder')->where($ta_map)->field('accept_time,fetch_time,finish_time,cancel_time,expire_time')->find();
                $order_info['accept_time']=$takeaway_order['accept_time'];//接受订单时间
                $order_info['fetch_time']=$takeaway_order['fetch_time'];//完成取货时间
                $order_info['finish_time']=$takeaway_order['finish_time'];//完成订单时间
                $order_info['cancel_time']=$takeaway_order['cancel_time'];//取消订单时间
                $order_info['expire_time']=$takeaway_order['expire_time'];//订单过期时间
            }
            if($type!=6) {//非到店付
                $order_goods = M('OrderGoods')->where($other_map)->field(true)->select();
                if(in_array($type,array(1,3))) {
                    $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'];
                        if ($order_info['type'] != 14) {
                            $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'];
                    if(in_array($type,array(1,3))) {
                        $order_goods[$k]['shipping']=$ordership[$v['goods_id']];
                    }
                }
                $this->assign('order_goods', $order_goods);
            }
            $invoice = unserialize($order_info['invoice_info']);
            $this->assign($invoice);
            $this->assign($order_info);
        }else{
            $this->error('无此操作权限');
        }
    }
    /**
     * 获取外卖订单信息
     */
    private function get_order($type=1,$postdata){
        $tabid=$postdata['tabid'];
        $postdata['shop_id']=MID;
        if(empty($tabid)){
            $all_order=D('Common/Shops')->shop_all_order($postdata,'','',10,'',$type);
        }elseif($tabid=='no_pay'){
            $all_order=D('Common/Shops')->shop_all_order($postdata,'no_pay','','','',$type);
        }elseif($tabid=='no_ship'){
            $all_order=D('Common/Shops')->shop_all_order($postdata,'no_ship','','','',$type);
        }elseif($tabid=='yes_ship'){
            $all_order=D('Common/Shops')->shop_all_order($postdata,'yes_ship','','','',$type);
        }elseif($tabid=='no_comment'){
            $all_order=D('Common/Shops')->shop_all_order($postdata,'no_comment','','','',$type);
        }
        $this->assign($all_order);$this->assign($postdata);
    }
    /**
     * 订单简单的变化，比如商家接单
     */
    public function order_change(){
        $param=I('param.');
        $return=D('Common/OrderInfo')->order_change($param);
        $this->ajaxReturn($return);
    }
	/**
	 * 发货弹窗
	 */
	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']='Shop';//店铺发货日志
		$postdata['order_type']=14;
		$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 comment(){
		$auth_code = 'comment_list';
        $ret=D('Shop/Shops')->shop_account_option($auth_code);
        if($ret['status']!=1) {$this->error($ret['msg']);}
		$g_map['shop_id'] = MID;
		$ys=idtoname(MID,'Shops','is_hide_phone');
		$dp_list = M('MemberDpList')->where($g_map)->field(true)->select();
		foreach($dp_list as $key => $value){
			$dp_list[$key]['pl_img'] = unserialize($value['dp_img']);
			$username=show_user_name($value['uid']);
			if($ys && strlen($username)==11){
				$dp_list[$key]['username']=str_cutmobile($username);
			}else{
				$dp_list[$key]['username']=$username;
			}
		}
		$this->assign('dp_list',$dp_list);
		$this->display();
	}
	/**
     * 评论是否显示
     */
    public function status(){
        $model = D('MemberDpList');
		$id     = intval(I('id'));
        $type	= I('type');
        $values = $model->where('id='.$id)->field($type)->find();
        $data[$type]=($values[$type]+1)%2;
        $res=$model->where('id='.$id)->save($data);
        $this->ajaxReturn($data[$type]);
    }
	/**
	 * 回复评论
	 */
	public function return_comment(){
		// $auth_code = 'comment_back';
  //       $ret=D('Shop/Shops')->shop_account_option($auth_code);
  //       if($ret['status']!=1) {$this->error($ret['msg']);}
		$id=I('id');
		$info=M('MemberDpList')->where('id='.$id)->field(true)->find();
		$this->assign($info);$this->display();
	}
	/**
	 * 回复评论
	 */
	public function do_return_comment(){
		$auth_code = 'comment_back';
        $ret=D('Shop/Shops')->shop_account_option($auth_code);
        if($ret['status']!=1) {$this->error($ret['msg']);}
		$postdata=I('param.');
		$info=M('MemberDpList')->where('id='.$postdata['id'])->field(true)->find();
		if($info){
			$save['reply'] = $postdata['reply'];
			$save['status'] = $postdata['status'];
			$status = M('MemberDpList')->where('id='.$postdata['id'])->save($save);
		}
		if($status){
			$return['status'] = 1;
			$return['info'] = '回复成功！';
		}else{
			$return['status'] = 0;
			$return['info'] = '回复失败！';
		}
		$this->ajaxReturn($return);
	}
	public function comment_sh(){
		$model = D('MemberDpList');
		$id = intval(I('id'));
        $type = I('type');
        $values = $model->where('id='.$id)->field($type)->find();
        $data[$type]=($values[$type]+1)%2;
        if($data[$type] = 0){
        	$data['status'] = 2;
        }
        $res=$model->where('id='.$id)->save($data);
        $this->ajaxReturn($data[$type]);
	}
	/**
	 * 退款退货
	 */
    public function back(){
		$postdata=I('param.');
		$this->assign($postdata);
		$tabid=$postdata['tabid'];$status_array=[];
		if(empty($tabid)){
			$status_array=['1'=>'未收货','2'=>'已收货'];
			$auth_code = 'back_goodslist';
	        $ret=D('Shop/Shops')->shop_account_option($auth_code);
	        if($ret['status']!=1) {$this->error($ret['msg']);}
			$this->assign('title_ass','退货');
			if($postdata['shop_status']>0){$postdata['shop_status']=$postdata['shop_status']-1;}
			$return=D('ReturnGoods')->return_list('',1,$postdata);
			foreach($return['list'] as $k=>$v){
				 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) $return['list'][$k]['tui_type']='线下';
                if($order_type==14) $return['list'][$k]['tui_type']='线上';
                if(($order_type==0 || $order_type==13) && $v['rorder_id']>0){$return['list'][$k]['tui_source']='线下直接退';}
                elseif($order_type==13 && $v['rorder_id']==0 && $v['order_id']>0){$return['list'][$k]['tui_source']='线下订单退';}
                elseif($order_type==14 && $v['rorder_id']==0 && $v['order_id']>0) {$return['list'][$k]['tui_source']='线上订单退';}
                else{$return['list'][$k]['tui_source']='线下直接退';}
			}
			$this->assign($return);
		}elseif($tabid=='refund'){
			$status_array=['1'=>'未处理','2'=>'已同意','3'=>'已拒绝'];
			if($postdata['shop_status']>0){$postdata['status']=$postdata['shop_status']-1;unset($postdata['shop_status']);}
			$auth_code = 'back_moneylist';
	        $ret=D('Shop/Shops')->shop_account_option($auth_code);
	        if($ret['status']!=1) {$this->error($ret['msg']);}
			$this->assign('title_ass','退款');
			$return=D('ReturnGoods')->refund_list('',$postdata);$this->assign($return);
		}
		$this->assign('status_array',$status_array);
		$tabid=$tabid?$tabid:'back';
        $this->display($tabid);
    }
	/**
	 * 退货的确认收货
	 */
    public function confirm_goods(){
    	$auth_code = 'back_goodsback';
        $ret=D('Shop/Shops')->shop_account_option($auth_code);
        if($ret['status']!=1) {$this->ajaxReturn($ret);exit;}
		$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']=MID;
        $map['id']=$id;
        $order_info = M('OrderInfo')->where($map)->field(true)->find();
        if($order_info) {
            //print_r($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', 'cashier');
        $tabid=$tabid?$tabid:'info';
        $this->display($tabid);
    }
    /**
     * 订单详情
     */
    public function sy_info(){
        $id = I('param.id');
        $postdata=I('param.');
        $tabid=$postdata['tabid'];
        if(empty($id)){$this->redirect('Order/cashier');}
        $map['shop_id']=MID;
        $map['id']=$id;
        $order_info = M('OrderInfo')->where($map)->field(true)->find();
        if($order_info) {
            //print_r($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', 'cashier');
        $tabid=$tabid?$tabid:'info';
        $this->assign('tplseo_title','收银订单信息_'.C('WEB_SITE_TITLE'));
        $this->display($tabid);
    }
	public function service_info(){
        $id = I('param.id');
        $postdata=I('param.');
        $tabid=$postdata['tabid'];
        if(empty($id)){$this->redirect('Order/index');}
        $map['shop_id']=MID;
        $map['id']=$id;
        $order_info = M('OrderInfo')->where($map)->field(true)->find();
        if($order_info) {
            //print_r($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);
			$service_type = idtoname($id,'MemberPackage','service_type','order_id');
			$this->assign('service_type',$service_type);
        }else{
            $this->error('无此操作权限');
        }
        $this->assign('menu_action_current', 'service_order');
        $tabid=$tabid?$tabid:'service_info';
		$this->assign('tplseo_title','订单信息_'.C('WEB_SITE_TITLE'));
        $this->display($tabid);
    }
    /**
      *选择物流
      */
    public function change_wuliu(){
    	if(C("DIS_TRIBUTE") == 0)return;
    	$id = I('get.id');
    	$distribute = M('Distribute')->field("name,code")->where(array('status'=>1))->cache(true)->select();
    	$this->assign('id',$id);
    	$this->assign('distribute',$distribute);
        $this->display();
    }
    /**
	 * 重新向第三方添加订单
	 */
    function add_order(){
    	if(C("DIS_TRIBUTE") == 0)return;
		$order_id = I('get.order_id');
		$reqStatus = D('Common/Distribute')->readd_order($order_id);
		if($reqStatus["status"] == 0){
			$this->error($reqStatus["msg"],U('Shop/order/to_order'));return;
		}else{
			$this->success('操作成功',U('Shop/order/to_order'));return;
		}
    }
	/**
	 * 外卖确认收货
	 */
	public function confirm_order(){
		$postdata['order_id']=I('order_id');
		$postdata['admin_id']=MID;//操作日志用
		$postdata['type']='Shop';//操作日志用
		$result=D('Common/OrderInfo')->confirm_order($postdata);
		$this->ajaxReturn($result);
	}
	/**
	 * 外卖取消订单
	 */
	public function cancel_order(){
		$postdata['order_id']=I('order_id');
		$postdata['admin_id']=MID;//操作日志用
		$postdata['type']='Shop';//操作日志用
		$return=D('Common/OrderInfo')->cancel_order($postdata);
		$this->ajaxReturn($return);
	}
	/**
	*销售单据
	*/
	public function offorder_list(){
		$postdata=I('param.');
		$postdata['shop_id']=MID;
		$postdata['is_offline']=1;
		$postdata['is_root']=1;
		$tabid=$postdata['tabid'];
		if(!empty($tabid)){
			$postdata['order_id']=$postdata['id'];
			$infos=$this->get_order_info($postdata);
			$this->assign($infos['data']['infos']);
			$this->assign('order_goods',$infos['list']);
			$this->assign('operable_list',$infos['data']['operable_list']);
			$this->assign('distribute_code',$infos['data']['distribute_code']);
		}else{
			$this->get_order_list($postdata);
		}
		$this->assign('title_ass','销售单据');
		// $this->assign('menu_tab','');
		$this->assign($postdata);
		$tabid=$tabid?$tabid:'offorder_list';
		$this->display($tabid);
	}
	/**
	*仿照pad 只做打印使用
	*/
	public function order_list($postdata){
        $return=array('status'=>0,'msg'=>'');
        if(empty($postdata['shop_id'])) {$postdata['shop_id']=MID;}
        $map=array();
        $map['shop_id']=$postdata['shop_id'];
        $map['is_delete']=0;
        if($postdata['is_offline']==1){
            $map['type']=array('IN','13,15');
            if($postdata['is_root']==0) $map['sign_building']=$postdata['sign_building'];
        }else{
            $map['type']=array('IN','2,6,11,12,14,3');
            if($postdata['status']!=0)$map['shipping_status']=$postdata['status'];
            if($postdata['status']==1){
                $map['status']=2;
                $map['pay_status']=3;
            }
             // 1110 商家中心用
            //if($postdata['is_onorder']){
                unset($map['pay_status']);unset($map['status']);unset($map['shipping_status']);
                if($postdata['send_type'])$map['send_type']=$postdata['send_type'];
                if($postdata['pay_status'])$map['pay_status']=$postdata['pay_status'];
                if($postdata['status'])$map['status']=$postdata['status'];
                if($postdata['shipping_status'])$map['shipping_status']=$postdata['shipping_status'];
            //}//1110 end
            unset($postdata['status']);
        }
        if($postdata['order_sn']) $map['order_sn']=$postdata['order_sn'];
        $order_list=M('OrderInfo')->where($map)->order('id desc')->select();
        foreach ($order_list as $k => $v) {
            if($v['type']==3){
                $order_list[$k]['goods_allmoney']=$v['goods_allmoney']-$v['used_score_money'];
                $order_list[$k]['order_amount']=$v['order_amount']-$v['used_score_money'];
                $order_list[$k]['goods_amount']=$v['goods_amount']-$v['used_score_money'];
                $order_list[$k]['order_money']=$v['order_money']-$v['used_score_money'];
            }
        }
        // $return['data']=$order_list;
        // return $return;
        $this->export_orders($order_list);
    }
      /**
    *导出
    */
    function export_orders($list){
    	$title = array('订单号', '下单时间', '买家','订单金额','产品名称和数量','收货人','收货人手机','省','市','区','详细地址','备注');
        $data = array();$ids=array();
        foreach ($list as $ko => $vo) {$ids[]=$vo['id'];
        	$data[$vo['id']][]="'".$vo['order_sn'];
        	$data[$vo['id']][]=time_format($vo['addtime']);
            $data[$vo['id']][] = show_user_name($vo['uid']);
            $data[$vo['id']][] = format_price($vo['order_amount']).($vo['pay_code']?idtoname($vo['pay_code'],'Payment','name','code'):'余额支付');                      
            $data[$vo['id']][]='';//产品名称和数量              
            $data[$vo['id']][]=$vo['consignee'];   //收货人         
            $data[$vo['id']][]=$vo['mobile'];
            $data[$vo['id']][]=idtoname($vo['province'],'Area');
            $data[$vo['id']][]=idtoname($vo['city'],'Area');
            $data[$vo['id']][]=idtoname($vo['district'],'Area');
            $data[$vo['id']][]=$vo['address'];
            $data[$vo['id']][]=str_replace('对本次交易的说明','',$vo['temp']);
            // $data[$ko][] ='未判断';//$this->type($vo['status'],$vo['pay_status']);
        }
        if($ids){
            $map=array();
            $map['order_id']=array('in',$ids);
            $ordergoods=M('OrderGoods')->where($map)->field('order_id,goods_name,goods_number')->select();
            foreach($ordergoods as $v){
                $data[$v['order_id']][4].='['.$v['goods_name'].':'.floatval($v['goods_number']).'] ';
            }
        }
        // export_xls($title, $data, 'utf8');
        export_csv('订单'.date('Ymdhis').'.csv',$title, $data);
        unset($title, $data);
        exit;
    }
	/**
     * 获取订单列表
     */
    public function get_order_list($postdata=array()){
        $return=array('status'=>3,'msg'=>'');
        $data=array();
        if($postdata['print']=='print'){$data=$this->order_list($postdata);exit;}
        else$data=D('Pad/Cashier')->order_list($postdata);
        if($data['status']==0){
        	$this->error($data['msg']);exit();
        }
        foreach($data['data']['list'] as $k => $v){
        	$postdata['order_id']=$v['id'];
        	$list[$k]['goods']=$this->get_order_info($postdata);
            $list[$k]['id']=$v['id'];
            $list[$k]['order_sn']=$v['order_sn'];
            $list[$k]['addtime']=time_format($v['addtime']);
            $list[$k]['goods_allmoney']=$v['goods_allmoney'];
            $list[$k]['order_amount']=$v['order_amount'];
            $list[$k]['send_type']=$v['send_type'];
            $list[$k]['type']=$v['type'];
            $list[$k]['status']=$v['status'];
            $list[$k]['pay_status']=$v['pay_status'];
            $list[$k]['is_comment']=$v['is_comment'];
            $list[$k]['shipping_status']=$v['shipping_status'];
        }
        $this->assign('page',$data['data']['page']);
        $this->assign('list',$list);
    }
    /**
     * 获取订单内容
     */
    public function get_order_info($postdata=array()){
        $return=array('status'=>3,'msg'=>'');
        $data=D('Pad/Cashier')->order_info($postdata);
        if($postdata['order_id']) $operable_list=D('Pad/Cashier')->operable_list($postdata['order_id']);//获取订单可操作列表
        $return['list']=$data['data']['order_goods'];
        $return['data']['infos']=$data['data']['infos'];
        $return['data']['distribute_code']=C("DIS_TRIBUTE");
        if($operable_list['status']==1) $return['data']['operable_list']=$operable_list['data'];
        return $return;
    }
    /**
     * 订单操作按钮列表
     */
    public function order_manage(){
        $return=array('status'=>3,'msg'=>'');
        $postdata=I('post.');
        $postdata['shop_id']=MID;
    $auth_code='';$manage_way='';
        if(in_array($postdata['order_type'],[2,6,11,12,14,3])){$manage_way='weborder_'.$postdata['manage_way'];};
        if(in_array($postdata['order_type'],[13,15])){$manage_way='orderinfo_'.$postdata['manage_way'];};
        switch($manage_way){
            //线上
            case 'weborder_unpay':$auth_code='weborder_nopay';//未付款
                break;
            case 'weborder_confirm':$auth_code='weborder_confirm';//确认
                break;
            case 'weborder_pay':$auth_code='weborder_pay';//付款
                break;
            case 'weborder_order_take':$auth_code='weborder_ordertake';//备货
                break;
             case 'weborder_prepare':$auth_code='weborder_prepare';//配货
                break;    
            case 'weborder_unship':$auth_code='weborder_unship';//未发货
                break;
            case 'weborder_receive':$auth_code='weborder_receive';//已收货
                break;
            // case 'weborder_after_service':$auth_code='weborder_after_service';//售后
            //     break;
            case 'weborder_cancel':$auth_code='weborder_cancel';//取消
                break;
            case 'weborder_invalid':$auth_code='weborder_invalid';//无效
                break;
            //线下
            case 'orderinfo_unpay':$auth_code='orderinfo_nopay';
                break;
            case 'orderinfo_confirm':$auth_code='orderinfo_confirm';
                break;
            case 'orderinfo_pay':$auth_code='orderinfo_pay';
                break;
            case 'orderinfo_order_take':$auth_code='orderinfo_ordertake';
                break;
            case 'orderinfo_send':$auth_code='orderinfo_send';
                break;
            case 'orderinfo_prepare':$auth_code='orderinfo_prepare';
                break;
            case 'orderinfo_unship':$auth_code='orderinfo_unship';
                break;
            case 'orderinfo_receive':$auth_code='orderinfo_receive';
                break;
            case 'orderinfo_cancel':$auth_code='orderinfo_cancel';
                break;
            case 'orderinfo_invalid':$auth_code='orderinfo_invalid';
                break;
            // case 'orderinfo_after_service':$auth_code='orderinfo_after_service';
            //     break;    
        }
        if($auth_code){
        	$postdata['auth_code']=$auth_code;
        	$ret=D('Shop/Shops')->shop_account_option($auth_code);
        	if($ret['status']!=1) {$return['msg']=$ret['msg']; $this->ajaxReturn($return);}
        }
        $result=D('Pad/Cashier')->order_manage($postdata);
        if($result['status']==0){$return['msg']=$result['msg']; $this->ajaxReturn($return);}
        $map=array();
        $map['type']=array('in','6,12,14');
        $map['pay_status']=3;
        $map['status']=2;
        $map['shipping_status']=1;
        $map['shop_id']=$postdata['shop_id'];
        $dingdan_count=M('OrderInfo')->where($map)->count();
        /*$map=array();
        $map['type']=array('in','6,12,14');
        $map['pay_status']=3;
        $map['shipping_status']=1;
        $map['shop_id']=$postdata['shop_id'];
        $dingdan_count=M('OrderInfo')->where($map)->count();*/
        $return['status']=1;
        $return['msg']=$result['msg'];
        $return['count']=$dingdan_count;
        $this->ajaxReturn($return);
    }
    /*
    *'1'=>'待备货','5'=>'待发货','3'=>'待完成','4'=>'已完成'
    */
    public function onorder_list(){
    	$auth_code = 'weborder_list';
		$ret=D('Shop/Shops')->shop_account_option($auth_code);
        if($ret['status']!=1) {$this->error($ret['msg']);}
    	$postdata=I('param.');
		$postdata['shop_id']=MID;
		$postdata['is_offline']=0;
		$postdata['is_root']=1;
		$postdata['status']=0;
		$tabid=$postdata['tabid'];
        $this->assign('title_ass','销售单据');
		if($tabid=='on_order_info'){
			$postdata['order_id']=$postdata['id'];
			$infos=$this->get_order_info($postdata);
			$this->assign($infos['data']['infos']);
			$this->assign('order_goods',$infos['list']);
			$this->assign('operable_list',$infos['data']['operable_list']);			
		}else{
			// if($tabid=='order_take'){$postdata['status']=1;}//戴备货
			// if($tabid=='no_receive'){$postdata['status']=3;}//待完成
			// if($tabid=='receive'){$postdata['status']=4;}//已完成
			// if($tabid=='no_ship'){$postdata['status']=5;}//代发货
			if($tabid)$postdata['is_onorder']=1;
			if($tabid=='order_take'){$postdata['shipping_status']=1;$postdata['pay_status']=3;$postdata['status']=2;}//戴备货
			if($tabid=='no_receive'){$postdata['shipping_status']=3;$postdata['status']=2;$postdata['pay_status']=3;}//待完成
			if($tabid=='receive'){$postdata['pay_status']=3;$postdata['shipping_status']=4;$postdata['status']=3;}//已完成
			if($tabid=='no_ship'){$postdata['pay_status']=3;$postdata['shipping_status']=5;$postdata['status']=2;}//代发货
			// if($tabid=='no_pay'){$postdata['pay_status']=1;$postdata['shipping_status']=1;$postdata['status']=1;};//未付款
			if($tabid=='is_qx'){$postdata['status']=4;};//取消
			$this->get_order_list($postdata);
			$tabid='onorder_list';
		}
		$distribute_code=C("DIS_TRIBUTE");
        if($distribute_code && !empty($infos['data']['infos']['send_type'])){
            if($infos['data']['infos']['send_type']!=2)$distribute_code=0;
        }
        $this->assign('distribute_code',$distribute_code);
		$this->assign($postdata);
		$tabid=$tabid?$tabid:'onorder_list';
		$this->display($tabid);
    }
    /**
     *选择物流
     */
    public function order_take(){
        if(C("DIS_TRIBUTE") == 0)return;
        $id = I('get.id');
        $distribute = M('Distribute')->field("name,code")->where(array('status'=>1))->cache(true)->select();
        $this->assign('id',$id);
        $this->assign('distribute',$distribute);
        $this->display();
    }
    /**
     * 发货
     */
    public function deliver_goods(){
        $return=array('status'=>3,'msg'=>'');
        $postdata=I('post.');
        if(empty($postdata['order_id'])){$return['msg']='参数错误'; $this->error($return['msg']);}
        $data['order_id']=$postdata['order_id'];
        $data['notice_sn']=123;
        $data['send_type']=$postdata['send_type'];
        $data['wuliu']=$postdata['send_type'];
       echo  $data['order_type']=idtoname($data['order_id'],'OrderInfo','type','id');
        $data['type']='Shop';
        $data['admin_id']=MID;
		$manage_way='';$auth_code='';
		if(in_array($data['order_type'],['2','6','11','12','14','3'])){$manage_way='weborder_send';};
        if(in_array($data['order_type'],['13','15'])){$manage_way='orderinfo_send';};
		switch($manage_way){
            //线上
            case 'orderinfo_send':$auth_code='orderinfo_send';//发货
                break;        
            case 'weborder_send':$auth_code='weborder_send';//发货
                break;    
        }
        if($auth_code){
        	$ret=D('Shop/Shops')->shop_account_option($auth_code);
        	if($ret['status']!=1) { $this->error($ret['msg']);}
        }
        $result=D("Common/OrderInfo")->ship_goods($data);
        if($result['status']){
            $return['status']=$result['status'];
            $return['msg']=$result['info'];
            $this->success($return['msg']);
        }else{
            $return['msg']=$result['info'];
            $this->error($return['msg']);
        }
    }
    public function order_print(){
		$id = I('get.id');
		$template = 'peihuo_template';
		$price='';
		$order_info = M('OrderInfo')->where('id='.$id)->field(true)->find();
        if($order_info['pay_code']=='' && $order_info['pay_status']==3){
            $order_info['pay_code']='Hanchao';
        }
		$pay_name = M('Payment')->where("code='".$order_info['pay_code']."'")->field('name')->find();
		if($order_info['promot_id']){
            $act_ids = explode(',', $order_info['promot_id']);
            $act_ids=array_filter($act_ids);
            $promote_arr = D('Common/Takeout')->getpromote($act_ids,$id);
			$promote_money = array_sum(i_array_column($promote_arr,'money'));
        }
		$order_info['discount'] = $promote_money;
		$order_goods = M('OrderGoods')->where('order_id='.$id)->field(true)->select();
		foreach($order_goods as $ke=>$va){
            $attr=unserialize($va['goods_attr']);
            $order_goods[$ke]['attr_name']=$attr['name'];
            $order_goods[$ke]['goods_price_format']=format_price($va['goods_price']);
            $order_goods[$ke]['org_parce']=$va['goods_price']*$va['goods_number'];
            $price +=  $order_goods[$ke]['org_parce'];
            $order_goods[$ke]['org_parce_format']=format_price($order_goods[$ke]['org_parce']);
            $order_goods[$ke]['org_integral']=$va['goods_integral']*$va['goods_number'];
		}
		$url = C('WEB_URL');
		$phone = C('WEB_SITE_PHONE');
		$name = C('WEB_SITE_TITLE');
		$admin = idtoname(MID,'Shops','name');
		$time = date('Y-m-d H:i:s',NOW_TIME);
		$this->assign('url',$url);
		$this->assign('phone',$phone);
		$this->assign('name',$name);
		$this->assign('admin',$admin);
		$this->assign('print_time',$time);
		$this->assign('pay_name',$pay_name['name']);
		$this->assign('order_info',$order_info);
		$this->assign('price',$price);
		$this->assign('order_goods',$order_goods);
		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();
    }
    public function sub_service(){
    	$order_id=I('id');
    	$type=I('type');
    	$this->assign('order_id',$order_id);
    	$this->assign('type',$type);
    	$this->display();
    }
    public function to_sub_service(){
    	$postdata=I('post.');
    	$postdata['shop_id']=MID;
    	if(empty($postdata['order_id']) && $postdata['type']!=1){$this->error('参数错误');}
        if(empty($postdata['content'])){$this->error('请输入内容，以便我们及时解决您的问题');}
		$manage_way=$auth_code='';
       	$type=idtoname($postdata['order_id'],'OrderInfo','type','id');
        if(in_array($type,array('2','6','11','12','14','3'))){$manage_way='weborder_after_service';};
        if(in_array($type,array('13','15'))){$manage_way='orderinfo_after_service';};
        switch($manage_way){
            //线上
            case 'orderinfo_after_service':$auth_code='orderinfo_after_service';
                break;        
            case 'weborder_after_service':$auth_code='weborder_after_service';
                break;    
        }
        if($auth_code){
        	$ret=D('Shop/Shops')->shop_account_option($auth_code);
        	if($ret['status']!=1) {$return['msg']=$ret['msg']; $this->ajaxReturn($return);}
        }
        $data = D('Common/Member')->submit_service($postdata);
    	if($data['status']){
            $return['status']=$data['status'];
            $return['msg']=$data['msg'];
        }else{
            $return['msg']=$data['msg'];
        }
        $this->ajaxReturn($return);
    }
    // 自提订单
    public function zt_index(){
    	 $name = "OrderInfo";
        $map=array();
        $postdata=I('param.');
        if($postdata['tabid']){
        	$this->zt_view();
        	$this->display('zt_view');
        }else{
	        $map['goods_allmoney'] = array('gt',0);
	        if($postdata['wm_type'] == 13){
	            $map['type']=13;
	        }elseif($postdata['wm_type'] == 14){
	            $map['type']=array('in','6,14');
	        }elseif($postdata['wm_type'] == 3){
	            $map['type']=3;
	        }else{
	            $map['type']=array('in','6,13,14,3');//拼团和收银订单和外卖
	        }
	        $order_shipping=array('5'=>'已备货','4'=>'已收货','3'=>'未备货');
	        $this->assign('order_shipping',$order_shipping);
	        if($postdata['order_sn'])$map['order_sn']=$postdata['order_sn'];
	        if($postdata['status'])$map['status']=$postdata['status'];
	        if($postdata['pay_status'])$map['pay_status']=$postdata['pay_status'];
	        if($postdata['shipping_status'])$map['shipping_status']=$postdata['shipping_status'];
	        $map['shop_id']=MID;
	        $map['send_type']=1;
	        //门店管理员
	        $list = $this->lists('OrderInfo',$map);
	        foreach($list as $k=>$v){
	            if($v['type']==3){
	                $list[$k]['goods_amount']=format_decimal($v['goods_amount']-$v['used_score_money']);
	            }else{
	                $list[$k]['goods_amount']=$v['goods_amount'];
	            }
	          	$post['order_id']=$v['id'];
	          	$post['shop_id']=MID;
	        	$list[$k]['goods']=$this->get_order_info($post);
	        }
	        $this->assign('list',$list);$this->assign($postdata);
        	$this->display('zt_index');
    	}
    }
 	public function zt_view(){
 		$id=I('id');if(empty($id))$this->error('参数不正确');
 		$postdata['shop_id']=MID;
 		$postdata['order_id']=$id;
		$infos=$this->get_order_info($postdata);
		$this->assign($infos['data']['infos']);
		$this->assign('ordergoods',$infos['list']);
		$this->assign('operable_list',$infos['data']['operable_list']);	
		$distribute_code=C("DIS_TRIBUTE");
        
        if($distribute_code && !empty($infos['data']['infos']['send_type'])){
            if($infos['data']['infos']['send_type']!=2)$distribute_code=0;
        }
		$this->assign('distribute_code',$distribute_code);
		$this->assign($postdata);
        // $this->display();
    }
	// 自提结束
}
?>