UNPKG

@posguru/finance-core

Version:

Universal JavaScript library for finance-related math functions, usable in both Node.js (Express) and React applications.

2 lines (1 loc) 13 kB
(function(l,P){typeof exports=="object"&&typeof module<"u"?P(exports):typeof define=="function"&&define.amd?define(["exports"],P):(l=typeof globalThis<"u"?globalThis:l||self,P(l.FinanceCore={}))})(this,function(l){"use strict";function P(t,r,a){var s,p;let n=0,o=0,i=0,u=(((s=t.items)==null?void 0:s.reduce((e,E)=>E.id&&E.id>e?E.id:e,0))||0)+1;if((p=t.items)==null||p.forEach((e,E)=>{var O,_;const v=r[e.productId];if(!v)return;e.id||(e.id=u++);const T=Number(v.salesPrice)*Number(e.quantity);e.amount=Number(v.salesPrice),e.totalAmount=T,e.productId=v.id,e.totalCost=T;let W=(((O=e.addons)==null?void 0:O.reduce((f,b)=>b.id&&b.id>f?b.id:f,0))||0)+1;if((_=e.addons)==null||_.forEach((f,b)=>{var j;const g=(j=v.addonItems)==null?void 0:j[f.addonItemId];g&&(f!=null&&f.id||(f.id=W++),f.orderItemsId=e.id,f.totalAmount=g.amount*f.quantity,e.totalCost+=f.totalAmount)}),i=i+e.totalCost,e!=null&&e.discountId){const f=a[e==null?void 0:e.discountId];let b=0;f.discountType==="PERCENT"?b=Number((e.totalCost*(Number(f.discount)/100)).toFixed(2)):e.totalCost>=Number(f.discount)?b=e.totalCost-Number(f.discount):(e.discountId=null,e.discount=null),o=o+b,e.discountAmount=b,e.totalCost=e.totalCost-b}n+=e.totalCost}),t.subTotal=n,t.totalAmount=i,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 y={PARTNER:"PARTNER",USER:"USER",LOCATION:"LOCATION",DEVICE:"DEVICE",VALUES:["PARTNER","USER","LOCATION","DEVICE"]},C={CLOUDE_KITCHEN:"CLOUDE_KITCHEN",RESTAURANT:"RESTAURANT",VALUES:["CLOUDE_KITCHEN","RESTAURANT"]},R={PRIVATE:"PRIVATE",STYLE:"STYLE",APPLICATION:"APPLICATION"},c=t=>typeof t=="string"?t.toLowerCase()==="true":!!t,A={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=>c(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=>c(t),override:!0},SERVICE_TYPES:{attribute:"service_types",value:[],jsonProperty:"serviceTypes",format:t=>Array.isArray(t)?t:[],override:!0}},S={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=>c(t),override:!0},IS_DELETED:{attribute:"is_deleted",value:!1,jsonProperty:"isDeleted",format:t=>c(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=>c(t)}},I={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=>c(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=>c(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=>c(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}},d={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=>c(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=>c(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)}},D={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=>c(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=>c(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=>c(t)}},L={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"]},m=(t,r,a=!1,n=!0)=>{const o={};return Object.values(t).forEach(i=>{let u=null;r[i.attribute]?u=i.format(r[i.attribute].value):a&&(u=i.value),n?o[i.jsonProperty]=u:o[i.jsonProperty]={...i,value:u}}),o},h=Object.values(d).map(t=>t.jsonProperty),U=Object.values(d).map(t=>t.attribute);async function M(t,r,a,n){const o={};for(const s of t)o[s.id]=s;o[r]={id:r,type:y.PARTNER,parent:null},await G(o,a,n);const i=w(o),u=Y(o);return N(u,o,i),K(o,i),V(o),delete o[r],o}const B=async(t,r,a)=>{const n=await r(t),o={};for(const s of n){const p=s.partnerId;let e=o[p];e||(e={},o[p]=e),e[s.name]=s}const i=new Set;for(const s of Object.keys(o)){const p=o[s];for(const e of Object.values(p))i.add(e)}const u=await a(t);for(const s of u){const p=o[s==null?void 0:s.partnerId];if(!p)continue;const e=p[s==null?void 0:s.profileName];e&&(e.attributes||(e.attributes={}),e.attributes[s.name]=s)}return i},w=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},Y=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},G=async(t,r,a)=>{const n=Object.keys(t),o=await B(n,r,a);for(const i of o){const u=t[i.partnerId],s=F(i,u);u.privateProfile=s}for(const i of Object.keys(t)){const u=t[i];u!=null&&u.privateProfile}},V=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)||{},i={};for(const u of Object.keys(o)){const s=o[u];i[u]=s.value??s.overriden}n.attributes={...i}}else r.push(a.id),console.warn("no user assigned this partner",JSON.stringify(a))}for(const a of r)delete t[a]},K=(t,r)=>{var n,o,i;const a=new Set;for(const u of Object.values(t))if((i=(o=(n=u.privateProfile)==null?void 0:n.attributes)==null?void 0:o.isDeleted)!=null&&i.value&&(a.add(u.id),r[u.id]))for(const s of r[u.id])a.add(s);for(const u of a)delete t[u]},F=(t,r)=>{const a={name:t.name,partnerId:r.id,type:t.type,attributes:t.attributes};let n={};r.type===y.PARTNER?n=m(d,t.attributes,!1,!1):r.type===y.LOCATION?n=m(I,t.attributes,!1,!1):r.type===y.DEVICE?n=m(A,t.attributes,!1,!1):r.type===y.USER&&(n=m(S,t.attributes,!1,!1));for(const o of Object.values(n))o.owner=r.id,o.profile=a.name;return a.attributes=n,a},k=(t,r)=>{if(!t||!r)return;const a=t.attributes||{},n=r.attributes||{};for(const o of Object.keys(a)){const i=a[o],u=n[o];u&&(u&&i&&u.value===null&&u.override===!0&&i.value?n[o]={...i}:n[o]={...u})}r.attributes=n},H=(t,r)=>{k(t,r)},J=(t,r)=>{const a=t==null?void 0:t.parent;if(!a)return;const o=r[a].privateProfile||null,i=t.privateProfile||null;i&&o&&H(o,i),t.privateProfile=i||void 0},N=(t,r,a)=>{J(t,r);const n=a[t.id];if(n)for(const o of n)N(r[o],r,a)};l.AdminProfileAttributes=D,l.DeviceProfileAttributes=A,l.LocationProfileAttributes=I,l.PartnerProfileAttributes=d,l.PartnerProfileAttributesDbKeys=U,l.PartnerProfileAttributesJsonKeys=h,l.UserProfileAttributes=S,l.applyInvoice=P,l.deserializeProfileAttribute=m,l.generateProfile=M,l.partnerModes=C,l.partnerTypes=y,l.profileTypes=R,l.serviceTypes=L,Object.defineProperty(l,Symbol.toStringTag,{value:"Module"})});