1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-09 12:42:36 +01:00
invoiceninja/app/Jobs/PurgeAccountData.php

90 lines
2.6 KiB
PHP
Raw Permalink Normal View History

2017-03-28 16:51:31 +02:00
<?php
namespace App\Jobs;
use App\Jobs\Job;
use App\Models\Document;
2017-05-01 11:29:45 +02:00
use App\Models\LookupAccount;
2017-03-28 16:51:31 +02:00
use Auth;
use DB;
use Exception;
2017-09-03 17:36:40 +02:00
use App\Ninja\Mailers\UserMailer;
2017-03-28 16:51:31 +02:00
class PurgeAccountData extends Job
{
/**
* Execute the job.
*
* @return void
*/
2017-09-03 17:36:40 +02:00
public function handle(UserMailer $userMailer)
2017-03-28 16:51:31 +02:00
{
$user = Auth::user();
$account = $user->account;
if (! $user->is_admin) {
throw new Exception(trans('texts.forbidden'));
}
// delete the documents from cloud storage
Document::scope()->each(function ($item, $key) {
$item->delete();
});
$tables = [
'activities',
'invitations',
'account_gateway_tokens',
'payment_methods',
'credits',
'expense_categories',
'expenses',
2017-06-26 14:57:40 +02:00
'recurring_expenses',
2017-03-28 16:51:31 +02:00
'invoice_items',
'payments',
'invoices',
'tasks',
'projects',
'products',
'vendor_contacts',
'vendors',
'contacts',
'clients',
2018-02-04 16:16:55 +01:00
'proposals',
'proposal_templates',
'proposal_snippets',
'proposal_categories',
2018-02-08 09:59:39 +01:00
'proposal_invitations',
'tax_rates',
2017-03-28 16:51:31 +02:00
];
foreach ($tables as $table) {
DB::table($table)->where('account_id', '=', $user->account_id)->delete();
}
$account->invoice_number_counter = 1;
$account->quote_number_counter = 1;
2017-06-28 16:10:57 +02:00
$account->credit_number_counter = $account->credit_number_counter > 0 ? 1 : 0;
$account->client_number_counter = $account->client_number_counter > 0 ? 1 : 0;
2017-03-28 16:51:31 +02:00
$account->save();
2017-05-01 11:29:45 +02:00
2017-08-02 21:37:41 +02:00
session([RECENTLY_VIEWED => false]);
2017-05-01 11:29:45 +02:00
if (env('MULTI_DB_ENABLED')) {
$current = config('database.default');
config(['database.default' => DB_NINJA_LOOKUP]);
2017-05-01 12:08:39 +02:00
$lookupAccount = LookupAccount::whereAccountKey($account->account_key)->firstOrFail();
2017-05-01 11:29:45 +02:00
DB::table('lookup_contacts')->where('lookup_account_id', '=', $lookupAccount->id)->delete();
DB::table('lookup_invitations')->where('lookup_account_id', '=', $lookupAccount->id)->delete();
2018-02-11 12:09:29 +01:00
DB::table('lookup_proposal_invitations')->where('lookup_account_id', '=', $lookupAccount->id)->delete();
2017-05-01 11:29:45 +02:00
config(['database.default' => $current]);
}
2017-09-03 17:36:40 +02:00
$subject = trans('texts.purge_successful');
$message = trans('texts.purge_details', ['account' => $user->account->getDisplayName()]);
$userMailer->sendMessage($user, $subject, $message);
2017-03-28 16:51:31 +02:00
}
}