pos-gis/app/Models/useredcModel.php

214 lines
7.2 KiB
PHP
Raw Permalink Normal View History

2024-10-07 06:13:42 +00:00
<?php
namespace App\Models;
use Exception;
use Datatables, DB;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Http;
use Yajra\DataTables\DataTables as DT;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\QueryException;
use Illuminate\Foundation\Auth\User as Authenticatable;
class useredcModel extends Model
{
protected $primaryKey = 'user_id';
protected $table = 'users';
public $timestamps = false;
public function initData($request,$route)
{
// INIT DB
2024-10-08 06:24:47 +00:00
$data['title'] = 'User Company ';
2024-10-07 06:13:42 +00:00
$data['actButton'] = ['edit'];
$data['tableHead'] =
array(
2024-10-08 06:24:47 +00:00
["Company","all","merchant_nm"],
2024-10-07 06:13:42 +00:00
["Username","all","username"],
["Nama User","all","user_nm"],
["No HP","all","phone_no"],
["KTP","all","ktp"],
["Alamat","all","address"],
["status","all","user_status_id"],
["Act","all","action"]
);
$data['db'] = $this->table;
$data['db_key'] = $this->primaryKey;
$data['route'] = $route;
$dtable = \DB::select("SELECT user_id,users.user_status_id,username,user_nm,users.phone_no,ktp,users.address,merchant_nm,user_status_nm from users
join merchant m on m.mid = users.mid
join reff_user_status on reff_user_status.user_status_id = users.user_status_id
where role_id = 'MERCHANT_OPR'
order by user_id desc");
// LIST DATA TABLE
$data['data_table'] = $dtable;
// FORM FIELD FOR STORE
// GET DATA FOR EDIT
if ($request->post('id')) {
$data['get_data_edit'] = collect(\DB::select("SELECT * FROM users where user_id = ?",[$request->post('id')]))->first();
}
foreach($data['tableHead'] as $v){
$arrHead[] = $v[2];
}
$data['head'] = implode(",",$arrHead);
return $data;
}
public function getDT($data,$init)
{
$dt = DT::of($data);
$dt->editColumn('user_status_id',function($data) {
return ($data->user_status_id == 1) ? '<span class="label label-success label-inline mr-2">'.$data->user_status_nm.'</span>':'<span class="label label-danger label-inline mr-2">'.$data->user_status_nm.'</span>';
});
return $dt;
}
public function storeCustom($act,Request $request)
{
DB::beginTransaction();
try{
$cGlobal = new Controller();
2024-10-08 06:24:47 +00:00
$titleTrail = 'User Company';
2024-10-07 06:13:42 +00:00
switch ($act) {
case 'storeUser':
$id = $request->input('get_id');
$oldData = null;
if ($id == "") {
$event = "Tambah ".$titleTrail;
$getMaxId = \DB::table('users')->max('user_id');
$datas = new useredcModel();
$datas->user_id = $getMaxId+1;
$datas->password_retry = 0;
// $datas->date_password = date('Y-m-d');
$datas->date_password = date('Y-m-d', strtotime("-62 days"));
$exitUser = DB::table('users')->where('username', $request->post('username'))->count();
if($exitUser > 0){
return response()->json([
'rc' => 99,
'rm' => "Username Sudah Terdaftar"
]);
}
$datas->password = $cGlobal->hashPassword('P@55w0rd');
}else{
$event = "Ubah ".$titleTrail;
$oldData = \DB::table($this->table)->where('user_id',$id)->first();
$datas = useredcModel::find($id);
if ($request->post('username') == $oldData->username) {
$valid = true;
}else{
$data = \DB::select("SELECT * FROM users where username = ?",[$request->post('username')]);
$valid = (count($data) > 0) ? false : true ;
}
if ($valid == false) {
return response()->json([
'rc' => 99,
'rm' => "Username Sudah Terdaftar"
]);
}
}
$datas->username = $cGlobal->cleanString($request->post('username'));
$datas->user_nm = $cGlobal->cleanString($request->post('user_nm'));
$datas->phone_no = $request->post('phone_no');
$datas->ktp = $request->post('ktp');
$datas->email = $request->post('email');
$datas->address = $request->post('address');
$datas->mid = $request->post('mid');
$datas->role_id = 'MERCHANT_OPR';
$datas->user_status_id = 1;
$datas->reg_date = date('Y-m-d H:i:s');
$datas->save();
$cGlobal->auditTrailValue($event,$titleTrail,$this->table,json_encode($datas),json_encode($oldData));
break;
case 'resetPassword':
$event = "Reset Password User";
$cGlobal->auditTrail($event,"User");
$data = useredcModel::find($request->id);
$data->password = $cGlobal->hashPassword('P@55w0rd');
$data->password_retry = 0;
$data->date_password = date('Y-m-d');
$data->save();
break;
case 'blockUser':
$data = useredcModel::find($request->id);
$data->user_status_id = 3;
$data->password_retry = 0;
$data->save();
$cGlobal->auditTrail("Block User ID ".$request->id,"User");
break;
case 'aktifkUser':
$data = useredcModel::find($request->id);
$data->user_status_id = 1;
$data->password_retry = 0;
$data->save();
$cGlobal->auditTrail("Aktifkan User ID ".$request->id,"User");
break;
}
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()
]);
}
}
}