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

[16.0][IMP] sale_order_carrier_auto_assign: assign carrier on create #3081

Closed
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
11 changes: 9 additions & 2 deletions sale_order_carrier_auto_assign/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ Sale Order Carrier Auto Assign

|badge1| |badge2| |badge3| |badge4| |badge5|

This module assigns automatically delivery carrier on sale order confirmation.
This module assigns automatically delivery carrier on sale order creation or confirmation.

You may also have a look at the module delivery_auto_refresh in delivery-carrier repository.

.. IMPORTANT::
This is an alpha version, the data model and design can change at any time without warning.
Expand All @@ -45,7 +47,10 @@ Configuration

To enable sale order carrier auto assign:

#. Go to *Settings > Sales > Carrier Auto Assign*.
#. Go to *Settings > Sales > Shipping*.

* Enable on creation: only the carrier will be set.
* Enable on confirmation: the carrier and delivery line will be set.

Bug Tracker
===========
Expand All @@ -64,6 +69,7 @@ Authors
~~~~~~~

* Camptocamp
* BCIM

Contributors
~~~~~~~~~~~~
Expand All @@ -73,6 +79,7 @@ Contributors
* Phuc (Tran Thanh) <phuc@trobz.com>
* Telmo Santos <telmo.santos@camptocamp.com>
* Tris Doan <tridm@trobz.com>
* Jacques-Etienne Baudoux (BCIM) <je@bcim.be>


Maintainers
Expand Down
8 changes: 4 additions & 4 deletions sale_order_carrier_auto_assign/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl)
{
"name": "Sale Order Carrier Auto Assign",
"summary": "Auto assign delivery carrier on sale order confirmation",
"version": "16.0.1.1.0",
"development_status": "Alpha",
"summary": "Auto assign delivery carrier on sales order",
"version": "16.0.2.0.0",
"category": "Operations/Inventory/Delivery",
"website": "https://github.com/OCA/sale-workflow",
"author": "Camptocamp, Odoo Community Association (OCA)",
"author": "Camptocamp, BCIM, Odoo Community Association (OCA)",
"maintainers": ["jbaudoux"],
"license": "AGPL-3",
"data": ["views/res_config_settings_views.xml"],
"application": False,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Copyright 2024 Jacques-Etienne Baudoux (BCIM) <je@bcim.be>
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).

from odoo.tools import sql


def migrate(cr, version):
if sql.column_exists(cr, "res_company", "carrier_auto_assign"):
sql.rename_column(
cr, "res_company", "carrier_auto_assign", "carrier_auto_assign_on_confirm"
)
8 changes: 7 additions & 1 deletion sale_order_carrier_auto_assign/models/res_company.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright 2023 Camptocamp SA
# Copyright 2024 Jacques-Etienne Baudoux (BCIM) <je@bcim.be>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import fields, models
Expand All @@ -7,4 +8,9 @@
class ResCompany(models.Model):
_inherit = "res.company"

carrier_auto_assign = fields.Boolean()
carrier_auto_assign_on_create = fields.Boolean(
"Set default shipping method automatically"
)
carrier_auto_assign_on_confirm = fields.Boolean(
"Ensure shipping method and cost line on confirmation"
)
11 changes: 8 additions & 3 deletions sale_order_carrier_auto_assign/models/res_config_settings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright 2023 Camptocamp SA
# Copyright 2024 Jacques-Etienne Baudoux (BCIM) <je@bcim.be>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import fields, models
Expand All @@ -7,8 +8,12 @@
class ResConfigSettings(models.TransientModel):
_inherit = "res.config.settings"

carrier_auto_assign = fields.Boolean(
related="company_id.carrier_auto_assign",
carrier_auto_assign_on_create = fields.Boolean(
related="company_id.carrier_auto_assign_on_create",
readonly=False,
)

carrier_auto_assign_on_confirm = fields.Boolean(
related="company_id.carrier_auto_assign_on_confirm",
readonly=False,
help="Enable carrier auto assign on sale order confirmation.",
)
80 changes: 64 additions & 16 deletions sale_order_carrier_auto_assign/models/sale_order.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,80 @@
# Copyright 2020 Camptocamp SA
# Copyright 2024 Jacques-Etienne Baudoux (BCIM) <je@bcim.be>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl)
from odoo import models

from odoo import api, models

class SaleOrder(models.Model):

class SaleOrder(models.Model):
_inherit = "sale.order"

def action_confirm(self):
for rec in self:
if not rec.company_id.carrier_auto_assign:
continue
rec._add_delivery_carrier_on_confirmation()
return super().action_confirm()

def _add_delivery_carrier_on_confirmation(self):
"""Automatically add delivery.carrier on sale order confirmation"""
def _set_delivery_carrier(self, set_delivery_line=True):
for order in self:
if order.delivery_set:
continue
delivery_wiz_action = order.action_open_delivery_wizard()
delivery_wiz_context = delivery_wiz_action.get("context", {})
if not delivery_wiz_context.get("default_carrier_id"):
continue
delivery_wiz = (
self.env[delivery_wiz_action.get("res_model")]
.with_context(**delivery_wiz_context)
.create({})
.new({})
)
delivery_wiz._get_shipment_rate()
delivery_wiz.button_confirm()

# Do not override carrier
if order.carrier_id:
delivery_wiz.carrier_id = order.carrier_id

# If the carrier isn't allowed, we won't default to it
if (
delivery_wiz.carrier_id
not in delivery_wiz.available_carrier_ids._origin
):
return

if not set_delivery_line or order.is_all_service:
# Only set the carrier
if order.carrier_id != delivery_wiz.carrier_id:
order.carrier_id = delivery_wiz.carrier_id
else:
delivery_wiz._get_shipment_rate()
delivery_wiz.button_confirm()

@api.onchange("partner_id", "partner_shipping_id")
def _add_delivery_carrier_on_partner_change(self):
partner = self.partner_shipping_id or self.partner_id
if not partner:
return
if self.company_id.carrier_auto_assign_on_create:
self._set_delivery_carrier(set_delivery_line=False)

def _is_auto_set_carrier_on_create(self):
self.ensure_one()
if self.state not in ("draft", "sent"):
return False
if self.company_id.carrier_auto_assign_on_create:
return True
return False

@api.model_create_multi
def create(self, vals_list):
orders = super().create(vals_list)
for order in orders:
if not order.carrier_id and order._is_auto_set_carrier_on_create():
order._set_delivery_carrier()
return orders

def _add_delivery_carrier_on_confirmation(self):
"""Automatically add delivery.carrier on sale order confirmation"""
for order in self:
if order.delivery_set:
continue
if order.is_all_service:
continue
order._set_delivery_carrier()

def action_confirm(self):
for order in self:
if not order.company_id.carrier_auto_assign_on_confirm:
continue
order._add_delivery_carrier_on_confirmation()
return super().action_confirm()
5 changes: 4 additions & 1 deletion sale_order_carrier_auto_assign/readme/CONFIGURE.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
To enable sale order carrier auto assign:

#. Go to *Settings > Sales > Carrier Auto Assign*.
#. Go to *Settings > Sales > Shipping*.

* Enable on creation: only the carrier will be set.
* Enable on confirmation: the carrier and delivery line will be set.
1 change: 1 addition & 0 deletions sale_order_carrier_auto_assign/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
* Phuc (Tran Thanh) <phuc@trobz.com>
* Telmo Santos <telmo.santos@camptocamp.com>
* Tris Doan <tridm@trobz.com>
* Jacques-Etienne Baudoux (BCIM) <je@bcim.be>

4 changes: 3 additions & 1 deletion sale_order_carrier_auto_assign/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
This module assigns automatically delivery carrier on sale order confirmation.
This module assigns automatically delivery carrier on sale order creation or confirmation.

You may also have a look at the module delivery_auto_refresh in delivery-carrier repository.
11 changes: 9 additions & 2 deletions sale_order_carrier_auto_assign/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,8 @@ <h1 class="title">Sale Order Carrier Auto Assign</h1>
!! source digest: sha256:b5f0ff98a101634f6d21500615b789f2e2cbd6d305d896b93dd77cb31461dec0
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Alpha" src="https://img.shields.io/badge/maturity-Alpha-red.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/sale-workflow/tree/16.0/sale_order_carrier_auto_assign"><img alt="OCA/sale-workflow" src="https://img.shields.io/badge/github-OCA%2Fsale--workflow-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/sale-workflow-16-0/sale-workflow-16-0-sale_order_carrier_auto_assign"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/sale-workflow&amp;target_branch=16.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>This module assigns automatically delivery carrier on sale order confirmation.</p>
<p>This module assigns automatically delivery carrier on sale order creation or confirmation.</p>
<p>You may also have a look at the module delivery_auto_refresh in delivery-carrier repository.</p>
<div class="admonition important">
<p class="first admonition-title">Important</p>
<p class="last">This is an alpha version, the data model and design can change at any time without warning.
Expand All @@ -393,8 +394,12 @@ <h1 class="title">Sale Order Carrier Auto Assign</h1>
<h1><a class="toc-backref" href="#toc-entry-1">Configuration</a></h1>
<p>To enable sale order carrier auto assign:</p>
<ol class="arabic simple">
<li>Go to <em>Settings &gt; Sales &gt; Carrier Auto Assign</em>.</li>
<li>Go to <em>Settings &gt; Sales &gt; Shipping</em>.</li>
</ol>
<ul class="simple">
<li>Enable on creation: only the carrier will be set.</li>
<li>Enable on confirmation: the carrier and delivery line will be set.</li>
</ul>
</div>
<div class="section" id="bug-tracker">
<h1><a class="toc-backref" href="#toc-entry-2">Bug Tracker</a></h1>
Expand All @@ -410,6 +415,7 @@ <h1><a class="toc-backref" href="#toc-entry-3">Credits</a></h1>
<h2><a class="toc-backref" href="#toc-entry-4">Authors</a></h2>
<ul class="simple">
<li>Camptocamp</li>
<li>BCIM</li>
</ul>
</div>
<div class="section" id="contributors">
Expand All @@ -420,6 +426,7 @@ <h2><a class="toc-backref" href="#toc-entry-5">Contributors</a></h2>
<li>Phuc (Tran Thanh) &lt;<a class="reference external" href="mailto:phuc&#64;trobz.com">phuc&#64;trobz.com</a>&gt;</li>
<li>Telmo Santos &lt;<a class="reference external" href="mailto:telmo.santos&#64;camptocamp.com">telmo.santos&#64;camptocamp.com</a>&gt;</li>
<li>Tris Doan &lt;<a class="reference external" href="mailto:tridm&#64;trobz.com">tridm&#64;trobz.com</a>&gt;</li>
<li>Jacques-Etienne Baudoux (BCIM) &lt;<a class="reference external" href="mailto:je&#64;bcim.be">je&#64;bcim.be</a>&gt;</li>
</ul>
</div>
<div class="section" id="maintainers">
Expand Down
Loading
Loading