Extended forms and started on recurring transactions.

This commit is contained in:
Sander Dorigo
2014-10-05 19:29:25 +02:00
parent 980d9ce885
commit 28aaea1aa3
10 changed files with 193 additions and 201 deletions

View File

@@ -16,19 +16,52 @@ class Form
* @return string
* @throws FireflyException
*/
public static function ffNumber($name, $value = null, array $options = [])
public static function ffInteger($name, $value = null, array $options = [])
{
$options['step'] = 'any';
$options['min'] = '0.01';
$options['step'] = '1';
return self::ffInput('number', $name, $value, $options);
}
public static function ffCheckbox($name, $value = 1, $checked = null, $options = [])
{
$options['checked'] = $checked ? true : null;
return self::ffInput('checkbox', $name, $value, $options);
}
public static function ffAmount($name, $value = null, array $options = [])
{
$options['step'] = 'any';
$options['min'] = '0.01';
return self::ffInput('amount', $name, $value, $options);
}
/**
* @param $name
* @param null $value
* @param array $options
* @return string
* @throws FireflyException
*/
public static function ffDate($name, $value = null, array $options = [])
{
return self::ffInput('date', $name, $value, $options);
}
/**
* @param $name
* @param null $value
* @param array $options
* @return string
* @throws FireflyException
*/
public static function ffTags($name, $value = null, array $options = [])
{
$options['data-role'] = 'tagsinput';
return self::ffInput('text', $name, $value, $options);
}
/**
* @param $name
* @param array $list
@@ -55,6 +88,22 @@ class Form
}
public static function label($name)
{
$labels = [
'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'
];
return isset($labels[$name]) ? $labels[$name] : str_replace('_',' ',ucfirst($name));
}
/**
* @param $type
* @param $name
@@ -72,18 +121,11 @@ class Form
$options['class'] = 'form-control';
$options['id'] = 'ffInput_' . $name;
$options['autocomplete'] = 'off';
$options['type'] = 'text';
$label = self::label($name);
/*
* Make label and placeholder look nice.
*/
$options['placeholder'] = isset($options['label']) ? $options['label'] : ucfirst($name);
$options['label'] = isset($options['label']) ? $options['label'] : ucfirst($name);
if ($name == 'account_id') {
$options['label'] = 'Asset account';
}
$options['label'] = str_replace(['_'], [' '], $options['label']);
$options['placeholder'] = str_replace(['_'], [' '], $options['placeholder']);
$options['placeholder'] = ucfirst($name);
/*
* Get the value.
@@ -129,7 +171,6 @@ class Form
/*
* Add some HTML.
*/
$label = isset($options['label']) ? $options['label'] : ucfirst($name);
$html = '<div class="' . $classes . '">';
$html .= '<label for="' . $options['id'] . '" class="col-sm-4 control-label">' . $label . '</label>';
$html .= '<div class="col-sm-8">';
@@ -138,14 +179,27 @@ class Form
/*
* Switch input type:
*/
unset($options['label']);
switch ($type) {
case 'text':
$html .= \Form::input('text', $name, $value, $options);
break;
case 'number':
case 'amount':
$html .= '<div class="input-group"><div class="input-group-addon">&euro;</div>';
$html .= \Form::input('number', $name, $value, $options);
$html .= '</div>';
break;
case 'number':
$html .= \Form::input('number', $name, $value, $options);
break;
case 'checkbox':
$checked = $options['checked'];
unset($options['checked'], $options['placeholder'], $options['autocomplete'], $options['class']);
$html .= '<div class="checkbox"><label>';
$html .= \Form::checkbox($name, $value, $checked, $options);
$html .= '</label></div>';
break;
case 'date':
$html .= \Form::input('date', $name, $value, $options);

View File

@@ -5,6 +5,7 @@ namespace Firefly\Storage\RecurringTransaction;
use Carbon\Carbon;
use Illuminate\Queue\Jobs\Job;
use Illuminate\Support\MessageBag;
/**
* Class EloquentRecurringTransactionRepository
@@ -45,7 +46,7 @@ class EloquentRecurringTransactionRepository implements RecurringTransactionRepo
}
/**
* @param Job $job
* @param Job $job
* @param array $payload
*
* @return mixed
@@ -57,13 +58,13 @@ class EloquentRecurringTransactionRepository implements RecurringTransactionRepo
/** @var \Importmap $importMap */
$importMap = $repository->findImportmap($payload['mapID']);
$user = $importMap->user;
$user = $importMap->user;
$this->overruleUser($user);
/*
* maybe the recurring transaction is already imported:
*/
$oldId = intval($payload['data']['id']);
$oldId = intval($payload['data']['id']);
$description = $payload['data']['description'];
$importEntry = $repository->findImportEntry($importMap, 'RecurringTransaction', $oldId);
@@ -84,17 +85,17 @@ class EloquentRecurringTransactionRepository implements RecurringTransactionRepo
$recurringTransaction = $this->findByName($payload['data']['description']);
if (is_null($recurringTransaction)) {
$amount = floatval($payload['data']['amount']);
$pct = intval($payload['data']['pct']);
$pct = intval($payload['data']['pct']);
$set = [
'name' => $description,
'match' => join(',', explode(' ', $description)),
'amount_min' => $amount * ($pct / 100) * -1,
'amount_max' => $amount * (1 + ($pct / 100)) * -1,
'date' => date('Y-m-') . $payload['data']['dom'],
'name' => $description,
'match' => join(',', explode(' ', $description)),
'amount_min' => $amount * ($pct / 100) * -1,
'amount_max' => $amount * (1 + ($pct / 100)) * -1,
'date' => date('Y-m-') . $payload['data']['dom'],
'repeat_freq' => 'monthly',
'active' => intval($payload['data']['inactive']) == 1 ? 0 : 1,
'automatch' => 1,
'active' => intval($payload['data']['inactive']) == 1 ? 0 : 1,
'automatch' => 1,
];
$recurringTransaction = $this->store($set);
@@ -131,45 +132,52 @@ class EloquentRecurringTransactionRepository implements RecurringTransactionRepo
/**
* @param $data
*
* @return mixed|\RecurringTransaction
* @return MessageBag
*/
public function store($data)
{
$messageBag = new MessageBag;
$recurringTransaction = new \RecurringTransaction(
[
'user_id' => $this->_user->id,
'name' => $data['name'],
'match' => join(' ', explode(',', $data['match'])),
'amount_max' => floatval($data['amount_max']),
'amount_min' => floatval($data['amount_min']),
'date' => new Carbon($data['date']),
'active' => isset($data['active']) ? intval($data['active']) : 0,
'automatch' => isset($data['automatch']) ? intval($data['automatch']) : 0,
'skip' => isset($data['skip']) ? intval($data['skip']) : 0,
'user_id' => $this->_user->id,
'name' => $data['name'],
'match' => join(' ', explode(',', $data['match'])),
'amount_max' => floatval($data['amount_max']),
'amount_min' => floatval($data['amount_min']),
'date' => new Carbon($data['date']),
'active' => isset($data['active']) ? intval($data['active']) : 0,
'automatch' => isset($data['automatch']) ? intval($data['automatch']) : 0,
'skip' => isset($data['skip']) ? intval($data['skip']) : 0,
'repeat_freq' => $data['repeat_freq'],
]
);
// unique name?
$count = $this->_user->recurringtransactions()->whereName($data['name'])->count();
if($count > 0) {
$messageBag->add('name', 'A recurring transaction with this name already exists.');
return $messageBag;
}
// both amounts zero?:
if ($recurringTransaction->amount_max == 0 && $recurringTransaction->amount_min == 0) {
$recurringTransaction->errors()->add('amount_max', 'Amount max and min cannot both be zero.');
return $recurringTransaction;
$messageBag->add('amount_max', 'Amount max and min cannot both be zero.');
return $messageBag;
}
if ($recurringTransaction->amount_max < $recurringTransaction->amount_min) {
$recurringTransaction->errors()->add('amount_max', 'Amount max must be more than amount min.');
return $recurringTransaction;
$messageBag->add('amount_max', 'Amount max must be more than amount min.');
return $messageBag;
}
if ($recurringTransaction->amount_min > $recurringTransaction->amount_max) {
$recurringTransaction->errors()->add('amount_max', 'Amount min must be less than amount max.');
return $recurringTransaction;
$messageBag->add('amount_max', 'Amount min must be less than amount max.');
return $messageBag;
}
if($recurringTransaction->date < Carbon::now()) {
$recurringTransaction->errors()->add('date', 'Must be in the future.');
return $recurringTransaction;
if ($recurringTransaction->date < Carbon::now()) {
$messageBag->add('date', 'Must be in the future.');
return $messageBag;
}
@@ -177,7 +185,7 @@ class EloquentRecurringTransactionRepository implements RecurringTransactionRepo
$recurringTransaction->save();
}
return $recurringTransaction;
return $messageBag;
}
/**
@@ -188,8 +196,8 @@ class EloquentRecurringTransactionRepository implements RecurringTransactionRepo
*/
public function update(\RecurringTransaction $recurringTransaction, $data)
{
$recurringTransaction->name = $data['name'];
$recurringTransaction->match = join(' ', explode(',', $data['match']));
$recurringTransaction->name = $data['name'];
$recurringTransaction->match = join(' ', explode(',', $data['match']));
$recurringTransaction->amount_max = floatval($data['amount_max']);
$recurringTransaction->amount_min = floatval($data['amount_min']);
@@ -199,10 +207,10 @@ class EloquentRecurringTransactionRepository implements RecurringTransactionRepo
return $recurringTransaction;
}
$recurringTransaction->date = new Carbon($data['date']);
$recurringTransaction->active = isset($data['active']) ? intval($data['active']) : 0;
$recurringTransaction->automatch = isset($data['automatch']) ? intval($data['automatch']) : 0;
$recurringTransaction->skip = isset($data['skip']) ? intval($data['skip']) : 0;
$recurringTransaction->date = new Carbon($data['date']);
$recurringTransaction->active = isset($data['active']) ? intval($data['active']) : 0;
$recurringTransaction->automatch = isset($data['automatch']) ? intval($data['automatch']) : 0;
$recurringTransaction->skip = isset($data['skip']) ? intval($data['skip']) : 0;
$recurringTransaction->repeat_freq = $data['repeat_freq'];
if ($recurringTransaction->validate()) {

View File

@@ -3,6 +3,7 @@
namespace Firefly\Storage\RecurringTransaction;
use Illuminate\Queue\Jobs\Job;
use Illuminate\Support\MessageBag;
/**
* Interface RecurringTransactionRepositoryInterface
@@ -34,7 +35,7 @@ interface RecurringTransactionRepositoryInterface
/**
* @param $data
*
* @return mixed
* @return MessageBag
*/
public function store($data);