Clean up old tests.

This commit is contained in:
James Cole
2021-03-12 18:31:19 +01:00
parent a05d006fa7
commit 81f5224b11
36 changed files with 792 additions and 3261 deletions

View File

@@ -27,6 +27,7 @@ use Exception;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Note;
use FireflyIII\Models\Recurrence;
use FireflyIII\Models\RecurrenceRepetition;
use FireflyIII\Services\Internal\Support\RecurringTransactionTrait;
use FireflyIII\Services\Internal\Support\TransactionTypeTrait;
use FireflyIII\User;
@@ -56,53 +57,55 @@ class RecurrenceUpdateService
*/
public function update(Recurrence $recurrence, array $data): Recurrence
{
$this->user = $recurrence->user;
$transactionType = $recurrence->transactionType;
if (isset($data['recurrence']['type'])) {
$transactionType = $this->findTransactionType(ucfirst($data['recurrence']['type']));
}
$this->user = $recurrence->user;
// update basic fields first:
$recurrence->transaction_type_id = $transactionType->id;
$recurrence->title = $data['recurrence']['title'] ?? $recurrence->title;
$recurrence->description = $data['recurrence']['description'] ?? $recurrence->description;
$recurrence->first_date = $data['recurrence']['first_date'] ?? $recurrence->first_date;
$recurrence->repeat_until = $data['recurrence']['repeat_until'] ?? $recurrence->repeat_until;
$recurrence->repetitions = $data['recurrence']['nr_of_repetitions'] ?? $recurrence->repetitions;
$recurrence->apply_rules = $data['recurrence']['apply_rules'] ?? $recurrence->apply_rules;
$recurrence->active = $data['recurrence']['active'] ?? $recurrence->active;
// if nr_of_repetitions is set, then drop the "repeat_until" field.
if (0 !== $recurrence->repetitions) {
$recurrence->repeat_until = null;
}
if (isset($data['recurrence']['repetition_end'])) {
if (in_array($data['recurrence']['repetition_end'], ['forever', 'until_date'])) {
$recurrence->repetitions = 0;
if (array_key_exists('recurrence', $data)) {
$info = $data['recurrence'];
if (array_key_exists('title', $info)) {
$recurrence->title = $info['title'];
}
if (in_array($data['recurrence']['repetition_end'], ['forever', 'times'])) {
$recurrence->repeat_until = null;
if (array_key_exists('description', $info)) {
$recurrence->description = $info['description'];
}
if (array_key_exists('first_date', $info)) {
$recurrence->first_date = $info['first_date'];
}
if (array_key_exists('repeat_until', $info)) {
$recurrence->repeat_until = $info['repeat_until'];
$recurrence->repetitions = 0;
}
if (array_key_exists('nr_of_repetitions', $info)) {
if (0 !== (int)$info['nr_of_repetitions']) {
$recurrence->repeat_until = null;
}
$recurrence->repetitions = $info['nr_of_repetitions'];
}
if (array_key_exists('apply_rules', $info)) {
$recurrence->apply_rules = $info['apply_rules'];
}
if (array_key_exists('active', $info)) {
$recurrence->active = $info['active'];
}
// update all meta data:
if (array_key_exists('notes', $info)) {
$this->setNoteText($recurrence, $info['notes']);
}
}
$recurrence->save();
// update all meta data:
if (isset($data['recurrence']['notes']) && null !== $data['recurrence']['notes']) {
$this->setNoteText($recurrence, $data['recurrence']['notes']);
}
// update all repetitions
if (null !== $data['repetitions']) {
$this->deleteRepetitions($recurrence);
$this->createRepetitions($recurrence, $data['repetitions'] ?? []);
if (array_key_exists('repetitions', $data)) {
Log::debug('Will update repetitions array');
// update each repetition or throw error yay
$this->updateRepetitions($recurrence, $data['repetitions'] ?? []);
}
// update all transactions (and associated meta-data)
if (null !== $data['transactions']) {
$this->deleteTransactions($recurrence);
$this->createTransactions($recurrence, $data['transactions'] ?? []);
}
// // update all transactions (and associated meta-data)
// if (array_key_exists('transactions', $data)) {
// $this->deleteTransactions($recurrence);
// $this->createTransactions($recurrence, $data['transactions'] ?? []);
// }
return $recurrence;
}
@@ -133,4 +136,76 @@ class RecurrenceUpdateService
}
}
/**
*
* @param Recurrence $recurrence
* @param array $repetitions
*/
private function updateRepetitions(Recurrence $recurrence, array $repetitions): void
{
$originalCount = $recurrence->recurrenceRepetitions()->count();
if (0 === count($repetitions)) {
// wont drop repetition, rather avoid.
return;
}
// user added or removed repetitions, delete all and recreate:
if ($originalCount !== count($repetitions)) {
Log::debug('Del + recreate');
$this->deleteRepetitions($recurrence);
$this->createRepetitions($recurrence, $repetitions);
return;
}
// loop all and try to match them:
if ($originalCount === count($repetitions)) {
Log::debug('Loop and find');
foreach ($repetitions as $current) {
$match = $this->matchRepetition($recurrence, $current);
if (null === $match) {
throw new FireflyException('Cannot match recurring repetition to existing repetition. Not sure what to do. Break.');
}
$fields = [
'type' => 'repetition_type',
'moment' => 'repetition_moment',
'skip' => 'repetition_skip',
'weekend' => 'weekend',];
foreach ($fields as $field => $column) {
if (array_key_exists($field, $current)) {
$match->$column = $current[$field];
$match->save();
}
}
}
}
}
/**
* @param array $data
*
* @return RecurrenceRepetition|null
*/
private function matchRepetition(Recurrence $recurrence, array $data): ?RecurrenceRepetition
{
$originalCount = $recurrence->recurrenceRepetitions()->count();
if (1 === $originalCount) {
Log::debug('Return the first one');
return $recurrence->recurrenceRepetitions()->first();
}
// find it:
$fields = ['id' => 'id',
'type' => 'repetition_type',
'moment' => 'repetition_moment',
'skip' => 'repetition_skip',
'weekend' => 'weekend',
];
$query = $recurrence->recurrenceRepetitions();
foreach ($fields as $field => $column) {
if (array_key_exists($field, $data)) {
$query->where($column, $data[$field]);
}
}
return $query->first();
}
}