# -*- coding: UTF-8 -*-

# Copyright (c) 2007 Daniele Favara <nomed@dsslive.org>.
#
# This is free software you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any
# later version.
#
# This software is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this software; see the file COPYING.  If not, write to the Free
# Software Foundation, Inc., 51 Franilin St, Fifth Floor, Boston, MA


import tables

from sqlalchemy.sql import and_, or_, not_

def get_tableobj(tablename):
    if tablename == 'visit':
        return getattr(tables, 'Visit')
    if tablename == 'biller':
        return getattr(tables, 'Biller')
    tablename=tablename.split('_')
    ret = ''
    for t in tablename:
        ret +=t.capitalize()
    return getattr(tables, ret)

def get_biller(id):
    ret = None
    if id:
        query = tables.session.query(tables.Biller)
        query_filter = query.filter(tables.biller.c.user_id==id)
        ret = query_filter.all()
    if len(ret) == 1:
        return ret[0]
    else:
        return None

def get_customer(id):
    ret = None
    if id:
        query = tables.session.query(tables.Customer)
        query_filter = query.filter(tables.customer.c.customer_id==id)
        ret =  query_filter.one()
    return ret

def get_invoice(id):
    ret = None
    if id:
        query = tables.session.query(tables.Invoice)
        query_filter = query.filter(tables.invoice.c.invoice_id==id)
        ret =  query_filter.one()
    return ret

def get_proforma(id):
    ret = None
    if id:
        query = tables.session.query(tables.Proforma)
        query_filter = query.filter(tables.proforma.c.invoice_id==id)
        ret =  query_filter.one()
    return ret

def get_product(id):
    ret = None
    if id:
        query = tables.session.query(tables.Product)
        query_filter = query.filter(tables.product.c.product_id==id)
        ret =  query_filter.one()
    return ret

def get_project(id):
    ret = None
    if id:
        query = tables.session.query(tables.Project)
        query_filter = query.filter(tables.project.c.project_id==id)
        ret =  query_filter.one()
    return ret

def get_milestone(id):
    ret = None
    if id:
        query = tables.session.query(tables.Milestone)
        query_filter = query.filter(tables.milestone.c.milestone_id==id)
        ret =  query_filter.one()
    return ret

def get_customers(id):
    ret = []
    if id:
        query = tables.session.query(tables.Customer)
        query_filter = query.filter(tables.customer.c.user_id==id)
        ret =  query_filter.all()
    return ret

def get_invoices(id):
    ret = []
    if id:
        biller = get_biller(id)
        if biller:
            query = tables.session.query(tables.Invoice)
            query_filter = query.filter(tables.invoice.c.biller_id==biller.biller_id)
            ret =  query_filter.all()
    return ret

def get_proformas(id):
    ret = []
    if id:
        biller = get_biller(id)
        if biller:
            query = tables.session.query(tables.Proforma)
            query_filter = query.filter(tables.proforma.c.biller_id==biller.biller_id)
            ret =  query_filter.all()
    return ret

def get_projects(id):
    ret = []
    if id:
        query = tables.session.query(tables.Project)
        query_filter = query.filter(tables.Project.c.user_id==id)
        ret =  query_filter.all()
    return ret

def get_project_unique(name, user_id, customer_id):
    query = tables.session.query(tables.Project)
    query_filter = query.filter(and_(
                               tables.project.c.name==name,
                               tables.project.c.user_id==user_id,
                               tables.project.c.customer_id==customer_id
                               )
                               )
    ret =  query_filter.all()
    if len(ret) > 0:
        return ret[0]
    else:
        return None