1
1
# Copyright 2020 Tecnativa - Ernesto Tejeda
2
2
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
3
3
4
- from odoo import fields , models
4
+ from ast import literal_eval
5
+ from collections import defaultdict
6
+
7
+ from odoo import _ , api , fields , models
8
+ from odoo .osv .expression import AND
9
+
10
+ PROCESSED_STATES = ["received" , "refunded" , "replaced" , "finished" ]
11
+ AWAITING_ACTION_STATES = ["waiting_return" , "waiting_replacement" , "confirmed" ]
5
12
6
13
7
14
class RmaOperation (models .Model ):
@@ -10,7 +17,107 @@ class RmaOperation(models.Model):
10
17
11
18
active = fields .Boolean (default = True )
12
19
name = fields .Char (required = True , translate = True )
20
+ color = fields .Integer ()
21
+ count_rma_draft = fields .Integer (compute = "_compute_count_rma" )
22
+ count_rma_awaiting_action = fields .Integer (compute = "_compute_count_rma" )
23
+ count_rma_processed = fields .Integer (compute = "_compute_count_rma" )
13
24
14
25
_sql_constraints = [
15
26
("name_uniq" , "unique (name)" , "That operation name already exists !" ),
16
27
]
28
+
29
+ @api .model
30
+ def _get_rma_draft_domain (self ):
31
+ return [("state" , "=" , "draft" )]
32
+
33
+ @api .model
34
+ def _get_rma_awaiting_action_domain (self ):
35
+ return [("state" , "in" , AWAITING_ACTION_STATES )]
36
+
37
+ @api .model
38
+ def _get_rma_processed_domain (self ):
39
+ return [("state" , "in" , PROCESSED_STATES )]
40
+
41
+ def _compute_count_rma (self ):
42
+ self .update (
43
+ {
44
+ "count_rma_draft" : 0 ,
45
+ "count_rma_processed" : 0 ,
46
+ "count_rma_awaiting_action" : 0 ,
47
+ }
48
+ )
49
+ state_by_op = defaultdict (int )
50
+ for group in self .env ["rma" ].read_group (
51
+ AND ([[("operation_id" , "!=" , False )]]),
52
+ groupby = ["operation_id" , "state" ],
53
+ fields = ["id" ],
54
+ lazy = False ,
55
+ ):
56
+ operation_id = group .get ("operation_id" )[0 ]
57
+ state = group .get ("state" )
58
+ count = group .get ("__count" )
59
+ if state == "draft" :
60
+ state_by_op [(operation_id , "count_rma_draft" )] += count
61
+ if state in PROCESSED_STATES :
62
+ state_by_op [(operation_id , "count_rma_processed" )] += count
63
+ if state in AWAITING_ACTION_STATES :
64
+ state_by_op [(operation_id , "count_rma_awaiting_action" )] += count
65
+ for (operation_id , field ), count in state_by_op .items ():
66
+ self .browse (operation_id ).update ({field : count })
67
+
68
+ def _get_action (self , name , domain ):
69
+ action = self .env ["ir.actions.actions" ]._for_xml_id ("rma.rma_action" )
70
+ action ["display_name" ] = name
71
+ context = {
72
+ "search_default_operation_id" : [self .id ],
73
+ "default_operation_id" : self .id ,
74
+ }
75
+ action_context = literal_eval (action ["context" ])
76
+ context = {** action_context , ** context }
77
+ action ["context" ] = context
78
+ action ["domain" ] = domain
79
+ return action
80
+
81
+ def get_action_rma_tree_draft (self ):
82
+ self .ensure_one ()
83
+ name = self .display_name + ": " + _ ("Draft" )
84
+ return self ._get_action (
85
+ name ,
86
+ domain = AND (
87
+ [
88
+ [("operation_id" , "=" , self .id )],
89
+ self ._get_rma_draft_domain (),
90
+ ]
91
+ ),
92
+ )
93
+
94
+ def get_action_rma_tree_awaiting_action (self ):
95
+ self .ensure_one ()
96
+ name = self .display_name + ": " + _ ("Awaiting Action" )
97
+ return self ._get_action (
98
+ name ,
99
+ domain = AND (
100
+ [
101
+ [("operation_id" , "=" , self .id )],
102
+ self ._get_rma_awaiting_action_domain (),
103
+ ]
104
+ ),
105
+ )
106
+
107
+ def get_action_rma_tree_processed (self ):
108
+ self .ensure_one ()
109
+ name = self .display_name + ": " + _ ("Processed" )
110
+ return self ._get_action (
111
+ name ,
112
+ domain = AND (
113
+ [
114
+ [("operation_id" , "=" , self .id )],
115
+ self ._get_rma_processed_domain (),
116
+ ]
117
+ ),
118
+ )
119
+
120
+ def get_action_all_rma (self ):
121
+ self .ensure_one ()
122
+ name = self .display_name
123
+ return self ._get_action (name , domain = [("operation_id" , "=" , self .id )])
0 commit comments