<? 
session_start();
$brcode = $_SESSION['brcode'];
$user = $_SESSION['user'];
if(strlen($brcode)<=0)
{
    print "<script> alert('Session timed out'); 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];
$d = $_REQUEST['d'];
$m = $_REQUEST['m'];
$y = $_REQUEST['y'];
$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;
    }
}

if(isset($_POST['save']))
{
    $rr=mysqli_query($db,"select max(ret_num) from veh_purchreturn where brcode='$brcode'");
    $ret_row=mysqli_fetch_row($rr);
    $retno=$ret_row[0];
    if($retno<=0)
        $retno='3751561900000';
    else
        $retno++;
    $d = $_REQUEST['d'];
    $m = $_REQUEST['m'];
    $y = $_REQUEST['y'];
    $dt=$y.'-'.$m.'-'.$d;
    $jvr=mysqli_query($db,"select max(jvno) from journal2");
    $j_row=mysqli_fetch_row($jvr);
    if($j_row[0]<=0)
        $jvno=1;
    else
        $jvno=($j_row[0]+1);
    $da = $_REQUEST['da'];
    $pinv = $_REQUEST['pinv'];
    $mcode = $_REQUEST['mcode'];
    $config = $_REQUEST['config'];
    $netamt = $_REQUEST['netamt'];
    $ledger = $_REQUEST['ledger'];
    $tax = $_REQUEST['tax'];
    $taxp = $_REQUEST['taxp'];
    $amt2 = $_REQUEST['amt2'];
    $cessp = $_REQUEST['cessp'];
    $cessamt = $_REQUEST['cessamt'];
    $invno = $_REQUEST['invno'];
    $roundoff=  $_REQUEST['roundoff'];
    if($invno <= '')
        $invno = 0;
    $sql="insert into veh_purchreturn values('$retno','$dt','$da','$pinv','$mcode','$config', '$netamt','$ledger','$brcode','$tax','$taxp','$amt2','$jvno','$cessp','$cessamt')";
    mysqli_query($db,$sql) or die(mysqli_error());
    $sql="insert into veh_movement(date,inv_no,qty,type,brcode,sup_type,da) values('$dt','$retno','1','pur_ret','$brcode','$ledger','$da')";
    mysqli_query($db,$sql) or die(mysqli_error());
    $sql="update veh_purchase set type='pur-ret' , sale_invoice='$pinv' where da='$da' and invno='$pinv' and brcode='$brcode'";
    mysqli_query($db,$sql) or die(mysqli_error());
    $sql="update vehopstock set transtype='pur-ret', sold='$pinv' where dano='$da'";
    mysqli_query($db,$sql) or die(mysqli_error());
    $sql="insert into journal2 values('$dt','$jvno','Dr','$ledger','$netamt', '0','Purchase Return: $invno','$brcode','P Ret','DA','$da','$invno')";
    mysqli_query($db,$sql) or die("J V Write error 1:".mysqli_error());
    if($tax <='')
        $tax = 0;
    if($cessamt <='')
        $cessamt = 0;
    if($amt2 <='')
        $amt2 = 0;        
    $sql="insert into journal2 values('$dt','$jvno','Cr','Output IGST $taxp% (Pur Ret)', '0','$tax','Purchase Return: $invno','$brcode','P Ret','DA','$da','$invno')";
    mysqli_query($db,$sql) or die("J V Write error 2:".mysqli_error());
    $sql="insert into journal2 values('$dt','$jvno','Cr','Input Cess (Purc.Ret)','0','$cessamt', 'P.Bill No: $invno','$brcode','V Ret','DA','$da','$invno')";
    
    mysqli_query($db,$sql) or die("J V Write error 2cess:".mysqli_error());
    $sql="insert into journal2 values('$dt','$jvno','Cr','Purchase ret GST$taxp%(vehicle)','0','$amt2','Purchase Return: $invno','$brcode','P Ret','DA','$da','$invno')";
    mysqli_query($db,$sql) or die("J V Write error 3:".mysqli_error());
    if ($roundoff<0)
    {
        $roff=abs($roundoff);
        $sql="insert into journal2 values('$dt','$jvno','Dr','Round Off','$roff','0', 'P.Bill No: $invno','$brcode','V Ret','DA','$da','$invno')";
        mysqli_query($db,$sql) or die("J V Write error 2cess:".mysqli_error());
    }
    else
    {
        $sql="insert into journal2 values('$dt','$jvno','Cr','Round Off','0','$roundoff', 'P.Bill No: $invno','$brcode','V Ret','DA','$da','$invno')";
        mysqli_query($db,$sql) or die("J V Write error 2cess:".mysqli_error());
    }
    print "<font color=green><B>Data has been saved</b></font>";
}
//id312790

?>
<script>
var pinv=[];
var mcode=[];
var config=[];
var netamt=[];
var ledger=[];
var tax=[];
var disc=[];
var amt2=[];

function fun1()
{
 var x=document.f1.da.selectedIndex; 
 
 if(x<=0)
   {
     document.f1.pinv.value='';
    document.f1.mcode.value='';
    document.f1.config.value='';
    document.f1.netamt.value='';
    document.f1.ledger.value='';
   }
 else
  { 
    document.f1.pinv.value=pinv[document.f1.da.selectedIndex];
    document.f1.mcode.value=mcode[document.f1.da.selectedIndex];
    document.f1.config.value=config[document.f1.da.selectedIndex];
    document.f1.netamt.value=netamt[document.f1.da.selectedIndex]; 
    document.f1.ledger.value=ledger[document.f1.da.selectedIndex];
    document.f1.tax.value=tax[document.f1.da.selectedIndex];
    //document.f1.disc.value=disc[document.f1.da.selectedIndex];
    document.f1.amt2.value=amt2[document.f1.da.selectedIndex];
//alert(netamt[document.f1.da.selectedIndex]);

  }
 calc();
}
function calc_old()
{
 net=parseFloat(document.f1.netamt.value);
 amt=(net* 100)/(100+parseFloat(document.f1.taxp.value)); 
 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);
}

function calc()
{
/*
 var pe=document.f1.taxp.value; 
var cessper=parseFloat(document.f1.cessp.value).toFixed(0);
var taxper1=parseFloat(document.f1.taxp.value).toFixed(0);


 net=parseFloat(document.f1.netamt.value);
 x=(100+parseFloat(cessper)); 
 zz=(100+parseFloat(pe)); 
  if(cessper>0)
  {
    yy=pe*cessper/100;
    zz=zz+yy;
  }

 amt=(net*100)/zz;

 tax=(amt*pe)/100;
 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 ces_amt=parseFloat((document.f1.tax.value*cessper)/100);
 document.f1.cessamt.value=ces_amt.toFixed(2);
var gtot=parseFloat(document.f1.cessamt.value)+parseFloat(document.f1.tax.value)+parseFloat(document.f1.amt2.value);
  var diff=net-gtot; 
 document.f1.amt2.value=(parseFloat(document.f1.amt2.value)+parseFloat(diff)).toFixed(2);
*/

if(document.f1.taxp.value==14.5)
document.f1.cessp.value=0;
else
{
document.f1.cessp.value=0;
}

var tax_amt=parseFloat(document.f1.amt2.value)*parseFloat(document.f1.taxp.value)/100;
document.f1.tax.value=tax_amt.toFixed(2);

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


var net_amt=parseFloat(document.f1.amt2.value)+parseFloat(document.f1.tax.value)+parseFloat(document.f1.cessamt.value);
document.f1.netamt.value=net_amt.toFixed(0);

var gt=parseFloat(document.f1.amt2.value)+parseFloat(document.f1.tax.value)+parseFloat(document.f1.cessamt.value);
 var round=parseFloat(document.f1.netamt.value)-gt;
//alert(round.toFixed(2));

document.f1.roundoff.value=round.toFixed(2);

}
function valid()
{
 submitOK="true";
 if(document.f1.da.value.length<=0)
 {
   submitOK="false";
   document.f1.da.focus();
   alert('Invalid DA 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>=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(isNaN(document.f1.netamt.value))
 {
   submitOK="false";
  document.f1.netamt.focus();
   alert('Invalid Net Amount');
 }
  if(submitOK=="true")
   return true;
  else
   return false;
}

</script>
<link rel="stylesheet" href="../style.css" type="text/css" />
<body class=bd onSubmit="return valid()">
<center>
<h3>PURCHASE RETURN (No. 
<?  $rr=mysqli_query($db,"select max(ret_num) from veh_purchreturn where brcode='$brcode'");
  $ret_row=mysqli_fetch_row($rr);
  $retno=$ret_row[0];
  if($retno<=0)
   $retno=1;
  else
   $retno++;
print $retno;
?>
 )</H3>
<form name='f1' action='' method='POST'>
<table border=1>
<tr><td>Date </td><td colspan=2>  dd <input type=text size=2 name='d' value='<?print date("d",time())?>'>
			mm <input type=text size=2 name='m' value='<?print date("m",time())?>'>
			yy <input type=text size=4 name='y' value='<?print date("Y",time())?>'>
</td><td colspan=3>&nbsp;</td></tr>
<tr><td> DA (opening stock)</td><td><select name='da' onChange=fun1()>
<option></option>
<?
$i=1;
$re=mysqli_query($db,"select a.sup,b.supname,a.pinv,a.dano,a.mcode,a.config,a.pval from vehopstock a, sup_master b where a.sup=b.supname and a.sold='-' and a.transtype!='pur-ret' and a.brcode='$brcode' and b.branch='$brcode' order by dano")or die('OP Err'.mysqli_error());
while($oprow=mysqli_fetch_array($re))
{
    print "\n<option>$oprow[dano]</option>";
    print "\n <script> pinv[$i]='$oprow[pinv]'; </script>";
    print "\n <script> mcode[$i]='$oprow[mcode]'; </script>";
    print "\n <script> config[$i]='$oprow[config]'; </script>";
    print "\n <script> amt2[$i]='$oprow[pval]'; </script>";
    print "\n <script> ledger[$i]='$oprow[supname]'; </script>";
    $i++;
} 
$result=mysqli_query($db,"select * from vehopstock where sold='N' and brcode='$brcode' order by dano")or die(mysqli_error());
while($row=mysqli_fetch_array($result))
{
    print "\n<option>$row[dano]</option>";
    print "\n <script> pinv[$i]='$row[pinv]'; </script>";
    print "\n <script> mcode[$i]='$row[mcode]'; </script>";
    print "\n <script> config[$i]='$row[config]'; </script>";
    print "\n <script> amt2[$i]='$row[pval]'; </script>";
    $i++;
}

$result=mysqli_query($db,"select * from veh_purchase where (sale_invoice='-' and type!='trans-out' and type!='pur-ret') and brcode='$brcode'")or die(mysqli_error());
print "select * from veh_purchase where (sale_invoice='-' and type!='trans-out' and type!='pur-ret') and brcode='$brcode'";
while($row=mysqli_fetch_array($result))
{
    print "\n<option>$row[da]</option>";
    print "\n <script> pinv[$i]='$row[invno]'; </script>";
    print "\n <script> mcode[$i]='$row[mcode]'; </script>";
    print "\n <script> config[$i]='$row[config]'; </script>";
    print "\n <script> netamt[$i]='$row[netamt]'; </script>";
    print "\n <script> tax[$i]='$row[tax]'; </script>";
    print "\n <script> amt2[$i]='$row[amt2]'; </script>";
    print "\n <script> disc[$i]='$row[disc]'; </script>";
    if($row['type']=='trans-in')
    {
        $led=mysqli_query($db,"select brname from branch where brcode='$row[ledger]'");
        $led_row=mysqli_fetch_row($led);
        print "\n <script> ledger[$i]='$led_row[0]'; </script>";
    }
    else
        print "\n <script> ledger[$i]='$row[ledger]'; </script>";
    $i++;
}
?>
</select></td>
<td>Pur Invoice</td><td><input type=text name='pinv' readonly ></td>
<td>Model Code</td><td><input type=text name='mcode' readonly ></td>
</tr>
<tr><td>Config</td><td colspan=3><input type=text name='config' size=40></td>
<tr><td><font color='red'>Net Amount</td><td><input type=text name='netamt' readonly onChange=calc()></td>

<td>Round Off</td><td><input type=text name='roundoff'></td>

  

<tr>

<?
if($brcode=='AUT009'|| $brcode=='AUT103')
{
    print "<tr><td>Cess %</td>";
    print "<td><input type=text name='cessp' size=2 value='0' onChange=calc()></td>";
    print "<td>Cess Amt</td><td><input type=text name='cessamt' ></td></tr>";
    print"<td>Tax %</td><td><input type=text name='taxp' size=5 value='14' onChange=calc()></td>";
    print" <td><input type=text name='tax' size=10 value='0' onChange=calc()></td>";
}
else
{
    print "<tr><td>Cess %</td>";
    print "<td><input type=text name='cessp' size=2 value='0' onChange=calc()></td>";
    print "<td>Cess Amt</td><td><input type=text name='cessamt'></td></tr>";
    print"<td>Tax %</td><td><input type=text name='taxp' size=5 value='14.5' onChange=calc()></td>";
    print" <td><input type=text name='tax' size=10 value='0' onChange=calc()></td>";
}
?>


    <td>Amount <br>Before Tax </td><td><input type=text  name='amt2' onchange=calc() ></td>
</tr>
<tr><td>Return to </td><td colspan=3>
<select name='ledger'>
<? $rrs=mysqli_query($db,"select * from sup_master where branch='$brcode'");
   while($su_row=mysqli_fetch_row($rrs))
    print "<option>$su_row[1]</option>";
?>
</select> 
 
</td>
<td align=center colspan=2><input type=submit value=' SAVE ' name='save'> <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>
<br>
<style>
.x{font-size:13}
</style>

<table border=1>
<th>Ret.No<th>Date <th>DA <th>Pur_Inv <th>M code<th>Config <th>Net Amt.<th>Ret. To 
<th>Tax <th>Tax % <th>Cess% <th>Cess<th>Amount <th>Del
<?
$res=mysqli_query($db,"select * from veh_purchreturn where brcode='$brcode'");
while($row=mysqli_fetch_array($res))
{
 print "<tr class=x><td><center>$row[0]</td><td>".implode('-', array_reverse(explode('-', $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>$row[7]</td><td align=right>$row[9]</td><td align=right>$row[10]</td>";
 print "<td align=right>$row[cessp]</td><td align=right>$row[cessamt]</td>";
 print "<td align=right>$row[11]</td>";
 print "<td><input type=button value='Edit' onClick=edpur('$row[da]')>";
 print "<input type=button value='Del' onClick=delpur('$row[da]')></td></tr>";
}
?>
</table>

<script>
 function delpur(x)
 {
  if(confirm('Do you want to delete da '+x+ ' ?'))
   {
     document.location='delpurreturn.php?da='+x;
   }
 }
 function edpur(x)
 {
  if(confirm('Do you want to edit da '+x+ ' ?'))
   {
     window.open('edpurreturn.php?da='+x);
   }
 }
</script>