UNPKG

beerbay-math

Version:

121 lines (100 loc) 3.79 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getShippingFinal = exports.getShippingGross = exports.packFreeQuantity = exports.packQuantity = void 0; var _cart = require("./cart"); /** * @method packQuantity * @description Given cart and selected country, return the amount of packages needed * @param {any[]} cart * @param {any} selectedCountry */ var packQuantity = function packQuantity(cart, selectedCountry) { var packsNum = (0, _cart.getCartQuantity)(cart) / 24; if (selectedCountry && selectedCountry.max_shipping) { packsNum = (0, _cart.getCartQuantity)(cart) / selectedCountry.max_shipping; } return Math.ceil(packsNum); }; /** * @method packFreeQuantity * @description Given a cart and selected country calculates the amount of packs with free shipping * @param {any[]} cart * @param {any} selectedCountry */ exports.packQuantity = packQuantity; var packFreeQuantity = function packFreeQuantity(cart, selectedCountry) { if (selectedCountry && selectedCountry.free_shipping) { var freeDiv = (0, _cart.getCartPrice)(cart) / selectedCountry.free_shipping; var freePacks = Math.floor(freeDiv); var PackQ = packQuantity(cart, selectedCountry); if (freePacks > PackQ) { return PackQ; } else { return freePacks; } } }; exports.packFreeQuantity = packFreeQuantity; var itemsEnTramo = function itemsEnTramo(cart, selectedCountry) { if (selectedCountry && selectedCountry.max_shipping) { return (0, _cart.getCartQuantity)(cart) - (packQuantity(cart, selectedCountry) - 1) * selectedCountry.max_shipping; } }; var tramoActual = function tramoActual(cart, selectedCountry) { if (selectedCountry && selectedCountry.limites_tramo) { for (var i in selectedCountry.limites_tramo) { if (selectedCountry.limites_tramo && selectedCountry.limites_tramo.hasOwnProperty(i)) { if (itemsEnTramo(cart, selectedCountry) >= selectedCountry.limites_tramo[i][0] && itemsEnTramo(cart, selectedCountry) <= selectedCountry.limites_tramo[i][1]) { return selectedCountry.shipping[i]; } } } } }; var tramoMayor = function tramoMayor(cart, selectedCountry) { if (selectedCountry && selectedCountry.shipping) { var tramos = []; for (var i in selectedCountry.shipping) { if (selectedCountry.shipping.hasOwnProperty(i)) { tramos.push(selectedCountry.shipping[i]); } } return Math.max.apply(Math, tramos); } }; /** * @method getShippingGross * @descsription Given a cart and selectedCoutnry calculate gross shipping costs * @param {any[]} cart * @param {any} selectedCountry */ var getShippingGross = function getShippingGross(cart, selectedCountry) { if (packQuantity(cart, selectedCountry) <= 1) { return tramoActual(cart, selectedCountry); } else { return tramoActual(cart, selectedCountry) + (packQuantity(cart, selectedCountry) - 1) * tramoMayor(cart, selectedCountry); } }; /** * @method getShippingFinal * @description Given a cart and selected country, calculates total shipping cost * @param {any[]} cart * @param {any} selectedCountry */ exports.getShippingGross = getShippingGross; var getShippingFinal = function getShippingFinal(cart, selectedCountry) { if (packQuantity(cart, selectedCountry) <= 1) { if (packFreeQuantity(cart, selectedCountry) == 1) { return 0; } else return tramoActual(cart, selectedCountry); } else { if (packFreeQuantity(cart, selectedCountry) >= packQuantity(cart, selectedCountry)) { return 0; } else { return getShippingGross(cart, selectedCountry) - packFreeQuantity(cart, selectedCountry) * tramoMayor(cart, selectedCountry); } } }; exports.getShippingFinal = getShippingFinal;