diff --git a/hr_expense_advance_clearing/models/hr_expense_sheet.py b/hr_expense_advance_clearing/models/hr_expense_sheet.py index ef4d7a276..5a061b479 100644 --- a/hr_expense_advance_clearing/models/hr_expense_sheet.py +++ b/hr_expense_advance_clearing/models/hr_expense_sheet.py @@ -117,30 +117,37 @@ def action_sheet_move_create(self): sheet.total_amount, precision_rounding=sheet.currency_id.rounding, ) - move_lines = ( - sheet.account_move_id.line_ids - | sheet.advance_sheet_id.account_move_id.line_ids - ) - account_id = emp_advance.property_account_expense_id.id - adv_move_lines = ( - self.env["account.move.line"] - .sudo() - .search( - [ - ("id", "in", move_lines.ids), - ("account_id", "=", account_id), - ("reconciled", "=", False), - ] - ) - ) - # Reconcile when line more than 1 - if len(adv_move_lines) > 1: - adv_move_lines.with_context(**ctx).reconcile() + # Reconcile advance lines for this sheet + self._reconcile_sheet_advance(sheet, emp_advance, ctx) # Update state on clearing advance when advance residual > total amount if sheet.advance_sheet_id and advance_residual != -1: sheet.write({"state": "done"}) return res + def _reconcile_sheet_advance(self, sheet, emp_advance, ctx=None): + """Reconcile advance of this sheet with the advance_sheet on the advance account.""" + ctx = dict(ctx or {}) + move_lines = ( + sheet.account_move_id.line_ids + | sheet.advance_sheet_id.account_move_id.line_ids + ) + account_id = emp_advance.property_account_expense_id.id + adv_move_lines = ( + self.env["account.move.line"] + .sudo() + .search( + [ + ("id", "in", move_lines.ids), + ("account_id", "=", account_id), + ("reconciled", "=", False), + ] + ) + ) + # Reconcile when line more than 1 + if len(adv_move_lines) > 1: + return adv_move_lines.with_context(**ctx).reconcile() + return False + def open_clear_advance(self): self.ensure_one() action = self.env.ref( diff --git a/hr_expense_advance_clearing/wizard/account_payment_register.py b/hr_expense_advance_clearing/wizard/account_payment_register.py index b90a65113..66b7bc50e 100644 --- a/hr_expense_advance_clearing/wizard/account_payment_register.py +++ b/hr_expense_advance_clearing/wizard/account_payment_register.py @@ -130,6 +130,17 @@ def _create_payment_return_advance(self, ctx, advance_account): def _get_product_advance(self): return self.env.ref("hr_expense_advance_clearing.product_emp_advance") + def reconcile_return_advance( + self, payment, expense_sheet, advance_account, ctx=None + ): + """Reconcile the return advance and + advance move on the advance account.""" + account_move_lines_to_reconcile = self.env["account.move.line"] + for line in payment.move_id.line_ids + expense_sheet.account_move_id.line_ids: + if line.account_id == advance_account and not line.reconciled: + account_move_lines_to_reconcile |= line + return account_move_lines_to_reconcile.with_context(**(ctx or {})).reconcile() + def expense_post_return_advance(self): """This is opposite operation of action_create_payments(), it return remaining advance from employee back to company @@ -163,11 +174,6 @@ def expense_post_return_advance(self): } expense_sheet.message_post(body=body) - # Reconcile the return advance and the advance, - # i.e. lookup on the advance account on move lines - account_move_lines_to_reconcile = self.env["account.move.line"] - for line in payment.move_id.line_ids + expense_sheet.account_move_id.line_ids: - if line.account_id == advance_account and not line.reconciled: - account_move_lines_to_reconcile |= line - res = account_move_lines_to_reconcile.with_context(**ctx).reconcile() - return res + return self.reconcile_return_advance( + payment, expense_sheet, advance_account, ctx + )