mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-29 18:20:01 +00:00
Fixed budget controller. [skip ci]
This commit is contained in:
@@ -29,15 +29,14 @@ class Budget implements BudgetInterface
|
||||
|
||||
foreach ($budgets as $budget) {
|
||||
foreach ($budget->limits as $limit) {
|
||||
$dateFormats = \Config::get('firefly.date_formats_by_period.' . $limit->repeat_freq);
|
||||
if (is_null($dateFormats)) {
|
||||
throw new \Firefly\Exception\FireflyException('No date formats for ' . $limit->repeat_freq);
|
||||
}
|
||||
|
||||
foreach ($limit->limitrepetitions as $rep) {
|
||||
$periodOrder = $rep->startdate->format($dateFormats['group_date']);
|
||||
$period = $rep->startdate->format($dateFormats['display_date']);
|
||||
$return[$periodOrder] = isset($return[$periodOrder]) ? $return[$periodOrder] : ['date' => $period];
|
||||
$periodOrder = $rep->periodOrder();
|
||||
$period = $rep->periodShow();
|
||||
$return[$periodOrder] = isset($return[$periodOrder])
|
||||
? $return[$periodOrder]
|
||||
: ['date' => $period,
|
||||
'budget_id' => $limit->budget_id];
|
||||
|
||||
}
|
||||
}
|
||||
@@ -45,17 +44,177 @@ class Budget implements BudgetInterface
|
||||
// put all the budgets under their respective date:
|
||||
foreach ($budgets as $budget) {
|
||||
foreach ($budget->limits as $limit) {
|
||||
$dateFormats = \Config::get('firefly.date_formats_by_period.' . $limit->repeat_freq);
|
||||
foreach ($limit->limitrepetitions as $rep) {
|
||||
$rep->left = $rep->left();
|
||||
|
||||
$month = $rep->startdate->format($dateFormats['group_date']);
|
||||
$month = $rep->periodOrder();
|
||||
$return[$month]['limitrepetitions'][] = $rep;
|
||||
}
|
||||
}
|
||||
}
|
||||
krsort($return);
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
}
|
||||
/**
|
||||
* @param \Budget $budget
|
||||
* @param $repetitionId
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function organizeRepetition(\Budget $budget, $repetitionId)
|
||||
{
|
||||
$result = [];
|
||||
$inRepetition = [];
|
||||
$repetition = \LimitRepetition::with('limit', 'limit.budget')->leftJoin(
|
||||
'limits', 'limit_repetitions.limit_id', '=', 'limits.id'
|
||||
)->leftJoin('components', 'limits.component_id', '=', 'components.id')->where(
|
||||
'components.user_id', \Auth::user()->id
|
||||
)
|
||||
->where('limit_repetitions.id', $repetitionId)->first(['limit_repetitions.*']);
|
||||
|
||||
// get transactions:
|
||||
$set = $repetition->limit->budget->transactionjournals()->with(
|
||||
'transactions', 'transactions.account', 'components', 'transactiontype'
|
||||
)->leftJoin(
|
||||
'transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id'
|
||||
)->where('transaction_types.type', 'Withdrawal')->where(
|
||||
'date', '>=', $repetition->startdate->format('Y-m-d')
|
||||
)->where('date', '<=', $repetition->enddate->format('Y-m-d'))->orderBy('date', 'DESC')->orderBy(
|
||||
'id', 'DESC'
|
||||
)->get(['transaction_journals.*']);
|
||||
|
||||
$result[0] = [
|
||||
'date' => $repetition->periodShow(),
|
||||
'limit' => $repetition->limit,
|
||||
'limitrepetition' => $repetition,
|
||||
'journals' => $set,
|
||||
'paginated' => false
|
||||
];
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Budget $budget
|
||||
*
|
||||
* @return mixed|void
|
||||
*/
|
||||
public function organizeRepetitions(\Budget $budget)
|
||||
{
|
||||
$result = [];
|
||||
$inRepetition = [];
|
||||
foreach ($budget->limits as $limit) {
|
||||
foreach ($limit->limitrepetitions as $repetition) {
|
||||
$order = $repetition->periodOrder();
|
||||
$result[$order] = [
|
||||
'date' => $repetition->periodShow(),
|
||||
'limitrepetition' => $repetition,
|
||||
'limit' => $limit,
|
||||
'journals' => [],
|
||||
'paginated' => false
|
||||
];
|
||||
$transactions = [];
|
||||
$set = $budget->transactionjournals()->with(
|
||||
'transactions', 'transactions.account', 'components', 'transactiontype'
|
||||
)->leftJoin(
|
||||
'transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id'
|
||||
)->where('transaction_types.type', 'Withdrawal')->where(
|
||||
'date', '>=', $repetition->startdate->format('Y-m-d')
|
||||
)->where('date', '<=', $repetition->enddate->format('Y-m-d'))->orderBy('date', 'DESC')->orderBy(
|
||||
'id', 'DESC'
|
||||
)->get(['transaction_journals.*']);
|
||||
foreach ($set as $entry) {
|
||||
$transactions[] = $entry;
|
||||
$inRepetition[] = $entry->id;
|
||||
}
|
||||
$result[$order]['journals'] = $transactions;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
$query = $budget->transactionjournals()->with(
|
||||
'transactions', 'transactions.account', 'components', 'transactiontype',
|
||||
'transactions.account.accounttype'
|
||||
)->whereNotIn(
|
||||
'transaction_journals.id', $inRepetition
|
||||
)->orderBy('date', 'DESC')->orderBy(
|
||||
'transaction_journals.id', 'DESC'
|
||||
);
|
||||
|
||||
// build paginator:
|
||||
$perPage = 25;
|
||||
$totalItems = $query->count();
|
||||
$page = intval(\Input::get('page')) > 1 ? intval(\Input::get('page')) : 1;
|
||||
$skip = ($page - 1) * $perPage;
|
||||
$set = $query->skip($skip)->take($perPage)->get();
|
||||
|
||||
// stupid paginator!
|
||||
$items = [];
|
||||
/** @var $item \TransactionJournal */
|
||||
foreach ($set as $item) {
|
||||
$items[] = $item;
|
||||
}
|
||||
$paginator = \Paginator::make($items, $totalItems, $perPage);
|
||||
$result['0000'] = ['date' => 'Not in an envelope', 'limit' => null, 'paginated' => true,
|
||||
'journals' => $paginator];
|
||||
|
||||
krsort($result);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Budget $budget
|
||||
*
|
||||
* @return mixed|void
|
||||
*/
|
||||
public function outsideRepetitions(\Budget $budget)
|
||||
{
|
||||
$inRepetitions = [];
|
||||
foreach ($budget->limits as $limit) {
|
||||
foreach ($limit->limitrepetitions as $repetition) {
|
||||
$set = $budget->transactionjournals()->leftJoin(
|
||||
'transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id'
|
||||
)->where('transaction_types.type', 'Withdrawal')->where(
|
||||
'date', '>=', $repetition->startdate->format('Y-m-d')
|
||||
)->where('date', '<=', $repetition->enddate->format('Y-m-d'))->orderBy('date', 'DESC')->get(
|
||||
['transaction_journals.id']
|
||||
);
|
||||
foreach ($set as $item) {
|
||||
$inRepetitions[] = $item->id;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
$query = $budget->transactionjournals()->with(
|
||||
'transactions', 'transactions.account', 'components', 'transactiontype',
|
||||
'transactions.account.accounttype'
|
||||
)->whereNotIn(
|
||||
'transaction_journals.id', $inRepetitions
|
||||
)->orderBy('date', 'DESC')->orderBy(
|
||||
'transaction_journals.id', 'DESC'
|
||||
);
|
||||
|
||||
// build paginator:
|
||||
$perPage = 25;
|
||||
$totalItems = $query->count();
|
||||
$page = intval(\Input::get('page')) > 1 ? intval(\Input::get('page')) : 1;
|
||||
$skip = ($page - 1) * $perPage;
|
||||
$set = $query->skip($skip)->take($perPage)->get();
|
||||
|
||||
// stupid paginator!
|
||||
$items = [];
|
||||
/** @var $item \TransactionJournal */
|
||||
foreach ($set as $item) {
|
||||
$items[] = $item;
|
||||
}
|
||||
$paginator = \Paginator::make($items, $totalItems, $perPage);
|
||||
$result = [0 => ['date' => 'Not in an envelope', 'limit' => null, 'paginated' => true,
|
||||
'journals' => $paginator]];
|
||||
return $result;
|
||||
}
|
||||
}
|
@@ -15,4 +15,27 @@ interface BudgetInterface {
|
||||
*/
|
||||
public function organizeByDate(Collection $budgets);
|
||||
|
||||
|
||||
/**
|
||||
* @param \Budget $budget
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function organizeRepetitions(\Budget $budget);
|
||||
|
||||
/**
|
||||
* @param \Budget $budget
|
||||
* @param $repetitionId
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function organizeRepetition(\Budget $budget, $repetitionId);
|
||||
|
||||
/**
|
||||
* @param \Budget $budget
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function outsideRepetitions(\Budget $budget);
|
||||
|
||||
}
|
Reference in New Issue
Block a user