trytond-patches/invoice_speedup.diff

501 lines
20 KiB
Diff

diff -r 3681a54fda0a trytond/trytond/modules/account_invoice/invoice.py
--- a/trytond/trytond/modules/account_invoice/invoice.py Tue Jul 07 16:46:50 2015 +0200
+++ b/trytond/trytond/modules/account_invoice/invoice.py Tue Jul 07 17:12:14 2015 +0200
@@ -859,34 +859,35 @@
'''
Return move line
'''
- Currency = Pool().get('currency.currency')
- res = {}
+ pool = Pool()
+ Currency = pool.get('currency.currency')
+ MoveLine = pool.get('account.move.line')
+ line = MoveLine()
if self.currency.id != self.company.currency.id:
with Transaction().set_context(date=self.currency_date):
- res['amount_second_currency'] = Currency.compute(
+ line.amount_second_currency = Currency.compute(
self.company.currency, amount, self.currency)
- res['second_currency'] = self.currency.id
+ line.amount_second_currency = -line.amount_second_currency
+ line.second_currency = self.currency.id
else:
- res['amount_second_currency'] = None
- res['second_currency'] = None
- if amount <= 0:
- res['debit'], res['credit'] = -amount, 0
+ line.amount_second_currency = None
+ line.second_currency = None
+ if amount >= Decimal('0.0'):
+ line.debit = Decimal('0.0')
+ line.credit = amount
else:
- res['debit'], res['credit'] = 0, amount
- if res['amount_second_currency']:
- res['amount_second_currency'] = (
- res['amount_second_currency'].copy_sign(
- res['debit'] - res['credit']))
- res['account'] = self.account.id
+ line.debit = - amount
+ line.credit = Decimal('0.0')
+ line.account = self.account.id
if self.account.party_required:
- res['party'] = self.party.id
- res['maturity_date'] = date
- res['description'] = self.description
- return res
+ line.party = self.party.id
+ line.maturity_date = date
+ line.description = self.description
+ return line
- def create_move(self):
+ def get_move(self):
'''
- Create account move for the invoice and return the created move
+ Compute account move for the invoice and return the created move
'''
pool = Pool()
Move = pool.get('account.move')
@@ -902,9 +903,9 @@
total = Decimal('0.0')
total_currency = Decimal('0.0')
for line in move_lines:
- total += line['debit'] - line['credit']
- if line['amount_second_currency']:
- total_currency += line['amount_second_currency']
+ total += line.debit - line.credit
+ if line.amount_second_currency:
+ total_currency += line.amount_second_currency
term_lines = self.payment_term.compute(total, self.company.currency,
self.invoice_date)
@@ -912,28 +913,23 @@
if not term_lines:
term_lines = [(Date.today(), total)]
for date, amount in term_lines:
- val = self._get_move_line(date, amount)
- if val['amount_second_currency']:
- remainder_total_currency += val['amount_second_currency']
- move_lines.append(val)
+ line = self._get_move_line(date, amount)
+ if line.amount_second_currency:
+ remainder_total_currency += line.amount_second_currency
+ move_lines.append(line)
if not self.currency.is_zero(remainder_total_currency):
- move_lines[-1]['amount_second_currency'] -= \
+ move_lines[-1].amount_second_currency -= \
remainder_total_currency
-
accounting_date = self.accounting_date or self.invoice_date
period_id = Period.find(self.company.id, date=accounting_date)
- move, = Move.create([{
- 'journal': self.journal.id,
- 'period': period_id,
- 'date': accounting_date,
- 'origin': str(self),
- 'company': self.company.id,
- 'lines': [('create', move_lines)],
- }])
- self.write([self], {
- 'move': move.id,
- })
+ move = Move()
+ move.journal = self.journal
+ move.period = period_id
+ move.date = accounting_date
+ move.origin = self
+ move.company = self.company
+ move.lines = move_lines
return move
def set_number(self):
@@ -970,10 +966,9 @@
with Transaction().set_context(
date=self.invoice_date or Date.today()):
number = Sequence.get_id(sequence.id)
- vals = {'number': number}
+ self.number = number
if not self.invoice_date and self.type == 'out':
- vals['invoice_date'] = Transaction().context['date']
- self.write([self], vals)
+ self.invoice_date = Transaction().context['date']
@classmethod
def check_modify(cls, invoices):
@@ -987,9 +982,12 @@
cls.raise_user_error('modify_invoice', (invoice.rec_name,))
def get_rec_name(self, name):
- return (self.number or unicode(self.id)
- + (self.reference and (' ' + self.reference) or '')
- + ' ' + self.party.rec_name)
+ name = self.number or unicode(self.id)
+ if self.reference:
+ name += ' %s' % self.reference
+ if self.party:
+ name += ' %s' % self.party.rec_name
+ return name
@classmethod
def search_rec_name(cls, name, clause):
@@ -1033,7 +1031,7 @@
all_invoices += invoices
update_tax = [i for i in all_invoices if i.state == 'draft']
super(Invoice, cls).write(*args)
- if update_tax:
+ if update_tax and not Transaction().context.get('skip_update_taxes'):
cls.update_taxes(update_tax)
@classmethod
@@ -1201,18 +1199,16 @@
'''
MoveLine = Pool().get('account.move.line')
- new_invoices = []
- for invoice in invoices:
- new_invoice, = cls.create([invoice._credit()])
- new_invoices.append(new_invoice)
- if refund:
- cls.post([new_invoice])
+ new_invoices = cls.create([i._credit() for i in invoices])
+ cls.update_taxes(new_invoices)
+ if refund:
+ cls.post(new_invoices)
+ for invoice, new_invoice in itertools.izip(invoices, new_invoices):
if new_invoice.state == 'posted':
MoveLine.reconcile([l for l in invoice.lines_to_pay
if not l.reconciliation] +
[l for l in new_invoice.lines_to_pay
if not l.reconciliation])
- cls.update_taxes(new_invoices)
return new_invoices
@classmethod
@@ -1232,10 +1228,16 @@
@ModelView.button
@Workflow.transition('validated')
def validate_invoice(cls, invoices):
+ to_write = []
for invoice in invoices:
if invoice.type == 'in':
invoice.set_number()
- invoice.create_move()
+ move = invoice.get_move()
+ if move != invoice.move:
+ invoice.move = invoice.get_move()
+ to_write.extend(([invoice], invoice._save_values))
+ if to_write:
+ cls.write(*to_write)
@classmethod
@ModelView.button
@@ -1243,14 +1245,29 @@
def post(cls, invoices):
Move = Pool().get('account.move')
- moves = []
+ to_create = []
for invoice in invoices:
invoice.set_number()
- moves.append(invoice.create_move())
- cls.write([i for i in invoices if i.state != 'posted'], {
- 'state': 'posted',
- })
- Move.post([m for m in moves if m.state != 'posted'])
+ move = invoice.get_move()
+ if move != invoice.move:
+ to_create.append(move._save_values)
+ invoices2move = {}
+ if to_create:
+ moves = Move.create(to_create)
+ for move in moves:
+ invoices2move[move.origin.id] = move.id
+ to_write = []
+ for invoice in invoices:
+ values = invoice._save_values
+ if invoice.id in invoices2move:
+ values['move'] = invoices2move[invoice.id]
+ if values:
+ to_write.extend(([invoice], values))
+ if to_write:
+ with Transaction().set_context(skip_update_taxes=True):
+ cls.write(*to_write)
+ cls.write(invoices, {'state': 'posted'})
+ Move.post([i.move for i in invoices if i.move.state != 'posted'])
for invoice in invoices:
if invoice.type == 'out':
invoice.print_invoice()
@@ -1289,14 +1306,17 @@
cancel_moves = []
delete_moves = []
+ to_write = []
for invoice in invoices:
if invoice.move:
if invoice.move.state == 'draft':
delete_moves.append(invoice.move)
elif not invoice.cancel_move:
invoice.cancel_move = invoice.move.cancel()
- invoice.save()
cancel_moves.append(invoice.cancel_move)
+ to_write.extend(([invoice], invoice._save_values))
+ if to_write:
+ cls.write(*to_write)
if delete_moves:
Move.delete(delete_moves)
if cancel_moves:
@@ -1852,6 +1872,7 @@
def _compute_taxes(self):
pool = Pool()
Currency = pool.get('currency.currency')
+ TaxLine = pool.get('account.tax.line')
res = []
if self.type != 'line':
@@ -1869,53 +1890,55 @@
date=self.invoice.currency_date):
amount = Currency.compute(self.invoice.currency,
amount, self.invoice.company.currency)
- res.append({
- 'code': base_code,
- 'amount': amount,
- 'tax': tax['tax'],
- })
+ tax_line = TaxLine()
+ tax_line.code = base_code
+ tax_line.amount = amount
+ tax_line.tax = tax['tax']
+ res.append(tax_line)
return res
def get_move_line(self):
'''
- Return a list of move lines values for invoice line
+ Return a list of move lines instances for invoice line
'''
- Currency = Pool().get('currency.currency')
- res = {}
+ pool = Pool()
+ Currency = pool.get('currency.currency')
+ MoveLine = pool.get('account.move.line')
if self.type != 'line':
return []
- res['description'] = self.description
+ line = MoveLine()
+ line.description = self.description
if self.invoice.currency != self.invoice.company.currency:
with Transaction().set_context(date=self.invoice.currency_date):
amount = Currency.compute(self.invoice.currency,
self.amount, self.invoice.company.currency)
- res['amount_second_currency'] = self.amount
- res['second_currency'] = self.invoice.currency.id
+ line.amount_second_currency = self.amount
+ line.second_currency = self.invoice.currency.id
else:
amount = self.amount
- res['amount_second_currency'] = None
- res['second_currency'] = None
+ line.amount_second_currency = None
+ line.second_currency = None
if amount >= 0:
if self.invoice.type == 'out':
- res['debit'], res['credit'] = 0, amount
+ line.debit, line.credit = 0, amount
else:
- res['debit'], res['credit'] = amount, 0
+ line.debit, line.credit = amount, 0
else:
if self.invoice.type == 'out':
- res['debit'], res['credit'] = -amount, 0
+ line.debit, line.credit = -amount, 0
else:
- res['debit'], res['credit'] = 0, -amount
- if res['amount_second_currency']:
- res['amount_second_currency'] = (
- res['amount_second_currency'].copy_sign(
- res['debit'] - res['credit']))
- res['account'] = self.account.id
+ line.debit, line.credit = 0, -amount
+ if line.amount_second_currency:
+ line.amount_second_currency = (
+ line.amount_second_currency.copy_sign(
+ line.debit - line.credit))
+ line.account = self.account.id
if self.account.party_required:
- res['party'] = self.invoice.party.id
+ line.party = self.invoice.party.id
computed_taxes = self._compute_taxes()
if computed_taxes:
- res['tax_lines'] = [('create', [tax for tax in computed_taxes])]
- return [res]
+ line.tax_lines = computed_taxes
+ return [line]
def _credit(self):
'''
@@ -2131,47 +2154,50 @@
def get_move_line(self):
'''
- Return a list of move lines values for invoice tax
+ Return a list of move lines instances for invoice tax
'''
- Currency = Pool().get('currency.currency')
- res = {}
+ pool = Pool()
+ Currency = pool.get('currency.currency')
+ MoveLine = pool.get('account.move.line')
+ TaxLine = pool.get('account.tax.line')
+ line = MoveLine()
if not self.amount:
return []
- res['description'] = self.description
+ line.description = self.description
if self.invoice.currency != self.invoice.company.currency:
with Transaction().set_context(date=self.invoice.currency_date):
amount = Currency.compute(self.invoice.currency, self.amount,
self.invoice.company.currency)
- res['amount_second_currency'] = self.amount
- res['second_currency'] = self.invoice.currency.id
+ line.amount_second_currency = self.amount
+ line.second_currency = self.invoice.currency.id
else:
amount = self.amount
- res['amount_second_currency'] = None
- res['second_currency'] = None
+ line.amount_second_currency = None
+ line.second_currency = None
if amount >= 0:
if self.invoice.type == 'out':
- res['debit'], res['credit'] = 0, amount
+ line.debit, line.credit = 0, amount
else:
- res['debit'], res['credit'] = amount, 0
+ line.debit, line.credit = amount, 0
else:
if self.invoice.type == 'out':
- res['debit'], res['credit'] = -amount, 0
+ line.debit, line.credit = -amount, 0
else:
- res['debit'], res['credit'] = 0, -amount
- if res['amount_second_currency']:
- res['amount_second_currency'] = (
- res['amount_second_currency'].copy_sign(
- res['debit'] - res['credit']))
- res['account'] = self.account.id
+ line.debit, line.credit = 0, -amount
+ if line.amount_second_currency:
+ line.amount_second_currency = (
+ line.amount_second_currency.copy_sign(
+ line.debit - line.credit))
+ line.account = self.account.id
if self.account.party_required:
- res['party'] = self.invoice.party.id
+ line.party = self.invoice.party.id
if self.tax_code:
- res['tax_lines'] = [('create', [{
- 'code': self.tax_code.id,
- 'amount': amount * self.tax_sign,
- 'tax': self.tax and self.tax.id or None
- }])]
- return [res]
+ tax_line = TaxLine()
+ tax_line.code = self.tax_code
+ tax_line.amount = amount * self.tax_sign
+ tax_line.tax = self.tax
+ line.tax_lines = [tax_line]
+ return [line]
def _credit(self):
'''
diff -r 0e69764f2826 trytond/trytond/modules/account_payment_type/invoice.py
--- a/trytond/trytond/modules/account_payment_type/invoice.py Tue Jul 07 10:10:50 2015 +0200
+++ b/trytond/trytond/modules/account_payment_type/invoice.py Tue Jul 07 17:06:02 2015 +0200
@@ -82,7 +82,7 @@
return self.company.party.customer_payment_type.id
def _get_move_line(self, date, amount):
- res = super(Invoice, self)._get_move_line(date, amount)
+ line = super(Invoice, self)._get_move_line(date, amount)
if self.payment_type:
- res['payment_type'] = self.payment_type
- return res
+ line.payment_type = self.payment_type
+ return line
diff -r 6429c9c53cb8 trytond/trytond/modules/account_bank/account.py
--- a/trytond/trytond/modules/account_bank/account.py Tue May 05 14:36:02 2015 +0200
+++ b/trytond/trytond/modules/account_bank/account.py Tue Jul 07 15:36:26 2015 +0200
@@ -179,10 +179,10 @@
'''
Add account bank to move line when post invoice.
'''
- res = super(Invoice, self)._get_move_line(date, amount)
+ line = super(Invoice, self)._get_move_line(date, amount)
if self.bank_account:
- res['bank_account'] = self.bank_account
- return res
+ line.bank_account = self.bank_account
+ return line
@classmethod
def create(cls, vlist):
diff -r 95d77335bdd5 trytond/trytond/modules/analytic_invoice/invoice.py
--- a/trytond/trytond/modules/analytic_invoice/invoice.py Sun Mar 01 15:34:41 2015 +0100
+++ b/trytond/trytond/modules/analytic_invoice/invoice.py Mon Nov 30 14:24:25 2015 +0100
@@ -25,32 +25,33 @@
return result
- def get_analytic_entry(self, entry, value):
- analytic_entry = {}
- analytic_entry['name'] = self.description
- analytic_entry['debit'] = value['debit']
- analytic_entry['credit'] = value['credit']
- analytic_entry['account'] = entry.account.id
- analytic_entry['journal'] = self.invoice.journal.id
- analytic_entry['date'] = (self.invoice.accounting_date or
- self.invoice.invoice_date)
- analytic_entry['reference'] = self.invoice.reference
- analytic_entry['party'] = self.invoice.party.id
- return analytic_entry
+ def get_analytic_entry(self, entry, line):
+ pool = Pool()
+ AnalyticLine = pool.get('analytic_account.line')
+ analytic_line = AnalyticLine()
+ analytic_line.name = self.description
+ analytic_line.debit = line.debit
+ analytic_line.credit = line.credit
+ analytic_line.account = entry.account
+ analytic_line.journal = self.invoice.journal
+ analytic_line.date = (self.invoice.accounting_date
+ or self.invoice.invoice_date)
+ analytic_line.reference = self.invoice.reference
+ analytic_line.party = self.invoice.party
+ return analytic_line
def get_move_line(self):
- values = super(InvoiceLine, self).get_move_line()
+ lines = super(InvoiceLine, self).get_move_line()
if self.analytic_accounts:
- for value in values:
- value['analytic_lines'] = []
- to_create = []
+ for line in lines:
+ analytic_lines = []
for entry in self.analytic_accounts:
if not entry.account:
continue
- to_create.append(self.get_analytic_entry(entry, value))
- if to_create:
- value['analytic_lines'] = [('create', to_create)]
- return values
+ analytic_lines.append(self.get_analytic_entry(entry,
+ line))
+ line.analytic_lines = analytic_lines
+ return lines
class AnalyticAccountEntry: