Skip to content

Commit 4c6fab5

Browse files
grindtildeathhailangvn
authored andcommitted
Create module sale_order_carrier_auto_assign
1 parent d268668 commit 4c6fab5

File tree

8 files changed

+82
-0
lines changed

8 files changed

+82
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import models
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright 2020 Camptocamp SA
2+
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl)
3+
{
4+
"name": "Sale Order Carrier Auto Assign",
5+
"summary": "Auto assign delivery carrier on sale order confirmation",
6+
"version": "13.0.1.0.0",
7+
"development_status": "Alpha",
8+
"category": "Operations/Inventory/Delivery",
9+
"website": "https://github.com/OCA/sale-workflow",
10+
"author": "Camptocamp, Odoo Community Association (OCA)",
11+
"license": "AGPL-3",
12+
"application": False,
13+
"installable": True,
14+
"depends": ["delivery"],
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import sale_order
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Copyright 2020 Camptocamp SA
2+
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl)
3+
from odoo import models
4+
5+
6+
class SaleOrder(models.Model):
7+
8+
_inherit = "sale.order"
9+
10+
def action_confirm(self):
11+
self._add_delivery_carrier_on_confirmation()
12+
return super().action_confirm()
13+
14+
def _add_delivery_carrier_on_confirmation(self):
15+
"""Automatically add delivery.carrier on sale order confirmation"""
16+
for order in self:
17+
if order.carrier_id or any(line.is_delivery for line in order.order_line):
18+
continue
19+
delivery_wiz_action = order.action_open_delivery_wizard()
20+
delivery_wiz_context = delivery_wiz_action.get("context", {})
21+
if not delivery_wiz_context.get("default_carrier_id"):
22+
continue
23+
delivery_wiz = (
24+
self.env[delivery_wiz_action.get("res_model")]
25+
.with_context(**delivery_wiz_context)
26+
.create({})
27+
)
28+
delivery_wiz.button_confirm()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* Akim Juillerat <[email protected]>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This module assigns automatically delivery carrier on sale order confirmation.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import test_sale_order_carrier_auto_assign
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Copyright 2020 Camptocamp SA
2+
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl)
3+
from odoo.tests import Form, SavepointCase
4+
5+
6+
class TestSaleOrderCarrierAutoAssign(SavepointCase):
7+
@classmethod
8+
def setUpClass(cls):
9+
super().setUpClass()
10+
cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True))
11+
cls.partner = cls.env.ref("base.res_partner_2")
12+
product = cls.env.ref("product.product_product_9")
13+
cls.normal_delivery_carrier = cls.env.ref("delivery.normal_delivery_carrier")
14+
sale_order_form = Form(cls.env["sale.order"])
15+
sale_order_form.partner_id = cls.partner
16+
with sale_order_form.order_line.new() as line_form:
17+
line_form.product_id = product
18+
cls.sale_order = sale_order_form.save()
19+
20+
def test_sale_order_carrier_auto_assign(self):
21+
self.assertEqual(
22+
self.partner.property_delivery_carrier_id, self.normal_delivery_carrier
23+
)
24+
self.assertFalse(self.sale_order.carrier_id)
25+
self.sale_order.action_confirm()
26+
self.assertEqual(self.sale_order.state, "sale")
27+
self.assertEqual(self.sale_order.carrier_id, self.normal_delivery_carrier)
28+
29+
def test_sale_order_carrier_auto_assign_no_carrier(self):
30+
self.partner.property_delivery_carrier_id = False
31+
self.assertFalse(self.sale_order.carrier_id)
32+
self.sale_order.action_confirm()
33+
self.assertEqual(self.sale_order.state, "sale")
34+
self.assertFalse(self.sale_order.carrier_id)

0 commit comments

Comments
 (0)