feat(inventory): curd,route service provider,custom js,menambahkan kolomprice,membuat trait delete attachment

This commit is contained in:
Yoga Pangestu 2024-11-26 21:09:03 +07:00
parent 86efb6cd04
commit b1a8aff56e
12 changed files with 388 additions and 12 deletions

View File

@ -0,0 +1,71 @@
<?php
namespace App\Http\Controllers\Admin\Manage;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Manage\InventoryRequest;
use App\Models\Inventory;
use App\Traits\DeleteAttachment;
use App\Traits\UploadAttachment;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
class InventoryController extends Controller
{
use DeleteAttachment, UploadAttachment;
public function index(): View
{
return view('pages.admin.manage.inventory', [
'pageTitle' => '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();
}
}

View File

@ -0,0 +1,45 @@
<?php
namespace App\Http\Requests\Admin\Manage;
use App\Traits\NotificationTrait;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;
class InventoryRequest extends FormRequest
{
use NotificationTrait;
public function authorize(): bool
{
return Auth::check();
}
protected function prepareForValidation(): void
{
$this->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);
}
}

View File

@ -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);

View File

@ -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)));
}
}

View File

@ -0,0 +1,17 @@
<?php
namespace App\Traits;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;
trait DeleteAttachment
{
public function deleteAttachment(string $path, Model $classModel): void
{
if ($classModel->attachment && $classModel->attachment->formatted_attachment) {
Storage::delete("$path/{$classModel->attachment->formatted_attachment}");
$classModel->attachment->delete();
}
}
}

View File

@ -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();

View File

@ -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();

View File

@ -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('');
}
});
});

View File

@ -61,7 +61,7 @@
<div class="flex-grow-1">
<p class="mb-xl-0 mb-sm-4">
Terima kasih, Anda telah melakukan absensi kehadiran masuk pada pukul
{{ $checkInAttendance->check_in }}.Semoga hari Anda produktif
{{ $checkInAttendance->check_in }}.Semoga hari Anda produktif.
</p>
</div>
@endif

View File

@ -0,0 +1,185 @@
@extends('layouts.admin')
@section('styles')
<link rel="stylesheet" type="text/css" href="{{ asset('assets/admin/css/vendors/datatables.css') }}">
@endsection
@section('app')
<div class="container-fluid">
<div class="row">
<div class="col-sm-12">
<div class="list-product-header">
<div>
<a href="javascript:void(0)" data-bs-toggle="modal" data-bs-target="#createModal"
data-modal-title="Tambah Barang" data-url="{{ route('manage.inventory.store') }}"
data-metho="post" class="btn btn-primary">
Tambah
</a>
</div>
</div>
<div class="card">
<div class="card-body">
<div class="table-responsive">
<table class="display" id="datatable">
<thead>
<tr>
<th>No</th>
<th>Nama</th>
<th>Kuantitas</th>
<th>Harga</th>
<th>Total</th>
<th>Gambar</th>
<th>Deskripsi</th>
<th>Tanggal</th>
<th>Aksi</th>
</tr>
</thead>
<tbody>
@foreach ($inventory as $item)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $item->name }}</td>
<td>{{ $item->quantity }}</td>
<td>{{ formatToRupiah($item->price) }}</td>
<td>{{ formatToRupiah($item->price * $item->quantity) }}</td>
<td>
@if ($item->attachment && $item->attachment->formatted_attachment)
<a
href="{{ Storage::url("inventory/{$item->attachment->formatted_attachment}") }}">
<img src="{{ Storage::url("inventory/{$item->attachment->formatted_attachment}") }}"
alt="{{ $item->name }}" width="77" height="77">
</a>
@else
<a href="{{ asset('assets/images/no-imge.png') }}">
<img src="{{ asset('assets/images/no-imge.png') }}"
alt="Default Image" style="width: 77px; height: 77px">
</a>
@endif
</td>
<td>{{ $item->description }}</td>
<td>{{ formatDateIndo($item->created_at) }}</td>
<td>
<ul class="action">
<li class="edit">
<a href="javascript:void(0)" data-bs-toggle="modal"
data-bs-target="#createModal" data-modal-title="Edit Barang"
data-url="{{ route('manage.inventory.update', encryptId($item->id)) }}"
data-method="put" data-name="{{ $item->name }}"
data-quantity="{{ $item->quantity }}"
data-price="{{ formatToRupiah($item->price) }}"
data-description="{{ $item->description }}">
<i data-feather="edit"></i>
</a>
</li>
<li class="delete">
<form
action="{{ route('manage.inventory.delete', encryptId($item->id)) }}"
method="post">
@csrf
@method('delete')
<a href="javascript:void(0)" class="delete-data"
data-bs-toggle="tooltip" data-bs-placement="left"
data-bs-title="Hapus">
<i data-feather="trash-2"></i>
</a>
</form>
</li>
</ul>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<div class="modal fade" id="createModal" tabindex="-1" role="dialog" aria-labelledby="createModal"
aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title"></h5>
<button class="btn-close py-0" type="button" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<form class="row g-3 custom-input" method="post" id="form" enctype="multipart/form-data">
<input type="hidden" name="_method" id="form-method">
@csrf
<div class="col-lg-12 position-relative">
<label class="form-label" for="name">Nama</label>
<input name="name" class="form-control @error('name') is-invalid @enderror"
id="name" type="text" placeholder="Masukan Nama" value="{{ old('name') }}">
@error('name')
<div class="invalid-feedback">
{{ $message }}
</div>
@enderror
</div>
<div class="col-lg-12 position-relative">
<label class="form-label" for="quantity">Kuantitas</label>
<input name="quantity" class="form-control @error('quantity') is-invalid @enderror"
id="quantity" type="number" placeholder="Masukan Kuantitas"
value="{{ old('quantity') }}">
@error('quantity')
<div class="invalid-feedback">
{{ $message }}
</div>
@enderror
</div>
<div class="col-lg-12 position-relative">
<label class="form-label" for="price">Harga
<span class="form-span"><i>(Per 1 item)</i></span>
</label>
<input name="price"
class="form-control rupiah-format @error('price') is-invalid @enderror" id="price"
type="text" placeholder="Masukan Harga" value="{{ old('price') }}"
inputmode="numeric">
@error('price')
<div class="invalid-feedback">
{{ $message }}
</div>
@enderror
</div>
<div class="col-lg-12 position-relative">
<label class="form-label" for="description">Deskripsi</label>
<textarea name="description" class="form-control @error('description') is-invalid @enderror" id="description"
rows="3" placeholder="Masukan Deskripsi">{{ old('description') }}</textarea>
@error('description')
<div class="invalid-feedback">
{{ $message }}
</div>
@enderror
</div>
<div class="col-lg-12 position-relative">
<label class="form-label" for="image">Gambar
<span class="form-span"><i>(.jpeg,.png,.jpg,.gif,.svg)</i></span>
</label>
<input name="image" class="form-control @error('image') is-invalid @enderror"
id="image" type="file" value="{{ old('image') }}"
accept="image/jpeg,image/png,image/jpg,image/gif,image/svg">
@error('image')
<div class="invalid-feedback">
{{ $message }}
</div>
@enderror
</div>
<div class="col-lg-12 position-relative">
<button class="btn btn-secondary" type="button" data-bs-dismiss="modal">Tutup</button>
<button class="btn btn-primary">Simpan</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
@section('afterScripts')
<script src="{{ asset('assets/admin/js/datatable/jquery.dataTables.min.js?ver=1.0.0') }}"></script>
<script src="{{ asset('assets/admin/js/datatable/client-side.js?ver=1.0.0') }}"></script>
<script src="{{ asset('assets/js/interactions/confirmation.js?ver=1.0.0') }}"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script src="{{ asset('assets/admin/js/custom/inventroy.js?ver=1.0.0') }}"></script>
<script src="{{ asset('assets/js/forms/rupiah-format.js?ver=1.0.0') }}"></script>
@endsection

View File

@ -120,6 +120,13 @@
<span>Kehadiran</span>
</a>
</li>
<li class="sidebar-list">
<i class="fa fa-thumb-tack"></i>
<a class="sidebar-link sidebar-title link-nav" href="{{ route('manage.inventory') }}">
<i data-feather="archive"></i>
<span>Barang</span>
</a>
</li>
</ul>
</div>
<div class="right-arrow" id="right-arrow"><i data-feather="arrow-right"></i></div>

View File

@ -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');
});
});
});