Skip to content

Commit

Permalink
[UPD] stock_logistics_workflow
Browse files Browse the repository at this point in the history
Added reservation logic for moves defined in odoo base
and added test for "by_date" resevation method
  • Loading branch information
FLAlvaroGomez committed Jan 10, 2025
1 parent 61e6526 commit 82e3bb9
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 7 deletions.
42 changes: 36 additions & 6 deletions stock_picking_mass_action/models/stock_picking.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,53 @@
# Copyright 2018 Tecnativa - Vicent Cubells
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).


from odoo import _, api
from odoo.models import Model
from odoo.osv import expression


class StockPicking(Model):
_inherit = "stock.picking"

@api.model
def check_assign_all(self, domain=None):
def check_assign_all(self, domain=None, batch_size=False, company=None):
"""Try to assign confirmed pickings"""
search_domain = [("state", "=", "confirmed")]
if not batch_size:
batch_size = 1000
move_assign_domain = self.env["procurement.group"]._get_moves_to_assign_domain(
company
)
reservation_index = next(
(i for i, t in enumerate(move_assign_domain) if t[0] == "reservation_date"),
None,
)
if reservation_index is not None:
original_date_condition = move_assign_domain[reservation_index]
move_assign_domain.pop(reservation_index)
move_assign_domain = expression.AND(
[
move_assign_domain,
["|", original_date_condition, ("reservation_date", "=", False)],
]
)

if domain:
search_domain += domain
move_assign_domain = expression.AND([domain, move_assign_domain])
else:
search_domain += [("picking_type_code", "=", "outgoing")]
records = self.search(search_domain, order="scheduled_date")
records.action_assign()
move_assign_domain = expression.AND(
[move_assign_domain, [("picking_code", "=", "outgoing")]]
)

moves_to_assign = self.env["stock.move"].search(
move_assign_domain,
limit=None,
order="reservation_date, priority desc, date asc, id asc",
)
total_items = len(moves_to_assign)
for i in range(0, total_items, batch_size):
batch = moves_to_assign[i : i + batch_size]
self.env["stock.move"].browse(batch.ids).sudo()._action_assign()

def action_immediate_transfer_wizard(self):
view = self.env.ref("stock.view_immediate_transfer")
Expand Down
36 changes: 35 additions & 1 deletion stock_picking_mass_action/tests/test_mass_action.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Copyright 2018 Tecnativa - Vicent Cubells
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from datetime import timedelta

from odoo import fields
from odoo.tests import common


Expand Down Expand Up @@ -95,10 +98,41 @@ def test_mass_action(self):
wiz_confirm.mass_action()
self.assertEqual(pick3.state, "confirmed")
self.assertEqual(pick4.state, "confirmed")
pickings.check_assign_all(domain=[("picking_type_code", "=", "outgoing")])
pickings.check_assign_all(domain=[("picking_code", "=", "outgoing")])
self.assertEqual(pick1.state, "assigned")
self.assertEqual(pick2.state, "assigned")

def test_mass_action_by_date(self):
# Set by date method
self.env.ref("stock.picking_type_out").write(
{"reservation_method": "by_date", "reservation_days_before": 4}
)
self.assertEqual(self.picking.state, "draft")
wiz = self.env["stock.picking.mass.action"]
# We test checking assign all with "by date" method
pickings = self.env["stock.picking"]
pickOutDate = self.picking.copy()
# Set scheduled date out of the range of 4 days
pickOutDate.scheduled_date = fields.Date.today() + timedelta(days=15)
pickings |= pickOutDate
pickInDate = self.picking.copy()
pickings |= pickInDate
self.assertEqual(pickOutDate.state, "draft")
self.assertEqual(pickInDate.state, "draft")
wiz_confirm = wiz.create(
{"picking_ids": [(6, 0, [pickOutDate.id, pickInDate.id])]}
)
wiz_confirm.confirm = True
wiz_confirm.mass_action()
self.assertEqual(pickOutDate.state, "confirmed")
self.assertEqual(pickInDate.state, "assigned")
pickings.check_assign_all()
self.assertEqual(pickOutDate.state, "confirmed")
# Change scheduled date in the range of 4 days
pickOutDate.scheduled_date = fields.Date.today() + timedelta(days=1)
pickings.check_assign_all()
self.assertEqual(pickOutDate.state, "assigned")

def test_mass_action_inmediate_transfer(self):
wiz_tranfer = self.env["stock.picking.mass.action"].create(
{"picking_ids": [(4, self.picking.id)], "confirm": True, "transfer": True}
Expand Down

0 comments on commit 82e3bb9

Please sign in to comment.