/* * installment | Backendorf * * @category Backendorf * @package installment.js * * @copyright Copyright (c) 2020 Backendorf - Magento Developer. * * @author Davi Backendorf */ define([ "jquery", 'Magento_Catalog/js/price-utils', 'mage/translate', 'Magento_Customer/js/customer-data' ], function ($, priceUtils, $t, customerData) { 'use strict'; $.widget('mage.installment', { 'options': { 'discounts': [], 'interest_type': null, 'minimum_installment_value': null, 'maximum_quantity_installments': null, 'interest': null, 'best_installment_in_cart': null, 'currency_symbol': '', 'show_first_installment': false, 'templates': { 'in_cart_template': null, 'discount_template': null, 'catalog_category_view': null, 'catalog_product_view': null, 'catalogsearch_result_index': null, 'all_installment_template': null, 'text_with_interest': null, 'text_free_interest': null } }, _create: function () { let widget = this; if (this.options.enabled && $('body').hasClass('catalog-product-view')) { try { $('body').on('afterReloadPrice', function (e, data) { let price = widget.getFinalPrice(data.price.final); widget.renderPrices(price); widget.updateAllInstallments(price); }); } catch (e) { console.info(e); } } }, /** * * @param productPrice * @returns {{}} */ getInstallments: function (productPrice) { productPrice = parseFloat(productPrice); let widget = this; const type_interest = this.options.interest_type; const taxa_juros_incremental = parseInt(this.options.taxa_juros_incremental) / 100; const info_interest = this.options.interest; const min_installment = this.options.minimum_installment_value; const max_installment = this.options.maximum_quantity_installments; let json_installments = {}; if (this.options.show_first_installment) { json_installments[1] = { 'installments_qty': 1, 'installment_value': widget.formatPrice(productPrice), 'total_installment': widget.formatPrice(productPrice), 'total_interest': 0, 'has_interest': 0 }; } let max_div = (productPrice / min_installment); max_div = parseInt(max_div); if (max_div > max_installment) { max_div = max_installment; } else { if (max_div > 12) { max_div = 12; } if (productPrice < min_installment) { max_div = 1; } } let limit = max_div; let incrementParcela = 0; for (let installment_number in info_interest) { installment_number = parseInt(installment_number); if (installment_number <= max_div) { let rate = parseFloat(info_interest[installment_number] / 100); let amount = 0; let installment_value = 0; if (rate > 0) { if (type_interest === "compound") { if (incrementParcela == 0) { amount = (productPrice * rate) + productPrice; installment_value = amount / installment_number; } else { amount = (productPrice * (rate + incrementParcela)) + productPrice; installment_value = amount / installment_number; } incrementParcela = incrementParcela + taxa_juros_incremental; } else { amount = (productPrice * rate) + productPrice; installment_value = amount / installment_number; } let total_interest = amount - productPrice; if (installment_value > 5 && installment_value > min_installment) { json_installments[installment_number] = { 'installments_qty': installment_number, 'installment_value': widget.formatPrice(installment_value), 'total_interest': widget.formatPrice(total_interest), 'amount': widget.formatPrice(amount), 'has_interest': true, 'rate': info_interest[installment_number] + "%" }; } } else { if (productPrice > 0 && installment_number > 0) { json_installments[installment_number] = { 'installments_qty': installment_number, 'installment_value': widget.formatPrice((productPrice / installment_number)), 'total_interest': widget.formatPrice(0), 'amount': widget.formatPrice(productPrice), 'has_interest': false, 'rate': info_interest[installment_number] + "%" }; } } } } _.each(json_installments, function (key) { if (key > limit) { delete json_installments[key]; } }); return json_installments; }, /** * * @param installment * @returns {*} */ getBestInstallment: function (installment) { let best = null; for (let i = this.options.maximum_quantity_installments; i >= 1; i--) { if (installment[i]) { if (!best) { best = installment[i]; } if (!installment[i]['has_interest']) { return installment[i]; } } } return best; }, /** * * @param price * @returns {[]} */ getDiscounts: function (price) { for (let i in this.options.discounts) { let discount = this.options.discounts[i]; if (discount.hasOwnProperty('percentage')) { const discountValue = price - ((price * discount.percentage) / 100); discount.value = this.formatPrice(discountValue); discount.valueNumber = discountValue; discount.valueDiscount = this.formatPrice(price - discountValue); this.options.discounts[i] = discount; } } return this.options.discounts; }, /** * * @param discounts */ renderDiscounts: function (discounts) { let productInfo = document.querySelector('.product-info-main'); let elValueDiscount = productInfo.querySelector('.value-discount'); let elPricewithdiscount = productInfo.querySelector('.pricewithdiscount .price'); for (let i in discounts) { const valueDiscount = discounts[i].valueDiscount; const value = discounts[i].value; elValueDiscount.textContent = valueDiscount; elPricewithdiscount.textContent = value; } }, /** * * @param price * */ updateAllInstallments: function (price) { let installments = this.getInstallments(price); if (installments) { let template = this.options.templates.all_installment_template; let html = ''; $('.backendorf-installment .all-installments-content').html(html); } }, /** * * @returns {null} */ getTemplate() { if ($('body.catalog-category-view').length > 0) { return this.options.templates.catalog_category_view; } else if ($('body.catalog-product-view').length > 0) { return this.options.templates.catalog_product_view; } else if ($('body.catalogsearch-result-index').length > 0) { return this.options.templates.catalogsearch_result_index; } else { return this.options.templates.catalog_product_view; } }, /** * * @param price */ renderPrices: function (price) { let installments = this.getInstallments(price); let productInfo = document.querySelector('.product-info-main'); let elInterestQty = productInfo.querySelector('.interest-qty'); let elInterestPrice = productInfo.querySelector('.interest-price'); if (installments) { let data = { 'bestInstallment': this.getBestInstallment(installments) }; elInterestQty.textContent = data.bestInstallment.installments_qty + 'x'; elInterestPrice.textContent = data.bestInstallment.installment_value; } }, /** * * @param price * @returns {number} */ getFinalPrice: function(price) { const isCashPriceOnCard = $('#is_installments_vista').val() === "1"; let discounts = this.getDiscounts(price); this.renderDiscounts(discounts); if(isCashPriceOnCard) { for (let i in discounts) { price = discounts[i].valueNumber; } } return price; }, /** * @param price * @returns {*} */ formatPrice: function (price) { let format = { decimalSymbol: ",", groupLength: 3, groupSymbol: ".", integerRequired: false, pattern: this.options.currency_symbol + " %s", precision: 2, requiredPrecision: 2 }; format = (window.checkoutConfig && window.checkoutConfig.priceFormat) ? window.checkoutConfig.priceFormat : format; return priceUtils.formatPrice(price, format); }, renderInterest: function (installment) { let template = (installment.has_interest) ? this.options.templates.text_with_interest : this.options.templates.text_free_interest; template = template.replace('{{amount}}', installment.amount) .replace('{{total_interest}}', installment.total_interest) .replace('{{rate}}', installment.rate) return ' ' + template; } }); return $.mage.installment; });