diff options
| author | Adrien Widart <awt@odoo.com> | 2020-12-14 09:44:11 +0000 |
|---|---|---|
| committer | Adrien Widart <awt@odoo.com> | 2020-12-17 13:53:44 +0000 |
| commit | 413d932d3cb57bd8cd1f59da950111efcdc53cdc (patch) | |
| tree | f016223d024ce3a18e044fb2f30f6190a3c5fe02 /addons/hr_expense | |
| parent | b389dcabf49fd05c5239d68fd26ef4239963a7e5 (diff) | |
[FIX] hr_expense: expense reconciliation
The reconciliation between an expense and a bank statement does not
update the expense and its sheet.
To reproduce the error:
(Need account_accountant)
1. Go to Expenses
2. Create one (e.g. an expense of $20)
3. Save, Create Report, Approve, Post Journal Entries
4. Go to Accounting > Bank
5. Create a bank statement (e.g. $-20)
6. Save, Post, Reconcile
7. Select the expense, Validate
8. Go to Expense > The created expense
=> The expense's state is still 'Approved'. It should be 'Paid'.
Moreover, if you click on the associated report ("View Report"), the
same error appears: the sheet's state is 'Posted' instead of 'Paid'.
The problem was that the method 'action_invoice_paid' (deleted in this
commit) was never reached. Moreover, there was no way to get the
expense's residual amount.
Notes:
- Manual backporting of 79fc2633734bd1685953079faf53b7ae7fbd7587
- Initial PR #63486
OPW-2389414
closes odoo/odoo#63302
Signed-off-by: adwid <adwid@users.noreply.github.com>
Diffstat (limited to 'addons/hr_expense')
| -rw-r--r-- | addons/hr_expense/models/account_move_line.py | 27 | ||||
| -rw-r--r-- | addons/hr_expense/models/hr_expense.py | 15 | ||||
| -rw-r--r-- | addons/hr_expense/views/hr_expense_views.xml | 1 |
3 files changed, 26 insertions, 17 deletions
diff --git a/addons/hr_expense/models/account_move_line.py b/addons/hr_expense/models/account_move_line.py index 2dd37f32e3b..ae80e369220 100644 --- a/addons/hr_expense/models/account_move_line.py +++ b/addons/hr_expense/models/account_move_line.py @@ -4,28 +4,21 @@ from odoo import api, fields, models -class AccountMove(models.Model): - _inherit = 'account.move' - - def action_invoice_paid(self): - # OVERRIDE to mark as paid the expense sheets. - res = super().action_invoice_paid() - - if self: - expense_sheets = self.env['hr.expense.sheet'].search([ - ('account_move_id', 'in', self.ids), - ('state', '!=', 'done'), - ]) - expense_sheets.set_to_paid() - - return res - - class AccountMoveLine(models.Model): _inherit = "account.move.line" expense_id = fields.Many2one('hr.expense', string='Expense', copy=False, help="Expense where the move line come from") + def reconcile(self): + # OVERRIDE + not_paid_expenses = self.expense_id.filtered(lambda expense: expense.state != 'done') + not_paid_expense_sheets = not_paid_expenses.sheet_id + res = super().reconcile() + paid_expenses = not_paid_expenses.filtered(lambda expense: expense.currency_id.is_zero(expense.amount_residual)) + paid_expenses.write({'state': 'done'}) + not_paid_expense_sheets.filtered(lambda sheet: all(expense.state == 'done' for expense in sheet.expense_line_ids)).set_to_paid() + return res + def _get_attachment_domains(self): attachment_domains = super(AccountMoveLine, self)._get_attachment_domains() if self.expense_id: diff --git a/addons/hr_expense/models/hr_expense.py b/addons/hr_expense/models/hr_expense.py index b173c7cd3ec..9e33b985aad 100644 --- a/addons/hr_expense/models/hr_expense.py +++ b/addons/hr_expense/models/hr_expense.py @@ -74,6 +74,7 @@ class HrExpense(models.Model): domain="[('company_id', '=', company_id), ('type_tax_use', '=', 'purchase')]", string='Taxes') untaxed_amount = fields.Float("Subtotal", store=True, compute='_compute_amount', digits='Account') total_amount = fields.Monetary("Total", compute='_compute_amount', store=True, currency_field='currency_id', tracking=True) + amount_residual = fields.Monetary(string='Amount Due', compute='_compute_amount_residual') company_currency_id = fields.Many2one('res.currency', string="Report Company Currency", related='sheet_id.currency_id', store=True, readonly=False) total_amount_company = fields.Monetary("Total (Company Currency)", compute='_compute_total_amount_company', store=True, currency_field='company_currency_id') company_id = fields.Many2one('res.company', string='Company', required=True, readonly=True, states={'draft': [('readonly', False)], 'refused': [('readonly', False)]}, default=lambda self: self.env.company) @@ -126,6 +127,20 @@ class HrExpense(models.Model): taxes = expense.tax_ids.compute_all(expense.unit_amount, expense.currency_id, expense.quantity, expense.product_id, expense.employee_id.user_id.partner_id) expense.total_amount = taxes.get('total_included') + @api.depends("sheet_id.account_move_id.line_ids") + def _compute_amount_residual(self): + for expense in self: + if not expense.sheet_id: + expense.amount_residual = expense.total_amount + continue + if not expense.currency_id or expense.currency_id == expense.company_id.currency_id: + residual_field = 'amount_residual' + else: + residual_field = 'amount_residual_currency' + payment_term_lines = expense.sheet_id.account_move_id.line_ids \ + .filtered(lambda line: line.expense_id == self and line.account_internal_type in ('receivable', 'payable')) + expense.amount_residual = -sum(payment_term_lines.mapped(residual_field)) + @api.depends('date', 'total_amount', 'company_currency_id') def _compute_total_amount_company(self): for expense in self: diff --git a/addons/hr_expense/views/hr_expense_views.xml b/addons/hr_expense/views/hr_expense_views.xml index 3cac7b337f7..82a2ebaff1a 100644 --- a/addons/hr_expense/views/hr_expense_views.xml +++ b/addons/hr_expense/views/hr_expense_views.xml @@ -145,6 +145,7 @@ </div> <field name="tax_ids" widget="many2many_tags" groups="account.group_account_readonly" attrs="{'readonly': [('is_editable', '=', False)]}" context="{'default_company_id': company_id}"/> <field name="total_amount" widget='monetary' options="{'currency_field': 'currency_id'}"/> + <field name="amount_residual" widget='monetary' options="{'currency_field': 'currency_id'}"/> </group><group> <field name="reference" attrs="{'readonly': [('is_ref_editable', '=', False)]}"/> <field name="date"/> |
