Files
erahn-click/app/Http/Controllers/TacController.php
T
2023-02-13 10:43:16 +08:00

130 lines
4.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){
$CustomerInfo = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') .'/api/customers/' . $request->icno)->json();
$poskod = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/poskod/' . $CustomerInfo['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 response(['status' => 1, 'customerinfo' => $CustomerInfo,'namadaerah' => $daerah['namadaerah'],'namanegeri' => $negeri['negeri']]);
}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 = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') .'/api/customers/' . $request->icno)->json();
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 . '. AR-RAHN CARELINE CENTRE 1300-22-4343';
// $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);
}
}