Skip to content

Commit

Permalink
sale_stock_release_channel_partner_by_date_delivery: fix computation …
Browse files Browse the repository at this point in the history
…of release channel
  • Loading branch information
sebalix committed Nov 29, 2024
1 parent 57d60ea commit c2196c5
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ def _compute_release_channel_id(self):
if not rec._check_release_channel_partner_date_requirements():
continue
channel_date = rec.release_channel_partner_date_id
rec.release_channel_id = channel_date.release_channel_id
if channel_date:
rec.release_channel_id = channel_date.release_channel_id

@api.depends(
"state",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,12 @@ def _get_release_channel_partner_date_domain(self):
]
domain = expression.AND([domain, carrier_domain])
return domain

def _compute_release_channel_id(self):
# pylint: disable=missing-return
super()._compute_release_channel_id()
for rec in self:
# Selected release channel and carrier have to be compatible
if rec.release_channel_id.carrier_ids and rec.carrier_id:
if rec.carrier_id not in rec.release_channel_id.carrier_ids:
rec.release_channel_id = False
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)

from odoo import fields
from odoo.tests.common import Form

from odoo.addons.sale_stock_release_channel_partner_by_date.tests.common import (
SaleReleaseChannelCase,
Expand All @@ -22,31 +23,41 @@ def setUpClass(cls):
}
)
cls.carrier_channel.action_wake_up()
cls.carrier2_channel = cls.default_channel.copy(
{
"name": "Test with carrier2",
"sequence": 10,
"carrier_ids": [(6, 0, cls.carrier2.ids)],
}
)
cls.carrier2_channel.action_wake_up()

def test_sale_order_with_wrong_carrier(self):
def test_sale_order_without_carrier_with_channel_date(self):
delivery_date = fields.Datetime.now()
# Create one specific channel that is matching the SO even if no carrier is set
# FIXME: not sure about this one, to check how it should behave
channel_date_model = self.env["stock.release.channel.partner.date"]
channel_date_model.create(
channel_date = channel_date_model.create(
{
"partner_id": self.customer.id,
"release_channel_id": self.carrier_channel.id,
"date": delivery_date.date(),
}
)
# With wrong carrier set: order doesn't detect the specific channel for carrier
order = self._create_sale_order(date=delivery_date)
order.carrier_id = self.carrier2
self.assertFalse(order.release_channel_id)
self.assertFalse(order.carrier_id)
self.assertEqual(order.release_channel_id, self.carrier_channel)
order.action_confirm()
self.assertFalse(order.release_channel_id)
self.assertFalse(order._get_release_channel_partner_date())
self.assertEqual(order.release_channel_id, self.carrier_channel)
self.assertEqual(order._get_release_channel_partner_date(), channel_date)
picking_out = order.picking_ids
self.assertFalse(picking_out.release_channel_id)
# Then delivery gets the default channel
# FIXME it's not the expected channel from the user POV
self.env["stock.release.channel"].assign_release_channel(picking_out)
self.assertEqual(picking_out.release_channel_id, self.default_channel)

def test_sale_order_with_carrier(self):
def test_sale_order_with_carrier_with_channel_date(self):
delivery_date = fields.Datetime.now()
channel_date_model = self.env["stock.release.channel.partner.date"]
channel_date = channel_date_model.create(
Expand All @@ -69,3 +80,89 @@ def test_sale_order_with_carrier(self):
# Then delivery gets the default channel
self.env["stock.release.channel"].assign_release_channel(picking_out)
self.assertEqual(picking_out.release_channel_id, self.carrier_channel)

def test_sale_order_with_carrier_without_channel_date(self):
delivery_date = fields.Datetime.now()
# With carrier set: order doesn't detect any specific channel
order = self._create_sale_order(date=delivery_date)
order.carrier_id = self.carrier
self.assertFalse(order.release_channel_id)
self.assertFalse(order._get_release_channel_partner_date())
# The user is able to set a release channel on the form
with Form(order) as form:
form.release_channel_id = self.carrier_channel
order.action_confirm()
self.assertEqual(order.release_channel_id, self.carrier_channel)
# A specific channel has been created at order confirmation
self.assertTrue(order._get_release_channel_partner_date())
picking_out = order.picking_ids
self.assertFalse(picking_out.release_channel_id)
# Then delivery then gets the channel defined on the SO
self.env["stock.release.channel"].assign_release_channel(picking_out)
self.assertEqual(picking_out.release_channel_id, self.carrier_channel)

def test_sale_order_with_incompatible_channel_and_carrier_1(self):
# Case 1: a release channel having a specific channel configured
# is detected, but incompatible with the selected carrier afterwards.
# => unset the detected release channel
delivery_date = fields.Datetime.now()
channel_date_model = self.env["stock.release.channel.partner.date"]
channel_date_model.create(
{
"partner_id": self.customer.id,
"release_channel_id": self.carrier_channel.id,
"date": delivery_date.date(),
}
)
order = self._create_sale_order(date=delivery_date)
self.assertEqual(order.release_channel_id, self.carrier_channel)
# => select a carrier
order.carrier_id = self.carrier2
self.assertFalse(order.release_channel_id)
# Confirm the order and check release channel on delivery
order.action_confirm()
self.assertFalse(order.release_channel_id)
self.assertFalse(order._get_release_channel_partner_date())
picking_out = order.picking_ids
self.assertFalse(picking_out.release_channel_id)
# Then delivery gets the default channel
self.env["stock.release.channel"].assign_release_channel(picking_out)
self.assertEqual(picking_out.release_channel_id, self.default_channel)

def test_sale_order_with_incompatible_channel_and_carrier_2(self):
# Case 2: a release channel having a specific channel configured
# is detected, but incompatible with the selected carrier afterwards.
# However another specific channel is compatible with the selected carrier.
# => update the selected release channel to be compatible with the
# selected carrier
delivery_date = fields.Datetime.now()
channel_date_model = self.env["stock.release.channel.partner.date"]
channel_date_model.create(
{
"partner_id": self.customer.id,
"release_channel_id": self.carrier_channel.id,
"date": delivery_date.date(),
}
)
order = self._create_sale_order(date=delivery_date)
self.assertEqual(order.release_channel_id, self.carrier_channel)
channel_date_model = self.env["stock.release.channel.partner.date"]
channel_date = channel_date_model.create(
{
"partner_id": self.customer.id,
"release_channel_id": self.carrier2_channel.id,
"date": delivery_date.date(),
}
)
# => select a carrier
order.carrier_id = self.carrier2
self.assertEqual(order.release_channel_id, self.carrier2_channel)
# Confirm the order and check release channel on delivery
order.action_confirm()
self.assertEqual(order.release_channel_id, self.carrier2_channel)
self.assertEqual(order._get_release_channel_partner_date(), channel_date)
picking_out = order.picking_ids
self.assertFalse(picking_out.release_channel_id)
# Then delivery gets the selected channel
self.env["stock.release.channel"].assign_release_channel(picking_out)
self.assertEqual(picking_out.release_channel_id, self.carrier2_channel)

0 comments on commit c2196c5

Please sign in to comment.