diff --git a/app/Api/V1/Requests/AccountUpdateRequest.php b/app/Api/V1/Requests/AccountUpdateRequest.php index 26cc659f77..1b9169ba94 100644 --- a/app/Api/V1/Requests/AccountUpdateRequest.php +++ b/app/Api/V1/Requests/AccountUpdateRequest.php @@ -59,12 +59,6 @@ class AccountUpdateRequest extends Request if (null !== $this->get('include_net_worth')) { $includeNetWorth = $this->boolean('include_net_worth'); } - $updateLocation = false; - - if ($this->has('longitude') && $this->has('latitude') && $this->has('zoom_level')) { - $updateLocation = true; - } - $data = [ 'name' => $this->nullableString('name'), 'active' => $active, @@ -85,12 +79,10 @@ class AccountUpdateRequest extends Request 'notes' => $this->nullableNlString('notes'), 'interest' => $this->nullableString('interest'), 'interest_period' => $this->nullableString('interest_period'), - 'has_location' => $updateLocation, - 'longitude' => '' === $this->string('longitude') ? null : $this->string('longitude'), - 'latitude' => '' === $this->string('latitude') ? null : $this->string('latitude'), - 'zoom_level' => '' === $this->string('zoom_level') ? null : $this->integer('zoom_level'), ]; + $data = $this->appendLocationData($data); + if ('liability' === $data['account_type']) { $data['opening_balance'] = bcmul($this->nullableString('liability_amount'), '-1'); $data['opening_balance_date'] = $this->date('liability_start_date'); diff --git a/app/Factory/AccountFactory.php b/app/Factory/AccountFactory.php index 91c37a0da9..296220e176 100644 --- a/app/Factory/AccountFactory.php +++ b/app/Factory/AccountFactory.php @@ -30,6 +30,7 @@ use FireflyIII\Models\AccountType; use FireflyIII\Models\Location; use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Services\Internal\Support\AccountServiceTrait; +use FireflyIII\Services\Internal\Support\LocationServiceTrait; use FireflyIII\User; use Log; @@ -40,7 +41,7 @@ use Log; */ class AccountFactory { - use AccountServiceTrait; + use AccountServiceTrait, LocationServiceTrait; /** @var AccountRepositoryInterface */ protected $accountRepository; @@ -134,15 +135,7 @@ class AccountFactory $this->updateNote($return, $data['notes'] ?? ''); // store location - if (true === ($data['has_location'] ?? false) && null !== $return) { - $location = new Location; - $location->latitude = $data['latitude'] ?? 52.3167; - $location->longitude = $data['longitude'] ?? 5.55; - $location->zoom_level = $data['zoom_level'] ?? 6; - $location->locatable()->associate($return); - $location->save(); - } - + $this->storeNewLocation($return, $data); } return $return; diff --git a/app/Http/Requests/Request.php b/app/Http/Requests/Request.php index 36c060f77a..4a7c072b2b 100644 --- a/app/Http/Requests/Request.php +++ b/app/Http/Requests/Request.php @@ -344,4 +344,45 @@ class Request extends FormRequest return $result; } + /** + * Read the submitted Request data and add new or updated Location data to the array. + * + * @param array $data + * + * @return array + */ + protected function appendLocationData(array $data): array + { + Log::debug('Now in appendLocationData()'); + $data['store_location'] = false; + $data['update_location'] = false; + $data['longitude'] = null; + $data['latitude'] = null; + $data['zoom_level'] = null; + + // for a POST (store, all fields must be present and accounted for: + if ('POST' === $this->method() && $this->has('longitude') && $this->has('latitude') && $this->has('zoom_level')) { + Log::debug('Method is POST and all fields present.'); + $data['store_location'] = true; + $data['longitude'] = '' === $this->string('longitude') ? null : $this->string('longitude'); + $data['latitude'] = '' === $this->string('latitude') ? null : $this->string('latitude'); + $data['zoom_level'] = '' === $this->string('zoom_level') ? null : $this->integer('zoom_level'); + } + if ('PUT' === $this->method() && $this->has('longitude') && $this->has('latitude') && $this->has('zoom_level')) { + Log::debug('Method is PUT and all fields present.'); + $data['update_location'] = true; + $data['longitude'] = '' === $this->string('longitude') ? null : $this->string('longitude'); + $data['latitude'] = '' === $this->string('latitude') ? null : $this->string('latitude'); + $data['zoom_level'] = '' === $this->string('zoom_level') ? null : $this->integer('zoom_level'); + } + if (null === $data['longitude'] || null === $data['latitude'] || null === $data['zoom_level']) { + Log::debug('One of the fields is NULL, wont save.'); + $data['store_location'] = false; + $data['update_location'] = false; + } + Log::debug(sprintf('Returning longitude: "%s", latitude: "%s", zoom level: "%s"', $data['longitude'], $data['latitude'], $data['zoom_level'])); + + return $data; + } + } diff --git a/app/Services/Internal/Support/LocationServiceTrait.php b/app/Services/Internal/Support/LocationServiceTrait.php new file mode 100644 index 0000000000..247282ec10 --- /dev/null +++ b/app/Services/Internal/Support/LocationServiceTrait.php @@ -0,0 +1,55 @@ +. + */ + +namespace FireflyIII\Services\Internal\Support; + +use FireflyIII\Models\Location; +use Illuminate\Database\Eloquent\Model; + +/** + * Class LocationServiceTrait + */ +trait LocationServiceTrait +{ + /** + * @param Model $model + * @param array $data + * + * @return Location|null + */ + protected function storeNewLocation(Model $model, array $data): ?Location + { + $data['store_location'] = $data['store_location'] ?? false; + if ($data['store_location']) { + $location = new Location; + $location->latitude = $data['latitude'] ?? config('firefly.default_location.latitude'); + $location->longitude = $data['longitude'] ?? config('firefly.default_location.longitude'); + $location->zoom_level = $data['zoom_level'] ?? config('firefly.default_location.zoom_level'); + $location->locatable()->associate($model); + $location->save(); + + return $location; + } + + return null; + } + +} \ No newline at end of file