2016-07-21 14:35:23 +02:00
|
|
|
<?php namespace App\Console\Commands;
|
2016-03-24 18:02:45 +01:00
|
|
|
|
|
|
|
use DateTime;
|
|
|
|
use App\Models\Document;
|
|
|
|
use Illuminate\Console\Command;
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* Class RemoveOrphanedDocuments
|
|
|
|
*/
|
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';
|
|
|
|
|
|
|
|
public function fire()
|
|
|
|
{
|
|
|
|
$this->info(date('Y-m-d').' Running RemoveOrphanedDocuments...');
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
$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()
|
|
|
|
{
|
2016-07-03 18:11:58 +02:00
|
|
|
return [];
|
2016-03-24 18:02:45 +01:00
|
|
|
}
|
|
|
|
}
|