2020-11-18 10:23:51 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
2021-04-18 20:31:07 +02:00
|
|
|
use App\Enums\TicketStatus;
|
|
|
|
|
use App\Enums\TicketType;
|
|
|
|
|
use App\Events\NotifySubscriptions;
|
|
|
|
|
use App\Events\TicketStatusChanged;
|
2020-11-18 10:23:51 +01:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2021-04-18 20:31:07 +02:00
|
|
|
use Illuminate\Http\UploadedFile;
|
|
|
|
|
use Kwaadpepper\Enum\Traits\CastsEnums;
|
2020-11-18 10:23:51 +01:00
|
|
|
|
|
|
|
|
class Ticket extends Model
|
|
|
|
|
{
|
|
|
|
|
use HasFactory;
|
2021-04-18 20:31:07 +02:00
|
|
|
use CastsEnums;
|
2020-11-18 10:23:51 +01:00
|
|
|
|
2021-04-18 20:31:07 +02:00
|
|
|
protected $enumCasts = [
|
|
|
|
|
'type' => TicketType::class,
|
|
|
|
|
'status' => TicketStatus::class
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
protected $fillable = [
|
2021-07-23 11:34:16 +02:00
|
|
|
'site_id',
|
2021-04-18 20:31:07 +02:00
|
|
|
'type',
|
|
|
|
|
'status',
|
|
|
|
|
'text'
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
protected static function booted()
|
|
|
|
|
{
|
2021-05-21 14:34:45 +02:00
|
|
|
static::creating(function (Ticket $ticket) {
|
|
|
|
|
// If evolution force the ticket status on estimateTodo
|
|
|
|
|
if ($ticket->type->equals(TicketType::evolution())) {
|
|
|
|
|
$ticket->status = TicketStatus::estimateTodo();
|
|
|
|
|
}
|
|
|
|
|
});
|
2021-04-18 20:31:07 +02:00
|
|
|
static::updating(function (Ticket $ticket) {
|
|
|
|
|
$oldStatus = Ticket::where('id', $ticket->id)->first()->status;
|
|
|
|
|
if (!$ticket->status->equals($oldStatus)) {
|
|
|
|
|
event(new TicketStatusChanged($ticket, $oldStatus));
|
|
|
|
|
event(new NotifySubscriptions($ticket, sprintf(
|
|
|
|
|
'Le ticket à changé de status : %s',
|
|
|
|
|
$ticket->status->label
|
|
|
|
|
)));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-23 11:34:16 +02:00
|
|
|
public function site()
|
2021-04-18 20:31:07 +02:00
|
|
|
{
|
2021-07-23 11:34:16 +02:00
|
|
|
return $this->belongsTo(Site::class);
|
2021-04-18 20:31:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function user()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(User::class, 'created_by');
|
2020-11-18 10:23:51 +01:00
|
|
|
}
|
|
|
|
|
|
2021-04-18 20:31:07 +02:00
|
|
|
public function messages()
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(Message::class, 'ticket_id');
|
2020-11-18 10:23:51 +01:00
|
|
|
}
|
2020-11-25 16:05:21 +01:00
|
|
|
|
2021-04-18 20:31:07 +02:00
|
|
|
public function subscriptions()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsToMany(User::class, 'subscriptions');
|
2020-11-25 16:05:21 +01:00
|
|
|
}
|
2020-11-26 15:48:35 +01:00
|
|
|
|
2021-07-23 11:34:16 +02:00
|
|
|
public static function saveFile(Site $site, Ticket $ticket, TicketType $type, UploadedFile $file)
|
2021-04-18 20:31:07 +02:00
|
|
|
{
|
2021-07-23 11:34:16 +02:00
|
|
|
$siteAssetFullPath = Site::ticketAssetsFullPath($site->slug_name);
|
|
|
|
|
$fileAssetPath = Site::ticketAssetsPath($site->slug_name);
|
2021-04-18 20:31:07 +02:00
|
|
|
$fileName = sprintf(
|
|
|
|
|
'%s-%s-%d-%d.%s',
|
2021-07-23 11:34:16 +02:00
|
|
|
$site->slug_name,
|
2021-04-18 20:31:07 +02:00
|
|
|
$type->getDefinition(),
|
|
|
|
|
$ticket->id,
|
|
|
|
|
rand(),
|
|
|
|
|
$file->extension()
|
|
|
|
|
);
|
2021-07-23 11:34:16 +02:00
|
|
|
$file->move(sprintf('%s/%d', $siteAssetFullPath, $ticket->id), $fileName);
|
2021-04-18 20:31:07 +02:00
|
|
|
return sprintf('%s/%d/%s', $fileAssetPath, $ticket->id, $fileName);
|
2020-11-26 15:48:35 +01:00
|
|
|
}
|
2020-11-18 10:23:51 +01:00
|
|
|
}
|