|
|
|
@ -5,38 +5,49 @@ namespace App\Http\Controllers;
|
|
|
|
|
use Carbon\Carbon; |
|
|
|
|
use Illuminate\Support\Str; |
|
|
|
|
use Illuminate\Http\Request; |
|
|
|
|
use Illuminate\Support\Facades\{DB,Validator}; |
|
|
|
|
use App\Models\{Company, HumanResource, Menu, ProductTransaction, RefferalCode, Role, RoleMenu, MenuCompany}; |
|
|
|
|
use Illuminate\Support\Facades\{DB,Log}; |
|
|
|
|
use App\Models\{User, Company, HumanResource, Menu, ProductTransaction, RefferalCode, Role, RoleMenu, MenuCompany}; |
|
|
|
|
|
|
|
|
|
const URL_EMAIL = 'https://notifapp.odm-iu.com/service-mail/notif_mail.php'; |
|
|
|
|
class UserRegisterController extends Controller |
|
|
|
|
{ |
|
|
|
|
public function add(Request $request) |
|
|
|
|
{ |
|
|
|
|
DB::transaction(function () use ($request) { |
|
|
|
|
$validator = Validator::make($request->all(), [ |
|
|
|
|
'refferal' => 'unique:refferal_code,code', |
|
|
|
|
'cluster' => 'required|string', |
|
|
|
|
'address' => 'required|string', |
|
|
|
|
'email' => 'required|unique:' . ($request->type_account === 'Company' ? 'm_company' : 'm_users') . ',email', |
|
|
|
|
'type_paket' => 'required|in:Basic, Free', |
|
|
|
|
'phone_no' => 'required', |
|
|
|
|
'type_account' => 'string|in:Personal, Company', |
|
|
|
|
'company_name' => 'required|string', |
|
|
|
|
'username' => 'string|required|unique:' . ($request->type_account === 'Personal' ? 'm_users' : '') . ',username' |
|
|
|
|
]); |
|
|
|
|
if ($validator->fails()) { |
|
|
|
|
return response()->json(['status' => 'failed', 'message' => $validator->errors()->first(), 'code' => 400], 400); |
|
|
|
|
} |
|
|
|
|
$data = $validator->validated(); |
|
|
|
|
DB::beginTransaction(); |
|
|
|
|
$this->validate($request, [ |
|
|
|
|
'cluster' => 'required|string', |
|
|
|
|
'address' => 'required|string', |
|
|
|
|
'email' => 'required|string', |
|
|
|
|
'type_paket' => ($request->type_account === 'Personal' ? 'required' : 'nullable') . '|in:Basic,Free', |
|
|
|
|
'phone_no' => 'required', |
|
|
|
|
'type_account' => 'string|in:Personal,Company', |
|
|
|
|
'company_name' => 'required|string', |
|
|
|
|
'username' => 'required|string' |
|
|
|
|
]); |
|
|
|
|
try { |
|
|
|
|
$data = $request->all(); |
|
|
|
|
|
|
|
|
|
$company_name = $data['company_name']; |
|
|
|
|
$type_account = $data['type_account']; |
|
|
|
|
$refferal = $data['refferal']; |
|
|
|
|
$refferal = $request->refferal ? $data['refferal'] : null; |
|
|
|
|
$cluster = $data['cluster']; |
|
|
|
|
$address = $data['address']; |
|
|
|
|
$phone_no = $data['phone_no']; |
|
|
|
|
$email = $data['email']; |
|
|
|
|
$username = $data['username']; |
|
|
|
|
$type_paket = $data['type_paket']; |
|
|
|
|
$type_paket = $request->type_account === 'Personal' ? $data['type_paket'] : ''; |
|
|
|
|
$company_address = $request->type_account === 'Company' ? $data['companyAddress'] : ''; |
|
|
|
|
$full_name = $request->type_account === 'Personal' ? $data['full_name'] : ''; |
|
|
|
|
|
|
|
|
|
// check email & username already exist |
|
|
|
|
$emailExists = Company::where('email', $email)->exists() || User::where('email', $email)->exists(); |
|
|
|
|
if ($emailExists) { |
|
|
|
|
return response()->json(['status' => 'failed', 'message' => 'Email already exists, please check again!', 'code' => 500], 500); |
|
|
|
|
} |
|
|
|
|
$usernameExists = User::where('username', $username)->exists(); |
|
|
|
|
if ($usernameExists) { |
|
|
|
|
return response()->json(['status' => 'failed', 'message' => 'Username already exists, please check again!', 'code' => 500], 500); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Get last registration number |
|
|
|
|
$getCompany = $this->getCompany(); |
|
|
|
@ -50,7 +61,9 @@ class UserRegisterController extends Controller
|
|
|
|
|
'address' => $address, |
|
|
|
|
'type_account' => $type_account, |
|
|
|
|
'username' => $username, |
|
|
|
|
'type_paket' => $type_paket |
|
|
|
|
'type_paket' => $type_paket, |
|
|
|
|
'companyAddress' => $company_address, |
|
|
|
|
'full_name' => $full_name |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
if(empty($refferal)) { |
|
|
|
@ -58,47 +71,48 @@ class UserRegisterController extends Controller
|
|
|
|
|
} else { |
|
|
|
|
$getRefferal = $this->getRefferalCode($refferal); |
|
|
|
|
if(empty($getRefferal)) { |
|
|
|
|
return response()->json(['status' => 'failed', 'message' => 'Refferal Code Not Found!', 'code' => 404], 404); |
|
|
|
|
return response()->json(['status' => 'failed', 'message' => 'Refferal code not found!', 'code' => 404], 404); |
|
|
|
|
} |
|
|
|
|
$addCompany = $this->addCompany($formDataCompany, $getRefferal['id']); |
|
|
|
|
$addCompany = $this->addCompany($formDataCompany, (int)$getRefferal['id']); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if(empty($addCompany)) { |
|
|
|
|
return; |
|
|
|
|
die(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$addRole = $this->addRole((int)$addCompany['id']); |
|
|
|
|
if(empty($addRole)) { |
|
|
|
|
return; |
|
|
|
|
die(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$addHR = $this->addHR((int)$addCompany['id'], (int)$addRole['id'], $formDataCompany); |
|
|
|
|
if(empty($addHR)) { |
|
|
|
|
return; |
|
|
|
|
die(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$getMenu = $this->getMenu(); |
|
|
|
|
if(empty($getMenu)) { |
|
|
|
|
return; |
|
|
|
|
die(); |
|
|
|
|
} |
|
|
|
|
$this->addTransaction((int)$addCompany['id'], $formDataCompany); |
|
|
|
|
if($request->type_account === 'Personal') { |
|
|
|
|
$this->addTransaction((int)$addCompany['id'], $formDataCompany); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$addMenuCompany = $this->addMenuCompany($getMenu, (int)$addCompany['id']); |
|
|
|
|
if(empty($addMenuCompany)) { |
|
|
|
|
return; |
|
|
|
|
die(); |
|
|
|
|
} |
|
|
|
|
$addMenuRole = $this->addMenuRole($getMenu, (int)$addRole['id']); |
|
|
|
|
|
|
|
|
|
if($addMenuRole) { |
|
|
|
|
return response()->json(['status' => 'success', 'message' => 'Register is successfully!', 'code' => 200], 200); |
|
|
|
|
if(empty($addMenuRole)) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
},3); |
|
|
|
|
DB::commit(); |
|
|
|
|
return response()->json(['status' => 'success', 'message' => 'Register is successful, please check your email!', 'generateRandom' => $addHR['generateRandom'], 'code' => 200], 200); |
|
|
|
|
} catch (\Throwable $th) { |
|
|
|
|
DB::rollBack(); |
|
|
|
|
Log::channel('daily')->error($th->getMessage()); |
|
|
|
|
return response()->json(['status' => 'failed', 'message' => 'Failed to register, please check again!', 'code' => 500], 500); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected function getRefferalCode($refferal) |
|
|
|
@ -119,11 +133,11 @@ class UserRegisterController extends Controller
|
|
|
|
|
'date_register' => Carbon::now(), |
|
|
|
|
'template_id' => 1, |
|
|
|
|
'email' => $formData['email'], |
|
|
|
|
'address' => $formData['address'], |
|
|
|
|
'address' => $formData['type_account'] === 'Company' ? $formData['companyAddress'] : $formData['address'], |
|
|
|
|
'phone_no' => $formData['phone_no'], |
|
|
|
|
'type_account' => $formData['type_account'], |
|
|
|
|
'is_active' => true, |
|
|
|
|
'discount_id' => $id_refferal === null ? null : (int)$id_refferal |
|
|
|
|
'discount_id' => $id_refferal === null ? null : $id_refferal |
|
|
|
|
); |
|
|
|
|
$result = Company::create($formData); |
|
|
|
|
return $result; |
|
|
|
@ -142,25 +156,27 @@ class UserRegisterController extends Controller
|
|
|
|
|
|
|
|
|
|
protected function addHR($id_company, $id_role, $data) |
|
|
|
|
{ |
|
|
|
|
$generatePassword = Str::password(10); |
|
|
|
|
$generateRandom = Str::random(8); |
|
|
|
|
$formData = array( |
|
|
|
|
'name'=> $data['company_name'], |
|
|
|
|
'name'=> $data['type_account'] === 'Company' ? $data['username'] : $data['full_name'], |
|
|
|
|
'phone_number'=> $data['phone_no'], |
|
|
|
|
'email'=> $data['email'], |
|
|
|
|
'username' => $data['username'], |
|
|
|
|
'password'=> $generatePassword, |
|
|
|
|
'role_id'=> (int)$id_role, |
|
|
|
|
'ktp_number'=> $data['type_account'] === 'Company' ? 'CP-' : 'PR-' . Str::random(5), |
|
|
|
|
'employee_type'=>'employee', |
|
|
|
|
'password'=> md5($generateRandom), |
|
|
|
|
'role_id'=> $id_role, |
|
|
|
|
'ktp_number'=> $data['type_account'] === 'Company' ? 'CP-'. $generateRandom : 'PR-' . $generateRandom, |
|
|
|
|
'employee_type'=> 'employee', |
|
|
|
|
'address' => $data['address'], |
|
|
|
|
'status_resource'=> 'active', |
|
|
|
|
'company_id'=> (int)$id_company |
|
|
|
|
'company_id'=> $id_company |
|
|
|
|
); |
|
|
|
|
$result = HumanResource::create($formData); |
|
|
|
|
|
|
|
|
|
if(!empty($result)) { |
|
|
|
|
$this->sendEmail($data['email'], $generateRandom); |
|
|
|
|
} |
|
|
|
|
return [ |
|
|
|
|
'result' => $result, |
|
|
|
|
'generatePassword' => $generatePassword |
|
|
|
|
'generateRandom' => $generateRandom |
|
|
|
|
]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -170,7 +186,7 @@ class UserRegisterController extends Controller
|
|
|
|
|
if($data->exists()){ |
|
|
|
|
$data->delete(); |
|
|
|
|
} |
|
|
|
|
if (is_array($baseDataMenu) && count($baseDataMenu) > 0 && isset($baseDataMenu)) { |
|
|
|
|
if (is_object($baseDataMenu) && count($baseDataMenu) > 0 && isset($baseDataMenu)) { |
|
|
|
|
$countRes = 0; |
|
|
|
|
foreach ($baseDataMenu as $menu) { |
|
|
|
|
$dataInsert = array( |
|
|
|
@ -193,16 +209,16 @@ class UserRegisterController extends Controller
|
|
|
|
|
if ($countRes > 0) { |
|
|
|
|
return $result; |
|
|
|
|
} else { |
|
|
|
|
return $result; |
|
|
|
|
die(); |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
return false; |
|
|
|
|
die(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected function addMenuRole($baseDataMenu, $id_role) |
|
|
|
|
{ |
|
|
|
|
if (is_array($baseDataMenu) && count($baseDataMenu) > 0 && isset($baseDataMenu)) { |
|
|
|
|
if (is_object($baseDataMenu) && count($baseDataMenu) > 0 && isset($baseDataMenu)) { |
|
|
|
|
$countRes = 0; |
|
|
|
|
foreach ($baseDataMenu as $menu) { |
|
|
|
|
$dataInsert = array( |
|
|
|
@ -219,10 +235,10 @@ class UserRegisterController extends Controller
|
|
|
|
|
if ($countRes > 0) { |
|
|
|
|
return $result; |
|
|
|
|
} else { |
|
|
|
|
return $result; |
|
|
|
|
die(); |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
return false; |
|
|
|
|
die(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -263,11 +279,68 @@ class UserRegisterController extends Controller
|
|
|
|
|
$lastRegistrationNumber = $company['registration_no']; |
|
|
|
|
$lastNumber = (int)preg_replace('/\D/', '', $lastRegistrationNumber); |
|
|
|
|
$newNumber = $lastNumber + 1; |
|
|
|
|
// $newRegistrationNumber = Str::beforeLast($lastRegistrationNumber, $lastNumber) . $newNumber; |
|
|
|
|
$newRegistrationNumber = 'RG-'. $newNumber; |
|
|
|
|
} else{ |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
return [ |
|
|
|
|
'newRegistrationNumber' => $newRegistrationNumber |
|
|
|
|
]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public function sendEmail($email, $password) |
|
|
|
|
{ |
|
|
|
|
$user = User::select('email', 'name', 'username')->where('email', $email)->first(); |
|
|
|
|
if (!$user) { |
|
|
|
|
return response()->json(['status' => 'error', 'message' => 'e-mail not found '], 400); |
|
|
|
|
} else { |
|
|
|
|
$this->reqHttpCurl($email, $password, $user->username, $user->name); |
|
|
|
|
return response()->json(['status' => 'error', 'code'=>400, 'message' => 'e-mail not found '], 400); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private function reqHttpCurl($email, $password, $username, $name) |
|
|
|
|
{ |
|
|
|
|
$postData = [ |
|
|
|
|
"to" => $email, |
|
|
|
|
"name" => $name, |
|
|
|
|
"username" => $username, |
|
|
|
|
"password" => $password, |
|
|
|
|
"from" => "app.integrasia@integrasiautama.com", |
|
|
|
|
"alias_from" => "OSPRO", |
|
|
|
|
"subject" => "Registration OSPRO", |
|
|
|
|
"body" => "registration-ospro" |
|
|
|
|
]; |
|
|
|
|
|
|
|
|
|
$curl = curl_init(); |
|
|
|
|
|
|
|
|
|
curl_setopt_array($curl, array( |
|
|
|
|
CURLOPT_URL => URL_EMAIL, |
|
|
|
|
CURLOPT_RETURNTRANSFER => true, |
|
|
|
|
CURLOPT_ENCODING => "", |
|
|
|
|
CURLOPT_MAXREDIRS => 10, |
|
|
|
|
CURLOPT_TIMEOUT => 30000, |
|
|
|
|
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
|
|
|
|
CURLOPT_CUSTOMREQUEST => "POST", |
|
|
|
|
CURLOPT_POSTFIELDS => json_encode($postData), |
|
|
|
|
CURLOPT_HTTPHEADER => array( |
|
|
|
|
// Set here requred headers |
|
|
|
|
"accept: */*", |
|
|
|
|
"accept-language: en-US,en;q=0.8", |
|
|
|
|
"content-type: application/json", |
|
|
|
|
), |
|
|
|
|
)); |
|
|
|
|
|
|
|
|
|
$response = curl_exec($curl); |
|
|
|
|
$err = curl_error($curl); |
|
|
|
|
|
|
|
|
|
curl_close($curl); |
|
|
|
|
|
|
|
|
|
if ($err) { |
|
|
|
|
echo "cURL Error #:" . $err; |
|
|
|
|
} else { |
|
|
|
|
print_r(json_decode($response)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|