Browse Source

transfer selesai

master
Gunawan19621 1 year ago
parent
commit
a3c5777f07
  1. 142
      app/Http/Controllers/TransferController.php
  2. 8
      app/Http/Requests/Transfer/ValidasiUpdateTransfer.php
  3. 33
      resources/views/dashboard/Transfer/create.blade.php
  4. 114
      resources/views/dashboard/Transfer/edit.blade.php
  5. 5
      resources/views/dashboard/Transfer/index.blade.php
  6. 5
      resources/views/dashboard/Transfer/show.blade.php
  7. 2
      routes/web.php

142
app/Http/Controllers/TransferController.php

@ -3,12 +3,13 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Models\Peti; use App\Models\Peti;
use App\Models\Customer;
use App\Models\Transfer; use App\Models\Transfer;
use App\Models\m_warehouse; use App\Models\m_warehouse;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use App\Http\Requests\Transfer\ValidasiCreateTransfer; use App\Http\Requests\Transfer\ValidasiCreateTransfer;
use App\Models\Customer; use App\Http\Requests\Transfer\ValidasiUpdateTransfer;
class TransferController extends Controller class TransferController extends Controller
{ {
@ -39,71 +40,32 @@ class TransferController extends Controller
/** /**
* Store a newly created resource in storage. * Store a newly created resource in storage.
*/ */
// public function store(ValidasiCreateTransfer $request)
// {
// try {
// $currenttype = Auth::user();
// $validatedData = $request->except('_token');
// $validatedData['created_by'] = $currenttype->fullname; // Menggunakan nama pengguna sebagai created_by
// $validatedData['updated_by'] = $currenttype->fullname; // Menggunakan nama pengguna sebagai updated_by
// // dd($validatedData);
// Transfer::create($validatedData);
// return redirect()->route('dashboard.transfer.index')->with('success', 'Data Transfer Peti berhasil ditambahkan');
// } catch (\Throwable $th) {
// // dd($th->getMessage());
// return redirect()->back()->with('error', 'Data Transfer Peti Gagal Ditambah.');
// }
// }
// public function store(ValidasiCreateTransfer $request)
// {
// try {
// $currenttype = Auth::user();
// $validatedData = $request->except('_token');
// $validatedData['created_by'] = $currenttype->fullname;
// $validatedData['updated_by'] = $currenttype->fullname;
// // Membuat entitas Transfer
// $transfer = Transfer::create($validatedData);
// // Update kolom di tabel 'petis'
// Peti::where('customer_id', $transfer->customer_id)
// ->update(['customer_id' => $transfer->customer_id]);
// Peti::where('warehouse_id', $transfer->destination_warehouse)
// ->update(['warehouse_id' => $transfer->destination_warehouse]);
// return redirect()->route('dashboard.transfer.index')->with('success', 'Data Transfer Peti berhasil ditambahkan');
// } catch (\Throwable $th) {
// return redirect()->back()->with('error', 'Data Transfer Peti Gagal Ditambah.');
// }
// }
public function store(ValidasiCreateTransfer $request) public function store(ValidasiCreateTransfer $request)
{ {
try { try {
$currenttype = Auth::user(); // Mendapatkan informasi pengguna yang sedang login
$validatedData = $request->except('_token'); $currentUser = Auth::user();
$validatedData['created_by'] = $currenttype->fullname;
$validatedData['updated_by'] = $currenttype->fullname; // Validasi data dari request
$validatedData = $request->validated();
// Menambahkan informasi pengguna ke dalam data yang akan disimpan
$validatedData['created_by'] = $currentUser->fullname;
$validatedData['updated_by'] = $currentUser->fullname;
// Membuat entitas Transfer // Membuat entitas Transfer
$transfer = Transfer::create([ $transfer = Transfer::create($validatedData);
'name_customer' => $request->name_customer,
'peti_id' => $request->peti_id,
'source_warehouse' => $request->source_warehouse,
'destination_warehouse' => $request->destination_warehouse,
]);
// Mengupdate warehouse_id pada model Peti // Mengupdate warehouse_id pada model Peti
$peti = Peti::where('id', $transfer->peti_id) Peti::where('id', $transfer->peti_id)
->update(['warehouse_id' => $transfer->destination_warehouse]); ->update([
'warehouse_id' => $transfer->destination_warehouse,
// dd($peti); 'customer_id' => $transfer->name_customer, // tambahan update customer id
]);
return redirect()->route('dashboard.transfer.index')->with('success', 'Data Transfer Peti berhasil ditambahkan'); return redirect()->route('dashboard.transfer.index')->with('success', 'Data Transfer Peti berhasil ditambahkan');
} catch (\Throwable $th) { } catch (\Throwable $th) {
return redirect()->back()->withErrors($th->getMessage()); return redirect()->back()->with('error', 'Data Transfer Peti gagal ditransfer');
} }
} }
@ -112,13 +74,13 @@ class TransferController extends Controller
/** /**
* Display the specified resource. * Display the specified resource.
*/ */
public function show($id) // public function show($id)
{ // {
$data = [ // $data = [
'active' => 'menu-transfer', // 'active' => 'menu-transfer',
]; // ];
return view('dashboard.Transfer.show', $data); // return view('dashboard.Transfer.show', $data);
} // }
/** /**
* Show the form for editing the specified resource. * Show the form for editing the specified resource.
@ -126,6 +88,10 @@ class TransferController extends Controller
public function edit($id) public function edit($id)
{ {
$data = [ $data = [
'transfer' => Transfer::findOrFail($id),
'peti' => Peti::get(),
'customer' => Customer::get(),
'warehouse' => m_warehouse::get(),
'active' => 'menu-transfer', 'active' => 'menu-transfer',
]; ];
return view('dashboard.Transfer.edit', $data); return view('dashboard.Transfer.edit', $data);
@ -134,9 +100,40 @@ class TransferController extends Controller
/** /**
* Update the specified resource in storage. * Update the specified resource in storage.
*/ */
public function update($request, $id) public function update(ValidasiUpdateTransfer $request, $id)
{ {
// // dd("oke");
try {
// Mendapatkan informasi pengguna yang sedang login
$currentUser = Auth::user();
// Validasi data dari request
$validatedData = $request->validated();
// Menambahkan informasi pengguna ke dalam data yang akan disimpan
$validatedData['updated_by'] = $currentUser->fullname;
// Mencari transfer berdasarkan ID
$transfer = Transfer::find($id);
if (!$transfer) {
return redirect()->route('dashboard.transfer.index')->with('error', 'Data Transfer Peti tidak ditemukan');
}
// Memperbarui data transfer
$transfer->update($validatedData);
// Mengupdate warehouse_id pada model Peti
Peti::where('id', $transfer->peti_id)
->update([
'warehouse_id' => $transfer->destination_warehouse,
'customer_id' => $transfer->name_customer,
]);
return redirect()->route('dashboard.transfer.index')->with('success', 'Data Transfer Peti berhasil diperbaharui');
} catch (\Throwable $th) {
return redirect()->back()->with('error', 'Data Transfer Peti gagal diperbaharui');
}
} }
/** /**
@ -144,12 +141,13 @@ class TransferController extends Controller
*/ */
public function destroy($id) public function destroy($id)
{ {
// try { // dd("oke");
// $typepeti = Peti::findOrFail($id); try {
// $typepeti->delete(); $typetransfer = Transfer::findOrFail($id);
// return redirect()->back()->with('success', 'Data peti berhasil dihapus'); $typetransfer->delete();
// } catch (\Throwable $th) { return redirect()->back()->with('success', 'Data transfer peti berhasil dihapus');
// return redirect()->back()->with('error', 'Data peti gagal dihapus'); } catch (\Throwable $th) {
// } return redirect()->back()->with('error', 'Data transfer peti gagal dihapus');
}
} }
} }

8
app/Http/Requests/Transfer/ValidasiUpdateTransfer.php

@ -11,7 +11,7 @@ class ValidasiUpdateTransfer extends FormRequest
*/ */
public function authorize(): bool public function authorize(): bool
{ {
return false; return true;
} }
/** /**
@ -22,7 +22,11 @@ class ValidasiUpdateTransfer extends FormRequest
public function rules(): array public function rules(): array
{ {
return [ return [
// 'peti_id' => 'nullable', // Optional, peti_id harus ada di tabel petis
'source_warehouse' => 'nullable', // Optional, source_warehouse harus ada di tabel m_warehouses
'destination_warehouse' => 'nullable', // Optional, destination_warehouse harus ada di tabel m_warehouses
'name_customer' => 'nullable', // Optional, name_customer harus ada di tabel customers
'date' => 'nullable', // Optional, harus berupa tanggal
]; ];
} }
} }

33
resources/views/dashboard/Transfer/create.blade.php

@ -40,8 +40,8 @@
<label for="source_warehouse" class="col-form-label">Asal Gudang: <span <label for="source_warehouse" class="col-form-label">Asal Gudang: <span
class="text-danger">*</span></label> class="text-danger">*</span></label>
<select class="form-control" name="source_warehouse" type="text" id="source_warehouse"> <select class="form-control" name="source_warehouse" type="text" id="source_warehouse" disabled>
<option disabled selected>Pilih Asal Gudang</option> <option disabled selected>Asal gudang akan otomatis terpilih ketika memilih detail peti.</option>
@foreach ($warehouse as $data) @foreach ($warehouse as $data)
<option value="{{ $data->id }}">{{ $data->name }}</option> <option value="{{ $data->id }}">{{ $data->name }}</option>
@endforeach @endforeach
@ -102,33 +102,4 @@
} }
}); });
</script> </script>
{{-- <script>
// Mendapatkan elemen select detail peti
const petiSelect = document.getElementById('peti_id');
// Mendapatkan elemen select asal gudang
const source_warehouseSelect = document.getElementById('source_warehouse');
// Mendapatkan elemen input Customer
const name_customerInput = document.getElementById('name_customer');
// Menambahkan event listener ke select detail peti
petiSelect.addEventListener('change', function() {
// Mendapatkan data-warehouse-id dari option yang dipilih
const selectedOption = this.options[this.selectedIndex];
const warehouseId = selectedOption.getAttribute('data-warehouse-id');
// Mendapatkan data-customer-name dari option yang dipilih
const customerName = selectedOption.getAttribute('data-customer-id');
// Memilih asal gudang berdasarkan data-warehouse-id
source_warehouseSelect.value = warehouseId;
// Mengisi input Customer dengan nama pelanggan
name_customerInput.value = customerName;
});
</script> --}}
@endsection @endsection

114
resources/views/dashboard/Transfer/edit.blade.php

@ -1,5 +1,117 @@
@extends('layouts.main') @extends('layouts.main')
@section('content') @section('content')
@include('layouts.components.alert-prompt') @include('layouts.components.alert-prompt')
<p>Halaman edit transfer</p> <div class="card shadow mb-4">
<div class="card-header py-3">
<div class="row">
<div class="col-6">
<h5 class="m-0 font-weight-bold text-primary mt-2">Edit Transfer Peti</h5>
</div>
</div>
</div>
<div class="card-body">
<form action="{{ route('dashboard.transfer.update', $transfer->id) }}" method="POST" enctype="multipart/form-data"
id="transferForm">
@csrf
@method('PUT')
<div class="form-group">
<label for="peti_id" class="col-form-label">Pilih Detail Peti: <span
class="text-danger">*</span></label>
<select class="form-control" name="peti_id" id="peti_id" disabled>
<option disabled selected>Pilih Detail Peti</option>
@foreach ($peti as $data_peti)
<option value="{{ $data_peti->id }}"
{{ $data_peti->id == $transfer->peti_id ? 'selected' : '' }}>
{{ $data_peti->fix_lot }}
</option>
@endforeach
</select>
<label for="date" class="col-form-label">Tanggal Transfer: <span
class="text-danger">*</span></label>
<input class="form-control" name="date" type="date" id="date" value="{{ $transfer->date }}">
<label for="name_customer" class="col-form-label">Customer: <span class="text-danger">*</span></label>
<select class="form-control" name="name_customer" id="name_customer" required>
<option disabled selected>Pilih Customer</option>
@foreach ($customer as $data_customer)
<option value="{{ $data_customer->id }}"
{{ $data_customer->id == $transfer->name_customer ? 'selected' : '' }}>
{{ $data_customer->name }}
</option>
@endforeach
</select>
<label for="source_warehouse" class="col-form-label">Asal Gudang: <span
class="text-danger">*</span></label>
<select class="form-control" name="source_warehouse" id="source_warehouse" required>
<option disabled selected>Pilih Asal Gudang</option>
@foreach ($warehouse as $data)
<option value="{{ $data->id }}"
{{ $data->id == $transfer->destination_warehouse ? 'selected' : '' }}>
{{ $data->name }}
</option>
@endforeach
</select>
<label for="destination_warehouse" class="col-form-label">Tujuan Gudang: <span
class="text-danger">*</span></label>
<select class="form-control" name="destination_warehouse" type="text">
<option disabled selected>Pilih Tujuan Gudang</option>
@foreach ($warehouse as $data)
<option value="{{ $data->id }}">{{ $data->name }}</option>
@endforeach
</select>
</div>
<div class="modal-footer d-flex justify-content-center">
<a href="{{ route('dashboard.transfer.index') }}" class="btn btn-secondary">Kembali</a>
<button type="submit" class="btn btn-primary" id="submitButton">Simpan</button>
</div>
</form>
</div>
</div>
{{--
</div>
<!-- auto disable form pada saat sudah di simpan-->
<script>
document.getElementById('transferForm').addEventListener('submit', function() {
document.getElementById('submitButton').setAttribute('disabled', 'true');
});
</script>
<!-- otomatis asal gudang dan Customer berdasarkan detail peti -->
<script>
// Mendapatkan elemen select detail peti
const petiSelect = document.getElementById('peti_id');
// Mendapatkan elemen select asal gudang
const source_warehouseSelect = document.getElementById('source_warehouse');
// Mendapatkan elemen select Customer
const name_customerSelect = document.getElementById('name_customer');
// Menambahkan event listener ke select detail peti
petiSelect.addEventListener('change', function() {
// Mendapatkan data-warehouse-id dari option yang dipilih
const selectedOption = this.options[this.selectedIndex];
const warehouseId = selectedOption.getAttribute('data-warehouse-id');
// Mendapatkan data-customer-name dari option yang dipilih
const customerName = selectedOption.getAttribute('data-customer-name');
// Memilih asal gudang berdasarkan data-warehouse-id
source_warehouseSelect.value = warehouseId;
// Memilih customer berdasarkan data-customer-name
for (let i = 0; i < name_customerSelect.options.length; i++) {
if (name_customerSelect.options[i].text === customerName) {
name_customerSelect.selectedIndex = i;
break;
}
}
});
</script> --}}
@endsection @endsection

5
resources/views/dashboard/Transfer/index.blade.php

@ -51,10 +51,11 @@
<td>{{ $data_transfer->sourceWarehouse->name ?? '' }}</td> <td>{{ $data_transfer->sourceWarehouse->name ?? '' }}</td>
<td>{{ $data_transfer->destinationWarehouse->name }}</td> <td>{{ $data_transfer->destinationWarehouse->name }}</td>
<td class="text-center"> <td class="text-center">
<a href="#" title="Edit"> <a href="{{ route('dashboard.transfer.edit', $data_transfer->id) }}" title="Edit">
<i class="fa fa-edit mr-2" style="font-size: 20px"></i> <i class="fa fa-edit mr-2" style="font-size: 20px"></i>
</a> </a>
<form action="#" method="POST" style="display: inline;"> <form action="{{ route('dashboard.transfer.destroy', $data_transfer->id) }}"
method="POST" style="display: inline;">
@csrf @csrf
@method('DELETE') @method('DELETE')
<button type="submit" <button type="submit"

5
resources/views/dashboard/Transfer/show.blade.php

@ -1,5 +0,0 @@
@extends('layouts.main')
@section('content')
@include('layouts.components.alert-prompt')
<p>Halaman show transfer</p>
@endsection

2
routes/web.php

@ -74,7 +74,7 @@ Route::prefix('dashboard')->name('dashboard.')->middleware(['auth'])->group(func
Route::get('transfer', 'index')->name('transfer.index'); Route::get('transfer', 'index')->name('transfer.index');
Route::get('transfer/create', 'create')->name('transfer.create'); Route::get('transfer/create', 'create')->name('transfer.create');
Route::post('transfer/store', 'store')->name('transfer.store'); Route::post('transfer/store', 'store')->name('transfer.store');
Route::get('transfer/{id}', 'show')->name('transfer.show'); // Route::get('transfer/{id}', 'show')->name('transfer.show');
Route::get('transfer/{id}/edit', 'edit')->name('transfer.edit'); Route::get('transfer/{id}/edit', 'edit')->name('transfer.edit');
Route::put('transfer/{id}', 'update')->name('transfer.update'); Route::put('transfer/{id}', 'update')->name('transfer.update');
Route::delete('transfer/delete/{id}', 'destroy')->name('transfer.destroy'); Route::delete('transfer/delete/{id}', 'destroy')->name('transfer.destroy');

Loading…
Cancel
Save