File size: 4,298 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
import { expect } from '@playwright/test';
import { MockApi, MOCK, Persona, unique } from '../../api';
import { DATA, PROCESS } from './labels';

export type DistRow = { TransactionId: string; TransactionType: string; ReceiptNumber: string | null; WorkOrderNumber: string | null; AccountClass: string; Account: string; EnteredDr: number; EnteredCr: number; AccountingStatus: string; TransferredToGL: string; PostedFlag: string };
/**

 * SCM data recipes (tests/data-recipes/scm) through the MOCK SCM pack: REST facade, /api/scm (internal) and /api/processes.

 * Receipts and invoices are created in the P2P pack (P2P owns them); SCM only reads them.

 */
export class SCMApi {
  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<T = any>(as: Persona, resource: string, q: string) {
    const r = await this.rest(as, 'GET', `/${resource}?q=${encodeURIComponent(q)}`); expect(r.status, `GET ${resource}?q=${q}`).toBe(200); return r.body as { items: T[]; count: number };
  }
  /** DR-SCM-ITM-001: fresh synthetic item per run (on-hand starts at 0). costProfile null = uncostable item (DR-SCM-TXN-001.uncostable). */
  async item(o: { costProfile?: null } = {}) {
    const n = unique('ZZ-SCM').toUpperCase(); const r = await this.rest('scm_integration', 'POST', '/itemsV2', { ItemNumber: n, ItemDescription: `${n} (MOCK)`, ...(o.costProfile === null ? { CostProfile: null } : {}) });
    expect(r.status, `create item ${n}: ${JSON.stringify(r.body)}`).toBe(201); return n;
  }
  async setCostProfile(item: string, profile: string = DATA.costProfile) { return this.api.ok('cost_accountant', 'PATCH', `/scm/items/${encodeURIComponent(item)}`, { costProfile: profile }); }
  async miscReceipt(item: string, qty: number, unitCost: number, subinv: string = DATA.subinvStores, as: Persona = 'scm_integration') {
    return this.api.ok(as, 'POST', '/scm/miscTransactions', { type: 'Miscellaneous Receipt', item, org: DATA.invOrg, subinv, qty, unitCost, alias: DATA.accountAlias });
  }
  async txn(id: string) { return this.api.ok('cost_accountant', 'GET', `/scm/transactions/${id}`); }
  async onhand(item: string, org: string = DATA.invOrg) {
    const b = await this.restQ('scm_integration', 'inventoryOnhandBalances', `ItemNumber='${item}';OrganizationCode='${org}'`);
    return b.items.reduce((a: number, x: any) => a + x.OnhandQuantity, 0) as number;
  }
  async process(as: Persona, 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; }
  async costRun() { return this.process('cost_accountant', PROCESS.costDistributions, { costOrg: DATA.costOrg }); }
  /** P2P chain (DR-PO-001 / DR-RCV-001 shape): approved 3-way PO for an SCM item, received in full. Returns PO and P2P receipt. */
  async receivedItemPo(item: string, quantity: number, unitPrice: number) {
    const po = await this.api.ok('buyer', 'POST', '/purchaseOrders', { bu: MOCK.bu, supplierId: await this.api.supplierId(), matchType: '3-WAY', lines: [{ itemCode: item, description: `${item} (MOCK)`, quantity, unitPrice }] });
    await this.api.ok('buyer', 'POST', `/purchaseOrders/${po.id}/submit`); await this.api.approveAll(po.id, 'purchaseOrders');
    const rc = await this.api.receive(po.id, quantity);
    return { po, receipt: rc as { number: string } };
  }
  async dists(q: string) { return (await this.restQ<DistRow>('cost_accountant', 'costAccountingDistributions', q)).items; }
  async rcvDists(q: string) { return (await this.restQ<DistRow>('cost_accountant', 'receiptAccountingDistributions', q)).items; }
  /** Sum of Dr / Cr for one account class across distribution rows. */
  static sum(rows: DistRow[], cls: string, side: 'EnteredDr' | 'EnteredCr') { return Math.round(rows.filter((r) => r.AccountClass === cls).reduce((a, r) => a + r[side], 0) * 100) / 100; }
  // Work orders (shared engine; P2M pack UI later). Used by the WOC-001 chain as a recipe stand-in.
  wo(method: string, path: string, data?: unknown) { return this.api.ok('scm_integration', method, `/scm/workOrders${path}`, data); }
}