181 lines
5.6 KiB
PHP
181 lines
5.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admins\PerangkatPrinter;
|
|
|
|
use App\Helpers\ResponseFormatter;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\PerangkatPrinter;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Mpdf\Tag\Q;
|
|
|
|
class PerangkatPrinterController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
return view('pages.admin.perangkat_printer.index');
|
|
}
|
|
|
|
|
|
public function getDataPrinter()
|
|
{
|
|
$data = PerangkatPrinter::get();
|
|
|
|
return datatables()->of($data)
|
|
->addColumn('status_printer', function ($data) {
|
|
if ($data->status == '1') {
|
|
return '<span class="badge badge-success">Aktif</span>';
|
|
} else {
|
|
return '<span class="badge badge-danger">Tidak Aktif</span>';
|
|
}
|
|
})
|
|
->addColumn('action', function ($data) {
|
|
return '
|
|
<button class="btn btn-sm btn-warning btn-edit-printer" data-printer=' . $data . '><i class="fas fa-edit"></i></button>
|
|
<button class="btn btn-sm btn-danger btn-hapus-printer" data-printer=' . $data . '><i class="fas fa-trash"></i></button>
|
|
';
|
|
})
|
|
->rawColumns(['action' => 'action', 'status_printer' => 'status_printer'])
|
|
->addIndexColumn()
|
|
->make(true);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
// validation
|
|
$validator = Validator::make($request->all(), [
|
|
'printer_nama' => 'required',
|
|
'printer_mm' => 'required',
|
|
'printer_for' => 'required',
|
|
], [
|
|
'printer_nama.required' => 'Nama tidak boleh kosong!',
|
|
'printer_mm.required' => 'Ukuran kertas printer tidak boleh kosong!',
|
|
'printer_for.required' => 'Printer digunakan untuk tidak boleh kosong!',
|
|
]);
|
|
|
|
// check validation
|
|
if ($validator->fails()) {
|
|
return ResponseFormatter::error($validator->errors()->first());
|
|
}
|
|
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
if ($request->printer_status != 'on' || $request->printer_status == null) {
|
|
$status = '0';
|
|
} else {
|
|
$status = '1';
|
|
}
|
|
|
|
// create new account
|
|
$printer = PerangkatPrinter::create([
|
|
'nama_printer' => $request->printer_nama,
|
|
'printer_connection' => $request->printer_connection,
|
|
'ip_address' => $request->ip_address,
|
|
'printer_mm' => $request->printer_mm,
|
|
'printer_for' => $request->printer_for,
|
|
'status' => $status,
|
|
]);
|
|
|
|
DB::commit();
|
|
return ResponseFormatter::success($printer, "Printer berhasil ditambahkan");
|
|
} catch (\Throwable $th) {
|
|
DB::rollBack();
|
|
return ResponseFormatter::error($th->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*/
|
|
public function show(string $id)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*/
|
|
public function edit(string $id)
|
|
{
|
|
// get data from printer
|
|
$printer = PerangkatPrinter::findOrFail($id);
|
|
return ResponseFormatter::success([
|
|
'printer' => $printer
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(Request $request, string $id)
|
|
{
|
|
|
|
// validation
|
|
$validator = Validator::make($request->all(), [
|
|
'printer_nama_edit' => 'required',
|
|
'printer_mm_edit' => 'required',
|
|
'printer_for_edit' => 'required',
|
|
], [
|
|
'printer_nama_edit.required' => 'Nama tidak boleh kosong!',
|
|
'printer_mm_edit.required' => 'Ukuran kertas printer tidak boleh kosong!',
|
|
'printer_for_edit.required' => 'Printer digunakan untuk tidak boleh kosong!'
|
|
]);
|
|
|
|
// check validation
|
|
if ($validator->fails()) {
|
|
return ResponseFormatter::error($validator->errors()->first());
|
|
}
|
|
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
if ($request->printer_status_edit != 'on' || $request->printer_status_edit == null) {
|
|
$status = '0';
|
|
} else {
|
|
$status = '1';
|
|
}
|
|
|
|
// create new account
|
|
$printer = PerangkatPrinter::findOrFail($id);
|
|
$printer->update([
|
|
'nama_printer' => $request->printer_nama_edit,
|
|
'printer_connection' => $request->printer_connection,
|
|
'ip_address' => $request->ip_address,
|
|
'printer_mm' => $request->printer_mm_edit,
|
|
'printer_for' => $request->printer_for_edit,
|
|
'status' => $status,
|
|
]);
|
|
|
|
DB::commit();
|
|
return ResponseFormatter::success($printer, "Printer berhasil diubah");
|
|
} catch (\Throwable $th) {
|
|
DB::rollBack();
|
|
return ResponseFormatter::error($th->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy(string $id)
|
|
{
|
|
// get data from printer
|
|
$printer = PerangkatPrinter::findOrFail($id);
|
|
|
|
// check printer
|
|
if (!$printer) {
|
|
return ResponseFormatter::error("Data printer tidak ditemukan!");
|
|
}
|
|
|
|
// delete printer
|
|
$printer->delete();
|
|
|
|
return ResponseFormatter::success(null, "Printer berhasil dihapus!");
|
|
}
|
|
}
|