@@ -21,14 +21,34 @@ class PickingBatchAutoCreateAction(Component):
21
21
22
22
_advisory_lock_name = "shopfloor_batch_picking_create"
23
23
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
+ ):
25
32
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 )
27
35
if not pickings :
36
+ search_user = None
28
37
pickings = self ._search_pickings (picking_types )
29
-
30
38
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
+ )
32
52
if not pickings :
33
53
return self .env ["stock.picking.batch" ].browse ()
34
54
return self ._create_batch (pickings )
@@ -62,21 +82,29 @@ def _lock(self):
62
82
"lock acquired to create a picking batch (%s)" , self .env .user .login
63
83
)
64
84
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
+ ):
66
88
domain = [
67
89
("picking_type_id" , "in" , picking_types .ids ),
68
90
("state" , "in" , ("assigned" , "partially_available" )),
69
91
("batch_id" , "=" , False ),
70
92
("user_id" , "=" , user .id if user else False ),
71
93
]
94
+ if commercial_partner :
95
+ domain .append (
96
+ ("partner_id.commercial_partner_id" , "=" , commercial_partner .id )
97
+ )
72
98
return domain
73
99
74
- def _search_pickings (self , picking_types , user = None ):
100
+ def _search_pickings (self , picking_types , user = None , commercial_partner = None ):
75
101
# We can't use a limit in the SQL search because the 'priority' fields
76
102
# is sometimes empty (it seems the inverse StockPicking.priority field
77
103
# mess up with default on stock.move), we have to sort in Python.
78
104
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
+ )
80
108
)
81
109
82
110
def _sort (self , pickings ):
0 commit comments