mirror of
				https://github.com/invoiceninja/invoiceninja.git
				synced 2025-11-04 00:17:34 -05:00 
			
		
		
		
	Support purging clients
This commit is contained in:
		
							parent
							
								
									47b4ae583f
								
							
						
					
					
						commit
						86e2c7c8c4
					
				@ -218,13 +218,22 @@ class ClientController extends BaseController
 | 
			
		||||
    {
 | 
			
		||||
        $action = Input::get('action');
 | 
			
		||||
        $ids = Input::get('public_id') ? Input::get('public_id') : Input::get('ids');
 | 
			
		||||
 | 
			
		||||
        if ($action == 'purge' && ! auth()->user()->is_admin) {
 | 
			
		||||
            return redirect('dashboard')->withError(trans('texts.not_authorized'));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $count = $this->clientService->bulk($ids, $action);
 | 
			
		||||
 | 
			
		||||
        $message = Utils::pluralize($action.'d_client', $count);
 | 
			
		||||
        Session::flash('message', $message);
 | 
			
		||||
 | 
			
		||||
        if ($action == 'purge') {
 | 
			
		||||
            return redirect('dashboard')->withMessage($message);
 | 
			
		||||
        } else {
 | 
			
		||||
            return $this->returnBulk(ENTITY_CLIENT, $action, $ids);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function statement($clientPublicId, $statusId = false, $startDate = false, $endDate = false)
 | 
			
		||||
    {
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										44
									
								
								app/Jobs/PurgeClientData.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								app/Jobs/PurgeClientData.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,44 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace App\Jobs;
 | 
			
		||||
 | 
			
		||||
use App\Jobs\Job;
 | 
			
		||||
use App\Models\Invoice;
 | 
			
		||||
use App\Models\LookupAccount;
 | 
			
		||||
use DB;
 | 
			
		||||
use Exception;
 | 
			
		||||
use App\Libraries\HistoryUtils;
 | 
			
		||||
 | 
			
		||||
class PurgeClientData extends Job
 | 
			
		||||
{
 | 
			
		||||
    public function __construct($client)
 | 
			
		||||
    {
 | 
			
		||||
        $this->client = $client;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Execute the job.
 | 
			
		||||
     *
 | 
			
		||||
     * @return void
 | 
			
		||||
     */
 | 
			
		||||
    public function handle()
 | 
			
		||||
    {
 | 
			
		||||
        $invoices = $this->client->invoices()->withTrashed()->get();
 | 
			
		||||
        $expenses = $this->client->expenses()->withTrashed()->get();
 | 
			
		||||
 | 
			
		||||
        foreach ($invoices as $invoice) {
 | 
			
		||||
            foreach ($invoice->documents as $document) {
 | 
			
		||||
                $document->delete();
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        foreach ($expenses as $expense) {
 | 
			
		||||
            foreach ($expense->documents as $document) {
 | 
			
		||||
                $document->delete();
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $this->client->forceDelete();
 | 
			
		||||
 | 
			
		||||
        HistoryUtils::deleteHistory($this->client);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -30,7 +30,7 @@ class ActivityRepository
 | 
			
		||||
 | 
			
		||||
        $activity->activity_type_id = $activityTypeId;
 | 
			
		||||
        $activity->adjustment = $balanceChange;
 | 
			
		||||
        $activity->client_id = $client ? $client->id : 0;
 | 
			
		||||
        $activity->client_id = $client ? $client->id : null;
 | 
			
		||||
        $activity->balance = $client ? ($client->balance + $balanceChange) : 0;
 | 
			
		||||
        $activity->notes = $notes ?: '';
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -2,6 +2,7 @@
 | 
			
		||||
 | 
			
		||||
namespace App\Ninja\Repositories;
 | 
			
		||||
 | 
			
		||||
use App\Jobs\PurgeClientData;
 | 
			
		||||
use App\Events\ClientWasCreated;
 | 
			
		||||
use App\Events\ClientWasUpdated;
 | 
			
		||||
use App\Models\Client;
 | 
			
		||||
@ -75,6 +76,11 @@ class ClientRepository extends BaseRepository
 | 
			
		||||
        return $query;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function purge($client)
 | 
			
		||||
    {
 | 
			
		||||
        dispatch(new PurgeClientData($client));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function save($data, $client = null)
 | 
			
		||||
    {
 | 
			
		||||
        $publicId = isset($data['public_id']) ? $data['public_id'] : false;
 | 
			
		||||
 | 
			
		||||
@ -25,6 +25,27 @@ class AddSlackNotifications extends Migration
 | 
			
		||||
            $table->boolean('auto_archive_quote')->default(false)->nullable();
 | 
			
		||||
            $table->boolean('auto_email_invoice')->default(true)->nullable();
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        Schema::table('expenses', function ($table) {
 | 
			
		||||
            $table->foreign('client_id')->references('id')->on('clients')->onDelete('cascade');
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        Schema::table('activities', function ($table) {
 | 
			
		||||
            $table->integer('task_id')->unsigned()->change();
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        DB::statement('UPDATE activities SET client_id = NULL WHERE client_id = 0');
 | 
			
		||||
 | 
			
		||||
        Schema::table('activities', function ($table) {
 | 
			
		||||
            $table->foreign('client_id')->references('id')->on('clients')->onDelete('cascade');
 | 
			
		||||
            $table->foreign('contact_id')->references('id')->on('contacts')->onDelete('cascade');
 | 
			
		||||
            $table->foreign('payment_id')->references('id')->on('payments')->onDelete('cascade');
 | 
			
		||||
            $table->foreign('invoice_id')->references('id')->on('invoices')->onDelete('cascade');
 | 
			
		||||
            $table->foreign('credit_id')->references('id')->on('credits')->onDelete('cascade');
 | 
			
		||||
            $table->foreign('task_id')->references('id')->on('tasks')->onDelete('cascade');
 | 
			
		||||
            $table->foreign('invitation_id')->references('id')->on('invitations')->onDelete('cascade');
 | 
			
		||||
            $table->foreign('expense_id')->references('id')->on('expenses')->onDelete('cascade');
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
 | 
			
		||||
@ -2786,6 +2786,9 @@ $LANG = array(
 | 
			
		||||
    'invoice_workflow' => 'Invoice Workflow',
 | 
			
		||||
    'quote_workflow' => 'Quote Workflow',
 | 
			
		||||
    'client_must_be_active' => 'Error: the client must be active',
 | 
			
		||||
    'purge_client' => 'Purge Client',
 | 
			
		||||
    'purged_client' => 'Successfully purged client',
 | 
			
		||||
    'purge_client_warning' => 'All related records (invoices, tasks, expenses, documents, etc) will also be deleted.'
 | 
			
		||||
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -52,6 +52,8 @@
 | 
			
		||||
                            ->withContents([
 | 
			
		||||
                              ($client->trashed() ? false : ['label' => trans('texts.archive_client'), 'url' => "javascript:onArchiveClick()"]),
 | 
			
		||||
                              ['label' => trans('texts.delete_client'), 'url' => "javascript:onDeleteClick()"],
 | 
			
		||||
                              auth()->user()->is_admin ? \DropdownButton::DIVIDER : false,
 | 
			
		||||
                              auth()->user()->is_admin ? ['label' => trans('texts.purge_client'), 'url' => "javascript:onPurgeClick()"] : false,
 | 
			
		||||
                            ]
 | 
			
		||||
                          )->split() !!}
 | 
			
		||||
                    @endcan
 | 
			
		||||
@ -401,6 +403,13 @@
 | 
			
		||||
		});
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
    function onPurgeClick() {
 | 
			
		||||
		sweetConfirm(function() {
 | 
			
		||||
			$('#action').val('purge');
 | 
			
		||||
			$('.mainForm').submit();
 | 
			
		||||
		}, "{{ trans('texts.purge_client_warning') . "\\n\\n" . trans('texts.no_undo') }}");
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
    function showEmailHistory(email) {
 | 
			
		||||
        window.emailBounceId = false;
 | 
			
		||||
        $('#emailHistoryModal .panel-body').html("{{ trans('texts.loading') }}...");
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user