Skip to content

Commit

Permalink
Merge PR #3163 into 16.0
Browse files Browse the repository at this point in the history
Signed-off-by pedrobaeza
  • Loading branch information
OCA-git-bot committed May 31, 2024
2 parents c5dc115 + 4038573 commit 550130f
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 73 deletions.
58 changes: 27 additions & 31 deletions product_supplierinfo_for_customer_sale/models/sale_order_line.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Copyright 2013-2017 Agile Business Group sagl
# (<http://www.agilebg.com>)
# Copyright 2021 ForgeFlow S.L. (https://www.forgeflow.com)
# Copyright 2024 Tecnativa - Víctor Martínez
# Copyright 2024 Tecnativa - Pedro M. Baeza
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import api, fields, models
Expand All @@ -25,38 +27,32 @@ def _compute_product_customer_code(self):
code = ""
line.product_customer_code = code

def _compute_name(self):
"""We need to override the method with product_id is set so that the product
code is not added and add custom code of customerinfo."""
empty_lines = self.filtered(lambda x: not x.product_id)
super(SaleOrderLine, empty_lines)._compute_name()
for item in self - empty_lines:
customerinfo = item.product_id._select_customerinfo(
partner=item.order_partner_id
)
if customerinfo.product_code:
# Avoid to put the standard internal reference
item = item.with_context(display_default_code=False)
super(SaleOrderLine, item)._compute_name()
if customerinfo.product_code:
item.name = f"[{customerinfo.product_code}] {item.name}"
return

@api.onchange("product_id")
def _onchange_product_id_warning(self):
result = super(SaleOrderLine, self)._onchange_product_id_warning()
for line in self.filtered(
lambda sol: sol.product_id.product_tmpl_id.customer_ids
and sol.order_id.pricelist_id.item_ids
):
product = line.product_id
items = self.env["product.pricelist.item"].search(
[
("pricelist_id", "=", line.order_id.pricelist_id.id),
("compute_price", "=", "formula"),
("base", "=", "partner"),
"|",
("applied_on", "=", "3_global"),
"|",
"&",
("categ_id", "=", product.categ_id.id),
("applied_on", "=", "2_product_category"),
"|",
"&",
("product_tmpl_id", "=", product.product_tmpl_id.id),
("applied_on", "=", "1_product"),
"&",
("product_id", "=", product.id),
("applied_on", "=", "0_product_variant"),
]
)
if items:
supplierinfo = line.product_id._select_customerinfo(
"""Assign the mininum quantity if set."""
res = super()._onchange_product_id_warning()
for line in self:
if line.product_id:
customerinfo = line.product_id._select_customerinfo(
partner=line.order_partner_id
)
if supplierinfo and supplierinfo.min_qty:
line.product_uom_qty = supplierinfo.min_qty
return result
if customerinfo.min_qty:
line.product_uom_qty = customerinfo.min_qty
return res
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# Copyright 2017 ForgeFlow S.L.
# (http://www.forgeflow.com)
# Copyright 2024 Tecnativa - Víctor Martínez
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from odoo.tests import Form
from odoo.tests.common import TransactionCase


class TestProductSupplierinfoForCustomerSale(TransactionCase):
def setUp(self):
super(TestProductSupplierinfoForCustomerSale, self).setUp()
super().setUp()
self.supplierinfo_model = self.env["product.supplierinfo"]
self.customerinfo_model = self.env["product.customerinfo"]
self.pricelist_item_model = self.env["product.pricelist.item"]
Expand Down Expand Up @@ -79,13 +81,14 @@ def _create_pricelist_item(self, name, pricelist, product):
)

def test_product_supplierinfo_for_customer_sale(self):
so = self.env["sale.order"].create(
{"partner_id": self.customer.id, "pricelist_id": self.pricelist.id}
)
line = self.env["sale.order.line"].create(
{"product_id": self.product.id, "order_id": so.id}
)
line._onchange_product_id_warning()
order_form = Form(self.env["sale.order"])
order_form.partner_id = self.customer
order_form.pricelist_id = self.pricelist
with order_form.order_line.new() as line_form:
line_form.product_id = self.product
order = order_form.save()
line = order.order_line
self.assertIn("00001", order.order_line.name)
self.assertEqual(
line.product_customer_code,
self.customerinfo.product_code,
Expand All @@ -98,13 +101,13 @@ def test_product_supplierinfo_for_customer_sale(self):
)

def test_product_supplierinfo_for_customer_sale_variant(self):
so = self.env["sale.order"].create(
{"partner_id": self.customer.id, "pricelist_id": self.pricelist.id}
)
line = self.env["sale.order.line"].create(
{"product_id": self.product_variant_1.id, "order_id": so.id}
)
line._onchange_product_id_warning()
order_form = Form(self.env["sale.order"])
order_form.partner_id = self.customer
order_form.pricelist_id = self.pricelist
with order_form.order_line.new() as line_form:
line_form.product_id = self.product_variant_1
order = order_form.save()
line = order.order_line
self.assertEqual(
line.product_customer_code,
self.customerinfo.product_code,
Expand All @@ -115,32 +118,26 @@ def test_product_supplierinfo_for_customer_sale_template(self):
customerinfo = self._create_partnerinfo(
"customer", self.customer, self.product_variant_2
)
so = self.env["sale.order"].create(
{"partner_id": self.customer.id, "pricelist_id": self.pricelist.id}
)
line = self.env["sale.order.line"].create(
{"product_id": self.product_variant_2.id, "order_id": so.id}
)
line._onchange_product_id_warning()
order_form = Form(self.env["sale.order"])
order_form.partner_id = self.customer
order_form.pricelist_id = self.pricelist
with order_form.order_line.new() as line_form:
line_form.product_id = self.product_variant_2
order = order_form.save()
line = order.order_line
self.assertEqual(
line.product_customer_code,
customerinfo.product_code,
"Error: Customer product code was not passed to sale order line",
)
# Test with product without variants
so2 = self.env["sale.order"].create(
{
"partner_id": self.customer.id,
"pricelist_id": self.pricelist_template.id,
}
)
line2 = self.env["sale.order.line"].create(
{
"product_id": self.product_template.product_variant_ids.id,
"order_id": so2.id,
}
)
line2._onchange_product_id_warning()
order_form = Form(self.env["sale.order"])
order_form.partner_id = self.customer
order_form.pricelist_id = self.pricelist_template
with order_form.order_line.new() as line_form:
line_form.product_id = self.product_template.product_variant_ids[0]
order2 = order_form.save()
line2 = order2.order_line
self.assertEqual(
line2.product_customer_code,
customerinfo.product_code,
Expand All @@ -151,13 +148,13 @@ def test_product_supplierinfo_for_customer_sale_variant_wo_template(self):
customerinfo = self._create_partnerinfo(
"customer", self.customer, self.product_variant_2, empty_variant=True
)
so = self.env["sale.order"].create(
{"partner_id": self.customer.id, "pricelist_id": self.pricelist.id}
)
line = self.env["sale.order.line"].create(
{"product_id": self.product_variant_2.id, "order_id": so.id}
)
line._onchange_product_id_warning()
order_form = Form(self.env["sale.order"])
order_form.partner_id = self.customer
order_form.pricelist_id = self.pricelist
with order_form.order_line.new() as line_form:
line_form.product_id = self.product_variant_2
order = order_form.save()
line = order.order_line
self.assertEqual(
line.product_customer_code,
customerinfo.product_code,
Expand Down

0 comments on commit 550130f

Please sign in to comment.