mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-10 23:45:48 -06:00
Make sure location data in request is processed in a unified way.
This commit is contained in:
parent
414e8d7153
commit
ca260387a2
@ -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');
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
55
app/Services/Internal/Support/LocationServiceTrait.php
Normal file
55
app/Services/Internal/Support/LocationServiceTrait.php
Normal file
@ -0,0 +1,55 @@
|
||||
<?php
|
||||
/**
|
||||
* LocationServiceTrait.php
|
||||
* Copyright (c) 2019 thegrumpydictator@gmail.com
|
||||
*
|
||||
* This file is part of Firefly III (https://github.com/firefly-iii).
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user