采购入库分配

This commit is contained in:
李鹏宇
2026-04-20 17:08:46 +08:00
parent 8059ee7193
commit bcb83e183b
3 changed files with 70 additions and 0 deletions
@@ -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'
@@ -195,4 +195,14 @@
<field name="state">code</field>
<field name="code">action = model.export_inventory_report()</field>
</record>
<record id="sync_fixed_assets_action" model="ir.actions.server">
<field name="name">同步历史固定资产</field>
<field name="model_id" ref="maintenance.model_maintenance_equipment"/>
<field name="binding_model_id" ref="maintenance.model_maintenance_equipment"/>
<field name="binding_view_types">list</field>
<field name="groups_id" eval="[(4, ref('base.group_system'))]"/>
<field name="state">code</field>
<field name="code">action = model.action_sync_fixed_assets()</field>
</record>
</odoo>
@@ -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):