<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($user)<=0)
{
  print "<script> alert('Invalid User'); document.location='index.php' </script>";
  return;
}
if (date('m') < 4) {//Upto June 2014-2015
    $fin_year = (date('Y')-1) . '-' . date('y');
} else {//After June 2015-2016
    $fin_year = date('Y') . '-' . (date('y') + 1);
  
}
include('opendb.php');
include('top.php');
?>
<link href="css/form.css" type="text/css" rel="stylesheet" media="screen" />
<html>
<body >
<center>
<title>GST Report</title>

<link type="text/css" href="jquery/jquery-ui.min.css" rel="stylesheet" />	
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="cal/jquery-ui1.js"></script>
<script type="text/javascript" src="jquery/jquery-1.7.min.js"></script>
<script type="text/javascript" src="jquery/jquery-ui.min.js"></script>




<script type="text/javascript">
$(function() {
$( "#ref_date" ).datepicker("destroy");
     var fyr=document.getElementById('f_yr').value;
     var splv=fyr.split("-");
     var syr=splv[0];
     var eyr='20'.concat(splv[1]);
     var syr1='01-04-';
     var eyr1='31-03-';
     var stfnyr=syr1.concat(syr);
     var efnyr=eyr1.concat(eyr);
     $("#ref_date").datepicker({ dateFormat: 'dd-mm-yy', minDate: stfnyr, maxDate: efnyr});

$( "#ref_date1" ).datepicker("destroy");
     var fyr=document.getElementById('f_yr').value;
     var splv=fyr.split("-");
     var syr=splv[0];
     var eyr='20'.concat(splv[1]);
     var syr1='01-04-';
     var eyr1='31-03-';
     var stfnyr=syr1.concat(syr);
     var efnyr=eyr1.concat(eyr);
     $("#ref_date1").datepicker({ dateFormat: 'dd-mm-yy', minDate: stfnyr, maxDate: efnyr});
});
</script>
<script>
function valid()
{
}
</script>
<script type="text/javascript">
$(function(){
    $(".orderto").live("keydown.autocomplete",function(){
$orderto = $(this);
$row = $orderto.parent().parent();
$(this).autocomplete({
		source: function( request, response ) {

		$.ajax({
			url: "searchvend.php",
			dataType: "json",
			data: "key="+$orderto.val(),
			success: function( data ) {
				response( $.map( data, function( item ) {
					return {
						label: item.orderto+' | '+  item.partname +' | '+  item.price  +' | '+  item.supqty  ,
						value: item.orderto,
						code:item.partname,
							price:item.price,
							supqty:item.supqty,
							dist:item.shidist,
						    custstt:item.custstt,
						    shippinvl:item.shipin,
						    custtot:item.mrgnprc,

					}
				}));
			}
		});
	}
	
,
	minLength: 2,
	select: function( event, ui ) {
	$row.find('.orderto').val(ui.item.orderto);


 
 
	//$row.find('.msno').val(ui.item.partname);
	
	//document.f1.addr.value=ui.item.adr;
	//document.f1.tin.value=ui.item.gst;
	//document.f1.shipgst.value=ui.item.gst;
	//document.f1.marginprc.value=ui.item.marginpr;
	//if(ui.item.custstt=='KERALA')
	//{
		//document.f1.vatorcst.value='INTRASTATE';
	//}
	//else
	//{
		//document.f1.vatorcst.value='INTERSTATE';
	//}
	//document.f1.shipdist.value=ui.item.shipdistvl;
	//document.f1.shippin.value=ui.item.shippinvl;
	//document.f1.shipto.value=ui.item.value;
	//vehiclemsno(ui.item.value);
	
	//document.f1.msno.focus();
	/*log( ui.item ?
	"Selected: " + ui.item.label :
	"Nothing selected, input was " + this.value);
	*/
	 }
	
	});

					
});
});
    </script>
<style>
#txt{
font-family: Arial, Helvetica;
background-color: #F2F2F2;
}
</style>
<link rel="stylesheet" type="text/css" media="all" href="jsDatePick_ltr.min.css" />
<script type="text/javascript" src="jsDatePick.min.1.3.js"></script>
<script type="text/javascript">
window.onload = function(){
new JsDatePick({
  useMode:2,
  target:"inputField",
  dateFormat:"%Y-%m-%d"
});
new JsDatePick({
  useMode:2,
  target:"inputField1",
  dateFormat:"%Y-%m-%d"
});

};
</script>

<SCRIPT LANGUAGE="JavaScript" SRC="util/PopupWindow.js"></SCRIPT>
<SCRIPT LANGUAGE="JavaScript" SRC="util/AnchorPosition.js"></SCRIPT>
<SCRIPT LANGUAGE="JavaScript" SRC="util/CalendarPopup.js"></SCRIPT>
<SCRIPT LANGUAGE="JavaScript" SRC="util/date.js"></SCRIPT>
<script>
var cal1 = new CalendarPopup();
cal1.showYearNavigation();

var cal2 = new CalendarPopup();
cal2.showYearNavigation();

</script>
<style>
th{
    // background-color: grey;
    font-weight: bold;
    color: white;
    border:1px solid #D7D7D1;
    border-collapse:collapse;
background: rgb(181,189,200); /* Old browsers */
background: -moz-linear-gradient(top, rgba(181,189,200,1) 0%, rgba(130,140,149,1) 36%, rgba(40,52,59,1) 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(181,189,200,1)), color-stop(36%,rgba(130,140,149,1)), color-stop(100%,rgba(40,52,59,1))); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top, rgba(181,189,200,1) 0%,rgba(130,140,149,1) 36%,rgba(40,52,59,1) 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top, rgba(181,189,200,1) 0%,rgba(130,140,149,1) 36%,rgba(40,52,59,1) 100%); /* Opera 11.10+ */
background: -ms-linear-gradient(top, rgba(181,189,200,1) 0%,rgba(130,140,149,1) 36%,rgba(40,52,59,1) 100%); /* IE10+ */
background: linear-gradient(to bottom, rgba(181,189,200,1) 0%,rgba(130,140,149,1) 36%,rgba(40,52,59,1) 100%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#b5bdc8', endColorstr='#28343b',GradientType=0 ); /* IE6-9 */
}
.ro_settled td
{
background: #6bba70; /* Old browsers */
background: -moz-linear-gradient(top, #6bba70 0%, #6bba70 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#6bba70), color-stop(100%,#6bba70)); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top, #6bba70 0%,#6bba70 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top, #6bba70 0%,#6bba70 100%); /* Opera 11.10+ */
background: -ms-linear-gradient(top, #6bba70 0%,#6bba70 100%); /* IE10+ */
background: linear-gradient(to bottom, #6bba70 0%,#6bba70 100%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#6bba70', endColorstr='#6bba70',GradientType=0 ); /* IE6-9 */
color:white;
}
.ro_settl td
{
background: #6bba70; /* Old browsers */
background: -moz-linear-gradient(top, #6bba70 0%, #6bba70 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#6bba70), color-stop(100%,#6bba70)); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top, #6bba70 0%,#6bba70 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top, #6bba70 0%,#6bba70 100%); /* Opera 11.10+ */
background: -ms-linear-gradient(top, #6bba70 0%,#6bba70 100%); /* IE10+ */
background: linear-gradient(to bottom, #6bba70 0%,#6bba70 100%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#6bba70', endColorstr='#6bba70',GradientType=0 ); /* IE6-9 */
color:red;
}
</style>
<body background="back_ground.jpg">
<?php //include('logo2.php'); ?>
<?php //include('includes/top.php'); ?>
<form action='<?print $PHP_SELF?>' name="f1" method='post' >
<br>
<table class="container"><tr><td>
<br>
<center>
<?
$brqry="select brname from branch where brcode='$brcode'";
$brrs=mysqli_query($db,$brqry);
$brrw=mysqli_fetch_array($brrs);
$brnchnm=$brrw[0];
?>

<table class="form" style="width:auto;" >
<tr><th style="background:grey;" colspan='2'>Receipt Allocation</th><input type=hidden name='f_yr' id='f_yr' value='<? echo $fin_year ?>'></tr>
<?
$yeardat=explode('-',$fin_year);
$startingyr=$yeardat[0];
$mgrp="";
?>
<tr><td>Date from </td><td><input type="text" id="ref_date" value="<?echo '01-04-'.$startingyr;?>" name="fromdt" readonly></td>
</tr>
<tr><td> To </td>
<td><input type="text" id="ref_date1" value="<?echo date('d-m-Y');?>" name="todt" readonly></td>
</tr>
<tr><td>Invoice Type</td><td><select name='invtype' id='invtype'>
<option>All</option>
</select></td></tr>
<tr><td colspan='2' align='center'>
<input type='submit' name='show' value='Show' >
</td>
</tr></form>
</table>
<br><br>
<?
if(isset($_POST['show']))
{
    $fromdt=$_REQUEST['fromdt'];
    $todt=$_REQUEST['todt'];
    $custname=$_REQUEST['custname'];
    $invtype=$_REQUEST['invtype'];
    $mfrom_date = $fromdt;
    $mto_date = $todt;
    $fromdt = implode('-', array_reverse(explode('-', $fromdt)));
    $todt = implode('-', array_reverse(explode('-', $todt)));
    print "<table class='report' border='1' style='width: 100%;'>";
    print "<tr><th COLSPAN=16>Allocation Report for the period from <$mfrom_date> to <$mto_date></th>";
    print "<tr><th>SL No.</th><th>Date</th><th>JV No.</th><th>Customer</th><th>Invoice Type & Number</th><th>Amount</th><th>Payment Mage</th><th>Balance Due</th><th>Days</th>";
    if($invtype=='All')
        $qry=mysqli_query($db,"select date,jvno,partic,cramt,j_type,narr from journal2 where date between '$fromdt' and '$todt' and brcode='$brcode' and (j_type = 'Spare Pur') and cramt>0 and partic !='Round Off' order by date,narr");
    else
        $qry=mysqli_query($db,"select date,jvno,partic,dramt,j_type,narr from journal2 where date between '$fromdt' and '$todt' and brcode='$brcode' and (j_type = '$invtype') and dramt>0 and partic !='Round Off' order by date,narr");        
    $i=1;
    while($row=mysqli_fetch_array($qry))
    {
        $custname = $row[2];
        $brres22=mysqli_query($db,"select sum(amtreceived) from allotreceipt_ven where brcode='$brcode' and customer = '$custname' and drjvno = '$row[jvno]'");
		$brrow22=mysqli_fetch_array($brres22);
		
		$minvno = trim(substr($row['narr'],12,20));
        $brres23=mysqli_query($db,"select sum(cramt) from journal2 where brcode='$brcode' and partic = '$custname' and narr = '$row[narr]' and against= 'Spare Pur'");
        $brrow23=mysqli_fetch_array($brres23);
        
		$mdate = substr($row[0],8,2)."-".substr($row[0],5,2)."-".substr($row[0],0,4);
		print "<tr><td align='right'>".$i++."</td><td align='right'>$mdate</td><td align='right'>$row[1]</td><td>$row[2]</td><td>$row[4]-$row[5]</td><td align='right'>$row[3]</td>";
		$received = $brrow22[0]+$brrow23[0];
		$mbal = $row[3]-$received;
        $diff = strtotime(date('Y-m-d')) - strtotime($mdate); 
        $mdays= abs(round($diff / 86400)); 
		print "<td align='right'>".number_format($received,2,".",",")."</td><td align='right'>".number_format($mbal,2,".",",")."</td><td>$mdays</td>";
        print "</tr>";
        $tinvamt+=$row[3];
        $treceived+=$received;
        $tmbal+=$mbal;
    }
    print "<tr><td colspan='5' align='center'><b> T O T A L</td><td align='right'>".number_format($tinvamt,2,".",",")."</td><td align='right'>".number_format($treceived,2,".",",")."</td><td align='right'>".number_format($tmbal,2,".",",")."</td>";
    print "</table>";
}
?>
<script>
function ascdetails(jvn,amt,jvtp)
{
	var jvno=jvn;
	var amount=amt;
	var jvtyp=jvtp;
	window.open("ascreceipt.php?jvno="+jvno+"&amount="+amount+"&typ="+jvtyp,'scrollbars=yes','menubar=0,resizable=1,width=550,height=550');
}
function paiddetails(jvn,amt,jvtp)
{
	var jvno=jvn;
	var amount=amt;
	var jvtyp=jvtp;
	window.open("paidreceipt.php?jvno="+jvno+"&amount="+amount+"&typ="+jvtyp,'scrollbars=yes','menubar=0,resizable=1,width=550,height=550');
}
function sparedetails(jvn,amt,jvtp)
{
	var jvno=jvn;
	var amount=amt;
	var jvtyp=jvtp;
	window.open("sparereceipt.php?jvno="+jvno+"&amount="+amount+"&typ="+jvtyp,'scrollbars=yes','menubar=0,resizable=1,width=550,height=550');
}
function billexc()
    {
   var dfrom=document.f1.fromdt.value;
   var dto=document.f1.todt.value;
   //var url="sale_summ_billwise.php?fromdt="+dfrom+"&&todt="+dto;
   //alert(url);
          document.location="sale_summ_billwise.php?dfrom="+dfrom+"&&dto="+dto,true;
    }
 function conslfn()
    {
        var dfrom=document.f1.fromdt.value;
        var dto=document.f1.todt.value;
        window.open("sale_summ_print.php?fromdt="+dfrom+"&&todt="+dto,true);

    }
  function pdfexc()
        {
            var dfrom=document.f1.fromdt.value;
            var dto=document.f1.todt.value;
            window.open("paypdf/sale_summ_billwise_pdf.php?fromdt="+dfrom+"&&todt="+dto,true);
        }
function dirprint()
    {
            var dfrom=document.f1.fromdt.value;
            var dto=document.f1.todt.value;
   //var url="sale_summ_billwise.php?fromdt="+dfrom+"&&todt="+dto;
   //alert(url);
            document.location="sale_summ_newprn.php?fromdt="+dfrom+"&&todt="+dto,true;
    }
   function print_laser()
    {
            var dfrom=document.f1.fromdt.value;
            var dto=document.f1.todt.value;
   //var url="sale_summ_billwise.php?fromdt="+dfrom+"&&todt="+dto;
   //alert(url);
            window.open("sale_summ_prn.php?fromdt="+dfrom+"&&todt="+dto,true);
    }
</script>