Skip to content

Commit

Permalink
[ADD] account: compute taxes line on account.move with onchange
Browse files Browse the repository at this point in the history
Allow the user to set taxes on lines when creating a new account.move.

task: https://www.odoo.com/web#id=33802&view_type=form&model=project.task&action=333&active_id=967&menu_id=4720
  • Loading branch information
smetl committed Jan 4, 2018
1 parent d9436e3 commit c598b05
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
46 changes: 46 additions & 0 deletions addons/account/models/account_move.py
Expand Up @@ -116,6 +116,52 @@ def _onchange_date(self):
reverse_date = fields.Date(string='Reversal Date', help='Date of the reverse accounting entry.')
reverse_entry_id = fields.Many2one('account.move', String="Reverse entry", store=True, readonly=True)

@api.onchange('line_ids')
def _onchange_line_ids(self):
'''Compute additional lines corresponding to the taxes set on the line_ids.
For example, add a line with 1000 debit and 15% tax, this onchange will add a new
line with 150 debit.
'''
# Retrieve the existing tax lines and drop them.
self.line_ids -= self.line_ids.filtered(lambda l: l.tax_line_id)

for line in self.line_ids:
if not line.tax_ids:
continue
balance = line.debit - line.credit
taxes_vals = line.tax_ids.compute_all(balance,
currency=line.currency_id, product=line.product_id, partner=line.partner_id)
bank_id = line.statement_id

# Create a new line for each tax.
for tax_vals in taxes_vals['taxes']:
name = line.name and line.name + ' ' + line['name'] or tax_vals['name']
tax = self.env['account.tax'].browse([tax_vals['id']])
line_vals = {
'account_id': tax.account_id.id,
'name': name,
'journal_id': line.journal_id.id,
'company_id': line.journal_id.company_id.id,
'tax_line_id': tax_vals['id'],
'partner_id': line.partner_id.id,
'statement_id': line.statement_id.id,
'debit': tax_vals['amount'] > 0 and tax_vals['amount'] or 0.0,
'credit': tax_vals['amount'] < 0 and -tax_vals['amount'] or 0.0,
'analytic_account_id': line.analytic_account_id.id if tax.analytic else False,
'date': line.date,
'move_id': self.id,
}
if bank_id:
context = {'date': line.date}
from_currency_id = bank_id.company_id.currency_id
to_currency_id = bank_id.currency_id
amount_currency = from_currency_id.with_context(context).compute(
tax_vals['amount'], currency=to_currency_id, round=True)
line_vals['currency_id'] = to_currency_id.id
line_vals['amount_currency'] = amount_currency
self.env['account.move.line'].new(line_vals)

@api.model
def fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False):
res = super(AccountMove, self).fields_view_get(
Expand Down
3 changes: 3 additions & 0 deletions addons/account/views/account_view.xml
Expand Up @@ -1468,6 +1468,7 @@
<field name="line_ids" widget="one2many_list"
context="{'line_ids': line_ids, 'journal_id': journal_id }">
<tree editable="bottom" string="Journal Items">
<field name="tax_line_id" invisible="1"/>
<field name="account_id" domain="[('company_id', '=', parent.company_id), ('deprecated', '=', False)]"/>
<field name="partner_id"
domain="['|', ('parent_id', '=', False), ('is_company', '=', True)]"/>
Expand All @@ -1480,6 +1481,8 @@
<field name="currency_id" options="{'no_create': True}" groups="base.group_multi_currency"/>
<field name="debit" sum="Total Debit"/>
<field name="credit" sum="Total Credit"/>
<field name="tax_ids" string="Tax Applied" widget="many2many_tags"
options="{'no_create': True}"/>
<field name="date_maturity" required="0" invisible="context.get('view_no_maturity', False)"/>
</tree>
</field>
Expand Down

0 comments on commit c598b05

Please sign in to comment.