Add timezone fields.

This commit is contained in:
James Cole 2024-11-06 14:10:34 +01:00
parent 225588f3e7
commit 0728668d41
No known key found for this signature in database
GPG Key ID: B49A324B7EAD6D80

View File

@ -101,7 +101,7 @@ class JournalUpdateService
'external_url',
];
$this->metaDate = ['interest_date', 'book_date', 'process_date', 'due_date', 'payment_date',
'invoice_date', ];
'invoice_date',];
}
public function setData(array $data): void
@ -111,7 +111,7 @@ class JournalUpdateService
public function setTransactionGroup(TransactionGroup $transactionGroup): void
{
$this->transactionGroup = $transactionGroup;
$this->transactionGroup = $transactionGroup;
$this->billRepository->setUser($transactionGroup->user);
$this->categoryRepository->setUser($transactionGroup->user);
$this->budgetRepository->setUser($transactionGroup->user);
@ -179,8 +179,8 @@ class JournalUpdateService
private function hasValidSourceAccount(): bool
{
app('log')->debug('Now in hasValidSourceAccount().');
$sourceId = $this->data['source_id'] ?? null;
$sourceName = $this->data['source_name'] ?? null;
$sourceId = $this->data['source_id'] ?? null;
$sourceName = $this->data['source_name'] ?? null;
if (!$this->hasFields(['source_id', 'source_name'])) {
$origSourceAccount = $this->getOriginalSourceAccount();
@ -194,11 +194,11 @@ class JournalUpdateService
// make a new validator.
/** @var AccountValidator $validator */
$validator = app(AccountValidator::class);
$validator = app(AccountValidator::class);
$validator->setTransactionType($expectedType);
$validator->setUser($this->transactionJournal->user);
$result = $validator->validateSource(['id' => $sourceId]);
$result = $validator->validateSource(['id' => $sourceId]);
app('log')->debug(
sprintf('hasValidSourceAccount(%d, "%s") will return %s', $sourceId, $sourceName, var_export($result, true))
);
@ -264,8 +264,8 @@ class JournalUpdateService
private function hasValidDestinationAccount(): bool
{
app('log')->debug('Now in hasValidDestinationAccount().');
$destId = $this->data['destination_id'] ?? null;
$destName = $this->data['destination_name'] ?? null;
$destId = $this->data['destination_id'] ?? null;
$destName = $this->data['destination_name'] ?? null;
if (!$this->hasFields(['destination_id', 'destination_name'])) {
app('log')->debug('No destination info submitted, grab the original data.');
@ -275,12 +275,12 @@ class JournalUpdateService
}
// make new account validator.
$expectedType = $this->getExpectedType();
$expectedType = $this->getExpectedType();
app('log')->debug(sprintf('(b) Expected type (new or unchanged) is %s', $expectedType));
// make a new validator.
/** @var AccountValidator $validator */
$validator = app(AccountValidator::class);
$validator = app(AccountValidator::class);
$validator->setTransactionType($expectedType);
$validator->setUser($this->transactionJournal->user);
$validator->source = $this->getValidSourceAccount();
@ -333,7 +333,7 @@ class JournalUpdateService
return $this->getOriginalSourceAccount();
}
$sourceInfo = [
$sourceInfo = [
'id' => (int) ($this->data['source_id'] ?? null),
'name' => $this->data['source_name'] ?? null,
'iban' => $this->data['source_iban'] ?? null,
@ -361,8 +361,8 @@ class JournalUpdateService
*/
private function updateAccounts(): void
{
$source = $this->getValidSourceAccount();
$destination = $this->getValidDestinationAccount();
$source = $this->getValidSourceAccount();
$destination = $this->getValidDestinationAccount();
// cowardly refuse to update if both accounts are the same.
if ($source->id === $destination->id) {
@ -375,7 +375,7 @@ class JournalUpdateService
$origSourceTransaction->account()->associate($source);
$origSourceTransaction->save();
$destTransaction = $this->getDestinationTransaction();
$destTransaction = $this->getDestinationTransaction();
$destTransaction->account()->associate($destination);
$destTransaction->save();
@ -397,7 +397,7 @@ class JournalUpdateService
return $this->getOriginalDestinationAccount();
}
$destInfo = [
$destInfo = [
'id' => (int) ($this->data['destination_id'] ?? null),
'name' => $this->data['destination_name'] ?? null,
'iban' => $this->data['destination_iban'] ?? null,
@ -426,7 +426,7 @@ class JournalUpdateService
{
app('log')->debug('Now in updateType()');
if ($this->hasFields(['type'])) {
$type = 'opening-balance' === $this->data['type'] ? 'opening balance' : $this->data['type'];
$type = 'opening-balance' === $this->data['type'] ? 'opening balance' : $this->data['type'];
app('log')->debug(
sprintf(
'Trying to change journal #%d from a %s to a %s.',
@ -459,9 +459,9 @@ class JournalUpdateService
{
$type = $this->transactionJournal->transactionType->type;
if ((
array_key_exists('bill_id', $this->data)
array_key_exists('bill_id', $this->data)
|| array_key_exists('bill_name', $this->data)
)
)
&& TransactionType::WITHDRAWAL === $type
) {
$billId = (int) ($this->data['bill_id'] ?? 0);
@ -478,7 +478,7 @@ class JournalUpdateService
private function updateField(string $fieldName): void
{
if (array_key_exists($fieldName, $this->data) && '' !== (string) $this->data[$fieldName]) {
$value = $this->data[$fieldName];
$value = $this->data[$fieldName];
if ('date' === $fieldName) {
if ($value instanceof Carbon) {
@ -490,6 +490,7 @@ class JournalUpdateService
}
// do some parsing.
app('log')->debug(sprintf('Create date value from string "%s".', $value));
$this->transactionJournal->date_tz = $value->format('e');
}
event(
new TriggeredAuditLog(
@ -579,7 +580,7 @@ class JournalUpdateService
if ($this->hasFields([$field])) {
$value = '' === $this->data[$field] ? null : $this->data[$field];
app('log')->debug(sprintf('Field "%s" is present ("%s"), try to update it.', $field, $value));
$set = [
$set = [
'journal' => $this->transactionJournal,
'name' => $field,
'data' => $value,
@ -598,7 +599,7 @@ class JournalUpdateService
if ($this->hasFields([$field])) {
try {
$value = '' === (string) $this->data[$field] ? null : new Carbon($this->data[$field]);
} catch (InvalidDateException|InvalidFormatException $e) { // @phpstan-ignore-line
} catch (InvalidDateException | InvalidFormatException $e) { // @phpstan-ignore-line
app('log')->debug(sprintf('%s is not a valid date value: %s', $this->data[$field], $e->getMessage()));
return;
@ -610,6 +611,13 @@ class JournalUpdateService
'data' => $value,
];
$factory->updateOrCreate($set);
// also set date with timezone.
$set = [
'journal' => $this->transactionJournal,
'name' => sprintf('%s_tz', $field),
'data' => $value->format('e'),
];
$factory->updateOrCreate($set);
}
}
}
@ -620,19 +628,19 @@ class JournalUpdateService
if (!$this->hasFields(['currency_id', 'currency_code'])) {
return;
}
$currencyId = $this->data['currency_id'] ?? null;
$currencyCode = $this->data['currency_code'] ?? null;
$currency = $this->currencyRepository->findCurrency($currencyId, $currencyCode);
$currencyId = $this->data['currency_id'] ?? null;
$currencyCode = $this->data['currency_code'] ?? null;
$currency = $this->currencyRepository->findCurrency($currencyId, $currencyCode);
// update currency everywhere.
$this->transactionJournal->transaction_currency_id = $currency->id;
$this->transactionJournal->save();
$source = $this->getSourceTransaction();
$source->transaction_currency_id = $currency->id;
$source = $this->getSourceTransaction();
$source->transaction_currency_id = $currency->id;
$source->save();
$dest = $this->getDestinationTransaction();
$dest->transaction_currency_id = $currency->id;
$dest = $this->getDestinationTransaction();
$dest->transaction_currency_id = $currency->id;
$dest->save();
// refresh transactions.
@ -648,7 +656,7 @@ class JournalUpdateService
return;
}
$value = $this->data['amount'] ?? '';
$value = $this->data['amount'] ?? '';
app('log')->debug(sprintf('Amount is now "%s"', $value));
try {
@ -663,9 +671,9 @@ class JournalUpdateService
$origSourceTransaction->amount = app('steam')->negative($amount);
$origSourceTransaction->balance_dirty = true;
$origSourceTransaction->save();
$destTransaction = $this->getDestinationTransaction();
$destTransaction->amount = app('steam')->positive($amount);
$destTransaction->balance_dirty = true;
$destTransaction = $this->getDestinationTransaction();
$destTransaction->amount = app('steam')->positive($amount);
$destTransaction->balance_dirty = true;
$destTransaction->save();
// refresh transactions.
$this->sourceTransaction->refresh();
@ -741,8 +749,8 @@ class JournalUpdateService
$source->foreign_amount = null;
$source->save();
$dest->foreign_currency_id = null;
$dest->foreign_amount = null;
$dest->foreign_currency_id = null;
$dest->foreign_amount = null;
$dest->save();
app('log')->debug(sprintf('Foreign amount is "%s" so remove foreign amount info.', $amount));
}