<?php 
 echo @$_GET['msg'];
  // session_start();
  // if(isset($_SESSION['userId'])){ 
if(isset($_POST['qty'])){
extract($_POST);
if($qty<=0){
	echo "Please Enter Qty";
}
	else
	{
	// $accname=strtoupper($accname);
  require 'connection.php'; 
 
 
   if($prate<0 OR $srate<0 OR $prate=='' OR $srate=='' ){
   	echo "Please Enter Valid Detail ! ";
   }
   else{
 $supamt=$prate*$qty;
  $custamt=$srate*$qty;
$supname="SELECT max(vno) as vno from chicksinout";
	$suprun=mysqli_query($connect,$supname) or die(mysqli_error($connect));
	$suprow=mysqli_fetch_array($suprun);
	$vno= $suprow['vno']+1;
	
	// Insert into Chicks table
 $query="INSERT INTO `chicksinout`(`vno`, `dated`, `supid`, `accid`, `itemid`, `vrate`, `srate`, `qty`) VALUES('$vno','$dated','$supid','$custid','1','$prate','$srate','$qty')";
  $result=mysqli_query($connect,$query) or die(mysqli_error($connect));

// Insert into Stock Ledger table Stock In
  $costin= ($qty)*($prate);
 $stockqty="INSERT INTO `stockledger`(`dated`, `accid`, `vno`, `refno`, `invoiceno`, `itemid`, `stockin`, `ratein`, `costin`) VALUES('$dated','$supid','$vno','$vno','Chk-PR','1','$qty','$srate','$costin')";
  $stockresult=mysqli_query($connect,$stockqty) or die(mysqli_error($connect));

// Insert into Stock Ledger table Stock Out
  $costout= ($qty)*($srate);
 $stockqty="INSERT INTO `stockledger`(`dated`, `accid`, `vno`, `refno`, `invoiceno`, `itemid`, `stockout`, `rateout`, `costout`) VALUES('$dated','$custid','$vno','$vno','Chk-SL','1','$qty','$srate','$costout')";
  $stockresult=mysqli_query($connect,$stockqty) or die(mysqli_error($connect));

// Insert  Ledger table FOR Vendor Account
  $des= 'Chicks  '.$qty. ' @ '.$prate;
 $ledgerqry="INSERT INTO `ledger`(`dated`, `accid`, `description`, `vno`, `refno`, `entrytype`, `credit`) VALUES('$dated','$supid','$des','$vno','$vno','Chk-PR','$costin')";
  $ledgerrun=mysqli_query($connect,$ledgerqry) or die(mysqli_error($connect));

// Insert  Ledger table FOR Purchase Account
  $saleledgerqry="INSERT INTO `ledger`(`dated`, `accid`, `description`, `vno`, `refno`, `entrytype`, `debit`) VALUES('$dated','1','$des','$vno','$vno','Chk-PR','$costin')";
  $saleledgerrun=mysqli_query($connect,$saleledgerqry) or die(mysqli_error($connect));

// Insert Ledger table FOR Customer Account
  $des= 'Chicks  '.$qty. ' @ '.$srate;;
 $ledgerqry="INSERT INTO `ledger`(`dated`, `accid`, `description`, `vno`, `refno`, `entrytype`, `debit`) VALUES('$dated','$custid','$des','$vno','$vno','Chk-SL','$costout')";
  $ledgerrun=mysqli_query($connect,$ledgerqry) or die(mysqli_error($connect));

// Insert  Ledger table FOR Sales Account
  $saleledgerqry="INSERT INTO `ledger`(`dated`, `accid`, `description`, `vno`, `refno`, `entrytype`, `credit`) VALUES('$dated','2','$des','$vno','$vno','Chk-SL','$costout')";
  $saleledgerrun=mysqli_query($connect,$saleledgerqry) or die(mysqli_error($connect));



$qry = "SELECT vno,qty,vrate, srate FROM chicksinout where dated='$dated' order by vno asc";
             $run = mysqli_query($connect, $qry) or die(mysqli_error($connect));
    while($row = mysqli_fetch_array($run)){
$vno=$row['vno'];


  $qry1 = "SELECT supid,accid FROM chicksinout where vno='$vno'";
  $run1 = mysqli_query($connect, $qry1) or die(mysqli_error($connect));
  $row1 = mysqli_fetch_array($run1);
$supid=$row1['supid'];
$accid=$row1['accid'];

$qry1 = "SELECT accname FROM accreg where accid='$supid'";
  $run1 = mysqli_query($connect, $qry1) or die(mysqli_error($connect));
  $row1 = mysqli_fetch_array($run1);
$supliername=$row1['accname'];

$qry1 = "SELECT accname FROM accreg where accid='$accid'";
  $run1 = mysqli_query($connect, $qry1) or die(mysqli_error($connect));
  $row1 = mysqli_fetch_array($run1);
$custname=$row1['accname'];

?>
                
         <tr>
        <td class="center"><?php echo $row['vno']; ?></td>
        <td class="center"><i style="color: green; font-weight: bold;"><?php echo $supliername; ?></i>
          <br><i style="color: brown; font-weight: bold;"><?php echo $custname; ?></i></td>
        <td class="center"><?php echo $row['qty'];?></td>
        <td style="color: green; font-weight: bold;" class="center"><?php echo $row['vrate']; ?></td>
        <td style="color: brown; font-weight: bold;" class="center"><?php echo $row['srate']; ?></td>

  
    </tr>


   <?php } 

   }


}

}
// }

?>
