From 65a3c4ba073d05bd64c9f7aff35e5a8159388ef1 Mon Sep 17 00:00:00 2001 From: chris Date: Thu, 13 Feb 2025 17:13:20 +0100 Subject: [PATCH] Make suppliers deletable. --- .../Controllers/Api/SupplierController.php | 29 +++++ app/Models/Incoming.php | 2 +- app/Models/Supplier.php | 3 + ...13_144625_add_softdeletes_to_suppliers.php | 28 +++++ resources/views/supplier/index.blade.php | 106 +++++++++++++++--- routes/api.php | 4 +- 6 files changed, 156 insertions(+), 16 deletions(-) create mode 100644 database/migrations/2025_02_13_144625_add_softdeletes_to_suppliers.php diff --git a/app/Http/Controllers/Api/SupplierController.php b/app/Http/Controllers/Api/SupplierController.php index 139213f..de152e2 100644 --- a/app/Http/Controllers/Api/SupplierController.php +++ b/app/Http/Controllers/Api/SupplierController.php @@ -18,6 +18,14 @@ class SupplierController extends Controller return response()->json($suppliers); } + /** + * Display a listing of the resource. + */ + public function withTrashed(): JsonResponse + { + return response()->json(Supplier::with(['invoices'])->withTrashed()->orderBy('name')->get()); + } + /** * Store a newly created resource in storage. */ @@ -69,4 +77,25 @@ class SupplierController extends Controller return response()->json($supplier); } + + /** + * Remove the specified resource from storage. + */ + public function destroy(Supplier $supplier): JsonResponse + { + $supplier->delete(); + + return response()->json(null, 204); + } + + /** + * Restore the specified resource. + */ + public function restore(int $id): JsonResponse + { + $supplier = Supplier::withTrashed()->findOrFail($id)->restore(); + + return response()->json($supplier); + } + } diff --git a/app/Models/Incoming.php b/app/Models/Incoming.php index 70610d9..8a7ab17 100644 --- a/app/Models/Incoming.php +++ b/app/Models/Incoming.php @@ -72,7 +72,7 @@ class Incoming extends Model */ public function supplier(): BelongsTo { - return $this->belongsTo(Supplier::class); + return $this->belongsTo(Supplier::class)->withTrashed(); } /** diff --git a/app/Models/Supplier.php b/app/Models/Supplier.php index 2c12d51..457c2b9 100644 --- a/app/Models/Supplier.php +++ b/app/Models/Supplier.php @@ -4,9 +4,12 @@ namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Database\Eloquent\SoftDeletes; class Supplier extends Model { + use SoftDeletes; + /** * The attributes that are mass assignable. * diff --git a/database/migrations/2025_02_13_144625_add_softdeletes_to_suppliers.php b/database/migrations/2025_02_13_144625_add_softdeletes_to_suppliers.php new file mode 100644 index 0000000..a28c2db --- /dev/null +++ b/database/migrations/2025_02_13_144625_add_softdeletes_to_suppliers.php @@ -0,0 +1,28 @@ +softDeletes(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('suppliers', function (Blueprint $table) { + $table->dropSoftDeletes(); + }); + } +}; diff --git a/resources/views/supplier/index.blade.php b/resources/views/supplier/index.blade.php index 7f707fa..ef86718 100644 --- a/resources/views/supplier/index.blade.php +++ b/resources/views/supplier/index.blade.php @@ -21,7 +21,9 @@ {{ __("supplier.Add new supplier by clicking add") }}

- {{ __('form.Add') }} + + {{ __('form.Add') }} + @@ -43,24 +45,56 @@ x-on:keydown.window.prevent.slash="$refs.search_supplier.focus()" x-model="search_supplier"/> - -
-
{{ __('common.Name') }}
-
{{ __('common.Email') }}
-
{{ __('invoice.Invoices') }}
-
{{ __('common.Created at') }}
+

+

{{ __('form.Saved') }}

+ +
+
+
{{ __('common.Name') }}
+
{{ __('common.Email') }}
+
{{ __('invoice.Invoices') }}
+
{{ __('common.Created at') }}
+
+
@@ -79,10 +113,14 @@ suppliers: [], search_supplier: '', + success: false, + error: false, + message: '', + init() { let vm = this; - axios.get('/supplier') - .then(function(response) { + axios.get('/supplier-with-trashed') + .then(function (response) { vm.suppliers = response.data; }) }, @@ -93,10 +131,50 @@ } return this.suppliers.filter((supplier) => { return supplier.name - .replace(/ /g, '') - .toLowerCase() - .includes(this.search_supplier.replace(/ /g, '').toLowerCase()) + .replace(/ /g, '') + .toLowerCase() + .includes(this.search_supplier.replace(/ /g, '').toLowerCase()) }); + }, + + deleteSupplier(index) { + let vm = this; + let supplier_key = Object.keys(this.suppliers).find(key => (this.suppliers[key].id == this.getFilteredSuppliers()[index].id)); + axios.delete('/supplier/' + this.suppliers[supplier_key].id) + .then(function (response) { + vm.error = false; + vm.success = true; + vm.message = ''; + vm.suppliers[supplier_key].deleted_at = 0; + window.setTimeout(function () { + vm.success = false; + }, 1000); + }) + .catch(function (error) { + vm.error = true; + vm.success = false; + vm.message = error.response.data.message; + }); + }, + + restoreSupplier(index) { + let vm = this; + let supplier_key = Object.keys(this.suppliers).find(key => (this.suppliers[key].id == this.getFilteredSuppliers()[index].id)); + axios.get('/supplier/' + this.suppliers[supplier_key].id + '/restore') + .then(function (response) { + vm.error = false; + vm.success = true; + vm.message = ''; + vm.suppliers[supplier_key].deleted_at = null; + window.setTimeout(function () { + vm.success = false; + }, 1000); + }) + .catch(function(error) { + vm.error = true; + vm.success = false; + vm.message = error.response.data.message; + }) } } } diff --git a/routes/api.php b/routes/api.php index 0a053c3..f384fd9 100644 --- a/routes/api.php +++ b/routes/api.php @@ -56,7 +56,9 @@ Route::group(['as' => 'api.'], function () { Route::post('/incoming', [IncomingController::class, 'store'])->name('incoming.store'); Route::apiResource('/project', ProjectController::class); Route::apiResource('/dashboard', DashboardController::class)->only(['index', 'update']); - Route::apiResource('/supplier', SupplierController::class)->only(['index', 'store', 'update']); + Route::apiResource('/supplier', SupplierController::class); + Route::get('/supplier-with-trashed', [SupplierController::class, 'withTrashed']); + Route::get('/supplier/{id}/restore', [SupplierController::class, 'restore'])->name('supplier.restore'); });