<?php
if (!defined('HUCART_COM')) exit('HuCart.COM No direct script access allowed');
class Order
{
	function Order()
	{

	}
	
	function info_list()
	{
		$page_size = 20 ;		
		$page_num_val 	= $this -> input ->  req["page_num"];
		
		if( $page_num_val > 1 )
		{
			$page_num  = $page_num_val;
			$limit_num = ceil( ( $page_num_val -1 ) * $page_size );
		}else{
			$page_num  = 1;
			$limit_num = 0 ;
		}

		//帐号 	真实姓名 	Email地址 	登录次数 	加入时间 	最后登录时间
		$sql_query = " SELECT ord_id, ord_sn , o.use_id, ord_follow, ord_allpay , ord_needpay , ord_pcdall, ord_address, ord_addtime, ord_paytime, ord_express, ord_status ,u.use_name  FROM  `".$this -> db -> prefix."order`  as o , ".$this -> db -> prefix."user as u  WHERE o.use_id = u.use_id  GROUP BY ord_id ORDER BY `ord_id` LIMIT  $limit_num , $page_size ";
		
		$info = $this -> db -> select_info($sql_query);
		
		foreach( (array)$info as $ikey => $ival )
		{
			if($info[$ikey]["ord_addtime"]> 100){
				$info[$ikey]["ord_addtime"] = date("Y-m-d H:i:s",$info[$ikey]["ord_addtime"]);
			}else{
				$info[$ikey]["ord_addtime"] = '无';
			}
			if($info[$ikey]["ord_paytime"]> 100)
			{
				$info[$ikey]["ord_paytime"] = date("Y-m-d H:i:s",$info[$ikey]["ord_paytime"]);
			}else{
				$info[$ikey]["ord_paytime"] = '无';
			}
		}
		
		
		
		$sql_query = " SELECT count(1) as CNUM  FROM  `".$this -> db -> prefix."order`  WHERE 1  ORDER BY `ord_id`  ";
		
		$infocount = $this -> db -> select_one($sql_query);
		
		//var_dump($info);
		
		$page_str = $this -> page ->get_admin_page( $url = '?load=order&act=info_list', $page_num  , $page_size  , $result_count = $infocount["CNUM"] );
		
		//$this -> view -> assign('ADMIN_BASEUSER',ADMIN_BASEUSER);
		
		$this -> view -> assign('page_str',$page_str);
		$this -> view -> assign('info',$info);
		$this -> view -> display('order.tpl');
		
		
		
	}
	
	function add(  )
	{
		///$this -> view ->  caching = true;
		//$this -> view ->  cache_lifetime = 86400 ;  //60 *  60 * 24
		
		$stepval 	= $this -> input ->  req["step"];
		
		if($stepval == '')
		{
			$this -> view -> assign('act_url',ADMIN_WEBURL."/index.php?load=order&act=add_info");
			$this -> view -> display('orderadd.tpl');
		}elseif($stepval == 2)
		{
			//var_dump($_REQUEST);
			
			$adduserval = $this -> input ->  req["adduser"];
			$addnameval = $this -> input ->  req["addname"];
			if($adduserval!='')
			{
				//查询客户信息
				$sql_query = "SELECT  add_id,use_id,add_phone,add_mobile,add_name,add_pcdall,add_address FROM ".$this -> db -> prefix."address  WHERE use_id IN ( SELECT use_id  FROM ".$this -> db -> prefix."user WHERE use_id = '".$adduserval."'  OR use_name = '".$adduserval."' ) limit 0 , 100 ";
				$info = $this -> db -> select_info($sql_query);
				
				//var_dump($info);
				
			}elseif($addnameval!='')
			{

				$sql_query = "SELECT  add_id,use_id,add_phone,add_mobile,add_name,add_pcdall,add_address FROM ".$this -> db -> prefix."address  WHERE add_name LIKE  '".$addnameval."%' limit 0 , 100 ";
				$info = $this -> db -> select_info($sql_query);

			}else{
			
				echo 'empty';
			
			}

			//查询

			$this -> view ->  assign('info',$info);
			
			$this -> view -> assign('act_url',ADMIN_WEBURL."/index.php?load=order&act=add_info");
			$this -> view -> display('orderstep2.tpl');
			
			

		}elseif($stepval == 3)
		{
			$addidval = $this -> input ->  req["addid"];
			
			//echo $addidval;
			$sql_query = "SELECT  add_id,use_id,add_phone,add_mobile,add_name,add_pcdall,add_address FROM ".$this -> db -> prefix."address  WHERE add_id = '".$addidval."' limit 0 , 1 ";
			$addressinfo = $this -> db -> select_one($sql_query);
			
			$this -> view ->  assign('addressinfo',$addressinfo);
			
			//查询商品信息
			$sqlpro = "SELECT pro_id,pro_name,pro_sell_price FROM ".$this -> db -> prefix."product WHERE pro_enabled = 1  GROUP BY pro_id ORDER BY pro_up_time DESC LIMIT 0, 30 ";
			
			$proInfo = $this -> db -> select_info($sqlpro);
			
			//var_dump( $proInfo );
			
			$this -> view ->  assign('proInfo',$proInfo);
			$this -> view -> display('orderstep3.tpl');


		}elseif($stepval == 4)
		{
			$use_idval = $this -> input ->  req["use_id"];
			$add_idval = $this -> input ->  req["add_id"];
			$odd_numval = $this -> input ->  req["odd_num"];
			$odd_priceval = $this -> input ->  req["odd_price"];
			
			//add_phone,add_mobile
			

			
			$sql_query = "SELECT  add_id,use_id,add_phone,add_mobile,add_name,add_province,add_city,add_district,add_pcdall,add_address FROM ".$this -> db -> prefix."address  WHERE add_id = '".$add_idval."' limit 0 , 1 ";
			$addressinfo = $this -> db -> select_one($sql_query);
			
			$ord_addtimeval = time();
			
			$ord_snval =  $this -> buildsn();
			$insertorder = "INSERT INTO `".$this -> db -> prefix."order` (`ord_sn` ,`use_id`,`ord_consignee`,`add_id`,`ord_addtime`,ord_province,ord_city,ord_district,ord_pcdall,ord_address,ord_mobile,ord_phone,ord_status) VALUES( '$ord_snval','$use_idval','".$addressinfo["add_name"]."','".$addressinfo["add_id"]."','".$ord_addtimeval."','".$addressinfo["add_province"]."','".$addressinfo["add_city"]."','".$addressinfo["add_district"]."','".$addressinfo["add_pcdall"]."','".$addressinfo["add_address"]."','".$addressinfo["add_mobile"]."','".$addressinfo["add_phone"]."',2)";
			
			$ord_idval  = $this -> db -> insertSql($insertorder);
			
			foreach((array) $odd_numval as $prokey => $proval)
			{
				if( $proval > 0 ){
				$insertdesc[$prokey] = " INSERT INTO `".$this -> db -> prefix."orderdesc` (`use_id` ,`ord_id`,`pro_id`,`odd_num`,`odd_price`) VALUES( '$use_idval','$ord_idval','$prokey','$proval','".$odd_priceval[$prokey]."')";
				$this -> db -> insertSql($insertdesc[$prokey]);
				}
			}
			
			$this -> view ->  assign('info_title',$this->lang['add_success']);
			$this -> view ->  assign('URL_TIME',URL_TIME);
			$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=order&act=info_list");
			$this -> view ->  display('suggestion.tpl');
		
		}
	}

	function buildsn()
	{
		$today = date('Ymd');
		$countNum = " SELECT COUNT(1) as CNUM FROM `".$this -> db -> prefix."order` WHERE ord_addtime >   ".strtotime($today) ;
		$countInfo = $this -> db -> select_one($countNum);

		if($countInfo["CNUM"] < 99999 )
		{
			$selectbig = "SELECT ord_sn FROM `".$this -> db -> prefix."order` WHERE ord_addtime > ".strtotime($today)."  GROUP BY ord_id ORDER BY ord_id DESC "; 
			$bigInfo = $this -> db -> select_one($selectbig);
			$oldsn =  substr($bigInfo["ord_sn"],-5);
		}else{
			exit('error');
		}
		
		
		return $today.substr( '000000'.($oldsn + 1),-5);

	}
	
	function del( )
	{
		$indel 	 = $this -> input ->  req["id"];

		$deletesql ="UPDATE `".$this -> db -> prefix."order` SET  ord_status = 5  WHERE `order`.`ord_id` IN ( $indel )  ";
		
		$this -> db -> query_sql($deletesql);
		
		$this -> view ->  assign('info_title',$this->lang['del_success']);
		$this -> view ->  assign('URL_TIME',URL_TIME);
		$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=order&act=info_list");
		$this -> view ->  display('suggestion.tpl');
	}
	
	function info()
	{
		$ord_idval = $this -> input ->  req["id"];
		$stepval = $this -> input ->  req["step"];
		
		$this -> view ->  assign('idval',$ord_idval);
		
		if($stepval == '')
		{
			$sql_query = " SELECT *  FROM  `".$this -> db -> prefix."order`  WHERE ord_id = '".$ord_idval."' ORDER BY `ord_id` ";
			
			$info = $this -> db -> select_one( $sql_query );
	
			//用户收货信息表
			$sqladdress = "SELECT  add_id,use_id,add_phone,add_mobile,add_name,add_pcdall,add_address FROM ".$this -> db -> prefix."address  WHERE use_id = '".$info["use_id"]."' limit 0 , 100 ";
			$infoAddress = $this -> db -> select_info($sqladdress);
			
			//var_dump( $infoAddress );
			
			$this -> view ->  assign('infoAddress',$infoAddress);	
			$this -> view ->  assign('act_url',ADMIN_WEBURL."/index.php?load=order&act=edit_info");
			$this -> view ->  assign('info',$info);
			$this -> view ->  display('orderedit.tpl');
		}elseif($stepval == 3)
		{
			
			//更新order地址信息
			$addidval = $this -> input ->  req["addid"];
			
			if( $addidval !='' )
			{
				$sql_query = "SELECT  add_id,use_id,add_phone,add_mobile,add_name,add_province,add_city,add_district,add_pcdall,add_address FROM ".$this -> db -> prefix."address  WHERE add_id = '".$addidval."' limit 0 , 1 ";
				$addressinfo = $this -> db -> select_one($sql_query);
				
				$updatesql = " UPDATE  ".$this -> db -> prefix."order SET ord_consignee ='".$addressinfo["add_name"]."' ,add_id ='".$addressinfo["add_id"]."' ,ord_province ='".$addressinfo["add_province"]."' , ord_city ='".$addressinfo["add_city"]."' ,ord_district ='".$addressinfo["add_district"]."' ,ord_pcdall ='".$addressinfo["add_pcdall"]."'  ,ord_address ='".$addressinfo["add_address"]."' , ord_mobile ='".$addressinfo["add_mobile"]."' ,ord_phone ='".$addressinfo["add_phone"]."' WHERE ord_id =".$ord_idval;
				$this -> db -> query_sql( $updatesql );
				
			}
			
 
			
			
			
			
			$sql_querydetail = " SELECT o.*,p.pro_name   FROM  `".$this -> db -> prefix."orderdesc` AS o  left join ".$this -> db -> prefix."product as p on p.pro_id = o.pro_id    WHERE o.ord_id = '".$ord_idval."' ORDER BY o.ord_id ";		//,p.pro_sell_price ,p.pro_num
			$infodetail = $this -> db -> select_info( $sql_querydetail ); 
			
			
			//查询商品信息
			$sqlpro = "SELECT pro_id,pro_name,pro_sell_price FROM ".$this -> db -> prefix."product WHERE pro_enabled = 1  GROUP BY pro_id ORDER BY pro_up_time DESC LIMIT 0, 30 ";
			
			$proInfo = $this -> db -> select_info($sqlpro);
			
			$this -> view ->  assign('infodetail',$infodetail);
			$this -> view ->  assign('proInfo',$proInfo);
			
			$sqlorder = " SELECT *  FROM  `".$this -> db -> prefix."order`  WHERE ord_id = '".$ord_idval."' ORDER BY `ord_id` ";
			
			$orderInfo = $this -> db -> select_one( $sqlorder );
			
			$this -> view ->  assign('orderInfo',$orderInfo);
			$this -> view ->  display('orderedit3.tpl');
			
			
			
			
		}elseif($stepval == 4)
		{
			
			$odd_numval   = $this -> input ->  req["odd_num"];
			$odd_priceval = $this -> input ->  req["odd_price"];
			$valval 	  = $this -> input ->  req["val"];
			$use_idval	  = $this -> input ->  req["use_id"];
			
			//删除 原信息
			$delsql = "DELETE FROM `".$this -> db -> prefix."orderdesc` WHERE `ord_id` = ".$ord_idval;
			$this -> db -> query_sql( $delsql );
			
			//添加 新信息
			foreach((array) $odd_numval as $prokey => $proval)
			{
				if( $proval > 0 ){
				$insertdesc[$prokey] = " INSERT INTO `".$this -> db -> prefix."orderdesc` (`use_id` ,`ord_id`,`pro_id`,`odd_num`,`odd_price`) VALUES( '$use_idval','$ord_idval','$prokey','$proval','".$odd_priceval[$prokey]."')";
				$this -> db -> insertSql($insertdesc[$prokey]);
				}
			}
			
			
			$this -> view ->  assign('info_title',$this->lang['edit_success']);
			$this -> view ->  assign('URL_TIME',URL_TIME);
			$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=order&act=info_list&id=".$ord_idval);
			$this -> view ->  display('suggestion.tpl');	
		
		}
	
	}
	
	
	function edit_info()
	{	
	
		$ord_idval 			= $this -> input ->  funpost("ord_id");
		$ord_nameval 		= $this -> input ->  funpost("ord_name");
		$ord_pinyinval 	 	= $this -> input ->  funpost("ord_pinyin");
		$ord_areaval 		= $this -> input ->  funpost("ord_area");
		
		$ord_addressval 	= $this -> input ->  funpost("ord_address");
		$ord_zipval 		= $this -> input ->  funpost("ord_zip");
		$ord_telval 		= $this -> input ->  funpost("ord_tel");
		$ord_faxval 		= $this -> input ->  funpost("ord_fax");
		
		$ord_websiteval 	= $this -> input ->  funpost("ord_website");
		$ord_emailval 		= $this -> input ->  funpost("ord_email");
		$ord_licenceval 	= $this -> input ->  funpost("ord_licence");
		$ord_chieftainval 	= $this -> input ->  funpost("ord_chieftain");
		$ord_bankrollval 	= $this -> input ->  funpost("ord_bankroll");
		$ord_turnoverval 	= $this -> input ->  funpost("ord_turnover");
		$ord_bankval 		= $this -> input ->  funpost("ord_bank");
		
		$ord_bankaccountval 	= $this -> input ->  funpost("ord_bankaccount");
		$ord_localtaxnoval 		= $this -> input ->  funpost("ord_localtaxno");
		$ord_nationaltaxnoval 	= $this -> input ->  funpost("ord_nationaltaxno");
		$ord_manageridval 		= $this -> input ->  funpost("ord_managerid");
		$ord_levelval 			= $this -> input ->  funpost("ord_level");
		$ord_satisfyval 		= $this -> input ->  funpost("ord_satisfy");
		$ord_creditval 			= $this -> input ->  funpost("ord_credit");
		$ord_losecountval 		= $this -> input ->  funpost("ord_losecount");
		$ord_statusval 			= $this -> input ->  funpost("ord_status");
		
		$addsetUp = '';
		
		if($ord_idval !='' and $ord_nameval !='' )
		{

			$updatecus = "UPDATE `".$this -> db -> prefix."order` SET  ord_name = '".$ord_nameval."' , ord_pinyin = '".$ord_pinyinval."' 
			, ord_area = '".$ord_areaval."'
			, ord_address = '".$ord_addressval."'
			, ord_zip = '".$ord_zipval."'
			, ord_tel = '".$ord_telval."'
			, ord_fax = '".$ord_faxval."'
			, ord_website = '".$ord_websiteval."'
			, ord_email = '".$ord_emailval."'
			, ord_licence = '".$ord_licenceval."'
			, ord_chieftain = '".$ord_chieftainval."'
			, ord_bankroll = '".$ord_bankrollval."'
			, ord_turnover = '".$ord_turnoverval."'
			, ord_bank = '".$ord_bankval."'
			, ord_bankaccount = '".$ord_bankaccountval."'
			, ord_localtaxno = '".$ord_localtaxnoval."'
			, ord_nationaltaxno = '".$ord_nationaltaxnoval."'
			, ord_managerid = '".$ord_manageridval."'
			, ord_level = '".$ord_levelval."'
			, ord_satisfy = '".$ord_satisfyval."'
			, ord_credit = '".$ord_creditval."'
			, ord_losecount = '".$ord_losecountval."'
			, ord_status = '".$ord_statusval."'
			 WHERE  ord_id = '".$ord_idval."' ";

			$this -> db -> query_sql( $updatecus );
			
			$this -> view ->  assign('info_title',$this->lang['edit_success']);
			$this -> view ->  assign('URL_TIME',URL_TIME);
			$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=order&act=info_list");
			$this -> view ->  display('suggestion.tpl');
			
				
		}else{
		
			$this -> view ->  assign('info_title',$this->lang['err_info_missing']);
			$this -> view ->  assign('URL_TIME',URL_TIME);
			$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=order&act=info&id=".$ord_idval);
			$this -> view ->  display('suggestion.tpl');	
		}
		
	}
	
	

	
	
}

$Order = new Order();

?>