<?php include('fun-Trial.php');?>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1>VAT Computations</h1>
<form action="" method="post">
<table width="100%" border="0" cellspacing="0" cellpadding="3">
  <tr class="row1">
    <td width="13%" align="right">Branch</td>
    <td width="20%"><select name="site" id="site">
		<option value="0">All</option>
		<?php 
		$site_sql=DB_query("Select loccode, prefix, locationname from locations where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in('".$_SESSION['AssignedBranch']."') ",$db);
		while($site_row=DB_fetch_array($site_sql))
		{?>
		<option value="<?php echo $site_row['loccode'];?>"<?php echo($_REQUEST['site']==$site_row['loccode']?'selected':'')?>><?php echo $site_row['locationname'];?></option>
		<?php 
		}
		?>
	  </select></td>
    <td width="13%" align="right">From Date</td>
    <td width="20%"><input name="fromDate" id="fromDate" type="text" size="9" value="<?php echo $_REQUEST['fromDate'];?>" class="datepicker"></td>
    <td width="13%" align="right">To Date</td>
    <td><input name="toDate" id="toDate" type="text" size="9" value="<?php    echo $_REQUEST['toDate'];?>" class="datepicker"></td>
  </tr>
  <tr class="row1">
  	<td colspan="6" style="text-align:center;"><input type="submit" name="View" value="View" class="search" /></td>
  </tr>
</table>

</form>


<?php 
  if($_REQUEST['View'])
  {
 
   	$site= $_REQUEST['site'];
     $fromDate=ConvertSQLDate ($_REQUEST['fromDate']); 
     $toDate= ConvertSQLDate($_REQUEST['toDate']); 
 
?>
<br>
 <table width="100%" border="0" cellspacing="0" cellpadding="3">
  <tr>
    <td width="50%" valign="top">		
    	<h3>OutPut Tax Computation</h3>
		<table width="100%" border="0" cellpadding="3" cellspacing="0" class="thborder">
		<tr>
		<th>Particulars</th>
		<th width="20%" style="text-align:center;">Assesable Value</th>
		<th width="20%" style="text-align:center;">Tax Amount</th>
		</tr>
	<?php
			$TotalINVAssesableAmount=array();
			$TotalINVTaxAmount=array();
			$TotalINVTaxVatAmount=array();
			$filter1=array();
			if($site!='0')
			{
			  	$loccode=$s->getField('locations','loccode',$site,'loccode');
			  	$filter1[]="branch_name='".$loccode."'";
			}
			else
			{
			  	$filter1[]="branch_name in(select loccode from locations where mst_companyid='".$_SESSION['ACGCompanyId']."')";
			}
			//print_r($filter1);

			if($fromDate!='//' &&  $toDate!='//')
			 	$filter1[]="invoiceDate>='".$fromDate."' and  invoiceDate   <='".$toDate."'";


			 $sql_salestax_data="select * from sales_tax_master where account_group in('VI','VO')";
			 $result_salestax_data=DB_query($sql_salestax_data,$db);
			 while($rs_salestax_data=DB_fetch_array($result_salestax_data))
			 {
			 
					 if(count($filter1)>0)
		 			 {
					   $f=implode("and  " , $filter1);
		  			    $query="select *,sum(taxableAmount) As AssesableAmount,sum(VatCstValue)  AS TaxAmount from invoice  where $f and VatCst_ledger='".$rs_salestax_data['salestax_id']."' group by VatCst_ledger";
					 
					 }
					 else
					 {			
					 		 		
					   $query="select *, sum(taxableAmount) As AssesableAmount,sum(VatCstValue)  AS TaxAmount  from invoice  where 1=1   group by VatCst_ledger";
			         }
			         //echo $query;
			   
			
			    
   	    $result=DB_query($query,$db);
		$i=1;
		if(DB_num_rows($result)>0)
		{
		
			while($myrow=DB_fetch_array($result))
			{

	?>		
		<tr>
		  <td><a href="#" onclick='popup("VatInvComputation.php?vid=<?php  echo $myrow['VatCst'].'&site='.$site.'&fromDate='.$fromDate.'&toDate='.$toDate.$rurl?>",900,600)'>
		  <?php echo $s->getField('sales_tax_master','salestax_id',$myrow['VatCst_ledger'],'tax_code');?></a>
		  
		  <?php $invledger= $s->getField('sales_tax_master','salestax_id',$myrow['VatCst_ledger'],'ledger_id'); ?></td>
		  <td style="text-align:right"><?php  echo $myrow['AssesableAmount'];
		   $TotalINVAssesableAmount[]=$myrow['AssesableAmount'];?></td>
		  
		   <td style="text-align:right"><?php  echo $myrow['TaxAmount'] ;
		       $TotalINVTaxAmount[]=$myrow['TaxAmount'];
			   $TotalINVTaxVatAmount[]= Get_AccountByLedger($invledger,'CR'); 
			 ?></td>
		  </tr>
		 
		 <?php } } }
		 ?>
		 <tr>
		  <td style="text-align:right">Total</td>
		  <td style="text-align:right"><?php echo array_sum($TotalINVAssesableAmount);?></td>
		  <td style="text-align:right"><?php echo array_sum($TotalINVTaxAmount);?></td>
		  </tr>
		 <?php 
		// } 
		  ?> 
		</table>
		</td>
    <td valign="top">
    	<h3>Input Tax Computation</h3>
		<table width="100%" border="0" cellpadding="3" cellspacing="0" class="thborder">
		<tr>
		<th>Particulars</th>
		<th width="20%" style="text-align:center;">Assesable Value</th>
		<th width="20%" style="text-align:center;">Tax Amount</th>
		</tr>
		<?php 
		$flag=1;
		$condtn="";
		if($_REQUEST['site']!="0")
		{
		$condtn.= "AND site='".$_REQUEST['site']."' ";
		}
		else if($_REQUEST['site']=="0")
		{
		$condtn="";
		}
		
		if($_REQUEST['fromDate']=="" && $_REQUEST['toDate']=="")
		{
		$fdate=$_SESSION['financialYrStart'];
		$tdate=$_SESSION['financialYrEnd'];		
		}
		else if($_REQUEST['fromDate']!="" && $_REQUEST['toDate']!="")
		{
		$fdate=convertSqlDate($_REQUEST['fromDate']);
		$tdate=convertSqlDate($_REQUEST['toDate']);
		}
		$main_query=DB_query("select * from sales_tax_master where account_group in('VI','CI')",$db);
		$vatpayAmt=0;
		while($main_result=DB_fetch_assoc($main_query))
			{
			$basic='';
			$vat_basic='';
		  // echo "SELECT SUM(tot_tax_amt) AS tot_tax_amt, SUM(taxvalue) AS tot_vat_exi,sum(vat_amt) as vat_cst FROM purchase_billbook inner join purchase_bill_details  on purchase_billbook.id=purchase_bill_details.purch_billbookid where bill_book_date BETWEEN '".$fdate."' AND '".$tdate."' $condtn   AND vat_id='".$main_result['salestax_id']."' GROUP BY purchase_bill_details.vat_cst";

		$vat_query=DB_query("SELECT SUM(basic_amt+Excise_Duty_Value+Edu_Cess+SHE_Cess+fright_amt+pkg_amt) AS tot_tax_amt, SUM(taxvalue) AS tot_vat_exi,sum(vat_amt) as vat_cst FROM purchase_billbook inner join purchase_bill_details  on purchase_billbook.id=purchase_bill_details.purch_billbookid where bill_book_date BETWEEN '".$fdate."' AND '".$tdate."' $condtn   AND vat_id='".$main_result['salestax_id']."' GROUP BY purchase_bill_details.vat_cst" ,$db);
			while($vat_result=DB_fetch_assoc($vat_query))
			{

		?> 
		
			<?php  $basic+=$vat_result['tot_tax_amt'];  ?>
			<?php $vat_basic+=$vat_result['tot_vat_exi'];  ?>
			<?php } 

			//echo "SELECT SUM(basic_amt) AS tot_basic_amt, SUM(vat_cst_amt) AS tot_vat_nexi,p.vat_cst as vat_cst FROM purchase_bill_details pd , purchase_billbook p WHERE p.id=pd.purch_billbookid AND p.bill_book_date BETWEEN '".$fdate."' AND '".$tdate."' $condtn  AND pd.vat_cst='".$main_result['salestax_id']."' GROUP BY p.vat_cst";
			
		/*	$vat_query1=DB_query("SELECT SUM(total_item_amount) AS tot_basic_amt, SUM(vat_cst_amt) AS tot_vat_nexi,p.vat_cst as vat_cst FROM purchase_bill_details pd , purchase_billbook p WHERE p.id=pd.purch_billbookid AND p.bill_book_date BETWEEN '".$fdate."' AND '".$tdate."' $condtn  AND p.vat_cst='".$main_result['salestax_id']."' GROUP BY p.vat_cst" ,$db);
			while($vat_result1=DB_fetch_assoc($vat_query1))
			{
		 
				$vat_name1=DB_fetch_array(DB_query("select  tax_code from sales_tax_master where salestax_id='".$vat_result1['vat_cst']."' ",$db));  ?>
				<?php $vat_result1['tot_basic_amt']; $basic+=$vat_result1['tot_basic_amt']; ?>
				<?php $vat_result1['tot_vat_nexi']; $vat_basic+=$vat_result1['tot_vat_nexi']; ?>
		
			<?php }   */
			
			if($basic>0 && $vat_basic>0){
			$flag=0;?>
			<tr>
			<td><?php echo $main_result['tax_code']; ?></td>
			<td style="text-align:right"><?php echo $basic; ?></td>
			<td style="text-align:right"><?php echo $vat_basic; ?></td>
		</tr>
			<?php
			}
			$vatpayAmt+=$vat_basic;
			}
			?>	
			<?php if($flag==1) {?>
			<tr>
				<td colspan="3">NO Result Found.</td>
			</tr>
			<?php }?>
		</table>	</td>
  </tr>
  <tr>
    <td valign="top">&nbsp;</td>
    <td valign="top"><table width="100%" width="100%" border="0" cellspacing="0" cellpadding="3">
      <tr class="row1">
        <td style="text-align:right">Total Input Tax :</td>
        <td width="31%">
		<?php //   $vatpayAmt= array_sum($TotalINVTaxAmount)- array_sum($TotalTaxAmount);?>
		<input name="Vatpayable" readonly type="text" value="<?php echo  $vatpayAmt;?>" style="text-align:right" /></td>
      </tr>	  
	  <tr class="row1">
        <td style="text-align:right">Vat Refundable/Carried Forward :</td>
        <td>
		<?php //   $vatpayAmt= array_sum($TotalINVTaxAmount)- array_sum($TotalTaxAmount);?>
		<input name="Vatpayable" readonly type="text" value="" style="text-align:right" /></td>
      </tr>	  
      <tr class="row1">
        <td style="text-align:right">Tax Challan Adjusment :</td>
        <td>
		<?php 
		$cdt="";
		if($_REQUEST['site']!="0")
		{
		$cdt.= "AND a.Branch='".$_REQUEST['site']."' ";
		}
		else if($_REQUEST['site']=="0")
		{
		$cdt="";
		}
		$adjust_amt=0;
		$ledgerquery=DB_query("SELECT AccountsID FROM accountledger WHERE ledgertype='salestax' and taxid in(select salestax_id from sales_tax_master where account_group in('VI','VO'))",$db);
		 		while($ledgeresult=DB_fetch_assoc($ledgerquery))
				{
					 
					// echo "SELECT SUM(amount) AS tot_amt FROM account_book a,account_book_details ad WHERE a.id=ad.account_bookid AND amount_type='DR' AND ledgerid='".$ledgeresult['AccountsID']."' AND a.Dates BETWEEN '".$fdate."' AND '".$tdate."' $cdt ";
					// echo "<br>";
					$adjust_sql=DB_fetch_assoc(DB_query("SELECT SUM(amount) AS tot_amt FROM account_book a,account_book_details ad WHERE a.id=ad.account_bookid AND amount_type='CR' AND ledgerid='".$ledgeresult['AccountsID']."' AND a.Dates BETWEEN '".$fdate."' AND '".$tdate."' $cdt ",$db));
					//echo $adjust_sql['tot_amt'];
					//echo "<br>";
					$adjust_amt+=$adjust_sql['tot_amt'];
				}
		$diff=$vatpayAmt-$adjust_amt;
		?>
	
		<input name="TaxChallanAdjustment" readonly type="text" value="<?php echo $adjust_amt;?>" style="text-align:right" /></td>
      </tr>
      <tr class="row1">
        <td style="text-align:right">Difference :</td>
        <td><input name="Difference" readonly type="text" style="text-align:right" value="<?php echo  $diff;?>"/></td>
      </tr>
    </table></td>
  </tr>
</table>
<?php } ?>

</div>
</div>