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 committed Mar 27, 2024
1 parent fe859ad commit c0defa4
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 35 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.",
not_general_discount_apply = 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.",
not_general_discount_apply = fields.Boolean(
string="Don't apply general discount",
compute="_compute_not_general_discount_apply",
inverse="_inverse_not_general_discount_apply",
search="_search_not_general_discount_apply",
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_not_general_discount_apply(self, operator, value):
templates = self.with_context(active_test=False).search(
[("product_variant_ids.general_discount_apply", operator, value)]
[("product_variant_ids.not_general_discount_apply", 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.not_general_discount_apply")
def _compute_not_general_discount_apply(self):
self.not_general_discount_apply = False
for template in self:
if len(template.product_variant_ids) == 1:
template.general_discount_apply = (
template.product_variant_ids.general_discount_apply
template.not_general_discount_apply = (
template.product_variant_ids.not_general_discount_apply
)

def _inverse_general_discount_apply(self):
def _inverse_not_general_discount_apply(self):
if len(self.product_variant_ids) == 1:
self.product_variant_ids.general_discount_apply = (
self.general_discount_apply
self.product_variant_ids.not_general_discount_apply = (
self.not_general_discount_apply
)
4 changes: 2 additions & 2 deletions sale_order_general_discount/models/sale_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def create(self, vals_list):
product = self.env["product.product"].browse(
vals.get("product_id", False)
)
if product and product.general_discount_apply:
if product and not product.not_general_discount_apply:
vals["discount"] = sale_order.general_discount
return super().create(vals_list)

Expand All @@ -36,7 +36,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.not_general_discount_apply 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,
"not_general_discount_apply": 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.not_general_discount_apply)
self.assertTrue(self.product2.product_tmpl_id.not_general_discount_apply)
self.product.product_tmpl_id.not_general_discount_apply = True
self.assertTrue(self.product.not_general_discount_apply)

def test_search_product_template_per_general_discount_apply(self):
def test_search_product_template_per_not_general_discount_apply(self):
self.assertEqual(
self.env["product.template"]
.search([("general_discount_apply", "=", 0)])
.search([("not_general_discount_apply", "=", 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="not_general_discount_apply" />
</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="not_general_discount_apply"
invisible="product_variant_count > 1"
/>
</xpath>
</field>
Expand Down

0 comments on commit c0defa4

Please sign in to comment.