52 lines
2.1 KiB
Python
52 lines
2.1 KiB
Python
# Generated by Django on 2026-10-13
|
|||
|
|
|
||
|
|
from django.db import migrations
|
||
|
|
|
||
|
|
def sync_unit_codes(apps, schema_editor):
|
||
|
|
Asset = apps.get_model('assets', 'Asset')
|
||
|
|
AssetUnit = apps.get_model('assets', 'AssetUnit')
|
||
|
|
UnitLog = apps.get_model('assets', 'UnitLog')
|
||
|
|
|
||
|
|
for asset in Asset.objects.all():
|
||
|
|
for unit in asset.units.all():
|
||
|
|
if not unit.unit_code.startswith(f"{asset.asset_code}-"):
|
||
|
|
if '-' in unit.unit_code:
|
||
|
|
suffix = unit.unit_code.rsplit('-', 1)[-1]
|
||
|
|
else:
|
||
|
|
suffix = unit.unit_code
|
||
|
|
|
||
|
|
target_code = f"{asset.asset_code}-{suffix}"
|
||
|
|
|
||
|
|
# Pastikan tidak bentrok dengan unit_code yang sudah ada di database
|
||
|
|
if AssetUnit.objects.filter(unit_code=target_code).exclude(pk=unit.pk).exists():
|
||
|
|
counter = 1
|
||
|
|
try:
|
||
|
|
base_num = int(suffix)
|
||
|
|
while AssetUnit.objects.filter(unit_code=target_code).exclude(pk=unit.pk).exists():
|
||
|
|
target_code = f"{asset.asset_code}-{base_num + counter:03d}"
|
||
|
|
counter += 1
|
||
|
|
except ValueError:
|
||
|
|
while AssetUnit.objects.filter(unit_code=target_code).exclude(pk=unit.pk).exists():
|
||
|
|
target_code = f"{asset.asset_code}-{suffix}-{counter}"
|
||
|
|
counter += 1
|
||
|
|
|
||
|
|
if unit.unit_code != target_code:
|
||
|
|
old_unit_code = unit.unit_code
|
||
|
|
unit.unit_code = target_code
|
||
|
|
unit.save(update_fields=['unit_code'])
|
||
|
|
UnitLog.objects.create(
|
||
|
|
unit=unit,
|
||
|
|
action='other',
|
||
|
|
notes=f"Sinkronisasi kode unit otomatis dari {old_unit_code} ke {target_code} mengikuti kode aset induk."
|
||
|
|
)
|
||
|
|
|
||
|
|
class Migration(migrations.Migration):
|
||
|
|
|
||
|
|
dependencies = [
|
||
|
|
('assets', '0015_assetassignment_created_at'),
|
||
|
|
]
|
||
|
|
|
||
|
|
operations = [
|
||
|
|
migrations.RunPython(sync_unit_codes, reverse_code=migrations.RunPython.noop),
|
||
|
|
]
|