<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../index.php'; </script>";
 return;
}
include("top.php");
include("../opendb.php");
include("../test.php");
include("../util/exqty.php");
//include ("calcsparestock.php");
$ker=mysqli_query($db,"select brstate from branch where brcode='$brcode'");
$kar=mysqli_fetch_array($ker);
$kk=$kar[0];
$pono=$_REQUEST['pono'];
$vatorcst=$_REQUEST['vatorcst'];
$taxtype=$_REQUEST['vatorcst'];
$mnfr=$_REQUEST['mnfr'];
$cstmr=$_REQUEST['cstmr'];
$cstmr=trim($cstmr);

$rs=mysqli_query($db,"select state1,gst,code from customer where acname='$cstmr' ");
//print $r=("select state1,gst,code from customer where acname='$cstmr'");

$rw=mysqli_fetch_array($rs);
print $kar[0];
print $rw[0];

if($vatorcst=='INTERSTATE')
{
if($rw[0]==$kar[0])
{
   // print "<script> alert('Customer belongs to Other State! Interstate billing not allowed!'); parent.document.location='spareinvoicedc.php'; </script>";    
}

}

if($vatorcst=='INTRASTATE')
{
if($rw[0]!=$kar[0])
{
    
   // print "<script> alert('Customer does not belongs to this State! Intrastate billing not allowed!'); parent.document.location='spareinvoicedc.php'; </script>";    
}

}

if($rw[1]=='NOT REGISTERED')
{
   // print "<script> alert('GST Information Not Updated For Customer".$cstmr." : Kindly Update'); parent.document.location='spareinvoicedc.php'; </script>";    
}
?>
<title>Spare Sales </title>
<script>
function valid()
{
 if(document.f1.paytype.selectedIndex<=0)
 {
  alert('Invalid Payment Type');
  document.f1.paytype.focus();
  return false;
 }
 if(confirm('Do you want to save?'))
   return true;
 else
   return false;
}
</script>
 <script type="text/javascript" src="../scripts/jquery-latest.js"></script>
	<link type="text/css" href="../includes/style.css" rel="stylesheet" />
<link href="../css/form.css" type="text/css" rel="stylesheet" media="screen" />
    <script type="text/javascript">
        $(function() {
          if ($.browser.msie && $.browser.version.substr(0,1)<7)
          {
			$('li').has('ul').mouseover(function(){
				$(this).children('ul').show();
				}).mouseout(function(){
				$(this).children('ul').hide();
				})
          }
        });        
    </script>
<link rel="stylesheet" href="../style.css" type="text/css" />
<body background="../back_ground.jpg">
<br><br><br>
<br>
<?

$result=mysqli_query($db,"select * from saleorder_sparedc where orderno='$pono' and brcode='$brcode'");
//$r=("select * from saleorder_spare where orderno='$pono' and brcode='$brcode'");
$row=mysqli_fetch_array($result);

$cusres=mysqli_query($db,"select * from customer where acname=trim('$row[orderto]')");
$c=("select * from customer where acname='$row[orderto]'");
$cusrow=mysqli_fetch_array($cusres);
if(mysqli_num_rows($cusres)<=0)
{
	//print 'invalid customer';
	//return;
}
if($invtype=='8')
{
	if($cusrow[tin]<=0)
	{
	//	print 'Customer does not have TIN';
	//	return;
	}
}

?><center>
<table class="container"><tr><td>

<form name="f1" action="svinvoicedc.php" method=post onSubmit="return valid()">
<input type=hidden name='saleordno' value='<?print $pono?>'>
<input type=hidden name='invtype' value='<?print $taxtype?>'>
<input type=hidden name='vatorcst' value='<?print $vatorcst?>'>

<?
$sq="select max(invno) from spareinvoicedc where brcode='$brcode' ";
$re=mysqli_query($db,$sq);
if(mysqli_num_rows($re)<1)
{
	$invno=$mprefix.'000001'; 
}
else
{
	$row11=mysqli_fetch_array($re);
       $invno=substr($row11[0],-6)+1;
	$invno=substr('000000'.$invno,-6);
	if($invno==0)
	{
		$invno='000001'; 
	}
	$invno=$mprefix.$invno; 
}	
 
$tbl="sparetmpdc".$brcode.$user;
print "<center><table border=0  cellspacing=1 cellpadding=3 class='form' ><th colspan=6><H3> SPARE INVOICE($invno) $taxtype-$vatorcst</TH>";
print "<tr><td>Sale Order No</td><td>$row[orderno]</td>";
print "<td>Sale Order Date </td><td>".datetohtml($row[date])."</td>";
//print "<td>Refered By</td><td><input type=text name='msno' value='$row[msno]'></td>
print"</tr>";
print "<tr><td>Customer</td><td colspan=2><select name='customer'><option value='$row[orderto]'>$row[orderto]
</option></select>";

print"Ship To State
    <select name='state1' id=state1><option value=$cusrow[state1]>$cusrow[state1]</option>
<option>Kerala</option><option>Karnataka</option><option>Tamil Nadu</option><option>Andhra Pradesh</option>
<option>Arunachal Pradesh</option><option>Assam</option><option>Bihar</option><option>Chandigarh</option><option>Chhattisgarh</option>
<option>Dadra and Nagar Haveli</option><option>Daman and Diu</option><option>Delhi</option><option>Goa</option>
<option>Gujarat</option><option>Haryana</option><option>Himachal Pradesh</option><option>Jammu and Kashmir</option>
<option>Jharkhand</option><option>Madhya Pradesh</option><option>Maharashtra</option><option>Manipur</option>
<option>Meghalaya</option><option>Mizoram</option><option>Nagaland</option><option>Orissa</option>
<option>Puducherry</option><option>Punjab</option><option>Rajasthan</option><option>Sikkim</option><option>Telangana</option>
<option>Tripura</option><option>Uttar Pradesh</option><option>Uttarakhand</option><option>West Bengal</option>
    </select>
</td>";

print "<td><font color=red>Payment Type</font></td><td><select name='paytype'><option></option>";
if($cusrow[cramt]<=0)
{
print "<option>Cash</option>";
print "<option>Card</option></select></td></tr>";
}
else
{
print "<option>Credit</option>";
print "<option>Card</option>";
print "<option>Cash</option></select></td></tr>";
}
print "<tr><td>Ship To Address</td><td><textarea name='addr'>$cusrow[addr1].$cusrow[dist1]</textarea></td>";
print "<td>GST No.</td><td><input type=text name='tin'  value='$cusrow[gst]'> <input type=hidden name='cusbst' readonly value='$cusrow[state1]'></td>";
if($kar[0]!='Kerala') 
{
if($user=='rajamys'|| $user=='rajathu') 
{
print "<td>Sale Bill No.</td><td><input type=text name='invno' value='$invno'></td></tr>";
}
else
{
print "<td>Sale Bill No.</td><td><input type=text  name='invno' value='$invno'></td></tr>";
}

?>
<tr></tr>
<?
if($brcode=='MAN959')
{
print "<tr><td>Sale Date</td><td>dd<input type=text name='d' size=2 maxlength=2 value='".date("d")."'>
mm<input type=text name='m' size=2 maxlength=2 value='".date("m")."'>
yy<input type=text name='y' size=4 maxlength=4 value='".date("Y")."'>
</td>";
print "<td>Entry Date</td><td>dd<input type=text name='d1' size=2 maxlength=2 value='".date("d")."'>
mm<input type=text name='m1' size=2 maxlength=2 value='".date("m")."'>
yy<input type=text name='y1' size=4 maxlength=4 value='".date("Y")."'>
</td>";
}
else
{
   if ($user=='shaji')
   {
print "<tr><td>Sale Date</td><td>dd<input type=text  name='d' size=2 maxlength=2 value='".date("d")."'>
mm<input type=text  name='m' size=2 maxlength=2 value='".date("m")."'>
yy<input type=text  name='y' size=4 maxlength=4 value='".date("Y")."'>
</td>";
print "<td>Entry Date</td><td>dd<input type=text name='d1' size=2 maxlength=2 value='".date("d")."'>
mm<input type=text  name='m1' size=2 maxlength=2 value='".date("m")."'>
yy<input type=text  name='y1' size=4 maxlength=4 value='".date("Y")."'>
</td>";
}
else
{
    print "<tr><td>Sale Date</td><td>dd<input type=text readonly name='d' size=2 maxlength=2 value='".date("d")."'>
mm<input type=text readonly name='m' size=2 maxlength=2 value='".date("m")."'>
yy<input type=text readonly name='y' size=4 maxlength=4 value='".date("Y")."'>
</td>";
print "<td>Entry Date</td><td>dd<input type=text readonly name='d1' size=2 maxlength=2 value='".date("d")."'>
mm<input type=text readonly name='m1' size=2 maxlength=2 value='".date("m")."'>
yy<input type=text readonly name='y1' size=4 maxlength=4 value='".date("Y")."'>
</td>";
}
    


}
}
else
{

if($brcode=='MAN959')
{
print "<tr><td>Sale Date</td><td>dd<input type=text name='d' size=2 maxlength=2 value='".date("d")."'>
mm<input type=text name='m' size=2 maxlength=2 value='".date("m")."'>
yy<input type=text name='y' size=4 maxlength=4 value='".date("Y")."'>
</td>";
print "<td>Entry Date</td><td>dd<input type=text name='d1' size=2 maxlength=2 value='".date("d")."'>
mm<input type=text name='m1' size=2 maxlength=2 value='".date("m")."'>
yy<input type=text name='y1' size=4 maxlength=4 value='".date("Y")."'>
</td>";
}
else
{
if ($user=='shaji')
{
print "<td>Sale Bill No.</td><td><input type=text  name='invno' value='$invno'></td></tr>";

print "<tr><td>Sale Date</td><td>dd<input type=text  name='d' size=2 maxlength=2 value='".date("d")."'>
mm<input type=text  name='m' size=2 maxlength=2 value='".date("m")."'>
yy<input type=text  name='y' size=4 maxlength=4 value='".date("Y")."'>
</td>";
print "<td>Entry Date</td><td>dd<input type=text  name='d1' size=2 maxlength=2 value='".date("d")."'>
mm<input type=text  name='m1' size=2 maxlength=2 value='".date("m")."'>
yy<input type=text  name='y1' size=4 maxlength=4 value='".date("Y")."'>
</td>";
}
else
{
   print "<td>Sale Bill No.</td><td><input type=text readonly name='invno' value='$invno'></td></tr>";

print "<tr><td>Sale Date</td><td>dd<input type=text readonly name='d' size=2 maxlength=2 value='".date("d")."'>
mm<input type=text readonly name='m' size=2 maxlength=2 value='".date("m")."'>
yy<input type=text readonly name='y' size=4 maxlength=4 value='".date("Y")."'>
</td>";
print "<td>Entry Date</td><td>dd<input type=text readonly name='d1' size=2 maxlength=2 value='".date("d")."'>
mm<input type=text readonly name='m1' size=2 maxlength=2 value='".date("m")."'>
yy<input type=text readonly name='y1' size=4 maxlength=4 value='".date("Y")."'>
</td>"; 
}


}

}

print "<td colspan=2><font color=red>Transportation Purpose</font> <input type=text name='ref' size=50></td> </tr>";
print "<tr><td>Engineer </td><td><select name='engineer'><option>Counter Sale</option>";
$enres=mysqli_query($db,"select * from psnauto_payroll.empmaster where (brcode = 'BAN682' or brcode = 'ERA153' or brcode = 'HUB570') and dept like '%service%' and doret='0000-00-00' order by empname");
//$enres=mysqli_query($db,"select * from empmaster where dept like '%service%' and doret='0000-00-00' order by empname");
while($enrow=mysqli_fetch_array($enres))
{
 print "<option>$enrow[empname]</option>";
}
print "<option>FOC</option></select></td><td>Model</td><td><select name='mtype'><option>Select</option>";
if($brcode=='PSN803')
{
$result=mysqli_query($db,"select * from machmaster1 where mtype='Generator' order by mtype,maname ");
}
else
{
//echo "select * from machmaster order by mtype,maname";//exit;
$result=mysqli_query($db,"select * from machmaster1 where mtype!='Generator' order by mtype,maname");
}
while($mrow=mysqli_fetch_array($result))
{
 print "<option>$mrow[maname]</option>";
}
print "<option>Others</option></select></td></tr>";
print "<tr><th colspan=10></tr>";
print"</table><br>";

?>
<link rel="stylesheet" href="../style.css" type="text/css" />
<style>
body {
    margin: 0px;
    padding: 0px;
        padding-top: 0px;
    background: #f2f2f2;
    font-family: Tahoma,Verdana, Geneva, sans-serif;
    font-size: 12px;
    padding-top: 0px;
}
//	tr{background-color:white}
	th{background-color:steelblue;color:white}
.right{text-align:right};
</style>
<center>

<table border=0 cellspacing=1 cellpadding=3  class='report'>
<th>No<th>P Code <th> Name <th>HSN<th>Ord Qty <th> Sale Qty
<th>Price <th>Disc% <th> A Price<th>Hand Chrge 
<?
if($vatorcst=='INTRASTATE')
   print "<th>GST INTRASTATE%<th>Tax Amt(CGST) <th>Tax Amt(SGST) ";
else
 print "<th>GST INTERSTATE% <th>Tax Amt(IGST) ";
?>
<th> Total
<?
    $taxres=mysqli_query($db,"select tax from taxmaster where type='$taxtype'");
    $taxrow=mysqli_fetch_row($taxres);
    $tax=$taxrow[0];
$i=1;
$result=mysqli_query($db,"select * from saleorder_sparedc where orderno='$pono' and brcode='$brcode' and sqty>sup_qty and closed='N'");
//$result=mysqli_query($db,"select * from saleorder_spare where orderno='$pono' and brcode='$brcode'");
while($row=mysqli_fetch_array($result))
{
	$itmres=mysqli_query($db,"select * from partmaster where partcode='$row[0]' ");
	$prtrow=mysqli_fetch_array($itmres);
	if ($kk=='Kerala')
     {
	$prres=mysqli_query($db,"select * from spareprmast where partcode='$row[0]' and brcode='ERA153' order by effectfrom desc");
	$prrow=mysqli_fetch_array($prres);
     }
     else
      {
	$prres=mysqli_query($db,"select * from spareprmast where partcode='$row[0]' and brcode='BAN682' order by effectfrom desc");
	$prrow=mysqli_fetch_array($prres);
      }
	if(mysqli_num_rows($prres)<=0)
 	{
 		//print "<tr><td colspan=10 align=center><font color=red>$row[0] -  Price could not be found</font></td></tr>";
	}
/*$qtyres=mysqli_query($db,"select supqty from $tbl where partcode='$row[0]'");
$qtyrow=mysqli_fetch_array($qtyres);
if($qtyrow[0]<=0)
 $availqty=0;
else
 $availqty=$qtyrow[0];
*/
//$availqty=exqty1($row[0],$brcode,$user);

print "\n<input type=hidden name='availqty[]' id='availqty$i' value='$availqty'>";
print "\n<tr bgcolor='white'; onMouseOver=bgColor='c1d7bb'; onMouseOut=bgColor='white'>\n<td>".$i."</td><td><input type=text name='partcode[]' value='$row[0]' size=15></td><td>";
print "<textarea name='partname[]' rows=1>$row[1]</textarea></td>";
print "<td align=right><input type=text name='hsn[]' size=10 value='$row[msno]' ></td><td align=right>$row[qty]</td>";
print "\n<td align=center><input type=text name='supqty[]' class=right id='$i' size=4 onChange=calc(this.id) value='0'></td>";

if($kar[0]=='Kerala' && $brcode!='MAN959')
{
        if($user == 'shaji' || $user == 'deepakp')
	{
	   print "<td align=center><input class=right type=text name='price[]' id='price$i' size=8 value='$prrow[lstprice]' onChange=calc($i)></td>";
         }
         else
	 {
	   print "<td align=center><input class=right type=text readonly  name='price[]' id='price$i' size=8 value='$prrow[lstprice]' onChange=calc($i)></td>";
	  
         }
}
else
{
	if($user == 'deepakp')
	{
 		print "<td align=center><input class=right type=text name='price[]' id='price$i' size=8 value='$prrow[lstprice]' onChange=calc($i)></td>";
	}
	else
	{
		if($brcode == 'MAN959' )
		{
			print "<td align=center><input class=right type=text  name='price[]' id='price$i' size=8 value='$prrow[lstprice]' onChange=calc($i)></td>";
		}
		else
		{
			print "<td align=center><input class=right type=text readonly name='price[]' id='price$i' size=8 value='$row[sqty]' onChange=calc($i)></td>";
		}
	} 
}








if($user == 'ajay' ||$user =='rameshhubl' ||$user =='deepakp' ||$user =='rameshilka' ||$user =='rameshsand' ||$user =='rameshhosp' ||$user=='rameshdave'||$user=='rameshchit'|| $user=='ramnadh'|| $user=='shaji'||$user=='rameshdhar' || $user=='seema' || $user=='ramjith')
{
print "<td align=center><input class=right type=text name='disc[]' id='dis$i' size=1 value='0' onChange=calc($i)></td>";
}
else
{
print "<td align=center><input class=right type=text readonly name='disc[]' id='dis$i' size=1 value='0' onChange=calc($i)></td>";
    
}
print "<td align=center><input class=right type=text readonly  name='aprice[]' id='ap$i' size=8></td>";



print "<td align=center><input class=right type=text   name='hand[]' id='hnd$i' size=3 value='0' onChange=calc($i)></td>";


    if($vatorcst=='INTRASTATE')
    {
        $query=mysqli_query($db,"select kar_tax_sales from partmaster where partcode='$row[0]'");
        $qry1=mysqli_fetch_row($query);
	    print "<td align=center style='width: 20%;'>CGST<input class=right type=text  name='taxcgst[]' id='taxcgst$i' onChange=calc($i) size=4 value='$qry1[0]'>SGST
	    <input class=right type=text onChange=calc($i)  name='taxsgst[]' id='taxsgst$i' size=4 value='$qry1[0]'></td>";
	    print "<input class=right type=hidden  name='taxigst[]' id='taxigst$i' size=4 value='0'>";
	    print "<td align=center><input class=right type=text readonly name='taxamtcgst[]' id='taxamtcgst$i' size=8 value='0'></td>";
	    print "<td align=center><input class=right type=text readonly name='taxamtsgst[]' id='taxamtsgst$i' size=8 value='0'></td>";
	    print "<input class=right type=hidden readonly name='taxamt[]' id='taxamt$i' size=8 value='0'>";
    }
	else
	{
	    $query=mysqli_query($db,"select kar_tax_sales from partmaster where partcode='$row[0]'");
        $qry1=mysqli_fetch_row($query);
        $tax=$qry1[0]*2;
	    print "<td align=center>IGST<input class=right type=text  name='taxigst[]' id='taxigst$i' onChange=calc($i) size=4 value='$tax'></td>";
	    print "<input class=right type=hidden  name='taxcgst[]' id='taxcgst$i' size=4 value='0'>
	    <input class=right type=hidden  name='taxsgst[]' id='taxsgst$i' size=4 value='0'>";
	    print "<input class=right type=hidden name='taxamtcgst[]' id='taxamtcgst$i' size=8 value='0'>";
	    print "<input class=right type=hidden name='taxamtsgst[]' id='taxamtsgst$i' size=8 value='0'>";	    
	    print "<td align=center><input class=right type=text readonly name='taxamt[]' id='taxamt$i' size=8 value='0'></td>";
	}




print "<td align=center><input class=right type=text readonly  name='tot[]' id='tot$i' value='0' size=10></td></tr>";
$i++;
}
print "\n<tr><td colspan=4> <center><b>Total </td><td align=center><input type=text class=right name='totsupqty[]' id='totsupqty' size=8></td>";
print "\n<td colspan=5 align=center></td><td align=center>";
print "<input type=text class=right id='tottx' size=8></td>";
print "<td><input type=text name=ggtot id='ggtot' size=10></td><td></td></tr>";

?>
<tr>
<th colspan=18><input type=submit value="Save" name="sv">
<input type=button value='Cancel' onClick=window.close()>
</table>
</form>

<script>
 function calc(x)
 {
 var ele='price'+x; 
 var disele='dis'+x;
 var actele='ap'+x;
 var hndele='hnd'+x;
 var taxele='taxcgst'+x;
 var taxsgstele='taxsgst'+x;
 var taxigstele='taxigst'+x;
 var totele='tot'+x;
 var txamtele='taxamt'+x;
 var focele='foc'+x;
 var warele='war'+x;
 var totsupq='totsupqty';
 var tx='tottx';
 var avail='availqty'+x;
 var taxamtcgstele='taxamtcgst'+x;
 var taxamtsgstele='taxamtsgst'+x;
 var cesspele='cessp'+x;
 var cessele='cessamt'+x;

 /*if(parseFloat(document.getElementById(x).value)>parseFloat(document.getElementById(avail).value) || document.getElementById(avail).value=='')
 {
   alert('Stock only '+document.getElementById(avail).value);
  document.getElementById(x).value=0;
  document.getElementById(x).focus();
   return false;
 }*/
 var amt=parseFloat(document.getElementById(ele).value)*(parseFloat(document.getElementById(x).value)); //+parseFloat(document.getElementById(warele).value));
 var disc=amt*parseFloat(document.getElementById(disele).value)/100;
 var actamt=amt-disc;
//document.getElementById(disele).value=parseFloat(disc);

document.getElementById(actele).value=parseFloat(actamt);
<?
if($kar[0]=='Kerala') 
{
?>
var hnd=parseFloat(document.getElementById(hndele).value);
<?
}
else
{
?>
var hnd=parseFloat(document.getElementById(hndele).value);
<?
}
?>


    var tx=parseFloat(document.getElementById(taxele).value);
    var txsgst=parseFloat(document.getElementById(taxsgstele).value);
    var txigst=parseFloat(document.getElementById(taxigstele).value);
    
    
var tax=(actamt*txigst)/100;
document.getElementById(txamtele).value=tax.toFixed(2);

 var taxc=(actamt*tx)/100;
document.getElementById(taxamtcgstele).value=taxc.toFixed(2);

 var taxs=(actamt*txsgst)/100;
document.getElementById(taxamtsgstele).value=taxs.toFixed(2);

document.getElementById(totele).value=(actamt+hnd+tax+taxc+taxs).toFixed(2);

tot=0;
for(j=1;j<<?print $i?>;j++)
 {
   var focele1='foc'+j;
   var warele1='war'+j;
//   f=parseFloat(document.getElementById(focele1).value); 
//   w=parseFloat(document.getElementById(warele1).value); 

   t=document.getElementById(j).value; 
   if(t.length<=0) 
     t=0;
   tot=parseFloat(tot)+parseFloat(t); //+f+w;
}

tottax1=0;
gt=0;
for(j=1;j<<?print $i?>;j++)
 {
 var tx='taxamt'+j; 
 var gtot='tot'+j
   t=document.getElementById(tx).value; 
   if(parseFloat(document.getElementById(gtot).value)>0)
   gt=parseFloat(gt)+parseFloat(document.getElementById(gtot).value); 

   if(t.length<=0) 
     t=0;
   tottax1=parseFloat(tottax1)+parseFloat(t);
}

document.getElementById(totsupq).value=parseFloat(tot).toFixed(2);
document.getElementById('tottx').value=parseFloat(tottax1).toFixed(2);
document.getElementById('ggtot').value=parseFloat(gt).toFixed(2);

 }
</script>

