# -*- coding: utf-8 -*- import logging import re from odoo import models, fields, api 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='外包商') subcontractor_name = fields.Char('', compute='_compute_subcontractor_ids', store=True) @api.depends('subcontractor_id') def _compute_subcontractor_ids(self): for item in self: if item.subcontractor_id: logging.info("subcontractor_ids: %s" % item.subcontractor_ids.ids) subcontractor_id = int(re.sub(r"\D", "", str(item.subcontractor_id.id))) item.subcontractor_ids = [subcontractor_id] item.subcontractor_name = item.subcontractor_id.name logging.info("subcontractor_ids: %s" % item.subcontractor_ids.ids) else: item.subcontractor_ids = [] item.subcontractor_name = '' 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'].sudo().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'].sudo().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' ) 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 None # 查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)],limit=1) 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