124 lines
5.6 KiB
Python
124 lines
5.6 KiB
Python
from odoo import models, fields, api
|
||
from odoo.exceptions import ValidationError
|
||
|
||
|
||
class ResProductTemplate(models.Model):
|
||
_inherit = 'product.template'
|
||
|
||
# 模型的长,宽,高,体积,精度,材料
|
||
model_long = fields.Float('模型长[mm]', digits=(16, 3))
|
||
model_width = fields.Float('模型宽[mm]', digits=(16, 3))
|
||
model_height = fields.Float('模型高[mm]', digits=(16, 3))
|
||
model_volume = fields.Float('模型体积[mm³]', digits=(16, 3))
|
||
model_precision = fields.Float('精度要求', digits=(16, 3))
|
||
model_type_id = fields.Many2one('sf.model.type', string='模型类型')
|
||
model_processing_panel = fields.Char('模型加工面板')
|
||
model_surface_process_id = fields.Many2one('sf.production.process', string='表面工艺')
|
||
model_process_parameters_id = fields.Many2one('sf.processing.technology', string='工艺参数')
|
||
model_price = fields.Float('模型单价', digits=(16, 3))
|
||
model_total_amount = fields.Float('模型金额', digits=(16, 3))
|
||
model_number = fields.Integer('模型数量', default=1)
|
||
model_remark = fields.Char('模型备注说明')
|
||
long = fields.Float('长[mm]', digits=(16, 3), onchange='add_product_size')
|
||
width = fields.Float('宽[mm]', digits=(16, 3), onchange='add_product_size')
|
||
height = fields.Float('高[mm]', digits=(16, 3), onchange='add_product_size')
|
||
materials_id = fields.Many2one('sf.production.materials', string='材料')
|
||
materials_type_id = fields.Many2one('sf.materials.model', string='材料型号')
|
||
volume = fields.Float(compute='_compute_volume', store=True)
|
||
single_manufacturing = fields.Boolean(string="单个制造")
|
||
|
||
@api.depends('long', 'width', 'height')
|
||
def _compute_volume(self):
|
||
self.volume = self.long * self.width * self.height
|
||
|
||
# 业务平台分配工厂后在智能工厂先创建销售订单再创建该产品
|
||
def product_create(self, product_id, item, order_id, order_number, i):
|
||
copy_product_id = product_id.with_user(self.env.ref("base.user_admin")).copy()
|
||
copy_product_id.product_tmpl_id.active = True
|
||
vals = {
|
||
'name': '%s-%s' % (order_id.name, i),
|
||
'model_long': item['model_long'],
|
||
'model_width': item['model_width'],
|
||
'model_height': item['model_height'],
|
||
'model_volume': item['model_volume'],
|
||
'model_price': item['price'],
|
||
'model_total_amount': item['total_amount'],
|
||
'model_number': item['number'],
|
||
'list_price': item['price'],
|
||
'materials_id': self.env['sf.production.materials'].search(
|
||
[('materials_no', '=', item['texture_code'])]).id,
|
||
'materials_type_id': self.env['sf.materials.model'].search(
|
||
[('materials_no', '=', item['texture_type_code'])]).id,
|
||
# 'model_surface_process_id': self.env['sf.production.process'].search(
|
||
# [('process_encode', '=', item['surface_process_code'])]).id,
|
||
# 'model_process_parameters_id': self.env['sf.processing.technology'].search(
|
||
# [('process_encode', '=', item['process_parameters_code'])]).id,
|
||
'model_remark': item['remark'],
|
||
'default_code': '%s-%s' % (order_number, i),
|
||
'barcode': item['barcode'],
|
||
'active': True
|
||
}
|
||
copy_product_id.sudo().write(vals)
|
||
return copy_product_id
|
||
|
||
# 根据模型类型默认给模型的长高宽加配置的长度;
|
||
@api.onchange('model_type_id')
|
||
def add_product_size(self):
|
||
if not self.model_type_id:
|
||
return
|
||
model_type = self.env['sf.model.type'].search(
|
||
[('id', '=', self.model_type_id.id)])
|
||
print(self.model_long)
|
||
print(self.model_width)
|
||
print(self.model_height)
|
||
for item in self:
|
||
print(item.model_long)
|
||
print(item.model_width)
|
||
print(item.model_height)
|
||
item.model_long = item.model_long + model_type.embryo_tolerance
|
||
item.model_width = item.model_width + model_type.embryo_tolerance
|
||
item.model_height = item.model_width + model_type.embryo_tolerance
|
||
|
||
|
||
class ResMrpBom(models.Model):
|
||
_inherit = 'mrp.bom'
|
||
|
||
# 业务平台分配工厂后在智能工厂先创建销售订单再创建该产品后再次进行创建bom
|
||
def bom_create(self, product):
|
||
bom_id = self.env['mrp.bom'].create({
|
||
'product_tmpl_id': product.product_tmpl_id.id,
|
||
'type': 'normal',
|
||
'product_qty': 1,
|
||
'product_uom_id': 1
|
||
})
|
||
return bom_id
|
||
|
||
# 生成产品BOM匹配胚料,胚料的匹配规则:
|
||
# 一、匹配的胚料类别需要带有胚料的标签;
|
||
# 二、胚料的材料型号与生成产品的材料型号一致;
|
||
# 三、胚料的长宽高均要大于模型的长宽高;
|
||
# 四、如果匹配成功多个胚料,则选取体积最小的胚料;
|
||
def bom_create_Line(self, product):
|
||
embryo = self.env['product.product'].search(
|
||
[('categ_id.is_embryo', '=', True), ('materials_type_id', '=', product.materials_type_id.id),
|
||
('long', '>', product.long), ('width', '>', product.width),
|
||
('height', '>', product.height)
|
||
],
|
||
limit=1,
|
||
order='volume desc'
|
||
)
|
||
vals = {
|
||
'bom_id': self.id,
|
||
'product_id': embryo.id,
|
||
'product_tmpl_id': embryo.product_tmpl_id.id,
|
||
'product_qty': 1,
|
||
'product_uom_id': 1
|
||
}
|
||
return self.env['mrp.bom.line'].create(vals)
|
||
|
||
|
||
class ResProductCategory(models.Model):
|
||
_inherit = "product.category"
|
||
|
||
is_embryo = fields.Boolean('胚料')
|