mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Some changes to facilitate piggy bank events (see issue #6) and extra view info for clarity. [skip ci]
This commit is contained in:
parent
5ac790433b
commit
87bddce1d3
@ -115,9 +115,23 @@ class PiggybankController extends BaseController
|
||||
|
||||
$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)
|
||||
->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();
|
||||
|
||||
foreach($piggies as $pig) {
|
||||
foreach ($piggies as $pig) {
|
||||
$pig->leftInAccount = $this->leftOnAccount($pig->account);
|
||||
}
|
||||
return $piggies;
|
||||
@ -104,12 +104,17 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface
|
||||
public function modifyAmount(\Piggybank $piggyBank, $amount)
|
||||
{
|
||||
$rep = $piggyBank->currentRelevantRep();
|
||||
\Log::debug('Amount before: ' . $rep->currentamount);
|
||||
$rep->currentamount += $amount;
|
||||
\Log::debug('Amount after: ' . $rep->currentamount);
|
||||
\Log::debug('validates: ' . $rep->validate());
|
||||
\Log::debug(print_r($rep->toArray(),true));
|
||||
$rep->save();
|
||||
if (!is_null($rep)) {
|
||||
$rep->currentamount += $amount;
|
||||
$rep->save();
|
||||
|
||||
// create event:
|
||||
$event = new \PiggybankEvent;
|
||||
$event->date = new Carbon;
|
||||
$event->amount = $amount;
|
||||
$event->piggybank()->associate($piggyBank);
|
||||
$event->save();
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
@ -129,7 +134,7 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface
|
||||
if ($data['reminder'] == 'none') {
|
||||
unset($data['reminder']);
|
||||
}
|
||||
if($data['startdate'] == '') {
|
||||
if ($data['startdate'] == '') {
|
||||
unset($data['startdate']);
|
||||
}
|
||||
|
||||
|
@ -51,24 +51,35 @@ class EloquentPiggybankTrigger
|
||||
// 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) {
|
||||
if ($rep->currentamount == 0) {
|
||||
$query = \Transaction::where('piggybank_id', $piggy->id)->leftJoin(
|
||||
'transaction_journals', 'transaction_journals.id', '=',
|
||||
'transactions.transaction_journal_id'
|
||||
);
|
||||
if (!is_null($rep->startdate)) {
|
||||
$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);
|
||||
$query = \Transaction::where('piggybank_id', $piggy->id)->leftJoin(
|
||||
'transaction_journals', 'transaction_journals.id', '=',
|
||||
'transactions.transaction_journal_id'
|
||||
);
|
||||
if (!is_null($rep->startdate)) {
|
||||
$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')
|
||||
);
|
||||
}
|
||||
|
||||
// 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();
|
||||
|
||||
|
||||
@ -138,8 +149,8 @@ class EloquentPiggybankTrigger
|
||||
$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');
|
||||
'transaction_journals.date', '<=', $rep->targetdate->format('Y-m-d')
|
||||
)->sum('transactions.amount');
|
||||
$rep->currentamount = floatval($sum);
|
||||
$rep->save();
|
||||
|
||||
|
@ -42,18 +42,18 @@ class Piggybank extends Ardent
|
||||
{
|
||||
public static $rules
|
||||
= [
|
||||
'account_id' => 'required|exists:accounts,id', // link to Account
|
||||
'name' => 'required|between:1,255', // name
|
||||
'targetamount' => 'required|min:0', // amount you want to save
|
||||
'startdate' => 'date', // when you started
|
||||
'targetdate' => 'date', // when its due
|
||||
'repeats' => 'required|between:0,1', // does it repeat?
|
||||
'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_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?
|
||||
'account_id' => 'required|exists:accounts,id', // link to Account
|
||||
'name' => 'required|between:1,255', // name
|
||||
'targetamount' => 'required|min:0', // amount you want to save
|
||||
'startdate' => 'date', // when you started
|
||||
'targetdate' => 'date', // when its due
|
||||
'repeats' => 'required|between:0,1', // does it repeat?
|
||||
'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_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_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
|
||||
= [
|
||||
@ -82,18 +82,18 @@ class Piggybank extends Ardent
|
||||
$end->endOfMonth();
|
||||
|
||||
return [
|
||||
'account_id' => 'factory|Account',
|
||||
'name' => 'string',
|
||||
'targetamount' => 'integer',
|
||||
'startdate' => $start->format('Y-m-d'),
|
||||
'targetdate' => $end->format('Y-m-d'),
|
||||
'repeats' => 0,
|
||||
'rep_length' => null,
|
||||
'rep_times' => 0,
|
||||
'rep_every' => 0,
|
||||
'reminder' => null,
|
||||
'account_id' => 'factory|Account',
|
||||
'name' => 'string',
|
||||
'targetamount' => 'integer',
|
||||
'startdate' => $start->format('Y-m-d'),
|
||||
'targetdate' => $end->format('Y-m-d'),
|
||||
'repeats' => 0,
|
||||
'rep_length' => null,
|
||||
'rep_times' => 0,
|
||||
'rep_every' => 0,
|
||||
'reminder' => null,
|
||||
'reminder_skip' => 0,
|
||||
'order' => 1,
|
||||
'order' => 1,
|
||||
];
|
||||
}
|
||||
|
||||
@ -212,4 +212,12 @@ class Piggybank extends Ardent
|
||||
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
|
||||
</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 -->
|
||||
<div class="modal fade" id="modal" tabindex="-1" role="dialog" aria-labelledby="modal" aria-hidden="true">
|
||||
|
Loading…
Reference in New Issue
Block a user