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

60
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':
if not getattr(invoice, 'invoice_date', False): continue
invoice.invoice_date = Date.today() if not getattr(invoice, 'invoice_date', False):
if not getattr(invoice, 'accounting_date', False): invoice.invoice_date = Date.today()
invoice.accounting_date = Date.today() if not getattr(invoice, 'accounting_date', False):
invoice.description = sale.reference invoice.accounting_date = Date.today()
invoice.save() invoice.description = sale.reference
Invoice.post(sale.invoices) invoices.extend(([invoice], invoice._save_values))
for payment in sale.payments: to_post.add(invoice)
invoice = sale.invoices[0]
payment.invoice = invoice.id if to_post:
# Because of account_invoice_party_without_vat module Invoice.write(*invoices)
# could be installed, invoice party may be different of Invoice.post(list(to_post))
# payment party if payment party has not any vat
# and both parties must be the same to_write = []
if payment.party != invoice.party: to_do = []
payment.party = invoice.party for sale in sales:
payment.save() 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(): 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):