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 6, 2023
1 parent bfe1a8f commit 7454475
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 95 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
123 changes: 91 additions & 32 deletions hr_expense_advance_clearing/models/hr_expense.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from odoo import _, api, fields, models
from odoo.exceptions import ValidationError
from odoo.tools import float_compare


class HrExpense(models.Model):
Expand Down Expand Up @@ -59,37 +60,95 @@ def onchange_advance(self):
)

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"))
"""Create move line (journal) from clearing"""
move_line_values_by_expense = {}
for expense in self:
advance_to_clear = expense.sheet_id.advance_sheet_residual
move_line_name = (
expense.employee_id.name + ": " + expense.name.split("\n")[0][:64]
)
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)
account_src = expense.account_id
account_dst = expense._get_expense_account_destination()
account_date = (
expense.date
or expense.sheet_id.accounting_date
or fields.Date.context_today(expense)
)

move_line_values = []
unit_amount = expense.unit_amount or expense.total_amount
quantity = expense.quantity if expense.unit_amount else 1
taxes = expense.tax_ids.with_context(round=True).compute_all(
unit_amount, expense.currency_id, quantity, expense.product_id
)
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
balance = expense.total_amount_company - expense.amount_tax_company
amount_currency = expense.total_amount - expense.amount_tax
move_line_src = {
"name": move_line_name,
"quantity": expense.quantity or 1,
"debit": balance if balance > 0 else 0,
"credit": -balance if balance < 0 else 0,
"amount_currency": amount_currency,
"account_id": account_src.id,
"product_id": expense.product_id.id,
"product_uom_id": expense.product_uom_id.id,
"analytic_distribution": expense.analytic_distribution,
"expense_id": expense.id,
"partner_id": partner_id,
"tax_ids": [(6, 0, expense.tax_ids.ids)],
"tax_tag_ids": [(6, 0, taxes["base_tags"])],
"currency_id": expense.currency_id.id,
}
move_line_values.append(move_line_src)
total_amount -= expense.total_amount_company
total_amount_currency -= expense.total_amount

# destination move line
emp_advance = self.env.ref(
"hr_expense_advance_clearing.product_emp_advance"
)
move_line_dst = {
"name": move_line_name,
"debit": total_amount > 0 and total_amount,
"credit": total_amount < 0 and -total_amount,
"account_id": emp_advance.property_account_expense_id.id,
"date_maturity": account_date,
"amount_currency": total_amount_currency,
"currency_id": expense.currency_id.id,
"expense_id": expense.id,
"partner_id": partner_id,
}
# Check clearing > advance, it will split line
credit = move_line_dst["credit"]
# cr payable -> cr advance
remain_payable = 0.0
payable_move_line = []
if (
float_compare(
credit,
advance_to_clear,
precision_rounding=expense.currency_id.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"] = account_dst
# Add destination first
move_line_values.append(move_line_dst)
if payable_move_line:
move_line_values.append(payable_move_line)
move_line_values_by_expense[expense.id] = move_line_values
return move_line_values_by_expense
64 changes: 54 additions & 10 deletions hr_expense_advance_clearing/models/hr_expense_sheet.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
from odoo.tools import float_compare
from odoo.tools.safe_eval import safe_eval
Expand Down Expand Up @@ -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(
Expand All @@ -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,
)
Expand All @@ -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):
Expand Down Expand Up @@ -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),
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions hr_expense_advance_clearing/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,9 @@ <h1 class="title">Employee Advance and Clearing</h1>
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:3f86d32ea88a35bf634ff890883afa7eed74a9d596f50690ca541b95f506662f
!! source digest: sha256:0b18f15b48427652d4b03f061e7f129f606678149ad968880eb441c870633931
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/hr-expense/tree/15.0/hr_expense_advance_clearing"><img alt="OCA/hr-expense" src="https://img.shields.io/badge/github-OCA%2Fhr--expense-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/hr-expense-15-0/hr-expense-15-0-hr_expense_advance_clearing"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/hr-expense&amp;target_branch=15.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/hr-expense/tree/16.0/hr_expense_advance_clearing"><img alt="OCA/hr-expense" src="https://img.shields.io/badge/github-OCA%2Fhr--expense-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/hr-expense-16-0/hr-expense-16-0-hr_expense_advance_clearing"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/hr-expense&amp;target_branch=16.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>Standard Expenses module allow employee to do the expense reimbursement only after the expense has been made.
In other world, employee will need to pay first and reimburse later.</p>
<p>This module, allow company to advance an amount to the employee.
Expand Down Expand Up @@ -470,7 +470,7 @@ <h1><a class="toc-backref" href="#toc-entry-3">Bug Tracker</a></h1>
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/hr-expense/issues">GitHub Issues</a>.
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
<a class="reference external" href="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</a>.</p>
<a class="reference external" href="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**">feedback</a>.</p>
<p>Do not contact contributors directly about support or help with technical issues.</p>
</div>
<div class="section" id="credits">
Expand Down Expand Up @@ -498,7 +498,7 @@ <h2><a class="toc-backref" href="#toc-entry-7">Maintainers</a></h2>
promote its widespread use.</p>
<p>Current <a class="reference external" href="https://odoo-community.org/page/maintainer-role">maintainer</a>:</p>
<p><a class="reference external image-reference" href="https://github.com/kittiu"><img alt="kittiu" src="https://github.com/kittiu.png?size=40px" /></a></p>
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/hr-expense/tree/15.0/hr_expense_advance_clearing">OCA/hr-expense</a> project on GitHub.</p>
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/hr-expense/tree/16.0/hr_expense_advance_clearing">OCA/hr-expense</a> project on GitHub.</p>
<p>You are welcome to contribute. To learn how please visit <a class="reference external" href="https://odoo-community.org/page/Contribute">https://odoo-community.org/page/Contribute</a>.</p>
</div>
</div>
Expand Down
Loading

0 comments on commit 7454475

Please sign in to comment.