From aeccf7b15ca3a3bdaeae45433c68203103149870 Mon Sep 17 00:00:00 2001 From: Saran440 Date: Fri, 19 Jul 2024 13:43:13 +0700 Subject: [PATCH] [MIG] hr_expense_advance_clearing: Migration to 17.0 --- hr_expense_advance_clearing/__manifest__.py | 2 +- .../models/account_move.py | 2 +- .../models/account_payment.py | 9 +- .../models/hr_expense.py | 10 +-- .../models/hr_expense_sheet.py | 38 ++++---- .../tests/test_hr_expense_advance_clearing.py | 54 +++++------ .../views/account_payment_view.xml | 5 +- .../views/hr_employee_views.xml | 2 +- .../views/hr_expense_views.xml | 38 ++++---- .../wizard/account_payment_register.py | 89 +++++-------------- 10 files changed, 96 insertions(+), 153 deletions(-) diff --git a/hr_expense_advance_clearing/__manifest__.py b/hr_expense_advance_clearing/__manifest__.py index 679fa9485..621ab5b88 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": "16.0.1.0.1", + "version": "17.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 777fdf0ab..7721f1f6f 100644 --- a/hr_expense_advance_clearing/models/account_move.py +++ b/hr_expense_advance_clearing/models/account_move.py @@ -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( diff --git a/hr_expense_advance_clearing/models/account_payment.py b/hr_expense_advance_clearing/models/account_payment.py index eaf692c5f..10b918b1d 100644 --- a/hr_expense_advance_clearing/models/account_payment.py +++ b/hr_expense_advance_clearing/models/account_payment.py @@ -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): @@ -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 diff --git a/hr_expense_advance_clearing/models/hr_expense.py b/hr_expense_advance_clearing/models/hr_expense.py index e1014be59..04814427f 100644 --- a/hr_expense_advance_clearing/models/hr_expense.py +++ b/hr_expense_advance_clearing/models/hr_expense.py @@ -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, diff --git a/hr_expense_advance_clearing/models/hr_expense_sheet.py b/hr_expense_advance_clearing/models/hr_expense_sheet.py index 9f7608491..ea8ad5e69 100644 --- a/hr_expense_advance_clearing/models/hr_expense_sheet.py +++ b/hr_expense_advance_clearing/models/hr_expense_sheet.py @@ -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( @@ -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" @@ -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")) @@ -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 @@ -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( @@ -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) @@ -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 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 f45f4e3a5..c343d863a 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 @@ -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": "test_email@email.com"} + ) 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( { @@ -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 @@ -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 @@ -201,7 +203,7 @@ 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") @@ -209,7 +211,7 @@ def test_1_clear_equal_advance(self): # 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 @@ -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() @@ -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, @@ -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, @@ -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: @@ -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) diff --git a/hr_expense_advance_clearing/views/account_payment_view.xml b/hr_expense_advance_clearing/views/account_payment_view.xml index 223cb63e8..849fad05e 100644 --- a/hr_expense_advance_clearing/views/account_payment_view.xml +++ b/hr_expense_advance_clearing/views/account_payment_view.xml @@ -22,10 +22,7 @@ - + diff --git a/hr_expense_advance_clearing/views/hr_employee_views.xml b/hr_expense_advance_clearing/views/hr_employee_views.xml index b558ac1f2..ed872da9b 100644 --- a/hr_expense_advance_clearing/views/hr_employee_views.xml +++ b/hr_expense_advance_clearing/views/hr_employee_views.xml @@ -10,7 +10,7 @@ name="action_open_advance_clearing" type="object" icon="fa-bars" - attrs="{'invisible': [('advance_count', '=', 0)]}" + invisible="advance_count == 0" > diff --git a/hr_expense_advance_clearing/views/hr_expense_views.xml b/hr_expense_advance_clearing/views/hr_expense_views.xml index 907db8a18..d82d1e352 100644 --- a/hr_expense_advance_clearing/views/hr_expense_views.xml +++ b/hr_expense_advance_clearing/views/hr_expense_views.xml @@ -129,22 +129,18 @@