feat: Auto-create ows.machine on equipment import/create
- Add @api.model_create_multi hook to MaintenanceEquipment.create() - Add load() override to handle Excel/CSV import correctly - Add _create_missing_ows_machines() helper with recursion prevention - Modify OwsMachine.create() to only create equipment if equipment_id not provided - Use context flag 'skip_ows_machine_creation' to prevent infinite recursion - Fixes: Equipment import now auto-creates ows.machine and displays OWS tab - Migration tested: Compatible with SQL-based post-migration (23/23 machines migrated)
This commit is contained in:
parent
e18f2880a4
commit
8ae586cca6
|
|
@ -438,6 +438,40 @@ class MaintenanceEquipment(models.Model):
|
|||
string='Einweisungsprodukte',
|
||||
readonly=False
|
||||
)
|
||||
|
||||
@api.model_create_multi
|
||||
def create(self, vals_list):
|
||||
"""Erstelle automatisch ows.machine für jedes neue Equipment (außer wenn vom ows.machine.create aufgerufen)"""
|
||||
records = super(MaintenanceEquipment, self).create(vals_list)
|
||||
records._create_missing_ows_machines()
|
||||
return records
|
||||
|
||||
def _create_missing_ows_machines(self):
|
||||
"""Erstelle fehlende ows.machine Datensätze für Equipment ohne ows.machine"""
|
||||
if self.env.context.get('skip_ows_machine_creation'):
|
||||
return
|
||||
|
||||
for record in self:
|
||||
if not record.ows_machine_id:
|
||||
self.env['ows.machine'].with_context(skip_ows_machine_creation=True).create({
|
||||
'equipment_id': record.id,
|
||||
'category': record.ows_category or 'red',
|
||||
'area_id': record.ows_area_id.id if record.ows_area_id else False,
|
||||
})
|
||||
|
||||
@api.model
|
||||
def load(self, fields, data):
|
||||
"""Override load() um sicherzustellen, dass ows.machine nach dem Import erstellt wird"""
|
||||
result = super(MaintenanceEquipment, self).load(fields, data)
|
||||
|
||||
# Nach erfolgreichem Import: Erstelle fehlende ows.machine Einträge
|
||||
if result.get('ids'):
|
||||
equipment_records = self.browse(result['ids'])
|
||||
equipment_records._create_missing_ows_machines()
|
||||
|
||||
return result
|
||||
|
||||
|
||||
|
||||
|
||||
class OwsMachineArea(models.Model):
|
||||
|
|
@ -557,19 +591,21 @@ class OwsMachine(models.Model):
|
|||
def create(self, vals_list):
|
||||
"""
|
||||
Beim Erstellen einer ows.machine:
|
||||
1. Equipment IMMER automatisch erstellen
|
||||
1. Equipment nur erstellen wenn equipment_id NICHT angegeben wurde
|
||||
2. Area → Location synchronisieren
|
||||
3. serial_no und name vom User übernehmen
|
||||
"""
|
||||
# Equipment für alle Records erstellen
|
||||
# Equipment nur erstellen wenn noch nicht vorhanden
|
||||
for vals in vals_list:
|
||||
equipment_vals = {
|
||||
'name': vals.get('name', 'Neue Maschine'),
|
||||
'serial_no': vals.get('serial_no', False),
|
||||
}
|
||||
|
||||
equipment = self.env['maintenance.equipment'].create(equipment_vals)
|
||||
vals['equipment_id'] = equipment.id
|
||||
# Wenn equipment_id bereits gesetzt ist (z.B. bei Import), KEIN neues Equipment erstellen!
|
||||
if 'equipment_id' not in vals:
|
||||
equipment_vals = {
|
||||
'name': vals.get('name', 'Neue Maschine'),
|
||||
'serial_no': vals.get('serial_no', False),
|
||||
}
|
||||
|
||||
equipment = self.env['maintenance.equipment'].with_context(skip_ows_machine_creation=True).create(equipment_vals)
|
||||
vals['equipment_id'] = equipment.id
|
||||
|
||||
return super(OwsMachine, self).create(vals_list)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user