Code cleanup.

This commit is contained in:
James Cole
2023-12-20 19:35:52 +01:00
parent c4f6366642
commit 64ec0cf62e
997 changed files with 12908 additions and 28136 deletions

View File

@@ -24,7 +24,6 @@ declare(strict_types=1);
namespace FireflyIII\Repositories\Bill;
use Carbon\Carbon;
use DB;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Factory\BillFactory;
use FireflyIII\Models\Attachment;
@@ -43,12 +42,9 @@ use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Query\JoinClause;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
use JsonException;
use Storage;
/**
* Class BillRepository.
*
*/
class BillRepository implements BillRepositoryInterface
{
@@ -56,9 +52,6 @@ class BillRepository implements BillRepositoryInterface
private User $user;
/**
* @inheritDoc
*/
public function billEndsWith(string $query, int $limit): Collection
{
$search = $this->user->bills();
@@ -66,14 +59,12 @@ class BillRepository implements BillRepositoryInterface
$search->where('name', 'LIKE', sprintf('%%%s', $query));
}
$search->orderBy('name', 'ASC')
->where('active', true);
->where('active', true)
;
return $search->take($limit)->get();
}
/**
* @inheritDoc
*/
public function billStartsWith(string $query, int $limit): Collection
{
$search = $this->user->bills();
@@ -81,7 +72,8 @@ class BillRepository implements BillRepositoryInterface
$search->where('name', 'LIKE', sprintf('%s%%', $query));
}
$search->orderBy('name', 'ASC')
->where('active', true);
->where('active', true)
;
return $search->take($limit)->get();
}
@@ -98,17 +90,10 @@ class BillRepository implements BillRepositoryInterface
$bill->order = $current;
$bill->save();
}
$current++;
++$current;
}
}
/**
* @param Bill $bill
*
* @return bool
*
*/
public function destroy(Bill $bill): bool
{
/** @var BillDestroyService $service */
@@ -118,9 +103,6 @@ class BillRepository implements BillRepositoryInterface
return true;
}
/**
* @inheritDoc
*/
public function destroyAll(): void
{
$this->user->bills()->delete();
@@ -128,11 +110,6 @@ class BillRepository implements BillRepositoryInterface
/**
* Find bill by parameters.
*
* @param int|null $billId
* @param string|null $billName
*
* @return Bill|null
*/
public function findBill(?int $billId, ?string $billName): ?Bill
{
@@ -159,10 +136,6 @@ class BillRepository implements BillRepositoryInterface
/**
* Find a bill by ID.
*
* @param int $billId
*
* @return Bill|null
*/
public function find(int $billId): ?Bill
{
@@ -171,10 +144,6 @@ class BillRepository implements BillRepositoryInterface
/**
* Find a bill by name.
*
* @param string $name
*
* @return Bill|null
*/
public function findByName(string $name): ?Bill
{
@@ -183,17 +152,13 @@ class BillRepository implements BillRepositoryInterface
/**
* Get all attachments.
*
* @param Bill $bill
*
* @return Collection
*/
public function getAttachments(Bill $bill): Collection
{
$set = $bill->attachments()->get();
/** @var Storage $disk */
$disk = Storage::disk('upload');
/** @var \Storage $disk */
$disk = \Storage::disk('upload');
return $set->each(
static function (Attachment $attachment) use ($disk) {
@@ -206,22 +171,15 @@ class BillRepository implements BillRepositoryInterface
);
}
/**
* @return Collection
*/
public function getBills(): Collection
{
return $this->user->bills()
->orderBy('order', 'ASC')
->orderBy('active', 'DESC')
->orderBy('name', 'ASC')->get();
->orderBy('order', 'ASC')
->orderBy('active', 'DESC')
->orderBy('name', 'ASC')->get()
;
}
/**
* @param Collection $accounts
*
* @return Collection
*/
public function getBillsForAccounts(Collection $accounts): Collection
{
$fields = [
@@ -243,32 +201,29 @@ class BillRepository implements BillRepositoryInterface
$ids = $accounts->pluck('id')->toArray();
return $this->user->bills()
->leftJoin(
'transaction_journals',
static function (JoinClause $join) {
$join->on('transaction_journals.bill_id', '=', 'bills.id')->whereNull('transaction_journals.deleted_at');
}
)
->leftJoin(
'transactions',
static function (JoinClause $join) {
$join->on('transaction_journals.id', '=', 'transactions.transaction_journal_id')->where('transactions.amount', '<', 0);
}
)
->whereIn('transactions.account_id', $ids)
->whereNull('transaction_journals.deleted_at')
->orderBy('bills.active', 'DESC')
->orderBy('bills.name', 'ASC')
->groupBy($fields)
->get($fields);
->leftJoin(
'transaction_journals',
static function (JoinClause $join) {
$join->on('transaction_journals.bill_id', '=', 'bills.id')->whereNull('transaction_journals.deleted_at');
}
)
->leftJoin(
'transactions',
static function (JoinClause $join) {
$join->on('transaction_journals.id', '=', 'transactions.transaction_journal_id')->where('transactions.amount', '<', 0);
}
)
->whereIn('transactions.account_id', $ids)
->whereNull('transaction_journals.deleted_at')
->orderBy('bills.active', 'DESC')
->orderBy('bills.name', 'ASC')
->groupBy($fields)
->get($fields)
;
}
/**
* Get all bills with these ID's.
*
* @param array $billIds
*
* @return Collection
*/
public function getByIds(array $billIds): Collection
{
@@ -277,24 +232,15 @@ class BillRepository implements BillRepositoryInterface
/**
* Get text or return empty string.
*
* @param Bill $bill
*
* @return string
*/
public function getNoteText(Bill $bill): string
{
/** @var Note|null $note */
/** @var null|Note $note */
$note = $bill->notes()->first();
return (string)$note?->text;
return (string)$note?->text;
}
/**
* @param Bill $bill
*
* @return array
*/
public function getOverallAverage(Bill $bill): array
{
/** @var JournalRepositoryInterface $repos */
@@ -321,7 +267,7 @@ class BillRepository implements BillRepositoryInterface
'currency_decimal_places' => $currency->decimal_places,
];
$result[$currencyId]['sum'] = bcadd($result[$currencyId]['sum'], $transaction->amount);
$result[$currencyId]['count']++;
++$result[$currencyId]['count'];
}
// after loop, re-loop for avg.
@@ -336,65 +282,50 @@ class BillRepository implements BillRepositoryInterface
return $result;
}
/**
* @param User|Authenticatable|null $user
*/
public function setUser(User | Authenticatable | null $user): void
public function setUser(null|Authenticatable|User $user): void
{
if ($user instanceof User) {
$this->user = $user;
}
}
/**
* @param int $size
*
* @return LengthAwarePaginator
*/
public function getPaginator(int $size): LengthAwarePaginator
{
return $this->user->bills()
->orderBy('active', 'DESC')
->orderBy('name', 'ASC')->paginate($size);
->orderBy('active', 'DESC')
->orderBy('name', 'ASC')->paginate($size)
;
}
/**
* The "paid dates" list is a list of dates of transaction journals that are linked to this bill.
*
* @param Bill $bill
* @param Carbon $start
* @param Carbon $end
*
* @return Collection
*/
public function getPaidDatesInRange(Bill $bill, Carbon $start, Carbon $end): Collection
{
//app('log')->debug('Now in getPaidDatesInRange()');
// app('log')->debug('Now in getPaidDatesInRange()');
return $bill->transactionJournals()
->before($end)->after($start)->get(
[
->before($end)->after($start)->get(
[
'transaction_journals.id',
'transaction_journals.date',
'transaction_journals.transaction_group_id',
]
);
)
;
}
/**
* Return all rules for one bill
*
* @param Bill $bill
*
* @return Collection
*/
public function getRulesForBill(Bill $bill): Collection
{
return $this->user->rules()
->leftJoin('rule_actions', 'rule_actions.rule_id', '=', 'rules.id')
->where('rule_actions.action_type', 'link_to_bill')
->where('rule_actions.action_value', $bill->name)
->get(['rules.*']);
->leftJoin('rule_actions', 'rule_actions.rule_id', '=', 'rules.id')
->where('rule_actions.action_type', 'link_to_bill')
->where('rule_actions.action_value', $bill->name)
->get(['rules.*'])
;
}
/**
@@ -402,18 +333,16 @@ class BillRepository implements BillRepositoryInterface
* 5= billid
*
* 5 => [['id' => 1, 'title' => 'Some rule'],['id' => 2, 'title' => 'Some other rule']]
*
* @param Collection $collection
*
* @return array
*/
public function getRulesForBills(Collection $collection): array
{
$rules = $this->user->rules()
->leftJoin('rule_actions', 'rule_actions.rule_id', '=', 'rules.id')
->where('rule_actions.action_type', 'link_to_bill')
->get(['rules.id', 'rules.title', 'rule_actions.action_value', 'rules.active']);
->leftJoin('rule_actions', 'rule_actions.rule_id', '=', 'rules.id')
->where('rule_actions.action_type', 'link_to_bill')
->get(['rules.id', 'rules.title', 'rule_actions.action_value', 'rules.active'])
;
$array = [];
/** @var Rule $rule */
foreach ($rules as $rule) {
$array[$rule->action_value] ??= [];
@@ -427,12 +356,6 @@ class BillRepository implements BillRepositoryInterface
return $return;
}
/**
* @param Bill $bill
* @param Carbon $date
*
* @return array
*/
public function getYearAverage(Bill $bill, Carbon $date): array
{
/** @var JournalRepositoryInterface $repos */
@@ -443,13 +366,14 @@ class BillRepository implements BillRepositoryInterface
$result = [];
$journals = $bill->transactionJournals()
->where('date', '>=', $date->year . '-01-01 00:00:00')
->where('date', '<=', $date->year . '-12-31 23:59:59')
->get();
->where('date', '>=', $date->year.'-01-01 00:00:00')
->where('date', '<=', $date->year.'-12-31 23:59:59')
->get()
;
/** @var TransactionJournal $journal */
foreach ($journals as $journal) {
/** @var Transaction|null $transaction */
/** @var null|Transaction $transaction */
$transaction = $journal->transactions()->where('amount', '<', 0)->first();
if (null === $transaction) {
continue;
@@ -466,7 +390,7 @@ class BillRepository implements BillRepositoryInterface
'currency_decimal_places' => $currency->decimal_places,
];
$result[$currencyId]['sum'] = bcadd($result[$currencyId]['sum'], $transaction->amount);
$result[$currencyId]['count']++;
++$result[$currencyId]['count'];
}
// after loop, re-loop for avg.
@@ -483,9 +407,6 @@ class BillRepository implements BillRepositoryInterface
/**
* Link a set of journals to a bill.
*
* @param Bill $bill
* @param array $transactions
*/
public function linkCollectionToBill(Bill $bill, array $transactions): void
{
@@ -501,11 +422,7 @@ class BillRepository implements BillRepositoryInterface
/**
* Given the date in $date, this method will return a moment in the future where the bill is expected to be paid.
*
* @param Bill $bill
* @param Carbon $date
*
* @return Carbon
* @throws JsonException
* @throws \JsonException
*/
public function nextExpectedMatch(Bill $bill, Carbon $date): Carbon
{
@@ -518,12 +435,12 @@ class BillRepository implements BillRepositoryInterface
}
// find the most recent date for this bill NOT in the future. Cache this date:
$start = clone $bill->date;
app('log')->debug('nextExpectedMatch: Start is ' . $start->format('Y-m-d'));
app('log')->debug('nextExpectedMatch: Start is '.$start->format('Y-m-d'));
while ($start < $date) {
app('log')->debug(sprintf('$start (%s) < $date (%s)', $start->format('Y-m-d'), $date->format('Y-m-d')));
$start = app('navigation')->addPeriod($start, $bill->repeat_freq, $bill->skip);
app('log')->debug('Start is now ' . $start->format('Y-m-d'));
app('log')->debug('Start is now '.$start->format('Y-m-d'));
}
$end = app('navigation')->addPeriod($start, $bill->repeat_freq, $bill->skip);
@@ -537,8 +454,8 @@ class BillRepository implements BillRepositoryInterface
$start = clone $end;
$end = app('navigation')->addPeriod($start, $bill->repeat_freq, $bill->skip);
}
app('log')->debug('nextExpectedMatch: Final start is ' . $start->format('Y-m-d'));
app('log')->debug('nextExpectedMatch: Matching end is ' . $end->format('Y-m-d'));
app('log')->debug('nextExpectedMatch: Final start is '.$start->format('Y-m-d'));
app('log')->debug('nextExpectedMatch: Matching end is '.$end->format('Y-m-d'));
$cache->store($start);
@@ -546,11 +463,8 @@ class BillRepository implements BillRepositoryInterface
}
/**
* @param array $data
*
* @return Bill
* @throws FireflyException
* @throws JsonException
* @throws \JsonException
*/
public function store(array $data): Bill
{
@@ -561,9 +475,6 @@ class BillRepository implements BillRepositoryInterface
return $factory->create($data);
}
/**
* @inheritDoc
*/
public function removeObjectGroup(Bill $bill): Bill
{
$bill->objectGroups()->sync([]);
@@ -571,12 +482,6 @@ class BillRepository implements BillRepositoryInterface
return $bill;
}
/**
* @param string $query
* @param int $limit
*
* @return Collection
*/
public function searchBill(string $query, int $limit): Collection
{
$query = sprintf('%%%s%%', $query);
@@ -584,9 +489,6 @@ class BillRepository implements BillRepositoryInterface
return $this->user->bills()->where('name', 'LIKE', $query)->take($limit)->get();
}
/**
* @inheritDoc
*/
public function setObjectGroup(Bill $bill, string $objectGroupTitle): Bill
{
$objectGroup = $this->findOrCreateObjectGroup($objectGroupTitle);
@@ -597,22 +499,17 @@ class BillRepository implements BillRepositoryInterface
return $bill;
}
/**
* @inheritDoc
*/
public function setOrder(Bill $bill, int $order): void
{
$bill->order = $order;
$bill->save();
}
/**
* @inheritDoc
*/
public function sumPaidInRange(Carbon $start, Carbon $end): array
{
$bills = $this->getActiveBills();
$return = [];
/** @var Bill $bill */
foreach ($bills as $bill) {
/** @var Collection $set */
@@ -630,7 +527,7 @@ class BillRepository implements BillRepositoryInterface
/** @var TransactionJournal $transactionJournal */
foreach ($set as $transactionJournal) {
/** @var Transaction|null $sourceTransaction */
/** @var null|Transaction $sourceTransaction */
$sourceTransaction = $transactionJournal->transactions()->where('amount', '<', 0)->first();
if (null !== $sourceTransaction) {
$amount = $sourceTransaction->amount;
@@ -642,28 +539,25 @@ class BillRepository implements BillRepositoryInterface
}
}
}
return $return;
}
/**
* @return Collection
*/
public function getActiveBills(): Collection
{
return $this->user->bills()
->where('active', true)
->orderBy('bills.name', 'ASC')
->get(['bills.*', DB::raw('((bills.amount_min + bills.amount_max) / 2) AS expectedAmount'),]); // @phpstan-ignore-line
->where('active', true)
->orderBy('bills.name', 'ASC')
->get(['bills.*', \DB::raw('((bills.amount_min + bills.amount_max) / 2) AS expectedAmount')]) // @phpstan-ignore-line
;
}
/**
* @inheritDoc
*/
public function sumUnpaidInRange(Carbon $start, Carbon $end): array
{
app('log')->debug(sprintf('Now in sumUnpaidInRange("%s", "%s")', $start->format('Y-m-d'), $end->format('Y-m-d')));
$bills = $this->getActiveBills();
$return = [];
/** @var Bill $bill */
foreach ($bills as $bill) {
app('log')->debug(sprintf('Processing bill #%d ("%s")', $bill->id, $bill->name));
@@ -693,32 +587,26 @@ class BillRepository implements BillRepositoryInterface
/**
* Between start and end, tells you on which date(s) the bill is expected to hit.
*
* @param Bill $bill
* @param Carbon $start
* @param Carbon $end
*
* @return Collection
*/
public function getPayDatesInRange(Bill $bill, Carbon $start, Carbon $end): Collection
{
$set = new Collection();
$currentStart = clone $start;
//app('log')->debug(sprintf('Now at bill "%s" (%s)', $bill->name, $bill->repeat_freq));
//app('log')->debug(sprintf('First currentstart is %s', $currentStart->format('Y-m-d')));
// app('log')->debug(sprintf('Now at bill "%s" (%s)', $bill->name, $bill->repeat_freq));
// app('log')->debug(sprintf('First currentstart is %s', $currentStart->format('Y-m-d')));
while ($currentStart <= $end) {
//app('log')->debug(sprintf('Currentstart is now %s.', $currentStart->format('Y-m-d')));
// app('log')->debug(sprintf('Currentstart is now %s.', $currentStart->format('Y-m-d')));
$nextExpectedMatch = $this->nextDateMatch($bill, $currentStart);
//app('log')->debug(sprintf('Next Date match after %s is %s', $currentStart->format('Y-m-d'), $nextExpectedMatch->format('Y-m-d')));
// app('log')->debug(sprintf('Next Date match after %s is %s', $currentStart->format('Y-m-d'), $nextExpectedMatch->format('Y-m-d')));
if ($nextExpectedMatch > $end) {// If nextExpectedMatch is after end, we continue
break;
}
$set->push(clone $nextExpectedMatch);
//app('log')->debug(sprintf('Now %d dates in set.', $set->count()));
// app('log')->debug(sprintf('Now %d dates in set.', $set->count()));
$nextExpectedMatch->addDay();
//app('log')->debug(sprintf('Currentstart (%s) has become %s.', $currentStart->format('Y-m-d'), $nextExpectedMatch->format('Y-m-d')));
// app('log')->debug(sprintf('Currentstart (%s) has become %s.', $currentStart->format('Y-m-d'), $nextExpectedMatch->format('Y-m-d')));
$currentStart = clone $nextExpectedMatch;
}
@@ -729,11 +617,6 @@ class BillRepository implements BillRepositoryInterface
/**
* Given a bill and a date, this method will tell you at which moment this bill expects its next
* transaction. Whether or not it is there already, is not relevant.
*
* @param Bill $bill
* @param Carbon $date
*
* @return Carbon
*/
public function nextDateMatch(Bill $bill, Carbon $date): Carbon
{
@@ -755,21 +638,14 @@ class BillRepository implements BillRepositoryInterface
return $start;
}
/**
* @param Bill $bill
*/
public function unlinkAll(Bill $bill): void
{
$this->user->transactionJournals()->where('bill_id', $bill->id)->update(['bill_id' => null]);
}
/**
* @param Bill $bill
* @param array $data
*
* @return Bill
* @throws FireflyException
* @throws JsonException
* @throws \JsonException
*/
public function update(Bill $bill, array $data): Bill
{