Skip to content

Commit f87f3ea

Browse files
committed
s_b_a_creation: group by commercial entity
New option "Group by commercial entity" allowing to create batch grouping operations of the same commercial enntity. This option ignores priorities or constraints/limits which normally apply on a batch creation.
1 parent f48c0f0 commit f87f3ea

File tree

6 files changed

+89
-10
lines changed

6 files changed

+89
-10
lines changed

shopfloor_batch_automatic_creation/actions/picking_batch_auto_create.py

+35-7
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,34 @@ class PickingBatchAutoCreateAction(Component):
2121

2222
_advisory_lock_name = "shopfloor_batch_picking_create"
2323

24-
def create_batch(self, picking_types, max_pickings=0, max_weight=0, max_volume=0):
24+
def create_batch(
25+
self,
26+
picking_types,
27+
max_pickings=0,
28+
max_weight=0,
29+
max_volume=0,
30+
group_by_commercial_partner=False,
31+
):
2532
self._lock()
26-
pickings = self._search_pickings(picking_types, user=self.env.user)
33+
search_user = self.env.user
34+
pickings = self._search_pickings(picking_types, user=search_user)
2735
if not pickings:
36+
search_user = None
2837
pickings = self._search_pickings(picking_types)
29-
3038
pickings = self._sort(pickings)
31-
pickings = self._apply_limits(pickings, max_pickings, max_weight, max_volume)
39+
if group_by_commercial_partner:
40+
# From the first operation we got, get all operations having the
41+
# same commercial entity
42+
picking = fields.first(pickings)
43+
commercial_partner = picking.partner_id.commercial_partner_id
44+
pickings = self._search_pickings(
45+
picking_types, user=search_user, commercial_partner=commercial_partner
46+
)
47+
else:
48+
# Otherwise process by priorities by applying the limits
49+
pickings = self._apply_limits(
50+
pickings, max_pickings, max_weight, max_volume
51+
)
3252
if not pickings:
3353
return self.env["stock.picking.batch"].browse()
3454
return self._create_batch(pickings)
@@ -62,21 +82,29 @@ def _lock(self):
6282
"lock acquired to create a picking batch (%s)", self.env.user.login
6383
)
6484

65-
def _search_pickings_domain(self, picking_types, user=None):
85+
def _search_pickings_domain(
86+
self, picking_types, user=None, commercial_partner=None
87+
):
6688
domain = [
6789
("picking_type_id", "in", picking_types.ids),
6890
("state", "in", ("assigned", "partially_available")),
6991
("batch_id", "=", False),
7092
("user_id", "=", user.id if user else False),
7193
]
94+
if commercial_partner:
95+
domain.append(
96+
("partner_id.commercial_partner_id", "=", commercial_partner.id)
97+
)
7298
return domain
7399

74-
def _search_pickings(self, picking_types, user=None):
100+
def _search_pickings(self, picking_types, user=None, commercial_partner=None):
75101
# We can't use a limit in the SQL search because the 'priority' fields
76102
# is sometimes empty (it seems the inverse StockPicking.priority field
77103
# mess up with default on stock.move), we have to sort in Python.
78104
return self.env["stock.picking"].search(
79-
self._search_pickings_domain(picking_types, user=user)
105+
self._search_pickings_domain(
106+
picking_types, user=user, commercial_partner=commercial_partner
107+
)
80108
)
81109

82110
def _sort(self, pickings):

shopfloor_batch_automatic_creation/models/shopfloor_menu.py

+6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ class ShopfloorMenu(models.Model):
1818
" and a max quantity of 3, the batch will only contain the 2"
1919
" priority transfers.",
2020
)
21+
batch_group_by_commercial_partner = fields.Boolean(
22+
string="Group by commercial entity",
23+
default=False,
24+
help="If enabled, transfers will be grouped by commercial entity."
25+
"This could mix priorities and will ignore the constraints to apply.",
26+
)
2127
batch_create_max_picking = fields.Integer(
2228
string="Max transfers",
2329
default=0,

shopfloor_batch_automatic_creation/readme/DESCRIPTION.rst

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ batch is available, it automatically creates a new batch for the user.
66
Some options can be configured on the Shopfloor menu:
77

88
* Activate or not the batch creation
9+
* Group by commercial entity
910
* Max number of transfer per batch
1011
* Max weight per batch
1112
* Max volume per batch

shopfloor_batch_automatic_creation/services/cluster_picking.py

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def _batch_auto_create(self):
1919
menu = self.work.menu
2020
return auto_batch.create_batch(
2121
self.picking_types,
22+
group_by_commercial_partner=menu.batch_group_by_commercial_partner,
2223
max_pickings=menu.batch_create_max_picking,
2324
max_volume=menu.batch_create_max_volume,
2425
max_weight=menu.batch_create_max_weight,

shopfloor_batch_automatic_creation/tests/test_batch_create.py

+33
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,36 @@ def test_cluster_picking_select(self):
156156
next_state="confirm_start",
157157
data=data,
158158
)
159+
160+
def test_create_batch_group_by_commercial_partner(self):
161+
"""Test batch creation by grouping all operations of the same
162+
commercial entity, ignoring priorities and limits.
163+
"""
164+
partner_model = self.env["res.partner"].sudo()
165+
partner1_com = partner_model.create(
166+
{"name": "COM PARTNER 1", "is_company": True}
167+
)
168+
partner1_contact = partner_model.create(
169+
{
170+
"name": "CONTACT PARTNER 1",
171+
"parent_id": partner1_com.id,
172+
"is_company": False,
173+
}
174+
)
175+
partner2_com = partner_model.create(
176+
{"name": "COM PARTNER 2", "is_company": True}
177+
)
178+
partner2_contact = partner_model.create(
179+
{
180+
"name": "CONTACT PARTNER 2",
181+
"parent_id": partner2_com.id,
182+
"is_company": False,
183+
}
184+
)
185+
self.picking1.write({"priority": "2", "partner_id": partner1_contact.id})
186+
self.picking2.write({"priority": "2", "partner_id": partner2_contact.id})
187+
self.picking3.write({"priority": "3", "partner_id": partner2_contact.id})
188+
batch = self.auto_batch.create_batch(
189+
self.picking_type, group_by_commercial_partner=True
190+
)
191+
self.assertEqual(batch.picking_ids, self.picking2 | self.picking3)

shopfloor_batch_automatic_creation/views/shopfloor_menu_views.xml

+13-3
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,26 @@
1212
>
1313
<field name="batch_create" />
1414
<field
15-
name="batch_create_max_picking"
15+
name="batch_group_by_commercial_partner"
1616
attrs="{'invisible': [('batch_create', '=', False)]}"
1717
/>
18+
<field
19+
name="batch_create_max_picking"
20+
attrs="{
21+
'invisible': ['|', ('batch_create', '=', False), ('batch_group_by_commercial_partner', '=', True)],
22+
}"
23+
/>
1824
<field
1925
name="batch_create_max_volume"
20-
attrs="{'invisible': [('batch_create', '=', False)]}"
26+
attrs="{
27+
'invisible': ['|', ('batch_create', '=', False), ('batch_group_by_commercial_partner', '=', True)],
28+
}"
2129
/>
2230
<field
2331
name="batch_create_max_weight"
24-
attrs="{'invisible': [('batch_create', '=', False)]}"
32+
attrs="{
33+
'invisible': ['|', ('batch_create', '=', False), ('batch_group_by_commercial_partner', '=', True)],
34+
}"
2535
/>
2636
</group>
2737
</group>

0 commit comments

Comments
 (0)