-
-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sale_stock_available_to_promise_release_block: add Unblock Release wi…
…zard This new wizard allows to give more options to the user regarding the unblocking process, like the scheduled date to set on unblocked moves, and re-assign automatically a stock operation on them (so they could be grouped together in the same transfer).
- Loading branch information
Showing
12 changed files
with
323 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
from . import models | ||
from . import wizards | ||
from .hooks import post_init_hook |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Copyright 2024 Camptocamp SA | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) | ||
|
||
import logging | ||
|
||
from odoo import SUPERUSER_ID, api | ||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
|
||
def post_init_hook(cr, registry): | ||
_logger.info("Remove original 'Unblock Release' server action...") | ||
env = api.Environment(cr, SUPERUSER_ID, {}) | ||
action = env.ref( | ||
"stock_available_to_promise_release_block.action_stock_move_unblock_release", | ||
raise_if_not_found=False, | ||
) | ||
action.unlink() |
35 changes: 35 additions & 0 deletions
35
sale_stock_available_to_promise_release_block/migrations/16.0.1.1.0/pre-migrate.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Copyright 2024 Camptocamp SA | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) | ||
import logging | ||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
|
||
def migrate(cr, version): | ||
if not version: | ||
return | ||
remove_unblock_release_ir_action_server(cr) | ||
|
||
|
||
def remove_unblock_release_ir_action_server(cr): | ||
# The same XML-ID will be used by a new window action to open a wizard | ||
_logger.info("Remove action 'action_sale_order_line_unblock_release'") | ||
queries = [ | ||
""" | ||
DELETE FROM ir_act_server | ||
WHERE id IN ( | ||
SELECT res_id | ||
FROM ir_model_data | ||
WHERE module='sale_stock_available_to_promise_release_block' | ||
AND name='action_sale_order_line_unblock_release' | ||
AND model='ir.actions.server' | ||
); | ||
""", | ||
""" | ||
DELETE FROM ir_model_data | ||
WHERE module='sale_stock_available_to_promise_release_block' | ||
AND name='action_sale_order_line_unblock_release'; | ||
""", | ||
] | ||
for query in queries: | ||
cr.execute(query) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
sale_stock_available_to_promise_release_block/security/ir.model.access.csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink | ||
access_unblock_release_sale,access.unblock.release,model_unblock_release,sales_team.group_sale_salesman,1,1,1,0 | ||
access_unblock_release_stock,access.unblock.release,model_unblock_release,stock.group_stock_user,1,1,1,0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
sale_stock_available_to_promise_release_block/views/stock_move.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<!-- Copyright 2024 Camptocamp SA | ||
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). --> | ||
<odoo> | ||
|
||
<!-- This new 'Unblock Release' window action replaces the original one (server action) --> | ||
<record id="action_stock_move_unblock_release" model="ir.actions.act_window"> | ||
<field name="name">Unblock Release</field> | ||
<field name="res_model">unblock.release</field> | ||
<field name="view_mode">form</field> | ||
<field name="target">new</field> | ||
<field name="binding_model_id" ref="stock.model_stock_move" /> | ||
<field name="binding_view_types">list</field> | ||
</record> | ||
|
||
</odoo> |
1 change: 1 addition & 0 deletions
1
sale_stock_available_to_promise_release_block/wizards/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from . import unblock_release |
103 changes: 103 additions & 0 deletions
103
sale_stock_available_to_promise_release_block/wizards/unblock_release.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
# Copyright 2024 Camptocamp SA | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) | ||
|
||
from odoo import api, fields, models | ||
|
||
|
||
class UnblockRelease(models.TransientModel): | ||
_name = "unblock.release" | ||
_description = "Unblock Release" | ||
|
||
order_line_ids = fields.Many2many( | ||
comodel_name="sale.order.line", | ||
string="Order Lines", | ||
) | ||
move_ids = fields.Many2many( | ||
comodel_name="stock.move", | ||
string="Delivery moves", | ||
) | ||
option = fields.Selection( | ||
selection=lambda self: self._selection_option(), | ||
string="Option", | ||
default="asap", | ||
required=True, | ||
) | ||
date_deadline = fields.Datetime( | ||
compute="_compute_date_deadline", store=True, readonly=False, required=True | ||
) | ||
|
||
_sql_constraints = [ | ||
( | ||
"check_scheduled_date", | ||
"CHECK (date_deadline::date >= now()::date)", | ||
"You cannot reschedule deliveries in the past.", | ||
), | ||
] | ||
|
||
def _selection_option(self): | ||
options = [ | ||
("free", "Free"), | ||
("asap", "As soon as possible"), | ||
] | ||
if self.env.context.get("from_sale_order_id"): | ||
options.append(("contextual", "Contextual")) | ||
return options | ||
|
||
@api.depends("option") | ||
def _compute_date_deadline(self): | ||
from_sale_order_id = self.env.context.get("from_sale_order_id") | ||
order = self.env["sale.order"].browse(from_sale_order_id).exists() | ||
for rec in self: | ||
rec.date_deadline = False | ||
if rec.option == "asap": | ||
rec.date_deadline = fields.Datetime.now() | ||
elif rec.option == "contextual" and order: | ||
rec.date_deadline = order.commitment_date or order.expected_date | ||
|
||
@api.model | ||
def default_get(self, fields_list): | ||
res = super().default_get(fields_list) | ||
active_model = self.env.context.get("active_model") | ||
active_ids = self.env.context.get("active_ids") | ||
from_sale_order_id = self.env.context.get("from_sale_order_id") | ||
from_sale_order = self.env["sale.order"].browse(from_sale_order_id).exists() | ||
if active_model == "sale.order.line" and active_ids: | ||
res["order_line_ids"] = [(6, 0, active_ids)] | ||
if active_model == "stock.move" and active_ids: | ||
res["move_ids"] = [(6, 0, active_ids)] | ||
if from_sale_order: | ||
res["option"] = "contextual" | ||
return res | ||
|
||
def validate(self): | ||
self.ensure_one() | ||
move_states = ( | ||
"draft", | ||
"waiting", | ||
"confirmed", | ||
"partially_available", | ||
"assigned", | ||
) | ||
moves = (self.order_line_ids.move_ids or self.move_ids).filtered_domain( | ||
[("state", "in", move_states), ("release_blocked", "=", True)] | ||
) | ||
# Unset current deliveries (keep track of them to delete empty ones at the end) | ||
pickings = moves.picking_id | ||
moves.picking_id = False | ||
# Update the scheduled date | ||
date_deadline = ( | ||
fields.Datetime.now() if self.option == "asap" else self.date_deadline | ||
) | ||
date_planned = fields.Datetime.subtract( | ||
date_deadline, days=self.env.company.security_lead | ||
) | ||
moves.date = date_planned | ||
# Re-assign deliveries: moves sharing the same criteria - like date - will | ||
# be part of the same delivery. | ||
# NOTE: this will also leverage stock_picking_group_by_partner_by_carrier | ||
# module if this one is installed for instance | ||
moves._assign_picking() | ||
# Unblock release | ||
moves.action_unblock_release() | ||
# Clean up empty deliveries | ||
pickings.filtered(lambda o: not o.move_ids and not o.printed).unlink() |
28 changes: 28 additions & 0 deletions
28
sale_stock_available_to_promise_release_block/wizards/unblock_release.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<!-- Copyright 2024 Camptocamp SA | ||
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). --> | ||
<odoo> | ||
|
||
<record id="unblock_release_view_form" model="ir.ui.view"> | ||
<field name="name">unblock.release.form</field> | ||
<field name="model">unblock.release</field> | ||
<field name="arch" type="xml"> | ||
<form> | ||
<sheet> | ||
<group> | ||
<field name="option" /> | ||
<field | ||
name="date_deadline" | ||
attrs="{'invisible': [('option', '=', 'asap')]}" | ||
/> | ||
</group> | ||
</sheet> | ||
<footer> | ||
<button name="validate" type="object" string="Validate" class="btn-primary" /> | ||
<button special="cancel" string="Cancel" class="btn-default" /> | ||
</footer> | ||
</form> | ||
</field> | ||
</record> | ||
|
||
</odoo> |