Package backend.logic

Klasse InvoiceService

java.lang.Object
backend.logic.InvoiceService

public class InvoiceService extends Object
Service class for managing invoices associated with users. Provides functionality to upload, validate, and insert invoices, and to check invoice data against OCR results.
  • Felddetails

  • Konstruktordetails

    • InvoiceService

      public InvoiceService()
      Default constructor that initializes an empty invoice list.
    • InvoiceService

      public InvoiceService(User user)
      Constructor that initializes the service with a given user. Loads all invoices for the user if a connection provider is available.
      Parameter:
      user - the user for whom invoices are to be managed
  • Methodendetails

    • setConnectionProvider

      public static void setConnectionProvider(ConnectionProvider provider)
      secures database connection
    • invoiceDateAlreadyUsed

      public boolean invoiceDateAlreadyUsed(LocalDate date, User user)
      Checks whether an invoice already exists for the given date and user.
      Parameter:
      date - the date to check
      user - the user whose invoices are checked
      Gibt zurück:
      true if an invoice with the same date already exists, false otherwise
    • invoiceDateAlreadyUsed

      public boolean invoiceDateAlreadyUsed(LocalDate date, User user, long excludeInvoiceId)
      Checks whether an invoice already exists for the given date and user, optionally excluding a specific invoice ID.
      Parameter:
      date - the date to check
      user - the user whose invoices are checked
      excludeInvoiceId - the ID of an invoice to exclude from the check
      Gibt zurück:
      true if a duplicate date is found, false otherwise
    • isValidDate

      public boolean isValidDate(LocalDate date)
      Validates whether a date is valid for invoice creation. A valid date must be within the current month, not in the future, and a workday.
      Parameter:
      date - the date to validate
      Gibt zurück:
      true if the date is valid, false otherwise
    • isWorkday

      public static boolean isWorkday(LocalDate date)
      Checks whether the given date falls on a workday (Monday to Friday).
      Parameter:
      date - the date to check
      Gibt zurück:
      true if the date is a workday, false otherwise
    • isValidFloat

      public boolean isValidFloat(String text)
      Checks whether a given string is a valid float number.
      Parameter:
      text - the text to validate
      Gibt zurück:
      true if the text represents a float, false otherwise
    • isAmountValid

      public boolean isAmountValid(String text)
      Validates whether a given string can be interpreted as a valid amount (float).
      Parameter:
      text - the string to validate
      Gibt zurück:
      true if the amount is valid, false otherwise
    • getAllInvoices

      public static List<Invoice> getAllInvoices(User user)
      Retrieves all invoices for the given user from the database.
      Parameter:
      user - the user whose invoices are to be retrieved
      Gibt zurück:
      a list of invoices
    • getInvoices

      public List<Invoice> getInvoices()
      Returns the list of invoices currently managed by this service instance.
      Gibt zurück:
      the list of invoices
    • addInvoice

      public static boolean addInvoice(Invoice invoice)
      Adds a new invoice to the database and handles OCR mismatch flagging and user flagging logic. Also persists flagged user updates if necessary.
      Parameter:
      invoice - the invoice to be added
      Gibt zurück:
      true if the invoice was successfully added, false otherwise
    • loadInvoice

      public Invoice loadInvoice(Reimbursement reimbursement)
      Loads the invoice associated with the given reimbursement. This method retrieves the invoice details from the database using the reimbursement's ID, and constructs an Invoice object including any binary file attached to it. The method also sets the user who created the invoice (only by ID).
      Parameter:
      reimbursement - The reimbursement containing the ID used to find the related invoice.
      Gibt zurück:
      The corresponding Invoice object, or null if no invoice is found.
    • updateInvoiceIfChanged

      public boolean updateInvoiceIfChanged(Invoice oldInvoice, Invoice newInvoice, User invoiceUser, boolean selfmade)
      Updates an existing invoice in the database if any of its attributes have changed. This method compares the old and new invoice data and updates the database accordingly. It also triggers a notification and flags the invoice if a self-made change is detected. If the invoice was previously not flagged and was changed by the user (selfmade = true), the invoice will be flagged and its associated reimbursement status will be set to "FLAGGED". Anomaly detection and flagged user tracking will also be triggered in that case.
      Parameter:
      oldInvoice - The existing invoice before changes.
      newInvoice - The invoice containing new values to compare.
      invoiceUser - The user associated with the invoice.
      selfmade - Indicates whether the change was made by the user themselves.
      Gibt zurück:
      true if at least one attribute was changed and updated; otherwise false.
    • getInvoiceById

      public static Invoice getInvoiceById(int id)
      Retrieves an invoice from the database by its unique ID. The returned invoice includes basic attributes such as ID, date, amount, category, and flagged status. It does not include the associated user or file content.
      Parameter:
      id - The ID of the invoice to retrieve.
      Gibt zurück:
      The Invoice object if found; otherwise null.