Fix payslip lookup logic and enable view option for finance executive

<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>
This commit is contained in:
Princy Jerin
2026-04-16 14:09:28 +05:30
parent 47ca953eac
commit 04ef7744f7
3 changed files with 17 additions and 14 deletions
@@ -89,24 +89,24 @@ void PayslipManagementService::savePayrolls()
std::pair<std::shared_ptr<Payroll>, std::shared_ptr<Payslip>> PayslipManagementService::getPayslipForMonth(const std::string& employeeId, int year, int month) std::pair<std::shared_ptr<Payroll>, std::shared_ptr<Payslip>> PayslipManagementService::getPayslipForMonth(const std::string& employeeId, int year, int month)
{ {
auto& payrolls = m_dataStore.getPayrolls(); auto& employees = m_dataStore.getEmployees();
auto& payslips = m_dataStore.getPayslips(); auto employeeIterator = employees.find(employeeId);
for (const auto& payrollsPair : payrolls) if (employeeIterator == employees.end())
{ {
const auto& payroll = payrollsPair.second; throw std::runtime_error("Employee not found!");
if (payroll->getEmployeeId() == employeeId) }
auto payroll = employeeIterator->second->getPayroll();
auto& payslips = employeeIterator->second->getEmployeePayslips();
for (const auto& payslipPair : payslips)
{ {
auto payslipIterator = payslips.find(payroll->getId()); const auto& payslip = payslipPair.second;
if (payslipIterator != payslips.end())
{ {
const auto& payslip = payslipIterator->second;
if (payslip->getTimestamp().getYear() == year && payslip->getTimestamp().getMonth() == month) if (payslip->getTimestamp().getYear() == year && payslip->getTimestamp().getMonth() == month)
{ {
return { payroll, payslip }; return { payroll, payslip };
} }
} }
} }
}
return { nullptr, nullptr }; return { nullptr, nullptr };
} }
@@ -65,6 +65,8 @@ bool FinanceExecutiveMenu::handleOperation(int choice)
{ {
switch (choice) switch (choice)
{ {
case 2:
viewPayslip(m_zenvyController);
case 4: case 4:
viewEmployees(m_zenvyController); viewEmployees(m_zenvyController);
break; break;
@@ -304,7 +304,7 @@ inline void viewPayslip(std::shared_ptr<ZenvyController> controller)
util::clear(); util::clear();
std::cout << "Enter the year: "; std::cout << "Enter the year: ";
util::read(year); util::read(year);
std::cout << "\nEnter the month: "; std::cout << "Enter the month: ";
util::read(month); util::read(month);
auto employee = controller->getCurrentEmployee(); auto employee = controller->getCurrentEmployee();
if (!employee) if (!employee)
@@ -318,7 +318,8 @@ inline void viewPayslip(std::shared_ptr<ZenvyController> controller)
auto payslip = result.second; auto payslip = result.second;
if (payroll && payslip) if (payroll && payslip)
{ {
std::cout << "\nPayslip for " << employee->getEmployeeName() << " (" << year << "-" << std::setw(2) << std::setfill('0') << month << ")\n\n"; util::clear();
std::cout << "Payslip for " << employee->getEmployeeName() << " (" << year << "-" << std::setw(2) << std::setfill('0') << month << ")\n\n";
std::cout << "Basic Salary : " << payroll->getBasicSalary() << "\n"; std::cout << "Basic Salary : " << payroll->getBasicSalary() << "\n";
std::cout << "House Rent Allowance : " << payroll->getHouseRentAllowance() << "\n"; std::cout << "House Rent Allowance : " << payroll->getHouseRentAllowance() << "\n";
std::cout << "Food Allowance : " << payroll->getFoodAllowance() << "\n"; std::cout << "Food Allowance : " << payroll->getFoodAllowance() << "\n";