Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 26 additions & 19 deletions hr_expense_advance_clearing/models/hr_expense_sheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
22 changes: 14 additions & 8 deletions hr_expense_advance_clearing/wizard/account_payment_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
)