From b1a8aff56ed198daec06a83cee5787a69d847f22 Mon Sep 17 00:00:00 2001 From: Yoga Pangestu Date: Tue, 26 Nov 2024 21:09:03 +0700 Subject: [PATCH] feat(inventory): curd,route service provider,custom js,menambahkan kolomprice,membuat trait delete attachment --- .../Admin/Manage/InventoryController.php | 71 +++++++ .../Admin/Manage/InventoryRequest.php | 45 +++++ app/Models/Inventory.php | 5 + app/Providers/RouteServiceProvider.php | 2 + app/Traits/DeleteAttachment.php | 17 ++ app/Traits/UploadAttachment.php | 21 +- ...24_11_18_191906_create_inventory_table.php | 1 + public/assets/admin/js/custom/inventroy.js | 36 ++++ .../pages/admin/manage/attendances.blade.php | 2 +- .../pages/admin/manage/inventory.blade.php | 185 ++++++++++++++++++ .../views/partials/admin/_sidebar.blade.php | 7 + routes/web.php | 8 + 12 files changed, 388 insertions(+), 12 deletions(-) create mode 100644 app/Http/Controllers/Admin/Manage/InventoryController.php create mode 100644 app/Http/Requests/Admin/Manage/InventoryRequest.php create mode 100644 app/Traits/DeleteAttachment.php create mode 100644 public/assets/admin/js/custom/inventroy.js create mode 100644 resources/views/pages/admin/manage/inventory.blade.php diff --git a/app/Http/Controllers/Admin/Manage/InventoryController.php b/app/Http/Controllers/Admin/Manage/InventoryController.php new file mode 100644 index 0000000..1adaf4d --- /dev/null +++ b/app/Http/Controllers/Admin/Manage/InventoryController.php @@ -0,0 +1,71 @@ + 'Barang', + 'inventory' => Inventory::latest()->get(), + ]); + } + + public function store(InventoryRequest $request): RedirectResponse + { + $validatedData = $request->validated(); + + DB::transaction(function () use ($validatedData) { + $newInventory = Inventory::create(array_merge($validatedData, [ + 'user_id' => Auth::id(), + 'barbershop_id' => Auth::user()->barbershop_id, + ])); + $this->uploadAttachment($validatedData['image'], 'inventory', Inventory::class, $newInventory->id); + }); + notify()->success('Data berhasil ditambahkan', 'Berhasil'); + + return back(); + } + + public function update(Inventory $inventory, InventoryRequest $request): RedirectResponse + { + $validatedData = $request->validated(); + + DB::transaction(function () use ($inventory, $validatedData) { + $inventory->update($validatedData); + + if (isset($validatedData['image'])) { + $this->uploadAttachment($validatedData['image'], 'inventory', Inventory::class, $inventory->id); + } + }); + + notify()->success('Data berhasil diubah', 'Berhasil'); + + return back(); + } + + public function delete(Inventory $inventory): RedirectResponse + { + DB::transaction(function () use ($inventory) { + $this->deleteAttachment('inventory', $inventory); + $inventory->delete(); + }); + + notify()->success('Data berhasil dihapus', 'Berhasil'); + + return back(); + } +} diff --git a/app/Http/Requests/Admin/Manage/InventoryRequest.php b/app/Http/Requests/Admin/Manage/InventoryRequest.php new file mode 100644 index 0000000..d87a6fb --- /dev/null +++ b/app/Http/Requests/Admin/Manage/InventoryRequest.php @@ -0,0 +1,45 @@ +merge([ + 'price' => removeRupiahFormatting($this->price), + ]); + } + + public function rules(): array + { + $isEdit = $this->isMethod('put'); + + return [ + 'name' => ['required', 'string', 'max:100'], + 'quantity' => ['required', 'numeric', 'min:0'], + 'price' => ['required', 'numeric', 'min:0', 'max:99999999.99'], + 'image' => $isEdit + ? ['nullable', 'image', 'mimes:jpeg,png,jpg,gif,svg', 'max:2048'] + : ['required', 'image', 'mimes:jpeg,png,jpg,gif,svg', 'max:2048'], + 'description' => ['required', 'string', 'max:65535'], + ]; + } + + protected function failedValidation(Validator $validator): void + { + $this->handleValidationFailure($validator); + } +} diff --git a/app/Models/Inventory.php b/app/Models/Inventory.php index aff1eb8..1d3047d 100644 --- a/app/Models/Inventory.php +++ b/app/Models/Inventory.php @@ -19,9 +19,14 @@ class Inventory extends Model 'barbershop_id', 'name', 'quantity', + 'price', 'description', ]; + protected $with = [ + 'attachment', + ]; + public function user(): BelongsTo { return $this->belongsTo(User::class); diff --git a/app/Providers/RouteServiceProvider.php b/app/Providers/RouteServiceProvider.php index dd86367..8a569ea 100644 --- a/app/Providers/RouteServiceProvider.php +++ b/app/Providers/RouteServiceProvider.php @@ -6,6 +6,7 @@ use App\Models\Cash; use App\Models\Customer; use App\Models\Expense; +use App\Models\Inventory; use App\Models\Payroll; use App\Models\Service; use App\Models\User; @@ -34,5 +35,6 @@ public function boot(): void Route::bind('expense', fn (string $expense) => Expense::findOrFail(decryptId($expense))); Route::bind('payroll', fn (string $payroll) => Payroll::findOrFail(decryptId($payroll))); Route::bind('cash', fn (string $cash) => Cash::findOrFail(decryptId($cash))); + Route::bind('inventory', fn (string $inventory) => Inventory::findOrFail(decryptId($inventory))); } } diff --git a/app/Traits/DeleteAttachment.php b/app/Traits/DeleteAttachment.php new file mode 100644 index 0000000..ffb2d9d --- /dev/null +++ b/app/Traits/DeleteAttachment.php @@ -0,0 +1,17 @@ +attachment && $classModel->attachment->formatted_attachment) { + Storage::delete("$path/{$classModel->attachment->formatted_attachment}"); + $classModel->attachment->delete(); + } + } +} diff --git a/app/Traits/UploadAttachment.php b/app/Traits/UploadAttachment.php index 2717c92..dc5e37f 100644 --- a/app/Traits/UploadAttachment.php +++ b/app/Traits/UploadAttachment.php @@ -20,17 +20,6 @@ public function uploadAttachment(UploadedFile|string $file, string $path, string $fileName = Str::uuid(); $fullFileName = $fileName.'.'.$extension; - $existingAttachment = Attachment::where('attachmentable_id', $featureId) - ->where('attachmentable_type', $classModel) - ->first(); - - if ($existingAttachment) { - if ($classModel !== 'App\\Models\\Attendance') { - $oldFilePath = $path.'/'.$existingAttachment->created_at->format('Y-m-d').'/'.$existingAttachment->name.'.'.$existingAttachment->extension; - Storage::disk('public')->delete($oldFilePath); - } - } - $date = now()->format('Y-m-d'); Storage::disk('public')->put($path.'/'.$date.'/'.$fullFileName, $image); @@ -42,6 +31,16 @@ public function uploadAttachment(UploadedFile|string $file, string $path, string 'size' => strlen($image), ]); } elseif ($file instanceof UploadedFile) { + $existingAttachment = Attachment::where('attachmentable_id', $featureId) + ->where('attachmentable_type', $classModel) + ->first(); + + if ($existingAttachment) { + $oldFilePath = $path.'/'.$existingAttachment->created_at->format('Y-m-d').'/'.$existingAttachment->name.'.'.$existingAttachment->extension; + Storage::disk('public')->delete($oldFilePath); + $existingAttachment->delete(); + } + $date = now()->format('Y-m-d'); $extension = $file->getClientOriginalExtension(); $fileName = Str::uuid(); diff --git a/database/migrations/2024_11_18_191906_create_inventory_table.php b/database/migrations/2024_11_18_191906_create_inventory_table.php index 82684b7..2fb5e66 100644 --- a/database/migrations/2024_11_18_191906_create_inventory_table.php +++ b/database/migrations/2024_11_18_191906_create_inventory_table.php @@ -17,6 +17,7 @@ public function up(): void $table->foreignId('barbershop_id')->constrained('barbershop')->cascadeOnDelete(); $table->string('name', 100); $table->integer('quantity'); + $table->decimal('price', 10, 0); $table->text('description'); $table->timestamps(); $table->softDeletes(); diff --git a/public/assets/admin/js/custom/inventroy.js b/public/assets/admin/js/custom/inventroy.js new file mode 100644 index 0000000..46f8894 --- /dev/null +++ b/public/assets/admin/js/custom/inventroy.js @@ -0,0 +1,36 @@ +$(document).ready(function () { + $("#createModal").on("show.bs.modal", function (event) { + const button = $(event.relatedTarget); + const data = { + modalTitle: button.data("modal-title"), + url: button.data("url"), + method: button.data("method"), + + name: button.data("name"), + quantity: button.data("quantity"), + price: button.data("price"), + description: button.data("description"), + }; + + $(".modal-title").text(data.modalTitle); + $("#form").attr('action', data.url); + $("#form-method").val(data.method); + + if (data.method === 'put') { + $("#name").val(data.name); + $("#quantity").val(data.quantity); + $("#price").val(data.price); + $("#description").val(data.description); + } + }); + + $('#createModal').on('hidden.bs.modal', function () { + const method = $("#form-method").val(); + if (method === 'put') { + $("#name").val(''); + $("#quantity").val(''); + $("#price").val(''); + $("#description").val(''); + } + }); +}); diff --git a/resources/views/pages/admin/manage/attendances.blade.php b/resources/views/pages/admin/manage/attendances.blade.php index 7fc32db..29d491b 100644 --- a/resources/views/pages/admin/manage/attendances.blade.php +++ b/resources/views/pages/admin/manage/attendances.blade.php @@ -61,7 +61,7 @@

Terima kasih, Anda telah melakukan absensi kehadiran masuk pada pukul - {{ $checkInAttendance->check_in }}.Semoga hari Anda produktif + {{ $checkInAttendance->check_in }}.Semoga hari Anda produktif.

@endif diff --git a/resources/views/pages/admin/manage/inventory.blade.php b/resources/views/pages/admin/manage/inventory.blade.php new file mode 100644 index 0000000..e939c57 --- /dev/null +++ b/resources/views/pages/admin/manage/inventory.blade.php @@ -0,0 +1,185 @@ +@extends('layouts.admin') +@section('styles') + +@endsection +@section('app') +
+
+
+
+ +
+
+
+
+ + + + + + + + + + + + + + + + @foreach ($inventory as $item) + + + + + + + + + + + + @endforeach + +
NoNamaKuantitasHargaTotalGambarDeskripsiTanggalAksi
{{ $loop->iteration }}{{ $item->name }}{{ $item->quantity }}{{ formatToRupiah($item->price) }}{{ formatToRupiah($item->price * $item->quantity) }} + @if ($item->attachment && $item->attachment->formatted_attachment) + attachment->formatted_attachment}") }}"> + attachment->formatted_attachment}") }}" + alt="{{ $item->name }}" width="77" height="77"> + + @else + + Default Image + + @endif + {{ $item->description }}{{ formatDateIndo($item->created_at) }} +
    +
  • + + + +
  • +
  • +
    + @csrf + @method('delete') + + + +
    +
  • +
+
+
+
+
+
+
+ + +
+@endsection +@section('afterScripts') + + + + + + +@endsection diff --git a/resources/views/partials/admin/_sidebar.blade.php b/resources/views/partials/admin/_sidebar.blade.php index 8f47dda..1a4388d 100644 --- a/resources/views/partials/admin/_sidebar.blade.php +++ b/resources/views/partials/admin/_sidebar.blade.php @@ -120,6 +120,13 @@ Kehadiran +
diff --git a/routes/web.php b/routes/web.php index b8986ca..ca95a0f 100644 --- a/routes/web.php +++ b/routes/web.php @@ -6,6 +6,7 @@ use App\Http\Controllers\Admin\Finance\ExpenseController; use App\Http\Controllers\Admin\Finance\PayrollController; use App\Http\Controllers\Admin\Manage\AttendanceController; +use App\Http\Controllers\Admin\Manage\InventoryController; use App\Http\Controllers\Admin\Master\BarbershopController; use App\Http\Controllers\Admin\Master\CustomerController; use App\Http\Controllers\Admin\Master\ServiceController; @@ -96,5 +97,12 @@ Route::post('store', [AttendanceController::class, 'store'])->name('manage.attendance.store'); Route::delete('delete/{attendance}', [AttendanceController::class, 'delete'])->name('manage.attendance.delete'); }); + + Route::get('/inventory', [InventoryController::class, 'index'])->name('manage.inventory'); + Route::prefix('inventory')->group(function () { + Route::post('store', [InventoryController::class, 'store'])->name('manage.inventory.store'); + Route::put('update/{inventory}', [InventoryController::class, 'update'])->name('manage.inventory.update'); + Route::delete('delete/{inventory}', [InventoryController::class, 'delete'])->name('manage.inventory.delete'); + }); }); });