Catch null pointer

This commit is contained in:
James Cole
2020-11-11 19:14:05 +01:00
parent ae9a9c1647
commit 80718baa16

View File

@@ -92,7 +92,7 @@ class BillRepository implements BillRepositoryInterface
public function findBill(?int $billId, ?string $billName): ?Bill public function findBill(?int $billId, ?string $billName): ?Bill
{ {
if (null !== $billId) { if (null !== $billId) {
$searchResult = $this->find((int) $billId); $searchResult = $this->find((int)$billId);
if (null !== $searchResult) { if (null !== $searchResult) {
Log::debug(sprintf('Found bill based on #%d, will return it.', $billId)); Log::debug(sprintf('Found bill based on #%d, will return it.', $billId));
@@ -100,7 +100,7 @@ class BillRepository implements BillRepositoryInterface
} }
} }
if (null !== $billName) { if (null !== $billName) {
$searchResult = $this->findByName((string) $billName); $searchResult = $this->findByName((string)$billName);
if (null !== $searchResult) { if (null !== $searchResult) {
Log::debug(sprintf('Found bill based on "%s", will return it.', $billName)); Log::debug(sprintf('Found bill based on "%s", will return it.', $billName));
@@ -193,25 +193,26 @@ class BillRepository implements BillRepositoryInterface
$fields = ['bills.id', 'bills.created_at', 'bills.updated_at', 'bills.deleted_at', 'bills.user_id', 'bills.name', 'bills.amount_min', $fields = ['bills.id', 'bills.created_at', 'bills.updated_at', 'bills.deleted_at', 'bills.user_id', 'bills.name', 'bills.amount_min',
'bills.amount_max', 'bills.date', 'bills.transaction_currency_id', 'bills.repeat_freq', 'bills.skip', 'bills.automatch', 'bills.active',]; 'bills.amount_max', 'bills.date', 'bills.transaction_currency_id', 'bills.repeat_freq', 'bills.skip', 'bills.automatch', 'bills.active',];
$ids = $accounts->pluck('id')->toArray(); $ids = $accounts->pluck('id')->toArray();
return $this->user->bills() return $this->user->bills()
->leftJoin( ->leftJoin(
'transaction_journals', 'transaction_journals',
static function (JoinClause $join) { static function (JoinClause $join) {
$join->on('transaction_journals.bill_id', '=', 'bills.id')->whereNull('transaction_journals.deleted_at'); $join->on('transaction_journals.bill_id', '=', 'bills.id')->whereNull('transaction_journals.deleted_at');
} }
) )
->leftJoin( ->leftJoin(
'transactions', 'transactions',
static function (JoinClause $join) { static function (JoinClause $join) {
$join->on('transaction_journals.id', '=', 'transactions.transaction_journal_id')->where('transactions.amount', '<', 0); $join->on('transaction_journals.id', '=', 'transactions.transaction_journal_id')->where('transactions.amount', '<', 0);
} }
) )
->whereIn('transactions.account_id', $ids) ->whereIn('transactions.account_id', $ids)
->whereNull('transaction_journals.deleted_at') ->whereNull('transaction_journals.deleted_at')
->orderBy('bills.active', 'DESC') ->orderBy('bills.active', 'DESC')
->orderBy('bills.name', 'ASC') ->orderBy('bills.name', 'ASC')
->groupBy($fields) ->groupBy($fields)
->get($fields); ->get($fields);
} }
/** /**
@@ -234,7 +235,7 @@ class BillRepository implements BillRepositoryInterface
$set = $bill->transactionJournals()->after($start)->before($end)->get(['transaction_journals.*']); $set = $bill->transactionJournals()->after($start)->before($end)->get(['transaction_journals.*']);
if ($set->count() > 0) { if ($set->count() > 0) {
$journalIds = $set->pluck('id')->toArray(); $journalIds = $set->pluck('id')->toArray();
$amount = (string) Transaction::whereIn('transaction_journal_id', $journalIds)->where('amount', '<', 0)->sum('amount'); $amount = (string)Transaction::whereIn('transaction_journal_id', $journalIds)->where('amount', '<', 0)->sum('amount');
$sum = bcadd($sum, $amount); $sum = bcadd($sum, $amount);
Log::debug(sprintf('Total > 0, so add to sum %f, which becomes %f', $amount, $sum)); Log::debug(sprintf('Total > 0, so add to sum %f, which becomes %f', $amount, $sum));
} }
@@ -260,10 +261,10 @@ class BillRepository implements BillRepositoryInterface
foreach ($bills as $bill) { foreach ($bills as $bill) {
/** @var Collection $set */ /** @var Collection $set */
$set = $bill->transactionJournals()->after($start)->before($end)->get(['transaction_journals.*']); $set = $bill->transactionJournals()->after($start)->before($end)->get(['transaction_journals.*']);
$currencyId = (int) $bill->transaction_currency_id; $currencyId = (int)$bill->transaction_currency_id;
if ($set->count() > 0) { if ($set->count() > 0) {
$journalIds = $set->pluck('id')->toArray(); $journalIds = $set->pluck('id')->toArray();
$amount = (string) Transaction::whereIn('transaction_journal_id', $journalIds)->where('amount', '<', 0)->sum('amount'); $amount = (string)Transaction::whereIn('transaction_journal_id', $journalIds)->where('amount', '<', 0)->sum('amount');
$return[$currencyId] = $return[$currencyId] ?? '0'; $return[$currencyId] = $return[$currencyId] ?? '0';
$return[$currencyId] = bcadd($amount, $return[$currencyId]); $return[$currencyId] = bcadd($amount, $return[$currencyId]);
Log::debug(sprintf('Total > 0, so add to sum %f, which becomes %f (currency %d)', $amount, $return[$currencyId], $currencyId)); Log::debug(sprintf('Total > 0, so add to sum %f, which becomes %f (currency %d)', $amount, $return[$currencyId], $currencyId));
@@ -296,7 +297,7 @@ class BillRepository implements BillRepositoryInterface
if ($total > 0) { if ($total > 0) {
$average = bcdiv(bcadd($bill->amount_max, $bill->amount_min), '2'); $average = bcdiv(bcadd($bill->amount_max, $bill->amount_min), '2');
$multi = bcmul($average, (string) $total); $multi = bcmul($average, (string)$total);
$sum = bcadd($sum, $multi); $sum = bcadd($sum, $multi);
Log::debug(sprintf('Total > 0, so add to sum %f, which becomes %f', $multi, $sum)); Log::debug(sprintf('Total > 0, so add to sum %f, which becomes %f', $multi, $sum));
} }
@@ -323,13 +324,13 @@ class BillRepository implements BillRepositoryInterface
$dates = $this->getPayDatesInRange($bill, $start, $end); $dates = $this->getPayDatesInRange($bill, $start, $end);
$count = $bill->transactionJournals()->after($start)->before($end)->count(); $count = $bill->transactionJournals()->after($start)->before($end)->count();
$total = $dates->count() - $count; $total = $dates->count() - $count;
$currencyId = (int) $bill->transaction_currency_id; $currencyId = (int)$bill->transaction_currency_id;
Log::debug(sprintf('Dates = %d, journalCount = %d, total = %d', $dates->count(), $count, $total)); Log::debug(sprintf('Dates = %d, journalCount = %d, total = %d', $dates->count(), $count, $total));
if ($total > 0) { if ($total > 0) {
$average = bcdiv(bcadd($bill->amount_max, $bill->amount_min), '2'); $average = bcdiv(bcadd($bill->amount_max, $bill->amount_min), '2');
$multi = bcmul($average, (string) $total); $multi = bcmul($average, (string)$total);
$return[$currencyId] = $return[$currencyId] ?? '0'; $return[$currencyId] = $return[$currencyId] ?? '0';
$return[$currencyId] = bcadd($return[$currencyId], $multi); $return[$currencyId] = bcadd($return[$currencyId], $multi);
Log::debug(sprintf('Total > 0, so add to sum %f, which becomes %f (for currency %d)', $multi, $return[$currencyId], $currencyId)); Log::debug(sprintf('Total > 0, so add to sum %f, which becomes %f (for currency %d)', $multi, $return[$currencyId], $currencyId));
@@ -363,7 +364,7 @@ class BillRepository implements BillRepositoryInterface
/** @var Note $note */ /** @var Note $note */
$note = $bill->notes()->first(); $note = $bill->notes()->first();
if (null !== $note) { if (null !== $note) {
return (string) $note->text; return (string)$note->text;
} }
return ''; return '';
@@ -381,14 +382,14 @@ class BillRepository implements BillRepositoryInterface
$repos->setUser($this->user); $repos->setUser($this->user);
// get and sort on currency // get and sort on currency
$result = []; $result = [];
$journals = $bill->transactionJournals()->get(); $journals = $bill->transactionJournals()->get();
/** @var TransactionJournal $journal */ /** @var TransactionJournal $journal */
foreach ($journals as $journal) { foreach ($journals as $journal) {
/** @var Transaction $transaction */ /** @var Transaction $transaction */
$transaction = $journal->transactions()->where('amount', '<', 0)->first(); $transaction = $journal->transactions()->where('amount', '<', 0)->first();
$currencyId = (int) $journal->transaction_currency_id; $currencyId = (int)$journal->transaction_currency_id;
$currency = $journal->transactionCurrency; $currency = $journal->transactionCurrency;
$result[$currencyId] = $result[$currencyId] ?? [ $result[$currencyId] = $result[$currencyId] ?? [
'sum' => '0', 'sum' => '0',
@@ -409,8 +410,9 @@ class BillRepository implements BillRepositoryInterface
* @var array $arr * @var array $arr
*/ */
foreach ($result as $currencyId => $arr) { foreach ($result as $currencyId => $arr) {
$result[$currencyId]['avg'] = bcdiv($arr['sum'], (string) $arr['count']); $result[$currencyId]['avg'] = bcdiv($arr['sum'], (string)$arr['count']);
} }
return $result; return $result;
} }
@@ -438,6 +440,7 @@ class BillRepository implements BillRepositoryInterface
public function getPaidDatesInRange(Bill $bill, Carbon $start, Carbon $end): Collection public function getPaidDatesInRange(Bill $bill, Carbon $start, Carbon $end): Collection
{ {
Log::debug('Now in getPaidDatesInRange()'); Log::debug('Now in getPaidDatesInRange()');
return $bill->transactionJournals() return $bill->transactionJournals()
->before($end)->after($start)->get( ->before($end)->after($start)->get(
[ [
@@ -559,8 +562,11 @@ class BillRepository implements BillRepositoryInterface
/** @var TransactionJournal $journal */ /** @var TransactionJournal $journal */
foreach ($journals as $journal) { foreach ($journals as $journal) {
/** @var Transaction $transaction */ /** @var Transaction $transaction */
$transaction = $journal->transactions()->where('amount', '<', 0)->first(); $transaction = $journal->transactions()->where('amount', '<', 0)->first();
$currencyId = (int) $journal->transaction_currency_id; if (null === $transaction) {
continue;
}
$currencyId = (int)$journal->transaction_currency_id;
$currency = $journal->transactionCurrency; $currency = $journal->transactionCurrency;
$result[$currencyId] = $result[$currencyId] ?? [ $result[$currencyId] = $result[$currencyId] ?? [
'sum' => '0', 'sum' => '0',
@@ -581,8 +587,9 @@ class BillRepository implements BillRepositoryInterface
* @var array $arr * @var array $arr
*/ */
foreach ($result as $currencyId => $arr) { foreach ($result as $currencyId => $arr) {
$result[$currencyId]['avg'] = bcdiv($arr['sum'], (string) $arr['count']); $result[$currencyId]['avg'] = bcdiv($arr['sum'], (string)$arr['count']);
} }
return $result; return $result;
} }
@@ -596,7 +603,7 @@ class BillRepository implements BillRepositoryInterface
{ {
/** @var Transaction $transaction */ /** @var Transaction $transaction */
foreach ($transactions as $transaction) { foreach ($transactions as $transaction) {
$journal = $bill->user->transactionJournals()->find((int) $transaction['transaction_journal_id']); $journal = $bill->user->transactionJournals()->find((int)$transaction['transaction_journal_id']);
$journal->bill_id = $bill->id; $journal->bill_id = $bill->id;
$journal->save(); $journal->save();
Log::debug(sprintf('Linked journal #%d to bill #%d', $journal->id, $bill->id)); Log::debug(sprintf('Linked journal #%d to bill #%d', $journal->id, $bill->id));
@@ -750,7 +757,7 @@ class BillRepository implements BillRepositoryInterface
$set = $this->user->bills()->orderBy('order', 'ASC')->get(); $set = $this->user->bills()->orderBy('order', 'ASC')->get();
$current = 1; $current = 1;
foreach ($set as $bill) { foreach ($set as $bill) {
if ((int) $bill->order !== $current) { if ((int)$bill->order !== $current) {
$bill->order = $current; $bill->order = $current;
$bill->save(); $bill->save();
} }