From bc8d5dbf2c2ee19b337195bdb4bee8e6ba8508cf Mon Sep 17 00:00:00 2001 From: "Denis Vermylen (dve)" Date: Thu, 4 Aug 2016 14:20:12 +0200 Subject: [PATCH] [REF] account: replace workflow by python code --- addons/account/__openerp__.py | 1 - addons/account/demo/account_invoice_demo.yml | 13 +- addons/account/models/account_invoice.py | 59 +++++++-- .../tests/test_account_customer_invoice.py | 4 +- .../tests/test_account_supplier_invoice.py | 2 +- .../test_bank_statement_reconciliation.py | 2 +- addons/account/tests/test_payment.py | 2 +- addons/account/tests/test_reconciliation.py | 4 +- addons/account/views/account_invoice_view.xml | 18 +-- .../views/account_invoice_workflow.xml | 113 ------------------ addons/account/views/account_unit_test.xml | 2 +- .../account/wizard/account_invoice_refund.py | 2 +- .../account/wizard/account_invoice_state.py | 4 +- addons/account_cancel/views/account_views.xml | 8 +- .../tests/test_delivery_stock_move.py | 4 +- addons/membership/tests/test_membership.py | 4 +- addons/point_of_sale/models/pos_order.py | 6 +- addons/sale/models/account_invoice.py | 4 +- addons/sale/tests/test_sale_order.py | 2 +- addons/sale_stock/tests/test_sale_stock.py | 2 +- addons/website_portal_sale/models/payment.py | 2 +- 21 files changed, 97 insertions(+), 161 deletions(-) delete mode 100644 addons/account/views/account_invoice_workflow.xml diff --git a/addons/account/__openerp__.py b/addons/account/__openerp__.py index 3be3a3bcccd..477655ac2b6 100644 --- a/addons/account/__openerp__.py +++ b/addons/account/__openerp__.py @@ -35,7 +35,6 @@ You could use this simplified accounting in case you work with an (external) acc 'views/account_end_fy.xml', 'views/account_invoice_view.xml', 'data/invoice_action_data.xml', - 'views/account_invoice_workflow.xml', 'views/partner_view.xml', 'views/product_view.xml', 'views/account_analytic_view.xml', diff --git a/addons/account/demo/account_invoice_demo.yml b/addons/account/demo/account_invoice_demo.yml index 52f16f3240d..a925aefa6dd 100644 --- a/addons/account/demo/account_invoice_demo.yml +++ b/addons/account/demo/account_invoice_demo.yml @@ -17,7 +17,8 @@ price_unit: 280.0 quantity: 5.0 - - !workflow {model: account.invoice, action: invoice_open, ref: demo_invoice_1} + !python {model: account.invoice, id: demo_invoice_1}: + self.action_invoice_open() - !record {model: account.invoice, id: demo_invoice_2}: company_id: base.main_company @@ -32,7 +33,8 @@ price_unit: 25 quantity: 20 - - !workflow {model: account.invoice, action: invoice_open, ref: demo_invoice_2} + !python {model: account.invoice, id: demo_invoice_2}: + self.action_invoice_open() - !record {model: account.invoice, id: demo_invoice_3}: company_id: base.main_company @@ -47,7 +49,8 @@ price_unit: 15.0 quantity: 5.0 - - !workflow {model: account.invoice, action: invoice_open, ref: demo_invoice_3} + !python {model: account.invoice, id: demo_invoice_3}: + self.action_invoice_open() - Set the context for supplier bills because the 'type' field is readonly in invoice form views, so it value won't be considered by the yaml import - @@ -90,7 +93,7 @@ if account: line_vals['account_id'] = account.id self._update('account.invoice.line', 'account', line_vals, 'ceo_wages_line') - self.env['account.invoice'].browse(ref('demo_invoice_january_wages')).signal_workflow('invoice_open') + self.env['account.invoice'].browse(ref('demo_invoice_january_wages')).action_invoice_open() - !record {model: account.invoice, id: demo_invoice_equipment_purchase}: company_id: base.main_company @@ -114,7 +117,7 @@ line_vals['invoice_line_tax_ids'] = [(6, 0, [tax_id])] self._update('account.invoice.line', 'account', line_vals, 'coffee_machine_line') self.env['account.invoice'].browse(ref('demo_invoice_equipment_purchase'))._onchange_invoice_line_ids() - self.env['account.invoice'].browse(ref('demo_invoice_equipment_purchase')).signal_workflow('invoice_open') + self.env['account.invoice'].browse(ref('demo_invoice_equipment_purchase')).action_invoice_open() - !python {model: account.bank.statement.line, id: False} : | account = self.env['account.account'].search([ diff --git a/addons/account/models/account_invoice.py b/addons/account/models/account_invoice.py index 87be174486f..bf4604cd21a 100644 --- a/addons/account/models/account_invoice.py +++ b/addons/account/models/account_invoice.py @@ -345,6 +345,17 @@ class AccountInvoice(models.Model): return invoice + @api.multi + def _write(self, vals): + pre_not_reconciled = self.filtered(lambda invoice: not invoice.reconciled) + pre_reconciled = self - pre_not_reconciled + res = super(AccountInvoice, self)._write(vals) + reconciled = self.filtered(lambda invoice: invoice.reconciled) + not_reconciled = self - reconciled + (reconciled & pre_reconciled).filtered(lambda invoice: invoice.state == 'open').action_invoice_paid() + (not_reconciled & pre_not_reconciled).filtered(lambda invoice: invoice.state == 'paid').action_invoice_cancel() + return res + @api.model def fields_view_get(self, view_id=None, view_type=False, toolbar=False, submenu=False): def get_view_id(xid, name): @@ -427,10 +438,6 @@ class AccountInvoice(models.Model): # dummy write on self to trigger recomputations return self.with_context(ctx).write({'invoice_line_ids': []}) - @api.multi - def confirm_paid(self): - return self.write({'state': 'paid'}) - @api.multi def unlink(self): for invoice in self: @@ -517,11 +524,11 @@ class AccountInvoice(models.Model): self.date_due = max(line[0] for line in pterm_list) @api.multi - def action_cancel_draft(self): + def action_invoice_draft(self): + if self.filtered(lambda inv: inv.state != 'cancel'): + raise UserError(_("Invoice must be cancelled in order to reset it to draft.")) # go from canceled state to draft state self.write({'state': 'draft', 'date': False}) - self.delete_workflow() - self.create_workflow() # Delete former printed invoice try: report_invoice = self.env['report']._get_report_from_name('account.report_invoice') @@ -536,6 +543,44 @@ class AccountInvoice(models.Model): attachment.unlink() return True + @api.multi + def action_invoice_proforma2(self): + if self.filtered(lambda inv: inv.state != 'draft'): + raise UserError(_("Invoice must be a draft in order to set it to Pro-forma.")) + return self.write({'state': 'proforma2'}) + + @api.multi + def action_invoice_open(self): + # lots of duplicate calls to action_invoice_open, so we remove those already open + to_open_invoices = self.filtered(lambda inv: inv.state != 'open') + if to_open_invoices.filtered(lambda inv: inv.state not in ['proforma2', 'draft']): + raise UserError(_("Invoice must be in draft or Pro-forma state in order to validate it.")) + to_open_invoices.action_date_assign() + to_open_invoices.action_move_create() + return to_open_invoices.invoice_validate() + + @api.multi + def action_invoice_paid(self): + # lots of duplicate calls to action_invoice_paid, so we remove those already paid + to_pay_invoices = self.filtered(lambda inv: inv.state != 'paid') + if to_pay_invoices.filtered(lambda inv: inv.state != 'open'): + raise UserError(_('Invoice must be validated in order to set it to register payemnt.')) + if to_pay_invoices.filtered(lambda inv: not inv.reconciled): + raise UserError(_('You cannot pay an invoice which is partially paid. You need to reconcile payment entries first.')) + return to_pay_invoices.write({'state': 'paid'}) + + @api.multi + def action_invoice_re_open(self): + if self.filtered(lambda inv: inv.state != 'paid'): + raise UserError(_('Invoice must be paid in order to set it to register payemnt.')) + return self.write({'state': 'open'}) + + @api.multi + def action_invoice_cancel(self): + if self.filtered(lambda inv: inv.state not in ['proforma2', 'draft', 'open']): + raise UserError(_("Invoice must be in draft or Pro-forma state in order to validate it.")) + return self.action_cancel() + @api.multi def get_formview_id(self): """ Update form view id of action to open the invoice """ diff --git a/addons/account/tests/test_account_customer_invoice.py b/addons/account/tests/test_account_customer_invoice.py index f24b9460cc8..0c559892644 100644 --- a/addons/account/tests/test_account_customer_invoice.py +++ b/addons/account/tests/test_account_customer_invoice.py @@ -71,7 +71,7 @@ class TestAccountCustomerInvoice(AccountTestUsers): self.assertEquals(self.account_invoice_customer0.state, 'draft') # I change the state of invoice to "Proforma2" by clicking PRO-FORMA button - self.account_invoice_customer0.signal_workflow('invoice_proforma2') + self.account_invoice_customer0.action_invoice_proforma2() # I check that the invoice state is now "Proforma2" self.assertEquals(self.account_invoice_customer0.state, 'proforma2') @@ -80,7 +80,7 @@ class TestAccountCustomerInvoice(AccountTestUsers): self.assertEquals(len(self.account_invoice_customer0.move_id), 0) # I validate invoice by creating on - self.account_invoice_customer0.signal_workflow('invoice_open') + self.account_invoice_customer0.action_invoice_open() # I check that the invoice state is "Open" self.assertEquals(self.account_invoice_customer0.state, 'open') diff --git a/addons/account/tests/test_account_supplier_invoice.py b/addons/account/tests/test_account_supplier_invoice.py index 81a2517f2db..671cfb2dcb0 100644 --- a/addons/account/tests/test_account_supplier_invoice.py +++ b/addons/account/tests/test_account_supplier_invoice.py @@ -36,7 +36,7 @@ class TestAccountSupplierInvoice(AccountingTestCase): self.assertTrue((invoice.state == 'draft'), "Initially vendor bill state is Draft") #change the state of invoice to open by clicking Validate button - invoice.signal_workflow('invoice_open') + invoice.action_invoice_open() #I cancel the account move which is in posted state and verifies that it gives warning message with self.assertRaises(Warning): diff --git a/addons/account/tests/test_bank_statement_reconciliation.py b/addons/account/tests/test_bank_statement_reconciliation.py index 56f38fa3e69..0aee2f90811 100644 --- a/addons/account/tests/test_bank_statement_reconciliation.py +++ b/addons/account/tests/test_bank_statement_reconciliation.py @@ -66,7 +66,7 @@ class TestBankStatementReconciliation(AccountingTestCase): 'name': '.', 'account_id': self.env['account.account'].search([('user_type_id', '=', self.env.ref('account.data_account_type_revenue').id)], limit=1).id, }) - invoice.signal_workflow('invoice_open') + invoice.action_invoice_open() mv_line = None for l in invoice.move_id.line_ids: diff --git a/addons/account/tests/test_payment.py b/addons/account/tests/test_payment.py index 374ceb9c5f3..d593f4fd8e8 100644 --- a/addons/account/tests/test_payment.py +++ b/addons/account/tests/test_payment.py @@ -55,7 +55,7 @@ class TestPayment(AccountingTestCase): 'name': 'something', 'account_id': self.account_revenue.id, }) - invoice.signal_workflow('invoice_open') + invoice.action_invoice_open() return invoice def reconcile(self, liquidity_aml, amount=0.0, amount_currency=0.0, currency_id=None): diff --git a/addons/account/tests/test_reconciliation.py b/addons/account/tests/test_reconciliation.py index ee31cf4a853..d957f7c9f5a 100644 --- a/addons/account/tests/test_reconciliation.py +++ b/addons/account/tests/test_reconciliation.py @@ -56,7 +56,7 @@ class TestReconciliation(AccountingTestCase): }) #validate invoice - invoice.signal_workflow('invoice_open') + invoice.action_invoice_open() return invoice def make_payment(self, invoice_record, bank_journal, amount=0.0, amount_currency=0.0, currency_id=None): @@ -283,7 +283,7 @@ class TestReconciliation(AccountingTestCase): }) ] }) - invoice.signal_workflow('invoice_open') + invoice.action_invoice_open() # We create a bank statement with two lines of 1.00 USD each. statement = self.acc_bank_stmt_model.create({ 'journal_id': self.bank_journal_usd_id, diff --git a/addons/account/views/account_invoice_view.xml b/addons/account/views/account_invoice_view.xml index cfb13ee4ac4..c10018794ea 100644 --- a/addons/account/views/account_invoice_view.xml +++ b/addons/account/views/account_invoice_view.xml @@ -217,11 +217,11 @@
-