Writeoff/Http/Controllers/ApprovalController.php

179 lines
5.8 KiB
PHP
Raw Normal View History

2023-12-19 11:00:24 +00:00
<?php
namespace Modules\Writeoff\Http\Controllers;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Auth;
use Modules\Writeoff\DataTables\ApprovalDataTable;
use Modules\Writeoff\Entities\Approval;
class ApprovalController extends Controller
{
public function __construct()
{
$this->middleware(function ($request, $next) {
$this->user = Auth::guard('web')->user();
return $next($request);
});
}
/**
* Display a listing of the resource.
*
* @return Renderable
*/
public function index(ApprovalDataTable $dataTable)
{
return $dataTable->render('writeoff::approval.index');
}
/**
* Show the form for creating a new resource.
*
* @return Renderable
*/
public function create()
{
return view('cetaklabel::create');
}
/**
* Store a newly created resource in storage.
*
* @param Request $request
*
* @return Renderable
*/
public function store(Request $request)
{
//
}
/**
* Show the specified resource.
*
* @param int $id
*
* @return Renderable
*/
public function show($id)
{
return view('cetaklabel::show');
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
*
* @return Renderable
*/
public function edit($id)
{
$authorization = Approval::findOrFail($id);
return view('writeoff::approval.edit', compact('authorization'));
}
/**
* Update the specified resource in storage.
*
* @param Request $request
* @param int $id
*
* @return Renderable
*/
public function update(Request $request, $id)
{
2024-01-17 04:07:44 +00:00
$_model = [
2024-01-17 04:34:12 +00:00
'Parameter Cabang' => 'Branch',
'Parameter Mata Uang' => 'Currency',
'Parameter Jenis Jaminan' => 'GuaranteeType',
2024-01-17 04:37:43 +00:00
'Parameter Jenis Fasilitas' => 'FacilityType',
'Parameter Jenis Pinjaman' => 'LoanType',
2024-01-17 04:40:34 +00:00
'Parameter Debitur' => 'Debitur',
2024-01-17 04:41:49 +00:00
'Parameter Rekening' => 'Rekening',
2024-01-17 04:49:27 +00:00
'Subrogasi Jamkrindo' => 'SubrogasiJamkrindo',
2024-01-17 04:52:49 +00:00
'Klaim Jamkrindo' => 'KlaimJamkrindo',
2024-02-21 07:30:52 +00:00
'Hapus Buku' => 'HapusBuku',
2024-04-02 08:49:39 +00:00
'Detail Jaminan' => 'DetailJaminan',
'Detail Pembayaran' => 'DetailPembayaran',
'Detail Penagihan' => 'DetailPenagihan',
2024-01-17 04:07:44 +00:00
];
2024-01-17 11:02:29 +00:00
2023-12-19 11:00:24 +00:00
try {
$approval = Approval::findOrFail($id);
$approval->approved_by = $this->user->id;
2024-01-23 03:09:37 +00:00
$approval->approved_at = date('Y-m-d H:i:s');
2023-12-19 11:00:24 +00:00
$approval->status = $request->status;
$model = "Modules\\Writeoff\\Entities\\" . $_model[$approval->menu];
2023-12-19 11:00:24 +00:00
$model = new $model();
2024-02-21 07:30:52 +00:00
2023-12-19 11:00:24 +00:00
if ($approval->status == 1) {
if ($approval->method == 'create') {
2024-01-17 04:07:44 +00:00
$data = json_decode($approval->new_request, true);
2023-12-19 11:00:24 +00:00
$data['authorized_status'] = 1;
$data['authorized_by'] = $this->user->id;
2024-01-23 03:09:37 +00:00
$data['authorized_at'] = date('Y-m-d H:i:s');
2023-12-19 11:00:24 +00:00
2024-01-17 04:07:44 +00:00
$model->fill($data);
$model->save();
2023-12-19 11:00:24 +00:00
} else if ($approval->method == 'update') {
$old = json_decode($approval->old_request, true);
$data = json_decode($approval->new_request, true);
$data['authorized_status'] = 1;
$data['authorized_by'] = $this->user->id;
2024-01-23 03:09:37 +00:00
$data['authorized_at'] = date('Y-m-d H:i:s');
$model = $model::findOrFail($old['id']);
2023-12-19 11:00:24 +00:00
2024-01-17 04:07:44 +00:00
$model->fill($data);
$model->save();
2023-12-19 11:00:24 +00:00
} else if ($approval->method == 'delete') {
$data = json_decode($approval->old_request, true);
$model = $model::findOrFail($data['id']);
$data_ = [];
$data_['authorized_status'] = 1;
$data_['authorized_by'] = $this->user->id;
2024-01-23 03:09:37 +00:00
$data_['authorized_at'] = date('Y-m-d H:i:s');
$model = $model::findOrFail($data['id']);
2023-12-19 11:00:24 +00:00
$model->fill($data_);
$model->save();
$model->delete();
}
}
$approval->save();
$approval->delete();
echo json_encode([
'status' => 'success',
2024-01-17 04:34:12 +00:00
'message' => 'Approval ' . $approval->description . ' successfully . '
2023-12-19 11:00:24 +00:00
]);
} catch (Exception $e) {
2024-01-17 04:41:49 +00:00
echo json_encode([
'status' => 'error',
'message' => 'Approval ' . $approval->description . ' failed . '
2024-01-17 04:40:34 +00:00
]);
2023-12-19 11:00:24 +00:00
}
}
/**
* Remove the specified resource from storage.
*
* @param int $id
*
* @return Renderable
*/
public function destroy($id)
{
//
}
}