<?php 

require 'connection.php';

for($count = 0; $count<count($_POST['hidden_itemid']); $count++)
{
	
 	$srno= $_POST['hidden_srno'][$count];
 	$itemid= $_POST['hidden_itemid'][$count];
 	$qty= $_POST['hidden_qty'][$count];
 	$rate= $_POST['hidden_rate'][$count];
 	$accid= $_POST['hidden_accid'][$count];
 	$dated= $_POST['hidden_dated'][$count];
 	$vno= $_POST['hidden_vno'][$count];
 	$vno3= $_POST['hidden_vno'][$count];

   $qry = "SELECT posted FROM purchase where prid = '$vno' and posted='posted'";

   $run = mysqli_query($connect, $qry) or die(mysqli_error($connect));
   $rowposted = mysqli_fetch_array($run);

  if ($run->num_rows > 0) {
  
$msg= "Invoice already saved ! operation failed ! or some field empty !";
die($msg);
}
else {

$amount=$rate * $qty;


// ************************************SALES TABLE INSERT*****************************************

// Check if Sales no exist or not
$chkprid= $vno;
  $qry = "SELECT prid FROM purchase  where prid = '$chkprid'";
   $run = mysqli_query($connect, $qry) or die(mysqli_error($connect));
   $count1 = mysqli_num_rows($run);
		if($count1==1){

			// Total amount create
	$qry2 = "SELECT totalamt FROM purchase  where prid = '$chkprid'";
   $run2 = mysqli_query($connect, $qry2) or die(mysqli_error($connect));
   $row2 = mysqli_fetch_array($run2);
   $prtotalamt= $row2['totalamt'];

   $totalaount= ($prtotalamt)+($amount);
	$qry1 = "UPDATE `purchase` SET dated='$dated',prid='$vno',accid='$accid' ,totalamt='$totalaount' where prid=$vno";
	$run = mysqli_query($connect, $qry1) or die(mysqli_error($connect));

 	}
 	else{
// Insert into Sales table
 $query="INSERT INTO `purchase`(`prid`, `dated`, `Invono`, `accid`, `totalamt`) VALUES('$vno','$dated','$vno','$accid','$amount')";
	$result=mysqli_query($connect,$query) or die(mysqli_error($connect));
 	}

// ***********************************END SALES TABLE INSERT******************************************

// ********************************START SET RATE TABLE INSERT****************************************

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

// ********************************END SET RATE TABLE INSERT****************************************


// *****************************SRTART STOCK LEDGER TABLE INSERT*************************************
	// Insert into stoctLedger table
	$pramount= $qty* $rate;
	$types= 'Med-PR';
	$query1="INSERT INTO `stockledger`(`srno`, `def`, `dated`, `accid`, `vno`, `refno`, `invoiceno`, `itemid`, `stockin`, `ratein`, `costin`) VALUES('$srno', '5', '$dated','$accid','$vno','$vno','$types','$itemid','$qty','$rate','$pramount')";
	$result1=mysqli_query($connect,$query1) or die(mysqli_error($connect));

// *****************************END STOCK LEDGER TABLE INSERT*************************************

// *****************************START LEDGER DETAIL TABLE INSERT*************************************

	// Insert into ledger
	$desc= 'Purchase # '. $vno;
$insrtledger="INSERT INTO `ledger`(`smid`, `dated`, `accid`, `vno`, `refno`, `entrytype`, `description`, `itemid`, `qty`, `rate`) VALUES('5', '$dated','$accid','$vno','$vno','$types','$desc','$itemid','$qty','$rate')";
	$resultledger=mysqli_query($connect,$insrtledger) or die(mysqli_error($connect));
// **********************END LEDGER DETAIL TABLE INSERT***************************
}

}



require 'connection.php';

    $posted="";
   $qrysl = "SELECT prid FROM purchase where  posted='$posted'";
   $runsl = mysqli_query($connect, $qrysl) or die(mysqli_error($connect));
   while($rowsl = mysqli_fetch_array($runsl)){
    $vno=$rowsl['prid']; 
    $tamount=0;
    
   $qry = "SELECT costin FROM stockledger where vno = $vno and def='5'";
   $run = mysqli_query($connect, $qry) or die(mysqli_error($connect));
   while($row = mysqli_fetch_array($run)){
    $tamount= $tamount+  $row['costin'];
}

 			// Get account ID grom Sales table
      $qry = "SELECT * FROM purchase where prid='$vno'";
      $run = mysqli_query($connect, $qry) or die(mysqli_error($connect));
   		$row = mysqli_fetch_array($run);
   	$accid= $row['accid'];
		$dated= $row['dated'];
		$Invono= $row['Invono'];
    $posted='posted';
  //       // For Sales Update Posted
    $purqry = "UPDATE `purchase` SET totalamt='$tamount',dated='$dated',Invono='$Invono',accid='$accid' ,posted='$posted' where prid=$vno";
		$purrun = mysqli_query($connect, $purqry) or die(mysqli_error($connect));

 // For Ledger Instert Party Account
    $desc= 'Purchase # '.$vno;
 $ledgerqry="INSERT INTO `ledger`(`smid`, `dated`, `accid`, `description`, `vno`, `refno`, `entrytype`, `credit`) VALUES('5', '$dated','$accid','$desc','$vno','$vno','Med-PR','$tamount')";
  $ledgerrun=mysqli_query($connect,$ledgerqry) or die(mysqli_error($connect));
  
// For Ledger Instert Sales Account
    $desc= 'Purchase # '.$vno;
 $purqry="INSERT INTO `ledger`(`smid`, `dated`, `accid`, `description`, `vno`, `refno`, `entrytype`, `debit`) VALUES('5', '$dated','1','$desc','$vno','$vno','Med-PR','$tamount')";
  $purrun=mysqli_query($connect,$purqry) or die(mysqli_error($connect));
}

if($runsl){
  echo "Invoice Posted successfully !";
}


 ?>