Skip to content

Commit

Permalink
[MIG] hr_expense_advance_clearing: Migration to 17.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Saran440 committed Jul 19, 2024
1 parent 1f9a613 commit aeccf7b
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 153 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": "16.0.1.0.1",
"version": "17.0.1.0.0",
"category": "Human Resources",
"author": "Ecosoft, Odoo Community Association (OCA)",
"license": "AGPL-3",
Expand Down
2 changes: 1 addition & 1 deletion hr_expense_advance_clearing/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def _check_hr_advance_move_reconciled(self):
av_moves = self.filtered("line_ids.expense_id.sheet_id.advance")
emp_advance = self.env.ref("hr_expense_advance_clearing.product_emp_advance")
reconciled_av_move_lines = av_moves.mapped("line_ids").filtered(
lambda l: l.product_id == emp_advance and l.matching_number
lambda line: line.product_id == emp_advance and line.matching_number
)
if reconciled_av_move_lines:
raise UserError(
Expand Down
9 changes: 8 additions & 1 deletion hr_expense_advance_clearing/models/account_payment.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright 2022 Ecosoft Co., Ltd. (https://ecosoft.co.th)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).

from odoo import fields, models
from odoo import api, fields, models


class AccountPayment(models.Model):
Expand All @@ -20,3 +20,10 @@ def _synchronize_from_moves(self, changed_fields):
else self
)
return super()._synchronize_from_moves(changed_fields)

@api.model
def _get_valid_payment_account_types(self):
account_types = super()._get_valid_payment_account_types()
if self.env.context.get("hr_return_advance"):
account_types.append("asset_current")
return account_types
10 changes: 5 additions & 5 deletions hr_expense_advance_clearing/models/hr_expense.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ def onchange_advance(self):

def _get_move_line_src(self, move_line_name, partner_id):
self.ensure_one()
unit_amount = self.unit_amount or self.total_amount
quantity = self.quantity if self.unit_amount else 1
price_unit = self.price_unit or self.total_amount
quantity = self.quantity if self.price_unit else 1
taxes = self.tax_ids.with_context(round=True).compute_all(
unit_amount, self.currency_id, quantity, self.product_id
price_unit, self.currency_id, quantity, self.product_id
)
amount_currency = self.total_amount - self.amount_tax
balance = self.total_amount_company - self.amount_tax_company
amount_currency = self.total_amount_currency - self.tax_amount_currency
balance = self.total_amount - self.tax_amount
ml_src_dict = {
"name": move_line_name,
"quantity": quantity,
Expand Down
38 changes: 15 additions & 23 deletions hr_expense_advance_clearing/models/hr_expense_sheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ class HrExpenseSheet(models.Model):
string="Clear Advance",
domain="[('advance', '=', True), ('employee_id', '=', employee_id),"
" ('clearing_residual', '>', 0.0)]",
readonly=True,
states={
"draft": [("readonly", False)],
"submit": [("readonly", False)],
"approve": [("readonly", False)],
},
help="Show remaining advance of this employee",
)
clearing_sheet_ids = fields.One2many(
Expand Down Expand Up @@ -64,16 +58,16 @@ def _check_advance_expense(self):
if advance_lines and len(advance_lines) != len(self.expense_line_ids):
raise ValidationError(_("Advance must contain only advance expense line"))

@api.depends("account_move_id.payment_state")
def _compute_payment_state(self):
@api.depends("account_move_ids.payment_state", "account_move_ids.amount_residual")
def _compute_from_account_move_ids(self):
"""After clear advance.
if amount residual is zero, payment state will change to 'paid'
"""
res = super()._compute_payment_state()
res = super()._compute_from_account_move_ids()
for sheet in self:
if (
sheet.advance_sheet_id
and sheet.account_move_id.state == "posted"
and sheet.account_move_ids.state == "posted"
and not sheet.amount_residual
):
sheet.payment_state = "paid"
Expand All @@ -82,20 +76,20 @@ def _compute_payment_state(self):
def _get_product_advance(self):
return self.env.ref("hr_expense_advance_clearing.product_emp_advance", False)

@api.depends("account_move_id.line_ids.amount_residual")
@api.depends("account_move_ids.line_ids.amount_residual")
def _compute_clearing_residual(self):
for sheet in self:
emp_advance = sheet._get_product_advance()
residual_company = 0.0
if emp_advance:
for line in sheet.sudo().account_move_id.line_ids:
for line in sheet.sudo().account_move_ids.line_ids:
if line.account_id == emp_advance.property_account_expense_id:
residual_company += line.amount_residual
sheet.clearing_residual = residual_company

def _compute_amount_payable(self):
for sheet in self:
rec_lines = sheet.account_move_id.line_ids.filtered(
rec_lines = sheet.account_move_ids.line_ids.filtered(
lambda x: x.credit and x.account_id.reconcile and not x.reconciled
)
sheet.amount_payable = -sum(rec_lines.mapped("amount_residual"))
Expand All @@ -116,8 +110,8 @@ def action_sheet_move_create(self):
precision_rounding=sheet.currency_id.rounding,
)
move_lines = (
sheet.account_move_id.line_ids
| sheet.advance_sheet_id.account_move_id.line_ids
sheet.account_move_ids.line_ids
| sheet.advance_sheet_id.account_move_ids.line_ids
)
emp_advance = sheet._get_product_advance()
account_id = emp_advance.property_account_expense_id.id
Expand Down Expand Up @@ -164,14 +158,12 @@ def _get_move_line_vals(self):
)
total_amount = 0.0
total_amount_currency = 0.0
partner_id = (
expense.employee_id.sudo().address_home_id.commercial_partner_id.id
)
partner_id = expense.employee_id.sudo().work_contact_id.id
# source move line
move_line_src = expense._get_move_line_src(move_line_name, partner_id)
move_line_values = [move_line_src]
total_amount -= expense.total_amount_company
total_amount_currency -= expense.total_amount
total_amount -= expense.total_amount
total_amount_currency -= expense.total_amount_currency

# destination move line
move_line_dst = expense._get_move_line_dst(
Expand Down Expand Up @@ -205,7 +197,7 @@ def _get_move_line_vals(self):
payable_move_line["amount_currency"] = -remain_payable
payable_move_line[
"account_id"
] = expense._get_expense_account_destination()
] = expense.sheet_id._get_expense_account_destination()
else:
advance_to_clear -= credit
# Add destination first (if credit is not zero)
Expand All @@ -216,10 +208,10 @@ def _get_move_line_vals(self):
move_line_vals.extend(move_line_values)
return move_line_vals

def _prepare_bill_vals(self):
def _prepare_bills_vals(self):
"""create journal entry instead of bills when clearing document"""
self.ensure_one()
res = super()._prepare_bill_vals()
res = super()._prepare_bills_vals()
if self.advance_sheet_id and self.payment_mode == "own_account":
if (
self.advance_sheet_residual <= 0.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ def setUpClass(cls):
"price_include": True,
}
)
employee_home = cls.env["res.partner"].create({"name": "Employee Home Address"})
employee_home = cls.env["res.partner"].create(
{"name": "Employee Home Address", "email": "[email protected]"}
)
cls.employee = cls.env["hr.employee"].create(
{"name": "Employee A", "address_home_id": employee_home.id}
{"name": "Employee A", "work_contact_id": employee_home.id}
)
advance_account = cls.env["account.account"].create(
{
Expand Down Expand Up @@ -87,7 +89,7 @@ def _create_expense(
expense.employee_id = employee
if not advance:
expense.product_id = product
expense.total_amount = amount
expense.total_amount_currency = amount
expense.payment_mode = payment_mode
expense = expense.save()
expense.tax_ids = False # Test no vat
Expand Down Expand Up @@ -188,10 +190,10 @@ def test_1_clear_equal_advance(self):
"""When clear equal advance, all set"""
# ------------------ Advance --------------------------
self.advance.action_submit_sheet()
self.advance.approve_expense_sheets()
self.advance.action_approve_expense_sheets()
self.advance.action_sheet_move_create()
self.assertEqual(self.advance.clearing_residual, 1000.0)
self._register_payment(self.advance.account_move_id, 1000.0)
self._register_payment(self.advance.account_move_ids, 1000.0)
self.assertEqual(self.advance.state, "done")
# ------------------ Clearing --------------------------
# Clear this with previous advance
Expand All @@ -201,15 +203,15 @@ def test_1_clear_equal_advance(self):
self.clearing_equal.advance_sheet_id = self.advance
self.assertEqual(self.clearing_equal.advance_sheet_residual, 1000.0)
self.clearing_equal.action_submit_sheet()
self.clearing_equal.approve_expense_sheets()
self.clearing_equal.action_approve_expense_sheets()
self.clearing_equal.action_sheet_move_create()
# 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.approve_expense_sheets()
self.clearing_more.action_approve_expense_sheets()
with self.assertRaises(ValidationError):
self.clearing_more.action_sheet_move_create()
# There are 2 clearing in advance
Expand All @@ -227,55 +229,55 @@ def test_1_clear_equal_advance(self):
)
# Check back state move in advance after create clearing
with self.assertRaises(UserError):
self.advance.account_move_id.button_draft()
self.advance.account_move_ids.button_draft()
with self.assertRaises(UserError):
self.advance.account_move_id.button_cancel()
self.advance.account_move_ids.button_cancel()
with self.assertRaises(UserError):
self.advance.account_move_id._reverse_moves()
self.advance.account_move_ids._reverse_moves()

def test_2_clear_more_than_advance(self):
"""When clear more than advance, do pay more"""
# ------------------ Advance --------------------------
self.advance.action_submit_sheet()
self.advance.approve_expense_sheets()
self.advance.action_approve_expense_sheets()
self.advance.action_sheet_move_create()
self.assertEqual(self.advance.clearing_residual, 1000.0)
self._register_payment(self.advance.account_move_id, 1000.0)
self._register_payment(self.advance.account_move_ids, 1000.0)
self.assertEqual(self.advance.state, "done")
# ------------------ Clearing --------------------------
# Clear this with previous advance
self.clearing_more.advance_sheet_id = self.advance
self.assertEqual(self.clearing_more.advance_sheet_residual, 1000.0)
self.clearing_more.action_submit_sheet()
self.clearing_more.approve_expense_sheets()
self.clearing_more.action_approve_expense_sheets()
self.clearing_more.action_sheet_move_create()
# 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)
self._register_payment(self.clearing_more.account_move_id, 200.0)
self._register_payment(self.clearing_more.account_move_ids, 200.0)
self.assertEqual(self.clearing_more.state, "done")

def test_3_clear_less_than_advance(self):
"""When clear less than advance, do return advance"""
# ------------------ Advance --------------------------
self.advance.action_submit_sheet()
self.advance.approve_expense_sheets()
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_id.button_draft()
self.advance.account_move_ids.button_draft()
self._register_payment(
self.advance.account_move_id, 200.0, hr_return_advance=True
self.advance.account_move_ids, 200.0, hr_return_advance=True
)
self.assertEqual(self.advance.clearing_residual, 1000.0)
self._register_payment(self.advance.account_move_id, 1000.0)
self._register_payment(self.advance.account_move_ids, 1000.0)
self.assertEqual(self.advance.state, "done")
# ------------------ Clearing, Return Advance --------------------------
# Clear this with previous advance
self.clearing_less.advance_sheet_id = self.advance
self.assertEqual(self.clearing_less.advance_sheet_residual, 1000.0)
self.clearing_less.action_submit_sheet()
self.clearing_less.approve_expense_sheets()
self.clearing_less.action_approve_expense_sheets()
register_payment = self.advance.with_context(
hr_return_advance=True
).action_register_payment()
Expand All @@ -287,7 +289,7 @@ def test_3_clear_less_than_advance(self):
# Test return advance over residual
with self.assertRaises(UserError):
self._register_payment(
self.advance.account_move_id,
self.advance.account_move_ids,
300.0,
ctx=register_payment["context"],
hr_return_advance=True,
Expand All @@ -298,7 +300,7 @@ def test_3_clear_less_than_advance(self):
self.assertEqual(self.clearing_less.advance_sheet_residual, 200.0)
# Back to advance and do return advance, clearing residual become 0.0
self._register_payment(
self.advance.account_move_id,
self.advance.account_move_ids,
200.0,
ctx=register_payment["context"],
hr_return_advance=True,
Expand All @@ -315,10 +317,10 @@ def test_4_clearing_product_advance(self):
# ------------------ Advance --------------------------
self.advance.expense_line_ids.clearing_product_id = self.product
self.advance.action_submit_sheet()
self.advance.approve_expense_sheets()
self.advance.action_approve_expense_sheets()
self.advance.action_sheet_move_create()
self.assertEqual(self.advance.clearing_residual, 1000.0)
self._register_payment(self.advance.account_move_id, 1000.0)
self._register_payment(self.advance.account_move_ids, 1000.0)
self.assertEqual(self.advance.state, "done")
# ------------------ Clearing --------------------------
with Form(self.env["hr.expense.sheet"]) as sheet:
Expand All @@ -329,10 +331,10 @@ def test_4_clearing_product_advance(self):
self.assertEqual(len(ex_sheet.expense_line_ids), 0)
ex_sheet._onchange_advance_sheet_id()
self.assertEqual(len(ex_sheet.expense_line_ids), 1)
reverse_move = self.advance.account_move_id._reverse_moves(
reverse_move = self.advance.account_move_ids._reverse_moves(
default_values_list=[
{"invoice_date": self.advance.account_move_id.date, "ref": False}
{"invoice_date": self.advance.account_move_ids.date, "ref": False}
],
cancel=True,
)
self.assertNotEqual(reverse_move, self.advance.account_move_id)
self.assertNotEqual(reverse_move, self.advance.account_move_ids)
5 changes: 1 addition & 4 deletions hr_expense_advance_clearing/views/account_payment_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@
<field name="inherit_id" ref="account.view_account_payment_form" />
<field name="arch" type="xml">
<xpath expr="//sheet//group[@name='group2']" position="inside">
<field
name="advance_id"
attrs="{'invisible': [('payment_type', '!=', 'inbound')]}"
/>
<field name="advance_id" invisible="payment_type != 'inbound'" />
</xpath>
</field>
</record>
Expand Down
2 changes: 1 addition & 1 deletion hr_expense_advance_clearing/views/hr_employee_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
name="action_open_advance_clearing"
type="object"
icon="fa-bars"
attrs="{'invisible': [('advance_count', '=', 0)]}"
invisible="advance_count == 0"
>
<field name="advance_count" string="Advance" widget="statinfo" />
</button>
Expand Down
Loading

0 comments on commit aeccf7b

Please sign in to comment.