<?php 
 // echo @$_GET['msg'];
 //  session_start();
 //  if(isset($_SESSION['userId'])){ 
if(isset($_POST['debit'])){
extract($_POST);
$description2 = 'Flock No. '.$description;
require 'connection.php';
$dr = $_POST['debit'];
if($dr!==""){
// Insert into Tr Voucher  table
$dated2=date("Y/m/d");

$supname="SELECT max(tvno) as tvno from trvoucher";
	$suprun=mysqli_query($connect,$supname) or die(mysqli_error($connect));
	$suprow=mysqli_fetch_array($suprun);
	$tvno= $suprow['tvno']+1;
	

 $query="INSERT INTO `trvoucher`( `dated2`,`tvno`, `accid`, `dated`, `amount`, `description`, `posted`, `status`) VALUES('$dated2','$tvno','$accid','$dated','$debit','$description2','Posted','Debit')";
	$result=mysqli_query($connect,$query) or die(mysqli_error($connect));

// Insert into Ledger From Account  table
 $query="INSERT INTO `ledger`(`dated2`,`dated`, `accid`, `comid`, `description`, `vno`, `refno`, `entrytype`, `debit`) VALUES('$dated2','$dated','$accid','5','$description2','$tvno','$tvno','Transfer','$debit')";
	$result=mysqli_query($connect,$query) or die(mysqli_error($connect));

	// Insert into Ledger To Account  table
  $query="INSERT INTO `ledger`(`dated2`,`dated`, `accid`, `comid`, `description`, `vno`, `refno`, `entrytype`, `credit`) VALUES('$dated2','$dated','$accidto','5','$description2','$tvno','$tvno','Transfer','$debit')";
	$result=mysqli_query($connect,$query) or die(mysqli_error($connect));

// Insert into PreFlkDtl

	    // data: {accid: accid, tvno: tvno, description: description, dated: dated, debit: debit, credit: credit, accidto: accidto},
$supname="SELECT accid2  from accreg where accid='$accid'";
	$suprun=mysqli_query($connect,$supname) or die(mysqli_error($connect));
	$suprow=mysqli_fetch_array($suprun);
	$expenseid= $suprow['accid2'];
	
	$supname="SELECT groupid  from accreg where accid='$accid'";
	$suprun=mysqli_query($connect,$supname) or die(mysqli_error($connect));
	$suprow=mysqli_fetch_array($suprun);
	$groupid= $suprow['groupid'];

$supname="SELECT groupname  from groupreg where groupid='$groupid'";
	$suprun=mysqli_query($connect,$supname) or die(mysqli_error($connect));
	$suprow=mysqli_fetch_array($suprun);
	$shedname= $suprow['groupname'];


  $query="INSERT INTO `preflkdtl`(`accid`,`flockno`, `expenseid`, `credit`, `fdqty`, `weight`) VALUES('$groupid','$description','$expenseid','$debit','0','0')";
	$result=mysqli_query($connect,$query) or die(mysqli_error($connect));
// **********************************monthly start**********************

$monthrep="SELECT sum(debit)-sum(credit) as balance  from preflkdtl where accid='$groupid' and flockno='$description'";
	$monthrun=mysqli_query($connect,$monthrep) or die(mysqli_error($connect));
	$monthrow=mysqli_fetch_array($monthrun);

	$balance= $monthrow['balance'];

	if($balance>0){

		$monthrep1="SELECT sum(debit)+sum(credit) as chckbal from monthlyreport where flockno='$description' and accid='$groupid'";
	$monthrun1=mysqli_query($connect,$monthrep1) or die(mysqli_error($connect));
	$monthrow1=mysqli_fetch_array($monthrun1);
	

	if($monthrow1['chckbal']>0){

      $query2 = "UPDATE `monthlyreport` SET `dated`='$dated', `debit`='$balance', `shedname`='$shedname' where accid='$groupid' and flockno='$description' ";
    $result1 = mysqli_query($connect, $query2) or die(mysqli_error($connect)); 
	
	
	}
	else
	{
		
	$query="INSERT INTO `monthlyreport`(`dated`,`accid`, `debit`, `flockno`) VALUES('$dated','$groupid','$balance','$description')";
	$result2=mysqli_query($connect,$query) or die(mysqli_error($connect));
	}

	}
	else{
		$balance=$balance*(-1);
		
	$monthrep1="SELECT accid from monthlyreport where flockno='$description' and accid='$groupid'";
	$monthrun1=mysqli_query($connect,$monthrep1) or die(mysqli_error($connect));
	$monthrow1=mysqli_fetch_array($monthrun1);
	if($monthrow1['accid']>0){


      $query = "UPDATE `monthlyreport` SET dated='$dated' ,credit='$balance', shedname='$shedname' where accid='$groupid' and Flockno='$description' ";
    $result = mysqli_query($connect, $query) or die(mysqli_error($connect)); 
	}
	else
	{

	$query="INSERT INTO `monthlyreport`(`dated`,`accid`, `credit`, `flockno`) VALUES('$dated','$groupid','$balance','$description')";
	$result=mysqli_query($connect,$query) or die(mysqli_error($connect));

	}
	}

// **********************************monthly End**********************


if($result){
	echo "Voucher Transfer Successfully";
}
}
$cr = $_POST['credit'];
if($cr!==""){

$dated2=date("Y/m/d");

$supname="SELECT max(tvno) as tvno from trvoucher";
	$suprun=mysqli_query($connect,$supname) or die(mysqli_error($connect));
	$suprow=mysqli_fetch_array($suprun);
	$tvno= $suprow['tvno']+1;
	
// Insert into Tr Voucher  table
 $query="INSERT INTO `trvoucher`(`dated2`,`tvno`, `accid`, `dated`, `amount`, `description`, `posted`, `status`) VALUES('$dated2','$tvno','$accid','$dated','$debit','$description2','Posted','Debit')";
	$result=mysqli_query($connect,$query) or die(mysqli_error($connect));

// Insert into Ledger From Account  table
 $query="INSERT INTO `ledger`(`dated2`,`dated`, `accid`, `comid`, `description`, `vno`, `refno`, `entrytype`, `credit`) VALUES('$dated2','$dated','$accid','5','$description2','$tvno','$tvno','Transfer','$credit')";
	$result=mysqli_query($connect,$query) or die(mysqli_error($connect));

	// Insert into Ledger To Account  table
  $query="INSERT INTO `ledger`(`dated2`,`dated`, `accid`, `comid`, `description`, `vno`, `refno`, `entrytype`, `debit`) VALUES('$dated2','$dated','$accidto','5','$description2','$tvno','$tvno','Transfer','$credit')";
	$result=mysqli_query($connect,$query) or die(mysqli_error($connect));

	    // data: {accid: accid, tvno: tvno, description: description, dated: dated, debit: debit, credit: credit, accidto: accidto},
	$supname="SELECT accid2  from accreg where accid='$accid'";
	$suprun=mysqli_query($connect,$supname) or die(mysqli_error($connect));
	$suprow=mysqli_fetch_array($suprun);
	$expenseid= $suprow['accid2'];
	
	$supname="SELECT groupid  from accreg where accid='$accid'";
	$suprun=mysqli_query($connect,$supname) or die(mysqli_error($connect));
	$suprow=mysqli_fetch_array($suprun);
	$groupid= $suprow['groupid'];

$supname="SELECT groupname  from groupreg where groupid='$groupid'";
	$suprun=mysqli_query($connect,$supname) or die(mysqli_error($connect));
	$suprow=mysqli_fetch_array($suprun);
	$shedname= $suprow['groupname'];



  $query="INSERT INTO `preflkdtl`(`accid`,`flockno`, `expenseid`, `debit`, `fdqty`, `weight`) VALUES('$groupid','$description','$expenseid','$credit','0','0')";
	$result=mysqli_query($connect,$query) or die(mysqli_error($connect));

// **********************************monthly start**********************

$monthrep="SELECT sum(debit)-sum(credit) as balance  from preflkdtl where accid='$groupid' and flockno='$description'";
	$monthrun=mysqli_query($connect,$monthrep) or die(mysqli_error($connect));
	$monthrow=mysqli_fetch_array($monthrun);

	$balance= $monthrow['balance'];

	if($balance>0){

		$monthrep1="SELECT sum(debit)+sum(credit) as chckbal from monthlyreport where flockno='$description' and accid='$groupid'";
	$monthrun1=mysqli_query($connect,$monthrep1) or die(mysqli_error($connect));
	$monthrow1=mysqli_fetch_array($monthrun1);
	

	if($monthrow1['chckbal']>0){

      $query2 = "UPDATE `monthlyreport` SET `dated`='$dated', `debit`='$balance', `shedname`='$shedname' where accid='$groupid' and flockno='$description' ";
    $result1 = mysqli_query($connect, $query2) or die(mysqli_error($connect)); 
	
	
	}
	else
	{
		
	$query="INSERT INTO `monthlyreport`(`dated`,`accid`, `debit`, `flockno`) VALUES('$dated','$groupid','$balance','$description')";
	$result2=mysqli_query($connect,$query) or die(mysqli_error($connect));
	}

	}
	else{
		$balance=$balance*(-1);
		
	$monthrep1="SELECT accid from monthlyreport where flockno='$description' and accid='$groupid'";
	$monthrun1=mysqli_query($connect,$monthrep1) or die(mysqli_error($connect));
	$monthrow1=mysqli_fetch_array($monthrun1);
	if($monthrow1['accid']>0){


      $query = "UPDATE `monthlyreport` SET dated='$dated' ,credit='$balance', shedname='$shedname' where accid='$groupid' and Flockno='$description' ";
    $result = mysqli_query($connect, $query) or die(mysqli_error($connect)); 
	}
	else
	{

	$query="INSERT INTO `monthlyreport`(`dated`,`accid`, `credit`, `flockno`) VALUES('$dated','$groupid','$balance','$description')";
	$result=mysqli_query($connect,$query) or die(mysqli_error($connect));

	}
	}

// **********************************monthly End**********************

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