Skip to content

Commit

Permalink
[FIX] intrastat_product: notedict
Browse files Browse the repository at this point in the history
  • Loading branch information
victoralmau committed Jun 14, 2022
1 parent 2c4e69f commit 6e28dfc
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 40 deletions.
6 changes: 5 additions & 1 deletion intrastat_product/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
76 changes: 37 additions & 39 deletions intrastat_product/models/intrastat_product_declaration.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import logging
import warnings
from datetime import date, datetime
from datetime import date

from dateutil.relativedelta import relativedelta

Expand Down Expand Up @@ -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:
Expand All @@ -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 = [
Expand All @@ -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:
Expand All @@ -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 = [
_(
Expand All @@ -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):
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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)
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
)
Expand All @@ -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:
Expand All @@ -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

Expand Down Expand Up @@ -756,41 +757,38 @@ 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",
"view_mode": "form",
"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",
}

Expand Down

0 comments on commit 6e28dfc

Please sign in to comment.