<?
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');
$acqry=mysqli_query($db,"select acfrom from  acperiod");
$acrow=mysqli_fetch_array($acqry);
$acfrmdt=$acrow[0];
?>
<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: "searchcust.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="";
$custname=$_REQUEST['custname'];
$prevyear=$_REQUEST['prevyear'];

if(strlen($prevyear)>0)$chked = 'checked'; else $chked = '';
$chked = 'checked';
?>
<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> Customer </td>
<td><input type="text" id="custname" name="custname" class='orderto' value ="<?echo $custname;?>"></td>
</tr>
<tr><td colspan='2'> <input type="checkbox" id="prevyear" name="prevyear" value="prevyear" <?echo $chked?>>
<label for="prevyear"> Include Previous Year</label><br></td>
</tr>
<tr><td align='center' colspan='2'>
<?if (strlen($custname)) {?>
    <input type='submit' name='show' value='<<Refresh>>' >
<?}else{?>
    <input type='submit' name='show' value='Show' >
<?}?>
</td>
</tr></form>
</table>
<br><br>
<?
if(isset($_POST['show']))
{
    $fromdt=$_REQUEST['fromdt'];
    $todt=$_REQUEST['todt'];
    
    $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>Pending Receipt 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><Details></th><th>Amount</th><th>Invoice Type</th><th></th>";
     if(strlen($custname)<=0)
    {
        $mstr = " and grp like '%Debtors%' ";
        $mstr1 = "";
    }   
    else
    {
        $mstr = " and led='$custname'";
        $mstr1 = " and partic='$custname'";
    }
    //$opqry=mysqli_query($db,"select op,led,grp,drcr,code from ledger where brcode='$brcode' and led='$custname' and op!=0");
    $opqry=mysqli_query($db,"select op,led,grp,drcr,code from ledger where brcode='$brcode' $mstr and op!=0");
    $i=1;
    while($opres=mysqli_fetch_array($opqry))
    {
        $mopbal = $opres['op'];
        if($opres['drcr']=='Cr')
        {
            $mopbal = $opres['op']*-1;
        }
        if($mopbal>=0)
            $mopbal = 0;
        else
            $mopbal = $mopbal * -1;
        //$brres=mysqli_query($db,"select sum(amtreceived) from allotreceipt where brcode='$brcode' and customer = '$custname' and crjvno = 'OP'");
        $brres=mysqli_query($db,"select sum(amtreceived) from allotreceipt where brcode='$brcode' and customer = '$opres[led]' and crjvno = 'OP'");
		$brrow=mysqli_fetch_array($brres);
	    $mamt   = $mopbal-$brrow[0];
	    $totamt = $mamt;
	    if(abs($mamt)>0)
	    {
	        print "<tr><td align='right'>".$i++."</td><td align='right'>$acfrmdt</td><td align='right'>0</td><td>$custname</td><td>OP</td><td align='right'>$totamt</td><td align='center'>OP</td>";
            print "<td><input type='button' name='ascbtn[]' id='ascbtn$i' value='Allocate' onclick=ascdetails('0','$totamt','OP','".$opres['code']."') ></td>";
            print "</tr>";
	    }
    }
    $qry=mysqli_query($db,"select date,jvno,partic,cramt,j_type,narr from journal2 where date between '$fromdt' and '$todt' and brcode='$brcode' $mstr1 and cramt>0 and against!='sp invoice'");
    //$qry=mysqli_query($db,"select date,jvno,partic,cramt,j_type,narr from journal2 where date between '$fromdt' and '$todt' and brcode='$brcode' and partic='$custname' and cramt>0 and against!='sp invoice'");
    //$qry=mysqli_query($db,"select date,jvno,partic,cramt,j_type from journal2 where date between '$fromdt' and '$todt' and brcode='$brcode' and partic='$custname' and cramt>0 ");
    //echo $sql;
    
    while($row=mysqli_fetch_array($qry))
    {
        
        $brres11=mysqli_query($db,"select grp,code from ledger where brcode='$brcode' and led = '$row[2]' ");
		$brrow11=mysqli_fetch_array($brres11);
        if($brrow11[0]=='Sundry Debtors')
        {
        
        
        //$brres22=mysqli_query($db,"select sum(amtreceived) from allotreceipt where brcode='$brcode' and customer = '$custname' and crjvno = '$row[jvno]'");
        $brres22=mysqli_query($db,"select sum(amtreceived) from allotreceipt where brcode='$brcode' and customer = '$row[2]' and crjvno = '$row[jvno]'");
		$brrow22=mysqli_fetch_array($brres22);
		
	    $mamt = $row['cramt']-$brrow22[0];
	    if($mamt>0)
	    {
	        $jtype=$row[4];
	      
	        $jtype=str_replace(" ","",$jtype);
	        
	    $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[5]</td><td align='right'>$mamt</td><td align='center'>$row[4]</td>";
        //$custname_pass = str_replace(" ","|",$custname);
        $custname_pass = str_replace(" ","|",$row[2]);
        print "<td><input type='button' name='ascbtn[]' id='ascbtn$i' value='Allocate' onclick=ascdetails('$row[1]','$mamt','$jtype','".$custname_pass."','$prevyear') ></td>";
        print "</tr>";
	    }
        }
    }
    print "</table>";
}
?>
<script>
function ascdetails(jvn,amt,jvtp,cname,prevyear)
{
	var jvno=jvn;
	var amount=amt;
	var jvtyp=jvtp;
	var cnametyp=cname;
	var prevyeartyp=prevyear;
	window.open("ascreceipt_21102024.php?jvno="+jvno+"&amount="+amount+"&typ="+jvtyp+"&cname="+cnametyp+"&prevyear="+prevyear,'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>