From de14d846c5e1e9f4f95c992c5c8f281c6f51bbc1 Mon Sep 17 00:00:00 2001 From: Nicolas Demangeat Date: Wed, 18 Jun 2025 11:10:46 +0200 Subject: [PATCH 1/3] feat: :poop: trying to fix invoices sorting, trying to make the delete cascade for site --- app/Http/Controllers/Bo/SiteController.php | 4 +-- app/Models/Invoice.php | 2 ++ app/Models/Site.php | 16 +++++++++- app/Models/Ticket.php | 2 ++ ...45213_add_deleted_at_to_invoices_table.php | 32 +++++++++++++++++++ ...150855_add_deleted_at_to_tickets_table.php | 32 +++++++++++++++++++ resources/ts/bo/base/common.ts | 27 +++++++++++++--- resources/ts/modules/sweetalertConfirm.ts | 20 +++++++++--- .../views/bo/pages/invoices/index.blade.php | 2 +- .../views/bo/pages/sites/index.blade.php | 2 ++ .../views/bo/pages/tickets/index.blade.php | 2 +- 11 files changed, 127 insertions(+), 14 deletions(-) create mode 100644 database/migrations/2025_06_17_145213_add_deleted_at_to_invoices_table.php create mode 100644 database/migrations/2025_06_17_150855_add_deleted_at_to_tickets_table.php diff --git a/app/Http/Controllers/Bo/SiteController.php b/app/Http/Controllers/Bo/SiteController.php index de9e73e..ee3aef5 100644 --- a/app/Http/Controllers/Bo/SiteController.php +++ b/app/Http/Controllers/Bo/SiteController.php @@ -28,11 +28,11 @@ class SiteController extends Controller if ($user->isAdmin()) { /** @var \Illuminate\Database\Eloquent\Builder $siteModels */ - $siteModels = Site::query(); + $siteModels = Site::query()->with(['invoices', 'tickets']); } else { /** @var \Illuminate\Database\Eloquent\Builder $siteModels */ $siteModels = Site::whereHas('users', function (\Illuminate\Database\Eloquent\Builder $query) use ($user) { - $query->where('id', $user->id); + $query->where('id', $user->id)->with(['invoices', 'tickets']); }); } diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index 75244de..80e4285 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -10,6 +10,7 @@ use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Str; use Mccarlosen\LaravelMpdf\LaravelMpdf; +use Illuminate\Database\Eloquent\SoftDeletes; /** * @property string $ref @@ -32,6 +33,7 @@ use Mccarlosen\LaravelMpdf\LaravelMpdf; class Invoice extends Model { use HasFactory; + use SoftDeletes; /** * The attributes that are fillable. diff --git a/app/Models/Site.php b/app/Models/Site.php index d8aa825..3bd1e52 100644 --- a/app/Models/Site.php +++ b/app/Models/Site.php @@ -57,6 +57,10 @@ class Site extends Model static::updating(function (self $model) { self::manageFields($model); }); + static::deleted(function ($site) { + $site->invoices()->delete(); + $site->tickets()->delete(); + }); } // * METHODS @@ -84,7 +88,7 @@ class Site extends Model * HasMany Message relation * The users of type client that would have access to this site tickets * - * @return \Illuminate\Database\Eloquent\Relations\HasMany + * @return \Illuminate\Database\Eloquent\Relations\belongsToMany */ public function users() { @@ -101,6 +105,16 @@ class Site extends Model return $this->hasMany(Ticket::class, 'site_id'); } + /** + * HasMany Invoices relation + * + * @return \Illuminate\Database\Eloquent\Relations\HasMany + */ + public function invoices() + { + return $this->hasMany(Invoice::class, 'site_id'); + } + /** * BelongsTo Brand relation * diff --git a/app/Models/Ticket.php b/app/Models/Ticket.php index c74230e..e546e64 100644 --- a/app/Models/Ticket.php +++ b/app/Models/Ticket.php @@ -16,12 +16,14 @@ use App\Models\Ticket\TicketType; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\ModelNotFoundException; +use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Support\Collection; use Illuminate\Support\Str; class Ticket extends Model { use HasFactory; + use SoftDeletes; /** * The attributes that are fillable. diff --git a/database/migrations/2025_06_17_145213_add_deleted_at_to_invoices_table.php b/database/migrations/2025_06_17_145213_add_deleted_at_to_invoices_table.php new file mode 100644 index 0000000..c34330b --- /dev/null +++ b/database/migrations/2025_06_17_145213_add_deleted_at_to_invoices_table.php @@ -0,0 +1,32 @@ +softDeletes(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down(): void + { + Schema::table('invoices', function (Blueprint $table) { + $table->dropColumn('deleted_at'); + }); + } +}; diff --git a/database/migrations/2025_06_17_150855_add_deleted_at_to_tickets_table.php b/database/migrations/2025_06_17_150855_add_deleted_at_to_tickets_table.php new file mode 100644 index 0000000..efdad61 --- /dev/null +++ b/database/migrations/2025_06_17_150855_add_deleted_at_to_tickets_table.php @@ -0,0 +1,32 @@ +softDeletes(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down(): void + { + Schema::table('tickets', function (Blueprint $table) { + $table->dropColumn('deleted_at'); + }); + } +}; diff --git a/resources/ts/bo/base/common.ts b/resources/ts/bo/base/common.ts index 89c7b86..37c1fa3 100644 --- a/resources/ts/bo/base/common.ts +++ b/resources/ts/bo/base/common.ts @@ -5,9 +5,8 @@ window.addEventListener("DOMContentLoaded", () => { /** * Popup delete item. */ - const elements = document.getElementsByClassName( - "confirmDeleteTS" - ) as HTMLCollectionOf; + const elements = document.getElementsByClassName("confirmDeleteTS") as HTMLCollectionOf; + for (const element of elements) { element.addEventListener("submit", function (e) { e.preventDefault(); @@ -17,15 +16,33 @@ window.addEventListener("DOMContentLoaded", () => { "confirmDoneJS can only be executed on an exising form" ); } + + const event = e as SubmitEvent; + let submitter: HTMLButtonElement | null = null; + + if (event.submitter) { + submitter = event.submitter as HTMLButtonElement; + } else { + submitter = document.activeElement as HTMLButtonElement; + } + + const tickets_number = submitter?.dataset.tickets ?? "0"; + const invoices_number = submitter?.dataset.invoices ?? "0"; + + const message = `Toutes les données vont être perdues. +
Tickets concernés : ${tickets_number} + Devis concernées : ${invoices_number}`; + sweetalert.methods.confirm( "Êtes-vous sûr ?", - "Toutes les données vont être perdu", + message, el as HTMLFormElement, // eslint-disable-next-line @typescript-eslint/no-unused-vars function (response: SweetAlertResult) { (el as HTMLFormElement).submit(); }, - { icon: "warning" } + { icon: "warning", html: "true" }, + true ); return false; }); diff --git a/resources/ts/modules/sweetalertConfirm.ts b/resources/ts/modules/sweetalertConfirm.ts index 04f4e87..020c311 100644 --- a/resources/ts/modules/sweetalertConfirm.ts +++ b/resources/ts/modules/sweetalertConfirm.ts @@ -11,7 +11,8 @@ export default { text: string = "", self: Object, after: (response: SweetAlertResult) => void = () => {}, - options: SweetAlertOptions = {} + options: SweetAlertOptions = {}, + html: Boolean = false ) { if (!self) { throw Error("self is needed to set \"this\" on callback"); @@ -19,10 +20,12 @@ export default { if (!after) { throw Error("callback is needed when using confirm"); } + const icon = options.icon ?? "warning"; - Swal.fire({ + + // define options to fire + const saOptions = { title: title, - text: text, color: getComputedStyle(document.body).getPropertyValue("--bs-body-color"), icon: icon, showCancelButton: true, @@ -40,7 +43,16 @@ export default { }, buttonsStyling: false, background: getComputedStyle(document.body).getPropertyValue("--bs-body-bg"), - }).then((result) => { + }; + + // Enable HTML content interpretation in the modal options + let htmlActive = {}; + if (html) { + htmlActive = {html: text}; + } + const fullOptions = {...saOptions, ...htmlActive}; + + Swal.fire(fullOptions).then((result) => { if (result.isConfirmed) { if (after) { after.apply(self, [result]); diff --git a/resources/views/bo/pages/invoices/index.blade.php b/resources/views/bo/pages/invoices/index.blade.php index 3afa6dc..091e047 100644 --- a/resources/views/bo/pages/invoices/index.blade.php +++ b/resources/views/bo/pages/invoices/index.blade.php @@ -53,7 +53,7 @@ {{ $invoiceModel->sent_client_date }} {{ $invoiceModel->signed_date }} {{ $invoiceModel->sent_accounting_date }} - {{ $invoiceModel->site->name }} + {{ $invoiceModel->site?->name }} {{ $invoiceModel->updated_at }}
diff --git a/resources/views/bo/pages/sites/index.blade.php b/resources/views/bo/pages/sites/index.blade.php index 8d7d3cd..319bfc9 100644 --- a/resources/views/bo/pages/sites/index.blade.php +++ b/resources/views/bo/pages/sites/index.blade.php @@ -78,6 +78,8 @@ @method('DELETE')
@@ -76,8 +77,7 @@ {{-- All types --}}
- + @foreach ($status as $statut) @@ -125,8 +124,7 @@ @can('assignTickets', App\Models\Ticket::class)
- + @if (session('tickets.pr') && session('tickets.st') != \App\Enums\Tickets\TicketStatusEnum::archived->value) @if (session('tickets.or')) - +   {{ __('see_mode', ['mode' => __('validation.custom.default')]) }} @@ -209,7 +205,7 @@
-
+
@php @@ -245,16 +241,13 @@ @endphp @include('bo.modules.table-col-sorter', [ 'cols' => $cols, - 'ignore' => session('tickets.or') - ? array_keys($cols) - : ['id', 'site', 'status', 'tickets'], + 'ignore' => session('tickets.or') ? array_keys($cols) : ['id', 'site', 'status', 'tickets'], ]) @if (!count($tickets)) - @@ -267,7 +260,7 @@ 'routeName' => 'ticket', 'model' => $ticket, 'models' => $tickets, - 'value' => ($key + 1) + (($tickets->currentPage() - 1) * $tickets->perPage()), + 'value' => $key + 1 + ($tickets->currentPage() - 1) * $tickets->perPage(), 'noRightsVisible' => false, ]) @endif @@ -291,16 +284,14 @@ 'sitename' => $ticket->site->name, ]) }}">{{ $ticket->site->name }} @else - {{ $ticket->site->name }} + {{ $ticket->site->name }} @endif @endif {{-- type --}} {{-- status --}} - +
+

{{ __('Aucun ticket') }}

- {{ $ticket->type->label() }} + {{ $ticket->type->label() }} @@ -347,13 +338,10 @@ $ticket_files = $ticket->getFiles(); @endphp @foreach ($ticket_files as $key => $file) - @if ( - $file !== null && - (\App\Lib\Helpers\ToolboxHelper::isImageFile($file) || \App\Lib\Helpers\ToolboxHelper::isPdfFile($file))) + @if ($file !== null && (\App\Lib\Helpers\ToolboxHelper::isImageFile($file) || \App\Lib\Helpers\ToolboxHelper::isPdfFile($file))) + data-gallery="gallery{{ $ticket->id }}" href="{{ asset($file) }}" + target="_blank"> @break @endif @@ -361,8 +349,8 @@ {{-- Modal du ticket --}} + data-bs-target="#ticket{{ $ticket->id }}Modal" type="button"> @@ -377,10 +365,8 @@ action="{{ route('bo.ticket.archive', $ticket) }}"> @method('PUT') @csrf - + @endif @endcan @@ -388,13 +374,12 @@ {{ $invoiceModel->sent_client_date }} {{ $invoiceModel->signed_date }} {{ $invoiceModel->sent_accounting_date }}{{ $invoiceModel->site?->name }} + {{ $invoiceModel->site->name }} {{ $invoiceModel->updated_at }}