biweeklybudget.biweeklypayperiod module

class biweeklybudget.biweeklypayperiod.BiweeklyPayPeriod(start_date, db_session)[source]

Bases: object

This object contains all logic related to working with pay periods, specifically finding a pay period for a given data, and figuring out the start and end dates of pay periods. Sure, the app is called “biweeklybudget” but there’s no reason to hard-code logic all over the place that’s this simple.

_data

Return the object-local data cache dict. Built it if not already present.

Returns:object-local data cache
Return type:dict
_dict_for_sched_trans(t)[source]

Return a dict describing the ScheduledTransaction t. Called from _trans_dict().

The resulting dict will have the following layout:

  • type (str) “Transaction” or “ScheduledTransaction”
  • id (int) the id of the object
  • date (date) the date of the transaction, or None for per-period ScheduledTransactions
  • sched_type (str) for ScheduledTransactions, the schedule type (“monthly”, “date”, or “per period”)
  • sched_trans_id None
  • description (str) the transaction description
  • amount (float) the transaction amount
  • budgeted_amount None
  • account_id (int) the id of the Account the transaction is against.
  • account_name (str) the name of the Account the transaction is against.
  • budget_id (int) the id of the Budget the transaction is against.
  • budget_name (str) the name of the Budget the transaction is against.
  • reconcile_id (int) the ID of the TxnReconcile, or None
Parameters:t (ScheduledTransaction) – ScheduledTransaction to describe
Returns:common-format dict describing t
Return type:dict
_dict_for_trans(t)[source]

Return a dict describing the Transaction t. Called from _trans_dict().

The resulting dict will have the following layout:

  • type (str) “Transaction” or “ScheduledTransaction”
  • id (int) the id of the object
  • date (date) the date of the transaction, or None for per-period ScheduledTransactions
  • sched_type (str) for ScheduledTransactions, the schedule type (“monthly”, “date”, or “per period”)
  • sched_trans_id (int) for Transactions, the ScheduledTransaction id that it was created from, or None.
  • description (str) the transaction description
  • amount (float) the transaction amount
  • budgeted_amount (float) the budgeted amount. This may be None.
  • account_id (int) the id of the Account the transaction is against.
  • account_name (str) the name of the Account the transaction is against.
  • budget_id (int) the id of the Budget the transaction is against.
  • budget_name (str) the name of the Budget the transaction is against.
  • reconcile_id (int) the ID of the TxnReconcile, or None
Parameters:t (Transaction) – transaction to describe
Returns:common-format dict describing t
Return type:dict
_income_budget_ids

Return a list of all Budget IDs for Income budgets.

Returns:list of income budget IDs
Return type:list
_make_budget_sums()[source]

Find the sums of all transactions per periodic budget ID ; return a dict where keys are budget IDs and values are per-budget dicts containing:

  • budget_amount (float) - the periodic budget starting_balance.
  • allocated (float) - sum of all ScheduledTransaction and Transaction amounts against the budget this period. For actual transactions, we use the budgeted_amount if present (not None).
  • spent (float) - the sum of all actual Transaction amounts against the budget this period.
  • trans_total (float) - the sum of spent amounts for Transactions that have them, or allocated amounts for ScheduledTransactions.
  • remaining (float) - the remaining amount in the budget. This is budget_amount minus the greater of allocated or trans_total. For income budgets, this is always positive.
Returns:dict of dicts, transaction sums and amounts per budget
Return type:dict
_make_combined_transactions()[source]

Combine all Transactions and ScheduledTransactions from self._data_cache into one ordered list of similar dicts, adding dates to the monthly ScheduledTransactions as appropriate and excluding ScheduledTransactions that have been converted to real Transactions. Store the finished list back into self._data_cache.

_make_overall_sums()[source]

Return a dict describing the overall sums for this pay period, namely:

  • allocated (float) total amount allocated via ScheduledTransaction, Transaction (counting the budgeted_amount for Transactions that have one), or Budget (not counting income budgets).
  • spent (float) total amount actually spent via Transaction.
  • income (float) total amount of income allocated this pay period. Calculated value (from _make_budget_sums() / self._data_cache['budget_sums']) should be negative, but is returned as its positive inverse (absolute value).
  • remaining (float) income minus the greater of allocated or spent
Returns:dict describing sums for the pay period
Return type:dict
_scheduled_transactions_date()[source]

Return a Query for all ScheduledTransaction defined by date (schedule_type == “date”) for this pay period.

Returns:Query matching all ScheduledTransactions defined by date, for this pay period.
Return type:sqlalchemy.orm.query.Query
_scheduled_transactions_monthly()[source]

Return a Query for all ScheduledTransaction defined by day of month (schedule_type == “monthly”) for this pay period.

Returns:Query matching all ScheduledTransactions defined by day of month (monthly) for this period.
Return type:sqlalchemy.orm.query.Query
_scheduled_transactions_per_period()[source]

Return a Query for all ScheduledTransaction defined by number per period (schedule_type == “per period”) for this pay period.

Returns:Query matching all ScheduledTransactions defined by number per period, for this pay period.
Return type:sqlalchemy.orm.query.Query
_trans_dict(t)[source]

Given a Transaction or ScheduledTransaction, return a dict of a common format describing the object.

The resulting dict will have the following layout:

  • type (str) “Transaction” or “ScheduledTransaction”
  • id (int) the id of the object
  • date (date) the date of the transaction, or None for per-period ScheduledTransactions
  • sched_type (str) for ScheduledTransactions, the schedule type (“monthly”, “date”, or “per period”)
  • sched_trans_id (int) for Transactions, the ScheduledTransaction id that it was created from, or None.
  • description (str) the transaction description
  • amount (float) the transaction amount
  • budgeted_amount (float) the budgeted amount. This may be None.
  • account_id (int) the id of the Account the transaction is against.
  • account_name (str) the name of the Account the transaction is against.
  • budget_id (int) the id of the Budget the transaction is against.
  • budget_name (str) the name of the Budget the transaction is against.
  • reconcile_id (int) the ID of the TxnReconcile, or None
Parameters:t (Transaction or ScheduledTransaction) – the object to return a dict for
Returns:dict describing t
Return type:dict
_transactions()[source]

Return a Query for all Transaction for this pay period.

Returns:Query matching all Transactions for this pay period
Return type:sqlalchemy.orm.query.Query
budget_sums

Return a dict of budget sums; the return value of _make_budget_sums().

Returns:dict of dicts, transaction sums and amounts per budget
Return type:dict
end_date

Return the date of the last day in this pay period. The pay period is generally considered to end at the last instant (i.e. 23:59:59) of this date.

Returns:last date in the pay period
Return type:datetime.date
filter_query(query, date_prop)[source]

Filter query for date_prop in this pay period. Returns a copy of the query.

e.g. to filter an existing query of OFXTransaction for the BiweeklyPayPeriod starting on 2017-01-14:

q = # some query here
p = BiweeklyPayPeriod(date(2017, 1, 14))
q = p.filter_query(q, OFXTransaction.date_posted)
Parameters:
  • query (sqlalchemy.orm.query.Query) – The query to filter
  • date_prop – the Model’s date property, to filter on.
Returns:

the filtered query

Return type:

sqlalchemy.orm.query.Query

next

Return the BiweeklyPayPeriod following this one.

Returns:next BiweeklyPayPeriod after this one
Return type:BiweeklyPayPeriod
overall_sums

Return a dict of overall sums; the return value of _make_overall_sums().

Returns:dict describing sums for the pay period
Return type:dict
static period_for_date(dt, db_session)[source]

Given a datetime, return the BiweeklyPayPeriod instance describing the pay period containing this date.

Todo

This is a very naive, poorly-performing implementation.

Parameters:
Returns:

BiweeklyPayPeriod containing the specified date

Return type:

BiweeklyPayPeriod

period_interval

Return the interval between BiweeklyPayPeriods as a timedelta.

Returns:interval between BiweeklyPayPeriods
Return type:datetime.timedelta
period_length

Return the length of a BiweeklyPayPeriod; this is calculated as period_interval minus one second.

Returns:length of one BiweeklyPayPeriod
Return type:datetime.timedelta
previous

Return the BiweeklyPayPeriod preceding this one.

Returns:previous BiweeklyPayPeriod before this one
Return type:BiweeklyPayPeriod
start_date

Return the starting date for this pay period. The period is generally considered to start at midnight (00:00) of this date.

Returns:start date for pay period
Return type:datetime.date
transactions_list

Return an ordered list of dicts, each representing a transaction for this pay period. Dicts have keys and values as described in _trans_dict().

Returns:ordered list of transaction dicts
Return type:list