f545d57f79
<UserStory> 1949: Model Refactoring</UserStory> UserStory #1949 <Changes> Replaced CSV-based serialization and deserialization in the Invoice model with fixed-size SerializedRecord structures for shared memory storage. Implemented serialize() method to convert Invoice objects into SerializedInvoice records using direct struct field assignment and strcpy_s for string fields. Implemented deserialize() method to reconstruct Invoice objects directly from SerializedInvoice types instead of parsing CSV strings. Updated Invoice class interfaces to use SerializedInvoice types, removing legacy CSV serialization APIs including getHeaders() function. Added SerializedRecords.h dependency and forward declarations for SerializedInvoice structure in Invoice header. Minor formatting adjustments in DataStore.cpp (closing brace placement). </Changes> <Test> N/A </Test> <Review> Sreeja Reghukumar, please review </Review>
524 lines
13 KiB
C++
524 lines
13 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 "SerializedRecords.h"
|
|
#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 object into a SerializedInvoice record.
|
|
Parameters:
|
|
- None
|
|
Returns:
|
|
- SerializedInvoice: Serialized representation of the invoice
|
|
*/
|
|
SerializedInvoice Invoice::serialize() const
|
|
{
|
|
SerializedInvoice serialized = {};
|
|
strcpy_s(serialized.id, sizeof(serialized.id), m_id.c_str());
|
|
strcpy_s(serialized.bookingId, sizeof(serialized.bookingId), m_bookingId.c_str());
|
|
strcpy_s(serialized.partIDs, sizeof(serialized.partIDs), getPartIDsAsString(m_partIDs).c_str());
|
|
serialized.invoiceDate = m_invoiceDate;
|
|
serialized.laborCost = m_laborCost;
|
|
serialized.partsCost = m_partsCost;
|
|
serialized.discountPercentage = m_discountPercentage;
|
|
serialized.totalAmount = m_totalAmount;
|
|
serialized.paymentDate = m_paymentDate;
|
|
serialized.paymentMethod = m_paymentMethod;
|
|
serialized.status = m_status;
|
|
return serialized;
|
|
}
|
|
|
|
/*
|
|
Function: deserialize
|
|
Description: Deserializes a SerializedInvoice record into an Invoice object.
|
|
Parameters:
|
|
- serializedInvoice: const SerializedInvoice&, serialized invoice record
|
|
Returns:
|
|
- Invoice*: Pointer to the deserialized Invoice object
|
|
*/
|
|
Invoice* Invoice::deserialize(const SerializedInvoice& serializedInvoice)
|
|
{
|
|
util::Vector<std::string> partIDs = getPartIDsAsVector(serializedInvoice.partIDs);
|
|
return Factory::getObject<Invoice>(
|
|
serializedInvoice.id,
|
|
serializedInvoice.bookingId,
|
|
serializedInvoice.invoiceDate,
|
|
partIDs,
|
|
serializedInvoice.laborCost,
|
|
serializedInvoice.partsCost,
|
|
serializedInvoice.discountPercentage,
|
|
serializedInvoice.totalAmount,
|
|
serializedInvoice.paymentDate,
|
|
serializedInvoice.paymentMethod,
|
|
serializedInvoice.status);
|
|
} |