package main_package;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

public class DataBaseMethodsDuplicate 
{
	 public String selectQueWithBranch(String query,int branchId)
		{
			
			if(branchId ==-1)
			{
				
			}
			else
			{
				if(query.contains("where"))
				{
					query=query+" and branch_id='"+branchId+"'";
				}
				else
				{
					query=query+" where branch_id='"+branchId+"'";
				}
			}
			
			System.out.println(query);
			return query;
		}
	
	public boolean centerDuplicacy(String centerName, String centerCode, Connection conn)
	{
		Statement st=null;
		ResultSet rr=null;
		try
		{
			 st=conn.createStatement();
			String query="select * from center where (centerName='"+centerName+"' and centerCode='"+centerCode+"')";
			 rr=st.executeQuery(query);
			if(rr.next())
			{
				return true;
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			try {
				if(st!=null)
				{
					st.close();
				}
				if(rr!=null)
				{
					rr.close();
				}
			} catch (Exception e2) {
				// TODO: handle exception
			}
		
		}
		return false;
	}
	
	public boolean RD_FD_Duplicacy(String user_no)
	{
		try (Connection con = DataBaseConnection.JAVAConnection();) 
		{
			Statement st = con.createStatement();
			String qq = "select * from customer_info where user_no='" + user_no+ "'";
			ResultSet rs = st.executeQuery(qq);
			if (rs.next()) 
			{
				return true;
			}
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		return false;
	}

	public boolean shareNoDuplicacy(String user_no)
	{
		try (Connection con = DataBaseConnection.JAVAConnection();) 
		{
			Statement st = con.createStatement();
			String qq = "select * from shareholder_info where shareholderno='"+ user_no + "'";
			ResultSet rs = st.executeQuery(qq);
			if (rs.next()) 
			{
				return true;
			}
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		return false;
	}

	public int applicationNoDuplicacy(String office_application_no) 
	{
		int i = 0;
		try (Connection con = DataBaseConnection.JAVAConnection();) 
		{
			Statement st = con.createStatement();
			String qq = "select * from shareholder_info where office_application_no='"+ office_application_no + "'";
			ResultSet rs = st.executeQuery(qq);
			if (rs.next()) 
			{
				i++;
			}
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		return i;
	}

	public boolean receiptDuplicacy(String receipt_no)
	{
		try (Connection con = DataBaseConnection.JAVAConnection();)
		{
			Statement st = con.createStatement();
			String qq = "select * from payment_receipt where receipt_no='"+ receipt_no + "'";
			ResultSet rs = st.executeQuery(qq);
			if (rs.next())
			{
				return true;
			}
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		return false;
	}
	
	 public boolean savingAccountNoDuplicacy(String no) 
	{
		try (Connection con = DataBaseConnection.JAVAConnection();) 
		{
			Statement st = con.createStatement();
			String qq = "select * from savingaccount where accountNo='"+no+"'";
			ResultSet rs = st.executeQuery(qq);
			if (rs.next()) 
			{
				return true;
			}
		} 
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return false;
	}

	public boolean loanReceiptDuplicacy(String receipt_no) 
	{
		try (Connection con = DataBaseConnection.JAVAConnection();)
		{
			Statement st = con.createStatement();
			String qq = "select * from loanpayment where receipt_no='"+ receipt_no + "'";
			ResultSet rs = st.executeQuery(qq);
			if (rs.next())
			{
				return true;
			}
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		return false;
	}

	public boolean checkDuplicateFDRExpense(int branchId,String itemName, Connection conn) 
	{
		Statement st=null;
		ResultSet rs=null;
		try
		{
			st = conn.createStatement();
			String qq = "select * from fixed_deposit_expense where fdr_no='"+ itemName + "'";
			rs = st.executeQuery(selectQueWithBranch(qq,branchId));
			if (rs.next())
			{
				return true;
			}
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		finally
		{
			try {
				if(st!=null)
				{
					st.close();
				}
				if(rs!=null)
				{
					rs.close();
				}
			} catch (Exception e2) {
				// TODO: handle exception
			}
		}
		return false;
	}
	
	public boolean checkDuplicateItemExpense(int branchId,String itemName) 
	{
		try (Connection con = DataBaseConnection.JAVAConnection();)
		{
			Statement st = con.createStatement();
			String qq = "select * from fixed_assets_items where name='"+ itemName + "'";
			ResultSet rs = st.executeQuery(selectQueWithBranch(qq,branchId));
			if (rs.next())
			{
				return true;
			}
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		return false;
	}
	
	
	public boolean checkDuplicateOtherExpenseCategory(int branchId,String itemName) 
	{
		try (Connection con = DataBaseConnection.JAVAConnection();)
		{
			Statement st = con.createStatement();
			String qq = "select * from other_expense_category where categoryName='"+ itemName + "'";
			ResultSet rs = st.executeQuery(selectQueWithBranch(qq,branchId));
			if (rs.next())
			{
				return true;
			}
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		return false;
	}

	public boolean checkDuplicateOtherIncomeCategory(int branchId,String itemName, Connection conn, String selectedCategoy) 
	{
		Statement st=null;
		ResultSet rs=null;
		try 
		{
			 st = conn.createStatement();
			String qq = "select * from other_income_category where categoryName='"+ itemName + "' and category_type='"+selectedCategoy+"'";
			rs = st.executeQuery(selectQueWithBranch(qq,branchId));
			if (rs.next())
			{
				return true;
			}
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		finally
		{
			try {
				if(st!=null)
				{
					st.close();
				}
				if(rs!=null)
				{
					rs.close();
				}
			} catch (Exception e2) {
				// TODO: handle exception
			}
		}
		return false;
	}
	
	public boolean checkPlanDuplicacy(int branchId,String type, String plan, String interest, Connection conn) 
	{
		Statement st=null;
		ResultSet rs=null;
		try
		{
			st = conn.createStatement();
			String qq = "select * from plans where type='"+type+"' and plan='"+plan+"' and interest='"+interest+"'";
			rs = st.executeQuery(selectQueWithBranch(qq,branchId));
			if (rs.next())
			{
				return true;
			}
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		finally
		{
			try {
				if(st!=null)
				{
					st.close();
				}
				if(rs!=null)
				{
					rs.close();
				}
			} catch (Exception e2) {
				// TODO: handle exception
			}
		}
		return false;
	}

	public boolean checkUserNameDuplicacy(String uname)
	{
		try(Connection con = DataBaseConnection.JAVAConnection();)
		{
			Statement st = con.createStatement();
			String qq = "select * from employee where uname='"+uname+"' and status='active'";
			ResultSet rs = st.executeQuery(qq);
			if (rs.next())
			{
				return true;
			}
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		return false;
	}
	
	public boolean checkLoginDuplicacy(String uname, Connection conn)
	{
		Statement st=null;
		try
		{
			 st = conn.createStatement();
			String qq = "select * from login where userid='"+uname+"'";
			ResultSet rs = st.executeQuery(qq);
			if (rs.next())
			{
				return true;
			}
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}

finally
		{
			try {
				if(st!=null)
				{
					st.close();
				}
				
			} catch (Exception e2) {
				// TODO: handle exception
			}
		}
		
		return false;
	}

	public boolean checkAccHead(int branchId,String type, String name, Connection conn)
	{
		Statement st=null;
		ResultSet rs=null;
		try
		{
			st=conn.createStatement();
			String qq="select * from account_head where type='"+type+"' and name='"+name+"'";
			rs=st.executeQuery(selectQueWithBranch(qq,branchId));
			if(rs.next())
			{
				return true;
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			try {
				if(st!=null)
				{
					st.close();
				}
				if(rs!=null)
				{
					rs.close();
				}
			} catch (Exception e2) {
				// TODO: handle exception
			}
		
		}
		return false;
	}

	public boolean checkDuplicateGroupLoan(String loanNo, String shareHolderNo)
	{
		try(Connection con=DataBaseConnection.JAVAConnection();)
		{
			Statement st=con.createStatement();
			String qq="select id from grouploan where loanNo='"+loanNo+"' and teamLeader='"+shareHolderNo+"' and status='Approve' and paidStatus='unpaid' ";
			ResultSet rs=st.executeQuery(qq);
			if(rs.next())
			{
				return true;
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return false;
	}
	
	public boolean checkShareHolderActivation(String userid) 
	{
		try(Connection con = DataBaseConnection.JAVAConnection();)
		{
			Statement st = con.createStatement();
			String qq = "select id from shareholder_info where shareholderno='"+userid+"' and account_status='active'";
			ResultSet rs = st.executeQuery(qq);
			if (rs.next())
			{
				return true;
			}
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		return false;
	}

}
