<?php
if (!defined('HUCART_COM')) exit('HuCart.COM No direct script access allowed');
class Customer
{
	function Customer()
	{
		//$GLOBALS['WebSe'] -> isOnLine();
	}
	
	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 customer_id, customer_name, customer_pinyin, customer_email, customer_tel,customer_fax,customer_losecount,customer_level,customer_satisfy,customer_credit,customer_status  FROM  customer  WHERE 1 GROUP BY customer_id ORDER BY `customer_id` LIMIT  $limit_num , $page_size ";
		
		$info = $this -> db -> select_info($sql_query);
		
		$sql_query = " SELECT count(1) as CNUM  FROM  customer  WHERE 1  ORDER BY `customer_id`  ";
		
		$infocount = $this -> db -> select_one($sql_query);
		
		//var_dump($info);
		
		$page_str = $this -> page ->get_admin_page( $url = '?load=customer&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('customer.tpl');
		
	}
	
	function add(  )
	{
		$this -> view ->  caching = true;
		$this -> view ->  cache_lifetime = 86400 ;  //60 *  60 * 24
		$this -> view -> assign('act_url',ADMIN_WEBURL."/index.php?/customer/add_info");
		$this -> view -> display('customeradd.tpl');
	}

	
	function add_info()
	{
		$customer_nameval 		= $this -> input ->  funpost("customer_name");
		$customer_pinyinval 		= $this -> input ->  funpost("customer_pinyin");
		$customer_areaval 		= $this -> input ->  funpost("customer_area");
		
		$customer_addressval 	= $this -> input ->  funpost("customer_address");
		$customer_zipval 		= $this -> input ->  funpost("customer_zip");
		$customer_telval 		= $this -> input ->  funpost("customer_tel");
		$customer_faxval 		= $this -> input ->  funpost("customer_fax");
		
		$customer_websiteval 	= $this -> input ->  funpost("customer_website");
		$customer_emailval 		= $this -> input ->  funpost("customer_email");
		$customer_licenceval 	= $this -> input ->  funpost("customer_licence");
		$customer_chieftainval 	= $this -> input ->  funpost("customer_chieftain");
		$customer_bankrollval 	= $this -> input ->  funpost("customer_bankroll");
		$customer_turnoverval 	= $this -> input ->  funpost("customer_turnover");
		$customer_bankval 		= $this -> input ->  funpost("customer_bank");
		
		$customer_bankaccountval 	= $this -> input ->  funpost("customer_bankaccount");
		$customer_localtaxnoval 	= $this -> input ->  funpost("customer_localtaxno");
		$customer_nationaltaxnoval 	= $this -> input ->  funpost("customer_nationaltaxno");
		$customer_manageridval 		= $this -> input ->  funpost("customer_managerid");
		$customer_levelval 			= $this -> input ->  funpost("customer_level");
		$customer_satisfyval 		= $this -> input ->  funpost("customer_satisfy");
		$customer_creditval 		= $this -> input ->  funpost("customer_credit");
		$customer_losecountval 		= $this -> input ->  funpost("customer_losecount");
		$customer_statusval 		= $this -> input ->  funpost("customer_status");
		
		
		$customer_regtimeval 	= time();
		
		if($customer_nameval !='' and $customer_emailval !='' and $customer_telval !='' and $customer_pinyinval !='' )
		{
			$sql_query = " SELECT count(1) as CNUM  FROM  customer  WHERE customer_name = '".$customer_nameval."' ORDER BY `customer_id` ";
			
			$info = $this -> db -> select_one($sql_query);
			
			if( $info["CNUM"] < 1 )
			{
				
				$insertip ="INSERT INTO `customer` (`customer_name` , `customer_pinyin` , `customer_area` , `customer_address` , `customer_zip`, `customer_tel` , `customer_fax` , `customer_website` , `customer_email` , `customer_licence` , `customer_chieftain` , `customer_bankroll` , `customer_turnover`, `customer_bank` , `customer_bankaccount` , `customer_localtaxno` , `customer_nationaltaxno` , `customer_managerid`, `customer_level` , `customer_satisfy` , `customer_credit` , `customer_losecount`  , `customer_status` ) VALUES (  '$customer_nameval', '$customer_pinyinval', '$customer_areaval', '$customer_addressval', '$customer_zipval', '$customer_telval', '$customer_faxval', '$customer_websiteval', '$customer_emailval', '$customer_licenceval', '$customer_chieftainval', '$customer_bankrollval', '$customer_turnoverval', '$customer_bankval', '$customer_bankaccountval', '$customer_localtaxnoval', '$customer_nationaltaxnoval', '$customer_manageridval', '$customer_levelval', '$customer_satisfyval', '$customer_creditval', '$customer_losecountval', '$customer_statusval')";

				$this -> db -> query_sql($insertip);
				$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?/customer/info_list");
				$this -> view ->  display('suggestion.tpl');	
				
			}else{
				
				$this -> view ->  assign('info_title',$this->lang['err_info_exist']);
				$this -> view ->  assign('URL_TIME',URL_TIME);
				$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?/customer/add/");
				$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?/customer/add/");
			$this -> view ->  display('suggestion.tpl');		}
			//var_dump($_REQUEST);
	}
	
	function del( )
	{
		$indel = implode(',', $delArray);

		//$deletesql ="DELETE FROM `customer` WHERE `customer`.`customer_id` in ( $indel ) and customer_name != '".ADMIN_BASEUSER."' ";
		$deletesql ="UPDATE `customer` SET  customer_status = 5 WHERE `customer`.`customer_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?/customer/info_list/");
		$this -> view ->  display('suggestion.tpl');
	}
	
	function info(  )
	{
		$infoid = $infoArray[0];
		
		$sql_query = " SELECT *  FROM  customer  WHERE customer_id = '".$infoid."' ORDER BY `customer_id` ";
		$info = $this -> db -> select_one($sql_query);
				
		$this -> view -> assign('act_url',ADMIN_WEBURL."/index.php?/customer/edit_info");
		$this -> view ->  assign('info',$info);
		$this -> view ->  display('customeredit.tpl');
	
	}
	
	
	function edit_info( )
	{	
	
		$customer_idval 		= $this -> input ->  funpost("customer_id");
		$customer_nameval 		= $this -> input ->  funpost("customer_name");
		$customer_pinyinval 	= $this -> input ->  funpost("customer_pinyin");
		$customer_areaval 		= $this -> input ->  funpost("customer_area");
		
		$customer_addressval 	= $this -> input ->  funpost("customer_address");
		$customer_zipval 		= $this -> input ->  funpost("customer_zip");
		$customer_telval 		= $this -> input ->  funpost("customer_tel");
		$customer_faxval 		= $this -> input ->  funpost("customer_fax");
		
		$customer_websiteval 	= $this -> input ->  funpost("customer_website");
		$customer_emailval 		= $this -> input ->  funpost("customer_email");
		$customer_licenceval 	= $this -> input ->  funpost("customer_licence");
		$customer_chieftainval 	= $this -> input ->  funpost("customer_chieftain");
		$customer_bankrollval 	= $this -> input ->  funpost("customer_bankroll");
		$customer_turnoverval 	= $this -> input ->  funpost("customer_turnover");
		$customer_bankval 		= $this -> input ->  funpost("customer_bank");
		
		$customer_bankaccountval 	= $this -> input ->  funpost("customer_bankaccount");
		$customer_localtaxnoval 	= $this -> input ->  funpost("customer_localtaxno");
		$customer_nationaltaxnoval 	= $this -> input ->  funpost("customer_nationaltaxno");
		$customer_manageridval 		= $this -> input ->  funpost("customer_managerid");
		$customer_levelval 			= $this -> input ->  funpost("customer_level");
		$customer_satisfyval 		= $this -> input ->  funpost("customer_satisfy");
		$customer_creditval 		= $this -> input ->  funpost("customer_credit");
		$customer_losecountval 		= $this -> input ->  funpost("customer_losecount");
		$customer_statusval 		= $this -> input ->  funpost("customer_status");
		
		
		$addsetUp = '';
		
		if($customer_idval !='' and $customer_nameval !='' )
		{

			
			$updatecus = "UPDATE `customer` SET  customer_name = '".$customer_nameval."' , customer_pinyin = '".$customer_pinyinval."' 
			, customer_area = '".$customer_areaval."'
			, customer_address = '".$customer_addressval."'
			, customer_zip = '".$customer_zipval."'
			, customer_tel = '".$customer_telval."'
			, customer_fax = '".$customer_faxval."'
			, customer_website = '".$customer_websiteval."'
			, customer_email = '".$customer_emailval."'
			, customer_licence = '".$customer_licenceval."'
			, customer_chieftain = '".$customer_chieftainval."'
			, customer_bankroll = '".$customer_bankrollval."'
			, customer_turnover = '".$customer_turnoverval."'
			, customer_bank = '".$customer_bankval."'
			, customer_bankaccount = '".$customer_bankaccountval."'
			, customer_localtaxno = '".$customer_localtaxnoval."'
			, customer_nationaltaxno = '".$customer_nationaltaxnoval."'
			, customer_managerid = '".$customer_manageridval."'
			, customer_level = '".$customer_levelval."'
			, customer_satisfy = '".$customer_satisfyval."'
			, customer_credit = '".$customer_creditval."'
			, customer_losecount = '".$customer_losecountval."'
			, customer_status = '".$customer_statusval."'

			
			 WHERE  customer_id = '".$customer_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?/customer/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?/customer/info/".$customer_idval);
			$this -> view ->  display('suggestion.tpl');	
		}
		
	}
	
	

	
	
}

$Customer = new Customer();

?>