@posguru/finance-core
Version:
Universal JavaScript library for finance-related math functions, usable in both Node.js (Express) and React applications.
708 lines (707 loc) • 17.4 kB
JavaScript
function Y(t, r, a) {
var i, c;
let n = 0, o = 0, s = 0, u = (((i = t.items) == null ? void 0 : i.reduce((e, y) => y.id && y.id > e ? y.id : e, 0)) || 0) + 1;
if ((c = t.items) == null || c.forEach((e, y) => {
var d, A;
const b = r[e.productId];
if (!b) return;
e.id || (e.id = u++);
const P = Number(b.salesPrice) * Number(e.quantity);
e.amount = Number(b.salesPrice), e.totalAmount = P, e.productId = b.id, e.totalCost = P;
let T = (((d = e.addons) == null ? void 0 : d.reduce((l, p) => p.id && p.id > l ? p.id : l, 0)) || 0) + 1;
if ((A = e.addons) == null || A.forEach((l, p) => {
var I;
const S = (I = b.addonItems) == null ? void 0 : I[l.addonItemId];
S && (l != null && l.id || (l.id = T++), l.orderItemsId = e.id, l.totalAmount = S.amount * l.quantity, e.totalCost += l.totalAmount);
}), s = s + e.totalCost, e != null && e.discountId) {
const l = a[e == null ? void 0 : e.discountId];
let p = 0;
l.discountType === "PERCENT" ? p = Number((e.totalCost * (Number(l.discount) / 100)).toFixed(2)) : e.totalCost >= Number(l.discount) ? p = e.totalCost - Number(l.discount) : (e.discountId = null, e.discount = null), o = o + p, e.discountAmount = p, e.totalCost = e.totalCost - p;
}
n += e.totalCost;
}), t.subTotal = n, t.totalAmount = s, t.totalDiscount = o, t.billAmount = n, Number(t == null ? void 0 : t.carryBagQuantity) && Number(t.carryBagFee) && (t.billAmount = Number(t.billAmount) + (Number(t == null ? void 0 : t.carryBagQuantity) && Number(t.carryBagFee))), t != null && t.serviceChargePercent && Number(t.serviceChargePercent)) {
const e = Number(n) * (Number(t.serviceChargePercent) / 100);
t.billAmount = Number(t.billAmount) + e, t.serviceCharge = e;
}
if ((t == null ? void 0 : t.serviceType) === "DELIVERY" && (t != null && t.deliveryChargeId) && (t != null && t.deliveryCharge)) {
const e = Number(t == null ? void 0 : t.deliveryCharge);
t.billAmount = Number(t.billAmount) + e;
}
return t;
}
const m = {
PARTNER: "PARTNER",
USER: "USER",
LOCATION: "LOCATION",
DEVICE: "DEVICE",
VALUES: ["PARTNER", "USER", "LOCATION", "DEVICE"]
}, G = {
CLOUDE_KITCHEN: "CLOUDE_KITCHEN",
RESTAURANT: "RESTAURANT",
VALUES: ["CLOUDE_KITCHEN", "RESTAURANT"]
}, V = {
PRIVATE: "PRIVATE",
STYLE: "STYLE",
APPLICATION: "APPLICATION"
}, f = (t) => typeof t == "string" ? t.toLowerCase() === "true" : !!t, _ = {
NAME: {
attribute: "name",
value: null,
jsonProperty: "name",
format: (t) => String(t)
},
LANGUAGES: {
attribute: "languages",
value: ["en-"],
jsonProperty: "languages",
format: (t) => JSON.parse(t),
override: !0
},
STATUS: {
attribute: "status",
value: !0,
jsonProperty: "status",
format: (t) => f(t),
override: !0
},
CURRENCY: {
attribute: "currency",
value: "GBP",
jsonProperty: "currency",
format: (t) => String(t),
override: !0
},
MINIMUM_CASH_ORDER: {
attribute: "minimum_cash_order",
value: 0,
jsonProperty: "minimumCashOrder",
format: (t) => Number(t),
override: !0
},
CARRY_BAG_FEE: {
attribute: "carry_bag_fee",
value: 0,
jsonProperty: "carryBagFee",
format: (t) => Number(t),
override: !0
},
VAT: {
attribute: "vat",
value: 0,
jsonProperty: "vat",
format: (t) => Number(t),
override: !0
},
TIME_ZONE: {
attribute: "time_zone",
value: null,
jsonProperty: "timeZone",
format: (t) => String(t),
override: !0
},
IS_DELETED: {
attribute: "is_deleted",
value: !1,
jsonProperty: "isDeleted",
format: (t) => f(t),
override: !0
},
SERVICE_TYPES: {
attribute: "service_types",
value: [],
// ✅ fixed instead of false
jsonProperty: "serviceTypes",
format: (t) => Array.isArray(t) ? t : [],
override: !0
}
}, O = {
PASSWORD: {
attribute: "password",
value: null,
jsonProperty: "password",
format: (t) => String(t)
},
EMAIL: {
attribute: "email",
value: null,
jsonProperty: "email",
format: (t) => String(t)
},
NAME: {
attribute: "name",
value: null,
jsonProperty: "name",
format: (t) => String(t)
},
PHONE: {
attribute: "phone",
value: null,
jsonProperty: "phone",
format: (t) => String(t)
},
PERMISSIONS: {
attribute: "permissions",
value: [],
jsonProperty: "permissions",
format: (t) => Array.isArray(t) ? t : []
},
STATUS: {
attribute: "status",
value: !0,
jsonProperty: "status",
format: (t) => f(t),
override: !0
},
IS_DELETED: {
attribute: "is_deleted",
value: !1,
jsonProperty: "isDeleted",
format: (t) => f(t),
override: !0
},
FP_TOKEN: {
attribute: "fp_token",
value: null,
jsonProperty: "fpToken",
format: (t) => String(t)
},
FP_DURATION: {
attribute: "fp_duration",
value: null,
jsonProperty: "fpDuration",
format: (t) => t ? new Date(t) : null
},
IS_OWNER: {
attribute: "is_owner",
value: !1,
jsonProperty: "isOwner",
format: (t) => f(t)
}
}, j = {
NAME: {
attribute: "name",
value: null,
jsonProperty: "name",
format: (t) => String(t)
},
EMAIL: {
attribute: "email",
value: null,
jsonProperty: "email",
format: (t) => String(t)
},
LANGUAGES: {
attribute: "languages",
value: ["en-US"],
jsonProperty: "languages",
format: (t) => JSON.parse(t),
override: !0
},
PHONE: {
attribute: "phone",
value: null,
jsonProperty: "phone",
format: (t) => String(t)
},
COUNTRY: {
attribute: "country",
value: null,
jsonProperty: "country",
format: (t) => String(t)
},
ADDRESS: {
attribute: "address",
value: null,
jsonProperty: "address",
format: (t) => String(t)
},
DESCRIPTION: {
attribute: "description",
value: null,
jsonProperty: "description",
format: (t) => String(t)
},
POSTCODE: {
attribute: "postcode",
value: null,
jsonProperty: "postcode",
format: (t) => String(t)
},
STATUS: {
attribute: "status",
value: !0,
jsonProperty: "status",
format: (t) => f(t),
override: !0
},
CURRENCY: {
attribute: "currency",
value: "GBP",
jsonProperty: "currency",
format: (t) => String(t),
override: !0
},
MINIMUM_CASH_ORDER: {
attribute: "minimum_cash_order",
value: 0,
jsonProperty: "minimumCashOrder",
format: (t) => Number(t),
override: !0
},
CARRY_BAG_FEE: {
attribute: "carry_bag_fee",
value: 0,
jsonProperty: "carryBagFee",
format: (t) => Number(t),
override: !0
},
VAT: {
attribute: "vat",
value: 0,
jsonProperty: "vat",
format: (t) => Number(t),
override: !0
},
TIME_ZONE: {
attribute: "time_zone",
value: null,
jsonProperty: "timeZone",
format: (t) => String(t),
override: !0
},
IS_DELETED: {
attribute: "is_deleted",
value: !1,
jsonProperty: "isDeleted",
format: (t) => f(t),
override: !0
},
SERVICE_TYPES: {
attribute: "service_types",
value: [],
jsonProperty: "serviceTypes",
format: (t) => Array.isArray(t) ? t : [],
override: !0
},
START_TOKEN_NUMBER: {
attribute: "start_token_number",
value: !1,
jsonProperty: "startTokenNumber",
format: (t) => f(t),
override: !0
},
LATITUDE: {
attribute: "latitude",
value: null,
jsonProperty: "latitude",
format: (t) => String(t),
override: !0
},
LONGITUDE: {
attribute: "longitude",
value: null,
jsonProperty: "longitude",
format: (t) => String(t),
override: !0
}
}, E = {
NAME: {
attribute: "name",
value: null,
jsonProperty: "name",
format: (t) => String(t)
},
EMAIL: {
attribute: "email",
value: null,
jsonProperty: "email",
format: (t) => String(t)
},
TYPE: {
attribute: "type",
value: null,
jsonProperty: "type",
format: (t) => String(t)
},
LANGUAGES: {
attribute: "languages",
value: ["en-US"],
jsonProperty: "languages",
format: (t) => JSON.parse(t),
cast: (t) => JSON.stringify(t),
override: !0
},
PHONE: {
attribute: "phone",
value: null,
jsonProperty: "phone",
format: (t) => String(t)
},
ORDER_TAB_ACCESS_TOKEN: {
attribute: "order_tab_access_token",
value: null,
jsonProperty: "orderTabAccessToken",
format: (t) => String(t)
},
MAX_ALLOWED_DEVICES: {
attribute: "max_allowed_devices",
value: null,
jsonProperty: "maxAllowedDevices",
format: (t) => Number(t)
},
STATUS: {
attribute: "status",
value: !0,
jsonProperty: "status",
format: (t) => f(t)
},
DOMAIN: {
attribute: "domain",
value: null,
jsonProperty: "domain",
format: (t) => String(t)
},
CITY: {
attribute: "city",
value: "",
jsonProperty: "city",
format: (t) => String(t)
},
COUNTRY: {
attribute: "country",
value: null,
jsonProperty: "country",
format: (t) => String(t)
},
POSTCODE: {
attribute: "postcode",
value: null,
jsonProperty: "postcode",
format: (t) => String(t)
},
CURRENCY: {
attribute: "currency",
value: "GBP",
jsonProperty: "currency",
format: (t) => String(t),
override: !0
},
SUBSCRIPTION: {
attribute: "subscription",
value: "BASIC",
jsonProperty: "subscription",
format: (t) => String(t)
},
LOGO_URL: {
attribute: "logo_url",
value: null,
jsonProperty: "logoUrl",
format: (t) => String(t),
override: !0
},
MINIMUM_CASH_ORDER: {
attribute: "minimum_cash_order",
value: 0,
jsonProperty: "minimumCashOrder",
format: (t) => Number(t),
override: !0
},
CARRY_BAG_FEE: {
attribute: "carry_bag_fee",
value: 0,
jsonProperty: "carryBagFee",
format: (t) => Number(t)
},
VAT: {
attribute: "vat",
value: 0,
jsonProperty: "vat",
format: (t) => Number(t),
override: !0
},
CERTIFICATION_DETAILS: {
attribute: "certification_details",
value: null,
jsonProperty: "certificationDetails",
format: (t) => String(t)
},
PARTNER_TYPE: {
attribute: "partner_type",
value: "",
jsonProperty: "partnerType",
format: (t) => String(t)
},
TIME_ZONE: {
attribute: "time_zone",
value: null,
jsonProperty: "timeZone",
format: (t) => String(t),
override: !0
},
IS_DELETED: {
attribute: "is_deleted",
value: !1,
jsonProperty: "isDeleted",
format: (t) => f(t),
override: !0
},
ADDRESS: {
attribute: "address",
value: null,
jsonProperty: "address",
format: (t) => String(t)
},
SERVICE_TYPES: {
attribute: "service_types",
value: [],
jsonProperty: "serviceTypes",
format: (t) => JSON.parse(t),
cast: (t) => JSON.stringify(t),
override: !0
},
SERVICE_CHARGES: {
attribute: "service_charges",
value: [],
jsonProperty: "serviceCharges",
format: (t) => JSON.parse(t),
cast: (t) => JSON.stringify(t),
override: !0
},
LATITUDE: {
attribute: "latitude",
value: null,
jsonProperty: "latitude",
format: (t) => String(t)
},
LONGITUDE: {
attribute: "longitude",
value: null,
jsonProperty: "longitude",
format: (t) => String(t)
}
}, K = {
PASSWORD: {
attribute: "password",
value: null,
jsonProperty: "password",
format: (t) => String(t)
},
EMAIL: {
attribute: "email",
value: null,
jsonProperty: "email",
format: (t) => String(t)
},
NAME: {
attribute: "name",
value: null,
jsonProperty: "name",
format: (t) => String(t)
},
PHONE: {
attribute: "phone",
value: null,
jsonProperty: "phone",
format: (t) => String(t)
},
PERMISSIONS: {
attribute: "permissions",
value: [],
jsonProperty: "permissions",
format: (t) => Array.isArray(t) ? t : []
},
STATUS: {
attribute: "status",
value: !0,
jsonProperty: "status",
format: (t) => f(t)
},
CREATED_AT: {
attribute: "created_at",
value: null,
jsonProperty: "createdAt",
format: (t) => t ? new Date(t) : null
},
IS_DELETED: {
attribute: "is_deleted",
value: !1,
jsonProperty: "isDeleted",
format: (t) => f(t)
},
FP_TOKEN: {
attribute: "fp_token",
value: null,
jsonProperty: "fpToken",
format: (t) => String(t)
},
FP_DURATION: {
attribute: "fp_duration",
value: null,
jsonProperty: "fpDuration",
format: (t) => t ? new Date(t) : null
},
IS_ADMIN: {
attribute: "is_admin",
value: !1,
jsonProperty: "isAdmin",
format: (t) => f(t)
}
}, F = {
TAKE_AWAY: "TAKE_AWAY",
DELIVERY: "DELIVERY",
DINE_IN: "DINE_IN",
WEBSITE_BOOKING: "WEBSITE_BOOKING",
COLLECTION: "COLLECTION",
TABLE_BOOKING: "TABLE_BOOKING",
VALUES: [
"TAKE_AWAY",
"DELIVERY",
"DINE_IN",
"COLLECTION",
"TABLE_BOOKING",
"WEBSITE_BOOKING"
]
}, v = (t, r, a = !1, n = !0) => {
const o = {};
return Object.values(t).forEach((s) => {
let u = null;
r[s.attribute] ? u = s.format(r[s.attribute].value) : a && (u = s.value), n ? o[s.jsonProperty] = u : o[s.jsonProperty] = { ...s, value: u };
}), o;
}, k = Object.values(
E
).map((t) => t.jsonProperty), H = Object.values(
E
).map((t) => t.attribute);
async function J(t, r, a, n) {
const o = {};
for (const i of t)
o[i.id] = i;
o[r] = {
id: r,
type: m.PARTNER,
parent: null
}, await D(o, a, n);
const s = C(o), u = R(o);
return N(u, o, s), U(o, s), L(o), delete o[r], o;
}
const g = async (t, r, a) => {
const n = await r(t), o = {};
for (const i of n) {
const c = i.partnerId;
let e = o[c];
e || (e = {}, o[c] = e), e[i.name] = i;
}
const s = /* @__PURE__ */ new Set();
for (const i of Object.keys(o)) {
const c = o[i];
for (const e of Object.values(c))
s.add(e);
}
const u = await a(t);
for (const i of u) {
const c = o[i == null ? void 0 : i.partnerId];
if (!c) continue;
const e = c[i == null ? void 0 : i.profileName];
e && (e.attributes || (e.attributes = {}), e.attributes[i.name] = i);
}
return s;
}, C = (t) => {
const r = {};
for (const a of Object.values(t)) {
const n = a != null && a.parent ? t[a.parent] : null;
n && (r[n.id] || (r[n.id] = []), r[n.id].push(a.id));
}
return r;
}, R = (t) => {
let r = null;
for (const a of Object.values(t))
if (!a.parent)
if (!r) r = a;
else throw new Error("find root faild: more than one root parent found");
if (!r) throw new Error("No root partner found");
return r;
}, D = async (t, r, a) => {
const n = Object.keys(t), o = await g(n, r, a);
for (const s of o) {
const u = t[s.partnerId], i = h(s, u);
u.privateProfile = i;
}
for (const s of Object.keys(t)) {
const u = t[s];
u != null && u.privateProfile;
}
}, L = (t) => {
const r = [];
for (const a of Object.values(t)) {
const n = a == null ? void 0 : a.privateProfile;
if (n) {
const o = (n == null ? void 0 : n.attributes) || {}, s = {};
for (const u of Object.keys(o)) {
const i = o[u];
s[u] = i.value ?? i.overriden;
}
n.attributes = { ...s };
} else
r.push(a.id), console.warn("no user assigned this partner", JSON.stringify(a));
}
for (const a of r)
delete t[a];
}, U = (t, r) => {
var n, o, s;
const a = /* @__PURE__ */ new Set();
for (const u of Object.values(t))
if ((s = (o = (n = u.privateProfile) == null ? void 0 : n.attributes) == null ? void 0 : o.isDeleted) != null && s.value && (a.add(u.id), r[u.id]))
for (const i of r[u.id])
a.add(i);
for (const u of a)
delete t[u];
}, h = (t, r) => {
const a = {
name: t.name,
partnerId: r.id,
type: t.type,
attributes: t.attributes
};
let n = {};
r.type === m.PARTNER ? n = v(E, t.attributes, !1, !1) : r.type === m.LOCATION ? n = v(j, t.attributes, !1, !1) : r.type === m.DEVICE ? n = v(_, t.attributes, !1, !1) : r.type === m.USER && (n = v(O, t.attributes, !1, !1));
for (const o of Object.values(n))
o.owner = r.id, o.profile = a.name;
return a.attributes = n, a;
}, M = (t, r) => {
if (!t || !r) return;
const a = t.attributes || {}, n = r.attributes || {};
for (const o of Object.keys(a)) {
const s = a[o], u = n[o];
u && (u && s && u.value === null && u.override === !0 && s.value ? n[o] = { ...s } : n[o] = { ...u });
}
r.attributes = n;
}, B = (t, r) => {
M(t, r);
}, w = (t, r) => {
const a = t == null ? void 0 : t.parent;
if (!a) return;
const o = r[a].privateProfile || null, s = t.privateProfile || null;
s && o && B(o, s), t.privateProfile = s || void 0;
}, N = (t, r, a) => {
w(t, r);
const n = a[t.id];
if (n)
for (const o of n)
N(r[o], r, a);
};
export {
K as AdminProfileAttributes,
_ as DeviceProfileAttributes,
j as LocationProfileAttributes,
E as PartnerProfileAttributes,
H as PartnerProfileAttributesDbKeys,
k as PartnerProfileAttributesJsonKeys,
O as UserProfileAttributes,
Y as applyInvoice,
v as deserializeProfileAttribute,
J as generateProfile,
G as partnerModes,
m as partnerTypes,
V as profileTypes,
F as serviceTypes
};