mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-12-17 20:08:52 +00:00
Code cleanup.
This commit is contained in:
@@ -5,6 +5,7 @@ use Carbon\Carbon;
|
||||
use FireflyIII\Database\CommonDatabaseCalls;
|
||||
use FireflyIII\Database\CUD;
|
||||
use FireflyIII\Database\SwitchUser;
|
||||
use FireflyIII\Exception\FireflyException;
|
||||
use FireflyIII\Exception\NotImplementedException;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\MessageBag;
|
||||
@@ -42,6 +43,7 @@ class Budget implements CUD, CommonDatabaseCalls, BudgetInterface
|
||||
* @param array $data
|
||||
*
|
||||
* @return \Eloquent
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function store(array $data)
|
||||
{
|
||||
@@ -51,8 +53,8 @@ class Budget implements CUD, CommonDatabaseCalls, BudgetInterface
|
||||
$budget->class = 'Budget';
|
||||
|
||||
if (!$budget->isValid()) {
|
||||
var_dump($budget->getErrors()->all());
|
||||
exit;
|
||||
\Log::error('Could not store budget: ' . $budget->getErrors()->toJson());
|
||||
throw new FireflyException($budget->getErrors()->first());
|
||||
}
|
||||
$budget->save();
|
||||
|
||||
@@ -68,12 +70,6 @@ class Budget implements CUD, CommonDatabaseCalls, BudgetInterface
|
||||
public function update(\Eloquent $model, array $data)
|
||||
{
|
||||
$model->name = $data['name'];
|
||||
if (!$model->isValid()) {
|
||||
var_dump($model->getErrors()->all());
|
||||
exit;
|
||||
}
|
||||
|
||||
|
||||
$model->save();
|
||||
|
||||
return true;
|
||||
@@ -91,25 +87,8 @@ class Budget implements CUD, CommonDatabaseCalls, BudgetInterface
|
||||
{
|
||||
$warnings = new MessageBag;
|
||||
$successes = new MessageBag;
|
||||
$errors = new MessageBag;
|
||||
|
||||
if (isset($model['name'])) {
|
||||
if (strlen($model['name']) < 1) {
|
||||
$errors->add('name', 'Name is too short');
|
||||
}
|
||||
if (strlen($model['name']) > 200) {
|
||||
$errors->add('name', 'Name is too long');
|
||||
|
||||
}
|
||||
} else {
|
||||
$errors->add('name', 'Name is mandatory');
|
||||
}
|
||||
$validator = \Validator::make($model, \Component::$rules);
|
||||
|
||||
if ($validator->invalid()) {
|
||||
$errors->merge($validator->errors());
|
||||
}
|
||||
|
||||
$errors = $validator->errors();
|
||||
|
||||
if (!$errors->has('name')) {
|
||||
$successes->add('name', 'OK');
|
||||
@@ -274,12 +253,12 @@ class Budget implements CUD, CommonDatabaseCalls, BudgetInterface
|
||||
// Add expenses that have no budget:
|
||||
return $this->getUser()->transactionjournals()->whereNotIn(
|
||||
'transaction_journals.id', function ($query) use ($start, $end) {
|
||||
$query->select('transaction_journals.id')->from('transaction_journals')->leftJoin(
|
||||
'component_transaction_journal', 'component_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id'
|
||||
)->leftJoin('components', 'components.id', '=', 'component_transaction_journal.component_id')->where(
|
||||
'transaction_journals.date', '>=', $start->format('Y-m-d')
|
||||
)->where('transaction_journals.date', '<=', $end->format('Y-m-d'))->where('components.class', 'Budget');
|
||||
}
|
||||
$query->select('transaction_journals.id')->from('transaction_journals')->leftJoin(
|
||||
'component_transaction_journal', 'component_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id'
|
||||
)->leftJoin('components', 'components.id', '=', 'component_transaction_journal.component_id')->where(
|
||||
'transaction_journals.date', '>=', $start->format('Y-m-d')
|
||||
)->where('transaction_journals.date', '<=', $end->format('Y-m-d'))->where('components.class', 'Budget');
|
||||
}
|
||||
)->before($end)->after($start)->lessThan(0)->transactionTypes(['Withdrawal'])->get();
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ use Carbon\Carbon;
|
||||
use FireflyIII\Database\CommonDatabaseCalls;
|
||||
use FireflyIII\Database\CUD;
|
||||
use FireflyIII\Database\SwitchUser;
|
||||
use FireflyIII\Exception\FireflyException;
|
||||
use FireflyIII\Exception\NotImplementedException;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\MessageBag;
|
||||
@@ -43,6 +44,7 @@ class Category implements CUD, CommonDatabaseCalls
|
||||
* @param array $data
|
||||
*
|
||||
* @return \Eloquent
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function store(array $data)
|
||||
{
|
||||
@@ -51,8 +53,8 @@ class Category implements CUD, CommonDatabaseCalls
|
||||
$category->class = 'Category';
|
||||
$category->user()->associate($this->getUser());
|
||||
if (!$category->isValid()) {
|
||||
var_dump($category->getErrors());
|
||||
exit();
|
||||
\Log::error('Could not store category: ' . $category->getErrors()->toJson());
|
||||
throw new FireflyException($category->getErrors()->first());
|
||||
}
|
||||
$category->save();
|
||||
|
||||
@@ -61,16 +63,17 @@ class Category implements CUD, CommonDatabaseCalls
|
||||
|
||||
/**
|
||||
* @param \Eloquent $model
|
||||
* @param array $data
|
||||
* @param array $data
|
||||
*
|
||||
* @return bool
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function update(\Eloquent $model, array $data)
|
||||
{
|
||||
$model->name = $data['name'];
|
||||
if (!$model->isValid()) {
|
||||
var_dump($model->getErrors()->all());
|
||||
exit;
|
||||
\Log::error('Could not store category: ' . $model->getErrors()->toJson());
|
||||
throw new FireflyException($model->getErrors()->first());
|
||||
}
|
||||
|
||||
|
||||
@@ -91,25 +94,8 @@ class Category implements CUD, CommonDatabaseCalls
|
||||
{
|
||||
$warnings = new MessageBag;
|
||||
$successes = new MessageBag;
|
||||
$errors = new MessageBag;
|
||||
|
||||
if (isset($model['name'])) {
|
||||
if (strlen($model['name']) < 1) {
|
||||
$errors->add('name', 'Name is too short');
|
||||
}
|
||||
if (strlen($model['name']) > 200) {
|
||||
$errors->add('name', 'Name is too long');
|
||||
|
||||
}
|
||||
} else {
|
||||
$errors->add('name', 'Name is mandatory');
|
||||
}
|
||||
$validator = \Validator::make($model, \Component::$rules);
|
||||
|
||||
if ($validator->invalid()) {
|
||||
$errors->merge($validator->getErrors());
|
||||
}
|
||||
|
||||
$errors = $validator->errors();
|
||||
|
||||
if (!$errors->has('name')) {
|
||||
$successes->add('name', 'OK');
|
||||
@@ -171,7 +157,7 @@ class Category implements CUD, CommonDatabaseCalls
|
||||
/**
|
||||
* @param $name
|
||||
*
|
||||
* @return static
|
||||
* @return \Category
|
||||
*/
|
||||
public function firstOrCreate($name)
|
||||
{
|
||||
@@ -203,11 +189,12 @@ class Category implements CUD, CommonDatabaseCalls
|
||||
* @param Carbon $date
|
||||
*
|
||||
* @return null
|
||||
* @throws NotImplementedException
|
||||
* @internal param \Category $budget
|
||||
*/
|
||||
public function repetitionOnStartingOnDate(\Category $category, Carbon $date)
|
||||
{
|
||||
return null;
|
||||
throw new NotImplementedException;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -42,34 +42,22 @@ class PiggyBank implements CUD, CommonDatabaseCalls, PiggybankInterface
|
||||
* @param array $data
|
||||
*
|
||||
* @return \Eloquent
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function store(array $data)
|
||||
{
|
||||
$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;
|
||||
|
||||
if ($data['remind_me'] == 0) {
|
||||
if (!isset($data['remind_me']) || (isset($data['remind_me']) && $data['remind_me'] == 0)) {
|
||||
$data['reminder'] = null;
|
||||
}
|
||||
$piggyBank = new \Piggybank($data);
|
||||
$piggyBank->save();
|
||||
|
||||
|
||||
$piggybank = new \Piggybank($data);
|
||||
if (!$piggybank->isValid()) {
|
||||
var_dump($piggybank->getErrors()->all());
|
||||
exit;
|
||||
}
|
||||
$piggybank->save();
|
||||
|
||||
return $piggybank;
|
||||
return $piggyBank;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Eloquent $model
|
||||
* @param array $data
|
||||
* @param array $data
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
@@ -80,21 +68,16 @@ class PiggyBank implements CUD, CommonDatabaseCalls, PiggybankInterface
|
||||
$model->account_id = intval($data['account_id']);
|
||||
$model->targetamount = floatval($data['targetamount']);
|
||||
$model->targetdate = isset($data['targetdate']) && $data['targetdate'] != '' ? $data['targetdate'] : null;
|
||||
$model->rep_every = isset($data['rep_every']) ? $data['rep_every'] : 0;
|
||||
$model->reminder_skip = isset($data['reminder_skip']) ? $data['reminder_skip'] : 0;
|
||||
$model->order = isset($data['order']) ? $data['order'] : 0;
|
||||
$model->remind_me = isset($data['remind_me']) ? intval($data['remind_me']) : 0;
|
||||
$model->rep_every = intval($data['rep_every']);
|
||||
$model->reminder_skip = intval($data['reminder_skip']);
|
||||
$model->order = intval($data['order']);
|
||||
$model->remind_me = intval($data['remind_me']);
|
||||
$model->reminder = isset($data['reminder']) ? $data['reminder'] : 'month';
|
||||
|
||||
if ($model->remind_me == 0) {
|
||||
$model->reminder = null;
|
||||
}
|
||||
|
||||
if (!$model->isValid()) {
|
||||
var_dump($model->getErrors());
|
||||
exit();
|
||||
}
|
||||
|
||||
$model->save();
|
||||
|
||||
return true;
|
||||
@@ -241,8 +224,7 @@ class PiggyBank implements CUD, CommonDatabaseCalls, PiggybankInterface
|
||||
* @throws FireflyException
|
||||
* @throws NotImplementedException
|
||||
*/
|
||||
public function findRepetitionByDate(\Piggybank $piggybank, /** @noinspection PhpUnusedParameterInspection */
|
||||
Carbon $date)
|
||||
public function findRepetitionByDate(\Piggybank $piggybank, Carbon $date)
|
||||
{
|
||||
$reps = $piggybank->piggybankrepetitions()->get();
|
||||
if ($reps->count() == 1) {
|
||||
|
||||
@@ -37,245 +37,64 @@ class RepeatedExpense implements CUD, CommonDatabaseCalls, PiggybankInterface
|
||||
*
|
||||
* @param \PiggybankRepetition $repetition
|
||||
*
|
||||
* @return \PiggybankRepetition
|
||||
* @return Collection
|
||||
*/
|
||||
public function calculateParts(\PiggybankRepetition $repetition)
|
||||
{
|
||||
\Log::debug('NOW in calculateParts()');
|
||||
\Log::debug('Repetition id is ' . $repetition->id);
|
||||
/** @var \Piggybank $piggyBank */
|
||||
$piggyBank = $repetition->piggybank()->first();
|
||||
$bars = new Collection;
|
||||
\Log::debug('connected piggy bank is: ' . $piggyBank->name . ' (#' . $piggyBank->id . ')');
|
||||
|
||||
/*
|
||||
* If no reminders are set, the repetition is split in exactly one part:
|
||||
*/
|
||||
if (is_null($piggyBank->reminder)) {
|
||||
$part = new PiggybankPart;
|
||||
$part->setRepetition($repetition);
|
||||
$part->setAmountPerBar(floatval($piggyBank->targetamount));
|
||||
$part->setCurrentamount($repetition->currentamount);
|
||||
$part->setCumulativeAmount($piggyBank->targetamount);
|
||||
$part->setStartdate(clone $repetition->startdate);
|
||||
$part->setTargetdate(clone $repetition->targetdate);
|
||||
$bars->push($part);
|
||||
$repetition->bars = $bars;
|
||||
|
||||
return $repetition;
|
||||
}
|
||||
$piggyBank = $repetition->piggybank()->first();
|
||||
$bars = new Collection;
|
||||
$currentStart = clone $repetition->startdate;
|
||||
/*
|
||||
* Loop between start and target instead of counting manually.
|
||||
*/
|
||||
$index = 0;
|
||||
//echo 'Looping!<br>';
|
||||
//echo $repetition->startdate . ' until ' . $repetition->targetdate . '<br>';
|
||||
|
||||
if (is_null($piggyBank->reminder)) {
|
||||
$entry = ['repetition' => $repetition, 'amountPerBar' => floatval($piggyBank->targetamount),
|
||||
'currentAmount' => floatval($repetition->currentamount), 'cumulativeAmount' => floatval($piggyBank->targetamount),
|
||||
'startDate' => clone $repetition->startdate, 'targetDate' => clone $repetition->targetdate];
|
||||
$bars->push($this->createPiggyBankPart($entry));
|
||||
|
||||
return $bars;
|
||||
}
|
||||
|
||||
while ($currentStart < $repetition->targetdate) {
|
||||
$currentTarget = \DateKit::endOfX($currentStart, $piggyBank->reminder);
|
||||
if ($currentTarget > $repetition->targetdate) {
|
||||
$currentTarget = clone $repetition->targetdate;
|
||||
}
|
||||
|
||||
// create a part:
|
||||
$part = new PiggybankPart;
|
||||
$part->setRepetition($repetition);
|
||||
$part->setCurrentamount($repetition->currentamount);
|
||||
$part->setStartdate($currentStart);
|
||||
$part->setTargetdate($currentTarget);
|
||||
$bars->push($part);
|
||||
//echo 'Loop #' . $index . ', from ' . $currentStart . ' until ' . $currentTarget . '<br />';
|
||||
|
||||
|
||||
/*
|
||||
* Jump to the next period by adding a day.
|
||||
*/
|
||||
$currentTarget = \DateKit::endOfX($currentStart, $piggyBank->reminder, $repetition->targetdate);
|
||||
$entry = ['repetition' => $repetition, 'amountPerBar' => null, 'currentAmount' => floatval($repetition->currentamount),
|
||||
'cumulativeAmount' => null, 'startDate' => $currentStart, 'targetDate' => $currentTarget];
|
||||
$bars->push($this->createPiggyBankPart($entry));
|
||||
$currentStart = clone $currentTarget;
|
||||
$currentStart->addDay();//\DateKit::addPeriod($currentTarget, $piggyBank->reminder, 0);
|
||||
$index++;
|
||||
$currentStart->addDay();
|
||||
|
||||
}
|
||||
/*
|
||||
* Loop parts again to add some
|
||||
*/
|
||||
$parts = $bars->count();
|
||||
$amountPerBar = floatval($piggyBank->targetamount) / $parts;
|
||||
$amountPerBar = floatval($piggyBank->targetamount) / $bars->count();
|
||||
$cumulative = $amountPerBar;
|
||||
/** @var PiggybankPart $bar */
|
||||
foreach ($bars as $index => $bar) {
|
||||
$bar->setAmountPerBar($amountPerBar);
|
||||
$bar->setCumulativeAmount($cumulative);
|
||||
if ($parts - 1 == $index) {
|
||||
if ($bars->count() - 1 == $index) {
|
||||
$bar->setCumulativeAmount($piggyBank->targetamount);
|
||||
}
|
||||
|
||||
$reminder = $piggyBank->reminders()
|
||||
->where('startdate', $bar->getStartdate()->format('Y-m-d'))
|
||||
->where('enddate', $bar->getTargetdate()->format('Y-m-d'))
|
||||
->first();
|
||||
if ($reminder) {
|
||||
$bar->setReminder($reminder);
|
||||
}
|
||||
|
||||
$cumulative += $amountPerBar;
|
||||
}
|
||||
|
||||
$repetition->bars = $bars;
|
||||
return $bars;
|
||||
}
|
||||
|
||||
return $repetition;
|
||||
//
|
||||
// // if($parts > 12) {
|
||||
// // $parts = 12;
|
||||
// // $currentStart = \DateKit::startOfPeriod(Carbon::now(), $piggyBank->reminder);
|
||||
// // $currentEnd = \DateKit::endOfPeriod($currentEnd, $piggyBank->reminder);
|
||||
// // }
|
||||
//
|
||||
// for ($i = 0; $i < $parts; $i++) {
|
||||
// /*
|
||||
// * If it's not the first repetition, jump the start date a [period]
|
||||
// * and jump the target date a [period]
|
||||
// */
|
||||
// if ($i > 0) {
|
||||
// $currentStart = clone $currentTarget;
|
||||
// $currentStart->addDay();
|
||||
// $currentTarget = \DateKit::addPeriod($currentStart, $piggyBank->reminder, 0);
|
||||
// }
|
||||
// /*
|
||||
// * If it's the first one, and has reminders, jump to the end of the [period]
|
||||
// */
|
||||
// if ($i == 0 && !is_null($piggyBank->reminder)) {
|
||||
// $currentTarget = \DateKit::endOfX($currentStart, $piggyBank->reminder);
|
||||
// }
|
||||
// if ($currentStart > $repetition->targetdate) {
|
||||
// break;
|
||||
// }
|
||||
//
|
||||
//
|
||||
// /*
|
||||
// * Jump one month ahead after the first instance:
|
||||
// */
|
||||
// // if ($i > 0) {
|
||||
// // $currentStart = \DateKit::addPeriod($currentStart, $piggyBank->reminder, 0);
|
||||
// // /*
|
||||
// // * Jump to the start of the period too:
|
||||
// // */
|
||||
// // $currentStart = \DateKit::startOfPeriod($currentStart, $piggyBank->reminder);
|
||||
// //
|
||||
// // }
|
||||
//
|
||||
//
|
||||
// /*
|
||||
// * Move the current start to the actual start of
|
||||
// * the [period] once the first iteration has passed.
|
||||
// */
|
||||
// // if ($i != 0) {
|
||||
// // $currentStart = \DateKit::startOfPeriod($currentStart, $piggyBank->reminder);
|
||||
// // }
|
||||
// // if($i == 0 && !is_null($piggyBank->reminder)) {
|
||||
// // $currentEnd = \DateKit::startOfPeriod($currentStart, $piggyBank->reminder);
|
||||
// // $currentEnd = \DateKit::endOfPeriod($currentEnd, $piggyBank->reminder);
|
||||
// // }
|
||||
//
|
||||
// $part = new PiggybankPart;
|
||||
// $part->setRepetition($repetition);
|
||||
// $part->setAmount($currentAmount);
|
||||
// $part->setAmountPerBar($amountPerBar);
|
||||
// $part->setCurrentamount($repetition->currentamount);
|
||||
// $part->setStartdate($currentStart);
|
||||
// $part->setTargetdate($currentTarget);
|
||||
// if (!is_null($piggyBank->reminder)) {
|
||||
// // might be a reminder for this range?
|
||||
// $reminder = $piggyBank->reminders()
|
||||
// ->where('startdate', $currentStart->format('Y-m-d'))
|
||||
// ->where('enddate', $currentTarget->format('Y-m-d'))
|
||||
// ->first();
|
||||
// if ($reminder) {
|
||||
// $part->setReminder($reminder);
|
||||
// }
|
||||
//
|
||||
// }
|
||||
//
|
||||
// // if (!is_null($piggyBank->reminder)) {
|
||||
// // $currentStart = \DateKit::addPeriod($currentStart, $piggyBank->reminder, 0);
|
||||
// // $currentEnd = \DateKit::endOfPeriod($currentStart, $piggyBank->reminder);
|
||||
// // }
|
||||
//
|
||||
//
|
||||
// $bars->push($part);
|
||||
// $currentAmount += $amountPerBar;
|
||||
// }
|
||||
// $repetition->bars = $bars;
|
||||
//
|
||||
// return $repetition;
|
||||
// exit;
|
||||
//
|
||||
//
|
||||
// $repetition->hello = 'World!';
|
||||
//
|
||||
// return $repetition;
|
||||
//
|
||||
// $return = new Collection;
|
||||
// $repetitions = $piggyBank->piggybankrepetitions()->get();
|
||||
// /** @var \PiggybankRepetition $repetition */
|
||||
// foreach ($repetitions as $repetition) {
|
||||
//
|
||||
//
|
||||
// if (is_null($piggyBank->reminder)) {
|
||||
// // simple, one part "repetition".
|
||||
// $part = new PiggybankPart;
|
||||
// $part->setRepetition($repetition);
|
||||
// } else {
|
||||
// $part = new PiggybankPart;
|
||||
// }
|
||||
//
|
||||
//
|
||||
// // end!
|
||||
// $return->push($part);
|
||||
// }
|
||||
//
|
||||
// exit;
|
||||
//
|
||||
// return $return;
|
||||
// $piggyBank->currentRelevantRep(); // get the current relevant repetition.
|
||||
// if (!is_null($piggyBank->reminder)) {
|
||||
// switch ($piggyBank->reminder) {
|
||||
// default:
|
||||
// throw new FireflyException('Cannot handle "' . $piggyBank->reminder . '" reminders for repeated expenses');
|
||||
// break;
|
||||
// case 'month':
|
||||
// $start = clone $piggyBank->currentRep->startdate;
|
||||
// $start->startOfMonth();
|
||||
// $end = clone $piggyBank->currentRep->targetdate;
|
||||
// $end->endOfMonth();
|
||||
// $piggyBank->parts = $start->diffInMonths($end);
|
||||
// unset($start, $end);
|
||||
// break;
|
||||
// }
|
||||
//
|
||||
// } else {
|
||||
// $piggyBank->parts = 1;
|
||||
// }
|
||||
//
|
||||
// // number of bars:
|
||||
// $piggyBank->barCount = floor(12 / $piggyBank->parts) == 0 ? 1 : floor(12 / $piggyBank->parts);
|
||||
// $amountPerBar = floatval($piggyBank->targetamount) / $piggyBank->parts;
|
||||
// $currentAmount = floatval($amountPerBar);
|
||||
// $bars = [];
|
||||
// $currentStart = clone $piggyBank->currentRep->startdate;
|
||||
// for ($i = 0; $i < $piggyBank->parts; $i++) {
|
||||
// // niet elke keer een andere dinges pakken? om target te redden?
|
||||
// if (!is_null($piggyBank->reminder)) {
|
||||
// $currentStart = \DateKit::addPeriod($currentStart, $piggyBank->reminder, 0);
|
||||
// }
|
||||
// $bars[] = [
|
||||
// 'amount' => $currentAmount,
|
||||
// 'date' => $currentStart
|
||||
// ];
|
||||
//
|
||||
//
|
||||
// $currentAmount += $amountPerBar;
|
||||
// }
|
||||
// $piggyBank->bars = $bars;
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
* @return PiggybankPart
|
||||
*/
|
||||
public function createPiggyBankPart(array $data)
|
||||
{
|
||||
$part = new PiggybankPart;
|
||||
$part->setRepetition($data['repetition']);
|
||||
$part->setAmountPerBar($data['amountPerBar']);
|
||||
$part->setCurrentamount($data['currentAmount']);
|
||||
$part->setCumulativeAmount($data['cumulativeAmount']);
|
||||
$part->setStartdate($data['startDate']);
|
||||
$part->setTargetdate($data['targetDate']);
|
||||
|
||||
return $part;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -47,7 +47,6 @@ class RecurringTransaction implements CUD, CommonDatabaseCalls, RecurringTransac
|
||||
*/
|
||||
public function store(array $data)
|
||||
{
|
||||
var_dump($data);
|
||||
$recurring = new \RecurringTransaction;
|
||||
$recurring->user()->associate($this->getUser());
|
||||
$recurring->name = $data['name'];
|
||||
@@ -58,22 +57,17 @@ class RecurringTransaction implements CUD, CommonDatabaseCalls, RecurringTransac
|
||||
$date = new Carbon($data['date']);
|
||||
|
||||
|
||||
$recurring->active = isset($data['active']) && intval($data['active']) == 1 ? 1 : 0;
|
||||
$recurring->automatch = isset($data['automatch']) && intval($data['automatch']) == 1 ? 1 : 0;
|
||||
$recurring->active = intval($data['active']);
|
||||
$recurring->automatch = intval($data['automatch']);
|
||||
$recurring->repeat_freq = $data['repeat_freq'];
|
||||
|
||||
/*
|
||||
* Jump to the start of the period.
|
||||
*/
|
||||
$date = DateKit::startOfPeriod($date, $data['repeat_freq']);
|
||||
$date = \DateKit::startOfPeriod($date, $data['repeat_freq']);
|
||||
$recurring->date = $date;
|
||||
$recurring->skip = intval($data['skip']);
|
||||
|
||||
if (!$recurring->isValid()) {
|
||||
var_dump($recurring->getErrors());
|
||||
exit();
|
||||
}
|
||||
|
||||
$recurring->save();
|
||||
|
||||
return $recurring;
|
||||
@@ -87,8 +81,6 @@ class RecurringTransaction implements CUD, CommonDatabaseCalls, RecurringTransac
|
||||
*/
|
||||
public function update(\Eloquent $model, array $data)
|
||||
{
|
||||
var_dump($data);
|
||||
|
||||
$model->name = $data['name'];
|
||||
$model->match = $data['match'];
|
||||
$model->amount_max = floatval($data['amount_max']);
|
||||
@@ -97,16 +89,10 @@ class RecurringTransaction implements CUD, CommonDatabaseCalls, RecurringTransac
|
||||
$date = new Carbon($data['date']);
|
||||
|
||||
$model->date = $date;
|
||||
$model->active = isset($data['active']) && intval($data['active']) == 1 ? 1 : 0;
|
||||
$model->automatch = isset($data['automatch']) && intval($data['automatch']) == 1 ? 1 : 0;
|
||||
$model->active = intval($data['active']);
|
||||
$model->automatch = intval($data['automatch']);
|
||||
$model->repeat_freq = $data['repeat_freq'];
|
||||
$model->skip = intval($data['skip']);
|
||||
|
||||
if (!$model->isValid()) {
|
||||
var_dump($model->getErrors());
|
||||
exit();
|
||||
}
|
||||
|
||||
$model->save();
|
||||
|
||||
return true;
|
||||
|
||||
@@ -78,64 +78,19 @@ class Transaction implements CUD, CommonDatabaseCalls
|
||||
*
|
||||
* @param array $model
|
||||
*
|
||||
* @return array
|
||||
* @return MessageBag
|
||||
*/
|
||||
public function validate(array $model)
|
||||
{
|
||||
$warnings = new MessageBag;
|
||||
$successes = new MessageBag;
|
||||
$errors = new MessageBag;
|
||||
|
||||
|
||||
if (!isset($model['account_id']) && !isset($model['account'])) {
|
||||
$errors = new MessageBag;
|
||||
if (is_null($model['account'])) {
|
||||
$errors->add('account', 'No account present');
|
||||
}
|
||||
if (isset($model['account']) && !($model['account'] instanceof \Account)) {
|
||||
$errors->add('account', 'No valid account present');
|
||||
}
|
||||
if (isset($model['account_id']) && intval($model['account_id']) < 0) {
|
||||
$errors->add('account', 'No valid account_id present');
|
||||
if (is_null($model['transaction_journal'])) {
|
||||
$errors->add('transaction_journal', 'No valid transaction journal present');
|
||||
}
|
||||
|
||||
if (isset($model['piggybank_id']) && intval($model['piggybank_id']) < 0) {
|
||||
$errors->add('piggybank', 'No valid piggybank_id present');
|
||||
}
|
||||
|
||||
if (!isset($model['transaction_journal_id']) && !isset($model['transaction_journal'])) {
|
||||
$errors->add('transaction_journal', 'No TJ present');
|
||||
}
|
||||
if (isset($model['transaction_journal']) && !($model['transaction_journal'] instanceof \TransactionJournal)) {
|
||||
$errors->add('transaction_journal', 'No valid transaction_journal present');
|
||||
}
|
||||
if (isset($model['transaction_journal_id']) && intval($model['transaction_journal_id']) < 0) {
|
||||
$errors->add('account', 'No valid transaction_journal_id present');
|
||||
}
|
||||
|
||||
if (isset($model['description']) && strlen($model['description']) > 255) {
|
||||
$errors->add('account', 'Description too long');
|
||||
}
|
||||
|
||||
if (!isset($model['amount'])) {
|
||||
$errors->add('amount', 'No amount present.');
|
||||
}
|
||||
if (isset($model['amount']) && floatval($model['amount']) == 0) {
|
||||
$errors->add('amount', 'Invalid amount.');
|
||||
}
|
||||
|
||||
if (!$errors->has('account')) {
|
||||
$successes->add('account', 'OK');
|
||||
}
|
||||
if (!$errors->has('')) {
|
||||
$successes->add('piggybank', 'OK');
|
||||
}
|
||||
if (!$errors->has('transaction_journal')) {
|
||||
$successes->add('transaction_journal', 'OK');
|
||||
}
|
||||
if (!$errors->has('amount')) {
|
||||
$successes->add('amount', 'OK');
|
||||
}
|
||||
|
||||
return ['errors' => $errors, 'warnings' => $warnings, 'successes' => $successes];
|
||||
return $errors;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -62,111 +62,24 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData
|
||||
*/
|
||||
public function store(array $data)
|
||||
{
|
||||
/** @var \FireflyIII\Database\TransactionType\TransactionType $typeRepository */
|
||||
$typeRepository = \App::make('FireflyIII\Database\TransactionType\TransactionType');
|
||||
|
||||
/** @var \FireflyIII\Database\Account\Account $accountRepository */
|
||||
$accountRepository = \App::make('FireflyIII\Database\Account\Account');
|
||||
|
||||
/** @var \FireflyIII\Database\TransactionCurrency\TransactionCurrency $currencyRepository */
|
||||
$currencyRepository = \App::make('FireflyIII\Database\TransactionCurrency\TransactionCurrency');
|
||||
|
||||
/** @var \FireflyIII\Database\Transaction\Transaction $transactionRepository */
|
||||
$transactionRepository = \App::make('FireflyIII\Database\Transaction\Transaction');
|
||||
|
||||
$journalType = $typeRepository->findByWhat($data['what']);
|
||||
$currency = $currencyRepository->findByCode($data['currency']);
|
||||
|
||||
$journal = new \TransactionJournal;
|
||||
$journal->transactionType()->associate($journalType);
|
||||
$journal->transactionCurrency()->associate($currency);
|
||||
$journal->user()->associate($this->getUser());
|
||||
$journal->description = $data['description'];
|
||||
$journal->date = $data['date'];
|
||||
$journal->completed = 0;
|
||||
|
||||
/*
|
||||
* This must be enough to store the journal:
|
||||
*/
|
||||
if (!$journal->isValid()) {
|
||||
\Log::error($journal->getErrors()->all());
|
||||
throw new FireflyException('store() transaction journal failed, but it should not!');
|
||||
}
|
||||
$journalType = $this->getJournalType($data['what']);
|
||||
$currency = $this->getJournalCurrency($data['currency']);
|
||||
$journal = new \TransactionJournal(
|
||||
['transaction_type_id' => $journalType->id, 'transaction_currency_id' => $currency->id, 'user_id' => $this->getUser()->id,
|
||||
'description' => $data['description'], 'date' => $data['date'], 'completed' => 0]
|
||||
);
|
||||
$journal->save();
|
||||
|
||||
/*
|
||||
* Still need to find the accounts related to the transactions.
|
||||
* This depends on the type of transaction.
|
||||
*/
|
||||
switch ($data['what']) {
|
||||
case 'withdrawal':
|
||||
$data['from'] = $accountRepository->find($data['account_id']);
|
||||
$data['to'] = $accountRepository->firstExpenseAccountOrCreate($data['expense_account']);
|
||||
break;
|
||||
case 'opening':
|
||||
break;
|
||||
case 'deposit':
|
||||
$data['to'] = $accountRepository->find($data['account_id']);
|
||||
$data['from'] = $accountRepository->firstRevenueAccountOrCreate($data['revenue_account']);
|
||||
break;
|
||||
case 'transfer':
|
||||
$data['from'] = $accountRepository->find($data['account_from_id']);
|
||||
$data['to'] = $accountRepository->find($data['account_to_id']);
|
||||
break;
|
||||
list($fromAccount, $toAccount) = $this->storeAccounts($data);
|
||||
|
||||
default:
|
||||
throw new FireflyException('Cannot save transaction journal with accounts based on $what "' . $data['what'] . '".');
|
||||
break;
|
||||
}
|
||||
|
||||
/*
|
||||
* Then store both transactions.
|
||||
*/
|
||||
$first = ['account' => $data['from'], 'transaction_journal' => $journal, 'amount' => ($data['amount'] * -1),];
|
||||
$validate = $transactionRepository->validate($first);
|
||||
if ($validate['errors']->count() == 0) {
|
||||
$transactionRepository->store($first);
|
||||
} else {
|
||||
throw new FireflyException($validate['errors']->first());
|
||||
}
|
||||
|
||||
$second = ['account' => $data['to'], 'transaction_journal' => $journal, 'amount' => floatval($data['amount']),];
|
||||
|
||||
$validate = $transactionRepository->validate($second);
|
||||
if ($validate['errors']->count() == 0) {
|
||||
$transactionRepository->store($second);
|
||||
} else {
|
||||
throw new FireflyException($validate['errors']->first());
|
||||
}
|
||||
|
||||
/*
|
||||
* Store the budget.
|
||||
*/
|
||||
if (isset($data['budget_id']) && intval($data['budget_id']) > 0) {
|
||||
/** @var \FireflyIII\Database\Budget\Budget $budgetRepository */
|
||||
$budgetRepository = \App::make('FireflyIII\Database\Budget\Budget');
|
||||
$budget = $budgetRepository->find(intval($data['budget_id']));
|
||||
if ($budget) {
|
||||
$journal->budgets()->save($budget);
|
||||
}
|
||||
}
|
||||
if (isset($data['category']) && strlen($data['category']) > 0) {
|
||||
/** @var \FireflyIII\Database\Category\Category $categoryRepository */
|
||||
$categoryRepository = \App::make('FireflyIII\Database\Category\Category');
|
||||
$category = $categoryRepository->firstOrCreate($data['category']);
|
||||
if ($category) {
|
||||
$journal->categories()->save($category);
|
||||
}
|
||||
}
|
||||
$this->storeTransaction(['account' => $fromAccount, 'transaction_journal' => $journal, 'amount' => floatval($data['amount'] * -1)]);
|
||||
$this->storeTransaction(['account' => $toAccount, 'transaction_journal' => $journal, 'amount' => floatval($data['amount'])]);
|
||||
$this->storeBudget($data, $journal);
|
||||
$this->storeCategory($data, $journal);
|
||||
|
||||
$journal->completed = 1;
|
||||
$journal->save();
|
||||
|
||||
/*
|
||||
* Trigger a search for a relevant recurring transaction.
|
||||
*/
|
||||
|
||||
|
||||
return $journal;
|
||||
}
|
||||
|
||||
@@ -254,6 +167,7 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData
|
||||
$model->components()->sync($components);
|
||||
|
||||
/*
|
||||
* TODO move to transaction thing.
|
||||
* Now we can update the transactions related to this journal.
|
||||
*/
|
||||
$amount = floatval($data['amount']);
|
||||
@@ -439,6 +353,123 @@ class TransactionJournal implements TransactionJournalInterface, CUD, CommonData
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $type
|
||||
*
|
||||
* @return \AccountType|null
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function getJournalType($type)
|
||||
{
|
||||
/** @var \FireflyIII\Database\TransactionType\TransactionType $typeRepository */
|
||||
$typeRepository = \App::make('FireflyIII\Database\TransactionType\TransactionType');
|
||||
|
||||
return $typeRepository->findByWhat($type);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $currency
|
||||
*
|
||||
* @return null|\TransactionCurrency
|
||||
*/
|
||||
public function getJournalCurrency($currency)
|
||||
{
|
||||
/** @var \FireflyIII\Database\TransactionCurrency\TransactionCurrency $currencyRepository */
|
||||
$currencyRepository = \App::make('FireflyIII\Database\TransactionCurrency\TransactionCurrency');
|
||||
|
||||
return $currencyRepository->findByCode($currency);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
* @return array
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function storeAccounts(array $data)
|
||||
{
|
||||
/** @var \FireflyIII\Database\Account\Account $accountRepository */
|
||||
$accountRepository = \App::make('FireflyIII\Database\Account\Account');
|
||||
$fromAccount = null;
|
||||
$toAccount = null;
|
||||
|
||||
switch ($data['what']) {
|
||||
case 'withdrawal':
|
||||
$fromAccount = $accountRepository->find($data['account_id']);
|
||||
$toAccount = $accountRepository->firstExpenseAccountOrCreate($data['expense_account']);
|
||||
break;
|
||||
case 'opening':
|
||||
break;
|
||||
case 'deposit':
|
||||
$fromAccount = $accountRepository->firstRevenueAccountOrCreate($data['revenue_account']);
|
||||
$toAccount = $accountRepository->find($data['account_id']);
|
||||
break;
|
||||
case 'transfer':
|
||||
$fromAccount = $accountRepository->find($data['account_from_id']);
|
||||
$toAccount = $accountRepository->find($data['account_to_id']);
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new FireflyException('Cannot save transaction journal with accounts based on $what "' . $data['what'] . '".');
|
||||
break;
|
||||
}
|
||||
|
||||
return [$fromAccount, $toAccount];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
* @return \Eloquent
|
||||
* @throws FireflyException
|
||||
*/
|
||||
public function storeTransaction($data)
|
||||
{
|
||||
|
||||
/** @var \FireflyIII\Database\Transaction\Transaction $repository */
|
||||
$repository = \App::make('FireflyIII\Database\Transaction\Transaction');
|
||||
|
||||
$errors = $repository->validate($data);
|
||||
if ($errors->count() > 0) {
|
||||
\Log::error('Could not store transaction: ' . $errors->toJson());
|
||||
throw new FireflyException('store() transaction failed, but it should not!');
|
||||
}
|
||||
|
||||
return $repository->store($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @param \TransactionJournal $journal
|
||||
*/
|
||||
public function storeBudget($data, \TransactionJournal $journal)
|
||||
{
|
||||
if (isset($data['budget_id']) && intval($data['budget_id']) > 0) {
|
||||
/** @var \FireflyIII\Database\Budget\Budget $budgetRepository */
|
||||
$budgetRepository = \App::make('FireflyIII\Database\Budget\Budget');
|
||||
$budget = $budgetRepository->find(intval($data['budget_id']));
|
||||
if ($budget) {
|
||||
$journal->budgets()->save($budget);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @param \TransactionJournal $journal
|
||||
*/
|
||||
public function storeCategory(array $data, \TransactionJournal $journal)
|
||||
{
|
||||
if (isset($data['category']) && strlen($data['category']) > 0) {
|
||||
/** @var \FireflyIII\Database\Category\Category $categoryRepository */
|
||||
$categoryRepository = \App::make('FireflyIII\Database\Category\Category');
|
||||
$category = $categoryRepository->firstOrCreate($data['category']);
|
||||
if ($category) {
|
||||
$journal->categories()->save($category);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an object with id $id.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user