diff --git a/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/datastores/DataStore.cpp b/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/datastores/DataStore.cpp index 1cade32..af6fdc6 100644 --- a/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/datastores/DataStore.cpp +++ b/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/datastores/DataStore.cpp @@ -561,5 +561,4 @@ bool DataStore::unlockDataStore() return false; } return ReleaseMutex(m_globalMutex) != 0; -} - +} \ No newline at end of file diff --git a/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/models/Invoice.cpp b/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/models/Invoice.cpp index f24a013..7788b84 100644 --- a/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/models/Invoice.cpp +++ b/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/models/Invoice.cpp @@ -9,6 +9,7 @@ Date: 19-May-2026 #include #include +#include "SerializedRecords.h" #include "Invoice.h" #include "Factory.h" #include "InventoryItem.h" @@ -34,7 +35,8 @@ Invoice::Invoice() m_discountPercentage(0.0), m_totalAmount(0.0), m_paymentMethod(util::PaymentMode()), - m_status(util::PaymentStatus()) {} + m_status(util::PaymentStatus()) { +} /* Function: Invoice @@ -57,16 +59,16 @@ Returns: Invoice::Invoice( const std::string& bookingId, ServiceBooking* booking, - const util::Timestamp& invoiceDate, - double laborCost, + const util::Timestamp& invoiceDate, + double laborCost, const util::Map& parts, double partsCost, - double discountPercentage, - double totalAmount, - const util::Timestamp& paymentDate, - util::PaymentMode paymentMethod, + 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), @@ -78,7 +80,7 @@ Invoice::Invoice( m_totalAmount(totalAmount), m_paymentDate(paymentDate), m_paymentMethod(paymentMethod), - m_status(status) + m_status(status) { int numberOfParts = m_parts.getSize(); auto partPointers = m_parts.getValues(); @@ -473,100 +475,50 @@ static util::Vector getPartIDsAsVector(const std::string& partIDsSt /* Function: serialize -Description: Serializes the invoice into a CSV-formatted string. +Description: Serializes the Invoice object into a SerializedInvoice record. Parameters: - None Returns: - - std::string: Serialized invoice record + - SerializedInvoice: Serialized representation of the invoice */ -std::string Invoice::serialize() const +SerializedInvoice 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(); + 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 CSV-formatted string into an Invoice object. +Description: Deserializes a SerializedInvoice record into an Invoice object. Parameters: - - record: const std::string&, serialized invoice record + - serializedInvoice: const SerializedInvoice&, 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) +Invoice* Invoice::deserialize(const SerializedInvoice& serializedInvoice) { - 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 partIDs = getPartIDsAsVector(partIDsString); - util::PaymentMode paymentMethod = util::getPaymentMode(paymentMethodString); - util::PaymentStatus status = util::getPaymentStatus(statusString); + util::Vector partIDs = getPartIDsAsVector(serializedInvoice.partIDs); return Factory::getObject( - id, - bookingId, - invoiceDate, + serializedInvoice.id, + serializedInvoice.bookingId, + serializedInvoice.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"; + serializedInvoice.laborCost, + serializedInvoice.partsCost, + serializedInvoice.discountPercentage, + serializedInvoice.totalAmount, + serializedInvoice.paymentDate, + serializedInvoice.paymentMethod, + serializedInvoice.status); } \ No newline at end of file diff --git a/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/models/Invoice.h b/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/models/Invoice.h index 3374de9..11343e7 100644 --- a/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/models/Invoice.h +++ b/Trenser.VehicleServiceSystem/Trenser.VehicleServiceSystem/models/Invoice.h @@ -6,7 +6,6 @@ Author: Trenser Date: 19-May-2026 */ - #pragma once #include #include "Map.h" @@ -16,6 +15,7 @@ Date: 19-May-2026 class ServiceBooking; class InventoryItem; +struct SerializedInvoice; class Invoice { @@ -39,14 +39,14 @@ public: Invoice( const std::string& bookingId, ServiceBooking* booking, - const util::Timestamp& invoiceDate, - double laborCost, - const util::Map& parts, + const util::Timestamp& invoiceDate, + double laborCost, + const util::Map& parts, double partsCost, - double discountPercentage, - double totalAmount, - const util::Timestamp& paymentDate, - util::PaymentMode paymentMethod, + double discountPercentage, + double totalAmount, + const util::Timestamp& paymentDate, + util::PaymentMode paymentMethod, util::PaymentStatus status ); Invoice( @@ -87,7 +87,6 @@ public: void setPaymentDate(const util::Timestamp& paymentDate); void setPaymentMethod(util::PaymentMode paymentMethod); void setStatus(util::PaymentStatus status); - std::string serialize() const; - static Invoice* deserialize(const std::string&); - static std::string getHeaders(); + SerializedInvoice serialize() const; + static Invoice* deserialize(const SerializedInvoice&); }; \ No newline at end of file