Skip to content

Commit

Permalink
Merge pull request #445 from ecosoft-odoo/16.0-mig-budget_control_adv…
Browse files Browse the repository at this point in the history
…ance_clearing

[16.0][MIG] budget_control_advance_clearing
  • Loading branch information
Saran440 authored Aug 2, 2024
2 parents 951740a + 17a749c commit 8fa43b1
Show file tree
Hide file tree
Showing 28 changed files with 1,609 additions and 0 deletions.
75 changes: 75 additions & 0 deletions budget_control_advance_clearing/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
=======================================================
Budget Control on Expense extension on Advance/Clearing
=======================================================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:bcf8834b892bb6a3bc48ad39bf03d25c79988339d47eac638e12e5de5a1914e3
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Alpha-red.png
:target: https://odoo-community.org/page/development-status
:alt: Alpha
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-ecosoft--odoo%2Fbudgeting-lightgray.png?logo=github
:target: https://github.com/ecosoft-odoo/budgeting/tree/16.0/budget_control_advance_clearing
:alt: ecosoft-odoo/budgeting

|badge1| |badge2| |badge3|

This module relate clearing expense with advance, and make ensure that clearing expense
will make budget commitment only in excess of advance amount.

.. IMPORTANT::
This is an alpha version, the data model and design can change at any time without warning.
Only for development or testing purpose, do not use in production.
`More details on development status <https://odoo-community.org/page/development-status>`_

**Table of contents**

.. contents::
:local:

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/ecosoft-odoo/budgeting/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/ecosoft-odoo/budgeting/issues/new?body=module:%20budget_control_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.

Credits
=======

Authors
~~~~~~~

* Ecosoft

Contributors
~~~~~~~~~~~~

* Kitti Upariphutthiphong <kittiu@ecosoft.co.th>
* Saran Lim. <saranl@ecosoft.co.th>

Maintainers
~~~~~~~~~~~

.. |maintainer-kittiu| image:: https://github.com/kittiu.png?size=40px
:target: https://github.com/kittiu
:alt: kittiu

Current maintainer:

|maintainer-kittiu|

This module is part of the `ecosoft-odoo/budgeting <https://github.com/ecosoft-odoo/budgeting/tree/16.0/budget_control_advance_clearing>`_ project on GitHub.

You are welcome to contribute.
5 changes: 5 additions & 0 deletions budget_control_advance_clearing/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import models
from . import report
from . import wizard
25 changes: 25 additions & 0 deletions budget_control_advance_clearing/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright 2020 Ecosoft Co., Ltd. (http://ecosoft.co.th)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

{
"name": "Budget Control on Expense extension on Advance/Clearing",
"version": "16.0.1.0.0",
"license": "AGPL-3",
"author": "Ecosoft, Odoo Community Association (OCA)",
"website": "https://github.com/ecosoft-odoo/budgeting",
"depends": [
"budget_control_expense",
"hr_expense_advance_clearing",
],
"data": [
"security/ir.model.access.csv",
"views/budget_period_view.xml",
"views/budget_control_view.xml",
"views/hr_expense_view.xml",
"views/budget_commit_forward_view.xml",
],
"installable": True,
"auto_install": True,
"maintainers": ["kittiu"],
"development_status": "Alpha",
}
10 changes: 10 additions & 0 deletions budget_control_advance_clearing/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import base_budget_move
from . import expense_budget_move
from . import advance_budget_move
from . import budget_period
from . import budget_control
from . import hr_expense
from . import budget_commit_forward
from . import account_move
30 changes: 30 additions & 0 deletions budget_control_advance_clearing/models/account_move.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright 2020 Ecosoft Co., Ltd. (http://ecosoft.co.th)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import models


class AccountMove(models.Model):
_inherit = "account.move"

def _recompute_budget_return_advance(self):
for rec in self:
if rec.payment_id.advance_id:
rec.payment_id.advance_id.recompute_budget_move()

def button_draft(self):
res = super().button_draft()
self._recompute_budget_return_advance()
return res

def button_cancel(self):
res = super().button_cancel()
self._recompute_budget_return_advance()
return res

def _reverse_moves(self, default_values_list=None, cancel=False):
res = super()._reverse_moves(
default_values_list=default_values_list, cancel=cancel
)
self._recompute_budget_return_advance()
return res
17 changes: 17 additions & 0 deletions budget_control_advance_clearing/models/advance_budget_move.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright 2020 Ecosoft Co., Ltd. (http://ecosoft.co.th)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import fields, models


class AdvanceBudgetMove(models.Model):
_name = "advance.budget.move"
_inherit = ["expense.budget.move"]
_description = "Advance Budget Moves"

clearing_id = fields.Many2one(
comodel_name="hr.expense",
readonly=True,
index=True,
help="Reference to clearing that uncommit this",
)
18 changes: 18 additions & 0 deletions budget_control_advance_clearing/models/base_budget_move.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2022 Ecosoft Co., Ltd. (http://ecosoft.co.th)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import models


class BudgetDoclineMixin(models.AbstractModel):
_inherit = "budget.docline.mixin"

def _get_domain_fwd_line(self, docline):
"""Change res_model in forward advance to hr.expense.advance"""
if self._budget_model() == "advance.budget.move":
return [
("res_model", "=", "hr.expense.advance"),
("res_id", "=", docline.id),
("forward_id.state", "=", "done"),
]
return super()._get_domain_fwd_line(docline)
66 changes: 66 additions & 0 deletions budget_control_advance_clearing/models/budget_commit_forward.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Copyright 2021 Ecosoft Co., Ltd. (http://ecosoft.co.th)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import fields, models


class BudgetCommitForward(models.Model):
_inherit = "budget.commit.forward"

advance = fields.Boolean(
default=True,
help="If checked, click review budget commitment will pull advance commitment",
)
forward_advance_ids = fields.One2many(
comodel_name="budget.commit.forward.line",
inverse_name="forward_id",
string="Fwd Advances",
domain=[("res_model", "=", "hr.expense.advance")],
)

def _get_budget_docline_model(self):
res = super()._get_budget_docline_model()
if self.advance:
res.append("hr.expense.advance")
return res

def _get_document_number(self, doc):
if doc._name == "hr.expense.advance":
return f"{doc.sheet_id._name},{doc.sheet_id.id}"
return super()._get_document_number(doc)

def _get_name_model(self, res_model, need_replace=False):
if res_model == "hr.expense.advance":
if need_replace:
return "hr_expense"
return "hr.expense"
return super()._get_name_model(res_model, need_replace)

def _get_base_from_extension(self, res_model):
"""For module extension"""
if res_model != "hr.expense.advance":
return super()._get_base_from_extension(res_model)
query_from = "JOIN hr_expense_sheet sheet ON sheet.id = a.sheet_id"
return query_from

def _get_base_domain_extension(self, res_model):
"""For module extension"""
if res_model not in ["hr.expense.advance", "hr.expense"]:
return super()._get_base_domain_extension(res_model)

query_where = " AND a.state != 'cancel'"
# Special case, model = hr.expense with advance
if res_model == "hr.expense.advance":
query_where += " AND a.advance = True AND sheet.clearing_residual > 0.0"
else:
query_where += " AND a.advance = False"
return query_where


class BudgetCommitForwardLine(models.Model):
_inherit = "budget.commit.forward.line"

res_model = fields.Selection(
selection_add=[("hr.expense.advance", "Advance")],
ondelete={"hr.expense.advance": "cascade"},
)
14 changes: 14 additions & 0 deletions budget_control_advance_clearing/models/budget_control.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright 2021 Ecosoft Co., Ltd. (http://ecosoft.co.th)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import fields, models


class BudgetControl(models.Model):
_inherit = "budget.control"

amount_advance = fields.Monetary(
string="Advance",
compute="_compute_budget_info",
help="Sum of advance amount",
)
69 changes: 69 additions & 0 deletions budget_control_advance_clearing/models/budget_period.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Copyright 2020 Ecosoft Co., Ltd. (http://ecosoft.co.th)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import api, fields, models


class BudgetPeriod(models.Model):
_inherit = "budget.period"

advance = fields.Boolean(
string="On Advance",
compute="_compute_control_advance",
store=True,
readonly=False,
help="Control budget on advance approved",
)

def _budget_info_query(self):
query = super()._budget_info_query()
query["info_cols"]["amount_advance"] = ("4_av_commit", True)
return query

@api.model
def check_budget(self, doclines, doc_type="account"):
if not doclines:
return
if doclines._name == "hr.expense":
sheet = doclines.mapped("sheet_id")
sheet.ensure_one()
if sheet.advance:
doc_type = "advance"
doclines = doclines.with_context(
alt_budget_move_model="advance.budget.move",
alt_budget_move_field="advance_budget_move_ids",
)
return super().check_budget(doclines, doc_type=doc_type)

@api.model
def check_budget_precommit(self, doclines, doc_type="account"):
"""If the clearing has related advance, uncommit first"""
budget_moves = False
if doclines._name == "hr.expense":
clearings = doclines.mapped("sheet_id").filtered("advance_sheet_id")
budget_moves = (
clearings.mapped("expense_line_ids")
.with_context(force_commit=True)
.uncommit_advance_budget()
)
res = super().check_budget_precommit(doclines, doc_type=doc_type)
if budget_moves:
budget_moves.unlink()
return res

@api.depends("control_budget")
def _compute_control_advance(self):
for rec in self:
rec.advance = rec.control_budget

@api.model
def _get_eligible_budget_period(self, date=False, doc_type=False):
budget_period = super()._get_eligible_budget_period(date, doc_type)
# Get period control budget.
# if doctype is advance, check special control too.
if doc_type == "advance":
return budget_period.filtered(
lambda l: (l.control_budget and l.advance)
or (not l.control_budget and l.advance)
)
return budget_period
19 changes: 19 additions & 0 deletions budget_control_advance_clearing/models/expense_budget_move.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2020 Ecosoft Co., Ltd. (http://ecosoft.co.th)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import api, models


class ExpenseBudgetMove(models.Model):
_inherit = "expense.budget.move"

@api.depends("sheet_id")
def _compute_source_document(self):
res = super()._compute_source_document()
for rec in self.filtered("sheet_id.advance_sheet_id"):
if hasattr(rec.sheet_id.advance_sheet_id, "number"):
display_name = rec.sheet_id.advance_sheet_id.number
else:
display_name = rec.sheet_id.advance_sheet_id.display_name
rec.source_document = rec.source_document or display_name
return res
Loading

0 comments on commit 8fa43b1

Please sign in to comment.