mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-23 12:27:05 +00:00
Some changes to facilitate piggy bank events (see issue #6) and extra view info for clarity. [skip ci]
This commit is contained in:
@@ -115,9 +115,23 @@ class PiggybankController extends BaseController
|
|||||||
|
|
||||||
$piggybanks = $this->_repository->get();
|
$piggybanks = $this->_repository->get();
|
||||||
|
|
||||||
|
// get the accounts with each piggy bank and check their balance; we might need to
|
||||||
|
// show the user a correction.
|
||||||
|
|
||||||
|
$accounts = [];
|
||||||
|
/** @var \Piggybank $piggybank */
|
||||||
|
foreach($piggybanks as $piggybank) {
|
||||||
|
$account = $piggybank->account;
|
||||||
|
$id = $account->id;
|
||||||
|
if(!isset($accounts[$id])) {
|
||||||
|
$accounts[$id] = ['account' => $account, 'left' => $this->_repository->leftOnAccount($account)];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return View::make('piggybanks.index')->with('piggybanks', $piggybanks)
|
return View::make('piggybanks.index')->with('piggybanks', $piggybanks)
|
||||||
->with('countRepeating', $countRepeating)
|
->with('countRepeating', $countRepeating)
|
||||||
->with('countNonRepeating', $countNonRepeating);
|
->with('countNonRepeating', $countNonRepeating)
|
||||||
|
->with('accounts',$accounts);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -0,0 +1,40 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
|
||||||
|
class CreatePiggyEvents extends Migration {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function up()
|
||||||
|
{
|
||||||
|
Schema::create('piggybank_events', function(Blueprint $table)
|
||||||
|
{
|
||||||
|
$table->increments('id');
|
||||||
|
$table->timestamps();
|
||||||
|
$table->integer('piggybank_id')->unsigned();
|
||||||
|
$table->date('date');
|
||||||
|
$table->decimal('amount', 10, 2);
|
||||||
|
|
||||||
|
// connect instance to piggybank.
|
||||||
|
$table->foreign('piggybank_id')
|
||||||
|
->references('id')->on('piggybanks')
|
||||||
|
->onDelete('cascade');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function down()
|
||||||
|
{
|
||||||
|
Schema::drop('piggybank_events');
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -71,7 +71,7 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface
|
|||||||
{
|
{
|
||||||
$piggies = \Auth::user()->piggybanks()->with(['account', 'piggybankrepetitions'])->get();
|
$piggies = \Auth::user()->piggybanks()->with(['account', 'piggybankrepetitions'])->get();
|
||||||
|
|
||||||
foreach($piggies as $pig) {
|
foreach ($piggies as $pig) {
|
||||||
$pig->leftInAccount = $this->leftOnAccount($pig->account);
|
$pig->leftInAccount = $this->leftOnAccount($pig->account);
|
||||||
}
|
}
|
||||||
return $piggies;
|
return $piggies;
|
||||||
@@ -104,12 +104,17 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface
|
|||||||
public function modifyAmount(\Piggybank $piggyBank, $amount)
|
public function modifyAmount(\Piggybank $piggyBank, $amount)
|
||||||
{
|
{
|
||||||
$rep = $piggyBank->currentRelevantRep();
|
$rep = $piggyBank->currentRelevantRep();
|
||||||
\Log::debug('Amount before: ' . $rep->currentamount);
|
if (!is_null($rep)) {
|
||||||
$rep->currentamount += $amount;
|
$rep->currentamount += $amount;
|
||||||
\Log::debug('Amount after: ' . $rep->currentamount);
|
$rep->save();
|
||||||
\Log::debug('validates: ' . $rep->validate());
|
|
||||||
\Log::debug(print_r($rep->toArray(),true));
|
// create event:
|
||||||
$rep->save();
|
$event = new \PiggybankEvent;
|
||||||
|
$event->date = new Carbon;
|
||||||
|
$event->amount = $amount;
|
||||||
|
$event->piggybank()->associate($piggyBank);
|
||||||
|
$event->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@@ -129,7 +134,7 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface
|
|||||||
if ($data['reminder'] == 'none') {
|
if ($data['reminder'] == 'none') {
|
||||||
unset($data['reminder']);
|
unset($data['reminder']);
|
||||||
}
|
}
|
||||||
if($data['startdate'] == '') {
|
if ($data['startdate'] == '') {
|
||||||
unset($data['startdate']);
|
unset($data['startdate']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -51,24 +51,35 @@ class EloquentPiggybankTrigger
|
|||||||
// piggy bank, and we can find transactions that fall within
|
// piggy bank, and we can find transactions that fall within
|
||||||
// that repetition (to fix the "saved amount".
|
// that repetition (to fix the "saved amount".
|
||||||
$reps = $piggy->piggybankrepetitions()->get();
|
$reps = $piggy->piggybankrepetitions()->get();
|
||||||
|
|
||||||
/** @var \PiggybankRepetition $rep */
|
/** @var \PiggybankRepetition $rep */
|
||||||
foreach ($reps as $rep) {
|
foreach ($reps as $rep) {
|
||||||
if ($rep->currentamount == 0) {
|
$query = \Transaction::where('piggybank_id', $piggy->id)->leftJoin(
|
||||||
$query = \Transaction::where('piggybank_id', $piggy->id)->leftJoin(
|
'transaction_journals', 'transaction_journals.id', '=',
|
||||||
'transaction_journals', 'transaction_journals.id', '=',
|
'transactions.transaction_journal_id'
|
||||||
'transactions.transaction_journal_id'
|
);
|
||||||
);
|
if (!is_null($rep->startdate)) {
|
||||||
if (!is_null($rep->startdate)) {
|
$query->where('transaction_journals.date', '>=', $rep->startdate->format('Y-m-d'));
|
||||||
$query->where('transaction_journals.date', '>=', $rep->startdate->format('Y-m-d'));
|
|
||||||
}
|
|
||||||
if (!is_null($rep->targetdate)) {
|
|
||||||
$query->where(
|
|
||||||
'transaction_journals.date', '<=', $rep->targetdate->format('Y-m-d')
|
|
||||||
);
|
|
||||||
}
|
|
||||||
$sum = $query->sum('transactions.amount');
|
|
||||||
$rep->currentamount = floatval($sum);
|
|
||||||
}
|
}
|
||||||
|
if (!is_null($rep->targetdate)) {
|
||||||
|
$query->where(
|
||||||
|
'transaction_journals.date', '<=', $rep->targetdate->format('Y-m-d')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// get events for piggy bank, save those as well:
|
||||||
|
$eventSumQuery = $piggy->piggybankevents();
|
||||||
|
if(!is_null($rep->startdate)) {
|
||||||
|
$eventSumQuery->where('date','>=',$rep->startdate->format('Y-m-d'));
|
||||||
|
}
|
||||||
|
if(!is_null($rep->targetdate)) {
|
||||||
|
$eventSumQuery->where('date','<=',$rep->targetdate->format('Y-m-d'));
|
||||||
|
}
|
||||||
|
$eventSum = floatval($eventSumQuery->sum('amount'));
|
||||||
|
|
||||||
|
|
||||||
|
$sum = $query->sum('transactions.amount');
|
||||||
|
$rep->currentamount = floatval($sum) + $eventSum;
|
||||||
$rep->save();
|
$rep->save();
|
||||||
|
|
||||||
|
|
||||||
@@ -138,8 +149,8 @@ class EloquentPiggybankTrigger
|
|||||||
$sum = \Transaction::where('piggybank_id', $repeated->id)->leftJoin(
|
$sum = \Transaction::where('piggybank_id', $repeated->id)->leftJoin(
|
||||||
'transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id'
|
'transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id'
|
||||||
)->where('transaction_journals.date', '>=', $rep->startdate->format('Y-m-d'))->where(
|
)->where('transaction_journals.date', '>=', $rep->startdate->format('Y-m-d'))->where(
|
||||||
'transaction_journals.date', '<=', $rep->targetdate->format('Y-m-d')
|
'transaction_journals.date', '<=', $rep->targetdate->format('Y-m-d')
|
||||||
)->sum('transactions.amount');
|
)->sum('transactions.amount');
|
||||||
$rep->currentamount = floatval($sum);
|
$rep->currentamount = floatval($sum);
|
||||||
$rep->save();
|
$rep->save();
|
||||||
|
|
||||||
|
@@ -42,18 +42,18 @@ class Piggybank extends Ardent
|
|||||||
{
|
{
|
||||||
public static $rules
|
public static $rules
|
||||||
= [
|
= [
|
||||||
'account_id' => 'required|exists:accounts,id', // link to Account
|
'account_id' => 'required|exists:accounts,id', // link to Account
|
||||||
'name' => 'required|between:1,255', // name
|
'name' => 'required|between:1,255', // name
|
||||||
'targetamount' => 'required|min:0', // amount you want to save
|
'targetamount' => 'required|min:0', // amount you want to save
|
||||||
'startdate' => 'date', // when you started
|
'startdate' => 'date', // when you started
|
||||||
'targetdate' => 'date', // when its due
|
'targetdate' => 'date', // when its due
|
||||||
'repeats' => 'required|between:0,1', // does it repeat?
|
'repeats' => 'required|between:0,1', // does it repeat?
|
||||||
'rep_length' => 'in:day,week,month,year', // how long is the period?
|
'rep_length' => 'in:day,week,month,year', // how long is the period?
|
||||||
'rep_every' => 'required|min:1|max:100', // how often does it repeat? every 3 years.
|
'rep_every' => 'required|min:1|max:100', // how often does it repeat? every 3 years.
|
||||||
'rep_times' => 'min:1|max:100', // how many times do you want to save this amount? eg. 3 times
|
'rep_times' => 'min:1|max:100', // how many times do you want to save this amount? eg. 3 times
|
||||||
'reminder' => 'in:day,week,month,year', // want a reminder to put money in this?
|
'reminder' => 'in:day,week,month,year', // want a reminder to put money in this?
|
||||||
'reminder_skip' => 'required|min:0|max:100', // every week? every 2 months?
|
'reminder_skip' => 'required|min:0|max:100', // every week? every 2 months?
|
||||||
'order' => 'required:min:1', // not yet used.
|
'order' => 'required:min:1', // not yet used.
|
||||||
];
|
];
|
||||||
public $fillable
|
public $fillable
|
||||||
= [
|
= [
|
||||||
@@ -82,18 +82,18 @@ class Piggybank extends Ardent
|
|||||||
$end->endOfMonth();
|
$end->endOfMonth();
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'account_id' => 'factory|Account',
|
'account_id' => 'factory|Account',
|
||||||
'name' => 'string',
|
'name' => 'string',
|
||||||
'targetamount' => 'integer',
|
'targetamount' => 'integer',
|
||||||
'startdate' => $start->format('Y-m-d'),
|
'startdate' => $start->format('Y-m-d'),
|
||||||
'targetdate' => $end->format('Y-m-d'),
|
'targetdate' => $end->format('Y-m-d'),
|
||||||
'repeats' => 0,
|
'repeats' => 0,
|
||||||
'rep_length' => null,
|
'rep_length' => null,
|
||||||
'rep_times' => 0,
|
'rep_times' => 0,
|
||||||
'rep_every' => 0,
|
'rep_every' => 0,
|
||||||
'reminder' => null,
|
'reminder' => null,
|
||||||
'reminder_skip' => 0,
|
'reminder_skip' => 0,
|
||||||
'order' => 1,
|
'order' => 1,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -212,4 +212,12 @@ class Piggybank extends Ardent
|
|||||||
return $this->hasMany('PiggybankRepetition');
|
return $this->hasMany('PiggybankRepetition');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
||||||
|
*/
|
||||||
|
public function piggybankevents()
|
||||||
|
{
|
||||||
|
return $this->hasMany('PiggybankEvent');
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
44
app/models/PiggybankEvent.php
Normal file
44
app/models/PiggybankEvent.php
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Carbon\Carbon;
|
||||||
|
use LaravelBook\Ardent\Ardent as Ardent;
|
||||||
|
|
||||||
|
class PiggybankEvent extends Ardent
|
||||||
|
{
|
||||||
|
|
||||||
|
public static $rules = [
|
||||||
|
'piggybank_id' => 'required|exists:piggybanks,id',
|
||||||
|
'date' => 'required|date',
|
||||||
|
'amount' => 'required|numeric'
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public static function factory()
|
||||||
|
{
|
||||||
|
$date = new Carbon;
|
||||||
|
return [
|
||||||
|
'piggybank_id' => 'factory|Piggybank',
|
||||||
|
'date' => $date->format('Y-m-d'),
|
||||||
|
'amount' => 10
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getDates()
|
||||||
|
{
|
||||||
|
return ['created_at', 'updated_at', 'date'];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||||
|
*/
|
||||||
|
public function piggybank()
|
||||||
|
{
|
||||||
|
return $this->belongsTo('Piggybank');
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -140,6 +140,23 @@
|
|||||||
@endif
|
@endif
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="row">
|
||||||
|
<div class="col-lg-6">
|
||||||
|
<h4>Account information</h4>
|
||||||
|
<table class="table">
|
||||||
|
<tr>
|
||||||
|
<th>Account</th>
|
||||||
|
<th>Left for piggy banks</th>
|
||||||
|
</tr>
|
||||||
|
@foreach($accounts as $account)
|
||||||
|
<tr>
|
||||||
|
<td>{{{$account['account']->name}}}</td>
|
||||||
|
<td>{{mf($account['left'])}}</td>
|
||||||
|
</tr>
|
||||||
|
@endforeach
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<!-- MODAL -->
|
<!-- MODAL -->
|
||||||
<div class="modal fade" id="modal" tabindex="-1" role="dialog" aria-labelledby="modal" aria-hidden="true">
|
<div class="modal fade" id="modal" tabindex="-1" role="dialog" aria-labelledby="modal" aria-hidden="true">
|
||||||
|
Reference in New Issue
Block a user