<?php 

require 'connection.php';

for($count = 0; $count<count($_POST['hidden_vno']); $count++)
{
 	$vno= $_POST['hidden_vno'][$count];
 	$refno= $_POST['hidden_refno'][$count];
 	$dated= $_POST['hidden_dated'][$count];
 	$supid= $_POST['hidden_accid'][$count];
 	$custid= $_POST['hidden_custid'][$count];
 	$qty= $_POST['hidden_qty'][$count];
 	$srate= $_POST['hidden_prate'][$count];
 	$crate= $_POST['hidden_rate'][$count];
 	$itemid= $_POST['hidden_itemid'][$count];
 	$samt= 0 ;
 	
 	$kat= 0 ;
 	$camt= $qty * $crate ;


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

// Insert into Broiler Transfer table
 $query="INSERT INTO `broilertransfer`(`vno`, `dated`, `supid`, `accid`, `itemid`, `vrate`, `crate`, `qty`, `kat`, `refno`, `status`) VALUES('$vno','$dated','$supid','$custid','$itemid','$srate','$crate','$qty','$kat','$refno','Posted')";
	$result=mysqli_query($connect,$query) or die(mysqli_error($connect));
// Insert into Stock Ledger table
	$costout= ($qty)*($crate);
	$costoutsup= ($qty)*($srate);

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


 $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;

	
	$des2=' weight= '.$qty.' @'.$srate;
	// PURCHASE
 $ledgerqry="INSERT INTO `ledger`(`dated`, `accid`, `description`, `vno`, `refno`, `entrytype`, `credit`) VALUES('$dated','$supid','$des2','$vno','$refno','BRL-PR','$costoutsup')";
	$ledgerrun=mysqli_query($connect,$ledgerqry) or die(mysqli_error($connect));

	$saleledgerqry="INSERT INTO `ledger`(`dated`, `accid`, `description`, `vno`, `refno`, `entrytype`, `debit`) VALUES('$dated','1','$des2','$vno','$refno','BRL-PR','$costoutsup')";
	$saleledgerrun=mysqli_query($connect,$saleledgerqry) or die(mysqli_error($connect));
	
	// SALES
 $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($result){
		echo ("Voucher Post");
	}