Make suppliers deletable.

This commit is contained in:
2025-02-13 17:13:20 +01:00
parent f3d8c00d78
commit 65a3c4ba07
6 changed files with 156 additions and 16 deletions

View File

@@ -18,6 +18,14 @@ class SupplierController extends Controller
return response()->json($suppliers); 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. * Store a newly created resource in storage.
*/ */
@@ -69,4 +77,25 @@ class SupplierController extends Controller
return response()->json($supplier); 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);
}
} }

View File

@@ -72,7 +72,7 @@ class Incoming extends Model
*/ */
public function supplier(): BelongsTo public function supplier(): BelongsTo
{ {
return $this->belongsTo(Supplier::class); return $this->belongsTo(Supplier::class)->withTrashed();
} }
/** /**

View File

@@ -4,9 +4,12 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
class Supplier extends Model class Supplier extends Model
{ {
use SoftDeletes;
/** /**
* The attributes that are mass assignable. * The attributes that are mass assignable.
* *

View File

@@ -0,0 +1,28 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('suppliers', function (Blueprint $table) {
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('suppliers', function (Blueprint $table) {
$table->dropSoftDeletes();
});
}
};

View File

@@ -21,7 +21,9 @@
{{ __("supplier.Add new supplier by clicking add") }} {{ __("supplier.Add new supplier by clicking add") }}
</p> </p>
</header> </header>
<a class="mt-6 inline-block" href="{{ route('supplier.create') }}"><x-primary-button>{{ __('form.Add') }}</x-primary-button></a> <a class="mt-6 inline-block" href="{{ route('supplier.create') }}">
<x-primary-button>{{ __('form.Add') }}</x-primary-button>
</a>
</section> </section>
</div> </div>
</div> </div>
@@ -43,24 +45,56 @@
x-on:keydown.window.prevent.slash="$refs.search_supplier.focus()" x-on:keydown.window.prevent.slash="$refs.search_supplier.focus()"
x-model="search_supplier"/> x-model="search_supplier"/>
</div> </div>
</header> </header>
<div class="cursor-pointer grid grid-cols-4 mt-4"> <p class="text-red-600 font-bold" x-text="message" x-show="error"></p>
<div class="font-bold border-b-2">{{ __('common.Name') }}</div> <p x-show="success" x-transition
<div class="font-bold border-b-2">{{ __('common.Email') }}</div> class="text-sm text-green-600 dark:text-green-400">{{ __('form.Saved') }}</p>
<div class="font-bold border-b-2 text-right">{{ __('invoice.Invoices') }}</div>
<div class="font-bold border-b-2 text-right">{{ __('common.Created at') }}</div> <div class="flex flex-row mt-4">
<div class="w-11/12 grid grid-cols-4">
<div class="font-bold border-b-2">{{ __('common.Name') }}</div>
<div class="font-bold border-b-2">{{ __('common.Email') }}</div>
<div class="font-bold border-b-2 text-right">{{ __('invoice.Invoices') }}</div>
<div class="font-bold border-b-2 text-right">{{ __('common.Created at') }}</div>
</div>
<div class="w-1/12 border-b-2"></div>
</div> </div>
<template x-for="(supplier, index) in getFilteredSuppliers()"> <template x-for="(supplier, index) in getFilteredSuppliers()">
<div class="even:bg-gray-100 odd:bg-white hover:bg-gray-400"> <div class="even:bg-gray-100 odd:bg-white hover:bg-gray-400 flex flex-row">
<div class="cursor-pointer grid grid-cols-4" x-on:click="window.location.href='/supplier/' + supplier.id;"> <div class="w-11/12 grid grid-cols-4"
:class="(supplier.deleted_at == null) ? 'cursor-pointer' : 'line-through';"
x-on:click="(supplier.deleted_at == null) ? window.location.href='/supplier/' + supplier.id : ''">
<div x-text="supplier.name"></div> <div x-text="supplier.name"></div>
<div x-text="supplier.email"></div> <div x-text="supplier.email"></div>
<div class="text-right" x-text="supplier.invoices.length"></div> <div class="text-right" x-text="supplier.invoices.length"></div>
<div class="text-right" x-text="supplier.created"></div> <div class="text-right" x-text="supplier.created"></div>
</div> </div>
<div class="w-1/12 place-items-end" x-data="{ tooltip: false }"
x-show="supplier.deleted_at == null">
<x-trash-icon class="cursor-pointer h-6"
x-on:click="deleteSupplier(index);"
x-on:mouseover="tooltip = true"
x-on:mouseleave="tooltip = false"
/>
<div x-show="tooltip"
class="text-sm text-white absolute bg-gray-900 rounded-lg p-2 transform -translate-y-16 translate-x-8">
{{ __('form.Delete') }}
</div>
</div>
<div class="w-1/12 place-items-end" x-data="{ tooltip: false }"
x-show="supplier.deleted_at != null">
<x-restore-icon class="cursor-pointer h-6"
x-on:click="restoreSupplier(index);"
x-on:mouseover="tooltip = true"
x-on:mouseleave="tooltip = false"
/>
<div x-show="tooltip"
class="text-sm text-white absolute bg-gray-900 rounded-lg p-2 transform -translate-y-16 translate-x-8">
{{ __('form.Restore') }}
</div>
</div>
</div> </div>
</template> </template>
@@ -79,10 +113,14 @@
suppliers: [], suppliers: [],
search_supplier: '', search_supplier: '',
success: false,
error: false,
message: '',
init() { init() {
let vm = this; let vm = this;
axios.get('/supplier') axios.get('/supplier-with-trashed')
.then(function(response) { .then(function (response) {
vm.suppliers = response.data; vm.suppliers = response.data;
}) })
}, },
@@ -93,10 +131,50 @@
} }
return this.suppliers.filter((supplier) => { return this.suppliers.filter((supplier) => {
return supplier.name return supplier.name
.replace(/ /g, '') .replace(/ /g, '')
.toLowerCase() .toLowerCase()
.includes(this.search_supplier.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;
})
} }
} }
} }

View File

@@ -56,7 +56,9 @@ Route::group(['as' => 'api.'], function () {
Route::post('/incoming', [IncomingController::class, 'store'])->name('incoming.store'); Route::post('/incoming', [IncomingController::class, 'store'])->name('incoming.store');
Route::apiResource('/project', ProjectController::class); Route::apiResource('/project', ProjectController::class);
Route::apiResource('/dashboard', DashboardController::class)->only(['index', 'update']); 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');
}); });