-
-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MIG] hr_expense_advance_clearing: Migration to 16.0
- Loading branch information
Showing
8 changed files
with
196 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
# Copyright 2019 Kitti Upariphutthiphong <[email protected]> | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). | ||
|
||
from odoo import _, api, fields, models | ||
from odoo import Command, _, api, fields, models | ||
from odoo.exceptions import ValidationError | ||
from odoo.tools import float_compare | ||
from odoo.tools.safe_eval import safe_eval | ||
|
@@ -64,6 +64,15 @@ 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): | ||
"""After clear advance. payment state will change to 'paid'""" | ||
res = super()._compute_payment_state() | ||
for sheet in self: | ||
if sheet.advance_sheet_id and sheet.account_move_id.state == "posted": | ||
sheet.payment_state = "paid" | ||
return res | ||
|
||
@api.depends("account_move_id.line_ids.amount_residual") | ||
def _compute_clearing_residual(self): | ||
emp_advance = self.env.ref( | ||
|
@@ -90,13 +99,13 @@ def _compute_clearing_count(self): | |
|
||
def action_sheet_move_create(self): | ||
res = super().action_sheet_move_create() | ||
# Reconcile advance of this sheet with the advance_sheet | ||
emp_advance = self.env.ref("hr_expense_advance_clearing.product_emp_advance") | ||
ctx = self._context.copy() | ||
ctx.update({"skip_account_move_synchronization": True}) | ||
for sheet in self: | ||
if not sheet.advance_sheet_id: | ||
continue | ||
amount_residual_bf_reconcile = sheet.advance_sheet_residual | ||
advance_residual = float_compare( | ||
sheet.advance_sheet_residual, | ||
amount_residual_bf_reconcile, | ||
sheet.total_amount, | ||
precision_rounding=sheet.currency_id.rounding, | ||
) | ||
|
@@ -116,10 +125,45 @@ def action_sheet_move_create(self): | |
] | ||
) | ||
) | ||
adv_move_lines.with_context(**ctx).reconcile() | ||
adv_move_lines.reconcile() | ||
# Update state on clearing advance when advance residual > total amount | ||
if sheet.advance_sheet_id and advance_residual != -1: | ||
sheet.write({"state": "done"}) | ||
if advance_residual != -1: | ||
sheet.write( | ||
{ | ||
"state": "done", | ||
} | ||
) | ||
# Update amount residual and state when advance residual < total amount | ||
else: | ||
sheet.write( | ||
{ | ||
"state": "post", | ||
"payment_state": "not_paid", | ||
"amount_residual": sheet.total_amount | ||
- amount_residual_bf_reconcile, | ||
} | ||
) | ||
return res | ||
|
||
def _prepare_bill_vals(self): | ||
"""create journal entry instead of bills when clearing document""" | ||
self.ensure_one() | ||
res = super()._prepare_bill_vals() | ||
if self.advance_sheet_id and self.payment_mode == "own_account": | ||
if ( | ||
self.advance_sheet_residual <= 0.0 | ||
): # Advance Sheets with no residual left | ||
raise ValidationError( | ||
_("Advance: %s has no amount to clear") % (self.name) | ||
) | ||
res["move_type"] = "entry" | ||
move_line_vals = [] | ||
|
||
for expense in self.expense_line_ids: | ||
# get move line values | ||
move_line_values_by_expense = expense._get_account_move_line_values() | ||
move_line_vals.extend(move_line_values_by_expense.get(expense.id)) | ||
res["line_ids"] = [Command.create(x) for x in move_line_vals] | ||
return res | ||
|
||
def open_clear_advance(self): | ||
|
@@ -156,7 +200,7 @@ def _prepare_clear_advance(self, line): | |
# Prepare the clearing expense | ||
clear_line_dict = { | ||
"advance": False, | ||
"name": False, | ||
"name": line.clearing_product_id.display_name, | ||
"product_id": line.clearing_product_id.id, | ||
"clearing_product_id": False, | ||
"date": fields.Date.context_today(self), | ||
|
@@ -166,7 +210,7 @@ def _prepare_clear_advance(self, line): | |
"av_line_id": line.id, | ||
} | ||
clear_line = self.env["hr.expense"].new(clear_line_dict) | ||
clear_line._compute_from_product_id_company_id() # Set some vals | ||
clear_line._compute_account_id() # Set some vals | ||
# Prepare the original advance line | ||
adv_dict = line._convert_to_write(line._cache) | ||
# remove no update columns | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.