2016-04-19 00:47:54 -07:00
< ? 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 ;
2016-05-12 17:23:51 -07:00
use \App\Models\Statuslabel ;
2016-04-19 00:47:54 -07:00
class Purge extends Command
{
/**
* The name and signature of the console command .
*
* @ var string
*/
2016-04-19 04:36:07 -07:00
protected $signature = 'snipeit:purge {--force=false}' ;
2016-04-19 00:47:54 -07:00
/**
* 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 ()
{
2016-04-19 04:36:07 -07:00
$force = $this -> option ( 'force' );
if (( $this -> confirm ( " \n **************************************************** \n THIS WILL PURGE ALL SOFT-DELETED ITEMS IN YOUR SYSTEM. \n There is NO undo. This WILL permanently destroy \n ALL of your deleted data. \n **************************************************** \n \n Do you wish to continue? No backsies! [y|N] " )) || $force == 'true' ) {
2016-04-19 00:47:54 -07:00
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 ;
2016-04-19 00:47:54 -07:00
foreach ( $assets as $asset ) {
2016-04-19 01:27:04 -07:00
$this -> info ( '- Asset "' . $asset -> showAssetName () . '" deleted.' );
$asset_assoc += $asset -> assetlog () -> count ();
2016-04-19 00:47:54 -07:00
$asset -> assetlog () -> forceDelete ();
2016-04-19 01:27:04 -07:00
$asset -> forceDelete ();
2016-04-19 00:47:54 -07:00
}
2016-04-19 01:27:04 -07:00
$this -> info ( $asset_assoc . ' corresponding log records purged.' );
2016-04-19 00:47:54 -07:00
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 00:47:54 -07:00
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.' );
2016-04-19 00:47:54 -07:00
foreach ( $accessories as $accessory ) {
2016-04-19 01:38:11 -07:00
$this -> info ( '- Accessory "' . $accessory -> name . '" deleted.' );
$accessory_assoc += $accessory -> assetlog () -> count ();
2016-04-19 00:47:54 -07:00
$accessory -> assetlog () -> forceDelete ();
2016-04-19 01:38:11 -07:00
$accessory -> forceDelete ();
2016-04-19 00:47:54 -07:00
}
2016-04-19 01:38:11 -07:00
$this -> info ( $accessory_assoc . ' corresponding log records purged.' );
2016-04-19 00:47:54 -07:00
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.' );
2016-04-19 00:47:54 -07:00
foreach ( $consumables as $consumable ) {
2016-04-19 01:38:11 -07:00
$this -> info ( '- Consumable "' . $consumable -> name . '" deleted.' );
2016-04-19 00:47:54 -07:00
$consumable -> assetlog () -> forceDelete ();
2016-04-19 01:38:11 -07:00
$consumable -> forceDelete ();
2016-04-19 00:47:54 -07:00
}
2016-04-19 01:38:11 -07:00
2016-04-19 00:47:54 -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.' );
2016-04-19 00:47:54 -07:00
foreach ( $components as $component ) {
2016-04-19 01:38:11 -07:00
$this -> info ( '- Component "' . $component -> name . '" deleted.' );
2016-04-19 00:47:54 -07:00
$component -> assetlog () -> forceDelete ();
2016-04-19 01:38:11 -07:00
$component -> forceDelete ();
2016-04-19 00:47:54 -07:00
}
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 00:47:54 -07:00
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 00:47:54 -07:00
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 00:47:54 -07:00
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-04-19 00:47:54 -07:00
2016-04-19 01:27:04 -07:00
$users = User :: whereNotNull ( 'deleted_at' ) -> withTrashed () -> get ();
$this -> info ( $users -> count () . ' users purged.' );
$user_assoc = 0 ;
2016-04-19 00:47:54 -07:00
foreach ( $users as $user ) {
2016-04-19 01:27:04 -07:00
$this -> info ( '- User "' . $user -> username . '" deleted.' );
$user_assoc += $user -> userlog () -> count ();
2016-04-19 00:47:54 -07:00
$user -> userlog () -> forceDelete ();
2016-04-19 01:27:04 -07:00
$user -> forceDelete ();
2016-04-19 00:47:54 -07:00
}
2016-04-19 01:38:11 -07:00
$this -> info ( $user_assoc . ' corresponding user log records purged.' );
2016-04-19 00:47:54 -07:00
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 ();
}
2016-04-19 00:47:54 -07:00
2016-05-12 17:23:51 -07:00
$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 00:47:54 -07:00
2016-04-19 01:27:04 -07:00
} else {
$this -> info ( 'Action canceled. Nothing was purged.' );
2016-04-19 00:47:54 -07:00
}
}
}