<?php $PageSecurity = 11;
include('includes/session.inc');
$title = _('Customer');
include('includes/header.inc');
include('includes/SQL_CommonFunctions.inc');
include('includes/function_lib.php');

if(isset($_POST['submit'])){
	//extract($_POST);
	  $gov_date = ChangDateToYmd($gov_date); 
	$lst_date = ChangDateToYmd($lst_date);
	$cst_date = ChangDateToYmd($cst_date);
	$excies_date = ChangDateToYmd($excies_date);
	$pf_date = ChangDateToYmd($pf_date);
	$esi_date = ChangDateToYmd($esi_date);
	$company_req_date = ChangDateToYmd($company_req_date);
	$termination_date = ChangDateToYmd($termination_date);
						$sql = "INSERT clientmaster SET
						`c_name`		='".$_POST['c_name']."', 
						`short_name`	='".$_POST['short_name']."', 
						`ledger_id`	='".$_POST['clientgroup']."', 
						`ccategory`		='".$_POST['ccategory']."', 
						`pre_address1`	='".$_POST['pre_address1']."',
						`pre_address2`	='".$_POST['pre_address2']."',
						`pre_address3`	='".$_POST['pre_address3']."',  
						`pre_area`		='".$_POST['pre_area']."', 
						`fax`			='".$_POST['fax']."', 
						`pre_country`	='".$_POST['pre_country']."',  
						`pre_mobile`	='".$_POST['pre_mobile']."',
						`pre_state`		='".$_POST['pre_state']."', 
						`pre_city`		='".$_POST['pre_city']."', 
						`pre_pincode`	='".$_POST['pre_pincode']."',
						`con_person_1`	='".$_POST['con_person_1']."',
						`con_per_des_1`	='".$_POST['con_per_des_1']."',
						`con_person_2`	='".$_POST['con_person_2']."',  
						`con_per_des_2`	='".$_POST['con_per_des_2']."',
						`gov_no`		='".$_POST['gov_no']."',  
						`per_email`    ='".$_POST['per_email']."',
						`lst_no`		='".$_POST['lst_no']."', 
 						`pf_no`			='".$_POST['pf_no']."', 
 						`cst_no`		= '".$_POST['cst_no']."', 
 						`esi_no`		='".$_POST['esi_no']."',
 						`excies_no`		='".$_POST['excies_no']."',  
 						`reference`		='".$_POST['reference']."', 
						`company_req_date`='". $company_req_date ."', 
						`termination_date`='". $termination_date ."', 
						`pc_tds`='".$_POST['pc_tds']."',
						`remarks`='".$_POST['remarks']."',
						`pan_no` = '".$_POST['pan_no']."',
						`Extension`='".$_POST['pan_no']."',
						`pre_phone1`='".$_POST['pre_phone1']."',
						`pre_phone2`='".$_POST['pre_phone2']."'";
						$res = DB_query($sql,$db,$ErrMsg,$DbgMsg);
						$s->pageLocation('Show_client.php');
						
	
}

echo '<a href="' . $rootpath . '/Show_client.php?' .SID . '&NewOrder=Yes">' . _('Customer>>') . '</a>'; 
echo '<a href="' . $rootpath . '/Client_Master.php?' .SID . '&NewOrder=Yes">' . _('Add Customer>>') . '</a>'; 

?>

<script>

function validation(form){
  
	if(document.getElementById('c_name').value==''){
		alert('Name should not be empty');
		document.getElementById('c_name').focus();
		return false;
	}
	if(document.getElementById('short_name').value==''){
		alert('Short name should not be empty');
		document.getElementById('short_name').focus();
		return false;
	}
	if(document.getElementById('ccategory').value==''){
		alert('Category should not be empty');
		document.getElementById('ccategory').focus();
		return false;
	}
	if(document.getElementById('pre_address1').value==''){
		alert('Address should not be empty');
		document.getElementById('pre_address1').focus();
		return false;
	}
	if(document.getElementById('pre_country').value==''){
		alert('Select your country');
		document.getElementById('pre_country').focus();
		return false;
	}
	if(document.getElementById('pre_state').value==''){
		alert('Select your state');
		document.getElementById('pre_state').focus();
		return false;
	}if(document.getElementById('pre_city').value==''){
		alert('Select your city');
		document.getElementById('pre_city').focus();
		return false;
	}
	if(document.getElementById('pre_pincode').value==''){
		alert('Pin code should not be empty');
		document.getElementById('pre_pincode').focus();
		return false;
	}
	if(isNaN(document.getElementById('pre_pincode').value)){
		alert('Pin Code number should be numberic');
		document.getElementById('pre_pincode').focus();
		return false;
	}
	if(document.getElementById('pre_phone1').value =="")
	{
	 	alert('Phone number should not be empty');
	 	document.getElementById('pre_phone1').focus();
	  	return false;
	}
	if(isNaN(document.getElementById('pre_phone1').value)){
		alert('Phone number should be numberic');
		document.getElementById('pre_phone1').focus();
		return false;
	}
		
	if(document.getElementById('pre_mobile').value==''){
		alert('Mobile number should not be empty');
		document.getElementById('pre_mobile').focus();
		return false;
	}
	if(isNaN(document.getElementById('pre_mobile').value)){
		alert('Mobile number should be numberic');
		document.getElementById('pre_mobile').focus();
		return false;
	}
	
	if(document.getElementById('per_email').value==''){
		alert('Email Id should not be empty');
		document.getElementById('per_email').focus();
		return false;
	}
		
	return true;
}	

function search_state(str)
{
	//alert(str);
if (str.length==0)
  {
  document.getElementById("searchstate").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("searchstate").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","get_state.php?country="+str,true);
xmlhttp.send();
}

function search_city(str)
{
	//alert(str);
	var country = document.getElementById("pre_country").value;
	//alert("country="+country);
	if (str.length==0)
  {
  document.getElementById("searchcity").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("searchcity").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","get_city.php?state="+str+"&country="+country,true);
xmlhttp.send();
}

</script>

<?php

if(isset($_POST['update'])){

    extract($_POST); 
    $gov_date = FormatDateForSQL($gov_date); 
	$lst_date = FormatDateForSQL($lst_date);
	$cst_date = FormatDateForSQL($cst_date);
	$excies_date = FormatDateForSQL($excies_date);
	$pf_date = FormatDateForSQL($pf_date);
	$esi_date = FormatDateForSQL($esi_date);
	$company_req_date = FormatDateForSQL($company_req_date);
	$termination_date = FormatDateForSQL($termination_date);
 
		    	  $sql = "Update clientmaster SET
				`c_name`		='".$_POST['c_name']."', 
				`short_name`	='".$_POST['short_name']."',
				`ledger_id`	='".$_POST['clientgroup']."',   
				`ccategory`		='".$_POST['ccategory']."', 
				`salesman_id`		='".$_POST['salesman_id']."', 				
				`pre_address1`	='".$_POST['pre_address1']."',
				`pre_address2`	='".$_POST['pre_address2']."',
				`pre_address3`	='".$_POST['pre_address3']."',  
				`pre_area`		='".$_POST['pre_area']."', 
				`fax`			='".$_POST['fax']."', 
				`pre_country`	='".$_POST['pre_country']."',  
				`pre_mobile`	='".$_POST['pre_mobile']."',
				`pre_state`		='".$_POST['pre_state']."', 
				`pre_city`		='".$_POST['pre_city']."', 
				`pre_pincode`	='".$_POST['pre_pincode']."',
				`con_person_1`	='".$_POST['con_person_1']."',
				`con_per_des_1`	='".$_POST['con_per_des_1']."',
				`con_person_2`	='".$_POST['con_person_2']."',  
				`con_per_des_2`	='".$_POST['con_per_des_2']."',
				`gov_no`		='".$_POST['gov_no']."',  
 				`per_email`    ='".$_POST['per_email']."',
				`lst_no`		='".$_POST['lst_no']."', 
 				`pf_no`			='".$_POST['pf_no']."', 
 				`cst_no`		= '".$_POST['cst_no']."', 
 				`esi_no`		='".$_POST['esi_no']."',
 				`excies_no`		='".$_POST['excies_no']."',  
 				`reference`		='".$_POST['reference']."', 
				`company_req_date`='". $company_req_date ."', 
				`termination_date`='". $termination_date ."', 
				`pc_tds`='".$_POST['pc_tds']."',
				`remarks`='".$_POST['remarks']."',
				`pan_no` = '".$_POST['pan_no']."',
				`Extension`='".$_POST['pan_no']."',
				`pre_phone1`='".$_POST['pre_phone1']."',
				`pre_phone2`='".$_POST['pre_phone2']."' where id='".$_REQUEST['pc_no']."'";

			
				$res = DB_query($sql,$db,$ErrMsg,$DbgMsg);
				 
				
				 if($res == true)
				{
				 header("Location: Show_client.php");
				}
}
?>
<link rel="stylesheet" href="css/jquery.ui.all.css" />
<script src="js/jquery-1.5.1.js"></script>
<script src="ui/jquery.ui.core.js"></script>
<script src="ui/jquery.ui.widget.js"></script>
<script src="ui/jquery.min.js"></script>
<script src="ui/jqueryui.min.js"></script>
<script>
$(function() {
$(".datepicker").datepicker({ dateFormat: 'dd/mm/yy' ,changeMonth: true,
		changeYear: true,
		yearRange: '-80:+20'});
});
</script>

<?php 
if($_GET['action']=='editRec'){
$SQL =  "select * from clientmaster Where clientmaster.id='".$_GET['pc_no']."' "; 
$result = DB_query($SQL,$db,$ErrMsg);
$row = DB_fetch_assoc($result);
}

?>
<form  enctype="multipart/form-data" method="post" action="" onsubmit="return validation(this.form);">
<input type="hidden" name="id" value="<?php if(isset($row['id'])) echo $row['id']; ?>" />
  <table width="100%" border="1" align="center">
    
    <tr>
      <td  colspan="5" align="center"><font color="blue" size="4">CUSTOMER INFORMATION</font></td>
    </tr>
    <tr>
      <td  ><span class="mandatory">*</span>Customer  Name </td>
      <td colspan="2">
	  <input   name="c_name" type="text" id="c_name" value='<?php if(isset($row['c_name'])) echo $row['c_name']; ?>' size="40" />
      <a target="_blank" href="/armtech/stockmaincategory.php?"></a></td>
      <td  ><span class="mandatory">*</span>Short Name </td>
      <td   ><input type="text" id="short_name"   name="short_name" value='<?php if(isset($row['short_name'])) echo $row['short_name']; ?>'  /></td>
    </tr>
    
	<tr>
	  <td><span class="mandatory">*</span>Customer Category  </td>
	  <td colspan="2"><select name="ccategory" id="ccategory">
        <option value="">Select</option>
        <option value="Excellent" <?php echo ($row['ccategory']=='Excellent'?"selected":"") ?> >Excellent</option>
        <option value="Good" <?php echo ($row['ccategory']=='Good'?"selected":"") ?>>Good</option>
        <option value="Average" <?php echo ($row['ccategory']=='Average'?"selected":"") ?>>Average</option>
        <option value="Poor" <?php echo ($row['ccategory']=='Poor'?"selected":"") ?>>Poor</option>
      </select></td>
	  <td><span class="mandatory">*</span>Account Group </td>
	  <td><?php 
	     $account_sql = "select B.AccountName as AccountName, B.AccountsID from accountgroupmaster A, accountledger B  where A.GroupName = LOWER('SUNDRY DEBTORS') and A.GroupId=B.UnderGroup ";
	  $accountgroups = DB_query($account_sql, $db); ?>
	    <select name="clientgroup" id="clientgroup" class="inputfld">
          <option value="000">Select Client Group</option>
          <?php while($accountrow = DB_fetch_array($accountgroups))
	  {?>
       <option value="<?php echo $accountrow['AccountsID']; ?>" <?php echo  ($row['ledger_id']==$accountrow['AccountsID']?"selected":"")?>><?php echo $accountrow['AccountName']; ?></option>
          <?php } ?>
        </select></td>
    </tr>
	<tr>
      <td>Sales Person  </td>
      <td colspan="2"> 
        
			<?php
 	$SQL_salesmaster=DB_query("SELECT * from salesman_master Where 1=1",$db);
	?>
	<select  name="salesman_id" style="width:140px;"  id="salesman_id" >
	<option >--Select SalesMan --</option>
	<?php
	while($row_sales_master=DB_fetch_array($SQL_salesmaster))
	{
	  $p="";
	  if($row_sales_master['id']==$row['salesman_id'])
	  $p="selected";
	 ?>
	 <option value="<?php  echo $row_sales_master['id']?>"<?php echo $p;?> ><?php  echo $row_sales_master['sales_name']?></option>
	<?php }?>
	</select></td>  
      <td>&nbsp;</td>
      <td>&nbsp;</td>
	</tr>
	<tr>
      <td colspan="5" align="center"><p>&nbsp;</p>
      <p align="center"><font color="blue" size="4">CUSTOMER ADDRESS </font></p></td>
    </tr>
    <tr>
      <td rowspan="3"><span class="mandatory">*</span> Address  </td>
      <td colspan="2"><input   name="pre_address1" type="text" id="pre_address1" value='<?php if(isset($row['pre_address1'])) echo $row['pre_address1']; ?>' size="40"  /></td>
      <td>Extension</td>
      <td><input type="text" id="pre_ext"   name="pre_ext" value='<?php if(isset($row['Extension'])) echo $row['Extension']; ?>'></td>
    </tr>
    <tr>
      <td colspan="2"><input   name="pre_address2" type="text" id="pre_address2" value='<?php if(isset($row['pre_address2'])) echo $row['pre_address2']; ?>' size="40"  /></td>
      <td><span class="mandatory">*</span> Phone 1</td>
      <td><input type="text" id="pre_phone1"   name="pre_phone1" value='<?php if(isset($row['pre_phone1'])) echo $row['pre_phone1']; ?>'></td>
    </tr>
    <tr>
      <td colspan="2"><input   name="pre_address3" type="text" id="pre_address3" value='<?php if(isset($row['pre_address3'])) echo $row['pre_address2']; ?>' size="40"  /></td>
      <td>Phone 2 </td>
      <td><input type="text" id="pre_phone2"   name="pre_phone2" value='<?php if(isset($row['pre_phone2'])) echo $row['pre_phone2']; ?>'></td>
    </tr>
    
    <tr>
      <td>Area </td>
      <td colspan="2"><input   name="pre_area" type="text" id="pre_area" value='<?php if(isset($row['pre_area'])) echo $row['pre_area']; ?>' size="40"  /></td>
      <td>Fax </td>
      <td><input type="text" id="fax"   name="fax" value='<?php if(isset($row['fax'])) echo $row['fax']; ?>'></td>
    </tr>
    <tr>
      <td><span class="mandatory">* Country </span></td>
      <td colspan="2"><select name="pre_country" id="pre_country" class="inputfld" onchange="search_state(this.value)">
          <option value="">---Select Country---</option>
        <?php 
			$c_sql = "select * from country_master";
			$c_result = DB_query($c_sql,$db);
			while($c_row=DB_fetch_array($c_result))
			{
				$p='';
				if($c_row['id']==$_POST['pre_country'])
				$p= "selected";
			?>
				<option value="<?php echo $c_row['id']; ?>"<?php echo ($row['pre_country']==$c_row['id']?"selected":"") ?>><?php echo $c_row['country']; ?></option>
	  <?php }
		?>
		</select></td>
      <td><span class="mandatory">*</span> Mobile </td>
      <td><input type="text" id="pre_mobile"   name="pre_mobile" value='<?php if(isset($row['pre_mobile'])) echo $row['pre_mobile']; ?>'></td>
    </tr>
    <tr>
     <td><span class="mandatory">*</span>State</td>
 <td colspan="2"><div id="searchstate">
 <select name="pre_state" id="pre_state" class="inputfld" onchange="search_city(this.value)">
       <option value="">---Select State---</option>
	   <?php 
	    $sql = "select * from state_master where country = '".$row['pre_country']."' ";
		$result=DB_query($sql,$db);
		while($myrow=DB_fetch_array($result))
		{ 
			$p='';
			if($myrow['id'] == $row['pre_state'])
			$p="selected";
		?>
		<option value="<?php echo $myrow['id'];?>" <?php echo ($row['pre_state']==$myrow['id']?"selected":"") ?>><?php echo $myrow['state']; ?></option>
		<?php }
	   ?>
      </select>
 </div></td>
      <td><span class="mandatory">*</span> Email Id </td>
      <td><input type="text" id="per_email"   name="per_email" value='<?php if(isset($row['per_email'])) echo $row['per_email']; ?>'  /></td>
    </tr>
    <tr>
      <td><span class="mandatory">*City</span></td>
      <td colspan="2"><div id="searchcity"><select name="pre_city" id="pre_city" class="inputfld">
       <option value="">---Select City---</option>
	   <?php 
	   $sql = "select * from city_master where country='".$row['pre_country']."' and state='".$row['pre_state']."'";
	   $result = DB_query($sql,$db);
	   while($city_row = DB_fetch_array($result))
	   {
	   		$p='';
			if($city_row['id'] == $row['pre_city'])
			$p="selected";
	   ?>
	  <option value="<?php echo $city_row['id']; ?>" <?php echo ($row['pre_city']==$city_row['id']?"selected":"") ?>><?php echo $city_row['city']; ?></option>
	   <?php }?>
      </select></div></td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>* Pin Code  </td>
      <td colspan="2"><input type="text" id="pre_pincode" name="pre_pincode" value='<?php if(isset($row['pre_pincode'])) echo $row['pre_pincode']; ?>' /></td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
   
    <tr>
      <td colspan="5" align="center"><p>&nbsp;</p>
      <p align="center"><font color="blue" size="4">CONTACT PERSON INFORMATION </font></p></td>
    </tr>
    <tr>
      <td>Contact Person 1 </td>
      <td colspan="2"><input   name="con_person_1" type="text" id="con_person_1" value='<?php if(isset($row['con_person_1'])) echo $row['con_person_1']; ?>' size="40"  /></td>
      <td>Contact Person 1 Designation</td>
      <td><input   name="con_per_des_1" type="text" id="con_per_des_1" value='<?php if(isset($row['con_per_des_1'])) echo $row['con_per_des_1']; ?>' size="40"  /></td>
    </tr>
    <tr>
      <td>Contact Person 2 </td>
      <td colspan="2"><input   name="con_person_2" type="text" id="con_person_2" value='<?php if(isset($row['con_person_2'])) echo $row['con_person_2']; ?>' size="40"  /></td>
      <td>Contact Person 2 Designation</td>
      <td><input   name="con_per_des_2" type="text" id="con_per_des_2" value='<?php if(isset($row['con_per_des_2'])) echo $row['con_per_des_2']; ?>' size="40"  /></td>
    </tr>
    <tr>
      <td colspan="5" align="center"><p>&nbsp;</p>
      <p align="center"><font color="blue" size="4">OTHER INFORMATION </font></p></td>
    </tr>
    <tr>
      <td colspan="5" align="center">
	  
	  <table width="100%" border="1">
	  
	  <tr>
      <td>Govt.Reg. No.&amp; Date  </td>
      <td ><input type="text" id="gov_no"   name="gov_no" value='<?php if(isset($row['gov_no'])) echo $row['gov_no']; ?>'  /></td>
    
      <td>PAN No. </td>
      <td><input type="text" id="pan_no"   name="pan_no" value='<?php if(isset($row['pan_no'])){echo $row['pan_no'] ;} ?>'/></td>
    </tr>
	
	  <tr>
      <td>TIN No.&amp;Date </td>
      <td><input type="text" id="lst_no"   name="lst_no" value='<?php if(isset($row['lst_no'])) echo $row['lst_no']; ?>'  /></td>
      
      <td>PF.No.&amp; Date  </td>
      <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td ><input type="text" id="pf_no"   name="pf_no" value='<?php if(isset($row['pf_no'])) echo $row['pf_no']; ?>' /></td>
           
        </tr>
      </table></td>
    </tr>
	
	
    <tr>
      <td>CST No.&amp; Date</td>
      <td><input type="text" id="cst_no"   name="cst_no" value='<?php if(isset($row['cst_no'])) echo $row['cst_no']; ?>'  /></td>
      
      <td>ESI No. &amp; Date</td>
      <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td ><input type="text" id="esi_no"   name="esi_no" value='<?php if(isset($row['esi_no'])) echo $row['esi_no']; ?>' /></td>
          
        </tr>
      </table></td>
    </tr>
    <tr>
      <td>Excise No.&amp;Date</td>
      <td><input type="text" id="excies_no"   name="excies_no" value='<?php if(isset($row['excies_no'])) echo $row['excies_no']; ?>'  /></td>
      
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>Reference</td>
      <td colspan="2"><input type="text" id="reference"   name="reference" size="60" value='<?php if(isset($row['reference'])) echo $row['reference']; ?>'  /></td>
      <td>&nbsp;</td>
     
    </tr>
    <tr>
      <td>Company Req. Date </td>
      <td ><input type="text" id="company_req_date" size="20"   name="company_req_date" value='<?php if(isset($row['company_req_date'])) {$date = explode('-',$row['company_req_date']); echo $date[2]."/".$date[1]."/".$date[0]; } else { echo date("d/m/Y"); } ?>' readonly="readonly" class="datepicker" /></td>
	  
      <td> Termination Date</td>
      <td><input type="text" id="termination_date"   name="termination_date" value='<?php if(isset($row['tin_date'])){ $date = explode('-',$row['termination_date']); echo $date[2]."/".$date[1]."/".$date[0]; }else  { echo date("d/m/Y"); } ?>' readonly="readonly" class="datepicker" /></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td colspan="2">&nbsp;</td>
      <td>&nbsp;</td>
       
    </tr>
    
    <tr>
      <td><strong>Remarks: </strong></td>
      <td colspan="3"><input type="text" id="remarks" name="remarks" size="100"  value="<?php  if(isset($row['remarks'])) { echo $row['remarks']; }?>" /></td>
    </tr>
  </table>
  </td>
  </tr>
  <tr><div align="center"><td style="text-align:center; border-bottom:1px solid #cccccc; padding:5px 0px 5px 0px; border-top:1px solid #cccccc;" colspan="5" ><span style="text-align:center;">
  <?php if(isset($_GET['action'])=='editRec') { ?>
  <input type="submit" value="Update" name="update" />
  <?php }else{ ?>
  <input type="submit" value="Save" name="submit" />
  <?php } ?>
   <input name="reset" type="reset" value="Reset" />
</span></td></div>
  </tr> 
	  
	  </table>
	  
	  
	  
	  </td>
    </tr>
		</table>
  </td></tr></table><div class="centre"></table></form>