@flowselections/floriday-voorraad 1.0.84 → 1.0.87
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/dist-lib/components/voorraad/FloridayTakeoverPanel.d.ts +7 -0
- package/dist-lib/components/voorraad/FloridayTakeoverPanel.d.ts.map +1 -0
- package/dist-lib/components/voorraad/FloridayTakeoverPanel.js +196 -0
- package/dist-lib/components/voorraad/VoorraadPage.d.ts.map +1 -1
- package/dist-lib/components/voorraad/VoorraadPage.js +2 -1
- package/dist-lib/integrations/supabase/types.d.ts +733 -0
- package/dist-lib/integrations/supabase/types.d.ts.map +1 -1
- package/dist-lib/lib/__tests__/floriday-fetch-status.test.d.ts +2 -0
- package/dist-lib/lib/__tests__/floriday-fetch-status.test.d.ts.map +1 -0
- package/dist-lib/lib/__tests__/floriday-fetch-status.test.js +25 -0
- package/dist-lib/lib/floriday-fetch-status.d.ts +17 -0
- package/dist-lib/lib/floriday-fetch-status.d.ts.map +1 -0
- package/dist-lib/lib/floriday-fetch-status.js +14 -0
- package/dist-lib/lib/floriday-takeover.functions.d.ts +65 -0
- package/dist-lib/lib/floriday-takeover.functions.d.ts.map +1 -0
- package/dist-lib/lib/floriday-takeover.functions.js +231 -0
- package/dist-lib/server-hooks/floriday-stock-reconcile.d.ts.map +1 -1
- package/dist-lib/server-hooks/floriday-stock-reconcile.js +5 -1
- package/dist-lib/server-hooks/guard.d.ts.map +1 -1
- package/dist-lib/server-hooks/guard.js +26 -3
- package/package.json +1 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"floriday-fetch-status.test.d.ts","sourceRoot":"","sources":["../../../src/lib/__tests__/floriday-fetch-status.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { fetchState, fetchStateBlocksTakeover } from "../floriday-fetch-status.js";
|
|
3
|
+
const row = (t, done, ok) => ({
|
|
4
|
+
backfill_target_sequence: t, backfill_done_at: done, last_success_at: ok,
|
|
5
|
+
});
|
|
6
|
+
describe("ophaalstatus artikelen", () => {
|
|
7
|
+
it("lopend ophalen blokkeert overnemen", () => {
|
|
8
|
+
expect(fetchState(row(500, null, "2026-10-20"), 120)).toBe("bezig");
|
|
9
|
+
expect(fetchStateBlocksTakeover(fetchState(row(500, null, null), 0))).toBe(true);
|
|
10
|
+
});
|
|
11
|
+
it("nog nooit gestart blokkeert overnemen", () => {
|
|
12
|
+
expect(fetchState(null, 0)).toBe("nooit_gestart");
|
|
13
|
+
expect(fetchState(row(null, null, null), 0)).toBe("nooit_gestart");
|
|
14
|
+
expect(fetchStateBlocksTakeover("nooit_gestart")).toBe(true);
|
|
15
|
+
});
|
|
16
|
+
it("na afloop met lege target komt de knop vrij", () => {
|
|
17
|
+
expect(fetchState(row(null, "2026-10-20", "2026-10-20"), 50)).toBe("compleet");
|
|
18
|
+
expect(fetchState(row(500, "2026-10-20", "2026-10-20"), 50)).toBe("compleet");
|
|
19
|
+
expect(fetchStateBlocksTakeover(fetchState(row(null, null, "2026-10-20"), 0))).toBe(false);
|
|
20
|
+
});
|
|
21
|
+
it("koppeling van vóór 1.0.95 zonder backfill is compleet", () => {
|
|
22
|
+
expect(fetchState(row(null, null, "2026-09-01"), 300)).toBe("compleet");
|
|
23
|
+
expect(fetchState(null, 300)).toBe("compleet");
|
|
24
|
+
});
|
|
25
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ophaalstatus van Floriday-authenticatie (floriday_sync_state) per entiteit.
|
|
3
|
+
* Dezelfde regel staat in de database (floriday_takeover_fetch_state); houd ze gelijk.
|
|
4
|
+
* - bezig: backfill_target_sequence gevuld én backfill_done_at leeg
|
|
5
|
+
* - nooit gestart: geen statusrij én geen gegevens in de cache, of een rij zonder
|
|
6
|
+
* enige geslaagde ophaling en zonder gegevens
|
|
7
|
+
* - compleet: al het andere (ook target leeg na afloop, of koppeling van vóór 1.0.95)
|
|
8
|
+
*/
|
|
9
|
+
export type FetchStateRow = {
|
|
10
|
+
backfill_target_sequence: number | null;
|
|
11
|
+
backfill_done_at: string | null;
|
|
12
|
+
last_success_at: string | null;
|
|
13
|
+
} | null;
|
|
14
|
+
export type FetchState = "bezig" | "nooit_gestart" | "compleet";
|
|
15
|
+
export declare function fetchState(row: FetchStateRow, cachedCount: number): FetchState;
|
|
16
|
+
export declare function fetchStateBlocksTakeover(s: FetchState): boolean;
|
|
17
|
+
//# sourceMappingURL=floriday-fetch-status.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"floriday-fetch-status.d.ts","sourceRoot":"","sources":["../../src/lib/floriday-fetch-status.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,MAAM,MAAM,aAAa,GAAG;IAC1B,wBAAwB,EAAE,MAAM,GAAG,IAAI,CAAC;IACxC,gBAAgB,EAAE,MAAM,GAAG,IAAI,CAAC;IAChC,eAAe,EAAE,MAAM,GAAG,IAAI,CAAC;CAChC,GAAG,IAAI,CAAC;AAET,MAAM,MAAM,UAAU,GAAG,OAAO,GAAG,eAAe,GAAG,UAAU,CAAC;AAEhE,wBAAgB,UAAU,CAAC,GAAG,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,GAAG,UAAU,CAM9E;AAED,wBAAgB,wBAAwB,CAAC,CAAC,EAAE,UAAU,GAAG,OAAO,CAE/D"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export function fetchState(row, cachedCount) {
|
|
2
|
+
if (row && row.backfill_target_sequence != null && !row.backfill_done_at)
|
|
3
|
+
return "bezig";
|
|
4
|
+
if (cachedCount > 0)
|
|
5
|
+
return "compleet";
|
|
6
|
+
if (!row)
|
|
7
|
+
return "nooit_gestart";
|
|
8
|
+
if (row.backfill_done_at || row.last_success_at)
|
|
9
|
+
return "compleet";
|
|
10
|
+
return "nooit_gestart";
|
|
11
|
+
}
|
|
12
|
+
export function fetchStateBlocksTakeover(s) {
|
|
13
|
+
return s !== "compleet";
|
|
14
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { type FetchState } from "./floriday-fetch-status.js";
|
|
2
|
+
export type TakeoverRow = {
|
|
3
|
+
tradeItemId: string;
|
|
4
|
+
name: string;
|
|
5
|
+
articleCode: string | null;
|
|
6
|
+
gtin: string | null;
|
|
7
|
+
vbnCode: string | null;
|
|
8
|
+
productId: string | null;
|
|
9
|
+
productName: string | null;
|
|
10
|
+
action: "bijwerken" | "aanmaken" | "niet";
|
|
11
|
+
reason: string | null;
|
|
12
|
+
suggestion: string | null;
|
|
13
|
+
erpStock: number | null;
|
|
14
|
+
erpPrice: number | null;
|
|
15
|
+
floridayStock: number | null;
|
|
16
|
+
stockSource: string | null;
|
|
17
|
+
supplyLinePrice: number | null;
|
|
18
|
+
catalogPrice: number | null;
|
|
19
|
+
floridayPrice: number | null;
|
|
20
|
+
priceSource: string | null;
|
|
21
|
+
priceDiffers: boolean;
|
|
22
|
+
forms: {
|
|
23
|
+
continuous: boolean;
|
|
24
|
+
continuousAvailable: boolean | null;
|
|
25
|
+
weekly: boolean;
|
|
26
|
+
weeks: Array<{
|
|
27
|
+
year: number;
|
|
28
|
+
week: number;
|
|
29
|
+
pieces: number;
|
|
30
|
+
available: boolean;
|
|
31
|
+
}>;
|
|
32
|
+
supply_lines: number;
|
|
33
|
+
customer_offers: number;
|
|
34
|
+
};
|
|
35
|
+
customerOffers: CustomerOfferView[];
|
|
36
|
+
};
|
|
37
|
+
export type CustomerOfferView = {
|
|
38
|
+
id: string;
|
|
39
|
+
customer: string | null;
|
|
40
|
+
price: number | null;
|
|
41
|
+
currency: string | null;
|
|
42
|
+
pieces: number | null;
|
|
43
|
+
from: string | null;
|
|
44
|
+
to: string | null;
|
|
45
|
+
};
|
|
46
|
+
export declare const getFloridayTakeoverPreview: import("@tanstack/start-client-core").OptionalFetcher<readonly [import("@tanstack/start-client-core").FunctionMiddlewareAfterServer<{}, unknown, undefined, {
|
|
47
|
+
supabase: import("../integrations/supabase/auth-middleware.js").AuthedSupabaseClient;
|
|
48
|
+
userId: string;
|
|
49
|
+
claims: import("@supabase/auth-js").JwtPayload;
|
|
50
|
+
}, undefined, undefined, undefined>], undefined, Promise<{
|
|
51
|
+
connectionId: string;
|
|
52
|
+
environment: any;
|
|
53
|
+
weeks: {
|
|
54
|
+
year: number;
|
|
55
|
+
week: number;
|
|
56
|
+
}[];
|
|
57
|
+
weekError: string | null;
|
|
58
|
+
fetchStatus: {
|
|
59
|
+
tradeItems: FetchState;
|
|
60
|
+
catalogPrices: FetchState;
|
|
61
|
+
customerOffers: FetchState;
|
|
62
|
+
};
|
|
63
|
+
rows: TakeoverRow[];
|
|
64
|
+
}>>;
|
|
65
|
+
//# sourceMappingURL=floriday-takeover.functions.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"floriday-takeover.functions.d.ts","sourceRoot":"","sources":["../../src/lib/floriday-takeover.functions.ts"],"names":[],"mappings":"AAeA,OAAO,EAAc,KAAK,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAEtE,MAAM,MAAM,WAAW,GAAG;IACxB,WAAW,EAAE,MAAM,CAAC;IACpB,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B,MAAM,EAAE,WAAW,GAAG,UAAU,GAAG,MAAM,CAAC;IAC1C,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,aAAa,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7B,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B,eAAe,EAAE,MAAM,GAAG,IAAI,CAAC;IAC/B,YAAY,EAAE,MAAM,GAAG,IAAI,CAAC;IAC5B,aAAa,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7B,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B,YAAY,EAAE,OAAO,CAAC;IACtB,KAAK,EAAE;QACL,UAAU,EAAE,OAAO,CAAC;QACpB,mBAAmB,EAAE,OAAO,GAAG,IAAI,CAAC;QACpC,MAAM,EAAE,OAAO,CAAC;QAChB,KAAK,EAAE,KAAK,CAAC;YAAE,IAAI,EAAE,MAAM,CAAC;YAAC,IAAI,EAAE,MAAM,CAAC;YAAC,MAAM,EAAE,MAAM,CAAC;YAAC,SAAS,EAAE,OAAO,CAAA;SAAE,CAAC,CAAC;QACjF,YAAY,EAAE,MAAM,CAAC;QACrB,eAAe,EAAE,MAAM,CAAC;KACzB,CAAC;IACF,cAAc,EAAE,iBAAiB,EAAE,CAAC;CACrC,CAAC;AAEF,MAAM,MAAM,iBAAiB,GAAG;IAC9B,EAAE,EAAE,MAAM,CAAC;IACX,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IACpB,EAAE,EAAE,MAAM,GAAG,IAAI,CAAC;CACnB,CAAC;AA2CF,eAAO,MAAM,0BAA0B;;;;;;;;;;;;;oBAmFF,UAAU;uBAAiB,UAAU;wBAAkB,UAAU;;;GAgFlG,CAAC"}
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Proefrapport "Overnemen uit Floriday". Leest alleen: niets wordt geschreven
|
|
3
|
+
* en niets gaat naar Floriday. Het daadwerkelijk overnemen gebeurt via de
|
|
4
|
+
* databasefunctie `floriday_takeover_apply` (één transactie, terug te draaien).
|
|
5
|
+
*
|
|
6
|
+
* Floriday (Suppliers API 2026v1), alleen lezen via de leesroute van de
|
|
7
|
+
* Floriday-authenticatiemodule:
|
|
8
|
+
* - GET /trade-items/base-supply?year&week → WeeklyBaseSupply { tradeItemId, numberOfPieces, isAvailable, basePricePerPiece { currency, value } }
|
|
9
|
+
* Daarnaast de lokaal bijgehouden Floriday-gegevens van deze koppeling:
|
|
10
|
+
* - trade items (TradeItemBase: tradeItemId, supplierArticleCode, articleGtin, vbnProductCode, tradeItemName)
|
|
11
|
+
* - doorlopende voorraad (GET /trade-items/{id}/continuous-stock, door de ophaaltaak bewaard)
|
|
12
|
+
* - catalogusprijzen en aanbodregels (supply lines), incl. klantspecifieke aanbiedingen (alleen tonen)
|
|
13
|
+
*/
|
|
14
|
+
import { createServerFn } from "@tanstack/react-start";
|
|
15
|
+
import { requireSupabaseAuth } from "../integrations/supabase/auth-middleware.js";
|
|
16
|
+
import { fetchState } from "./floriday-fetch-status.js";
|
|
17
|
+
function offerView(id, d) {
|
|
18
|
+
const pr = d?.pricePerPiece ?? d?.price ?? null;
|
|
19
|
+
const price = typeof pr === "number" ? pr : pr?.value != null ? Number(pr.value) : null;
|
|
20
|
+
const orgs = d?.customerOrganizationIds ?? (d?.customerOrganizationId ? [d.customerOrganizationId] : []);
|
|
21
|
+
return {
|
|
22
|
+
id,
|
|
23
|
+
customer: d?.customerOrganizationName ?? (Array.isArray(orgs) && orgs.length ? orgs.join(", ") : null),
|
|
24
|
+
price: Number.isFinite(price) ? price : null,
|
|
25
|
+
currency: pr?.currency ?? d?.currency ?? null,
|
|
26
|
+
pieces: d?.numberOfPieces != null ? Number(d.numberOfPieces) : d?.availableNumberOfPieces != null ? Number(d.availableNumberOfPieces) : null,
|
|
27
|
+
from: d?.period?.startDateTime ?? d?.startDateTime ?? d?.validFrom ?? null,
|
|
28
|
+
to: d?.period?.endDateTime ?? d?.endDateTime ?? d?.validTo ?? null,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
function offerTradeItems(d) {
|
|
32
|
+
const ids = new Set();
|
|
33
|
+
if (d?.tradeItemId)
|
|
34
|
+
ids.add(String(d.tradeItemId).toLowerCase());
|
|
35
|
+
for (const l of Array.isArray(d?.lines) ? d.lines : Array.isArray(d?.tradeItems) ? d.tradeItems : []) {
|
|
36
|
+
if (l?.tradeItemId)
|
|
37
|
+
ids.add(String(l.tradeItemId).toLowerCase());
|
|
38
|
+
}
|
|
39
|
+
return [...ids];
|
|
40
|
+
}
|
|
41
|
+
function isoWeek(d) {
|
|
42
|
+
const t = new Date(Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate()));
|
|
43
|
+
const day = t.getUTCDay() || 7;
|
|
44
|
+
t.setUTCDate(t.getUTCDate() + 4 - day);
|
|
45
|
+
const y = t.getUTCFullYear();
|
|
46
|
+
const w = Math.ceil(((t.getTime() - Date.UTC(y, 0, 1)) / 86400000 + 1) / 7);
|
|
47
|
+
return { year: y, week: w };
|
|
48
|
+
}
|
|
49
|
+
function nameOf(data) {
|
|
50
|
+
const n = data?.tradeItemName;
|
|
51
|
+
if (typeof n === "string")
|
|
52
|
+
return n;
|
|
53
|
+
return String(n?.nl ?? n?.en ?? Object.values(n ?? {})[0] ?? "").trim();
|
|
54
|
+
}
|
|
55
|
+
const norm = (v) => String(v ?? "").trim().toLowerCase();
|
|
56
|
+
export const getFloridayTakeoverPreview = createServerFn({ method: "POST" })
|
|
57
|
+
.middleware([requireSupabaseAuth])
|
|
58
|
+
.handler(async ({ context }) => {
|
|
59
|
+
const { data: canWrite } = await context.supabase.rpc("voorraad_can_write");
|
|
60
|
+
if (canWrite !== true)
|
|
61
|
+
throw new Error("Je hebt geen rechten om voorraad over te nemen.");
|
|
62
|
+
const { resolveActiveFloridayConnection } = await import("./floriday-env.server.js");
|
|
63
|
+
const conn = await resolveActiveFloridayConnection(context.supabase);
|
|
64
|
+
const { supabaseAdmin } = await import("../integrations/supabase/client.server.js");
|
|
65
|
+
const admin = supabaseAdmin;
|
|
66
|
+
const nowIso = new Date().toISOString();
|
|
67
|
+
const [items, products, stock, cont, catalog, lines, syncRows, offers] = await Promise.all([
|
|
68
|
+
admin.from("floriday_trade_items_cache").select("floriday_id, data").eq("connection_id", conn.id).eq("is_deleted", false).limit(5000),
|
|
69
|
+
admin.from("products").select("id, product, barcode, article_number, floriday_trade_item_id, sale_price").limit(10000),
|
|
70
|
+
admin.from("trade_item_stock").select("trade_item_key, physical_stock").eq("connection_id", conn.id).limit(20000),
|
|
71
|
+
admin.from("floriday_continuous_stock_cache").select("trade_item_id, number_of_pieces").eq("connection_id", conn.id).limit(5000),
|
|
72
|
+
admin.from("floriday_catalog_prices_cache").select("trade_item_id, price, updated_at").eq("connection_id", conn.id).eq("is_deleted", false).or(`valid_to.is.null,valid_to.gte.${nowIso}`).order("updated_at", { ascending: false }).limit(20000),
|
|
73
|
+
admin.from("trade_item_supply_lines").select("trade_item_id, supply_type, price_per_piece, updated_at").eq("connection_id", conn.id).order("updated_at", { ascending: false }).limit(20000),
|
|
74
|
+
admin.from("floriday_sync_state").select("entity_type, backfill_target_sequence, backfill_done_at, last_success_at").eq("connection_id", conn.id).in("entity_type", ["trade-items", "catalog-prices", "customer-offers"]),
|
|
75
|
+
admin.from("floriday_customer_offers_cache").select("floriday_id, data").eq("connection_id", conn.id).eq("is_deleted", false).limit(5000),
|
|
76
|
+
]);
|
|
77
|
+
for (const r of [items, products, stock, cont, catalog, lines, syncRows, offers])
|
|
78
|
+
if (r.error)
|
|
79
|
+
throw new Error(r.error.message);
|
|
80
|
+
// Weekaanbod: huidige en twee komende weken, live gelezen (1 per seconde).
|
|
81
|
+
const now = new Date();
|
|
82
|
+
const weeks = [0, 1, 2].map((i) => isoWeek(new Date(now.getTime() + i * 7 * 86400000)));
|
|
83
|
+
let weekError = null;
|
|
84
|
+
const weekly = new Map();
|
|
85
|
+
try {
|
|
86
|
+
const { readBaseSupplyWeeks } = await import("./base-supply-reader.server.js");
|
|
87
|
+
const out = await readBaseSupplyWeeks(weeks);
|
|
88
|
+
if (out.error)
|
|
89
|
+
weekError = out.error;
|
|
90
|
+
for (const w of out.results) {
|
|
91
|
+
if (!w.ok) {
|
|
92
|
+
weekError = weekError ?? w.error;
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
for (const r of w.rows) {
|
|
96
|
+
const arr = weekly.get(r.tradeItemId) ?? [];
|
|
97
|
+
arr.push({ year: w.year, week: w.week, pieces: r.numberOfPieces, available: r.isAvailable, price: r.basePricePerPiece });
|
|
98
|
+
weekly.set(r.tradeItemId, arr);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
catch (e) {
|
|
103
|
+
weekError = e?.message ?? String(e);
|
|
104
|
+
}
|
|
105
|
+
const byFid = new Map();
|
|
106
|
+
const unlinked = [];
|
|
107
|
+
for (const p of products.data ?? []) {
|
|
108
|
+
if (p.floriday_trade_item_id)
|
|
109
|
+
byFid.set(String(p.floriday_trade_item_id).toLowerCase(), p);
|
|
110
|
+
else
|
|
111
|
+
unlinked.push(p);
|
|
112
|
+
}
|
|
113
|
+
const stockBy = new Map();
|
|
114
|
+
for (const s of stock.data ?? [])
|
|
115
|
+
stockBy.set(String(s.trade_item_key), Number(s.physical_stock ?? 0));
|
|
116
|
+
const contBy = new Map();
|
|
117
|
+
for (const c of cont.data ?? [])
|
|
118
|
+
contBy.set(String(c.trade_item_id), Number(c.number_of_pieces ?? 0));
|
|
119
|
+
const catBy = new Map();
|
|
120
|
+
for (const c of catalog.data ?? []) {
|
|
121
|
+
const k = String(c.trade_item_id);
|
|
122
|
+
if (!catBy.has(k) && Number(c.price) > 0)
|
|
123
|
+
catBy.set(k, Number(c.price));
|
|
124
|
+
}
|
|
125
|
+
const lineBy = new Map();
|
|
126
|
+
for (const l of lines.data ?? []) {
|
|
127
|
+
const k = String(l.trade_item_id);
|
|
128
|
+
const cur = lineBy.get(k) ?? { price: null, count: 0, customer: 0 };
|
|
129
|
+
const isCustomer = /customer/i.test(String(l.supply_type ?? ""));
|
|
130
|
+
if (isCustomer)
|
|
131
|
+
cur.customer++;
|
|
132
|
+
else {
|
|
133
|
+
cur.count++;
|
|
134
|
+
if (cur.price == null && Number(l.price_per_piece) > 0)
|
|
135
|
+
cur.price = Number(l.price_per_piece);
|
|
136
|
+
}
|
|
137
|
+
lineBy.set(k, cur);
|
|
138
|
+
}
|
|
139
|
+
const syncBy = new Map();
|
|
140
|
+
for (const r of syncRows.data ?? [])
|
|
141
|
+
syncBy.set(String(r.entity_type), r);
|
|
142
|
+
const offersBy = new Map();
|
|
143
|
+
for (const o of offers.data ?? []) {
|
|
144
|
+
for (const tid of offerTradeItems(o.data)) {
|
|
145
|
+
const arr = offersBy.get(tid) ?? [];
|
|
146
|
+
arr.push(offerView(String(o.floriday_id), o.data));
|
|
147
|
+
offersBy.set(tid, arr);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
const fetchStatus = {
|
|
151
|
+
tradeItems: fetchState(syncBy.get("trade-items") ?? null, (items.data ?? []).length),
|
|
152
|
+
catalogPrices: fetchState(syncBy.get("catalog-prices") ?? null, (catalog.data ?? []).length),
|
|
153
|
+
customerOffers: fetchState(syncBy.get("customer-offers") ?? null, (offers.data ?? []).length),
|
|
154
|
+
};
|
|
155
|
+
const cur = weeks[0];
|
|
156
|
+
const rows = (items.data ?? []).map((it) => {
|
|
157
|
+
const fid = String(it.floriday_id).toLowerCase();
|
|
158
|
+
const d = it.data ?? {};
|
|
159
|
+
const p = byFid.get(fid) ?? null;
|
|
160
|
+
const w = (weekly.get(fid) ?? []).sort((a, b) => a.year - b.year || a.week - b.week);
|
|
161
|
+
const contRaw = contBy.has(fid) ? contBy.get(fid) : null;
|
|
162
|
+
const line = lineBy.get(fid) ?? { price: null, count: 0, customer: 0 };
|
|
163
|
+
// Aanbodvorm uit Floriday zelf: staat er een doorlopende-voorraadrecord, dan is dat de vorm; anders weekaanbod huidige week.
|
|
164
|
+
let floridayStock = null;
|
|
165
|
+
let stockSource = null;
|
|
166
|
+
if (contRaw != null) {
|
|
167
|
+
floridayStock = contRaw;
|
|
168
|
+
stockSource = "Doorlopende voorraad";
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
const now = w.find((x) => x.year === cur.year && x.week === cur.week);
|
|
172
|
+
if (now) {
|
|
173
|
+
floridayStock = now.pieces;
|
|
174
|
+
stockSource = `Weekaanbod week ${now.week}`;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
// Prijs: aanbodregel gaat voor op catalogusprijs.
|
|
178
|
+
const weekPrice = w.find((x) => x.price != null && x.price > 0)?.price ?? null;
|
|
179
|
+
const supplyLinePrice = line.price ?? weekPrice;
|
|
180
|
+
const catalogPrice = catBy.get(fid) ?? null;
|
|
181
|
+
const floridayPrice = supplyLinePrice ?? catalogPrice;
|
|
182
|
+
const priceSource = supplyLinePrice != null ? "Aanbodregel" : catalogPrice != null ? "Catalogusprijs" : null;
|
|
183
|
+
let suggestion = null;
|
|
184
|
+
if (!p) {
|
|
185
|
+
const gtin = norm(d.articleGtin), code = norm(d.supplierArticleCode), nm = norm(nameOf(d));
|
|
186
|
+
const hit = unlinked.find((u) => (gtin && norm(u.barcode) === gtin) || (code && norm(u.article_number) === code) || (nm && norm(u.product) === nm));
|
|
187
|
+
if (hit)
|
|
188
|
+
suggestion = `Lijkt op bestaand artikel "${hit.product}" (niet automatisch gekoppeld)`;
|
|
189
|
+
}
|
|
190
|
+
const erpStock = p
|
|
191
|
+
? stockBy.get(fid) ?? stockBy.get(`product:${p.id}`) ?? stockBy.get(String(p.id)) ?? null
|
|
192
|
+
: null;
|
|
193
|
+
const name = nameOf(d) || String(d.supplierArticleCode ?? fid);
|
|
194
|
+
let action = p ? "bijwerken" : "aanmaken";
|
|
195
|
+
let reason = null;
|
|
196
|
+
if (floridayStock == null && floridayPrice == null) {
|
|
197
|
+
action = p ? "niet" : "aanmaken";
|
|
198
|
+
reason = "Geen aantal en geen prijs gevonden in Floriday";
|
|
199
|
+
}
|
|
200
|
+
return {
|
|
201
|
+
tradeItemId: fid, name,
|
|
202
|
+
articleCode: d.supplierArticleCode ?? null,
|
|
203
|
+
gtin: d.articleGtin ?? null,
|
|
204
|
+
vbnCode: d.vbnProductCode != null ? String(d.vbnProductCode) : null,
|
|
205
|
+
productId: p?.id ?? null, productName: p?.product ?? null,
|
|
206
|
+
action, reason, suggestion,
|
|
207
|
+
erpStock, erpPrice: p?.sale_price != null ? Number(p.sale_price) : null,
|
|
208
|
+
floridayStock, stockSource,
|
|
209
|
+
supplyLinePrice, catalogPrice, floridayPrice, priceSource,
|
|
210
|
+
priceDiffers: supplyLinePrice != null && catalogPrice != null && Math.abs(supplyLinePrice - catalogPrice) > 0.0001,
|
|
211
|
+
forms: {
|
|
212
|
+
continuous: contRaw != null && contRaw > 0,
|
|
213
|
+
continuousAvailable: contRaw == null ? null : contRaw > 0,
|
|
214
|
+
weekly: w.length > 0,
|
|
215
|
+
weeks: w.map(({ year, week, pieces, available }) => ({ year, week, pieces, available })),
|
|
216
|
+
supply_lines: line.count,
|
|
217
|
+
customer_offers: (offersBy.get(fid) ?? []).length || line.customer,
|
|
218
|
+
},
|
|
219
|
+
customerOffers: offersBy.get(fid) ?? [],
|
|
220
|
+
};
|
|
221
|
+
});
|
|
222
|
+
rows.sort((a, b) => a.name.localeCompare(b.name, "nl"));
|
|
223
|
+
return {
|
|
224
|
+
connectionId: conn.id,
|
|
225
|
+
environment: conn.environment ?? null,
|
|
226
|
+
weeks,
|
|
227
|
+
weekError,
|
|
228
|
+
fetchStatus,
|
|
229
|
+
rows,
|
|
230
|
+
};
|
|
231
|
+
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"floriday-stock-reconcile.d.ts","sourceRoot":"","sources":["../../src/server-hooks/floriday-stock-reconcile.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"floriday-stock-reconcile.d.ts","sourceRoot":"","sources":["../../src/server-hooks/floriday-stock-reconcile.ts"],"names":[],"mappings":"AA2PA,wHAAwH;AACxH,wBAAsB,4BAA4B,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,CAAC,CAEtF"}
|
|
@@ -117,7 +117,7 @@ async function handle(request) {
|
|
|
117
117
|
}
|
|
118
118
|
const { data: settings, error: setErr } = await admin
|
|
119
119
|
.from("floriday_inventory_settings")
|
|
120
|
-
.select("id, connection_id, product_id, floriday_trade_item_id, desired_quantity, desired_availability, stock_mode, sync_status")
|
|
120
|
+
.select("id, connection_id, product_id, floriday_trade_item_id, desired_quantity, desired_availability, stock_mode, sync_status, erp_leading")
|
|
121
121
|
.eq("connection_id", connection.id)
|
|
122
122
|
.not("floriday_trade_item_id", "is", null);
|
|
123
123
|
if (setErr)
|
|
@@ -180,6 +180,10 @@ async function handle(request) {
|
|
|
180
180
|
.eq("id", s.id);
|
|
181
181
|
if (status === "DRIFT") {
|
|
182
182
|
drift++;
|
|
183
|
+
// Alleen melden, tenzij het ERP leidend is (erp_leading true, of NULL =
|
|
184
|
+
// artikel van vóór 1.0.84). Floriday-leidende artikelen nooit corrigeren.
|
|
185
|
+
if (s.erp_leading === false)
|
|
186
|
+
continue;
|
|
183
187
|
await admin.rpc("floriday_recompute_inventory", {
|
|
184
188
|
p_connection_id: s.connection_id,
|
|
185
189
|
p_product_id: s.product_id,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"guard.d.ts","sourceRoot":"","sources":["../../src/server-hooks/guard.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"guard.d.ts","sourceRoot":"","sources":["../../src/server-hooks/guard.ts"],"names":[],"mappings":"AA4DA,wBAAsB,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAUhE;AAED,wBAAsB,cAAc,CAClC,OAAO,EAAE,OAAO,EAChB,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,OAAO,CAAC,QAAQ,CAAC,EAC/C,KAAK,EAAE,MAAM,GACZ,OAAO,CAAC,QAAQ,CAAC,CAUnB"}
|
|
@@ -13,8 +13,31 @@ const CORS = {
|
|
|
13
13
|
"Access-Control-Allow-Methods": "POST, OPTIONS",
|
|
14
14
|
"Access-Control-Allow-Headers": "content-type, apikey, x-api-key, authorization",
|
|
15
15
|
};
|
|
16
|
-
/**
|
|
17
|
-
|
|
16
|
+
/**
|
|
17
|
+
* Deze verwerkers staan zonder expliciete instelling alleen aan zolang er
|
|
18
|
+
* minstens één ERP-leidend artikel is (erp_leading = true, of een artikel van
|
|
19
|
+
* vóór 1.0.84 met een andere modus dan Uit). Anders uit: een Shell die alles
|
|
20
|
+
* uit Floriday overneemt, verstuurt dan niets.
|
|
21
|
+
*/
|
|
22
|
+
const DEFAULT_ON_IF_ERP_LEADING = new Set(["floriday_base_supply", "logistics_tasks"]);
|
|
23
|
+
async function hasErpLeadingArticle(admin) {
|
|
24
|
+
const lead = await admin
|
|
25
|
+
.from("floriday_inventory_settings")
|
|
26
|
+
.select("id", { count: "exact", head: true })
|
|
27
|
+
.eq("erp_leading", true);
|
|
28
|
+
if (lead.error)
|
|
29
|
+
return false; // fail closed
|
|
30
|
+
if ((lead.count ?? 0) > 0)
|
|
31
|
+
return true;
|
|
32
|
+
const legacy = await admin
|
|
33
|
+
.from("floriday_inventory_settings")
|
|
34
|
+
.select("id", { count: "exact", head: true })
|
|
35
|
+
.is("erp_leading", null)
|
|
36
|
+
.neq("stock_mode", "DISABLED");
|
|
37
|
+
if (legacy.error)
|
|
38
|
+
return false;
|
|
39
|
+
return (legacy.count ?? 0) > 0;
|
|
40
|
+
}
|
|
18
41
|
function json(body, status = 200) {
|
|
19
42
|
return new Response(JSON.stringify(body), {
|
|
20
43
|
status,
|
|
@@ -40,7 +63,7 @@ export async function hookEnabled(name) {
|
|
|
40
63
|
.eq("key", `voorraad_hook_enabled.${name}`)
|
|
41
64
|
.maybeSingle();
|
|
42
65
|
if (!data)
|
|
43
|
-
return
|
|
66
|
+
return DEFAULT_ON_IF_ERP_LEADING.has(name) && (await hasErpLeadingArticle(admin));
|
|
44
67
|
return String(data.value).trim().toLowerCase() === "true";
|
|
45
68
|
}
|
|
46
69
|
export async function runGuardedHook(request, name, handle, label) {
|