mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-24 04:41:01 +00:00
chore: code cleanup.
This commit is contained in:
@@ -70,6 +70,59 @@ class AccountDestroyService
|
||||
$account->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
* @param Account $moveTo
|
||||
*/
|
||||
public function moveTransactions(Account $account, Account $moveTo): void
|
||||
{
|
||||
Log::debug(sprintf('Move from account #%d to #%d', $account->id, $moveTo->id));
|
||||
DB::table('transactions')->where('account_id', $account->id)->update(['account_id' => $moveTo->id]);
|
||||
|
||||
$collection = Transaction::groupBy('transaction_journal_id', 'account_id')
|
||||
->where('account_id', $moveTo->id)
|
||||
->get(['transaction_journal_id', 'account_id', DB::raw('count(*) as the_count')]);
|
||||
if (0 === $collection->count()) {
|
||||
return;
|
||||
}
|
||||
|
||||
/** @var JournalDestroyService $service */
|
||||
$service = app(JournalDestroyService::class);
|
||||
$user = $account->user;
|
||||
/** @var stdClass $row */
|
||||
foreach ($collection as $row) {
|
||||
if ((int)$row->the_count > 1) {
|
||||
$journalId = (int)$row->transaction_journal_id;
|
||||
$journal = $user->transactionJournals()->find($journalId);
|
||||
if (null !== $journal) {
|
||||
Log::debug(sprintf('Deleted journal #%d because it has the same source as destination.', $journal->id));
|
||||
$service->destroy($journal);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
*/
|
||||
private function destroyJournals(Account $account): void
|
||||
{
|
||||
/** @var JournalDestroyService $service */
|
||||
$service = app(JournalDestroyService::class);
|
||||
|
||||
Log::debug('Now trigger account delete response #'.$account->id);
|
||||
/** @var Transaction $transaction */
|
||||
foreach ($account->transactions()->get() as $transaction) {
|
||||
Log::debug('Now at transaction #'.$transaction->id);
|
||||
/** @var TransactionJournal $journal */
|
||||
$journal = $transaction->transactionJournal()->first();
|
||||
if (null !== $journal) {
|
||||
Log::debug('Call for deletion of journal #'.$journal->id);
|
||||
$service->destroy($journal);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
*/
|
||||
@@ -106,69 +159,6 @@ class AccountDestroyService
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
* @param Account $moveTo
|
||||
*/
|
||||
public function moveTransactions(Account $account, Account $moveTo): void
|
||||
{
|
||||
Log::debug(sprintf('Move from account #%d to #%d', $account->id, $moveTo->id));
|
||||
DB::table('transactions')->where('account_id', $account->id)->update(['account_id' => $moveTo->id]);
|
||||
|
||||
$collection = Transaction::groupBy('transaction_journal_id', 'account_id')
|
||||
->where('account_id', $moveTo->id)
|
||||
->get(['transaction_journal_id', 'account_id', DB::raw('count(*) as the_count')]);
|
||||
if (0 === $collection->count()) {
|
||||
return;
|
||||
}
|
||||
|
||||
/** @var JournalDestroyService $service */
|
||||
$service = app(JournalDestroyService::class);
|
||||
$user = $account->user;
|
||||
/** @var stdClass $row */
|
||||
foreach ($collection as $row) {
|
||||
if ((int)$row->the_count > 1) {
|
||||
$journalId = (int)$row->transaction_journal_id;
|
||||
$journal = $user->transactionJournals()->find($journalId);
|
||||
if (null !== $journal) {
|
||||
Log::debug(sprintf('Deleted journal #%d because it has the same source as destination.', $journal->id));
|
||||
$service->destroy($journal);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
* @param Account $moveTo
|
||||
*/
|
||||
private function updateRecurrences(Account $account, Account $moveTo): void
|
||||
{
|
||||
DB::table('recurrences_transactions')->where('source_id', $account->id)->update(['source_id' => $moveTo->id]);
|
||||
DB::table('recurrences_transactions')->where('destination_id', $account->id)->update(['destination_id' => $moveTo->id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
*/
|
||||
private function destroyJournals(Account $account): void
|
||||
{
|
||||
/** @var JournalDestroyService $service */
|
||||
$service = app(JournalDestroyService::class);
|
||||
|
||||
Log::debug('Now trigger account delete response #'.$account->id);
|
||||
/** @var Transaction $transaction */
|
||||
foreach ($account->transactions()->get() as $transaction) {
|
||||
Log::debug('Now at transaction #'.$transaction->id);
|
||||
/** @var TransactionJournal $journal */
|
||||
$journal = $transaction->transactionJournal()->first();
|
||||
if (null !== $journal) {
|
||||
Log::debug('Call for deletion of journal #'.$journal->id);
|
||||
$service->destroy($journal);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
*/
|
||||
@@ -187,4 +177,14 @@ class AccountDestroyService
|
||||
$destroyService->destroyById((int)$recurrenceId);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Account $account
|
||||
* @param Account $moveTo
|
||||
*/
|
||||
private function updateRecurrences(Account $account, Account $moveTo): void
|
||||
{
|
||||
DB::table('recurrences_transactions')->where('source_id', $account->id)->update(['source_id' => $moveTo->id]);
|
||||
DB::table('recurrences_transactions')->where('destination_id', $account->id)->update(['destination_id' => $moveTo->id]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,7 +52,7 @@ class BudgetDestroyService
|
||||
DB::table('budget_transaction')->where('budget_id', (int)$budget->id)->delete();
|
||||
|
||||
// also delete all budget limits
|
||||
foreach($budget->budgetlimits()->get() as $limit) {
|
||||
foreach ($budget->budgetlimits()->get() as $limit) {
|
||||
$limit->delete();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,20 +31,6 @@ use FireflyIII\Models\RecurrenceTransaction;
|
||||
*/
|
||||
class RecurrenceDestroyService
|
||||
{
|
||||
/**
|
||||
* Delete recurrence by ID
|
||||
*
|
||||
* @param int $recurrenceId
|
||||
*/
|
||||
public function destroyById(int $recurrenceId): void
|
||||
{
|
||||
$recurrence = Recurrence::find($recurrenceId);
|
||||
if (null === $recurrence) {
|
||||
return;
|
||||
}
|
||||
$this->destroy($recurrence);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete recurrence.
|
||||
*
|
||||
@@ -67,4 +53,18 @@ class RecurrenceDestroyService
|
||||
// delete recurrence
|
||||
$recurrence->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete recurrence by ID
|
||||
*
|
||||
* @param int $recurrenceId
|
||||
*/
|
||||
public function destroyById(int $recurrenceId): void
|
||||
{
|
||||
$recurrence = Recurrence::find($recurrenceId);
|
||||
if (null === $recurrence) {
|
||||
return;
|
||||
}
|
||||
$this->destroy($recurrence);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user