# Generated by Django 5.2.9 on 2026-02-12 08:26

import django.db.models.deletion
from django.db import migrations, models
from wbcrm.models import Product as CRMProduct

def migrate_accounting_product(apps, schema_editor):
    Product = apps.get_model('wbportfolio', 'Product')
    Instrument  = apps.get_model('wbfdm', 'Instrument')
    for product in Product.objects.all():
        if not product.accounting_product:
            instrument = Instrument.objects.get(pk=product.instrument_ptr_id)
            product.accounting_product_id = CRMProduct.objects.get_or_create_internal_product(f"{instrument.name} {instrument.isin}")[0].id
            product.save()


class Migration(migrations.Migration):

    dependencies = [
        ('wbcrm', '0020_alter_product_unique_together_product_unique_product'),
        ('wbportfolio', '0098_claim_created_claim_updated'),
    ]

    operations = [
        migrations.AddField(
            model_name='product',
            name='accounting_product',
            field=models.OneToOneField(default=None, blank=True, null=True, help_text='The counterpart CRM product this pms Product belongs to.', on_delete=django.db.models.deletion.PROTECT, to='wbcrm.product', verbose_name='Accounting Product'),
            preserve_default=False,
        ),
        migrations.RunPython(migrate_accounting_product),
        migrations.AlterField(
            model_name='product',
            name='accounting_product',
            field=models.OneToOneField(help_text='The counterpart CRM product this pms Product belongs to.', on_delete=django.db.models.deletion.PROTECT, to='wbcrm.product', verbose_name='Accounting Product'),
        ),
        migrations.AddField(
            model_name='orderproposal',
            name='execution_kwargs',
            field=models.JSONField(default=dict),
        ),
    ]
