573 lines
14 KiB
C++
573 lines
14 KiB
C++
/*
|
|
File: Invoice.cpp
|
|
Description: Implements the Invoice class which represents an invoice in the Vehicle Service Management System.
|
|
Provides constructors, accessors, and mutators for invoice details such as ID, booking, costs,
|
|
discount percentage, total amount, payment details, and status.
|
|
Author: Trenser
|
|
Date: 19-May-2026
|
|
*/
|
|
|
|
#include <sstream>
|
|
#include <stdexcept>
|
|
#include "Invoice.h"
|
|
#include "Factory.h"
|
|
#include "InventoryItem.h"
|
|
#include "StringHelper.h"
|
|
|
|
int Invoice::m_uid = 0;
|
|
|
|
/*
|
|
Function: Invoice
|
|
Description: Default constructor that initializes a new invoice with a unique ID,
|
|
null booking, zero costs, zero discount, zero total amount,
|
|
and default payment method and status.
|
|
Parameters:
|
|
- None
|
|
Returns:
|
|
- A new Invoice object.
|
|
*/
|
|
Invoice::Invoice()
|
|
: m_id("INV" + std::to_string(++m_uid)),
|
|
m_booking(nullptr),
|
|
m_laborCost(0.0),
|
|
m_partsCost(0.0),
|
|
m_discountPercentage(0.0),
|
|
m_totalAmount(0.0),
|
|
m_paymentMethod(util::PaymentMode()),
|
|
m_status(util::PaymentStatus()) {}
|
|
|
|
/*
|
|
Function: Invoice
|
|
Description: Parameterized constructor that initializes a new invoice with a unique ID and specified details.
|
|
Parameters:
|
|
- bookingId: ID of the associated service booking.
|
|
- booking: Pointer to the ServiceBooking object.
|
|
- invoiceDate: Timestamp of when the invoice was created.
|
|
- laborCost: Cost of labor for the service.
|
|
- parts: Map of inventory items used in the service.
|
|
- partsCost: Total cost of parts.
|
|
- discountPercentage: Discount applied to the invoice.
|
|
- totalAmount: Final total amount after discount.
|
|
- paymentDate: Timestamp of when payment was made.
|
|
- paymentMethod: Payment mode (ONLINE/OFFLINE).
|
|
- status: Payment status (PENDING/COMPLETED).
|
|
Returns:
|
|
- A new Invoice object.
|
|
*/
|
|
Invoice::Invoice(
|
|
const std::string& bookingId,
|
|
ServiceBooking* booking,
|
|
const util::Timestamp& invoiceDate,
|
|
double laborCost, const util::Map<std::string,
|
|
InventoryItem*>& parts,
|
|
double partsCost,
|
|
double discountPercentage,
|
|
double totalAmount,
|
|
const util::Timestamp& paymentDate,
|
|
util::PaymentMode paymentMethod,
|
|
util::PaymentStatus status
|
|
)
|
|
: m_id("INV" + std::to_string(++m_uid)),
|
|
m_bookingId(bookingId),
|
|
m_booking(booking),
|
|
m_invoiceDate(invoiceDate),
|
|
m_laborCost(laborCost),
|
|
m_parts(parts),
|
|
m_partsCost(partsCost),
|
|
m_discountPercentage(discountPercentage),
|
|
m_totalAmount(totalAmount),
|
|
m_paymentDate(paymentDate),
|
|
m_paymentMethod(paymentMethod),
|
|
m_status(status)
|
|
{
|
|
int numberOfParts = m_parts.getSize();
|
|
auto partPointers = m_parts.getValues();
|
|
for (int index = 0; index < numberOfParts; index++)
|
|
{
|
|
m_partIDs.push_back(partPointers[index]->getId());
|
|
}
|
|
}
|
|
|
|
Invoice::Invoice(
|
|
const std::string& id,
|
|
const std::string& bookingId,
|
|
const util::Timestamp& invoiceDate,
|
|
const util::Vector<std::string>& partIDs,
|
|
double laborCost,
|
|
double partsCost,
|
|
double discountPercentage,
|
|
double totalAmount,
|
|
const util::Timestamp& paymentDate,
|
|
util::PaymentMode paymentMethod,
|
|
util::PaymentStatus status
|
|
)
|
|
: m_id(id),
|
|
m_bookingId(bookingId),
|
|
m_booking(nullptr),
|
|
m_invoiceDate(invoiceDate),
|
|
m_partIDs(partIDs),
|
|
m_laborCost(laborCost),
|
|
m_partsCost(partsCost),
|
|
m_discountPercentage(discountPercentage),
|
|
m_totalAmount(totalAmount),
|
|
m_paymentDate(paymentDate),
|
|
m_paymentMethod(paymentMethod),
|
|
m_status(status)
|
|
{
|
|
int idNumber = util::extractNumber(m_id);
|
|
if (idNumber > m_uid)
|
|
{
|
|
m_uid = idNumber;
|
|
}
|
|
}
|
|
|
|
/*
|
|
Function: getId
|
|
Description: Retrieves the unique ID of the invoice.
|
|
Returns:
|
|
- const std::string& representing the invoice ID.
|
|
*/
|
|
const std::string& Invoice::getId() const
|
|
{
|
|
return m_id;
|
|
}
|
|
|
|
/*
|
|
Function: getBookingId
|
|
Description: Retrieves the booking ID associated with the invoice.
|
|
Returns:
|
|
- const std::string& representing the booking ID.
|
|
*/
|
|
const std::string& Invoice::getBookingId() const
|
|
{
|
|
return m_bookingId;
|
|
}
|
|
|
|
/*
|
|
Function: getBooking
|
|
Description: Retrieves the pointer to the associated ServiceBooking.
|
|
Returns:
|
|
- ServiceBooking* representing the booking.
|
|
*/
|
|
ServiceBooking* Invoice::getBooking() const
|
|
{
|
|
return m_booking;
|
|
}
|
|
|
|
/*
|
|
Function: getInvoiceDate
|
|
Description: Retrieves the timestamp of the invoice creation date.
|
|
Returns:
|
|
- const util::Timestamp& representing the invoice date.
|
|
*/
|
|
const util::Timestamp& Invoice::getInvoiceDate() const
|
|
{
|
|
return m_invoiceDate;
|
|
}
|
|
|
|
/*
|
|
Function: getLaborCost
|
|
Description: Retrieves the labor cost associated with the invoice.
|
|
Returns:
|
|
- double representing the labor cost.
|
|
*/
|
|
double Invoice::getLaborCost() const
|
|
{
|
|
return m_laborCost;
|
|
}
|
|
|
|
/*
|
|
Function: getPartIDs
|
|
Description: Retrieves the IDs of parts used in the invoice.
|
|
Parameters:
|
|
- None
|
|
Returns:
|
|
- const util::Vector<std::string>&: Part IDs
|
|
*/
|
|
const util::Vector<std::string>& Invoice::getPartIDs() const
|
|
{
|
|
return m_partIDs;
|
|
}
|
|
|
|
/*
|
|
Function: getParts
|
|
Description: Retrieves the map of inventory items used in the service.
|
|
Returns:
|
|
- const util::Map<int, InventoryItem*>& representing the parts.
|
|
*/
|
|
const util::Map<std::string, InventoryItem*>& Invoice::getParts() const
|
|
{
|
|
return m_parts;
|
|
}
|
|
|
|
/*
|
|
Function: getPartsCost
|
|
Description: Retrieves the total cost of parts used in the service.
|
|
Returns:
|
|
- double representing the parts cost.
|
|
*/
|
|
double Invoice::getPartsCost() const
|
|
{
|
|
return m_partsCost;
|
|
}
|
|
|
|
/*
|
|
Function: getDiscountPercentage
|
|
Description: Retrieves the discount percentage applied to the invoice.
|
|
Returns:
|
|
- double representing the discount percentage.
|
|
*/
|
|
double Invoice::getDiscountPercentage() const
|
|
{
|
|
return m_discountPercentage;
|
|
}
|
|
|
|
/*
|
|
Function: getTotalAmount
|
|
Description: Retrieves the total amount of the invoice after discount.
|
|
Returns:
|
|
- double representing the total amount.
|
|
*/
|
|
double Invoice::getTotalAmount() const
|
|
{
|
|
return m_totalAmount;
|
|
}
|
|
|
|
/*
|
|
Function: getPaymentDate
|
|
Description: Retrieves the timestamp of the payment date.
|
|
Returns:
|
|
- const util::Timestamp& representing the payment date.
|
|
*/
|
|
const util::Timestamp& Invoice::getPaymentDate() const
|
|
{
|
|
return m_paymentDate;
|
|
}
|
|
|
|
/*
|
|
Function: getPaymentMethod
|
|
Description: Retrieves the payment mode used for the invoice.
|
|
Returns:
|
|
- util::PaymentMode representing the payment method.
|
|
*/
|
|
util::PaymentMode Invoice::getPaymentMethod() const
|
|
{
|
|
return m_paymentMethod;
|
|
}
|
|
|
|
/*
|
|
Function: getStatus
|
|
Description: Retrieves the payment status of the invoice.
|
|
Returns:
|
|
- util::PaymentStatus representing the payment status.
|
|
*/
|
|
util::PaymentStatus Invoice::getStatus() const
|
|
{
|
|
return m_status;
|
|
}
|
|
|
|
/*
|
|
Function: setId
|
|
Description: Sets the unique ID of the invoice.
|
|
Parameters:
|
|
- id: New invoice ID string.
|
|
Returns:
|
|
- void
|
|
*/
|
|
void Invoice::setId(const std::string& id)
|
|
{
|
|
m_id = id;
|
|
}
|
|
|
|
/*
|
|
Function: setBookingId
|
|
Description: Sets the booking ID associated with the invoice.
|
|
Parameters:
|
|
- bookingId: New booking ID string.
|
|
Returns:
|
|
- void
|
|
*/
|
|
void Invoice::setBookingId(const std::string& bookingId)
|
|
{
|
|
m_bookingId = bookingId;
|
|
}
|
|
|
|
/*
|
|
Function: setBooking
|
|
Description: Sets the associated ServiceBooking pointer.
|
|
Parameters:
|
|
- booking: Pointer to the ServiceBooking object.
|
|
Returns:
|
|
- void
|
|
*/
|
|
void Invoice::setBooking(ServiceBooking* booking)
|
|
{
|
|
m_booking = booking;
|
|
}
|
|
|
|
/*
|
|
Function: setInvoiceDate
|
|
Description: Sets the invoice creation date.
|
|
Parameters:
|
|
- invoiceDate: New timestamp for the invoice date.
|
|
Returns:
|
|
- void
|
|
*/
|
|
void Invoice::setInvoiceDate(const util::Timestamp& invoiceDate)
|
|
{
|
|
m_invoiceDate = invoiceDate;
|
|
}
|
|
|
|
/*
|
|
Function: setLaborCost
|
|
Description: Sets the labor cost for the invoice.
|
|
Parameters:
|
|
- laborCost: New labor cost value.
|
|
Returns:
|
|
- void
|
|
*/
|
|
void Invoice::setLaborCost(double laborCost)
|
|
{
|
|
m_laborCost = laborCost;
|
|
}
|
|
|
|
/*
|
|
Function: setParts
|
|
Description: Sets the inventory items used in the service.
|
|
Parameters:
|
|
- parts: Map of inventory items.
|
|
Returns:
|
|
- void
|
|
*/
|
|
void Invoice::setParts(const util::Map<std::string, InventoryItem*>& parts)
|
|
{
|
|
m_parts = parts;
|
|
m_partIDs.clear();
|
|
int numberOfParts = m_parts.getSize();
|
|
auto partPointers = m_parts.getValues();
|
|
for (int index = 0; index < numberOfParts; index++)
|
|
{
|
|
m_partIDs.push_back(partPointers[index]->getId());
|
|
}
|
|
}
|
|
|
|
/*
|
|
Function: setPartsCost
|
|
Description: Sets the total cost of parts used in the service.
|
|
Parameters:
|
|
- partsCost: New parts cost value.
|
|
Returns:
|
|
- void
|
|
*/
|
|
void Invoice::setPartsCost(double partsCost)
|
|
{
|
|
m_partsCost = partsCost;
|
|
}
|
|
|
|
/*
|
|
Function: setDiscountPercentage
|
|
Description: Sets the discount percentage applied to the invoice.
|
|
Parameters:
|
|
- discountPercentage: New discount percentage value.
|
|
Returns:
|
|
- void
|
|
*/
|
|
void Invoice::setDiscountPercentage(double discountPercentage)
|
|
{
|
|
m_discountPercentage = discountPercentage;
|
|
}
|
|
|
|
/*
|
|
Function: setTotalAmount
|
|
Description: Sets the total amount of the invoice.
|
|
Parameters:
|
|
- totalAmount: New total amount value.
|
|
Returns:
|
|
- void
|
|
*/
|
|
void Invoice::setTotalAmount(double totalAmount)
|
|
{
|
|
m_totalAmount = totalAmount;
|
|
}
|
|
|
|
/*
|
|
Function: setPaymentDate
|
|
Description: Sets the payment date for the invoice.
|
|
Parameters:
|
|
- paymentDate: New timestamp for the payment date.
|
|
Returns:
|
|
- void
|
|
*/
|
|
void Invoice::setPaymentDate(const util::Timestamp& paymentDate)
|
|
{
|
|
m_paymentDate = paymentDate;
|
|
}
|
|
|
|
/*
|
|
Function: setPaymentMethod
|
|
Description: Sets the payment mode for the invoice.
|
|
Parameters:
|
|
- paymentMethod: New payment mode value.
|
|
Returns:
|
|
- void
|
|
*/
|
|
void Invoice::setPaymentMethod(util::PaymentMode paymentMethod)
|
|
{
|
|
m_paymentMethod = paymentMethod;
|
|
}
|
|
|
|
/*
|
|
Function: setStatus
|
|
Description: Sets the payment status of the invoice.
|
|
Parameters:
|
|
- status: New payment status value.
|
|
Returns:
|
|
- void
|
|
*/
|
|
void Invoice::setStatus(util::PaymentStatus status)
|
|
{
|
|
m_status = status;
|
|
}
|
|
|
|
/*
|
|
Function: getPartIDsAsString (static helper)
|
|
Description: Converts a vector of part IDs into a single string separated by '|'.
|
|
Parameters:
|
|
- partIDs: const util::Vector<std::string>&, vector of part IDs
|
|
Returns:
|
|
- std::string: Concatenated part IDs string
|
|
*/
|
|
static std::string getPartIDsAsString(const util::Vector<std::string>& partIDs)
|
|
{
|
|
int numberOfParts = partIDs.getSize();
|
|
std::string partIDsString;
|
|
for (int index = 0; index < numberOfParts; index++)
|
|
{
|
|
partIDsString += partIDs[index];
|
|
if (index < numberOfParts - 1)
|
|
{
|
|
partIDsString += '|';
|
|
}
|
|
}
|
|
return partIDsString;
|
|
}
|
|
|
|
/*
|
|
Function: getPartIDsAsVector (static helper)
|
|
Description: Converts a string of part IDs separated by '|' into a vector.
|
|
Parameters:
|
|
- partIDsString: const std::string&, concatenated part IDs string
|
|
Returns:
|
|
- util::Vector<std::string>: Vector of part IDs
|
|
*/
|
|
static util::Vector<std::string> getPartIDsAsVector(const std::string& partIDsString)
|
|
{
|
|
util::Vector<std::string> partIDs;
|
|
std::string partID;
|
|
std::istringstream serializedPartIDs(partIDsString);
|
|
while (getline(serializedPartIDs, partID, '|'))
|
|
{
|
|
partIDs.push_back(partID);
|
|
}
|
|
return partIDs;
|
|
}
|
|
|
|
/*
|
|
Function: serialize
|
|
Description: Serializes the invoice into a CSV-formatted string.
|
|
Parameters:
|
|
- None
|
|
Returns:
|
|
- std::string: Serialized invoice record
|
|
*/
|
|
std::string Invoice::serialize() const
|
|
{
|
|
std::ostringstream serializedInvoice;
|
|
serializedInvoice << m_id << ','
|
|
<< m_bookingId << ','
|
|
<< m_invoiceDate.toString() << ','
|
|
<< m_laborCost << ','
|
|
<< getPartIDsAsString(m_partIDs) << ','
|
|
<< m_partsCost << ','
|
|
<< m_discountPercentage << ','
|
|
<< m_totalAmount << ','
|
|
<< m_paymentDate.toString() << ','
|
|
<< util::getPaymentModeString(m_paymentMethod) << ','
|
|
<< util::getPaymentStatusString(m_status);
|
|
return serializedInvoice.str();
|
|
}
|
|
|
|
/*
|
|
Function: deserialize
|
|
Description: Deserializes a CSV-formatted string into an Invoice object.
|
|
Parameters:
|
|
- record: const std::string&, serialized invoice record
|
|
Returns:
|
|
- Invoice*: Pointer to the deserialized Invoice object
|
|
Throws:
|
|
- std::runtime_error if data is invalid
|
|
*/
|
|
Invoice* Invoice::deserialize(const std::string& record)
|
|
{
|
|
std::string id, bookingId;
|
|
std::string invoiceDateString, laborCostString, partIDsString;
|
|
std::string partsCostString, discountPercentageString, totalAmountString;
|
|
std::string paymentDateString, paymentMethodString, statusString;
|
|
double laborCost, partsCost, discountPercentage, totalAmount;
|
|
std::istringstream serializedInvoice(record);
|
|
getline(serializedInvoice, id, ',');
|
|
getline(serializedInvoice, bookingId, ',');
|
|
getline(serializedInvoice, invoiceDateString, ',');
|
|
getline(serializedInvoice, laborCostString, ',');
|
|
getline(serializedInvoice, partIDsString, ',');
|
|
getline(serializedInvoice, partsCostString, ',');
|
|
getline(serializedInvoice, discountPercentageString, ',');
|
|
getline(serializedInvoice, totalAmountString, ',');
|
|
getline(serializedInvoice, paymentDateString, ',');
|
|
getline(serializedInvoice, paymentMethodString, ',');
|
|
getline(serializedInvoice, statusString, ',');
|
|
util::Timestamp invoiceDate;
|
|
util::Timestamp paymentDate;
|
|
try
|
|
{
|
|
invoiceDate = util::Timestamp::fromString(invoiceDateString);
|
|
paymentDate = util::Timestamp::fromString(paymentDateString);
|
|
laborCost = std::stod(laborCostString);
|
|
partsCost = std::stod(partsCostString);
|
|
discountPercentage = std::stod(discountPercentageString);
|
|
totalAmount = std::stod(totalAmountString);
|
|
}
|
|
catch (...)
|
|
{
|
|
throw std::runtime_error("Invalid invoice data");
|
|
}
|
|
util::Vector<std::string> partIDs = getPartIDsAsVector(partIDsString);
|
|
util::PaymentMode paymentMethod = util::getPaymentMode(paymentMethodString);
|
|
util::PaymentStatus status = util::getPaymentStatus(statusString);
|
|
return Factory::getObject<Invoice>(
|
|
id,
|
|
bookingId,
|
|
invoiceDate,
|
|
partIDs,
|
|
laborCost,
|
|
partsCost,
|
|
discountPercentage,
|
|
totalAmount,
|
|
paymentDate,
|
|
paymentMethod,
|
|
status
|
|
);
|
|
}
|
|
|
|
/*
|
|
Function: getHeaders
|
|
Description: Retrieves the CSV headers for invoice serialization.
|
|
Parameters:
|
|
- None
|
|
Returns:
|
|
- std::string: Header string ("ID,BookingID,InvoiceDate,LaborCost,PartIDs,PartsCost,DiscountPercentage,TotalAmount,PaymentDate,PaymentMethod,Status")
|
|
*/
|
|
std::string Invoice::getHeaders()
|
|
{
|
|
return "ID,BookingID,InvoiceDate,LaborCost,PartIDs,PartsCost,DiscountPercentage,TotalAmount,PaymentDate,PaymentMethod,Status";
|
|
} |