Package backend.logic
Klasse InvoiceService
java.lang.Object
backend.logic.InvoiceService
Service class for managing invoices associated with users.
Provides functionality to upload, validate, and insert invoices,
and to check invoice data against OCR results.
-
Feldübersicht
Felder -
Konstruktorübersicht
KonstruktorenKonstruktorBeschreibungDefault constructor that initializes an empty invoice list.InvoiceService
(User user) Constructor that initializes the service with a given user. -
Methodenübersicht
Modifizierer und TypMethodeBeschreibungstatic boolean
addInvoice
(Invoice invoice) Adds a new invoice to the database and handles OCR mismatch flagging and user flagging logic.getAllInvoices
(User user) Retrieves all invoices for the given user from the database.static Invoice
getInvoiceById
(int id) Retrieves an invoice from the database by its unique ID.Returns the list of invoices currently managed by this service instance.boolean
invoiceDateAlreadyUsed
(LocalDate date, User user) Checks whether an invoice already exists for the given date and user.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.boolean
isAmountValid
(String text) Validates whether a given string can be interpreted as a valid amount (float).boolean
isValidDate
(LocalDate date) Validates whether a date is valid for invoice creation.boolean
isValidFloat
(String text) Checks whether a given string is a valid float number.static boolean
Checks whether the given date falls on a workday (Monday to Friday).loadInvoice
(Reimbursement reimbursement) Loads the invoice associated with the given reimbursement.static void
setConnectionProvider
(ConnectionProvider provider) secures database connectionboolean
updateInvoiceIfChanged
(Invoice oldInvoice, Invoice newInvoice, User invoiceUser, boolean selfmade) Updates an existing invoice in the database if any of its attributes have changed.
-
Felddetails
-
connectionProvider
-
invoices
-
-
Konstruktordetails
-
InvoiceService
public InvoiceService()Default constructor that initializes an empty invoice list. -
InvoiceService
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
secures database connection -
invoiceDateAlreadyUsed
Checks whether an invoice already exists for the given date and user.- Parameter:
date
- the date to checkuser
- the user whose invoices are checked- Gibt zurück:
- true if an invoice with the same date already exists, false otherwise
-
invoiceDateAlreadyUsed
Checks whether an invoice already exists for the given date and user, optionally excluding a specific invoice ID.- Parameter:
date
- the date to checkuser
- the user whose invoices are checkedexcludeInvoiceId
- the ID of an invoice to exclude from the check- Gibt zurück:
- true if a duplicate date is found, false otherwise
-
isValidDate
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
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
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
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
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
Returns the list of invoices currently managed by this service instance.- Gibt zurück:
- the list of invoices
-
addInvoice
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
Loads the invoice associated with the given reimbursement. This method retrieves the invoice details from the database using the reimbursement's ID, and constructs anInvoice
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, ornull
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; otherwisefalse
.
-
getInvoiceById
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; otherwisenull
.
-