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

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

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

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


//**********************************************
if($save)
{
 if(strlen($da)<=0)
 {
  print "<script> alert('Invalid Customer'); history.back(1); </script>";
  return;
 }
  $re=mysqli_query($db,"select * from item_purchase where code='$item'") or die($tbl.' err:'.mysqli_error());
$row=mysqli_fetch_array($re);
if(strlen($cust)>0)
 $customer=$cust;
else
 $customer=$dea;
$invdate=$y.'-'.$m.'-'.$d;
    $sql="insert into invoice_item (code,item_code,invno,brcode,da,soldto,saletype,invdate,qty,chasis) values('$row[code]','$row[item_code]','$invno','$brcode','$da','$customer','adj','$invdate','1','$chasis')";
  mysqli_query($db,$sql) or die('Invoice Err:'.mysqli_error());

$sql2="update item_purchase set sale_count=(sale_count+1) ,sale_invoice='$invno',sale_da='$da' where code='$row[code]' and brcode='$brcode'";
mysqli_query($db,$sql2) or die('Update Err:'.mysqli_error());

print "<font color=green><blink><b> Data has been saved</b></font></blink>";
}
?>
<script>
chasisar=new Array();
 function valid()
 {
  if(document.f1.cust.selectedIndex<=0 && document.f1.dea.selectedIndex<=0)
  {
    alert('Invalid Customer/Dealer');
    return false;
  }
  if(document.f1.da.value.length<=0)
  {
    alert('Invalid DA');
    return false;
  }
  if(document.f1.chasis.value.length<=0)
  {
    alert('Invalid Chasis');
    return false;
  }

 }
function setChasis()
{
 document.f1.chasis.value=chasisar[document.f1.item.selectedIndex];
}
 
</script>

<style>
body,td{font-family:'arial narrow';letter-spacing:1;}
tr{background-color:c1d7bb}
.mnu{margin-left:5}
</style>
<BODY BGCOLOR='IVORY'>
<center>
<style>
.x{font-size:13.96}
th{background-color:598259;color:ivory}
</style>

<form name=f1 action='<?print $PHP_SELF?>' onSubmit="return valid()">
<table>
<th colspan=2><b>STOCK ADJUSTMENT - ITEMS</b>
<tr><td>Select Item</td><td><select name='item' onChange=setChasis()><option></option>
<?
$tot=0;
 $rr=mysqli_query($db," select a.itname,b.code,b.chasis from itemmaster a,item_purchase b where a.code=b.item_code and (b.qty-b.sale_count)>0 and b.brcode='$brcode'")or die('itemmaster err'.mysqli_error());
$i=1;
  while($item_row=mysqli_fetch_array($rr))
{
 print "\n<option value='$item_row[1]'>$item_row[0]-$item_row[chasis]</option>";
 print "<script>chasisar[$i]='$item_row[chasis]'</script>";
 $i++;
}
?>
</select> (Item Name and Chasis)</td></tr>
<tr><td>
<font color=red>ENTER DA </td><td><input type=text name='da' size=19></font>
</td></tr>

<tr><td>
<font color=red>ENTER Chasis </td><td><input type=text name='chasis' size=19></font>
</td></tr>

<tr> <td>Veh Invoice Date </td><td>
dd <input type=text name='d' size=2 value='<?print date('d');?>'>
mm <input type=text name='m' size=2 value='<?print date('m');?>'>
yy <input type=text name='y' size=4 value='<?print date('Y');?>'>
</td> </tr>
<tr>
<td>Veh Invoice No</td><td><input type=text name='invno'></td></tr>
<tr>
<td>Customer </td><td><select name='cust'><option></option>
<?
 $cusres=mysqli_query($db,"select acname from cust_master where branch='$brcode' order by acname")or die(mysqli_error());
 while($cusrow=mysqli_fetch_row($cusres))
 {
   print "<option>$cusrow[0]</option>";
 }
?>
</select></td></tr>

<tr>
<td>Dealer </td><td><select name='dea'><option></option>
<?
 $cusres=mysqli_query($db,"select brname from br_master where brcode='$brcode' order by brname")or die(mysqli_error());
 while($cusrow=mysqli_fetch_row($cusres))
 {
   print "<option>$cusrow[0]</option>";
 }
?>
</select></td></tr>
<th colspan=2 align=center><input type=submit value='SAVE' name='save'></td></tr>
</table>

</form>
