Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion contract_sale_generation/models/contract_line.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,36 @@
# Copyright (C) 2020 Open Source Integrators
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import models
from odoo import _, api, exceptions, models


class ContractLine(models.Model):
_inherit = "contract.line"

@api.constrains(
"display_type",
"product_id",
)
def _constrain_sale_product_display(self):
"""If the contract generates a sale, its lines must comply with sale order lines."""
sale_contract_lines = self.filtered(
lambda line: line.contract_id.generation_type == "sale"
)
for line in sale_contract_lines:
# Stay aligned with accountable_required_fields SQL constraint:
# CHECK(
# display_type IS NOT NULL
# OR (product_id IS NOT NULL AND product_uom IS NOT NULL)
# )
if not (line.display_type or (line.product_id and line.uom_id)):
raise exceptions.ValidationError(
_(
"The line %(name)s must set a Product/UoM "
"or have a different display type",
name=line.name,
)
)

def _prepare_sale_line_vals(self, dates, order_id=False):
sale_line_vals = {
"product_id": self.product_id.id,
Expand Down
21 changes: 20 additions & 1 deletion contract_sale_generation/tests/test_contract_sale.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Copyright 2017 Angel Moya <angel.moya@pesol.es>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import fields
from odoo import exceptions, fields
from odoo.exceptions import ValidationError
from odoo.tests.common import SavepointCase

Expand Down Expand Up @@ -112,3 +112,22 @@ def test_contract_sale_analytic(self):
orders = self.env["sale.order"].browse()
orders |= self.contract.recurring_create_sale()
self.assertEqual(self.analytic_account, orders.mapped("analytic_account_id"))

def test_constrain_sale_contract_line(self):
"""If the contract generates a sale, its lines must have a product."""
# Arrange
contract = self.contract.copy(
default={
"generation_type": "sale",
}
)
# pre-condition
self.assertEqual(contract.generation_type, "sale")

# Act
with self.assertRaises(exceptions.ValidationError) as ve:
contract.contract_line_ids.product_id = False

# Assert
exc_message = ve.exception.args[0]
self.assertIn("must set a Product", exc_message)
Loading