import { http } from './http.js'; /** * Append-only stock and payment ledger. * Entry = { id, manifestarticle_id, sku, transaction_type, amount, transaction_text, hash_version, prev_hash, created_at } * Input = { manifestarticle_id, transaction_type, amount, transaction_text } */ export const ledger = { /** * GET /api/ledger/history?offset=0&limit=50&search=...&seekMonths=-1 * Append-only ledger audit history with chunk paging, search, and seek filtering * @param {{ offset?: number, limit?: number, search?: string, seekMonths?: number, sortModel?: Array, filterModel?: object, sku?: string, transactionType?: string }} [params] * → { ledger: LedgerEntry[], history?: LedgerEntry[], total: number, unfilteredTotal?: number } */ // #region GET /api/ledger/history — getHistory getHistory({ offset = 0, limit = 50, search, seekMonths, sortModel, filterModel, sku, transactionType } = {}) { const params = new URLSearchParams(); params.set('offset', String(Math.max(0, Math.floor(Number(offset) || 0)))); params.set('limit', String(Math.max(1, Math.floor(Number(limit) || 50)))); if (search && String(search).trim()) params.set('search', String(search).trim()); if (seekMonths != null && Number(seekMonths) < 0) { params.set('seekMonths', String(Math.floor(Number(seekMonths)))); } if (sku != null) params.set('sku', String(sku)); if (transactionType != null) params.set('transaction_type', String(transactionType)); if (sortModel && sortModel.length) params.set('sort', JSON.stringify(sortModel)); if (filterModel && Object.keys(filterModel).length) params.set('filter', JSON.stringify(filterModel)); const qs = params.toString(); return http(`/api/ledger/history${qs ? `?${qs}` : ''}`); }, // #endregion /** * GET /api/ledger/manifestarticle/:manifestarticleId * Query: ?transaction_type=... * → 200 { ledger: LedgerEntry[] } */ // #region GET /api/ledger/manifestarticle/:manifestarticleId — listByManifestarticleId listByManifestarticleId(manifestarticleId, transactionType) { const query = transactionType ? `?transaction_type=${encodeURIComponent(transactionType)}` : ''; return http(`/api/ledger/manifestarticle/${encodeURIComponent(manifestarticleId)}${query}`); }, // #endregion /** * GET /api/ledger * Query: { manifestarticleId?, sku?, transactionType? } * → 200 { ledger: LedgerEntry[] } */ // #region GET /api/ledger — list list({ manifestarticleId, sku, transactionType } = {}) { const params = new URLSearchParams(); if (manifestarticleId != null) params.set('manifestarticle_id', String(manifestarticleId)); if (sku != null) params.set('sku', String(sku)); if (transactionType != null) params.set('transaction_type', String(transactionType)); const qs = params.toString(); return http(`/api/ledger${qs ? `?${qs}` : ''}`); }, // #endregion };