 <?php 
if(isset($_POST['prvno'])){
extract($_POST);
require 'connection.php';

	$qry="DELETE from stockledger where vno='$prvno' and itemid='$itemid' and def=5";
  	  $run = mysqli_query($connect, $qry) or die(mysqli_error($connect));


	$qry="DELETE from ledger where vno='$prvno' and itemid='$itemid' and smid=5";
  	  $run = mysqli_query($connect, $qry) or die(mysqli_error($connect));


	$qry="DELETE from ledger where vno='$prvno' and itemid='0' and smid=5 and entrytype='Med-PR'";
  	  $run = mysqli_query($connect, $qry) or die(mysqli_error($connect));


?>

 <tr>
                    <th>Sr. No.</th>
                    <th>ID</th>
                    <th>Product Name</th>
                    <th>Rate</th>
                    <th>Qty</th>
                    <th>Amount</th>
                    <th>Action</th>
                  </tr>


                  <?php
$sr=1;
$tamt=0;
 $qry = "SELECT * FROM stockledger INNER JOIN itemreg ON stockledger.itemid = itemreg.itemid where vno = $prvno and def='5'";
   $run = mysqli_query($connect, $qry) or die(mysqli_error($connect));
   while($row = mysqli_fetch_array($run)){
	?>

	<tr>
 <td> <?php echo $row['srno']; ?> </td>
 <td > <?php echo $row['itemid']; ?> </td>
 <td> <?php echo $row['itemname']; ?> </td>
<td> <?php echo $row['ratein']; ?> </td>
<td> <?php echo $row['stockin']; ?>  </td>
<td> <?php echo $row['costin']; ?>  </td>
<td> <button id="dltbtn" class="glyphicon glyphicon-trash" style="color: red;"></button>  </td>

</tr>
<?php
	$sr= $sr+1;
	$tamt=$tamt+$row['costin'];
}
?>

<?php


		// Get account ID grom Sales table
      $qry = "SELECT * FROM purchase where prid='$prvno'";
      $run = mysqli_query($connect, $qry) or die(mysqli_error($connect));
   		$row = mysqli_fetch_array($run);
   	$accid= $row['accid'];
		$dated= $row['dated'];
		$Invono= $row['Invono'];
    $posted='posted';
  //       // For Sales Update Posted
    $purqry = "UPDATE `purchase` SET totalamt='$tamt',dated='$dated',Invono='$Invono',accid='$accid' ,posted='$posted' where prid=$prvno";
		$purrun = mysqli_query($connect, $purqry) or die(mysqli_error($connect));

 // For Ledger Instert Party Account
    $desc= 'Purchase # '.$prvno;
 $ledgerqry="INSERT INTO `ledger`(`smid`, `dated`, `accid`, `description`, `vno`, `refno`, `entrytype`, `credit`) VALUES('5', '$dated','$accid','$desc','$prvno','$prvno','Med-PR','$tamt')";
  $ledgerrun=mysqli_query($connect,$ledgerqry) or die(mysqli_error($connect));
  
// For Ledger Instert Sales Account
    $desc= 'Purchase # '.$prvno;
 $purqry="INSERT INTO `ledger`(`smid`, `dated`, `accid`, `description`, `vno`, `refno`, `entrytype`, `debit`) VALUES('5', '$dated','1','$desc','$prvno','$prvno','Med-PR','$tamt')";
  $purrun=mysqli_query($connect,$purqry) or die(mysqli_error($connect));

	?>



	<script type="text/javascript">
	$("#tamt").val(<?php echo $tamt; ?>)




	</script>

<?php
}
 	?>


<!-- ********************sales*********************** -->

 <?php 
if(isset($_POST['slvno'])){
extract($_POST);
echo "OKOK";
require 'connection.php';


	$qry="DELETE from stockledger where vno='$slvno' and itemid='$itemid' and def=4";
  	  $run = mysqli_query($connect, $qry) or die(mysqli_error($connect));


	$qry="DELETE from ledger where vno='$slvno' and itemid='$itemid' and smid=4";
  	  $run = mysqli_query($connect, $qry) or die(mysqli_error($connect));


	$qry="DELETE from ledger where vno='$slvno' and itemid='0' and smid=4";
  	  $run = mysqli_query($connect, $qry) or die(mysqli_error($connect));





$sr=1;
$tamt=0;


 $qry1 = "SELECT accid FROM sales where saleid = '$slvno'";
   $run1 = mysqli_query($connect, $qry1) or die(mysqli_error($connect));
   $acidname = mysqli_fetch_array($run1);

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



?>

  <tr>
 <td style="color: maroon;"> Account Name :  </td>

 <td style="color: maroon;"> <?php echo $accname['accname']; ?> </td>
 <td>   </td>
 <td>   </td>
 <td>   </td>
 
</tr>
  <tr>
                    <th>Sr. No.</th>
                    <th>ID</th>
                    <th>Product Name</th>
                    <th>Rate</th>
                    <th>Qty</th>
                    <th>Amount</th>
                    <th>Action</th>
                  </tr>
<?php

  
 $qry = "SELECT * FROM stockledger INNER JOIN itemreg ON stockledger.itemid = itemreg.itemid where vno = '$slvno' and def='4'";
   $run = mysqli_query($connect, $qry) or die(mysqli_error($connect));
   while($row = mysqli_fetch_array($run)){
	?>


	<tr>
 <td> <?php echo $row['srno']; ?> </td>
 <td> <?php echo $row['itemid']; ?> </td>
 <td> <?php echo $row['itemname']; ?> </td>
<td> <?php echo $row['rateout']; ?> </td>
<td> <?php echo $row['stockout']; ?>  </td>
<td> <?php echo $row['costout']; ?>  </td>

<td> <button id="dltbtn" class="glyphicon glyphicon-trash" style="color: red;"></button>  </td>
</tr>
<?php
	$sr= $sr+1;
	$tamt=$tamt+$row['costout'];
}


// ****************POST BUTTON**************************

require 'connection.php';

  
    
   $qry = "SELECT costout FROM stockledger where vno = $slvno and def='4'";
   $run = mysqli_query($connect, $qry) or die(mysqli_error($connect));
   while($row = mysqli_fetch_array($run)){
    $tamount= $tamount+  $row['costout'];
}

         // Get account ID grom Sales table
      $qry = "SELECT * FROM sales where saleid='$slvno'";
      $run = mysqli_query($connect, $qry) or die(mysqli_error($connect));
         $row = mysqli_fetch_array($run);
      $accid= $row['accid'];
      $dated= $row['dated'];
      $Invono= $row['billno'];
      $accid= $row['accid'];
    $dated= $row['dated'];
    $posted='posted';
  //       // For Sales Update Posted
    $purqry = "UPDATE `sales` SET netamt='$tamount',dated='$dated',billno='$Invono',accid='$accid' ,posted='$posted' where saleid=$slvno";
      $purrun = mysqli_query($connect, $purqry) or die(mysqli_error($connect));

 // For Ledger Instert Party Account
    $desc= 'Sales # '.$slvno;
 $ledgerqry="INSERT INTO `ledger`(`smid`, `dated`, `accid`, `description`, `vno`, `refno`, `entrytype`, `debit`) VALUES('4', '$dated','$accid','$desc','$slvno','$slvno','Med-SL','$tamount')";
  $ledgerrun=mysqli_query($connect,$ledgerqry) or die(mysqli_error($connect));
  
// For Ledger Instert Sales Account
    $desc= 'Sales # '.$slvno;
 $purqry="INSERT INTO `ledger`(`smid`, `dated`, `accid`, `description`, `vno`, `refno`, `entrytype`, `credit`) VALUES('4', '$dated','2','$desc','$slvno','$slvno','Med-SL','$tamount')";
  $purrun=mysqli_query($connect,$purqry) or die(mysqli_error($connect));



?>

	<script type="text/javascript">
	$("#tamt").val(<?php echo $tamt; ?>)

	</script>
<?php
}
?>
// <!-- ********************end sales************************** -->