@managesome/eposmatic-toolkit 0.4.17 → 0.4.20

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.
Files changed (38) hide show
  1. package/esm/constants/ENUMS.js +1 -0
  2. package/esm/constants/enums/accounting.js +80 -0
  3. package/esm/helpers/materialized-reports/materialized-discount-close-day-helpers.js +6 -0
  4. package/esm/helpers/materialized-reports/materialized-reports-selection-helpers.js +4 -6
  5. package/esm/schemas/models/index.js +2 -0
  6. package/esm/schemas/models/tenant_chart_of_accounts.js +80 -0
  7. package/esm/schemas/models/tenant_journal_entries.js +147 -0
  8. package/esm/schemas/types/index.js +2 -0
  9. package/esm/schemas/types/tenant_chart_of_accounts.js +1 -0
  10. package/esm/schemas/types/tenant_journal_entries.js +1 -0
  11. package/lib/constants/ENUMS.d.ts +1 -0
  12. package/lib/constants/ENUMS.js +1 -0
  13. package/lib/constants/enums/accounting.d.ts +62 -0
  14. package/lib/constants/enums/accounting.js +83 -0
  15. package/lib/helpers/materialized-reports/materialized-discount-close-day-helpers.d.ts +7 -0
  16. package/lib/helpers/materialized-reports/materialized-discount-close-day-helpers.js +6 -0
  17. package/lib/helpers/materialized-reports/materialized-reports-selection-helpers.js +4 -6
  18. package/lib/helpers/order/payment-collection.d.ts +25 -0
  19. package/lib/helpers/order/payment-collection.js +51 -0
  20. package/lib/schemas/models/index.d.ts +2 -0
  21. package/lib/schemas/models/index.js +2 -0
  22. package/lib/schemas/models/tenant_chart_of_accounts.d.ts +66 -0
  23. package/lib/schemas/models/tenant_chart_of_accounts.js +83 -0
  24. package/lib/schemas/models/tenant_hr_documents.d.ts +3 -3
  25. package/lib/schemas/models/tenant_journal_entries.d.ts +132 -0
  26. package/lib/schemas/models/tenant_journal_entries.js +150 -0
  27. package/lib/schemas/types/index.d.ts +2 -0
  28. package/lib/schemas/types/index.js +2 -0
  29. package/lib/schemas/types/tenant_chart_of_accounts.d.ts +23 -0
  30. package/lib/schemas/types/tenant_chart_of_accounts.js +2 -0
  31. package/lib/schemas/types/tenant_journal_entries.d.ts +34 -0
  32. package/lib/schemas/types/tenant_journal_entries.js +2 -0
  33. package/lib/tests/materialized-reports/materialized-discount-close-day.test.js +16 -0
  34. package/lib/tests/materialized-reports/materialized-reports-selection-helpers.test.js +26 -18
  35. package/lib/tests/payment-collection-timestamps.test.d.ts +1 -0
  36. package/lib/tests/payment-collection-timestamps.test.js +71 -0
  37. package/lib/tests/reporting-contract.test.js +7 -0
  38. package/package.json +1 -1
@@ -1,5 +1,6 @@
1
1
  // NOTE: split into ./enums/* (per-domain files) so bundlers only pull what is used.
2
2
  // This file stays as a barrel — existing imports from "./ENUMS" keep working.
3
+ export * from './enums/accounting';
3
4
  export * from './enums/cash';
4
5
  export * from './enums/communications';
5
6
  export * from './enums/delivery';
@@ -0,0 +1,80 @@
1
+ // Auto-split from constants/ENUMS.ts — see ENUMS.ts barrel for exports.
2
+ /* ============================== Chart of accounts ============================== */
3
+ export var ACCOUNT_TYPE_ENUM;
4
+ (function (ACCOUNT_TYPE_ENUM) {
5
+ ACCOUNT_TYPE_ENUM["ASSET"] = "ASSET";
6
+ ACCOUNT_TYPE_ENUM["LIABILITY"] = "LIABILITY";
7
+ ACCOUNT_TYPE_ENUM["EQUITY"] = "EQUITY";
8
+ ACCOUNT_TYPE_ENUM["INCOME"] = "INCOME";
9
+ ACCOUNT_TYPE_ENUM["COGS"] = "COGS";
10
+ ACCOUNT_TYPE_ENUM["EXPENSE"] = "EXPENSE";
11
+ })(ACCOUNT_TYPE_ENUM || (ACCOUNT_TYPE_ENUM = {}));
12
+ export const ALL_ACCOUNT_TYPES = Object.values(ACCOUNT_TYPE_ENUM);
13
+ export var ACCOUNT_NORMAL_BALANCE_ENUM;
14
+ (function (ACCOUNT_NORMAL_BALANCE_ENUM) {
15
+ ACCOUNT_NORMAL_BALANCE_ENUM["DEBIT"] = "DEBIT";
16
+ ACCOUNT_NORMAL_BALANCE_ENUM["CREDIT"] = "CREDIT";
17
+ })(ACCOUNT_NORMAL_BALANCE_ENUM || (ACCOUNT_NORMAL_BALANCE_ENUM = {}));
18
+ export const ALL_ACCOUNT_NORMAL_BALANCES = Object.values(ACCOUNT_NORMAL_BALANCE_ENUM);
19
+ /** The side that increases each account type — derived on create, never client input. */
20
+ export const ACCOUNT_TYPE_NORMAL_BALANCE = {
21
+ [ACCOUNT_TYPE_ENUM.ASSET]: ACCOUNT_NORMAL_BALANCE_ENUM.DEBIT,
22
+ [ACCOUNT_TYPE_ENUM.LIABILITY]: ACCOUNT_NORMAL_BALANCE_ENUM.CREDIT,
23
+ [ACCOUNT_TYPE_ENUM.EQUITY]: ACCOUNT_NORMAL_BALANCE_ENUM.CREDIT,
24
+ [ACCOUNT_TYPE_ENUM.INCOME]: ACCOUNT_NORMAL_BALANCE_ENUM.CREDIT,
25
+ [ACCOUNT_TYPE_ENUM.COGS]: ACCOUNT_NORMAL_BALANCE_ENUM.DEBIT,
26
+ [ACCOUNT_TYPE_ENUM.EXPENSE]: ACCOUNT_NORMAL_BALANCE_ENUM.DEBIT,
27
+ };
28
+ /* ================><================= */
29
+ export var JOURNAL_ENTRY_SOURCE_ENUM;
30
+ (function (JOURNAL_ENTRY_SOURCE_ENUM) {
31
+ /** Hand-written entry. */
32
+ JOURNAL_ENTRY_SOURCE_ENUM["MANUAL"] = "MANUAL";
33
+ /** Money in/out recorded from the Banking page. */
34
+ JOURNAL_ENTRY_SOURCE_ENUM["BANKING"] = "BANKING";
35
+ /** Auto-posted from a completed order. */
36
+ JOURNAL_ENTRY_SOURCE_ENUM["SALE"] = "SALE";
37
+ /** One consolidated entry per shift close (all orders of the shift merged). */
38
+ JOURNAL_ENTRY_SOURCE_ENUM["SHIFT_CLOSE"] = "SHIFT_CLOSE";
39
+ JOURNAL_ENTRY_SOURCE_ENUM["PURCHASE"] = "PURCHASE";
40
+ JOURNAL_ENTRY_SOURCE_ENUM["EXPENSE"] = "EXPENSE";
41
+ JOURNAL_ENTRY_SOURCE_ENUM["PAYROLL"] = "PAYROLL";
42
+ /** Auto-posted from an inventory utility transaction. */
43
+ JOURNAL_ENTRY_SOURCE_ENUM["INVENTORY"] = "INVENTORY";
44
+ /** Opening balances — balanced against the opening-balance equity account. */
45
+ JOURNAL_ENTRY_SOURCE_ENUM["OPENING"] = "OPENING";
46
+ /** Manual adjustment / correction. */
47
+ JOURNAL_ENTRY_SOURCE_ENUM["ADJUSTMENT"] = "ADJUSTMENT";
48
+ /** Reversal of a previously posted entry. */
49
+ JOURNAL_ENTRY_SOURCE_ENUM["REVERSAL"] = "REVERSAL";
50
+ })(JOURNAL_ENTRY_SOURCE_ENUM || (JOURNAL_ENTRY_SOURCE_ENUM = {}));
51
+ export const ALL_JOURNAL_ENTRY_SOURCES = Object.values(JOURNAL_ENTRY_SOURCE_ENUM);
52
+ export var JOURNAL_ENTRY_STATUS_ENUM;
53
+ (function (JOURNAL_ENTRY_STATUS_ENUM) {
54
+ JOURNAL_ENTRY_STATUS_ENUM["POSTED"] = "POSTED";
55
+ JOURNAL_ENTRY_STATUS_ENUM["VOIDED"] = "VOIDED";
56
+ })(JOURNAL_ENTRY_STATUS_ENUM || (JOURNAL_ENTRY_STATUS_ENUM = {}));
57
+ export const ALL_JOURNAL_ENTRY_STATUSES = Object.values(JOURNAL_ENTRY_STATUS_ENUM);
58
+ /** Form-level concept: which side a typed amount falls on. */
59
+ export var JOURNAL_ENTRY_LINE_SIDE_ENUM;
60
+ (function (JOURNAL_ENTRY_LINE_SIDE_ENUM) {
61
+ JOURNAL_ENTRY_LINE_SIDE_ENUM["DEBIT"] = "DEBIT";
62
+ JOURNAL_ENTRY_LINE_SIDE_ENUM["CREDIT"] = "CREDIT";
63
+ })(JOURNAL_ENTRY_LINE_SIDE_ENUM || (JOURNAL_ENTRY_LINE_SIDE_ENUM = {}));
64
+ export const ALL_JOURNAL_ENTRY_LINE_SIDES = Object.values(JOURNAL_ENTRY_LINE_SIDE_ENUM);
65
+ /* ================><================= */
66
+ /** Which basis a single report is generated on. */
67
+ export var REPORTING_BASIS_ENUM;
68
+ (function (REPORTING_BASIS_ENUM) {
69
+ REPORTING_BASIS_ENUM["ACCRUAL"] = "ACCRUAL";
70
+ REPORTING_BASIS_ENUM["CASH"] = "CASH";
71
+ })(REPORTING_BASIS_ENUM || (REPORTING_BASIS_ENUM = {}));
72
+ export const ALL_REPORTING_BASES = Object.values(REPORTING_BASIS_ENUM);
73
+ /** Tenant-level preference: one basis or both side by side. */
74
+ export var REPORTING_METHOD_ENUM;
75
+ (function (REPORTING_METHOD_ENUM) {
76
+ REPORTING_METHOD_ENUM["ACCRUAL_AND_CASH"] = "ACCRUAL_AND_CASH";
77
+ REPORTING_METHOD_ENUM["ACCRUAL_ONLY"] = "ACCRUAL_ONLY";
78
+ REPORTING_METHOD_ENUM["CASH_ONLY"] = "CASH_ONLY";
79
+ })(REPORTING_METHOD_ENUM || (REPORTING_METHOD_ENUM = {}));
80
+ export const ALL_REPORTING_METHODS = Object.values(REPORTING_METHOD_ENUM);
@@ -532,6 +532,12 @@ export function buildTenantDiscountCloseDayPlan(input) {
532
532
  return null;
533
533
  continue;
534
534
  }
535
+ // A missing period entry document is a partial write only when the period
536
+ // COUNTER document exists. Refuse to rebuild from an unverified fetch.
537
+ const periodCounterExists = existingByScope.has(scopeKey(compiled));
538
+ if (periodCounterExists && input.existingEntriesComplete !== true) {
539
+ return null;
540
+ }
535
541
  // Never persisted: rebuild from the sibling days, minus this day's stored rows (a
536
542
  // correction may have REMOVED rows), then merge this day's fresh rows on top.
537
543
  const folded = subtractMaterializedDiscountEntrySections(refoldPeriodEntrySections(compiled.scope_context, String(compiled.scope_field)), storedDayEntrySections);
@@ -656,12 +656,10 @@ const decodeMaterializedReportScopeGroups = (selection) => {
656
656
  window: {
657
657
  fromHour: window.fromHour,
658
658
  toHour: window.toHour,
659
- // A weekday-refined day set is GAPPED — a continuous SPAN can
660
- // never compile it. Weekday + window therefore always compiles
661
- // RECURRING (the same hours on each matching weekday).
662
- kind: SPAN_VIEWS.has(view) && weekdayFilter == null
663
- ? MATERIALIZED_REPORT_WINDOW_KIND_ENUM.SPAN
664
- : MATERIALIZED_REPORT_WINDOW_KIND_ENUM.RECURRING,
659
+ // A time-of-day window means the SAME hours on every selected
660
+ // day. SPAN would read period documents for middle days, which
661
+ // contain all 24 hours and cannot honour the window.
662
+ kind: MATERIALIZED_REPORT_WINDOW_KIND_ENUM.RECURRING,
665
663
  },
666
664
  }
667
665
  : {}),
@@ -67,6 +67,7 @@ export * from './tenant_cash_drawer_sessions';
67
67
  export * from './tenant_cash_drawers';
68
68
  export * from './tenant_cash_in_hand_transactions';
69
69
  export * from './tenant_cash_in_out';
70
+ export * from './tenant_chart_of_accounts';
70
71
  export * from './tenant_clients';
71
72
  export * from './tenant_day_sessions';
72
73
  export * from './tenant_hr_attendance_records';
@@ -87,6 +88,7 @@ export * from './tenant_inventory_department_transactions';
87
88
  export * from './tenant_inventory_department_utilities';
88
89
  export * from './tenant_inventory_departments';
89
90
  export * from './tenant_inventory_entries';
91
+ export * from './tenant_journal_entries';
90
92
  export * from './tenant_metadata';
91
93
  export * from './tenant_order_item_instructions';
92
94
  export * from './tenant_purchase_order_vendor_returns';
@@ -0,0 +1,80 @@
1
+ import { Schema } from 'mongoose';
2
+ import { ACCOUNT_TYPE_NORMAL_BALANCE, ALL_ACCOUNT_NORMAL_BALANCES, ALL_ACCOUNT_TYPES, } from '../../constants';
3
+ export const TenantChartOfAccountsSchema = new Schema({
4
+ _id: {
5
+ type: Schema.Types.ObjectId,
6
+ required: true,
7
+ immutable: true,
8
+ },
9
+ tenant_id: {
10
+ type: String,
11
+ required: true,
12
+ immutable: true,
13
+ ref: 'tenants',
14
+ },
15
+ code: {
16
+ type: String,
17
+ trim: true,
18
+ uppercase: true,
19
+ match: /^[A-Z0-9][A-Z0-9-_]{0,23}$/,
20
+ },
21
+ name: {
22
+ type: String,
23
+ required: true,
24
+ trim: true,
25
+ maxlength: 120,
26
+ },
27
+ type: {
28
+ type: String,
29
+ required: true,
30
+ enum: ALL_ACCOUNT_TYPES,
31
+ },
32
+ increases_on: {
33
+ type: String,
34
+ required: true,
35
+ enum: ALL_ACCOUNT_NORMAL_BALANCES,
36
+ default() {
37
+ return ACCOUNT_TYPE_NORMAL_BALANCE[this.type];
38
+ },
39
+ },
40
+ parent_id: {
41
+ type: Schema.Types.ObjectId,
42
+ ref: 'tenant_chart_of_accounts',
43
+ },
44
+ is_system: {
45
+ type: Boolean,
46
+ default: false,
47
+ },
48
+ is_archived: {
49
+ type: Boolean,
50
+ default: false,
51
+ },
52
+ description: {
53
+ type: String,
54
+ trim: true,
55
+ maxlength: 300,
56
+ default: '',
57
+ },
58
+ sort_id: {
59
+ type: Number,
60
+ required: true,
61
+ min: 0,
62
+ },
63
+ account_number: {
64
+ type: String,
65
+ trim: true,
66
+ },
67
+ bank_name: {
68
+ type: String,
69
+ trim: true,
70
+ },
71
+ bank_identifier_code: {
72
+ type: String,
73
+ trim: true,
74
+ },
75
+ }, {
76
+ timestamps: true,
77
+ versionKey: false,
78
+ minimize: false,
79
+ });
80
+ export const TENANT_CHART_OF_ACCOUNTS_COLLECTION_NAME = 'tenant_chart_of_accounts';
@@ -0,0 +1,147 @@
1
+ import { Schema } from 'mongoose';
2
+ import { ALL_JOURNAL_ENTRY_SOURCES, ALL_JOURNAL_ENTRY_STATUSES, JOURNAL_ENTRY_STATUS_ENUM } from '../../constants';
3
+ const TenantJournalEntryLineSchema = new Schema({
4
+ line_no: {
5
+ type: Number,
6
+ required: true,
7
+ min: 1,
8
+ },
9
+ account_id: {
10
+ type: Schema.Types.ObjectId,
11
+ required: true,
12
+ ref: 'tenant_chart_of_accounts',
13
+ },
14
+ account_code: {
15
+ type: String,
16
+ required: true,
17
+ },
18
+ debit: {
19
+ type: Number,
20
+ required: true,
21
+ min: 0,
22
+ validate: {
23
+ validator: (value) => Number.isInteger(value),
24
+ message: 'debit must be an integer (minor units)',
25
+ },
26
+ },
27
+ credit: {
28
+ type: Number,
29
+ required: true,
30
+ min: 0,
31
+ validate: {
32
+ validator: (value) => Number.isInteger(value),
33
+ message: 'credit must be an integer (minor units)',
34
+ },
35
+ },
36
+ memo: {
37
+ type: String,
38
+ trim: true,
39
+ maxlength: 500,
40
+ default: '',
41
+ },
42
+ }, { _id: true });
43
+ TenantJournalEntryLineSchema.pre('validate', function (next) {
44
+ const line = this;
45
+ if (line.debit > 0 === line.credit > 0) {
46
+ return next(new Error('Each journal line must have exactly one side greater than zero'));
47
+ }
48
+ next();
49
+ });
50
+ export const TenantJournalEntriesSchema = new Schema({
51
+ _id: {
52
+ type: Schema.Types.ObjectId,
53
+ required: true,
54
+ immutable: true,
55
+ },
56
+ tenant_id: {
57
+ type: String,
58
+ required: true,
59
+ immutable: true,
60
+ ref: 'tenants',
61
+ },
62
+ entry_number: {
63
+ type: String,
64
+ required: true,
65
+ immutable: true,
66
+ },
67
+ description: {
68
+ type: String,
69
+ required: true,
70
+ trim: true,
71
+ maxlength: 300,
72
+ },
73
+ reference: {
74
+ type: String,
75
+ trim: true,
76
+ maxlength: 120,
77
+ default: '',
78
+ },
79
+ source_type: {
80
+ type: String,
81
+ required: true,
82
+ enum: ALL_JOURNAL_ENTRY_SOURCES,
83
+ },
84
+ source_id: {
85
+ type: String,
86
+ },
87
+ lines: {
88
+ type: [TenantJournalEntryLineSchema],
89
+ required: true,
90
+ validate: [
91
+ {
92
+ validator: (lines) => lines.length >= 2,
93
+ message: 'A journal entry needs at least two lines',
94
+ },
95
+ {
96
+ validator: (lines) => {
97
+ const totalDebit = lines.reduce((total, line) => total + line.debit, 0);
98
+ const totalCredit = lines.reduce((total, line) => total + line.credit, 0);
99
+ return totalDebit > 0 && totalDebit === totalCredit;
100
+ },
101
+ message: 'Debits must equal credits and be greater than zero',
102
+ },
103
+ ],
104
+ },
105
+ total_debit: {
106
+ type: Number,
107
+ required: true,
108
+ min: 1,
109
+ validate: {
110
+ validator: (value) => Number.isInteger(value),
111
+ message: 'total_debit must be an integer (minor units)',
112
+ },
113
+ },
114
+ total_credit: {
115
+ type: Number,
116
+ required: true,
117
+ min: 1,
118
+ validate: {
119
+ validator: (value) => Number.isInteger(value),
120
+ message: 'total_credit must be an integer (minor units)',
121
+ },
122
+ },
123
+ status: {
124
+ type: String,
125
+ required: true,
126
+ enum: ALL_JOURNAL_ENTRY_STATUSES,
127
+ default: JOURNAL_ENTRY_STATUS_ENUM.POSTED,
128
+ },
129
+ voided_at: {
130
+ type: Schema.Types.Date,
131
+ default: null,
132
+ },
133
+ voided_by: {
134
+ type: String,
135
+ },
136
+ void_reason: {
137
+ type: String,
138
+ },
139
+ created_by: {
140
+ type: String,
141
+ },
142
+ }, {
143
+ timestamps: true,
144
+ versionKey: false,
145
+ minimize: false,
146
+ });
147
+ export const TENANT_JOURNAL_ENTRIES_COLLECTION_NAME = 'tenant_journal_entries';
@@ -65,6 +65,7 @@ export * from './tenant_cash_drawer_sessions';
65
65
  export * from './tenant_cash_drawers';
66
66
  export * from './tenant_cash_in_hand_transactions';
67
67
  export * from './tenant_cash_in_out';
68
+ export * from './tenant_chart_of_accounts';
68
69
  export * from './tenant_clients';
69
70
  export * from './tenant_day_sessions';
70
71
  export * from './tenant_hr_attendance_records';
@@ -82,6 +83,7 @@ export * from './tenant_hr_shifts';
82
83
  export * from './tenant_instore_shift_sessions';
83
84
  export * from './tenant_instore_shifts';
84
85
  export * from './tenant_inventory_entries';
86
+ export * from './tenant_journal_entries';
85
87
  export * from './tenant_metadata';
86
88
  export * from './tenant_order_item_instructions';
87
89
  export * from './tenant_purchase_order_vendor_returns';
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -1,3 +1,4 @@
1
+ export * from './enums/accounting';
1
2
  export * from './enums/cash';
2
3
  export * from './enums/communications';
3
4
  export * from './enums/delivery';
@@ -16,6 +16,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
17
17
  };
18
18
  Object.defineProperty(exports, "__esModule", { value: true });
19
+ __exportStar(require("./enums/accounting"), exports);
19
20
  __exportStar(require("./enums/cash"), exports);
20
21
  __exportStar(require("./enums/communications"), exports);
21
22
  __exportStar(require("./enums/delivery"), exports);
@@ -0,0 +1,62 @@
1
+ export declare enum ACCOUNT_TYPE_ENUM {
2
+ ASSET = "ASSET",
3
+ LIABILITY = "LIABILITY",
4
+ EQUITY = "EQUITY",
5
+ INCOME = "INCOME",
6
+ COGS = "COGS",
7
+ EXPENSE = "EXPENSE"
8
+ }
9
+ export declare const ALL_ACCOUNT_TYPES: ACCOUNT_TYPE_ENUM[];
10
+ export declare enum ACCOUNT_NORMAL_BALANCE_ENUM {
11
+ DEBIT = "DEBIT",
12
+ CREDIT = "CREDIT"
13
+ }
14
+ export declare const ALL_ACCOUNT_NORMAL_BALANCES: ACCOUNT_NORMAL_BALANCE_ENUM[];
15
+ /** The side that increases each account type — derived on create, never client input. */
16
+ export declare const ACCOUNT_TYPE_NORMAL_BALANCE: Record<ACCOUNT_TYPE_ENUM, ACCOUNT_NORMAL_BALANCE_ENUM>;
17
+ export declare enum JOURNAL_ENTRY_SOURCE_ENUM {
18
+ /** Hand-written entry. */
19
+ MANUAL = "MANUAL",
20
+ /** Money in/out recorded from the Banking page. */
21
+ BANKING = "BANKING",
22
+ /** Auto-posted from a completed order. */
23
+ SALE = "SALE",
24
+ /** One consolidated entry per shift close (all orders of the shift merged). */
25
+ SHIFT_CLOSE = "SHIFT_CLOSE",
26
+ PURCHASE = "PURCHASE",
27
+ EXPENSE = "EXPENSE",
28
+ PAYROLL = "PAYROLL",
29
+ /** Auto-posted from an inventory utility transaction. */
30
+ INVENTORY = "INVENTORY",
31
+ /** Opening balances — balanced against the opening-balance equity account. */
32
+ OPENING = "OPENING",
33
+ /** Manual adjustment / correction. */
34
+ ADJUSTMENT = "ADJUSTMENT",
35
+ /** Reversal of a previously posted entry. */
36
+ REVERSAL = "REVERSAL"
37
+ }
38
+ export declare const ALL_JOURNAL_ENTRY_SOURCES: JOURNAL_ENTRY_SOURCE_ENUM[];
39
+ export declare enum JOURNAL_ENTRY_STATUS_ENUM {
40
+ POSTED = "POSTED",
41
+ VOIDED = "VOIDED"
42
+ }
43
+ export declare const ALL_JOURNAL_ENTRY_STATUSES: JOURNAL_ENTRY_STATUS_ENUM[];
44
+ /** Form-level concept: which side a typed amount falls on. */
45
+ export declare enum JOURNAL_ENTRY_LINE_SIDE_ENUM {
46
+ DEBIT = "DEBIT",
47
+ CREDIT = "CREDIT"
48
+ }
49
+ export declare const ALL_JOURNAL_ENTRY_LINE_SIDES: JOURNAL_ENTRY_LINE_SIDE_ENUM[];
50
+ /** Which basis a single report is generated on. */
51
+ export declare enum REPORTING_BASIS_ENUM {
52
+ ACCRUAL = "ACCRUAL",
53
+ CASH = "CASH"
54
+ }
55
+ export declare const ALL_REPORTING_BASES: REPORTING_BASIS_ENUM[];
56
+ /** Tenant-level preference: one basis or both side by side. */
57
+ export declare enum REPORTING_METHOD_ENUM {
58
+ ACCRUAL_AND_CASH = "ACCRUAL_AND_CASH",
59
+ ACCRUAL_ONLY = "ACCRUAL_ONLY",
60
+ CASH_ONLY = "CASH_ONLY"
61
+ }
62
+ export declare const ALL_REPORTING_METHODS: REPORTING_METHOD_ENUM[];
@@ -0,0 +1,83 @@
1
+ "use strict";
2
+ // Auto-split from constants/ENUMS.ts — see ENUMS.ts barrel for exports.
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.ALL_REPORTING_METHODS = exports.REPORTING_METHOD_ENUM = exports.ALL_REPORTING_BASES = exports.REPORTING_BASIS_ENUM = exports.ALL_JOURNAL_ENTRY_LINE_SIDES = exports.JOURNAL_ENTRY_LINE_SIDE_ENUM = exports.ALL_JOURNAL_ENTRY_STATUSES = exports.JOURNAL_ENTRY_STATUS_ENUM = exports.ALL_JOURNAL_ENTRY_SOURCES = exports.JOURNAL_ENTRY_SOURCE_ENUM = exports.ACCOUNT_TYPE_NORMAL_BALANCE = exports.ALL_ACCOUNT_NORMAL_BALANCES = exports.ACCOUNT_NORMAL_BALANCE_ENUM = exports.ALL_ACCOUNT_TYPES = exports.ACCOUNT_TYPE_ENUM = void 0;
5
+ /* ============================== Chart of accounts ============================== */
6
+ var ACCOUNT_TYPE_ENUM;
7
+ (function (ACCOUNT_TYPE_ENUM) {
8
+ ACCOUNT_TYPE_ENUM["ASSET"] = "ASSET";
9
+ ACCOUNT_TYPE_ENUM["LIABILITY"] = "LIABILITY";
10
+ ACCOUNT_TYPE_ENUM["EQUITY"] = "EQUITY";
11
+ ACCOUNT_TYPE_ENUM["INCOME"] = "INCOME";
12
+ ACCOUNT_TYPE_ENUM["COGS"] = "COGS";
13
+ ACCOUNT_TYPE_ENUM["EXPENSE"] = "EXPENSE";
14
+ })(ACCOUNT_TYPE_ENUM || (exports.ACCOUNT_TYPE_ENUM = ACCOUNT_TYPE_ENUM = {}));
15
+ exports.ALL_ACCOUNT_TYPES = Object.values(ACCOUNT_TYPE_ENUM);
16
+ var ACCOUNT_NORMAL_BALANCE_ENUM;
17
+ (function (ACCOUNT_NORMAL_BALANCE_ENUM) {
18
+ ACCOUNT_NORMAL_BALANCE_ENUM["DEBIT"] = "DEBIT";
19
+ ACCOUNT_NORMAL_BALANCE_ENUM["CREDIT"] = "CREDIT";
20
+ })(ACCOUNT_NORMAL_BALANCE_ENUM || (exports.ACCOUNT_NORMAL_BALANCE_ENUM = ACCOUNT_NORMAL_BALANCE_ENUM = {}));
21
+ exports.ALL_ACCOUNT_NORMAL_BALANCES = Object.values(ACCOUNT_NORMAL_BALANCE_ENUM);
22
+ /** The side that increases each account type — derived on create, never client input. */
23
+ exports.ACCOUNT_TYPE_NORMAL_BALANCE = {
24
+ [ACCOUNT_TYPE_ENUM.ASSET]: ACCOUNT_NORMAL_BALANCE_ENUM.DEBIT,
25
+ [ACCOUNT_TYPE_ENUM.LIABILITY]: ACCOUNT_NORMAL_BALANCE_ENUM.CREDIT,
26
+ [ACCOUNT_TYPE_ENUM.EQUITY]: ACCOUNT_NORMAL_BALANCE_ENUM.CREDIT,
27
+ [ACCOUNT_TYPE_ENUM.INCOME]: ACCOUNT_NORMAL_BALANCE_ENUM.CREDIT,
28
+ [ACCOUNT_TYPE_ENUM.COGS]: ACCOUNT_NORMAL_BALANCE_ENUM.DEBIT,
29
+ [ACCOUNT_TYPE_ENUM.EXPENSE]: ACCOUNT_NORMAL_BALANCE_ENUM.DEBIT,
30
+ };
31
+ /* ================><================= */
32
+ var JOURNAL_ENTRY_SOURCE_ENUM;
33
+ (function (JOURNAL_ENTRY_SOURCE_ENUM) {
34
+ /** Hand-written entry. */
35
+ JOURNAL_ENTRY_SOURCE_ENUM["MANUAL"] = "MANUAL";
36
+ /** Money in/out recorded from the Banking page. */
37
+ JOURNAL_ENTRY_SOURCE_ENUM["BANKING"] = "BANKING";
38
+ /** Auto-posted from a completed order. */
39
+ JOURNAL_ENTRY_SOURCE_ENUM["SALE"] = "SALE";
40
+ /** One consolidated entry per shift close (all orders of the shift merged). */
41
+ JOURNAL_ENTRY_SOURCE_ENUM["SHIFT_CLOSE"] = "SHIFT_CLOSE";
42
+ JOURNAL_ENTRY_SOURCE_ENUM["PURCHASE"] = "PURCHASE";
43
+ JOURNAL_ENTRY_SOURCE_ENUM["EXPENSE"] = "EXPENSE";
44
+ JOURNAL_ENTRY_SOURCE_ENUM["PAYROLL"] = "PAYROLL";
45
+ /** Auto-posted from an inventory utility transaction. */
46
+ JOURNAL_ENTRY_SOURCE_ENUM["INVENTORY"] = "INVENTORY";
47
+ /** Opening balances — balanced against the opening-balance equity account. */
48
+ JOURNAL_ENTRY_SOURCE_ENUM["OPENING"] = "OPENING";
49
+ /** Manual adjustment / correction. */
50
+ JOURNAL_ENTRY_SOURCE_ENUM["ADJUSTMENT"] = "ADJUSTMENT";
51
+ /** Reversal of a previously posted entry. */
52
+ JOURNAL_ENTRY_SOURCE_ENUM["REVERSAL"] = "REVERSAL";
53
+ })(JOURNAL_ENTRY_SOURCE_ENUM || (exports.JOURNAL_ENTRY_SOURCE_ENUM = JOURNAL_ENTRY_SOURCE_ENUM = {}));
54
+ exports.ALL_JOURNAL_ENTRY_SOURCES = Object.values(JOURNAL_ENTRY_SOURCE_ENUM);
55
+ var JOURNAL_ENTRY_STATUS_ENUM;
56
+ (function (JOURNAL_ENTRY_STATUS_ENUM) {
57
+ JOURNAL_ENTRY_STATUS_ENUM["POSTED"] = "POSTED";
58
+ JOURNAL_ENTRY_STATUS_ENUM["VOIDED"] = "VOIDED";
59
+ })(JOURNAL_ENTRY_STATUS_ENUM || (exports.JOURNAL_ENTRY_STATUS_ENUM = JOURNAL_ENTRY_STATUS_ENUM = {}));
60
+ exports.ALL_JOURNAL_ENTRY_STATUSES = Object.values(JOURNAL_ENTRY_STATUS_ENUM);
61
+ /** Form-level concept: which side a typed amount falls on. */
62
+ var JOURNAL_ENTRY_LINE_SIDE_ENUM;
63
+ (function (JOURNAL_ENTRY_LINE_SIDE_ENUM) {
64
+ JOURNAL_ENTRY_LINE_SIDE_ENUM["DEBIT"] = "DEBIT";
65
+ JOURNAL_ENTRY_LINE_SIDE_ENUM["CREDIT"] = "CREDIT";
66
+ })(JOURNAL_ENTRY_LINE_SIDE_ENUM || (exports.JOURNAL_ENTRY_LINE_SIDE_ENUM = JOURNAL_ENTRY_LINE_SIDE_ENUM = {}));
67
+ exports.ALL_JOURNAL_ENTRY_LINE_SIDES = Object.values(JOURNAL_ENTRY_LINE_SIDE_ENUM);
68
+ /* ================><================= */
69
+ /** Which basis a single report is generated on. */
70
+ var REPORTING_BASIS_ENUM;
71
+ (function (REPORTING_BASIS_ENUM) {
72
+ REPORTING_BASIS_ENUM["ACCRUAL"] = "ACCRUAL";
73
+ REPORTING_BASIS_ENUM["CASH"] = "CASH";
74
+ })(REPORTING_BASIS_ENUM || (exports.REPORTING_BASIS_ENUM = REPORTING_BASIS_ENUM = {}));
75
+ exports.ALL_REPORTING_BASES = Object.values(REPORTING_BASIS_ENUM);
76
+ /** Tenant-level preference: one basis or both side by side. */
77
+ var REPORTING_METHOD_ENUM;
78
+ (function (REPORTING_METHOD_ENUM) {
79
+ REPORTING_METHOD_ENUM["ACCRUAL_AND_CASH"] = "ACCRUAL_AND_CASH";
80
+ REPORTING_METHOD_ENUM["ACCRUAL_ONLY"] = "ACCRUAL_ONLY";
81
+ REPORTING_METHOD_ENUM["CASH_ONLY"] = "CASH_ONLY";
82
+ })(REPORTING_METHOD_ENUM || (exports.REPORTING_METHOD_ENUM = REPORTING_METHOD_ENUM = {}));
83
+ exports.ALL_REPORTING_METHODS = Object.values(REPORTING_METHOD_ENUM);
@@ -86,6 +86,13 @@ export interface IBuildTenantDiscountCloseDayPlanInput {
86
86
  * degrades. That is why (2) is part of the contract rather than a hint.
87
87
  */
88
88
  existingEntries?: IMaterializedTenantDiscountEntries[];
89
+ /**
90
+ * Set only when `existingEntries` contains every stored DAY-scoped entry
91
+ * document in the touched periods. A period entry document that is missing
92
+ * while its counter document exists can only be repaired from that complete
93
+ * set; without this acknowledgement the plan refuses to guess.
94
+ */
95
+ existingEntriesComplete?: boolean;
89
96
  /** Why this close is applied — stored as every history row's `reason`. */
90
97
  reason: string;
91
98
  /** Force structural writes even when the watermarks are up to date. */
@@ -543,6 +543,12 @@ function buildTenantDiscountCloseDayPlan(input) {
543
543
  return null;
544
544
  continue;
545
545
  }
546
+ // A missing period entry document is a partial write only when the period
547
+ // COUNTER document exists. Refuse to rebuild from an unverified fetch.
548
+ const periodCounterExists = existingByScope.has(scopeKey(compiled));
549
+ if (periodCounterExists && input.existingEntriesComplete !== true) {
550
+ return null;
551
+ }
546
552
  // Never persisted: rebuild from the sibling days, minus this day's stored rows (a
547
553
  // correction may have REMOVED rows), then merge this day's fresh rows on top.
548
554
  const folded = (0, exports.subtractMaterializedDiscountEntrySections)(refoldPeriodEntrySections(compiled.scope_context, String(compiled.scope_field)), storedDayEntrySections);
@@ -663,12 +663,10 @@ const decodeMaterializedReportScopeGroups = (selection) => {
663
663
  window: {
664
664
  fromHour: window.fromHour,
665
665
  toHour: window.toHour,
666
- // A weekday-refined day set is GAPPED — a continuous SPAN can
667
- // never compile it. Weekday + window therefore always compiles
668
- // RECURRING (the same hours on each matching weekday).
669
- kind: SPAN_VIEWS.has(view) && weekdayFilter == null
670
- ? MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_WINDOW_KIND_ENUM.SPAN
671
- : MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_WINDOW_KIND_ENUM.RECURRING,
666
+ // A time-of-day window means the SAME hours on every selected
667
+ // day. SPAN would read period documents for middle days, which
668
+ // contain all 24 hours and cannot honour the window.
669
+ kind: MATERIALIZED_REPORTS_1.MATERIALIZED_REPORT_WINDOW_KIND_ENUM.RECURRING,
672
670
  },
673
671
  }
674
672
  : {}),
@@ -0,0 +1,25 @@
1
+ import { IOrderPayment } from '../../schemas/types/orders';
2
+ /**
3
+ * Server-owned `collected_at` stamping for order payments.
4
+ *
5
+ * A payment line is the unit a credit bill lives on: the sale is issued on the
6
+ * punch day, and money may arrive later (full or partial). `order_settled_at`
7
+ * only moves on FULL settlement, so it cannot anchor a partial collection.
8
+ * This helper records the LAST collection moment on the payment itself,
9
+ * derived by comparing `amount_paid` with the stored previous version.
10
+ *
11
+ * Rules:
12
+ * - never trust the incoming `collected_at` (clients may send anything);
13
+ * - a NEW payment line with money in (`amount_paid > 0`) gets `now`;
14
+ * - an EXISTING line whose `amount_paid` increased gets `now`;
15
+ * - unchanged / decreased / zero-amount lines keep the stored timestamp
16
+ * (`null` when unknown — legacy rows are not backfilled here).
17
+ *
18
+ * Pure and never throws. `previousPayments` must be the SERVER copy of the
19
+ * order's payments before the write (pass none on create).
20
+ */
21
+ export interface IStampPaymentCollectionTimestampsOptions {
22
+ /** Override for the collection instant (tests / offline replays). */
23
+ now?: Date | string | null;
24
+ }
25
+ export declare function stampPaymentCollectionTimestamps(payments: IOrderPayment[] | null | undefined, previousPayments?: IOrderPayment[] | null | undefined, options?: IStampPaymentCollectionTimestampsOptions): IOrderPayment[];