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

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


if($sprint)
{ 

//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;

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;
}
}

//
 $re=mysqli_query($db,"select max(invno) from veh_invoice where brcode='$brcode' and taxtype='$taxtype'")or die(mysqli_error());
 $inv_row=mysqli_fetch_row($re);
 if($inv_row[0]<=0)
  $invno=1;
 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];

$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);


  $sql="insert into veh_invoice values('$cust','$formno','$mcode','$rep','$area', '$fin','$invno', '$invdate','$da','$config','$unit','$disc','$netamt', '$taxp','$tax', '$amt2','$color','$engno','$segment','$chasis','$remark','$brcode','d','$jvno','$emcode','8','$cessp', '$cessamt','$taxtype')";
 mysqli_query($db,$sql) or die(mysqli_error());
log_entry($brcode,$user,$REMOTE_ADDR,"Veh Invoice entry dealer-$da");
 $sql="insert into veh_movement (date,inv_no,qty,type,brcode,sup_type,da)values('$invdate', '$invno','1','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());


 $cust_name=$cust;
if($taxtype=='CST')
{
$sql="insert into journal2 values('$invdate','$jvno','Dr','$cust_name','$netamt', '0','DLR Sales INV No: $invno','$brcode','V sale(DLR)','DA','$da','$invno')";
mysqli_query($db,$sql) or die("J V Write error 1:".mysqli_error());

$sql="insert into journal2 values('$invdate','$jvno','Cr','CST $taxp%', '0','$tax','DLR Sales INV No: $invno','$brcode','V sale(DLR)','DA','$da','$invno')";
mysqli_query($db,$sql) or die("J V Write error 2:".mysqli_error());

 $sql="insert into journal2 values('$invdate','$jvno','Cr','CST Sale $taxp%(vehicle)','0','$amt2','Sales INV No: $invno','$brcode','V sale(DLR)','DA','$da','$invno')";
mysqli_query($db,$sql) or die("J V Write error 3:".mysqli_error());

}
if($taxtype=='VAT' && ($brcode=='AUT009'|| $brcode=='AUT103'))
{
$sql="insert into journal2 values('$invdate','$jvno','Dr','$cust_name','$netamt', '0','DLR Sales INV No: $invno','$brcode','V sale(DLR)','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','DLR Sales INV No: $invno','$brcode','V sale(DLR)','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','DLR Sales INV No: $invno','$brcode','V sale(DLR)','DA','$da','$invno')";
mysqli_query($db,$sql) or die("J V Write error 3:".mysqli_error());
}
if($taxtype=='VAT' && $brcode!='AUT009' && $brcode!='AUT103')
{
$sql="insert into journal2 values('$invdate','$jvno','Dr','$cust_name','$netamt', '0','DLR Sales INV No: $invno','$brcode','V sale(DLR)','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 13.5', '0','$tax','DLR Sales INV No: $invno','$brcode','V sale(DLR)','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 13.5(vehicle)','0','$amt2','DLR Sales INV No: $invno','$brcode','V sale(DLR)','DA','$da','$invno')";
mysqli_query($db,$sql) 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(DLR)','DA','$da','$invno')";
mysqli_query($db,$sql) 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(DLR)','DA','$da','$invno')";
mysqli_query($db,$sql) or die("J V Write error dif:".mysqli_error());
}
if($taxtype=='VAT')
 print "<script>window.open('prnvehinvoice.php?invno=$invno&da=$da&typ=d');</script>";
else
 print "<script>window.open('prnvehinvoice_cst.php?invno=$invno&da=$da&typ=d');</script>";


print "<font color=green> Data has been saved</font>";
} // end save

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

<script>
var adr=[];
var ar1=[];
var config=[];
var model=[];
var rep=[];
var area=[];
var color=[];
var engno=[];
var chasis=[];

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];

  }
/*
  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='';
   }
*/
}

function valid()
{
 submitOK="true";
 

if(document.f1.da.value.length<=0)
 {
   submitOK="false";
   document.f1.da.focus();
   alert('Invalid DA');
 }


 if(document.f1.unit.value.length<=0)
 {
   submitOK="false";
   document.f1.unit.focus();
   alert('Invalid unit price');
 }

if(document.f1.netamt.value.length<=0)
 {
   submitOK="false";
   document.f1.netamt.focus();
   alert('Invalid net amount');
 }


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>=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(document.f1.segment.value.length<=0)
 {
   submitOK="false";
   alert('Invalid Segment No.');
 }*/

  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 setttype()
{
 if(document.f1.taxtype.selectedIndex==1)
  {
    document.f1.taxp.value=2;
    document.f1.cessp.value=0;
    document.f1.cessamt.value=0;

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

 calc();
}

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='';
   }

}
 

</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?>' onSubmit="return valid()" >
<table cellspacing=1 cellpadding=1 bgcolor=black WIDTH=100%>
<tr><td align=center bgcolor='GRAY'><FONT COLOR='IVORY'><B>INVOICE TO DEALER</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> (INVOICE No:

<?
$rs=mysqli_query($db,"select max(invno) from veh_invoice where brcode='$brcode'")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>
<TD >Dealer </td><td colspan=5>
<?
$ress=mysqli_query($db,"select * from sup_master where branch='$brcode'") or die(mysqli_error());
print "<select name='cust'>";
while($cust_row=mysqli_fetch_row($ress))
{
  print "<option>$cust_row[1]</option>";
}
?>
</select>
Tax Type <select name='taxtype' onChange='setttype()'><option>VAT</option><option>CST</option></select>
</td>
</tr>
<tr>
<td>Invoice No </td><td><input type=text readonly name='invno' value='<?print $pno?>' size=8 style={background-color:silver}></td>
<td>Invoice Date </td><td colspan=2>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></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'")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]='$row[config]'; \n";
 print "color[$i]='$row[color]'; \n engno[$i]='$row[engno]'; \n chasis[$i]='$row[chasis]'; </script>";
 $i++;
}
$result=mysqli_query($db,"select * from vehopstock where brcode='$brcode' and sold='-'")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]'; </script>";
 $i++;
}
?>
</select>
</td>
<td>Model Code </td><td><input type=text readonly name='mcode'></td>
<td>Eicher Model 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 onChange=calc()></td>
<tr><td>Net Amount</td><td><input type=text readonly name='netamt' onChange=calc()></td>
<?
if($brcode=='AUT009'|| $brcode=='AUT103')
{
?>
    <td>Tax %</td><td><input type=text  name='taxp' size=5 value='14.50' onChange=calc()>
 <input type=text name='tax' readonly size=10 value='14'></td>
<?
}
else
{
?>
<td>Tax %</td><td><input type=text  name='taxp' size=5 value='13.5' onChange=calc()>
 <input type=text name='tax'  size=10 value='13.5'></td>
<?
}

?>

<tr><td>Cess % 
<?
if($brcode=='Aut141')
{
?>
</td><td><input type=text name='cessp' value='0' readonly size=1 onChange=calc()>
<?
}
else
{
?>
</td><td><input type=text name='cessp' value='0' readonly size=1 onChange=calc()>
<?
}

?>
</td><td>Cess Amt <input type=text name='cessamt' value='0' size=4>
</td>
    <td>Amount <br>Before Tax </td><td><input type=text readonly  name='amt2' ></td>
</tr>
<tr><td>Color</td>
<td><input type=text name='color'></td><td>Engine No</td><td><input type=text readonly name='engno'></td></tr>
<tr><td>Segment </td><td colspan=4><select name='segment'><option></option>
<?
 $re=mysqli_query($db,"select * from seg_master");
 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=right bgcolor='green'> 

<input type=submit  value=' SAVE AND PRINT BILL ' name='sprint'  >
<input type=button value='  CANCEL  ' onClick=document.location='../blank.html'></td></tr>

<?
//
$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);

 var btamt2=parseFloat(document.f1.amt2.value);
 if(cessper==0 && taxper==2)
	{ amt=(btamt2*2/100); }
  else if(cessper==0 && taxper==13.5)
	 amt=(btamt2*13.5/100); 
else if(cessper==0 && taxper==14)
	 amt=(btamt2*14/100); 


 document.f1.tax.value=parseFloat(amt).toFixed(2);

 var netamt2=parseFloat(document.f1.amt2.value)+parseFloat(document.f1.tax.value);
 document.f1.netamt.value=(netamt2).toFixed(0);
 //var cessper=parseFloat(document.f1.cessp.value).toFixed(0);

 //var ces_amt=parseFloat((document.f1.tax.value*cessper)/100);
 //document.f1.cessamt.value=ces_amt.toFixed(2);
*/


var taxper=parseFloat(document.f1.taxp.value);
document.f1.netamt.value=document.f1.unit.value-document.f1.disc.value;
 

 var cessper=parseFloat(document.f1.cessp.value).toFixed(0);

 net=parseFloat(document.f1.netamt.value);
 
/*if(cessper==0 && taxper==2)
	{ amt=(net*100)/102; }
 else if(cessper==0 && taxper==13.5)
	 amt=(net*100)/113.5; 
 else if(cessper==0)
	 amt=(net*100)/114; */

amt=((net*100)/(100+taxper));

 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.tax.value*cessper)/100);
 document.f1.cessamt.value=ces_amt.toFixed(2);

}

function calc12()
{
document.f1.netamt.value=document.f1.unit.value-document.f1.disc.value;
 net=parseFloat(document.f1.netamt.value);
 var cessper=parseFloat(document.f1.cessp.value).toFixed(0);
 if(cessper==1)
	 amt=(net*100)/112.625; 
 else if(cessper==0)
	 amt=(net*100)/112.5; 

 document.f1.amt2.value=parseFloat(amt).toFixed(2);

 tax=parseFloat(document.f1.amt2.value)*parseFloat(document.f1.taxp.value)/100;
// document.f1.tax.value=(net-amt).toFixed(2);
 document.f1.tax.value=(tax).toFixed(2);
 var cessper=parseFloat(document.f1.cessp.value).toFixed(0);
 var ces_amt=parseFloat((document.f1.tax.value*cessper)/100);
 document.f1.cessamt.value=ces_amt.toFixed(2);
}

function calc9()
{
document.f1.netamt.value=document.f1.unit.value-document.f1.disc.value;
 net=parseFloat(document.f1.netamt.value);
 amt=(net* 100)/112.5; 
 document.f1.amt2.value=parseFloat(amt.toFixed(2));

 tax=parseFloat(document.f1.amt2.value)*parseFloat(document.f1.taxp.value)/100;
 document.f1.tax.value=(net-amt).toFixed(2);
}
</script>
<style>
.x,th{font-size:13}
</style>
<table border=1>
<th>No <th>Date <th>INV No.<th>DA <th>Dealer <th>Config <th>Chasis <th>Net Amt.
<th>Edit/Del
<?
$i=1;
$r=mysqli_query($db,"select b.invdate,b.invno,b.da,b.cust,b.config,b.chasis,b.netamt  from veh_invoice b,sup_master a where brcode='$brcode' and b.cust=a.supname and a.branch=b.brcode order by b.invno desc");
while($row=mysqli_fetch_array($r))
{
 print "<tr class=x bgColor='ivory' onMouseOver=bgColor='c1d7bb' onMouseOut=bgColor='ivory'><td>".$i++."</td><td>".datetohtml($row[0])."</td>";
 print "<td>$row[1]</td><td>$row[2]</td><td>$row[3]</td><td>$row[4]</td>";
 print "<td>$row[5]</td><td align=right>$row[6]</td><td><center>";
 if($level>=4)
{
 print "<input type=button value='Edit' onClick=edi('".str_replace(' ','*',$row[2])."','$row[1]')> <input type=button value='Del' onClick=del('$row[2]','$row[1]')>";
}
}
?>
</table>

<script>
 function edi(x,y)
 {
   if(confirm('Do you want to edit invoice '+y+ ' ?'))
    document.location='editinvoice-d.php?invno='+y+'&da='+x;
 }
 function del(x,y)
 {
   if(confirm('Do you want to Delete invoice '+y+ ' ?'))
    document.location='delinvoice-d.php?invno='+y+'&da='+x;
 }
</script>


