<?php 
session_start();
ob_start(); 
include "header.php";
//$objIms->Semifinisheditem_alter();

function callvalue($sale_no,$val)
{
   $sql_viewtmp="select sum((qty*unit_price)-((qty*unit_price*discount)/100)) as taxvalue from view_sales_mstrdet where session_login_id = '".$_SESSION['login_id']."' and sale_no='$sale_no' and cgst='$val' group by client_id";
				$res_viewtmp=mysql_query($sql_viewtmp);
				$rsw=mysql_fetch_array($res_viewtmp);
				if(isset($rsw["taxvalue"]))
				{
				$showamt=$rsw["taxvalue"];
				}
				else
				{
				 $showamt=0;
				}
				 
				return $showamt;
}

function callvaluetotal($s_date,$end_date,$client_id,$val)
{
   $sql_viewtmp="select sum(((qty*unit_price)-((qty*unit_price*discount)/100)) as taxvalue from view_sales_mstrdet where session_login_id = '".$_SESSION['login_id']."' and  stampdate>='$s_date' and stampdate<='$end_date' and client_id='$client_id' and cgst='$val' group by client_id";
				$res_viewtmp=mysql_query($sql_viewtmp);
				$rsw=mysql_fetch_array($res_viewtmp);
				if(isset($rsw["taxvalue"]))
				{
				$showamt=$rsw["taxvalue"];
				}
				else
				{
				 $showamt=0;
				}
				 
				return $showamt;
}


?>


<!-- 	<script>
	$(document).ready(function() {
  $("#btnExport").click(function(e) {
    e.preventDefault();

    //getting data from our table
    var data_type = 'data:application/vnd.ms-excel';
    var table_div = document.getElementById('table_wrapper');
    var table_html = table_div.outerHTML.replace(/ /g, '%20');

    var a = document.createElement('a');
    a.href = data_type + ', ' + table_html;
    a.download = document.getElementById('rahul').value + '.xls';
    a.click();
  });
});
</script> -->



		
	
		 <style>
			select.error { background-color:#ffa8a8; border: 1px dotted red;  }
			input.error{ background-color:#ffa8a8;   }
			textarea.error{ background-color:#ffa8a8; border: 1px dotted red;  }
         </style>
		 
        


<style type="text/css">
<!--
.table_bg{ border:#CCCCCC solid 1px; border-radius:5px; height:auto; width:auto; padding:20px; margin-bottom:10px;}


-->
</style>

<script type="text/javascript">
 
function printDiv(divName) {
     var printContents = document.getElementById(divName).innerHTML;
     var originalContents = document.body.innerHTML;

     document.body.innerHTML = printContents;

     window.print();

     document.body.innerHTML = originalContents;
}

</script>

<!-- Page wrapper  -->
        <div class="page-wrapper">
         
            <!-- Container fluid  -->
            <div class="container-fluid">
                <!-- Start Page Content -->
                <div class="row">
                    <div class="col-12">
                        <div class="card">
                            <div class="card-body">
							<h4>GST BB Report<span style="float:right"><button onclick="goBack()" class="btn btn-info btn-sm">Go Back</button><script>
function goBack() {
  window.history.back();
}
</script></span></h4>
							<hr />
<form name="form1" id="form1" method="post">
  <table id="example23" class="display nowrap table table-hover table-striped table-bordered" cellspacing="0" width="100%">
    <tr>
      <td  nowrap="nowrap"><strong>From Date </strong></td>
      <td><strong>:</strong></td>
      <td><input name="s_date" type="date" class="tcal form-control" id="s_date"   value="<?php if(isset($_POST["s_date"])){echo $_POST["s_date"];}else{echo date('d-m-Y');}?>"/></td>
      <td ><strong>To Date </strong></td>
      <td ><strong>:</strong></td>
      <td ><input name="end_date" type="date" class="tcal form-control" id="end_date"  value="<?php if(isset($_POST["end_date"])){echo $_POST["end_date"];}else{echo date('d-m-Y');}?>" /></td>
      
      <td ><input type="submit" name="Choose" value="Choose" class="btn btn-info" /></td>
      </tr>
  </table>
  
   <table id="example23" class="display nowrap table table-hover table-striped table-bordered" cellspacing="0" width="100%">
  <tr>
  <td><div align="right">Client Name</div></td>
      <td >
	    <div align="center">
	      <select name="client_name" id="client_name" style="width:200px;"  class="required form-control">
	        <option value="">---Please Select---</option>
	        <?php 
			
			$sdt = date('d-m-y', strtotime($_POST["s_date"]));
$s_date=$objIms->DateToInt($sdt);
$edt = date('d-m-y', strtotime($_POST["end_date"]));
$end_date=$objIms->DateToInt($edt);
							$sqlc="select client_id,client_name from view_sales_mstrdet where session_login_id = '".$_SESSION['login_id']."' and stampdate>='".$s_date."' and stampdate<='".$end_date."' and cancel_status=0 and tin_no<>'' group by client_id";
							//echo $sqlc;
							$c=mysql_query($sqlc);
							if(mysql_num_rows($c)>0){
							while($cs=mysql_fetch_array($c))
							{?>
	        <option value="<?=$cs["client_id"];?>" <?php if(($cs["client_id"])==$_POST["client_name"]){ ?> selected="selected" <?php }?>>
	          <?=$cs["client_name"];?>
	          </option>
	        <?php }}?>
	            </select>
        </div></td>
	<td ><input type="submit" name="Search" value="Search" class="btn btn-info" /></td>
	</tr>
  </table>
  
</form>
<br />

<?php 
if(isset($_POST["Search"]))
{
$sdt = date('d-m-y', strtotime($_POST["s_date"]));
$s_date=$objIms->DateToInt($sdt);
$edt = date('d-m-y', strtotime($_POST["end_date"]));
$end_date=$objIms->DateToInt($edt);
/*$s_date=$objIms->DateToInt($_POST["s_date"]);
$end_date=$objIms->DateToInt($_POST["end_date"]);*/
$client_id=$_POST["client_name"];
$sqlqw="select * from tbl_client where session_login_id = '".$_SESSION['login_id']."' and id='$client_id' order by client_name";
							//echo $sqlc;
							$qw=mysql_query($sqlqw);							
							$rwqw=mysql_fetch_array($qw);
							$client_name=$rwqw["client_name"];

?>
     
       <input type="hidden" name="rahul" id="rahul" value="<?=$client_name?>" />     
		
            <?php 
				   $sql_viewtmp="select *,sum(((qty*unit_price)-((qty*unit_price*discount)/100))+(((qty*unit_price)-((qty*unit_price*discount)/100))*(cgst/100))+(((qty*unit_price)-((qty*unit_price*discount)/100))*(sgst/100))) as taxvalue from view_sales_mstrdet where session_login_id = '".$_SESSION['login_id']."' and stampdate>='$s_date' and stampdate<='$end_date' and client_id='$client_id' and cancel_status=0 and tin_no<>'' group by client_id,sale_no";
				$res_viewtmp=mysql_query($sql_viewtmp);
				if(mysql_num_rows($res_viewtmp)>0)
				{ 
					$i=1;
					$sums=0;
					$tottaxval=0;
					$totcgst=0;
					$totsgst=0;
					$totigst=0;
					?>
					<table id="tbl1" class="table2excel">
					<div id="table_wrapper">
					<div id="printableArea">
					<div class="panel">
					<div class="panel-heading">GSTR1 B2B Report For <strong><?=$client_name?></strong> From <?php echo $_POST["s_date"];?> To <?php echo $_POST["end_date"];?></div>
					<p>&nbsp;</p>
                     <table width="100%" class="table table-bordered">
                     <tr height="32" align="left">
            <th nowrap="nowrap" width="6%">Si No</th>
            <th nowrap="nowrap" width="24%">Sale Date </th>
            <th nowrap="nowrap" width="35%">Sale No </th>
            </table>
            <table id="tbl2" class="table2excel">
            <th nowrap="nowrap" width="35%">GST No. </th>
            
           
            
            <th nowrap="nowrap" width="18%">Invoice Value </th>
            <?php $sqlc="SELECT distinct cgst FROM tbl_sales_detail where  session_login_id = '".$_SESSION['login_id']."' order by cgst";
							//echo $sqlc;
							$c=mysql_query($sqlc);
							if(mysql_num_rows($c)>0){
							while($cs=mysql_fetch_array($c))
							{?>
            <th nowrap="nowrap" width="18%">Taxable value @<?=$cs["cgst"];?> </th>
			<?php }}?>
            
            
                     </tr>
            <?php
				while($rows=mysql_fetch_assoc($res_viewtmp))
					{
					$item_id=$rows["item_id"];	
					$unit_id=$rows["unit_id"];
					$client_id=$rows["client_id"];	
						
					$sql_it="select * from tbl_item where id='$item_id' and session_login_id = '".$_SESSION['login_id']."'";
				$res_it=mysql_query($sql_it);
				$rsit=mysql_fetch_array($res_it);
					
					$sql_ut="select * from tbl_unit where id='$unit_id' and session_login_id = '".$_SESSION['login_id']."'";
				$res_ut=mysql_query($sql_ut);
				$rsut=mysql_fetch_array($res_ut);
				
					
			?>
           </table>
           <table id="tbl3" class="table2excel">
            <tr height="32">
            <td nowrap="nowrap" width="6%"><?php echo $i++;?></td>
           
		    <td nowrap="nowrap" width="24%"><?php echo $objIms->IntToDate($rows["stampdate"]);?></td>
		    
		    <td nowrap="nowrap" width="35%"><?php echo $rows["sale_no"];?></td>		    
            <td nowrap="nowrap" width="35%"><?php echo $rows["tin_no"];?></td>
            
            
            <td nowrap="nowrap" width="18%"><?php echo $taxval=round($rows["taxvalue"]);$tottaxval+=$taxval;?></td>
            </table>
           <?php $sqlc="SELECT distinct cgst FROM tbl_sales_detail where session_login_id = '".$_SESSION['login_id']."' order by cgst";
							//echo $sqlc;
							$c=mysql_query($sqlc);
							if(mysql_num_rows($c)>0){
							while($cs=mysql_fetch_array($c))
							{?> <td nowrap="nowrap" width="18%"><?php echo $cgstval=round(callvalue($rows["sale_no"],$cs["cgst"]),2);$totcgst+=$cgstval;?></td>
             <?php }}?>
            
            </tr>
            
            <?php }?>
            <table id="tbl4" class="table2excel">
			<tr height="32">
              <td colspan="4" nowrap="nowrap"><div align="right"><strong>Total : </strong></div></td>
              <td nowrap="nowrap"><strong><?php echo $tottaxval;?></strong></td>
              
              <?php $sqlc="SELECT distinct cgst FROM tbl_sales_detail where session_login_id = '".$_SESSION['login_id']."' order by cgst";
							//echo $sqlc;
							$c=mysql_query($sqlc);
							if(mysql_num_rows($c)>0){
							while($cs=mysql_fetch_array($c))
							{?> <td nowrap="nowrap"><strong><?php echo round(callvaluetotal($s_date,$end_date,$client_id,$cs["cgst"]),2);?></strong></td>
              <?php }}?>
              
            </tr>  
            </table> 
           </table>
		   </div>
		   </div>
		   </div>
    <?php }
	else
	{
	echo '<span style="color:#FF0000">No Data Found</span>';
	}
	}?>
	
		

   
<p></p>
		<br />
        


</div>
<br />

<div align="center"><span class="btn btn-info" onClick="printDiv('printableArea')">Print</span> &nbsp;&nbsp;<button class="btn btn-info" onclick="tablesToExcel(['tbl1','tbl2','tbl3','tbl4','tbl5','tbl6'], ['ProductDay1','ProductDay2','ProductDay3','ProductDay4','ProductDay5','ProductDay6'], 'TestBook.xls', 'Excel')">Export to Excel</button></div>


 </div>
                    </div>
                </div>
                <!-- End PAge Content -->
            </div>
            <!-- End Container fluid  -->
<?php include 'footer.php' ?>

<script>
	$(document).ready(function() {
  $("#btnExport").click(function(e) {
    e.preventDefault();

    //getting data from our table
    var data_type = 'data:application/vnd.ms-excel';
    var table_div = document.getElementById('table_wrapper');
    var table_html = table_div.outerHTML.replace(/ /g, '%20');

    var a = document.createElement('a');
    a.href = data_type + ', ' + table_html;
    a.download = document.getElementById('rahul').value + '.xls';
    a.click();
  });
});
</script>

 <script src="../js/lib/datatables/datatables.min.js"></script>
    <script src="../js/lib/datatables/cdn.datatables.net/buttons/1.2.2/js/dataTables.buttons.min.js"></script>
    <script src="../js/lib/datatables/cdn.datatables.net/buttons/1.2.2/js/buttons.flash.min.js"></script>
    <script src="../js/lib/datatables/cdnjs.cloudflare.com/ajax/libs/jszip/2.5.0/jszip.min.js"></script>
    <script src="../js/lib/datatables/cdn.rawgit.com/bpampuch/pdfmake/0.1.18/build/pdfmake.min.js"></script>
    <script src="../js/lib/datatables/cdn.rawgit.com/bpampuch/pdfmake/0.1.18/build/vfs_fonts.js"></script>
    <script src="../js/lib/datatables/cdn.datatables.net/buttons/1.2.2/js/buttons.html5.min.js"></script>
    <script src="../js/lib/datatables/cdn.datatables.net/buttons/1.2.2/js/buttons.print.min.js"></script>
    <script src="../js/lib/datatables/datatables-init.js"></script>
     <script type="text/javascript">
    var tablesToExcel = (function() {
    var uri = 'data:application/vnd.ms-excel;base64,'
    , tmplWorkbookXML = '<?xml version="1.0"?><?mso-application progid="Excel.Sheet"?><Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">'
      + '<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office"><Author>Axel Richter</Author><Created>{created}</Created></DocumentProperties>'
      + '<Styles>'
      + '<Style ss:ID="Currency"><NumberFormat ss:Format="Currency"></NumberFormat></Style>'
      + '<Style ss:ID="Date"><NumberFormat ss:Format="Medium Date"></NumberFormat></Style>'
      + '</Styles>' 
      + '{worksheets}</Workbook>'
    , tmplWorksheetXML = '<Worksheet ss:Name="{nameWS}"><Table>{rows}</Table></Worksheet>'
    , tmplCellXML = '<Cell{attributeStyleID}{attributeFormula}><Data ss:Type="{nameType}">{data}</Data></Cell>'
    , base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }
    , format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) }
    return function(tables, wsnames, wbname, appname) {
      var ctx = "";
      var workbookXML = "";
      var worksheetsXML = "";
      var rowsXML = "";

      for (var i = 0; i < tables.length; i++) {
        if (!tables[i].nodeType) tables[i] = document.getElementById(tables[i]);
        for (var j = 0; j < tables[i].rows.length; j++) {
          rowsXML += '<Row>'
          for (var k = 0; k < tables[i].rows[j].cells.length; k++) {
            var dataType = tables[i].rows[j].cells[k].getAttribute("data-type");
            var dataStyle = tables[i].rows[j].cells[k].getAttribute("data-style");
            var dataValue = tables[i].rows[j].cells[k].getAttribute("data-value");
            dataValue = (dataValue)?dataValue:tables[i].rows[j].cells[k].innerHTML;
            var dataFormula = tables[i].rows[j].cells[k].getAttribute("data-formula");
            dataFormula = (dataFormula)?dataFormula:(appname=='Calc' && dataType=='DateTime')?dataValue:null;
            ctx = {  attributeStyleID: (dataStyle=='Currency' || dataStyle=='Date')?' ss:StyleID="'+dataStyle+'"':''
                   , nameType: (dataType=='Number' || dataType=='DateTime' || dataType=='Boolean' || dataType=='Error')?dataType:'String'
                   , data: (dataFormula)?'':dataValue
                   , attributeFormula: (dataFormula)?' ss:Formula="'+dataFormula+'"':''
                  };
            rowsXML += format(tmplCellXML, ctx);
          }
          rowsXML += '</Row>'
        }
        ctx = {rows: rowsXML, nameWS: wsnames[i] || 'Sheet' + i};
        worksheetsXML += format(tmplWorksheetXML, ctx);
        rowsXML = "";
      }

      ctx = {created: (new Date()).getTime(), worksheets: worksheetsXML};
      workbookXML = format(tmplWorkbookXML, ctx);



      var link = document.createElement("A");
      link.href = uri + base64(workbookXML);
      link.download = wbname || 'Workbook.xls';
      link.target = '_blank';
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
    }
  })();
  </script>
    </script>