mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-11-06 01:45:22 +00:00
Now with the added capability of adding money to a piggy bank from a transfer. Also various fixes (see issue #6). [skip ci]
This commit is contained in:
@@ -114,6 +114,8 @@ class Chart implements ChartInterface
|
||||
$q->where('startdate', $start->format('Y-m-d'));
|
||||
}]
|
||||
)->orderBy('name', 'ASC')->get();
|
||||
$limitInPeriod = '';
|
||||
$spentInPeriod = '';
|
||||
|
||||
foreach ($budgets as $budget) {
|
||||
$budget->count = 0;
|
||||
|
||||
@@ -16,6 +16,9 @@ class EloquentCategoryRepository implements CategoryRepositoryInterface
|
||||
*/
|
||||
public function createOrFind($name)
|
||||
{
|
||||
if(strlen($name) == 0) {
|
||||
return null;
|
||||
}
|
||||
$category = $this->findByName($name);
|
||||
if (!$category) {
|
||||
return $this->store(['name' => $name]);
|
||||
@@ -55,7 +58,7 @@ class EloquentCategoryRepository implements CategoryRepositoryInterface
|
||||
*/
|
||||
public function findByName($name)
|
||||
{
|
||||
if ($name == '') {
|
||||
if ($name == '' || strlen($name) == 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -163,20 +163,15 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface
|
||||
$piggy->startdate
|
||||
= isset($data['startdate']) && strlen($data['startdate']) > 0 ? new Carbon($data['startdate']) : null;
|
||||
|
||||
// everything we can update for NON repeating piggy banks:
|
||||
if ($piggy->repeats == 0) {
|
||||
// if non-repeating there is only one PiggyBank instance and we can delete it safely.
|
||||
// it will be recreated.
|
||||
$piggy->piggybankrepetitions()->first()->delete();
|
||||
} else {
|
||||
// we can delete all of them, because reasons
|
||||
foreach ($piggy->piggybankrepetitions()->get() as $rep) {
|
||||
$rep->delete();
|
||||
}
|
||||
foreach ($piggy->piggybankrepetitions()->get() as $rep) {
|
||||
$rep->delete();
|
||||
}
|
||||
|
||||
if ($piggy->repeats == 1) {
|
||||
$piggy->rep_every = intval($data['rep_every']);
|
||||
$piggy->rep_length = $data['rep_length'];
|
||||
}
|
||||
|
||||
if ($piggy->validate()) {
|
||||
// check the things we check for new piggies
|
||||
$piggy->save();
|
||||
|
||||
@@ -34,8 +34,8 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
|
||||
* A gains 200 (200). * -1
|
||||
* B loses 200 (-200). * 1
|
||||
*
|
||||
* @param \Account $from
|
||||
* @param \Account $toAccount
|
||||
* @param \Account $from
|
||||
* @param \Account $toAccount
|
||||
* @param $description
|
||||
* @param $amount
|
||||
* @param \Carbon\Carbon $date
|
||||
@@ -287,6 +287,8 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
|
||||
case 'transfer':
|
||||
$fromAccount = $accountRepository->find(intval($data['account_from_id']));
|
||||
$toAccount = $accountRepository->find(intval($data['account_to_id']));
|
||||
|
||||
|
||||
break;
|
||||
}
|
||||
// fall back to cash if necessary:
|
||||
@@ -310,6 +312,39 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
|
||||
return $transactionJournal;
|
||||
}
|
||||
|
||||
// here we're done and we have transactions in the journal:
|
||||
// do something with the piggy bank:
|
||||
if ($what == 'transfer') {
|
||||
/** @var \Firefly\Storage\Piggybank\PiggybankRepositoryInterface $piggyRepository */
|
||||
$piggyRepository = \App::make('Firefly\Storage\Piggybank\PiggybankRepositoryInterface');
|
||||
|
||||
if (isset($data['piggybank_id'])) {
|
||||
/** @var \Piggybank $piggyBank */
|
||||
$piggyBank = $piggyRepository->find(intval($data['piggybank_id']));
|
||||
|
||||
if ($piggyBank) {
|
||||
if ($toAccount->id == $piggyBank->account_id) {
|
||||
|
||||
// find the transaction connected to the $toAccount:
|
||||
/** @var \Transaction $transaction */
|
||||
$transaction
|
||||
= $transactionJournal->transactions()->where('account_id', $toAccount->id)->first();
|
||||
// connect the piggy to it:
|
||||
$transaction->piggybank()->associate($piggyBank);
|
||||
$transaction->save();
|
||||
} else {
|
||||
\Session::flash(
|
||||
'warning',
|
||||
'Piggy bank "' . e($piggyBank->name) . '" is not set to draw money from account "' . e(
|
||||
$toAccount->name
|
||||
) . '", so the money isn\'t added to the piggy bank.'
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// attach:
|
||||
if (!is_null($budget)) {
|
||||
$transactionJournal->budgets()->save($budget);
|
||||
@@ -390,10 +425,13 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
|
||||
break;
|
||||
case 'Transfer':
|
||||
// means transaction[0] is account that sent the money (from).
|
||||
/** @var \Account $fromAccount */
|
||||
$fromAccount = $accountRepository->find($data['account_from_id']);
|
||||
/** @var \Account $toAccount */
|
||||
$toAccount = $accountRepository->find($data['account_to_id']);
|
||||
$journal->transactions[0]->account()->associate($fromAccount);
|
||||
$journal->transactions[1]->account()->associate($toAccount);
|
||||
|
||||
break;
|
||||
default:
|
||||
throw new FireflyException('Cannot edit this!');
|
||||
|
||||
@@ -45,6 +45,24 @@ class EloquentPiggybankTrigger
|
||||
} catch (QueryException $e) {
|
||||
}
|
||||
}
|
||||
|
||||
// whatever we did here, we now have all repetitions for this
|
||||
// piggy bank, and we can find transactions that fall within
|
||||
// that repetition (to fix the "saved amount".
|
||||
$reps = $piggy->piggybankrepetitions()->get();
|
||||
/** @var \PiggybankRepetition $rep */
|
||||
foreach ($reps as $rep) {
|
||||
$sum = \Transaction::where('piggybank_id', $piggy->id)->leftJoin(
|
||||
'transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id'
|
||||
)->where('transaction_journals.date', '>=', $rep->startdate->format('Y-m-d'))->where(
|
||||
'transaction_journals.date', '<=', $rep->targetdate->format('Y-m-d')
|
||||
)->sum('transactions.amount');
|
||||
$rep->currentamount = floatval($sum);
|
||||
$rep->save();
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
unset($piggy, $piggybanks, $rep);
|
||||
|
||||
@@ -59,14 +77,8 @@ class EloquentPiggybankTrigger
|
||||
$rep->targetdate = $repeated->targetdate;
|
||||
$rep->currentamount = 0;
|
||||
try {
|
||||
\Log::debug(
|
||||
'Creating initial rep ('.$repeated->name.') (from ' . ($rep->startdate ? $rep->startdate->format('d-m-Y')
|
||||
: 'NULL') . ' to '
|
||||
. ($rep->targetdate ? $rep->targetdate->format('d-m-Y') : 'NULL') . ')'
|
||||
);
|
||||
$rep->save();
|
||||
} catch (QueryException $e) {
|
||||
\Log::error('FAILED initital repetition.');
|
||||
}
|
||||
unset($rep);
|
||||
|
||||
@@ -109,6 +121,19 @@ class EloquentPiggybankTrigger
|
||||
}
|
||||
}
|
||||
}
|
||||
$reps = $repeated->piggybankrepetitions()->get();
|
||||
/** @var \PiggybankRepetition $rep */
|
||||
foreach ($reps as $rep) {
|
||||
$sum = \Transaction::where('piggybank_id', $repeated->id)->leftJoin(
|
||||
'transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id'
|
||||
)->where('transaction_journals.date', '>=', $rep->startdate->format('Y-m-d'))->where(
|
||||
'transaction_journals.date', '<=', $rep->targetdate->format('Y-m-d')
|
||||
)->sum('transactions.amount');
|
||||
$rep->currentamount = floatval($sum);
|
||||
$rep->save();
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user