Make suppliers deletable.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -72,7 +72,7 @@ class Incoming extends Model
|
||||
*/
|
||||
public function supplier(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Supplier::class);
|
||||
return $this->belongsTo(Supplier::class)->withTrashed();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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.
|
||||
*
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -21,7 +21,9 @@
|
||||
{{ __("supplier.Add new supplier by clicking add") }}
|
||||
</p>
|
||||
</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>
|
||||
</div>
|
||||
</div>
|
||||
@@ -43,24 +45,56 @@
|
||||
x-on:keydown.window.prevent.slash="$refs.search_supplier.focus()"
|
||||
x-model="search_supplier"/>
|
||||
</div>
|
||||
|
||||
</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>
|
||||
<p x-show="success" x-transition
|
||||
class="text-sm text-green-600 dark:text-green-400">{{ __('form.Saved') }}</p>
|
||||
|
||||
<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>
|
||||
|
||||
<template x-for="(supplier, index) in getFilteredSuppliers()">
|
||||
<div class="even:bg-gray-100 odd:bg-white hover:bg-gray-400">
|
||||
<div class="cursor-pointer grid grid-cols-4" x-on:click="window.location.href='/supplier/' + supplier.id;">
|
||||
<div class="even:bg-gray-100 odd:bg-white hover:bg-gray-400 flex flex-row">
|
||||
<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.email"></div>
|
||||
<div class="text-right" x-text="supplier.invoices.length"></div>
|
||||
<div class="text-right" x-text="supplier.created"></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>
|
||||
</template>
|
||||
|
||||
@@ -79,9 +113,13 @@
|
||||
suppliers: [],
|
||||
search_supplier: '',
|
||||
|
||||
success: false,
|
||||
error: false,
|
||||
message: '',
|
||||
|
||||
init() {
|
||||
let vm = this;
|
||||
axios.get('/supplier')
|
||||
axios.get('/supplier-with-trashed')
|
||||
.then(function (response) {
|
||||
vm.suppliers = response.data;
|
||||
})
|
||||
@@ -97,6 +135,46 @@
|
||||
.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;
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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');
|
||||
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user