# Generated by Django 2.2.9 on 2020-12-04 11:18

import json

from django.db import migrations
from invoicing.models import Invoice


def string_supplier_additional_info(*args, **kwargs):
    for invoice in Invoice.objects.all().only('id', 'supplier_additional_info'):
        if isinstance(invoice.supplier_additional_info, dict):
            invoice.supplier_additional_info = json.dumps(invoice.supplier_additional_info)
            invoice.save(update_fields=['supplier_additional_info'])


def json_supplier_additional_info(*args, **kwargs):
    for invoice in Invoice.objects.all().only('id', 'supplier_additional_info'):
        if invoice.supplier_additional_info is None:
            invoice.supplier_additional_info = {}
            invoice.save(update_fields=['supplier_additional_info'])

        if isinstance(invoice.supplier_additional_info, str):
            invoice.supplier_additional_info = json.loads(invoice.supplier_additional_info)
            invoice.save(update_fields=['supplier_additional_info'])


class Migration(migrations.Migration):

    dependencies = [
        ('invoicing', '0024_auto_20200901_1241'),
    ]

    operations = [
        migrations.RunPython(json_supplier_additional_info, string_supplier_additional_info)

    ]
