pos-gis/app/Http/Controllers/RefController.php

610 lines
21 KiB
PHP
Raw Normal View History

2024-10-07 06:13:42 +00:00
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use Auth;
use Illuminate\Database\QueryException;
use App\Imports\DeviceImport;
use Maatwebsite\Excel\Facades\Excel;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Contracts\Encryption\DecryptException;
date_default_timezone_set('Asia/Jakarta');
class RefController extends Controller
{
// LIST BUTTON
// edit,active,setting_menu
public function getInit($request,$route)
{
$modelName = '\\App\Models\\'.$route.'Model';
$model = new $modelName;
$data = $model->initData($request,$route);
return $data;
}
public function index(Request $request)
{
$checkRole = $this->checkRoleAccessRef($request->get('type'));
if ($checkRole == false) {
return view('errors.404');
}
//INIT
$init = $this->getInit($request,$request->get('type'));
$route = $init['route'];
$param['title'] = $init['title'];
$param['tableHead'] = $init['tableHead'];
$param['head'] = $init['head'];
$view = 'content.ref.index';
//END INIT
$param['table'] = route('ref.data')."?type=".$route;
$param['edit'] = route('ref.edit')."?type=".$route;
$param['store'] = route('ref.store')."?type=".$route;
$param['storeCustom'] = route('ref.storeCustom')."?type=".$route;
$param['setActive'] = route('ref.setActive')."?type=".$route;
$param['delete'] = route('ref.hapus')."?type=".$route;
$param['import_xls'] = route('ref.import.xls')."?type=".$route;
$param['vForm'] = 'content.ref.'.$route.'.form';
$param['vAction'] = 'content.ref.'.$route.'.action';
$param['init'] = $init;
//REF
$param['ref_role'] = \DB::select("SELECT * FROM groups_admin WHERE is_active = 't'");
return $this->bsGetView($view,$param);
}
public function data(Request $request)
{
$checkRole = $this->checkRoleAccessRef($request->get('type'));
if ($checkRole == false) {
return view('errors.404');
}
$init = $this->getInit($request,$request->get('type'));
$type = $request->get('type');
$data = $init['data_table'];
$modelName = '\\App\Models\\'.$init['route'].'Model';
$model = new $modelName;
$dt = $model->getDT($data,$init);
$dt->addColumn('action', function ($data) use($init,$type) {
// $param['id_key'] = $data->{$init['db_key']};
$param['id_key'] = Crypt::encryptString($data->{$init['db_key']});
$param['data'] = $data;
$param['type'] = $type;
//SET BUTTON
return $this->getButtonAction($init['actButton'],$param);
});
$dt->rawColumns(['logo','user_status_id','is_hotel','is_resto','is_mini_atm_active','tax_list','sn_list','is_sale_active','tsid','action','is_active','is_park_active','is_market_active','product_nominal']);
return $dt->make(true);
}
public function store(Request $request)
{
$checkRole = $this->checkRoleAccessRef($request->get('type'));
if ($checkRole == false) {
return view('errors.404');
}
DB::beginTransaction();
try{
//GET INIT
$init = $this->getInit($request,$request->get('type'));
$get_id = $request->input('get_id');
$setField = $init['set_field'];
if ($get_id) {
$get_id = $this->decryptIdOnly($get_id);
}
// SPECIAL CONDITION
$check = $this->specialCondition($init['db'],$setField,$get_id);
if ($check['rc'] == 99) {
return response()->json([
'rc' => 99,
'rm' => $check['rm']
]);
}
//IF EDIT DATA
if ($get_id) {
if (isset($init['timestamps'])) {
$setField['updated_at'] = date('Y-m-d H:i:s');
}
$oldData = DB::table($init['db'])->where($init['db_key'], $get_id)->first();
DB::table($init['db'])->where($init['db_key'], $get_id)->update($setField);
$event = "Edit Data ".$request->get('type');
$this->auditTrailValue($event,"Referensi",$init['db'],json_encode($setField),json_encode($oldData));
//IF ADD DATA
}else {
if (isset($init['timestamps'])) {
$setField['created_at'] = date('Y-m-d H:i:s');
$setField['updated_at'] = date('Y-m-d H:i:s');
}
$seq = DB::table($init['db'])->max($init['db_key']);
$setField[$init['db_key']] = $seq+1;
DB::table($init['db'])->insert($setField);
$event = "Tambah Data ".$request->get('type');
$this->auditTrailValue($event,"Referensi",$init['db'],json_encode($setField),'');
}
DB::commit();
return response()->json([
'rc' => 0,
'rm' => "sukses"
]);
}
catch (QueryException $e){
if($e->getCode() == '23505'){
$response = "Terjadi Duplikasi Data, Data Gagal Disimpan !";
}else{
$response = "Terjadi Kesalahan, Data Tidak Sesuai !";
}
DB::rollback();
return response()->json([
'rc' => 99,
'rm' => $response,
// // 'msg' => $e->getMessage()
]);
}
}
public function storeCustom(Request $request)
{
$checkRole = $this->checkRoleAccessRef($request->get('type'));
if ($checkRole == false) {
return view('errors.404');
}
$decryt = $this->decryptId($request);
if ($decryt['rc'] == 0) {
$init = $this->getInit($decryt['data'],$request->get('type'));
$modelName = '\\App\Models\\'.$init['route'].'Model';
$model = new $modelName;
return $model->storeCustom($request->get('act'),$decryt['data']);
}
}
public function edit(Request $request)
{
$checkRole = $this->checkRoleAccessRef($request->get('type'));
if ($checkRole == false) {
return view('errors.404');
}
$decryt = $this->decryptId($request);
if ($decryt['rc'] == 0) {
$init = $this->getInit($decryt['data'],$request->get('type'));
$getData = $init['get_data_edit'];
if($request->get('type') == 'merchant'){
$getData['merchant']->get_id = Crypt::encryptString($getData['merchant']->mid);
}
else if($request->get('type') == 'toko'){
$getData['outlet']->outlet_id = Crypt::encryptString($getData['outlet']->outlet_id);
}
else{
$getData->{$init['db_key']} = Crypt::encryptString($getData->{$init['db_key']});
}
return response()->json([
'rc' => 0,
'data' => $getData
]);
}
}
public function hapus(Request $request)
{
$checkRole = $this->checkRoleAccessRef($request->get('type'));
if ($checkRole == false) {
return view('errors.404');
}
$decryt = $this->decryptId($request);
if ($decryt['rc'] == 0) {
$init = $this->getInit($decryt['data'],$request->get('type'));
DB::table($init['db'])->where($init['db_key'], $decryt['data']->id)->delete();
$event = "Hapus Data ".$request->get('type');
$this->auditTrailValue($event,"Data",$init['db'],$decryt['data']->id,'');
return response()->json([
'rc' => 0,
'rm' => "Berhasil"
]);
}
}
public function SetActive(Request $request)
{
//GET INIT
$checkRole = $this->checkRoleAccessRef($request->get('type'));
if ($checkRole == false) {
return view('errors.404');
}
$decryt = $this->decryptId($request);
if ($decryt['rc'] == 0) {
$init = $this->getInit($decryt['data'],$request->get('type'));
$id = $request->input('id');
$isActive = $request->input('active');
if ($isActive == true) {
DB::table($init['db'])->where($init['db_key'], $decryt['data']->id)->update([
'is_active' => false,
]);
$reqMessage = 'berhasil dinonaktifkan';
$event = $request->get('type')." ".$reqMessage;
// $this->auditTrail($event,"Referensi");
$this->auditTrailValue($event,"Aktifasi",$init['db'],$decryt['data']->id,'');
} else {
if ($init['db'] == 'mst_wscreen') {
DB::table('mst_wscreen')->update(['is_active' => false]);
}
DB::table($init['db'])->where($init['db_key'], $decryt['data']->id)->update([
'is_active' => true,
]);
$reqMessage = 'berhasil diaktifkan';
$event = $request->get('type')." ".$reqMessage;
// $this->auditTrail($event,"Referensi");
$this->auditTrailValue($event,"Aktifasi",$init['db'],$decryt['data']->id,'');
}
return response()->json([
'rc' => 0,
'rm' => $reqMessage
]);
}
}
public function importXls(Request $request)
{
if($request->hasFile('xls_file')){
$import = new DeviceImport;
Excel::import($import, $request->file('xls_file'));
if ($import->validate) {
return response()->json([
'rc' => 99,
'rm' => 'Data Tidak Sesuai',
'msg'=> $import->validate
]);
}else{
return response()->json([
'rc' => 0,
'rm' => 'berhasil',
'msg' => 'berhasil'
]);
}
}
}
public function getButtonAction($typeButton,$param)
{
$editBtn ="";
$detailBtn ="";
$ActiveBtn = "";
$deleteBtn = "";
// DETAIL BUTTON
if ( in_array("detail", $typeButton)) {
$detailBtn = '<li class="navi-item">
<a onclick="detail(`'.$param['id_key'].'`)" class="navi-link">
<span class="navi-icon"><i class="la la-arrows-alt"></i></span>
<span class="navi-text"> Detail</span>
</a>
</li>';
}
// EDIT BUTTON
if (in_array("edit", $typeButton)) {
$editBtn = ' <li class="navi-item">
<a onclick="edit(`'.$param['id_key'].'`)" class="navi-link">
<span class="navi-icon"><i class="la la-edit"></i></span>
<span class="navi-text"> Edit</span>
</a>
</li>';
}
// ACTIVE BUTTON
if (in_array("active", $typeButton)) {
if ($param['data']->is_active == 't') {
$ActiveBtn = '
<li class="navi-item">
<a onclick="setActive(`'.$param['id_key'].'`,`'.$param['data']->is_active.'`)" class="navi-link">
<span class="navi-icon"><i class="la la-times-circle-o"></i> </span>
<span class="navi-text">Deactived</span>
</a>
</li>';
}else{
$ActiveBtn = '
<li class="navi-item">
<a onclick="setActive(`'.$param['id_key'].'`,`'.$param['data']->is_active.'`)" class="navi-link">
<span class="navi-icon"><i class="la la-check-circle-o"></i> </span>
<span class="navi-text">Active</span>
</a>
</li>';
}
}
// DELETE BUTTON
if ( in_array("delete", $typeButton)) {
$deleteBtn = '<li class="navi-item">
<a onclick="hapus(`'.$param['id_key'].'`)" class="navi-link">
<span class="navi-icon"><i class="la la-trash"></i></span>
<span class="navi-text"> Hapus</span>
</a>
</li>';
}
$etcButton = '';
// CONDITION BUTTON
// if ($param['type'] == 'merchant') {
// }
if ($param['type'] == 'statustrx') {
$etcButton .= '<li class="navi-item">
<a onclick="detail(`'.$param['data']->id.'`)" class="navi-link">
<span class="navi-icon"><i class="la la-list"></i></span>
<span class="navi-text">Detail</span>
</a>
</li>';
}
if ($param['type'] == 'useredc') {
$etcButton .= '<li class="navi-item">
<a onclick="resetPassword(`'.$param['id_key'].'`)" class="navi-link">
<span class="navi-icon"><i class="la la-key"></i></span>
<span class="navi-text">Reset Password</span>
</a>
</li>';
if (Auth::guard('admin')->user()->role == 1) {
if ($param['data']->user_status_id == 3) {
$etcButton .= '<li class="navi-item">
<a onclick="aktifUser(`'.$param['id_key'].'`)" class="navi-link">
<span class="navi-icon"><i class="la la-check-circle"></i> </span>
<span class="navi-text">Aktifkan User</span>
</a>
</li>';
}else{
$etcButton .= '<li class="navi-item">
<a onclick="blockUser(`'.$param['id_key'].'`)" class="navi-link">
<span class="navi-icon"><i class="la la-ban"></i> </span>
<span class="navi-text">Block User</span>
</a>
</li>';
}
}
}
if ($param['type'] == 'rekaptrx') {
$etcButton .= '<li class="navi-item">
<a onclick="detail(`'.$param['data']->jenis_transaksi.'`)" class="navi-link">
<span class="navi-icon"><i class="la la-list"></i></span>
<span class="navi-text">Detail</span>
</a>
</li>';
}
if ($param['type'] == 'merchant') {
$etcButton = '
<li class="navi-item">
<a onclick="tax(`'.$param['id_key'].'`)" class="navi-link">
<span class="navi-icon"><i class="la la-list"></i> </span>
<span class="navi-text">List Tax</span>
</a>
</li>';
}
if ($param['type'] == 'toko') {
$etcButton .= '<li class="navi-item">
<a onclick="confirm(`'.$param['data']->outlet_id.'`,`'.$param['data']->outlet_nm.'`)" class="navi-link">
<span class="navi-icon"><i class="la la-qrcode"></i></span>
2024-10-08 06:24:47 +00:00
<span class="navi-text">Confirm Agent</span>
2024-10-07 06:13:42 +00:00
</a>
</li>';
$etcButton .= '<li class="navi-item">
<a onclick="edc(`'.$param['id_key'].'`,`'.$param['data']->outlet_nm.'`)" class="navi-link">
<span class="navi-icon"><i class="la la-list"></i></span>
<span class="navi-text">Link EDC</span>
</a>
</li>';
}
// CONDITION BUTTON
$resultBtn = $etcButton.$editBtn.$ActiveBtn.$detailBtn.$deleteBtn;
return '
<div class="dropdown dropdown-inline">
<a href="javascript:;" class="btn btn-sm btn-clean btn-icon mr-2" data-toggle="dropdown">
<span class="svg-icon svg-icon-md">
<svg width="24px" height="24px" viewBox="0 0 24 24" version="1.1">
<g stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<rect x="0" y="0" width="24" height="24"/>
<path d="M5,8.6862915 L5,5 L8.6862915,5 L11.5857864,2.10050506 L14.4852814,5 L19,5 L19,9.51471863 L21.4852814,12 L19,14.4852814 L19,19 L14.4852814,19 L11.5857864,21.8994949 L8.6862915,19 L5,19 L5,15.3137085 L1.6862915,12 L5,8.6862915 Z M12,15 C13.6568542,15 15,13.6568542 15,12 C15,10.3431458 13.6568542,9 12,9 C10.3431458,9 9,10.3431458 9,12 C9,13.6568542 10.3431458,15 12,15 Z" fill="#000000"/>
</g>
</svg>
</span>
</a>
<div class="dropdown-menu dropdown-menu-sm dropdown-menu-right">
<ul class="navi flex-column navi-hover py-2">
<li class="navi-header font-weight-bolder text-uppercase font-size-xs text-primary pb-2">
Choose an action:
</li>
'.$resultBtn.'
</ul>
</div>
</div>
';
}
public function specialCondition($db,$setField,$get_id)
{
//IF EDIT DATA
if ($get_id) {
if ($db == 'device') {
$data = DB::table('device')->where('device_id',$get_id)->first();
$validText = '';
if ($setField['sn'] == $data->sn) {
}else{
$CheckSN = DB::table('device')->where('sn',$setField['sn'])->count();
$validSN = ($CheckSN > 0) ? false : true;
if ($validSN == false) {
$validText .= '<li>Serial Number Telah Digunakan.</li>';
}
}
if ($setField['imei'] == $data->imei) {
}else{
$CheckIMEI = DB::table('device')->where('imei',$setField['imei'])->count();
$validIMEI = ($CheckIMEI > 0) ? false : true;
if ($validIMEI == false) {
$validText .= '<li>IMEI Telah Digunakan.</li>';
}
}
$validTextCont = '<ul style="margin-left:-20px;list-style-type:circle;">'.$validText.'</ul>';
if ($validText) {
$response['rm'] = $validTextCont;
$response['rc'] = 99;
}else{
$response['rm'] = "Pass";
$response['rc'] = 0;
}
}else{
$response['rm'] = "Pass";
$response['rc'] = 0;
}
return $response;
}
//IF ADD DATA
else{
if ($db == 'device') {
$CheckSN = DB::table('device')->where('sn',$setField['sn'])->count();
$CheckIMEI = DB::table('device')->where('imei',$setField['imei'])->count();
$validText = '';
if ($CheckSN > 0) {
$validText .= '<li>Serial Number Telah Digunakan.</li>';
}
if ($CheckIMEI > 0) {
$validText .= '<li>IMEI Telah Digunakan.</li>';
}
$validTextCont = '<ul style="margin-left:-20px;list-style-type:circle;">'.$validText.'</ul>';
if ($validText) {
$response['rm'] = $validTextCont;
$response['rc'] = 99;
}else{
$response['rm'] = "Pass";
$response['rc'] = 0;
}
}
else{
$response['rm'] = "Pass";
$response['rc'] = 0;
}
return $response;
}
}
}