mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-30 02:26:58 +00:00
All kinds of new code, especially for the piggy banks.
This commit is contained in:
@@ -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.
|
||||
*
|
||||
|
@@ -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.
|
||||
}
|
||||
}
|
@@ -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.
|
||||
}
|
||||
}
|
@@ -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.
|
||||
}
|
||||
}
|
@@ -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);
|
||||
|
||||
}
|
@@ -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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -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.
|
||||
}
|
||||
}
|
@@ -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.
|
||||
}
|
||||
}
|
@@ -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.
|
||||
}
|
||||
}
|
@@ -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.
|
||||
}
|
||||
}
|
@@ -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.
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user