Exemplo n.º 1
0
def session(request, dbtransaction, monkeypatch):
    """Mock the get_session function used for all sessions in taxcli."""
    session = get_session(connection=dbtransaction)
    monkeypatch.setattr(taxcli.helper.postgres, 'get_session', lambda *args: session)

    def teardown():
        # Explicitly remove the session so that we'll get a new session every time we go here.
        session.close()
    request.addfinalizer(teardown)

    return session
Exemplo n.º 2
0
def add_invoice_file(args):
    session = get_session()

    if not os.path.isfile(args['file']):
        print('Invalid file or file path')
        sys.exit(1)
    invoice_path = args['file']
    with open(invoice_path, "rb") as file_descriptor:
        _, extension = os.path.splitext(invoice_path)
        invoice_file = file_descriptor.read()
        invoice_file_type = extension[1:]

    alias = None
    invoice_number = None

    while not alias:
        alias = input('Alias for the invoices ("help" for a list):')
        if alias == 'help':
            contacts = session.query(Contact).all()
            for contact in contacts:
                print(contact.alias)
            alias = None
        else:
            exists = session.query(Contact).get(alias)
            if not exists:
                print("Alias doesn't exists.")
                alias = None

    while not invoice_number:
        invoice_number = input(
            'Invoice number for this alias("help" for a list):')
        if invoice_number == 'help':
            invoices = session.query(Invoice) \
                .filter(Invoice.contact_alias == alias) \
                .order_by(Invoice.date.desc()) \
                .all()
            print_invoices(invoices)
            invoice_number = None
        else:
            invoice = session.query(Invoice) \
                .filter(Invoice.contact_alias == alias) \
                .filter(Invoice.invoice_number == invoice_number) \
                .one_or_none()
            if not invoice:
                print("Alias doesn't exists.")
                alias = None

    invoice.invoice_file = invoice_file
    invoice.invoice_file_type = invoice_file_type
    session.add(invoice)
    session.commit()
Exemplo n.º 3
0
def list_invoice_data(args):
    session = get_session()

    alias = None
    invoice_number = None

    while not alias:
        alias = input('Alias for the invoices ("help" for a list):')
        if alias == 'help':
            contacts = session.query(Contact).all()
            for contact in contacts:
                print(contact.alias)
            alias = None
        else:
            exists = session.query(Contact).get(alias)
            if not exists:
                print("Alias doesn't exists.")
                alias = None

    done = False
    while not done:
        invoice_number = input('Invoice number for this alias \n'
                               '("help" for a list, leave empty for all):')
        if invoice_number == 'help':
            invoices = session.query(Invoice) \
                .filter(Invoice.contact_alias == alias) \
                .order_by(Invoice.date.desc()) \
                .all()
            print_invoices(invoices)
            invoice_number = None
        else:
            if invoice_number:
                invoices = session.query(Invoice) \
                    .filter(Invoice.contact_alias == alias) \
                    .filter(Invoice.invoice_number == invoice_number) \
                    .one_or_none()
            else:
                invoices = session.query(Invoice) \
                    .filter(Invoice.contact_alias == alias) \
                    .order_by(Invoice.date.asc()) \
                    .all()
            if not invoices:
                if invoice_number:
                    print("No such invoice_number.")
                else:
                    print("No invoices for this alias.")
                    done = False
            else:
                done = True
    print_invoices(invoices)
Exemplo n.º 4
0
def get_contact_data():
    session = get_session()

    alias = None
    name = None
    addressline1 = None
    addressline2 = None
    addressline3 = None
    city = None
    zip_or_postcode = None
    country = None

    while not alias:
        alias = input('Enter an alias for this contact:')
        exists = session.query(Contact).get(alias)
        if exists or alias == 'help':
            print('Alias already exists')
            alias = None

    while not name:
        name = input('Enter the name of this contact:')

    print('Please enter the Address of this contact:')
    while not addressline1:
        addressline1 = input("First address line:")

    addressline2 = input('Additional address information:')
    if addressline2:
        addressline3 = input('Additional address information:')
    else:
        addressline2 = None

    while not city:
        city = input('city:')

    while not zip_or_postcode:
        zip_or_postcode = input('zip or postcode:')

    while not country:
        country = input("country:")

    new_contact = Contact(alias, name, addressline1, city, zip_or_postcode,
                          country)

    new_contact.addressline2 = addressline2
    new_contact.addressline3 = addressline3

    session.add(new_contact)
    session.commit()
Exemplo n.º 5
0
def delete_invoice_data(args):
    session = get_session()

    alias = None
    invoice_number = None

    while not alias:
        alias = input('Alias for this invoice ("help" for a list):')
        if alias == 'help':
            contacts = session.query(Contact).all()
            for contact in contacts:
                print(contact.alias)
            alias = None
        else:
            exists = session.query(Contact).get(alias)
            if not exists:
                print("Alias doesn't exists.")
                alias = None

    while not invoice_number:
        invoice_number = input(
            'Invoice number for this alias("help" for a list):')
        if invoice_number == 'help':
            invoices = session.query(Invoice) \
                .filter(Invoice.contact_alias == alias) \
                .all()
            for invoice in invoices:
                print(invoice.invoice_number)
            invoice_number = None
        else:
            invoice = session.query(Invoice) \
                .filter(Invoice.contact_alias == alias) \
                .filter(Invoice.invoice_number == invoice_number) \
                .one_or_none()
            if not invoice:
                print("Alias doesn't exists.")
                alias = None

    session.delete(invoice)
    session.commit()
Exemplo n.º 6
0
def get_month(args):
    month = int(args['month'])
    year = int(args['year'])

    session = get_session()

    # Expenses
    expenses = session.query(Invoice) \
        .filter(extract('month', Invoice.date) == month) \
        .filter(extract('year', Invoice.date) == year) \
        .filter(Invoice.invoice_type == 'expense') \
        .order_by(Invoice.date.asc()) \
        .all()

    refund_tax = calculate_tax(expenses)
    get_invoice_files(expenses)

    print('Expenses: \n')
    print_invoices(expenses)

    # Income
    incomes = session.query(Invoice) \
        .filter(extract('month', Invoice.date) == month) \
        .filter(extract('year', Invoice.date) == year) \
        .filter(Invoice.invoice_type == 'income') \
        .order_by(Invoice.date.asc()) \
        .all()

    received_tax = calculate_tax(incomes)
    income_amount = calculate_netto_amount(incomes)
    get_invoice_files(incomes)

    print('Incomes: \n')
    print_invoices(incomes)

    print('\n\n')
    print('Overall income: {}'.format(income_amount))
    print('Overall sales tax to be refunded: {0:.2f}'.format(refund_tax))
    print('Overall sales tax to be payed: {0:.2f}'.format(received_tax))
Exemplo n.º 7
0
#!/bin/env python3
#
# This is a short script for deleting all tables/entries and creating a clean db_schema.
#
import os
import sys
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))

from taxcli.models import Contact, Invoice
from taxcli.helper.postgres import get_session

session = get_session()

session.commit()
Exemplo n.º 8
0
def get_invoice_data(args):
    session = get_session()

    # Get file from arguments
    if args['file']:
        if not os.path.isfile(args['file']):
            print('Invalid file or file path')
            sys.exit(1)
        invoice_path = args['file']
        with open(invoice_path, "rb") as file_descriptor:
            _, extension = os.path.splitext(invoice_path)
            invoice_file = file_descriptor.read()
            extension = extension[1:]
    else:
        invoice_file = None
        extension = None

    # New Invoice
    new_invoice = Invoice(invoice_file=invoice_file,
                          invoice_extension=extension)

    # Get an contact alias of an already existing contact
    while True:
        alias = input('Alias for this invoice ("help" for a list):')
        if alias == 'help':
            contacts = session.query(Contact).all()
            for contact in contacts:
                print(contact.alias)
            alias = None
        else:
            exists = session.query(Contact).get(alias)
            if not exists:
                print("Alias doesn't exists.")
                alias = None
            else:
                new_invoice.contact_alias = alias
                break

    # Get a invoice number
    while True:
        invoice_number = input('Invoice number:')
        if invoice_number:
            new_invoice.invoice_number = invoice_number
            break

    # Check if we already have an invoice with this number for this contact
    exists = session.query(Invoice) \
        .filter(Invoice.contact_alias == alias) \
        .filter(Invoice.invoice_number == invoice_number) \
        .one_or_none()
    if exists:
        print(
            "There already is an invoice with this number for this contact. Aborting"
        )
        sys.exit(1)

    # Get a date
    while True:
        date = input("Date ('YYYY-MM-DD'):")
        try:
            date = datetime.strptime(date, "%Y-%m-%d").date()
        except:
            print('Invalid date format')
            date = None
        else:
            new_invoice.date = date
            break

    # Get invoice type
    while True:
        invoice_type = input(
            "Invoice type (income or expense)[default: expense]:")
        invoice_type = 'expense' if invoice_type == '' else invoice_type
        if invoice_type not in ['income', 'expense']:
            print('Invalid invoice type')
            invoice_type = None
        else:
            if invoice_type == 'expense':
                new_invoice.invoice_type = InvoiceTypes.expense
                break
            else:
                new_invoice.invoice_type = InvoiceTypes.income
                break

    # Get sales tax
    while True:
        sales_tax = input("Sales tax [default: 19]:")
        sales_tax = 19 if sales_tax == '' else sales_tax
        try:
            sales_tax = int(sales_tax)
        except:
            print("Enter a valid number")
            sales_tax = None
        else:
            if sales_tax not in [0, 7, 19]:
                print("Sales tax has to be 7 or 19 percent")
            else:
                new_invoice.sales_tax = sales_tax
                break

    # Get amount
    while True:
        amount = input("Amount:")
        try:
            amount = float(amount)
        except:
            print("Not a Number.")
            amount = None
        else:
            new_invoice.amount = amount
            break

    if invoice_type == 'expense':
        netto = amount - amount * sales_tax / 100
        while True:
            acquisition_invoice = input("Acquisition invoice [true, false]:")
            if acquisition_invoice.lower() == 'false':
                acquisition_invoice = False
                break
            elif acquisition_invoice.lower() == 'true':
                acquisition_invoice = True
                break
            else:
                print("Invalid input: 'true' or 'false'")

        if acquisition_invoice:
            # The amount is higher than 1000, thereby it has to be an AfA
            if netto > 1000:
                while True:
                    afa = input("Time window for AfA (years):")
                    try:
                        afa = int(afa)
                    except:
                        print("Enter a valid number or nothing")
                        afa = None
                    else:
                        new_invoice.afa = afa
                        break

            # The amount is higher than 1000, thereby it has to be an AfA
            elif netto > 150:
                new_invoice.pooling = True
            # The amount is lower than 150. Thereby we need to check manually
            # if this is supposed to be a GwG or if this should go into pooling.
            else:
                while True:
                    gwg = input("Is this a GwG [true, false]:")
                    if gwg.lower() == 'false':
                        new_invoice.pooling = True
                        break
                    elif gwg.lower() == 'true':
                        new_invoice.gwg = True
                        break
                    else:
                        print("Invalid input: 'true' or 'false'")

    session.add(new_invoice)
    session.commit()
Exemplo n.º 9
0
def get_year(args):
    year = int(args['year'])

    session = get_session()

    # Regular expenses
    expense_invoices = session.query(Invoice) \
        .filter(extract('year', Invoice.date) == year) \
        .filter(Invoice.invoice_type == 'expense') \
        .filter(Invoice.gwg == False) \
        .filter(Invoice.afa == None) \
        .filter(Invoice.pooling == False) \
        .order_by(Invoice.date.asc()) \
        .all()  # NOQA

    # Ust.VA + overall expense calculation
    refund_tax = calculate_tax(expense_invoices)
    expense_amount = calculate_netto_amount(expense_invoices)
    get_invoice_files(expense_invoices)

    print('Expenses (Services of other companies):')
    print_invoices(expense_invoices)

    # GwG invoices
    gwg_invoices = session.query(Invoice) \
        .filter(extract('year', Invoice.date) == year) \
        .filter(Invoice.invoice_type == 'expense') \
        .filter(Invoice.gwg == True) \
        .order_by(Invoice.date.asc()) \
        .all()  # NOQA

    # Ust.VA + overall expense calculation
    refund_tax += calculate_tax(gwg_invoices)
    gwg_amount = calculate_netto_amount(gwg_invoices)
    get_invoice_files(gwg_invoices)

    print('\nGwG:')
    print_invoices(gwg_invoices)

    # Tax pool invoices
    pool_invoices = session.query(Invoice) \
        .filter(extract('year', Invoice.date) == year) \
        .filter(Invoice.invoice_type == 'expense') \
        .filter(Invoice.pooling == True) \
        .order_by(Invoice.date.asc()) \
        .all()  # NOQA

    # Ust.VA + overall expense calculation
    refund_tax += calculate_tax(pool_invoices)
    pool_amount = calculate_pool(session, 2016)
    get_invoice_files(pool_invoices)

    print('\nPool invoices:')
    print_invoices(pool_invoices)

    # AfA calculation
    afa_invoices = session.query(Invoice) \
        .filter(Invoice.afa != None) \
        .filter(Invoice.invoice_type == 'expense') \
        .filter(extract('year', Invoice.date) >= (year-Invoice.afa)) \
        .order_by(Invoice.date.asc()) \
        .all()  # NOQA

    refund_tax += calculate_tax(afa_invoices)
    afa_amount = calculate_afa(afa_invoices, year)
    get_invoice_files(afa_invoices)

    print('\nAfA:')
    print_invoices(afa_invoices)

    # Income and Ust. VA income calculation
    incomes = session.query(Invoice) \
        .filter(extract('year', Invoice.date) == year) \
        .filter(Invoice.invoice_type == 'income') \
        .order_by(Invoice.date.asc()) \
        .all()

    received_tax = calculate_tax(incomes)
    income_amount = calculate_netto_amount(incomes)
    get_invoice_files(incomes)

    # Total expenses this year
    all_expense_invoices = session.query(Invoice) \
        .filter(extract('year', Invoice.date) == year) \
        .filter(Invoice.invoice_type == 'expense') \
        .all()  # NOQA
    total_expense_amount = calculate_brutto_amount(all_expense_invoices)

    print('\nIncomes:')
    print_invoices(incomes)

    print('\n\n')
    print('Overall income: {0:.2f}'.format(income_amount))
    print('Overall expense: {0:.2f}'.format(total_expense_amount))

    print('\nExpense refunds: {0:.2f}'.format(expense_amount))
    print('Gwg refunds: {0:.2f}'.format(gwg_amount))
    print('Pool refunds: {0:.2f}'.format(pool_amount))
    print('Afa refunds: {0:.2f}'.format(afa_amount))

    print('\nOverall sales tax to be refunded: {0:.2f}'.format(refund_tax))
    print('Overall sales tax to pay: {0:.2f}'.format(received_tax))