功能刀具清单 bom添加查询排序与清单明细行删除校验

This commit is contained in:
liaodanlong
2024-09-11 10:23:02 +08:00
parent e1f7aeaa96
commit 9eee8557c2
5 changed files with 40 additions and 6 deletions

View File

@@ -1,8 +1,9 @@
# -*- coding: utf-8 -*-
from collections import Counter
from xml import etree
from odoo import models, fields, api, Command
from odoo.exceptions import UserError
from odoo.exceptions import UserError, AccessError
from odoo.http import request
@@ -28,6 +29,26 @@ class jikimo_bom(models.Model):
result.append((bom.id, '功能刀具物料清单'))
return result
def check_types_in_list(self):
# 统计每个元素的类型
type_counts = Counter(item.cutting_tool_material_id.name for item in self.product_ids)
return all(count > 0 for count in type_counts.values()) and len(type_counts) == self.options.split('+')
def write(self, vals):
# 在更新模型时记录旧的 Many2many ID 列表
if 'product_ids' in vals:
old_product_counter = Counter(self.product_ids.ids)
super(jikimo_bom, self).write(vals)
new_product_counter = Counter(self.product_ids.ids)
delete_product_counter = old_product_counter - new_product_counter
if delete_product_counter:
# 删除操作
if self.check_types_in_list():
return True
else:
raise UserError('每种物料最少要有一个')
return super(jikimo_bom, self).write(vals)
def bom_product_domains(self, assembly_options):
self.options = assembly_options
cutting_tool_materials = self.env['sf.cutting.tool.material'].search(
@@ -65,6 +86,7 @@ class jikimo_bom(models.Model):
# product = self.env['product.product'].search(domain)
# if product:
# products = products + product
domains = domains + [('stock_move_count', '>', 0)]
return domains
def generate_bill_materials(self, assembly_options):
@@ -88,6 +110,16 @@ class jikimo_bom_line(models.Model):
class ProductProduct(models.Model):
_inherit = 'product.product'
_order = 'cutting_tool_material_id, cutting_tool_type_id'
stock_move_count = fields.Integer(string='stock_move count', compute='_compute_stock_move_count', store=True)
@api.depends('stock_move_ids')
def _compute_stock_move_count(self):
for record in self:
if record.stock_move_ids:
record.stock_move_count = len(record.stock_move_ids)
else:
record.stock_move_count = 0
def search(self, args, offset=0, limit=None, order=None, count=False):
# 你可以在这里修改 `args` 以调整搜索条件