From a41d4ff25184b1685a2e82b2e2759a1c506abf55 Mon Sep 17 00:00:00 2001 From: Raimon Esteve Date: Wed, 17 Jan 2018 14:26:42 +0100 Subject: [PATCH] Refactoring to post/write/do with multiple sales and omit cancel invocies in workflow to end task-033187 --- sale.py | 60 +++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 41 insertions(+), 19 deletions(-) diff --git a/sale.py b/sale.py index 88000c6..7a51271 100644 --- a/sale.py +++ b/sale.py @@ -57,7 +57,11 @@ class Sale: def workflow_to_end(cls, sales): pool = Pool() Invoice = pool.get('account.invoice') + StatementLine = pool.get('account.statement.line') Date = pool.get('ir.date') + + invoices = [] + to_post = set() for sale in sales: if sale.state == 'draft': cls.quote([sale]) @@ -73,27 +77,45 @@ class Sale: False) if sale.invoices and not grouping: for invoice in sale.invoices: - if invoice.state == 'draft': - if not getattr(invoice, 'invoice_date', False): - invoice.invoice_date = Date.today() - if not getattr(invoice, 'accounting_date', False): - invoice.accounting_date = Date.today() - invoice.description = sale.reference - invoice.save() - Invoice.post(sale.invoices) - for payment in sale.payments: - invoice = sale.invoices[0] - payment.invoice = invoice.id - # Because of account_invoice_party_without_vat module - # could be installed, invoice party may be different of - # payment party if payment party has not any vat - # and both parties must be the same - if payment.party != invoice.party: - payment.party = invoice.party - payment.save() + if not invoice.state == 'draft': + continue + if not getattr(invoice, 'invoice_date', False): + invoice.invoice_date = Date.today() + if not getattr(invoice, 'accounting_date', False): + invoice.accounting_date = Date.today() + invoice.description = sale.reference + invoices.extend(([invoice], invoice._save_values)) + to_post.add(invoice) + + if to_post: + Invoice.write(*invoices) + Invoice.post(list(to_post)) + + to_write = [] + to_do = [] + for sale in sales: + for payment in sale.payments: + invoices = [invoice for invoice in sale.invoices + if invoice and invoice.state == 'posted'] + if not invoices: + continue + payment.invoice = invoices[0] + # Because of account_invoice_party_without_vat module + # could be installed, invoice party may be different of + # payment party if payment party has not any vat + # and both parties must be the same + if payment.party != invoice.party: + payment.party = invoice.party + to_write.extend(([payment], payment._save_values)) if sale.is_done(): - cls.do([sale]) + to_do.append(sale) + + if to_write: + StatementLine.write(*to_write) + + if to_do: + cls.do(to_do) @classmethod def get_paid_amount(cls, sales, names):