<?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/excise_tax_format".$rurl;
$targetpage1="index.php?route=Account/tax-master".$rurl;

?>
<script type="text/javascript">
function searchdata(str)
{
  if(document.getElementById("chk_"+str).checked)
  {

  	document.getElementById("openingBalance_"+str).removeAttribute("readonly");

  	document.getElementById("openingdate_"+str).removeAttribute("readonly");
  }
  else  
  {
  	document.getElementById("openingBalance_"+str).setAttribute("readonly", "true");
  	document.getElementById("openingdate_"+str).setAttribute("readonly", "true");
  }


}


</script>
<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 class="row1">
    <td><div align="right"><A HREF="javascript:history.go(-1)"> [Go Back]</A> </div></td>
  </tr>
</table>
<h3 align="center">Excise Tax Master</h3>
<?php 
if($_REQUEST['action'] == 'del' && $_REQUEST['id'] != '')
{
	$delet_sql = "Delete from excise_tax 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'];
	$cond="id='".$_REQUEST['id']."'";
	$s->RecordEdit('excise_tax', $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']."' ";
		$result=DB_query($sql,$db);
	}*/
            $lastid=$_REQUEST['id'];
	        $data12['AccountName']=$_REQUEST['caption'];
			$data12['UnderGroup']=$_REQUEST['acc_group'];
			$data12['CompanyId']=$_SESSION['ACGCompanyId'];
			$data12['ledgertype']=$_REQUEST['ledgertype'];
			$data12['taxid']=$lastid;
            $chkledgerExist=DB_query("select * from accountledger where taxid='".$lastid."'   and ledgertype='".$_REQUEST['ledgertype']."'",$db);
			if(DB_num_rows($chkledgerExist)==0)
			{
				$rowinsert= $s->RecordInsert('accountledger', $data12);
				 $Lastreq_id=DB_Last_Insert_ID($db,'accountledger',$ErrMsg);
                  for($i=0;$i<count($_REQUEST['chkdtl']);$i++)
	              {
	              	if(isset($_REQUEST['chkdtl'][$i]))
	              	{
	              	$site=$_REQUEST['chkdtl'][$i];
			              	if($_REQUEST['openingBalance'][$site]!='')
			              	{
			              		//$sitedata[]=$site;
			              	$data_dtls['accountledger_id']=$Lastreq_id;
					        $data_dtls['site']=$site;
					        $data_dtls['openingtype']=$_REQUEST['openingtype'][$site];
					        $data_dtls['openingBalance']=$_REQUEST['openingBalance'][$site];
					        $data_dtls['openingdate']=FormatDateForSQL($_REQUEST['openingdate'][$site]);
					        $s->RecordInsert('accountledger_dtls', $data_dtls);	
					        }
			        }
	              }

			 }
			else
			{
			
				$cond="taxid='".$lastid."' and ledgertype='".$_REQUEST['ledgertype']."'";
				$rowinsert= $s->RecordEdit('accountledger', $data12, $cond);
				$Lastreq_id=$s->GetFieldValue("accountledger","taxid='".$lastid."' and ledgertype='".$_REQUEST['ledgertype']."'","AccountsID");
				 for($i=0;$i<count($_REQUEST['chkdtl']);$i++)
				   {
				   	if(isset($_REQUEST['chkdtl'][$i]))
	              	{
	              		$site=$_REQUEST['chkdtl'][$i];
						        if($_REQUEST['accountledger_dtls_id'][$site]!='' && $_REQUEST['openingBalance'][$site]!='')
						        {
						        	//$sitedata[]=$site;
						        	$data_dtls['accountledger_id']=$Lastreq_id;
							        $data_dtls['site']=$site;
							        $data_dtls['openingtype']=$_REQUEST['openingtype'][$site];
							        $data_dtls['openingBalance']=$_REQUEST['openingBalance'][$site];
							        $data_dtls['openingdate']=FormatDateForSQL($_REQUEST['openingdate'][$site]);
							        $cond1="id='".$_REQUEST['accountledger_dtls_id'][$site]."'";
							        $s->RecordEdit('accountledger_dtls', $data_dtls,$cond1);	
						        }
						        else if($_REQUEST['openingBalance'][$site]!='' && $_REQUEST['accountledger_dtls_id'][$site]=='')
						        {
							   	$data_dtls['accountledger_id']=$Lastreq_id;
						        $data_dtls['site']=$site;
						        $data_dtls['openingtype']=$_REQUEST['openingtype'][$site];
						        $data_dtls['openingBalance']=$_REQUEST['openingBalance'][$site];
						        $data_dtls['openingdate']=FormatDateForSQL($_REQUEST['openingdate'][$site]);
						        $s->RecordInsert('accountledger_dtls', $data_dtls);	 
						        }
				   }
				}

			}

 	$s->pageLocation($targetpage1.'&daction=Update');
}
else if($_REQUEST['action']=='edit')
{
$sql=DB_query("select * from excise_tax 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'];
	
	$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);
	}
	$account_data=DB_fetch_assoc(DB_query("select * from accountledger 
		where ledgertype='".$ledgertype."' and taxid='".$_REQUEST['id']."' ",$db));

	//echo "select * from accountledger_dtls where accountledger_id='".$account_data['AccountsID']."'";
$sql_get=DB_query("select * from accountledger_dtls where accountledger_id='".$account_data['AccountsID']."'",$db);
while($row1=DB_fetch_array($sql_get))
{
 $data_row[$row1['site']][site]=$row1['site'];
 $data_row[$row1['site']][id]=$row1['id'];
 $data_row[$row1['site']][openingtype]=$row1['openingtype'];
 $data_row[$row1['site']][opening_bal]=$row1['openingBalance'];
 $data_row[$row1['site']][opening_dat]=$row1['openingdate'];
}
}
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 excise_tax set
							caption='".$_REQUEST['caption']."',                   
							def_rate='".$_REQUEST['def_rate']."',  
							label ='".$final_caption."',                    
							form_type='".$_REQUEST['form_type']."',
							ledgertype='".$_REQUEST['ledgertype']."'";
	
	$result = DB_query($sql,$db); 
	$excise_id=DB_Last_Insert_ID($db,'excise_tax','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">
<?php
$ch1='';
$ch2='';
if($form_type=='Purchase'){$ch1='checked';}
elseif($form_type=='Sales'){$ch2='checked';}
?>
<input name="form_type" type="radio" value="Sales" id="form_type1" <?php echo $ch2;?>/>
Sales
<input name="form_type" type="radio" value="Purchase" id="form_type2" <?php echo $ch1;?>/> 
Purchase</p>
<table width="100%" border="0" cellpadding="0" cellspacing="0" style="background-color: #CCC;border: solid 1px #CCC">
<?php if(isset($_REQUEST['daction'])) {?>
<tr>
<td colspan="3"><?php
 echo "<div class='success'>Record ".$_REQUEST['daction'] ."Successfully</div>" ;
 ?></td>
</tr>
<?php } ?>
 <tr class="row1">
    <td>
	<table width="100%" cellspacing="0" cellpadding="0"  border="1">
     <tr class="row1">
        <td class="tabledata">Tax Name</td>
        <td><input type="text" name="caption" id="caption" value="<?php echo $caption;?>" style="width: 260px;"></td>
      
        <td class="tabledata">Under</td>
        <td>
		<?php  
 		$pcode= $MainGroupId;
 		function CategoryShow($pcode, $PcateID, $dashVal)
		{
	    global $db;
		$dashVal = $dashVal . "-";
	    $sqlCate = "select * from accountgroupmaster  where  MainGroupId = '".$PcateID."' and Type!='N'";
 		$rsCate  = DB_query($sqlCate,$db);	
 		if(DB_num_rows($rsCate)>0)
		{
			while($rowCate = DB_fetch_array($rsCate))
			{
				$sqlCateSel = "select * from accountgroupmaster where GroupId  =".$rowCate['GroupId']."  and Type!='N'";
				$rsCateSel	= DB_query($sqlCateSel,$db);
				$px='';
				 if($rowCate['GroupId']==$pcode)
				 $px="selected";
 		?>
 				<option value="<?php echo $rowCate['GroupId'];?>" <?php echo $px ?>><?php echo $dashVal;?>
				 <?php echo $rowCate['GroupName'];?> </option>
	   <?php			
				   CategoryShow($pcode,$rowCate['GroupId'], $dashVal);
			}
		}
	}   
   		?> 
		<select name="acc_group" id="acc_group" style="width: 260px;">
		 <?php 
	$dashVal = "&nbsp;&nbsp;&nbsp;";
	$sqlCate = "select * from  accountgroupmaster where Type!='N' and (GroupName like 'duty%' OR GroupName like 'duties%')"     ;

	$rsCate  = DB_query($sqlCate,$db);
	if(DB_num_rows($rsCate)>0)
	{
		while($rowCate =DB_fetch_array($rsCate))
		{
 				$sqlCateSel = "select * from accountgroupmaster where MainGroupId   =".$rowCate['GroupId']." and Type!='N'";
				$rsCateSel	= DB_query($sqlCateSel,$db);
				$px='';
				 if($rowCate['GroupId']==$pcode)
				 $px="selected";
			echo $dashVal;	
		  ?>
		<option value="<?php echo $rowCate['GroupId'];?>"<?php echo $px ?>> 
		<?php echo  $rowCate['GroupName'] ;?></option>
 		<?php			
			 CategoryShow($pcode, $rowCate['GroupId'], $dashVal);
		}
	}
?>
		</select>		</td>
      
        <td class="tabledata">Rate %</td>
        <td><input type="text" name="def_rate" id="def_rate"   value="<?php echo $def_rate;?>" style="width: 260px;" /></td>
      </tr>
	  
	
	  

			<tr>
			<td class="tabledata">Ledger Type</td>
			<td><select name="ledgertype" id="" style="width: 160px;">
			<option value="excisetax" <?php if($ledgertype=='excisetax')echo "selected";?>>Excise</option>
			<option value="cess" <?php if($ledgertype=='cess')echo "selected";?>>Cess On Duty</option>
			</select><input type="text" name="ledgertypename" value="<?php echo $ledgertype;?>" style="width: 100px;" />
			</td>
			</tr>
			<?php
  $i=0;
   $sql=DB_query("select loccode,locationname from locations where mst_companyid='".$_SESSION['ACGCompanyId']."'",$db);
   while($row1=DB_fetch_array($sql))
   {
   ?>
   <tr class="row1">   
   	<td><input type="checkbox" name="chkdtl[]" id="chk_<?php echo $i; ?>" value="<?php echo $row1['loccode'];?>" onclick="searchdata(<?php echo $i; ?>)"
    <?php if($data_row[$row1['loccode']][site]==$row1['loccode']) { $dis=""; ?> checked <?php } else { $dis="readonly"; }?>	></td>
    <td ><?php echo $row1['locationname']; ?><input name="site[]" type="hidden" size="27"  value="<?php echo $row1['loccode'];?>">
     <input type="hidden" name="accountledger_dtls_id[<?php echo $row1['loccode'];?>]" value="<?php echo  $data_row[$row1['loccode']][id]; ?>">
    </td>
    <td style="text-align:center"><input name="openingBalance[<?php echo $row1['loccode'];?>]" id="openingBalance_<?php echo $i; ?>" type="text" size="27"  value="<?php echo  $data_row[$row1['loccode']][opening_bal]; ?>" <?php echo $dis; ?>   ></td> 
    <td style="text-align:center"><input name="openingdate[<?php echo $row1['loccode'];?>]" id="openingdate_<?php echo $i; ?>" type="text" size="27"  
    	value="<?php if($data_row[$row1['loccode']][opening_dat]) echo  ConvertSQLDate($data_row[$row1['loccode']][opening_dat]); else echo ConvertSQLDate($_SESSION['financialYrStart']); ?>" class='datepicker' <?php echo $dis; ?>></td> 
    <td style="text-align:center"><input name="openingtype[<?php echo $row1['loccode'];?>]" id="opp_<?php echo $i; ?>" type="radio" size="27"  value="DR" 
    	<?php  echo ($data_row[$row1['loccode']][openingtype]=='DR'?'checked':'')?>  >DR	
	<input name="openingtype[<?php echo $row1['loccode'];?>]" type="radio" size="27" id="opp_<?php echo $i; ?>" value="CR"
	 <?php  echo ($data_row[$row1['loccode']][openingtype]=='CR'?'checked':'')?>  >CR </td>  
	
  </tr>
  <?php $i++; } ?>
		 </table></td>
  </tr>

</table> 
<p align="center"><?php if($_REQUEST['action'] == 'edit') {?>
<input type="submit" name="add_to_list" value="Save" onclick="return check_formtype()" <?php if($_SESSION['frmEdit']=='0'){?>disabled<?php } ?>/><?php } else {?>
<input type="submit" name="add_to_list" value="Save" onclick="return check_formtype()" <?php if($_SESSION['frmcreate']=='0'){?>disabled<?php } ?>/><?php } ?>
     <a href="index.php?route=Account/tax-master&modid=<?php echo $_REQUEST['modid']; ?>&frmid=<?php echo $_REQUEST['frmid']; ?>"> <input type="button" name="cancel" value="Cancel" /></a></p>

 <!-- <table width="100%" height="58" border="0" cellpadding="2" cellspacing="1" style="border: solid 1px #CCC">
    <tr class="row1">
      <td class="tabledata">Tax Name</td>
	  <td class="tabledata">Rate %</td>
	  <td class="tabledata">Sale/Purchase</td>
	  <td class="tabledata">Ledger Type</td>
	  <?php if($_SESSION['frmEdit']=='1'){?><td class="tabledata">Edit</td><?php } ?>
	  <?php if($_SESSION['frmDelete']=='1'){?><td class="tabledata">Delete</td><?php } ?>
    </tr>
    <?php 	
		$get_sql = "Select * from excise_tax";
		$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><?php echo $get_row['form_type']; ?></td>
	   <td><?php echo $get_row['ledgertype']; ?></td>
	  <?php if($_SESSION['frmEdit']=='1'){?><td><a href="<?php  echo $targetpage;?>&action=edit&id=<?php echo $get_row['id'] ; ?>"><img src="images/icon-edit.png" width="10" height="10" /></a></td><?php } ?>
	 <?php if($_SESSION['frmDelete']=='1'){?><td><?php if($num3>0 || $num4>0){echo "Not allowed";}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="10" height="10"/></a><?php } ?></td><?php } ?>
	  
    </tr>
	<?php } ?>
  </table>--->
</form>
</div>
</div>
