From be1aa4bbdec8555110884891c5b3fc4c9489b454 Mon Sep 17 00:00:00 2001 From: marinaGD Date: Mon, 27 Aug 2018 16:18:48 -0300 Subject: [PATCH 1/4] add warning when some and error when all the products doesnt have recorded weight --- weight_purchase/models/purchase_order.py | 44 ++++++++++++++++++------ 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/weight_purchase/models/purchase_order.py b/weight_purchase/models/purchase_order.py index d27955ce..ad3e7851 100644 --- a/weight_purchase/models/purchase_order.py +++ b/weight_purchase/models/purchase_order.py @@ -2,24 +2,46 @@ # © 2018 Trustcode # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). -from odoo import api, models +from odoo import api, models, _ +from odoo.exceptions import UserError class PurchaseOrder(models.Model): _inherit = 'purchase.order' - @api.onchange('total_despesas', 'total_seguro', 'total_frete') - def _onchange_despesas_frete_seguro(self): + @api.onchange('total_frete') + def _onchange_frete(self): super(PurchaseOrder, self)._onchange_despesas_frete_seguro() - full_weight = 0 - for line in self.order_line: - if line.product_id.fiscal_type == 'product': - full_weight += (line.product_id.weight * line.product_qty) + full_weight = self._calc_total_weight() + res = {} for line in self.order_line: + valor_frete = self._calc_percentual_weight( + line, full_weight) + line.update({ + 'valor_frete': valor_frete + }) + if valor_frete == 0: + res = {'warning': { + 'title': _('Warning'), + 'message': _("O produto %s tem peso igual a zero, \ + caso não seja alterado, o rateio do frete \ + não o considerará." % (line.product_id.name))}} + if 'warning' in res: + return res + + def _calc_percentual_weight(self, line, full_weight): if line.product_id.fiscal_type == 'service': - continue + return total_weight = (line.product_id.weight * line.product_qty) percentual = total_weight / full_weight - line.update({ - 'valor_frete': self.total_frete * percentual - }) + return self.total_frete * percentual + + def _calc_total_weight(self): + full_weight = 0 + for line in self.order_line: + if line.product_id.fiscal_type == 'product': + full_weight += (line.product_id.weight * line.product_qty) + if full_weight == 0: + raise UserError("Nenhum dos produtos possui peso cadastrado. \ + É necessario corrigir para o calculo do frete.") + return full_weight From c4e556470f821a97a408287bd2f3e6f07bd1b339 Mon Sep 17 00:00:00 2001 From: marinaGD Date: Mon, 27 Aug 2018 16:25:44 -0300 Subject: [PATCH 2/4] add product_id in onchange dependency --- weight_purchase/models/purchase_order.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/weight_purchase/models/purchase_order.py b/weight_purchase/models/purchase_order.py index ad3e7851..01352d1c 100644 --- a/weight_purchase/models/purchase_order.py +++ b/weight_purchase/models/purchase_order.py @@ -9,7 +9,7 @@ class PurchaseOrder(models.Model): _inherit = 'purchase.order' - @api.onchange('total_frete') + @api.onchange('total_frete', 'product_id') def _onchange_frete(self): super(PurchaseOrder, self)._onchange_despesas_frete_seguro() full_weight = self._calc_total_weight() From b208987f9d331c36521c0615dfa0261eae509e01 Mon Sep 17 00:00:00 2001 From: marinaGD Date: Mon, 27 Aug 2018 17:47:04 -0300 Subject: [PATCH 3/4] fix flake8 --- weight_purchase/models/purchase_order.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/weight_purchase/models/purchase_order.py b/weight_purchase/models/purchase_order.py index 01352d1c..b9626eed 100644 --- a/weight_purchase/models/purchase_order.py +++ b/weight_purchase/models/purchase_order.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # © 2018 Trustcode # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). @@ -42,6 +41,6 @@ def _calc_total_weight(self): if line.product_id.fiscal_type == 'product': full_weight += (line.product_id.weight * line.product_qty) if full_weight == 0: - raise UserError("Nenhum dos produtos possui peso cadastrado. \ - É necessario corrigir para o calculo do frete.") + raise UserError(_("Nenhum dos produtos possui peso cadastrado. \ + É necessário corrigir para o calculo do frete.")) return full_weight From 68d4740a385a1b6c5d0d92858b9118ed0ac080fb Mon Sep 17 00:00:00 2001 From: marinaGD Date: Tue, 28 Aug 2018 16:56:35 -0300 Subject: [PATCH 4/4] fix _ and when any product is added --- weight_purchase/models/purchase_order.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/weight_purchase/models/purchase_order.py b/weight_purchase/models/purchase_order.py index b9626eed..51efb267 100644 --- a/weight_purchase/models/purchase_order.py +++ b/weight_purchase/models/purchase_order.py @@ -8,9 +8,13 @@ class PurchaseOrder(models.Model): _inherit = 'purchase.order' - @api.onchange('total_frete', 'product_id') + @api.onchange('total_despesas', 'total_seguro', 'product_id', + 'total_frete', 'total_despesas_aduana') def _onchange_frete(self): super(PurchaseOrder, self)._onchange_despesas_frete_seguro() + if self.total_frete == 0 or ( + not self.fiscal_position_id.fiscal_type == 'import'): + return full_weight = self._calc_total_weight() res = {} for line in self.order_line: @@ -20,11 +24,14 @@ def _onchange_frete(self): 'valor_frete': valor_frete }) if valor_frete == 0: - res = {'warning': { + res = { + 'warning': { 'title': _('Warning'), 'message': _("O produto %s tem peso igual a zero, \ caso não seja alterado, o rateio do frete \ - não o considerará." % (line.product_id.name))}} + não o considerará.") % (line.product_id.name), + } + } if 'warning' in res: return res @@ -40,7 +47,7 @@ def _calc_total_weight(self): for line in self.order_line: if line.product_id.fiscal_type == 'product': full_weight += (line.product_id.weight * line.product_qty) - if full_weight == 0: + if full_weight == 0 and self.order_line: raise UserError(_("Nenhum dos produtos possui peso cadastrado. \ É necessário corrigir para o calculo do frete.")) return full_weight