From 838ceecb39e567a0cc9f04a5ba1d02fb8bd1e06a Mon Sep 17 00:00:00 2001 From: "Santiago J. Said" <said.santiago@gmail.com> Date: Mon, 5 Mar 2018 10:32:40 -0300 Subject: [PATCH] Deprecate the usage of financing_tables, now they are built in the PoS frontend --- models/product.py | 90 +++++++++++++++++++++-------------------- static/src/js/db.js | 57 ++++++++++++++++++++++++++ static/src/js/models.js | 22 ++++++++-- 3 files changed, 122 insertions(+), 47 deletions(-) diff --git a/models/product.py b/models/product.py index 67073c7..e1dbea4 100644 --- a/models/product.py +++ b/models/product.py @@ -33,6 +33,7 @@ class ProductProduct(models.Model): @api.model def search_read(self, domain=None, fields=None, offset=0, limit=None, order=None): if 'financing_tables' in fields: + _logger.warning('financing_tables is being read. This fields should not be read anymore.') fields.remove('financing_tables') simple_res = super(ProductProduct, self).search_read(domain, fields, offset, limit, order) pos_config_id = self.env.user.pos_config.id @@ -54,50 +55,51 @@ class ProductProduct(models.Model): @api.depends("financings", "financings.quota_amount") def compute_financing_tables(self): - table_template = """ - <table class="tooltip_table"> - <tr> - <th class="tooltip_table_header">{}</th> - <th class="tooltip_table_header">{}</th> - <th class="tooltip_table_header">{}</th> - <th class="tooltip_table_header">{}</th> - <th class="tooltip_table_header">{}</th> - </tr> - %s - </table> - """.format(_("Quota"), _("Quota Amount"), _("Total"), _("A.P.R."), _("T.F.C.")) - - data_template = """ - <tr> - <td class="tooltip_table_data">{f.financing_config_id.quota_qty}</td> - <td class="tooltip_table_data">{f.quota_amount_taxed:.2f}</td> - <td class="tooltip_table_data">{f.total_taxed:.2f}</td> - <td class="tooltip_table_data">{f.apr:.3f}</td> - <td class="tooltip_table_data">{f.tfc:.3f}</td> - </tr> - """ - no_financing_msg = _('This product is not properly configured!') - empty_json = json.dumps(no_financing_msg) - for product in self: - if not product.financings: - product.financing_tables = empty_json - continue - - # Build table rows - rows = {} - for financing in product.financings: - rows.setdefault(financing.pos_config_id.id, {}).setdefault( - financing.customer_type, []).append(data_template.format(f=financing)) - - # Join rows with the table header - tables = {} - for pos_config_id, data in rows.items(): - for customer_type, row in data.items(): - table = table_template % "\n".join(row) - tables.setdefault(pos_config_id, {})[customer_type] = table - - #product.financing_tables = ";".join(itertools.chain(*tables.items())) - product.financing_tables = json.dumps(tables) + _logger.warning('financing_tables has been deprecated. Now they are computed in JS. This field is goind to be removed.') + # table_template = """ + # <table class="tooltip_table"> + # <tr> + # <th class="tooltip_table_header">{}</th> + # <th class="tooltip_table_header">{}</th> + # <th class="tooltip_table_header">{}</th> + # <th class="tooltip_table_header">{}</th> + # <th class="tooltip_table_header">{}</th> + # </tr> + # %s + # </table> + # """.format(_("Quota"), _("Quota Amount"), _("Total"), _("A.P.R."), _("T.F.C.")) + # + # data_template = """ + # <tr> + # <td class="tooltip_table_data">{f.financing_config_id.quota_qty}</td> + # <td class="tooltip_table_data">{f.quota_amount_taxed:.2f}</td> + # <td class="tooltip_table_data">{f.total_taxed:.2f}</td> + # <td class="tooltip_table_data">{f.apr:.3f}</td> + # <td class="tooltip_table_data">{f.tfc:.3f}</td> + # </tr> + # """ + # no_financing_msg = _('This product is not properly configured!') + # empty_json = json.dumps(no_financing_msg) + # for product in self: + # if not product.financings: + # product.financing_tables = empty_json + # continue + # + # # Build table rows + # rows = {} + # for financing in product.financings: + # rows.setdefault(financing.pos_config_id.id, {}).setdefault( + # financing.customer_type, []).append(data_template.format(f=financing)) + # + # # Join rows with the table header + # tables = {} + # for pos_config_id, data in rows.items(): + # for customer_type, row in data.items(): + # table = table_template % "\n".join(row) + # tables.setdefault(pos_config_id, {})[customer_type] = table + # + # #product.financing_tables = ";".join(itertools.chain(*tables.items())) + # product.financing_tables = json.dumps(tables) @api.onchange("lst_price") def onchange_lst_price(self): diff --git a/static/src/js/db.js b/static/src/js/db.js index 63cc13d..f0b4652 100644 --- a/static/src/js/db.js +++ b/static/src/js/db.js @@ -18,6 +18,8 @@ function financing_db(instance, module) { var PosDBParent = module.PosDB; + var round_di = instance.web.round_decimals; + var round_pr = instance.web.round_precision; module.PosDB = module.PosDB.extend({ init: function (options) { @@ -84,6 +86,61 @@ function financing_db(instance, module) { //} } }, + get_table: function(product_id, customer_type){ + var self = this; + var table_header = '<table class="tooltip_table">' + + '<tr><th class="tooltip_table_header">Cuotas</th>' + + '<th class="tooltip_table_header">$</th>' + + '<th class="tooltip_table_header">Total</th>' + + '<th class="tooltip_table_header">T.E.A.</th>' + + '<th class="tooltip_table_header">C.F.T.</th></tr>'; + var closing_tag = '</table>'; + // Table + try { + var financings = this.financing_by_product_id[product_id][customer_type]; + } catch (e) { + return ''; + } + var table = ''; + try { + var tax_id = this.product_by_id[product_id].taxes_id[0]; + var tax = window.posmodel.taxes_by_id[tax_id]; + var tax_amount = 1 + tax.amount; + } catch (e) { + var tax_amount = 1; // No tax applied + } + for (var k = 0; k < financings.length; k++) { + var financing = financings[k]; + total_round = (round_di(parseFloat(financing.total) || 0, 2) * tax_amount).toFixed(2); + qa_round = (round_di(parseFloat(financing.quota_amount) || 0, 2) * tax_amount).toFixed(2); + table += '<tr><td class="tooltip_table_data">' + + financing.quota_qty.toString() + '</td><td class="tooltip_table_data">' + + qa_round.toString() + '</td><td class="tooltip_table_data">' + + total_round.toString() + '</td><td class="tooltip_table_data">' + + financing.apr.toString() + '</td><td class="tooltip_table_data">' + + financing.tfc.toString() + '</td></tr>'; + }; + // Table + var the_table = table_header + table + closing_tag; + return the_table; + }, + set_financing_tables: function(financings) { + // In: Financings + // Out: Set db.table_by_product_id = {product_id: {customer_type: html_table}} + var self = this; + var res = {}; + var product_ids = Object.keys(this.product_by_id); + for (var i = 0, len = product_ids.length; i < len; i++) { + var product_id = parseInt(product_ids[i]); + var associated_tab = this.get_table(product_id, 'associated'); + var non_associated_tab = this.get_table(product_id, 'non_associated'); + res[product_id] = { + 'associated': associated_tab, + 'non_associated': non_associated_tab + }; + } + this.table_by_product_id = res; + }, add_financings: function (financings) { // build an useful financing map table as follows: product_id,customer_type -> [financings] diff --git a/static/src/js/models.js b/static/src/js/models.js index 2f44e6d..554b2c0 100644 --- a/static/src/js/models.js +++ b/static/src/js/models.js @@ -372,7 +372,7 @@ function financing_models(instance, module) { var product_model = _.find(this.models, function(model){ return model.model === 'product.product'; }); - product_model.fields.push('financing_tables'); + // product_model.fields.push('financing_tables'); product_model.fields.push('no_interest'); product_model.loaded = function(self, products) { self.db.add_products_with_pos(products, self) } @@ -575,13 +575,28 @@ function financing_models(instance, module) { this.pos_widget = options.pos_widget; }, + set_product_ui_tooltip: function (product_ui, table) { + var table_header = '<table class="tooltip_table">' + + '<tr><th class="tooltip_table_header">Cuotas</th>' + + '<th class="tooltip_table_header">$</th>' + + '<th class="tooltip_table_header">Total</th>' + + '<th class="tooltip_table_header">T.E.A.</th>' + + '<th class="tooltip_table_header">C.F.T.</th></tr>'; + var closing_tag = '</table>'; + $(product_ui).find('.product-img').attr( + 'data-original-title', table_header + table + closing_tag + ); + $(product_ui).find('.product-img').attr('data-toggle', 'tooltip'); + $(product_ui).find('.product-img').tooltip({delay: {show: 50, hide: 100}}); + }, + /** * @param partner */ show_product_financing: function (partner) { if (!this.pos_widget.product_screen) return; var product_list_ui = this.pos_widget.product_screen.$('.product-list span.product'); - customer_type = partner ? partner.customer_type : false + customer_type = partner ? partner.customer_type : false; // build financings table that will be showed for (var i = 0, len = product_list_ui.length; i < len; i++) { var product_ui = product_list_ui[i]; @@ -618,7 +633,7 @@ function financing_models(instance, module) { }; function get_financings(pos_model) { - var res_product_pricelist = pos_model.find_model('product.pricelist'); + var res_product_pricelist = pos_model.find_model('product.product'); if (_.size(res_product_pricelist) == 1) { var pricelist_index = parseInt(Object.keys(res_product_pricelist)[0]); pos_model.models.splice(++pricelist_index, 0, @@ -634,6 +649,7 @@ function financing_models(instance, module) { domain: function(pos_model, tmp){ return [['pos_config_id', '=', pos_model.config.id]]; }, loaded: function (self, financings) { self.db.add_financings(financings); + self.db.set_financing_tables(financings); } }); pos_model.models.splice(++pricelist_index, 0, { -- GitLab