diff --git a/intrastat_product/models/intrastat_product_declaration.py b/intrastat_product/models/intrastat_product_declaration.py index 068e0242a..570d97af5 100644 --- a/intrastat_product/models/intrastat_product_declaration.py +++ b/intrastat_product/models/intrastat_product_declaration.py @@ -98,6 +98,9 @@ def default_get(self, fields_list): ) declaration_type = fields.Selection( selection="_get_declaration_type", + compute="_compute_declaration_type", + store=True, + precompute=True, string="Type", required=True, tracking=True, @@ -189,6 +192,45 @@ def _compute_year_month(self): if this.year and this.month: this.year_month = "-".join([this.year, this.month]) + @api.depends("company_id", "year", "month") + def _compute_declaration_type(self): + for this in self: + company = this.company_id + declaration_type = False + if company: + if ( + company.intrastat_arrivals == "exempt" + and company.intrastat_dispatches != "exempt" + ): + declaration_type = "dispatches" + elif ( + company.intrastat_dispatches == "exempt" + and company.intrastat_arrivals != "exempt" + ): + declaration_type = "arrivals" + elif ( + company.intrastat_dispatches != "exempt" + and company.intrastat_arrivals != "exempt" + and this.year + and this.month + ): + existing_decls = self.search( + [ + ("year", "=", this.year), + ("month", "=", this.month), + ("company_id", "=", company.id), + ] + ) + if len(existing_decls) == 1: + declaration_type = ( + existing_decls.declaration_type == "arrivals" + and "dispatches" + or "arrivals" + ) + elif not existing_decls: + declaration_type = "dispatches" + this.declaration_type = declaration_type + @api.constrains("company_id") def _check_company_country(self): for this in self: