File size: 4,984 Bytes
af1bee9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { expect } from '@playwright/test';
import { MockApi, MOCK } from '../../api';
import { DATA } from './labels';

export type StatementLine = { amount: number; reference?: string };
/** R2R data recipes (tests/data-recipes/r2r) through the MOCK R2R pack's internal API (/api/r2r) and the shared /api/processes. */
export class R2RApi {
  constructor(public api: MockApi) {}
  refs(): Promise<{ openPeriod: string; nextPeriod: string; closePeriod: string; prevPeriod: string; ledger: string }> { return this.api.ok('gl_accountant', 'GET', '/r2r/refs'); }
  async stage(name: string, content: string) { return this.api.ok('gl_accountant', 'POST', '/r2r/files', { name, content }); }
  async process(as: any, name: string, params: Record<string, unknown>) { const p = await this.api.ok(as, 'POST', '/processes', { name, params }); expect(p.status, `${name}: ${p.log}`).toBe('SUCCEEDED'); return p; }
  /** CSV stand-in for a bank statement file (DR-R2R-BST-001): header columns repeated per line. */
  statementCsv(number: string, opening: number, lines: StatementLine[]) {
    const closing = Math.round((opening + lines.reduce((a, l) => a + l.amount, 0)) * 100) / 100;
    return { closing, csv: ['StatementNumber,BankAccount,Opening,Closing,Amount,Reference', ...lines.map((l) => `${number},${DATA.bankAccount},${opening.toFixed(2)},${closing.toFixed(2)},${l.amount.toFixed(2)},${l.reference ?? ''}`)].join('\n') };
  }
  /** GL interface CSV stand-in (FBDI GlInterface / workbook rows). */
  async interfaceCsv(rows: { account: string; debit?: number; credit?: number }[], o: { source: string; groupId: string }) {
    const r = await this.refs();
    return ['Ledger,Source,Category,Period,GroupId,Account,Debit,Credit', ...rows.map((x) => `${DATA.ledger},${o.source},${DATA.category},${r.openPeriod},${o.groupId},${x.account},${x.debit ?? 0},${x.credit ?? 0}`)].join('\n');
  }
  async balance(as: any, account: string, period: string) {
    const f = `AccountBalanceFinder;accountCombination=${account},accountingPeriod=${period},ledgerSetName=${DATA.ledger}`;
    const r = await this.api.call(as, 'GET', `/ledgerBalances?finder=${encodeURIComponent(f)}`, undefined, MOCK.restBase);
    expect(r.status).toBe(200); return r.body as { items: { PeriodActivity: number; PeriodDebit: number; PeriodCredit: number }[]; count: number };
  }
  async activity(account: string, period: string) { const b = await this.balance('gl_accountant', account, period); return b.items[0]?.PeriodActivity ?? 0; }
  /** DR-R2R-JNL-003 style recipe: save a manual journal through the mock API (optionally foreign currency / reference) and post it. */
  async postedJournal(as: any, j: { name: string; lines: { account: string; debit?: number; credit?: number }[]; period?: string; currency?: string; rateType?: string; reference?: string; post?: boolean }) {
    const r = await this.refs();
    const b = await this.api.ok(as, 'POST', '/r2r/journals', { name: j.name, ledger: DATA.ledger, period: j.period ?? r.openPeriod, category: DATA.category, lines: j.lines, currency: j.currency, rateType: j.rateType, reference: j.reference });
    if (j.post === false) return b;
    return this.api.ok(as, 'POST', `/r2r/journals/${b.id}/post`);
  }
  async completeJournal(as: any, id: string) { return this.api.ok(as, 'POST', `/r2r/journals/${id}/complete`); }
  async approveJournal(as: any, id: string) { return this.api.ok(as, 'POST', `/r2r/journals/${id}/approve`); }
  /** Synthetic daily rate through the Fusion-style currencyRates resource (integration_user; DR-R2R-RATE-001). */
  async rate(from: string, to: string, type: string, rate: number, date = new Date().toISOString().slice(0, 10)) {
    const r = await this.api.call('integration_user', 'POST', '/currencyRates', { FromCurrency: from, ToCurrency: to, ConversionType: type, ConversionDate: date, ConversionRate: rate }, MOCK.restBase);
    expect(r.status, `rate ${from}->${to} ${type}`).toBe(201); return r.body;
  }
  async openPeriod(period: string) { return this.api.ok('gl_manager', 'POST', '/r2r/periods/open', { ledger: DATA.ledger, period }); }
  async systemTxn(ref: string, amount: number) { return this.api.ok('cash_manager', 'POST', '/r2r/systemTxns', { ref, amount }); }
  async loadStatement(number: string, opening: number, lines: StatementLine[]) {
    const s = this.statementCsv(number, opening, lines); await this.stage(`${number}.csv`, s.csv);
    await this.process('cash_manager', 'Load and Import Bank Statement', { format: DATA.statementFormat, dataFile: `${number}.csv` }); return s;
  }
  async batches(as: any, q: string) { return this.restQ(as, 'journalBatches', q); }
  async restQ(as: any, resource: string, q: string) {
    const r = await this.api.call(as, 'GET', `/${resource}?q=${encodeURIComponent(q)}`, undefined, MOCK.restBase);
    expect(r.status, `GET ${resource}?q=${q}`).toBe(200); return r.body as { items: any[]; count: number };
  }
}