OCA/hr-expense

[16.0] hr_expense_invoice: migrating to v16, request for guidance with _prepare_bill_vals modules

Closed this issue · 1 comments

As I'm still learning Python, I could use some guidance. It appears like I'm correctly overwriting _prepare_bill_vals but the moves are created incorrectly anyway.
The core hr.expense.sheet now contains a _do_create_moves method, which contains the following line:

moves = self.env['account.move'].create([sheet._prepare_bill_vals() for sheet in own_account_sheets])
_prepare_bill_vals() sets several values for the move to be created, including partner_id and account_id. We need to overwrite these for hr_expense_invoice, so I have the following method:

def _prepare_bill_vals(self):
    prepared_bill_vals = super()._prepare_bill_vals()
    new_line_ids = []
    for line in prepared_bill_vals['line_ids']:
        if 'expense_id' in line[2]:
            expense = self.env['hr.expense'].browse(line[2]['expense_id'])
            if not expense.invoice_id:
                new_line_ids.append(line)
                continue
            new_line = (line[0], line[1], line[2].copy())
            new_line[2]['partner_id'] = expense.invoice_id.partner_id.commercial_partner_id.id
            new_line[2]['account_id'] = expense.invoice_id.line_ids.filtered(lambda l: l.account_type == "liability_payable").account_id.id
            new_line_ids.append(new_line)
    prepared_bill_vals['line_ids'] = new_line_ids
    return prepared_bill_vals

I can see in my debugger that prepared_bill_vals has updated the partner_id and account_id as required, but for some reason the moves are still being created with the Employee that created the expense as the partner_id and the expense category's account as the account_id.
Any guidance would be greatly appreciated! TIA!

Closing as someone else migrated this module. Thanks for migrating!