diff --git a/yuthon_maintain/models/inherit_maintenance_equipment.py b/yuthon_maintain/models/inherit_maintenance_equipment.py
index cd72e0355..2174d7fbc 100644
--- a/yuthon_maintain/models/inherit_maintenance_equipment.py
+++ b/yuthon_maintain/models/inherit_maintenance_equipment.py
@@ -244,6 +244,47 @@ class InheritMaintenanceEquipment(models.Model):
'target': 'self',
}
+ @api.model
+ def action_sync_fixed_assets(self):
+ """一次性补录:将历史已入库的固定资产产品同步到 maintenance.equipment"""
+ from odoo.exceptions import UserError
+ if not self.env.user.has_group('base.group_system'):
+ raise UserError('只有系统管理员才能执行此操作!')
+ pickings = self.env['stock.picking'].search([
+ ('receipt_status', '=', 'receipt'),
+ ('picking_type_code', '=', 'incoming'),
+ ])
+ created_count = 0
+ for picking in pickings:
+ for move in picking.move_ids_without_package:
+ product = move.product_id
+ if product and '固定资产' in product.product_tag_ids.mapped('name'):
+ qty = int(move.quantity or move.product_uom_qty or 1)
+ base_name = product.name
+ for _ in range(qty):
+ existing_count = self.env['maintenance.equipment'].search_count([
+ '|',
+ ('name', '=', base_name),
+ ('name', 'like', base_name + '-'),
+ ])
+ new_name = f'{base_name}-{existing_count + 1}'
+ self.env['maintenance.equipment'].create({
+ 'name': new_name,
+ 'mainten_state': 'ing',
+ 'assign_date': False,
+ })
+ created_count += 1
+ return {
+ 'type': 'ir.actions.client',
+ 'tag': 'display_notification',
+ 'params': {
+ 'title': '同步完成',
+ 'message': f'已成功创建 {created_count} 条固定资产设备记录',
+ 'type': 'success',
+ 'sticky': False,
+ }
+ }
+
class MaintenanceEquipmentLine(models.Model):
_name = 'maintenance.equipment.line'
diff --git a/yuthon_maintain/views/maintenance_equipment_views.xml b/yuthon_maintain/views/maintenance_equipment_views.xml
index 84c373bba..f10362509 100644
--- a/yuthon_maintain/views/maintenance_equipment_views.xml
+++ b/yuthon_maintain/views/maintenance_equipment_views.xml
@@ -195,4 +195,14 @@
code
action = model.export_inventory_report()
+
+
+ 同步历史固定资产
+
+
+ list
+
+ code
+ action = model.action_sync_fixed_assets()
+
\ No newline at end of file
diff --git a/yuthon_stock/models/inherit_stock_picking.py b/yuthon_stock/models/inherit_stock_picking.py
index 0baebbf65..3f66a913b 100644
--- a/yuthon_stock/models/inherit_stock_picking.py
+++ b/yuthon_stock/models/inherit_stock_picking.py
@@ -249,6 +249,25 @@ class StockPicking(models.Model):
'certification_date': fields.Datetime.now(),
'receipt_status': 'receipt' # 状态变为入库
})
+ # 自动为标签含"固定资产"的产品创建 maintenance.equipment 记录
+ for move in record.move_ids_without_package:
+ product = move.product_id
+ if product and '固定资产' in product.product_tag_ids.mapped('name'):
+ qty = int(move.quantity or move.product_uom_qty or 1)
+ base_name = product.name
+ for _ in range(qty):
+ # 查询已存在的同名设备数量(包含 base_name 和 base_name-N)
+ existing_count = self.env['maintenance.equipment'].search_count([
+ '|',
+ ('name', '=', base_name),
+ ('name', 'like', base_name + '-'),
+ ])
+ new_name = f'{base_name}-{existing_count + 1}'
+ self.env['maintenance.equipment'].create({
+ 'name': new_name,
+ 'mainten_state': 'ing',
+ 'assign_date': False,
+ })
return super(StockPicking, self).button_validate()
def action_refresh_json_display(self):