117 lines
3.3 KiB
PHP
117 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Enums\Tickets\TicketStatusEnum;
|
|
use App\Models\Site;
|
|
use App\Models\Ticket;
|
|
use App\Notifications\ExportTicketsStateNotification;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Queue\Queueable;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Facades\Notification;
|
|
|
|
class ExportTicketsState implements ShouldQueue
|
|
{
|
|
use Queueable;
|
|
|
|
/**
|
|
* CSV File headers
|
|
*
|
|
* @var array
|
|
*/
|
|
private $header = [
|
|
"Priorité service",
|
|
"Site",
|
|
"Numéro de ticket",
|
|
"Priorité",
|
|
"Date de MEP souhaitée",
|
|
"Type de ticket",
|
|
"Titre du ticket",
|
|
"Statut du ticket",
|
|
"Chef de projet",
|
|
"Utilisateur assigné",
|
|
"Date de dernière activité",
|
|
"Date de création",
|
|
"Lien vers le ticket",
|
|
"Lien vers l'issue GitLab"
|
|
];
|
|
|
|
|
|
/**
|
|
* Execute the job.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function handle(): void
|
|
{
|
|
// L'ordre commence à 0 et reprendra à 1 pour chaque site.
|
|
$order = 0;
|
|
// Dernier site traité.
|
|
$lastSiteId = null;
|
|
// Insertion de la ligne d'en-têtes.
|
|
$csvlines[] = $this->header;
|
|
|
|
// Récupération des tickets.
|
|
Ticket::query()
|
|
->whereNotIn(
|
|
'status',
|
|
[TicketStatusEnum::archived, TicketStatusEnum::production]
|
|
)
|
|
->orderBy(Site::select('name')
|
|
->whereColumn('sites.id', 'tickets.site_id'))
|
|
->orderBy('order')->get()->each(function (Ticket $ticket) use (&$csvlines, &$lastSiteId, &$order) {
|
|
$csvlines[] = [
|
|
"",
|
|
$ticket->site->name,
|
|
$ticket->id,
|
|
$lastSiteId === $ticket->site_id ? $order + 1 : 1,
|
|
$ticket->date_mep?->format('d-m-Y') ?? 'No MEP Date',
|
|
$ticket->type->label(),
|
|
$ticket->title,
|
|
$ticket->status->label(),
|
|
$ticket->projectManager->name ?? 'No PM',
|
|
$ticket->assignedUser()->name ?? 'No Assigned User',
|
|
Carbon::create($ticket->getRawOriginal('last_activity'))->format('d-m-Y H:i:s'),
|
|
$ticket->created_at->format('d-m-Y H:i:s'),
|
|
route('bo.ticket.show', $ticket->id),
|
|
$ticket->gitlab_issue ?? 'No GitLab Issue'
|
|
];
|
|
|
|
$order = $lastSiteId === $ticket->site_id ? $order + 1 : 1;
|
|
$lastSiteId = $ticket->site_id;
|
|
});
|
|
|
|
Notification::route('mail', config('mail.MAIL_ADMIN'))
|
|
->notify(new ExportTicketsStateNotification($this->array2csv($csvlines, ";")));
|
|
}
|
|
|
|
/**
|
|
* converts an array to a csv stream
|
|
*
|
|
* @param array $data
|
|
* @param string $delimiter
|
|
* @param string $enclosure
|
|
* @param string $escape_char
|
|
*
|
|
* @return string
|
|
*/
|
|
protected function array2csv(
|
|
array $data,
|
|
string $delimiter = ',',
|
|
string $enclosure = '"',
|
|
string $escape_char = "\\"
|
|
) {
|
|
$f = fopen('php://memory', 'r+');
|
|
|
|
// UTF-8 BOM.
|
|
$BOM = "\xEF\xBB\xBF";
|
|
fwrite($f, $BOM);
|
|
|
|
foreach ($data as $item) {
|
|
fputcsv($f, $item, $delimiter, $enclosure, $escape_char);
|
|
}
|
|
rewind($f);
|
|
return stream_get_contents($f);
|
|
}
|
|
}
|