From 6e28dfca43b3884c1ec5f33d3ec204a0230d03d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Mart=C3=ADnez?= Date: Tue, 14 Jun 2022 13:23:41 +0200 Subject: [PATCH] [FIX] intrastat_product: notedict --- intrastat_product/models/account_move.py | 6 +- .../models/intrastat_product_declaration.py | 76 +++++++++---------- 2 files changed, 42 insertions(+), 40 deletions(-) diff --git a/intrastat_product/models/account_move.py b/intrastat_product/models/account_move.py index 0223ac063..60e123f84 100644 --- a/intrastat_product/models/account_move.py +++ b/intrastat_product/models/account_move.py @@ -84,12 +84,16 @@ def compute_intrastat_lines(self): def _get_intrastat_line_vals(self, line): vals = {} + notedict = { + "note": "", + "line_nbr": 0, + } decl_model = self.env["intrastat.product.declaration"] if decl_model._is_product(line): hs_code = line.product_id.get_hs_code_recursively() if not hs_code: return vals - weight, qty = decl_model._get_weight_and_supplunits(line, hs_code) + weight, qty = decl_model._get_weight_and_supplunits(line, hs_code, notedict) product_country = line.product_id.origin_country_id product_state = line.product_id.origin_state_id country = product_country or product_state.country_id diff --git a/intrastat_product/models/intrastat_product_declaration.py b/intrastat_product/models/intrastat_product_declaration.py index 405d3a330..3f30721bd 100644 --- a/intrastat_product/models/intrastat_product_declaration.py +++ b/intrastat_product/models/intrastat_product_declaration.py @@ -5,7 +5,7 @@ import logging import warnings -from datetime import date, datetime +from datetime import date from dateutil.relativedelta import relativedelta @@ -240,7 +240,7 @@ def _account_config_warning(self, msg): msg, action.id, _("Go to Accounting Configuration Settings screen") ) - def _get_partner_country(self, inv_line, eu_countries): + def _get_partner_country(self, inv_line, notedict, eu_countries): inv = inv_line.move_id country = inv.src_dest_country_id or inv.partner_id.country_id if not country: @@ -256,7 +256,7 @@ def _get_partner_country(self, inv_line, eu_countries): or "-", ) ] - self._format_line_note(inv_line, line_notes) + self._format_line_note(inv_line, notedict, line_notes) else: if country not in eu_countries and country.code != "GB": line_notes = [ @@ -266,7 +266,7 @@ def _get_partner_country(self, inv_line, eu_countries): ) % (inv.name, country.name) ] - self._format_line_note(inv_line, line_notes) + self._format_line_note(inv_line, notedict, line_notes) if country and country.code == "GB" and self.year >= "2021": vat = inv.commercial_partner_id.vat if not vat: @@ -285,7 +285,7 @@ def _get_partner_country(self, inv_line, eu_countries): inv.commercial_partner_id.display_name, ) ] - self._format_line_note(inv_line, line_notes) + self._format_line_note(inv_line, notedict, line_notes) elif not vat.startswith("XI"): line_notes = [ _( @@ -304,7 +304,7 @@ def _get_partner_country(self, inv_line, eu_countries): inv.commercial_partner_id.display_name, ) ] - self._format_line_note(inv_line, line_notes) + self._format_line_note(inv_line, notedict, line_notes) return country def _get_intrastat_transaction(self, inv_line): @@ -322,8 +322,7 @@ def _get_intrastat_transaction(self, inv_line): elif invoice.type == "in_refund": return company.intrastat_transaction_in_refund - def _get_weight_and_supplunits(self, inv_line, hs_code): - line_nbr = self._line_nbr + def _get_weight_and_supplunits(self, inv_line, hs_code, notedict): line_qty = inv_line.quantity product = inv_line.product_id intrastat_unit_id = hs_code.intrastat_unit_id @@ -336,7 +335,7 @@ def _get_weight_and_supplunits(self, inv_line, hs_code): if not source_uom: line_notes = [_("Missing unit of measure.")] - self._note += self._format_line_note(inv_line, line_nbr, line_notes) + self._note += self._format_line_note(inv_line, notedict, line_notes) return weight, suppl_unit_qty if intrastat_unit_id: @@ -350,7 +349,7 @@ def _get_weight_and_supplunits(self, inv_line, hs_code): ) % intrastat_unit_id.name, ] - self._note += self._format_line_note(inv_line, line_nbr, line_notes) + self._note += self._format_line_note(inv_line, notedict, line_notes) return weight, suppl_unit_qty if target_uom.category_id == source_uom.category_id: suppl_unit_qty = source_uom._compute_quantity(line_qty, target_uom) @@ -362,7 +361,7 @@ def _get_weight_and_supplunits(self, inv_line, hs_code): ) % (source_uom.name, target_uom.name) ] - self._note += self._format_line_note(inv_line, line_nbr, line_notes) + self._note += self._format_line_note(inv_line, notedict, line_notes) return weight, suppl_unit_qty if weight: @@ -377,7 +376,7 @@ def _get_weight_and_supplunits(self, inv_line, hs_code): line_notes = [ _("Missing weight on product %s.") % product.name_get()[0][1] ] - self._note += self._format_line_note(inv_line, line_nbr, line_notes) + self._note += self._format_line_note(inv_line, notedict, line_notes) return weight, suppl_unit_qty if source_uom == pce_uom: weight = product.weight * line_qty # product.weight_net @@ -396,7 +395,7 @@ def _get_weight_and_supplunits(self, inv_line, hs_code): ) % (source_uom.name, product.name_get()[0][1]) ] - self._note += self._format_line_note(inv_line, line_nbr, line_notes) + self._note += self._format_line_note(inv_line, notedict, line_notes) return weight, suppl_unit_qty return weight, suppl_unit_qty @@ -564,25 +563,25 @@ def _is_product(self, invoice_line): else: return False - def _gather_invoices_init(self): + def _gather_invoices_init(self, notedict): """ placeholder for localization modules """ - def _format_line_note(self, line, line_nbr, line_notes): + def _format_line_note(self, line, notedict, line_notes): indent = 8 * " " - note = _("Invoice %s, line %s") % (line.move_id.name, line_nbr) + note = _("Invoice %s, line %s") % (line.move_id.name, notedict["line_nbr"]) note += ":\n" for line_note in line_notes: note += indent + line_note note += "\n" - return note + notedict["note"] += note - def _gather_invoices(self): + def _gather_invoices(self, notedict): lines = [] accessory_costs = self.company_id.intrastat_accessory_costs eu_countries = self.env.ref("base.europe").country_ids - self._gather_invoices_init() + self._gather_invoices_init(notedict) domain = self._prepare_invoice_domain() order = "journal_id, name" invoices = self.env["account.move"].search(domain, order=order) @@ -622,7 +621,9 @@ def _gather_invoices(self): ) continue - partner_country = self._get_partner_country(inv_line, eu_countries) + partner_country = self._get_partner_country( + inv_line, notedict, eu_countries + ) partner_country_code = ( invoice.commercial_partner_id._get_intrastat_country_code() ) @@ -637,7 +638,7 @@ def _gather_invoices(self): % (inv_line.product_id.name_get()[0][1]) ] self._note += self._format_line_note( - inv_line, line_nbr, line_notes + inv_line, notedict, line_notes ) continue else: @@ -655,7 +656,7 @@ def _gather_invoices(self): suppl_unit_qty = inv_intrastat_line.transaction_suppl_unit_qty else: weight, suppl_unit_qty = self._get_weight_and_supplunits( - inv_line, hs_code + inv_line, hs_code, notedict ) total_inv_weight += weight @@ -756,33 +757,30 @@ def action_gather(self): self._extended = True else: self._extended = False - + notedict = { + "note": "", + "line_nbr": 0, + } self.computation_line_ids.unlink() self.declaration_line_ids.unlink() - lines = self._gather_invoices() + lines = self._gather_invoices(notedict) + vals = {"note": notedict["note"]} + vals = {"note": notedict["note"]} if not lines: - self.action = "nihil" - note = ( + vals["action"] = "nihil" + vals["note"] += ( "\n" + _("No records found for the selected period !") + "\n" + _("The Declaration Action has been set to 'nihil'.") ) - self._note += note else: - self.write({"computation_line_ids": [(0, 0, x) for x in lines]}) + vals["computation_line_ids"] = [(0, 0, x) for x in lines] - if self._note: - note_header = ( - "\n\n>>> " - + fields.Datetime.to_string( - fields.Datetime.context_timestamp(self, datetime.now()) - ) - + "\n" - ) - self.note = note_header + self._note + (self.note or "") - result_view = self.env.ref("intrastat_base.intrastat_result_view_form") + self.write(vals) + if vals["note"]: + result_view = self.env.ref("intrastat_product.intrastat_result_view_form") return { "name": _("Generate lines from invoices: results"), "view_type": "form", @@ -790,7 +788,7 @@ def action_gather(self): "res_model": "intrastat.result.view", "view_id": result_view.id, "target": "new", - "context": dict(self._context, note=self._note), + "context": dict(self._context, note=vals["note"]), "type": "ir.actions.act_window", }