<?
session_start();
include_once('top.php');
include_once('../opendb.php');
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
//$br=$_SESSION['branch'];
if(strlen($user)<=0)
{
  print "<script> alert('Invalid User'); document.location='inde.php' </script>";
  return;
}
if(isset($_POST['del']))
{
    //$code=$_REQUEST['code'];
    //$sql="delete from dispatchinvoice where brcode='$brcode' and invno = '$code'";
    //print $sql;
    //exit;
    //mysqli_query($db,$sql) or die(mysqli_error($db));
    
    //print "<script> alert('Data has been deleted !!!');document.location='dispatch.php';</script>";

}

if(isset($_POST['save']))
{
	$dt=date("Y-m-d");
	$invno=$_REQUEST['grp'];
	$custval=$_REQUEST['custval'];
	$invdate=$_REQUEST['invdate'];
	$boxvl=$_REQUEST['boxvl'];
	$dispdate=$_REQUEST['dispdate'];
	$transportnm=$_REQUEST['transportnm'];
	$transportnm2=$_REQUEST['transportnm2'];
    $transportnm=trim($transportnm);
    $vehicleno=$_REQUEST['vehicleno'];
	$lrno=$_REQUEST['lrno'];
	$lrdate=$_REQUEST['lrdate'];
	$lramount=$_REQUEST['lramount'];
	$pay_type = $_REQUEST['paytype'];
	if($pay_type == 'paid'){
	    $paytype = "Paid";
	}else if($pay_type == "topay"){
	    $paytype = "To Pay";
	}else{
	    $paytype = "-";
	}
	$qry=mysqli_query($db,"select * from dispatchinvoice where invno='$invno' and brcode='$brcode'");
 	if(mysqli_num_rows($qry)>0)
 	{
   		print "<script> alert('Could not save: Duplicate invoice'); document.location='dispatch.php'</script>";
 	}
	else
	{
	    $sq="select max(invno) from dispatchinvoice where brcode='$brcode'";
        $re=mysqli_query($db,$sq);
        $jcres=mysqli_query($db,"select max(invno) from dispatchinvoice where brcode='$brcode'");
        $jcrow=mysqli_fetch_row($jcres);
        if($jcrow[0]<=0)
            $jcno=1;
        else
            $jcno=($jcrow[0]+1);
  
        $query=mysqli_query($db,"select invno,entrydate,customer,sum(aprice) aprice,sum(cgstamt)cgstamt,sum(sgstamt)sgstamt,sum(cessamt)cess ,sum(item_total)tot, '','','','',sum(igstamt)igstamt from sap_einvoice  where brcode='$brcode' and invno='$invno' group by invno");
	    $sprrow=mysqli_fetch_array($query);
		$sqlqry="insert into dispatchinvoice(invno,invtype ,invdate,customer,addr,tin,tot,brcode,ref,engineer,mnfr_code,disp_date,box,trans_name,veh_no,lrno,lrdate,lramount,trans_name2,paytype) values('$sprrow[invno]','$sprrow[invtype]','$sprrow[entrydate]','$sprrow[customer]','$addr','$tin','$sprrow[tot]', '$brcode','$ref','$sprrow[engineer]','$mnfr','$dispdate','$boxvl','$transportnm','$vehicleno','$lrno','$lrdate','$lramount','$transportnm2','$paytype')";
        print 		$sqlqry;
        mysqli_query($db,$sqlqry)or die('Invoice Err:'.mysqli_error());
	    print "<script> document.location='dispatch.php'</script>";
		return;
	}
} // end save




?>
<!DOCTYPE html>
<html><head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<title>Partmaster</title>






<link rel="stylesheet" href="../css/form.css" type="text/css" />  
<link rel="stylesheet" href="../style.css" type="text/css" />
<link type="text/css" href="../css/redmond/jquery-ui-1.8.16.custom.css" rel="stylesheet" />	
<script type="text/javascript" src="../js/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="../js/jquery-ui-1.8.16.custom.min.js"></script>
<script type="text/javascript">
  $(function() {
    $( "#dispdate" ).datepicker({ dateFormat: 'yy-mm-dd' });
    $( "#lrdate" ).datepicker({ dateFormat: 'yy-mm-dd' });
  });
  </script>
















<script type="text/javascript">
$(document).ready(function(){

    $("select.grp").change(function(){

        var selectedGrp = $(".grp option:selected").val();

        $.ajax({

            type: "POST",

            url: "selectsubgrp.php",

            data: { grp : selectedGrp } 

        }).done(function(data){

            $("#response").html(data);

        });

    });

});


</script>

<!-- js code below for active enter with textfield-->


<script type="text/javascript">

/***********************************************
* Disable "Enter" key in Form script
* This notice must stay intact for use
***********************************************/
                
function handleEnter (field, event) {
		var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
		if (keyCode == 13) {
			var i;
			for (i = 0; i < field.form.elements.length; i++)
				if (field == field.form.elements[i])
					break;
			i = (i + 1) % field.form.elements.length;
			field.form.elements[i].focus();
			return false;
		} 
		else
		return true;
	}      








</script>

<style>
#txt{
font-family: Arial, Helvetica;
background-color: #F2F2F2;
}
table.form{
font-weight: normal;border:1px;
} 
</style>
<style>
.cont{
      background-color:#fff;
      //background:url(images/bg-inner.jpg) #e9e0c3 repeat-x;
      width: 100%;position: absolute ;
      height:99%;
      /* Optional - just for DIV)*/
      //border: solid 1px #000000;
      -moz-box-shadow: 0px 5px 5px #888;
      -webkit-box-shadow: 0px 5px 5px #888;
      box-shadow: 0px 5px 10px #888;
    
      }


#pg{
       text-decoration:none;
}
#pg button{
       background-color:#3b5998;
}

</style>



<body bgcolor="#A9F5A9">
<div class='cont' style="background-color:#E0F8E0" >



<?
include('top.php');

?>
<font style="font-family: 'Trebuchet MS',Verdana,Helvetica,sans-serif; font-size:15px; color:white;">  &nbsp;&nbsp;Master >> Item Master</font>

<style>
th{background-color:steelblue;color:white}
</style>
<script>
function valid()
{
 
 if(document.f1.pcode.value.length<=0)
 {
   alert("Invalid Itemcode.");
   document.f1.pcode.focus();
   return false;
 }

if(document.f1.pname.value.length<=0)
 {
   alert("Invalid ItemName.");
   document.f1.pname.focus();
   return false;
 }
if(document.f1.grp.value=='--select--')
	{
		alert("Invalid Group.");
		document.f1.grp.focus();
		return false;

	}
if(document.f1.subgrp.value=='--select--')
	{
		alert("Invalid SubGroup.");
		document.f1.subgrp.focus();
		return false;

	}

if(document.f1.srate.value.length<=0)
 {
   alert("Invalid SaleRate.");
   document.f1.srate.focus();
   return false;
 }
if(document.f1.prate.value.length<=0)
 {
   alert("Invalid Purchase Rate.");
   document.f1.prate.focus();
   return false;
 }



}
</script>
<script type="text/javascript">

/***********************************************
* Disable "Enter" key in Form script- By Nurul Fadilah(nurul@REMOVETHISvolmedia.com)
* This notice must stay intact for use
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/
                
function handleEnter (field, event) {
		var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
		if (keyCode == 13) {
			var i;
			for (i = 0; i < field.form.elements.length; i++)
				if (field == field.form.elements[i])
					break;
			i = (i + 1) % field.form.elements.length;
			field.form.elements[i].focus();
			return false;
		} 
		else
		return true;
	}      


</script>
<script type="text/javascript">
$(function(){
$(".partcode").live("keydown.autocomplete",function(){
$partcode = $(this);
$row = $partcode.parent().parent();
$(this).autocomplete({
		source: function( request, response ) {
		$.ajax({
			url: "searchinv.php",
			dataType: "json",
			data: "key="+$partcode.val(),
			success: function( data ) {
				response( $.map( data, function( item ) {
					return {
						label: item.partcode+' | '+  item.partname  +' | '+  item.date   ,
						value: item.partcode,
						name:item.partname,
						qty:item.supqty,
						price:item.price,
					}
				}));
			}
		});
	}
,
	minLength: 2,
	select: function( event, ui ) {
	$row.find('.partname1').val(ui.item.name);
	$row.find('.supqty').val(ui.item.qty);
	$row.find('.rate').val(ui.item.price);
	 }
	});
});
$(".partname").live("keydown.autocomplete",function(){
$partcode = $(this);
$row = $partcode.parent().parent();
$(this).autocomplete({
		source: function( request, response ) {
		$.ajax({
			url: "searchinv.php?type=name",
			dataType: "json",
			data: "key="+$partcode.val(),
			success: function( data ) {
				response( $.map( data, function( item ) {
					return {
						label: item.partcode+' | '+  item.partname+' | '+  item.date    ,
						value: item.partcode,
						code:item.brcode,
					}
				}));
			}
		});
	}
,
	minLength: 2,
	select: function( event, ui ) {

	$row.find('.partcode1').val(ui.item.acname);
	$row.find('.supqty').val(ui.item.brcode);
	$row.find('.rate').val(ui.item.price);
	 }
	});
});
});
</script>
<?
$xx=$_GET['id']+1;
?>
<body>
<center>
<table cellspacing=1 cellpadding=1 bgcolor=black>
<tr><td align=center bgcolor='white'>
<form name='f1' action='<?print $PHP_SELF?>' method='post' onSubmit="return valid()">
<table cellspacing=1 cellpadding=3 class='form'>
<th colspan=4><font size=3>DISPATCH ENTRY</th>
<tr><td>Invoice Number</td><td><input type=text name='grp' class='partcode' onchange='getinvno(this.value)'></td>
    

 <td>Box <input type=text id='boxvl' name='boxvl' ></td>
<td>Dispatch Date <input type=text id='dispdate' name='dispdate' ></td>  
</tr>

<tr>
  <td>Prim. Transporter</td><td><input type=text id='transportnm' name='transportnm' ></td>
<td>Sec. Transporter <input type=text id='transportnm2' name='transportnm2' ></td><td>Vehicle No <input type=text id='vehicleno' name='vehicleno' ></td>  
</tr>
<tr>
  <td>LR No</td><td><input type=text id='lrno' name='lrno' ></td>
<td>LR Date<input type=text id='lrdate' name='lrdate'></td><td>LR Amount<input type=text id='lramount' name='lramount'></td>
</tr>
<tr>
    <td>Payment Type</td>
    <td>
        <select id="paytype" name="paytype">
            <option value="">Select Type</option>
            <option value="paid">Paid</option>
            <option value="topay">To Pay</option>
        </select>    
    </td>
</tr>
<th colspan=4><input type=submit value='Save' name='save'>
    <th colspan=4><input type=submit value='Show List' name='show'>    
</table>
<br><br>
<table cellspacing=1 cellpadding=3 class='form'>
<tr>
<?
if($_REQUEST['show'])
{
    $g=mysqli_query($db,"select disp_date,invdate,invno,customer,trans_name,trans_name2,box,veh_no,lrno,lrdate,lramount,paytype from dispatchinvoice where brcode = '$brcode' and disp_date >'2025-03-31' order by disp_date desc ");
    print "<th>Dispatch Date</th><th>Invoice No.</th><th>Invoice Date</th><th>Customer</th><th>Prim. Transporter</th><th>Sec. Transporter</th><th>Boxes</th><th>Vehicle No.</th><th>LR No.</th><th>LR Date</th><th>LR Amount</th><th>Pay Type</th>";
    while($grow=mysqli_fetch_array($g))
    {
        $df=implode('-', array_reverse(explode('-', $grow['disp_date'])));
        $invdt=implode('-', array_reverse(explode('-', $grow['invdate'])));
        print "<tr><td>$df</td><td>$grow[invno]</td><td>$invdt</td><td>$grow[customer]</td><td>$grow[trans_name]</td><td>$grow[trans_name2]</td><td>$grow[box]</td><td>$grow[veh_no]</td><td>$grow[lrno]</td><td>$grow[lrdate]</td><td align='right'>$grow[lramount]</td><td>$grow[paytype]</td><td><input type=button value='Edit' name='edit' onClick=edfun($grow[invno])><input type=submit value='Del' name='del'></td><input type=hidden name='code' value='$grow[invno]'></tr>";
    } 
}
?>
</tr>    
</table>
</form>
</td></tr>
</table>

<script type="text/javascript">
function getinvno(str)
{
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
	if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {        
		document.getElementById("strtxt").innerHTML=xmlhttp.responseText;
      
    }
  }
xmlhttp.open("GET","showinvdtls.php?invno="+str,true);
xmlhttp.send();

}

function edfun(x)
 {
  if (confirm('do you want to edit '+x+' ?'))
	 {
		document.location='editdispatch.php?code='+x; 
	 }
 }
</script>
