@robosystems/client 0.2.48 → 0.2.49

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -67,14 +67,128 @@ export interface PeriodCloseStatus {
67
67
  totalDraft: number;
68
68
  totalPosted: number;
69
69
  }
70
+ /**
71
+ * Outcome of an idempotent `createClosingEntry` call.
72
+ *
73
+ * - `created` — no prior draft, new draft created
74
+ * - `unchanged` — prior draft matches current schedule fact, no-op
75
+ * - `regenerated` — prior draft was stale, replaced with a fresh one
76
+ * - `removed` — prior draft existed but schedule no longer covers this period
77
+ * - `skipped` — no prior draft and no in-scope fact; nothing to do
78
+ */
79
+ export type ClosingEntryOutcome = 'created' | 'unchanged' | 'regenerated' | 'removed' | 'skipped';
80
+ /**
81
+ * Result of an idempotent closing-entry call. `entry_id`, `amount`, and
82
+ * related fields are null for `removed` and `skipped` outcomes.
83
+ */
70
84
  export interface ClosingEntry {
71
- entryId: string;
85
+ outcome: ClosingEntryOutcome;
86
+ entryId: string | null;
87
+ status: string | null;
88
+ postingDate: string | null;
89
+ memo: string | null;
90
+ debitElementId: string | null;
91
+ creditElementId: string | null;
92
+ amount: number | null;
93
+ reason: string | null;
94
+ }
95
+ export type LedgerEntryType = 'standard' | 'adjusting' | 'closing' | 'reversing';
96
+ export interface FiscalPeriodSummary {
97
+ name: string;
98
+ startDate: string;
99
+ endDate: string;
72
100
  status: string;
101
+ closedAt: string | null;
102
+ }
103
+ export interface FiscalCalendarState {
104
+ graphId: string;
105
+ fiscalYearStartMonth: number;
106
+ closedThrough: string | null;
107
+ closeTarget: string | null;
108
+ gapPeriods: number;
109
+ catchUpSequence: string[];
110
+ closeableNow: boolean;
111
+ blockers: string[];
112
+ lastCloseAt: string | null;
113
+ initializedAt: string | null;
114
+ lastSyncAt: string | null;
115
+ periods: FiscalPeriodSummary[];
116
+ }
117
+ export interface InitializeLedgerOptions {
118
+ closedThrough?: string | null;
119
+ fiscalYearStartMonth?: number;
120
+ earliestDataPeriod?: string | null;
121
+ autoSeedSchedules?: boolean;
122
+ note?: string | null;
123
+ }
124
+ export interface InitializeLedgerResult {
125
+ fiscalCalendar: FiscalCalendarState;
126
+ periodsCreated: number;
127
+ warnings: string[];
128
+ }
129
+ export interface ClosePeriodOptions {
130
+ note?: string | null;
131
+ allowStaleSync?: boolean;
132
+ }
133
+ export interface ClosePeriodResult {
134
+ period: string;
135
+ entriesPosted: number;
136
+ targetAutoAdvanced: boolean;
137
+ fiscalCalendar: FiscalCalendarState;
138
+ }
139
+ export interface DraftLineItemView {
140
+ lineItemId: string;
141
+ elementId: string;
142
+ elementCode: string | null;
143
+ elementName: string;
144
+ debitAmount: number;
145
+ creditAmount: number;
146
+ description: string | null;
147
+ }
148
+ export interface DraftEntryView {
149
+ entryId: string;
150
+ postingDate: string;
151
+ type: string;
152
+ memo: string | null;
153
+ provenance: string | null;
154
+ sourceStructureId: string | null;
155
+ sourceStructureName: string | null;
156
+ lineItems: DraftLineItemView[];
157
+ totalDebit: number;
158
+ totalCredit: number;
159
+ balanced: boolean;
160
+ }
161
+ export interface PeriodDraftsView {
162
+ period: string;
163
+ periodStart: string;
164
+ periodEnd: string;
165
+ draftCount: number;
166
+ totalDebit: number;
167
+ totalCredit: number;
168
+ allBalanced: boolean;
169
+ drafts: DraftEntryView[];
170
+ }
171
+ export interface ManualClosingLineItem {
172
+ elementId: string;
173
+ debitAmount?: number;
174
+ creditAmount?: number;
175
+ description?: string | null;
176
+ }
177
+ export interface CreateManualClosingEntryOptions {
73
178
  postingDate: string;
74
179
  memo: string;
75
- debitElementId: string;
76
- creditElementId: string;
77
- amount: number;
180
+ lineItems: ManualClosingLineItem[];
181
+ entryType?: LedgerEntryType;
182
+ }
183
+ export interface TruncateScheduleOptions {
184
+ newEndDate: string;
185
+ reason: string;
186
+ }
187
+ export interface TruncateScheduleResult {
188
+ structureId: string;
189
+ newEndDate: string;
190
+ factsDeleted: number;
191
+ reason: string;
78
192
  }
79
193
  export interface CreateScheduleOptions {
80
194
  name: string;
@@ -85,7 +199,7 @@ export interface CreateScheduleOptions {
85
199
  entryTemplate: {
86
200
  debitElementId: string;
87
201
  creditElementId: string;
88
- entryType?: string;
202
+ entryType?: LedgerEntryType;
89
203
  memoTemplate?: string;
90
204
  };
91
205
  taxonomyId?: string;
@@ -221,7 +335,14 @@ export declare class LedgerClient {
221
335
  */
222
336
  getPeriodCloseStatus(graphId: string, periodStart: string, periodEnd: string): Promise<PeriodCloseStatus>;
223
337
  /**
224
- * Create a draft closing entry from a schedule's facts for a period.
338
+ * Idempotently create (or refresh) a draft closing entry from a schedule's
339
+ * facts for a period. The `outcome` field describes what actually happened:
340
+ *
341
+ * - `created` — new draft
342
+ * - `unchanged` — existing draft still matches the schedule; no-op
343
+ * - `regenerated` — existing draft was stale; replaced
344
+ * - `removed` — schedule no longer covers this period; stale draft deleted
345
+ * - `skipped` — no existing draft and no in-scope fact; nothing to do
225
346
  */
226
347
  createClosingEntry(graphId: string, structureId: string, postingDate: string, periodStart: string, periodEnd: string, memo?: string): Promise<ClosingEntry>;
227
348
  /**
@@ -239,4 +360,57 @@ export declare class LedgerClient {
239
360
  startDate?: string;
240
361
  endDate?: string;
241
362
  }): Promise<AccountRollupsResponse>;
363
+ /**
364
+ * Initialize the fiscal calendar for a graph. Creates FiscalPeriod rows
365
+ * for the data window, sets `closed_through` / `close_target`, and emits
366
+ * an `initialized` audit event. Fails with 409 if already initialized.
367
+ */
368
+ initializeLedger(graphId: string, options?: InitializeLedgerOptions): Promise<InitializeLedgerResult>;
369
+ /**
370
+ * Get the current fiscal calendar state — pointers, gap, closeable status.
371
+ */
372
+ getFiscalCalendar(graphId: string): Promise<FiscalCalendarState>;
373
+ /**
374
+ * Set the close target for a graph. Validates that the target is not in
375
+ * the future and not before `closed_through`.
376
+ */
377
+ setCloseTarget(graphId: string, period: string, note?: string | null): Promise<FiscalCalendarState>;
378
+ /**
379
+ * Close a fiscal period — the final commit action.
380
+ *
381
+ * Validates closeable gates, transitions all draft entries in the period
382
+ * to `posted`, marks the FiscalPeriod closed, and advances `closed_through`
383
+ * (auto-advancing `close_target` when reached).
384
+ */
385
+ closePeriod(graphId: string, period: string, options?: ClosePeriodOptions): Promise<ClosePeriodResult>;
386
+ /**
387
+ * Reopen a closed fiscal period. Requires a non-empty `reason` for the
388
+ * audit log. Posted entries stay posted; the period transitions to
389
+ * `closing` so the user can post adjustments and re-close.
390
+ */
391
+ reopenPeriod(graphId: string, period: string, reason: string, note?: string | null): Promise<FiscalCalendarState>;
392
+ /**
393
+ * List all draft entries in a fiscal period for review before close.
394
+ * Fully expanded with line items, element metadata, and per-entry balance.
395
+ *
396
+ * Pure read — call repeatedly without side effects.
397
+ */
398
+ listPeriodDrafts(graphId: string, period: string): Promise<PeriodDraftsView>;
399
+ /**
400
+ * Truncate a schedule — end it early by deleting facts with
401
+ * `period_start > new_end_date`, along with any stale draft entries they
402
+ * produced. Historical posted facts are preserved.
403
+ *
404
+ * `new_end_date` must be a month-end date (service enforces this).
405
+ */
406
+ truncateSchedule(graphId: string, structureId: string, options: TruncateScheduleOptions): Promise<TruncateScheduleResult>;
407
+ /**
408
+ * Create a manual draft closing entry with arbitrary balanced line items.
409
+ * Not tied to a schedule — used for disposals, adjustments, and other
410
+ * one-off closing events.
411
+ *
412
+ * Line items must sum to balanced debits/credits. Rejects entries
413
+ * targeting an already-closed period.
414
+ */
415
+ createManualClosingEntry(graphId: string, options: CreateManualClosingEntryOptions): Promise<ClosingEntry>;
242
416
  }
@@ -432,7 +432,14 @@ class LedgerClient {
432
432
  };
433
433
  }
434
434
  /**
435
- * Create a draft closing entry from a schedule's facts for a period.
435
+ * Idempotently create (or refresh) a draft closing entry from a schedule's
436
+ * facts for a period. The `outcome` field describes what actually happened:
437
+ *
438
+ * - `created` — new draft
439
+ * - `unchanged` — existing draft still matches the schedule; no-op
440
+ * - `regenerated` — existing draft was stale; replaced
441
+ * - `removed` — schedule no longer covers this period; stale draft deleted
442
+ * - `skipped` — no existing draft and no in-scope fact; nothing to do
436
443
  */
437
444
  async createClosingEntry(graphId, structureId, postingDate, periodStart, periodEnd, memo) {
438
445
  const body = {
@@ -449,15 +456,7 @@ class LedgerClient {
449
456
  throw new Error(`Create closing entry failed: ${JSON.stringify(response.error)}`);
450
457
  }
451
458
  const data = response.data;
452
- return {
453
- entryId: data.entry_id,
454
- status: data.status,
455
- postingDate: data.posting_date,
456
- memo: data.memo,
457
- debitElementId: data.debit_element_id,
458
- creditElementId: data.credit_element_id,
459
- amount: data.amount,
460
- };
459
+ return toClosingEntry(data);
461
460
  }
462
461
  // ── Closing Book ─────────────────────────────────────────────────────
463
462
  /**
@@ -492,5 +491,247 @@ class LedgerClient {
492
491
  }
493
492
  return response.data;
494
493
  }
494
+ // ── Fiscal Calendar ─────────────────────────────────────────────────
495
+ /**
496
+ * Initialize the fiscal calendar for a graph. Creates FiscalPeriod rows
497
+ * for the data window, sets `closed_through` / `close_target`, and emits
498
+ * an `initialized` audit event. Fails with 409 if already initialized.
499
+ */
500
+ async initializeLedger(graphId, options) {
501
+ const body = {
502
+ closed_through: options?.closedThrough ?? null,
503
+ fiscal_year_start_month: options?.fiscalYearStartMonth,
504
+ earliest_data_period: options?.earliestDataPeriod ?? null,
505
+ auto_seed_schedules: options?.autoSeedSchedules,
506
+ note: options?.note ?? null,
507
+ };
508
+ const response = await (0, sdk_gen_1.initializeLedger)({
509
+ path: { graph_id: graphId },
510
+ body,
511
+ });
512
+ if (response.error) {
513
+ throw new Error(`Initialize ledger failed: ${JSON.stringify(response.error)}`);
514
+ }
515
+ const data = response.data;
516
+ return {
517
+ fiscalCalendar: toFiscalCalendarState(data.fiscal_calendar),
518
+ periodsCreated: data.periods_created ?? 0,
519
+ warnings: data.warnings ?? [],
520
+ };
521
+ }
522
+ /**
523
+ * Get the current fiscal calendar state — pointers, gap, closeable status.
524
+ */
525
+ async getFiscalCalendar(graphId) {
526
+ const response = await (0, sdk_gen_1.getFiscalCalendar)({
527
+ path: { graph_id: graphId },
528
+ });
529
+ if (response.error) {
530
+ throw new Error(`Get fiscal calendar failed: ${JSON.stringify(response.error)}`);
531
+ }
532
+ return toFiscalCalendarState(response.data);
533
+ }
534
+ /**
535
+ * Set the close target for a graph. Validates that the target is not in
536
+ * the future and not before `closed_through`.
537
+ */
538
+ async setCloseTarget(graphId, period, note) {
539
+ const body = {
540
+ period,
541
+ note: note ?? null,
542
+ };
543
+ const response = await (0, sdk_gen_1.setCloseTarget)({
544
+ path: { graph_id: graphId },
545
+ body,
546
+ });
547
+ if (response.error) {
548
+ throw new Error(`Set close target failed: ${JSON.stringify(response.error)}`);
549
+ }
550
+ return toFiscalCalendarState(response.data);
551
+ }
552
+ /**
553
+ * Close a fiscal period — the final commit action.
554
+ *
555
+ * Validates closeable gates, transitions all draft entries in the period
556
+ * to `posted`, marks the FiscalPeriod closed, and advances `closed_through`
557
+ * (auto-advancing `close_target` when reached).
558
+ */
559
+ async closePeriod(graphId, period, options) {
560
+ const body = {
561
+ note: options?.note ?? null,
562
+ allow_stale_sync: options?.allowStaleSync,
563
+ };
564
+ const response = await (0, sdk_gen_1.closeFiscalPeriod)({
565
+ path: { graph_id: graphId, period },
566
+ body,
567
+ });
568
+ if (response.error) {
569
+ throw new Error(`Close period failed: ${JSON.stringify(response.error)}`);
570
+ }
571
+ const data = response.data;
572
+ return {
573
+ period: data.period,
574
+ entriesPosted: data.entries_posted ?? 0,
575
+ targetAutoAdvanced: data.target_auto_advanced ?? false,
576
+ fiscalCalendar: toFiscalCalendarState(data.fiscal_calendar),
577
+ };
578
+ }
579
+ /**
580
+ * Reopen a closed fiscal period. Requires a non-empty `reason` for the
581
+ * audit log. Posted entries stay posted; the period transitions to
582
+ * `closing` so the user can post adjustments and re-close.
583
+ */
584
+ async reopenPeriod(graphId, period, reason, note) {
585
+ const body = {
586
+ reason,
587
+ note: note ?? null,
588
+ };
589
+ const response = await (0, sdk_gen_1.reopenFiscalPeriod)({
590
+ path: { graph_id: graphId, period },
591
+ body,
592
+ });
593
+ if (response.error) {
594
+ throw new Error(`Reopen period failed: ${JSON.stringify(response.error)}`);
595
+ }
596
+ return toFiscalCalendarState(response.data);
597
+ }
598
+ /**
599
+ * List all draft entries in a fiscal period for review before close.
600
+ * Fully expanded with line items, element metadata, and per-entry balance.
601
+ *
602
+ * Pure read — call repeatedly without side effects.
603
+ */
604
+ async listPeriodDrafts(graphId, period) {
605
+ const response = await (0, sdk_gen_1.listPeriodDrafts)({
606
+ path: { graph_id: graphId, period },
607
+ });
608
+ if (response.error) {
609
+ throw new Error(`List period drafts failed: ${JSON.stringify(response.error)}`);
610
+ }
611
+ const data = response.data;
612
+ return {
613
+ period: data.period,
614
+ periodStart: data.period_start,
615
+ periodEnd: data.period_end,
616
+ draftCount: data.draft_count,
617
+ totalDebit: data.total_debit,
618
+ totalCredit: data.total_credit,
619
+ allBalanced: data.all_balanced,
620
+ drafts: (data.drafts ?? []).map((d) => ({
621
+ entryId: d.entry_id,
622
+ postingDate: d.posting_date,
623
+ type: d.type,
624
+ memo: d.memo ?? null,
625
+ provenance: d.provenance ?? null,
626
+ sourceStructureId: d.source_structure_id ?? null,
627
+ sourceStructureName: d.source_structure_name ?? null,
628
+ lineItems: d.line_items.map((li) => ({
629
+ lineItemId: li.line_item_id,
630
+ elementId: li.element_id,
631
+ elementCode: li.element_code ?? null,
632
+ elementName: li.element_name,
633
+ debitAmount: li.debit_amount,
634
+ creditAmount: li.credit_amount,
635
+ description: li.description ?? null,
636
+ })),
637
+ totalDebit: d.total_debit,
638
+ totalCredit: d.total_credit,
639
+ balanced: d.balanced,
640
+ })),
641
+ };
642
+ }
643
+ // ── Schedule mutations ──────────────────────────────────────────────
644
+ /**
645
+ * Truncate a schedule — end it early by deleting facts with
646
+ * `period_start > new_end_date`, along with any stale draft entries they
647
+ * produced. Historical posted facts are preserved.
648
+ *
649
+ * `new_end_date` must be a month-end date (service enforces this).
650
+ */
651
+ async truncateSchedule(graphId, structureId, options) {
652
+ const body = {
653
+ new_end_date: options.newEndDate,
654
+ reason: options.reason,
655
+ };
656
+ const response = await (0, sdk_gen_1.truncateSchedule)({
657
+ path: { graph_id: graphId, structure_id: structureId },
658
+ body,
659
+ });
660
+ if (response.error) {
661
+ throw new Error(`Truncate schedule failed: ${JSON.stringify(response.error)}`);
662
+ }
663
+ const data = response.data;
664
+ return {
665
+ structureId: data.structure_id,
666
+ newEndDate: data.new_end_date,
667
+ factsDeleted: data.facts_deleted,
668
+ reason: data.reason,
669
+ };
670
+ }
671
+ /**
672
+ * Create a manual draft closing entry with arbitrary balanced line items.
673
+ * Not tied to a schedule — used for disposals, adjustments, and other
674
+ * one-off closing events.
675
+ *
676
+ * Line items must sum to balanced debits/credits. Rejects entries
677
+ * targeting an already-closed period.
678
+ */
679
+ async createManualClosingEntry(graphId, options) {
680
+ const body = {
681
+ posting_date: options.postingDate,
682
+ memo: options.memo,
683
+ entry_type: options.entryType,
684
+ line_items: options.lineItems.map((li) => ({
685
+ element_id: li.elementId,
686
+ debit_amount: li.debitAmount ?? 0,
687
+ credit_amount: li.creditAmount ?? 0,
688
+ description: li.description ?? null,
689
+ })),
690
+ };
691
+ const response = await (0, sdk_gen_1.createManualClosingEntry)({
692
+ path: { graph_id: graphId },
693
+ body,
694
+ });
695
+ if (response.error) {
696
+ throw new Error(`Create manual closing entry failed: ${JSON.stringify(response.error)}`);
697
+ }
698
+ return toClosingEntry(response.data);
699
+ }
495
700
  }
496
701
  exports.LedgerClient = LedgerClient;
702
+ // ── Internal helpers ──────────────────────────────────────────────────
703
+ function toClosingEntry(data) {
704
+ return {
705
+ outcome: data.outcome,
706
+ entryId: data.entry_id ?? null,
707
+ status: data.status ?? null,
708
+ postingDate: data.posting_date ?? null,
709
+ memo: data.memo ?? null,
710
+ debitElementId: data.debit_element_id ?? null,
711
+ creditElementId: data.credit_element_id ?? null,
712
+ amount: data.amount ?? null,
713
+ reason: data.reason ?? null,
714
+ };
715
+ }
716
+ function toFiscalCalendarState(data) {
717
+ return {
718
+ graphId: data.graph_id,
719
+ fiscalYearStartMonth: data.fiscal_year_start_month,
720
+ closedThrough: data.closed_through ?? null,
721
+ closeTarget: data.close_target ?? null,
722
+ gapPeriods: data.gap_periods ?? 0,
723
+ catchUpSequence: data.catch_up_sequence ?? [],
724
+ closeableNow: data.closeable_now ?? false,
725
+ blockers: data.blockers ?? [],
726
+ lastCloseAt: data.last_close_at ?? null,
727
+ initializedAt: data.initialized_at ?? null,
728
+ lastSyncAt: data.last_sync_at ?? null,
729
+ periods: (data.periods ?? []).map((p) => ({
730
+ name: p.name,
731
+ startDate: p.start_date,
732
+ endDate: p.end_date,
733
+ status: p.status,
734
+ closedAt: p.closed_at ?? null,
735
+ })),
736
+ };
737
+ }