Skip to content

Commit

Permalink
[MIG] hr_expense_advance_clearing: Migration to 16.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Saran440 committed Nov 29, 2023
1 parent 4550458 commit e901f3a
Show file tree
Hide file tree
Showing 8 changed files with 232 additions and 111 deletions.
12 changes: 6 additions & 6 deletions hr_expense_advance_clearing/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Employee Advance and Clearing
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:3f86d32ea88a35bf634ff890883afa7eed74a9d596f50690ca541b95f506662f
!! source digest: sha256:0b18f15b48427652d4b03f061e7f129f606678149ad968880eb441c870633931
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
Expand All @@ -17,13 +17,13 @@ Employee Advance and Clearing
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fhr--expense-lightgray.png?logo=github
:target: https://github.com/OCA/hr-expense/tree/15.0/hr_expense_advance_clearing
:target: https://github.com/OCA/hr-expense/tree/16.0/hr_expense_advance_clearing
:alt: OCA/hr-expense
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/hr-expense-15-0/hr-expense-15-0-hr_expense_advance_clearing
:target: https://translation.odoo-community.org/projects/hr-expense-16-0/hr-expense-16-0-hr_expense_advance_clearing
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/builds?repo=OCA/hr-expense&target_branch=15.0
:target: https://runboat.odoo-community.org/builds?repo=OCA/hr-expense&target_branch=16.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|
Expand Down Expand Up @@ -112,7 +112,7 @@ Bug Tracker
Bugs are tracked on `GitHub Issues <https://github.com/OCA/hr-expense/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`feedback <https://github.com/OCA/hr-expense/issues/new?body=module:%20hr_expense_advance_clearing%0Aversion:%2015.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
`feedback <https://github.com/OCA/hr-expense/issues/new?body=module:%20hr_expense_advance_clearing%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Expand Down Expand Up @@ -152,6 +152,6 @@ Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:

|maintainer-kittiu|

This module is part of the `OCA/hr-expense <https://github.com/OCA/hr-expense/tree/15.0/hr_expense_advance_clearing>`_ project on GitHub.
This module is part of the `OCA/hr-expense <https://github.com/OCA/hr-expense/tree/16.0/hr_expense_advance_clearing>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
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": "15.0.1.2.0",
"version": "16.0.1.0.0",
"category": "Human Resources",
"author": "Ecosoft, Odoo Community Association (OCA)",
"license": "AGPL-3",
Expand Down
100 changes: 58 additions & 42 deletions hr_expense_advance_clearing/models/hr_expense.py
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


Expand All @@ -26,12 +26,13 @@ class HrExpense(models.Model):
help="Expense created from this advance expense line",
)

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

@api.constrains("advance")
def _check_advance(self):
for expense in self.filtered("advance"):
emp_advance = self.env.ref(
"hr_expense_advance_clearing.product_emp_advance"
)
emp_advance = expense._get_product_advance()
if not emp_advance.property_account_expense_id:
raise ValidationError(
_("Employee advance product has no payable account")
Expand All @@ -54,42 +55,57 @@ def _check_advance(self):
def onchange_advance(self):
self.tax_ids = False
if self.advance:
self.product_id = self.env.ref(
"hr_expense_advance_clearing.product_emp_advance"
)
self.product_id = self._get_product_advance()

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
taxes = self.tax_ids.with_context(round=True).compute_all(
unit_amount, self.currency_id, quantity, self.product_id
)
amount_currency = self.total_amount - self.amount_tax
balance = self.total_amount_company - self.amount_tax_company
ml_src_dict = {
"name": move_line_name,
"quantity": quantity,
"debit": balance if balance > 0 else 0,
"credit": -balance if balance < 0 else 0,
"amount_currency": amount_currency,
"account_id": self.account_id.id,
"product_id": self.product_id.id,
"product_uom_id": self.product_uom_id.id,
"analytic_distribution": self.analytic_distribution,
"expense_id": self.id,
"partner_id": partner_id,
"tax_ids": [Command.set(self.tax_ids.ids)],
"tax_tag_ids": [Command.set(taxes["base_tags"])],
"currency_id": self.currency_id.id,
}
return ml_src_dict

def _get_account_move_line_values(self):
move_line_values_by_expense = super()._get_account_move_line_values()
# Only when do the clearing, change cr payable to cr advance
emp_advance = self.env.ref("hr_expense_advance_clearing.product_emp_advance")
sheets = self.mapped("sheet_id").filtered("advance_sheet_id")
sheets_x = sheets.filtered(lambda x: x.advance_sheet_residual <= 0.0)
if sheets_x: # Advance Sheets with no residual left
raise ValidationError(
_("Advance: %s has no amount to clear")
% ", ".join(sheets_x.mapped("name"))
)
for sheet in sheets:
advance_to_clear = sheet.advance_sheet_residual
for move_lines in move_line_values_by_expense.values():
payable_move_line = False
for move_line in move_lines:
credit = move_line["credit"]
if not credit:
continue
# cr payable -> cr advance
remain_payable = 0.0
if credit > advance_to_clear:
remain_payable = credit - advance_to_clear
move_line["credit"] = advance_to_clear
advance_to_clear = 0.0
# extra payable line
payable_move_line = move_line.copy()
payable_move_line["credit"] = remain_payable
else:
advance_to_clear -= credit
# advance line
move_line["account_id"] = emp_advance.property_account_expense_id.id
if payable_move_line:
move_lines.append(payable_move_line)
return move_line_values_by_expense
def _get_move_line_dst(
self,
move_line_name,
partner_id,
total_amount,
total_amount_currency,
account_advance,
):
account_date = (
self.date
or self.sheet_id.accounting_date
or fields.Date.context_today(self)
)
ml_dst_dict = {
"name": move_line_name,
"debit": total_amount > 0 and total_amount,
"credit": total_amount < 0 and -total_amount,
"account_id": account_advance.id,
"date_maturity": account_date,
"amount_currency": total_amount_currency,
"currency_id": self.currency_id.id,
"expense_id": self.id,
"partner_id": partner_id,
}
return ml_dst_dict
134 changes: 119 additions & 15 deletions hr_expense_advance_clearing/models/hr_expense_sheet.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# 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 import float_compare, float_is_zero
from odoo.tools.safe_eval import safe_eval


Expand Down Expand Up @@ -64,12 +64,22 @@ 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

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")
def _compute_clearing_residual(self):
emp_advance = self.env.ref(
"hr_expense_advance_clearing.product_emp_advance", False
)
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:
Expand All @@ -90,20 +100,20 @@ 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,
)
move_lines = (
sheet.account_move_id.line_ids
| sheet.advance_sheet_id.account_move_id.line_ids
)
emp_advance = sheet._get_product_advance()
account_id = emp_advance.property_account_expense_id.id
adv_move_lines = (
self.env["account.move.line"]
Expand All @@ -116,10 +126,104 @@ 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 _get_move_line_vals(self):
self.ensure_one()
move_line_vals = []
advance_to_clear = self.advance_sheet_residual
emp_advance = self._get_product_advance()
account_advance = emp_advance.property_account_expense_id
for expense in self.expense_line_ids:
move_line_name = (
expense.employee_id.name + ": " + expense.name.split("\n")[0][:64]
)
total_amount = 0.0
total_amount_currency = 0.0
partner_id = (
expense.employee_id.sudo().address_home_id.commercial_partner_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

# destination move line
move_line_dst = expense._get_move_line_dst(
move_line_name,
partner_id,
total_amount,
total_amount_currency,
account_advance,
)
# Check clearing > advance, it will split line
credit = move_line_dst["credit"]
# cr payable -> cr advance
remain_payable = 0.0
payable_move_line = []
rounding = expense.currency_id.rounding
if (
float_compare(
credit,
advance_to_clear,
precision_rounding=rounding,
)
== 1
):
remain_payable = credit - advance_to_clear
move_line_dst["credit"] = advance_to_clear
move_line_dst["amount_currency"] = -advance_to_clear
advance_to_clear = 0.0
# extra payable line
payable_move_line = move_line_dst.copy()
payable_move_line["credit"] = remain_payable
payable_move_line["amount_currency"] = -remain_payable
payable_move_line[
"account_id"
] = expense._get_expense_account_destination()
else:
advance_to_clear -= credit
# Add destination first (if credit is not zero)
if not float_is_zero(move_line_dst["credit"], precision_rounding=rounding):
move_line_values.append(move_line_dst)
if payable_move_line:
move_line_values.append(payable_move_line)
move_line_vals.extend(move_line_values)
return move_line_vals

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 = self._get_move_line_vals()
res["line_ids"] = [Command.create(x) for x in move_line_vals]
return res

def open_clear_advance(self):
Expand Down Expand Up @@ -156,7 +260,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),
Expand All @@ -166,7 +270,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
Expand Down
Loading

0 comments on commit e901f3a

Please sign in to comment.