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. Build 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 objectdate
(date) the date of the transaction, or None for per-period ScheduledTransactionssched_type
(str) for ScheduledTransactions, the schedule type (“monthly”, “date”, or “per period”)sched_trans_id
Nonedescription
(str) the transaction descriptionamount
(Decimal.decimal) the transaction amountbudgeted_amount
Noneaccount_id
(int) the id of the Account the transaction is against.account_name
(str) the name of the Account the transaction is against.reconcile_id
(int) the ID of the TxnReconcile, or Nonebudgets
(dict) dict of information on the Budgets this Transaction is against. Keys are budget IDs (int), values are dicts with keys “amount” (Decimal) and “name” (string).
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 objectdate
(date) the date of the transaction, or None for per-period ScheduledTransactionssched_type
(str) for ScheduledTransactions, the schedule type (“monthly”, “date”, or “per period”)sched_trans_id
(int) for Transactions, the ScheduledTransactionid
that it was created from, or None.description
(str) the transaction descriptionamount
(Decimal.decimal) the transaction amountbudgeted_amount
(Decimal.decimal) 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.reconcile_id
(int) the ID of the TxnReconcile, or Noneplanned_budget_id
(int) the id of the Budget the transaction was planned against, if any. May be None.planned_budget_name
(str) the name of the Budget the transaction was planned against, if any. May be None.budgets
(dict) dict of information on the Budgets this Transaction is against. Keys are budget IDs (int), values are dicts with keys “amount” (Decimal) and “name” (string).
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
(Decimal.decimal) - the periodic budgetstarting_balance
.allocated
(Decimal.decimal) - sum of allScheduledTransaction
andTransaction
amounts against the budget this period. For actual transactions, we use thebudgeted_amount
if present (not None).spent
(Decimal.decimal) - the sum of all actualTransaction
amounts against the budget this period.trans_total
(Decimal.decimal) - the sum of spent amounts for Transactions that have them, or allocated amounts for ScheduledTransactions.remaining
(Decimal.decimal) - the remaining amount in the budget. This isbudget_amount
minus the greater ofallocated
ortrans_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 intoself._data_cache
.
-
_make_overall_sums
()[source]¶ Return a dict describing the overall sums for this pay period, namely:
allocated
(Decimal.decimal) total amount allocated viaScheduledTransaction
,Transaction
(counting thebudgeted_amount
for Transactions that have one), orBudget
(not counting income budgets).spent
(Decimal.decimal) total amount actually spent viaTransaction
.income
(Decimal.decimal) 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
(Decimal.decimal) income minus the greater ofallocated
orspent
for current or future pay periods, or minusspent
for pay periods ending in the past (is_in_past
)
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 objectdate
(date) the date of the transaction, or None for per-period ScheduledTransactionssched_type
(str) for ScheduledTransactions, the schedule type (“monthly”, “date”, or “per period”)sched_trans_id
(int) for Transactions, the ScheduledTransactionid
that it was created from, or None.description
(str) the transaction descriptionamount
(Decimal.decimal) the transaction amountbudgeted_amount
(Decimal.decimal) 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.budgets
(dict) dict of information on the Budgets this Transaction is against. Keys are budget IDs (int), values are dicts with keys “amount” (Decimal) and “name” (string).reconcile_id
(int) the ID of the TxnReconcile, or Noneplanned_budget_id
(int) the id of the Budget the transaction was planned against, if any. May be None.planned_budget_name
(str) the name of the Budget the transaction was planned against, if any. May be None.
Parameters: t ( Transaction
orScheduledTransaction
) – the object to return a dict forReturns: 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
-
clear_cache
()[source]¶ Clear the cached transaction, budget and sum data stored in self._data_cache and returned by
_data
.
-
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
fordate_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
- query (
-
is_in_past
¶
-
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: - dt (
datetime
ordate
) – datetime or date to find the pay period for - db_session (sqlalchemy.orm.session.Session) – active database session to use for queries
Returns: BiweeklyPayPeriod containing the specified date
Return type: - dt (
-
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
-