Skip to content

Commit

Permalink
Remove the future invoices simulation feature
Browse files Browse the repository at this point in the history
It is now replaced by the more general contract_forecast module.
  • Loading branch information
fcayre committed Oct 13, 2023
1 parent 0f03109 commit c0be09c
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 191 deletions.
6 changes: 3 additions & 3 deletions commown_contract_forecast/models/contract_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ def _prepare_contract_line_forecast_period(
)

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

values["discount"] = discount_values["total"]
if discount_values["descriptions"]:
Expand Down
6 changes: 3 additions & 3 deletions commown_cooperative_campaign/models/discount.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@ def compute(self, contract_line, date):
"""Optin if valid and not simulating before computing the actual value
This is because we optin at first contract invoice generation,
i.e. when the customer receives is device. The sale could
i.e. when the customer receives its device. The sale could
indeed be canceled before this date.
"""

# Use no_check_coop_ws context to disable optin check in the
# `is_valid` method here as the aim is... to optin!
if not self._context.get("is_simulation") and self.with_context(
if not self._context.get("bypass_coop_campaigns") and self.with_context(
no_check_coop_ws=True
).is_valid(contract_line, date):
campaign = self.coupon_campaign_id
Expand Down Expand Up @@ -165,7 +165,7 @@ def _compute_condition_coupon_from_campaign(self, contract_line, date):

# Do not call the cooperative WS if we are simulating
# future invoices...
if not self._context.get("is_simulation"):
if not self._context.get("bypass_coop_campaigns"):
contract = contract_line.contract_id
partner = contract.partner_id
campaign = self.coupon_campaign_id
Expand Down
51 changes: 31 additions & 20 deletions commown_cooperative_campaign/tests/test_discount.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pytz
import requests
import requests_mock
from requests_mock.exceptions import NoMockAddress

from odoo.fields import Date
from odoo.tools import mute_logger
Expand Down Expand Up @@ -44,11 +45,23 @@ def setUp(self):
self.so.partner_id.country_id = self.env.ref("base.fr").id
self.customer_key = self.campaign.coop_partner_identifier(self.so.partner_id)

def invoice(self, optin, optout=None, mock_optin=False, check_mock_calls=True):
def invoice(
self,
optin,
optout=None,
mock_optin=False,
check_mock_calls=True,
env=None,
):
"""Create an invoice from contract mocking the cooperative web service
with give optin and optout dates generators.
"""
date = self.contract.recurring_next_date
if env is None:
contract = self.contract
else:
contract = env["contract.contract"].browse(self.contract.id)

date = contract.recurring_next_date

events = [{"type": "optin", "ts": optin(date)}]
if optout is not None:
Expand All @@ -66,11 +79,11 @@ def invoice(self, optin, optout=None, mock_optin=False, check_mock_calls=True):
"id": 1,
"campaign": {},
"member": {},
"optin_ts": ts_after(self.contract.recurring_next_date, 0),
"optin_ts": ts_after(contract.recurring_next_date, 0),
"optout_ts": None,
},
)
invoice = self.contract.recurring_create_invoice()
invoice = contract.recurring_create_invoice()

if check_mock_calls:
reqs = rm.request_history
Expand Down Expand Up @@ -190,22 +203,20 @@ def test_contract_end(self):
)
self.contract.with_context(test_queue_job_no_delay=True).date_end = date_end

def test_is_simulation(self):
"Don't call optin WS when simulating the future invoices"
def test_bypass_coop_campaigns(self):
"Don't call optin WS when bypass_coop_campaigns is in the context"

pypath = "odoo.addons.commown_cooperative_campaign.models.discount."
optin_path = pypath + "coop_ws_optin"
comp_path = pypath + (
"ContractTemplateAbstractDiscountLine."
"_compute_condition_coupon_from_campaign"
)
def do_test(env):
before1 = partial(ts_before, days=1)
self.invoice(before1, mock_optin=False, check_mock_calls=False, env=env)

with mock.patch(optin_path) as m_optin:
with mock.patch(comp_path) as m_compute:
report = self.env.ref(
"contract_variable_discount.report_simulate_payments_html"
)
report.render({"docs": self.contract}, "ir.qweb")
# Check test prequisite without modifying the DB, to revert the side effect of
# this check (which otherwise would create an invoice and prevent a new optin
# call when repeating):
with self.env.cr.savepoint():
with self.assertRaises(NoMockAddress) as err:
do_test(self.env)

self.assertTrue(m_compute.call_count > 1)
self.assertEqual(m_optin.call_count, 0)
# Same call with same DB state but with the bypass_coop_campaigns context
# variable should not raise:
do_test(self.env(context=dict(self.env.context, bypass_coop_campaigns=True)))
2 changes: 0 additions & 2 deletions contract_variable_discount/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
"web_domain_field",
],
"data": [
"data/report.xml",
"data/report_simulate_payments.xml",
"security/ir.model.access.csv",
"views/contract.xml",
"views/contract_template.xml",
Expand Down
14 changes: 0 additions & 14 deletions contract_variable_discount/data/report.xml

This file was deleted.

59 changes: 0 additions & 59 deletions contract_variable_discount/data/report_simulate_payments.xml

This file was deleted.

59 changes: 0 additions & 59 deletions contract_variable_discount/models/contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

import logging
import uuid

from odoo import _, api, fields, models

Expand Down Expand Up @@ -171,61 +170,3 @@ def _convert_contract_lines(self, contract):
):
contract_line.contract_template_line_id = contract_template_line
return new_lines

@api.multi
def simulate_payments(self):
self.ensure_one()

max_date = self.date_start

for contract_line in self.contract_line_ids:
for discount_line in contract_line._applicable_discount_lines():
date_start = discount_line._compute_date(contract_line, "start")
if date_start > max_date:
max_date = date_start
if discount_line.end_type != "empty":
date_end = discount_line._compute_date(contract_line, "end")
if date_end > max_date:
max_date = date_end

_logger.debug("Contract payment simulation until: %s...", max_date)

inv_data = []
last_amount = None

point_name = uuid.uuid1().hex
self.env.cr.execute('SAVEPOINT "%s"' % point_name)

# This is ugly (as is_auto_pay is introduced by contract_payment_auto)
# BUT it is so dangerous not to unset it in the simulation that we
# prefer this over more complex and not so secure solutions...
if getattr(self, "is_auto_pay", False):
self.is_auto_pay = False

last_date = self.recurring_next_date
try:
while last_date < max_date:
# Use context to make it possible to avoid external side effects
# (cooperative campaign through http api for instance):
inv = self.with_context(is_simulation=True).recurring_create_invoice()
last_date = inv.date_invoice
if last_amount != inv.amount_total:
_logger.debug(
"> KEEP invoice %s (amount %s)",
inv.date_invoice,
inv.amount_total,
)
last_amount = inv.amount_total
data = inv.read()[0]
data["invoice_line_ids"] = inv.invoice_line_ids.read()
inv_data.append(data)
else:
_logger.debug(
"> SKIP invoice %s (amount %s)",
inv.date_invoice,
inv.amount_total,
)

return inv_data
finally:
self.env.cr.execute('ROLLBACK TO SAVEPOINT "%s"' % point_name)
31 changes: 0 additions & 31 deletions contract_variable_discount/tests/test_contract.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from datetime import date

import lxml.html
from mock import patch

from odoo import fields, models
Expand Down Expand Up @@ -280,33 +279,3 @@ def test_condition_and_description(self):
inv3.mapped("invoice_line_ids.name"),
["Services from 04/29/2016 to 05/28/2016"],
)

def test_simulate_payments(self):
contract = self.contract
self.set_cdiscounts(self._discounts_1())

contract.recurring_next_date = contract.date_start
contract.contract_line_ids.recurring_next_date = contract.date_start

report = self.env.ref(
"contract_variable_discount.report_simulate_payments_html"
)
fragment = report.render({"docs": contract}, "ir.qweb")

html = (
'<html><head><meta charset="utf-8"/></head>'
"<body>%s<//body></html>" % fragment
)
doc = lxml.html.fromstring(html)

# First column contains dates:
self.assertEqual(
doc.xpath("//tbody/tr/td[1][not(@colspan)]/text()"),
["2016-02-15", "2018-02-15", "2019-02-15"],
)

# Fourth contains discounts:
self.assertEqual(
[n.text_content() for n in doc.xpath("//tbody/tr/td[4]")],
["5.0 %", "15.0 %", "25.0 %"],
)

0 comments on commit c0be09c

Please sign in to comment.