[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.

OPW-2389414

closes odoo/odoo#63486

Signed-off-by: adwid <adwid@users.noreply.github.com>
This commit is contained in:
Adrien Widart
2020-12-17 06:54:30 +00:00
parent 1b2aef65f4
commit 79fc263373
3 changed files with 26 additions and 17 deletions
+10 -17
View File
@@ -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:
+15
View File
@@ -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:
@@ -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"/>