import { expect } from '@playwright/test'; import { MockApi, MOCK, Persona, unique } from '../../api'; import { DATA, PROCESS } from './labels'; export type ItemRow = { ExpenditureItemId: string; ProjectNumber: string; TaskNumber: string; ExpenditureType: string; Quantity: number; RawCost: number; DocumentNumber: string | null; Status: string; AccountingStatus: string; TransferredToGL: string; PostedFlag: string; distributions?: { AccountClass: string; Account: string; EnteredDr: number; EnteredCr: number }[] }; const today = () => new Date().toISOString().slice(0, 10); /** * PPM data recipes (tests/data-recipes/ppm) through the MOCK PPM pack: REST facade, /api/ppm (internal) and /api/processes. * Payables invoices are created in the P2P pack (P2P owns them); AR invoices are read from the O2C pack. */ export class PPMApi { constructor(public api: MockApi) {} rest(as: Persona, method: string, path: string, data?: unknown) { return this.api.call(as, method, path, data, MOCK.restBase); } async restQ(as: Persona, resource: string, q: string, extra = '') { const r = await this.rest(as, 'GET', `/${resource}?q=${encodeURIComponent(q)}${extra}`); expect(r.status, `GET ${resource}?q=${q}`).toBe(200); return r.body as { items: T[]; count: number }; } async items(q: string, extra = '') { return (await this.restQ('project_accountant', 'projectExpenditureItems', q, extra)).items; } async process(as: Persona, name: string, params: Record = {}) { const p = await this.api.ok(as, 'POST', '/processes', { name, params }); expect(p.status, `${name}: ${p.log}`).toBe('SUCCEEDED'); return p; } /** DR-PPM-CST-001.payroll_cost_import: synthetic payroll cost file (stand-in for the HCM hand-off until the HCM pack exists). */ async payrollFile(o: { hours?: number; task?: string; employee?: string } = {}) { const content = `EmployeeNumber,ProjectNumber,TaskNumber,Hours,Date\n${o.employee ?? DATA.employee},${DATA.project},${o.task ?? '1.1'},${o.hours ?? 10},${today()}`; return this.api.ok('project_accountant', 'POST', '/ppm/files', { name: `${unique('ZZ-PAYROLL')}.csv`, content }); } /** DR-PPM-CST-001 end state: 10 hours costed on task 1.1 (500.00 at the ASSUMED 50.00 cost rate). */ async costedLabor(hours = 10) { await this.payrollFile({ hours }); await this.process('project_accountant', PROCESS.importPayroll, { businessUnit: DATA.bu }); await this.process('project_accountant', PROCESS.importAndProcess, { businessUnit: DATA.bu, transactionSource: DATA.laborSource }); return (await this.items(`ProjectNumber='${DATA.project}';ExpenditureType='${DATA.laborType}'`))[0]; } /** DR-PPM-CST-002 (STAND-IN coding): validated + accounted P2P invoice 10 x 25.00 = 250.00, project-coded through the mock side table. */ async projectCodedApInvoice(o: { account?: boolean } = {}) { const inv = await this.api.validatedInvoice(); await this.api.ok('ap_specialist', 'POST', '/ppm/apInvoiceCoding', { invoiceNumber: inv.invoiceNumber, project: DATA.project, task: '1.1', expenditureType: DATA.supplierType }); if (o.account !== false) { const p = await this.api.ok('ap_specialist', 'POST', '/processes', { name: 'Create Accounting', params: { subledger: 'Payables', accountingMode: 'Final' } }); expect(p.status, `P2P Create Accounting: ${p.log}`).toBe('SUCCEEDED'); } return inv as { invoiceNumber: string }; } /** DR-PPM-CTR-001: active T&M contract 10,000.00 on ZZ-PRJ-001 (created, submitted, approved). */ async activeContract(amount = 10000) { const c = await this.api.ok('contract_admin', 'POST', '/ppm/contracts', { type: DATA.contractType, customer: DATA.customer, amount, billPlan: `T&M at ${DATA.billRateSchedule}`, revenuePlan: DATA.revenuePlan, project: DATA.project }); await this.api.ok('contract_admin', 'POST', `/ppm/contracts/${c.number}/submit`); await this.api.ok('contract_approver', 'POST', `/ppm/contracts/${c.number}/approve`); return c.number as string; } async invoices(contract: string) { return (await this.restQ('billing_specialist_ppm', 'projectContractInvoices', `ContractNumber='${contract}'`)).items; } /** DR-PPM-BIL-002: approved project invoice (10 h x 80.00 = 800.00) on an active contract. */ async approvedInvoice() { await this.costedLabor(); const contract = await this.activeContract(); await this.process('billing_specialist_ppm', PROCESS.generateInvoices, { contract, throughDate: today() }); const inv = (await this.invoices(contract))[0]; await this.api.ok('billing_specialist_ppm', 'POST', `/ppm/invoices/${inv.InvoiceNumber}/approve`); return inv.InvoiceNumber as string; } async adjustment(itemId: string, task = '1.2', qty = 4, as: Persona = 'project_accountant') { return (await this.api.ok(as, 'POST', `/ppm/items/${itemId}/transfer`, { task, qty })).adj.number as string; } }