@flowselections/floriday-voorraad 1.0.21 → 1.0.22
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useVoorraadData.d.ts","sourceRoot":"","sources":["../../src/hooks/useVoorraadData.ts"],"names":[],"mappings":"AAEA,OAAO,EAAsB,KAAK,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAqB5E;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,YAAY,EAAE,MAAM,GAAG,IAAI;;;;;
|
|
1
|
+
{"version":3,"file":"useVoorraadData.d.ts","sourceRoot":"","sources":["../../src/hooks/useVoorraadData.ts"],"names":[],"mappings":"AAEA,OAAO,EAAsB,KAAK,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAqB5E;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,YAAY,EAAE,MAAM,GAAG,IAAI;;;;;EAuS1D"}
|
|
@@ -37,6 +37,7 @@ export function useVoorraadData(connectionId) {
|
|
|
37
37
|
groupKeysByFloridayId: new Map(),
|
|
38
38
|
bomProdIds: new Set(),
|
|
39
39
|
bomFloridayIds: new Set(),
|
|
40
|
+
membersByGroupKey: new Map(),
|
|
40
41
|
});
|
|
41
42
|
const [loading, setLoading] = useState(false);
|
|
42
43
|
const [error, setError] = useState(null);
|
|
@@ -50,7 +51,7 @@ export function useVoorraadData(connectionId) {
|
|
|
50
51
|
setLoading(true);
|
|
51
52
|
setError(null);
|
|
52
53
|
try {
|
|
53
|
-
const [pubRes, stock, groupsRes, bomRes] = await Promise.all([
|
|
54
|
+
const [pubRes, stock, groupsRes, bomRes, groupItemsRes] = await Promise.all([
|
|
54
55
|
supabase
|
|
55
56
|
.from("floriday_trade_items_cache")
|
|
56
57
|
.select("data,floriday_id,is_deleted")
|
|
@@ -65,6 +66,9 @@ export function useVoorraadData(connectionId) {
|
|
|
65
66
|
supabase
|
|
66
67
|
.from("product_bom_items")
|
|
67
68
|
.select("parent_product_id,parent_trade_item_floriday_id"),
|
|
69
|
+
supabase
|
|
70
|
+
.from("product_group_items")
|
|
71
|
+
.select("product_group_id,trade_item_id,quantity"),
|
|
68
72
|
]);
|
|
69
73
|
if (pubRes.error)
|
|
70
74
|
throw pubRes.error;
|
|
@@ -105,6 +109,15 @@ export function useVoorraadData(connectionId) {
|
|
|
105
109
|
if (r.parent_trade_item_floriday_id)
|
|
106
110
|
bomFloridayIds.add(r.parent_trade_item_floriday_id);
|
|
107
111
|
});
|
|
112
|
+
const membersByGroupKey = new Map();
|
|
113
|
+
(groupItemsRes.data ?? []).forEach((r) => {
|
|
114
|
+
if (!r.product_group_id || !r.trade_item_id)
|
|
115
|
+
return;
|
|
116
|
+
const key = `group:${r.product_group_id}`;
|
|
117
|
+
const arr = membersByGroupKey.get(key) ?? [];
|
|
118
|
+
arr.push({ tradeItemId: String(r.trade_item_id), quantity: Number(r.quantity ?? 1) });
|
|
119
|
+
membersByGroupKey.set(key, arr);
|
|
120
|
+
});
|
|
108
121
|
setTypeSets({
|
|
109
122
|
groupProdIds,
|
|
110
123
|
groupFloridayIds,
|
|
@@ -112,6 +125,7 @@ export function useVoorraadData(connectionId) {
|
|
|
112
125
|
groupKeysByFloridayId,
|
|
113
126
|
bomProdIds,
|
|
114
127
|
bomFloridayIds,
|
|
128
|
+
membersByGroupKey,
|
|
115
129
|
});
|
|
116
130
|
}
|
|
117
131
|
catch (e) {
|
|
@@ -150,6 +164,9 @@ export function useVoorraadData(connectionId) {
|
|
|
150
164
|
})
|
|
151
165
|
.on("postgres_changes", { event: "*", schema: "public", table: "product_bom_items" }, () => {
|
|
152
166
|
loadPublished();
|
|
167
|
+
})
|
|
168
|
+
.on("postgres_changes", { event: "*", schema: "public", table: "product_group_items" }, () => {
|
|
169
|
+
loadPublished();
|
|
153
170
|
})
|
|
154
171
|
.subscribe();
|
|
155
172
|
return () => {
|
|
@@ -196,16 +213,37 @@ export function useVoorraadData(connectionId) {
|
|
|
196
213
|
const floridayId = item.source === "published" ? item.id : (item.floridayId ?? undefined);
|
|
197
214
|
const groupKey = (prodId && typeSets.groupKeysByProdId.get(prodId)) ||
|
|
198
215
|
(floridayId && typeSets.groupKeysByFloridayId.get(floridayId));
|
|
199
|
-
|
|
200
|
-
if (!groupStock)
|
|
216
|
+
if (!groupKey)
|
|
201
217
|
return item;
|
|
202
|
-
|
|
203
|
-
|
|
218
|
+
// Aggregeer voorraad van alle leden — een productgroep zelf heeft geen eigen
|
|
219
|
+
// voorraad, alleen de som van zijn onderliggende artikelen.
|
|
220
|
+
const members = typeSets.membersByGroupKey.get(groupKey) ?? [];
|
|
221
|
+
let physical = 0;
|
|
222
|
+
let incoming = 0;
|
|
223
|
+
let minimum = 0;
|
|
224
|
+
let memberReserved = 0;
|
|
225
|
+
for (const m of members) {
|
|
226
|
+
const s = stockMap.get(`product:${m.tradeItemId}`) ??
|
|
227
|
+
stockMap.get(m.tradeItemId);
|
|
228
|
+
if (!s)
|
|
229
|
+
continue;
|
|
230
|
+
const qty = m.quantity > 0 ? m.quantity : 1;
|
|
231
|
+
physical += s.physical_stock * qty;
|
|
232
|
+
incoming += s.incoming_stock * qty;
|
|
233
|
+
minimum += s.minimum_stock * qty;
|
|
234
|
+
memberReserved += s.reserved_stock * qty;
|
|
235
|
+
}
|
|
236
|
+
const groupStock = stockMap.get(groupKey);
|
|
237
|
+
const reserved = (groupStock?.reserved_stock ?? 0) + memberReserved;
|
|
238
|
+
const available = Math.max(0, physical - reserved);
|
|
204
239
|
return {
|
|
205
240
|
...item,
|
|
241
|
+
quantity: physical,
|
|
242
|
+
minQuantity: minimum,
|
|
243
|
+
incomingQuantity: incoming,
|
|
206
244
|
reservedQuantity: reserved,
|
|
207
245
|
availableQuantity: available,
|
|
208
|
-
economicQuantity: available +
|
|
246
|
+
economicQuantity: available + incoming,
|
|
209
247
|
};
|
|
210
248
|
};
|
|
211
249
|
// Voorkom dubbele rijen: Floriday-cache-items waar we al een lokaal product voor hebben
|