Refactoring to post/write/do with multiple sales and omit cancel invocies in workflow to end

task-033187
This commit is contained in:
Raimon Esteve 2018-01-17 14:26:42 +01:00
parent 4b1ece67c6
commit a41d4ff251

36
sale.py
View File

@ -57,7 +57,11 @@ class Sale:
def workflow_to_end(cls, sales): def workflow_to_end(cls, sales):
pool = Pool() pool = Pool()
Invoice = pool.get('account.invoice') Invoice = pool.get('account.invoice')
StatementLine = pool.get('account.statement.line')
Date = pool.get('ir.date') Date = pool.get('ir.date')
invoices = []
to_post = set()
for sale in sales: for sale in sales:
if sale.state == 'draft': if sale.state == 'draft':
cls.quote([sale]) cls.quote([sale])
@ -73,27 +77,45 @@ class Sale:
False) False)
if sale.invoices and not grouping: if sale.invoices and not grouping:
for invoice in sale.invoices: for invoice in sale.invoices:
if invoice.state == 'draft': if not invoice.state == 'draft':
continue
if not getattr(invoice, 'invoice_date', False): if not getattr(invoice, 'invoice_date', False):
invoice.invoice_date = Date.today() invoice.invoice_date = Date.today()
if not getattr(invoice, 'accounting_date', False): if not getattr(invoice, 'accounting_date', False):
invoice.accounting_date = Date.today() invoice.accounting_date = Date.today()
invoice.description = sale.reference invoice.description = sale.reference
invoice.save() invoices.extend(([invoice], invoice._save_values))
Invoice.post(sale.invoices) 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: for payment in sale.payments:
invoice = sale.invoices[0] invoices = [invoice for invoice in sale.invoices
payment.invoice = invoice.id if invoice and invoice.state == 'posted']
if not invoices:
continue
payment.invoice = invoices[0]
# Because of account_invoice_party_without_vat module # Because of account_invoice_party_without_vat module
# could be installed, invoice party may be different of # could be installed, invoice party may be different of
# payment party if payment party has not any vat # payment party if payment party has not any vat
# and both parties must be the same # and both parties must be the same
if payment.party != invoice.party: if payment.party != invoice.party:
payment.party = invoice.party payment.party = invoice.party
payment.save() to_write.extend(([payment], payment._save_values))
if sale.is_done(): 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 @classmethod
def get_paid_amount(cls, sales, names): def get_paid_amount(cls, sales, names):