Skip to content
Permalink
Browse files

[FIX] account_voucher: round globally

- Enable multi-currency. Company currency = USD, another currency = SGD
- Update the exchange rate
- Create a purchase journal. Assign SGD to this journal
- Create a purchase tax of 7.00 %
- Enable tax rounding globally
- Create a purchase receipt with the following values
  62.04 @ 7% tax
  7.29 @ 7% tax

An error occurs: 'Cannot create unbalanced journal entry.'

Each line of the receipt creates its own tax line, which is therefore
rounded => the total of the receipt is computed with a 'round globally'
configuration, while the taxes are computed with a 'round per line'
configuration.

This is because `apply_taxes=True` is set in the context. This creates
the tax line by forcing the rounding. Therefore, we call the method
`_apply_taxes` explicitly in this specific case, and manually round the
taxes once they are merged.

opw-1938623

closes #31194
  • Loading branch information...
nim-odoo committed Feb 18, 2019
1 parent cece883 commit 3e147220fd3b410ad8b530daccff5f697e1749c6
Showing with 32 additions and 1 deletion.
  1. +32 −1 addons/account_voucher/models/account_voucher.py
@@ -291,6 +291,8 @@ def voucher_move_line_create(self, line_total, move_id, company_currency, curren
:return: Tuple build as (remaining amount not allocated on voucher lines, list of account_move_line created in this method)
:rtype: tuple(float, list of int)
'''
tax_calculation_rounding_method = self.env.user.company_id.tax_calculation_rounding_method
tax_lines_vals = []
for line in self.line_ids:
#create one move line per voucher line where amount is not 0.0
if not line.price_subtotal:
@@ -318,7 +320,36 @@ def voucher_move_line_create(self, line_total, move_id, company_currency, curren
'currency_id': company_currency != current_currency and current_currency or False,
'payment_id': self._context.get('payment_id'),
}
self.env['account.move.line'].with_context(apply_taxes=True).create(move_line)
# When global rounding is activated, we must wait until all tax lines are computed to
# merge them.
if tax_calculation_rounding_method == 'round_globally':
self.env['account.move.line'].create(move_line)
tax_lines_vals += self.env['account.move.line'].with_context(round=False)._apply_taxes(
move_line,
move_line.get('debit', 0.0) - move_line.get('credit', 0.0)
)
else:
self.env['account.move.line'].with_context(apply_taxes=True).create(move_line)

# When round globally is set, we merge the tax lines
if tax_calculation_rounding_method == 'round_globally':
tax_lines_vals_merged = {}
for tax_line_vals in tax_lines_vals:
key = (
tax_line_vals['tax_line_id'],
tax_line_vals['account_id'],
tax_line_vals['analytic_account_id'],
)
if key not in tax_lines_vals_merged:
tax_lines_vals_merged[key] = tax_line_vals
else:
tax_lines_vals_merged[key]['debit'] += tax_line_vals['debit']
tax_lines_vals_merged[key]['credit'] += tax_line_vals['credit']
currency = self.env['res.currency'].browse(company_currency)
for vals in tax_lines_vals_merged.values():
vals['debit'] = currency.round(vals['debit'])
vals['credit'] = currency.round(vals['credit'])
self.env['account.move.line'].create(vals)
return line_total

@api.multi

0 comments on commit 3e14722

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.