New stuff for categories and transactions.

This commit is contained in:
James Cole
2016-05-08 13:45:23 +02:00
parent 3588bd881c
commit 98e683329e
15 changed files with 1146 additions and 849 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -4,6 +4,8 @@ declare(strict_types = 1);
namespace FireflyIII\Repositories\Category;
use Carbon\Carbon;
use FireflyIII\Models\Category;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
/**
@@ -15,18 +17,149 @@ interface CategoryRepositoryInterface
{
// /**
// * Returns a collection of Categories appended with the amount of money that has been earned
// * 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 "earned".
// *
// * @param $accounts
// * @param $start
// * @param $end
// *
// * @return Collection
// */
// public function earnedForAccountsPerMonth(Collection $accounts, Carbon $start, Carbon $end): Collection;
/**
* Returns a collection of Categories appended with the amount of money that has been earned
* 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 "earned".
* @param Category $category
*
* @param $accounts
* @param $start
* @param $end
*
* @return Collection
* @return bool
*/
public function earnedForAccountsPerMonth(Collection $accounts, Carbon $start, Carbon $end): Collection;
public function destroy(Category $category): bool;
// /**
// * This method returns a very special collection for each category:
// *
// * category, year, expense/earned, amount
// *
// * categories can be duplicated.
// *
// * @param Collection $categories
// * @param Collection $accounts
// * @param Carbon $start
// * @param Carbon $end
// *
// * @return Collection
// */
// public function listMultiYear(Collection $categories, Collection $accounts, Carbon $start, Carbon $end): Collection;
// /**
// * Returns a list of transaction journals in the range (all types, all accounts) that have no category
// * associated to them.
// *
// * @param Carbon $start
// * @param Carbon $end
// *
// * @return Collection
// */
// public function listNoCategory(Carbon $start, Carbon $end): Collection;
// /**
// * 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".
// *
// * @param $accounts
// * @param $start
// * @param $end
// *
// * @return Collection
// */
// public function spentForAccountsPerMonth(Collection $accounts, Carbon $start, Carbon $end): Collection;
// /**
// * Returns the total amount of money related to transactions without any category connected to
// * it. Returns either the earned amount.
// *
// * @param Collection $accounts
// * @param Carbon $start
// * @param Carbon $end
// *
// * @return string
// */
// public function sumEarnedNoCategory(Collection $accounts, Carbon $start, Carbon $end): string;
// /**
// * Returns the total amount of money related to transactions without any category connected to
// * it. Returns either the spent amount.
// *
// * @param Collection $accounts
// * @param Carbon $start
// * @param Carbon $end
// *
// * @return string
// */
// public function sumSpentNoCategory(Collection $accounts, Carbon $start, Carbon $end): string;
// /**
// * @param Category $category
// * @param Carbon|null $start
// * @param Carbon|null $end
// *
// * @return int
// */
// public function countJournals(Category $category, Carbon $start = null, Carbon $end = null): int;
/**
* @param Collection $categories
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return string
*/
public function earnedInPeriod(Collection $categories, Collection $accounts, Carbon $start, Carbon $end): string;
// /**
// * Returns an array with the following key:value pairs:
// *
// * yyyy-mm-dd:<amount>
// *
// * Where yyyy-mm-dd is the date and <amount> is the money earned using DEPOSITS in the $category
// * from all the users accounts.
// *
// * @param Category $category
// * @param Carbon $start
// * @param Carbon $end
// * @param Collection $accounts
// *
// * @return array
// */
// public function earnedPerDay(Category $category, Carbon $start, Carbon $end, Collection $accounts): array;
/**
* Find a category
*
* @param int $categoryId
*
* @return Category
*/
public function find(int $categoryId) : Category;
// /**
// * @param Category $category
// *
// * @return Carbon
// */
/**
* @param Category $category
* @param Collection $accounts
*
* @return Carbon
*/
public function firstUseDate(Category $category, Collection $accounts): Carbon;
/**
* Returns a list of all the categories belonging to a user.
@@ -36,67 +169,119 @@ interface CategoryRepositoryInterface
public function getCategories(): Collection;
/**
* This method returns a very special collection for each category:
* @param Category $category
* @param int $page
* @param int $pageSize
*
* category, year, expense/earned, amount
* @return LengthAwarePaginator
*/
public function getJournals(Category $category, int $page, int $pageSize): LengthAwarePaginator;
/**
* @param Collection $categories
* @param Collection $accounts
* @param array $types
* @param Carbon $start
* @param Carbon $end
*
* categories can be duplicated.
* @return Collection
*/
public function journalsInPeriod(Collection $categories, Collection $accounts, array $types, Carbon $start, Carbon $end): Collection;
/**
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return Collection
*/
public function journalsInPeriodWithoutCategory(Collection $accounts, Carbon $start, Carbon $end) : Collection;
/**
* @param Category $category
* @param Collection $accounts
*
* @return Carbon
*/
public function lastUseDate(Category $category, Collection $accounts): Carbon;
/**
* @param Collection $categories
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return Collection
*/
public function listMultiYear(Collection $categories, Collection $accounts, Carbon $start, Carbon $end): Collection;
/**
* Returns a list of transaction journals in the range (all types, all accounts) that have no category
* associated to them.
*
* @param Carbon $start
* @param Carbon $end
*
* @return Collection
*/
public function listNoCategory(Carbon $start, Carbon $end): Collection;
/**
* 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".
*
* @param $accounts
* @param $start
* @param $end
*
* @return Collection
*/
public function spentForAccountsPerMonth(Collection $accounts, Carbon $start, Carbon $end): Collection;
/**
* Returns the total amount of money related to transactions without any category connected to
* it. Returns either the earned amount.
*
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return string
*/
public function sumEarnedNoCategory(Collection $accounts, Carbon $start, Carbon $end): string;
public function spentInPeriod(Collection $categories, Collection $accounts, Carbon $start, Carbon $end): string;
// /**
// * @param Category $category
// * @param int $page
// * @param int $pageSize
// *
// * @return Collection
// */
// public function getJournals(Category $category, int $page, int $pageSize = 50): Collection;
// /**
// * @param Category $category
// * @param Collection $accounts
// *
// * @param Carbon $start
// * @param Carbon $end
// *
// * @return Collection
// */
// public function getJournalsForAccountsInRange(Category $category, Collection $accounts, Carbon $start, Carbon $end): Collection;
// /**
// * @param Category $category
// * @param Carbon $start
// * @param Carbon $end
// * @param int $page
// * @param int $pageSize
// *
// *
// * @return Collection
// */
// public function getJournalsInRange(Category $category, Carbon $start, Carbon $end, int $page, int $pageSize = 50): Collection;
// /**
// * @param Category $category
// *
// * @return Carbon
// */
// public function getLatestActivity(Category $category): Carbon;
// /**
// * Returns an array with the following key:value pairs:
// *
// * yyyy-mm-dd:<amount>
// *
// * Where yyyy-mm-dd is the date and <amount> is the money spent using WITHDRAWALS in the $category
// * from all the users accounts.
// *
// * @param Category $category
// * @param Carbon $start
// * @param Carbon $end
// * @param Collection $accounts
// *
// * @return array
// */
// public function spentPerDay(Category $category, Carbon $start, Carbon $end, Collection $accounts): array;
/**
* Returns the total amount of money related to transactions without any category connected to
* it. Returns either the spent amount.
* @param array $data
*
* @param Collection $accounts
* @param Carbon $start
* @param Carbon $end
*
* @return string
* @return Category
*/
public function sumSpentNoCategory(Collection $accounts, Carbon $start, Carbon $end): string;
public function store(array $data): Category;
/**
* @param Category $category
* @param array $data
*
* @return Category
*/
public function update(Category $category, array $data): Category;
}

View File

@@ -1,322 +0,0 @@
<?php
declare(strict_types = 1);
namespace FireflyIII\Repositories\Category;
use Carbon\Carbon;
use DB;
use FireflyIII\Models\Category;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Shared\ComponentRepository;
use FireflyIII\User;
use Illuminate\Database\Query\JoinClause;
use Illuminate\Support\Collection;
/**
* Class SingleCategoryRepository
*
* @package FireflyIII\Repositories\Category
*/
class SingleCategoryRepository extends ComponentRepository implements SingleCategoryRepositoryInterface
{
/** @var User */
private $user;
/**
* SingleCategoryRepository constructor.
*
* @param User $user
*/
public function __construct(User $user)
{
$this->user = $user;
}
/**
* @param Category $category
* @param Carbon|null $start
* @param Carbon|null $end
*
* @return int
*/
public function countJournals(Category $category, Carbon $start = null, Carbon $end = null): int
{
$query = $category->transactionjournals();
if (!is_null($start)) {
$query->after($start);
}
if (!is_null($end)) {
$query->before($end);
}
return $query->count();
}
/**
* @param Category $category
*
* @return bool
*/
public function destroy(Category $category): bool
{
$category->delete();
return true;
}
/**
* Returns an array with the following key:value pairs:
*
* yyyy-mm-dd:<amount>
*
* Where yyyy-mm-dd is the date and <amount> is the money earned using DEPOSITS in the $category
* from all the users $accounts.
*
* @param Category $category
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return array
*/
public function earnedPerDay(Category $category, Carbon $start, Carbon $end, Collection $accounts): array
{
/** @var Collection $query */
$query = $category->transactionjournals()
->expanded()
->transactionTypes([TransactionType::DEPOSIT])
->before($end)
->after($start)
->groupBy('transaction_journals.date');
$query->leftJoin(
'transactions as destination', function (JoinClause $join) {
$join->on('destination.transaction_journal_id', '=', 'transaction_journals.id')->where('destination.amount', '>', 0);
}
);
if ($accounts->count() > 0) {
$ids = $accounts->pluck('id')->toArray();
$query->whereIn('destination.account.id', $ids);
}
$result = $query->get(['transaction_journals.date as dateFormatted', DB::raw('SUM(`destination`.`amount`) AS `sum`')]);
$return = [];
foreach ($result->toArray() as $entry) {
$return[$entry['dateFormatted']] = $entry['sum'];
}
return $return;
}
/**
* Find a category
*
* @param int $categoryId
*
* @return Category
*/
public function find(int $categoryId) : Category
{
$category = $this->user->categories()->find($categoryId);
if (is_null($category)) {
$category = new Category;
}
return $category;
}
/**
* @param Category $category
*
* @return Carbon
*/
public function getFirstActivityDate(Category $category): Carbon
{
/** @var TransactionJournal $first */
$first = $category->transactionjournals()->orderBy('date', 'ASC')->first();
if ($first) {
return $first->date;
}
return new Carbon;
}
/**
* @param Category $category
* @param int $page
* @param int $pageSize
*
* @return Collection
*/
public function getJournals(Category $category, int $page, int $pageSize = 50): Collection
{
$offset = $page > 0 ? $page * $pageSize : 0;
return $category->transactionjournals()->expanded()->take($pageSize)->offset($offset)->get(TransactionJournal::queryFields());
}
/**
* @param Category $category
* @param Collection $accounts
*
* @param Carbon $start
* @param Carbon $end
*
* @return Collection
*/
public function getJournalsForAccountsInRange(Category $category, Collection $accounts, Carbon $start, Carbon $end): Collection
{
$ids = $accounts->pluck('id')->toArray();
return $category->transactionjournals()
->after($start)
->before($end)
->expanded()
->whereIn('source_account.id', $ids)
->whereNotIn('destination_account.id', $ids)
->get(TransactionJournal::queryFields());
}
/**
* @param Category $category
* @param Carbon $start
* @param Carbon $end
* @param int $page
* @param int $pageSize
*
*
* @return Collection
*/
public function getJournalsInRange(Category $category, Carbon $start, Carbon $end, int $page, int $pageSize = 50): Collection
{
$offset = $page > 0 ? $page * $pageSize : 0;
return $category->transactionjournals()
->after($start)
->before($end)
->expanded()
->take($pageSize)
->offset($offset)
->get(TransactionJournal::queryFields());
}
/**
* @param Category $category
*
* @return Carbon
*/
public function getLatestActivity(Category $category): Carbon
{
$first = new Carbon('1900-01-01');
$second = new Carbon('1900-01-01');
$latest = $category->transactionjournals()
->orderBy('transaction_journals.date', 'DESC')
->orderBy('transaction_journals.order', 'ASC')
->orderBy('transaction_journals.id', 'DESC')
->first();
if ($latest) {
$first = $latest->date;
}
// could also be a transaction, nowadays:
$latestTransaction = $category->transactions()
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
->orderBy('transaction_journals.date', 'DESC')
->orderBy('transaction_journals.order', 'ASC')
->orderBy('transaction_journals.id', 'DESC')
->first(['transactions.*', 'transaction_journals.date']);
if ($latestTransaction) {
$second = new Carbon($latestTransaction->date);
}
if ($first > $second) {
return $first;
}
return $second;
}
/**
* Returns an array with the following key:value pairs:
*
* yyyy-mm-dd:<amount>
*
* Where yyyy-mm-dd is the date and <amount> is the money spent using DEPOSITS in the $category
* from all the users accounts.
*
* @param Category $category
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return array
*/
public function spentPerDay(Category $category, Carbon $start, Carbon $end, Collection $accounts): array
{
/** @var Collection $query */
$query = $category->transactionjournals()
->expanded()
->transactionTypes([TransactionType::WITHDRAWAL])
->before($end)
->after($start)
->groupBy('transaction_journals.date');
$query->leftJoin(
'transactions as source', function (JoinClause $join) {
$join->on('source.transaction_journal_id', '=', 'transaction_journals.id')->where('source.amount', '<', 0);
}
);
if ($accounts->count() > 0) {
$ids = $accounts->pluck('id')->toArray();
$query->whereIn('source.account_id', $ids);
}
$result = $query->get(['transaction_journals.date as dateFormatted', DB::raw('SUM(`source`.`amount`) AS `sum`')]);
$return = [];
foreach ($result->toArray() as $entry) {
$return[$entry['dateFormatted']] = $entry['sum'];
}
return $return;
}
/**
* @param array $data
*
* @return Category
*/
public function store(array $data): Category
{
$newCategory = Category::firstOrCreateEncrypted(
[
'user_id' => $data['user'],
'name' => $data['name'],
]
);
$newCategory->save();
return $newCategory;
}
/**
* @param Category $category
* @param array $data
*
* @return Category
*/
public function update(Category $category, array $data): Category
{
// update the account:
$category->name = $data['name'];
$category->save();
return $category;
}
}

View File

@@ -1,138 +0,0 @@
<?php
declare(strict_types = 1);
namespace FireflyIII\Repositories\Category;
use Carbon\Carbon;
use FireflyIII\Models\Category;
use Illuminate\Support\Collection;
/**
* Interface SingleCategoryRepositoryInterface
*
* @package FireflyIII\Repositories\Category
*/
interface SingleCategoryRepositoryInterface
{
/**
* @param Category $category
* @param Carbon|null $start
* @param Carbon|null $end
*
* @return int
*/
public function countJournals(Category $category, Carbon $start = null, Carbon $end = null): int;
/**
* @param Category $category
*
* @return bool
*/
public function destroy(Category $category): bool;
/**
* Returns an array with the following key:value pairs:
*
* yyyy-mm-dd:<amount>
*
* Where yyyy-mm-dd is the date and <amount> is the money earned using DEPOSITS in the $category
* from all the users accounts.
*
* @param Category $category
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return array
*/
public function earnedPerDay(Category $category, Carbon $start, Carbon $end, Collection $accounts): array;
/**
* Find a category
*
* @param int $categoryId
*
* @return Category
*/
public function find(int $categoryId) : Category;
/**
* @param Category $category
*
* @return Carbon
*/
public function getFirstActivityDate(Category $category): Carbon;
/**
* @param Category $category
* @param int $page
* @param int $pageSize
*
* @return Collection
*/
public function getJournals(Category $category, int $page, int $pageSize = 50): Collection;
/**
* @param Category $category
* @param Collection $accounts
*
* @param Carbon $start
* @param Carbon $end
*
* @return Collection
*/
public function getJournalsForAccountsInRange(Category $category, Collection $accounts, Carbon $start, Carbon $end): Collection;
/**
* @param Category $category
* @param Carbon $start
* @param Carbon $end
* @param int $page
* @param int $pageSize
*
*
* @return Collection
*/
public function getJournalsInRange(Category $category, Carbon $start, Carbon $end, int $page, int $pageSize = 50): Collection;
/**
* @param Category $category
*
* @return Carbon
*/
public function getLatestActivity(Category $category): Carbon;
/**
* Returns an array with the following key:value pairs:
*
* yyyy-mm-dd:<amount>
*
* Where yyyy-mm-dd is the date and <amount> is the money spent using WITHDRAWALS in the $category
* from all the users accounts.
*
* @param Category $category
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
*
* @return array
*/
public function spentPerDay(Category $category, Carbon $start, Carbon $end, Collection $accounts): array;
/**
* @param array $data
*
* @return Category
*/
public function store(array $data): Category;
/**
* @param Category $category
* @param array $data
*
* @return Category
*/
public function update(Category $category, array $data): Category;
}