1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/app/Jobs/PurgeAccountData.php

74 lines
1.9 KiB
PHP
Raw 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;
class PurgeAccountData extends Job
{
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$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',
'invoice_items',
'payments',
'invoices',
'tasks',
'projects',
'products',
'vendor_contacts',
'vendors',
'contacts',
'clients',
];
foreach ($tables as $table) {
DB::table($table)->where('account_id', '=', $user->account_id)->delete();
}
$account->invoice_number_counter = 1;
$account->quote_number_counter = 1;
$account->client_number_counter = 1;
$account->save();
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();
config(['database.default' => $current]);
}
2017-03-28 16:51:31 +02:00
}
}