snipe-it/app/Console/Commands/Purge.php

175 lines
6.6 KiB
PHP
Raw Normal View History

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use DB;
use \App\Models\Asset;
use \App\Models\AssetModel;
use \App\Models\Location;
use \App\Models\Company;
use \App\Models\License;
use \App\Models\Accessory;
use \App\Models\Component;
use \App\Models\Consumable;
use \App\Models\Category;
use \App\Models\User;
use \App\Models\Supplier;
use \App\Models\Manufacturer;
use \App\Models\Depreciation;
use \App\Models\Statuslabel;
class Purge extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'snipeit:purge {--force=false}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Purge all soft-deleted deleted records in the database. This will rewrite history for items that have been edited, or checked in or out. It will also reqrite history for users associated with deleted items.';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$force = $this->option('force');
if (($this->confirm("\n****************************************************\nTHIS WILL PURGE ALL SOFT-DELETED ITEMS IN YOUR SYSTEM. \nThere is NO undo. This WILL permanently destroy \nALL of your deleted data. \n****************************************************\n\nDo you wish to continue? No backsies! [y|N]")) || $force == 'true') {
2016-04-19 01:27:04 -07:00
/**
* Delete assets
*/
$assets = Asset::whereNotNull('deleted_at')->withTrashed()->get();
$assetcount = $assets->count();
$this->info($assets->count().' assets purged.');
$asset_assoc = 0;
foreach ($assets as $asset) {
2016-04-19 01:27:04 -07:00
$this->info('- Asset "'.$asset->showAssetName().'" deleted.');
$asset_assoc += $asset->assetlog()->count();
$asset->assetlog()->forceDelete();
2016-04-19 01:27:04 -07:00
$asset->forceDelete();
}
2016-04-19 01:27:04 -07:00
$this->info($asset_assoc.' corresponding log records purged.');
2016-04-19 01:27:04 -07:00
$locations = Location::whereNotNull('deleted_at')->withTrashed()->get();
$this->info($locations->count().' locations purged.');
2016-04-19 01:38:11 -07:00
foreach ($locations as $location) {
$this->info('- Location "'.$location->name.'" deleted.');
$location->forceDelete();
}
2016-04-19 01:27:04 -07:00
$accessories = Accessory::whereNotNull('deleted_at')->withTrashed()->get();
2016-04-19 01:38:11 -07:00
$accessory_assoc=0;
$this->info($accessories->count().' accessories purged.');
foreach ($accessories as $accessory) {
2016-04-19 01:38:11 -07:00
$this->info('- Accessory "'.$accessory->name.'" deleted.');
$accessory_assoc += $accessory->assetlog()->count();
$accessory->assetlog()->forceDelete();
2016-04-19 01:38:11 -07:00
$accessory->forceDelete();
}
2016-04-19 01:38:11 -07:00
$this->info($accessory_assoc.' corresponding log records purged.');
2016-04-19 01:27:04 -07:00
$consumables = Consumable::whereNotNull('deleted_at')->withTrashed()->get();
2016-04-19 02:28:54 -07:00
$this->info($consumables->count().' consumables purged.');
foreach ($consumables as $consumable) {
2016-04-19 01:38:11 -07:00
$this->info('- Consumable "'.$consumable->name.'" deleted.');
$consumable->assetlog()->forceDelete();
2016-04-19 01:38:11 -07:00
$consumable->forceDelete();
}
2016-04-19 01:38:11 -07:00
2016-04-19 01:27:04 -07:00
$components = Component::whereNotNull('deleted_at')->withTrashed()->get();
2016-04-19 02:28:54 -07:00
$this->info($components->count().' components purged.');
foreach ($components as $component) {
2016-04-19 01:38:11 -07:00
$this->info('- Component "'.$component->name.'" deleted.');
$component->assetlog()->forceDelete();
2016-04-19 01:38:11 -07:00
$component->forceDelete();
}
2016-04-19 01:27:04 -07:00
$licenses = License::whereNotNull('deleted_at')->withTrashed()->get();
2016-04-19 02:28:54 -07:00
$this->info($licenses->count().' licenses purged.');
2016-04-19 01:38:11 -07:00
foreach ($licenses as $license) {
$this->info('- License "'.$license->name.'" deleted.');
$license->assetlog()->forceDelete();
$license->licenseseats()->forceDelete();
$license->forceDelete();
}
2016-04-19 01:27:04 -07:00
$models = AssetModel::whereNotNull('deleted_at')->withTrashed()->get();
2016-04-19 02:28:54 -07:00
$this->info($models->count().' asset models purged.');
2016-04-19 01:38:11 -07:00
foreach ($models as $model) {
$this->info('- Asset Model "'.$model->name.'" deleted.');
$model->forceDelete();
}
2016-04-19 01:27:04 -07:00
$categories = Category::whereNotNull('deleted_at')->withTrashed()->get();
2016-04-19 02:28:54 -07:00
$this->info($categories->count().' categories purged.');
2016-04-19 01:38:11 -07:00
foreach ($categories as $category) {
$this->info('- Category "'.$category->name.'" deleted.');
$category->forceDelete();
}
2016-04-19 01:27:04 -07:00
$suppliers = Supplier::whereNotNull('deleted_at')->withTrashed()->get();
2016-04-19 02:28:54 -07:00
$this->info($suppliers->count().' suppliers purged.');
2016-04-19 01:38:11 -07:00
foreach ($suppliers as $supplier) {
$this->info('- Supplier "'.$supplier->name.'" deleted.');
$supplier->forceDelete();
}
2016-06-09 12:25:51 -07:00
$users = User::whereNotNull('deleted_at')->where('show_in_list','!=','0')->withTrashed()->get();
2016-04-19 01:27:04 -07:00
$this->info($users->count().' users purged.');
$user_assoc = 0;
foreach ($users as $user) {
2016-04-19 01:27:04 -07:00
$this->info('- User "'.$user->username.'" deleted.');
$user_assoc += $user->userlog()->count();
$user->userlog()->forceDelete();
2016-04-19 01:27:04 -07:00
$user->forceDelete();
}
2016-04-19 01:38:11 -07:00
$this->info($user_assoc.' corresponding user log records purged.');
2016-04-19 01:27:04 -07:00
$manufacturers = Manufacturer::whereNotNull('deleted_at')->withTrashed()->get();
2016-04-19 02:28:54 -07:00
$this->info($manufacturers->count().' manufacturers purged.');
2016-04-19 01:38:11 -07:00
foreach ($manufacturers as $manufacturer) {
$this->info('- Manufacturer "'.$manufacturer->name.'" deleted.');
$manufacturer->forceDelete();
}
$status_labels = Statuslabel::whereNotNull('deleted_at')->withTrashed()->get();
2016-04-19 02:28:54 -07:00
$this->info($status_labels->count().' status labels purged.');
2016-04-19 01:38:11 -07:00
foreach ($status_labels as $status_label) {
$this->info('- Status Label "'.$status_label->name.'" deleted.');
$status_label->forceDelete();
}
2016-04-19 01:27:04 -07:00
} else {
$this->info('Action canceled. Nothing was purged.');
}
}
}