diff --git a/app/Generator/Chart/Category/ChartJsCategoryChartGenerator.php b/app/Generator/Chart/Category/ChartJsCategoryChartGenerator.php index 8a2ed404c0..8bb0402d25 100644 --- a/app/Generator/Chart/Category/ChartJsCategoryChartGenerator.php +++ b/app/Generator/Chart/Category/ChartJsCategoryChartGenerator.php @@ -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); } } diff --git a/app/Http/Controllers/Chart/CategoryController.php b/app/Http/Controllers/Chart/CategoryController.php index d2adca7078..2314d61c74 100644 --- a/app/Http/Controllers/Chart/CategoryController.php +++ b/app/Http/Controllers/Chart/CategoryController.php @@ -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); diff --git a/app/Http/Controllers/CronController.php b/app/Http/Controllers/CronController.php index a10531c1ae..a2ecd13e04 100644 --- a/app/Http/Controllers/CronController.php +++ b/app/Http/Controllers/CronController.php @@ -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,44 +53,11 @@ 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 '
'; - 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"; } 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"; + } + } + + } + } + } diff --git a/app/Repositories/Category/CategoryRepository.php b/app/Repositories/Category/CategoryRepository.php index bd5e0e83b8..534188bace 100644 --- a/app/Repositories/Category/CategoryRepository.php +++ b/app/Repositories/Category/CategoryRepository.php @@ -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,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); } ) - ->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. + '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( - [ - 'categories.*', - DB::Raw('DATE_FORMAT(`transaction_journals`.`date`,"%Y-%m") as `dateFormatted`'), - DB::Raw('SUM(`t_src`.`amount`) AS `spent`') - ] - ); + ->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`'), + DB::Raw('SUM(`t_src`.`amount`) AS `spent`') + ] + ); 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 - $single = 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( - [ - DB::Raw('SUM(`transactions`.`amount`) as `sum`)')] - ); + $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') + ->transactionTypes($types); + if (count($accountIds) > 0) { + $query->whereIn('transactions.account_id', $accountIds); + } + + + $single = $query->first( + [ + DB::Raw('SUM(`transactions`.`amount`) as `sum`') + ] + ); if (!is_null($single)) { return $single->sum; } diff --git a/app/Repositories/Category/CategoryRepositoryInterface.php b/app/Repositories/Category/CategoryRepositoryInterface.php index dd96800255..7de84fe660 100644 --- a/app/Repositories/Category/CategoryRepositoryInterface.php +++ b/app/Repositories/Category/CategoryRepositoryInterface.php @@ -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. *