You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
369 lines
14 KiB
369 lines
14 KiB
<?php |
|
|
|
namespace App\Http\Controllers; |
|
|
|
use Dompdf\Dompdf; |
|
use Dompdf\Options; |
|
use App\Models\Peti; |
|
use App\Models\Customer; |
|
use App\Models\Type_peti; |
|
use Mockery\Matcher\Type; |
|
use App\Imports\PetiImport; |
|
use App\Models\m_warehouse; |
|
use App\Models\Kondisi_Peti; |
|
use Illuminate\Http\Request; |
|
use Illuminate\Support\Collection; |
|
use Illuminate\Support\Facades\Auth; |
|
use Maatwebsite\Excel\Facades\Excel; |
|
use App\Http\Requests\ValidasiCreatePeti; |
|
use App\Http\Requests\ValidasiUpdatePeti; |
|
use SimpleSoftwareIO\QrCode\Facades\QrCode; |
|
|
|
class PetiController extends Controller |
|
{ |
|
/** |
|
* Display a listing of the resource. |
|
*/ |
|
public function index(Request $request) |
|
{ |
|
$perPage = $request->input('perPage', 5); |
|
|
|
$query = Peti::with(['customer', 'warehouse', 'tipe_peti', 'kondisipeti', 'transfer', 'assetStatuses']) |
|
->orderBy('created_at', 'desc'); |
|
// Tambahkan logika pencarian |
|
$search = $request->input('search') ?? ''; |
|
if ($search) { |
|
$query->where(function ($q) use ($search) { |
|
$q->where('fix_lot', 'like', "%$search%") |
|
->orWhere('created_by', 'like', "%$search%") |
|
->orWhereHas('customer', function ($customerQuery) use ($search) { |
|
$customerQuery->where('name', 'like', "%$search%") |
|
->orWhere('code_customer', 'like', "%$search%") |
|
->orWhere('lot_no', 'like', "%$search%"); |
|
}) |
|
->orWhereHas('warehouse', function ($warehouseQuery) use ($search) { |
|
$warehouseQuery->where('name', 'like', "%$search%"); |
|
}) |
|
->orWhereHas('tipe_peti', function ($tipePetiQuery) use ($search) { |
|
$tipePetiQuery->where('type', 'like', "%$search%") |
|
->orWhere('size_peti', 'like', "%$search%"); |
|
}) |
|
->orWhereHas('kondisipeti', function ($kondisiPetiQuery) use ($search) { |
|
$kondisiPetiQuery->where('nama_kondisi', 'like', "%$search%"); |
|
}) |
|
->orWhere('packing_no', 'like', "%$search%") |
|
->orWhere('status', 'like', "%$search%"); |
|
}); |
|
} |
|
|
|
if ($perPage == 'Semua') { |
|
$chunkSize = 100; |
|
$petis = new Collection(); |
|
$currentPage = 1; |
|
|
|
$query->chunk($chunkSize, function ($petiChunk) use ($petis, &$currentPage) { |
|
foreach ($petiChunk as $peti) { |
|
$peti->setAttribute('i', ($currentPage - 1) * $petiChunk->perPage() + 1); |
|
$petis->push($peti); |
|
$currentPage++; |
|
} |
|
}); |
|
} else { |
|
$petis = $query->paginate($perPage); |
|
} |
|
|
|
$data = [ |
|
'peti' => $petis, |
|
'kondisiPeti' => Kondisi_Peti::all(), |
|
'i' => ($petis->currentPage() - 1) * $petis->perPage() + 1, |
|
'search' => $search, |
|
'active' => 'menu-peti', |
|
]; |
|
return view('dashboard.Master_Data.Manajemen_Peti.Peti.index', $data); |
|
} |
|
|
|
/** |
|
* Show the form for creating a new resource. |
|
*/ |
|
public function create() |
|
{ |
|
$data = [ |
|
'kondisiPeti' => Kondisi_Peti::get(), |
|
'typepeti' => Type_peti::all(), |
|
'customer' => Customer::all(), |
|
'warehouse' => m_warehouse::all(), |
|
'active' => 'menu-peti', |
|
]; |
|
return view('dashboard.Master_Data.Manajemen_Peti.Peti.create', $data); |
|
} |
|
|
|
/** |
|
* Store a newly created resource in storage. |
|
*/ |
|
public function store(ValidasiCreatePeti $request) |
|
{ |
|
try { |
|
$currenttype = Auth::user(); |
|
|
|
for ($i = 0; $i < $request->jumlah; $i++) { |
|
$validatedData = $request->except('_token'); |
|
|
|
// Ambil nomor urutan otomatis untuk packing_no |
|
$latestPackingNo = Peti::max('packing_no'); |
|
$nextPackingNo = $latestPackingNo + 1; |
|
$validatedData['packing_no'] = '0' . $nextPackingNo; |
|
|
|
$code_customer = Customer::where('id', $validatedData['customer_id'])->first()->code_customer; |
|
$type = Type_peti::where('id', $validatedData['tipe_peti_id'])->first()->type; |
|
$size_peti = Type_peti::where('id', $validatedData['tipe_peti_id'])->first()->size_peti; |
|
$lot_no = Customer::where('id', $validatedData['customer_id'])->first()->lot_no; |
|
$packing_no = $validatedData['packing_no']; |
|
|
|
// Generate nilai 'fix_lot' sesuai format yang diinginkan |
|
$fixLot = $code_customer . $type . $size_peti . $lot_no . $packing_no; |
|
$validatedData['fix_lot'] = $fixLot; |
|
|
|
// Merubah warna menjadi kapital |
|
$validatedData['warna'] = strtoupper($validatedData['warna']); |
|
|
|
$validatedData['created_by'] = $currenttype->fullname; // Menggunakan nama pengguna sebagai created_by |
|
$validatedData['updated_by'] = $currenttype->fullname; // Menggunakan nama pengguna sebagai updated_by |
|
|
|
// Jika 't_warehouse_id' tidak diset, ambil nilainya dari 'warehouse_id' |
|
$validatedData['t_warehouse_id'] = $validatedData['warehouse_id']; |
|
// dd($validatedData); |
|
Peti::create($validatedData); |
|
} |
|
|
|
return redirect()->route('dashboard.peti.index')->with('success', 'Data peti berhasil ditambahkan'); |
|
} catch (\Throwable $th) { |
|
return redirect()->back()->with('error', 'Data peti Gagal Ditambah.'); |
|
} |
|
} |
|
|
|
/** |
|
* Display the specified resource. |
|
*/ |
|
public function show($id) |
|
{ |
|
$data = [ |
|
'peti' => Peti::findOrFail($id), |
|
'typepeti' => Type_peti::all(), |
|
'customer' => Customer::all(), |
|
'warehouse' => m_warehouse::all(), |
|
'active' => 'menu-peti', |
|
]; |
|
|
|
return view('dashboard.Master_Data.Manajemen_Peti.Peti.show', $data); |
|
} |
|
|
|
/** |
|
* Show the form for editing the specified resource. |
|
*/ |
|
public function edit($id) |
|
{ |
|
$data = [ |
|
'kondisiPeti' => Kondisi_Peti::get(), |
|
'peti' => Peti::findOrFail($id), |
|
'typepeti' => Type_peti::all(), |
|
'customer' => Customer::all(), |
|
'warehouse' => m_warehouse::all(), |
|
'active' => 'menu-peti', |
|
]; |
|
return view('dashboard.Master_Data.Manajemen_Peti.Peti.edit', $data); |
|
} |
|
|
|
/** |
|
* Update the specified resource in storage. |
|
*/ |
|
public function update(ValidasiUpdatePeti $request, $id) |
|
{ |
|
// dd($request->all()); |
|
try { |
|
$currentuser = Auth::user(); |
|
$typepeti = Peti::findOrFail($id); |
|
|
|
if (!$typepeti) { |
|
return redirect()->back()->with('error', 'Data peti tidak ditemukan.'); |
|
} |
|
|
|
$validatedData = $request->except('_token', '_method'); |
|
|
|
$code_customer = Customer::where('id', $validatedData['customer_id'])->first()->code_customer; |
|
$type = Type_peti::where('id', $validatedData['tipe_peti_id'])->first()->type; |
|
$size_peti = Type_peti::where('id', $validatedData['tipe_peti_id'])->first()->size_peti; |
|
$lot_no = Customer::where('id', $validatedData['customer_id'])->first()->lot_no; |
|
$packing_no = $typepeti->packing_no; // Mengambil packing_no dari entitas yang sudah ada |
|
|
|
// Generate nilai 'fix_lot' sesuai format yang diinginkan |
|
$fixLot = $code_customer . $type . $size_peti . $lot_no . $packing_no; |
|
$validatedData['fix_lot'] = $fixLot; |
|
$validatedData['updated_by'] = $currentuser->fullname; |
|
$validatedData['t_warehouse_id'] = $validatedData['warehouse_id']; |
|
|
|
// Tambahkan perubahan yang diperlukan ke entitas Peti |
|
$typepeti->update($validatedData); |
|
|
|
// // Menambahkan nama pengguna yang melakukan pembaruan |
|
// $typepeti->update(['updated_by' => $currentuser->fullname]); |
|
|
|
return redirect()->route('dashboard.peti.index')->with('success', 'Data peti berhasil diperbaharui'); |
|
} catch (\Throwable $th) { |
|
return redirect()->back()->with('error', 'Data peti gagal diperbaharui'); |
|
} |
|
} |
|
|
|
/** |
|
* Remove the specified resource from storage. |
|
*/ |
|
public function destroy($id) |
|
{ |
|
try { |
|
$typepeti = Peti::findOrFail($id); |
|
$typepeti->delete(); |
|
return redirect()->back()->with('success', 'Data peti berhasil dihapus'); |
|
} catch (\Throwable $th) { |
|
return redirect()->back()->with('error', 'Data peti gagal dihapus'); |
|
} |
|
} |
|
|
|
public function cetakPdf($id) |
|
{ |
|
try { |
|
$peti = Peti::find($id); |
|
|
|
// Generate QR Code |
|
$qrcode = base64_encode(QrCode::format('svg')->size(150)->generate( |
|
$peti->fix_lot . ';' . $peti->id . ';' . $peti->warehouse->id |
|
)); |
|
|
|
// Inisialisasi Dompdf |
|
$options = new Options(); |
|
$options->set('isHtml5ParserEnabled', true); |
|
$options->set('isRemoteEnabled', true); |
|
$options->set('defaultFont', 'Arial'); |
|
$dompdf = new Dompdf($options); |
|
|
|
// Load HTML dari view |
|
$html = view('dashboard.Master_Data.Manajemen_Peti.Peti.label_pdf', compact('peti', 'qrcode'))->render(); |
|
$dompdf->loadHtml($html); |
|
|
|
// Render PDF (portrait A4) |
|
$dompdf->setPaper('A4', 'portrait'); |
|
$dompdf->render(); |
|
$output = $dompdf->output(); |
|
|
|
// Download file PDF dengan nama yang sesuai |
|
return response()->stream( |
|
function () use ($output) { |
|
echo $output; |
|
}, |
|
200, |
|
[ |
|
'Content-Type' => 'application/pdf', |
|
'Content-Disposition' => 'inline; filename="label_Peti.pdf"', |
|
] |
|
); |
|
} catch (\Exception $e) { |
|
return redirect()->back()->with('error', 'Terjadi kesalahan saat mencetak PDF.'); |
|
} |
|
} |
|
|
|
public function AllPdf(Request $request) |
|
{ |
|
$peti_ids = $request->input('peti_ids'); |
|
$selectedIds = explode(',', $peti_ids); |
|
|
|
// Mengambil data dari database berdasarkan ID yang dipilih |
|
$peti = Peti::whereIn('id', $selectedIds)->get(); |
|
|
|
// Inisialisasi Dompdf dengan opsi |
|
$options = new Options(); |
|
$options->set('isHtml5ParserEnabled', true); |
|
$options->set('isRemoteEnabled', true); |
|
$options->set('defaultFont', 'Arial'); |
|
|
|
// Mengatur ukuran kertas sesuai dengan printer Zebra JT230 |
|
$options->set('isPhpEnabled', true); // Diperlukan agar perubahan ukuran kertas berfungsi |
|
$options->set('isHtml5ParserEnabled', true); |
|
$options->set('isPhpEnabled', true); |
|
$options->set('isPhpEnabled', true); |
|
$options->set('isPhpEnabled', true); |
|
$options->set('isPhpEnabled', true); |
|
$options->set('isPhpEnabled', true); |
|
$options->set('isPhpEnabled', true); |
|
$options->set('isPhpEnabled', true); |
|
$options->set('isPhpEnabled', true); |
|
$options->set('isPhpEnabled', true); |
|
$options->set('isPhpEnabled', true); |
|
$options->set('isPhpEnabled', true); |
|
$options->set('isPhpEnabled', true); |
|
$options->set('isPhpEnabled', true); |
|
$options->set('isPhpEnabled', true); |
|
$options->set('isPhpEnabled', true); |
|
$options->set('isPhpEnabled', true); |
|
|
|
$dompdf = new Dompdf($options); |
|
|
|
// Load HTML dari view |
|
$html = view('dashboard.Master_Data.Manajemen_Peti.Peti.all_print', compact('peti'))->render(); |
|
$dompdf->loadHtml($html); |
|
|
|
// Render PDF (portrait A4) |
|
$dompdf->setPaper([0, 0, 198.5, 396], 'portrait'); // Ukuran kertas lebar 7cm dan tinggi 14cm |
|
$dompdf->render(); |
|
$output = $dompdf->output(); |
|
|
|
// Download file PDF dengan nama yang sesuai |
|
return response()->stream( |
|
function () use ($output) { |
|
echo $output; |
|
}, |
|
200, |
|
[ |
|
'Content-Type' => 'application/pdf', |
|
'Content-Disposition' => 'inline; filename="label_Peti.pdf"', |
|
] |
|
); |
|
} |
|
|
|
public function importPeti(Request $request) |
|
{ |
|
$import = new PetiImport; |
|
|
|
try { |
|
Excel::import($import, request()->file('file')); |
|
|
|
if ($import->getRowCount() > 0) { |
|
return redirect()->route('dashboard.peti.index')->with('success', 'Data Peti berhasil diimport'); |
|
} else { |
|
$errors = $import->getErrors(); |
|
|
|
if (!empty($errors)) { |
|
session()->flash('error', $errors); |
|
} |
|
|
|
return redirect()->back()->with('import', $import)->with('error', 'Data Peti gagal di import.'); |
|
} |
|
} catch (\Maatwebsite\Excel\Validators\ValidationException $e) { |
|
$failures = $e->failures(); |
|
|
|
foreach ($failures as $failure) { |
|
session()->flash('error', ['Baris ' . $failure->row() => implode(', ', $failure->errors())]); |
|
} |
|
} |
|
} |
|
|
|
public function deleteSelected(Request $request) |
|
{ |
|
$petiIds = $request->input('peti_ids'); |
|
|
|
if (!empty($petiIds)) { |
|
Peti::destroy($petiIds); |
|
|
|
return response()->json(['message' => 'Data peti terpilih berhasil dihapus.']); |
|
} else { |
|
return response()->json(['error' => 'Tidak ada data peti terpilih untuk dihapus.'], 400); |
|
} |
|
} |
|
}
|
|
|