All kinds of new code, especially for the piggy banks.

This commit is contained in:
Sander Dorigo
2014-11-02 14:58:12 +01:00
parent 3231effd20
commit 2f8b10e82c
26 changed files with 676 additions and 89 deletions

View File

@@ -10,9 +10,10 @@ class Form
{
/**
* @param $name
* @param null $value
* @param $name
* @param null $value
* @param array $options
*
* @return string
* @throws FireflyException
*/
@@ -40,7 +41,7 @@ class Form
public static function ffAmount($name, $value = null, array $options = [])
{
$options['step'] = 'any';
$options['min'] = '0.01';
$options['min'] = '0.01';
return self::ffInput('amount', $name, $value, $options);
}
@@ -61,9 +62,10 @@ class Form
}
/**
* @param $name
* @param null $value
* @param $name
* @param null $value
* @param array $options
*
* @return string
* @throws FireflyException
*/
@@ -73,9 +75,10 @@ class Form
}
/**
* @param $name
* @param null $value
* @param $name
* @param null $value
* @param array $options
*
* @return string
* @throws FireflyException
*/
@@ -86,10 +89,11 @@ class Form
}
/**
* @param $name
* @param $name
* @param array $list
* @param null $selected
* @param null $selected
* @param array $options
*
* @return string
* @throws FireflyException
*/
@@ -99,9 +103,10 @@ class Form
}
/**
* @param $name
* @param null $value
* @param $name
* @param null $value
* @param array $options
*
* @return string
* @throws FireflyException
*/
@@ -111,16 +116,25 @@ class Form
}
public static function label($name)
/**
* @param $name
* @param $options
*
* @return string
*/
public static function label($name, $options)
{
if (isset($options['label'])) {
return $options['label'];
}
$labels = [
'amount_min' => 'Amount (min)',
'amount_max' => 'Amount (max)',
'match' => 'Matches on',
'repeat_freq' => 'Repetition',
'amount_min' => 'Amount (min)',
'amount_max' => 'Amount (max)',
'match' => 'Matches on',
'repeat_freq' => 'Repetition',
'account_from_id' => 'Account from',
'account_to_id' => 'Account to',
'account_id' => 'Asset account'
'account_to_id' => 'Account to',
'account_id' => 'Asset account'
];
return isset($labels[$name]) ? $labels[$name] : str_replace('_', ' ', ucfirst($name));
@@ -174,28 +188,29 @@ class Form
case 'create':
$return = '<div class="form-group"><label for="return_to_form" class="col-sm-4 control-label">';
$return .= 'Return here</label><div class="col-sm-8"><div class="radio"><label>';
$return .= \Form::radio('post_submit_action','create_another', $previousValue == 'create_another');
$return .= \Form::radio('post_submit_action', 'create_another', $previousValue == 'create_another');
$return .= 'After storing, return here to create another one.</label></div></div></div>';
break;
case 'update':
$return = '<div class="form-group"><label for="return_to_edit" class="col-sm-4 control-label">';
$return .= 'Return here</label><div class="col-sm-8"><div class="radio"><label>';
$return .= \Form::radio('post_submit_action','return_to_edit', $previousValue == 'return_to_edit');
$return .= \Form::radio('post_submit_action', 'return_to_edit', $previousValue == 'return_to_edit');
$return .= 'After updating, return here.</label></div></div></div>';
break;
default:
throw new FireflyException('Cannot create ffOptionsList for option (store+return) ' . $type);
break;
}
return $store.$validate.$return;
return $store . $validate . $return;
}
/**
* @param $type
* @param $name
* @param null $value
* @param $type
* @param $name
* @param null $value
* @param array $options
* @param array $list
*
* @return string
* @throws FireflyException
*/
@@ -204,10 +219,10 @@ class Form
/*
* add some defaults to this method:
*/
$options['class'] = 'form-control';
$options['id'] = 'ffInput_' . $name;
$options['class'] = 'form-control';
$options['id'] = 'ffInput_' . $name;
$options['autocomplete'] = 'off';
$label = self::label($name);
$label = self::label($name, $options);
/*
* Make label and placeholder look nice.
*/
@@ -216,9 +231,9 @@ class Form
/*
* Get prefilled value:
*/
if(\Session::has('prefilled')) {
if (\Session::has('prefilled')) {
$prefilled = \Session::get('prefilled');
$value = isset($prefilled[$name]) && is_null($value) ? $prefilled[$name] : $value;
$value = isset($prefilled[$name]) && is_null($value) ? $prefilled[$name] : $value;
}
/*

View File

@@ -193,7 +193,7 @@ class EloquentPiggybankTrigger
'Firefly\Trigger\Piggybanks\EloquentPiggybankTrigger@updateRelatedTransfer'
);
$events->listen(
'piggybanks.check', 'Firefly\Trigger\Piggybanks\EloquentPiggybankTrigger@checkRepeatingPiggies'
'piggybanks.storepiggybanks.check', 'Firefly\Trigger\Piggybanks\EloquentPiggybankTrigger@checkRepeatingPiggies'
);
}

View File

@@ -27,6 +27,37 @@ class Account implements CUD, CommonDatabaseCalls, AccountInterface
$this->setUser(\Auth::user());
}
/**
* @param Ardent $model
* @param array $data
*
* @return bool
*/
public function update(Ardent $model, array $data)
{
$model->name = $data['name'];
$model->active = isset($data['active']) ? intval($data['active']) : 0;
$model->save();
if (isset($data['openingbalance']) && isset($data['openingbalancedate'])) {
$openingBalance = $this->openingBalanceTransaction($model);
$openingBalance->date = new Carbon($data['openingbalancedate']);
$openingBalance->save();
$amount = floatval($data['openingbalance']);
/** @var \Transaction $transaction */
foreach ($openingBalance->transactions as $transaction) {
if ($transaction->account_id == $model->id) {
$transaction->amount = $amount;
} else {
$transaction->amount = $amount * -1;
}
$transaction->save();
}
}
return true;
}
/**
* Get all asset accounts. Optional JSON based parameters.
*

View File

@@ -24,6 +24,7 @@ class AccountType implements AccountTypeInterface, CUD, CommonDatabaseCalls
* @param $what
*
* @return \AccountType|null
* @throws FireflyException
*/
public function findByWhat($what)
{
@@ -125,4 +126,15 @@ class AccountType implements AccountTypeInterface, CUD, CommonDatabaseCalls
{
// TODO: Implement getByIds() method.
}
/**
* @param Ardent $model
* @param array $data
*
* @return bool
*/
public function update(Ardent $model, array $data)
{
// TODO: Implement update() method.
}
}

View File

@@ -154,4 +154,15 @@ class Budget implements CUD, CommonDatabaseCalls, BudgetInterface
}
)->before($end)->after($start)->lessThan(0)->transactionTypes(['Withdrawal'])->get();
}
/**
* @param Ardent $model
* @param array $data
*
* @return bool
*/
public function update(Ardent $model, array $data)
{
// TODO: Implement update() method.
}
}

View File

@@ -115,4 +115,15 @@ class Category implements CUD, CommonDatabaseCalls, CategoryInterface
{
// TODO: Implement findByWhat() method.
}
/**
* @param Ardent $model
* @param array $data
*
* @return bool
*/
public function update(Ardent $model, array $data)
{
// TODO: Implement update() method.
}
}

View File

@@ -46,4 +46,12 @@ interface CUD
*/
public function store(array $data);
/**
* @param Ardent $model
* @param array $data
*
* @return bool
*/
public function update(Ardent $model, array $data);
}

View File

@@ -2,6 +2,7 @@
namespace FireflyIII\Database;
use Carbon\Carbon;
use FireflyIII\Exception\NotImplementedException;
use Illuminate\Support\MessageBag;
use LaravelBook\Ardent\Ardent;
use Illuminate\Support\Collection;
@@ -76,7 +77,81 @@ class Piggybank implements CUD, CommonDatabaseCalls, PiggybankInterface
*/
public function validate(array $model)
{
// TODO: Implement validate() method.
$warnings = new MessageBag;
$successes = new MessageBag;
$errors = new MessageBag;
/*
* Name validation:
*/
if (!isset($model['name'])) {
$errors->add('name', 'Name is mandatory');
}
if (isset($model['name']) && strlen($model['name']) == 0) {
$errors->add('name', 'Name is too short');
}
if (isset($model['name']) && strlen($model['name']) > 100) {
$errors->add('name', 'Name is too long');
}
if (intval($model['account_id']) == 0) {
$errors->add('account_id', 'Account is mandatory');
}
if ($model['targetdate'] == '' && isset($model['remind_me']) && intval($model['remind_me']) == 1) {
$errors->add('targetdate', 'Target date is mandatory when setting reminders.');
}
if ($model['targetdate'] != '') {
try {
new Carbon($model['targetdate']);
} catch (\Exception $e) {
$errors->add('date', 'Invalid date.');
}
}
if (floatval($model['targetamount']) < 0.01) {
$errors->add('targetamount', 'Amount should be above 0.01.');
}
if (!in_array(ucfirst($model['reminder']), \Config::get('firefly.piggybank_periods'))) {
$errors->add('reminder', 'Invalid reminder period (' . $model['reminder'] . ')');
}
// check period.
if (!$errors->has('reminder') && !$errors->has('targetdate') && isset($model['remind_me']) && intval($model['remind_me']) == 1) {
$today = new Carbon;
$target = new Carbon($model['targetdate']);
switch ($model['reminder']) {
case 'week':
$today->addWeek();
break;
case 'month':
$today->addMonth();
break;
case 'year':
$today->addYear();
break;
}
if ($today > $target) {
$errors->add('reminder', 'Target date is too close to today to set reminders.');
}
}
$validator = \Validator::make($model, \Piggybank::$rules);
if ($validator->invalid()) {
$errors->merge($errors);
}
// add ok messages.
$list = ['name', 'account_id', 'targetamount', 'targetdate', 'remind_me', 'reminder'];
foreach ($list as $entry) {
if (!$errors->has($entry) && !$warnings->has($entry)) {
$successes->add($entry, 'OK');
}
}
return [
'errors' => $errors,
'warnings' => $warnings,
'successes' => $successes
];
}
/**
@@ -86,7 +161,22 @@ class Piggybank implements CUD, CommonDatabaseCalls, PiggybankInterface
*/
public function store(array $data)
{
// TODO: Implement store() method.
$data['rep_every'] = isset($data['rep_every']) ? $data['rep_every'] : 0;
$data['reminder_skip'] = isset($data['reminder_skip']) ? $data['reminder_skip'] : 0;
$data['order'] = isset($data['order']) ? $data['order'] : 0;
$data['remind_me'] = isset($data['remind_me']) ? intval($data['remind_me']) : 0;
$data['startdate'] = isset($data['startdate']) ? $data['startdate'] : Carbon::now()->format('Y-m-d');
$data['targetdate'] = isset($data['targetdate']) && $data['targetdate'] != '' ? $data['targetdate'] : null;
$piggybank = new \Piggybank($data);
if (!$piggybank->validate()) {
var_dump($piggybank->errors()->all());
exit;
}
$piggybank->save();
\Event::fire('piggybanks.store', [$piggybank]);
$piggybank->save();
}
/**

View File

@@ -29,8 +29,8 @@ class Recurring implements CUD, CommonDatabaseCalls, RecurringInterface
/**
* @param \RecurringTransaction $recurring
* @param Carbon $current
* @param Carbon $currentEnd
* @param Carbon $start
* @param Carbon $end
*
* @return \TransactionJournal|null
*/
@@ -129,4 +129,15 @@ class Recurring implements CUD, CommonDatabaseCalls, RecurringInterface
{
// TODO: Implement findByWhat() method.
}
/**
* @param Ardent $model
* @param array $data
*
* @return bool
*/
public function update(Ardent $model, array $data)
{
// TODO: Implement update() method.
}
}

View File

@@ -171,4 +171,25 @@ class Transaction implements TransactionInterface, CUD, CommonDatabaseCalls
{
// TODO: Implement findByWhat() method.
}
/**
* @param Ardent $model
* @param array $data
*
* @return bool
*/
public function update(Ardent $model, array $data)
{
// TODO: Implement update() method.
}
/**
* @param array $ids
*
* @return Collection
*/
public function getByIds(array $ids)
{
// TODO: Implement getByIds() method.
}
}

View File

@@ -116,4 +116,15 @@ class TransactionCurrency implements TransactionCurrencyInterface, CUD, CommonDa
{
// TODO: Implement getByIds() method.
}
/**
* @param Ardent $model
* @param array $data
*
* @return bool
*/
public function update(Ardent $model, array $data)
{
// TODO: Implement update() method.
}
}

View File

@@ -267,4 +267,15 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData
{
// TODO: Implement getByIds() method.
}
/**
* @param Ardent $model
* @param array $data
*
* @return bool
*/
public function update(Ardent $model, array $data)
{
// TODO: Implement update() method.
}
}

View File

@@ -115,4 +115,15 @@ class TransactionType implements TransactionTypeInterface, CUD, CommonDatabaseCa
{
// TODO: Implement getByIds() method.
}
/**
* @param Ardent $model
* @param array $data
*
* @return bool
*/
public function update(Ardent $model, array $data)
{
// TODO: Implement update() method.
}
}

View File

@@ -0,0 +1,50 @@
<?php
namespace FireflyIII\Shared\Toolkit;
use Illuminate\Support\Collection;
/**
* Class Form
*
* @package FireflyIII\Shared\Toolkit
*/
class Form {
/**
* Takes any collection and tries to make a sensible select list compatible array of it.
*
* @param Collection $set
* @param null $titleField
*
* @return mixed
*/
public function makeSelectList(Collection $set, $titleField = null)
{
$selectList = [];
/** @var Model $entry */
foreach ($set as $entry) {
$id = intval($entry->id);
$title = null;
if (is_null($titleField)) {
// try 'title' field.
if (isset($entry->title)) {
$title = $entry->title;
}
// try 'name' field
if (is_null($title)) {
$title = $entry->name;
}
// try 'description' field
if (is_null($title)) {
$title = $entry->description;
}
} else {
$title = $entry->$titleField;
}
$selectList[$id] = $title;
}
return $selectList;
}
}