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

include("../opendb.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;
}
}
//


$da=str_replace('*',' ',$da);
$re=mysqli_query($db,"select * from veh_invoice where invno='$invno' and da='$da' and brcode='$brcode'");
 $inrow=mysqli_fetch_array($re);

if($sprint1)
{ 
mysqli_query($db,"delete from journal2 where jvno='$inrow[jvno]' and brcode='$brcode'");

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


$invdate=$y.'-'.$m.'-'.$d;

  $sql="update veh_invoice set cust='$cust',model_code='$mcode',invdate='$invdate',
  config='$config',unit='$unit',disc='$disc',netamt='$netamt',taxp='$taxp',tax='$tax',
  amt2='$amt2',color='$color',engno='$engno',segment='$segment',chasis='$chasis',
 remark='$remark',emcode='$emcode',cessp='$cessp', cessamt='$cessamt',taxtype='$taxtype',jvno='$jvno' where da='$da' and invno='$invno' and brcode='$brcode'";
 mysqli_query($db,$sql) or die(mysqli_error());
 



 $cust_name=$cust;

   $sql="update veh_purchase set sale_invoice='$invno', type='sale' where da='$da' and brcode='$brcode'";


 mysqli_query($db,$sql) or die(mysqli_error());




if($taxtype=='VAT')



{
if($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());
}
else
{
$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());
}

}




else
{
$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')
 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>";

log_entry($brcode,$user,$REMOTE_ADDR,"Edit invoice to dealer - DA=$da,JV=$jvno");
print "<script>window.open('prnvehinvoice.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.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.unit.value.length<=0)
 {
   submitOK="false";
   alert('Invalid unit price');
 }



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(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 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=2;
    document.f1.cessp.value=0;
    document.f1.cessamt.value=0;

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

 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?>' 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:

<?
print $invno.')';
?>
<input type=hidden name='pno' value='<?print $pno?>'>
<input type=hidden name='jvno' value='<?print $inrow[jvno]?>'>
<input type=hidden name='invdate' value='<?print $inrow[invdate]?>'>

</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'>";
print "<option>$inrow[0]</option>";
while($cust_row=mysqli_fetch_row($ress))
{
  print "<option>$cust_row[1]</option>";
}
?>
</select>
Tax Type <select name='taxtype' onChange='setttype()'>
<option><?print $inrow[taxtype]?></option>
<option>VAT</option><option>CST</option></select>
</td>
</tr>
<tr>
<td>Invoice No </td><td><input type=text readonly name='invno' value='<?print $inrow[invno]?>' size=8 style={background-color:silver}></td>
<td>Invoice Date </td><td colspan=2>
<?
 $dt=explode('-',$inrow[invdate]);
?>
dd <input type=text name='d' size=2 value='<?print $dt[2]?>'>
mm <input type=text name='m' size=2 value='<?print $dt[1]?>'>
yy <input type=text name='y' size=4 value='<?print $dt[0]?>'>
</td></tr>
<tr><td>DA </td><td>
<select name='da' onclick=alert("Use&nbsp;change&nbsp;da&nbsp;from&nbsp;utilities");>
<option><?print $da?></option>
</select>
</td>
<td>Model Code <input type=text  name='mcode' value='<?print $inrow[mcode]?>'></td>
<td>Eicher Model Code</td><td><input type=text name='emcode' size=10 value='<?print $inrow[emcode]?>'></td></tr>
<tr><td>config </td><td colspan=5><input type=text name='config' size=63 value='<?print $inrow[config]?>'></td></tr>
<tr><td><font color=red>Unit Price </td><td><input type=text name='unit' size=10 onChange=calc() value='<?print $inrow[unit]?>'> </td>
    <td>Discount </td><td><input type=text name='disc' SIZE=8 onChange=calc() value='<?print $inrow[disc]?>'></td>
<tr><td>Net Amount</td><td><input type=text readonly name='netamt' value='<?print $inrow[netamt]?>' onChange=calc()></td>
    <td>Tax %</td><td><input type=text  name='taxp' size=5 value='<?print $inrow[taxp]?>' >
 <input type=text name='tax' readonly size=10 value='<?print $inrow[tax]?>'></td>

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

?>
</td><td>Cess Amt <input type=text name='cessamt' readonly value='0' size=4>
</td>
    <td>Amount <br>Before Tax </td><td><input type=text readonly  name='amt2' value='<?print $inrow[amt2]?>' ></td>
</tr>
<tr><td>Color</td>
<td><input type=text name='color' value='<?print $inrow[color]?>'></td>
<td>Engine No</td><td><input type=text readonly name='engno' value='<?print $inrow[engno]?>'></td>
</tr>
<tr><td>Segment </td><td colspan=4><select name='segment'>
<option> <?print $inrow[segment]?></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  name='chasis'  value='<?print $inrow[chasis]?>'></td>
<td>Remarks </td><td colspan=3><textarea name='remark' cols=50><?print $inrow[remark]?></textarea></td></tr>
<tr><td colspan=6 align=right bgcolor='green'> 

<input type=submit  value=' UPDATE AND PRINT BILL ' name='sprint1'>
<input type=button value='  CANCEL  ' onClick=document.location='../blank.html'></td></tr>
</table>
</form>
</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>
</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 && taxper==14)
	 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 calcERR()
{
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>

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


