<?php 
 echo @$_GET['msg'];
  // session_start();
  // if(isset($_SESSION['userId'])){ 
if(isset($_POST['accname'])){
extract($_POST);
if($accname==''){
	echo "Please Enter Account Title";
}
	else
	{
	$accname=strtoupper($accname);
$dated=date("Y/m/d");
                    require 'connection.php'; 

  $qry = "SELECT accname FROM accreg where accname='$accname'";
   $run = mysqli_query($connect, $qry) or die(mysqli_error($connect));
   $row = mysqli_fetch_array($run);
   $fetgrpname=$row['accname'];
   if($fetgrpname==$accname){
   	echo "This Account Title already exist ! ";
   }
   else{

$supname="SELECT max(accid) as accid from accreg";
	$suprun=mysqli_query($connect,$supname) or die(mysqli_error($connect));
	$suprow=mysqli_fetch_array($suprun);
	$accid= $suprow['accid']+1;
	
	 $stockqty="INSERT INTO `accreg`(`dated`,`accid`,`accno`,`groupid`,`accname`, `address`, `phone`, `accid2`) VALUES('$dated','$accid','$accid','$grpid','$accname','$adrsbox','$contbox','$acctype')";
	$stockresult=mysqli_query($connect,$stockqty) or die(mysqli_error($connect));

$chkopbal=0;
$supname="SELECT debit+credit  as balnc from ledger where accid='$accid' and entrytype='Adjustment'";
	$suprun=mysqli_query($connect,$supname) or die(mysqli_error($connect));
	$suprow=mysqli_fetch_array($suprun);
	$chkopbal= $suprow['balnc'];
	if($opbal>0){
if($chkopbal==0){
	if($baltype==0){
		// debit
	$description='OPENING BALANCE';
	 $lgropbal="INSERT INTO `ledger`(`description`,`dated`,`accid`,`vno`,`debit`,`entrytype`) VALUES('$description','$dated','$accid','0','$opbal','Adjustment')";
	$stockresult=mysqli_query($connect,$lgropbal) or die(mysqli_error($connect));
}
	if($baltype==1){
		// credit
	$description='OPENING BALANCE';
	
	 $lgropbal="INSERT INTO `ledger`(`description`,`dated`,`accid`,`vno`,`credit`,`entrytype`) VALUES('$description','$dated','$accid','0','$opbal','Adjustment')";
	$stockresult=mysqli_query($connect,$lgropbal) or die(mysqli_error($connect));
}
}
}

echo "Account created....";
   }


}

}
// **************************************EDIT ACCOUNT********************************
if(isset($_POST['accid'])){
extract($_POST);
if($accnam==''){
	echo "Please Enter Account Title";
}
	else
	{
	$accnam=strtoupper($accnam);
$dated=date("Y/m/d");
 require 'connection.php'; 
  //  
    $purqry = "UPDATE `accreg` SET accname='$accnam',groupid='$grpid',address='$adrsbox',phone='$contbox' where accid=$accid";
		$purrun = mysqli_query($connect, $purqry) or die(mysqli_error($connect));
echo "Account Updated....";
   }


}

// **************************************Delete ACCOUNT********************************
if(isset($_POST['acciddlt'])){
extract($_POST);

 require 'connection.php'; 
  //  
    $purqry = "UPDATE `accreg` SET activityacc=1 where accid=$acciddlt";
		$purrun = mysqli_query($connect, $purqry) or die(mysqli_error($connect));
echo "Account Deleted....";


}


// }

?>
