mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-23 22:35:03 +00:00
Simplified some code.
This commit is contained in:
@@ -100,9 +100,9 @@ class ChartJsCategoryChartGenerator implements CategoryChartGenerator
|
|||||||
],
|
],
|
||||||
];
|
];
|
||||||
foreach ($entries as $entry) {
|
foreach ($entries as $entry) {
|
||||||
if ($entry['sum'] != 0) {
|
if ($entry->spent != 0) {
|
||||||
$data['labels'][] = $entry['name'];
|
$data['labels'][] = $entry->name;
|
||||||
$data['datasets'][0]['data'][] = round(($entry['sum'] * -1), 2);
|
$data['datasets'][0]['data'][] = round(($entry->spent * -1), 2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -14,6 +14,7 @@ use Navigation;
|
|||||||
use Preferences;
|
use Preferences;
|
||||||
use Response;
|
use Response;
|
||||||
use Session;
|
use Session;
|
||||||
|
use stdClass;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class CategoryController
|
* Class CategoryController
|
||||||
@@ -115,19 +116,17 @@ class CategoryController extends Controller
|
|||||||
return Response::json($cache->get()); // @codeCoverageIgnore
|
return Response::json($cache->get()); // @codeCoverageIgnore
|
||||||
}
|
}
|
||||||
|
|
||||||
$array = $repository->getCategoriesAndExpenses($start, $end);
|
// get data for categories (and "no category"):
|
||||||
// sort by callback:
|
$set = $repository->spentForAccountsPerMonth(new Collection, $start, $end);
|
||||||
uasort(
|
$outside = $repository->sumSpentNoCategory(new Collection, $start, $end);
|
||||||
$array,
|
|
||||||
function ($left, $right) {
|
|
||||||
if ($left['sum'] == $right['sum']) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ($left['sum'] < $right['sum']) ? -1 : 1;
|
// this is a "fake" entry for the "no category" entry.
|
||||||
}
|
$entry = new stdClass();
|
||||||
);
|
$entry->name = trans('firefly.no_category');
|
||||||
$set = new Collection($array);
|
$entry->spent = $outside;
|
||||||
|
$set->push($entry);
|
||||||
|
|
||||||
|
$set = $set->sortBy('spent');
|
||||||
$data = $this->generator->frontpage($set);
|
$data = $this->generator->frontpage($set);
|
||||||
$cache->store($data);
|
$cache->store($data);
|
||||||
|
|
||||||
|
@@ -11,6 +11,34 @@ use FireflyIII\User;
|
|||||||
*/
|
*/
|
||||||
class CronController extends Controller
|
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.
|
* Firefly doesn't have anything that should be in the a cron job, except maybe this one, and it's fairly exceptional.
|
||||||
@@ -25,44 +53,11 @@ class CronController extends Controller
|
|||||||
|
|
||||||
if (strlen(env('SENDGRID_USERNAME')) > 0 && strlen(env('SENDGRID_PASSWORD')) > 0) {
|
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>';
|
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);
|
||||||
|
|
||||||
|
|
||||||
$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.
|
|
||||||
*/
|
|
||||||
if (is_array($data)) {
|
|
||||||
echo 'Found ' . count($data) . ' entries in the SendGrid ' . $name . ' list.' . "\n";
|
|
||||||
foreach ($data as $entry) {
|
|
||||||
$address = $entry->email;
|
|
||||||
$user = User::where('email', $address)->where('blocked', 0)->first();
|
|
||||||
if (!is_null($user)) {
|
|
||||||
echo 'Found a user: ' . $address . ', who is now blocked.' . "\n";
|
|
||||||
$user->blocked = 1;
|
|
||||||
$user->blocked_code = 'bounced';
|
|
||||||
$user->password = 'bounced';
|
|
||||||
$user->save();
|
|
||||||
} else {
|
|
||||||
echo 'Found no user: ' . $address . ', did nothing.' . "\n";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
echo 'Done!' . "\n";
|
echo 'Done!' . "\n";
|
||||||
} else {
|
} else {
|
||||||
@@ -71,4 +66,29 @@ class CronController extends Controller
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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) {
|
||||||
|
$address = $entry->email;
|
||||||
|
$user = User::where('email', $address)->where('blocked', 0)->first();
|
||||||
|
if (!is_null($user)) {
|
||||||
|
echo 'Found a user: ' . $address . ', who is now blocked.' . "\n";
|
||||||
|
$user->blocked = 1;
|
||||||
|
$user->blocked_code = 'bounced';
|
||||||
|
$user->password = 'bounced';
|
||||||
|
$user->save();
|
||||||
|
} else {
|
||||||
|
echo 'Found no user: ' . $address . ', did nothing.' . "\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -19,68 +19,6 @@ use Illuminate\Support\Collection;
|
|||||||
class CategoryRepository implements CategoryRepositoryInterface
|
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.
|
* 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
|
* 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.
|
* 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 $accounts
|
||||||
* @param $start
|
* @param $start
|
||||||
@@ -243,7 +181,7 @@ group by categories.id, transaction_types.type, dateFormatted
|
|||||||
public function spentForAccountsPerMonth(Collection $accounts, Carbon $start, Carbon $end)
|
public function spentForAccountsPerMonth(Collection $accounts, Carbon $start, Carbon $end)
|
||||||
{
|
{
|
||||||
$accountIds = $accounts->pluck('id')->toArray();
|
$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('category_transaction_journal', 'category_transaction_journal.category_id', '=', 'categories.id')
|
||||||
->leftJoin('transaction_journals', 'category_transaction_journal.transaction_journal_id', '=', 'transaction_journals.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('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id')
|
||||||
@@ -257,22 +195,26 @@ group by categories.id, transaction_types.type, dateFormatted
|
|||||||
$join->on('t_dest.transaction_journal_id', '=', 'transaction_journals.id')->where('t_dest.amount', '>', 0);
|
$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(
|
->whereIn(
|
||||||
'transaction_types.type', [TransactionType::WITHDRAWAL, TransactionType::TRANSFER, TransactionType::OPENING_BALANCE]
|
'transaction_types.type', [TransactionType::WITHDRAWAL, TransactionType::TRANSFER, TransactionType::OPENING_BALANCE]
|
||||||
)// spent on these things.
|
)// spent on these things.
|
||||||
->where('transaction_journals.date', '>=', $start->format('Y-m-d'))
|
->where('transaction_journals.date', '>=', $start->format('Y-m-d'))
|
||||||
->where('transaction_journals.date', '<=', $end->format('Y-m-d'))
|
->where('transaction_journals.date', '<=', $end->format('Y-m-d'))
|
||||||
->groupBy('categories.id')
|
->groupBy('categories.id')
|
||||||
->groupBy('dateFormatted')
|
->groupBy('dateFormatted');
|
||||||
->get(
|
|
||||||
[
|
if (count($accountIds) > 0) {
|
||||||
'categories.*',
|
$query->whereIn('t_src.account_id', $accountIds)// from these accounts (spent)
|
||||||
DB::Raw('DATE_FORMAT(`transaction_journals`.`date`,"%Y-%m") as `dateFormatted`'),
|
->whereNotIn('t_dest.account_id', $accountIds);//-- but not from these accounts (spent internally)
|
||||||
DB::Raw('SUM(`t_src`.`amount`) AS `spent`')
|
}
|
||||||
]
|
|
||||||
);
|
$collection = $query->get(
|
||||||
|
[
|
||||||
|
'categories.*',
|
||||||
|
DB::Raw('DATE_FORMAT(`transaction_journals`.`date`,"%Y-%m") as `dateFormatted`'),
|
||||||
|
DB::Raw('SUM(`t_src`.`amount`) AS `spent`')
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
return $collection;
|
return $collection;
|
||||||
}
|
}
|
||||||
@@ -329,19 +271,24 @@ group by categories.id, transaction_types.type, dateFormatted
|
|||||||
}
|
}
|
||||||
|
|
||||||
// is withdrawal or transfer AND account_from is in the list of $accounts
|
// is withdrawal or transfer AND account_from is in the list of $accounts
|
||||||
$single = Auth::user()
|
$query = Auth::user()
|
||||||
->transactionjournals()
|
->transactionjournals()
|
||||||
->leftJoin('category_transaction_journal', 'category_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
|
->leftJoin('category_transaction_journal', 'category_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
|
||||||
->whereNull('category_transaction_journal.id')
|
->whereNull('category_transaction_journal.id')
|
||||||
->before($end)
|
->before($end)
|
||||||
->after($start)
|
->after($start)
|
||||||
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
||||||
->whereIn('transactions.account_id', $accountIds)
|
->transactionTypes($types);
|
||||||
->transactionTypes($types)
|
if (count($accountIds) > 0) {
|
||||||
->first(
|
$query->whereIn('transactions.account_id', $accountIds);
|
||||||
[
|
}
|
||||||
DB::Raw('SUM(`transactions`.`amount`) as `sum`)')]
|
|
||||||
);
|
|
||||||
|
$single = $query->first(
|
||||||
|
[
|
||||||
|
DB::Raw('SUM(`transactions`.`amount`) as `sum`')
|
||||||
|
]
|
||||||
|
);
|
||||||
if (!is_null($single)) {
|
if (!is_null($single)) {
|
||||||
return $single->sum;
|
return $single->sum;
|
||||||
}
|
}
|
||||||
|
@@ -27,16 +27,6 @@ interface CategoryRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function earnedForAccountsPerMonth(Collection $accounts, Carbon $start, Carbon $end);
|
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.
|
* Returns a list of all the categories belonging to a user.
|
||||||
*
|
*
|
||||||
|
Reference in New Issue
Block a user