def __call__(self):

        total_income = Decimal(0.0)
        total_taxes = Decimal(0.0)

        for account_name in self.income_accounts:
            account = get_account(account_name)

            for split in get_splits(account, self._start.date, self._end.date):
                value = get_decimal(split.GetAmount()) * -1
                total_income += value

        for account_name in self.tax_accounts:
            account = get_account(account_name)
            for split in get_splits(account,  self._start.date, self._end.date):
                value = get_decimal(split.GetAmount())
                total_taxes += value

        tax_value = calculate_tax(self._tax_name, self._tax_status, total_income)

        result = self._generate_result()
        result['data']['income'] = total_income
        result['data']['tax_value'] = tax_value
        result['data']['taxes_paid'] = total_taxes

        return result
def store_investment(bucket, value):
    other_account_name = get_corr_account_full_name(value)
    other_account = get_account(other_account_name)

    account_type = AccountTypes(other_account.GetType())

    # Find the correct amount that was paid from the account into this account.
    change_amount = get_decimal(value.GetValue())

    if change_amount > 0:
        # Need to get the value from the corr account split.
        for parent_splits in value.parent.GetSplitList():
            if parent_splits.GetAccount().get_full_name() == other_account_name:
                change_amount = -get_decimal(parent_splits.GetValue())

    if account_type == AccountTypes.mutual_fund or account_type == AccountTypes.asset or \
       account_type == AccountTypes.equity:
        # Asset or mutual fund transfer
        bucket['money_in'] += change_amount
    elif account_type == AccountTypes.income:
        bucket['income'] += get_decimal(value.GetValue())
    elif account_type == AccountTypes.expense:
        bucket['expense'] += get_decimal(value.GetValue())
    else:
        print 'Unknown account type: %s' % account_type

    return bucket
    def __call__(self):

        total_balance = Decimal('0.0')
        currency = get_currency()

        for account_name in self.accounts:
            multiplier = Decimal('1.0')
            if isinstance(account_name, basestring):
                account = get_account(account_name)
            else:
                account = get_account(account_name[0])
                multiplier = Decimal(account_name[1])

            balance = get_balance_on_date(account, self.as_of.date, currency)
            total_balance += (balance * multiplier)

        payload = self._generate_result()
        payload['data']['balance'] = total_balance
        payload['data']['goal'] = self.goal_amount

        return payload
    def __call__(self):
        account = get_account(self.account)

        balance = get_balance_on_date(account, self.when.date)

        payload = self._generate_result()
        payload["data"]["balance"] = balance
        payload["data"]["good_value"] = self.good_value
        payload["data"]["warn_value"] = self.warn_value
        payload["data"]["error_value"] = self.error_value

        return payload
    def __call__(self):

        account = get_account(self.account_name)

        todays_date = datetime.today()
        beginning_of_month = datetime(todays_date.year, todays_date.month, 1)

        start_of_trend = beginning_of_month - relativedelta(months=self.past_trend)
        end_of_trend = start_of_trend + relativedelta(months=self.past_trend + self.future_trend)

        payload = self._generate_result()
        payload['data']['trend'] = []

        for dt in rrule(MONTHLY, dtstart=start_of_trend, until=end_of_trend):
            time_value = time.mktime(dt.timetuple())

            balance = account.GetBalanceAsOfDate(time_value)
            payload['data']['trend'].append(dict(date=dt.strftime('%Y-%m-%d'),
                                                 balance=get_decimal(balance)))

        return payload
    def __call__(self):

        today = date.today()
        today_time = time.mktime(today.timetuple())

        credit_amount = Decimal(0.0)
        credit_used = Decimal(0.0)

        for credit_definition in self._credit_accounts:
            account = get_account(credit_definition['account'])
            limit = credit_definition.get('limit', '0.0')

            credit_amount += Decimal(limit)

            balance = get_decimal(account.GetBalanceAsOfDate(today_time))
            credit_used += balance

        payload = self._generate_result()
        payload['data']['credit_limit'] = credit_amount + credit_used
        payload['data']['credit_amount'] = -credit_used

        return payload
    def __call__(self):
        contribution_total = Decimal("0.0")
        today = date.today()
        beginning_of_year = date(today.year, 1, 1)

        for account_name in self.income_accounts:
            account = get_account(account_name)

            for split in get_splits(account, self._start.date, self._end.date):
                parent = split.parent

                for income_split in parent.GetSplitList():

                    if income_split.GetAccount().get_full_name() in self.retirement_accounts:
                        contribution_total += get_decimal(income_split.GetAmount())

        result = self._generate_result()
        result["data"]["contributionLimit"] = self.contribution_limit
        result["data"]["contribution"] = contribution_total
        result["data"]["dayOfYear"] = (today - beginning_of_year).days + 1
        result["data"]["daysInYear"] = (date(today.year, 12, 31) - beginning_of_year).days + 1

        return result
def configure(json_configuration):
    global _currency
    account_name = json_configuration.get('currency', dict()).get('account_name', 'Income')

    _currency = get_account(account_name).GetCommodity()
    def __call__(self):
        account = get_account(self.account_name)

        data = dict(purchases=[], dividend=[], value=[])

        last_dividend = Decimal('0.0')
        last_purchase = Decimal('0.0')

        currency = get_currency()

        purchases = dict()
        dividends = dict()
        values = dict()

        for split in account.GetSplitList():
            other_account_name = get_corr_account_full_name(split)
            other_account = get_account(other_account_name)

            account_type = AccountTypes(other_account.GetType())
            date = datetime.fromtimestamp(split.parent.GetDate())

            # Store previous data
            if len(purchases):
                previous_date = date - relativedelta(days=1)
                previous_date_key = time.mktime(previous_date.timetuple())
                purchases[previous_date_key] = last_purchase
                dividends[previous_date_key] = last_dividend
                values[previous_date_key] = get_balance_on_date(account, previous_date, currency)

            # Find the correct amount that was paid from the account into this account.
            change_amount = get_decimal(split.GetValue())

            if change_amount > 0:
                # Need to get the value from the corr account split.
                for parent_splits in split.parent.GetSplitList():
                    if parent_splits.GetAccount().get_full_name() == other_account_name:
                        change_amount = -get_decimal(parent_splits.GetValue())

            if account_type == AccountTypes.mutual_fund or account_type == AccountTypes.asset:
                # Asset or mutual fund transfer
                last_purchase += change_amount
            else:
                last_dividend += get_decimal(split.GetValue())

            key = time.mktime(date.timetuple())
            purchases[key] = last_purchase
            dividends[key] = last_dividend
            values[key] = get_balance_on_date(account, date, currency)

        # Now get all of the price updates in the database.
        price_database = get_session().get_book().get_price_db()
        commodity = account.GetCommodity()
        for price in price_database.get_prices(commodity, None):
            date = time.mktime(price.get_time().timetuple())

            values[date] = max(values.get(date, Decimal('0.0')),
                               get_balance_on_date(account, price.get_time(), currency))

        data['purchases'] = sorted([(key, value) for key, value in purchases.iteritems()], key=itemgetter(0))
        data['dividend'] = sorted([(key, value) for key, value in dividends.iteritems()], key=itemgetter(0))
        data['value'] = sorted([(key, value) for key, value in values.iteritems()], key=itemgetter(0))

        results = self._generate_result()
        results['data'] = data

        return results