<?php    
include('./includes/AccountGroupMaster_Class.php');
$table='sales_tax_master';
$action=$_REQUEST['action'];
$daction=$_REQUEST['daction'];
$msgErr='';
$status=true;


$vat_account=array("VI"=>'VAT INPUT',"VO"=>'VAT OUTPUT',"CI"=>'CST INPUT',"CO"=>'CST OUTPUT',"GI"=>'GST INPUT',"GO"=>"GST OUTPUT");
  $modid=$_REQUEST['modid'];
$frmid=$_REQUEST['frmid'];
 $rurl="&modid=".$modid."&frmid=".$frmid;
$targetpage="index.php?route=Account/SaleTax".$rurl;

//************************Delete*********************************************** 
if($action=='del')
{
	$condition="salestax_id='".$_REQUEST['salestax_id']."'";
	$s->DeleteRecord($table,$condition);
	$delete_ledger=DB_query("Delete from accountledger where taxid=".$_REQUEST['salestax_id']." and ledgertype='salestax'",$db);
	$delete_ledger1=DB_query("delete from accountledger_dtls where accountledger_id='".$_REQUEST['salestax_id']."'",$db);
	 $s->pageLocation($targetpage);
} 
  
//************************Edit*********************************************** 

if(!$_REQUEST['Save'] &&  $status && $action=='Edit')
{
	$condition="salestax_id='".$_REQUEST['salestax_id']."'";
	$allready=$s->GetRecord($table,  $condition);
	$Select=DB_query($allready,$db);
	$RData=DB_fetch_array($Select);		
   
	//$ledgerSQL=DB_fetch_array(DB_query("Select openingBalance, openingdate, openingtype 
  //  from accountledger where taxid='".$_REQUEST['salestax_id']."' and ledgertype='salestax'",$db));
	// extract($ledgerSQL);	
	$tax_code=$RData['tax_code'];
	$taxrate =$RData['taxrate'];
	$account_group=$RData['account_group'];
	$sales_for =$RData['sales_for'];
	$surcharge_rate =$RData['surcharge_rate'];
	$form_type =$RData['form_type'];
	$ledger_id =$RData['ledger_id'];
	$account_data=DB_fetch_assoc(DB_query("select * from accountledger 
		where ledgertype='salestax' and taxid='".$_GET['salestax_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['sales_for']=$_REQUEST['account_group'];
	$data['account_group']=$_REQUEST['account_group'];
	$data['surcharge_rate']=$_REQUEST['surcharge_rate'];
	$data['form_type']=$_REQUEST['form_type'];
	$data['ledger_id']=$_REQUEST['ledger_id'];
	
    $cond="salestax_id='".$_REQUEST['salestax_id']."'";
 	$update= $s->RecordEdit($table, $data, $cond);

         $lastid=$_REQUEST['salestax_id'];

  	        $data12['AccountName']=$_REQUEST['tax_code'];
			$data12['UnderGroup']=$_REQUEST['ledger_id'];
			$data12['CompanyId']=$_SESSION['ACGCompanyId'];
			$data12['ledgertype']='salestax';
			$data12['taxid']=$lastid;
            $chkledgerExist=DB_query("select * from accountledger where taxid='".$lastid."'   and ledgertype='salestax'",$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='salestax'";
				$rowinsert= $s->RecordEdit('accountledger', $data12, $cond);
				$Lastreq_id=$s->GetFieldValue("accountledger","taxid='".$lastid."' and ledgertype='salestax'","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);	 
						        }
				   }
				}
			}
	
/*	$check_ledger="Select * from accountledger where taxid='".$_REQUEST['salestax_id']."' and ledgertype='salestax'";
	$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['salestax_id']."',
							AccountName='".$_REQUEST['tax_code']."',
							UnderGroup='".$_REQUEST['ledger_id']."',
							openingBalance='".$_REQUEST['openingBalance']."',
							openingtype='".$_REQUEST['openingtype']."',
							openingdate='".FormatDateForSQL($_REQUEST['openingdate'])."',
							ledgertype='salestax'";
							
		$result=DB_query($sql,$db);
	}
	else
	{
		$sql="Update accountledger set
							AccountName='".$_REQUEST['tax_code']."',
							UnderGroup='".$_REQUEST['ledger_id']."',
							openingBalance='".$_REQUEST['openingBalance']."',
							openingtype='".$_REQUEST['openingtype']."',
							openingdate='".FormatDateForSQL($_REQUEST['openingdate'])."'
							where taxid='".$_REQUEST['salestax_id']."' and ledgertype='salestax'";
							
		$result=DB_query($sql,$db);
	}

 	if(!$update)
	{
	   die('Error'.DB_error());
	}  
	*/
  $s->pageLocation($targetpage.'&daction=Updated'.$rurl);
  $s->pageLocation($targetpage);
}
 
//************************INSERT*********************************************** 
if($_REQUEST['Save'] &&  $status && $action=='')
{

$data['tax_code']=$_REQUEST['tax_code'];
$data['taxrate']=$_REQUEST['taxrate'];
$data['sales_for']=$_REQUEST['account_group'];
$data['account_group']=$_REQUEST['account_group'];
$data['surcharge_rate']=$_REQUEST['surcharge_rate'];
$data['form_type']=$_REQUEST['form_type'];
$data['ledger_id']=$_REQUEST['ledger_id'];
$insert= $s->RecordInsert($table, $data);

$sale_master_id=DB_Last_Insert_ID($db,'sales_tax_master','id');

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

$data1['AccountName']=$_REQUEST['tax_code'];
$data1['taxid']=$sale_master_id;
$data1['UnderGroup']=$_REQUEST['ledger_id'];
$data1['CompanyId']=$_SESSION['ACGCompanyId'];
$data1['ledgertype']='salestax';

$sitedata='';
$chkledgerExist=DB_query("select * from accountledger where taxid='".$sale_master_id."'  and ledgertype='salestax'",$db);
if(DB_num_rows($chkledgerExist)==0)
{
 	$rowinsert= $s->RecordInsert('accountledger', $data1);
 	 $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])
	              		{
	              		
		              	$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='".$sale_master_id."'   and ledgertype='salestax'";
 $rowinsert= $s->RecordEdit('accountledger', $data1, $cond);

 $Lastreq_id=$s->GetFieldValue("accountledger","taxid='".$sale_master_id."' and ledgertype='salestax'","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]=='')
						        {
						       // $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);	 
						        }
				   }
				}

}

 	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">
<h1><?php echo $s->GetFieldValue("modules_form","id='".$_REQUEST['frmid']."' and moduleid='".$_REQUEST['modid']."'","frm_name"); ?></h1>
<form action="" method="post">
<table width="100%" border="0" cellspacing="0" cellpadding="3"> 
  <?php if(isset($daction))
  {?>
    <tr class="row1">  
    <td colspan="5" ><div class="success"><?php echo "Record ".$daction." Successfully" ?></div></td>
  </tr>
  <?php }
  if(isset($msgErr)&&$msgErr!='' )
   {?>
    <tr class="row1">  
    <td colspan="5" ><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">Tax Name</td>
    <td width="25%"><input name="tax_code" type="text" value="<?php echo $tax_code;?>"></td>
	<td width="13%" align="right">Tax Rate</td>
	<td width="25%"><input type="text" name="taxrate" style="width:90%" value="<?php  echo $taxrate;?>"/> %</td>
	<td></td>
  </tr>
  <tr class="row1">
    <td align="right">Account Group</td>
    <td><select name="account_group">	
	        <option value="0" >Select</option>
			<option value="VI" <?php if($account_group=='VI') echo "selected";?>>VAT INPUT</option>
	        <option value="VO" <?php if($account_group=='VO') echo "selected";?>>VAT OUTPUT</option>
	        <option value="CI" <?php if($account_group=='CI') echo "selected";?>>CST INPUT</option>
	        <option value="CO" <?php if($account_group=='CO') echo "selected";?>>CST OUTPUT</option>
	        <option value="GI" <?php if($account_group=='GI') echo "selected";?>>GST INPUT</option>
	        <option value="GO" <?php if($account_group=='GO') echo "selected";?>>GST OUTPUT</option>
			<?php 	
	         ?>
        </select></td><!--<A HREF="javascript:history.go(-1)">-->
	<td align="right">Surcharge Rate</td>
	<td><input type="text" name="surcharge_rate" style="width:90%" value="<?php  echo $surcharge_rate;?>"/> %</td>
	<td></td>
  </tr>
  
  <tr class="row1">
    <td align="right">Tax Group</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);
			}
		}
	} 
	
  	//$result_ledger = DB_query("SELECT *  FROM accountgroupmaster  WHERE GroupName like '%Duty And Taxes%'",$db);
  			?>
      <select name="ledger_id">
       <?php 
	$dashVal = "&nbsp;&nbsp;&nbsp;";
	
	$sqlCate = "select * from  accountgroupmaster where MainGroupId = 0 and Type!='N' and NatureOfGroup=2";
	$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></td>
    <td></td>
    <td></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><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' <?php echo $dis; ?> style="width:90%;" /></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':''); 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>  	
  </tr>
  <?php $i++; } ?>
  <!--<tr>
    <th>Opening Balance/Type</th>
    <td><input type="text" name="openingBalance" style="width:60%" 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>
	<th>Opening Date </th>
	<td><input type="text" name="openingdate" style="width:100%" class="datepicker" value="<?php echo ConvertSQLDate($openingdate);?>"/></td>
  </tr>-->  
  <tr class="row1">
  	<td colspan="5" style="text-align:center;">
  		<input type="submit" name="Save" class="search" value="<?php echo ($action=='Edit'?"Edit":"Save") ?>" />  <a href="javascript:history.go(-1)"><input type="button" name="Back" value="Back" class="search" /></a>
  	</td>
  </tr>
</table>
</form> 
<br>
 <table width="100%" border="0" cellspacing="0" cellpadding="3" class="thborder">
    <tr>
       <th width="12%">Tax</th>
	   <th width="20%">Account Group</th>
	   <th>Tax Group</th>
       <th width="12%">Tax Rate</th>
	   <th width="6%" style="text-align:center;">Edit</th>
       <th width="6%" style="text-align:center;">Delete</th>
    </tr> 
  
   <?php 
   $GetRecord = "Select * from $table";
   $ResultData = DB_query($GetRecord,$db);
   $i=1;
   if(DB_num_rows($ResultData)==0)
   echo "<td colspan='3'>--No Record--</td>";
   
   while($RowData = DB_fetch_array($ResultData))
   {   	
		//$check1=DB_query("Select * from venderquotations where taxname=".$RowData['salestax_id']."",$db);
		//$check2=DB_query("Select * from vendorquotationdetails where taxname=".$RowData['salestax_id']."",$db);
		$ledger_id=$s->GetFieldValue('accountledger',"taxid=".$RowData['salestax_id']." and ledgertype='salestax'",'AccountsID');
		if($ledger_id!=''){$check3=DB_query("Select * from account_book_details where ledgerid=".$ledger_id."",$db);}
		$check4=DB_query("Select * from purchase_billbook where vat_cst=".$RowData['salestax_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>
    <td><?php echo $RowData['tax_code']; ?></td>
    <td><?php echo $vat_account[$RowData['account_group']]; ?></td>
	<td><?php 	   
	   //ledger_id
	  echo $s->getField('accountgroupmaster','GroupId',$RowData['ledger_id'],'GroupName');	   
	    ?></td>
	<td><?php echo $RowData['taxrate']; ?> <strong>%</strong></td>   
    <td style="text-align:center;"><a href="<?php echo $targetpage;?>&action=Edit&salestax_id=<?php echo $RowData['salestax_id']; ?>"><img src="images/icon-edit.png"  width="15" title="Edit Document" /></a></td>
	<td style="text-align:center;"><?php if($num3>0 || $num4>0){ ?><img src="images/Icon_Delete.png" width="15" title="No Permission to Delete Document" style="opacity:0.6;" /><?php }else{?><a href="<?php echo $targetpage;?>&action=del&salestax_id=<?php echo $RowData['salestax_id']; ?>" onclick="return confirm('Do You Want To Delete This Record')"><img src="images/Icon_Delete.png" width="15" title="Delete Document" /></a><?php } ?></td>	
   </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>
