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

	}
	
	function infolist()
	{
		$pageSize = 20 ;		
		$pageNumval 	= $GLOBALS['Reque'] -> reqval["pageNum"];
		
		if( $pageNumval > 1 )
		{
			$pageNum  = $pageNumval;
			$LIMITnum = ceil( ( $pageNumval -1 ) * $pageSize );
		}else{
			$pageNum  = 1;
			$LIMITnum = 0 ;
		}

		//帐号 	真实姓名 	Email地址 	登录次数 	加入时间 	最后登录时间
		$sqlQuery = " 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  `".SQL_PREFIX."order`  as o , ".SQL_PREFIX."user as u  WHERE o.use_id = u.use_id  GROUP BY ord_id ORDER BY `ord_id` LIMIT  $LIMITnum , $pageSize ";
		
		$info = $GLOBALS['MySql'] -> selectInfo($sqlQuery);
		
		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"] = '无';
			}
		}
		
		
		
		$sqlQuery = " SELECT count(1) as CNUM  FROM  `".SQL_PREFIX."order`  WHERE 1  ORDER BY `ord_id`  ";
		
		$infocount = $GLOBALS['MySql'] -> selectOne($sqlQuery);
		
		//var_dump($info);
		
		$PageStr = $GLOBALS['Page']->buildPageStr( $url = '?load=order&act=infolist', $pageNum  , $pageSize  , $resultCount = $infocount["CNUM"] );
		
		//$GLOBALS['Templ']->assign('ADMIN_BASEUSER',ADMIN_BASEUSER);
		
		$GLOBALS['Templ']->assign('PageStr',$PageStr);
		$GLOBALS['Templ']->assign('info',$info);
		$GLOBALS['Templ']->display('order_tpl.html');
		
		
		
	}
	
	function add(  )
	{
		///$GLOBALS['Templ'] -> caching = true;
		//$GLOBALS['Templ'] -> cache_lifetime = 86400 ;  //60 *  60 * 24
		
		$stepval 	= $GLOBALS['Reque'] -> reqval["step"];
		
		if($stepval == '')
		{
			$GLOBALS['Templ']->assign('actUrl',EMPLOYEE_WEBURL."/index.php?load=order&act=addinfo");
			$GLOBALS['Templ']->display('orderadd_tpl.html');
		}elseif($stepval == 2)
		{
			//var_dump($_REQUEST);
			
			$adduserval = $GLOBALS['Reque'] -> reqval["adduser"];
			$addnameval = $GLOBALS['Reque'] -> reqval["addname"];
			if($adduserval!='')
			{
				//查询客户信息
				$sqlQuery = "SELECT  add_id,use_id,add_phone,add_mobile,add_name,add_pcdall,add_address FROM ".SQL_PREFIX."address  WHERE use_id IN ( SELECT use_id  FROM ".SQL_PREFIX."user WHERE use_id = '".$adduserval."'  OR use_name = '".$adduserval."' ) limit 0 , 100 ";
				$info = $GLOBALS['MySql'] -> selectInfo($sqlQuery);
				
				//var_dump($info);
				
			}elseif($addnameval!='')
			{

				$sqlQuery = "SELECT  add_id,use_id,add_phone,add_mobile,add_name,add_pcdall,add_address FROM ".SQL_PREFIX."address  WHERE add_name LIKE  '".$addnameval."%' limit 0 , 100 ";
				$info = $GLOBALS['MySql'] -> selectInfo($sqlQuery);

			}else{
			
				echo 'empty';
			
			}

			//查询

			$GLOBALS['Templ'] -> assign('info',$info);
			
			$GLOBALS['Templ']->assign('actUrl',EMPLOYEE_WEBURL."/index.php?load=order&act=addinfo");
			$GLOBALS['Templ']->display('orderstep2_tpl.html');
			
			

		}elseif($stepval == 3)
		{
			$addidval = $GLOBALS['Reque'] -> reqval["addid"];
			
			//echo $addidval;
			$sqlQuery = "SELECT  add_id,use_id,add_phone,add_mobile,add_name,add_pcdall,add_address FROM ".SQL_PREFIX."address  WHERE add_id = '".$addidval."' limit 0 , 1 ";
			$addressinfo = $GLOBALS['MySql'] -> selectOne($sqlQuery);
			
			$GLOBALS['Templ'] -> assign('addressinfo',$addressinfo);
			
			//查询商品信息
			$sqlpro = "SELECT pro_id,pro_name,pro_sellprice FROM ".SQL_PREFIX."product WHERE pro_enabled = 1  GROUP BY pro_id ORDER BY pro_uptime DESC LIMIT 0, 30 ";
			
			$proInfo = $GLOBALS['MySql'] -> selectInfo($sqlpro);
			
			//var_dump( $proInfo );
			
			$GLOBALS['Templ'] -> assign('proInfo',$proInfo);
			$GLOBALS['Templ']->display('orderstep3_tpl.html');


		}elseif($stepval == 4)
		{
			$use_idval = $GLOBALS['Reque'] -> reqval["use_id"];
			$add_idval = $GLOBALS['Reque'] -> reqval["add_id"];
			$odd_numval = $GLOBALS['Reque'] -> reqval["odd_num"];
			$odd_priceval = $GLOBALS['Reque'] -> reqval["odd_price"];
			
			//add_phone,add_mobile
			

			
			$sqlQuery = "SELECT  add_id,use_id,add_phone,add_mobile,add_name,add_province,add_city,add_district,add_pcdall,add_address FROM ".SQL_PREFIX."address  WHERE add_id = '".$add_idval."' limit 0 , 1 ";
			$addressinfo = $GLOBALS['MySql'] -> selectOne($sqlQuery);
			
			$ord_addtimeval = time();
			
			$ord_snval =  $this -> buildsn();
			$insertorder = "INSERT INTO `".SQL_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  = $GLOBALS['MySql'] -> insertSql($insertorder);
			
			foreach((array) $odd_numval as $prokey => $proval)
			{
				if( $proval > 0 ){
				$insertdesc[$prokey] = " INSERT INTO `".SQL_PREFIX."orderdesc` (`use_id` ,`ord_id`,`pro_id`,`odd_num`,`odd_price`) VALUES( '$use_idval','$ord_idval','$prokey','$proval','".$odd_priceval[$prokey]."')";
				$GLOBALS['MySql'] -> insertSql($insertdesc[$prokey]);
				}
			}
			
			$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_addsusse']);
			$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
			$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=order&act=infolist");
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');
		
		}
	}

	function buildsn()
	{
		$today = date('Ymd');
		$countNum = " SELECT COUNT(1) as CNUM FROM `".SQL_PREFIX."order` WHERE ord_addtime >   ".strtotime($today) ;
		$countInfo = $GLOBALS['MySql'] -> selectOne($countNum);

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

	}
	
	function del( )
	{
		$indel 	 = $GLOBALS['Reque'] -> reqval["id"];

		$deletesql ="UPDATE `".SQL_PREFIX."order` SET  ord_status = 5  WHERE `order`.`ord_id` IN ( $indel )  ";
		
		$GLOBALS['MySql'] -> querySql($deletesql);
		
		$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_delsusse']);
		$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
		$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=order&act=infolist");
		$GLOBALS['Templ'] -> display('suggestion_tpl.html');
	}
	
	function info()
	{
		$ord_idval = $GLOBALS['Reque'] -> reqval["id"];
		$stepval = $GLOBALS['Reque'] -> reqval["step"];
		
		$GLOBALS['Templ'] -> assign('idval',$ord_idval);
		
		if($stepval == '')
		{
			$sqlQuery = " SELECT *  FROM  `".SQL_PREFIX."order`  WHERE ord_id = '".$ord_idval."' ORDER BY `ord_id` ";
			
			$info = $GLOBALS['MySql'] -> selectOne( $sqlQuery );
	
			//用户收货信息表
			$sqladdress = "SELECT  add_id,use_id,add_phone,add_mobile,add_name,add_pcdall,add_address FROM ".SQL_PREFIX."address  WHERE use_id = '".$info["use_id"]."' limit 0 , 100 ";
			$infoAddress = $GLOBALS['MySql'] -> selectInfo($sqladdress);
			
			//var_dump( $infoAddress );
			
			$GLOBALS['Templ'] -> assign('infoAddress',$infoAddress);	
			$GLOBALS['Templ'] -> assign('actUrl',EMPLOYEE_WEBURL."/index.php?load=order&act=editinfo");
			$GLOBALS['Templ'] -> assign('info',$info);
			$GLOBALS['Templ'] -> display('orderedit_tpl.html');
		}elseif($stepval == 3)
		{
			
			//更新order地址信息
			$addidval = $GLOBALS['Reque'] -> reqval["addid"];
			
			if( $addidval !='' )
			{
				$sqlQuery = "SELECT  add_id,use_id,add_phone,add_mobile,add_name,add_province,add_city,add_district,add_pcdall,add_address FROM ".SQL_PREFIX."address  WHERE add_id = '".$addidval."' limit 0 , 1 ";
				$addressinfo = $GLOBALS['MySql'] -> selectOne($sqlQuery);
				
				$updatesql = " UPDATE  ".SQL_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;
				$GLOBALS['MySql'] -> querySql( $updatesql );
				
			}
			
 
			
			
			
			
			$sqlQuerydetail = " SELECT o.*,p.pro_name   FROM  `".SQL_PREFIX."orderdesc` AS o  left join ".SQL_PREFIX."product as p on p.pro_id = o.pro_id    WHERE o.ord_id = '".$ord_idval."' ORDER BY o.ord_id ";		//,p.pro_sellprice ,p.pro_num
			$infodetail = $GLOBALS['MySql'] -> selectInfo( $sqlQuerydetail ); 
			
			
			//查询商品信息
			$sqlpro = "SELECT pro_id,pro_name,pro_sellprice FROM ".SQL_PREFIX."product WHERE pro_enabled = 1  GROUP BY pro_id ORDER BY pro_uptime DESC LIMIT 0, 30 ";
			
			$proInfo = $GLOBALS['MySql'] -> selectInfo($sqlpro);
			
			$GLOBALS['Templ'] -> assign('infodetail',$infodetail);
			$GLOBALS['Templ'] -> assign('proInfo',$proInfo);
			
			$sqlorder = " SELECT *  FROM  `".SQL_PREFIX."order`  WHERE ord_id = '".$ord_idval."' ORDER BY `ord_id` ";
			
			$orderInfo = $GLOBALS['MySql'] -> selectOne( $sqlorder );
			
			$GLOBALS['Templ'] -> assign('orderInfo',$orderInfo);
			$GLOBALS['Templ'] -> display('orderedit3_tpl.html');
			
			
			
			
		}elseif($stepval == 4)
		{
			
			$odd_numval   = $GLOBALS['Reque'] -> reqval["odd_num"];
			$odd_priceval = $GLOBALS['Reque'] -> reqval["odd_price"];
			$valval 	  = $GLOBALS['Reque'] -> reqval["val"];
			$use_idval	  = $GLOBALS['Reque'] -> reqval["use_id"];
			
			//删除 原信息
			$delsql = "DELETE FROM `".SQL_PREFIX."orderdesc` WHERE `ord_id` = ".$ord_idval;
			$GLOBALS['MySql'] -> querySql( $delsql );
			
			//添加 新信息
			foreach((array) $odd_numval as $prokey => $proval)
			{
				if( $proval > 0 ){
				$insertdesc[$prokey] = " INSERT INTO `".SQL_PREFIX."orderdesc` (`use_id` ,`ord_id`,`pro_id`,`odd_num`,`odd_price`) VALUES( '$use_idval','$ord_idval','$prokey','$proval','".$odd_priceval[$prokey]."')";
				$GLOBALS['MySql'] -> insertSql($insertdesc[$prokey]);
				}
			}
			
			
			$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_editsusse']);
			$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
			$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=order&act=infolist&id=".$ord_idval);
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');	
		
		}
	
	}
	
	
	function editinfo()
	{	
	
		$ord_idval 			= $GLOBALS['Reque'] -> funpost("ord_id");
		$ord_nameval 		= $GLOBALS['Reque'] -> funpost("ord_name");
		$ord_pinyinval 	 	= $GLOBALS['Reque'] -> funpost("ord_pinyin");
		$ord_areaval 		= $GLOBALS['Reque'] -> funpost("ord_area");
		
		$ord_addressval 	= $GLOBALS['Reque'] -> funpost("ord_address");
		$ord_zipval 		= $GLOBALS['Reque'] -> funpost("ord_zip");
		$ord_telval 		= $GLOBALS['Reque'] -> funpost("ord_tel");
		$ord_faxval 		= $GLOBALS['Reque'] -> funpost("ord_fax");
		
		$ord_websiteval 	= $GLOBALS['Reque'] -> funpost("ord_website");
		$ord_emailval 		= $GLOBALS['Reque'] -> funpost("ord_email");
		$ord_licenceval 	= $GLOBALS['Reque'] -> funpost("ord_licence");
		$ord_chieftainval 	= $GLOBALS['Reque'] -> funpost("ord_chieftain");
		$ord_bankrollval 	= $GLOBALS['Reque'] -> funpost("ord_bankroll");
		$ord_turnoverval 	= $GLOBALS['Reque'] -> funpost("ord_turnover");
		$ord_bankval 		= $GLOBALS['Reque'] -> funpost("ord_bank");
		
		$ord_bankaccountval 	= $GLOBALS['Reque'] -> funpost("ord_bankaccount");
		$ord_localtaxnoval 		= $GLOBALS['Reque'] -> funpost("ord_localtaxno");
		$ord_nationaltaxnoval 	= $GLOBALS['Reque'] -> funpost("ord_nationaltaxno");
		$ord_manageridval 		= $GLOBALS['Reque'] -> funpost("ord_managerid");
		$ord_levelval 			= $GLOBALS['Reque'] -> funpost("ord_level");
		$ord_satisfyval 		= $GLOBALS['Reque'] -> funpost("ord_satisfy");
		$ord_creditval 			= $GLOBALS['Reque'] -> funpost("ord_credit");
		$ord_losecountval 		= $GLOBALS['Reque'] -> funpost("ord_losecount");
		$ord_statusval 			= $GLOBALS['Reque'] -> funpost("ord_status");
		
		$addsetUp = '';
		
		if($ord_idval !='' and $ord_nameval !='' )
		{

			$updatecus = "UPDATE `".SQL_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."' ";

			$GLOBALS['MySql'] -> querySql( $updatecus );
			
			$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_editsusse']);
			$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
			$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=order&act=infolist");
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');
			
				
		}else{
		
			$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_losssusse']);
			$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
			$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=order&act=info&id=".$ord_idval);
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');	
		}
		
	}
	
	

	
	
}

$Order = new Order();

?>