"use strict"; var Product = function Product(product) { this.product = product; }; let warningCount = 0; var CartViewModel = { cart: ko.observable({}), subtotal: ko.observable(0), envio: ko.observable(0), total: ko.observable(0), discount: ko.observable(0), errorMessage: ko.observable(''), envioGratis: ko.observable(0), orderFormId: null, hasError: ko.observable(false), valorMinimoEnvioGratis: ko.observable(), porcentajeEnvioGratis: ko.observable(0), resetCart: function resetCart() { this.cart.removeAll(); }, setCart: function setCart(order) { this.cart(order); this.setShipping(); }, setShipping: function setShipping() { var findShipping = this.cart().totalizers.find(function (total) { return total.id == "Shipping"; }); if (findShipping && findShipping.value != 0) { this.envio(removeLastTwoDigits(findShipping.value)); } }, addToCart: function addToCart(p) { this.cart.items.push(p); }, calculateSubtotal: function calculateSubtotal() { var subtotal = 0; this.cart().items.forEach(function (i) { subtotal += i.price * i.quantity(); }); this.subtotal(this.cart().value === 0 ? 0 : removeLastTwoDigits(subtotal)); }, calculateTotal: function calculateTotal() { var val = 0; var findDiscount = this.cart().totalizers.find(function (total) { return total.id == "Discounts"; }); this.cart().items.forEach(function (item) { val += item.price * item.quantity(); }); if (findDiscount) { val += findDiscount.value; } this.total(removeLastTwoDigits(val)); updateTopBar(val); }, calculateDiscount: function calculateDiscount() { var findDiscount = this.cart().totalizers.find(function (type) { return type.id == "Discounts"; }); if (findDiscount) { this.discount(removeLastTwoDigits(findDiscount.value)); } else { this.discount(0); } }, calculateFreeShipping: function calculateFreeShipping() { var minValueFreeShipping = localStorage.getItem('minValueFreeShipping'); if (minValueFreeShipping) { this.valorMinimoEnvioGratis(minValueFreeShipping); } else { this.valorMinimoEnvioGratis(0); } var restante = minValueFreeShipping - this.total(); this.envioGratis(restante); }, calculatePercentShipping: function calculatePercentShipping() { var p = this.total() / this.valorMinimoEnvioGratis() * 100; this.porcentajeEnvioGratis(p <= 100 ? p : 100); }, minimumPrice: function minimumPrice() { let Total = this.total(); if (Total <= 40000) { if (warningCount === 0) { $('#cart-resume header').after(`
¡Estás a un paso de finalizar tu compra!

Recuerda que necesitas un mínimo de $40.000 en el carrito para proceder al checkout.

`); $('.pay-btn').addClass('not-success'); warningCount++; } } else { if (warningCount > 0) { $('.container-minimun-price').remove(); $('.pay-btn').removeClass('not-success'); warningCount = 0; } } } }; function removeLastTwoDigits(number) { if (number != null) { number = number.toString(); number = number.slice(0, -2); return parseInt(number); } else { return 0; } } function calculatePricingCart() { CartViewModel.calculateSubtotal(); CartViewModel.calculateTotal(); CartViewModel.calculateFreeShipping(); CartViewModel.calculatePercentShipping(); CartViewModel.calculateDiscount(); CartViewModel.minimumPrice(); } ko.applyBindings(CartViewModel, document.getElementById('cart-resume')); function setQuantityProductCart(p) { calculatePricingCart(); updateCart(itemUpdate(p)); } function itemUpdate(p, remove) { var searchIndex = CartViewModel.cart().items.findIndex(function (item) { return item.id === p.id; }); return [{ hasBundleItems: false, id: p.id, index: searchIndex, quantity: remove ? 0 : p.quantity(), seller: p.seller }]; } function incrementQuantityProductCart(p, e) { e.target.parentElement.parentElement.innerHTML = '
'; var previousCount = parseInt(p.quantity()); p.quantity(previousCount + 1); calculatePricingCart(); updateCart(itemUpdate(p)).then(function (res) { console.log(res); }); } function decrementQuantityProductCart(p, e) { var previousCount = parseInt(p.quantity()); if (previousCount > 1) { e.target.parentElement.parentElement.innerHTML = '
'; p.quantity(previousCount - 1); updateCart(itemUpdate(p)); } } function deleteProductCart(p) { updateCart(itemUpdate(p, true)); } function clearCart() { CartViewModel.cart().items.forEach(function (p) { deleteProductCart(p); }); } function subTotalItem(price, quantity) { return price * quantity; } function checkEmailWishList(user) { try { if (user.email != null) { localStorage.setItem('currentEmail', user.email); if (localStorage.getItem('wishlist') == null) { MasterDataService.getObjectsRegistrationFree('WL', 'search?_fields=id_product,id,is_deleted&_where=(email=' + user.email + ')&_from=0&_to=14').then(function (res) { if (res.length > 0) { localStorage.setItem('wishlist', JSON.stringify(res)); } else { localStorage.setItem('wishlist', JSON.stringify([])); } checkFavoriteProductIcon(); checkFavoriteInView(); }).fail(function (a) { }); } } if (localStorage.getItem('currentEmail') != user.email) { localStorage.setItem('currentEmail', user.email); } } catch (error) { } } function updateCart(itemsToUpdate) { $.ajax({ url: '/api/checkout/pub/orderForm/' + CartViewModel.orderFormId + '/items/update/', headers: { 'Content-Type': 'application/json' }, dataType: 'json', data: JSON.stringify({ orderItems: itemsToUpdate }), type: 'POST', beforeSend: function beforeSend() { }, complete: function complete() { }, success: function success(res) { var order = res; var hasError = false; checkEmailWishList(order.clientProfileData); if (order.items.length > 0) { if (order.items[itemsToUpdate[0].index] != null) { var quantityUpdated = order.items[itemsToUpdate[0].index].quantity; if (itemsToUpdate[0].quantity > quantityUpdated) { CartViewModel.errorMessage('Solo hay ' + quantityUpdated + (quantityUpdated === 1 ? ' unidad' : ' unidades')); } else { CartViewModel.errorMessage(''); } } } order.items.forEach(function (item) { item.quantity = ko.observable(item.quantity); if (item.availability != 'available') { hasError = true; } }); window.loggedIn = order.loggedIn; hasError ? CartViewModel.hasError(true) : CartViewModel.hasError(false); CartViewModel.orderFormId = order.orderFormId; CartViewModel.setCart(order); calculatePricingCart(); if (order.items.length > 0) { $('.count-items-cart').css({ display: '' }).html(order.items.length); } else { $(".count-items-cart").css({ display: "none" }); } return true; }, error: function error(e) { console.log(e); } }); return true; } function getOrderForm(updateModel) { $.ajax({ url: '/api/checkout/pub/orderForm/', headers: { 'Content-Type': 'application/json' }, dataType: 'json', type: 'POST', beforeSend: function beforeSend() { }, complete: function complete() { }, success: function success(res) { var order = res; var hasError = false; window.loggedIn = order.loggedIn; checkEmailWishList(order.clientProfileData); if (updateModel) { order.items.forEach(function (item) { item.quantity = ko.observable(item.quantity); if (item.availability != 'available') { hasError = true; } }); CartViewModel.orderFormId = order.orderFormId; CartViewModel.setCart(order); calculatePricingCart(); } else { HeaderViewModel.order(order); var totalizers = HeaderViewModel.order().totalizers; var totalOrder = totalizers.length > 0 ? totalizers[0].value : 0; updateTopBar(totalOrder); } hasError ? CartViewModel.hasError(true) : CartViewModel.hasError(false); if (order.items.length > 0) { $('.count-items-cart').css({ display: '' }).html(order.items.length); } else { $(".count-items-cart").css({ display: "none" }); } }, error: function error(e) { console.log(e); } }); } function updateTopBar(total) { var totalOrder = total / 100; var minValueFreeShipping = localStorage.getItem('minValueFreeShipping'); if (minValueFreeShipping) { if (minValueFreeShipping != 0) { var amountNeeded = minValueFreeShipping - totalOrder; localStorage.setItem("amountNeeded", amountNeeded); var formatter = new Intl.NumberFormat('es-CO', { style: 'currency', currency: 'COP' }); var amountNeededFormat = formatter.format(amountNeeded); amountNeededFormat = amountNeededFormat.split(","); if (amountNeeded > 0) { $(".note-text.msg-almost").fadeIn("fast", function () { $(this).css({ "display": "flex", "align-items": "center" }); }); $(".note-text.msg-already").fadeOut(); $(".note-text.msg-almost #amountNeeded").text("".concat(amountNeededFormat[0])); } else { $(".note-text.msg-already").fadeIn("fast", function () { $(this).css({ "display": "flex", "align-items": "center" }); }); $(".note-text.msg-almost").fadeOut(); } } } }