|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Console\Commands; |
| 4 | + |
| 5 | +use App\Model\helpdesk\Ticket\Ticket_Thread; |
| 6 | +use App\Model\helpdesk\Ticket\Tickets; |
| 7 | +use Carbon\Carbon; |
| 8 | +use Illuminate\Console\Command; |
| 9 | + |
| 10 | +class ArchiveOldTickets extends Command |
| 11 | +{ |
| 12 | + /** |
| 13 | + * The name and signature of the console command. |
| 14 | + * |
| 15 | + * @var string |
| 16 | + */ |
| 17 | + protected $signature = 'tickets:archive {--days=365}'; |
| 18 | + |
| 19 | + /** |
| 20 | + * The console command description. |
| 21 | + * |
| 22 | + * @var string |
| 23 | + */ |
| 24 | + protected $description = 'Archive tickets older than a specified number of days'; |
| 25 | + |
| 26 | + /** |
| 27 | + * Execute the console command. |
| 28 | + * |
| 29 | + * @return mixed |
| 30 | + */ |
| 31 | + public function handle() |
| 32 | + { |
| 33 | + $days = $this->option('days'); |
| 34 | + $this->info("Archiving tickets older than $days days..."); |
| 35 | + $date = Carbon::now()->subDays($days); |
| 36 | + $tickets = Tickets::where('updated_at', '<', $date)->where('status', '!=', 5)->get(); |
| 37 | + |
| 38 | + if ($tickets->isEmpty()) { |
| 39 | + $this->info('No tickets to archive.'); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + foreach ($tickets as $ticket) { |
| 44 | + $ticket->status = 5; // Assuming 5 is the status for "Archived" |
| 45 | + $ticket->save(); |
| 46 | + |
| 47 | + $thread = new Ticket_Thread(); |
| 48 | + $thread->ticket_id = $ticket->id; |
| 49 | + $thread->user_id = 1; // System user |
| 50 | + $thread->is_internal = 1; |
| 51 | + $thread->body = 'Ticket has been archived by the system.'; |
| 52 | + $thread->save(); |
| 53 | + |
| 54 | + $this->info("Archived ticket #{$ticket->id}"); |
| 55 | + } |
| 56 | + $this->info('Done.'); |
| 57 | + } |
| 58 | +} |
0 commit comments