diff --git a/sale_cost.py b/sale_cost.py index 91ac3a9..c80f076 100644 --- a/sale_cost.py +++ b/sale_cost.py @@ -2,7 +2,7 @@ # copyright notices and license terms. from trytond.pool import PoolMeta from trytond.model import fields -from trytond.pyson import Eval, Or, Not, Bool, And, In +from trytond.pyson import Eval, Not, And, In from trytond.modules.document_cost_apply_invoice.cost import ( ApplyMethodCostMixin, ApplyMethodCostDocumentMixin, ApplyInvoiceAccountMixin @@ -35,25 +35,6 @@ class SaleCost(ApplyMethodCostDocumentMixin, metaclass=PoolMeta): @classmethod def __setup__(cls): super().__setup__() - invisible_condition = cls._buttons['apply']['invisible'] - cls._buttons['apply']['invisible'] = Or( - invisible_condition, - ((Eval('apply_method').in_(['invoice_in', 'invoice_out']) - & Bool(Eval('invoice_lines'))) - ) - ) - cls._buttons['apply']['depends'].append('invoice_lines') - - invisible_condition = cls._buttons['unapply']['invisible'] - cls._buttons['unapply']['invisible'] = Or( - invisible_condition, - ( - (Eval('apply_method').in_(['invoice_in', 'invoice_out']) - & Not(Bool(Eval('invoice_lines')))) - ) - ) - cls._buttons['unapply']['depends'].append('invoice_lines') - readonly_description = And( Eval('apply_method').in_(['invoice_in', 'invoice_out']), Eval('invoice_lines')) @@ -80,6 +61,32 @@ class SaleCost(ApplyMethodCostDocumentMixin, metaclass=PoolMeta): def non_explodable(self): return super().non_explodable or bool(self.invoice_lines) + def get_appliable(self, name=None): + res = super().get_appliable(name) + if self.apply_method in ['invoice_in', 'invoice_out']: + res &= not bool(self.invoice_lines) + return res + + def get_applied(self, name=None): + return super().get_applied(name) or ( + self.apply_method in ['invoice_in', 'invoice_out'] + and bool(self.invoice_lines)) + + @classmethod + def search_applied(cls, name, clause): + domain = super().search_applied(name, clause) + + join_operator, operator1, operator2 = 'AND', 'in', '!=' + if ((clause[1] == '!=' and clause[2]) + or (clause[1] == '=' and not clause[2])): + join_operator, operator1, operator2 = 'OR', 'not in', '=' + + domain.append([join_operator, + ('apply_method', operator1, ['invoice_in', 'invoice_out']), + ('invoice_lines', operator2, None)]) + + return domain + class SaleCostDiscount(metaclass=PoolMeta): __name__ = 'sale.cost' diff --git a/tests/scenario_sale_cost_apply_invoice.rst b/tests/scenario_sale_cost_apply_invoice.rst index 8fbf8b8..b3a22d0 100644 --- a/tests/scenario_sale_cost_apply_invoice.rst +++ b/tests/scenario_sale_cost_apply_invoice.rst @@ -184,6 +184,7 @@ Sale 2 products:: Check cost applying:: + >>> Cost = Model.get('sale.cost') >>> InvoiceLine = Model.get('account.invoice.line') >>> invoice_cost, = [c for c in sale.costs if c.type_ == type_invoice] >>> lines = InvoiceLine.find([('origin', 'like', 'sale.cost,%')]) @@ -197,6 +198,18 @@ Check cost applying:: trytond.exceptions.UserError: Must define Invoice Party for cost of type "Invoice" on Document "1". - >>> invoice_cost.invoice_party = supplier >>> invoice_cost.save() + >>> bool(invoice_cost.applied) + False + >>> bool(invoice_cost.appliable) + True + >>> bool(Cost.find([('id', '=', invoice_cost.id), ('applied', '=', False)])) + True + >>> bool(Cost.find([('id', '=', invoice_cost.id), ('applied', '!=', False)])) + False + >>> bool(Cost.find([('id', '=', invoice_cost.id), ('applied', '=', True)])) + False + >>> bool(Cost.find([('id', '=', invoice_cost.id), ('applied', '!=', True)])) + True >>> sale.click('confirm') >>> line, = InvoiceLine.find([('origin', '=', 'sale.cost,%s' % invoice_cost.id)]) >>> line.amount @@ -210,7 +223,19 @@ Check cost applying:: >>> line, = InvoiceLine.find([('origin', '=', 'sale.cost,%s' % invoice_out_cost.id)]) >>> line.amount Decimal('-8.70') - + >>> invoice_cost.reload() + >>> bool(invoice_cost.applied) + True + >>> bool(invoice_cost.appliable) + False + >>> bool(Cost.find([('id', '=', invoice_cost.id), ('applied', '=', False)])) + False + >>> bool(Cost.find([('id', '=', invoice_cost.id), ('applied', '!=', False)])) + True + >>> bool(Cost.find([('id', '=', invoice_cost.id), ('applied', '=', True)])) + True + >>> bool(Cost.find([('id', '=', invoice_cost.id), ('applied', '!=', True)])) + False Create invoice and add line:: diff --git a/tests/scenario_sale_cost_intrastat.rst b/tests/scenario_sale_cost_intrastat.rst index 5268bc1..d4b02c1 100644 --- a/tests/scenario_sale_cost_intrastat.rst +++ b/tests/scenario_sale_cost_intrastat.rst @@ -63,9 +63,13 @@ Create account categories:: Create countries and subdivisions:: >>> Subdivision = Model.get('country.subdivision') + >>> SubdivisionType = Model.get('party.address.subdivision_type') >>> Country = Model.get('country.country') >>> country_es = Country(name="Spain", code="ES", eu_member=True) >>> country_es.save() + >>> sub_type, = SubdivisionType.find([('country_code', '=', country_es.code)]) + >>> sub_type.types = list(sub_type.types) + ['state'] + >>> sub_type.save() >>> madrid = Subdivision( ... name="Madrid", code="ES-MA", type='state', country=country_es) >>> madrid.save()