<?php
/**
 * 订单管理
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaogg <xiaogg@sina.cn>
 */
 
namespace Admin\Controller;
class ShopOrderController extends BaseController{
    public function _initialize() {
        parent::_initialize();
		$all_shop=M('Shops')->where('status=1')->field('id,name')->select();
		$arrs=array();
		foreach($all_shop as $k=>$v){
			$arrs[$v['id']]=$v['name'];
		}
		$this->assign('shops_id',$arrs);
		$this->status_pay=strtoarray(c('STATUS_PAY'));$this->assign('status_pay',$this->status_pay);
        $this->status_shipping=strtoarray(c('SHIPPING_STATUS'));$this->assign('status_shipping',$this->status_shipping);
        $this->status_order=strtoarray(c('STATUS_ORDER'));$this->assign('status_order',$this->status_order);
    } 
    public function index(){
        $name = CONTROLLER_NAME;
        $map=array();
        $postdata['order_sn']=I('order_sn');
		$postdata['shop_id']=I('shop');
		$postdata['type']=I('type');
        $postdata['status']=I('status');
        $postdata['pay_status']=I('pay_status');
        $postdata['shipping_status']=I('shipping_status');
        $postdata['date_from']=I('date_from');
		$postdata['date_to']=I('date_to');
        $this->assign('postdata',$postdata);
		if($postdata['order_sn'])$map['oi.order_sn']=$postdata['order_sn'];
		if($postdata['shop_id'])$map['og.shop_id']=$postdata['shop_id'];
        if($postdata['money_status'])$map['og.is_balance']=$postdata['money_status'];
        if($postdata['type'])$map['oi.type']=$postdata['type'];
        if($postdata['status'])$map['oi.status']=$postdata['status'];
        if($postdata['pay_status'])$map['oi.pay_status']=$postdata['pay_status'];
        if($postdata['shipping_status'])$map['oi.shipping_status']=$postdata['shipping_status'];
		if($postdata['date_from']){
			$map['oi.addtime'] = array(array('GT',strtotime($postdata['date_from'])),array('lt',strtotime($postdata['date_to'])));	
		}
		if(!$postdata['shop_id']){$map['og.shop_id']=array(array('GT','0'));}
		$map['og.status']=array('in',array(1,2));
		$list=M('OrderGoods')->alias('og')->join('LEFT JOIN __ORDER_INFO__ oi ON og.order_id=oi.id')->where($map)->field('og.id,og.order_id,oi.order_sn,og.goods_id,oi.order_amount,og.is_balance,oi.province,oi.city,oi.district,oi.address,og.shop_id,oi.status,oi.pay_status,og.status')->select();
		$OrderInfoModel=D('Common/OrderInfo');
		foreach($list as $k=>$v){
			$list[$k]['province'] = idtoname($v['province'],'area','name');
			$list[$k]['city'] = idtoname($v['city'],'area','name');
			$list[$k]['district'] = idtoname($v['district'],'area','name');
			$list[$k]['all_supplymoney']=$OrderInfoModel->jisuansupply($v['id']);	
		}
        $this->assign('list',$list);$this->assign($postdata);
	    $this->getNodeTree();
        $this->display();
    }
	public function deletes(){
		$post=I('post.');
		if($post['ids']){
			$ids=$post['ids'];
			foreach($ids as $k=>$v){
				$order_id=$va;
				$map_og['order_id']=$v;
				$og_info=M('OrderInfo')->where($map_og)->delete();	
			}
			if($og_info){
				$this->success('删除成功！');
        	}else{
            $this->error('删除失败！');
			}
		}
	}
	public function go_money(){
		$c=I('get.');
		//print_r($c);exit;
		if($c['ids']){
			$ids=$c['ids'];
			foreach($ids as $k=>$v){
				$return=D('Common/OrderInfo')->shop_balance($v);
/*				$map_og['order_id']=$v;
				$og_info=M('OrderGoods')->where($map_og)->field('order_id,goods_id,goods_number,shop_id')->select();
				$goods_info=array();
				$i=0;
				foreach($og_info as $val){
					$map_g['id']=$val['goods_id'];
					$goods_info[]=M('Goods')->alias('g')->where('g.id='.$val['goods_id'])->field('(g.supply_price*og.goods_number) as price_to,og.shop_id,og.goods_id,g.supply_price,order_id')->JOIN('LEFT JOIN __ORDER_GOODS__ og ON og.goods_id=g.id')->find();
					$i++;
				}
				foreach($goods_info as $v){
					$map_s['id']=$v['shop_id'];
					$shop_info = M('Shops')->where($map_s)->field(true)->find();
					$return=moneychange($shop_info['uid'],$v['price_to']);
				}
				if($return){
					$where_or['id']=$v;
					$con['money_status']=1;
					$or_info = M('OrderInfo')->where($where_or)->save($con);
				}*/
				if($return==false){break;}
			}	
		}else{
			foreach($c['id'] as $va){
				if($va){
					//店铺结算
					$return=D('Common/OrderInfo')->shop_balance($va);
/*					$order_id=$va;
					$map_og['order_id']=$order_id;
					$og_info=M('OrderGoods')->where($map_og)->field('order_id,goods_id,goods_number,shop_id')->select();
					$goods_info=array();
					$i=0;
					foreach($og_info as $val){
						$map_g['id']=$val['goods_id'];
						$goods_info[]=M('Goods')->alias('g')->where('g.id='.$val['goods_id'])->field('(g.supply_price*og.goods_number) as price_to,og.shop_id,og.goods_id,g.supply_price,order_id')->JOIN('LEFT JOIN __ORDER_GOODS__ og ON og.goods_id=g.id')->find();
						$i++;
					}
					foreach($goods_info as $v){
						$map_s['id']=$v['shop_id'];
						$shop_info = M('Shops')->where($map_s)->field(true)->find();
						$reture=moneychange($shop_info['uid'],$v['price_to']);
					}
					if($reture){
						$where_or['id']=$order_id;
						$con['money_status']=1;
						$or_info = M('OrderInfo')->where($where_or)->save($con);
					}*/
					if($return==false){break;}
				}
			}
		}
		$data['status']=$return?0:1;
		$this->ajaxReturn($data);	
	}
    public function _before_edit(){
        $id=I('id');
        if(empty($id))$this->error('参数不正确');
        $this->getNodeTree();
        $ordergoods=M('OrderGoods')->where('order_id='.$id)->select();
       $this->assign('ordergoods',$ordergoods);
    }
    public function _before_update(){
        $_POST['province']=$_POST['area_sheng'];
        $_POST['city']=$_POST['area_shi'];
        $_POST['district']=$_POST['area_xian'];
        if($_POST['pay_fee_old']!=$_POST['pay_fee'] || $_POST['shipping_fee_old']!=$_POST['shipping_fee']){
            $_POST['order_amount']=$_POST['goods_amount']+$_POST['pay_fee']+$_POST['shipping_fee'];
        }
        unset($_POST['area_sheng'],$_POST['area_shi'],$_POST['area_xian'],$_POST['pay_fee_old'],$_POST['shipping_fee_old']);
        action_log('update_order', 'Admin',I('id'),UID);
    }    
    public function _before_view(){
       $id=I('id');
       if(empty($id))$this->error('参数不正确');
       $operable_list=$this->operable_list($id);
       $this->assign('operable_list',$operable_list);
       $ordergoods=M('OrderGoods')->where('order_id='.$id)->select();
       $this->assign('ordergoods',$ordergoods);       
       $actionlog=M('ActionLog')->where('action_id in (9,10) and record_id='.$id)->order('id desc')->select();
       $this->assign('actionlog',$actionlog);       
    }
    public function tixian(){
        $name = CONTROLLER_NAME;
        $map=array();
        $postdata=I('param.');
		//print_r($postdata);exit;
        if(isset($postdata['order_sn']))$map['order_sn']=$postdata['order_sn'];
        $map['type']=3;
        if(isset($postdata['status']))$map['status']=$postdata['status'];
        if(isset($postdata['pay_status']))$map['pay_status']=$postdata['pay_status'];
        if(isset($postdata['shipping_status']))$map['shipping_status']=$postdata['shipping_status'];
        if(isset($postdata['date_from']))$map['addtime'][]=array('EGT',strtotime($postdata['date_from']));
        if(isset($postdata['date_to']))$map['addtime'][]=array('elt',strtotime($postdata['date_to']));
        if(isset($keyword)){
            $whereor['uid']=$keyword;
            $whereor['consignee']=array('like','%'.$keyword.'%');
            $whereor['address']=array('like','%'.$keyword.'%');
            $whereor['mobile']=array('like','%'.$keyword.'%');
            $whereor['tel']=array('like','%'.$keyword.'%');
            $whereor['email']=array('like','%'.$keyword.'%');
            $whereor['sign_building']=array('like','%'.$keyword.'%');
            $whereor['temp']=array('like','%'.$keyword.'%');
            $whereor['_logic'] = 'or';
            $map['_complex'] = $whereor;
        }
        $list = $this->lists($name,$map);
        $this->assign('list',$list);$this->assign($postdata);
	    $this->getNodeTree();
        $list = $this->lists($name,$map);
        $this->display();
    }
    public function ordergoods(){
        $id=I('id');
        if(empty($id))$this->error('参数不正确');
        $ordergoods=M('OrderGoods')->where('order_id='.$id)->select();
        $this->assign('ordergoods',$ordergoods);
        $this->display();
    }
    public function updateordergoods(){
        $mod=M('OrderInfo');
        $id=I('post.order_id');if(empty($id))$this->error('参数不正确');
        $total=I('post.ordergoodstotal');if(empty($total))$this->error('商品价格不正确');
        $orderinfo=$mod->field('goods_amount,order_amount')->find($id);
        $new_order_amount=$orderinfo['order_amount']-$orderinfo['goods_amount']+$total;
        $data['goods_amount']=$total;
        $data['order_amount']=$new_order_amount;
        $mod->where('id='.$id)->save($data);action_log('update_order', 'Admin',I('id'),UID,'更新订单商品价格');
        $this->success('更新成功',U('OrderInfo/view',array('id'=>$id)));
    }
	public function order_print(){
		$id = I('get.id');
		$price='';
		$order_info = M('OrderInfo')->where('id='.$id)->field(true)->find();
		$pay_name = M('Payment')->where("code='".$order_info['pay_code']."'")->field('name')->find();
		$order_goods = M('OrderGoods')->where('order_id='.$id)->field(true)->select();
		foreach($order_goods as $ke=>$va){
			$price += $va['goods_price'];
		}
		$url = C('WEB_URL');
		$phone = C('WEB_SITE_PHONE');
		$name = C('WEB_SITE_TITLE');
		$admin = idtoname(UID,'admin','username');
		$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('time',$time);
		$this->assign('pay_name',$pay_name['name']);
		$this->assign($order_info);
		$this->assign('price',$price);
		$this->assign('order_goods',$order_goods);
		$this->display();
    }
	public function mprint(){
		$datainfo=I('get.');
		$data['status']=1;
		$data['url']=U('Admin/ShopOrder/view_print',array('order_sn'=>$datainfo['order_sn'],'shop_id'=>$datainfo['shop'],'type'=>$datainfo['type'],'status'=>$datainfo['status'],'pay_status'=>$datainfo['pay_status'],'shipping_status'=>$datainfo['shipping_status'],'date_from'=>$datainfo['date_from'],'date_to'=>$datainfo['date_to']));;
		$this->ajaxReturn($data);
	}
	public function view_print(){
		$data_info=I('get.');
		if($data_info['order_sn'])$map['order_sn']=$data_info['order_sn'];
		if($data_info['shop_id'])$map['shop_id']=$data_info['shop_id'];
        if($data_info['type'])$map['type']=$data_info['type'];
        if($data_info['status'])$map['status']=$data_info['status'];
        if($data_info['pay_status'])$map['pay_status']=$data_info['pay_status'];
        if($data_info['shipping_status'])$map['shipping_status']=$data_info['shipping_status'];
		if($data_info['date_from']){
			$map['addtime'] = array(array('GT',strtotime($data_info['date_from'])),array('lt',strtotime($data_info['date_to'])));	
		}
		//print_r($data_info);exit;
		if(!$data_info['shop_id']){$map['shop_id']=array(array('GT','0'));}
		$list=M('OrderInfo')->field(true)->where($map)->select();
		$all_suppliers=M('Suppliers')->where('is_check=1')->field(true)->select();
		$i=0;
		foreach($list as $k=>$v){
			foreach($all_suppliers as $ke=>$va){
				if($list[$k]['address'] == $va['address']){
					$list[$k]['best_send']=$va['id'];
				}elseif($list[$k]['district'] == $va['district']){
					$list[$k]['best_send']=$va['id'];
				}elseif($list[$k]['city'] == $va['city']){
					$list[$k]['best_send']=$va['id'];
				}elseif($list[$k]['province'] == $va['province']){
					$list[$k]['best_send']=$va['id'];
				}else{
					$list[$k]['best_send']=$list[$k]['suppliers_id'];
				}
			}
			$list[$k]['province'] = idtoname($v['province'],'area','name');
			$list[$k]['city'] = idtoname($v['city'],'area','name');
			$list[$k]['district'] = idtoname($v['district'],'area','name');
			$info[$i]=M('OrderGoods')->alias('og')->where('og.order_id='.$v['id'])->field('og.*,g.money_status,g.supply_price')->JOIN('LEFT JOIN __GOODS__ g ON g.id=og.goods_id')->select();
			$i++;	
		}
		foreach($info as $ke1=>$va1){
			$all_supplymoney=$mon_status=0;
			foreach($va1 as $valu){
				$all_supplymoney+=$valu['supply_price'];
			}
			$info[$ke1]['all_supplymoney'] = $all_supplymoney;
		}
		foreach($list as $k1=>$v1){
			foreach($info as $k2=>$v2){
				if($k1==$k2){
				$list[$k1]['all_supplymoney'] = $info[$k2]['all_supplymoney'];
				}
			}	
		}
		foreach($list as $v){
			$sum +=$v['all_supplymoney'];
		}
	   $this->assign('list',$list);
	   $this->assign('data_info',$data_info);
	   $this->assign('sum',$sum);
	   $this->display();
    }
    /* 得到节点树 */
	private function getNodeTree($pid = '') {
	   //支付方式
       $payment_select=M('Payment')->where(array('status'=>array('egt',0)))->field('id,name')->order('sort,id desc')->select();
       foreach($payment_select as $v){$payment[$v['id']]=$v['name'];}
       //配送方式
       $shipping_select=M('Shipping')->where(array('status'=>array('egt',0)))->field('id,name')->order('sort,id desc')->select();
       foreach($shipping_select as $v){$shipping[$v['id']]=$v['name'];}
       $this->assign('payment',$payment);
       $this->assign('shipping',$shipping);
       unset($payment_select,$payment,$shipping_select,$shipping);
	}
}