Skip to content

Commit

Permalink
[MIG] hr_expense_advance_clearing: Migration to 18.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Saran440 committed Jan 14, 2025
1 parent 8fdce63 commit 277a766
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 109 deletions.
2 changes: 1 addition & 1 deletion hr_expense_advance_clearing/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

{
"name": "Employee Advance and Clearing",
"version": "17.0.1.0.0",
"version": "18.0.1.0.0",
"category": "Human Resources",
"author": "Ecosoft, Odoo Community Association (OCA)",
"license": "AGPL-3",
Expand Down
28 changes: 14 additions & 14 deletions hr_expense_advance_clearing/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,20 @@ def _reverse_moves(self, default_values_list=None, cancel=False):
)

@api.depends(
"line_ids.matched_debit_ids.debit_move_id.move_id.payment_id.is_matched",
"line_ids.matched_debit_ids.debit_move_id.move_id.line_ids.amount_residual",
"line_ids.matched_debit_ids.debit_move_id.move_id.line_ids.amount_residual_currency",
"line_ids.matched_credit_ids.credit_move_id.move_id.payment_id.is_matched",
"line_ids.matched_credit_ids.credit_move_id.move_id.line_ids.amount_residual",
"line_ids.matched_credit_ids.credit_move_id.move_id.line_ids.amount_residual_currency",
"line_ids.balance",
"line_ids.currency_id",
"line_ids.amount_currency",
"line_ids.amount_residual",
"line_ids.amount_residual_currency",
"line_ids.payment_id.state",
"line_ids.full_reconcile_id",
"state",
'line_ids.matched_debit_ids.debit_move_id.move_id.origin_payment_id.is_matched',
'line_ids.matched_debit_ids.debit_move_id.move_id.line_ids.amount_residual',
'line_ids.matched_debit_ids.debit_move_id.move_id.line_ids.amount_residual_currency',
'line_ids.matched_credit_ids.credit_move_id.move_id.origin_payment_id.is_matched',
'line_ids.matched_credit_ids.credit_move_id.move_id.line_ids.amount_residual',
'line_ids.matched_credit_ids.credit_move_id.move_id.line_ids.amount_residual_currency',
'line_ids.balance',
'line_ids.currency_id',
'line_ids.amount_currency',
'line_ids.amount_residual',
'line_ids.amount_residual_currency',
'line_ids.payment_id.state',
'line_ids.full_reconcile_id',
'state'
)
def _compute_amount(self):
"""Compute amount residual for advance clearing case."""
Expand Down
35 changes: 31 additions & 4 deletions hr_expense_advance_clearing/models/hr_expense_sheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ class HrExpenseSheet(models.Model):
clearing_count = fields.Integer(
compute="_compute_clearing_count",
)
payment_return_ids = fields.One2many(
comodel_name="account.payment",
inverse_name="advance_id",
string="Payment Return",
readonly=True,
help="Show reference return advance on advance",
)
return_count = fields.Integer(
compute="_compute_return_count"
)
clearing_residual = fields.Monetary(
string="Amount to clear",
compute="_compute_clearing_residual",
Expand Down Expand Up @@ -85,7 +95,7 @@ def _compute_clearing_residual(self):
property_account_expense_id = emp_advance.with_company(
sheet.company_id
).property_account_expense_id
for line in sheet.sudo().account_move_id.line_ids:
for line in sheet.sudo().account_move_ids.line_ids:
if line.account_id == property_account_expense_id:
residual_company += line.amount_residual
sheet.clearing_residual = residual_company
Expand All @@ -97,12 +107,19 @@ def _compute_amount_payable(self):
)
sheet.amount_payable = -sum(rec_lines.mapped("amount_residual"))

@api.depends("clearing_sheet_ids")
def _compute_clearing_count(self):
for sheet in self:
sheet.clearing_count = len(sheet.clearing_sheet_ids)

def action_sheet_move_create(self):
res = super().action_sheet_move_create()
@api.depends("payment_return_ids")
def _compute_return_count(self):
for sheet in self:
sheet.return_count = len(sheet.payment_return_ids)

def action_sheet_move_post(self):
"""Post journal entries with clearing document"""
res = super().action_sheet_move_post()
for sheet in self:
if not sheet.advance_sheet_id:
continue
Expand Down Expand Up @@ -301,10 +318,20 @@ def action_open_clearings(self):
"name": _("Clearing Sheets"),
"type": "ir.actions.act_window",
"res_model": "hr.expense.sheet",
"view_mode": "tree,form",
"view_mode": "list,form",
"domain": [("id", "in", self.clearing_sheet_ids.ids)],
}

def action_open_payment_return(self):
self.ensure_one()
return {
"name": _("Payment Return"),
"type": "ir.actions.act_window",
"res_model": "account.payment",
"view_mode": "list,form",
"domain": [("id", "in", self.payment_return_ids.ids)],
}

def action_register_payment(self):
action = super().action_register_payment()
if self.env.context.get("hr_return_advance"):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from odoo import fields
from odoo.exceptions import UserError, ValidationError
from odoo.tests.common import Form, tagged
from odoo.tests import Form, tagged
from odoo.tools import mute_logger

from odoo.addons.hr_expense.tests.common import TestExpenseCommon
Expand All @@ -13,9 +13,9 @@
@tagged("-at_install", "post_install")
class TestHrExpenseAdvanceClearing(TestExpenseCommon):
@classmethod
def setUpClass(cls, chart_template_ref=None):
super().setUpClass(chart_template_ref=chart_template_ref)
advance_account = cls.company_data["default_account_assets"]
def setUpClass(cls):
super().setUpClass()
advance_account = cls.company_data["default_account_deferred_expense"]
advance_account.reconcile = True
cls.emp_advance = cls.env.ref("hr_expense_advance_clearing.product_emp_advance")
cls.emp_advance.property_account_expense_id = advance_account
Expand Down Expand Up @@ -171,7 +171,7 @@ def test_1_clear_equal_advance(self):
# ------------------ Advance --------------------------
self.advance.action_submit_sheet()
self.advance.action_approve_expense_sheets()
self.advance.action_sheet_move_create()
self.advance.action_sheet_move_post()
self.assertEqual(self.advance.clearing_residual, 1000.0)
self._register_payment(self.advance.account_move_ids, 1000.0)
self.assertEqual(self.advance.state, "done")
Expand All @@ -184,16 +184,16 @@ def test_1_clear_equal_advance(self):
self.assertEqual(self.clearing_equal.advance_sheet_residual, 1000.0)
self.clearing_equal.action_submit_sheet()
self.clearing_equal.action_approve_expense_sheets()
self.clearing_equal.action_sheet_move_create()
self.clearing_equal.action_sheet_move_post()
# Equal amount, state change to Paid and advance is cleared
self.assertEqual(self.clearing_equal.state, "done")
self.assertEqual(self.clearing_equal.advance_sheet_residual, 0.0)
# Clear this with previous advance is done
self.clearing_more.advance_sheet_id = self.advance
self.clearing_more.action_submit_sheet()
self.clearing_more.action_approve_expense_sheets()
# Can't approved clearing, because advance residual is 0.0
with self.assertRaises(ValidationError):
self.clearing_more.action_sheet_move_create()
self.clearing_more.action_approve_expense_sheets()
# There are 2 clearing in advance
self.assertEqual(self.advance.clearing_count, 2)
# Check link clearing in advance must be equal clearing count
Expand Down Expand Up @@ -221,7 +221,7 @@ def test_2_clear_more_than_advance(self):
# ------------------ Advance --------------------------
self.advance.action_submit_sheet()
self.advance.action_approve_expense_sheets()
self.advance.action_sheet_move_create()
self.advance.action_sheet_move_post()
self.assertEqual(self.advance.clearing_residual, 1000.0)
self._register_payment(self.advance.account_move_ids, 1000.0)
self.assertEqual(self.advance.state, "done")
Expand All @@ -231,7 +231,7 @@ def test_2_clear_more_than_advance(self):
self.assertEqual(self.clearing_more.advance_sheet_residual, 1000.0)
self.clearing_more.action_submit_sheet()
self.clearing_more.action_approve_expense_sheets()
self.clearing_more.action_sheet_move_create()
self.clearing_more.action_sheet_move_post()
# More amount, state not changed to paid, and has to pay 200 more
self.assertEqual(self.clearing_more.state, "post")
self.assertEqual(self.clearing_more.amount_payable, 200.0)
Expand All @@ -244,13 +244,7 @@ def test_3_clear_less_than_advance(self):
# ------------------ Advance --------------------------
self.advance.action_submit_sheet()
self.advance.action_approve_expense_sheets()
self.advance.action_sheet_move_create()
# Test return advance register payment with move state draft
with self.assertRaises(UserError):
self.advance.account_move_ids.button_draft()
self._register_payment(
self.advance.account_move_ids, 200.0, hr_return_advance=True
)
self.advance.action_sheet_move_post()
self.assertEqual(self.advance.clearing_residual, 1000.0)
self._register_payment(self.advance.account_move_ids, 1000.0)
self.assertEqual(self.advance.state, "done")
Expand All @@ -276,7 +270,7 @@ def test_3_clear_less_than_advance(self):
ctx=register_payment["context"],
hr_return_advance=True,
)
self.clearing_less.action_sheet_move_create()
self.clearing_less.action_sheet_move_post()
# Less amount, state set to done. Still remain 200 to be returned
self.assertEqual(self.clearing_less.state, "done")
self.assertEqual(self.clearing_less.advance_sheet_residual, 200.0)
Expand All @@ -301,7 +295,7 @@ def test_4_clearing_product_advance(self):
self.advance.expense_line_ids.clearing_product_id = self.product_a
self.advance.action_submit_sheet()
self.advance.action_approve_expense_sheets()
self.advance.action_sheet_move_create()
self.advance.action_sheet_move_post()
self.assertEqual(self.advance.clearing_residual, 1000.0)
self._register_payment(self.advance.account_move_ids, 1000.0)
self.assertEqual(self.advance.state, "done")
Expand Down
21 changes: 15 additions & 6 deletions hr_expense_advance_clearing/views/hr_expense_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<record id="action_my_hr_advance_sheet" model="ir.actions.act_window">
<field name="name">Advances</field>
<field name="res_model">hr.expense.sheet</field>
<field name="view_mode">tree,kanban,form,pivot,graph,activity</field>
<field name="view_mode">list,kanban,form,pivot,graph,activity</field>
<field name="search_view_id" ref="hr_expense.hr_expense_sheet_view_search" />
<field name="domain">[('state', '!=', 'cancel'), ('advance', '=', True)]</field>
<field
Expand All @@ -23,7 +23,7 @@
<record id="action_my_hr_expense_sheet" model="ir.actions.act_window">
<field name="name">Expenses</field>
<field name="res_model">hr.expense.sheet</field>
<field name="view_mode">tree,kanban,form,pivot,graph,activity</field>
<field name="view_mode">list,kanban,form,pivot,graph,activity</field>
<field name="search_view_id" ref="hr_expense.hr_expense_sheet_view_search" />
<field
name="domain"
Expand Down Expand Up @@ -145,8 +145,8 @@
</field>
</field>
</record>
<record id="view_hr_expense_sheet_tree" model="ir.ui.view">
<field name="name">hr.expense.sheet.tree</field>
<record id="view_hr_expense_sheet_list" model="ir.ui.view">
<field name="name">hr.expense.sheet.list</field>
<field name="model">hr.expense.sheet</field>
<field name="inherit_id" ref="hr_expense.view_hr_expense_sheet_tree" />
<field name="arch" type="xml">
Expand Down Expand Up @@ -206,7 +206,7 @@
</group>
</xpath>
<xpath
expr="//field[@name='expense_line_ids']/tree/field[@name='name']"
expr="//field[@name='expense_line_ids']/list/field[@name='name']"
position="after"
>
<field name="employee_id" invisible="1" />
Expand All @@ -231,7 +231,7 @@
<attribute name="force_save">True</attribute>
</xpath>
<xpath
expr="//field[@name='expense_line_ids']/tree/field[@name='price_unit']"
expr="//field[@name='expense_line_ids']/list/field[@name='price_unit']"
position="attributes"
>
<attribute name="force_save">True</attribute>
Expand All @@ -246,6 +246,15 @@
>
<field name="clearing_count" widget="statinfo" string="Clearings" />
</button>
<button
name="action_open_payment_return"
class="oe_stat_button"
icon="fa-file-text-o"
type="object"
invisible="not advance"
>
<field name="return_count" widget="statinfo" string="Return" />
</button>
</xpath>
</field>
</record>
Expand Down
84 changes: 19 additions & 65 deletions hr_expense_advance_clearing/wizard/account_payment_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
class AccountPaymentRegister(models.TransientModel):
_inherit = "account.payment.register"

def _get_product_advance(self):
return self.env.ref("hr_expense_advance_clearing.product_emp_advance", False)

def _validate_over_return(self):
"""Actual remaining = amount to clear - clear pending
and it is not legit to return more than remaining"""
Expand Down Expand Up @@ -44,66 +41,23 @@ def _validate_over_return(self):
}
)

def action_create_payments(self):
if self._context.get("hr_return_advance", False):
def _init_payments(self, to_process, edit_mode=False):
if self.env.context.get("hr_return_advance"):
self._validate_over_return()
self.expense_post_return_advance()
return {"type": "ir.actions.act_window_close"}
return super().action_create_payments()

def expense_post_return_advance(self):
"""This is opposite operation of action_create_payments(),
it return remaining advance from employee back to company
"""
self.ensure_one()
ctx = self._context.copy()
ctx.update({"skip_account_move_synchronization": True})

if self._context.get("active_model") == "account.move":
lines = (
self.env["account.move"]
.browse(self._context.get("active_ids", []))
.line_ids
)
elif self._context.get("active_model") == "account.move.line":
lines = self.env["account.move.line"].browse(
self._context.get("active_ids", [])
)

expense_sheet = lines.expense_id.sheet_id
emp_advance = self._get_product_advance()
advance_account = emp_advance.property_account_expense_id
# Create return advance and post it
batches = self._get_batches()
first_batch_result = batches[0]
payment_vals = self._create_payment_vals_from_wizard(first_batch_result)
# Set new payment_type and payment entry to be Dr Bank, Cr Advance
payment_vals["advance_id"] = expense_sheet.id
payment_vals["partner_type"] = "customer"
payment_vals["destination_account_id"] = advance_account.id
payment_vals_list = [payment_vals]
payment = (
self.env["account.payment"].with_context(**ctx).create(payment_vals_list)
)
payment.action_post()

# Log the return advance in the chatter
body = _(
"A remaining advance return of %(amount)s %(symbol)s with the reference "
"%(ref)s related to your expense %(name)s has been made."
) % {
"amount": payment.amount,
"symbol": payment.currency_id.symbol,
"ref": payment._get_html_link(),
"name": expense_sheet.name,
}
expense_sheet.message_post(body=Markup(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_ids.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
active_ids = self.env.context.get("active_ids", [])
if self.env.context.get("active_model") == "account.move":
lines = (
self.env["account.move"]
.browse(active_ids)
.line_ids
)
elif self.env.context.get("active_model") == "account.move.line":
lines = self.env["account.move.line"].browse(active_ids)

expense_sheet = lines.expense_id.sheet_id
for x in to_process:
x["create_vals"]["partner_type"] = "customer"
x["create_vals"]["advance_id"] = expense_sheet.id

payments = super()._init_payments(to_process, edit_mode)
return payments

0 comments on commit 277a766

Please sign in to comment.