295 lines
11 KiB
C++
295 lines
11 KiB
C++
#include <stdexcept>
|
|
#include "ServiceManagementService.h"
|
|
#include "AuthenticationManagementService.h"
|
|
#include "Service.h"
|
|
#include "ServiceBooking.h"
|
|
#include "ComboPackage.h"
|
|
#include "Factory.h"
|
|
#include "UserManagementService.h"
|
|
#include "ServiceBooking.h"
|
|
#include "Factory.h"
|
|
#include "JobCard.h"
|
|
#include "Timestamp.h"
|
|
#include "Service.h"
|
|
#include "Enums.h"
|
|
#include "InventoryItem.h"
|
|
#include "AuthenticationManagementService.h"
|
|
#include "PaymentManagementService.h"
|
|
#include "NotificationManagementService.h"
|
|
#include "User.h"
|
|
|
|
util::Map<std::string, ServiceBooking*> ServiceManagementService::getServiceBookings()
|
|
{
|
|
return m_dataStore.getServiceBookings();
|
|
}
|
|
|
|
ServiceBooking* ServiceManagementService::getServiceBooking(const std::string& serviceID)
|
|
{
|
|
auto currentServiceBookings = getServiceBookings();
|
|
for (int iterator = 0; iterator < currentServiceBookings.getSize(); iterator++)
|
|
{
|
|
if (currentServiceBookings.getValueAt(iterator)->getId() == serviceID)
|
|
{
|
|
return currentServiceBookings.getValueAt(iterator);
|
|
}
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
void ServiceManagementService::createJobCard(const std::string& bookingID, const std::string& technicianID, const std::string& serviceID)
|
|
{
|
|
UserManagementService m_userManagementService;
|
|
ServiceBooking* currentBooking = getServiceBooking(bookingID);
|
|
auto& currentJobCards = m_dataStore.getJobCards();
|
|
if (currentBooking == nullptr)
|
|
{
|
|
throw std::runtime_error("Service Booking not available");
|
|
}
|
|
auto& currentServices = currentBooking->getServices();
|
|
if (currentServices.find(serviceID) == -1)
|
|
{
|
|
throw std::runtime_error("Invalid service Id");
|
|
}
|
|
Service* currentService = currentServices.getValueAt(currentServices.find(serviceID));
|
|
User* selectedTechnician = m_userManagementService.getUser(technicianID);
|
|
if (selectedTechnician == nullptr)
|
|
{
|
|
throw std::runtime_error("Technician not available");
|
|
}
|
|
auto& inventoryItems = currentService->getRequiredInventoryItems();
|
|
for (int iterator = 0; iterator < inventoryItems.getSize(); iterator++)
|
|
{
|
|
InventoryItem* currentInventoryItem = inventoryItems.getValueAt(iterator);
|
|
if (currentInventoryItem->getQuantity() == 0)
|
|
{
|
|
std::string errorMessage = "Failed to create job card, " + currentInventoryItem->getPartName() + " is out of stock.";
|
|
throw std::runtime_error(errorMessage);
|
|
}
|
|
else
|
|
{
|
|
int currentStockQuantity = currentInventoryItem->getQuantity();
|
|
currentInventoryItem->setQuantity(currentStockQuantity - 1);
|
|
}
|
|
}
|
|
currentBooking->setAssignedTechnician(selectedTechnician);
|
|
currentBooking->setAssignedTechnicianId(selectedTechnician->getId());
|
|
std::string title = "Job card created";
|
|
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());
|
|
currentJobCards.insert(jobCard->getId(), jobCard);
|
|
sendNotification(selectedTechnician, title, message);
|
|
}
|
|
|
|
void ServiceManagementService::createService(const std::string& name, const util::Vector<std::string>& inventoryItemIDs, double laborCost)
|
|
{
|
|
util::Map<std::string, InventoryItem*> currentServiceInventoryItems;
|
|
auto inventoryItems = m_dataStore.getInventoryItems();
|
|
for (int iteratorOne = 0; iteratorOne < inventoryItemIDs.getSize(); iteratorOne++)
|
|
{
|
|
std::string currentItemID = inventoryItemIDs[iteratorOne];
|
|
bool itemFound = false;
|
|
for (int iteratorTwo = 0; iteratorTwo < inventoryItems.getSize(); iteratorTwo++)
|
|
{
|
|
InventoryItem* currentInventoryItem = inventoryItems.getValueAt(iteratorTwo);
|
|
if (currentInventoryItem && currentInventoryItem->getId() == currentItemID)
|
|
{
|
|
itemFound = true;
|
|
currentServiceInventoryItems.insert(currentInventoryItem->getId(), currentInventoryItem);
|
|
break;
|
|
}
|
|
}
|
|
if (!itemFound)
|
|
{
|
|
throw std::runtime_error("Inventory item with ID '" + currentItemID + "' not found.");
|
|
}
|
|
}
|
|
Service* newService = Factory::getObject<Service>(name, currentServiceInventoryItems, laborCost);
|
|
if (newService == nullptr)
|
|
{
|
|
throw std::runtime_error("Unable to create new service.");
|
|
}
|
|
util::Map<std::string, Service*>& currentServices = m_dataStore.getServices();
|
|
if (currentServices.find(newService->getId()) != -1)
|
|
{
|
|
throw std::runtime_error("Service with this ID Already exists.");
|
|
}
|
|
currentServices.insert(newService->getId(), newService);
|
|
}
|
|
|
|
util::Map<std::string, Service*> ServiceManagementService::getServices()
|
|
{
|
|
return m_dataStore.getServices();
|
|
}
|
|
|
|
void ServiceManagementService::removeService(const std::string& serviceID)
|
|
{
|
|
util::Map<std::string, Service*> currentServices = getServices();
|
|
if (currentServices.find(serviceID) != -1)
|
|
{
|
|
currentServices.getValueAt(currentServices.find(serviceID))->setState(util::State::INACTIVE);
|
|
}
|
|
else
|
|
{
|
|
throw std::runtime_error("Service not found.");
|
|
}
|
|
}
|
|
|
|
util::Map<std::string, ServiceBooking*> ServiceManagementService::getServiceBookings(const std::string& customerID)
|
|
{
|
|
util::Map<std::string, ServiceBooking*> currentServiceBookings = getServiceBookings();
|
|
util::Map<std::string, ServiceBooking*> currentUserServiceBookings;
|
|
if (currentServiceBookings.getSize() != 0)
|
|
{
|
|
for (int iterator = 0; iterator < currentServiceBookings.getSize(); iterator++)
|
|
{
|
|
auto currentServiceBooking = currentServiceBookings.getValueAt(iterator);
|
|
if (currentServiceBooking->getCustomerId() == customerID)
|
|
{
|
|
currentUserServiceBookings.insert(currentServiceBooking->getId(), currentServiceBooking);
|
|
}
|
|
}
|
|
}
|
|
return currentUserServiceBookings;
|
|
}
|
|
|
|
util::Map<std::string, JobCard*> ServiceManagementService::getJobCards(const std::string& technicianID)
|
|
{
|
|
util::Map<std::string, JobCard*> jobCards = m_dataStore.getJobCards();
|
|
util::Map<std::string, JobCard*> technicianJobCards;
|
|
for (int iterator = 0; iterator < jobCards.getSize(); iterator++)
|
|
{
|
|
JobCard* currentJobCard = jobCards.getValueAt(iterator);
|
|
if (currentJobCard->getTechnicianId() == technicianID)
|
|
{
|
|
technicianJobCards.insert(currentJobCard->getId(), currentJobCard);
|
|
}
|
|
}
|
|
return technicianJobCards;
|
|
}
|
|
|
|
static bool hasAllJobCardsinServiceBookingCompleted(std::string bookingId, util::Map<std::string, JobCard*>& currentAssignedJobs)
|
|
{
|
|
for (int iterator = 0; iterator < currentAssignedJobs.getSize(); iterator++)
|
|
{
|
|
JobCard* currentJob = currentAssignedJobs.getValueAt(iterator);
|
|
if (currentJob->getBookingId() == bookingId)
|
|
{
|
|
if (currentJob->getStatus() == util::ServiceJobStatus::STARTED)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void ServiceManagementService::completeJob(const std::string& jobID)
|
|
{
|
|
AuthenticationManagementService authenticationManagementService;
|
|
PaymentManagementService paymentManagementService;
|
|
bool jobStatusUpdated = false, serviceBookingCompleted;
|
|
JobCard* currentJob;
|
|
User* currentTechnician = authenticationManagementService.getAuthenticatedUser();
|
|
if (currentTechnician == nullptr)
|
|
{
|
|
throw std::runtime_error("Unable to fetch current technician.");
|
|
}
|
|
util::Map<std::string, JobCard*> currentAssignedJobs = getJobCards(currentTechnician->getId());
|
|
if (currentAssignedJobs.getSize() == 0)
|
|
{
|
|
throw std::runtime_error("No job cards assigned to the technician.");
|
|
}
|
|
if (currentAssignedJobs.find(jobID) != -1)
|
|
{
|
|
currentJob = currentAssignedJobs.getValueAt(currentAssignedJobs.find(jobID));
|
|
if (currentJob == nullptr)
|
|
{
|
|
throw std::runtime_error("Unable to fetch current job.");
|
|
}
|
|
if (currentJob->getStatus() == util::ServiceJobStatus::STARTED)
|
|
{
|
|
currentJob->setStatus(util::ServiceJobStatus::COMPLETED);
|
|
jobStatusUpdated = true;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
throw std::runtime_error("Failed to complete the job, some error occured or job already completed.");
|
|
}
|
|
if (!jobStatusUpdated)
|
|
{
|
|
throw std::runtime_error("Failed to complete the job, some error occured or job already completed.");
|
|
}
|
|
|
|
serviceBookingCompleted = hasAllJobCardsinServiceBookingCompleted(currentJob->getBookingId(), currentAssignedJobs);
|
|
if (serviceBookingCompleted)
|
|
{
|
|
currentJob->getBooking()->setStatus(util::ServiceJobStatus::COMPLETED);
|
|
paymentManagementService.generateInvoice(currentJob->getBooking());
|
|
std::string title = "Service Booking completed,Invoice Generated.\n";
|
|
std::string message = "Services completed for the booking and invoice generated.\n";
|
|
sendNotification(currentJob->getBooking()->getCustomer(), title, message);
|
|
}
|
|
}
|
|
|
|
void ServiceManagementService::purchaseService(const util::Vector<std::string>& serviceIDs, const std::string& vehicleNumber, const std::string& vehicleBrand, const std::string& vehicleModel)
|
|
{
|
|
AuthenticationManagementService m_authenticationManagementService;
|
|
auto authenticatedUser = m_authenticationManagementService.getAuthenticatedUser();
|
|
if (authenticatedUser == nullptr)
|
|
{
|
|
throw std::runtime_error("No user is currently logged in!");
|
|
}
|
|
auto& servicesMap = m_dataStore.getServices();
|
|
auto& serviceBookingMap = m_dataStore.getServiceBookings();
|
|
util::Map<std::string, Service*> selectedServices;
|
|
int selectedServicesCount = serviceIDs.getSize();
|
|
for (int index = 0; index < selectedServicesCount; index++)
|
|
{
|
|
int serviceIndex = servicesMap.find(serviceIDs[index]);
|
|
if (serviceIndex == -1)
|
|
{
|
|
throw std::runtime_error("Service not found!");
|
|
}
|
|
Service* service = servicesMap.getValueAt(serviceIndex);
|
|
selectedServices[service->getId()] = service;
|
|
}
|
|
ServiceBooking* serviceBooking = Factory::getObject<ServiceBooking>(util::ServiceJobStatus::STARTED, selectedServices, authenticatedUser->getId(), authenticatedUser, vehicleNumber, vehicleBrand, vehicleModel, 0);
|
|
if (serviceBooking == nullptr)
|
|
{
|
|
throw std::runtime_error("Failed to create service booking");
|
|
}
|
|
serviceBookingMap[serviceBooking->getId()] = serviceBooking;
|
|
sendNotification(authenticatedUser,
|
|
"Service Booking succeeded",
|
|
"Your service booking has been successfully placed with ID " + serviceBooking->getId());
|
|
}
|
|
|
|
void ServiceManagementService::purchaseComboPackage(const std::string& comboPackageID, const std::string& vehicleNumber, const std::string& vehicleBrand, const std::string& vehicleModel)
|
|
{
|
|
AuthenticationManagementService m_authenticationManagementService;
|
|
auto authenticatedUser = m_authenticationManagementService.getAuthenticatedUser();
|
|
if (authenticatedUser == nullptr)
|
|
{
|
|
throw std::runtime_error("No user is currently logged in!");
|
|
}
|
|
auto& comboPackagesMap = m_dataStore.getComboPackages();
|
|
auto& serviceBookingMap = m_dataStore.getServiceBookings();
|
|
int comboPackageIndex = comboPackagesMap.find(comboPackageID);
|
|
if (comboPackageIndex == -1)
|
|
{
|
|
throw std::runtime_error("Combo Package not found!");
|
|
}
|
|
const ComboPackage* comboPackage = comboPackagesMap[comboPackageID];
|
|
util::Map<std::string, Service*> selectedServices = comboPackage->getServices();
|
|
ServiceBooking* serviceBooking = Factory::getObject<ServiceBooking>(util::ServiceJobStatus::STARTED, selectedServices, authenticatedUser->getId(), authenticatedUser, vehicleNumber, vehicleBrand, vehicleModel, comboPackage->getDiscountPercentage());
|
|
if (serviceBooking == nullptr)
|
|
{
|
|
throw std::runtime_error("Failed to create combo package service booking");
|
|
}
|
|
serviceBookingMap[serviceBooking->getId()] = serviceBooking;
|
|
sendNotification(authenticatedUser,
|
|
"Combo Package Service Booking succeeded",
|
|
"Your service booking for the combo package has been successfully placed with ID " + serviceBooking->getId());
|
|
}
|