 <?php 
if(isset($_POST['vno'])){
extract($_POST);
require 'connection.php';
$posted='posted';
$qry="DELETE from ledger where vno='$vno' and entrytype='BRL-PR' or vno='$vno' and entrytype='BRL-SL'";
  	  $run = mysqli_query($connect, $qry) or die(mysqli_error($connect));

$qry="DELETE from stockledger where vno='$vno' and invoiceno='BRL-PR' or vno='$vno' and invoiceno='BRL-SL'";
  	  $run = mysqli_query($connect, $qry) or die(mysqli_error($connect));

      // For Broiler Voucher Update In Table
      $qry = "UPDATE `broilertransfer` SET dated='$dated' ,supid='$supid' ,accid='$custid' ,itemid='$itemid' ,vrate='$srate' ,crate='$crate' ,qty='$qty' , refno='$refno', status='$posted', posted='$posted' where vno='$vno' ";
    $run = mysqli_query($connect, $qry) or die(mysqli_error($connect)); 

$samt= 0 ;
$kat= 0 ;
$camt= $qty * $crate;
$costin= ($qty)*($srate);
if($srate>0){

// **************Purchase record***************


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

// Insert  Ledger table FOR CUSTOMER ACCOUNT
  $supname="SELECT * from accreg where accid='$supid'";
  $suprun=mysqli_query($connect,$supname) or die(mysqli_error($connect));
  $suprow=mysqli_fetch_array($suprun);
  $acname= $suprow['accname'];
$des='Weight= '.$qty.' @ '.$srate;


 $ledgerqry="INSERT INTO `ledger`(`dated`, `accid`, `description`, `vno`, `refno`, `entrytype`, `credit`) VALUES('$dated','$supid','$des','$vno','$refno','BRL-PR','$costin')";
  $ledgerrun=mysqli_query($connect,$ledgerqry) or die(mysqli_error($connect));

  $saleledgerqry="INSERT INTO `ledger`(`dated`, `accid`, `description`, `vno`, `refno`, `entrytype`, `debit`) VALUES('$dated','1','$des','$vno','$refno','BRL-PR','$costin')";
  $saleledgerrun=mysqli_query($connect,$saleledgerqry) or die(mysqli_error($connect));

}

// ******************************************



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

// Insert  Ledger table FOR CUSTOMER ACCOUNT
	$supname="SELECT * from accreg where accid='$supid'";
	$suprun=mysqli_query($connect,$supname) or die(mysqli_error($connect));
	$suprow=mysqli_fetch_array($suprun);
	$acname= $suprow['accname'];

$des='Weight= '.$qty.' @ '.$crate;

 $ledgerqry="INSERT INTO `ledger`(`dated`, `accid`, `description`, `vno`, `refno`, `entrytype`, `debit`) VALUES('$dated','$custid','$des','$vno','$refno','BRL-SL','$costout')";
	$ledgerrun=mysqli_query($connect,$ledgerqry) or die(mysqli_error($connect));

	$saleledgerqry="INSERT INTO `ledger`(`dated`, `accid`, `description`, `vno`, `refno`, `entrytype`, `credit`) VALUES('$dated','2','$des','$vno','$refno','BRL-SL','$costout')";
	$saleledgerrun=mysqli_query($connect,$saleledgerqry) or die(mysqli_error($connect));
	
	if($saleledgerrun){
		echo ("Record Updated Successfully !");
	}
	}


// ***********************delete voucher*********************


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

	$saleledgerqry="DELETE FROM broilertransfer where vno='$vnodlt'";
	$saleledgerrun=mysqli_query($connect,$saleledgerqry) or die(mysqli_error($connect));

	$saleledgerqry="DELETE FROM ledger where vno='$vnodlt' and entrytype='BRL-SL'";
	$saleledgerrun=mysqli_query($connect,$saleledgerqry) or die(mysqli_error($connect));

	$saleledgerqry="DELETE FROM ledger where vno='$vnodlt' and entrytype='BRL-PR'";
	$saleledgerrun=mysqli_query($connect,$saleledgerqry) or die(mysqli_error($connect));

$saleledgerqry="DELETE FROM stockledger where vno='$vnodlt' and invoiceno='BRL-PR'";
	$saleledgerrun=mysqli_query($connect,$saleledgerqry) or die(mysqli_error($connect));

$saleledgerqry="DELETE FROM stockledger where vno='$vnodlt' and invoiceno='BRL-SL'";
	$saleledgerrun=mysqli_query($connect,$saleledgerqry) or die(mysqli_error($connect));

	if($saleledgerrun){
		echo ("Record Deleted Successfully !");
	}
}


?>