Add support for #2006

This commit is contained in:
James Cole 2019-02-08 07:14:45 +01:00
parent d63c9c9aea
commit 311d51464d
10 changed files with 155 additions and 10 deletions

View File

@ -104,7 +104,7 @@ class Controller extends BaseController
$obj = null;
if (null !== $date) {
try {
$obj = new Carbon($date);
$obj = Carbon::parse($date);
} catch (InvalidDateException $e) {
// don't care
Log::error(sprintf('Invalid date exception in API controller: %s', $e->getMessage()));

View File

@ -26,6 +26,7 @@ namespace FireflyIII\Api\V1\Requests;
use FireflyIII\Rules\BelongsUser;
use FireflyIII\Rules\IsBoolean;
use FireflyIII\Rules\IsDateOrTime;
use FireflyIII\Validation\TransactionValidation;
use Illuminate\Validation\Validator;
@ -59,7 +60,7 @@ class TransactionRequest extends Request
{
$data = [
'type' => $this->string('type'),
'date' => $this->date('date'),
'date' => $this->dateTime('date'),
'description' => $this->string('description'),
'piggy_bank_id' => $this->integer('piggy_bank_id'),
'piggy_bank_name' => $this->string('piggy_bank_name'),
@ -103,7 +104,7 @@ class TransactionRequest extends Request
// basic fields for journal:
'type' => 'required|in:withdrawal,deposit,transfer,opening-balance,reconciliation',
'description' => 'between:1,255',
'date' => 'required|date',
'date' => ['required', new IsDateOrTime],
'piggy_bank_id' => ['numeric', 'nullable', 'mustExist:piggy_banks,id', new BelongsUser],
'piggy_bank_name' => ['between:1,255', 'nullable', new BelongsUser],
'bill_id' => ['numeric', 'nullable', 'mustExist:bills,id', new BelongsUser],

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Factory;
use Carbon\Carbon;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Services\Internal\Support\JournalServiceTrait;
@ -70,14 +71,18 @@ class TransactionJournalFactory
Log::debug(sprintf('Going to store a %s', $type->type));
$description = app('steam')->cleanString($data['description']);
$description = str_replace(["\n", "\t", "\r"], "\x20", $description);
$journal = TransactionJournal::create(
/** @var Carbon $carbon */
$carbon = $data['date'];
$carbon->setTimezone(config('app.timezone'));
$journal = TransactionJournal::create(
[
'user_id' => $data['user'],
'transaction_type_id' => $type->id,
'bill_id' => null,
'transaction_currency_id' => $defaultCurrency->id,
'description' => $description,
'date' => $data['date']->format('Y-m-d'),
'date' => $carbon->format('Y-m-d H:i:s'),
'order' => 0,
'tag_count' => 0,
'completed' => 0,
@ -92,7 +97,7 @@ class TransactionJournalFactory
/** @var TransactionFactory $factory */
$factory = app(TransactionFactory::class);
$factory->setUser($this->user);
$totalAmount= '0';
$totalAmount = '0';
Log::debug(sprintf('Found %d transactions in array.', \count($data['transactions'])));
/** @var array $trData */
foreach ($data['transactions'] as $index => $trData) {

View File

@ -23,7 +23,9 @@ declare(strict_types=1);
namespace FireflyIII\Http\Requests;
use Carbon\Carbon;
use Carbon\Exceptions\InvalidDateException;
use Illuminate\Foundation\Http\FormRequest;
use Log;
/**
* Class Request.
@ -130,4 +132,42 @@ class Request extends FormRequest
{
return $this->get($field) ? new Carbon($this->get($field)) : null;
}
/**
* Return date time or NULL.
*
* @param string $field
*
* @return Carbon|null
*/
protected function dateTime(string $field): ?Carbon
{
if (null === $this->get($field)) {
return null;
}
$value = (string)$this->get($field);
if (10 === \strlen($value)) {
// probably a date format.
try {
$result = Carbon::createFromFormat('Y-m-d', $value);
} catch (InvalidDateException $e) {
Log::error(sprintf('"%s" is not a valid date: %s', $value, $e->getMessage()));
return null;
}
return $result;
}
// is an atom string, I hope?
try {
$result = Carbon::parse($value);
} catch (InvalidDateException $e) {
Log::error(sprintf('"%s" is not a valid date or time: %s', $value, $e->getMessage()));
return null;
}
return $result;
}
}

View File

@ -84,7 +84,7 @@ class TransactionJournal extends Model
'created_at' => 'datetime',
'updated_at' => 'datetime',
'deleted_at' => 'datetime',
'date' => 'date',
'date' => 'datetime',
'interest_date' => 'date',
'book_date' => 'date',
'process_date' => 'date',

View File

@ -0,0 +1,61 @@
<?php
declare(strict_types=1);
namespace FireflyIII\Rules;
use Carbon\Carbon;
use Carbon\Exceptions\InvalidDateException;
use Illuminate\Contracts\Validation\Rule;
use Log;
/**
* Class IsDateOrTime
*/
class IsDateOrTime implements Rule
{
/**
* Get the validation error message.
*
* @return string|array
*/
public function message()
{
return (string)trans('validation.date_or_time');
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
*
* @return bool
*/
public function passes($attribute, $value): bool
{
$value = (string)$value;
if (10 === \strlen($value)) {
// probably a date format.
try {
Carbon::createFromFormat('Y-m-d', $value);
} catch (InvalidDateException $e) {
Log::error(sprintf('"%s" is not a valid date: %s', $value, $e->getMessage()));
return false;
}
return true;
}
// is an atom string, I hope?
try {
Carbon::parse($value);
} catch (InvalidDateException $e) {
Log::error(sprintf('"%s" is not a valid date or time: %s', $value, $e->getMessage()));
return false;
}
return true;
}
}

View File

@ -77,7 +77,7 @@ class TransactionTransformer extends AbstractTransformer
'description' => $transaction->description,
'journal_description' => $transaction->description,
'transaction_description' => $transaction->transaction_description,
'date' => $transaction->date->format('Y-m-d'),
'date' => $transaction->date->toAtomString(),
'type' => $transaction->transaction_type_type,
'identifier' => $transaction->identifier,
'journal_id' => (int)$transaction->journal_id,

View File

@ -11,7 +11,7 @@ class ChangesForV4710 extends Migration
*
* @return void
*/
public function down()
public function down(): void
{
Schema::dropIfExists('group_journals');
Schema::dropIfExists('transaction_groups');
@ -22,7 +22,7 @@ class ChangesForV4710 extends Migration
*
* @return void
*/
public function up()
public function up(): void
{
if (!Schema::hasTable('transaction_groups')) {
Schema::create(

View File

@ -0,0 +1,37 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class ChangesForV4711 extends Migration
{
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
//
}
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::table(
'transaction_journals', function (Blueprint $table) {
$table->dateTimeTz('date')->change();
}
);
Schema::table('preferences', function (Blueprint $table) {
$table->text('data')->nullable()->change();
}
);
}
}

View File

@ -25,6 +25,7 @@ declare(strict_types=1);
return [
'iban' => 'This is not a valid IBAN.',
'zero_or_more' => 'The value cannot be negative.',
'date_or_time' => 'The value must be a valid date or time value (ISO 8601).',
'source_equals_destination' => 'The source account equals the destination account.',
'unique_account_number_for_user' => 'It looks like this account number is already in use.',
'unique_iban_for_user' => 'It looks like this IBAN is already in use.',