<? 
session_start();
$brcode = $_SESSION['brcode'];
$user   = $_SESSION['user'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out11'); parent.document.location='../ind.php'; </script>";
 return;
}

include("../opendb.php");
include("../test.php");

//newly added

$us=mysqli_query($db,"select level from bruser where username='$user'")or die(mysqli_error());
$r=mysqli_fetch_row($us);
$user_level=$r[0];
$dt=$y.'-'.$m.'-'.$d;
//print $user_level;
//print $dt;

if($user_level<=4)
{
    $sq="select to_days(curdate())-to_days('$dt')";
    $dtre=mysqli_query($db,$sq)or die(mysqli_error());
    $dtrow=mysqli_fetch_row($dtre);
if($dtrow[0]>3 || $dtrow[0]<0)
{
 print "<script> alert('Invalid Date, Entry possible for last 3 days only'); history.back(1); </script>";
 return;
}
}

$sprint=$_REQUEST['sprint'];
if($sprint)
{ 
    
    $rep=$_REQUEST['rep'];
    $y=$_REQUEST['y'];
    $m=$_REQUEST['m'];
    $d=$_REQUEST['d'];
    $taxp=$_REQUEST['taxp'];
    $tax=$_REQUEST['tax'];
    $amt2=$_REQUEST['amt2'];
    $cessamt=$_REQUEST['cessamt'];
    $formno=$_REQUEST['formno'];
    $netamt=$_REQUEST['netamt'];
    $cust=$_REQUEST['cust'];
    $cust=trim($cust);
    
    $query=mysqli_query($db,"select code from cust_master where acname='$cust' and branch='$brcode'");
    $custrw=mysqli_fetch_array($query);
    $custcod=$custrw[0];
    if($custcod=='')
    {
       
       $supquery=mysqli_query($db,"select code from sup_master where supname='$cust' and branch='$brcode'");
    $suprow=mysqli_fetch_array($supquery); 
    $custcod=$suprow[0];
    }
    $cust=$custcod;
    
    $mcode=$_REQUEST['mcode'];
    $area=$_REQUEST['area'];
    $fin=$_REQUEST['fin'];
    $da=$_REQUEST['da'];
    $config=$_REQUEST['config'];
    $unit=$_REQUEST['unit'];
    $disc=$_REQUEST['disc'];
    $color=$_REQUEST['color'];
    $engno=$_REQUEST['engno'];    
    $segment=$_REQUEST['segment'];
    $chasis=$_REQUEST['chasis'];
    $remark=$_REQUEST['remark'];
    $netamt=$_REQUEST['netamt'];
    $emcode=$_REQUEST['emcode'];
    $tp=$_REQUEST['tp'];
    $cessp=$_REQUEST['cessp'];
    $cessamt=$_REQUEST['cessamt'];
    $taxtype=$_REQUEST['taxtype'];
    $tcsper=$_REQUEST['tcsper'];
    $tcsamt=$_REQUEST['tcsamt'];
    $hsncode=$_REQUEST['hsncode'];
    
    if(strlen($brcode)<=0)
    {
        print "<script> alert('Session timed out'); parent.document.location='../ind.php'; </script>";
        return;
    }
    
    $mmm=mysqli_query($db,"select invno from veh_invoice where invno='$pno' and type !='d' and brcode='$brcode' and taxtype='$taxtype'");
    if(mysqli_num_rows($mmm)>0)
    { 
        
        print "<script> alert('Duplicate invoice no'); history.back(1); </script>"; return;}
       
    $jvr=mysqli_query($db,"select max(jvno) from journal2  where brcode='$brcode'");
    $j_row=mysqli_fetch_row($jvr);
    if($j_row[0]<=0)
        $jvno=1;
    else
        $jvno=($j_row[0]+1);
    $resdn=mysqli_query($db,"select max(jvno) from psnindia_psndbdn.journal2 where brcode='$brcode'");
    $rowdn=mysqli_fetch_row($resdn);
    if($rowdn[0]<=0)
    {
        $jvnodn=1;
    }
    else
    {
        $jvnodn=($rowdn[0]+1);
    }   
    //$re=mysqli_query($db,"select max(invno) from veh_invoice where brcode='$brcode' and formno > '' ")or die(mysqli_error());
    $re=mysqli_query($db,"select max(invno) from veh_invoice where brcode='$brcode' and da> '' ")or die(mysqli_error());
    $inv_row=mysqli_fetch_row($re);
    if($inv_row[0]<=0)
        $invno=375123240001;
    else 
        $invno=$inv_row[0]+1;
    $invdate=$y.'-'.$m.'-'.$d;
    $rr=mysqli_query($db,"select code from rep_master where repname='$rep'");
    $rep_row=mysqli_fetch_row($rr);
    
    $rep=$rep_row[0];
    $cgstp=$taxp/2;
    $sgstp=$taxp/2;
    $igstp=$taxp;
    $cgstamt = $tax/2;
    $sgstamt = $tax/2;
    $igstamt = $tax;
    $ttt=$cessamt+$amt2+$tax;
    if ($formno<='')
        $formno=0;
    if($ttt!=$netamt)
    {
        $dif=($netamt-$ttt);
    }
    
    $supqry=mysqli_query($db,"select code from sup_master where code='$cust'");
    if(mysqli_num_rows($supqry)>0)
    { 
       $area='DEALER';
    }
    
    
    
    
    if($taxtype=='INTRASTATE')
    {
        $sql="insert into veh_invoice (cust,formno,model_code,rep,area,fin,invno,invdate,da,config,unit,disc,netamt,taxp,tax,amt2,color,engno,segment,chasis,remark,brcode,type,jvno,emcode,tp,cessp,cessamt,taxtype,tcsper,tcsamt,cgstp,sgstp,igstp,cgstamt,sgstamt,igstamt,hsncode) values('$cust','$formno','$mcode','$rep','$area', '$fin','$invno', '$invdate','$da','$config','$netamt','$disc','$netamt', '$taxp','$tax', '$amt2','$color','$engno','$segment','$chasis','$remark','$brcode','-','$jvno', '$emcode','$tp','$cessp','$cessamt','$taxtype','$tcsper','$tcsamt','$cgstp','$sgstp','0','$cgstamt','$sgstamt','0','$hsncode')";
        $sqldn="insert into psnindia_psndbdn.veh_invoice (cust,formno,model_code,rep,area,fin,invno,invdate,da,config,unit,disc,netamt,taxp,tax,amt2,color,engno,segment,chasis,remark,brcode,type,jvno,emcode,tp,cessp,cessamt,taxtype,tcsper,tcsamt,cgstp,sgstp,igstp,cgstamt,sgstamt,igstamt,hsncode) values('$cust','$formno','$mcode','$rep','$area', '$fin','$invno', '$invdate','$da','$config','$netamt','$disc','$netamt', '$taxp','$tax', '$amt2','$color','$engno','$segment','$chasis','$remark','$brcode','-','$jvnodn', '$emcode','$tp','$cessp','$cessamt','$taxtype','$tcsper','$tcsamt','$cgstp','$sgstp','0','$cgstamt','$sgstamt','0','$hsncode')";

        
    }
    else
    {
        $sql="insert into veh_invoice (cust,formno,model_code,rep,area,fin,invno,invdate,da,config,unit,disc,netamt,taxp,tax,amt2,color,engno,segment,chasis,remark,brcode,type,jvno,emcode,tp,cessp,cessamt,taxtype,tcsper,tcsamt,cgstp,sgstp,igstp,cgstamt,sgstamt,igstamt,hsncode) values('$cust','$formno','$mcode','$rep','$area', '$fin','$invno', '$invdate','$da','$config','$netamt','$disc','$netamt', '$taxp','$tax', '$amt2','$color','$engno','$segment','$chasis','$remark','$brcode','-','$jvno', '$emcode','$tp','$cessp','$cessamt','$taxtype','$tcsper','$tcsamt','0','0','$igstp','0','0','$igstamt','$hsncode')";
        $sqldn="insert into psnindia_psndbdn.veh_invoice (cust,formno,model_code,rep,area,fin,invno,invdate,da,config,unit,disc,netamt,taxp,tax,amt2,color,engno,segment,chasis,remark,brcode,type,jvno,emcode,tp,cessp,cessamt,taxtype,tcsper,tcsamt,cgstp,sgstp,igstp,cgstamt,sgstamt,igstamt,hsncode) values('$cust','$formno','$mcode','$rep','$area', '$fin','$invno', '$invdate','$da','$config','$netamt','$disc','$netamt', '$taxp','$tax', '$amt2','$color','$engno','$segment','$chasis','$remark','$brcode','-','$jvnodn', '$emcode','$tp','$cessp','$cessamt','$taxtype','$tcsper','$tcsamt','0','0','$igstp','0','0','$igstamt','$hsncode')";
    
        
    }
    
     mysqli_query($db,$sql) or die(mysqli_error());
     mysqli_query($db,$sqldn) or die(mysqli_error());
    
    log_entry($brcode,$user,"$jvno|Veh Invoice|Add","Veh Invoice entry-$da",$db);
    $sql="insert into veh_movement (date,inv_no,qty,type,brcode,sup_type,da)values('$invdate', '$invno','1','CST sale','$brcode','$cust','$da')";
    mysqli_query($db,$sql)or die(mysqli_error());
 //$re=mysqli_query($db,"select * from vehopstock where dano='$da' and brcode='$brcode'");
// if(mysqli_num_rows($re)>0)
// {
    mysqli_query($db,"update vehopstock set sold='$invno' where dano='$da' and brcode='$brcode'");
// }
    $sql="update veh_purchase set sale_invoice='$invno', type='sale' where da='$da' and brcode='$brcode'";
    mysqli_query($db,$sql)or die(mysqli_error());
    mysqli_query($db,"update veh_transin set sale_invoice='$invno' where da='$da' and brcode='$brcode'") or die(mysqli_error());
    $cure=mysqli_query($db,"select code,acname from cust_master where code='$cust'");
    $cust_row=mysqli_fetch_row($cure);
    $cust_name=strtoupper($cust_row[1]);
    if($cust_name=='')
    {
        $cure=mysqli_query($db,"select code,supname from sup_master where code='$cust'");
        $cust_row=mysqli_fetch_row($cure);
        $cust_name=strtoupper($cust_row[1]);
    }
    if($taxtype=='INTERSTATE')
    {
    	$sql="insert into journal2 values('$invdate','$jvno','Dr','$cust_name','$netamt', '0','INTERSTATE Sales INV No: $invno','$brcode','V sale','DA','$da','$invno')";
    	mysqli_query($db,$sql) or die("J V Write error 1:".mysqli_error());
    	$sql="insert into journal2 values('$invdate','$jvno','Cr','IGST Output $taxp%', '0','$tax','INTERSTATE Sales INV No: $invno','$brcode','V sale','DA','$da','$invno')";
    	mysqli_query($db,$sql) or die("J V Write error 2:".mysqli_error());
    	$sql="insert into journal2 values('$invdate','$jvno','Cr','INTERSTATE Sale $taxp%(vehicle)','0','$amt2','INTERSTATE Sales INV No: $invno','$brcode','V sale','DA','$da','$invno')";
    	mysqli_query($db,$sql) or die("J V Write error 3:".mysqli_error());
    	if($tcsper>0)
        {
	        $sql="insert into journal2 values('$invdate','$jvno','Cr','Tax Collected at Source (TCS) @1%','0','$tcsamt','INTERSTATE Sales INV No: $invno','$brcode','V sale','DA','$da','$invno')";
	        mysqli_query($db,$sql) or die("J V Write error 4:".mysqli_error());
		}
		
		$sqldn1="insert into psnindia_psndbdn.journal2 values('$invdate','$jvnodn','Dr','$cust_name','$netamt', '0','INTERSTATE Sales INV No: $invno','$brcode','V sale','DA','$da','$invno')";
    	mysqli_query($db,$sqldn1) or die("J V Write error 1:".mysqli_error());
    	$sqldn2="insert into psnindia_psndbdn.journal2 values('$invdate','$jvnodn','Cr','IGST Output $taxp%', '0','$tax','INTERSTATE Sales INV No: $invno','$brcode','V sale','DA','$da','$invno')";
    	mysqli_query($db,$sqldn2) or die("J V Write error 2:".mysqli_error());
    	$sqldn3="insert into psnindia_psndbdn.journal2 values('$invdate','$jvnodn','Cr','INTERSTATE Sale $taxp%(vehicle)','0','$amt2','INTERSTATE Sales INV No: $invno','$brcode','V sale','DA','$da','$invno')";
    	mysqli_query($db,$sqldn3) or die("J V Write error 3:".mysqli_error());
    	if($tcsper>0)
        {
	        $sqldn4="insert into psnindia_psndbdn.journal2 values('$invdate','$jvnodn','Cr','Tax Collected at Source (TCS) @1%','0','$tcsamt','INTERSTATE Sales INV No: $invno','$brcode','V sale','DA','$da','$invno')";
	        mysqli_query($db,$sqldn4) or die("J V Write error 4:".mysqli_error());
		}

    }// end if cst
    else
    {
    	if($brcode=='AUT009'|| $brcode=='AUT103')
    	{
		    $sql="insert into journal2 values('$invdate','$jvno','Dr','$cust_name','$netamt', '0','Sales INV No: $invno','$brcode','V sale','DA','$da','$invno')";
		    mysqli_query($db,$sql) or die("J V Write error 1:".mysqli_error());
		    $sql="insert into journal2 values('$invdate','$jvno','Cr','Output tax 14.5', '0','$tax','Sales INV No: $invno','$brcode','V sale','DA','$da','$invno')";
		    mysqli_query($db,$sql) or die("J V Write error 2:".mysqli_error());
		    $sql="insert into journal2 values('$invdate','$jvno','Cr','Output Cess', '0','$cessamt','Sales INV No: $invno','$brcode','V sale','DA','$da','$invno')";
		    mysqli_query($db,$sql) or die("J V Write error cess:".mysqli_error());
		    $sql="insert into journal2 values('$invdate','$jvno','Cr','Sales 14.5(vehicle)','0','$amt2','Sales INV No: $invno','$brcode','V sale','DA','$da','$invno')";
		    mysqli_query($db,$sql) or die("J V Write error 3:".mysqli_error());
	    }
	    else
	    {
    		$sql="insert into journal2 values('$invdate','$jvno','Dr','$cust_name','$netamt', '0','Sales INV No: $invno','$brcode','V sale','DA','$da','$invno')";
		    mysqli_query($db,$sql) or die("J V Write error 1:".mysqli_error());
		    if($brcode=='PSN672')
		    {
    			$sql="insert into journal2 values('$invdate','$jvno','Cr','Output tax 5', '0','$tax','Sales INV No: $invno','$brcode','V sale','DA','$da','$invno')";
			    mysqli_query($db,$sql) or die("J V Write error 2:".mysqli_error());
		    }
		    else
		    {
    			$cgstamt = $tax /2;
                $cgstp = $taxp/2;    			
			    $sql="insert into journal2 values('$invdate','$jvno','Cr','CGST Output $cgstp%', '0','$cgstamt','Sales INV No: $invno','$brcode','V sale','DA','$da','$invno')";
			    mysqli_query($db,$sql) or die("J V Write error 2:".mysqli_error());
			    $sql="insert into journal2 values('$invdate','$jvno','Cr','SGST Output $cgstp%', '0','$cgstamt','Sales INV No: $invno','$brcode','V sale','DA','$da','$invno')";
			    mysqli_query($db,$sql) or die("J V Write error 2:".mysqli_error());
    		}
    		if($cessamt>0)
    		{
		        $sql="insert into journal2 values('$invdate','$jvno','Cr','Kerala Flood Cess @ 1%', '0','$cessamt','Sales INV No: $invno','$brcode','V sale','DA','$da','$invno')";
		        mysqli_query($db,$sql) or die("J V Write error cess:".mysqli_error());
    		}
		    $sql="insert into journal2 values('$invdate','$jvno','Cr','Sales $taxp%(vehicle)','0','$amt2','Sales INV No: $invno','$brcode','V sale','DA','$da','$invno')";
		    mysqli_query($db,$sql) or die("J V Write error 3:".mysqli_error());
	        if($tcsper>0)
            {
    			$sql="insert into journal2 values('$invdate','$jvno','Cr','Tax Collected at Source (TCS) @1%','0','$tcsamt','Sales INV No: $invno','$brcode','V sale','DA','$da','$invno')";
			    mysqli_query($db,$sql) or die("J V Write error 3:".mysqli_error());
        	}
        	
        	$sqldn5="insert into psnindia_psndbdn.journal2 values('$invdate','$jvnodn','Dr','$cust_name','$netamt', '0','Sales INV No: $invno','$brcode','V sale','DA','$da','$invno')";
		    mysqli_query($db,$sqldn5) or die("J V Write error 1:".mysqli_error());
		    if($brcode=='PSN672')
		    {
    			$sqldn6="insert into psnindia_psndbdn.journal2 values('$invdate','$jvnodn','Cr','Output tax 5', '0','$tax','Sales INV No: $invno','$brcode','V sale','DA','$da','$invno')";
			    mysqli_query($db,$sqldn6) or die("J V Write error 2:".mysqli_error());
		    }
		    else
		    {
    			$cgstamt = $tax /2;
                $cgstp = $taxp/2;    			
			    $sqldn7="insert into psnindia_psndbdn.journal2 values('$invdate','$jvnodn','Cr','CGST Output $cgstp%', '0','$cgstamt','Sales INV No: $invno','$brcode','V sale','DA','$da','$invno')";
			    mysqli_query($db,$sqldn7) or die("J V Write error 2:".mysqli_error());
			    $sqldn8="insert into psnindia_psndbdn.journal2 values('$invdate','$jvnodn','Cr','SGST Output $cgstp%', '0','$cgstamt','Sales INV No: $invno','$brcode','V sale','DA','$da','$invno')";
			    mysqli_query($db,$sqldn8) or die("J V Write error 2:".mysqli_error());
    		}
    		if($cessamt>0)
    		{
		        $sqldn9="insert into psnindia_psndbdn.journal2 values('$invdate','$jvnodn','Cr','Kerala Flood Cess @ 1%', '0','$cessamt','Sales INV No: $invno','$brcode','V sale','DA','$da','$invno')";
		        mysqli_query($db,$sqldn9) or die("J V Write error cess:".mysqli_error());
    		}
		    $sqldn10="insert into psnindia_psndbdn.journal2 values('$invdate','$jvnodn','Cr','Sales $taxp%(vehicle)','0','$amt2','Sales INV No: $invno','$brcode','V sale','DA','$da','$invno')";
		    mysqli_query($db,$sqldn10) or die("J V Write error 3:".mysqli_error());
	        if($tcsper>0)
            {
    			$sqldn11="insert into psnindia_psndbdn.journal2 values('$invdate','$jvnodn','Cr','Tax Collected at Source (TCS) @1%','0','$tcsamt','Sales INV No: $invno','$brcode','V sale','DA','$da','$invno')";
			    mysqli_query($db,$sqldn11) or die("J V Write error 3:".mysqli_error());
        	}
        	
        	
        	
        	
	    }
	    $jvd=mysqli_query($db,"select sum(dramt)-sum(cramt) from journal2 where jvno='$jvno' and brcode='$brcode'");
	    $difrow=mysqli_fetch_array($jvd);
	    $dif=$difrow[0];
	    $x=abs($dif);
	    if($dif<0)
	    {
    		$sql="insert into journal2 values('$invdate','$jvno','Dr','Round Off','$x', '0','Round ofSINV No: $invno','$brcode','V sale','DA','$da','$invno')";
		    mysqli_query($db,$sql) or die("J V Write error dif1:".mysqli_error());
		    $sqldn12="insert into psnindia_psndbdn.journal2 values('$invdate','$jvnodn','Dr','Round Off','$x', '0','Round ofSINV No: $invno','$brcode','V sale','DA','$da','$invno')";
		    mysqli_query($db,$sqldn12) or die("J V Write error dif1:".mysqli_error());
		    
	    }
	    else if($dif>0)
	    {
    		$sql="insert into journal2 values('$invdate','$jvno','Cr','Round Off','0','$x','Round of adjusted SINV No: $invno','$brcode','V sale','DA','$da','$invno')";
		    mysqli_query($db,$sql) or die("J V Write error dif:".mysqli_error());
		    $sqldn13="insert into psnindia_psndbdn.journal2 values('$invdate','$jvnodn','Cr','Round Off','0','$x','Round of adjusted SINV No: $invno','$brcode','V sale','DA','$da','$invno')";
		    mysqli_query($db,$sqldn13) or die("J V Write error dif:".mysqli_error());
	    }
    }// end else cst
    //if($cessp>0)
        print "<script>window.open('prnvehinvoice22_gstcess.php?invno=$invno&da=$da');</script>";
    //else        
      //  print "<script>window.open('prnvehinvoice22_gst.php?invno=$invno&da=$da');</script>"
      
      
} // end save

?>
<link type="text/css" href="../jquery/jquery-ui.min.css" rel="stylesheet" />	
<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" src="../jquery/jquery.jqEasyCharCounter.min.js"></script>
<style>
select,input,body,td{font-family:'arial narrow';letter-spacing:1;font-size:13}
tr{background-color:c1d7bb}
.mnu{margin-left:5}
</style>
<body bgcolor='ivory'>
  <script type="text/javascript">
    var cur=1;
	 var prevcur=0;
	 var gst='';
	 var marginpr='';
	 var custstt='';
	 var shipdistvl='';
	 var shippinvl='';
$(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,
							adr:item.price,
							gst:item.supqty,
							marginpr:item.mrgnprc,
						    custstt:item.custstt,
						    shipdistvl:item.shidist,
						    shippinvl:item.shipin,

					}
				}));
			}
		});
	}
	
,
	minLength: 2,
	select: function( event, ui ) {

	$row.find('.orderto').val(ui.item.orderto);
	var cusstr=ui.item.code+','+ui.item.adr+','+ui.item.gst+','+marginpr+','+custstt;
    cusstr=cusstr.trim();
	document.f1.addr.value=cusstr;

	
	//document.f1.msno.focus();
	/*log( ui.item ?
	"Selected: " + ui.item.label :
	"Nothing selected, input was " + this.value);
	*/
	 }
	
	});

					
});











});
</script>    
    
    
    
    
    
    
<script>
var adr=[];
var ar1=[]; 
var config=[];
var config2=[];
var model=[];
var rep=[];
var area=[];
var color=[];
var engno=[];
var chasis=[];
var em_code=[];
var cess_amt=[];
var hsncode=[];
var tax_perc=[];
function fun1()
{
    var x=document.f1.da.selectedIndex; 
    if(x<=0)
    {
        document.f1.mcode.value='';
        document.f1.config.value='';
    }
    else
    { 
        document.f1.mcode.value=ar1[document.f1.da.selectedIndex];
        document.f1.config.value=config[document.f1.da.selectedIndex];
        document.f1.color.value=color[document.f1.da.selectedIndex];
        document.f1.engno.value=engno[document.f1.da.selectedIndex];
        document.f1.chasis.value=chasis[document.f1.da.selectedIndex];
        document.f1.emcode.value=em_code[document.f1.da.selectedIndex];
        document.f1.cessp.value=cess_amt[document.f1.da.selectedIndex];
        document.f1.hsncode.value=hsncode[document.f1.da.selectedIndex];
        document.f1.taxp.value=tax_perc[document.f1.da.selectedIndex];
    }
    //if(document.f1.model.value != document.f1.mcode.value)
    //{
      //  alert('Model code doesn\'t match with order form');
        //document.f1.mcode.value='';
        //document.f1.config.value='';
        //document.f1.emcode.value= '';  
    //}
}
function chmcode()
{ 
    document.f1.config.value=config2[document.f1.mcode.selectedIndex];
if(document.f1.model.value != document.f1.mcode.value)
   {
       alert('Model code doesn\'t match with order form');
       document.f1.mcode.value='';
       document.f1.config.value='';
      document.f1.emcode.value= '';  
   }
}
function valid()
{
 submitOK="true";
 if(document.f1.invno.value.length<=0)
 {
   submitOK="false";
   alert('Invalid Invoice No.');
 }
 if(document.f1.cust.value.length<=0)
 {
   submitOK="false";
   alert('Invalid Customer');
 }
 if(document.f1.fin.selectedIndex<=0)
 {
   submitOK="false";
   alert('Invalid Financer');
 }
/*
 if(document.f1.formno.seletedIndex<=0)
 {
   submitOK="false";
   alert('Invalid Formno');
 }
 */
 if(document.f1.unit.value.length<=0)
 {
   submitOK="false";
   alert('Invalid unit price');
 }
 if(document.f1.segment.selectedIndex<=0)
 {
   submitOK="false";
   alert('Invalid Segment No.');
 }



var day1=document.f1.d.value;
var mon1=document.f1.m.value;
var year1=document.f1.y.value;
var chdt=day1+"/"+mon1+"/"+year1;

var tempdt=year1+"-"+mon1+"-"+day1;
var tempact=document.f1.act.value;
var tempacf=document.f1.acf.value;


function ValidateDate() 
{
 
    var msg = '';
 
    this.isDate = function(date) 
     {
 
        var explodeData = date.explode("/");
        var day = explodeData[0];
        var month = explodeData[1];
        var year = explodeData[2];
 
        var thisMonth = new Date(year, month, 0);
        var daysInMonth = thisMonth.getDate();
        var pattern = /^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/;
 
        if (!pattern.test(date)) {
            msg = "invalid date format";
        }
        else if (day > daysInMonth) {
            msg = "invalid day";
        }
        else if (month > 12) {
            msg = "invalid month";
        }
 
        if (msg) {
            return false;
        } else {
            return true;
        }
    }
 
    this.getMsg = function() 
    {
 
        return msg;
    }
}

 
 vDate = new ValidateDate();
 
if (vDate.isDate(chdt))

 {
if(tempdt>'2016-05-31')
{

//submitOK="false";
//document.f1.m.focus();

//alert('date should be on 31-05-2016');


}

if(tempdt>=tempacf&&tempdt<=tempact)
{

//alert('hii');

}
else
{
submitOK="false";

document.f1.m.focus();

alert('date should be in between current Financial Year');

}

 //alert("Date is valid");
 }
else 

{
 submitOK="false";

document.f1.d.focus();
alert("Date is not valid: " + vDate.getMsg()); 
}




if(submitOK=="true")
{
 x=confirm("Do you want to save?");
 if(x)
   submitOK="true";
 else
    submitOK="false";
}
  if(submitOK=="true")
   return true;
  else
   return false;
}

function chkey(abc)
{
 yy=abc.value
 if(yy.indexOf(' ')>-1) 
  {
    alert('Space is not allowed');
    abc.value='';
  }
}

function setadr()
{
  var x= document.f1.cust.selectedIndex ; 
  if(x>0)
    document.f1.addr.value=adr[document.f1.cust.selectedIndex];
  else
    document.f1.addr.value='';
}
function setModel()
{
  var x= document.f1.formno.selectedIndex ; 
  if(x>0)
    {
     document.f1.model.value=model[document.f1.formno.selectedIndex];
     document.f1.rep.value=rep[document.f1.formno.selectedIndex];
     document.f1.area.value=area[document.f1.formno.selectedIndex];

   }
  else
    {
      document.f1.model.value='';
     document.f1.rep.value='';
     document.f1.area.value='';
   }

}
 
function setttype()
{
 if(document.f1.taxtype.selectedIndex==1)
  {
    document.f1.taxp.value=18;
    document.f1.cessp.value=0;
    document.f1.cessamt.value=0;

 }
 else
 {
  document.f1.cessp.value=1;
    document.f1.taxp.value=18;
 }

 calc();
}
</script>


<style>
body,td{font-family:'arial narrow';letter-spacing:1;}
th{background-color:green;color:white}
.mnu{margin-left:5}
</style>
<body bgcolor='ivory'>


<form name='f1' action='<?print $PHP_SELF?>' method='POST' onSubmit="return valid()">
<table cellspacing=1 cellpadding=1 bgcolor=black>
<tr><td align=center bgcolor='GRAY'><FONT COLOR='IVORY'><B>CUSTOMER DETAILS</TD></TR>
<TR><td align=center bgcolor='ivory'>
<table>
<tr><td><font color=red>Customer</td><td colspan=4> <input type='text' name='cust' id='orderto' placeholder='Type Customer Name Here' class='orderto' size=100>


 </td></tr>
 <?
 //echo "select a.formno,a.model,b.repname,b.dist from orderform a,rep_master b where a.rname=b.code and a.dt>='2017-10-01' and a.formno not in (select formno from veh_invoice where brcode='$brcode') and a.brcode='$brcode'";
 ?>
<tr>
<td>Address </td><td><textarea name='addr' cols=40></textarea></td>
<td><font color=red>Form No</td><td> <select name='formno' onChange='setModel()'>
<option></option>
<?
$rr=mysqli_query($db,"select a.formno,a.model,b.repname,b.dist from orderform a,rep_master b where a.rname=b.code and a.dt>='2017-10-01' and a.formno not in (select formno from veh_invoice where brcode='$brcode') and a.brcode='$brcode'")or die(mysqli_error());
 $i=1;
while($row=mysqli_fetch_row($rr))
{
 print "\n<option>$row[0]</option>";
 print "\n<script> model[$i]='$row[1]' </script>";
 print "\n<script> rep[$i]='$row[2]' </script>";
 print "\n<script> area[$i]='$row[3]' </script>";

 $i++;
}
?>
</select> 

<!-- <input type=text readonly name='model'>-->
</td></tr>
<tr><td>Rep<td><input type=text readonly name='rep'></td></tr>
<tr> <td>Area </td><td><input type=text name='area' readonly>
</td>

<td>Mod Code</td><td><input type=text name='model' readonly></td></tr>
<tr><td><font color=red>Financer</font> </td><td colspan=3> <select name='fin'> <option></option>
<?
 $re=mysqli_query($db,"select code,finname from fin_master where branch='$brcode' order by finname");
  while($row=mysqli_fetch_row($re))
  {
   print "<option value='$row[1]'>$row[1]</option>";
  }
?>
</select>
</td></tr>
</table>
 </td></tr>

</table>




<br>
<CENTER>
<table cellspacing=1 cellpadding=1 bgcolor=green>
<tr  bgColor='c1d7bb'><td align=center>
<table border=1 cellspacing=1 cellpadding=2>
<tr bgColor='c1d7bb'><td colspan=7 align=center bgcolor='green'>
<font color='ivory'><STRONG>VEHICLE SALES  (Inv No:

<?
$rs=mysqli_query($db,"select max(invno) from veh_invoice where brcode='$brcode' and da > ''")or die(mysqli_error());
 $p_row=mysqli_fetch_row($rs);
 if(mysqli_num_rows($rs)<=0)
  $pno=1;
 else
    $pno=$p_row[0]+1;
print $pno.')';
?>
<input type=hidden name='pno' value='<?print $pno?>'>
</TD></TR>
<tr>

</tr>
<tr>
<td>Invoice No </td><td><input type=text  name='invno' value='<?print $pno?>' size=15 style={background-color:silver}></td>
<td>Invoice Date </td><td>dd <input type=text name='d' size=2 value='<?print date("d",time())?>'>
mm <input type=text name='m' size=2 value='<?print date("m",time())?>'>
yy <input type=text name='y' size=4 value='<?print date("Y",time())?>'>
</td>
<td>Tax Type</td><td><select name='taxtype' onChange='setttype()'><option>INTRASTATE</option><option>INTERSTATE</option></select>
</td>
</tr>
<tr><td>DA </td><td>
<select name='da' onChange=fun1()>
<option></option>
<?
$i=1;
$result=mysqli_query($db,"select * from veh_purchase where sale_invoice='-' and  brcode='$brcode' order by da")or die(mysqli_error());
while($row=mysqli_fetch_array($result))
{
    print "\n<option>$row[da]</option>";
    print "\n<script> ar1[$i]='$row[mcode]';\n config[$i]='EICHER $row[config]'; \n";
    print "color[$i]='$row[color]'; \n engno[$i]='$row[engno]'; \n chasis[$i]='$row[chasis]';";
    print "em_code[$i]='$row[emcode]';";
    print "cess_amt[$i]='$row[cessp]';";
    print "tax_perc[$i]='$row[taxp]';";
    print "hsncode[$i]='$row[hsncode]'; </script>";
    $i++;
}
$result=mysqli_query($db,"select * from vehopstock where brcode='$brcode' and sold='-' order by dano")or die(mysqli_error());
while($row=mysqli_fetch_array($result))
{
    print "\n<option>$row[dano]</option>";
    print "\n<script> ar1[$i]='$row[mcode]';\n config[$i]='$row[config]'; \n";
    print "color[$i]='$row[color]'; \n engno[$i]='$row[engno]'; \n chasis[$i]='$row[chno]';\n cess_amt[$i]='0'; </script>";
    $i++;
}
?>
</select>
</td>
<td>Model Code </td><td><select name='mcode' onChange=chmcode()>
<option></option>
<?
$rr=mysqli_query($db,"select * from veh_master where brcode='$brcode' order by mcode");
$k=1;
while($rrow=mysqli_fetch_array($rr))
{
 print "<option value='$rrow[1]'>$rrow[1]-$rrow[2]</option>";
 print "\n<script>config2[$k]='$rrow[2]';</script>";
 $k++;
}
?>
</select>

</td><td>Eicher M.Code</td><td><input type=text name='emcode' size=10></td>
</tr>
<tr><td>config </td><td colspan=5><input type=text name='config' size=93></td></tr>
<tr><td><font color=red>Unit Price </td><td><input type=text name='unit'  size=8 onChange=calc() > </td>
    <td>Discount </td><td><input type=text name='disc' SIZE=8 value='0' onChange=calc()>Amount Before Tax <input type=text   name='amt2' onChange=calc() ></td>

<tr>
    <td>Tax %</td>

<?
if($brcode=='AUT009' || $brcode=='AUT103')
{
?>
	<td><input type=text  name='taxp' size=5 value='18' onChange=calc()>
<?
}
elseif ($brcode == 'PSN705')
{
?>
	<td><input type=text  name='taxp' size=5 value='18' onChange=calc()>
<?
}
else
{
?>
	<td colspan=3><input type=text  name='taxp' size=5 value='18' onChange=calc()>
<?
}
?>
 

<input type=text name='tax' size=10 readonly value='0'>

Cess % 
<?
if($brcode=='AUT009' || $brcode=='AUT103')
{
?>
<input type=text name='cessp' value='0' size=1 onChange=calc()>
<?
}
else
{
?>
<input type=text name='cessp' value='0'  size=3 onChange=calc()>
<?
}

?>
Cess Amt <input type=text name='cessamt' value='0' readonly size=10>
TCS Per <input type=text name='tcsper' value='0'  size=4 onChange=calc()>
TCS Amount <input type=text name='tcsamt' readonly  size=10>
</td>
    </tr>
    
    <tr><td>Net Amount</td><td><input type=text readonly name='netamt' onChange=calc()></td><td></td><td></td></tr>
<tr><td>Color</td>
<td><input type=text name='color'></td><td>Engine No</td><td><input type=text readonly name='engno'>HSN Code :<input type=text name='hsncode' size=13></td></tr>
<tr>
<td><font color=red>Segment </td><td colspan=5><select name='segment'><option></option>
<?
 $re=mysqli_query($db,"select * from seg_master order by sname");
 while($row=mysqli_fetch_row($re))
 {
   print "<option>$row[1]</option>";
 }
?>
</select></td>
</tr>
<tr><td>Chasis No</td><td><input type=text readonly name='chasis'></td>
<td>Remarks </td><td colspan=3><textarea name='remark' cols=55></textarea></td></tr>
<tr><td colspan=6 align=center bgcolor='green'> 
<font color=white size=4>
<input type=radio name='tp' value='8'>8A
<input type=radio name='tp' value='8b' checked=true>8B
</font>
<input type=submit  value=' SAVE AND PRINT BILL ' name='sprint'>


<!--<input type=BUTTON value='  SAVE AND ADD ITEMS  ' name='svadd'> -->
<input type=button value='  CANCEL  ' onClick=history.back(1)></td></tr>

<?
//new
$acpd=mysqli_query($db,"select * from acperiod") or die ('err'.mysqli_error());
$acrow=mysqli_fetch_row($acpd);


?>
<tr><td><input type=hidden name='acf' value='<?print $acrow[0]?>'></td>
<td><input type=hidden name='act' value='<?print $acrow[1]?>'></td>
</tr>

</table>
</form>
</td></tr>
</table>
</center>
<br>
<script>
function calc()
{
var taxper=parseFloat(document.f1.taxp.value);
document.f1.amt2.value=document.f1.unit.value-document.f1.disc.value;
var cessper=parseFloat(document.f1.cessp.value).toFixed(0);
//net=parseFloat(document.f1.netamt.value);

//amt=((net*100)/(100+taxper));
/*
if(net >= 1000000)
{
	if(document.f1.tcsper.value>0)
	{
	    ces_p = 0;
	    if(parseFloat(document.f1.cessp.value)>0)
	    {
	        ces_p = 1.01;
	        amt=(net/(1+(28.95748895+ces_p)/100));
	    }
	    else
	    {
	        amt=(net/(1+(28.95997785)/100));
	    //amt=(net/(1+(29.28+ces_p)/100));
	        //amt=(net/(1+(28.95748895+ces_p)/100));
	    }
	    //alert(amt);
	}
	else
	{
		//alert(parseFloat(document.f1.cessp.value));
		amt=(net/(1+(parseFloat(document.f1.taxp.value)+parseFloat(document.f1.cessp.value))/100));
			
		document.f1.tcsper.value=0;
	}
}
else
{
	amt=(net/(1+(parseFloat(document.f1.taxp.value)+parseFloat(document.f1.cessp.value))/100));
	document.f1.tcsper.value=0;
}
*/
//document.f1.amt2.value=parseFloat(amt).toFixed(2);

tax=parseFloat(document.f1.amt2.value)*parseFloat(document.f1.taxp.value)/100;

document.f1.tax.value=(tax).toFixed(2);
var cessper=parseFloat(document.f1.cessp.value).toFixed(0);
var ces_amt=parseFloat((document.f1.amt2.value*cessper)/100);
document.f1.cessamt.value=ces_amt.toFixed(2);
var tcsamt=(parseFloat(document.f1.amt2.value)+parseFloat(document.f1.tax.value)+ces_amt)*parseFloat(document.f1.tcsper.value)/100;
document.f1.tcsamt.value=tcsamt.toFixed(2);
document.f1.netamt.value=(parseFloat(document.f1.amt2.value)+parseFloat(document.f1.tax.value)+parseFloat(document.f1.cessamt.value)+parseFloat(document.f1.tcsamt.value)).toFixed(2);
document.f1.netamt.value=Math.round(document.f1.netamt.value);

}




function calc_tcs()
{
var tcsamt=(parseFloat(document.f1.amt2.value)+parseFloat(document.f1.tax.value))*parseFloat(document.f1.tcsper.value)/100;
document.f1.tcsamt.value=tcsamt.toFixed(2);
//document.f1.netamt.value=(parseFloat(document.f1.netamt.value)+tcsamt);
}

</script>