<link href="css/form.css" type="text/css" rel="stylesheet" media="screen"  />
<html>
<body >


<?
session_start();
include("opendb.php");
include("test.php");
$ker=mysqli_query($db,"select brstate from branch where brcode='$brcode'");
$kar=mysqli_fetch_array($ker);

$re=mysqli_query($db,"select * from mdet where code='$code'");
$row=mysqli_fetch_array($re);

$mtyperes=mysqli_query($db,"select * from minput where code='$code'");
$mtrow=mysqli_fetch_array($mtyperes);

$rr=mysqli_query($db,"select acname from customer where code='$mtrow[cust]'");
$crow=mysqli_fetch_array($rr);

$rr1=mysqli_query($db,"select empname from empmaster where empcode='$mtrow[engineer]'");
$crow1=mysqli_fetch_array($rr1);


$dt=explode('-',$row[dcomm]);


if($save)
{
 $stard=$y1.'-'.$m1.'-'.$d1;
 $endd=$y2.'-'.$m2.'-'.$d2;

$re=mysqli_query($db,"select * from ascdetails where mser='$mser'  and startd>='$stard' and endd<='$endd'");



if(mysqli_num_rows($re)>0)
{
$rr=mysqli_fetch_array($re);
$sql="update ascdetails set        mser='$mser',engineer='$engineer',startd='$stard',endd='$endd',amt='$amt',maxvisit='$maxvisit',maxwd='$maxwd',lastser='$lastser',brcode='$brcode' where mser='$mser' and sno='$rr[sno]'"; print $sql;
 mysqli_query($db,$sql)or die(mysqli_error());
}
else
{
$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);

 $re=mysqli_query($db,"select max(invno) from ascdetails where brcode='$brcode'")or die(mysqli_error());
 $inv_row=mysqli_fetch_row($re);
 if($inv_row[0]<=0)
  $invno=1;
 else 
  $invno=$inv_row[0]+1;
 $entrydate=date("Y-m-d");


 $sql="insert into ascdetails(mser,engineer,startd,endd,amt,maxvisit,maxwd,lastser,jvno,invno,entrydate,ascno,customer) values('".trim($mser)."','$engineer', '$stard','$endd','$amt','$maxvisit','$maxwd','$lastser','$jvno','$invno','$entrydate','$invno','$cust')";

print $sql;

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

//------------------------posting start
$stax=$amt*10.3/100;
$tot=$amt+$stax;

$sql="insert into journal2 values('$entrydate','$jvno','Dr','Asc receivable','".round($tot)."', '0','Asc :$inqno:$prospect:inv-$invno:$lastserv:$user1','$brcode','ServiceCAll','$lastserv','$prospect','$invno')";
mysqli_query($db,$sql) or die("J V Write error 101:".mysqli_error());

$sql="insert into journal2 values('$entrydate','$jvno','Cr','SERVICE TAX 10.3%', '0','$stax','ServiceCall :$inqno:$prospect:inv-$invno:$lastserv:$user1','$brcode','ServiceCAll','$lastserv','$prospect','$invno')";
mysqli_query($db,$sql) or die("J V Write error 2:".mysqli_error());

$sql="insert into journal2 values('$entrydate','$jvno','Cr','Asc Income', '0','$amt','ServiceCall :$inqno:$prospect:inv-$invno:$lastserv:$user1','$brcode','ServiceCAll','$lastserv','$prospect','$invno')";
mysqli_query($db,$sql) or die("J V Write error 2:".mysqli_error());

$jvdifres=mysqli_query($db,"select sum(dramt)-sum(cramt) from journal2 where jvno='$jvno' and brcode='$brcode'");
$jvdifrow=mysqli_fetch_row($jvdifres);
$dif=$jvdifrow[0];

if($dif<0)
{
 $xx=abs($dif);
$sql="insert into journal2 values('$entrydate','$jvno','Dr','Round Off','".$xx."', '0','ServiceCall :$inqno:$prospect:inv-$invno:$lastserv:$user1','$brcode','ServiceCAll','$lastserv','$prospect','$invno')";
mysqli_query($db,$sql) or die("J V Write error dif1:".mysqli_error());
}
else if($dif>0)
{
 $xx=abs($dif);
 $sql="insert into journal2 values('$entrydate','$jvno','Cr','Round Off','0','".$xx."','ServiceCall :$inqno:$prospect:inv-$invno:$lastserv:$user1','$brcode','ServiceCAll','$lastserv','$prospect','$invno')";
mysqli_query($db,$sql) or die("J V Write error dif:".mysqli_error());
}

$jdres=mysqli_query($db,"select sum(dramt),sum(cramt) from journal2 where jvno='$jvno' and brcode='$brcode'");
$jdiff=mysqli_fetch_row($jdres);
if($jdiff[0]!=$jdiff[1]||$jdiff[0]==0)
{
 print "<sctipt> alert('Error in posting. Please check the data now');</script>";
}


//----------------------posting end



}




?>
 <script> 
  opener.document.f1.msno.value='<?print $mser ?>';
  window.close();
 </script>
<?
}
?>
<script>

function valid()
{
if(document.f1.mser.value.length<=0)
 { alert ('Invalid Machine Serial No.'); return false;}
 if(confirm('Do you want to save?'))
  return true;
 else
  return false;
}
var t;
function timecount()
{
 if(!window.closed)
  window.focus();
 t=setTimeout("timecount()",100);
}
</script>
<body onLoad=timecount()>
<center>
<form name='f1' action='<?Print $PHP_SELF ?>' method='post' onSubmit="return valid()">

<table cellspacing=1 cellpadding=1 bgcolor=lightsteelblue>
<th colspan=6 bgColor=steelblue height=21><font color='ivory'><b>A S C  &nbsp; DETAILS</th>
<?
 $re=mysqli_query($db,"select * from ascdetails where mser='$mtrow[msno]'");

  $row=mysqli_fetch_array($re);
  
?>

<tr><td>Machine Serial No.</td><td><input type=text readonly name='mser' value='<?print $mtrow[msno]?>'></td><td>Customer</td><td><input type=text readonly name='cust' value='<?print $crow[0]?>'></td></tr>
<tr><td>Engineer</td><td><input type=text readonly name='engineer' value='<?print $crow1[0]?>'></td>
</tr>
<tr><td>ASC Start Date</td><td>dd<input type=text name='d1' size=2>
				mm<input type=text name='m1' size=2>
				yy<input type=text name='y1' size=4>
</td></tr>
<tr><td>ASC End Date</td><td>dd<input type=text name='d2' size=2>
				mm<input type=text name='m2' size=2>
				yy<input type=text name='y2' size=4>
</td></tr>
<tr><td>Amount</td><td><input type=text name='amt'></td></tr>
<tr><td>Max No. of Visits</td><td><input type=text name='maxvisit'></td></tr>
<tr><td>Max working days</td><td><input type=text name='maxwd' size=5 maxlength=5></td></tr>
<tr>
<td>Last Asc</td><td><select name='lastser'>
<?
 $re=mysqli_query($db,"select criteria from ascmaster order by criteria");
  while($row=mysqli_fetch_array($re))
  {
    print "\n<option value='$row[0]'>$row[0]</option>";
  }
?>
</select>
</td>
</tr>
<tr><td colspan=2 align=center bgcolor='steelblue'> <input type=submit value='Save' name='save'>
<input type=button value='Cancel' onClick=window.close()> </td></tr>
</table>

<br>
<table border=2 cellspacing=1 cellpadding=1 bgcolor='steelblue' align=center>
<tr><td colspan=12 align=center bgcolor='lightsteelblue'>Asc Details </td></tr>

<th>No<th >Msno<th >Engineer<th>AscStartDate
<th>AscEndDate<th >Amount<th >No.of Visit<th> No.of Days
<th>AscBranch<th>LastService<th>Customer<th>Engineer
<?
$inqres1=mysqli_query($db,"select * from ascdetails where mser='$mtrow[msno]'")or die(mysqli_error());

$i=1;
while($inqrow=mysqli_fetch_array($inqres1))
{
 print "<tr bgcolor='white'><td  >$i</td><td>$inqrow[0]</td><td >".getrepname($inqrow[1])."</td><td>".datetohtml($inqrow[2])."</td>";
 print "<td >".datetohtml($inqrow[3])."</td><td >$inqrow[4]</td><td >$inqrow[5]</td>";
 print "<td >$inqrow[6]</td><td>".getbrname($inqrow[8])."</td><td>$inqrow[9]</td><td>$inqrow[14]</td><td>$inqrow[1]</td></tr>";
 $i++;
}
print "</table>";
?>
</form>

<input type=button value="<- Back" onClick=history.back(1) align=center>


