Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[12.0][IMP] delivery_multi_destination: Introduce 'based on destination' delivery type #871

Open
wants to merge 1 commit into
base: 12.0
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion delivery_multi_destination/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

{
"name": "Multiple destinations for the same delivery method",
"version": "12.0.1.1.0",
"version": "12.0.2.0.0",
"category": "Delivery",
"website": "https://github.com/OCA/delivery-carrier",
"author": "Tecnativa, "
Expand Down
1 change: 1 addition & 0 deletions delivery_multi_destination/demo/delivery_carrier_demo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<field name="name">International Carrier Inc.</field>
<field name="sequence">4</field>
<field name="destination_type">multi</field>
<field name="delivery_type">base_on_destination</field>
<field name="product_id" ref="product_product_delivery_carrier_multi"/>
</record>

Expand Down
15 changes: 15 additions & 0 deletions delivery_multi_destination/migrations/12.0.2.0.0/pre-migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2022 Coop IT Easy SC
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)
from openupgradelib import openupgrade


@openupgrade.migrate()
def migrate(env, version):
openupgrade.logged_query(
env.cr,
"""
UPDATE delivery_carrier
SET delivery_type = 'base_on_destination'
WHERE destination_type = 'multi'
"""
)
29 changes: 28 additions & 1 deletion delivery_multi_destination/models/delivery_carrier.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,35 @@ class DeliveryCarrier(models.Model):
('one', 'One destination'),
('multi', 'Multiple destinations'),
],
default="one", required=True,
compute="_compute_destination_type",
inverse="_inverse_destination_type",
store=True,
)
delivery_type = fields.Selection(
selection_add=[("base_on_destination", "Based on Destination")]
)

@api.depends("delivery_type")
def _compute_destination_type(self):
for carrier in self:
if carrier.delivery_type == "base_on_destination":
carrier.destination_type = "multi"
else:
carrier.destination_type = "one"

def _inverse_destination_type(self):
for carrier in self:
# Switch to multi
if carrier.destination_type == "multi":
carrier.delivery_type = "base_on_destination"
# Switch away from multi -> we know that destination_type is
# non-multi. However, in a hypothetical scenario where we switch
# from one non-multi destination_type to another, we don't want to
# forcibly reset delivery_type to 'fixed' each time, so we check
# whether delivery_type is invalid for a non-multi destination_type
# before we forcibly reset to 'fixed'.
elif carrier.delivery_type == "base_on_destination":
carrier.delivery_type = "fixed"

def search(self, args, offset=0, limit=None, order=None, count=False):
"""Don't show by default children carriers."""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Introduced 'based on destination' delivery type. This is the delivery type used
by all multi-destination carriers instead of 'fixed'. Consequently, destination
type has been turned into a computed field that checks if the delivery type is
'based on destination' or not.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def setUpClass(cls):
'name': 'Test carrier multi',
'product_id': cls.product.id,
'destination_type': 'multi',
'delivery_type': 'fixed',
'delivery_type': 'base_on_destination',
'fixed_price': 100,
'child_ids': [
(0, 0, {
Expand Down Expand Up @@ -123,6 +123,20 @@ def test_delivery_multi_destination(self):
order.get_delivery_price()
self.assertAlmostEqual(order.delivery_price, 150, 2)

def test_compute(self):
self.carrier_multi.delivery_type = "fixed"
self.assertEqual(self.carrier_multi.destination_type, "one")
self.carrier_multi.delivery_type = "base_on_destination"
self.assertEqual(self.carrier_multi.destination_type, "multi")

def test_inverse(self):
self.carrier_multi.destination_type = "one"
self.assertEqual(self.carrier_multi.destination_type, "one")
self.assertEqual(self.carrier_multi.delivery_type, "fixed")
self.carrier_multi.destination_type = "multi"
self.assertEqual(self.carrier_multi.destination_type, "multi")
self.assertEqual(self.carrier_multi.delivery_type, "base_on_destination")

def test_search(self):
carriers = self.env['delivery.carrier'].search([])
children_carrier = self.carrier_multi.with_context(
Expand Down
Loading