04ef7744f7
<UserStory> EMP010 : View Payslip </UserStory> <Changes> - Refactored getPayslipForMonth() to fetch payroll and payslips via employee data - Added validation for invalid employee ID - Simplified payslip search using employee-specific payslip records - Enabled “View Payslip” option in Finance Executive menu - Minor UI cleanup in payslip view (spacing and screen clear) </Changes> <Review> Smitha Mohan </Review>
137 lines
4.9 KiB
C++
137 lines
4.9 KiB
C++
#include <stdexcept>
|
|
#include <algorithm>
|
|
#include "PayslipManagementService.h"
|
|
#include "ApplicationConfig.h"
|
|
#include "AuthorizationHelper.h"
|
|
#include "Enums.h"
|
|
#include "FileManager.h"
|
|
#include "Factory.h"
|
|
|
|
void PayslipManagementService::updateSalary(const std::string& employeeId, double basicSalary, double houseRentAllowance, double foodAllowance, double employeePFContribution, double employerPFContribution)
|
|
{
|
|
util::enforceAuthorization(m_dataStore.getAuthenticatedEmployee()->getEmployeeType(), Enums::EmployeeType::FINANCE);
|
|
auto employee = m_dataStore.getEmployees().find(employeeId);
|
|
if (employee != m_dataStore.getEmployees().end() && employee->second->getEmployeeType() != Enums::EmployeeType::ADMIN)
|
|
{
|
|
auto payroll = employee->second->getPayroll();
|
|
payroll->setBasicSalary(basicSalary);
|
|
payroll->setHouseRentAllowance(houseRentAllowance);
|
|
payroll->setFoodAllowance(foodAllowance);
|
|
payroll->setEmployeePFContribution(employeePFContribution);
|
|
payroll->setEmployerPFContribution(employerPFContribution);
|
|
}
|
|
else
|
|
{
|
|
throw std::runtime_error("Employee not found, unable to update the salary");
|
|
}
|
|
}
|
|
|
|
void PayslipManagementService::generatePayslips()
|
|
{
|
|
util::enforceAuthorization(m_dataStore.getAuthenticatedEmployee()->getEmployeeType(), Enums::EmployeeType::FINANCE);
|
|
auto& employees = m_dataStore.getEmployees();
|
|
auto& payslips = m_dataStore.getPayslips();
|
|
util::Timestamp currentTimestamp;
|
|
int currentMonth = currentTimestamp.getMonth();
|
|
int currentYear = currentTimestamp.getYear();
|
|
for (const auto& employeePair : employees)
|
|
{
|
|
auto& employee = employeePair.second;
|
|
if (employee->getEmployeeType() == Enums::EmployeeType::ADMIN)
|
|
{
|
|
continue;
|
|
}
|
|
auto& employeePayslips = employee->getEmployeePayslips();
|
|
auto payslipForTheMonth = std::find_if(employeePayslips.begin(), employeePayslips.end(),
|
|
[currentMonth, currentYear](const auto& payslipPair)
|
|
{
|
|
auto& payslip = payslipPair.second;
|
|
auto& timestamp = payslip->getTimestamp();
|
|
return (timestamp.getMonth() == currentMonth && timestamp.getYear() == currentYear);
|
|
}
|
|
);
|
|
if (payslipForTheMonth == employeePayslips.end())
|
|
{
|
|
auto payroll = employee->getPayroll();
|
|
double salary;
|
|
salary = payroll->getBasicSalary() + payroll->getFoodAllowance() + payroll->getHouseRentAllowance() - payroll->getEmployeePFContribution() - payroll->getEmployerPFContribution();
|
|
std::shared_ptr<Payslip> payslip = Factory::getObject<Payslip>(salary, employee->getId());
|
|
employee->addPayslip(payslip);
|
|
payslips.emplace(payslip->getId(), payslip);
|
|
}
|
|
}
|
|
}
|
|
|
|
void PayslipManagementService::loadPayrolls()
|
|
{
|
|
FileManager<Payroll> payrollFileManager(Config::File::PAYROLL_FILE);
|
|
auto& payrolls = m_dataStore.getPayrolls();
|
|
auto& employees = m_dataStore.getEmployees();
|
|
auto payrollObjects = payrollFileManager.load();
|
|
for (const auto& payrollPair : payrollObjects)
|
|
{
|
|
auto employeeIterator = employees.find(payrollPair.second->getEmployeeId());
|
|
if (employeeIterator == employees.end())
|
|
{
|
|
throw std::runtime_error("Payroll Object not associated with an existing employee");
|
|
}
|
|
employeeIterator->second->setEmployeePayroll(payrollPair.second);
|
|
}
|
|
payrolls.insert(payrollObjects.begin(), payrollObjects.end());
|
|
}
|
|
|
|
void PayslipManagementService::savePayrolls()
|
|
{
|
|
FileManager<Payroll> payrollFileManager(Config::File::PAYROLL_FILE);
|
|
auto& payrolls = m_dataStore.getPayrolls();
|
|
payrollFileManager.save(payrolls);
|
|
}
|
|
|
|
std::pair<std::shared_ptr<Payroll>, std::shared_ptr<Payslip>> PayslipManagementService::getPayslipForMonth(const std::string& employeeId, int year, int month)
|
|
{
|
|
auto& employees = m_dataStore.getEmployees();
|
|
auto employeeIterator = employees.find(employeeId);
|
|
if (employeeIterator == employees.end())
|
|
{
|
|
throw std::runtime_error("Employee not found!");
|
|
}
|
|
auto payroll = employeeIterator->second->getPayroll();
|
|
auto& payslips = employeeIterator->second->getEmployeePayslips();
|
|
for (const auto& payslipPair : payslips)
|
|
{
|
|
const auto& payslip = payslipPair.second;
|
|
{
|
|
if (payslip->getTimestamp().getYear() == year && payslip->getTimestamp().getMonth() == month)
|
|
{
|
|
return { payroll, payslip };
|
|
}
|
|
}
|
|
}
|
|
return { nullptr, nullptr };
|
|
}
|
|
|
|
void PayslipManagementService::loadPayslips()
|
|
{
|
|
FileManager<Payslip> payslipFileManager(Config::File::PAYSLIP_FILE);
|
|
auto& payslips = m_dataStore.getPayslips();
|
|
auto& employees = m_dataStore.getEmployees();
|
|
auto payslipObjects = payslipFileManager.load();
|
|
for (const auto& payslipPair : payslipObjects)
|
|
{
|
|
auto employeeIterator = employees.find(payslipPair.second->getEmployeeId());
|
|
if (employeeIterator == employees.end())
|
|
{
|
|
throw std::runtime_error("Payslip Object not associated with an existing employee");
|
|
}
|
|
employeeIterator->second->addPayslip(payslipPair.second);
|
|
}
|
|
payslips.insert(payslipObjects.begin(), payslipObjects.end());
|
|
}
|
|
|
|
void PayslipManagementService::savePayslips()
|
|
{
|
|
FileManager<Payslip> payslipFileManager(Config::File::PAYSLIP_FILE);
|
|
auto& payslips = m_dataStore.getPayslips();
|
|
payslipFileManager.save(payslips);
|
|
}
|