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

require 'connection.php';

// Fetch Voucher No.
  $qryvno = "SELECT max(vno) as vno FROM feedinout";
             $runvno = mysqli_query($connect, $qryvno) or die(mysqli_error($connect));
            $rowvno = mysqli_fetch_array($runvno);
            $vno=$rowvno['vno']+1;


 $qryvno = "SELECT BiltyCr FROM accreg where accid='$supid'";
             $runvno = mysqli_query($connect, $qryvno) or die(mysqli_error($connect));
            $rowvno = mysqli_fetch_array($runvno);
            $ComBilty=$rowvno['BiltyCr'];

 $qryvno = "SELECT fullrate as fullrate FROM feedrate where itemid='$itemid' and accid='$supid'";
             $runvno = mysqli_query($connect, $qryvno) or die(mysqli_error($connect));
            $rowvno = mysqli_fetch_array($runvno);
            $FullRate=$rowvno['fullrate'];

 $qryvno = "SELECT fdpercent,BiltyCr FROM accreg where accid='$custid'";
             $runvno = mysqli_query($connect, $qryvno) or die(mysqli_error($connect));
            $rowvno = mysqli_fetch_array($runvno);
            $CustPercent=$rowvno['fdpercent'];
            $CustBiltyCr=$rowvno['BiltyCr'];
            $crate=($FullRate)-(($FullRate)*($CustPercent)/100);

if($ComBilty==1){
	$pbilty=1;
}
else{
	$pbilty=0;
	$CustBiltyCr=0;
}

    
// Insert into Feed In Out table
 $query="INSERT INTO `feedinout`(`dated`,`vno`, `supid`, `custid`, `qty`, `srate`, `crate`, `itemid`, `biltyexp`, `posted`) VALUES('$dated','$vno','$supid','$custid','$qty','$srate','$crate','$itemid','$frieght','Posted')";
	$result=mysqli_query($connect,$query) or die(mysqli_error($connect));

// Insert into Set Rate table
 $query="INSERT INTO `setrate`(`accid`, `itemid`, `prate`) VALUES('$supid','$itemid','$srate')";
	$result=mysqli_query($connect,$query) or die(mysqli_error($connect));

// Insert into Set Rate table
 $querycust="INSERT INTO `setrate`(`accid`, `itemid`, `srate`) VALUES('$custid','$itemid','$crate')";
	$resultcust=mysqli_query($connect,$querycust) 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('1','$dated','$supid','$vno','$refno','Purchase','$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('1','$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.' On '. $refno2;
		$desbilty=' Bilty of Purchase# '. $vno;
// For Supplier account ledger
		// For Detail Ledger
	 $ledgerqrydetail="INSERT INTO `ledger`(`smid`,`dated`, `accid`, `description`, `vno`, `refno`, `entrytype`, `itemid`,`rate`,`qty`) VALUES('1','$dated','$supid','$des','$vno','$refno','Purchase','$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('1','$dated','$supid','$des','$vno','$refno','Purchase','$costin')";
	$ledgerrun=mysqli_query($connect,$ledgerqry) or die(mysqli_error($connect));
	
	// For Pending Bilty
	$pbilty = $_POST['custbilty2'];
	if($ComBilty==1){

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

	}

	$bll = $_POST['frieght'];
if($bll>1){

// For Bilty Amount Debit in Supplier Account

 $ledgerqry="INSERT INTO `ledger`(`smid`,`dated`, `accid`, `description`, `vno`, `refno`, `entrytype`, `debit`) VALUES('1','$dated','$supid','$desbilty','$vno','$refno','Purchase','$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('1','$dated','3','$desbilty','$vno','$refno','Purchase','$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('1','$dated','1','$des','$vno','$refno','Purchase','$costin')";
	$saleledgerrun=mysqli_query($connect,$saleledgerqry) or die(mysqli_error($connect));

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

	// Insert  Ledger table For Customer ACCOUNT
	
		$descust=$qty . ' '.$acname.' @ '.$crate.' Bilty '. $frieght;
	
	// For Detail Ledger
	 $ledgerqrydetail2="INSERT INTO `ledger`(`smid`,`dated`, `accid`, `description`, `vno`, `refno`, `entrytype`, `itemid`,`rate`,`qty`) VALUES('1','$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('1','$dated','$custid','$descust','$vno','$refno','Sales','$costout')";
	$ledgerrun2=mysqli_query($connect,$ledgerqry2) or die(mysqli_error($connect));

 $blty = $CustBiltyCr;

// 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('1','$dated','3','$desbilty','$vno','$refno','Purchase','$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('1','$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('1','$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');
}
 ?>
