from trytond.model import fields
from trytond.pool import Pool, PoolMeta
from trytond.pyson import Bool, Eval, Id
from trytond.model import (ModelSQL, ModelView)
from trytond.tools import is_full_text, lstrip_wildcard
from trytond.transaction import Transaction, inactive_records
from decimal import getcontext, Decimal, ROUND_HALF_UP
from sql.aggregate import Count, Max, Min, Sum, Avg, BoolOr
from sql.conditionals import Case
from sql import Column, Literal
from sql.functions import CurrentTimestamp, DateTrunc
from trytond.wizard import Button, StateTransition, StateView, Wizard
import datetime
import logging

class AnalyticDimension(ModelSQL, ModelView):
    'Analytic Dimension'
    __name__ = 'analytic.dimension'

    name = fields.Char('Name', required=True)
    code = fields.Char('Code', required=True)
    active = fields.Boolean('Active')

class AnalyticDimensionValue(ModelSQL, ModelView):
    'Analytic Dimension Value'
    __name__ = 'analytic.dimension.value'

    dimension = fields.Many2One(
        'analytic.dimension',
        'Dimension',
        required=True,
        ondelete='CASCADE'
    )

    name = fields.Char('Name', required=True)
    code = fields.Char('Code')

    parent = fields.Many2One(
        'analytic.dimension.value',
        'Parent',
        domain=[
            ('dimension', '=', Eval('dimension')),
        ],
        depends=['dimension']
    )

    children = fields.One2Many(
        'analytic.dimension.value',
        'parent',
        'Children'
    )

    active = fields.Boolean('Active')

class AnalyticDimensionAssignment(ModelSQL, ModelView):
    'Analytic Dimension Assignment'
    __name__ = 'analytic.dimension.assignment'

    dimension = fields.Many2One(
        'analytic.dimension',
        'Dimension',
        required=True
    )

    value = fields.Many2One(
        'analytic.dimension.value',
        'Value',
        domain=[
            ('dimension', '=', Eval('dimension')),
        ],
        depends=['dimension']
    )

    purchase = fields.Many2One(
        'purchase.purchase',
        'Purchase',
        ondelete='CASCADE'
    )
