-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
301 lines (264 loc) · 12.1 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
from odoo import tools, models, fields, api, _
from odoo.exceptions import ValidationError
from datetime import date,datetime
class ProductProduct(models.Model):
_inherit = 'product.product'
def write(self, vals):
res = super(ProductProduct, self).write(vals)
if 'standard_price' in vals:
for rec in self:
vals = {
#'cost_id': self.id,
'product_id': rec.id,
'date': str(date.today()),
'total_unit_cost': rec.standard_price,
# 'direct_cost': line.direct_cost,
# 'indirect_cost': line.indirect_cost,
}
cost_id = self.env['product.product.cost'].create(vals)
return res
class MrpBom(models.Model):
_inherit = "mrp.bom"
# Demo methods
def _get_bom_all_product_qties_dict(self, product_dict={}):
for bom_line in self.bom_line_ids:
if not bom_line.product_id.bom_ids:
if bom_line.product_id.id not in product_dict:
product_dict[bom_line.product_id.id] = ['Component',bom_line.product_qty,self.id]
else:
if bom_line.product_id.id not in product_dict:
product_dict[bom_line.product_id.id] = ['Sub-assembly - %s'%(bom_line.product_id.display_name),bom_line.product_qty,self.id]
new_bom = bom_line.product_id.bom_ids[0]
product_dict = new_bom._get_bom_all_product_qties_dict(product_dict)
return product_dict
def _compute_costs(self):
for rec in self:
product = rec.product_tmpl_id.product_variant_ids[0]
rec.total_cost = product.standard_price
rec.indirect_cost = product.indirect_cost
rec.direct_cost = product.direct_cost
def update_product_costs(self):
for rec in self:
product_ids = self.env['product.product'].search([('product_tmpl_id','=',rec.product_tmpl_id.id)])
for product_id in product_ids:
vals = {
'standard_price': rec.total_cost,
'indirect_cost': rec.indirect_cost,
'direct_cost': rec.direct_cost,
}
product_id.write(vals)
total_cost = fields.Float('Total Cost',compute=_compute_costs)
indirect_cost = fields.Float('Direct Cost',compute=_compute_costs)
direct_cost = fields.Float('Indirect Cost',compute=_compute_costs)
class MrpBomCost(models.Model):
_name = 'mrp.bom.cost'
_description = 'mrp.bom.cost'
_inherit = ['mail.thread', 'mail.activity.mixin']
def btn_update_costs(self):
self.ensure_one()
standard_price = 0
for line in self.line_ids.filtered(lambda l: l.bom_id.id == self.bom_id.id):
product_id = line.product_id
product_id.standard_price = line.price_unit
product_id.indirect_cost = line.indirect_cost
product_id.direct_cost = line.direct_cost
standard_price = standard_price + line.price_unit
product_id = self.product_id
product_id.standard_price = standard_price
vals = {
'cost_id': self.id,
'product_id': product_id.id,
'date': str(date.today()),
'total_unit_cost': line.price_unit,
'direct_cost': line.direct_cost,
'indirect_cost': line.indirect_cost,
}
cost_id = self.env['product.product.cost'].create(vals)
def btn_fill_components(self):
self.ensure_one()
if not self.bom_id:
raise ValidationError('Debe seleccionar la lista de materiales')
for line in self.line_ids:
line.unlink()
products = self.bom_id._get_bom_all_product_qties_dict({})
for key,values in products.items():
product = self.env['product.product'].browse(key)
price_unit = product.standard_price
vals = {
'cost_id': self.id,
'product_id': key,
'bom_id': values[2],
'qty': values[1] * self.qty,
'standard_price': product.standard_price,
'line_type': values[0],
'price_unit': price_unit,
'total_cost': price_unit * values[1] * self.qty,
'uom_id': self.env['product.product'].browse(key).uom_id.id,
}
line_id = self.env['mrp.bom.cost.line'].create(vals)
line_id._compute_items()
def _compute_items(self):
for rec in self:
res = 0
res1 = 0
res2 = 0
for item in rec.line_ids.filtered(lambda l: l.bom_id.id == rec.bom_id.id):
res = res + item.price_unit * item.qty
res1 = res1 + item.total_direct_cost
res2 = res2 + item.total_indirect_cost
if rec.product_tmpl_id.standard_price != res:
res = rec.product_tmpl_id.standard_price
rec.total_cost = res
rec.total_direct_cost = res1
rec.total_indirect_cost = res2
def btn_show_components(self):
self.ensure_one()
self.only_components = True
for line in self.line_ids:
if not line.product_id.bom_ids:
line.active = False
def btn_show_all_items(self):
self.ensure_one()
self.only_components = False
lines = self.env['mrp.bom.cost.line'].search([('cost_id','=',self.id),('active','=',False)])
for line in lines:
line.active = True
def btn_show_first_level(self):
self.ensure_one()
self.only_components = True
lines = self.env['mrp.bom.cost.line'].search([('cost_id','=',self.id),'|',('active','=',False),('active','=',True)])
lines.write({'active': True})
lines = self.env['mrp.bom.cost.line'].search([('cost_id','=',self.id),('bom_id','!=',self.bom_id.id)])
lines.write({'active': False})
def btn_analyze_impact(self):
self.ensure_one()
for impact in self.impact_ids:
impact.unlink()
process = True
product_ids = self.product_id.ids
while process:
bom_lines = self.env['mrp.bom.line'].search([('product_id','in',product_ids)])
product_ids = []
if bom_lines:
for bom_line in bom_lines:
vals = {
'cost_id': self.id,
'bom_id': bom_line.bom_id.id,
}
self.env['mrp.bom.cost.impact'].create(vals)
product_ids.append(bom_line.bom_id.product_id.id)
else:
process = False
@api.onchange('line_ids.direct_cost','line_ids.indirect_cost')
def onchange_costs(self):
for line in self.line_ids:
line.price_unit = line.direct_cost + line.indirect_cost
name = fields.Char('Nombre')
bom_id = fields.Many2one('mrp.bom',string='Lista de Materiales')
product_tmpl_id = fields.Many2one('product.template',string='Producto',related='bom_id.product_tmpl_id')
product_id = fields.Many2one('product.product',string='Producto',related='bom_id.product_id')
date = fields.Date('Fecha',default=fields.Date.today())
qty = fields.Integer('Cantidad',default=1)
line_ids = fields.One2many(comodel_name='mrp.bom.cost.line',inverse_name='cost_id',string='Lineas')
total_cost = fields.Float('Costo Total',compute=_compute_items)
total_direct_cost = fields.Float('Costo Directo Total',compute=_compute_items)
total_indirect_cost = fields.Float('Costo Indirecto Total',compute=_compute_items)
temporary = fields.Boolean('temporary',default=False)
only_components = fields.Boolean('Only components',default=False)
impact_ids = fields.One2many(comodel_name='mrp.bom.cost.impact',inverse_name='cost_id',string='Impacto')
class MrpBomCostImpact(models.Model):
_name = 'mrp.bom.cost.impact'
_description = 'mrp.bom.cost.impact'
cost_id = fields.Many2one('mrp.bom.cost',string='Costo')
bom_id = fields.Many2one('mrp.bom',string='Lista de materiales')
product_id = fields.Many2one('product.product',string='Producto',related='bom_id.product_id')
class MrpBomCostLine(models.Model):
_name = 'mrp.bom.cost.line'
_description = 'mrp.bom.cost.line'
def _compute_items(self):
for rec in self:
res = 0
res1 = 0
res2 = 0
if rec.product_id:
res = rec.product_id.standard_price
res1 = rec.product_id.direct_cost
res2 = rec.product_id.indirect_cost
rec.direct_cost = res1
rec.indirect_cost = res2
rec.total_direct_cost = res1 * rec.qty
rec.total_indirect_cost = res2 * rec.qty
def _compute_update_date(self):
for rec in self:
res = None
if rec.product_id.cost_ids:
res = str(rec.product_id.cost_ids[0].write_date)[:10]
rec.update_date = res
def _compute_line_item(self):
for rec in self:
res = 0
if rec._origin.id:
items = self.env['mrp.bom.cost.line'].search([('cost_id','=',rec.cost_id.id),('id','<',rec._origin.id)])
else:
items = self.env['mrp.bom.cost.line'].search([('cost_id','=',rec.cost_id.id),('id','<',rec.id)])
if items:
res = len(items)
rec.line_item = res + 1
def show_document(self):
self.ensure_one()
if self.line_type == 'Component':
view_id = self.env.ref('product.product_normal_form_view').id
res_model = 'product.product'
res_id = self.product_id.id
return {
'name': _('Show document'),
'res_model': res_model,
'view_mode': 'form',
'res_id': res_id,
'type': 'ir.actions.act_window',
'target': 'fullscreen',
}
else:
view_id = self.env.ref('mrp_bom_costs.mrp_bom_cost_form').id
res_model = 'mrp.bom.cost'
vals = {
'name': self.product_id.name,
'bom_id': self.product_id.bom_ids[0].id,
'product_id': self.product_id.id,
'product_tmpl_id': self.product_id.product_tmpl_id.id,
'qty': self.qty,
'temporary': True,
}
res_id = self.env['mrp.bom.cost'].create(vals)
res_id.btn_fill_components()
return {
'name': _('Show document'),
'res_model': 'mrp.bom.cost',
'view_mode': 'form',
'res_id': res_id.id,
'type': 'ir.actions.act_window',
'target': 'fullscreen',
}
@api.onchange('direct_cost','indirect_cost')
def onchange_costs(self):
self.price_unit = self.direct_cost + self.indirect_cost
line_item = fields.Integer('#',compute=_compute_line_item)
cost_id = fields.Many2one('mrp.bom.cost',string='Costo')
product_id = fields.Many2one('product.product',string='Componente')
bom_id = fields.Many2one('mrp.bom',string='BoM')
qty = fields.Float('Cantidad')
price_unit = fields.Float('Costo Unitario')
standard_price = fields.Float('Costo')
direct_cost = fields.Float('Costo Directo Unitario')
indirect_cost = fields.Float('Costo Indirecto Unitario')
uom_id = fields.Many2one('uom.uom',string='Unidad de medida')
total_cost = fields.Float('Costo Total')
total_direct_cost = fields.Float('Costo Directo Total')
total_indirect_cost = fields.Float('Costo Indirecto Total')
line_type = fields.Char('Tipo Componente')
update_date = fields.Date('Fecha actualizacion',compute=_compute_update_date)
tag_ids = fields.Many2many(comodel_name='product.tag',relname='tag_item_rel',col1='item_id',col2='tag_id',string='Tags')
active = fields.Boolean('Active',default=True)
class ProductProductCost(models.Model):
_inherit = 'product.product.cost'
cost_id = fields.Many2one('mrp.bom.cost','Costo')