<?
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('includes/top.php');
include('opendb.php');
$custpan = $_REQUEST['custpan'];
$custname = $_REQUEST['custname'];
$custphone = $_REQUEST['custphone'];
$vehbranch = $_REQUEST['vehbranch'];
$fromdate = $_REQUEST['fromdate'];
$todate = $_REQUEST['todate'];
?>
<link href="css/form.css" type="text/css" rel="stylesheet" media="screen" />
<html>
<body >
<center>
<title>Sale 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>
<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>
<script type="text/javascript">

$(function(){
  $(".panno").live("keydown.autocomplete",function(){
  $supnm = $(this);
  $row = $supnm.parent().parent();
  $(this).autocomplete({
    source: function( request, response ) {
    $.ajax({
      url: "search1_sales.php",
      dataType: "json",
      data: "key="+$supnm.val(),
      success: function( data ) {
        response( $.map( data, function( item ) {
          return {
            label:  item.supnm+' | '+ item.partcode+' | '+  item.partname+' | '+  item.stock+' | '+  item.gstno +' | '+  item.rate ,
            value: item.supnm,
            name:item.partname,
            partcode:item.partcode,
            stock:item.stock,
            rate:item.rate,
            gstno:item.gstno,
            }
        }));
      }
    });
  }
  ,
  minLength: 2,
  select: function( event, ui ) {
        document.getElementById("custname").value=ui.item.partcode;
  //$row.find('.partname').val(ui.item.name);
        document.getElementById("custphone").value=ui.item.stock;
  $row.find('.qty').focus();
  /*log( ui.item ?
  "Selected: " + ui.item.label :
  "Nothing selected, input was " + this.value);
  */
   }

  }).data("ui-autocomplete")._renderItem = function (ul, item) {
         return $("<li></li>")
             .data("item.autocomplete", item)
             .append( item.label )
             .appendTo(ul);
     };
  });
  
  $(".custnm").live("keydown.autocomplete",function(){
  $supnm = $(this);
  $row = $supnm.parent().parent();
  $(this).autocomplete({
    source: function( request, response ) {
    $.ajax({
      
      url: "search1_sales.php?type=name",
      dataType: "json",
      data: "key="+$supnm.val(),
      success: function( data ) {
        response( $.map( data, function( item ) {
          return {
            label:  item.supnm+' | '+ item.partcode+' | '+  item.partname+' | '+  item.stock+' | '+  item.gstno +' | '+  item.rate ,
            value: item.partcode,
            name:item.partname,
            supname:item.supnm,
            stock:item.stock,
            rate:item.rate,
            gstno:item.gstno,
            }
        }));
      }
    });
  }
  ,
  minLength: 2,
  select: function( event, ui ) {
        //document.getElementById("custname").value=ui.item.supname;
        document.getElementById("custpan").value=ui.item.supname;
  //$row.find('.partname').val(ui.item.name);
        document.getElementById("custphone").value=ui.item.stock;
  $row.find('.qty').focus();
  /*log( ui.item ?
  "Selected: " + ui.item.label :
  "Nothing selected, input was " + this.value);
  */
   }

  }).data("ui-autocomplete")._renderItem = function (ul, item) {
         return $("<li></li>")
             .data("item.autocomplete", item)
             .append( item.label )
             .appendTo(ul);
     };
  });

  
  
});
</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=6>SALE REPORT</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>PAN</td><td><input type="text" id="custpan"  name="custpan" class='panno' size="50px" value='<?echo $custpan;?>'></td>
<td>Customer Name </td><td><input type="text" id="custname"  name="custname" class='custnm' size="50px" value='<?echo $custname?>' ></td></tr>
<tr><td>Phone</td><td><input type="text" id="custphone"  name="custphone"  size="50px" value='<?echo $custphone?>' >
<tr><td>Branch</td><td>
<select id="vehbranch"  name="vehbranch">
<?
if($vehbranch>'')
    print "<option value='".$vehbranch."'>".$vehbranch."</option>";
?>
<option value='select'>-All-</option>
<option value='LCV-COCHIN'>LCV-COCHIN</option>
<option value='LCV-THRISSUR'>LCV-THRISSUR</option>
<option value='LCV-ALLEPPEY'>LCV-ALLEPPEY</option>
<option value='LCV-PALAKKAD'>LCV-PALAKKAD</option>
<option value='LCV-THRISSUR'>LCV-THRISSUR</option>
<option value='HCV-COCHIN'>HCV-COCHIN</option>
<option value='HCV-THRISSUR'>HCV-THRISSUR</option>
<option value='HCV-ALLEPPEY'>HCV-ALLEPPEY</option>
<option value='HCV-PALAKKAD'>HCV-PALAKKAD</option>
<option value='TERRA-COCHIN'>TERRA-COCHIN</option>
<option value='TERRA-THRISSUR'>TERRA-THRISSUR</option>
<option value='TERRA-ALLEPPEY'>TERRA-ALLEPPEY</option>
<option value='TERRA-PALAKKAD'>TERRA-PALAKKAD</option>
</select></td>
<td>From <input type="date" id="fromdate"  name="fromdate"></td><td>To <input type="date" id="todate"  name="todate"></td>
<tr><th style="background:grey;" colspan=6><input type='submit' name='show' value='Show'></th></tr>
</form>
</table>
<br><br>
<?
$submit = $_REQUEST['show'];
if($submit)
{
    $mstr ='';
    if($custpan>'') $mstr .= " and pan_no like '$custpan%' ";
    if($custname>'') $mstr .= " and customer_name like '$custname%' ";
    if($custphone>'') $mstr .= " and phone like '$custphone%' ";
    
    if($vehbranch!='select')
    {
    if($vehbranch>'') $mstr .= " and branch like '$vehbranch%' ";
    }
    //print "select * from sales_details where brcode='$brcode' $mstr group by sl_no order by sl_no desc";
    $qry=mysqli_query($db,"select * from sales_details where brcode='$brcode' $mstr group by sl_no order by sl_no desc");
    //echo "select* from sales_details where brcode='$brcode' group by sl_no order by sl_no desc";
	
    print "<table class='report' border='1' style='width: 100%;'>";
    
    print "<tr><th>SlNo</th><th>Customer</th><th>Address</th><th>Phone<th>Email</th><th>GST No</th>";
   
    print "<th>PAN</th><th>Configuration</th><th>Invoice Amt</th><th>Payment Details</th>";
    while($row=mysqli_fetch_array($qry))
    {
        print "<tr><td>".$row[2]."</td><td>".$row[3]."</td><td>".$row[4]."</td><td>$row[5]</td><td>$row[6]</td><td>$row[7]</td><td>$row[8]</td><td>$row[9]</td><td align='right'>$row[12]</td>";
       
        print "<td><input type='button' name='updbtn' id='updbtn' value='View' onclick='upddetails($row[0])'></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;

    //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></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></tr>";
    print "</table>";
}
?>
<script>
function calcamt()
{
    if(document.getElementById("totamt").value>0)
    {
        var total=parseFloat(document.getElementById("totamt").value);
    }
    else
    {
        var total=0;
    }
    if(document.getElementById("discamt").value>0)
    {
    var discamt=parseFloat(document.getElementById("discamt").value);
    }
    else
    {
        var discamt=0;
    }
    if(document.getElementById("subamt").value>0)
    {
    var subventamt=parseFloat(document.getElementById("subamt").value);
    }
    else
    {
        var subventamt=0;
    }
    if(document.getElementById("othamt").value>0)
    {
    var otheramt=parseFloat(document.getElementById("othamt").value);
    }
    else
    {
        var otheramt=0;
    }
    if(document.getElementById("refndamt").value>0)
    {
    var refundamt=parseFloat(document.getElementById("refndamt").value);
    }
    else
    {
        var refundamt=0;
    }
    var dueamtvl=total-discamt-subventamt-otheramt+refundamt;
    document.getElementById("dueamt").value=dueamtvl;
}
function upddetails(rcid)
{
    var width = 800;
    var height = 600;
    var left = (screen.width/2)-(width/2);
    var top = (screen.height/2)-(height/2);

    window.open("viewsaledtl.php?recordid="+rcid, "popup",
        "width="+width+",height="+height+",top="+top+",left="+left);
    //window.open("updsalepdetails.php?recordid="+rcid,true);
}
</script>