<?
include_once('includes/top.php');
include_once('opendb.php');
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
$fromdt=$_REQUEST['fromdt'];
$todt=$_REQUEST['todt'];

//$br=$_SESSION['branch'];
if(strlen($user)<=0)
{
  print "<script> alert('Invalid User'); document.location='inde.php' </script>";
  return;
}

?>
<link href="css/form.css" type="text/css" rel="stylesheet" media="screen"  />
<html>
<body >

<title>Sale Summary Report</title>
<link rel="stylesheet" href="style.css" type="text/css" />
<script type="text/javascript" src="scripts/jquery-latest.js"></script>
<link type="text/css" href="includes/style1.css" rel="stylesheet" />
<link rel="stylesheet" href="cal/jquery-ui.css" />
<script src="cal/jquery-1.9.1.js"></script>
<script src="cal/jquery-ui1.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<link type="text/css" href="css/form.css" rel="stylesheet" />
<script type="text/javascript">
$(function() {
$( "#ref_date" ).datepicker("destroy");
     var fyr=document.getElementById('f_yr').value;
     var splv=fyr.explode("-");
     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.explode("-");
     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>
<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;" align='center' >
<tr><th style="background:grey;">SALES SUMMARY REPORT BILLWISE</th></tr>
<tr><td><input type=hidden name='f_yr' id='f_yr' value='<? echo $fin_year ?>'>
<? if($fromdt > 0){?>
Show Report From <input type="text" id="ref_date" value="<?echo $fromdt;?>" name="fromdt" readonly  >
<?} else {?>
Show Report From <input type="text" id="ref_date" value="<?echo date('d-m-Y');?>" name="fromdt" readonly  >
<?}?>
<? if($todt > 0){?>
 To <input type="text" id="ref_date1" name="todt" value="<?echo $todt;?>" readonly > </td></tr>
<?} else {?>
To <input type="text" id="ref_date1" name="todt" value="<?echo date('d-m-Y');?>" readonly > </td></tr>
<?}?>
<tr><td align='center'>
<input type='submit' name='show' value='Show' >
<input type=button value="Print Report" onclick=conslfn()> <input type=button value="Excel" onclick=billexc()><input type=button value="PDF" onclick=pdfexc()><input type=button value="Print" onclick=dirprint()><input type=button value="Print(Laser)" onclick=print_laser()></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)));
  //$tbl='sales_summ'.$brcode.$user;
  //mysqli_query($db,"delete from $tbl");
  //mysqli_query($db,"create table $tbl(gname varchar(50),total double(14,2),mba double(14,2),strbtnml double(14,2),strbtspl double(14,2),redct double(14,2),befrtx double(14,2),taxamt double(14,2),nettot double(14,2),dbttot double(14,2))");

    $qry=mysqli_query($db,"select gname,invdate,sum(total),sum(mba),sum(strbtnml),sum(strbtspl),(sum(redct)+sum(total*staffdisc/100)),sum(befrtx),sum(taxamt),sum(nettot),invno,pmode from prod_invoice where invdate between '$fromdt' and '$todt' and brcode='$brcode' group by invno,gname");
  print "<table class='report' border='1'>";

  print "<tr><th COLSPAN=16>KERALA KHADI & VILLAGE INDUSTRIES BOARD</th></tr>";
  print "<tr><th COLSPAN=16>CENTER NAME : $brnchnm </th></tr>";
  print "<tr><th COLSPAN=16>Sales Summary for the period from <$mfrom_date> to <$mto_date></th>";
  print "<tr><th>BillNo</th><th>Cotton</th><th>Poly</th><th>Reeled<br>Silk</th><th>Spun<br>Silk</th><th>VIP</th><th>Woollen</th><th>Others</th><th>MDA</th><th>State Rebate<br> Normal</th><th>State Rebate <br>Special</th><th>Reduction</th><th>Net Cash</th><th>Tax</th><th>Total Amount</th><th>Debtors</th>";
    $colspan=6;
  $mstr[]='';
  $i=1;
  $mgname = '.';
  $mtot_cot = 0;
  $mtot_pol = 0;
  $mtot_rel = 0;
  $mtot_spu = 0;
  $mtot_vip = 0;
  $mtot_woo = 0;
  $mtot_oth = 0;
  $mtot_total = 0;
  $mtot_mba= 0;
  $mtot_strbtnml = 0;
  $mtot_strbtspl = 0;
  $mtot_redct = 0;
  $mtot_befrtx = 0;
  $mtot_taxamt = 0;
  $mtot_nettot = 0;
  $mtot_dbtrs = 0;
  $mgr_total = 0;
  $mgr_cot = 0;
  $mgr_pol = 0;
  $mgr_rel = 0;
  $mgr_spu = 0;
  $mgr_vip = 0;
  $mgr_woo = 0;
  $mgr_oth = 0;
  $mgr_mba= 0;
  $mgr_strbtnml = 0;
  $mgr_strbtspl = 0;
  $mgr_redct = 0;
  $mgr_befrtx = 0;
  $mgr_taxamt = 0;
  $mgr_nettot = 0;
  $mgr_dbtrs = 0;

  $ctr=0;
  $mgr_dbtrs=0; $mgr_dbtrs1=0;
  $minvno = '';
  while($row=mysqli_fetch_array($qry))
    {
    if($minvno <>  $row[10] && $ctr <> 0)
    {
        print "<tr><td>$minvno</td><td align='right'>".number_format($mtot_cot,2,'.','')."</td><td align='right'>".number_format($mtot_pol,2,'.','')."</td><td align='right'>".number_format($mtot_rel,2,'.','')."</td><td align='right'>".number_format($mtot_spu,2,'.','')."</td><td align='right'>".number_format($mtot_vip,2,'.','')."</td><td align='right'>".number_format($mtot_woo,2,'.','')."</td><td align='right'>".number_format($mtot_oth,2,'.','')."</td><td align='right'>".number_format($mtot_mba,2,'.','')."</td><td align='right'>".number_format($mtot_strbtnml,2,'.','')."</td><td align='right'>".number_format($mtot_strbtspl,2,'.','')."</td><td align='right'>".number_format($mtot_redct,2,'.','')."</td><td align='right'>".number_format($mtot_befrtx,2,'.','')."</td><td align='right'>".number_format($mtot_taxamt,2,'.','')."</td><td align='right'>".number_format($mtot_nettot,2,'.','')."</td><td align='right'>".number_format($mtot_dbtrs, 2, '.', '')."</td></tr>";
        $mgr_cot +=  $mtot_cot;
        $mgr_pol +=  $mtot_pol;
        $mgr_rel +=  $mtot_rel;
        $mgr_spu +=  $mtot_spu;
        $mgr_vip +=  $mtot_vip;
        $mgr_woo +=  $mtot_woo;
        $mgr_oth +=  $mtot_oth;
        $mgr_mba+=  $mtot_mba;
        $mgr_strbtnml +=  $mtot_strbtnml;
        $mgr_strbtspl +=  $mtot_strbtspl;
        $mgr_redct +=  $mtot_redct;
        $mgr_befrtx +=  $mtot_befrtx;
        $mgr_taxamt +=  $mtot_taxamt;
        $mgr_nettot +=  $mtot_nettot;
        $mgr_dbtrs +=  $mtot_dbtrs;
        $mtot_cot = 0;
        $mtot_pol = 0;
        $mtot_rel = 0;
        $mtot_spu = 0;
        $mtot_vip = 0;
        $mtot_woo = 0;
        $mtot_oth = 0;
        $mtot_mba= 0;
        $mtot_strbtnml = 0;
        $mtot_strbtspl = 0;
        $mtot_redct = 0;
        $mtot_befrtx = 0;
        $mtot_taxamt = 0;
        $mtot_nettot = 0;
        $mtot_dbtrs = 0;
    }
    $minvno = $row[10];
    ++$ctr;
    $date = $row[1];
        // *** getting staff discount from (Especially for Bed)
        $mstaffdisc_amt = 0;
        $mdummy=0;
        $rbt_tbl=mysqli_query($db,"select item_code,qty,staffdisc,total from prod_invoice where brcode = '$brcode' and invno = '$row[10]' and gname = '$row[0]' and staffdisc > 0");
        while($rrow=mysqli_fetch_array($rbt_tbl))
        {
             $rbt_val=mysqli_query($db,"select rbtbl,rbtvl from partmaster where partcode = '$rrow[0]'");
             $row22=mysqli_fetch_row($rbt_val);
             if($row22[0]=='Yes')
                  $mstaffdisc_amt += ($row22[1]*$rrow[1])*$rrow[2]/100;
             else
             {
                  $mstaffdisc_amt += $rrow[3]*$rrow[2]/100;
                  $mdummy +=  $rrow[3]*$rrow[2]/100;
             }
        }
        $mval = $mstaffdisc_amt;
    if($row[11]=='CREDIT')
    {
      //$dbtrs=$row[9];
                        $dbtrs=$row[2]-($row[3]+$row[4]+$row[5]+$mval);
      $cshvl=0;
      //$mtot_dbtrs+=$row[9];
                        $mtot_dbtrs+=$row[2]-($row[3]+$row[4]+$row[5]+$mval)+$row[8];
    }
    else
    {
      $dbtrs=0;
      $cshvl=$row[2]-($row[3]+$row[4]+$row[5]+$row[6]);
      $mtot_nettot += $row[2]-($row[3]+$row[4]+$row[5]+$row[6])+$row[8];

    }
    if($row[0]== 'COTTON')
      $mtot_cot += $row[2];
    else if ($row[0]== 'POLYSTER')
      $mtot_pol += $row[2];
    else if ($row[0]== 'REELED SILK')
      $mtot_rel += $row[2];
    else if ($row[0]== 'SPUNSILK')
      $mtot_spu += $row[2];
    else if ($row[0]== '0% TAXABLE VIP' || $row[0]== '5% TAXABLE VIP' || $row[0]== '14.5% TAXABLE VIP' || $row[0]== '1% TAXABLE VIP')
      $mtot_vip += $row[2];
    else if ($row[0]== 'WOOLEN')
      $mtot_woo += $row[2];
    else
      $mtot_oth += $row[2];
    $mtot_mba+= $row[3];
    $mtot_strbtnml +=$row[4];
    $mtot_strbtspl +=$row[5];
    $mtot_redct +=$mval;
    $mtot_befrtx +=$row[2]-($row[3]+$row[4]+$row[5]+$mval);
    $mtot_taxamt +=$row[8];
    $i++;
  }
  $mgr_cot +=  $mtot_cot;
  $mgr_pol +=  $mtot_pol;
  $mgr_rel +=  $mtot_rel;
  $mgr_spu +=  $mtot_spu;
  $mgr_vip +=  $mtot_vip;
  $mgr_woo +=  $mtot_woo;
  $mgr_oth +=  $mtot_oth;
  $mgr_mba+=  $mtot_mba;
  $mgr_strbtnml +=  $mtot_strbtnml;
  $mgr_strbtspl +=  $mtot_strbtspl;
  $mgr_redct +=  $mtot_redct;
  $mgr_befrtx +=  $mtot_befrtx;
  $mgr_taxamt +=  $mtot_taxamt;
  $mgr_nettot +=  $mtot_nettot;
  $mgr_dbtrs +=  $mtot_dbtrs;

  print "<tr><td>$minvno</td><td align='right'>".number_format($mtot_cot,2,'.','')."</td><td align='right'>".number_format($mtot_pol,2,'.','')."</td><td align='right'>".number_format($mtot_rel,2,'.','')."</td><td align='right'>".number_format($mtot_spu,2,'.','')."</td><td align='right'>".number_format($mtot_vip,2,'.','')."</td><td align='right'>".number_format($mtot_woo,2,'.','')."</td><td align='right'>".number_format($mtot_oth,2,'.','')."</td><td align='right'>".number_format($mtot_mba,2,'.','')."</td><td align='right'>".number_format($mtot_strbtnml,2,'.','')."</td><td align='right'>".number_format($mtot_strbtspl,2,'.','')."</td><td align='right'>".number_format($mtot_redct,2,'.','')."</td><td align='right'>".number_format($mtot_befrtx,2,'.','')."</td><td align='right'>".number_format($mtot_taxamt,2,'.','')."</td><td align='right'>".number_format($mtot_nettot,2,'.','')."</td><td align='right'>".number_format($mtot_dbtrs, 2, '.', '')."</td></tr>";
  print "<tr><td><b>Grand Total</b></td><td align='right'><b>".number_format($mgr_cot,2)."</b></td><td align='right'><b>".number_format($mgr_pol,2)."</b></td><td align='right'><b>".number_format($mgr_rel,2)."</b></td><td align='right'><b>".number_format($mgr_spu,2)."</b></td><td align='right'><b>".number_format($mgr_vip,2)."</b></td><td align='right'><b>".number_format($mgr_woo,2)."</b></td><td align='right'><b>".number_format($mgr_oth,2)."</b></td><td align='right'><b>".number_format($mgr_mba,2)."</b></td><td align='right'><b>".number_format($mgr_strbtnml,2)."</b></td><td align='right'><b>".number_format($mgr_strbtspl,2)."</b></td><td align='right'><b>".number_format($mgr_redct,2)."</b></td><td align='right'><b>".number_format($mgr_befrtx,2)."</b></td><td align='right'><b>".number_format($mgr_taxamt,2)."</b></td><td align='right'><b>".number_format($mgr_nettot,2)."</b></td><td align='right'><b>".number_format($mgr_dbtrs,2)."</b></td></tr>";
  print "</table>";

}
?>
<script>
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>