<?php 
//$PageSecurity = 10;
//include('includes/session.inc');
//$title = _('Excise Tax Format');
//include('includes/header.inc');
//include('includes/SQL_CommonFunctions.inc');
//include('includes/function_lib.php');
$modid=$_REQUEST['modid'];
$frmid=$_REQUEST['frmid'];
$rurl="&modid=".$modid."&frmid=".$frmid;
$targetpage="index.php?route=Account/custom_duty".$rurl;
error_reporting(0);
?>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div"> 
<!--<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td><div align="right"><A HREF="javascript:history.go(-1)"> [Go Back]</A> </div></td>
  </tr>
</table>-->
<h1><strong>Custom Duty Charges</strong></h1>
<?php 
if($_REQUEST['action'] == 'del' && $_REQUEST['id'] != '')
{
	$delet_sql = "Delete from custom_duty WHERE id='".$_REQUEST['id']."'";
	$delete_result = DB_query($delet_sql,$db);
	$delete_ledger=DB_query("Delete from accountledger where taxid=".$_REQUEST['id']." and ledgertype='excisetax'",$db);
	$s->pageLocation($targetpage.'&daction=Deleted');
}
if($_REQUEST['action']=='edit' &&  $_REQUEST['add_to_list'])
{
	$data['caption'] =$_REQUEST['caption']; 
    $data['def_rate']  =$_REQUEST['def_rate'];
	$data['form_type']  =$_REQUEST['form_type'];
	$data['ledgertype']  =$_REQUEST['ledgertype'];
	$data['type']  =$_REQUEST['types'];
	$cond="id='".$_REQUEST['id']."'";
	$s->RecordEdit('custom_duty', $data, $cond) ;
	
	$check_ledger="Select * from accountledger where taxid='".$_REQUEST['id']."' and ledgertype='".$_REQUEST['ledgertypename']."'"; 
	$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']."',
							AccountName='".$_REQUEST['caption']."',
							UnderGroup='".$_REQUEST['acc_group']."',
							openingBalance='".$_REQUEST['openingBalance']."',
							openingtype='".$_REQUEST['openingtype']."',
							openingdate='".FormatDateForSQL($_REQUEST['openingdate'])."',
							ledgertype='".$_REQUEST['ledgertype']."'";
		$result=DB_query($sql,$db);
	}
	else
	{
		$sql="Update accountledger set
							AccountName='".$_REQUEST['caption']."',
							UnderGroup='".$_REQUEST['acc_group']."',
							openingBalance='".$_REQUEST['openingBalance']."',
							openingtype='".$_REQUEST['openingtype']."',
							openingdate='".FormatDateForSQL($_REQUEST['openingdate'])."',
							ledgertype='".$_REQUEST['ledgertype']."'
							where taxid='".$_REQUEST['id']."' and ledgertype='".$_REQUEST['ledgertypename']."'";
		$result=DB_query($sql,$db);
	}
 	$s->pageLocation($targetpage.'&daction=Update');
}
else if($_REQUEST['action']=='edit')
{
$sql=DB_query("select * from custom_duty where id='".$_REQUEST['id']."'",$db);;
$row=DB_fetch_array( $sql);
$caption =$row['caption']; 
$def_rate  =$row['def_rate']; 
$form_type  =$row['form_type'];
$ledgertype=$row['ledgertype'];
$type=$row['type'];
	
	$ledgerSQL=DB_query("Select openingBalance, openingdate, openingtype from accountledger where taxid='".$_REQUEST['id']."' and ledgertype='".$ledgertype."'",$db);
	if(DB_num_rows($ledgerSQL)>0)
	{
		$ledgerRow=DB_fetch_array($ledgerSQL);
		extract($ledgerRow);
	}
}
if($_REQUEST['add_to_list'] && $_REQUEST['action']!='edit' )
{

$status=true;
$msgerr='';
if(!isset($_POST['caption'])){
$msgerr[]='Please Enter Caption';
$status=false;
}
if(!isset($_POST['def_rate'])){
$msgerr[]='Please Enter Def_rate';
$status=false;
}

if($status)
{
	$caption_arr=array();
	$caption_arr=explode(" ",$_REQUEST['caption']);
	$final_caption=implode("_",$caption_arr);
                                    
	$sql = "INSERT INTO custom_duty set
							caption='".$_REQUEST['caption']."',                   
							def_rate='".$_REQUEST['def_rate']."',  
							label ='".$final_caption."',                    
							form_type='".$_REQUEST['form_type']."',
							ledgertype='".$_REQUEST['ledgertype']."',
							type='".$_REQUEST['types']."'";
	
	$result = DB_query($sql,$db); 
	$excise_id=DB_Last_Insert_ID($db,'custom_duty','id');
	
	$insert_acc_ledger="Insert into accountledger set
							taxid='".$excise_id."',
							AccountName='".$_REQUEST['caption']."',
							UnderGroup='".$_REQUEST['acc_group']."',
							openingBalance='".$_REQUEST['openingBalance']."',
							openingtype='".$_REQUEST['openingtype']."',
							openingdate='".FormatDateForSQL($_REQUEST['openingdate'])."',
							ledgertype='".$_REQUEST['ledgertype']."'";
							
	$result2=DB_query($insert_acc_ledger,$db);
	$s->pageLocation($targetpage);
		
}		
}
?>

<form action="" method="post">
<p align="center">
<?php
$ch1='';
$ch2='';
if($form_type=='Purchase'){$ch1='checked';}
elseif($form_type=='Sales'){$ch2='checked';}
?>
<?php if(isset($_REQUEST['daction'])) {?>
<?php
 echo "<div class='success'>Record ".$_REQUEST['daction'] ."Successfully</div>" ;
 ?>
<?php } ?>
</p>
	<table width="100%" cellspacing="0" cellpadding="3" border="0">
      <tr class="row1">
		<td width="13%" align="right"><strong>Duty ledger Name</strong></td>
        <td width="20%"><input type="text" name="caption" id="caption" value="<?php echo $caption;?>" style="width: 98%"></td>      
        <td width="13%" align="right"><strong>Under</strong></td>
        <td width="20%"><select name="acc_group" id="acc_group" style="width:98%;">
		<?php 
		$accgroup_sql = "select * from accountgroupmaster where GroupName like '%Duty And Taxes%'";
		$accgroup_result = DB_query($accgroup_sql,$db);
		while($accgroup_row = DB_fetch_array($accgroup_result))
		{ 	$p='';
			if($accgroup_row['AccountsID'] ==  $ledger)
			$p="selected";
		?>
		<option value="<?php echo $accgroup_row['GroupId'];?>" <?php echo $p;?>><?php echo $accgroup_row['GroupName'];?></option>
		<?php } ?>
		</select></td>      
        <td width="13%" align="right"><strong>Rate %</strong></td>
        <td><input type="text" name="def_rate" id="def_rate" style="width:98%;" value="<?php echo $def_rate;?>" /></td>
      </tr>	  
	  <tr class="row1">
	    <td align="right"><strong>Opening Balance</strong></td>
	    <td><input type="text" name="openingBalance" style="width:98%" value="<?php echo $openingBalance;?>"/></td>
		<td align="right"><strong>Opening Type</strong></td>
		<td><input name="openingtype" type="radio" size="27" value="DR" <?php  echo ($openingtype=='DR'?'checked':'')?>  >DR
		<input name="openingtype" type="radio" size="27" value="CR" <?php  echo ($openingtype=='CR'?'checked':'')?>  >CR</td>
		<td align="right"><strong>Opening Date</strong></td>
		<td><input type="text" name="openingdate" style="width:98%" class="datepicker" value="<?php echo ConvertSQLDate($openingdate);?>"/></td>
      </tr>
	  <tr class="row1">
	    <td align="right"><strong>Type</strong></td>
		<td><select name="types" id="types" style="width:98%;">
		<option value="">--Select--</option>
			<option value="Custom" <?php if($type=='Custom') { ?> selected="selected"<?php } ?>>Custom</option>
			<option value="CVD" <?php if($type=='CVD') { ?> selected="selected"<?php } ?>>CVD</option>
			<option value="ECESS" <?php if($type=='ECESS') { ?> selected="selected"<?php } ?>>ECESS</option>
			<option value="SHE-CESS" <?php if($type=='SHE-CESS') { ?> selected="selected"<?php } ?>>SHE-CESS</option>
			<option value="ADC" <?php if($type=='ADC') { ?> selected="selected"<?php } ?>>ADC</option>
			</select></td>
		<td colspan="4"></td>
	  </tr>  
	  <tr class="row1">
	    <td colspan="6" align="center"><input type="submit" name="add_to_list" value="Save" onclick="return check_formtype()" class="search" />  <input type="submit" name="cancel" value="Cancel" class="search" /></td>
	  </tr>
    </table>
<br>

  <table width="100%" border="1" cellpadding="3" cellspacing="0" style="border: solid 1px #808080; border-collapse:collapse;">
    <tr>
      <th><strong>Tax Name</strong></th>
	  <th width="10%"><strong>Rate %</strong></th>	 
	  <th width="10%" style="text-align:center;"><strong>Edit</strong></th>
	  <th width="10%" style="text-align:center;"><strong>Delete</strong></th>
    </tr>
    <?php 
	 	$get_sql = "Select * from custom_duty";
		$get_result = DB_query($get_sql, $db);
		
	while($get_row = DB_fetch_array($get_result))
	{ 
		//$check1=DB_query("Select * from venderquotations where Excise_Duty=".$get_row['id']."",$db);
		//$check2=DB_query("Select * from vendorquotationdetails where Excise_Duty=".$get_row['id']."",$db);
		$ledger_id=$s->GetFieldValue('accountledger',"taxid=".$get_row['id']." and ledgertype='".$get_row['ledgertype']."'",'AccountsID');
		if($ledger_id!=''){$check3=DB_query("Select * from account_book_details where ledgerid=".$ledger_id."",$db);}
		$check4=DB_query("Select * from purchase_bill_details where Excise_Duty=".$get_row['id']."",$db);
		//$num1=DB_num_rows($check1);
		//$num2=DB_num_rows($check2);
		if($ledger_id!='')
		$num3=DB_num_rows($check3);
		$num4=DB_num_rows($check4);
	?>
	<tr bgcolor="#FFFFFF">
      <td><?php echo $get_row['caption'];;?></td>
	  <td><?php echo $get_row['def_rate']; ?></td> 
	  <td style="text-align:center;"><a href="<?php  echo $targetpage;?>&action=edit&id=<?php echo $get_row['id'] ; ?>"><img src="images/icon-edit.png" width="13" height="13" title="Edit Document" /></a></td>
	  <td style="text-align:center;"><?php if($num1>0 || $num2>0 || $num3>0 || $num4>0){ ?><img src="images/Icon_Delete.png" width="13" height="13" title="No permission to Delete Document" style="opacity:0.6;" /><?php } else { ?><a href="<?php  echo $targetpage;?>&action=del&id=<?php echo $get_row['id'] ; ?>" onclick="return confirm('This record will be deleted')"><img src="images/Icon_Delete.png" width="13" height="13" title="Delete Document" /></a><?php } ?></td>	  
    </tr>
	<?php } ?>
  </table>
</form>
</div>
</div>