<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../index.html'; </script>";
 return;
}
include("../opendb.php");
include("../test.php");
if(isset($_POST['Generate']))
{
    ?>
    <style>
    td,th,body{font-size:14;font-family:'arial narrow';letter-spacing:1}
    </style>
    <center>
    <?
    $d1=$_REQUEST['d1'];
    $m1=$_REQUEST['m1'];
    $y1=$_REQUEST['y1'];
    $dfrom=$y1.'-'.$m1.'-'.$d1;
    $asonres=mysqli_query($db,"select count(brcode) from dailycash where asondate = '$dfrom' ");
    $asonrow=mysqli_fetch_array($asonres);
    //print "select count(brcode) from dailycash where asondate = '$dfrom' ".$asonrow;
    if($asonrow[0]<=0)
    {
        $invre=mysqli_query($db,"select brcode,brname from branch where brman > 0");
        while($invrow=mysqli_fetch_array($invre))
        {
            $brres=mysqli_query($db,"select op,drcr,led from ledger where brcode='$invrow[0]' and `grp` LIKE '%CASH%'");
            $mbal=0;
            while($brrow=mysqli_fetch_array($brres))
            {  
                if($brrow[1]=='Dr')
                    $mbal += $brrow[0];
                else
                    $mbal += -1*$brrow[0];
                $jrres=mysqli_query($db,"select sum(dramt),sum(cramt) from journal2 where brcode='$invrow[0]' and partic = '$brrow[2]' and date <= '$dfrom' ");
                $jrrow=mysqli_fetch_array($jrres);
                $mbal += $jrrow[0]-$jrrow[1];
            }    
            date_default_timezone_set('Asia/Kolkata');
            $mdate=date("Y-m-d H:i:s");
            
            $daires=mysqli_query($db,"select * from dailycashupld where brcode='$invrow[0]' and date = '$dfrom' ");
            $dairow=mysqli_fetch_array($daires);
            if(strlen($dairow[0])>0)
                mysqli_query($db,"insert into dailycash(brcode,division,brname,datetime,syscashbal,asondate,curr_2000,curr_500,curr_200,curr_100,curr_50,curr_20,curr_10,coins) values ('$invrow[0]','EICHER','$invrow[1]','$mdate','$mbal','$dfrom','$dairow[5]','$dairow[6]','$dairow[7]','$dairow[8]','$dairow[9]','$dairow[10]','$dairow[11]','$dairow[12]')") or die(mysqli_error($db));
            else
                mysqli_query($db,"insert into dailycash(brcode,division,brname,datetime,syscashbal,asondate) values ('$invrow[0]','EICHER','$invrow[1]','$mdate','$mbal','$dfrom')") or die(mysqli_error($db));
            
        }
    
        
        
    }
    else
    {
        print "Already Generated:.....!";
    }
    
    ?>
    <table>
    <TR><TD COLSPAN=2 ALIGN=CENTER><b> PSN AUTOMOTIVE MARKETING P LTD </b><BR>
	 <?print $brrow[braddr].",".$brrow[brcity].",<br><u> PH - ".$brrow[brphone]?></u>
    TIN: <?print $brrow[tin]?>, CST: <?print $brrow[cstno]?>&nbsp;
    </td></tr>
    <tr><td><input type='button' name='printbtn' value='Print' onclick="opnurl()"></td></tr>
    <tr><td COLSPAN=2 ALIGN=CENTER>
    <?print "<h4><br>CASH BALANCE AS ON ".datetohtml($dfrom);
    print "<tr><th>Branch</th><th>Manual Cash</th><th>System Cash</th><th>Variance</th><th>Br.InCharge Sign</th><th>Br.Acc.Sign</th><th>2000</th><th>500</th><th>200</th><th>100</th><th>50</th><th>20</th><th>10</th><th>Coins</th><th>Total</th><th>Short/ Excess</th><th>Remarks</th><th>Uploaded</th>";
    print "<form id='category' name='category' method='POST' action='updbal.php'>";
    print "<input type='hidden' name='datefrm' id='datefrm' value='$dfrom'>";
    $asres=mysqli_query($db,"select * from dailycash where asondate = '$dfrom'");
    $mbal=0;
    $i=1;
    while($asrow=mysqli_fetch_array($asres))
    {
        $slres=mysqli_query($db,"select * from dailycashupld where brcode='$asrow[1]' and date = '$asrow[5]'");
        $slrow=mysqli_fetch_array($slres);   
        $mphoto = "No";
        $mfile="";
        if($slrow[0]>0)
        {
            $mphoto = "Yes";
            $mfile  = "http://psnauto.in/amplcash/upldcash/$asrow[1]_$asrow[5].jpg";
        }
        print "<tr><td>$asrow[brname]</td><td><input type='text' name='manualcsh[]' id='manualcsh$i' value='$asrow[phycashbal]' size='10px' onKeyup='calculate1(this.value,this,this.id)'></td><td><input type='text' name='systmcsh[]' id='systmcsh$i' size='10px' value='$asrow[syscashbal]' readonly></td><td><input type='text' name='variance[]' id='variance$i' value='$asrow[variange]' readonly size=5px></td><td><select name='brnchinsign[]' id='brnchinsign$i'><option value='Yes'>Yes</option><option value='No'>No</option></select></td><td><select name='brnchaccntsign[]' id='brnchaccntsign$i'><option value='Yes'>Yes</option><option value='No'>No</option></select></td>";
        print "<td><input type='text' name='twothsndcount[]' id='twothsndcount$i' value='$asrow[curr_2000]' size='4' onKeyup='calculate1(this.value,this,this.id)'></td><td><input type='text' name='fivehndrdcount[]' id='fivehndrdcount$i' value='$asrow[curr_500]' size='4' onKeyup='calculate1(this.value,this,this.id)'></td><td><input type='text' name='twohndrdcount[]' id='twohndrdcount$i' value='$asrow[curr_200]' size='4' onKeyup='calculate1(this.value,this,this.id)'></td><td><input type='text' name='hndrdcount[]' id='hndrdcount$i' value='$asrow[curr_100]' size='4' onKeyup='calculate1(this.value,this,this.id)'></td>";
        print "<td><input type='text' name='fiftycount[]' id='fiftycount$i' value='$asrow[curr_50]' size='4' onKeyup='calculate1(this.value,this,this.id)'></td><td><input type='text' name='twentycount[]' id='twentycount$i' value='$asrow[curr_20]' size='4' onKeyup='calculate1(this.value,this,this.id)'></td>";
        print "<td><input type='text' name='tencount[]' id='tencount$i' value='$asrow[curr_10]' size='4' onKeyup='calculate1(this.value,this,this.id)'></td><td><input type='text' name='coincount[]' id='coincount$i' value='$asrow[coins]' size='4' onKeyup='calculate1(this.value,this,this.id)'></td><td><input type='text' name='total[]' id='total$i' value='$asrow[curr_total]' size='4'  readonly></td><td><input type='text' name='shrtexcs[]' id='shrtexcs$i' readonly value='$asrow[short_excess]' size=4></td><td><input type='text' name='remarks[]' id='remarks$i' value='$asrow[remarks]'><input type='hidden' name='rwid[]' id='rwid$i' value='$asrow[id]'></td><td>$mphoto";
        if($mphoto=='Yes'){
            print "<img src='$mfile' width=50px height=50px onClick=window.open('viewimage.php?imgid=$asrow[id]','mywindow','menubar=0,resizable=1,width=1150,height=950')>";
            }
        print "</td>";
        $i++;
    }
    ?>
    </td></tr>
    <tr><td></td><td><input type='submit' value='Update'></td></tr>
    </table>
    </form>
    <?
}// end if
?>

<link rel="stylesheet" href="../style.css" type="text/css" />
<style>
//	tr{background-color:white}
	th{background-color:steelblue;color:white}
	td{font-size:13}
.right{text-align:right};
</style>

<center>
<h3>GENERATE CASH BALANCE</H3>
<form action='<?Print $PHP_SELF?>' method='post'>
<table cellspacing=1 cellpadding=1 bgcolor='steelblue'>
<tr><td bgcolor='white' align=center>
<table class=noprint>
<tr><td>As on Date</td><td>dd <input type=text name='d1' size=2 value='<?print date("d")?>'>
mm <input type=text name='m1' size=2 value='<?print date("m")?>'>
yy <input type=text name='y1' size=4 value='<?print date("Y")?>'></td></tr>

<tr><td colspan=2 align=center bgcolor='lightsteelblue'><input type=submit value='Generate' name='Generate'></td></tr>

</table>
</td></tr>
</table>
</form>
<script>
function calculate1(x,xx,z)
{//y--;
    var thenum = z.match(/\d+$/)[0];
    var y=thenum;
 var qele='manualcsh'+y;  
 var ratele='systmcsh'+y;
 var varele='variance'+y;
 var twothsndele='twothsndcount'+y;
 var fivehndrdele='fivehndrdcount'+y;
 var twohndrdele='twohndrdcount'+y;
 var hndrdele='hndrdcount'+y;
 var fiftyele='fiftycount'+y;
 var twentyele='twentycount'+y;
 var tenele='tencount'+y;
 var coincountele='coincount'+y;
 var totalele='total'+y;
 var shrtexele='shrtexcs'+y;
 
 var amount=parseFloat(document.getElementById(qele).value)-parseFloat(document.getElementById(ratele).value);
 document.getElementById(varele).value=amount;
 if(parseFloat(document.getElementById(twothsndele).value)>0)
 {
     var twothosandvl=parseFloat(document.getElementById(twothsndele).value)*2000;
 }
 else
 {
     var twothosandvl=0;
     
 }
 if(parseFloat(document.getElementById(fivehndrdele).value)>0)
 {
     var fivehndrdvl=parseFloat(document.getElementById(fivehndrdele).value)*500;
 }
 else
 {
     var fivehndrdvl=0;
     
 }
 if(parseFloat(document.getElementById(twohndrdele).value)>0)
 {
     var twohndrdvl=parseFloat(document.getElementById(twohndrdele).value)*200;
 }
 else
 {
     var twohndrdvl=0;
     
 }
  if(parseFloat(document.getElementById(hndrdele).value)>0)
 {
     var hndrdvl=parseFloat(document.getElementById(hndrdele).value)*100;
 }
 else
 {
     var hndrdvl=0;
     
 }
 if(parseFloat(document.getElementById(fiftyele).value)>0)
 {
     var fiftyvl=parseFloat(document.getElementById(fiftyele).value)*50;
 }
 else
 {
     var fiftyvl=0;
     
 }
 if(parseFloat(document.getElementById(twentyele).value)>0)
 {
     var twentyvl=parseFloat(document.getElementById(twentyele).value)*20;
 }
 else
 {
     var twentyvl=0;
     
 }
 if(parseFloat(document.getElementById(tenele).value)>0)
 {
     var tenvl=parseFloat(document.getElementById(tenele).value)*10;
 }
 else
 {
     var tenvl=0;
     
 }
 document.getElementById(totalele).value=twothosandvl+fivehndrdvl+twohndrdvl+hndrdvl+fiftyvl+twentyvl+tenvl+parseFloat(document.getElementById(coincountele).value);
 document.getElementById(shrtexele).value=parseFloat(document.getElementById(totalele).value)-parseFloat(document.getElementById(qele).value);
}
function opnurl()
{
    var datefrm=document.getElementById('datefrm').value;
    window.open("printcbal.php?dfrom="+datefrm);
}
</script>