11# Copyright 2022 CreuBlanca
22# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
33
4- from odoo import api , fields , models
4+ from odoo import Command , api , fields , models
55
66
77class PurchaseOrder (models .Model ):
@@ -24,8 +24,9 @@ def action_view_equipments(self):
2424 items = self .env ["maintenance.equipment" ].search (
2525 [("purchase_id" , "=" , self .id )]
2626 )
27- action = self .env .ref ("maintenance.hr_equipment_action" )
28- action_dict = action .sudo ().read ()[0 ]
27+ action_dict = self .env ["ir.actions.act_window" ]._for_xml_id (
28+ "maintenance.hr_equipment_action"
29+ )
2930 if len (items ) == 1 :
3031 res = self .env .ref ("maintenance.hr_equipment_view_form" , False )
3132 action_dict ["views" ] = [(res and res .id or False , "form" )]
@@ -36,29 +37,25 @@ def action_view_equipments(self):
3637 action_dict = {"type" : "ir.actions.act_window_close" }
3738 return action_dict
3839
40+ def _get_lines_to_create_equipments (self ):
41+ return self .order_line .filtered (
42+ lambda x : (
43+ x .product_qty > len (x .equipment_ids )
44+ and x .product_id
45+ and x .equipment_category_id
46+ )
47+ )
48+
3949 def button_approve (self , force = False ):
4050 result = super ().button_approve (force = force )
41- equipment_model = self .env ["maintenance.equipment" ]
42- for order in self .filtered (lambda po : po .state in ("purchase" , "done" )):
43- for line in order .order_line .filtered (
44- lambda x : (
45- not x .equipment_ids
46- and x .product_id
47- and x .product_id .product_tmpl_id .maintenance_ok
48- )
49- ):
50- if not line .equipment_category_id :
51- line ._set_equipment_category ()
52- # Create equipments
53- limit = int (line .product_qty ) + 1
54- vals = line ._prepare_equipment_vals ()
55- equipment_ids = []
56- for _i in range (1 , limit ):
57- equipment = equipment_model .create (vals )
58- equipment_ids .append ((4 , equipment .id ))
59- line .equipment_ids = equipment_ids
51+ self ._create_equipments ()
6052 return result
6153
54+ def _create_equipments (self ):
55+ for order in self .filtered (lambda po : po .state in ("purchase" , "done" )):
56+ for line in order ._get_lines_to_create_equipments ():
57+ line .create_equipments ()
58+
6259
6360class PurchaseOrderLine (models .Model ):
6461 _inherit = "purchase.order.line"
@@ -70,8 +67,9 @@ class PurchaseOrderLine(models.Model):
7067 store = True ,
7168 readonly = False ,
7269 )
73- equipment_ids = fields .Many2many (
70+ equipment_ids = fields .One2many (
7471 comodel_name = "maintenance.equipment" ,
72+ inverse_name = "purchase_line_id" ,
7573 string = "Equipments" ,
7674 copy = False ,
7775 )
@@ -80,13 +78,11 @@ class PurchaseOrderLine(models.Model):
8078 @api .depends ("product_id" )
8179 def _compute_equipment_category_id (self ):
8280 for item in self :
83- if (
84- item .product_id .maintenance_ok
85- and item .product_id .product_tmpl_id .categ_id .equipment_category_ids
86- ):
87- item .equipment_category_id = fields .first (
88- item .product_id .product_tmpl_id .categ_id .equipment_category_ids
81+ if item .product_id .product_tmpl_id .equipment_category_id :
82+ item .equipment_category_id = (
83+ item .product_id .product_tmpl_id .equipment_category_id
8984 )
85+
9086 else :
9187 item .equipment_category_id = item .equipment_category_id
9288
@@ -100,37 +96,28 @@ def _compute_equipment_count(self):
10096 for item in self :
10197 item .equipment_count = mapping .get (item .id , 0 )
10298
103- def _prepare_equipment_category_vals (self ):
104- categ = self .product_id .product_tmpl_id .categ_id
105- return {"name" : categ .name , "product_category_id" : categ .id }
99+ def _qty_to_create_equipments (self ):
100+ return int (self .product_qty - len (self .equipment_ids ))
106101
107- def _set_equipment_category (self ):
108- if not self .equipment_category_id :
109- category_model = self .env ["maintenance.equipment.category" ]. sudo ()
110- category = fields . first (
111- self . product_id . product_tmpl_id . categ_id . equipment_category_ids
112- )
113- if not category :
114- category = category_model . create (
115- self . _prepare_equipment_category_vals ( )
116- )
117- self . equipment_category_id = category . id
102+ def create_equipments (self ):
103+ self .ensure_one ()
104+ equipment_model = self .env ["maintenance.equipment" ]
105+ equipments = equipment_model . create (
106+ [
107+ self . _prepare_equipment_vals ( )
108+ for _ in range ( self . _qty_to_create_equipments ())
109+ ]
110+ )
111+ self . equipment_ids = [ Command . link ( eq . id ) for eq in equipments ]
112+ return equipments
118113
119114 def _prepare_equipment_vals (self ):
120115 return {
121116 "purchase_line_id" : self .id ,
122117 "name" : self .product_id .name ,
123- "product_id" : self .product_id .id ,
124118 "category_id" : self .equipment_category_id .id ,
125119 "assign_date" : self .order_id .date_order ,
126120 "effective_date" : self .order_id .date_planned ,
127121 "partner_id" : self .order_id .partner_id .id ,
128122 "partner_ref" : self .order_id .partner_ref ,
129123 }
130-
131- def _prepare_account_move_line (self , move = False ):
132- result = super ()._prepare_account_move_line (move = move )
133- result ["equipment_ids" ] = [
134- (4 , equipment .id ) for equipment in self .equipment_ids
135- ]
136- return result
0 commit comments