2017-01-30 20:40:43 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
2016-04-21 12:02:19 +02:00
|
|
|
|
|
|
|
use DB;
|
|
|
|
use Illuminate\Console\Command;
|
2017-05-01 14:46:57 +02:00
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
2016-04-21 12:02:19 +02:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
2017-01-30 20:40:43 +01:00
|
|
|
* Class PruneData.
|
2016-07-03 18:11:58 +02:00
|
|
|
*/
|
2016-04-21 12:02:19 +02:00
|
|
|
class PruneData extends Command
|
|
|
|
{
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-04-21 12:02:19 +02:00
|
|
|
protected $name = 'ninja:prune-data';
|
2017-05-01 14:17:52 +02:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-04-21 12:02:19 +02:00
|
|
|
protected $description = 'Delete inactive accounts';
|
|
|
|
|
|
|
|
public function fire()
|
|
|
|
{
|
2017-10-24 09:59:26 +02:00
|
|
|
$this->info(date('r').' Running PruneData...');
|
2016-04-21 12:02:19 +02:00
|
|
|
|
2017-05-01 14:17:52 +02:00
|
|
|
if ($database = $this->option('database')) {
|
|
|
|
config(['database.default' => $database]);
|
|
|
|
}
|
|
|
|
|
2016-04-21 12:02:19 +02:00
|
|
|
// delete accounts who never registered, didn't create any invoices,
|
|
|
|
// hansn't logged in within the past 6 months and isn't linked to another account
|
2017-05-03 18:26:03 +02:00
|
|
|
$sql = 'select c.id
|
|
|
|
from companies c
|
|
|
|
left join accounts a on a.company_id = c.id
|
2017-05-03 20:00:42 +02:00
|
|
|
left join clients cl on cl.account_id = a.id
|
2017-05-03 18:26:03 +02:00
|
|
|
left join tasks t on t.account_id = a.id
|
|
|
|
left join expenses e on e.account_id = a.id
|
|
|
|
left join users u on u.account_id = a.id and u.registered = 1
|
|
|
|
where c.created_at < DATE_SUB(now(), INTERVAL 6 MONTH)
|
2017-05-04 13:16:09 +02:00
|
|
|
and c.trial_started is null
|
|
|
|
and c.plan is null
|
2017-05-03 18:26:03 +02:00
|
|
|
group by c.id
|
2017-05-03 20:00:42 +02:00
|
|
|
having count(cl.id) = 0
|
2017-05-03 18:26:03 +02:00
|
|
|
and count(t.id) = 0
|
|
|
|
and count(e.id) = 0
|
|
|
|
and count(u.id) = 0';
|
2016-04-21 12:02:19 +02:00
|
|
|
|
|
|
|
$results = DB::select($sql);
|
2017-05-01 14:17:52 +02:00
|
|
|
|
2016-04-21 12:02:19 +02:00
|
|
|
foreach ($results as $result) {
|
2017-05-03 18:26:03 +02:00
|
|
|
$this->info("Deleting company: {$result->id}");
|
|
|
|
try {
|
|
|
|
DB::table('companies')
|
|
|
|
->where('id', '=', $result->id)
|
|
|
|
->delete();
|
|
|
|
} catch (\Illuminate\Database\QueryException $e) {
|
|
|
|
// most likely because a user_account record exists which doesn't cascade delete
|
|
|
|
$this->info("Unable to delete companyId: {$result->id}");
|
|
|
|
}
|
2016-04-21 12:02:19 +02:00
|
|
|
}
|
2017-05-01 14:17:52 +02:00
|
|
|
|
2016-04-21 12:02:19 +02:00
|
|
|
$this->info('Done');
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-04-21 12:02:19 +02:00
|
|
|
protected function getArguments()
|
|
|
|
{
|
2016-07-03 18:11:58 +02:00
|
|
|
return [];
|
2016-04-21 12:02:19 +02:00
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-04-21 12:02:19 +02:00
|
|
|
protected function getOptions()
|
|
|
|
{
|
2017-05-01 14:17:52 +02:00
|
|
|
return [
|
|
|
|
['database', null, InputOption::VALUE_OPTIONAL, 'Database', null],
|
|
|
|
];
|
2016-04-21 12:02:19 +02:00
|
|
|
}
|
|
|
|
}
|