<?php 
 // echo @$_GET['msg'];
 //  session_start();
 //  if(isset($_SESSION['userId'])){ 
if(isset($_POST['camt'])){
extract($_POST);

require 'connection.php';
// Insert into Feed In Out table
 $query="INSERT INTO `feedslret`(`dated`,`vno`, `supid`, `custid`, `qty`, `srate`, `crate`, `itemid`) VALUES('$dated','$vno','$supid','$custid','$qty','$srate','$crate','$itemid')";
	$result=mysqli_query($connect,$query) or die(mysqli_error($connect));

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

// Insert into Stock Ledger table Stockout
	$costout= ($qty)*($crate);
 $stockqty1="INSERT INTO `stockledger`(`def`,`dated`, `accid`, `vno`, `refno`, `invoiceno`, `itemid`, `stockout`, `rateout`, `costout`) VALUES('2','$dated','$custid','$vno','$refno','Sales','$itemid','$qty','$crate','$costout')";
	$stockresult1=mysqli_query($connect,$stockqty1) or die(mysqli_error($connect));


// Insert  Ledger table For Supplier ACCOUNT
	$supname="SELECT * from itemreg where itemid='$itemid'";
	$suprun=mysqli_query($connect,$supname) or die(mysqli_error($connect));
	$suprow=mysqli_fetch_array($suprun);
	$acname= $suprow['itemname'];
	

		$des=$qty . ' / '.$acname;
		$desbilty=' Bilty '. ' of Sales-Return# '. $vno;
// For Supplier account ledger
		// For Detail Ledger
	 $ledgerqrydetail="INSERT INTO `ledger`(`smid`,`dated`, `accid`, `description`, `vno`, `refno`, `entrytype`, `itemid`,`rate`,`qty`) VALUES('2','$dated','$supid','$des','$vno','$refno','Sales-Return','$itemid','$srate','$qty')";
	$ledgerrundetail=mysqli_query($connect,$ledgerqrydetail) or die(mysqli_error($connect));
		// For  Ledger
 $ledgerqry="INSERT INTO `ledger`(`smid`,`dated`, `accid`, `description`, `vno`, `refno`, `entrytype`, `credit`) VALUES('2','$dated','$supid','$des','$vno','$refno','Sales-Return','$costin')";
	$ledgerrun=mysqli_query($connect,$ledgerqry) or die(mysqli_error($connect));
	// For Pending Bilty
	$pbilty = 1;
	if($pbilty==0){

		 $ledgerqry="INSERT INTO `ledger`(`smid`,`dated`, `accid`, `description`, `vno`, `refno`, `entrytype`, `debit`) VALUES('2','$dated','$supid','$desbilty','$vno','$refno','Sales-Return','$frieght')";
	$ledgerrun=mysqli_query($connect,$ledgerqry) or die(mysqli_error($connect));

	}

	$bll = 0;
if($bll>1){

// For Bilty Amount Debit in Supplier Account

 $ledgerqry="INSERT INTO `ledger`(`smid`,`dated`, `accid`, `description`, `vno`, `refno`, `entrytype`, `debit`) VALUES('2','$dated','$supid','$desbilty','$vno','$refno','Sales-Return','$frieght')";
	$ledgerrun=mysqli_query($connect,$ledgerqry) or die(mysqli_error($connect));
// For Bilty Amount Credit in Cash Book Account
 $ledgerqry="INSERT INTO `ledger`(`smid`,`dated`, `accid`, `description`, `vno`, `refno`, `entrytype`, `credit`) VALUES('2','$dated','3','$desbilty','$vno','$refno','Sales-Return','$frieght')";
	$ledgerrun=mysqli_query($connect,$ledgerqry) or die(mysqli_error($connect));
}



$des1 = 'Invoice No. '. $vno;

// Insert  Ledger table For Purchase ACCOUNT
	$saleledgerqry="INSERT INTO `ledger`(`smid`,`dated`, `accid`, `description`, `vno`, `refno`, `entrytype`, `debit`) VALUES('2','$dated','4','$des','$vno','$refno','Sales-Return','$costin')";
	$saleledgerrun=mysqli_query($connect,$saleledgerqry) or die(mysqli_error($connect));

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

	// Insert  Ledger table For Customer ACCOUNT
	
		$descust=$qty . ' '.$acname.' @ '.$crate;
	
	// For Detail Ledger
	 $ledgerqrydetail2="INSERT INTO `ledger`(`smid`,`dated`, `accid`, `description`, `vno`, `refno`, `entrytype`, `itemid`,`rate`,`qty`) VALUES('2','$dated','$custid','$descust','$vno','$refno','Sales','$itemid','$crate','$qty')";
	$ledgerrundetail2=mysqli_query($connect,$ledgerqrydetail2) or die(mysqli_error($connect));


 $ledgerqry2="INSERT INTO `ledger`(`smid`,`dated`, `accid`, `description`, `vno`, `refno`, `entrytype`, `debit`) VALUES('2','$dated','$custid','$descust','$vno','$refno','Sales','$costout')";
	$ledgerrun2=mysqli_query($connect,$ledgerqry2) or die(mysqli_error($connect));

 $blty = 0;

// For Customer Bilty Credit
if($blty>0){
if($pbilty>0){
		 // For Bilty Amount Debiy in Cash Book Account
 $ledgerqry="INSERT INTO `ledger`(`smid`,`dated`, `accid`, `description`, `vno`, `refno`, `entrytype`, `debit`) VALUES('2','$dated','3','$desbilty','$vno','$refno','Sales-Return','$frieght')";
	$ledgerrun=mysqli_query($connect,$ledgerqry) or die(mysqli_error($connect));
	}
	$descust2='Bilty credit of sales No. '.$refno;
$ledgerqrycustbilty="INSERT INTO `ledger`(`smid`,`dated`, `accid`, `description`, `vno`, `refno`, `entrytype`, `credit`) VALUES('2','$dated','$custid','$descust2','$vno','$refno','Sales','$frieght')";
	$ledgerruncustbilty=mysqli_query($connect,$ledgerqrycustbilty) or die(mysqli_error($connect));

}

// Insert  Ledger table For Sales ACCOUNT
$dessales = 'Sales ID '. $vno;

	$saleledgerqry2="INSERT INTO `ledger`(`smid`,`dated`, `accid`, `description`, `vno`, `refno`, `entrytype`, `credit`) VALUES('2','$dated','2','$dessales','$vno','$refno','Sales','$costout')";
	$saleledgerrun2=mysqli_query($connect,$saleledgerqry2) or die(mysqli_error($connect));

	if($result){
		echo ("Voucher Post");
	}
}
// }
// else
// {
// header('location: index.php');
// }
 ?>
