Files

105 lines
3.9 KiB
PHP
Raw Permalink Normal View History

<?php
2022-12-29 19:42:26 +01:00
/**
* LinkToBill.php
2020-02-16 13:57:05 +01:00
* Copyright (c) 2019 james@firefly-iii.org
*
* 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/>.
*/
declare(strict_types=1);
namespace FireflyIII\TransactionRules\Actions;
2025-01-03 09:05:19 +01:00
use FireflyIII\Enums\TransactionTypeEnum;
2023-08-13 15:01:12 +02:00
use FireflyIII\Events\Model\Rule\RuleActionFailedOnArray;
2026-01-24 13:52:35 +01:00
use FireflyIII\Events\Model\TransactionGroup\TransactionGroupRequestsAuditLogEntry;
use FireflyIII\Models\RuleAction;
2022-10-02 06:23:31 +02:00
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
2020-08-23 08:51:58 +02:00
use FireflyIII\User;
2025-02-23 12:47:04 +01:00
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
/**
* Class LinkToBill.
*/
class LinkToBill implements ActionInterface
{
/**
* TriggerInterface constructor.
*/
2026-01-23 15:09:50 +01:00
public function __construct(
private readonly RuleAction $action
) {}
2020-08-23 07:42:14 +02:00
public function actOnArray(array $journal): bool
{
2023-11-28 17:18:31 +01:00
/** @var User $user */
$user = User::find($journal['user_id']);
2023-12-20 19:35:52 +01:00
2020-08-23 08:51:58 +02:00
/** @var BillRepositoryInterface $repository */
$repository = app(BillRepositoryInterface::class);
$repository->setUser($user);
$billName = $this->action->getValue($journal);
$bill = $repository->findByName($billName);
/** @var TransactionJournal $object */
$object = TransactionJournal::with('transactionType')->find($journal['transaction_journal_id']);
$type = $object->transactionType->type;
2020-08-23 08:51:58 +02:00
if (null !== $bill && TransactionTypeEnum::WITHDRAWAL->value === $type) {
$count = DB::table('transaction_journals')
2026-01-23 15:09:50 +01:00
->where('id', '=', $journal['transaction_journal_id'])
->where('bill_id', $bill->id)
->count()
;
2022-10-02 14:37:50 +02:00
if (0 !== $count) {
2026-01-23 15:09:50 +01:00
Log::error(sprintf(
'RuleAction LinkToBill could not set the bill of journal #%d to bill "%s": already set.',
$journal['transaction_journal_id'],
$billName
));
// event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.already_linked_to_subscription', ['name' => $billName])));
2023-12-20 19:35:52 +01:00
2022-10-02 14:37:50 +02:00
return false;
}
DB::table('transaction_journals')->where('id', '=', $journal['transaction_journal_id'])->update(['bill_id' => $bill->id]);
2026-01-23 15:09:50 +01:00
Log::debug(sprintf(
'RuleAction LinkToBill set the bill of journal #%d to bill #%d ("%s").',
$journal['transaction_journal_id'],
$bill->id,
$bill->name
));
2020-08-23 08:51:58 +02:00
2023-11-28 17:18:31 +01:00
/** @var TransactionJournal $object */
2022-10-02 20:23:11 +02:00
$object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']);
2026-01-24 13:52:35 +01:00
event(new TransactionGroupRequestsAuditLogEntry($this->action->rule, $object, 'set_bill', null, $bill->name));
2022-10-02 06:23:31 +02:00
2020-08-23 08:51:58 +02:00
return true;
}
2026-01-23 15:09:50 +01:00
Log::error(sprintf(
'RuleAction LinkToBill could not set the bill of journal #%d to bill "%s": no such bill found or not a withdrawal.',
$journal['transaction_journal_id'],
$billName
));
2023-08-13 15:01:12 +02:00
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_find_subscription', ['name' => $billName])));
2023-12-20 19:35:52 +01:00
2020-08-23 08:51:58 +02:00
return false;
2020-08-23 07:42:14 +02:00
}
}