Skip to content

Commit

Permalink
[IMP] sale_order_general_discount: change name field and logical
Browse files Browse the repository at this point in the history
  • Loading branch information
miguel-S73 authored and adrip-s73 committed Jul 31, 2024
1 parent 1fcbdf1 commit 0c8f381
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 50 deletions.
10 changes: 4 additions & 6 deletions sale_order_general_discount/models/product_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
class ProductProduct(models.Model):
_inherit = "product.product"

general_discount_apply = fields.Boolean(
string="Apply general discount",
default=True,
required=True,
help="If this checkbox is ticked, it means changing general discount on sale order "
"will impact sale order lines with this related product.",
bypass_general_discount = fields.Boolean(
string="Don't apply general discount",
help="If this checkbox is not ticked, it means changing general discount on "
"sale order will impact sale order lines with this related product.",
)
34 changes: 17 additions & 17 deletions sale_order_general_discount/models/product_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,32 @@
class ProductTemplate(models.Model):
_inherit = "product.template"

general_discount_apply = fields.Boolean(
string="Apply general discount",
compute="_compute_general_discount_apply",
inverse="_inverse_general_discount_apply",
search="_search_general_discount_apply",
help="If this checkbox is ticked, it means changing general discount on sale order "
"will impact sale order lines with this related product.",
bypass_general_discount = fields.Boolean(
string="Don't apply general discount",
compute="_compute_bypass_general_discount",
inverse="_inverse_bypass_general_discount",
search="_search_bypass_general_discount",
help="If this checkbox is not ticked, it means changing general discount on "
"sale order will impact sale order lines with this related product.",
)

def _search_general_discount_apply(self, operator, value):
def _search_bypass_general_discount(self, operator, value):
templates = self.with_context(active_test=False).search(
[("product_variant_ids.general_discount_apply", operator, value)]
[("product_variant_ids.bypass_general_discount", operator, value)]
)
return [("id", "in", templates.ids)]

@api.depends("product_variant_ids.general_discount_apply")
def _compute_general_discount_apply(self):
self.general_discount_apply = True
@api.depends("product_variant_ids.bypass_general_discount")
def _compute_bypass_general_discount(self):
self.bypass_general_discount = False
for template in self:
if len(template.product_variant_ids) == 1:
template.general_discount_apply = (
template.product_variant_ids.general_discount_apply
template.bypass_general_discount = (
template.product_variant_ids.bypass_general_discount
)

def _inverse_general_discount_apply(self):
def _inverse_bypass_general_discount(self):
if len(self.product_variant_ids) == 1:
self.product_variant_ids.general_discount_apply = (
self.general_discount_apply
self.product_variant_ids.bypass_general_discount = (
self.bypass_general_discount
)
18 changes: 1 addition & 17 deletions sale_order_general_discount/models/sale_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,6 @@ class SaleOrderLine(models.Model):
readonly=False,
)

@api.model_create_multi
def create(self, vals_list):
"""Apply general discount for sale order lines which are not created
from sale order form view.
"""
for vals in vals_list:
if "discount" not in vals and "order_id" in vals:
sale_order = self.env["sale.order"].browse(vals["order_id"])
if sale_order.general_discount:
product = self.env["product.product"].browse(
vals.get("product_id", False)
)
if product and product.general_discount_apply:
vals["discount"] = sale_order.general_discount
return super().create(vals_list)

@api.depends("order_id", "order_id.general_discount")
def _compute_discount(self):
res = super()._compute_discount()
Expand All @@ -36,7 +20,7 @@ def _compute_discount(self):
# the case where a discount was set to a value != 0 and then
# set again to 0 to remove the discount on all the lines at the same
# time
if line.product_id.general_discount_apply and (
if not line.product_id.bypass_general_discount and (
line.order_id.general_discount or line.order_id._origin.general_discount
):
line.discount = line.order_id.general_discount
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def setUpClass(cls):
{
"name": "test product without general discount",
"type": "service",
"general_discount_apply": False,
"bypass_general_discount": True,
}
)
cls.order = cls.env["sale.order"].create(
Expand Down Expand Up @@ -158,15 +158,15 @@ def test_compute_discount(self):
self.assertEqual(self.order.order_line[1].discount, 0)

def test_product_template(self):
self.assertTrue(self.product.product_tmpl_id.general_discount_apply)
self.assertFalse(self.product2.product_tmpl_id.general_discount_apply)
self.product2.product_tmpl_id.general_discount_apply = True
self.assertTrue(self.product2.general_discount_apply)
self.assertFalse(self.product.product_tmpl_id.bypass_general_discount)
self.assertTrue(self.product2.product_tmpl_id.bypass_general_discount)
self.product.product_tmpl_id.bypass_general_discount = True
self.assertTrue(self.product.bypass_general_discount)

def test_search_product_template_per_general_discount_apply(self):
def test_search_product_template_per_bypass_general_discount(self):
self.assertEqual(
self.env["product.template"]
.search([("general_discount_apply", "=", 0)])
.search([("bypass_general_discount", "=", True)])
.id,
self.product2.product_tmpl_id.id,
)
6 changes: 3 additions & 3 deletions sale_order_general_discount/views/product_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<field name="inherit_id" ref="product.product_normal_form_view" />
<field name="arch" type="xml">
<xpath expr="//group[@name='group_standard_price']" position="inside">
<field name="general_discount_apply" />
<field name="bypass_general_discount" />
</xpath>
</field>
</record>
Expand All @@ -19,8 +19,8 @@
<field name="arch" type="xml">
<xpath expr="//group[@name='group_standard_price']" position="inside">
<field
name="general_discount_apply"
attrs="{'invisible': [('product_variant_count', '>', 1)]}"
name="bypass_general_discount"
invisible="product_variant_count > 1"
/>
</xpath>
</field>
Expand Down

0 comments on commit 0c8f381

Please sign in to comment.