middleware('auth'); $this->customerService = new CustomerService(); } /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { $search = false; $api_url = config('api.url'); $auth_user = Auth::user(); $idtype = 'nokp'; $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/' . $auth_user['cawangan'])->json(); $bank = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/bank')->json(); $pekerjaan = Http::withOptions(['verify' => config('app.api_verify')])->get($api_url . '/api/pekerjaan')->json(); $blacklist_search = false; return view('customers.' . $cawangan_info['version'] . '.index', compact('search', 'blacklist_search', 'cawangan_info', 'bank', 'api_url', 'idtype', 'pekerjaan')); } public function info(Request $request) { $auth_user = Auth::user(); $api_url = config('api.url'); // 🔹 Panggil maklumat cawangan $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')]) ->get($api_url . '/api/cawangan/' . $auth_user['cawangan']) ->json(); // 🔹 Panggil maklumat pelanggan (customer) $customer_info = Http::withOptions(['verify' => config('app.api_verify')]) ->get($api_url . '/api/customers/' . Session::get('noic')) ->json(); // 🔹 Panggil maklumat pekerjaan $pekerjaan = Http::withOptions(['verify' => config('app.api_verify')]) ->get($api_url . '/api/pekerjaan') ->json(); $komoditi = Komoditi::first(); $min_komoditi = $komoditi['min_komoditi']; // 🔹 Panggil baki lelong $bakilelong = Http::withOptions(['verify' => config('app.api_verify')]) ->get($api_url . '/api/bakilelong/cawangan/' . $auth_user['cawangan'] . '/' . Session::get('noic') . '/baki') ->json(); // 🔹 Panggil rugi lelong $rugilelong = Http::withOptions(['verify' => config('app.api_verify')]) ->get($api_url . '/api/bakilelong/cawangan/' . $auth_user['cawangan'] . '/' . Session::get('noic') . '/rugi') ->json(); // 🔹 Reset session sementara $request->session()->forget('page_reload'); $request->session()->forget('norujukan'); $customer_page = ($cawangan_info['version'] == 'v1') ? 'customers.v1.info' : 'customers.v2.info'; // Logik papar butang "Assign sebagai Anggota" $showAssignAnggotaBtn = false; $noic = Session::get('noic'); if (!empty($customer_info)) { // 🔹 Panggil API anggota $anggota_info = Http::withOptions(['verify' => config('app.api_verify')]) ->get($api_url . '/api/customer/anggota', [ 'noic' => $noic ]) ->json(); // 🔹 Semak jika ada data anggota dan belum di-tag sebagai anggota $isAnggotaExist = isset($anggota_info) && !empty($anggota_info) && !isset($anggota_info['message']); if ($isAnggotaExist && isset($customer_info['tag_anggota']) && $customer_info['tag_anggota'] == 0) { $showAssignAnggotaBtn = true; } } // Return ke view $getApprovalError = Http::withOptions(['verify' => config('app.api_verify')]) ->get($api_url . '/api/getApproval', [ 'kodcaw' => $auth_user['cawangan'], 'teller' => $auth_user['name'], ]) ->json(); if ($request->tebus_auto_tawarruq_berjaya === 'true') { $request->session()->flash('activate_tab', 'SAG_baru'); } return view($customer_page, compact( 'cawangan_info', 'api_url', 'customer_info', 'bakilelong', 'rugilelong', 'min_komoditi', 'pekerjaan', 'getApprovalError', 'showAssignAnggotaBtn' )); } public function search(Request $request) { $noic = $request->noic; $idtype = $request->customerIDType; Session::put('noic', $noic); $api_url = config('api.url'); $auth_user = Auth::user(); // Dapatkan maklumat asas $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')]) ->get($api_url . '/api/cawangan/' . $auth_user['cawangan']) ->json(); $bank = Http::withOptions(['verify' => config('app.api_verify')]) ->get($api_url . '/api/bank') ->json(); $customer_info = Http::withOptions(['verify' => config('app.api_verify')]) ->get($api_url . '/api/customers/' . $noic) ->json(); $pekerjaan = Http::withOptions(['verify' => config('app.api_verify')]) ->get($api_url . '/api/pekerjaan') ->json(); $blacklist = Http::withOptions(['verify' => config('app.api_verify')]) ->get($api_url . '/api/customer/blacklist', ['nokp' => $noic]) ->json(); // Reset session lama $request->session()->forget('page_reload'); $request->session()->forget('norujukan'); $blacklist_search = false; $search = true; // SEMAK STATUS ANGGOTA DAN PELANGGAN $isAnggota = false; $isPelanggan = false; $isNewCustomer = false; $showAssignAnggotaBtn = false; $showDaftarPelangganBtn = false; // Semak dalam sistem anggota $anggota_info = Http::withOptions(['verify' => config('app.api_verify')]) ->get(config('api.url') . '/api/customer/anggota', [ 'noic' => $noic ]) ->json(); // ✅ Semak betul-betul jika ada rekod anggota if (!empty($anggota_info) && isset($anggota_info['data']) && !empty($anggota_info['data'])) { $isAnggota = true; } elseif (isset($anggota_info['noic']) && !empty($anggota_info['noic'])) { $isAnggota = true; } if ($customer_info && isset($customer_info['id'])) { $isPelanggan = true; } // ✅ Tentukan butang mana nak paparkan (logik diperbetulkan) if ($isPelanggan && !$isAnggota) { // Pelanggan dah berdaftar tapi bukan anggota koperasi $showAssignAnggotaBtn = true; } elseif ($isAnggota && !$isPelanggan) { // Anggota koperasi tapi belum berdaftar sebagai pelanggan $showDaftarPelangganBtn = true; } elseif (!$isAnggota && !$isPelanggan) { // Bukan anggota dan bukan pelanggan (pelanggan baru) $showDaftarPelangganBtn = true; $isNewCustomer = true; } // 🔍 Debug log untuk semak nilai sebenar Log::info('DEBUG CUSTOMER STATUS', [ 'noic' => $noic, 'isAnggota' => $isAnggota, 'isPelanggan' => $isPelanggan, 'isNewCustomer' => $isNewCustomer, 'showAssignAnggotaBtn' => $showAssignAnggotaBtn, 'showDaftarPelangganBtn' => $showDaftarPelangganBtn, ]); // PROSES PAPARAN MENGIKUT HASIL CARIAN if ($customer_info) { // Ada maklumat pelanggan if ($blacklist) { $blacklist_search = true; return view('customers.' . $cawangan_info['version'] . '.index', compact( 'blacklist_search', 'customer_info', 'search', 'noic', 'cawangan_info', 'bank', 'api_url', 'idtype', 'isAnggota', 'isPelanggan', 'isNewCustomer', 'showAssignAnggotaBtn', 'showDaftarPelangganBtn', 'auth_user' )); } else { // Simpan dalam session & redirect ke paparan info pelanggan Session::put('customer_info', $customer_info); return redirect()->route('customer_info')->with([ 'customer_info' => $customer_info, 'cawangan_info' => $cawangan_info, 'api_url' => $api_url, 'isAnggota' => $isAnggota, 'isPelanggan' => $isPelanggan, 'isNewCustomer' => $isNewCustomer, 'showAssignAnggotaBtn' => $showAssignAnggotaBtn, 'showDaftarPelangganBtn' => $showDaftarPelangganBtn, 'auth_user' => $auth_user ]); } } else { // Tiada customer dalam sistem if ($blacklist) { $blacklist_search = true; } return view('customers.' . $cawangan_info['version'] . '.index', compact( 'blacklist_search', 'customer_info', 'search', 'noic', 'cawangan_info', 'bank', 'api_url', 'idtype', 'pekerjaan', 'isAnggota', 'isPelanggan', 'isNewCustomer', 'showAssignAnggotaBtn', 'showDaftarPelangganBtn', 'auth_user' )); } } //Cari By IC No public function cari(Request $request) { $api_url = config('api.url'); $customers_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/customers/' . $request->icno)->json(); if (sizeof($customers_info) > 0) return $customers_info; else return response(["no-data"], 200); } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create(Request $request) { // dd($request); } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { if ($request->idtype == 'nokp') { $nokpbaru = $request->noic; $nokplama = ''; } elseif ($request->idtype == 'passport') { $nokpbaru = ''; $nokplama = $request->noic; } if ($request->warganegara) { $warganegara = 'Y'; } else { $warganegara = 'N'; } if ($request->bumiputera) { $bumiputera = 'Y'; } else { $bumiputera = 'N'; } if ($request->jantina == 'Lelaki') { $jantina = 'L'; } elseif ($request->jantina == 'Perempuan') { $jantina = 'P'; } $split_date = explode('/', $request->tarikh_lahir); $tarikh_lahir = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0]; $tarikhlahir = Carbon::parse($tarikh_lahir); $auth_user = Auth::user(); $latest_customer = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/customers/latest/' . $auth_user['cawangan'])->json(); if ($latest_customer == null) { $nosiri_latest = 1; } else { $nosiri_latest = $latest_customer['nosiri'] + 1; } $count = sprintf("%08d", $nosiri_latest); $no_pelanggan = $request->bangsa . substr($request->noic, 6, 2) . '-' . $count; $detail_poskod = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/poskod/' . $request->poskod)->json(); if (empty($detail_poskod)) { $detail_poskod = Http::withOptions(['verify' => config('app.api_verify')]) ->post(config('api.url') . '/api/poskod', [ 'poskod' => $request->poskod, 'koddaerah' => $request->koddaerah, 'kodnegeri' => $request->kodnegeri, 'user' => auth()->user(), ])->json(); } $customer_info = Http::withOptions(['verify' => config('app.api_verify')])->post(config('api.url') . '/api/customers/create/' . $auth_user['cawangan'], [ 'kodcaw' => $auth_user['cawangan'], 'nopelanggan' => $no_pelanggan, 'nokpbaru' => $nokpbaru, 'nokplama' => $nokplama, 'nama' => $request->name, 't_lahir' => $tarikhlahir->toDateString(), 'jantina' => $jantina, 'alamat' => $request->alamat, 'poskod' => $request->poskod, 'koddaerah' => $detail_poskod['koddaerah'], 'kodnegeri' => $detail_poskod['kodnegeri'], 'telefon' => $request->main_phone, 'bumiputra' => $bumiputera, 'warganegara' => $warganegara, 'bangsa' => $request->bangsa, 'kodugama' => $request->agama, 'kodbank' => $request->bank_name, 'noakaun' => $request->no_akaun, 'teller' => $auth_user['name'], 'telefon2' => $request->alternate_phone, 'nosiri' => $nosiri_latest, 'nota' => $request->nota, 'kodkerja' => $request->pekerjaan, 'nota_pekerjaan' => $request->nota_pekerjaan, 'notaarcc' => $request->notaarcc, 'tujuangadai' => $request->tujuan_gadai, ]); Session::put('customer_info', $customer_info); Session::put('noic', $request->noic); event(new PelangganNotify('lulus', $auth_user['cawangan'])); return redirect()->route('customer_info')->with(['customer_info' => $customer_info]); } public function getOTP(Request $request) { $auth_user = Auth::user(); $expired = Carbon::now(); $expired = new Carbon(); $expired = $expired->addHours(24); $currentDateTime = Carbon::now(); $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/' . $auth_user['cawangan'])->json(); $nama_cawangan = $cawangan_info["details_caw"]; $nokp = $request->noic_otp; $customer = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/customers/' . $nokp)->json(); $recipient = '6' . $customer['telefon']; // $recipient = '60189025461'; //pembinaan token function token($length) { $characters = array( "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "1", "2", "3", "4", "5", "6", "7", "8", "9" ); if ($length < 0 || $length > count($characters)) return null; shuffle($characters); return implode("", array_slice($characters, 0, $length)); } $otp_token = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/otp/' . $nokp)->json(); $otp_token_used = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/otp/used/' . $nokp)->json(); if (is_array($otp_token) || is_object($otp_token)) { if ($otp_token) { return response(['error' => "OTP Akaun Telah Di Jana"]); } else { if (is_array($otp_token_used) || is_object($otp_token_used)) { if ($otp_token_used) { return response(['error' => "Pelanggan Sudah Mendaftar Pada Customer Portal"]); } else { $random = token(6); $otp_baru = Http::withOptions(['verify' => config('app.api_verify')])->post(config('api.url') . '/api/otp/generateotp/' . $nokp, [ 'otptoken' => $random, 'expired' => $expired, 'kodcaw' => $auth_user['cawangan'] ]); // $message = "Your OTP code is " . $random; $message = 'AR-RAHN PKB ' . $nama_cawangan . ' : OTP: ' . $random . ' ' . $currentDateTime . '. AR-RAHN CARELINE CENTRE 1300-22-4343'; $this->sendWhatsAppMessage($message, $recipient); return response()->json(['otp' => $random]); } } else { $random = token(6); $otp_baru = Http::withOptions(['verify' => config('app.api_verify')])->post(config('api.url') . '/api/otp/generateotp/' . $nokp, [ 'otptoken' => $random, 'expired' => $expired, 'kodcaw' => $auth_user['cawangan'] ]); // $message = "Your OTP code is " . $random; $message = 'AR-RAHN PKB ' . $nama_cawangan . ' : OTP: ' . $random . ' ' . $currentDateTime . '. AR-RAHN CARELINE CENTRE 1300-22-4343'; $this->sendWhatsAppMessage($message, $recipient); return response()->json(['otp' => $random]); } } } else { if (is_array($otp_token_used) || is_object($otp_token_used)) { if ($otp_token_used) { return response(['error' => "Pelanggan Sudah Mendaftar Pada Customer Portal"]); } else { $random = token(6); $otp_baru = Http::withOptions(['verify' => config('app.api_verify')])->post(config('api.url') . '/api/otp/generateotp/' . $nokp, [ 'otptoken' => $random, 'expired' => $expired, 'kodcaw' => $auth_user['cawangan'] ]); // $message = "Your OTP code is " . $random; $message = 'AR-RAHN PKB ' . $nama_cawangan . ' : OTP: ' . $random . ' ' . $currentDateTime . '. AR-RAHN CARELINE CENTRE 1300-22-4343'; $this->sendWhatsAppMessage($message, $recipient); return response()->json(['otp' => $random]); } } else { $random = token(6); $otp_baru = Http::withOptions(['verify' => config('app.api_verify')])->post(config('api.url') . '/api/otp/generateotp/' . $nokp, [ 'otptoken' => $random, 'expired' => $expired, 'kodcaw' => $auth_user['cawangan'] ]); // $message = "Your OTP code is " . $random; $message = 'AR-RAHN PKB ' . $nama_cawangan . ' : OTP: ' . $random . ' ' . $currentDateTime . '. AR-RAHN CARELINE CENTRE 1300-22-4343'; $this->sendWhatsAppMessage($message, $recipient); return response()->json(['otp' => $random]); } } } public function sendWhatsAppMessage(string $message, string $recipient) { $status = Http::withOptions(['verify' => config('app.api_verify')])->get('http://gateway.onewaysms.com.my:10001/api.aspx', [ 'apiusername' => 'APIUK330HDAFO', 'apipassword' => 'APIUK330HDAFOUK330', 'senderid' => 'INFO', 'mobileno' => $recipient, 'message' => $message, 'languagetype' => 1 ]); return ($status); } public function assignAnggota(Request $request) { $noic = $request->noic; $response = Http::withOptions(['verify' => config('app.api_verify')]) ->put(config('api.url') . '/api/customers/' . $noic . '/assign-anggota'); if ($response->successful()) { return redirect()->back()->with('success', 'Pelanggan berjaya ditandakan sebagai anggota koperasi.'); } else { return redirect()->back()->with('error', 'Gagal mengemaskini status anggota. Sila cuba semula.'); } } }