1
1
# Copyright 2024 Antoni Marroig(APSL-Nagarro)<[email protected] >
2
2
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
3
3
4
- from odoo import _ , fields , models
4
+ from odoo import _ , api , fields , models
5
+
6
+ from odoo .addons .quality_control_oca .models .qc_trigger_line import _filter_trigger_lines
5
7
6
8
7
9
class RepairOrder (models .Model ):
@@ -13,6 +15,91 @@ class RepairOrder(models.Model):
13
15
"Inspections" ,
14
16
)
15
17
18
+ auto_generate_qc_inspection = fields .Boolean (
19
+ related = "company_id.repair_auto_generate_qc_inspection" ,
20
+ )
21
+ generate_qc_inspection_state = fields .Selection (
22
+ related = "company_id.repair_generate_qc_inspection_state" ,
23
+ )
24
+ show_button_create_qc_inspection = fields .Boolean (
25
+ compute = "_compute_show_button_create_qc_inspection"
26
+ )
27
+
28
+ @api .depends (
29
+ "auto_generate_qc_inspection" ,
30
+ "generate_qc_inspection_state" ,
31
+ "state" ,
32
+ "invoice_method" ,
33
+ )
34
+ def _compute_show_button_create_qc_inspection (self ):
35
+ self .show_button_create_qc_inspection = False
36
+ for repair in self .filtered (lambda r : not r .auto_generate_qc_inspection ):
37
+ if repair .generate_qc_inspection_state == "confirmed" :
38
+ if repair .state == "confirmed" :
39
+ self .show_button_create_qc_inspection = True
40
+ elif repair .state == "ready" and repair .invoice_method == "b4repair" :
41
+ self .show_button_create_qc_inspection = True
42
+ elif (
43
+ repair .generate_qc_inspection_state == "done" and repair .state == "done"
44
+ ):
45
+ self .show_button_create_qc_inspection = True
46
+
47
+ def create_qc_inspection (self ):
48
+ self .ensure_one ()
49
+ inspection_model = self .env ["qc.inspection" ].sudo ()
50
+ qc_trigger = self .sudo ().env .ref ("repair_quality_control.qc_trigger_repair" )
51
+ trigger_lines = set ()
52
+ for model in [
53
+ "qc.trigger.product_category_line" ,
54
+ "qc.trigger.product_template_line" ,
55
+ "qc.trigger.product_line" ,
56
+ ]:
57
+ partner = self .partner_id if qc_trigger .partner_selectable else False
58
+ trigger_lines = trigger_lines .union (
59
+ self .env [model ]
60
+ .sudo ()
61
+ .get_trigger_line_for_product (
62
+ qc_trigger , self .product_id .sudo (), partner = partner
63
+ )
64
+ )
65
+ for trigger_line in _filter_trigger_lines (trigger_lines ):
66
+ inspection_model ._make_inspection (self , trigger_line )
67
+
68
+ def action_repair_confirm (self ):
69
+ result = super ().action_repair_confirm ()
70
+ for rep in self :
71
+ auto_generate = rep .auto_generate_qc_inspection
72
+ if auto_generate and rep .generate_qc_inspection_state == "confirmed" :
73
+ rep .create_qc_inspection ()
74
+ return result
75
+
76
+ def action_repair_done (self ):
77
+ result = super ().action_repair_done ()
78
+ for rep in self :
79
+ auto_generate = rep .auto_generate_qc_inspection
80
+ if auto_generate and rep .generate_qc_inspection_state == "done" :
81
+ if rep .invoice_method != "after_repair" :
82
+ rep .create_qc_inspection ()
83
+ return result
84
+
85
+ def action_repair_invoice_create (self ):
86
+ result = super ().action_repair_invoice_create ()
87
+ for rep in self :
88
+ if rep .state == "done" :
89
+ auto_generate = rep .auto_generate_qc_inspection
90
+ if auto_generate and rep .generate_qc_inspection_state == "done" :
91
+ if rep .invoice_method == "after_repair" :
92
+ rep .create_qc_inspection ()
93
+ return result
94
+
95
+ def action_repair_cancel (self ):
96
+ inspections = self .sudo ().inspection_ids
97
+ draft_inspections = inspections .filtered (lambda i : i .state == "draft" )
98
+ draft_inspections .unlink ()
99
+ inspections -= draft_inspections
100
+ inspections .action_cancel ()
101
+ return super ().action_repair_cancel ()
102
+
16
103
def action_create_qc_inspection (self ):
17
104
self .ensure_one ()
18
105
action = self .env ["ir.actions.act_window" ]._for_xml_id (
@@ -22,14 +109,10 @@ def action_create_qc_inspection(self):
22
109
action ["views" ] = [(False , "form" )]
23
110
action ["target" ] = "current"
24
111
action ["name" ] = _ ("Create Inspection" )
25
-
26
112
action ["context" ] = {
27
113
"default_qty" : self .product_qty ,
28
- "default_repair_id" : self .id ,
29
- "default_object_id" : f"product.product,{ self .product_id .id } " ,
114
+ "default_object_id" : f"repair.order,{ self .id } " ,
30
115
}
31
- if self .lot_id :
32
- action ["context" ]["default_object_id" ] = f"stock.lot,{ self .lot_id .id } "
33
116
return action
34
117
35
118
def action_view_repair_inspections (self ):
0 commit comments