1
1
# Copyright 2013-2019 Camptocamp SA
2
2
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)
3
- from odoo import _ , api , fields , models
3
+ from odoo import Command , api , fields , models
4
+ from odoo .exceptions import UserError
4
5
5
6
6
7
class StockBatchPicking (models .Model ):
@@ -13,9 +14,24 @@ class StockBatchPicking(models.Model):
13
14
_inherit = "stock.picking.batch"
14
15
15
16
carrier_id = fields .Many2one (
16
- "delivery.carrier" , "Carrier" , states = { "done" : [( "readonly" , True )]}
17
+ comodel_name = "delivery.carrier" ,
17
18
)
18
- option_ids = fields .Many2many ("delivery.carrier.option" , string = "Options" )
19
+ option_ids = fields .Many2many (
20
+ comodel_name = "delivery.carrier.option" ,
21
+ )
22
+ option_ids_domain = fields .Binary (
23
+ string = "Options domain" ,
24
+ help = "Dynamic domain used for the carrier options" ,
25
+ compute = "_compute_option_ids_domain" ,
26
+ )
27
+
28
+ @api .depends ("carrier_id" )
29
+ def _compute_option_ids_domain (self ):
30
+ for rec in self :
31
+ options_domain = None
32
+ if available_options := self .carrier_id .available_option_ids :
33
+ options_domain = [("id" , "in" , available_options .ids )]
34
+ rec .option_ids_domain = options_domain
19
35
20
36
def action_set_options (self ):
21
37
"""Apply options to picking of the batch
@@ -26,7 +42,7 @@ def action_set_options(self):
26
42
for rec in self :
27
43
options_datas = {
28
44
"carrier_id" : rec .carrier_id .id ,
29
- "option_ids" : [( 6 , 0 , rec .option_ids .ids )],
45
+ "option_ids" : [Command . set ( rec .option_ids .ids )],
30
46
}
31
47
rec .picking_ids .write (options_datas )
32
48
@@ -36,38 +52,29 @@ def _get_options_to_add(self, carrier=None):
36
52
return options .filtered (lambda rec : rec .mandatory or rec .by_default )
37
53
38
54
@api .onchange ("carrier_id" )
39
- def carrier_id_change (self ):
55
+ def onchange_carrier_id (self ):
40
56
"""Inherit this method in your module"""
41
- if self .carrier_id :
42
- available_options = self .carrier_id .available_option_ids
43
- default_options = self ._get_options_to_add ()
44
- self .option_ids = [(6 , 0 , default_options .ids )]
45
- self .carrier_code = self .carrier_id .code
46
- return {
47
- "domain" : {
48
- "option_ids" : [("id" , "in" , available_options .ids )],
49
- }
50
- }
51
- return {}
57
+ if not self .carrier_id :
58
+ return
59
+ default_options = self ._get_options_to_add ()
60
+ self .option_ids = [Command .set (default_options .ids )]
61
+ self .carrier_code = self .carrier_id .code
52
62
53
63
@api .onchange ("option_ids" )
54
- def option_ids_change (self ):
55
- res = {}
64
+ def onchange_option_ids (self ):
56
65
if not self .carrier_id :
57
- return res
66
+ return
67
+
58
68
for available_option in self .carrier_id .available_option_ids :
59
69
if available_option .mandatory and available_option not in self .option_ids :
60
- res ["warning" ] = {
61
- "title" : _ ("User Error !" ),
62
- "message" : _ (
63
- "You can not remove a mandatory option."
70
+ # Optionally, reset the options to the default values.
71
+ self .option_ids = self ._get_options_to_add ()
72
+ raise UserError (
73
+ self .env ._ (
74
+ "You cannot remove a mandatory option. "
64
75
"\n Please reset options to default."
65
- ),
66
- }
67
- # Due to https://github.com/odoo/odoo/issues/2693 we cannot
68
- # reset options
69
- # self.option_ids = self._get_options_to_add()
70
- return res
76
+ )
77
+ )
71
78
72
79
def _values_with_carrier_options (self , values ):
73
80
values = values .copy ()
@@ -77,7 +84,7 @@ def _values_with_carrier_options(self, values):
77
84
carrier = self .env ["delivery.carrier" ].browse (carrier_id )
78
85
options = self ._get_options_to_add (carrier )
79
86
if options :
80
- values .update (option_ids = [( 6 , 0 , options .ids )])
87
+ values .update (option_ids = [Command . set ( options .ids )])
81
88
return values
82
89
83
90
def write (self , values ):
@@ -93,16 +100,17 @@ def write(self, values):
93
100
self .purge_tracking_references ()
94
101
return result
95
102
96
- @api .model
97
- def create (self , values ):
103
+ @api .model_create_multi
104
+ def create (self , vals_list ):
98
105
"""Set the default options when the delivery method is set on creation
99
106
100
107
So we are sure that the options are always in line with the
101
108
current delivery method.
102
109
103
110
"""
104
- values = self ._values_with_carrier_options (values )
105
- return super ().create (values )
111
+ for values in vals_list :
112
+ self ._values_with_carrier_options (values )
113
+ return super ().create (vals_list )
106
114
107
115
def purge_tracking_references (self ):
108
116
"""Purge tracking for each picking and destination package"""
0 commit comments