diff --git a/modules/account_stock_eu/__init__.py b/modules/account_stock_eu/__init__.py index 3794d375cb..11f293b695 100644 --- a/modules/account_stock_eu/__init__.py +++ b/modules/account_stock_eu/__init__.py @@ -4,7 +4,7 @@ from trytond.pool import Pool from . import ( - account, account_stock_eu, carrier, company, country, customs, stock) + account, account_stock_eu, carrier, company, country, customs, sale, stock) __all__ = ['register'] @@ -38,6 +38,10 @@ def register(): stock.Move_Incoterm, module='account_stock_eu', type_='model', depends=['incoterm']) + Pool.register( + sale.Sale_Incoterm, + module='account_stock_eu', type_='model', + depends=['sale', 'incoterm']) Pool.register( stock.Move_Consignment, module='account_stock_eu', type_='model', diff --git a/modules/account_stock_eu/sale.py b/modules/account_stock_eu/sale.py new file mode 100644 index 0000000000..e7b02a0471 --- /dev/null +++ b/modules/account_stock_eu/sale.py @@ -0,0 +1,48 @@ +# This file is part of Tryton. The COPYRIGHT file at the top level of +# this repository contains the full copyright notices and license terms. + +from trytond.model import fields +from trytond.modules.account.exceptions import FiscalYearNotFoundError +from trytond.pool import Pool, PoolMeta + + +class Sale_Incoterm(metaclass=PoolMeta): + __name__ = 'sale.sale' + + @property + @fields.depends('company') + def _incoterm_required(self): + pool = Pool() + FiscalYear = pool.get('account.fiscalyear') + + required = super()._incoterm_required + + if not required and self.company and self.company.incoterms: + if (self.warehouse and self.warehouse.address + and self.shipment_address): + try: + fiscalyear = FiscalYear.find( + self.company.id, + date=self.sale_date) + except FiscalYearNotFoundError: + pass + else: + if fiscalyear.intrastat_extended: + from_country = self.warehouse.address.country + if from_country: + from_europe = from_country.is_member( + 'country.organization_eu', + self.sale_date) + else: + from_europe = None + to_country = self.shipment_address.country + if to_country: + to_europe = to_country.is_member( + 'country.organization_eu', + self.sale_date) + else: + to_europe = None + required = ( + (from_country != to_country) + and not (from_europe and to_europe)) + return required