File size: 7,801 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { expect } from '@playwright/test';
import { MockApi, MOCK, unique } from '../../api';
import { DATA } from './labels';

/** O2C data recipes (tests/data-recipes/o2c) built through the MOCK O2C pack's internal API (/api/o2c) and shared /api/processes. */
export class O2CApi {
  constructor(public api: MockApi) {}
  private ok(as: string, method: string, path: string, data?: unknown) { return this.api.ok(as as any, method, `/o2c${path}`, data); }
  seed(): Promise<{ creditExposureInvoice: string; otherBuOrder: string; otherBuInvoice: string }> { return this.ok('integration_user', 'GET', '/test/seed'); }
  async process(as: string, name: string, params: Record<string, unknown> = {}) {
    const p = await this.api.ok(as as any, 'POST', '/processes', { name, params });
    expect(p.status, `${name}: ${p.log}`).toBe('SUCCEEDED'); return p;
  }
  /** DR-O2C-ORD-001: submitted order awaiting shipping (qty 5 x ZZ_O2C_ITEM). */
  async bookedOrder(o: { customer?: string; quantity?: number } = {}) {
    const ord = await this.ok('order_entry', 'POST', '/orders', { customerName: o.customer ?? DATA.customer, bu: DATA.bu, customerPO: unique('ZZ-CPO'), lines: [{ item: DATA.item, quantity: o.quantity ?? 5 }] });
    return this.ok('order_entry', 'POST', `/orders/${ord.id}/submit`);
  }
  /** Credit-held order for the credit customer (DR-O2C-CRD-001). */
  async creditHeldOrder() { const o = await this.bookedOrder({ customer: DATA.creditCustomer }); expect(o.status).toBe('ON_HOLD'); return o; }
  /** Pick wave via the shared scheduled process; returns { pickSlip, shipment } numbers. */
  async pickWave(orderNumber: string) {
    const p = await this.process('shipping_user', 'Create Pick Wave', { order: orderNumber, shipFromOrg: DATA.shipFromOrg });
    return { pickSlip: p.result.pickSlips[0] as string, shipment: p.result.shipments[0] as string };
  }
  /** DR-O2C-ORD-002: order picked and ship-confirmed (line Shipped, awaiting billing). */
  async shippedOrder() {
    const o = await this.bookedOrder(); const w = await this.pickWave(o.number);
    await this.ok('shipping_user', 'POST', `/pickSlips/${w.pickSlip}/confirm`);
    const sh = (await this.ok('shipping_user', 'GET', '/shipments')).items.find((s: any) => s.number === w.shipment);
    await this.ok('shipping_user', 'POST', `/shipments/${sh.id}/confirm`); return o;
  }
  /** DR-O2C-AR-001: open AR invoice 583.20 created by AutoInvoice from a shipped order. */
  async openInvoice() {
    const o = await this.shippedOrder(); await this.process('billing_specialist', 'Import AutoInvoice', { businessUnit: DATA.bu, transactionSource: DATA.arSourceOm });
    const inv = (await this.ok('ar_specialist', 'GET', '/invoices')).items.find((i: any) => i.salesOrder === o.number);
    expect(inv, `AutoInvoice invoice for order ${o.number}`).toBeTruthy(); return { order: o, invoice: inv };
  }
  /** DR-O2C-RCP-001: receipt fully applied to an open invoice. */
  async appliedReceipt() {
    const { invoice, order } = await this.openInvoice();
    const r = await this.ok('ar_specialist', 'POST', '/receipts', { bu: DATA.bu, method: DATA.receiptMethod, number: unique('ZZ-RCPT'), amount: invoice.total, customerName: DATA.customer });
    const applied = await this.ok('ar_specialist', 'POST', `/receipts/${r.id}/apply`, { invoiceId: invoice.id }); return { invoice, order, receipt: applied };
  }
  // ---------------- P2 recipes (MOCK only; synthetic ZZ data) ----------------
  /** ISO date offset from today (box clock). */
  static day(offset: number) { const d = new Date(); d.setDate(d.getDate() + offset); return d.toISOString().slice(0, 10); }
  /** Manual invoice (Net 30) for a customer: subtotal + 8% tax (ASSUMED rate). */
  async manualInvoice(o: { customer?: string; unitPrice: number; trxDate?: string; quantity?: number }) {
    return this.ok('billing_specialist', 'POST', '/invoices', { bu: DATA.bu, source: DATA.arSourceManual, type: DATA.arTypeInvoice, customerName: o.customer ?? DATA.customer,
      trxDate: o.trxDate ?? O2CApi.day(0), lines: [{ description: DATA.memoLine, quantity: o.quantity ?? 1, unitPrice: o.unitPrice }] });
  }
  /** DR-O2C-AR-006 (COL-002/003): overdue customer with 583.20 current (trx today-20, due in 10 days) and 432.00 45 days past due (trx today-75). */
  async overdueCustomerInvoices() {
    const current = await this.manualInvoice({ customer: DATA.overdueCustomer, unitPrice: 540, trxDate: O2CApi.day(-20) });
    const overdue = await this.manualInvoice({ customer: DATA.overdueCustomer, unitPrice: 400, trxDate: O2CApi.day(-75) });
    return { current, overdue };
  }
  async creditMemo(invoiceId: string, quantity = -1) { return this.ok('billing_specialist', 'POST', `/invoices/${invoiceId}/credit`, { lineNum: 1, quantity }); }
  async approvedAdjustment(invoiceId: string, amount: number) {
    const a = await this.ok('ar_specialist', 'POST', `/invoices/${invoiceId}/adjustments`, { activity: DATA.adjustmentActivity, amount });
    return a.status === 'APPROVED' ? a : this.ok('ar_manager', 'POST', `/adjustments/${a.id}/approve`);
  }
  async confirmPick(pickSlip: string, pickedQuantity?: number) { return this.ok('shipping_user', 'POST', `/pickSlips/${pickSlip}/confirm`, pickedQuantity === undefined ? {} : { pickedQuantity }); }
  async customerId(name: string = DATA.customer) { const r = await this.restGet('customer_admin', 'accounts', { OrganizationName: name }); return r.items[0].PartyId as string; }
  async setPaymentDetails(name: string, d: { paymentTerms?: string; receiptMethod?: string }) { return this.ok('customer_admin', 'POST', `/customers/${await this.customerId(name)}/payment`, d); }
  async stageAutoInvoiceLines(rows: Record<string, unknown>[]) { return this.ok('integration_user', 'POST', '/autoInvoiceLines', { rows }); }
  async stageFile(name: string, content: string) { return this.ok('integration_user', 'POST', '/files', { name, content }); }
  async stageRevenueSource(orderNumber: string, lines: { item: string; sellingPrice: number }[]) { return this.ok('revenue_analyst', 'POST', '/revenueSources', { orderNumber, lines }); }
  async collections() { return this.ok('ar_manager', 'GET', '/collections'); }
  async stageLockbox(name: string, content: string) { return this.ok('ar_specialist', 'POST', '/lockboxFiles', { name, content }); }
  async onhand(item: string = DATA.item, org: string = DATA.shipFromOrg) {
    const r = await this.api.restOne('integration_user', 'inventoryOnhandBalances', 'ItemNumber', item);
    return r.items.find((x: any) => x.OrganizationCode === org).OnhandQuantity as number;
  }
  /** DR-O2C-INT-003 stand-in: the MOCK's synthetic OM extension/flexfield inventory (same output keys as the pod prober export). */
  extensionInventory(): Promise<{ source: string; om_extensions: OmExtension[]; page_extensions: PageExtension[] }> { return this.ok('order_entry', 'GET', '/extensions'); }
  /** REST facade GET with an exact-match q filter (Fusion-style). */
  async restGet(as: string, resource: string, q: Record<string, string>) {
    const qs = Object.entries(q).map(([k, v]) => `${k}='${v}'`).join(';');
    const r = await this.api.call(as as any, 'GET', `/${resource}?q=${encodeURIComponent(qs)}`, undefined, MOCK.restBase);
    expect(r.status, `GET ${resource} as ${as}`).toBe(200); return r.body as { items: any[]; count: number };
  }
}

export type OmExtension = { name: string; event: string; kind: string; trigger: Record<string, string>;
  effect: { kind: 'attribute' | 'message' | 'log'; label?: string; value?: string; text?: string; table?: string } };
export type PageExtension = { name: string; type: string; context: string; field: string; label: string; values: string[] };