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:
@@ -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');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -104,13 +104,18 @@ class EloquentPiggybankRepository implements PiggybankRepositoryInterface
|
||||
public function modifyAmount(\Piggybank $piggyBank, $amount)
|
||||
{
|
||||
$rep = $piggyBank->currentRelevantRep();
|
||||
\Log::debug('Amount before: ' . $rep->currentamount);
|
||||
if (!is_null($rep)) {
|
||||
$rep->currentamount += $amount;
|
||||
\Log::debug('Amount after: ' . $rep->currentamount);
|
||||
\Log::debug('validates: ' . $rep->validate());
|
||||
\Log::debug(print_r($rep->toArray(),true));
|
||||
$rep->save();
|
||||
|
||||
// create event:
|
||||
$event = new \PiggybankEvent;
|
||||
$event->date = new Carbon;
|
||||
$event->amount = $amount;
|
||||
$event->piggybank()->associate($piggyBank);
|
||||
$event->save();
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
|
||||
|
||||
@@ -51,9 +51,9 @@ 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'
|
||||
@@ -66,9 +66,20 @@ class EloquentPiggybankTrigger
|
||||
'transaction_journals.date', '<=', $rep->targetdate->format('Y-m-d')
|
||||
);
|
||||
}
|
||||
$sum = $query->sum('transactions.amount');
|
||||
$rep->currentamount = floatval($sum);
|
||||
|
||||
// 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();
|
||||
|
||||
|
||||
|
||||
@@ -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">
|
||||
|
||||
Reference in New Issue
Block a user