# -*- coding: utf-8 -*-
# Generated by Django 1.11.9 on 2018-03-21 05:31
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('aims', '0057_organisationbudgetsusd'),
    ]

    operations = [
        migrations.CreateModel(
            name='OrganisationActivityBudgetsUsd',
            fields=[
                ('code', models.CharField(max_length=80, primary_key=True, serialize=False)),
                ('act_id', models.ForeignKey('aims.Activity', related_name='budget_set_usd', null=True, blank=True, on_delete=models.DO_NOTHING)),
                ('year', models.CharField(max_length=4)),
                ('quarter', models.CharField(max_length=1)),
                ('currency', models.CharField(max_length=3)),
                ('value', models.DecimalField(decimal_places=2, max_digits=15)),
                ('usd_value', models.DecimalField(decimal_places=2, max_digits=15)),
            ],
            options={
                'managed': False,
                'db_table': 'aims_organisation_budgets_usd',
            },
        ),

        # View to hold Organisation / Activity Budgets by year & quarter, converted to USD
        migrations.RunSQL("""
            DROP VIEW IF EXISTS aims_organisation_budgets_usd;

            CREATE VIEW aims_organisation_budgets_usd AS (
                WITH budget_usd AS (
                    SELECT aims_organisation.code as code,
                        aims_activity.id as act_id,
                        aims_budget.id as budget_id, aims_budget.currency_id as curr_id,
                        DATE_PART('year', aims_budget.period_start) as year,
                        EXTRACT(QUARTER from date_trunc('quarter', aims_budget.period_start)) as quarter,
                        aims_budget.currency_id as base_currency,
                        SUM(aims_budget.value) as tot_base_value,
                        COALESCE(aims_currency_exchange_rate.rate, 1.00000000) as exch_rate_usd,
                        ROUND(COALESCE((SUM(aims_budget.value) * aims_currency_exchange_rate.rate), SUM(aims_budget.value)), 2) as tot_usd_value
                    FROM aims_organisation
                    JOIN aims_activity ON (aims_activity.reporting_organisation_id = aims_organisation.code)
                    JOIN aims_budget ON (aims_activity.id = aims_budget.activity_id)
                    LEFT JOIN aims_currency_exchange_rate ON (aims_budget.value_date = aims_currency_exchange_rate.date AND aims_budget.currency_id = aims_currency_exchange_rate.base_currency_id)
                    GROUP BY code, aims_activity.id, aims_budget.id, aims_budget.period_start, aims_budget.currency_id, aims_currency_exchange_rate.rate
                )
                SELECT  code,
                        act_id,
                        to_char(year, '9999') as year,
                        to_char(quarter, '9') as quarter,
                        base_currency,
                        sum(tot_base_value) as value,
                        sum(tot_usd_value) as usd_value
                FROM budget_usd
                GROUP BY code, act_id, year, quarter, base_currency
                ORDER BY year, quarter
            );
        """),
    ]
