@open-tender/store
Version:
A library of hooks, reducers, utility functions, and types for use with Open Tender applications that utilize our in-store POS API
177 lines (176 loc) • 7.11 kB
JavaScript
import { __assign } from "tslib";
export var emptyRefund = function (error) {
if (!error || !error.params)
return false;
return Object.values(error.params).includes('Nothing to refund');
};
export var makeRefundCartLookup = function (cart) {
var lookup = {};
cart.forEach(function (item) {
var line_no = item.line_no, quantity = item.quantity, price_total = item.price_total, _a = item.groups, groups = _a === void 0 ? [] : _a;
if (!line_no)
return;
lookup[line_no.toString()] = { quantity: quantity, price_total: price_total };
groups.forEach(function (group) {
return group.options.forEach(function (option) {
var line_no = option.line_no, quantity = option.quantity, price = option.price;
if (!line_no)
return;
lookup[line_no] = { quantity: quantity, price: price };
});
});
});
return lookup;
};
var makeGiftCardAmountsLookup = function (gift_cards) {
return gift_cards.reduce(function (obj, i) {
var _a;
if (!i.id)
return obj;
return __assign(__assign({}, obj), (_a = {}, _a[i.id] = i.amount, _a));
}, {});
};
var makeSurchargeAmountsLookup = function (surcharges) {
return surcharges.reduce(function (obj, i) {
var _a;
return (__assign(__assign({}, obj), (_a = {}, _a[i.id] = i.amount, _a)));
}, {});
};
var makeDiscountsAmountsLookup = function (discounts) {
return discounts.reduce(function (obj, i) {
var _a;
return (__assign(__assign({}, obj), (_a = {}, _a[i.id] = i.amount, _a)));
}, {});
};
var makeTaxesAmountsLookup = function (taxes) {
return taxes.reduce(function (obj, i) {
var _a;
return (__assign(__assign({}, obj), (_a = {}, _a[i.id] = i.amount, _a)));
}, {});
};
var makeTendersAmountsLookup = function (tenders) {
return tenders.reduce(function (obj, i, idx) {
var _a;
return (__assign(__assign({}, obj), (_a = {}, _a[i.tender_index || idx] = i.amount, _a)));
}, {});
};
export var makeRefundLookup = function (order) {
var cart = order.cart, gift_cards = order.gift_cards, surcharges = order.surcharges, discounts = order.discounts, taxes = order.taxes, totals = order.totals, tenders = order.tenders;
var subtotal = totals.subtotal, gift_card = totals.gift_card, surcharge = totals.surcharge, discount = totals.discount, tax = totals.tax, tip = totals.tip, shipping = totals.shipping, total = totals.total;
return {
cart: makeRefundCartLookup(cart),
gift_cards: gift_cards ? makeGiftCardAmountsLookup(gift_cards) : {},
surcharges: makeSurchargeAmountsLookup(surcharges),
discounts: makeDiscountsAmountsLookup(discounts),
taxes: makeTaxesAmountsLookup(taxes),
tenders: makeTendersAmountsLookup(tenders),
subtotal: subtotal,
gift_card: gift_card,
surcharge: surcharge,
discount: discount,
tax: tax,
tip: tip,
shipping: shipping,
total: total
};
};
var adjustAmount = function (amount, refundAmount) {
return (parseFloat(amount) + parseFloat(refundAmount)).toFixed(2);
};
var adjustAmountNegative = function (amount, refundAmount) {
return (parseFloat(amount) + parseFloat(refundAmount)).toFixed(2);
};
// export const adjustAmounts = (
// order: Order,
// lookup: Record<string, Record<string, Money> | Record<string, unknown>>,
// list: 'gift_cards' | 'surcharges' | 'discounts' | 'taxes' | 'tenders',
// key: 'id' | 'code' = 'id'
// ) => {
// if (!list || !order[list]) return order
// order[list].forEach(i => {
// const amount = lookup[list][i[key]]
// if (amount) i.amount = adjustAmount(i.amount, amount)
// })
// return order
// }
// const amounts = [
// 'subtotal',
// 'gift_card',
// 'surcharge',
// 'discount',
// 'tax',
// 'tip',
// 'shipping',
// 'total'
// ]
export var makeNetOrder = function (order, refunds) {
if (!refunds || !refunds.length)
return order;
var copy = JSON.parse(JSON.stringify(order));
refunds.forEach(function (refund) {
var _a;
var lookup = makeRefundLookup(refund);
// adjust cart
copy.cart.forEach(function (i) {
if (!i.line_no)
return;
var item = lookup.cart[i.line_no];
if (item) {
i.quantity -= item.quantity;
i.price_total = adjustAmount(i.price_total, item.price_total);
}
if (i.quantity === 0 && i.groups) {
i.groups.forEach(function (group) {
return group.options.forEach(function (option) {
option.quantity = 0;
option.price_total = '0.00';
});
});
}
});
// adjust arrays
// copy = adjustAmounts(copy, lookup, 'gift_cards', 'code')
// copy = adjustAmounts(copy, lookup, 'surcharges')
// copy = adjustAmounts(copy, lookup, 'discounts')
// copy = adjustAmounts(copy, lookup, 'taxes')
// copy = adjustAmounts(copy, lookup, 'tenders', 'tender_index')
(_a = copy.gift_cards) === null || _a === void 0 ? void 0 : _a.forEach(function (i) {
var amount = i.id ? lookup.gift_cards[i.id] : null;
if (amount)
i.amount = adjustAmount(i.amount, amount);
});
copy.surcharges.forEach(function (i) {
var amount = lookup.surcharges[i.id];
if (amount)
i.amount = adjustAmount(i.amount, amount);
});
copy.discounts.forEach(function (i) {
var amount = lookup.discounts[i.id];
if (amount && i.amount)
i.amount = adjustAmount(i.amount, amount);
});
copy.taxes.forEach(function (i) {
var amount = lookup.taxes[i.id];
if (amount)
i.amount = adjustAmount(i.amount, amount);
});
copy.tenders.forEach(function (i, idx) {
var amount = lookup.tenders[i.tender_index || idx];
if (amount)
i.amount = adjustAmount(i.amount, amount);
});
// adjust totals
// amounts.forEach(i => {
// copy.totals[i] = adjustAmount(copy.totals[i], lookup[i])
// })
copy.totals.subtotal = adjustAmount(copy.totals.subtotal, lookup.subtotal);
copy.totals.gift_card = adjustAmount(copy.totals.gift_card, lookup.gift_card);
copy.totals.surcharge = adjustAmount(copy.totals.surcharge, lookup.surcharge);
copy.totals.discount = adjustAmountNegative(copy.totals.discount, lookup.discount);
copy.totals.tax = adjustAmount(copy.totals.tax, lookup.tax);
copy.totals.tip = adjustAmount(copy.totals.tip, lookup.tip);
copy.totals.shipping = adjustAmount(copy.totals.shipping, lookup.shipping);
copy.totals.total = adjustAmount(copy.totals.total, lookup.total);
});
return copy;
};