Skip to content

Commit

Permalink
Add contract_forecast and its sibling module commown_contract_forecast
Browse files Browse the repository at this point in the history
  • Loading branch information
fcayre committed Oct 10, 2023
1 parent 5f27af6 commit 1a98000
Show file tree
Hide file tree
Showing 15 changed files with 199 additions and 0 deletions.
1 change: 1 addition & 0 deletions commown/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# Commown modules
"account_invoice_merge_auto_pay",
"account_move_slimpay_import",
"commown_contract_forecast",
"commown_shipping",
"commown_ergonomy_asset",
"commown_lead_risk_analysis",
Expand Down
35 changes: 35 additions & 0 deletions commown_contract_forecast/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
**This file is going to be generated by oca-gen-addon-readme.**

*Manual changes will be overwritten.*

Please provide content in the ``readme`` directory:

* **DESCRIPTION.rst** (required)
* INSTALL.rst (optional)
* CONFIGURE.rst (optional)
* **USAGE.rst** (optional, highly recommended)
* DEVELOP.rst (optional)
* ROADMAP.rst (optional)
* HISTORY.rst (optional, recommended)
* **CONTRIBUTORS.rst** (optional, highly recommended)
* CREDITS.rst (optional)

Content of this README will also be drawn from the addon manifest,
from keys such as name, authors, maintainers, development_status,
and license.

A good, one sentence summary in the manifest is also highly recommended.


Automatic changelog generation
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

`HISTORY.rst` can be auto generated using `towncrier <https://pypi.org/project/towncrier>`_.

Just put towncrier compatible changelog fragments into `readme/newsfragments`
and the changelog file will be automatically generated and updated when a new fragment is added.

Please refer to `towncrier` documentation to know more.

NOTE: the changelog will be automatically generated when using `/ocabot merge $option`.
If you need to run it manually, refer to `OCA/maintainer-tools README <https://github.com/OCA/maintainer-tools>`_.
1 change: 1 addition & 0 deletions commown_contract_forecast/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
19 changes: 19 additions & 0 deletions commown_contract_forecast/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2023 Commown SCIC
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl-3.0)

{
"name": "Commown contract forecast integration",
"summary": "Integrate the contract_forecast module into Commown odoo-based management software",
"version": "12.0.1.0.0",
"development_status": "Alpha",
"category": "Accounting/Accounting",
"website": "https://commown.coop",
"author": "Commown SCIC",
"maintainers": ["fcayre"],
"license": "AGPL-3",
"installable": True,
"depends": [
"contract_forecast",
"commown_contract_variable_discount",
],
}
2 changes: 2 additions & 0 deletions commown_contract_forecast/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import contract_line
from . import contract_line_forecast_period
36 changes: 36 additions & 0 deletions commown_contract_forecast/models/contract_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from odoo import _, api, models


class ContractLine(models.Model):

_inherit = "contract.line"

@api.multi
def _prepare_contract_line_forecast_period(
self, period_date_start, period_date_end, recurring_next_date
):
values = super()._prepare_contract_line_forecast_period(
period_date_start,
period_date_end,
recurring_next_date,
)

# By-pass cooperative campaigns discount computations
discount_values = self.with_context(is_simulation=True).compute_discount(
period_date_start
)

values["discount"] = discount_values["total"]
if discount_values["descriptions"]:
values["name"] += "\n" + (
_("Applied discounts:\n- %s")
% "\n- ".join(discount_values["descriptions"])
)

return values

@api.model
def _get_forecast_update_trigger_fields(self):
fields = super()._get_forecast_update_trigger_fields()
fields.remove("recurring_next_date")
return fields
15 changes: 15 additions & 0 deletions commown_contract_forecast/models/contract_line_forecast_period.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from odoo import fields, models


class ContractLineForecastPeriod(models.Model):

_inherit = "contract.line.forecast.period"

contract_template_id = fields.Many2one(
comodel_name="contract.template",
string="Contract template",
readonly=True,
related="contract_line_id.contract_id.contract_template_id",
store=True,
index=True,
)
1 change: 1 addition & 0 deletions commown_contract_forecast/readme/CONFIGURE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
See contract_forecast setup instructions.
1 change: 1 addition & 0 deletions commown_contract_forecast/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Florent Cayré <florent@commown.coop> https://commown.coop
3 changes: 3 additions & 0 deletions commown_contract_forecast/readme/CREDITS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
The development of this module has been financially supported by:

* Commown SCIC
2 changes: 2 additions & 0 deletions commown_contract_forecast/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This module extends the functionality of contract_forecast to integrate it with
other Commown-specific modules and practices.
1 change: 1 addition & 0 deletions commown_contract_forecast/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_contract_line
75 changes: 75 additions & 0 deletions commown_contract_forecast/tests/test_contract_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Copyright 2023 Commown SCIC
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl-3.0)

from odoo import fields

from odoo.addons.contract.tests.test_contract import TestContractBase
from odoo.addons.queue_job.tests.common import trap_jobs


class ContractLineComputeForecastTC(TestContractBase):
"""XXX maybe use a mixin from the contract_variable_discount module
Also wondering if commown_contract_variable_discount would not be required here as
cooperative campaigns have a specific behaviour in forecasts.
"""

def cdiscount(self, c_line=None, **kwargs):
kwargs.setdefault("contract_line_id", (c_line or self.acct_line).id)
return self.env["contract.discount.line"].create(kwargs)

def set_cdiscounts(self, discounts):
self.acct_line.specific_discount_line_ids = discounts

def test_forecast_with_discount(self):
"Check that forecast take discounts into account"

self.set_cdiscounts(
self.cdiscount(
name="1 year loyalty",
amount_type="percent",
amount_value=10.0,
start_reference="date_start",
start_value=1,
start_unit="years",
end_type="relative",
end_reference="date_start",
end_value=2,
end_unit="years",
)
| self.cdiscount(
name="2 year loyalty",
amount_type="percent",
amount_value=20.0,
start_reference="date_start",
start_value=2,
start_unit="years",
end_type="absolute",
end_value=False, # no end
)
)

date = fields.Date.today()
clines = self.contract.contract_line_ids
clines.write({"recurring_next_date": date})

self.contract.partner_id.company_id.update(
{
"enable_contract_forecast": True,
"contract_forecast_rule_type": "monthly",
"contract_forecast_interval": 36,
}
)

self.assertFalse(self.contract.mapped("contract_line_ids.forecast_period_ids"))

with trap_jobs() as trap:
self.contract.date_start = date

trap.assert_jobs_count(1, only=clines._generate_forecast_periods)
trap.perform_enqueued_jobs()

self.assertEqual(
clines.mapped("forecast_period_ids").mapped("discount"),
[0.0] * 12 + [10.0] * 12 + [20.0] * 12,
)
6 changes: 6 additions & 0 deletions setup/commown_contract_forecast/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)

0 comments on commit 1a98000

Please sign in to comment.