<?php  $table='dbl_dealer_charge_master';
$action=$_REQUEST['action'];
$daction=$_REQUEST['daction'];
$msgErr='';
$status=true;
 $modid=$_REQUEST['modid'];
$frmid=$_REQUEST['frmid'];
$url="&modid=".$modid."&frmid=".$frmid;
$targetpage="index.php?route=DealerDistrbutions/Setup/charge_master".$url;



 if(!$_REQUEST['Save']  && $action=='Edit')
{
	    $condition="id='".$_REQUEST['id']."'";
	    $allready=$s->GetRecord($table,  $condition);
	    $Select=DB_query($allready,$db);
	    $RData=DB_fetch_array($Select);		
  	    $company=$RData['company'];
	    $Site=$RData['site'];
	    $chargecode=$RData['charge_code'];
	    $charge_name=$RData['charge_name'];
	    $chargedescription=$RData['charge_desc'];
		$charge_rate=$RData['taxrate'];
	    //$ledger_id=$RDate['ledger_id'];
	    $status=$RData['status'];
        $id=$RData['id'];
			 
}
  
//************************Edit*********************************************** 


if( $action=='Delete')
{
	  $sql="delete from  dbl_dealer_charge_master where id='".$_REQUEST['id']."'";
	  $Res=DB_query($sql,$db);
	 // $sql1="delete from  accountledger where taxid='".$_REQUEST['id']."' and ledgertype='chargetax'";
	 // $Res2=DB_query($sql1,$db);
	  prnMsg(_('Deleted Successfully'));
	  echo "<script>location.href='index.php?route=DealerDistrbutions/Setup/charge_master&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
}
 

if($_REQUEST['Save']  && $action=='Edit')
{
    $data['charge_code']=$_REQUEST['chargecode'];
    $data['charge_name']=$_REQUEST['chargename'];
    $data['charge_desc']=$_REQUEST['chargedescription'];
    $data['site']=$_REQUEST['Site'];
    //$data['ledger_id']=$_REQUEST['ledger_id'];
    $data['taxrate']=$_REQUEST['charge_rate'];
    $data['company']=$_REQUEST['company'];
    $cond="id='".$_REQUEST['id']."'";
 	$update= $s->EditRecord($table, $data, $cond);
	$row=DB_query($update,$db);
	//$check_ledger="Select * from accountledger where taxid='".$_REQUEST['id']."' and ledgertype='chargetax'";
	//$check_ledger_res=DB_query($check_ledger,$db);
	//$check_ledger_row=DB_fetch_array($check_ledger_res);
	//if(DB_num_rows($check_ledger_res)=='0')
//	{
//		$sql="Insert into accountledger set
//							taxid='".$_REQUEST['id']."',
//							CompanyId='".$_SESSION['company']."',
//							AccountName='".$_REQUEST['chargecode']."',
//							UnderGroup='".$_REQUEST['ledger_id']."',
//							ledgertype='chargetax'";
//							
//		$result=DB_query($sql,$db);
//	}
//	else
//	{
//		$sql="Update accountledger set
//							AccountName='".$_REQUEST['chargecode']."',
//							UnderGroup='".$_REQUEST['ledger_id']."'
//							where taxid='".$_REQUEST['id']."' and ledgertype='chargetax'";
//							
//		$result=DB_query($sql,$db);
//	}
	if(!$row)
	{
	   die('Error'.DB_error());
	} 	
	
  $s->pageLocation($targetpage.'&action=Addnew&daction=Updated');
}
 
//************************INSERT*********************************************** 
if($_REQUEST['Save'] && $action!='Edit')
{	
    $data['company']=$_REQUEST['CompanyId'];
    $data['charge_code']=$_REQUEST['chargecode'];
    $data['charge_name']=$_REQUEST['chargename'];
    $data['charge_desc']=$_REQUEST['chargedescription'];
    $data['created_date']=date('Y-m-d');
    $data['created_by']=$_SESSION['UserID'];
    $data['site']=$_REQUEST['Site'];
   // $data['ledger_id']=$_REQUEST['ledger_id'];
    $data['taxrate']=$_REQUEST['charge_rate'];
    $data['company']=$_REQUEST['company'];
	$insert= $s->insertRecord($table, $data);
	$row=DB_query($insert,$db);
	$sale_master_id=DB_Last_Insert_ID($db,$table,'id');
	if(!$row)
	{
	   die('Error'.DB_error());
	   
	}
	
	     //$insert_acc_ledger="Insert into accountledger set
//							 taxid='".$sale_master_id."',
//							 CompanyId='".$_REQUEST['company']."',
//							 AccountName='".$_REQUEST['chargecode']."',
//							 UnderGroup='".$_REQUEST['ledger_id']."',
//							 ledgertype='chargetax'";
//							
//              $result2=DB_query($insert_acc_ledger,$db);


	
	
	
	
	//prnMsg(_('Record inserted'),'Success');
	 $s->pageLocation($targetpage.'&action=Addnew&daction=Inserted');
 	}
	
?>


<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1>View Charge Master</h1>

 <form action="" method="post">

<table width="69%" border="1" cellspacing="3" cellpadding="3">
      
  <tr class="row1">
    
	<td width="16%" class="tabledata1"  colspan="2" align="center">Company Name<font color="#FF0000"><strong>*</strong></font></td>
   <td colspan="2"><select name="company" id="company"  onchange="getSite(this.value)" style="width:100%;">
    	<!--where CompanyId='".$_SESSION['ACGCompanyId']."'-->
    	
			<?php
			$CompanyName=DB_query("Select * from companies  ",$db);
			while($CompanyRow=DB_fetch_array($CompanyName))
			{ ?>
			<option value="<?php echo $CompanyRow['CompanyId']?>" <?php echo($company==$CompanyRow['CompanyId']?'selected':'');?>><?php echo $CompanyRow['CompanyName']?></option>	
				
		<?php	}
			
			   
			?>
		</select>
   </td>
    <!--<td width="18%" class="tabledata1">Unit</td>
    <td width="39%">
    	<select name="Site" id="SiteName"  style="width:100%">
    	<option value="">--Select--</option>
		<option value="0">All</option>
			<?php
			//$SiteName=DB_query("Select * from locations ",$db);
			//while($SiteRow=DB_fetch_array($SiteName))
			//{ ?>
			<option value="<?php //echo $SiteRow['loccode']?>"  <?php //echo($Site==$SiteRow['loccode']?'selected':'');?>  ><?php //echo $SiteRow['locationname']?></option>	
				
		<?php	//}
			
			   
			?>
		</select></td>-->
	  </tr>	  
	   
  <tr class="row1">
    
	<td width="16%" class="tabledata1">Charge Code<font color="#FF0000"><strong>*</strong></font></td>
    <td width="27%"><input type="text" name="chargecode" id="chargecode" style="width:100%" value="<?php if(isset($chargecode)) echo $chargecode; ?>" />  </td>
    <td width="18%" class="tabledata1">Charge Name<font color="#FF0000"><strong>*</strong></font></td>
    <td width="39%"><input type="text" name="chargename" id="chargename" style="width:100%"  value="<?php if(isset($charge_name)) echo $charge_name; ?>"/>  </td>
  </tr>
  <tr class="row1">
    <td class="tabledata1">Charge Description</td>
    <td><input type="text" name="chargedescription" id="chargedescription" style="width:100%"  value="<?php if(isset($chargedescription)) echo $chargedescription; ?>" /></td>
   <td class="tabledata1">Charge Rate<font color="#FF0000"><strong>*</strong></font></td>
    <td width="32%"><input type="text" name="charge_rate" id="charge_rate" style="width:100%;" value="<?php if(isset($charge_rate)) echo $charge_rate; ?>" >    </td>
  </tr> 
  <!--<tr class="row1">
   <td class="tabledata1">Tax Group </td>
    <td><?php
  	//$result_ledger = DB_query("SELECT *  FROM accountgroupmaster  ",$db);
  			?>
      <select name="ledger_id" style="width:100%">
        <?php 			
 			//while($myrow_ledger = DB_fetch_row($result_ledger))
			//{
			//  $p='';
			//  if($myrow_ledger[0]==$ledger_id)
			//  $p="selected";
			?>
        <option value="<?php //echo $myrow_ledger[0]; ?>"<?php //echo $p; ?>><?php //echo $myrow_ledger[1]; ?></option>
        <?php //}?>
      </select></td>
	  <td></td><td></td>
	  
	  </tr>-->
	
   
  <tr class="row1">
    <td colspan="4" align="center"> 
	
      <input type="submit" name="Save" value="Save" onclick="return validate();" class="search" >    </td>
    </tr>
</table>
</form>
</div></div>
<script language="javascript">
function getSite(str)
{
	
if(str.length==0)
{
	document.getElementById("SiteName").innerHTML="";
	return;
	
}
if(window.XMLHttpRequest)
{
	xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp	=new ActiveXobject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
	if(xmlhttp.readyState==4 && xmlhttp.status==200)
	{
	  	
		document.getElementById("SiteName").innerHTML=xmlhttp.responseText;
		
	}
	
}
xmlhttp.open("GET","getSite1.php?Company="+str,true);
xmlhttp.send();	
	
	
} 


function validate()
{

	if(document.getElementById("company").value=='0')
	{
		alert("Please Select The Company First");
		document.getElementById("company").focus();
		return false;
	}
	if(document.getElementById("chargecode").value=='')
	{
		alert("Please Fill The Change Code First");
		document.getElementById("chargecode").focus();
		return false;
	}
	if(document.getElementById("chargename").value=='')
	{
		alert("Please Fill The Change Name First");
		document.getElementById("chargename").focus();
		return false;
	}
	if(document.getElementById("charge_rate").value=='')
	{
		alert("Please Fill The Change Rate  First");
		document.getElementById("charge_rate").focus();
		return false;
	}
}

</script>