Browse Source

fix: handle validation add & update data

pull/1/head
wahyun 6 months ago
parent
commit
1c387c4b60
  1. 59
      app/Http/Controllers/HumanResourceController.php

59
app/Http/Controllers/HumanResourceController.php

@ -2,12 +2,8 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Models\User;
use App\Models\UserToProyek;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Models\HumanResource; use App\Models\{HumanResource,ProductTransaction,UserToActivity,UserToProyek,User};
use App\Models\UserToActivity;
use App\Models\ProductTransaction;
use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Artisan;
const URL_EMAIL = 'https://notifapp.odm-iu.com/service-mail/notif_mail.php'; const URL_EMAIL = 'https://notifapp.odm-iu.com/service-mail/notif_mail.php';
@ -16,29 +12,36 @@ class HumanResourceController extends Controller
{ {
public function add(Request $request) public function add(Request $request)
{ {
$messages = [
'ktp_number.unique' => 'Nomor KTP/ID Card tidak boleh sama',
'username.unique' => 'Username tidak boleh sama',
'required' => 'Data tidak boleh kosong'
];
$this->validate($request, [ $this->validate($request, [
'name' => 'required', 'name' => 'required|string',
'ktp_number' => 'required|string|unique:m_users,ktp_number', 'ktp_number' => 'required|string|unique:m_users,ktp_number',
'employee_type' => 'required', 'employee_type' => 'required|in:employee,subcon,freelance',
'company_id' => 'required', 'company_id' => 'required|integer',
'username' => 'unique:m_users,username', 'username' => 'string|unique:m_users,username',
]); 'email' => 'string|unique:m_users,email'
], $messages);
$data = $request->all(); $data = $request->all();
!$request->is_customer ? $this->validate($request,['role_id'=>'required','divisi_id'=>'required']) : ''; !$request->is_customer ? $this->validate($request,['role_id' => 'required','divisi_id' => 'required'], $messages) : '';
if (isset($request->password) && $request->password != "") { if (isset($request->password) && $request->password != "") {
$data['password'] = md5($request->password); $data['password'] = md5($request->password);
} }
$transaction = ProductTransaction::query() $transaction = ProductTransaction::query()
->where('company_id', $request->company_id); ->where('company_id', (int) $request->company_id);
$cloneQueryTransaction = clone $transaction; $cloneQueryTransaction = clone $transaction;
$countCreate = false; $countCreate = false;
$projectResult = HumanResource::query() $projectResult = HumanResource::query()
->selectRaw('count(*) as total_hr') ->selectRaw('count(*) as total_hr')
->where('company_id', $request->company_id) ->where('company_id', (int) $request->company_id)
->first(); ->first();
if($transaction->where([['type_paket','Basic'],['amount','!=',null]])->exists()) { if($transaction->where([['type_paket','Basic'],['amount','!=',null]])->exists()) {
if($projectResult['total_hr'] < 50) { if($projectResult['total_hr'] < 50) {
@ -67,6 +70,7 @@ class HumanResourceController extends Controller
public function add_user(Request $request) public function add_user(Request $request)
{ {
$this->validate($request, [ $this->validate($request, [
'role_id' => 'required', 'role_id' => 'required',
'name' => 'required', 'name' => 'required',
@ -111,12 +115,30 @@ class HumanResourceController extends Controller
public function update(Request $request, $id) public function update(Request $request, $id)
{ {
$messages = [
'ktp_number.unique' => 'Nomor KTP/ID Card tidak boleh sama',
'username.unique' => 'Username tidak boleh sama',
'required' => 'Data tidak boleh kosong'
];
!$request->is_customer ? $this->validate($request,['role_id'=>'required','divisi_id'=>'required']) : ''; $request->validate($request, [
'name' => 'required|string',
'ktp_number' => 'required|string|unique:m_users,ktp_number,'.$id . ',id',
'employee_type' => 'required|in:employee,subcon,freelance',
'company_id' => 'required|integer',
'username' => 'unique:m_users,username,'.$id . ',id',
'email' => 'string|unique:m_users,email,'.$id . ',id'
], $messages);
!$request->is_customer ? $this->validate($request,['role_id'=>'required', 'divisi_id'=>'required'], $messages) : '';
$request->password ? $this->validate($request,['password'=>'required|min:8']) : '';
$data = $request->all();
if (!$id || (int) $id < 0 || $id == "") { if (!$id || (int) $id < 0 || $id == "") {
return response()->json(['status' => 'failed', 'message' => 'Id is required!', 'code' => 400], 400); return response()->json(['status' => 'failed', 'message' => 'Id is required!', 'code' => 400], 400);
} }
$humanresource = HumanResource::find($id); $humanresource = HumanResource::find($id);
if(empty($humanresource)) { if(empty($humanresource)) {
return response()->json(['status' => 'failed', 'message' => 'Data Human Resource not found!', 'code' => 400], 400); return response()->json(['status' => 'failed', 'message' => 'Data Human Resource not found!', 'code' => 400], 400);
@ -149,20 +171,11 @@ class HumanResourceController extends Controller
} else { } else {
$countCreate = true; $countCreate = true;
} }
$request->ktp_number !== $humanresource['ktp_number'] ? $this->validate($request,['ktp_number'=>'unique:m_users,ktp_number|string']) : '';
$request->email !== $humanresource['email'] ? $this->validate($request,['email'=>'unique:m_users,email|email']) : '';
$request->password ? $this->validate($request,['password'=>'required|min:8']) : '';
$data = $request->all();
if (isset($request->password) && $request->password != "") { if (isset($request->password) && $request->password != "") {
$data['password'] = md5($request->password); $data['password'] = md5($request->password);
} }
if (isset($data['username']) && HumanResource::where('username', $data['username'])->where('id', '<>', $id)->exists()) {
return response()->json(['status' => 'failed', 'message' => 'Username already exists!', 'code' => 400], 400);
}
if ($countCreate) { if ($countCreate) {
$result = $humanresource->update($data); $result = $humanresource->update($data);
} else { } else {

Loading…
Cancel
Save