Skip to content

Commit

Permalink
[FIX] contract_sale_generation
Browse files Browse the repository at this point in the history
  • Loading branch information
etobella committed Oct 18, 2023
1 parent 0b50e4f commit 5b7145f
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 25 deletions.
7 changes: 7 additions & 0 deletions contract_sale_generation/models/contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@
# Copyright 2018 Therp BV <https://therp.nl>.
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

import logging

from odoo import _, api, fields, models

_logger = logging.getLogger(__name__)


class ContractContract(models.Model):
_inherit = "contract.contract"
Expand Down Expand Up @@ -70,6 +74,9 @@ def recurring_create_sale(self):
This method triggers the creation of the next sale order of the
contracts even if their next sale order date is in the future.
"""
_logger.warning(
"recurring_create_invoice is no longer necessary. It should be removed on v17"
)
sales = self._recurring_create_sale()
for sale_rec in sales:
self.message_post(
Expand Down
46 changes: 34 additions & 12 deletions contract_sale_generation/tests/test_contract_sale.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ def to_date(date):


class TestContractSale(ContractSaleCommon, SavepointCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.contract.generation_type = "sale"

def test_check_discount(self):
with self.assertRaises(ValidationError):
self.contract_line.write({"discount": 120})
Expand All @@ -24,7 +29,9 @@ def test_contract(self):
self.assertAlmostEqual(self.contract_line.price_subtotal, 50.0)
self.contract_line.price_unit = 100.0
self.contract.partner_id = self.partner.id
self.contract.recurring_create_sale()
self.env["contract.manually.single.invoice"].create(
{"date": self.contract.recurring_next_date, "contract_id": self.contract.id}
).create_invoice()
self.sale_monthly = self.contract._get_related_sales()
self.assertTrue(self.sale_monthly)
self.assertEqual(self.contract_line.recurring_next_date, recurring_next_date)
Expand All @@ -39,7 +46,9 @@ def test_contract_autoconfirm(self):
self.assertAlmostEqual(self.contract_line.price_subtotal, 50.0)
self.contract_line.price_unit = 100.0
self.contract.partner_id = self.partner.id
self.contract.recurring_create_sale()
self.env["contract.manually.single.invoice"].create(
{"date": self.contract.recurring_next_date, "contract_id": self.contract.id}
).create_invoice()
self.sale_monthly = self.contract._get_related_sales()
self.assertTrue(self.sale_monthly)
self.assertEqual(self.contract_line.recurring_next_date, recurring_next_date)
Expand Down Expand Up @@ -77,19 +86,30 @@ def test_onchange_contract_template_id(self):
self.assertDictEqual(res, self.template_vals)

def test_contract_count_sale(self):
self.contract.recurring_create_sale()
self.contract.recurring_create_sale()
self.contract.recurring_create_sale()
self.env["contract.manually.single.invoice"].create(
{"date": self.contract.recurring_next_date, "contract_id": self.contract.id}
).create_invoice()
self.env["contract.manually.single.invoice"].create(
{"date": self.contract.recurring_next_date, "contract_id": self.contract.id}
).create_invoice()
self.env["contract.manually.single.invoice"].create(
{"date": self.contract.recurring_next_date, "contract_id": self.contract.id}
).create_invoice()
self.contract._compute_sale_count()
self.assertEqual(self.contract.sale_count, 3)

def test_contract_count_sale_2(self):
orders = self.env["sale.order"]
orders |= self.contract.recurring_create_sale()
orders |= self.contract.recurring_create_sale()
orders |= self.contract.recurring_create_sale()
self.env["contract.manually.single.invoice"].create(
{"date": self.contract.recurring_next_date, "contract_id": self.contract.id}
).create_invoice()
self.env["contract.manually.single.invoice"].create(
{"date": self.contract.recurring_next_date, "contract_id": self.contract.id}
).create_invoice()
self.env["contract.manually.single.invoice"].create(
{"date": self.contract.recurring_next_date, "contract_id": self.contract.id}
).create_invoice()
action = self.contract.action_show_sales()
self.assertEqual(set(action["domain"][0][2]), set(orders.ids))
self.assertEqual(len(self.env[action["res_model"]].search(action["domain"])), 3)

def test_cron_recurring_create_sale(self):
self.contract_line.date_start = "2020-01-01"
Expand All @@ -109,6 +129,8 @@ def test_cron_recurring_create_sale(self):
)

def test_contract_sale_analytic(self):
orders = self.env["sale.order"].browse()
orders |= self.contract.recurring_create_sale()
self.env["contract.manually.single.invoice"].create(
{"date": self.contract.recurring_next_date, "contract_id": self.contract.id}
).create_invoice()
orders = self.contract._get_related_sales()
self.assertEqual(self.analytic_account, orders.mapped("analytic_account_id"))
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,15 @@ def test_contract_next_date(self):
self.assertEqual(
fields.Date.to_date("2020-01-15"), self.contract.recurring_next_date
)

self.contract.recurring_create_sale()
self.env["contract.manually.single.invoice"].create(
{"date": self.contract.recurring_next_date, "contract_id": self.contract.id}
).create_invoice()
self.assertEqual(
fields.Date.to_date("2020-01-22"), self.contract.recurring_next_date
)
self.contract.recurring_create_sale()
self.env["contract.manually.single.invoice"].create(
{"date": self.contract.recurring_next_date, "contract_id": self.contract.id}
).create_invoice()
self.assertEqual(
fields.Date.to_date("2020-01-29"), self.contract.recurring_next_date
)
10 changes: 0 additions & 10 deletions contract_sale_generation/views/contract.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,6 @@
name="sale_autoconfirm"
/>
</xpath>
<xpath expr="//button[@name='recurring_create_invoice']" position="before">
<button
attrs="{'invisible': [('generation_type','!=','sale')]}"
class="oe_link"
groups="base.group_no_one"
name="recurring_create_sale"
string="CREATE SALES"
type="object"
/>
</xpath>
<xpath expr="//button[@name='action_show_invoices']" position="after">
<button
attrs="{'invisible': [('generation_type','!=','sale')]}"
Expand Down

0 comments on commit 5b7145f

Please sign in to comment.