122 lines
4.6 KiB
Python
122 lines
4.6 KiB
Python
# -*- coding: utf-8 -*-
|
||
from odoo import models, fields
|
||
|
||
|
||
class ResProductCategory(models.Model):
|
||
_inherit = "product.category"
|
||
|
||
type = fields.Selection(
|
||
[("成品", "成品"), ("坯料", "坯料"), ("原材料", "原材料"), ("表面工艺", "表面工艺"), ("刀具", "刀具"),
|
||
("夹具", "夹具"), ("功能刀具", "功能刀具")],
|
||
default="", string="类型")
|
||
|
||
|
||
class ResProductProduct(models.Model):
|
||
_inherit = 'product.product'
|
||
|
||
single_manufacturing = fields.Boolean(string="单个制造")
|
||
is_bfm = fields.Boolean('业务平台是否自动创建', default=False)
|
||
|
||
|
||
class ResProducTemplate(models.Model):
|
||
_inherit = 'product.template'
|
||
|
||
single_manufacturing = fields.Boolean(string="单个制造")
|
||
|
||
|
||
class ResMrpBomMo(models.Model):
|
||
_inherit = 'mrp.bom'
|
||
|
||
subcontractor_id = fields.Many2one('res.partner', string='外包商')
|
||
|
||
def bom_create_line_has(self, embryo):
|
||
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)
|
||
|
||
# 业务平台分配工厂后在智能工厂先创建销售订单再创建该产品后再次进行创建bom
|
||
def bom_create(self, product, bom_type, product_type):
|
||
bom_id = self.env['mrp.bom'].create({
|
||
'product_tmpl_id': product.product_tmpl_id.id,
|
||
'type': bom_type,
|
||
# 'subcontractor_id': '' or subcontract.partner_id.id,
|
||
'product_qty': 1,
|
||
'product_uom_id': 1
|
||
})
|
||
if bom_type == 'subcontract' and product_type is not False:
|
||
subcontract = self.get_supplier(product.materials_type_id)
|
||
bom_id.subcontractor_id = subcontract.partner_id.id
|
||
return bom_id
|
||
|
||
# 坯料BOM组件:选取当前坯料原材料,
|
||
# 然后根据当前的坯料的体积得出需要的原材料重量(立方米m³) *材料密度 * 1000 = 所需原材料重量KG(公斤)
|
||
# 坯料所需原材料公式:当前的坯料的体积(立方米m³) *材料密度 * 1000 = 所需原材料重量KG(公斤)
|
||
|
||
def bom_create_line(self, embryo):
|
||
# 选取当前坯料原材料
|
||
raw_bom_line = self.get_raw_bom(embryo)
|
||
if raw_bom_line:
|
||
qty = 1
|
||
if round(embryo.volume * raw_bom_line.materials_type_id.density / 1000000) > 1:
|
||
qty = round(embryo.volume * raw_bom_line.materials_type_id.density / 1000000)
|
||
bom_line = self.env['mrp.bom.line'].create({
|
||
'bom_id': self.id,
|
||
'product_id': raw_bom_line.id,
|
||
'product_tmpl_id': raw_bom_line.product_tmpl_id.id,
|
||
'product_qty': qty,
|
||
'product_uom_id': raw_bom_line.uom_id.id,
|
||
})
|
||
return bom_line
|
||
else:
|
||
return False
|
||
|
||
# 查询材料型号默认排第一的供应商
|
||
|
||
def get_supplier(self, materials_type):
|
||
seller_id = self.env['sf.supplier.sort'].search(
|
||
[('materials_model_id', '=', materials_type.id)],
|
||
limit=1,
|
||
order='sequence asc')
|
||
return seller_id
|
||
|
||
# 匹配bom
|
||
|
||
def get_bom(self, product):
|
||
embryo_has = self.env['product.product'].search(
|
||
[('categ_id.type', '=', '坯料'), ('materials_type_id', '=', product.materials_type_id.id),
|
||
('length', '>', product.length), ('width', '>', product.width),
|
||
('height', '>', product.height), ('is_bfm', '=', False)
|
||
],
|
||
limit=1,
|
||
order='volume desc'
|
||
)
|
||
logging.info('get_bom-vals:%s' % embryo_has)
|
||
if embryo_has:
|
||
rate_of_waste = ((embryo_has.volume - product.model_volume) % embryo_has.volume) * 100
|
||
if rate_of_waste <= 20:
|
||
return embryo_has
|
||
else:
|
||
return
|
||
|
||
# 查bom的原材料
|
||
def get_raw_bom(self, product):
|
||
raw_bom = self.env['product.product'].search(
|
||
[('categ_id.type', '=', '原材料'), ('materials_type_id', '=', product.materials_type_id.id)])
|
||
return raw_bom
|
||
|
||
|
||
class ResSupplierInfo(models.Model):
|
||
_inherit = 'product.supplierinfo'
|
||
|
||
def _compute_is_subcontractor(self):
|
||
for supplier in self:
|
||
boms = supplier.product_id.variant_bom_ids
|
||
boms |= supplier.product_tmpl_id.bom_ids.filtered(lambda b: not b.product_id or b.product_id in (
|
||
supplier.product_id or supplier.product_tmpl_id.product_variant_ids))
|
||
supplier.is_subcontractor = supplier.partner_id in boms.subcontractor_id
|