<?php 
$PageSecurity = 11;
include('includes/DefinePOClass.php');
include('includes/DefineSerialItems.php');
include('includes/SQL_CommonFunctions.inc');
$sql=DB_query("SELECT * FROM accountgroupmaster WHERE GroupName REGEXP '^current liabilit'",$db);
$row=DB_fetch_array($sql);
$sql1="Select * from accountgroupmaster where MainGroupId in (".$row['GroupId'].")";
$result1=DB_query($sql1,$db);
while($row1=DB_fetch_array($result1))
{$ledger_array[]=$row1['GroupId'];}
function getUnderLiabilities($ledger_array,$under_group)
{
	global $db;
	$str=implode(',',$ledger_array);
	$SQL="Select * from accountgroupmaster where MainGroupId in ($str)";
	$RESULT=DB_query($SQL,$db);
	while($ROW=DB_fetch_array($RESULT))
	{
		$array2[]=$ROW['GroupId'];
	}
	if($array2!=''){
	if(in_array($under_group,$array2)==1)
	{
		$bool=1;
	}
	elseif(in_array($under_group,$array2)=='')
	{
		 return getUnderLiabilities($array2,$under_group);
	}}
	
	return $bool;
	
}
?>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1>GST Register</h1>
<form action="" name="main_account" method="post" onsubmit="return validate()">
<table width="100%" border="0" cellpadding="3" cellspacing="0">
<tr class="row1"> 
  <td width="13%" align="right">Branch</td>
  <td><select name="branch" id="branch">
        <option value="0">All</option>
        <?php 
           $sql_locations=DB_query("select loccode,locationname from locations order by locationname",$db);
           while($result_locations=DB_fetch_array($sql_locations))
           {
        ?> 
        <option value="<?php echo $result_locations['loccode']; ?>" <?php if($_REQUEST['branch']==$result_locations['loccode']) {?> selected="selected" <?php } ?>><?php echo $result_locations['locationname']; ?></option>
     <?php } ?>
  </select></td> 
  <td width="13%" align="right">From Date</td>
  <td width="20%"><input name="fromdate"  id="fromdate" type="text" class="datepicker" value="<?php if($_REQUEST['fromdate']!='') { echo $_REQUEST['fromdate']; } else { echo ConvertSQLDate($_SESSION['financialYrStart']); } ?>"/></td>
  <td width="13%" align="right">To Date</td>
  <td width="20%"><input name="todate" id="todate" type="text" class="datepicker" value="<?php if($_REQUEST['todate']!='') { echo $_REQUEST['todate']; } else { echo ConvertSQLDate($_SESSION['financialYrEnd']); } ?>" onchange="return date_check();" /></td>  
</tr>
<tr class="row1"> 
  <td colspan="6" style="text-align:center;"><input name="Search" type="submit" class="search"   value="Search"/></td>
</tr>
</table>
</form>
<p align="right"><a href="gst_input_register_print.php" class="search" target="_blank">Print</a></p>
<div style="width:100%; height:360px; float:left; overflow:auto;">
<table width="130%" border="0" cellspacing="0" cellpadding="3" class="thborder">
<thead>
  <tr>
    <th width="2%">S. No.</th>
    <th width="4%">CIN No.</th>
    <th width="4%">GSTIN</th>
    <th width="4%">Purchase Bill No.</th>
    <!--<th width="4%">Line Item</th> -->
    <th width="4%">Date</th>
    <th width="3%">Vendor Code</th>
    <th>Vendor Name</th>
    <th width="3%">State Code</th>
     <th width="7%">State</th>
    <th width="4%">Product Code</th>
    <th width="6%">Part Name</th>
    <th width="3%" style="text-align:center;">Qty.</th>
    <th width="3%" style="text-align:center;">Rate</th>
    <th width="5%" style="text-align:center;">Product Value</th>
    <th width="3%" style="text-align:center;">Discount %</th>
    <th width="5%" style="text-align:center;">Product Value after Discount</th>
    <th width="3%" style="text-align:center;">Tax %</th>   
    <th width="3%" style="text-align:center;">Tax Amt.</th>
    <th width="3%" style="text-align:center;">SGST</th>
    <th width="3%" style="text-align:center;">CGST</th>
    <th width="3%" style="text-align:center;">IGST</th>
    <th width="3%" style="text-align:center;">UGST</th>
    <th width="4%" style="text-align:center;">Total Value on Invoice</th>
  </tr>
  </thead>
  <tbody><?php 
  if($_REQUEST['Search'])
  {
    $filter=array();

    $fromdate=ConvertSQLDate($_REQUEST['fromdate']);
    $todate=ConvertSQLDate($_REQUEST['todate']);

     if($_REQUEST['branch']!='0')
    {
      $filter[]="site='".$_REQUEST['branch']."'";
    }
    else
    {
      $filter[]="1=1";
    }

    if($_REQUEST['fromdate']!='' && $_REQUEST['todate']!='')
    {
      $filter[]="bill_book_date between '".$fromdate."' and '".$todate."'";
    }
     if(!empty($filter))
     {
       $filter1=implode(" and ",$filter);
     }
     else
     {
      $filter1="1=1";
     }
   
   $GetRecord="SELECT p.id AS main_id,pd.mst_productid,pd.quantity,pd.rate,pd.total_price,pd.discount_amount as disc,pd.vat_id,pd.vat_amt,pd.discount,pd.sgst_amt,pd.cgst_amt,pd.igst_amt,pd.ugst_amt,p.site,p.bill_book_no,p.bill_book_date,p.vendor,pd.gst_tax_amt,gst_tax_perc FROM purchase_billbook p
INNER JOIN purchase_bill_details pd ON p.id=pd.purch_billbookid where $filter1 ";
  $_SESSION['gst_register']=$GetRecord;
  $_SESSION['gst_from_date']=$_REQUEST['fromdate'];
  $_SESSION['gst_to_date']=$_REQUEST['todate'];

 
  $result=DB_query($GetRecord,$db);
  $i=1;
  while($in_fetch=DB_fetch_array($result))
    {
           $item_code=$s->GetFieldValue('item_master',"id='".$in_fetch['mst_productid']."'",'StockID');
           $item_name=$s->GetFieldValue('item_master',"id='".$in_fetch['mst_productid']."'",'item_name');  
          
      ?>
  	<tr>
     <td><?php echo $i; ?></td>
      <td><?php echo $s->GetFieldValue('suppliers',"id='".$in_fetch['vendor']."'",'cin_no'); ?></td>
      <td><?php echo $s->GetFieldValue('suppliers',"id='".$in_fetch['vendor']."'",'gst_no'); ?></td>
      <td><?php echo $in_fetch['bill_book_no']; ?></td>
  		<!--<td></td> -->
  		<td><?php echo ConvertSqlDate($in_fetch['bill_book_date']);?></td>
  		<td><?php echo $s->GetFieldValue('suppliers',"id='".$in_fetch['vendor']."'",'vendor_code');?></td>
  		<td><?php echo $s->GetFieldValue('suppliers',"id='".$in_fetch['vendor']."'",'vendor_name');?></td>
      <td style="text-align:center;"><?php $state_id=$s->GetFieldValue('suppliers',"id='".$in_fetch['vendor']."'",'state'); echo $s->GetFieldValue('state_master',"id='".$state_id."'",'state_code');?></td>
  		<td><?php 
       echo $s->GetFieldValue('state_master',"id='".$state_id."'",'state');?></td>
      <td><?php echo $item_code;?></td>
  		<td><?php echo $item_name;?></td>
  		<td style="text-align:right;"><?php echo $in_fetch['quantity'];?></td>
  		<td style="text-align:right;"><?php echo $in_fetch['rate'];?></td>
  		<td style="text-align:right;"><?php echo $in_fetch['total_price']; $basic[]=$in_fetch['total_price']; ?></td>
  		<td style="text-align:right;"><?php echo $in_fetch['discount']; 

          $discount_amount=$in_fetch['discount'];
                             if($discount_amount!='0.00')
                             {
                               $dis_perc1=$in_fetch['total_price']*$in_fetch['discount'];
                                $dis_per=$dis_perc1/100;
                             }   
                             else
                             {
                               $dis_per='0.00';
                             }
                             


       ?></td>
      <td style="text-align:right;"><?php echo $in_fetch['total_price']-$dis_per; $val_after_disc=$in_fetch['total_price']-$dis_per; $disc_arr[]=$in_fetch['total_price']-$dis_per; ?></td>
      <td style="text-align:right;"><?php  echo $in_fetch['gst_tax_perc']; ?>
       </td>
      <td style="text-align:right;"><?php echo $in_fetch['gst_tax_amt']; $vat_arr[]=$in_fetch['vat_amt']; ?></td>
      <td style="text-align:right;"><?php echo $in_fetch['sgst_amt']; $sgst_arr[]=$in_fetch['sgst_amt']; ?></td>
      <td style="text-align:right;"><?php echo $in_fetch['cgst_amt']; $cgst_arr[]=$in_fetch['cgst_amt']; ?></td>
      <td style="text-align:right;"><?php echo $in_fetch['igst_amt']; $igst_arr[]=$in_fetch['igst_amt'];  ?></td>
      <td style="text-align:right;"><?php echo $in_fetch['ugst_amt']; $ugst_arr[]=$in_fetch['ugst_amt']; ?></td>
       <td style="text-align:right;">
        <?php
         /*   if($in_fetch['vat_amt']!='0.00')
           {
              $total_invoice=$val_after_disc+$in_fetch['vat_amt'];
              echo number_format((float)$total_invoice,2,'.',',');
           }
           else
           { */
              $total_invoice=$val_after_disc+$in_fetch['sgst_amt']+$in_fetch['cgst_amt']+$in_fetch['igst_amt']+$in_fetch['ugst_amt'];
              echo number_format((float)$total_invoice,2,'.',',');
          // }
             $tot_inv[]=$total_invoice;
    
         ?></td>
  	</tr><?php $i++; }?>
   
    <tr>
      <td colspan="13" style="text-align:right;"><strong>Total</strong></td>
      <td style="text-align:right;"><strong><?php if(!empty($basic)) { echo number_format((float)array_sum($basic),2,'.',','); } else { echo "0.00"; } ?></strong></td>
      <td style="text-align:right;"></td>
      <td style="text-align:right;"><strong><?php if(!empty($disc_arr)) { echo number_format((float)array_sum($disc_arr),2,'.',','); } else { echo "0.00"; } ?></strong></td>
      <td style="text-align:right;"></td>
      <td style="text-align:right;"><strong><?php if(!empty($vat_arr)) { echo number_format((float)array_sum($vat_arr),2,'.',','); } else { echo "0.00"; } ?></strong></td>
      <td style="text-align:right;"><strong><?php if(!empty($sgst_arr)) { echo number_format((float)array_sum($sgst_arr),2,'.',','); } else { echo "0.00"; } ?></strong></td>
     <td style="text-align:right;"><strong><?php if(!empty($cgst_arr)) { echo number_format((float)array_sum($cgst_arr),2,'.',','); } else { echo "0.00"; } ?></strong></td>
     <td style="text-align:right;"><strong><?php if(!empty($igst_arr)) { echo number_format((float)array_sum($igst_arr),2,'.',','); } else { echo "0.00"; } ?></strong></td>
     <td style="text-align:right;"><strong><?php if(!empty($ugst_arr)) { echo number_format((float)array_sum($ugst_arr),2,'.',','); } else { echo "0.00"; } ?></strong></td>
      <td style="text-align:right;"><strong><?php if(!empty($tot_inv)) { echo number_format((float)array_sum($tot_inv),2,'.',','); } else { echo "0.00"; } ?></strong></td>
    </tr>
    
    <?php } else { ?>
     <tr>
       <td colspan="24"><strong>No Records Found</strong></td>
    </tr>
   
    <?php } ?>
     

  </tbody>
	</table>

</div>
</div>
<script>
function validate()
{
 
   if(document.getElementById('todate').value=='')
  {
    alert("please Enter From Date");
    document.getElementById('todate').focus();
    return false;
  }

  if(document.getElementById('fromdate').value=='')
  {
    alert("please Enter To Date");
    document.getElementById('fromdate').focus();
    return false;
  }
}
</script>