fix(export csv): added a filter to check if item is approved

This commit is contained in:
Matthieu Haineault 2025-08-21 16:27:17 -04:00
parent 9a150715b0
commit bfe145854f

View File

@ -18,54 +18,67 @@ export interface CsvRow {
export class CsvExportService { export class CsvExportService {
constructor(private readonly prisma: PrismaService) {} constructor(private readonly prisma: PrismaService) {}
async collectTransaction(period_id: number, companies: ExportCompany[]): Promise<CsvRow[]> { async collectTransaction( period_id: number, companies: ExportCompany[], approved: boolean = true):
const companyCodes = companies.map(c => c === ExportCompany.TARGO ? 1 : 2); Promise<CsvRow[]> {
const company_codes = companies.map(c => c === ExportCompany.TARGO ? 1 : 2);
const period = await this.prisma.payPeriods.findFirst({ const period = await this.prisma.payPeriods.findFirst({
where: { pay_period_no: period_id }, where: { pay_period_no: period_id },
}); });
if(!period) { if(!period) throw new NotFoundException(`Pay period ${period_id} not found`);
throw new NotFoundException(`Pay period ${period_id} not found`);
}
const startDate = period.period_start; const start_date = period.period_start;
const endDate = period.period_end; const end_date = period.period_end;
const approved_filter = approved ? { is_approved: true } : {};
//fetching shifts //fetching shifts
const shifts = await this.prisma.shifts.findMany({ const shifts = await this.prisma.shifts.findMany({
where: { date: { gte: startDate, lte: endDate }, where: {
timesheet: { employee: { company_code: { in: companyCodes} } }, date: { gte: start_date, lte: end_date },
...approved_filter,
timesheet: {
employee: { company_code: { in: company_codes} } },
}, },
include: { bank_code: true, include: {
timesheet: { include: {employee: { include: { user:true, bank_code: true,
supervisor: { include: { user:true } }, timesheet: { include: {
}}, employee: { include: {
}}, user:true,
supervisor: { include: {
user:true } } } } } },
}, },
}); });
//fetching expenses //fetching expenses
const expenses = await this.prisma.expenses.findMany({ const expenses = await this.prisma.expenses.findMany({
where: { date: { gte: startDate, lte: endDate }, where: {
timesheet: { employee: { company_code: { in: companyCodes} } }, date: { gte: start_date, lte: end_date },
...approved_filter,
timesheet: { employee: { company_code: { in: company_codes} } },
}, },
include: { bank_code: true, include: { bank_code: true,
timesheet: { include: { employee: { include: { user: true, timesheet: { include: {
supervisor: { include: { user:true } }, employee: { include: {
} }, user: true,
} }, supervisor: { include: {
}, user:true } } } } } },
},
}); });
//fetching leave requests //fetching leave requests
const leaves = await this.prisma.leaveRequests.findMany({ const leaves = await this.prisma.leaveRequests.findMany({
where : { start_date_time: { gte: startDate, lte: endDate }, where : {
employee: { company_code: { in: companyCodes } }, start_date_time: { gte: start_date, lte: end_date },
employee: { company_code: { in: company_codes } },
}, },
include: { bank_code: true, include: {
employee: { include: { user: true, bank_code: true,
supervisor: { include: { user: true } }, employee: { include: {
}}, user: true,
supervisor: { include: {
user: true } } } },
}, },
}); });
@ -74,7 +87,7 @@ export class CsvExportService {
//Shifts Mapping //Shifts Mapping
for (const shift of shifts) { for (const shift of shifts) {
const emp = shift.timesheet.employee; const emp = shift.timesheet.employee;
const week_number = this.computeWeekNumber(startDate, shift.date); const week_number = this.computeWeekNumber(start_date, shift.date);
const hours = this.computeHours(shift.start_time, shift.end_time); const hours = this.computeHours(shift.start_time, shift.end_time);
rows.push({ rows.push({
@ -85,7 +98,7 @@ export class CsvExportService {
quantity_hours: hours, quantity_hours: hours,
amount: undefined, amount: undefined,
week_number, week_number,
pay_date: this.formatDate(endDate), pay_date: this.formatDate(end_date),
holiday_date: undefined, holiday_date: undefined,
}); });
} }
@ -93,7 +106,7 @@ export class CsvExportService {
//Expenses Mapping //Expenses Mapping
for (const e of expenses) { for (const e of expenses) {
const emp = e.timesheet.employee; const emp = e.timesheet.employee;
const week_number = this.computeWeekNumber(startDate, e.date); const week_number = this.computeWeekNumber(start_date, e.date);
rows.push({ rows.push({
company_code: emp.company_code, company_code: emp.company_code,
@ -103,7 +116,7 @@ export class CsvExportService {
quantity_hours: undefined, quantity_hours: undefined,
amount: Number(e.amount), amount: Number(e.amount),
week_number, week_number,
pay_date: this.formatDate(endDate), pay_date: this.formatDate(end_date),
holiday_date: undefined, holiday_date: undefined,
}); });
} }
@ -115,7 +128,7 @@ export class CsvExportService {
const start = l.start_date_time; const start = l.start_date_time;
const end = l.end_date_time ?? start; const end = l.end_date_time ?? start;
const week_number = this.computeWeekNumber(startDate, start); const week_number = this.computeWeekNumber(start_date, start);
const hours = this.computeHours(start, end); const hours = this.computeHours(start, end);
rows.push({ rows.push({
@ -126,7 +139,7 @@ export class CsvExportService {
quantity_hours: hours, quantity_hours: hours,
amount: undefined, amount: undefined,
week_number, week_number,
pay_date: this.formatDate(endDate), pay_date: this.formatDate(end_date),
holiday_date: undefined, holiday_date: undefined,
}); });
} }
@ -146,7 +159,7 @@ export class CsvExportService {
generateCsv(rows: CsvRow[]): Buffer { generateCsv(rows: CsvRow[]): Buffer {
const header = [ const header = [
'company_code', 'company_code',
'external_payrol_id', 'external_payroll_id',
'full_name', 'full_name',
'bank_code', 'bank_code',
'quantity_hours', 'quantity_hours',