Fix Inventory Quantity Is Not Decremented When Job Card Is Created

Changes:

- Added retrieval of tracked service bookings and inventory items in
  ServiceManagementService::createJobCard().
- Validated service booking ID and inventory item indices before
  proceeding with job card creation.
- Decremented inventory item quantities when job cards are created and
  marked corresponding tracked inventory records as MODIFIED.
- Updated tracked service booking state to MODIFIED when technician is
  assigned and job status changes.
- Persisted changes by saving job cards, service bookings, and inventory
  items to the datastore.

Fixes #2075
This commit is contained in:
Jissin Mathew
2026-06-17 15:29:46 +05:30
parent 974d4efe02
commit 4243f4e43f
@@ -596,6 +596,14 @@ void ServiceManagementService::createJobCard(const std::string& bookingID, const
UserManagementService m_userManagementService; UserManagementService m_userManagementService;
ServiceBooking* currentBooking = getServiceBooking(bookingID); ServiceBooking* currentBooking = getServiceBooking(bookingID);
auto& currentTrackedJobCards = m_dataStore.getJobCards(); auto& currentTrackedJobCards = m_dataStore.getJobCards();
auto& currentTrackedInventoryItems = m_dataStore.getInventoryItems();
auto& currentTrackedServiceBookings = m_dataStore.getServiceBookings();
int currentTrackedServiceBookingIndex = currentTrackedServiceBookings.find(bookingID);
if (currentTrackedServiceBookingIndex == -1)
{
throw std::runtime_error("Invalid service booking id.");
}
auto& currentTrackedServiceBooking = currentTrackedServiceBookings.getValueAt(currentTrackedServiceBookingIndex);
if (currentBooking == nullptr) if (currentBooking == nullptr)
{ {
throw std::runtime_error("Service Booking not available"); throw std::runtime_error("Service Booking not available");
@@ -624,10 +632,18 @@ void ServiceManagementService::createJobCard(const std::string& bookingID, const
for (int iterator = 0; iterator < inventoryItems.getSize(); iterator++) for (int iterator = 0; iterator < inventoryItems.getSize(); iterator++)
{ {
InventoryItem* currentInventoryItem = inventoryItems.getValueAt(iterator); InventoryItem* currentInventoryItem = inventoryItems.getValueAt(iterator);
const std::string& currentInventoryItemId = inventoryItems.getKeyAt(iterator);
if (currentInventoryItem) if (currentInventoryItem)
{ {
int trackedCurrentInventoryItemIndex = currentTrackedInventoryItems.find(currentInventoryItemId);
if (trackedCurrentInventoryItemIndex == -1)
{
throw std::runtime_error("Invalid inventory item index.");
}
auto& trackedCurrentInventoryItem = currentTrackedInventoryItems.getValueAt(trackedCurrentInventoryItemIndex);
int currentStockQuantity = currentInventoryItem->getQuantity(); int currentStockQuantity = currentInventoryItem->getQuantity();
currentInventoryItem->setQuantity(currentStockQuantity - 1); currentInventoryItem->setQuantity(currentStockQuantity - 1);
trackedCurrentInventoryItem.state = RecordState::MODIFIED;
} }
} }
currentBooking->setAssignedTechnician(selectedTechnician); currentBooking->setAssignedTechnician(selectedTechnician);
@@ -636,6 +652,7 @@ void ServiceManagementService::createJobCard(const std::string& bookingID, const
{ {
currentBooking->setStatus(util::ServiceJobStatus::STARTED); currentBooking->setStatus(util::ServiceJobStatus::STARTED);
} }
currentTrackedServiceBooking.state = RecordState::MODIFIED;
std::string title = "Job card created"; std::string title = "Job card created";
std::string message = "Job card created for the service and you are assigned for that."; std::string message = "Job card created for the service and you are assigned for that.";
JobCard* jobCard = Factory::getObject<JobCard>(bookingID, currentBooking, currentService, serviceID, technicianID, selectedTechnician, util::Timestamp(), util::ServiceJobStatus::STARTED, util::Timestamp()); JobCard* jobCard = Factory::getObject<JobCard>(bookingID, currentBooking, currentService, serviceID, technicianID, selectedTechnician, util::Timestamp(), util::ServiceJobStatus::STARTED, util::Timestamp());
@@ -652,6 +669,8 @@ void ServiceManagementService::createJobCard(const std::string& bookingID, const
message = "A technician has been assigned to your Service Booking with ID " + bookingID; message = "A technician has been assigned to your Service Booking with ID " + bookingID;
sendNotification(currentBooking->getCustomer(), title, message); sendNotification(currentBooking->getCustomer(), title, message);
m_dataStore.saveJobCards(); m_dataStore.saveJobCards();
m_dataStore.saveServiceBookings();
m_dataStore.saveInventoryItems();
} }
/* /*