@sneat/extension-budgetus-contract 0.0.1
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.
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
function getLiabilitiesByPeriod(liabilitiesMode, recurringHappenings, space) {
|
|
2
|
+
const byPeriod = {};
|
|
3
|
+
Object.entries(recurringHappenings).forEach((entry) => {
|
|
4
|
+
const [id, brief] = entry;
|
|
5
|
+
processHappening(liabilitiesMode, space, byPeriod, { id, brief });
|
|
6
|
+
});
|
|
7
|
+
return byPeriod;
|
|
8
|
+
}
|
|
9
|
+
function processHappening(liabilitiesMode, space, byPeriod, happening) {
|
|
10
|
+
// console.log('budget.processHappening()', happening);
|
|
11
|
+
Object.entries(happening.brief.slots || {}).forEach(([slotID, slot]) => {
|
|
12
|
+
if (slot.repeats === 'weekly' || slot.repeats === 'monthly') {
|
|
13
|
+
processSlot(liabilitiesMode, space, byPeriod, happening, slotID, slot);
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
function processSlot(liabilitiesMode, space, byPeriod, happening, slotID, slot) {
|
|
18
|
+
let liabilities = byPeriod[slot.repeats] || {
|
|
19
|
+
happenings: [],
|
|
20
|
+
contacts: [],
|
|
21
|
+
};
|
|
22
|
+
const hLiabilityIndex = liabilities.happenings.findIndex((l) => l.happening.id === happening.id);
|
|
23
|
+
let hLiability = hLiabilityIndex >= 0
|
|
24
|
+
? liabilities.happenings[hLiabilityIndex]
|
|
25
|
+
: {
|
|
26
|
+
happening: { ...happening, space },
|
|
27
|
+
valuesByCurrency: {},
|
|
28
|
+
};
|
|
29
|
+
const prices = happening.brief.prices?.filter((p) => (liabilitiesMode === 'expenses'
|
|
30
|
+
? p.amount.value > 0
|
|
31
|
+
: liabilitiesMode === 'incomes'
|
|
32
|
+
? p.amount.value < 0
|
|
33
|
+
: true) &&
|
|
34
|
+
(p.expenseQuantity || p.term)) || [];
|
|
35
|
+
if (prices.length) {
|
|
36
|
+
for (let priceIdx = 0; priceIdx < (prices.length || 0); priceIdx++) {
|
|
37
|
+
const price = prices[priceIdx];
|
|
38
|
+
hLiability = { ...hLiability, priceAmount: price.amount };
|
|
39
|
+
hLiability = processPrice(hLiability, slot, price, liabilities);
|
|
40
|
+
}
|
|
41
|
+
if (hLiabilityIndex >= 0) {
|
|
42
|
+
liabilities = {
|
|
43
|
+
...liabilities,
|
|
44
|
+
happenings: liabilities.happenings.map((h) => h.happening.id == hLiability.happening.id ? hLiability : h),
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
liabilities = {
|
|
49
|
+
...liabilities,
|
|
50
|
+
happenings: [...liabilities.happenings, hLiability],
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
byPeriod[slot.repeats] = liabilities;
|
|
55
|
+
}
|
|
56
|
+
function processPrice(happeningLiability, slot, price, liabilities) {
|
|
57
|
+
if (slot.repeats === 'weekly' && slot.weekdays) {
|
|
58
|
+
happeningLiability = { ...happeningLiability, times: slot.weekdays.length };
|
|
59
|
+
for (let wdIdx = 0; wdIdx < (slot?.weekdays?.length || 0); wdIdx++) {
|
|
60
|
+
let amountValue = happeningLiability.valuesByCurrency[price.amount.currency] || 0;
|
|
61
|
+
amountValue += price.amount.value;
|
|
62
|
+
happeningLiability = {
|
|
63
|
+
...happeningLiability,
|
|
64
|
+
valuesByCurrency: {
|
|
65
|
+
...happeningLiability?.valuesByCurrency,
|
|
66
|
+
[price.amount.currency]: amountValue,
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
const happeningContacts = happeningLiability.happening.brief?.related?.['contactus']?.['contacts'] || {};
|
|
70
|
+
Object.keys(happeningContacts).forEach((itemID) => {
|
|
71
|
+
let contactLiability = liabilities.contacts.find((c) => c.contact.id == itemID);
|
|
72
|
+
if (!contactLiability) {
|
|
73
|
+
contactLiability = {
|
|
74
|
+
contact: {
|
|
75
|
+
id: itemID,
|
|
76
|
+
space: happeningLiability.happening.space,
|
|
77
|
+
},
|
|
78
|
+
valuesByCurrency: {},
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
contactLiability.valuesByCurrency[price.amount.currency] =
|
|
82
|
+
(contactLiability.valuesByCurrency[price.amount.currency] || 0) +
|
|
83
|
+
price.amount.value;
|
|
84
|
+
liabilities = {
|
|
85
|
+
...liabilities,
|
|
86
|
+
contacts: [...liabilities.contacts, contactLiability],
|
|
87
|
+
};
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return happeningLiability;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Generated bundle index. Do not edit.
|
|
96
|
+
*/
|
|
97
|
+
|
|
98
|
+
export { getLiabilitiesByPeriod };
|
|
99
|
+
//# sourceMappingURL=sneat-extension-budgetus-contract.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sneat-extension-budgetus-contract.mjs","sources":["../../../../../../libs/extensions/budgetus/contract/src/lib/budget-calc-periods.ts","../../../../../../libs/extensions/budgetus/contract/src/sneat-extension-budgetus-contract.ts"],"sourcesContent":["import { IIdAndBrief } from '@sneat/core';\nimport {\n ICalendarHappeningBrief,\n IHappeningPrice,\n IHappeningSlot,\n} from '@sneat/extension-calendarius-contract';\nimport { ISpaceContext } from '@sneat/space-models';\nimport {\n IHappeningLiability,\n LiabilitiesByPeriod,\n IPeriodLiabilities,\n LiabilitiesMode,\n} from './budget-component-types';\n\nexport function getLiabilitiesByPeriod(\n liabilitiesMode: LiabilitiesMode,\n recurringHappenings: Record<string, ICalendarHappeningBrief>,\n space: ISpaceContext,\n): LiabilitiesByPeriod {\n const byPeriod: LiabilitiesByPeriod = {};\n\n Object.entries(recurringHappenings).forEach((entry) => {\n const [id, brief] = entry;\n processHappening(liabilitiesMode, space, byPeriod, { id, brief });\n });\n return byPeriod;\n}\n\nfunction processHappening(\n liabilitiesMode: LiabilitiesMode,\n space: ISpaceContext,\n byPeriod: LiabilitiesByPeriod,\n happening: IIdAndBrief<ICalendarHappeningBrief>,\n): void {\n // console.log('budget.processHappening()', happening);\n Object.entries(happening.brief.slots || {}).forEach(([slotID, slot]) => {\n if (slot.repeats === 'weekly' || slot.repeats === 'monthly') {\n processSlot(liabilitiesMode, space, byPeriod, happening, slotID, slot);\n }\n });\n}\n\nfunction processSlot(\n liabilitiesMode: LiabilitiesMode,\n space: ISpaceContext,\n byPeriod: LiabilitiesByPeriod,\n happening: IIdAndBrief<ICalendarHappeningBrief>,\n slotID: string,\n slot: IHappeningSlot,\n): void {\n let liabilities: IPeriodLiabilities = byPeriod[slot.repeats] || {\n happenings: [],\n contacts: [],\n };\n const hLiabilityIndex = liabilities.happenings.findIndex(\n (l) => l.happening.id === happening.id,\n );\n let hLiability: IHappeningLiability =\n hLiabilityIndex >= 0\n ? liabilities.happenings[hLiabilityIndex]\n : {\n happening: { ...happening, space },\n valuesByCurrency: {},\n };\n\n const prices =\n happening.brief.prices?.filter(\n (p) =>\n (liabilitiesMode === 'expenses'\n ? p.amount.value > 0\n : liabilitiesMode === 'incomes'\n ? p.amount.value < 0\n : true) &&\n (p.expenseQuantity || p.term),\n ) || [];\n\n if (prices.length) {\n for (let priceIdx = 0; priceIdx < (prices.length || 0); priceIdx++) {\n const price = prices[priceIdx];\n hLiability = { ...hLiability, priceAmount: price.amount };\n hLiability = processPrice(hLiability, slot, price, liabilities);\n }\n if (hLiabilityIndex >= 0) {\n liabilities = {\n ...liabilities,\n happenings: liabilities.happenings.map((h) =>\n h.happening.id == hLiability.happening.id ? hLiability : h,\n ),\n };\n } else {\n liabilities = {\n ...liabilities,\n happenings: [...liabilities.happenings, hLiability],\n };\n }\n }\n byPeriod[slot.repeats] = liabilities;\n}\n\nfunction processPrice(\n happeningLiability: IHappeningLiability,\n slot: IHappeningSlot,\n price: IHappeningPrice,\n liabilities: IPeriodLiabilities,\n): IHappeningLiability {\n if (slot.repeats === 'weekly' && slot.weekdays) {\n happeningLiability = { ...happeningLiability, times: slot.weekdays.length };\n for (let wdIdx = 0; wdIdx < (slot?.weekdays?.length || 0); wdIdx++) {\n let amountValue: number =\n happeningLiability.valuesByCurrency[price.amount.currency] || 0;\n amountValue += price.amount.value;\n happeningLiability = {\n ...happeningLiability,\n valuesByCurrency: {\n ...happeningLiability?.valuesByCurrency,\n [price.amount.currency]: amountValue,\n },\n };\n const happeningContacts =\n happeningLiability.happening.brief?.related?.['contactus']?.[\n 'contacts'\n ] || {};\n Object.keys(happeningContacts).forEach((itemID) => {\n let contactLiability = liabilities.contacts.find(\n (c) => c.contact.id == itemID,\n );\n if (!contactLiability) {\n contactLiability = {\n contact: {\n id: itemID,\n space: happeningLiability.happening.space,\n },\n valuesByCurrency: {},\n };\n }\n contactLiability.valuesByCurrency[price.amount.currency] =\n (contactLiability.valuesByCurrency[price.amount.currency] || 0) +\n price.amount.value;\n liabilities = {\n ...liabilities,\n contacts: [...liabilities.contacts, contactLiability],\n };\n });\n }\n }\n return happeningLiability;\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":"SAcgB,sBAAsB,CACpC,eAAgC,EAChC,mBAA4D,EAC5D,KAAoB,EAAA;IAEpB,MAAM,QAAQ,GAAwB,EAAE;IAExC,MAAM,CAAC,OAAO,CAAC,mBAAmB,CAAC,CAAC,OAAO,CAAC,CAAC,KAAK,KAAI;AACpD,QAAA,MAAM,CAAC,EAAE,EAAE,KAAK,CAAC,GAAG,KAAK;AACzB,QAAA,gBAAgB,CAAC,eAAe,EAAE,KAAK,EAAE,QAAQ,EAAE,EAAE,EAAE,EAAE,KAAK,EAAE,CAAC;AACnE,IAAA,CAAC,CAAC;AACF,IAAA,OAAO,QAAQ;AACjB;AAEA,SAAS,gBAAgB,CACvB,eAAgC,EAChC,KAAoB,EACpB,QAA6B,EAC7B,SAA+C,EAAA;;IAG/C,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,KAAK,C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B,CAAC;iBACtD;AACH,YAAA,CAAC,CAAC;QACJ;IACF;AACA,IAAA,OAAO,kBAAkB;AAC3B;;AClJA;;AAEG;;;;"}
|
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@sneat/extension-budgetus-contract",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"publishConfig": {
|
|
5
|
+
"access": "public"
|
|
6
|
+
},
|
|
7
|
+
"peerDependencies": {
|
|
8
|
+
"@sneat/core": "0.12.1",
|
|
9
|
+
"@sneat/space-models": "0.12.1",
|
|
10
|
+
"@sneat/extension-contactus-contract": "0.12.1",
|
|
11
|
+
"@sneat/extension-calendarius-contract": "0.12.1",
|
|
12
|
+
"vitest": "^4.0.9"
|
|
13
|
+
},
|
|
14
|
+
"sideEffects": false,
|
|
15
|
+
"module": "fesm2022/sneat-extension-budgetus-contract.mjs",
|
|
16
|
+
"typings": "types/sneat-extension-budgetus-contract.d.ts",
|
|
17
|
+
"exports": {
|
|
18
|
+
"./package.json": {
|
|
19
|
+
"default": "./package.json"
|
|
20
|
+
},
|
|
21
|
+
".": {
|
|
22
|
+
"types": "./types/sneat-extension-budgetus-contract.d.ts",
|
|
23
|
+
"default": "./fesm2022/sneat-extension-budgetus-contract.mjs"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"type": "module",
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"tslib": "^2.3.0"
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { IContactContext } from '@sneat/extension-contactus-contract';
|
|
2
|
+
import { IAmount, IHappeningContext, RepeatPeriod, ICalendarHappeningBrief } from '@sneat/extension-calendarius-contract';
|
|
3
|
+
import { ISpaceContext } from '@sneat/space-models';
|
|
4
|
+
|
|
5
|
+
type LiabilitiesMode = 'incomes' | 'expenses' | 'balance';
|
|
6
|
+
type AmountsByCurrency = {
|
|
7
|
+
[id: string]: number;
|
|
8
|
+
};
|
|
9
|
+
interface ILiabilityBase {
|
|
10
|
+
readonly valuesByCurrency: AmountsByCurrency;
|
|
11
|
+
}
|
|
12
|
+
interface IHappeningLiability extends ILiabilityBase {
|
|
13
|
+
priceAmount?: IAmount;
|
|
14
|
+
times?: number;
|
|
15
|
+
readonly happening: IHappeningContext;
|
|
16
|
+
}
|
|
17
|
+
interface IPeriodLiabilities {
|
|
18
|
+
readonly happenings: readonly IHappeningLiability[];
|
|
19
|
+
readonly contacts: readonly IContactLiability[];
|
|
20
|
+
}
|
|
21
|
+
type LiabilitiesByPeriod = Partial<Record<RepeatPeriod, IPeriodLiabilities>>;
|
|
22
|
+
interface IContactLiability extends ILiabilityBase {
|
|
23
|
+
readonly contact: IContactContext;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
declare function getLiabilitiesByPeriod(liabilitiesMode: LiabilitiesMode, recurringHappenings: Record<string, ICalendarHappeningBrief>, space: ISpaceContext): LiabilitiesByPeriod;
|
|
27
|
+
|
|
28
|
+
export { getLiabilitiesByPeriod };
|
|
29
|
+
export type { AmountsByCurrency, IContactLiability, IHappeningLiability, ILiabilityBase, IPeriodLiabilities, LiabilitiesByPeriod, LiabilitiesMode };
|