<?php    
$table='service_tax_master';
$action=$_REQUEST['action'];
$daction=$_REQUEST['daction'];
$msgErr='';
$status=true;

$modid=$_REQUEST['modid'];
$frmid=$_REQUEST['frmid'];
$rurl="&modid=".$modid."&frmid=".$frmid;
$targetpage="index.php?route=Account/ServiceTax".$rurl;
//************************Delete*********************************************** 
if( $action=='del')
{
	$condition="id='".$_REQUEST['id']."'";
	$s->DeleteRecord($table,$condition);
	$accountid=$s->GetFieldValue("accountledger","taxid=".$_REQUEST['id']." 
		and (ledgertype='servicetax' || ledgertype='scess')","AccountsID");
	$delete_ledger=DB_query("Delete from accountledger where taxid=".$_REQUEST['id']." 
		and (ledgertype='servicetax' || ledgertype='scess')",$db);
	$delete_ledger1=DB_query("Delete from accountledger_dtls where accountledger_id='".$accountid."'",$db);
	 $s->pageLocation($targetpage.'&daction=Deleted'.$rurl);

}
//************************Edit*********************************************** 

if(!$_REQUEST['Save'] &&  $status && $action=='Edit')
{
	$condition="id='".$_REQUEST['id']."'";
	$allready=$s->GetRecord($table,  $condition);
	$Select=DB_query($allready,$db);
	$RData=DB_fetch_array($Select);		
	$tax_code=$RData['tax_code'];
	$taxrate =$RData['taxrate'];
	$under_group =$RData['under_group'];
	$tax_type=$RData['tax_type'];
	$type=$RData['type'];
		
 	/*$ledgersq1=DB_query("Select openingBalance, openingdate, openingtype from accountledger 
	where taxid='".$_REQUEST['id']."' and (ledgertype='servicetax' || ledgertype='scess')",$db);
	$ledgerSQL=DB_fetch_array($ledgersq1);
	$numrow1=DB_num_rows($ledgersq1);
	if($numrow1>0)
	extract($ledgerSQL);*/
      
      
		$account_data=DB_fetch_assoc(DB_query("select * from accountledger 
				where (ledgertype='servicetax' || ledgertype='scess') and taxid='".$_GET['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['Save'] &&  $status && $action=='Edit')
{
  $data['tax_code']=$_REQUEST['tax_code'];
  $data['taxrate']=$_REQUEST['taxrate'];
  $data['tax_type']=$_REQUEST['tax_type'];
  $data['under_group']=$_REQUEST['under_group'];
  $data['type']=$_REQUEST['type'];
  $cond="id='".$_REQUEST['id']."'";

  $update= $s->RecordEdit($table, $data, $cond);
 
  // $check_ledger="Select * from accountledger where taxid='".$_REQUEST['id']."' && (ledgertype='servicetax' || ledgertype='scess') ";

 // $check_ledger_res=DB_query($check_ledger,$db);
  //$check_ledger_row=DB_fetch_array($check_ledger_res);
 	
	if($_REQUEST['type']=='ST')
	 $tp='servicetax';
	 else
	 $tp='scess';
	//echo $_REQUEST['type'].$tp;

           $lastid=$_REQUEST['id'];

  	        $data12['AccountName']=$_REQUEST['tax_code'];
			$data12['UnderGroup']=$_REQUEST['under_group'];
			$data12['CompanyId']=$_SESSION['ACGCompanyId'];
			$data12['ledgertype']=$tp;
			$data12['taxid']=$lastid;
            $chkledgerExist=DB_query("select * from accountledger where taxid='".$lastid."' and  ledgertype='".$tp."' ",$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='".$tp."'";
				$rowinsert= $s->RecordEdit('accountledger', $data12, $cond);
				$Lastreq_id=$s->GetFieldValue("accountledger","taxid='".$lastid."' and ledgertype='".$tp."'","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);	 
						        }
				   }
				}

			}

  /*if(DB_num_rows($check_ledger_res)=='0')
	{
	 
		$sql="Insert into accountledger set
							taxid='".$_REQUEST['id']."',
							AccountName='".$_REQUEST['tax_code']."',
							UnderGroup='".$_REQUEST['under_group']."',
							openingBalance='".$_REQUEST['openingBalance']."',
							openingtype='".$_REQUEST['openingtype']."',
							openingdate='".FormatDateForSQL($_REQUEST['openingdate'])."',
							ledgertype='".$tp."'";
							
		$result=DB_query($sql,$db);
	}
	else
	{
		
		 $sql="Update accountledger set
							AccountName='".$_REQUEST['tax_code']."',
							UnderGroup='".$_REQUEST['under_group']."',
							openingBalance='".$_REQUEST['openingBalance']."',
							openingtype='".$_REQUEST['openingtype']."',
							openingdate='".FormatDateForSQL($_REQUEST['openingdate'])."'
							where taxid='".$_REQUEST['id']."' and ledgertype='".$tp."'";
							
		$result=DB_query($sql,$db);
		
	}*/
	
 	if(!$update)
	{
	   die('Error'.DB_error());
	}  
  $s->pageLocation($targetpage.'&daction=Updated'.$rurl);
 
}
 
//************************INSERT*********************************************** 
if($_REQUEST['Save'] &&  $status && $action=='')
{
  $data['tax_code']=$_REQUEST['tax_code'];
 $data['taxrate']=$_REQUEST['taxrate'];
 $data['under_group']=$_REQUEST['under_group'];
  $data['tax_type']=$_REQUEST['tax_type'];
   $data['type']=$_REQUEST['type'];
   $insert= $s->RecordInsert($table, $data);
 	
	$service_tax_id=DB_Last_Insert_ID($db,'service_tax_master',$ErrMsg);

	/*$insert_acc_ledger="Insert into accountledger set
							taxid='".$service_tax_id."',
							AccountName='".$_REQUEST['tax_code']."',
							UnderGroup='".$_REQUEST['under_group']."',
							openingBalance='".$_REQUEST['openingBalance']."',
							openingtype='".$_REQUEST['openingtype']."',
							openingdate='".FormatDateForSQL($_REQUEST['openingdate'])."',
							ledgertype='servicetax'";
							
	$result2=DB_query($insert_acc_ledger,$db);*/

	if($_REQUEST['type']=='ST')
	 $tp='servicetax';
	 else
	 $tp='scess';
	//echo $_REQUEST['type'].$tp;

           $lastid=$service_tax_id;

  	        $data12['AccountName']=$_REQUEST['tax_code'];
			$data12['UnderGroup']=$_REQUEST['under_group'];
			$data12['CompanyId']=$_SESSION['ACGCompanyId'];
			$data12['ledgertype']=$tp;
			$data12['taxid']=$lastid;
            $chkledgerExist=DB_query("select * from accountledger where taxid='".$lastid."' and  ledgertype='".$tp."' ",$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='".$tp."'";
				$rowinsert= $s->RecordEdit('accountledger', $data12, $cond);
				$Lastreq_id=$s->GetFieldValue("accountledger","taxid='".$lastid."' and ledgertype='".$tp."'","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);	 
						        }
				   }
				}
			}
	
	if(!$insert)
	{
	   die('Error'.DB_error());
	} 
  $s->pageLocation( $targetpage.'&daction=Inserted');
}
?>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<!--<p align="right" style="float:right;"><A HREF="javascript:history.go(-1)"> [Go Back]</A> </p>-->

<h1>Service Tax  Master</h1>
<form action="" method="post">
<table width="100%" border="0" cellspacing="0" cellpadding="3">
  <?php if(isset($daction))
  {?>
    <tr>  
    <td colspan="6"><div class="success"><?php echo "Record ".$daction." Successfully" ?></div></td>
  </tr>
  <?php }
  if(isset($msgErr)&&$msgErr!='' )
   {?>
    <tr>  
    <td colspan="6" ><div class="error"><?php 
for($kp=0;$kp<count($msgErr);$kp++)
 {
		echo  $msgErr[$kp].'<br>';
 }
  ?></div></td>
  </tr>
  
   <?php }?>   
   
  <tr class="row1">
    <td width="13%" align="right"><strong>Tax Name</strong></td>
    <td width="20%"><input name="tax_code" style="width:98%;" type="text"  value="<?php echo $tax_code;?>"></td>	
    <td width="13%" align="right"><strong>Tax Rate</strong></td>
    <td width="20%"><input type="text" name="taxrate" value="<?php  echo $taxrate;?>" style="width:90%"/> %</td>
    <td width="13%" align="right"><strong>Under Group</strong></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="under_group" style="width:98%;">			
			 <?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>
	</tr>  
   <tr class="row1">
    <td align="right"><strong>Tax Type</strong></td>
    <td><select name="tax_type" id="tax_type"  style="width:98%;">
			<option value="R" <?php if($tax_type=='R')echo "selected";?>>Receivable</option>
			<option value="P" <?php if($tax_type=='P')echo "selected";?>>Payable</option>
	    </select>
	</td>   
    <td align="right"><strong>Tax Type</strong></td>
	<td><select name="type" id="type"  style="width:98%;">
			<option value="ST" <?php if($type=='ST')echo "selected";?>>Service Tax</option>
			<option value="E" <?php if($type=='E')echo "selected";?>>Education Cess</option>
			<option value="S" <?php if($type=='S')echo "selected";?>>She Cess</option>
	    </select></td>
	<td></td>
	<td></td>
  <!-- <td >Opening Balance/Type</td>
    <td><input type="text" name="openingBalance" style="width:200px;" value="<?php echo $openingBalance;?>"/>
      <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>Opening Date </td>
	<td><input type="text" name="openingdate" class="datepicker" value="<?php echo ConvertSQLDate($openingdate);?>"/></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 align="right"><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; ?>  style="width:98%;" /></td> 
    <td><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' style="width:98%;" <?php echo $dis; ?> /></td> 
    <td align="right"><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':''); if($data_row[$row1['loccode']][openingtype]=='') { ?>  checked <?php } ?>  >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>  
	<td></td>
  </tr>
  <?php $i++; } ?>
  <tr class="row1">
  	<td colspan="6" style="text-align:center;">
  		
<?php if($_REQUEST['action'] == 'Edit') {?>
<input type="submit" name="Save" class="search" value="Edit" <?php if($_SESSION['frmEdit']=='0'){?>disabled<?php } ?>/><?php } else {?>
<input type="submit" name="Save" class="search" value="Save" <?php if($_SESSION['frmcreate']=='0'){?>disabled<?php } ?>/>
<?php } ?>
  	</td>
  </tr>
</table>
</form>
 
 
 <table width="100%" border="1" cellspacing="0" cellpadding="3" style="border:solid 1px #808080; border-collapse:collapse;">
  <tr>
      <th width="12%">Tax Code</th>
	  <th>Under</th>
      <th width="12%">Tax Rate</th>
	  <th width="12%">Tax Type</th>
	  <?php if($_SESSION['frmEdit']=='1'){?><th width="6%" style="text-align:center;">Edit</th><?php } ?>
      <?php if($_SESSION['frmDelete']=='1'){?><th width="6%" style="text-align:center;">Delete</th><?php } ?>
  </tr> 
  
   <?php 
   $GetRecord = "Select * from $table";
   $ResultData = DB_query($GetRecord,$db);
   $i=1;
   if(DB_num_rows($ResultData)==0)
   echo "<td colspan='6' style='text-align:center; background:#fff;'>--No Record--</td>";
   
   while($RowData = DB_fetch_array($ResultData))
   {
		//$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=".$RowData['id']." and ledgertype='servicetax'",'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 $RowData['tax_code']; ?></td>
    <td><?php 
	   echo ucfirst($s->getField('accountgroupmaster','GroupId',$RowData['under_group'],'GroupName'));	   
	    ?></td>
	<td><?php echo $RowData['taxrate']; ?> <strong>%</strong> </td>
    <td><?php if($RowData['tax_type']!='') echo ($RowData['tax_type']=='R'?'Receivable':'Payable')?></td>   
    <?php if($_SESSION['frmEdit']=='1'){?>
    <td style="text-align:center;"><a href="<?php echo $targetpage;?>&action=Edit&id=<?php echo $RowData['id']; ?>"><img src="images/icon-edit.png"  width="13" height="13" title="Edit Document" /></a></td>
    <?php } ?>
	<?php if($_SESSION['frmDelete']=='1'){?>
	<td style="text-align:center;"><?php if($num3>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 $RowData['id']; ?>" onclick="return confirm('Do You Want To Delete This Record')"><img src="images/Icon_Delete.png" width="13" height="13" title="Delete Document" /></a><?php } ?></td>
	<?php } ?>
   </tr>
	<?php $i++;}   
   ?> 
</table>
</div>
</div>
<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>