2017-01-30 20:40:43 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
2016-03-24 18:02:45 +01:00
|
|
|
|
|
|
|
use App\Models\Document;
|
2017-01-30 20:40:43 +01:00
|
|
|
use DateTime;
|
2016-03-24 18:02:45 +01:00
|
|
|
use Illuminate\Console\Command;
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
2017-01-30 20:40:43 +01:00
|
|
|
* Class RemoveOrphanedDocuments.
|
2016-07-03 18:11:58 +02:00
|
|
|
*/
|
2016-03-24 18:02:45 +01:00
|
|
|
class RemoveOrphanedDocuments extends Command
|
|
|
|
{
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-03-24 18:02:45 +01:00
|
|
|
protected $name = 'ninja:remove-orphaned-documents';
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-03-24 18:02:45 +01:00
|
|
|
protected $description = 'Removes old documents not associated with an expense or invoice';
|
2017-05-01 14:17:52 +02:00
|
|
|
|
2016-03-24 18:02:45 +01:00
|
|
|
public function fire()
|
|
|
|
{
|
|
|
|
$this->info(date('Y-m-d').' Running RemoveOrphanedDocuments...');
|
|
|
|
|
2017-05-01 14:17:52 +02:00
|
|
|
if ($database = $this->option('database')) {
|
|
|
|
config(['database.default' => $database]);
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
$documents = Document::whereRaw('invoice_id IS NULL AND expense_id IS NULL AND updated_at <= ?', [new DateTime('-1 hour')])
|
2016-03-24 18:02:45 +01:00
|
|
|
->get();
|
2017-05-01 14:17:52 +02:00
|
|
|
|
2016-03-24 18:02:45 +01:00
|
|
|
$this->info(count($documents).' orphaned document(s) found');
|
|
|
|
|
|
|
|
foreach ($documents as $document) {
|
|
|
|
$document->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->info('Done');
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-03-24 18:02:45 +01:00
|
|
|
protected function getArguments()
|
|
|
|
{
|
2016-07-03 18:11:58 +02:00
|
|
|
return [];
|
2016-03-24 18:02:45 +01:00
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-03-24 18:02:45 +01:00
|
|
|
protected function getOptions()
|
|
|
|
{
|
2017-05-01 14:17:52 +02:00
|
|
|
return [
|
|
|
|
['database', null, InputOption::VALUE_OPTIONAL, 'Database', null],
|
|
|
|
];
|
2016-03-24 18:02:45 +01:00
|
|
|
}
|
|
|
|
}
|