mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-23 14:26:58 +00:00
Simplified some code.
This commit is contained in:
@@ -100,9 +100,9 @@ class ChartJsCategoryChartGenerator implements CategoryChartGenerator
|
||||
],
|
||||
];
|
||||
foreach ($entries as $entry) {
|
||||
if ($entry['sum'] != 0) {
|
||||
$data['labels'][] = $entry['name'];
|
||||
$data['datasets'][0]['data'][] = round(($entry['sum'] * -1), 2);
|
||||
if ($entry->spent != 0) {
|
||||
$data['labels'][] = $entry->name;
|
||||
$data['datasets'][0]['data'][] = round(($entry->spent * -1), 2);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -14,6 +14,7 @@ use Navigation;
|
||||
use Preferences;
|
||||
use Response;
|
||||
use Session;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* Class CategoryController
|
||||
@@ -115,19 +116,17 @@ class CategoryController extends Controller
|
||||
return Response::json($cache->get()); // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
$array = $repository->getCategoriesAndExpenses($start, $end);
|
||||
// sort by callback:
|
||||
uasort(
|
||||
$array,
|
||||
function ($left, $right) {
|
||||
if ($left['sum'] == $right['sum']) {
|
||||
return 0;
|
||||
}
|
||||
// get data for categories (and "no category"):
|
||||
$set = $repository->spentForAccountsPerMonth(new Collection, $start, $end);
|
||||
$outside = $repository->sumSpentNoCategory(new Collection, $start, $end);
|
||||
|
||||
return ($left['sum'] < $right['sum']) ? -1 : 1;
|
||||
}
|
||||
);
|
||||
$set = new Collection($array);
|
||||
// this is a "fake" entry for the "no category" entry.
|
||||
$entry = new stdClass();
|
||||
$entry->name = trans('firefly.no_category');
|
||||
$entry->spent = $outside;
|
||||
$set->push($entry);
|
||||
|
||||
$set = $set->sortBy('spent');
|
||||
$data = $this->generator->frontpage($set);
|
||||
$cache->store($data);
|
||||
|
||||
|
@@ -11,6 +11,34 @@ use FireflyIII\User;
|
||||
*/
|
||||
class CronController extends Controller
|
||||
{
|
||||
/** @var array */
|
||||
protected $set = [];
|
||||
|
||||
/** @var array */
|
||||
protected $parameters = [];
|
||||
|
||||
|
||||
/**
|
||||
* CronController constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->set = [
|
||||
'blocks' => 'https://api.sendgrid.com/api/blocks.get.json',
|
||||
'bounces' => 'https://api.sendgrid.com/api/bounces.get.json',
|
||||
'invalids' => 'https://api.sendgrid.com/api/invalidemails.get.json',
|
||||
|
||||
];
|
||||
$this->parameters = [
|
||||
'api_user' => env('SENDGRID_USERNAME'),
|
||||
'api_key' => env('SENDGRID_PASSWORD'),
|
||||
'date' => 1,
|
||||
'days' => 7
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Firefly doesn't have anything that should be in the a cron job, except maybe this one, and it's fairly exceptional.
|
||||
@@ -25,28 +53,25 @@ class CronController extends Controller
|
||||
|
||||
if (strlen(env('SENDGRID_USERNAME')) > 0 && strlen(env('SENDGRID_PASSWORD')) > 0) {
|
||||
|
||||
$set = [
|
||||
'blocks' => 'https://api.sendgrid.com/api/blocks.get.json',
|
||||
'bounces' => 'https://api.sendgrid.com/api/bounces.get.json',
|
||||
'invalids' => 'https://api.sendgrid.com/api/invalidemails.get.json',
|
||||
|
||||
];
|
||||
echo '<pre>';
|
||||
foreach ($set as $name => $URL) {
|
||||
foreach ($this->set as $name => $url) {
|
||||
$data = json_decode(file_get_contents($url . '?' . http_build_query($this->parameters)));
|
||||
$this->processResult($name, $data);
|
||||
|
||||
}
|
||||
echo 'Done!' . "\n";
|
||||
} else {
|
||||
echo 'Please fill in SendGrid details.';
|
||||
}
|
||||
|
||||
$parameters = [
|
||||
'api_user' => env('SENDGRID_USERNAME'),
|
||||
'api_key' => env('SENDGRID_PASSWORD'),
|
||||
'date' => 1,
|
||||
'days' => 7
|
||||
];
|
||||
$fullURL = $URL . '?' . http_build_query($parameters);
|
||||
$data = json_decode(file_get_contents($fullURL));
|
||||
}
|
||||
|
||||
/*
|
||||
* Loop the result, if any.
|
||||
/**
|
||||
* @param string $name
|
||||
* @param array $data
|
||||
*/
|
||||
protected function processResult($name, array $data)
|
||||
{
|
||||
if (is_array($data)) {
|
||||
echo 'Found ' . count($data) . ' entries in the SendGrid ' . $name . ' list.' . "\n";
|
||||
foreach ($data as $entry) {
|
||||
@@ -62,13 +87,8 @@ class CronController extends Controller
|
||||
echo 'Found no user: ' . $address . ', did nothing.' . "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
echo 'Done!' . "\n";
|
||||
} else {
|
||||
echo 'Please fill in SendGrid details.';
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -19,68 +19,6 @@ use Illuminate\Support\Collection;
|
||||
class CategoryRepository implements CategoryRepositoryInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* TODO REMOVE ME
|
||||
*
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getCategoriesAndExpenses(Carbon $start, Carbon $end)
|
||||
{
|
||||
$set = Auth::user()->categories()
|
||||
->leftJoin('category_transaction_journal', 'category_transaction_journal.category_id', '=', 'categories.id')
|
||||
->leftJoin('transaction_journals', 'category_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
|
||||
->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id')
|
||||
->leftJoin(
|
||||
'transactions', function (JoinClause $join) {
|
||||
$join->on('transactions.transaction_journal_id', '=', 'transaction_journals.id')->where('transactions.amount', '<', 0);
|
||||
}
|
||||
)
|
||||
->where('transaction_journals.date', '>=', $start->format('Y-m-d'))
|
||||
->where('transaction_journals.date', '<=', $end->format('Y-m-d'))
|
||||
->whereIn('transaction_types.type', [TransactionType::WITHDRAWAL])
|
||||
->whereNull('transaction_journals.deleted_at')
|
||||
->groupBy('categories.id')
|
||||
->orderBy('totalAmount')
|
||||
->get(
|
||||
[
|
||||
'categories.*',
|
||||
DB::Raw('SUM(`transactions`.`amount`) as `totalAmount`')
|
||||
]
|
||||
);
|
||||
$array = [];
|
||||
/** @var Category $entry */
|
||||
foreach ($set as $entry) {
|
||||
$id = $entry->id;
|
||||
$array[$id] = ['name' => $entry->name, 'sum' => $entry->totalAmount];
|
||||
}
|
||||
|
||||
// without category:
|
||||
// TODO REMOVE ME
|
||||
$single = Auth::user()->transactionjournals()
|
||||
->leftJoin('category_transaction_journal', 'category_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
|
||||
->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id')
|
||||
->leftJoin(
|
||||
'transactions', function (JoinClause $join) {
|
||||
$join->on('transactions.transaction_journal_id', '=', 'transaction_journals.id')->where('transactions.amount', '<', 0);
|
||||
}
|
||||
)
|
||||
->whereNull('category_transaction_journal.id')
|
||||
->whereNull('transaction_journals.deleted_at')
|
||||
->where('transaction_journals.date', '>=', $start->format('Y-m-d'))
|
||||
->where('transaction_journals.date', '<=', $end->format('Y-m-d'))
|
||||
->whereIn('transaction_types.type', [TransactionType::WITHDRAWAL])
|
||||
->whereNull('transaction_journals.deleted_at')
|
||||
->first([DB::Raw('SUM(transactions.amount) as `totalAmount`')]);
|
||||
$noCategory = is_null($single->totalAmount) ? '0' : $single->totalAmount;
|
||||
$array[0] = ['name' => trans('firefly.no_category'), 'sum' => $noCategory];
|
||||
|
||||
return $array;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of all the categories belonging to a user.
|
||||
*
|
||||
@@ -232,7 +170,7 @@ group by categories.id, transaction_types.type, dateFormatted
|
||||
/**
|
||||
* Returns a collection of Categories appended with the amount of money that has been spent
|
||||
* in these categories, based on the $accounts involved, in period X, grouped per month.
|
||||
* The amount earned in category X in period X is saved in field "spent".
|
||||
* The amount spent in category X in period X is saved in field "spent".
|
||||
*
|
||||
* @param $accounts
|
||||
* @param $start
|
||||
@@ -243,7 +181,7 @@ group by categories.id, transaction_types.type, dateFormatted
|
||||
public function spentForAccountsPerMonth(Collection $accounts, Carbon $start, Carbon $end)
|
||||
{
|
||||
$accountIds = $accounts->pluck('id')->toArray();
|
||||
$collection = Auth::user()->categories()
|
||||
$query = Auth::user()->categories()
|
||||
->leftJoin('category_transaction_journal', 'category_transaction_journal.category_id', '=', 'categories.id')
|
||||
->leftJoin('transaction_journals', 'category_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
|
||||
->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id')
|
||||
@@ -257,16 +195,20 @@ group by categories.id, transaction_types.type, dateFormatted
|
||||
$join->on('t_dest.transaction_journal_id', '=', 'transaction_journals.id')->where('t_dest.amount', '>', 0);
|
||||
}
|
||||
)
|
||||
->whereIn('t_src.account_id', $accountIds)// from these accounts (spent)
|
||||
->whereNotIn('t_dest.account_id', $accountIds)//-- but not from these accounts (spent internally)
|
||||
->whereIn(
|
||||
'transaction_types.type', [TransactionType::WITHDRAWAL, TransactionType::TRANSFER, TransactionType::OPENING_BALANCE]
|
||||
)// spent on these things.
|
||||
->where('transaction_journals.date', '>=', $start->format('Y-m-d'))
|
||||
->where('transaction_journals.date', '<=', $end->format('Y-m-d'))
|
||||
->groupBy('categories.id')
|
||||
->groupBy('dateFormatted')
|
||||
->get(
|
||||
->groupBy('dateFormatted');
|
||||
|
||||
if (count($accountIds) > 0) {
|
||||
$query->whereIn('t_src.account_id', $accountIds)// from these accounts (spent)
|
||||
->whereNotIn('t_dest.account_id', $accountIds);//-- but not from these accounts (spent internally)
|
||||
}
|
||||
|
||||
$collection = $query->get(
|
||||
[
|
||||
'categories.*',
|
||||
DB::Raw('DATE_FORMAT(`transaction_journals`.`date`,"%Y-%m") as `dateFormatted`'),
|
||||
@@ -329,18 +271,23 @@ group by categories.id, transaction_types.type, dateFormatted
|
||||
}
|
||||
|
||||
// is withdrawal or transfer AND account_from is in the list of $accounts
|
||||
$single = Auth::user()
|
||||
$query = Auth::user()
|
||||
->transactionjournals()
|
||||
->leftJoin('category_transaction_journal', 'category_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
|
||||
->whereNull('category_transaction_journal.id')
|
||||
->before($end)
|
||||
->after($start)
|
||||
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
||||
->whereIn('transactions.account_id', $accountIds)
|
||||
->transactionTypes($types)
|
||||
->first(
|
||||
->transactionTypes($types);
|
||||
if (count($accountIds) > 0) {
|
||||
$query->whereIn('transactions.account_id', $accountIds);
|
||||
}
|
||||
|
||||
|
||||
$single = $query->first(
|
||||
[
|
||||
DB::Raw('SUM(`transactions`.`amount`) as `sum`)')]
|
||||
DB::Raw('SUM(`transactions`.`amount`) as `sum`')
|
||||
]
|
||||
);
|
||||
if (!is_null($single)) {
|
||||
return $single->sum;
|
||||
|
@@ -27,16 +27,6 @@ interface CategoryRepositoryInterface
|
||||
*/
|
||||
public function earnedForAccountsPerMonth(Collection $accounts, Carbon $start, Carbon $end);
|
||||
|
||||
/**
|
||||
* Corrected for tags.
|
||||
*
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getCategoriesAndExpenses(Carbon $start, Carbon $end);
|
||||
|
||||
/**
|
||||
* Returns a list of all the categories belonging to a user.
|
||||
*
|
||||
|
Reference in New Issue
Block a user