shutro 0.0.0-stage → 0.2.0
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.
- package/LICENSE +21 -0
- package/README.md +99 -2
- package/dist/audit/index.d.ts +59 -0
- package/dist/audit/index.js +96 -0
- package/dist/cli/bin.d.ts +2 -0
- package/dist/cli/bin.js +11 -0
- package/dist/cli/cli.d.ts +10 -0
- package/dist/cli/cli.js +164 -0
- package/dist/cli/config.d.ts +18 -0
- package/dist/cli/config.js +49 -0
- package/dist/cli/errors.d.ts +16 -0
- package/dist/cli/errors.js +23 -0
- package/dist/cli/generate/generate.d.ts +33 -0
- package/dist/cli/generate/generate.js +84 -0
- package/dist/cli/generate/render.d.ts +23 -0
- package/dist/cli/generate/render.js +249 -0
- package/dist/cli/index.d.ts +12 -0
- package/dist/cli/index.js +12 -0
- package/dist/cli/init.d.ts +2 -0
- package/dist/cli/init.js +65 -0
- package/dist/cli/inspect.d.ts +10 -0
- package/dist/cli/inspect.js +82 -0
- package/dist/cli/packages.d.ts +6 -0
- package/dist/cli/packages.js +6 -0
- package/dist/cli/project.d.ts +18 -0
- package/dist/cli/project.js +39 -0
- package/dist/cli/resolve.d.ts +2 -0
- package/dist/cli/resolve.js +28 -0
- package/dist/core/compute/graph.d.ts +52 -0
- package/dist/core/compute/graph.js +162 -0
- package/dist/core/data/data.d.ts +59 -0
- package/dist/core/data/data.js +119 -0
- package/dist/core/data/load.d.ts +37 -0
- package/dist/core/data/load.js +236 -0
- package/dist/core/env/env.d.ts +468 -0
- package/dist/core/env/env.js +916 -0
- package/dist/core/env/identity.d.ts +43 -0
- package/dist/core/env/identity.js +65 -0
- package/dist/core/env/inspector.d.ts +55 -0
- package/dist/core/env/inspector.js +85 -0
- package/dist/core/env/time.d.ts +12 -0
- package/dist/core/env/time.js +38 -0
- package/dist/core/errors.d.ts +256 -0
- package/dist/core/errors.js +360 -0
- package/dist/core/events/events.d.ts +44 -0
- package/dist/core/events/events.js +58 -0
- package/dist/core/events/workers.d.ts +56 -0
- package/dist/core/events/workers.js +155 -0
- package/dist/core/index.d.ts +51 -0
- package/dist/core/index.js +42 -0
- package/dist/core/model/company.d.ts +6 -0
- package/dist/core/model/company.js +31 -0
- package/dist/core/model/faces.d.ts +42 -0
- package/dist/core/model/faces.js +1 -0
- package/dist/core/model/field.d.ts +479 -0
- package/dist/core/model/field.js +461 -0
- package/dist/core/model/generated.d.ts +77 -0
- package/dist/core/model/generated.js +1 -0
- package/dist/core/model/id.d.ts +33 -0
- package/dist/core/model/id.js +4 -0
- package/dist/core/model/model.d.ts +387 -0
- package/dist/core/model/model.js +566 -0
- package/dist/core/model/partition.d.ts +31 -0
- package/dist/core/model/partition.js +71 -0
- package/dist/core/model/related.d.ts +17 -0
- package/dist/core/model/related.js +60 -0
- package/dist/core/model/typed.d.ts +107 -0
- package/dist/core/model/typed.js +46 -0
- package/dist/core/money/currency.d.ts +39 -0
- package/dist/core/money/currency.js +37 -0
- package/dist/core/money/decimal.d.ts +26 -0
- package/dist/core/money/decimal.js +51 -0
- package/dist/core/money/fx.d.ts +68 -0
- package/dist/core/money/fx.js +124 -0
- package/dist/core/money/money.d.ts +84 -0
- package/dist/core/money/money.js +173 -0
- package/dist/core/money/quantity.d.ts +67 -0
- package/dist/core/money/quantity.js +108 -0
- package/dist/core/money/rounding.d.ts +11 -0
- package/dist/core/money/rounding.js +12 -0
- package/dist/core/recordset/aggregate.d.ts +22 -0
- package/dist/core/recordset/aggregate.js +258 -0
- package/dist/core/recordset/audit.d.ts +33 -0
- package/dist/core/recordset/audit.js +262 -0
- package/dist/core/recordset/compute.d.ts +36 -0
- package/dist/core/recordset/compute.js +414 -0
- package/dist/core/recordset/constraints.d.ts +9 -0
- package/dist/core/recordset/constraints.js +61 -0
- package/dist/core/recordset/domain.d.ts +31 -0
- package/dist/core/recordset/domain.js +300 -0
- package/dist/core/recordset/onchange.d.ts +43 -0
- package/dist/core/recordset/onchange.js +240 -0
- package/dist/core/recordset/recordset.d.ts +110 -0
- package/dist/core/recordset/recordset.js +789 -0
- package/dist/core/recordset/relations.d.ts +15 -0
- package/dist/core/recordset/relations.js +13 -0
- package/dist/core/recordset/rls.d.ts +15 -0
- package/dist/core/recordset/rls.js +108 -0
- package/dist/core/recordset/security.d.ts +56 -0
- package/dist/core/recordset/security.js +269 -0
- package/dist/core/recordset/sequences.d.ts +31 -0
- package/dist/core/recordset/sequences.js +154 -0
- package/dist/core/recordset/source.d.ts +9 -0
- package/dist/core/recordset/source.js +19 -0
- package/dist/core/recordset/sqlErrors.d.ts +6 -0
- package/dist/core/recordset/sqlErrors.js +39 -0
- package/dist/core/recordset/transitions.d.ts +10 -0
- package/dist/core/recordset/transitions.js +80 -0
- package/dist/core/recordset/types.d.ts +415 -0
- package/dist/core/recordset/types.js +8 -0
- package/dist/core/recordset/values.d.ts +33 -0
- package/dist/core/recordset/values.js +177 -0
- package/dist/core/recordset/writes.d.ts +42 -0
- package/dist/core/recordset/writes.js +786 -0
- package/dist/core/registry/ddl.d.ts +31 -0
- package/dist/core/registry/ddl.js +174 -0
- package/dist/core/registry/extension.d.ts +94 -0
- package/dist/core/registry/extension.js +90 -0
- package/dist/core/registry/module.d.ts +89 -0
- package/dist/core/registry/module.js +98 -0
- package/dist/core/registry/registry.d.ts +130 -0
- package/dist/core/registry/registry.js +591 -0
- package/dist/core/security/runtime.d.ts +102 -0
- package/dist/core/security/runtime.js +232 -0
- package/dist/driver-pg/driver.d.ts +88 -0
- package/dist/driver-pg/driver.js +177 -0
- package/dist/driver-pg/errors.d.ts +72 -0
- package/dist/driver-pg/errors.js +182 -0
- package/dist/driver-pg/index.d.ts +7 -0
- package/dist/driver-pg/index.js +7 -0
- package/dist/i18n/index.d.ts +25 -0
- package/dist/i18n/index.js +51 -0
- package/dist/migrate/apply.d.ts +78 -0
- package/dist/migrate/apply.js +167 -0
- package/dist/migrate/catalog.d.ts +10 -0
- package/dist/migrate/catalog.js +122 -0
- package/dist/migrate/diff.d.ts +98 -0
- package/dist/migrate/diff.js +160 -0
- package/dist/migrate/errors.d.ts +21 -0
- package/dist/migrate/errors.js +31 -0
- package/dist/migrate/index.d.ts +18 -0
- package/dist/migrate/index.js +18 -0
- package/dist/migrate/render.d.ts +5 -0
- package/dist/migrate/render.js +101 -0
- package/dist/migrate/schema.d.ts +61 -0
- package/dist/migrate/schema.js +233 -0
- package/dist/security/index.d.ts +147 -0
- package/dist/security/index.js +278 -0
- package/dist/sequence/index.d.ts +63 -0
- package/dist/sequence/index.js +106 -0
- package/dist/sql/ast.d.ts +49 -0
- package/dist/sql/ast.js +47 -0
- package/dist/sql/builder/builder.d.ts +215 -0
- package/dist/sql/builder/builder.js +308 -0
- package/dist/sql/builder/expression.d.ts +80 -0
- package/dist/sql/builder/expression.js +126 -0
- package/dist/sql/compile.d.ts +18 -0
- package/dist/sql/compile.js +41 -0
- package/dist/sql/errors.d.ts +13 -0
- package/dist/sql/errors.js +16 -0
- package/dist/sql/index.d.ts +19 -0
- package/dist/sql/index.js +19 -0
- package/dist/sql/tag.d.ts +47 -0
- package/dist/sql/tag.js +123 -0
- package/package.json +82 -4
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { type Currency } from './currency.js';
|
|
2
|
+
import { Decimal, type DecimalInput } from './decimal.js';
|
|
3
|
+
import { type RoundingMethod } from './rounding.js';
|
|
4
|
+
/** JSON form of {@link Money}: the amount as an exact decimal string. */
|
|
5
|
+
export interface MoneyJson {
|
|
6
|
+
readonly amount: string;
|
|
7
|
+
readonly currency: string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* An immutable amount in one currency (RFC 0006 §7.3).
|
|
11
|
+
*
|
|
12
|
+
* - Arithmetic is exact and **unrounded**; call {@link Money.round} at the points where rounding is defined.
|
|
13
|
+
* - Combining or comparing amounts in different currencies throws {@link CurrencyMismatchError}. Conversion is a
|
|
14
|
+
* separate, dated operation.
|
|
15
|
+
* - `isZero`, `equals` and `compare` work at the currency's rounding precision (like Odoo's `float_is_zero`).
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* const price = Money.of('85000.00', BDT);
|
|
20
|
+
* price.times('0.15').round(); // BDT 12750.00
|
|
21
|
+
* Money.of('1000', BDT).allocate([1, 1, 1]); // 333.34, 333.33, 333.33
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export declare class Money {
|
|
25
|
+
/** The exact amount. */
|
|
26
|
+
readonly amount: Decimal;
|
|
27
|
+
/** The currency. */
|
|
28
|
+
readonly currency: Currency;
|
|
29
|
+
private constructor();
|
|
30
|
+
/** An amount in `currency`. */
|
|
31
|
+
static of(amount: DecimalInput, currency: Currency): Money;
|
|
32
|
+
/** Zero in `currency`. */
|
|
33
|
+
static zero(currency: Currency): Money;
|
|
34
|
+
/**
|
|
35
|
+
* Sum of amounts in one currency. An empty list needs `currency` to know which zero to return.
|
|
36
|
+
* @throws {CurrencyMismatchError} if the amounts (or `currency`) differ.
|
|
37
|
+
* @throws {EmptyMoneySumError} for an empty list without `currency`.
|
|
38
|
+
*/
|
|
39
|
+
static sum(items: readonly Money[], currency?: Currency): Money;
|
|
40
|
+
/** `this + other`. @throws {CurrencyMismatchError} */
|
|
41
|
+
plus(other: Money): Money;
|
|
42
|
+
/** `this - other`. @throws {CurrencyMismatchError} */
|
|
43
|
+
minus(other: Money): Money;
|
|
44
|
+
/** `this × factor`, unrounded. */
|
|
45
|
+
times(factor: DecimalInput): Money;
|
|
46
|
+
/** `this ÷ divisor`, unrounded (64 significant digits). @throws {DivisionByZeroError} */
|
|
47
|
+
dividedBy(divisor: DecimalInput): Money;
|
|
48
|
+
/** `-this`. */
|
|
49
|
+
negated(): Money;
|
|
50
|
+
/** `|this|`. */
|
|
51
|
+
abs(): Money;
|
|
52
|
+
/** Rounded to the currency's rounding unit, with the currency's method unless `method` is given. */
|
|
53
|
+
round(method?: RoundingMethod): Money;
|
|
54
|
+
/** True when the amount rounds to zero in its currency. */
|
|
55
|
+
isZero(): boolean;
|
|
56
|
+
/** True when the amount rounds to more than zero. */
|
|
57
|
+
isPositive(): boolean;
|
|
58
|
+
/** True when the amount rounds to less than zero. */
|
|
59
|
+
isNegative(): boolean;
|
|
60
|
+
/** Equal at currency precision. @throws {CurrencyMismatchError} */
|
|
61
|
+
equals(other: Money): boolean;
|
|
62
|
+
/** Same currency and exactly the same amount (no rounding). Never throws. */
|
|
63
|
+
equalsExactly(other: Money): boolean;
|
|
64
|
+
/** -1, 0 or 1 at currency precision. @throws {CurrencyMismatchError} */
|
|
65
|
+
compare(other: Money): -1 | 0 | 1;
|
|
66
|
+
/**
|
|
67
|
+
* Splits the rounded amount by `ratios` with the largest-remainder method (RFC 0006 §7.6): every share is a
|
|
68
|
+
* multiple of the rounding unit, the shares add up **exactly** to `this.round()`, each is within one unit of its
|
|
69
|
+
* exact proportional value, and ties go to the earliest share.
|
|
70
|
+
*
|
|
71
|
+
* @throws {InvalidAllocationError} for an empty list, a negative ratio, or ratios that sum to zero.
|
|
72
|
+
*/
|
|
73
|
+
allocate(ratios: readonly DecimalInput[]): readonly Money[];
|
|
74
|
+
/** The exact amount as a Decimal. */
|
|
75
|
+
toDecimal(): Decimal;
|
|
76
|
+
/**
|
|
77
|
+
* `{ amount: '85000.00', currency: 'BDT' }`. The amount is exact: padded to the currency's decimal places, never
|
|
78
|
+
* truncated (an unrounded `1.2345` stays `'1.2345'`).
|
|
79
|
+
*/
|
|
80
|
+
toJSON(): MoneyJson;
|
|
81
|
+
/** `'BDT 85000.00'`. Locale-aware formatting is the job of `format(lang)` (later). */
|
|
82
|
+
toString(): string;
|
|
83
|
+
private assertSameCurrency;
|
|
84
|
+
}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { CurrencyMismatchError, DivisionByZeroError, EmptyMoneySumError, InvalidAllocationError, } from '../errors.js';
|
|
2
|
+
import { sameCurrency } from './currency.js';
|
|
3
|
+
import { Decimal, normalizeZero, toDecimal } from './decimal.js';
|
|
4
|
+
import { roundToUnit } from './rounding.js';
|
|
5
|
+
/**
|
|
6
|
+
* An immutable amount in one currency (RFC 0006 §7.3).
|
|
7
|
+
*
|
|
8
|
+
* - Arithmetic is exact and **unrounded**; call {@link Money.round} at the points where rounding is defined.
|
|
9
|
+
* - Combining or comparing amounts in different currencies throws {@link CurrencyMismatchError}. Conversion is a
|
|
10
|
+
* separate, dated operation.
|
|
11
|
+
* - `isZero`, `equals` and `compare` work at the currency's rounding precision (like Odoo's `float_is_zero`).
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* const price = Money.of('85000.00', BDT);
|
|
16
|
+
* price.times('0.15').round(); // BDT 12750.00
|
|
17
|
+
* Money.of('1000', BDT).allocate([1, 1, 1]); // 333.34, 333.33, 333.33
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export class Money {
|
|
21
|
+
/** The exact amount. */
|
|
22
|
+
amount;
|
|
23
|
+
/** The currency. */
|
|
24
|
+
currency;
|
|
25
|
+
constructor(amount, currency) {
|
|
26
|
+
this.amount = amount;
|
|
27
|
+
this.currency = currency;
|
|
28
|
+
Object.freeze(this);
|
|
29
|
+
}
|
|
30
|
+
/** An amount in `currency`. */
|
|
31
|
+
static of(amount, currency) {
|
|
32
|
+
return new Money(toDecimal(amount), currency);
|
|
33
|
+
}
|
|
34
|
+
/** Zero in `currency`. */
|
|
35
|
+
static zero(currency) {
|
|
36
|
+
return new Money(new Decimal(0), currency);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Sum of amounts in one currency. An empty list needs `currency` to know which zero to return.
|
|
40
|
+
* @throws {CurrencyMismatchError} if the amounts (or `currency`) differ.
|
|
41
|
+
* @throws {EmptyMoneySumError} for an empty list without `currency`.
|
|
42
|
+
*/
|
|
43
|
+
static sum(items, currency) {
|
|
44
|
+
const first = items[0];
|
|
45
|
+
const target = currency ?? first?.currency;
|
|
46
|
+
if (target === undefined) {
|
|
47
|
+
throw new EmptyMoneySumError('Money.sum() was given an empty list and no currency.', 'Pass the currency for the empty case: Money.sum(amounts, currency).');
|
|
48
|
+
}
|
|
49
|
+
let total = Money.zero(target);
|
|
50
|
+
for (const item of items)
|
|
51
|
+
total = total.plus(item);
|
|
52
|
+
return total;
|
|
53
|
+
}
|
|
54
|
+
/** `this + other`. @throws {CurrencyMismatchError} */
|
|
55
|
+
plus(other) {
|
|
56
|
+
this.assertSameCurrency(other, 'add');
|
|
57
|
+
return new Money(this.amount.plus(other.amount), this.currency);
|
|
58
|
+
}
|
|
59
|
+
/** `this - other`. @throws {CurrencyMismatchError} */
|
|
60
|
+
minus(other) {
|
|
61
|
+
this.assertSameCurrency(other, 'subtract');
|
|
62
|
+
return new Money(this.amount.minus(other.amount), this.currency);
|
|
63
|
+
}
|
|
64
|
+
/** `this × factor`, unrounded. */
|
|
65
|
+
times(factor) {
|
|
66
|
+
return new Money(this.amount.times(toDecimal(factor)), this.currency);
|
|
67
|
+
}
|
|
68
|
+
/** `this ÷ divisor`, unrounded (64 significant digits). @throws {DivisionByZeroError} */
|
|
69
|
+
dividedBy(divisor) {
|
|
70
|
+
const d = toDecimal(divisor);
|
|
71
|
+
if (d.isZero()) {
|
|
72
|
+
throw new DivisionByZeroError(`Cannot divide ${this.toString()} by zero.`, 'Check the divisor (quantity, ratio, number of installments) before dividing; use allocate() to split amounts.');
|
|
73
|
+
}
|
|
74
|
+
return new Money(this.amount.dividedBy(d), this.currency);
|
|
75
|
+
}
|
|
76
|
+
/** `-this`. */
|
|
77
|
+
negated() {
|
|
78
|
+
return new Money(normalizeZero(this.amount.negated()), this.currency);
|
|
79
|
+
}
|
|
80
|
+
/** `|this|`. */
|
|
81
|
+
abs() {
|
|
82
|
+
return new Money(this.amount.abs(), this.currency);
|
|
83
|
+
}
|
|
84
|
+
/** Rounded to the currency's rounding unit, with the currency's method unless `method` is given. */
|
|
85
|
+
round(method = this.currency.roundingMethod) {
|
|
86
|
+
return new Money(roundToUnit(this.amount, this.currency.rounding, method), this.currency);
|
|
87
|
+
}
|
|
88
|
+
/** True when the amount rounds to zero in its currency. */
|
|
89
|
+
isZero() {
|
|
90
|
+
return this.round().amount.isZero();
|
|
91
|
+
}
|
|
92
|
+
/** True when the amount rounds to more than zero. */
|
|
93
|
+
isPositive() {
|
|
94
|
+
return this.round().amount.gt(0);
|
|
95
|
+
}
|
|
96
|
+
/** True when the amount rounds to less than zero. */
|
|
97
|
+
isNegative() {
|
|
98
|
+
return this.round().amount.lt(0);
|
|
99
|
+
}
|
|
100
|
+
/** Equal at currency precision. @throws {CurrencyMismatchError} */
|
|
101
|
+
equals(other) {
|
|
102
|
+
return this.compare(other) === 0;
|
|
103
|
+
}
|
|
104
|
+
/** Same currency and exactly the same amount (no rounding). Never throws. */
|
|
105
|
+
equalsExactly(other) {
|
|
106
|
+
return sameCurrency(this.currency, other.currency) && this.amount.eq(other.amount);
|
|
107
|
+
}
|
|
108
|
+
/** -1, 0 or 1 at currency precision. @throws {CurrencyMismatchError} */
|
|
109
|
+
compare(other) {
|
|
110
|
+
this.assertSameCurrency(other, 'compare');
|
|
111
|
+
const c = this.round().amount.cmp(other.round().amount);
|
|
112
|
+
return c < 0 ? -1 : c > 0 ? 1 : 0;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Splits the rounded amount by `ratios` with the largest-remainder method (RFC 0006 §7.6): every share is a
|
|
116
|
+
* multiple of the rounding unit, the shares add up **exactly** to `this.round()`, each is within one unit of its
|
|
117
|
+
* exact proportional value, and ties go to the earliest share.
|
|
118
|
+
*
|
|
119
|
+
* @throws {InvalidAllocationError} for an empty list, a negative ratio, or ratios that sum to zero.
|
|
120
|
+
*/
|
|
121
|
+
allocate(ratios) {
|
|
122
|
+
const weights = ratios.map(toDecimal);
|
|
123
|
+
const totalWeight = weights.reduce((a, b) => a.plus(b), new Decimal(0));
|
|
124
|
+
if (weights.length === 0 ||
|
|
125
|
+
weights.some((w) => w.isNegative() && !w.isZero()) ||
|
|
126
|
+
totalWeight.isZero()) {
|
|
127
|
+
throw new InvalidAllocationError(`Cannot allocate ${this.toString()} by ratios [${weights.map((w) => w.toFixed()).join(', ')}].`, 'Pass at least one ratio; ratios must be zero or positive and not all zero.');
|
|
128
|
+
}
|
|
129
|
+
const unit = this.currency.rounding;
|
|
130
|
+
const totalUnits = this.round().amount.dividedBy(unit); // an integer
|
|
131
|
+
const units = totalUnits.abs();
|
|
132
|
+
// Exact integer arithmetic: scale ratios to integers, then share_i = floor(units·w_i / W) with an exact
|
|
133
|
+
// remainder. Comparing inexact quotients could floor a near-integer share the wrong way and break Σ = total.
|
|
134
|
+
const scale = new Decimal(10).pow(Math.max(...weights.map((w) => w.decimalPlaces())));
|
|
135
|
+
const ints = weights.map((w) => w.times(scale));
|
|
136
|
+
const sumInts = ints.reduce((a, b) => a.plus(b), new Decimal(0));
|
|
137
|
+
const counts = ints.map((w) => units.times(w).dividedToIntegerBy(sumInts));
|
|
138
|
+
const remainders = ints.map((w) => units.times(w).mod(sumInts));
|
|
139
|
+
let remaining = counts.reduce((a, b) => a.minus(b), units).toNumber();
|
|
140
|
+
const byRemainder = remainders
|
|
141
|
+
.map((r, i) => ({ i, r }))
|
|
142
|
+
.sort((a, b) => b.r.cmp(a.r) || a.i - b.i);
|
|
143
|
+
for (const { i } of byRemainder) {
|
|
144
|
+
if (remaining <= 0)
|
|
145
|
+
break;
|
|
146
|
+
counts[i] = (counts[i] ?? new Decimal(0)).plus(1);
|
|
147
|
+
remaining--;
|
|
148
|
+
}
|
|
149
|
+
const sign = totalUnits.isNegative() ? -1 : 1;
|
|
150
|
+
return Object.freeze(counts.map((count) => new Money(normalizeZero(count.times(unit).times(sign)), this.currency)));
|
|
151
|
+
}
|
|
152
|
+
/** The exact amount as a Decimal. */
|
|
153
|
+
toDecimal() {
|
|
154
|
+
return this.amount;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* `{ amount: '85000.00', currency: 'BDT' }`. The amount is exact: padded to the currency's decimal places, never
|
|
158
|
+
* truncated (an unrounded `1.2345` stays `'1.2345'`).
|
|
159
|
+
*/
|
|
160
|
+
toJSON() {
|
|
161
|
+
const places = Math.max(this.currency.decimalPlaces, this.amount.decimalPlaces());
|
|
162
|
+
return { amount: normalizeZero(this.amount).toFixed(places), currency: this.currency.code };
|
|
163
|
+
}
|
|
164
|
+
/** `'BDT 85000.00'`. Locale-aware formatting is the job of `format(lang)` (later). */
|
|
165
|
+
toString() {
|
|
166
|
+
return `${this.currency.code} ${this.toJSON().amount}`;
|
|
167
|
+
}
|
|
168
|
+
assertSameCurrency(other, operation) {
|
|
169
|
+
if (!sameCurrency(this.currency, other.currency)) {
|
|
170
|
+
throw new CurrencyMismatchError(`Cannot ${operation} ${this.toString()} and ${other.toString()}: they are in different currencies.`, 'Convert one amount first: await env.fx.convert(amount, currency, { date }), and use the date of the document (invoice date / accounting date), not today.');
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { type Decimal, type DecimalInput } from './decimal.js';
|
|
2
|
+
import { type RoundingMethod } from './rounding.js';
|
|
3
|
+
/** A unit of measure (a snapshot of a `uom.uom` record, RFC 0006 §7.4). */
|
|
4
|
+
export interface Uom {
|
|
5
|
+
readonly name: string;
|
|
6
|
+
/** Units convert only within a category ('Unit', 'Weight', 'Length', ...). */
|
|
7
|
+
readonly category: string;
|
|
8
|
+
/** How many of the category's reference unit one of this unit is (dozen: 12, gram: 0.001 when kg is the reference). */
|
|
9
|
+
readonly ratio: Decimal;
|
|
10
|
+
/** Smallest quantity in this unit (units: 1, kg: 0.001). */
|
|
11
|
+
readonly rounding: Decimal;
|
|
12
|
+
/** Database id of the `uom.uom` record, when known. */
|
|
13
|
+
readonly id: number | undefined;
|
|
14
|
+
}
|
|
15
|
+
/** Input of {@link defineUom}. */
|
|
16
|
+
export interface UomInput {
|
|
17
|
+
readonly name: string;
|
|
18
|
+
readonly category: string;
|
|
19
|
+
readonly ratio: DecimalInput;
|
|
20
|
+
readonly rounding: DecimalInput;
|
|
21
|
+
readonly id?: number;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Creates a {@link Uom}.
|
|
25
|
+
* @throws {InvalidUomError} for an empty name or category, or a non-positive ratio or rounding.
|
|
26
|
+
*/
|
|
27
|
+
export declare function defineUom(input: UomInput): Uom;
|
|
28
|
+
/**
|
|
29
|
+
* An immutable quantity in one unit of measure. Mixing units requires an explicit {@link Quantity.to}, and
|
|
30
|
+
* conversion is only possible within a category.
|
|
31
|
+
*/
|
|
32
|
+
export declare class Quantity {
|
|
33
|
+
/** The exact value. */
|
|
34
|
+
readonly value: Decimal;
|
|
35
|
+
/** The unit. */
|
|
36
|
+
readonly uom: Uom;
|
|
37
|
+
private constructor();
|
|
38
|
+
/** A quantity of `value` in `uom`. */
|
|
39
|
+
static of(value: DecimalInput, uom: Uom): Quantity;
|
|
40
|
+
/**
|
|
41
|
+
* The same quantity in `target` (exact, unrounded): `Quantity.of(3, dozen).to(unit)` → 36 units.
|
|
42
|
+
* @throws {IncompatibleUomCategoryError} when the categories differ.
|
|
43
|
+
*/
|
|
44
|
+
to(target: Uom): Quantity;
|
|
45
|
+
/** `this + other`, same unit. @throws {UomMismatchError} */
|
|
46
|
+
plus(other: Quantity): Quantity;
|
|
47
|
+
/** `this - other`, same unit. @throws {UomMismatchError} */
|
|
48
|
+
minus(other: Quantity): Quantity;
|
|
49
|
+
/** `this × factor`, unrounded. */
|
|
50
|
+
times(factor: DecimalInput): Quantity;
|
|
51
|
+
/** Rounded to the unit's rounding (`half-up` unless given; procurement typically uses `up`). */
|
|
52
|
+
round(method?: RoundingMethod): Quantity;
|
|
53
|
+
/** True when the quantity rounds to zero in its unit. */
|
|
54
|
+
isZero(): boolean;
|
|
55
|
+
/** -1, 0 or 1 at the unit's precision. @throws {UomMismatchError} */
|
|
56
|
+
compare(other: Quantity): -1 | 0 | 1;
|
|
57
|
+
/** Equal at the unit's precision. @throws {UomMismatchError} */
|
|
58
|
+
equals(other: Quantity): boolean;
|
|
59
|
+
/** `{ value: '2.5', uom: 'kg' }`. */
|
|
60
|
+
toJSON(): {
|
|
61
|
+
readonly value: string;
|
|
62
|
+
readonly uom: string;
|
|
63
|
+
};
|
|
64
|
+
/** `'2.5 kg'`. */
|
|
65
|
+
toString(): string;
|
|
66
|
+
private assertSameUom;
|
|
67
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { IncompatibleUomCategoryError, InvalidUomError, UomMismatchError } from '../errors.js';
|
|
2
|
+
import { normalizeZero, toDecimal } from './decimal.js';
|
|
3
|
+
import { roundToUnit } from './rounding.js';
|
|
4
|
+
/**
|
|
5
|
+
* Creates a {@link Uom}.
|
|
6
|
+
* @throws {InvalidUomError} for an empty name or category, or a non-positive ratio or rounding.
|
|
7
|
+
*/
|
|
8
|
+
export function defineUom(input) {
|
|
9
|
+
const ratio = toDecimal(input.ratio);
|
|
10
|
+
const rounding = toDecimal(input.rounding);
|
|
11
|
+
if (input.name.trim() === '' || input.category.trim() === '') {
|
|
12
|
+
throw new InvalidUomError('A unit of measure needs a name and a category.', "Pass e.g. { name: 'kg', category: 'Weight', ... }.");
|
|
13
|
+
}
|
|
14
|
+
for (const [label, value] of [
|
|
15
|
+
['ratio', ratio],
|
|
16
|
+
['rounding', rounding],
|
|
17
|
+
]) {
|
|
18
|
+
if (value.isZero() || value.isNegative()) {
|
|
19
|
+
throw new InvalidUomError(`Unit ${input.name} has ${label} ${value.toFixed()}.`, `The ${label} must be positive (ratio: reference units per ${input.name}; rounding: smallest quantity).`);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return Object.freeze({
|
|
23
|
+
name: input.name,
|
|
24
|
+
category: input.category,
|
|
25
|
+
ratio,
|
|
26
|
+
rounding,
|
|
27
|
+
id: input.id,
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
function sameUom(a, b) {
|
|
31
|
+
return a === b || (a.name === b.name && a.category === b.category && a.ratio.eq(b.ratio));
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* An immutable quantity in one unit of measure. Mixing units requires an explicit {@link Quantity.to}, and
|
|
35
|
+
* conversion is only possible within a category.
|
|
36
|
+
*/
|
|
37
|
+
export class Quantity {
|
|
38
|
+
/** The exact value. */
|
|
39
|
+
value;
|
|
40
|
+
/** The unit. */
|
|
41
|
+
uom;
|
|
42
|
+
constructor(value, uom) {
|
|
43
|
+
this.value = value;
|
|
44
|
+
this.uom = uom;
|
|
45
|
+
Object.freeze(this);
|
|
46
|
+
}
|
|
47
|
+
/** A quantity of `value` in `uom`. */
|
|
48
|
+
static of(value, uom) {
|
|
49
|
+
return new Quantity(toDecimal(value), uom);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* The same quantity in `target` (exact, unrounded): `Quantity.of(3, dozen).to(unit)` → 36 units.
|
|
53
|
+
* @throws {IncompatibleUomCategoryError} when the categories differ.
|
|
54
|
+
*/
|
|
55
|
+
to(target) {
|
|
56
|
+
if (this.uom.category !== target.category) {
|
|
57
|
+
throw new IncompatibleUomCategoryError(`Cannot convert ${this.uom.name} (${this.uom.category}) to ${target.name} (${target.category}).`, 'Units convert only within one category; check the product unit and the unit used on the document.');
|
|
58
|
+
}
|
|
59
|
+
if (sameUom(this.uom, target))
|
|
60
|
+
return this;
|
|
61
|
+
return new Quantity(normalizeZero(this.value.times(this.uom.ratio).dividedBy(target.ratio)), target);
|
|
62
|
+
}
|
|
63
|
+
/** `this + other`, same unit. @throws {UomMismatchError} */
|
|
64
|
+
plus(other) {
|
|
65
|
+
this.assertSameUom(other, 'add');
|
|
66
|
+
return new Quantity(this.value.plus(other.value), this.uom);
|
|
67
|
+
}
|
|
68
|
+
/** `this - other`, same unit. @throws {UomMismatchError} */
|
|
69
|
+
minus(other) {
|
|
70
|
+
this.assertSameUom(other, 'subtract');
|
|
71
|
+
return new Quantity(this.value.minus(other.value), this.uom);
|
|
72
|
+
}
|
|
73
|
+
/** `this × factor`, unrounded. */
|
|
74
|
+
times(factor) {
|
|
75
|
+
return new Quantity(this.value.times(toDecimal(factor)), this.uom);
|
|
76
|
+
}
|
|
77
|
+
/** Rounded to the unit's rounding (`half-up` unless given; procurement typically uses `up`). */
|
|
78
|
+
round(method = 'half-up') {
|
|
79
|
+
return new Quantity(roundToUnit(this.value, this.uom.rounding, method), this.uom);
|
|
80
|
+
}
|
|
81
|
+
/** True when the quantity rounds to zero in its unit. */
|
|
82
|
+
isZero() {
|
|
83
|
+
return this.round().value.isZero();
|
|
84
|
+
}
|
|
85
|
+
/** -1, 0 or 1 at the unit's precision. @throws {UomMismatchError} */
|
|
86
|
+
compare(other) {
|
|
87
|
+
this.assertSameUom(other, 'compare');
|
|
88
|
+
const c = this.round().value.cmp(other.round().value);
|
|
89
|
+
return c < 0 ? -1 : c > 0 ? 1 : 0;
|
|
90
|
+
}
|
|
91
|
+
/** Equal at the unit's precision. @throws {UomMismatchError} */
|
|
92
|
+
equals(other) {
|
|
93
|
+
return this.compare(other) === 0;
|
|
94
|
+
}
|
|
95
|
+
/** `{ value: '2.5', uom: 'kg' }`. */
|
|
96
|
+
toJSON() {
|
|
97
|
+
return { value: normalizeZero(this.value).toFixed(), uom: this.uom.name };
|
|
98
|
+
}
|
|
99
|
+
/** `'2.5 kg'`. */
|
|
100
|
+
toString() {
|
|
101
|
+
return `${normalizeZero(this.value).toFixed()} ${this.uom.name}`;
|
|
102
|
+
}
|
|
103
|
+
assertSameUom(other, operation) {
|
|
104
|
+
if (!sameUom(this.uom, other.uom)) {
|
|
105
|
+
throw new UomMismatchError(`Cannot ${operation} ${this.toString()} and ${other.toString()}: they are in different units.`, `Convert first, e.g. other.to(${this.uom.name}) with Quantity.to(uom).`);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type Decimal } from './decimal.js';
|
|
2
|
+
/**
|
|
3
|
+
* How to round to a currency's or unit's rounding unit:
|
|
4
|
+
* - `half-up`: halves away from zero (1.005 → 1.01, -1.005 → -1.01). Common accounting practice, Odoo's default.
|
|
5
|
+
* - `half-even`: banker's rounding (1.005 → 1.00, 1.015 → 1.02).
|
|
6
|
+
* - `up`: away from zero (1.001 → 1.01). For procurement quantities.
|
|
7
|
+
* - `down`: towards zero (1.009 → 1.00).
|
|
8
|
+
*/
|
|
9
|
+
export type RoundingMethod = 'half-up' | 'half-even' | 'up' | 'down';
|
|
10
|
+
/** Rounds `value` to a multiple of `unit` (e.g. 0.01, 0.05 for cash rounding, 1). */
|
|
11
|
+
export declare function roundToUnit(value: Decimal, unit: Decimal, method: RoundingMethod): Decimal;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Decimal as DecimalJs } from 'decimal.js';
|
|
2
|
+
import { normalizeZero } from './decimal.js';
|
|
3
|
+
const MODES = {
|
|
4
|
+
'half-up': DecimalJs.ROUND_HALF_UP,
|
|
5
|
+
'half-even': DecimalJs.ROUND_HALF_EVEN,
|
|
6
|
+
up: DecimalJs.ROUND_UP,
|
|
7
|
+
down: DecimalJs.ROUND_DOWN,
|
|
8
|
+
};
|
|
9
|
+
/** Rounds `value` to a multiple of `unit` (e.g. 0.01, 0.05 for cash rounding, 1). */
|
|
10
|
+
export function roundToUnit(value, unit, method) {
|
|
11
|
+
return normalizeZero(value.dividedBy(unit).toDecimalPlaces(0, MODES[method]).times(unit));
|
|
12
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { ScopeInternals } from '../env/env.js';
|
|
2
|
+
import type { AnyModel } from '../model/model.js';
|
|
3
|
+
import { type TranslationScope } from './domain.js';
|
|
4
|
+
/** The untyped query `aggregate()` receives (typed in `AggregateQuery`). */
|
|
5
|
+
export interface AggregateQueryRaw {
|
|
6
|
+
readonly where?: Readonly<Record<string, unknown>>;
|
|
7
|
+
readonly groupBy?: readonly string[];
|
|
8
|
+
readonly measures: Readonly<Record<string, unknown>>;
|
|
9
|
+
readonly order?: readonly string[];
|
|
10
|
+
readonly limit?: number;
|
|
11
|
+
readonly groupingSets?: readonly (readonly string[])[];
|
|
12
|
+
readonly rollup?: boolean;
|
|
13
|
+
readonly cube?: boolean;
|
|
14
|
+
}
|
|
15
|
+
/** What the aggregate runtime needs besides the scope. */
|
|
16
|
+
export interface AggregateHost {
|
|
17
|
+
readonly internals: ScopeInternals;
|
|
18
|
+
readonly translation: TranslationScope;
|
|
19
|
+
currency(id: number): Promise<import('../money/currency.js').Currency>;
|
|
20
|
+
}
|
|
21
|
+
/** Runs an aggregate query. @throws {AccessError} @throws {InvalidDomainError} */
|
|
22
|
+
export declare function runAggregate(host: AggregateHost, model: AnyModel, query: AggregateQueryRaw): Promise<Record<string, unknown>[]>;
|