diff options
| -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"/> |
