diff --git a/invoice.py b/invoice.py index d04b3a367d..9ffc768b5f 100644 --- a/invoice.py +++ b/invoice.py @@ -1,5 +1,7 @@ #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 functools import wraps + from trytond.model import ModelView, Workflow, fields from trytond.pool import Pool, PoolMeta from trytond.transaction import Transaction @@ -9,6 +11,18 @@ __all__ = ['Invoice', 'InvoiceLine'] __metaclass__ = PoolMeta +def process_purchase(func): + @wraps(func) + def wrapper(cls, invoices): + pool = Pool() + Purchase = pool.get('purchase.purchase') + func(cls, invoices) + with Transaction().set_user(0, set_context=True): + Purchase.process([p for i in cls.browse(invoices) + for p in i.purchases]) + return wrapper + + class Invoice: __name__ = 'account.invoice' purchase_exception_state = fields.Function(fields.Selection([ @@ -84,22 +98,19 @@ class Invoice: return super(Invoice, cls).draft(invoices) @classmethod - def paid(cls, invoices): - pool = Pool() - Purchase = pool.get('purchase.purchase') - super(Invoice, cls).paid(invoices) - with Transaction().set_user(0, set_context=True): - Purchase.process([p for i in cls.browse(invoices) - for p in i.purchases]) + @process_purchase + def post(cls, invoices): + super(Invoice, cls).post(invoices) @classmethod + @process_purchase + def paid(cls, invoices): + super(Invoice, cls).paid(invoices) + + @classmethod + @process_purchase def cancel(cls, invoices): - pool = Pool() - Purchase = pool.get('purchase.purchase') super(Invoice, cls).cancel(invoices) - with Transaction().set_user(0, set_context=True): - Purchase.process([p for i in cls.browse(invoices) - for p in i.purchases]) class InvoiceLine: diff --git a/tests/scenario_purchase.rst b/tests/scenario_purchase.rst index 72fc513687..8eed003955 100644 --- a/tests/scenario_purchase.rst +++ b/tests/scenario_purchase.rst @@ -282,6 +282,9 @@ Open supplier invoice:: u'in_invoice' >>> len(invoice.lines) 2 + >>> for line in invoice.lines: + ... line.quantity = 1 + ... line.save() >>> invoice.invoice_date = today >>> invoice.save() >>> Invoice.post([invoice.id], config.context) @@ -290,13 +293,22 @@ Open supplier invoice:: u'posted' >>> payable.reload() >>> (payable.debit, payable.credit) == \ - ... (Decimal('0.00'), Decimal('25.00')) + ... (Decimal('0.00'), Decimal('10.00')) True >>> expense.reload() >>> (expense.debit, expense.credit) == \ - ... (Decimal('25.00'), Decimal('0.00')) + ... (Decimal('10.00'), Decimal('0.00')) True +Check second invoices:: + + >>> config.user = purchase_user.id + >>> purchase.reload() + >>> len(purchase.invoices) + 2 + >>> sum(l.quantity for i in purchase.invoices for l in i.lines) + 5.0 + Create a Return:: >>> config.user = purchase_user.id @@ -357,10 +369,10 @@ Open supplier credit note:: >>> credit_note.state u'posted' >>> payable.reload() - >>> (payable.debit, payable.credit) == (Decimal(20), Decimal(25)) + >>> (payable.debit, payable.credit) == (Decimal(20), Decimal(10)) True >>> expense.reload() - >>> (expense.debit, expense.credit) == (Decimal(25), Decimal(20)) + >>> (expense.debit, expense.credit) == (Decimal(10), Decimal(20)) True Mixing return and purchase:: @@ -443,10 +455,10 @@ Checking the invoice:: >>> mix_credit_note.state u'posted' >>> payable.reload() - >>> (payable.debit, payable.credit) == (Decimal(30), Decimal(60)) + >>> (payable.debit, payable.credit) == (Decimal(30), Decimal(45)) True >>> expense.reload() - >>> (expense.debit, expense.credit) == (Decimal(60), Decimal(30)) + >>> (expense.debit, expense.credit) == (Decimal(45), Decimal(30)) True Mixing stuff with an invoice method 'on shipment'::