# Generated by Django 4.1.7 on 2023-03-26 13:25

from django.db import migrations
import mptt
import mptt.managers


def rebuild_ledger(apps, schema_editor):
    manager = mptt.managers.TreeManager()
    Ledger = apps.get_model('dea', 'Ledger')
    manager.model = Ledger
    mptt.register(Ledger, order_insertion_by=['name'])
    manager.contribute_to_class(Ledger, 'objects')
    manager.rebuild()

def noop_reverse(apps, schema_editor):
    pass

def insertData(apps,schema_editor):
    entity = apps.get_model('dea', 'EntityType')
    entity.objects.bulk_create([
        entity(name='Person'), entity(name='Organisation')
    ])

    transactiontype_de = apps.get_model('dea', 'TransactionType_DE')
    transactiontype_de.objects.bulk_create([
        transactiontype_de(XactTypeCode='Cr', name='Credit'),
        transactiontype_de(XactTypeCode='Dr', name='Debit')])

    transactiontype_ext = apps.get_model('dea', 'TransactionType_Ext')
    transactiontype_ext.objects.bulk_create(
        [transactiontype_ext(XactTypeCode_ext='AC', description='Adjust Credit'),
         transactiontype_ext(XactTypeCode_ext='AD',
                             description='Adjust Debit'),
         transactiontype_ext(XactTypeCode_ext='CPU',
                             description='Cash Purchase'),
         transactiontype_ext(XactTypeCode_ext='CRPU',
                             description='Credit Purchase'),
         transactiontype_ext(XactTypeCode_ext='CRSL',
                             description='Credit Sale'),
         transactiontype_ext(XactTypeCode_ext='CSL',
                             description='Cash Sale'),
         transactiontype_ext(XactTypeCode_ext='IP',
                             description='Interest Paid'),
         transactiontype_ext(XactTypeCode_ext='IR',
                             description='Interest Received'),
         transactiontype_ext(XactTypeCode_ext='LG',
                             description='Loan Given'),
         transactiontype_ext(XactTypeCode_ext='LT',
                             description='Loan Taken'),
         transactiontype_ext(XactTypeCode_ext='LR',
                             description='Loan Released'),
         transactiontype_ext(XactTypeCode_ext='LP',
                             description='Loan Paid'),
         transactiontype_ext(XactTypeCode_ext='PYT',
                             description='Payment'),
         transactiontype_ext(XactTypeCode_ext='RCT', description='Receipt')])

    acc_type = apps.get_model('dea', 'AccountType')
    acc_type.objects.bulk_create(
        [acc_type(AccountType='Asset', description='Asset'),
         acc_type(AccountType='Liability', description='Liabilities'),
         acc_type(AccountType='Equity', description='Equity'),
         acc_type(AccountType='Income', description='Income'),
         acc_type(AccountType='Expense', description='Expense')])

    acc_type_ext = apps.get_model('dea', 'AccountType_Ext')
    acc_type_ext.objects.bulk_create([
        acc_type_ext(description='Creditor', XactTypeCode_id='Cr'),
        acc_type_ext(description='Debtor', XactTypeCode_id='Dr')])

    ledger = apps.get_model('dea', 'Ledger')
    ca = ledger.objects.create(AccountType_id=1, name='Current Assets',
                        lft=0, rght=0, level=0, tree_id=0)
    ledger.objects.create(AccountType_id=1, name='Fixed Assets',
                          lft=0, rght=0, level=0, tree_id=0)
    cih = ledger.objects.create(AccountType_id=1, name='Cash In Hand', parent=ca,
                          lft=0, rght=0, level=0, tree_id=0)
    ledger.objects.create(AccountType_id = 1,name='Cash',parent = cih,
                          lft=0, rght=0, level=0, tree_id=0)
    ledger.objects.create(
        AccountType_id=1, name='Loans & Advances', parent=ca,
         lft=0, rght=0, level=0, tree_id=0)
    ledger.objects.create(
        AccountType_id=1, name='Sundry Debtors', parent=ca, 
        lft=0, rght=0, level=0, tree_id=0)
    inv = ledger.objects.create(
        AccountType_id=1, name='Inventory', parent=ca,
         lft=0, rght=0, level=0, tree_id=0)
    ledger.objects.create(AccountType_id=1, name='GST INV',
                          parent=inv, lft=0, rght=0, level=0, tree_id=0)
    ledger.objects.create(AccountType_id=1, name='Non-GST INV',
                          parent=inv, lft=0, rght=0, level=0, tree_id=0)
    ledger.objects.create(
        AccountType_id=1, name='Interest Receivables', parent=ca,
         lft=0, rght=0, level=0, tree_id=0)

    cl = ledger.objects.create(
        AccountType_id=2, name='Current Liabilities',
         lft=0, rght=0, level=0, tree_id=0)
    ledger.objects.create(
        AccountType_id=2, name='Loans',parent=cl,
                           lft=0, rght=0, level=0, tree_id=0)
    ledger.objects.create(
        AccountType_id=2, name='Interest Payable', parent=cl, lft=0, rght=0, level=0, tree_id=0)
    ledger.objects.create(
        AccountType_id=2, name='Sundry Creditors', parent=cl, lft=0, rght=0, level=0, tree_id=0)
    dt = ledger.objects.create(
        AccountType_id=2, name='Duties & Taxes', parent=cl, lft=0, rght=0, level=0, tree_id=0)
    cgst = ledger.objects.create(
        AccountType_id=2, name='CGST', parent=dt, lft=0, rght=0, level=0, tree_id=0)
    sgst = ledger.objects.create(
        AccountType_id=2, name='SGST', parent=dt, lft=0, rght=0, level=0, tree_id=0)
    igst = ledger.objects.create(
        AccountType_id=2, name='IGST', parent=dt, lft=0, rght=0, level=0, tree_id=0)
    ledger.objects.create(AccountType_id=2, name='Input CGST',
                          parent=cgst, lft=0, rght=0, level=0, tree_id=0)
    ledger.objects.create(
        AccountType_id=2, name='Output CGST', parent=cgst, lft=0, rght=0, level=0, tree_id=0)
    ledger.objects.create(AccountType_id=2, name='Input SGST',
                          parent=sgst, lft=0, rght=0, level=0, tree_id=0)
    ledger.objects.create(
        AccountType_id=2, name='Output SGST', parent=sgst, lft=0, rght=0, level=0, tree_id=0)
    ledger.objects.create(AccountType_id=2, name='Input IGST',
                          parent=igst, lft=0, rght=0, level=0, tree_id=0)
    ledger.objects.create(
        AccountType_id=2, name='Output IGST', parent=igst, lft=0, rght=0, level=0, tree_id=0)
    ledger.objects.create(
        AccountType_id=2, name='Tax Liability', parent=cl, lft=0, rght=0, level=0, tree_id=0)
    ledger.objects.create(AccountType_id=2, name='Capital',
                          parent=cl, lft=0, rght=0, level=0, tree_id=0)
    ledger.objects.create(AccountType_id=2, name='Capital A/c', parent=cl,lft=0, rght=0, level=0, tree_id=0)

    ledger.objects.create(AccountType_id=4, name='Interest Received',lft=0, rght=0, level=0, tree_id=0)
    ledger.objects.create(AccountType_id=4, name='Sales',
                          lft=0, rght=0, level=0, tree_id=0)

    ledger.objects.create(AccountType_id=5, name='Interest Paid',lft=0, rght=0, level=0, tree_id=0)
    ledger.objects.create(AccountType_id=5, name='Purchase',lft=0, rght=0, level=0, tree_id=0)
    cogs = ledger.objects.create(
        AccountType_id=5, name='COGS', lft=0, rght=0, level=0, tree_id=0)
    ledger.objects.create(AccountType_id=5, name='GST COGS',
                          parent=cogs, lft=0, rght=0, level=0, tree_id=0)
    ledger.objects.create(
        AccountType_id=5, name='Non-GST COGS', parent=cogs, lft=0, rght=0, level=0, tree_id=0)


class Migration(migrations.Migration):

    dependencies = [
        ('dea', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(insertData),
        migrations.RunPython(rebuild_ledger,noop_reverse)
    ]