1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-08 20:22:42 +01:00
invoiceninja/app/Console/Commands/PruneData.php

85 lines
2.3 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
namespace App\Console\Commands;
use DB;
use Illuminate\Console\Command;
2017-05-01 14:46:57 +02:00
use Symfony\Component\Console\Input\InputOption;
/**
2017-01-30 20:40:43 +01:00
* Class PruneData.
*/
class PruneData extends Command
{
/**
* @var string
*/
protected $name = 'ninja:prune-data';
2017-05-01 14:17:52 +02:00
/**
* @var string
*/
protected $description = 'Delete inactive accounts';
2019-09-13 01:30:10 +02:00
public function handle()
{
2017-10-24 09:59:26 +02:00
$this->info(date('r').' Running PruneData...');
2017-05-01 14:17:52 +02:00
if ($database = $this->option('database')) {
config(['database.default' => $database]);
}
// 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';
$results = DB::select($sql);
2017-05-01 14:17:52 +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}");
}
}
2017-05-01 14:17:52 +02:00
$this->info('Done');
}
/**
* @return array
*/
protected function getArguments()
{
return [];
}
/**
* @return array
*/
protected function getOptions()
{
2017-05-01 14:17:52 +02:00
return [
['database', null, InputOption::VALUE_OPTIONAL, 'Database', null],
];
}
}