From 277a766ae7ef124a517c2f90aad9910070229274 Mon Sep 17 00:00:00 2001 From: Saran440 Date: Tue, 14 Jan 2025 15:36:26 +0700 Subject: [PATCH] [MIG] hr_expense_advance_clearing: Migration to 18.0 --- hr_expense_advance_clearing/__manifest__.py | 2 +- .../models/account_move.py | 28 +++---- .../models/hr_expense_sheet.py | 35 +++++++- .../tests/test_hr_expense_advance_clearing.py | 32 +++---- .../views/hr_expense_views.xml | 21 +++-- .../wizard/account_payment_register.py | 84 +++++-------------- 6 files changed, 93 insertions(+), 109 deletions(-) diff --git a/hr_expense_advance_clearing/__manifest__.py b/hr_expense_advance_clearing/__manifest__.py index 621ab5b88..e847a42f1 100644 --- a/hr_expense_advance_clearing/__manifest__.py +++ b/hr_expense_advance_clearing/__manifest__.py @@ -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", diff --git a/hr_expense_advance_clearing/models/account_move.py b/hr_expense_advance_clearing/models/account_move.py index 7721f1f6f..066404eac 100644 --- a/hr_expense_advance_clearing/models/account_move.py +++ b/hr_expense_advance_clearing/models/account_move.py @@ -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.""" diff --git a/hr_expense_advance_clearing/models/hr_expense_sheet.py b/hr_expense_advance_clearing/models/hr_expense_sheet.py index d1cd471af..fc9fb9e76 100644 --- a/hr_expense_advance_clearing/models/hr_expense_sheet.py +++ b/hr_expense_advance_clearing/models/hr_expense_sheet.py @@ -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", @@ -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 @@ -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 @@ -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"): diff --git a/hr_expense_advance_clearing/tests/test_hr_expense_advance_clearing.py b/hr_expense_advance_clearing/tests/test_hr_expense_advance_clearing.py index 314269e7c..58f4f5266 100644 --- a/hr_expense_advance_clearing/tests/test_hr_expense_advance_clearing.py +++ b/hr_expense_advance_clearing/tests/test_hr_expense_advance_clearing.py @@ -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 @@ -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 @@ -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") @@ -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 @@ -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") @@ -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) @@ -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") @@ -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) @@ -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") diff --git a/hr_expense_advance_clearing/views/hr_expense_views.xml b/hr_expense_advance_clearing/views/hr_expense_views.xml index 03a655ce4..d9ff48c30 100644 --- a/hr_expense_advance_clearing/views/hr_expense_views.xml +++ b/hr_expense_advance_clearing/views/hr_expense_views.xml @@ -5,7 +5,7 @@ Advances hr.expense.sheet - tree,kanban,form,pivot,graph,activity + list,kanban,form,pivot,graph,activity [('state', '!=', 'cancel'), ('advance', '=', True)] Expenses hr.expense.sheet - tree,kanban,form,pivot,graph,activity + list,kanban,form,pivot,graph,activity - - hr.expense.sheet.tree + + hr.expense.sheet.list hr.expense.sheet @@ -206,7 +206,7 @@ @@ -231,7 +231,7 @@ True True @@ -246,6 +246,15 @@ > + diff --git a/hr_expense_advance_clearing/wizard/account_payment_register.py b/hr_expense_advance_clearing/wizard/account_payment_register.py index e46ffa37d..29c8544b6 100644 --- a/hr_expense_advance_clearing/wizard/account_payment_register.py +++ b/hr_expense_advance_clearing/wizard/account_payment_register.py @@ -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""" @@ -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 \ No newline at end of file