Files
erahn-click/app/Http/Controllers/TacController.php
T
2025-06-16 12:28:38 +08:00

204 lines
7.2 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use App\Models\tacportal;
use App\Models\User;
use Carbon\Carbon;
class TacController extends Controller
{
public function TACrequest(Request $request){
$tac = tacportal::where([['icno',$request->icno],['tac_no',$request->tac]])->get()->toArray();
if($tac){
$request->session()->put('registerlog_tac_entered', true);
$DetailsCustomer = $this->getCustomerData($request->icno, true);
return response(['status' => 1, 'customerinfo' => $DetailsCustomer['customerinfo'],'namadaerah' => $DetailsCustomer['daerah']['namadaerah'],'namanegeri' => $DetailsCustomer['negeri']['negeri']]);
}else{
return response(['status' => 0]);
}
}
public function TACrequestfe(Request $request){
$tac = tacportal::where([['icno',$request->icno],['tac_no',$request->tac]])->get()->toArray();
// dd($tac);
if($tac){
// dd($request->icno);
$emel = User::where('icno','=', $request->icno)->first()->toArray();
return response(['status' => 1, 'useremel' => $emel]);
}else{
return response(['status' => 0]);
}
}
public function requestKadPengenalan(Request $request){
$currentDateTime = Carbon::now();
$expiretac = tacportal::where('icno',$request->icno)
->whereDate('created_at','=',$currentDateTime)
->whereDate('expired_at','=',$currentDateTime)
->whereTime('expired_at','>=',$currentDateTime->toTimeString())
->whereTime('created_at','<=',$currentDateTime->toTimeString())
->latest()
->first();
if($expiretac){
return response(['notexpired' => true]);
}
$tac = User::where('icno',$request->icno)->get()->toArray();
if($tac){
return response(['message' => true]);
}
$CustomerInfo = $this->getCustomerData($request->icno, false);
if($CustomerInfo)
{
$generatestatus = $this->generateTAC($CustomerInfo);
if($generatestatus){
$backphone = substr($CustomerInfo['telefon'],-4);
return response(['notelefon' => $backphone,'status' => 1]);
}else{
}
}else{
return response(['status' => 0],200);
}
}
public function requestKadPengenalanfe(Request $request){
$currentDateTime = Carbon::now();
$expiretac = tacportal::where('icno',$request->icno)
->whereDate('created_at','=',$currentDateTime)
->whereDate('expired_at','=',$currentDateTime)
->whereTime('expired_at','>=',$currentDateTime->toTimeString())
->whereTime('created_at','<=',$currentDateTime->toTimeString())
->latest()
->first();
if($expiretac){
return response(['notexpired' => true]);
}
$CustomerInfo = $this->getCustomerData($request->icno, false);
if($CustomerInfo)
{
$generatestatus = $this->generateTAC($CustomerInfo);
if($generatestatus){
$backphone = substr($CustomerInfo['telefon'],-4);
return response(['notelefon' => $backphone,'status' => 1]);
}else{
}
}else{
return response(['status' => 0],200);
}
}
private function generateTAC($customer){
$currentDateTime = Carbon::now();
$expired_at = Carbon::now()->addMinutes(5);
$tac_token = $this->randum_number(6,2,false);
$tac = new tacportal;
$tac->icno = ($customer['kpbaru']) ? $customer['kpbaru'] : $customer['kplama'];
$tac->notel = $customer['telefon'];
$tac->tac_no = $tac_token;
$tac->created_at = $currentDateTime->toDateTimeString();
$tac->expired_at = $expired_at;
$tac->save();
$mobileno = '6' . $customer['telefon'];
$messages = 'AR-RAHN PKB PORTAL : TAC: ' . $tac_token . ' ' . $currentDateTime . '. HUBUNGI AR-RAHN CARELINE CENTRE';
$status = Http::get('http://gateway.onewaysms.com.my:10001/api.aspx',[
'apiusername' => 'APIUK330HDAFO',
'apipassword' => 'APIUK330HDAFOUK330',
'senderid' => 'INFO',
'mobileno' => $mobileno,
'message'=> $messages,
'languagetype'=> 1
]);
return response($status);
//return response(1);
}
private function randum_number($len = 6,$dup = 1, $sort = false){
if($dup < 1)
throw new InvalidArgumentException('Second argument is < 1');
$num = range(0,9);
shuffle($num);
$num = array_slice($num, 0, ($len-$dup) + 1);
if($dup > 0){
$k = array_rand($num, 1);
for($i=0;$i<($dup-1);$i++)
{
$num[] = $num[$k];
}
}
if($sort){
sort($num);
}
return implode('',$num);
}
private function getCustomerData($icno,$details = false){
$CustomerInfo_PKB = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') .'/api/customers/' . $icno)->json();
$CustomerInfo_KOOPKB = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url_koop') .'/api/customers/' . $icno)->json();
if($CustomerInfo_KOOPKB){
if($details == true){
$poskod = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/poskod/' . $CustomerInfo_KOOPKB['poskod'])->json();
$daerah = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') .'/api/daerah/' . $poskod['koddaerah'])->json();
$negeri =Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') .'/api/negeri/' . $poskod['kodnegeri'])->json();
return ['customerinfo' => $CustomerInfo_KOOPKB,'poskod' => $poskod,'daerah' => $daerah,'negeri' => $negeri];
}
$CustomerInfo = $CustomerInfo_KOOPKB;
}else if($CustomerInfo_PKB){
if($details == true){
$poskod = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url_koop') . '/api/poskod/' . $CustomerInfo_PKB['poskod'])->json();
$daerah = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url_koop') .'/api/daerah/' . $poskod['koddaerah'])->json();
$negeri =Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url_koop') .'/api/negeri/' . $poskod['kodnegeri'])->json();
return ['customerinfo' => $CustomerInfo_PKB,'poskod' => $poskod,'daerah' => $daerah,'negeri' => $negeri];
}
$CustomerInfo = $CustomerInfo_PKB;
}else{
$CustomerInfo = null;
}
return $CustomerInfo;
}
}