<?php

namespace App\Http\Controllers;

use App\Services\ImbangdugaServices as ImbanganDuga;

//Events
use App\Events\StatusLiked;
use App\Events\NotifikasiPembayaran;
use App\Events\OperasiNotice;
use App\Events\PendahuluanSerahan;
use App\Events\NotifikasiKelulusan;
use App\Events\NotifikasiUbahHapus;
use App\Events\NotifikasiAGH;
use App\Events\NotifikasiPetiBesi;
use App\Events\AutoSerahan;

use App\User;
use App\Cawangan;
use App\CawanganAR;
use App\Lejeram;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Hash;
use Carbon\Carbon;
use PDF;
use DateTime;
use App\Helper;
use Log;

class KhazanahController extends Controller
{


    public function __construct()
    {
        $this->middleware('auth');
    }

    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();
        if($auth_user->roles == 'admin'){
            return view('admin.customer_index',compact('search','cawangan_info','bank','api_url','idtype','auth_user'));
        }else{
            return view('khazanah.index',compact('search','cawangan_info','bank','api_url','idtype','auth_user'));
        }
    }

    public function search(Request $request){
        $noic = $request->noic;
        $idtype = $request->customerIDType;
        Session::put('noic', $noic);
        $api_url = config('api.url');
        $auth_user = Auth::user();
        $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();
        $customer_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/customers/'. $noic)->json();

        // if($customers_info){
        //     $customer_info = $customers_info[0];
        // }else{
        //     $customer_info = $customers_info;
        // }
        $request->session()->forget('page_reload');
        $request->session()->forget('norujukan');

        $search = true;

        if($customer_info){
            Session::put('customer_info_khazanah',$customer_info);
            if($auth_user->roles == 'admin'){
                return redirect()->route('admin.customer_info')->with( ['customer_info' => $customer_info,'cawangan_info'=>$cawangan_info,'api_url'=>$api_url]);
            }
                return redirect()->route('khazanah.customer_info')->with( ['customer_info' => $customer_info,'cawangan_info'=>$cawangan_info,'api_url'=>$api_url]);

            // return redirect()->back()->with('failed','Maaf,Tiada Data Pelanggan...');

        }else{
            if($auth_user->roles == 'admin'){
                return view('admin.customer_index',compact('search','noic','cawangan_info','bank','api_url','idtype'));
            }
                // return view('khazanah.index',compact('search','noic','cawangan_info','bank','api_url','idtype'));
            return redirect()->back()->with('failed','Maaf, hanya taraf teller sahaja yang boleh mendaftar');
        }
    }

    public function searchAdmin(Request $request){
        $noic = $request->noic;
        $idtype = $request->customerIDType;
        Session::put('noic', $noic);
        $api_url = config('api.url');
        $auth_user = Auth::user();
        $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();
        $customer_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/admin/customers/nokp/search',[
            'ic' =>  $noic
        ])->json();

        $request->session()->forget('page_reload');
        $request->session()->forget('norujukan');

        $search = true;

        if($customer_info){
            Session::put('customer_info_khazanah',$customer_info);
            return redirect()->route('admin.customer_info')->with( ['customer_info' => $customer_info,'cawangan_info'=>$cawangan_info,'api_url'=>$api_url]);
        }else{
            return view('admin.customer_index',compact('search','noic','cawangan_info','bank','api_url','idtype'));
        }
    }

    public function infoAdmin(Request $request){
        $auth_user = Auth::user();
        $api_url = config('api.url');
        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
        $customer_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/admin/customers/nokp/search',[
            'ic' =>  Session::get('noic')
        ])->json();

        $bakilelong = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/bakilelong/cawangan/' . $auth_user['cawangan'] . '/' . Session::get('noic') . '/baki')->json();
        $rugilelong = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/bakilelong/cawangan/' . $auth_user['cawangan'] . '/' . Session::get('noic') . '/rugi')->json();

        $negeri = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/negeri/')->json();



        $request->session()->forget('page_reload');
        $request->session()->forget('norujukan');

        return view('admin.customer_info',compact('cawangan_info','api_url','customer_info','bakilelong','rugilelong','negeri'));
    }

    public function info(Request $request){
        $auth_user = Auth::user();
        $api_url = config('api.url');
        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
        $customer_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/customers/'. Session::get('noic'))->json();
        // if($customers_info){
        //     $customer_info = $customers_info[0];
        // }else{
        //     $customer_info = $customers_info;
        // }

        $bakilelong = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/bakilelong/cawangan/' . $auth_user['cawangan'] . '/' . Session::get('noic') . '/baki')->json();
        $rugilelong = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/bakilelong/cawangan/' . $auth_user['cawangan'] . '/' . Session::get('noic') . '/rugi')->json();

        $negeri = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/negeri/')->json();



        $request->session()->forget('page_reload');
        $request->session()->forget('norujukan');

        if($auth_user->roles == 'admin'){
            return view('admin.customer_info',compact('cawangan_info','api_url','customer_info','bakilelong','rugilelong','negeri'));
        }else{
            return view('khazanah.' . $cawangan_info['version'] . '.info',compact('cawangan_info','api_url','customer_info','bakilelong','rugilelong','negeri'));
        }
    }

    public function menu()
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');
        $gadai = Http::withOptions(['verify' => config('app.api_verify')])->get( config('api.url') .'/api/gadai/serah_marhun/khazanah/'. $auth_user['cawangan'])->json();
        $listserah = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/listSerahMarhunController/'.$auth_user['cawangan'])->json();

        $array_reqteller = [];

        $reqTeller = Http::withOptions(['verify' => config('app.api_verify')])->get( config('api.url') .'/api/requestTeller/get/'. $auth_user['cawangan'])->json();

        // dd($reqTeller);
        $countmodal = 0;$countserahan = 0;
        foreach($reqTeller as $index=>$req)
        {
            if($req['modal'] != 0)
                $countmodal++;
            else if($req['serahan'] != 0)
                $countserahan++;
        }

        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
        return view('khazanah.menu',compact('cawangan_info','api_url','gadai','listserah','countmodal','countserahan'));
    }

    public function pendahuluanserahandaftar(Request $request)
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');

        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();

        // $listgldetail = Http::withOptions(['verify' => config('app.api_verify')])->get( config('api.url') .'/api/listgldetail/'. $auth_user['cawangan'])->json();
        $listgldetail = Http::withOptions(['verify' => config('app.api_verify')])->get( config('api.url') .'/api/listgldetail/'. $auth_user['cawangan'],['name' => $auth_user['name']])->json();
        

        $denominasi=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/DenominasiController/'.$auth_user['cawangan'])->json();
        // dd($denominasi);
        if(sizeof($denominasi) == 0)
        {
            $denominasi=Http::withOptions(['verify' => config('app.api_verify')])->post(config('api.url').'/api/DenominasiCreate/'.$auth_user['cawangan'],['name' => $auth_user['name']])->json();  
            
        }
        else
        {
            $denominasi=$denominasi[0];
        }

        $proses = Http::withOptions(['verify' => config('app.api_verify')])->get( config('api.url_koop') .'/api/autoserahan/checkapproval',[
            'pc_name' => $auth_user['name']
        ])->json();

        return view('khazanah.pendahuluanserahanmain',compact('cawangan_info','listgldetail','denominasi','proses'));
    }


    public function dashboard()
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');

        $harini = Carbon::now();
        $harini = new Carbon;

        //Hari Ini
        $ringkasanharian = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/gadai/laporan/ringkasanharian/'. $auth_user['cawangan'],['tarikh' => $harini->toDateString()])->json();
        $ringkasanharian = $ringkasanharian[0];

        $keseluruhanupahharian = $ringkasanharian['lelongupahsimpan'] + $ringkasanharian['tebus'][0]['upahsimpan'] + $ringkasanharian['advansur'][0]['upahsimpan'];

        //Upah Terkumpul
        $upahsimpan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/KeuntunganSebenar/'. $auth_user['cawangan'],['tarikh' => $harini->toDateString()])->json();
        $keuntungan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/UntungRugiCalculation/'. $auth_user['cawangan'],['tarikh' => $harini->toDateString()])->json();

        //Peti Besi Cawangan
        $getVault=Http::withOptions(['verify' => config('app.api_verify')])->get( config('api.url').'/api/VaultController/'.$auth_user['cawangan'])->json();

        if(sizeof($getVault) == 0)
        {
            $peti_besi_terkini=Http::withOptions(['verify' => config('app.api_verify')])->post(config('api.url').'/api/daftarVault/'.$auth_user['cawangan'])->json();
            $getVault = $peti_besi_terkini['bakiakhir'];

        }else{
            $getVault = $getVault[0]['bakiakhir'];
        }

        //Check Komuditi
        $komuditicheck = Http::withOptions(['verify' => config('app.api_verify')])->get( config('api.url').'/api/komuditi/check')->json();

        //Dashboardall
        $dashboard=Http::withOptions(['verify' => config('app.api_verify')])->get( config('api.url').'/api/Dashboard_all/'.$auth_user['cawangan'])->json();

        //Marhun Terkumpul
        $marhun = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/marhunterkumpul/laporan/'. $auth_user['cawangan'] .'/'. $harini->toDateString())->json();

        $totalmarhun = 0;

        foreach($marhun as $index=>$m)
        {
            $totalmarhun += $m['n_marhun'];
        }

        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
        return view('khazanah.dashboard',compact('komuditicheck','cawangan_info','api_url','dashboard','totalmarhun','ringkasanharian','getVault','keseluruhanupahharian','upahsimpan','keuntungan'));
    }

    public function changedatedashboard(Request $request)
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');

        //Hari Ini
        $ringkasanharian = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/gadai/laporan/ringkasanharian/'. $auth_user['cawangan'],['tarikh' => $request->tarikh])->json();
        $ringkasanharian = $ringkasanharian[0];

        $keseluruhanupahharian = $ringkasanharian['lelongupahsimpan'] + $ringkasanharian['tebus'][0]['upahsimpan'] + $ringkasanharian['advansur'][0]['upahsimpan'];

        //Upah Terkumpul
        $upahsimpan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/KeuntunganSebenar/'. $auth_user['cawangan'],['tarikh' => $request->tarikh])->json();
        $keuntungan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/UntungRugiCalculation/'. $auth_user['cawangan'],['tarikh' => $request->tarikh])->json();

        //Peti Besi Cawangan
        $getVault=Http::withOptions(['verify' => config('app.api_verify')])->get( config('api.url').'/api/VaultController/'.$auth_user['cawangan'])->json();

        if(sizeof($getVault) == 0)
        {
            $peti_besi_terkini=Http::withOptions(['verify' => config('app.api_verify')])->post(config('api.url').'/api/daftarVault/'.$auth_user['cawangan'])->json();
            $getVault = $peti_besi_terkini['bakiakhir'];

        }else{
            $getVault = $getVault[0]['bakiakhir'];
        }

        //Check Komuditi
        $komuditicheck = Http::withOptions(['verify' => config('app.api_verify')])->get( config('api.url').'/api/komuditi/checkdate',['tarikh' => $request->tarikh])->json();

        //Dashboardall
        $dashboard=Http::withOptions(['verify' => config('app.api_verify')])->get( config('api.url').'/api/Dashboard_all/'.$auth_user['cawangan'])->json();

        //Marhun Terkumpul
        $marhun = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/marhunterkumpul/laporan/'. $auth_user['cawangan'] .'/'. $request->tarikh)->json();

        $totalmarhun = 0;

        foreach($marhun as $index=>$m)
        {
            $totalmarhun += $m['n_marhun'];
        }

        dd($totalmarhun,$dashboard);
        //return view('khazanah.dashboard',compact('komuditicheck','cawangan_info','api_url','dashboard','totalmarhun','ringkasanharian','getVault','keseluruhanupahharian','upahsimpan','keuntungan'));
    }

    public function alirantunai()
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');

        $harini = Carbon::now();
        $harini = new Carbon;

        $arraytunaiharini = [];
        $arraytunai = [];
        $arraygadaitebus = [];
        $arraytuntutbaki = [];
        $arrayaddtunai = [];


        // $teller = Http::withOptions(['verify' => config('app.api_verify')])->get( config('api.url') .'/api/get/tellerwork/'. $auth_user['cawangan'])->json();

        $teller = User::select('name as kodlaluan')->where('cawangan',$auth_user['cawangan'])
        ->whereIn('roles',['teller','ppc','pc','khazanah'])
        ->get()->toArray();

        foreach($teller as $index1 => $ls)
        {
            $tunai = Http::withOptions(['verify' => config('app.api_verify')])->get( config('api.url') .'/api/GetCurrentTunai/'. $auth_user['cawangan'],['kodlaluan' => $ls['kodlaluan']])->json();
            $tunaipast = Http::withOptions(['verify' => config('app.api_verify')])->get( config('api.url') .'/api/getPastTunai/'. $auth_user['cawangan'],['kodlaluan' => $ls['kodlaluan']])->json();

            $gadai_tebus = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/gadaitebus/listharini/'.$auth_user['cawangan'].'/teller/',['tarikh' => $harini->toDateString(),'teller' => $ls['kodlaluan']])->json();
            $tuntutbaki = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/TuntutBaki/'.$auth_user['cawangan'].'/teller/',['teller' => $ls['kodlaluan']])->json();
            $addtunai = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/getAddTunai/'.$auth_user['cawangan'].'/kodlaluan/',['kodlaluan' => $ls['kodlaluan']])->json();

            array_push($arraygadaitebus,$gadai_tebus);
            array_push($arraytuntutbaki,$tuntutbaki);
            array_push($arrayaddtunai,$addtunai);
            array_push($arraytunaiharini,$tunai);
            array_push($arraytunai,$tunaipast);

        }

        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();

        $petibesi = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/VaultController/all/'. $auth_user['cawangan'])->json();

        return view('khazanah.alirantunai',compact('cawangan_info','api_url','arraytunaiharini','arraytunai','teller','petibesi','arraygadaitebus','arraytuntutbaki','arrayaddtunai'));
    }


    public function gadaian()
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');

        $gadai = Http::withOptions(['verify' => config('app.api_verify')])->get( config('api.url') .'/api/gadai/serah_marhun/khazanah/'. $auth_user['cawangan'])->json();
        $petibesi = Http::withOptions(['verify' => config('app.api_verify')])->get( config('api.url') .'/api/gadai/serah_marhun/khazanah/petibesi/'. $auth_user['cawangan'])->json();



        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
        return view('khazanah.gadaian',compact('cawangan_info','api_url','gadai','petibesi'));
    }

    //KHAZANAH FARIS EDITED

    public function tebus()
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');
        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
        $listserah = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/listSerahMarhunController/'.$auth_user['cawangan'])->json();
        $senaraitebus = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/SenaraiPenebusanKhazanah/'.$auth_user['cawangan'])->json();


        return view('khazanah.tebus',compact('cawangan_info','api_url','listserah','senaraitebus'));
    }


    public function barcodescannedtebus(Request $request)
    {
        $auth_user = Auth::user();

        $update=Http::withOptions(['verify' => config('app.api_verify')])->put(config('api.url').'/api/updatetagterima/'.$auth_user['cawangan'].'/'.$request->norujukan_barcode,['nama' => Auth::user()->name])->json();

        if($update == 'success')
        {
            return redirect()->route('khazanah.tebus');
        }else if($update == 'unavailable'){
            return redirect()->route('khazanah.tebus')->with(['type' => 'unavailable', 'message' => 'Norujukan Tidak Wujud!', 'norujukan' => $request->norujukan_barcode]);
        }else{
            return redirect()->route('khazanah.tebus')->with(['type' => 'scanned', 'message' => 'Norujukan Dah Discan!', 'norujukan' => $request->norujukan_barcode]);
        }

    }

    public function pendahuluanserah($id)
    {

        $array_reqteller = [];

        $auth_user = Auth::user();
        $api_url = config('api.url');

        $gadai = Http::withOptions(['verify' => config('app.api_verify')])->get( config('api.url') .'/api/gadai/serah_marhun/khazanah/'. $auth_user['cawangan'])->json();
        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();

        $reqTeller = Http::withOptions(['verify' => config('app.api_verify')])->get( config('api.url') .'/api/requestTeller/get/'. $auth_user['cawangan'])->json();


        foreach($reqTeller as $index=>$req)
        {
            if($id == $req['no'])
                array_push($array_reqteller,["teller" => $req]);
        }

        $array_reqteller = $array_reqteller[0]['teller'];

        $listUser=User::where('cawangan',$auth_user['cawangan'] )->get();
        $denominasi=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/DenominasiController/'.$auth_user['cawangan'])->json();

        if(sizeof($denominasi) == 0)
        {
            $denominasi=Http::withOptions(['verify' => config('app.api_verify')])->post(config('api.url').'/api/DenominasiCreate/'.$auth_user['cawangan'])->json();
        }
        else
        {
            $denominasi=$denominasi[0];
        }


        return view('khazanah.pendahuluanserah',compact('cawangan_info','api_url','listUser','denominasi','array_reqteller'));
    }



    public function khazanahserahan($id)
    {
      $array_reqteller = [];

      $auth_user = Auth::user();
      $api_url = config('api.url');

      $reqTeller = Http::withOptions(['verify' => config('app.api_verify')])->get( config('api.url') .'/api/requestTeller/get/'. $auth_user['cawangan'])->json();

      $gadai = Http::withOptions(['verify' => config('app.api_verify')])->get( config('api.url') .'/api/gadai/serah_marhun/khazanah/'. $auth_user['cawangan'])->json();
      $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();

      $listUser=User::where('cawangan',$auth_user['cawangan'] )->get();
      $denominasi=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/DenominasiController/'.$auth_user['cawangan'])->json();

      foreach($reqTeller as $index=>$req)
      {

          if($id == $req['no'])
              array_push($array_reqteller,["teller" => $req]);

      }

      $array_reqteller = $array_reqteller[0]['teller'];


      return view('khazanah.khazanahserahan',compact('cawangan_info','api_url','listUser','denominasi','array_reqteller'));

    }


    ///KELULUSAN///

    public function kelulusan()
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');
        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();

        //declare array baru
        $arraykelulusan = [];
        $arraykelulusanmohonid = [];

        $kelulusan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/SenaraiKelulusan/' . $auth_user['cawangan'])->json();
        $kelulusanmohonid = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/GetLulusAll/' . $auth_user['cawangan'])->json();

        return view('khazanah.kelulusan',compact('cawangan_info','api_url','kelulusan','kelulusanmohonid'));

    }

    public function mintakatalaluansemula(Request $request)
    {

        if(Hash::check($request->passsemula, Auth::user()->password))
        {
            return "True";
        }
        else
        {
            return "False";
        }

    }

    public function kelulusanrekodlepas()
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');
        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();

        //declare array baru
        $arraykelulusan = [];

        $kelulusan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/RekodDahLepas/' . $auth_user['cawangan'])->json();
        $kelulusanmohonid = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/GetLulusAll/' . $auth_user['cawangan'])->json();


        foreach($kelulusan as $index=>$k)
        {
            $komen = explode("|",$k['komen']);

            $results=array_search("",$komen,true);
            if($results !== false) {
                unset($komen[$results]);
            }

            array_push($arraykelulusan,['norujukan' => $k['norujukan'], 'pinjaman' => $k['jumpinj'], 'nilaimarhun' => ($komen[1] ?? $k['nilaimarhun'] ?? '-'), 'berat' => ($komen[2] ?? $k['berat'] ?? '-'),'margin' => ($komen[3] ?? $k['margin'] ?? '-'), 'komen' => ($komen[0] ?? $k['komen'] ?? '-'), 'status' => $k['status'], 'pelulus' => $k['khazanah'],]);
        }

        return view('khazanah.rekodtelahlulus',compact('cawangan_info','api_url','arraykelulusan','kelulusanmohonid'));

    }



    public function getkelulusan(Request $request)
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');

        $norujukan = $request->norujukan;

        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();

        $kelulusan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/ListLatestLulus/' . $auth_user['cawangan'] . '/' . $norujukan)->json();

        if(sizeof($kelulusan) > 0)
        {
            $kelulusan = $kelulusan[0]['masalulus'];
        }
        else
        {
            $kelulusan = '';
        }

        return response($kelulusan);
    }

    public function updatekelulusan(Request $request)
    {
        $auth_user = Auth::user();

        $api_url = config('api.url');

        $norujukan = $request->norujukan;
        $pelulus = $request->pelulus;

        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();

        $getlulus = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/GetLulus/'. $auth_user['cawangan'],['norujukan' => $norujukan])->json();

        if($request->khazanah != null)
        {
            if($request->batal != null)
            {

                $updatelulus = Http::withOptions(['verify' => config('app.api_verify')])->put(config('api.url') . '/api/UpdateKelulusan/kepohonan/' . $auth_user['cawangan'] . '/' . $norujukan,[
                    "pembatalan" => $request->batal,
                    "komen" => $request->komenubahhapus,
                    "khazanah" => $request->khazanah
                ])->json();

                if($updatelulus)
                {
                    event(new NotifikasiUbahHapus($norujukan,"batal",$request->komenubahhapus,$request->teller));
                }

            }else{
                $updatelulus = Http::withOptions(['verify' => config('app.api_verify')])->put(config('api.url') . '/api/UpdateKelulusan/kepohonan/' . $auth_user['cawangan'] . '/' . $norujukan,[
                    "khazanah" => $request->khazanah
                ])->json();

                if($updatelulus)
                {
                    event(new NotifikasiUbahHapus($norujukan,"lulus","tiada",$request->teller));
                }
            }
        }
        else
        {
            if($pelulus == 'khazanah')
            {
                if($request->pembatalan == null)
                {

                    $updatelulus = Http::withOptions(['verify' => config('app.api_verify')])->put(config('api.url') . '/api/UpdateKelulusan/' . $auth_user['cawangan'] . '/' . $norujukan,[
                        "pengesahan" => "khazanah",
                        "namakhazanah" => $request->namakhazanah,
                        'siri' => $request->siri
                    ])->json();


                    if($updatelulus)
                    {
                        event(new NotifikasiKelulusan($norujukan,"lulus",$request->siri,$request->komenkelulusan,$request->teller));
                    }

                }else{
                    $updatelulus = Http::withOptions(['verify' => config('app.api_verify')])->put(config('api.url') . '/api/UpdateKelulusan/' . $auth_user['cawangan'] . '/' . $norujukan,[
                        "pengesahan" => "khazanah",
                        'siri' => $request->siri,
                        "namakhazanah" => $request->namakhazanah,
                        "pembatalan" => "batal"
                    ])->json();


                    if($updatelulus)
                    {
                        event(new NotifikasiKelulusan($norujukan,"batal",$request->siri,$request->komenkelulusan,$request->teller));
                    }

                }
            }
            else
            {
                $updatelulus = Http::withOptions(['verify' => config('app.api_verify')])->put(config('api.url') . '/api/UpdateKelulusanKomen/' . $auth_user['cawangan'] . '/' . $norujukan,[
                    "komen" => $request->komen,
                    'siri' => $request->siri
                ])->json();

                if($request->pembatalan == null)
                {
                    $updatelulus = Http::withOptions(['verify' => config('app.api_verify')])->put(config('api.url') . '/api/UpdateKelulusan/' . $auth_user['cawangan'] . '/' . $norujukan,[
                        "pengesahan" => "operasi",
                        'siri' => $request->siri,
                        "namakhazanah" => $request->namakhazanah
                    ])->json();

                }else{

                    $updatelulus = Http::withOptions(['verify' => config('app.api_verify')])->put(config('api.url') . '/api/UpdateKelulusan/' . $auth_user['cawangan'] . '/' . $norujukan,[
                        "pengesahan" => "operasi",
                        'siri' => $request->siri,
                        "pembatalan" => "batal",
                        "namakhazanah" => $request->namakhazanah
                    ])->json();

                    event(new NotifikasiKelulusan($norujukan,"batal",$request->siri,$request->komenkelulusan,$request->teller));

                }

                event(new OperasiNotice($auth_user['cawangan'],$auth_user['name']));
            }
        }


        return response()->json($updatelulus,200);
    }

    public function createkelulusan(Request $request)
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');

        $current = Carbon::now();
        $current = new Carbon();

        $norujukan = $request->norujukan;


        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();

        $getsiri = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/ListLatestLulus/'. $auth_user['cawangan'] . '/' . $norujukan)->json();

        if(isset($getsiri['siri']))
        {
            $siri = $getsiri['siri'] + 1;
        }else{
            $siri = 1;
        }

        $createlulus = Http::withOptions(['verify' => config('app.api_verify')])->post(config('api.url') . '/api/CreateKelulusan/' . $auth_user['cawangan'] . '/' . $norujukan . '/roles/khazanah', [
            'komen' => $request->komen,
            'dategadai' => $current->toDateString(),
            'siri' => $siri,
            'bilgadai' => 1,
            'jumpinjam' => $request->jumpinjam,
            'jenistebus' => "G",
            'margin' => $request->margin,
            'berat' => $request->berat,
            'nilaimarhun' => $request->nilaimarhun,
            'status' => $request->status,
            'teller' => $request->teller,
            'pelulus' => "khazanah"
        ])->json();

        event(new StatusLiked($auth_user['cawangan'],$auth_user['name']));

        return response()->json($siri,201);
    }

    ///END KELULUSAN///


    public function createpanjar(Request $request)
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');
        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();

        if($request->jenismodal === 'perubatan')
        {
            $listUser=User::where('name','=',$request->tuntutan)->get();

            if(floatval($request->bayaran) > floatval($listUser[0]->perubatan))
            {
                return redirect('/panjar')->with('messageinformation',['status' => 'error','message' => 'Baki perubatan tidak mencukupi.']);
            }
        }

        $request->merge(['kodlaluan' => $auth_user['name']]);
        $data_request = $request->except('_token');

        $updatepanjar = Http::withOptions(['verify' => config('app.api_verify')])->post( config('api.url') .'/api/panjar/createpanjar/'. $auth_user['cawangan'],$data_request)->json();
        // $updatepanjar = Http::withOptions(['verify' => config('app.api_verify')])->post( config('api.url') .'/api/panjar/createpanjar/'. $auth_user['cawangan'],['name' => $auth_user['name']],$data_request)->json();
        // dd($updatepanjar);
        if($updatepanjar['result'] == 'success')
        {
            if($request->jenismodal === 'perubatan')
            {
                $listUser = User::where('name','=',$request->tuntutan)->first();
                $listUser = optional($listUser)->toArray(); 
                $totalsebenar = floatval($listUser['perubatan_asal']) - floatval($updatepanjar['totalperubatan']);
                User::where('cawangan','=',$auth_user['cawangan'])
                ->where('name','=',$request->tuntutan)
                ->update(array(
                    'perubatan' => $totalsebenar
                ));
            }

            return redirect('/panjar')->with('messageinformation',['message' => 'Panjar Berjaya Disimpan.','status' => 'success']);
        }else{

            return redirect('/panjar')->with('messageinformation',['message' => 'Panjar Mengalami Masalah Untuk Disimpan.','status' => 'error']);
        }

        return response($updateVault);
    }

    public function deletepanjar(Request $request)
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');
        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();

        $request->merge(['kodlaluan' => $auth_user['name']]);
        $data_request = $request->except('_token');

        $updatepanjar = Http::withOptions(['verify' => config('app.api_verify')])->post( config('api.url') .'/api/panjar/deletepanjar/'. $auth_user['cawangan'],$data_request)->json();

        if($updatepanjar['result'] == 'success')
        {
            if($updatepanjar['jenis'] === 'perubatan')
            {
                $listUser = User::where('name','=',$updatepanjar['tuntutoleh'])->first();
                $listUser = optional($listUser)->toArray();
                $totalsebenar = floatval($listUser['perubatan_asal']) - floatval($updatepanjar['totalperubatan']);
                User::where('cawangan','=',$auth_user['cawangan'])
                ->where('name','=',$updatepanjar['tuntutoleh'])
                ->update(array(
                    'perubatan' => $totalsebenar
                ));

            }
            return 'berjaya';
        }else{
            return 'gagal';
        }
    }

    public function pendahuluan()
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');
        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
        $tunai=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/TunaiController/'.$auth_user['cawangan'])->json();


        return view('khazanah.pendahuluan',compact('cawangan_info','api_url','auth_user','tunai'));

    }

    public function serahan()
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');
        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
        return view('khazanah.serahan',compact('cawangan_info','api_url'));

    }

    public function SenaraiPengguna()
    {
        $auth_user = Auth::user();
        $listUser=User::where('cawangan',$auth_user['cawangan'] )->get();
        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
        //dd($listUser);
        return view('khazanah.pendahuluan',compact('listUser','cawangan_info'));
    }

    public function barcodescanned(Request $request){

        $auth_user = Auth::user();
        $scan_barcode = Http::withOptions(['verify' => config('app.api_verify')])->put(config('api.url').'/api/gadai/serah_marhun/khazanah/'. $auth_user['cawangan'] .'/' . $request->norujukan_barcode)->json();

        event(new NotifikasiPembayaran('TellerkepadaKhazanah'));

        if($scan_barcode == 'success')
        {
            return redirect()->route('khazanah.gadaian');
        }else if($scan_barcode == 'unavailable'){
            return redirect()->route('khazanah.gadaian')->with(['type' => 'unavailable', 'message' => 'Norujukan Tidak Wujud!', 'norujukan' => $request->norujukan_barcode]);
        }else{
            return redirect()->route('khazanah.gadaian')->with(['type' => 'scanned', 'message' => 'Norujukan Dah Discan!', 'norujukan' => $request->norujukan_barcode]);
        }
    }

    public function LaporanSemakBarcode(){
        $auth_user=Auth::user();
        $marhun_serah_petibesi=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/gadai/serah_marhun/khazanah/petibesi/'.$auth_user['cawangan'])->json();
        $cawangan=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];
        // dd($cawangan_detail);
        return view('print.laporanscanbarcode', compact('marhun_serah_petibesi','cawangan_detail'));
    }

    public function LaporanSemakBarcodePDF(){

        $auth_user=Auth::user();
        $marhun_serah_petibesi=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/gadai/serah_marhun/khazanah/petibesi/'.$auth_user['cawangan'])->json();
        $cawangan=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];

        $pdf=PDF::loadView('khazanah.laporanscanbarcode',compact('marhun_serah_petibesi','cawangan_detail'))->setPaper('A4','landscape');
        $today_date=date('Y-m-d');
        $filename="serahan";
        return $pdf->stream($filename.$today_date.'.pdf');
    }

    public function laporan(){
        $auth_user = Auth::user();
        $api_url = config('api.url');
        $gadai = Http::withOptions(['verify' => config('app.api_verify')])->get( config('api.url') .'/api/gadai/serah_marhun/khazanah/'. $auth_user['cawangan'])->json();
        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
        return view('khazanah.laporan.index',compact('cawangan_info','api_url','gadai'));
    }

    public function denominasicalculationpendahuluan($request)
    {

        // dd(request()->all());

        $auth_user = Auth::user();

        $denominasilist = Http::withOptions(['verify' => config('app.api_verify')])->get( config('api.url') .'/api/DenominasiController/'. $auth_user['cawangan'])->json();

        // dd($denominasilist);

        //bundle initialize
        $rekodb1 = $denominasilist[0]['bd1']; $rekodb5 = $denominasilist[0]['bd5']; $rekodb10 = $denominasilist[0]['bd10']; $rekodb20 = $denominasilist[0]['bd20'];
        $rekodb50 = $denominasilist[0]['bd50']; $rekodb100 = $denominasilist[0]['bd100'];

        //wang Syilling Initialize
        $rekodsen1 = $denominasilist[0]['txtsen1']; $rekodsen5 = $denominasilist[0]['txtsen5']; $rekodsen10 = $denominasilist[0]['txtsen10'];
        $rekodsen20 = $denominasilist[0]['txtsen20']; $rekodsen50 = $denominasilist[0]['txtsen50'];

        //Wang Ringgit Initialize
        $rekodring1 = $denominasilist[0]['txtring1']; $rekodring5 = $denominasilist[0]['txtring5']; $rekodring10 = $denominasilist[0]['txtring10'];
        $rekodring20 = $denominasilist[0]['txtring20']; $rekodring50 = $denominasilist[0]['txtring50']; $rekodring100 = $denominasilist[0]['txtring100'];

        //Tolok Rekod
        $rekodb1 -= intval($request->ambilanb1);
        $rekodb5 -= intval($request->ambilanb5);
        $rekodb10 -= intval($request->ambilanb10);
        $rekodb20 -= intval($request->ambilanb20);
        $rekodb50 -= intval($request->ambilanb50);
        $rekodb100 -= intval($request->ambilanb100);

        $rekodsen1 -= intval($request->ambilansen1);
        $rekodsen5 -= intval($request->ambilansen5);
        $rekodsen10 -= intval($request->ambilansen10);
        $rekodsen20 -= intval($request->ambilansen20);
        $rekodsen50 -= intval($request->ambilansen50);

        $rekodring1 -= intval($request->ambilanring1);
        $rekodring5 -= intval($request->ambilanring5);
        $rekodring10 -= intval($request->ambilanring10);
        $rekodring20 -= intval($request->ambilanring20);
        $rekodring50 -= intval($request->ambilanring50);
        $rekodring100 -= intval($request->ambilanring100);


        // dd($rekodring1);
        // dd($rekodb1,$rekodb5,$rekodb10,$rekodb20,$rekodb50,$rekodb100,$rekodsen1,$rekodsen5,$rekodsen10,$rekodsen20,$rekodsen50,$rekodring1,$rekodring5,$rekodring10,$rekodring20,$rekodring50,$rekodring100);


          $denominasicreate = Http::withOptions(['verify' => config('app.api_verify')])->put(config('api.url').'/api/DenominasiUpdate/'.$auth_user['cawangan'],[

            'bd1' => $rekodb1,
            'bd5'=> $rekodb5,
            'bd10'=> $rekodb10,
            'bd20'=> $rekodb20,
            'bd50'=> $rekodb50,
            'bd100'=> $rekodb100,
            'txtring1'=> $rekodring1,
            'txtring5'=> $rekodring5,
            'txtsen1'=> $rekodsen1,
            'txtsen5'=> $rekodsen5,
            'txtsen10'=> $rekodsen10,
            'txtsen20'=> $rekodsen20,
            'txtsen50'=> $rekodsen50,
            'txtring10'=> $rekodring10,
            'txtring20'=> $rekodring20,
            'txtring50'=> $rekodring50,
            'txtring100'=> $rekodring100,
            'txtring1000'=> "0",
            'jumlahbundle'=> "0",
            'txtjumlah'=> "0",
            'jumlahkeping' => "0"

          ]);

          event(new PendahuluanSerahan("Lulus","pendahuluan",$auth_user['cawangan'],$request->teller));

          $updaterequest=Http::withOptions(['verify' => config('app.api_verify')])->put(config('api.url').'/api/requestTeller/update/'. $auth_user['cawangan'],[
            'kodlaluan' => $request->teller,
            'no' => $request->no,
            'jenis' =>'pendahuluan',
            'khazanah' => Auth::user()->name

        ]);

        return redirect()->route('khazanah.pendahulu');

    }

    public function denominasicalculationserahan($request)
    {


        $auth_user = Auth::user();

        $denominasilist = Http::withOptions(['verify' => config('app.api_verify')])->get( config('api.url') .'/api/DenominasiController/'. $auth_user['cawangan'])->json();

        //bundle initialize
        $rekodb1 = $denominasilist[0]['bd1']; $rekodb5 = $denominasilist[0]['bd5']; $rekodb10 = $denominasilist[0]['bd10']; $rekodb20 = $denominasilist[0]['bd20'];
        $rekodb50 = $denominasilist[0]['bd50']; $rekodb100 = $denominasilist[0]['bd100'];

        //wang Syilling Initialize
        $rekodsen1 = $denominasilist[0]['txtsen1']; $rekodsen5 = $denominasilist[0]['txtsen5']; $rekodsen10 = $denominasilist[0]['txtsen10'];
        $rekodsen20 = $denominasilist[0]['txtsen20']; $rekodsen50 = $denominasilist[0]['txtsen50'];

        //Wang Ringgit Initialize
        $rekodring1 = $denominasilist[0]['txtring1']; $rekodring5 = $denominasilist[0]['txtring5']; $rekodring10 = $denominasilist[0]['txtring10'];
        $rekodring20 = $denominasilist[0]['txtring20']; $rekodring50 = $denominasilist[0]['txtring50']; $rekodring100 = $denominasilist[0]['txtring100'];

        //Tolok Rekod
        $rekodb1 += intval($request->ambilanb1);
        $rekodb5 += intval($request->ambilanb5);
        $rekodb10 += intval($request->ambilanb10);
        $rekodb20 += intval($request->ambilanb20);
        $rekodb50 += intval($request->ambilanb50);
        $rekodb100 += intval($request->ambilanb100);

        $rekodsen1 += intval($request->ambilansen1);
        $rekodsen5 += intval($request->ambilansen5);
        $rekodsen10 += intval($request->ambilansen10);
        $rekodsen20 += intval($request->ambilansen20);
        $rekodsen50 += intval($request->ambilansen50);

        $rekodring1 += intval($request->ambilanring1);
        $rekodring5 += intval($request->ambilanring5);
        $rekodring10 += intval($request->ambilanring10);
        $rekodring20 += intval($request->ambilanring20);
        $rekodring50 += intval($request->ambilanring50);
        $rekodring100 += intval($request->ambilanring100);


          $denominasicreate = Http::withOptions(['verify' => config('app.api_verify')])->put(config('api.url').'/api/DenominasiUpdate/'.$auth_user['cawangan'],[

            'bd1' => $rekodb1,
            'bd5'=> $rekodb5,
            'bd10'=> $rekodb10,
            'bd20'=> $rekodb20,
            'bd50'=> $rekodb50,
            'bd100'=> $rekodb100,
            'txtring1'=> $rekodring1,
            'txtring5'=> $rekodring5,
            'txtsen1'=> $rekodsen1,
            'txtsen5'=> $rekodsen5,
            'txtsen10'=> $rekodsen10,
            'txtsen20'=> $rekodsen20,
            'txtsen50'=> $rekodsen50,
            'txtring10'=> $rekodring10,
            'txtring20'=> $rekodring20,
            'txtring50'=> $rekodring50,
            'txtring100'=> $rekodring100,
            'txtring1000'=> "0",
            'jumlahbundle'=> "0",
            'txtjumlah'=> "0",
            'jumlahkeping' => "0"

          ]);

          event(new PendahuluanSerahan("Lulus","serahan",$auth_user['cawangan'],$request->teller));

          $updaterequest=Http::withOptions(['verify' => config('app.api_verify')])->put(config('api.url').'/api/requestTeller/update/'. $auth_user['cawangan'],[
            'kodlaluan' => $request->teller,
            'no' => $request->no,
            'jenis' => 'serahan',
            'khazanah' => Auth::user()->name
        ]);

        return redirect()->route('khazanah.serahan');
    }

    public function kemaskiniPendahuluan(Request $request)
    {

        $auth_user = Auth::user();
        $api_url = config('api.url');
        $peti_besi_terkini=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/VaultController/'.$auth_user['cawangan'])->json();

        if(sizeof($peti_besi_terkini) == 0)
        {
            $peti_besi_terkini=Http::withOptions(['verify' => config('app.api_verify')])->post(config('api.url').'/api/daftarVault/'.$auth_user['cawangan'])->json();
            $baki_awal = $peti_besi_terkini["bakiakhir"];
        }
        else
        {
        $baki_awal = $peti_besi_terkini[0]["bakiakhir"];
        }

        $amount = $request->amt;

        if($amount > $baki_awal){
            return redirect()->route('khazanah.pendahulu')->with('message', 'Jumlah yang dipinta melebihi baki dalam peti besi!.');
        }
        else{
            $response_data = $request->except('_token');
            $test = Http::withOptions(['verify' => config('app.api_verify')])->post(config('api.url').'/api/panjar/updatetunai/'.$auth_user['cawangan'],$response_data)->json();
            
            if($test == 'success'){
                event(new PendahuluanSerahan("Lulus","pendahuluan",$auth_user['cawangan'],$request->teller));
                return redirect()->route('khazanah.pendahulu');
            }else{
                return redirect()->route('khazanah.pendahulu')->with('error','Permohonan tidak berjaya! Permohonan Semula perlu melebihi 1 minit.');
            }
        }

    }

    public function KemaskiniSerahan(Request $request)
    {

      $auth_user = Auth::user();
      $api_url = config('api.url');

      $peti_besi_terkini=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/VaultController/'.$auth_user['cawangan'])->json();

      if(sizeof($peti_besi_terkini) == 0)
      {
          $peti_besi_terkini=Http::withOptions(['verify' => config('app.api_verify')])->post(config('api.url').'/api/daftarVault/'.$auth_user['cawangan'])->json();
      }
      $response_data = $request->except('_token');
      $test = Http::withOptions(['verify' => config('app.api_verify')])->post(config('api.url').'/api/panjar/serahantunai/'.$auth_user['cawangan'],$response_data)->json();
      
      if($test == 'success'){
          event(new PendahuluanSerahan("Lulus","serahan",$auth_user['cawangan'],$request->teller));
          return redirect()->route('khazanah.serahan');
      }else{
          return redirect()->route('khazanah.serahan')->with('error','Permohonan tidak berjaya! Permohonan Semula perlu melebihi 1 minit.');
      }
      
    }



    public function laporanGadaian(){
        $auth_user = Auth::user();
        $api_url = config('api.url');
        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
        return view('khazanah.laporan.gadaian',compact('cawangan_info','api_url'));
    }

    public function cetakLaporanGadai(Request $request){
        // dd($request);
        $api_url = config('api.url');
        $auth_user = Auth::user();
        $current = Carbon::now();
        $current = new Carbon();
        $tarikh_cetak = $current->toDateString();
        if($request->datetarikh == null){
            $gadai_date = $current->toDateString();
        }else{
            $split_date = explode('-',$request->datetarikh);
            $gadai_date = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
        }
        $gadai = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/gadai/laporan/'. $auth_user['cawangan'] .'/'. $gadai_date)->json();
        $cawangan=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];

        return view('print.laporan_gadaian', compact('gadai','cawangan_detail','tarikh_cetak','gadai_date','api_url'));
    }

    public function laporanPenebusan(){
        $auth_user = Auth::user();
        $api_url = config('api.url');
        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
        return view('khazanah.laporan.penebusan',compact('cawangan_info','api_url'));
    }

    public function cetakLaporanPenebusan(Request $request){
        // dd($request);
        $api_url = config('api.url');
        $auth_user = Auth::user();
        $current = Carbon::now();
        $current = new Carbon();
        $tarikh_cetak = $current->toDateString();
        if($request->datetarikh == null){
            $tebus_date = $current->toDateString();
        }else{
            $split_date = explode('-',$request->datetarikh);
            $tebus_date = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
        }
        $tebus = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/tebus/laporan/'. $auth_user['cawangan'] .'/'. $tebus_date)->json();
        $tebuspengasingan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/tebus/laporanjenistawarruq/'. $auth_user['cawangan'],['tarikh' => $tebus_date])->json();
        $upahlama = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/advansurdetails/'. $auth_user['cawangan'],['tarikh' => $tebus_date])->json();
        $ansuran = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/ansuransummary/'. $auth_user['cawangan'],['tarikh' => $tebus_date])->json();

        $cawangan=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];
        // dd($gadai);
        return view('print.laporan_penebusan', compact('tebus','cawangan_detail','tarikh_cetak','tebus_date','tebuspengasingan','ansuran','api_url','upahlama'));
    }

    public function laporanLelongan(){
        $auth_user = Auth::user();
        $api_url = config('api.url');
        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
        $senaraibatch = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/senaraibatch/aktif/'. $auth_user['cawangan'])->json();

        return view('lelongan.lelongan',compact('cawangan_info','api_url','senaraibatch'));
    }

    public function cetaklaporanLelongan(Request $request){
        // dd($request->all());
        $api_url = config('api.url');
        $auth_user = Auth::user();
        $current = Carbon::now();
        $current = new Carbon();
        $tarikh_cetak = $current->toDateString();

        if($request->jeniscetakan === 'cetak')
        {
            if($request->fasa == 1)
            {
                $lelong = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/senaraibatch/laporan/'. $auth_user['cawangan'] . '/' . 'fasa1',['batchno' => $request->batchno])->json();
            }else{
                $lelong = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/senaraibatch/laporan/'. $auth_user['cawangan'] . '/' . 'fasa2',['batchno' => $request->batchno])->json();
            }

            // $lelong = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/senaraibatch/laporan/'. $auth_user['cawangan'],['batchno' => $request->batchno])->json();

            $cawangan=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
            $cawangan_detail=$cawangan[0];

            return view('print.laporan_lelongan', compact('lelong','cawangan_detail','api_url','tarikh_cetak'));

        }else if($request->jeniscetakan === 'excel')
        {

            if($request->fasa == 1)
            {
                $lelong = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/senaraibatch/laporan/'. $auth_user['cawangan'] . '/' . 'fasa1',['batchno' => $request->batchno])->json();
            }else{
                $lelong = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/senaraibatch/laporan/'. $auth_user['cawangan'] . '/' . 'fasa2',['batchno' => $request->batchno])->json();
            }
            $cawangan=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
            $cawangan_detail=$cawangan[0];

            return view('print.laporan_lelongan_excel', compact('lelong','cawangan_detail','api_url','tarikh_cetak'));

        }else if($request->jeniscetakan === 'pdf'){

            if($request->fasa == 1)
            {
                $lelong = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/senaraibatch/laporan/'. $auth_user['cawangan'] . '/' . 'fasa1',['batchno' => $request->batchno])->json();
                $fasa = 'fasa1';
            }else{
                $lelong = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/senaraibatch/laporan/'. $auth_user['cawangan'] . '/' . 'fasa2',['batchno' => $request->batchno])->json();
                $fasa = 'fasa2';
            }
            $notislelong = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/details/notislelong/'. $auth_user['cawangan'])->json();
            $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
            $cawangan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
            $cawangan_detail=$cawangan[0];

            // return view('print.notislelong', compact('cawangan_info','notislelong','api_url','cawangan_detail','lelong','tarikh_cetak'));

            $pdf=PDF::loadView('print.notislelong', compact('cawangan_info','notislelong','api_url','cawangan_detail','lelong','fasa','tarikh_cetak'))->setPaper('A4','potrait');
            return $pdf->download('notislelong_'.$tarikh_cetak.'_'.$fasa.'.pdf');

            // return $pdf->stream('notislelong_'.$tarikh_cetak.'.pdf');
            // return view('print.notislelong', compact('cawangan_info','api_url','cawangan_detail','lelong','tarikh_cetak'));
        }

    }

    public function laporanAnsuran(){
        $auth_user = Auth::user();
        $api_url = config('api.url');

        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
        return view('khazanah.laporan.ansuran',compact('cawangan_info','api_url'));
    }

    public function cetakLaporanAnsuran(Request $request){
        // dd($request);
        $api_url = config('api.url');
        $auth_user = Auth::user();
        $current = Carbon::now();
        $current = new Carbon();
        $tarikh_cetak = $current->toDateString();
        if($request->datetimepicker1 == null){
            $tebus_date = $current->toDateString();
        }else{
            $split_date = explode('-',$request->datetimepicker1);
            $tebus_date = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
        }
        $tebus = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/ansuran/laporan/'. $auth_user['cawangan'],['tarikh' => $tebus_date])->json();
        $cawangan=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];
        // dd($gadai);
        return view('print.laporan_ansuran', compact('tebus','cawangan_detail','tarikh_cetak','tebus_date','api_url'));
    }

    public function laporanPelangganBaru(){
        $auth_user = Auth::user();
        $api_url = config('api.url');
        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
        return view('khazanah.laporan.pelangganbaru',compact('cawangan_info','api_url'));
    }

    public function cetakLaporanPelangganBaru(Request $request){
        // dd($request);
        $api_url = config('api.url');
        $auth_user = Auth::user();
        $current = Carbon::now();
        $current = new Carbon();
        $tarikh_cetak = $current->toDateString();
        if($request->datetimepicker1 == null){
            $tebus_date = $current->toDateString();
        }else{
            $split_date = explode('-',$request->datetimepicker1);
            $tebus_date = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
        }
        $senaraipelanggan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/customers/laporan/'. $auth_user['cawangan'] .'/'. $tebus_date)->json();
        $cawangan=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];
        // dd($gadai);
        return view('print.laporan_pelangganbaru', compact('senaraipelanggan','cawangan_detail','tarikh_cetak','tebus_date','api_url'));
    }



    public function cetakStok_AuditBersih(Request $request)
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');
        $tarikh_aging=$request;
       // dd($tarikh_aging);
       $num=$request->num;
       $current = Carbon::now();
       $current = new Carbon();
       $tarikh_cetak=$current->toDateString();


       if($request->datetimepicker1 == null){
        $tebus_date = $current->toDateString();
       }else
       {
        $split_date = explode('-',$request->datetimepicker1);
        $tarikh_stok = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
       }


       $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();

       $cawangan=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
       $cawangan_detail=$cawangan[0];

      // $data_3=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/gadai/aging/test/'.$auth_user['cawangan'].'/'.$tarikh_aging.'/'.$num3)->json();
       $data=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/gadai/stokauditbersih/'.$auth_user['cawangan'].'/'.$tarikh_stok)->json();
      //dd($data);



       return view('print.laporan_stok_audit_bersih', compact('cawangan','cawangan_detail','api_url','data','tarikh_cetak','tarikh_stok'));

    }








    public  function stokAuditBersih()
    {


        $auth_user = Auth::user();
        $api_url = config('api.url');
        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
        return view('khazanah.laporan.stokauditbersih',compact('cawangan_info','api_url'));



    }



    public function stokAuditDetails()
    {


      $auth_user = Auth::user();
      $api_url = config('api.url');
      $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();

          return view('khazanah.laporan.stokauditdetails',compact('cawangan_info','api_url'));


    }




    public function cetak_stokAuditDetails(Request $request)
    {

        $auth_user = Auth::user();
        $api_url = config('api.url');
        $tarikh_aging=$request;
       // dd($tarikh_aging);
       $num=$request->num;
       $current = Carbon::now();
       $current = new Carbon();
       $tarikh_cetak=$current->toDateString();


       if($request->datetimepicker1 == null){
        $tebus_date = $current->toDateString();
       }else
       {
        $split_date = explode('-',$request->datetimepicker1);
        $tarikh_stok = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
       }


       $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();

       $cawangan=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
       $cawangan_detail=$cawangan[0];
      // $data_3=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/gadai/aging/test/'.$auth_user['cawangan'].'/'.$tarikh_aging.'/'.$num3)->json();
       $data=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/gadai/stokauditbersih/'.$auth_user['cawangan'].'/'.$tarikh_stok)->json();
      // dd($data);
       return view('print.laporan_stok_audit_details', compact('cawangan','cawangan_detail','api_url','data','tarikh_cetak','tarikh_stok'));

    }

    public function laporanStokAudit(){
        $auth_user = Auth::user();
        $api_url = config('api.url');


        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
        $stokaudit = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/gadai/cawangan/'. $auth_user['cawangan'])->json();

        return view('khazanah.laporan.stokaudit',compact('cawangan_info','api_url'));
    }

    public function laporanStokAuditBackdate(){
        $auth_user = Auth::user();
        $api_url = config('api.url');


        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
        $stokaudit = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/gadai/cawangan/'. $auth_user['cawangan'])->json();

        return view('khazanah.laporan.stokauditbackdate',compact('cawangan_info','api_url'));
    }

    public function cetakLaporanStokAudit(Request $request){

        $api_url = config('api.url');
        $auth_user = Auth::user();
        $current = Carbon::now();
        $current = new Carbon();
        $tarikh_cetak = $current->toDateString();
        if($request->datetimepicker1 == null){
            $tebus_date = $current->toDateString();
        }else{
            $split_date = explode('-',$request->datetimepicker1);
            $tebus_date = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
        }

        $cawangan=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];

        if($request->radaktif === 'aktif')
        {
            $stokaudit = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/stokaudit/aktif/'. $auth_user['cawangan'],['tarikh' => $tebus_date])->json();
            return view('print.laporan_stokaudit', compact('stokaudit','cawangan_detail','tarikh_cetak','tebus_date','api_url'));
        }else{
            $stokaudit = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/stokaudit/takaktif/'. $auth_user['cawangan'],['tarikh' => $tebus_date])->json();
            return view('print.laporan_stokauditbelumaktif', compact('stokaudit','cawangan_detail','tarikh_cetak','tebus_date','api_url'));
        }


        // dd($gadai);
    }

    public function cetakLaporanStokAuditBackdate(Request $request){

        $api_url = config('api.url');
        $auth_user = Auth::user();
        $current = Carbon::now();
        $current = new Carbon();
        $tarikh_cetak = $current->toDateString();

        $split_date = explode('-',$request->datetimepicker1);
        $bulan = $split_date[0];
        $tahun = $split_date[1];

        if($bulan == 1){
            $namabulan = 'Januari';
        }elseif($bulan == 2){
            $namabulan = 'Februari';
        }elseif($bulan == 3){
            $namabulan = 'Mac';
        }elseif($bulan == 4){
            $namabulan = 'April';
        }elseif($bulan == 5){
            $namabulan = 'Mei';
        }elseif($bulan == 6){
            $namabulan = 'Jun';
        }elseif($bulan == 7){
            $namabulan = 'Julai';
        }elseif($bulan == 8){
            $namabulan = 'Ogos';
        }elseif($bulan == 9){
            $namabulan = 'September';
        }elseif($bulan == 10){
            $namabulan = 'Oktober';
        }elseif($bulan == 11){
            $namabulan = 'November';
        }elseif($bulan == 12){
            $namabulan = 'Disember';
        }

        $tebus_date = $namabulan . ' ' . $tahun;

        $cawangan=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];

        $stokaudit = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/stokaudit/backdate',['kodcaw' => $auth_user['cawangan'],'tahun'=>$tahun,'bulan'=>$bulan])->json();

        return view('print.laporan_stokauditbackdate', compact('stokaudit','cawangan_detail','tarikh_cetak','tebus_date','api_url'));

    }

    public function laporanBatalSuratGadai(){
        $auth_user = Auth::user();
        $api_url = config('api.url');


        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();

        return view('khazanah.laporan.batalsuratgadai',compact('cawangan_info','api_url'));
    }

    public function cetakLaporanBatalSuratGadai(Request $request){
        // dd($request);
        $api_url = config('api.url');
        $auth_user = Auth::user();
        $current = Carbon::now();
        $current = new Carbon();
        $tarikh_cetak = $current->toDateString();
        if($request->datetimepicker1 == null){
            $tebus_date = $current->toDateString();
        }else{
            $split_date = explode('-',$request->datetimepicker1);
            $tebus_date = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
        }
        $batalgadai = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/batalgadai/laporan/'. $auth_user['cawangan'] .'/'. $tebus_date)->json();
        $cawangan=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];
        // dd($gadai);
        return view('print.laporan_batalsuratgadai', compact('batalgadai','cawangan_detail','tarikh_cetak','tebus_date','api_url'));
    }

    public function laporanPetiBesi(){
        $auth_user = Auth::user();
        $api_url = config('api.url');


        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();

        return view('khazanah.laporan.petibesi',compact('cawangan_info','api_url'));
    }

    public function cetakLaporanPetiBesi(Request $request){
        // dd($request);
        $api_url = config('api.url');
        $auth_user = Auth::user();
        $current = Carbon::now();
        $current = new Carbon();
        $tarikh_cetak = $current->toDateString();
        if($request->datetimepicker1 == null){
            $tebus_date = $current->toDateString();
        }else{
            $split_date = explode('-',$request->datetimepicker1);
            $tebus_date = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
        }
        $petibesi = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/laporanpetibesi/'. $auth_user['cawangan'] .'/'. $tebus_date)->json();

        if(!$petibesi)
        {
            return redirect()->back()->with('errormessage', 'Tiada data pada tarikh tersebut.');
        }


        $cawangan=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];

        return view('print.laporan_petibesi', compact('petibesi','cawangan_detail','tarikh_cetak','tebus_date','api_url'));
    }


    public function laporanMarhunHarian(){
        $auth_user = Auth::user();
        $api_url = config('api.url');


        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();

        return view('khazanah.laporan.marhunharian',compact('cawangan_info','api_url'));
    }

    public function cetakLaporanMarhunHarian(Request $request){
        // dd($request);
        $api_url = config('api.url');
        $auth_user = Auth::user();
        $current = Carbon::now();
        $current = new Carbon();
        $tarikh_cetak = $current->toDateString();
        if($request->datetimepicker1 == null){
            $tebus_date = $current->toDateString();
        }else{
            $split_date = explode('-',$request->datetimepicker1);
            $tebus_date = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
        }
        $marhun = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/marhun/laporan/'. $auth_user['cawangan'] .'/'. $tebus_date)->json();
        $cawangan=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];
        // dd($gadai);
        return view('print.laporan_marhunharian', compact('marhun','cawangan_detail','tarikh_cetak','tebus_date','api_url'));
    }

    public function laporanMarhunTerkumpul(){
        $auth_user = Auth::user();
        $api_url = config('api.url');


        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();

        return view('khazanah.laporan.marhunterkumpul',compact('cawangan_info','api_url'));
    }

    public function cetakLaporanMarhunTerkumpul(Request $request){
        // dd($request);
        $api_url = config('api.url');
        $auth_user = Auth::user();
        $current = Carbon::now();
        $current = new Carbon();
        $tarikh_cetak = $current->toDateString();
        if($request->datetimepicker1 == null){
            $tebus_date = $current->toDateString();
        }else{
            $split_date = explode('-',$request->datetimepicker1);
            $tebus_date = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
        }
        $marhun = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/marhunterkumpul/laporan/'. $auth_user['cawangan'] .'/'. $tebus_date)->json();
        $cawangan=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];
        // dd($gadai);
        return view('print.laporan_marhunterkumpul', compact('marhun','cawangan_detail','tarikh_cetak','tebus_date','api_url'));
    }

    public function laporanRingkasanTerkumpul(){
        $auth_user = Auth::user();
        $api_url = config('api.url');

        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();

        return view('khazanah.laporan.ringkasanterkumpul',compact('cawangan_info','api_url'));
    }

    public function laporanRingkasanHarian(){
        $auth_user = Auth::user();
        $api_url = config('api.url');


        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();

        return view('khazanah.laporan.ringkasanharian',compact('cawangan_info','api_url'));
    }

    public function cetakLaporanRingkasanHarian(Request $request){

        $api_url = config('api.url');
        $auth_user = Auth::user();
        $current = Carbon::now();
        $current = new Carbon();
        $tarikh_cetak = $current->toDateString();

        if($request->dateringkasanharian == null){
            $ringkasan_date = $current->toDateString();
        }else{
            $split_date = explode('-',$request->dateringkasanharian);
            $ringkasan_date = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
        }

        $cawangan=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];

        $laporan_ringkasan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/laporan/harian',[
            'kodcaw'=>$auth_user['cawangan'],
            'tarikh'=>$ringkasan_date
        ])->json();

        $laporan = $laporan_ringkasan[0];

        $kawalanstok = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/kawalanstok/laporan/'. $auth_user['cawangan'] .'/'. $ringkasan_date)->json();

        /* Kawalan Stok */
        //Gadai
        $totalbilgadaikeseluruhan = $kawalanstok[0]['gadai1'][0]['bil'] + $kawalanstok[0]['gadai2'][0]['bil'] + $kawalanstok[0]['gadai3'][0]['bil'];
        $totalbilgadaisehari = $kawalanstok[0]['gadaiini1'][0]['bil'] + $kawalanstok[0]['gadaiini2'][0]['bil'] + $kawalanstok[0]['gadaiini3'][0]['bil'];
        $totalbilgadai = $totalbilgadaikeseluruhan + $totalbilgadaisehari;

        $totalpinjgadaikeseluruhan = $kawalanstok[0]['gadai1'][0]['pinj'] + $kawalanstok[0]['gadai2'][0]['pinj'] + $kawalanstok[0]['gadai3'][0]['pinj'];
        $totalpinjgadaisehari = $kawalanstok[0]['gadaiini1'][0]['pinj'] + $kawalanstok[0]['gadaiini2'][0]['pinj'] + $kawalanstok[0]['gadaiini3'][0]['pinj'];
        $totalpinjgadai = $totalpinjgadaikeseluruhan + $totalpinjgadaisehari;

        //Tebus
        $totalbiltebuskeseluruhan = $kawalanstok[1]['tebus1'][0]['bil'] + $kawalanstok[1]['tebus2'][0]['bil'] + $kawalanstok[1]['tebus3'][0]['bil'];
        $totalbiltebussehari = $kawalanstok[1]['tebusini1'][0]['bil'] + $kawalanstok[1]['tebusini2'][0]['bil'] + $kawalanstok[1]['tebusini3'][0]['bil'];
        $totalbiltebus = $totalbiltebuskeseluruhan + $totalbiltebussehari;

        $totalpinjtebuskeseluruhan = $kawalanstok[1]['tebus1'][0]['pinj'] + $kawalanstok[1]['tebus2'][0]['pinj'] + $kawalanstok[1]['tebus3'][0]['pinj'];
        $totalpinjtebussehari = $kawalanstok[1]['tebusini1'][0]['pinj'] + $kawalanstok[1]['tebusini2'][0]['pinj'] + $kawalanstok[1]['tebusini3'][0]['pinj'];
        $totalpinjtebus = $totalpinjtebuskeseluruhan + $totalpinjtebussehari;

        //Ansuran

        $totalbilanskeseluruhan = $kawalanstok[4]['ans1']['bil'] + $kawalanstok[4]['ans2']['bil'] + $kawalanstok[4]['ans3']['bil'];
        $totalbilanssehari = $kawalanstok[4]['ansini1']['bil'] + $kawalanstok[4]['ansini2']['bil'] + $kawalanstok[4]['ansini3']['bil'];
        $totalbilans = $totalbilanskeseluruhan + $totalbilanssehari;

        $totalpinjanskeseluruhan = $kawalanstok[4]['ans1']['pinj'] + $kawalanstok[4]['ans2']['pinj'] + $kawalanstok[4]['ans3']['pinj'];
        $totalpinjanssehari = $kawalanstok[4]['ansini1']['pinj'] + $kawalanstok[4]['ansini2']['pinj'] + $kawalanstok[4]['ansini3']['pinj'];
        $totalpinjans = floatval($totalpinjanskeseluruhan + $totalpinjanssehari);

        //Lelong
        $totalbillelongkeseluruhan = $kawalanstok[2]['lelong1'][0]['bil'] + $kawalanstok[2]['lelong2'][0]['bil'] + $kawalanstok[2]['lelong3'][0]['bil'];
        $totalbillelongsehari = $kawalanstok[2]['lelongini1'][0]['bil'] + $kawalanstok[2]['lelongini2'][0]['bil'] + $kawalanstok[2]['lelongini3'][0]['bil'];
        $totalbillelong = $totalbillelongkeseluruhan + $totalbillelongsehari;

        $totalpinjlelongkeseluruhan = $kawalanstok[2]['lelong1'][0]['pinj'] + $kawalanstok[2]['lelong2'][0]['pinj'] + $kawalanstok[2]['lelong3'][0]['pinj'];
        $totalpinjlelongsehari = $kawalanstok[2]['lelongini1'][0]['pinj'] + $kawalanstok[2]['lelongini2'][0]['pinj'] + $kawalanstok[2]['lelongini3'][0]['pinj'];
        $totalpinjlelong = floatval($totalpinjlelongkeseluruhan + $totalpinjlelongsehari);

        $totalbilhapuskirakeseluruhan = $kawalanstok[3]['hapuskira1'][0]['bil'] + $kawalanstok[3]['hapuskira2'][0]['bil'] + $kawalanstok[3]['hapuskira3'][0]['bil'];
        $totalbilhapuskirasehari = $kawalanstok[3]['hapuskiraini1'][0]['bil'] + $kawalanstok[3]['hapuskiraini2'][0]['bil'] + $kawalanstok[3]['hapuskiraini3'][0]['bil'];
        $totalbilhapuskira = $totalbilhapuskirakeseluruhan + $totalbilhapuskirasehari;

        $totalpinjhapuskirakeseluruhan = $kawalanstok[3]['hapuskira1'][0]['pinj'] + $kawalanstok[3]['hapuskira2'][0]['pinj'] + $kawalanstok[3]['hapuskira3'][0]['pinj'];
        $totalpinjhapuskirasehari = $kawalanstok[3]['hapuskiraini1'][0]['pinj'] + $kawalanstok[3]['hapuskiraini2'][0]['pinj'] + $kawalanstok[3]['hapuskiraini3'][0]['pinj'];
        $totalpinjhapuskira = $totalpinjhapuskirakeseluruhan + $totalpinjhapuskirasehari;

        $totalbilkawalanstok = $totalbilgadai-$totalbiltebus/*-$totalbilans*/-$totalbillelong-$totalbilhapuskira;
        $totalpinjkawalanstok = $totalpinjgadai-$totalpinjtebus-$totalpinjans-$totalpinjlelong-$totalpinjhapuskira;

        return view('print.laporan_ringkasanharian', compact('cawangan_detail','tarikh_cetak','ringkasan_date','api_url','laporan','totalbilkawalanstok','totalpinjkawalanstok'));
    }

    public function laporanRingkasanHarianUjrah(){
        $auth_user = Auth::user();
        $api_url = config('api.url');

        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();

        return view('khazanah.laporanujrah.ringkasanharianujrah',compact('cawangan_info','api_url'));
    }

    public function cetakLaporanRingkasanHarianUjrah(Request $request){

        $api_url = config('api.url');
        $auth_user = Auth::user();
        $current = Carbon::now();
        $current = new Carbon();
        $tarikh_cetak = $current->toDateString();

        $kod_cawangan = $auth_user['cawangan'];

        if($request->dateringkasanharian == null){
            $startDate = $current->toDateString();
        }else{
            $split_date = explode('-',$request->dateringkasanharian);
            $startDate = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
        }

        if($request->dateringkasanharian == null){
            $endDate = $current->toDateString();
        }else{
            $split_date = explode('-',$request->dateringkasanharian);
            $endDate = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
        }

        $cawangan=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];

        $laporan_ringkasan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/reports/ringkasan-harian/' . $kod_cawangan,[
            'startDate' => $startDate,
            'endDate' => $endDate,
        ])->json();

        $laporan = $laporan_ringkasan['reportRingkasanHarian'][0];

        $kawalanstok = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/reports/kawalan-stok/' . $kod_cawangan, [
            'startDate' => $startDate,
            'endDate' => $endDate,
        ])->json();

        /* Kawalan Stok */
        //Gadai
        $totalbilgadaikeseluruhan = $kawalanstok[0]['gadai1'][0]['bil'] + $kawalanstok[0]['gadai2'][0]['bil'] + $kawalanstok[0]['gadai3'][0]['bil'];
        $totalbilgadaisehari = $kawalanstok[0]['gadaiini1'][0]['bil'] + $kawalanstok[0]['gadaiini2'][0]['bil'] + $kawalanstok[0]['gadaiini3'][0]['bil'];
        $totalbilgadai = $totalbilgadaikeseluruhan + $totalbilgadaisehari;

        $totalpinjgadaikeseluruhan = $kawalanstok[0]['gadai1'][0]['pinj'] + $kawalanstok[0]['gadai2'][0]['pinj'] + $kawalanstok[0]['gadai3'][0]['pinj'];
        $totalpinjgadaisehari = $kawalanstok[0]['gadaiini1'][0]['pinj'] + $kawalanstok[0]['gadaiini2'][0]['pinj'] + $kawalanstok[0]['gadaiini3'][0]['pinj'];
        $totalpinjgadai = $totalpinjgadaikeseluruhan + $totalpinjgadaisehari;

        //Tebus
        $totalbiltebuskeseluruhan = $kawalanstok[1]['tebus1'][0]['bil'] + $kawalanstok[1]['tebus2'][0]['bil'] + $kawalanstok[1]['tebus3'][0]['bil'];
        $totalbiltebussehari = $kawalanstok[1]['tebusini1'][0]['bil'] + $kawalanstok[1]['tebusini2'][0]['bil'] + $kawalanstok[1]['tebusini3'][0]['bil'];
        $totalbiltebus = $totalbiltebuskeseluruhan + $totalbiltebussehari;

        $totalpinjtebuskeseluruhan = $kawalanstok[1]['tebus1'][0]['pinj'] + $kawalanstok[1]['tebus2'][0]['pinj'] + $kawalanstok[1]['tebus3'][0]['pinj'];
        $totalpinjtebussehari = $kawalanstok[1]['tebusini1'][0]['pinj'] + $kawalanstok[1]['tebusini2'][0]['pinj'] + $kawalanstok[1]['tebusini3'][0]['pinj'];
        $totalpinjtebus = $totalpinjtebuskeseluruhan + $totalpinjtebussehari;

        //Ansuran

        $totalbilanskeseluruhan = $kawalanstok[4]['ans1']['bil'] + $kawalanstok[4]['ans2']['bil'] + $kawalanstok[4]['ans3']['bil'];
        $totalbilanssehari = $kawalanstok[4]['ansini1']['bil'] + $kawalanstok[4]['ansini2']['bil'] + $kawalanstok[4]['ansini3']['bil'];
        $totalbilans = $totalbilanskeseluruhan + $totalbilanssehari;

        $totalpinjanskeseluruhan = $kawalanstok[4]['ans1']['pinj'] + $kawalanstok[4]['ans2']['pinj'] + $kawalanstok[4]['ans3']['pinj'];
        $totalpinjanssehari = $kawalanstok[4]['ansini1']['pinj'] + $kawalanstok[4]['ansini2']['pinj'] + $kawalanstok[4]['ansini3']['pinj'];
        $totalpinjans = floatval($totalpinjanskeseluruhan + $totalpinjanssehari);

        //Lelong
        $totalbillelongkeseluruhan = $kawalanstok[2]['lelong1'][0]['bil'] + $kawalanstok[2]['lelong2'][0]['bil'] + $kawalanstok[2]['lelong3'][0]['bil'];
        $totalbillelongsehari = $kawalanstok[2]['lelongini1'][0]['bil'] + $kawalanstok[2]['lelongini2'][0]['bil'] + $kawalanstok[2]['lelongini3'][0]['bil'];
        $totalbillelong = $totalbillelongkeseluruhan + $totalbillelongsehari;

        $totalpinjlelongkeseluruhan = $kawalanstok[2]['lelong1'][0]['pinj'] + $kawalanstok[2]['lelong2'][0]['pinj'] + $kawalanstok[2]['lelong3'][0]['pinj'];
        $totalpinjlelongsehari = $kawalanstok[2]['lelongini1'][0]['pinj'] + $kawalanstok[2]['lelongini2'][0]['pinj'] + $kawalanstok[2]['lelongini3'][0]['pinj'];
        $totalpinjlelong = floatval($totalpinjlelongkeseluruhan + $totalpinjlelongsehari);

        $totalbilhapuskirakeseluruhan = $kawalanstok[3]['hapuskira1'][0]['bil'] + $kawalanstok[3]['hapuskira2'][0]['bil'] + $kawalanstok[3]['hapuskira3'][0]['bil'];
        $totalbilhapuskirasehari = $kawalanstok[3]['hapuskiraini1'][0]['bil'] + $kawalanstok[3]['hapuskiraini2'][0]['bil'] + $kawalanstok[3]['hapuskiraini3'][0]['bil'];
        $totalbilhapuskira = $totalbilhapuskirakeseluruhan + $totalbilhapuskirasehari;

        $totalpinjhapuskirakeseluruhan = $kawalanstok[3]['hapuskira1'][0]['pinj'] + $kawalanstok[3]['hapuskira2'][0]['pinj'] + $kawalanstok[3]['hapuskira3'][0]['pinj'];
        $totalpinjhapuskirasehari = $kawalanstok[3]['hapuskiraini1'][0]['pinj'] + $kawalanstok[3]['hapuskiraini2'][0]['pinj'] + $kawalanstok[3]['hapuskiraini3'][0]['pinj'];
        $totalpinjhapuskira = $totalpinjhapuskirakeseluruhan + $totalpinjhapuskirasehari;

        $totalbilkawalanstok = $totalbilgadai-$totalbiltebus/*-$totalbilans*/-$totalbillelong-$totalbilhapuskira;
        $totalpinjkawalanstok = $totalpinjgadai-$totalpinjtebus-$totalpinjans-$totalpinjlelong-$totalpinjhapuskira;

        $report_data = [
            'cawangan_detail' => $cawangan[0],
            'tarikh_cetak' => Helper::formatDate(Carbon::now()),
            'ringkasan_date' => sprintf('%s hingga %s', Helper::formatDate($request->startDate), Helper::formatDate($request->endDate)),
            'api_url' => url()->current(),
            'laporan' => $laporan,
            'totalbilkawalanstok' => $totalbilkawalanstok,
            'totalpinjkawalanstok' => $totalpinjkawalanstok,
        ];

        return view('print.laporan_ringkasanharianv2', $report_data);
    }

    // public function cetakLaporanRingkasanTerkumpul(Request $request){
    //     // dd($request);
    //     $api_url = config('api.url');
    //     $auth_user = Auth::user();
    //     $current = Carbon::now();
    //     $current = new Carbon();
    //     $tarikh_cetak = $current->toDateString();
    //     if($request->datetimepicker1 == null){
    //         $tebus_date = $current->toDateString();
    //     }else{
    //         $split_date = explode('-',$request->datetimepicker1);
    //         $tebus_date = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
    //     }
    //     $marhun = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/gadai/laporan/ringkasan/'. $auth_user['cawangan'],['tarikh' => $tebus_date])->json();
    //     $marhun = $marhun[0];

    //     $keseluruhanupahsimpan = $marhun['lelongupahsimpan'] + $marhun['tebus'][0]['upahsimpan'] + $marhun['advansur'][0]['upahsimpan'];

    //     $cawangan=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
    //     $cawangan_detail=$cawangan[0];
    //     // dd($gadai);
    //     return view('print.laporan_ringkasanterkumpul', compact('keseluruhanupahsimpan','marhun','cawangan_detail','tarikh_cetak','tebus_date','api_url'));
    // }

    public function cetakLaporanRingkasanTerkumpul(Request $request){
        // dd($request);
        $api_url = config('api.url');
        $auth_user = Auth::user();
        $current = Carbon::now();
        $current = new Carbon();
        $tarikh_cetak = $current->toDateString();

        if($request->datemula == null){
            $mula_date = '2017-01-01';
        }else{
            $split_date = explode('-', $request->datemula);
            $mula_date = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
        }

        if($request->datehingga == null){
            $hingga_date = $current->toDateString();
        }else{
            $split_date = explode('-', $request->datehingga);
            $hingga_date = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
        }

        $laporan_ringkasan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/laporan/terkumpul',[
            'kodcaw' => $auth_user['cawangan'],
            'mula' => $mula_date,
            'akhir' => $hingga_date
        ])->json();

        $laporan_ringkasan = $laporan_ringkasan[0];

        $cawangan=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];

        return view('print.laporan_ringkasanterkumpul', compact('laporan_ringkasan','cawangan_detail','tarikh_cetak','mula_date','hingga_date','api_url'));
    }

    public function laporanKawalanStok(){
        $auth_user = Auth::user();
        $api_url = config('api.url');


        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();

        return view('khazanah.laporan.kawalanstok',compact('cawangan_info','api_url'));
    }

    public function cetakLaporanKawalanStok(Request $request){
        // dd($request);
        $api_url = config('api.url');
        $auth_user = Auth::user();
        $current = Carbon::now();
        $current = new Carbon();
        $tarikh_cetak = $current->toDateString();
        if($request->datetimepicker1 == null){
            $tebus_date = $current->toDateString();
        }else{
            $split_date = explode('-',$request->datetimepicker1);
            $tebus_date = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
        }
        $kawalanstok = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/kawalanstok/laporan/'. $auth_user['cawangan'] .'/'. $tebus_date)->json();

        $cawangan=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];

        return view('print.laporan_kawalanstok', compact('kawalanstok','cawangan_detail','tarikh_cetak','tebus_date','api_url'));
    }


    public function kakitangan()
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');
        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();

        $teller = User::where([['roles','=','teller'],['cawangan','=',$auth_user['cawangan']]])->get();
        $khazanah = User::whereIn('roles',['pc','ppc'])->where('cawangan','=',$auth_user['cawangan'])->get();

        $ppc = User::where([['roles','=','ppc'],['cawangan','=',$auth_user['cawangan']]])->first();
        $pc = User::where([['roles','=','pc'],['cawangan','=',$auth_user['cawangan']]])->first();

        $old_data = [
            'teller' => $teller->pluck('name')->toArray(),
            'khazanah' => $khazanah->pluck('name')->toArray(),
            'pc'=> $pc ? $pc->name : null,
            'ppc' => $ppc ? $ppc->name : null,
        ];

        $new_data = [
            'teller' => $teller->pluck('name')->toArray(),
            'khazanah' => $khazanah->pluck('name')->toArray(),
            'pc' => $pc ? $pc->name : null,
            'ppc' => $ppc ? $ppc->name : null,
        ];

        Http::withOptions(['verify' => config('app.api_verify')])->post(config('api.url'). '/api/kakitangan/'. $auth_user['cawangan'], ['namaoperasi' => $auth_user['name'],'new_data' => $new_data])->json();

        return view('khazanah.kakitangan',compact('cawangan_info','api_url','teller','khazanah','pc','ppc'));
    }

    public function capaian()
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');
        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
        return view('khazanah.capaian',compact('cawangan_info','api_url'));
    }


    public function pelanggan()
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');
        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
        return view('khazanah.pelanggan',compact('cawangan_info','api_url'));
    }

    public function panjar()
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');
        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();

        $usercawangan = User::where('cawangan','=',$auth_user['cawangan'])->get();

        $denominasi=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/DenominasiController/'.$auth_user['cawangan'])->json();

        if(sizeof($denominasi) == 0)
        {
            $denominasi=Http::withOptions(['verify' => config('app.api_verify')])->post(config('api.url').'/api/DenominasiCreate/'.$auth_user['cawangan'])->json();
        }
        else
        {
            $denominasi=$denominasi[0];
        }

        return view('khazanah.panjar',compact('cawangan_info','api_url','usercawangan','denominasi'));
    }

    public function bajet()
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');
        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();

        $data_sebenar = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/admin/dashboard/aging',['kodcaw' => $auth_user['cawangan'], 'options' => 'sebenar'])->json();
        $data_lelong = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/admin/dashboard/aging',['kodcaw' => $auth_user['cawangan'], 'options' => 'datalelong'])->json();
        $data_filter = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/admin/dashboard/aging',['kodcaw' => $auth_user['cawangan'], 'options' => 'filter'])->json();

        return view('khazanah.bajet',compact('cawangan_info','api_url','data_sebenar','data_lelong','data_filter'));
    }



    public function laporanAging_Bulanan()//added by fariz 2021-01-19
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');


        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
        return view('khazanah.laporan.agingbulanan',compact('cawangan_info','api_url'));
    }


    public function cetaklaporang_Aging(Request $request) //added by fariz 2021-01-19
    {

        $auth_user = Auth::user();
        $api_url = config('api.url');
        $tarikh_aging=$request;
       // dd($tarikh_aging);
       $num=$request->num;
       $current = Carbon::now();
       $current = new Carbon();


       if($request->datetimepicker1 == null){
        $tebus_date = $current->toDateString();
       }else
       {
        $split_date = explode('-',$request->datetimepicker1);
        $tarikh_aging = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
       }

       $num1=$request->num1;
       $num2=$request->num2;
       $num3=$request->num3;
       $num4=$request->num4;

       $num5=$request->num5;
       $num6=$request->num6;
       $num7=$request->num7;
       $num8=$request->num8;

       $num9=$request->num9;
       $num10=$request->num10;
       $num11=$request->num11;
       $num12=$request->num12;
       $num13=$request->num13;

        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();

        $cawangan=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];


         $data=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/gadai/aging/test/'.$auth_user['cawangan'].'/'.$tarikh_aging.'/'.$num1)->json();
         $data_2=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/gadai/aging/test/'.$auth_user['cawangan'].'/'.$tarikh_aging.'/'.$num2)->json();
         $data_3=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/gadai/aging/test/'.$auth_user['cawangan'].'/'.$tarikh_aging.'/'.$num3)->json();


         $data_4=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/gadai/aging/test/'.$auth_user['cawangan'].'/'.$tarikh_aging.'/'.$num4)->json();
         $data_5=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/gadai/aging/test/'.$auth_user['cawangan'].'/'.$tarikh_aging.'/'.$num5)->json();
         $data_6=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/gadai/aging/test/'.$auth_user['cawangan'].'/'.$tarikh_aging.'/'.$num6)->json();
         $data7=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/gadai/aging/test/'.$auth_user['cawangan'].'/'.$tarikh_aging.'/'.$num7)->json();
         $data_8=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/gadai/aging/test/'.$auth_user['cawangan'].'/'.$tarikh_aging.'/'.$num8)->json();
         $data_9=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/gadai/aging/test/'.$auth_user['cawangan'].'/'.$tarikh_aging.'/'.$num9)->json();
         $data_10=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/gadai/aging/test/'.$auth_user['cawangan'].'/'.$tarikh_aging.'/'.$num10)->json();
         $data_11=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/gadai/aging/test/'.$auth_user['cawangan'].'/'.$tarikh_aging.'/'.$num11)->json();
         $data_12=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/gadai/aging/test/'.$auth_user['cawangan'].'/'.$tarikh_aging.'/'.$num12)->json();
         $data_13=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/gadai/aging/lebih12bulan/'.$auth_user['cawangan'].'/'.$tarikh_aging)->json();



         $data2=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/gadai/JumlahagingBulanan/test/'.$auth_user['cawangan'].'/'.$tarikh_aging)->json();


        return view('print.laporan_aging_bulanan', compact('cawangan_info', 'api_url','cawangan_detail','data','data2','data_2','data_3','data_4','data_5',
        'data_6','data7','data_8','data_9','data_10','data_11','data_12','data_13','tarikh_aging'));
    }


    public function pendahuluanserahanHQ(Request $request)
    {

        $auth_user = Auth::user();

        $gldetail = Http::withOptions(['verify' => config('app.api_verify')])->post(config('api.url').'/api/gldetailkaunter/'. $auth_user['cawangan'],[
                    'amaun' => $request->amaun,
                    'jenisgldetail' => $request->jenisgldetail,
                    'keterangan' => $request->keterangan,
                    'kodcaw' => $request->kodcaw,
                    'teller' => $request->teller,
                    'dtacct' => $request->dtacct,
                    'ktacct' => $request->ktacct
        ])->json();
       
                        $amaun=$request->amaun;

         $peti_besi_terkini=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/VaultController/'.$auth_user['cawangan'])->json();

        if(sizeof($peti_besi_terkini) == 0){
            $peti_besi_terkini=Http::withOptions(['verify' => config('app.api_verify')])->post(config('api.url').'/api/daftarVault/'.$auth_user['cawangan'])->json();

            $pendahuluanserahan = Http::withOptions(['verify' => config('app.api_verify')])->put(config('api.url').'/api/UpdateVaultPendahuluanSerahan/'. $auth_user['cawangan'],[
                'jenispendahuluanserahan' => $request->jenisgldetail,
                'amaun' => $request->amaun,
            ])->json();

        }else{

            $pendahuluanserahan = Http::withOptions(['verify' => config('app.api_verify')])->put(config('api.url').'/api/UpdateVaultPendahuluanSerahan/'. $auth_user['cawangan'],[
                'jenispendahuluanserahan' => $request->jenisgldetail,
                'amaun' => $request->amaun,
            ])->json();
        }

        return $this->denominasipendahuluanserahan($request,$gldetail);
    }

    public function denominasipendahuluanserahan($request,$gldetail)
    {
        $auth_user = Auth::user();

        $denominasilist = Http::withOptions(['verify' => config('app.api_verify')])->get( config('api.url') .'/api/DenominasiController/'. $auth_user['cawangan'])->json();

        //bundle initialize
        $rekodb1 = $denominasilist[0]['bd1']; $rekodb5 = $denominasilist[0]['bd5']; $rekodb10 = $denominasilist[0]['bd10']; $rekodb20 = $denominasilist[0]['bd20'];
        $rekodb50 = $denominasilist[0]['bd50']; $rekodb100 = $denominasilist[0]['bd100'];

        //wang Syilling Initialize
        $rekodsen1 = $denominasilist[0]['txtsen1']; $rekodsen5 = $denominasilist[0]['txtsen5']; $rekodsen10 = $denominasilist[0]['txtsen10'];
        $rekodsen20 = $denominasilist[0]['txtsen20']; $rekodsen50 = $denominasilist[0]['txtsen50'];

        //Wang Ringgit Initialize
        $rekodring1 = $denominasilist[0]['txtring1']; $rekodring5 = $denominasilist[0]['txtring5']; $rekodring10 = $denominasilist[0]['txtring10'];
        $rekodring20 = $denominasilist[0]['txtring20']; $rekodring50 = $denominasilist[0]['txtring50']; $rekodring100 = $denominasilist[0]['txtring100'];

        if($request->jenisgldetail == 'pendahuluan')
        {
            // $rekodb1 += intval($request->bundle1);
            // $rekodb5 += intval($request->bundle5);
            // $rekodb10 += intval($request->bundle10);
            // $rekodb20 += intval($request->bundle20);
            // $rekodb50 += intval($request->bundle50);
            // $rekodb100 += intval($request->bundle100);

            $rekodsen1 += intval($request->ambilansen1);
            $rekodsen5 += intval($request->ambilansen5);
            $rekodsen10 += intval($request->ambilansen10);
            $rekodsen20 += intval($request->ambilansen20);
            $rekodsen50 += intval($request->ambilansen50);

            $rekodring1 += intval($request->ambilanring1);
            $rekodring5 += intval($request->ambilanring5);
            $rekodring10 += intval($request->ambilanring10);
            $rekodring20 += intval($request->ambilanring20);
            $rekodring50 += intval($request->ambilanring50);
            $rekodring100 += intval($request->ambilanring100);

            $denominasiupdate = Http::withOptions(['verify' => config('app.api_verify')])->put(config('api.url').'/api/DenominasiUpdate/'.$auth_user['cawangan'],[

                'bd1' => $rekodb1,
                'bd5'=> $rekodb5,
                'bd10'=> $rekodb10,
                'bd20'=> $rekodb20,
                'bd50'=> $rekodb50,
                'bd100'=> $rekodb100,
                'txtring1'=> $rekodring1,
                'txtring5'=> $rekodring5,
                'txtsen1'=> $rekodsen1,
                'txtsen5'=> $rekodsen5,
                'txtsen10'=> $rekodsen10,
                'txtsen20'=> $rekodsen20,
                'txtsen50'=> $rekodsen50,
                'txtring10'=> $rekodring10,
                'txtring20'=> $rekodring20,
                'txtring50'=> $rekodring50,
                'txtring100'=> $rekodring100,
                'txtring1000'=> "0",
                'jumlahbundle'=> "0",
                'txtjumlah'=> "0",
                'jumlahkeping' => "0"

              ]);

        }else if($request->jenisgldetail == 'serahan')
        {
            // $rekodb1 -= intval($request->bundle1);
            // $rekodb5 -= intval($request->bundle5);
            // $rekodb10 -= intval($request->bundle10);
            // $rekodb20 -= intval($request->bundle20);
            // $rekodb50 -= intval($request->bundle50);
            // $rekodb100 -= intval($request->bundle100);

            $rekodsen1 -= intval($request->ambilansen1);
            $rekodsen5 -= intval($request->ambilansen5);
            $rekodsen10 -= intval($request->ambilansen10);
            $rekodsen20 -= intval($request->ambilansen20);
            $rekodsen50 -= intval($request->ambilansen50);

            $rekodring1 -= intval($request->ambilanring1);
            $rekodring5 -= intval($request->ambilanring5);
            $rekodring10 -= intval($request->ambilanring10);
            $rekodring20 -= intval($request->ambilanring20);
            $rekodring50 -= intval($request->ambilanring50);
            $rekodring100 -= intval($request->ambilanring100);


            $denominasiupdate = Http::withOptions(['verify' => config('app.api_verify')])->put(config('api.url').'/api/DenominasiUpdate/'.$auth_user['cawangan'],[

                'bd1' => $rekodb1,
                'bd5'=> $rekodb5,
                'bd10'=> $rekodb10,
                'bd20'=> $rekodb20,
                'bd50'=> $rekodb50,
                'bd100'=> $rekodb100,
                'txtring1'=> $rekodring1,
                'txtring5'=> $rekodring5,
                'txtsen1'=> $rekodsen1,
                'txtsen5'=> $rekodsen5,
                'txtsen10'=> $rekodsen10,
                'txtsen20'=> $rekodsen20,
                'txtsen50'=> $rekodsen50,
                'txtring10'=> $rekodring10,
                'txtring20'=> $rekodring20,
                'txtring50'=> $rekodring50,
                'txtring100'=> $rekodring100,
                'txtring1000'=> "0",
                'jumlahbundle'=> "0",
                'txtjumlah'=> "0",
                'jumlahkeping' => "0"

              ]);
        }

        $denopendser = Http::withOptions(['verify' => config('app.api_verify')])->post(config('api.url').'/api/requestdenopendser/'.$auth_user['cawangan'],[
            'kodlaluan' => $auth_user['name'],
            'amaun' => floatval($request->amaun),
            'bd1' => 0,
            'bd5' => 0,
            'bd10' => 0,
            'bd20' => 0,
            'bd50' => 0,
            'bd100' => 0,
            'txtring1' => intval($request->ambilanring1),
            'txtring5' => intval($request->ambilanring5),
            'txtring10' => intval($request->ambilanring10),
            'txtring20' => intval($request->ambilanring20),
            'txtring50' => intval($request->ambilanring50),
            'txtring100' => intval($request->ambilanring100),
            'txtring1000' => 0,
            'txtsen1' => intval($request->ambilansen1),
            'txtsen5' => intval($request->ambilansen5),
            'txtsen10' => intval($request->ambilansen10),
            'txtsen20' => intval($request->ambilansen20),
            'txtsen50' => intval($request->ambilansen50),
            'jenis' => $request->jenisgldetail,
            'recno' => intval($gldetail['id'])
          ]);

        if($denominasiupdate)
            return "LULUS";
        else
            return "GAGAL";
    }

    public function deletependahuluanserahan(Request $request)
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');
        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();

        $denopendser = Http::withOptions(['verify' => config('app.api_verify')])->get( config('api.url') .'/api/getdenopendser/'. $auth_user['cawangan'],['recno' => $request->recno])->json();


        if(sizeof($denopendser) > 0)
            $denopendser = $denopendser[0];

        $denominasilist = Http::withOptions(['verify' => config('app.api_verify')])->get( config('api.url') .'/api/DenominasiController/'. $auth_user['cawangan'])->json();

        //bundle initialize
        $rekodb1 = $denominasilist[0]['bd1']; $rekodb5 = $denominasilist[0]['bd5']; $rekodb10 = $denominasilist[0]['bd10']; $rekodb20 = $denominasilist[0]['bd20'];
        $rekodb50 = $denominasilist[0]['bd50']; $rekodb100 = $denominasilist[0]['bd100'];

        //wang Syilling Initialize
        $rekodsen1 = $denominasilist[0]['txtsen1']; $rekodsen5 = $denominasilist[0]['txtsen5']; $rekodsen10 = $denominasilist[0]['txtsen10'];
        $rekodsen20 = $denominasilist[0]['txtsen20']; $rekodsen50 = $denominasilist[0]['txtsen50'];

        //Wang Ringgit Initialize
        $rekodring1 = $denominasilist[0]['txtring1']; $rekodring5 = $denominasilist[0]['txtring5']; $rekodring10 = $denominasilist[0]['txtring10'];
        $rekodring20 = $denominasilist[0]['txtring20']; $rekodring50 = $denominasilist[0]['txtring50']; $rekodring100 = $denominasilist[0]['txtring100'];

        if($request->jenisserahan === 'pendahuluan')
        {
            //Tolok Rekod
            $rekodsen1 -= intval($denopendser['txtsen1']);
            $rekodsen5 -= intval($denopendser['txtsen5']);
            $rekodsen10 -= intval($denopendser['txtsen10']);
            $rekodsen20 -= intval($denopendser['txtsen20']);
            $rekodsen50 -= intval($denopendser['txtsen50']);

            $rekodring1 -= intval($denopendser['txtring1']);
            $rekodring5 -= intval($denopendser['txtring5']);
            $rekodring10 -= intval($denopendser['txtring10']);
            $rekodring20 -= intval($denopendser['txtring20']);
            $rekodring50 -= intval($denopendser['txtring50']);
            $rekodring100 -= intval($denopendser['txtring100']);
        }else{
            //Tolok Rekod
            $rekodsen1 += intval($denopendser['txtsen1']);
            $rekodsen5 += intval($denopendser['txtsen5']);
            $rekodsen10 += intval($denopendser['txtsen10']);
            $rekodsen20 += intval($denopendser['txtsen20']);
            $rekodsen50 += intval($denopendser['txtsen50']);

            $rekodring1 += intval($denopendser['txtring1']);
            $rekodring5 += intval($denopendser['txtring5']);
            $rekodring10 += intval($denopendser['txtring10']);
            $rekodring20 += intval($denopendser['txtring20']);
            $rekodring50 += intval($denopendser['txtring50']);
            $rekodring100 += intval($denopendser['txtring100']);
        }


        $denominasicreate = Http::withOptions(['verify' => config('app.api_verify')])->put(config('api.url').'/api/DenominasiUpdate/'.$auth_user['cawangan'],[
            'bd1' => $rekodb1,
            'bd5'=> $rekodb5,
            'bd10'=> $rekodb10,
            'bd20'=> $rekodb20,
            'bd50'=> $rekodb50,
            'bd100'=> $rekodb100,
            'txtring1'=> $rekodring1,
            'txtring5'=> $rekodring5,
            'txtsen1'=> $rekodsen1,
            'txtsen5'=> $rekodsen5,
            'txtsen10'=> $rekodsen10,
            'txtsen20'=> $rekodsen20,
            'txtsen50'=> $rekodsen50,
            'txtring10'=> $rekodring10,
            'txtring20'=> $rekodring20,
            'txtring50'=> $rekodring50,
            'txtring100'=> $rekodring100,
            'txtring1000'=> "0",
            'jumlahbundle'=> "0",
            'txtjumlah'=> "0",
            'jumlahkeping' => "0"
          ]);

        $deletependser = Http::withOptions(['verify' => config('app.api_verify')])->post(config('api.url') . '/api/DeletePendSer/'. $auth_user['cawangan'],[
            'recno' => $request->recno
        ])->json();

        $updateVault = Http::withOptions(['verify' => config('app.api_verify')])->put( config('api.url').'/api/UpdateVault/PendahuluanSerahan/'.$auth_user['cawangan']);

        $upVault = $updateVault->getStatusCode();
        $upDeno = $denominasicreate->getStatusCode();

        if($upVault == 200 && $upDeno == 200)
        {
            return 'berjaya';
        }else{
            return 'gagal';
        }
    }




    public function laporanImbangduga()
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');
        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();


        return view('khazanah.laporan.imbangduga', compact('cawangan_info','api_url'));
    }


    public function cetakImbangDuga(Request $request)
    {

        // dd(request()->all());

        $auth_user = Auth::user();
        $api_url = config('api.url');

        // dd($tarikh_aging);
        $num=$request->num;
        $current = Carbon::now();
        $current = new Carbon();

        $tarikh_cetak = $current->toDateString();

        $tarikh_imbangan= $current->toDateString();
        $tarikh_imbangan2= $current->toDateString();

        if($request->datetimepicker1 == null){
            $tarikh_cetak = $current->toDateString();
        }else
        {
            $split_date = explode('-',$request->datetimepicker1);
            $tarikh_imbangan = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
        }


        $tarikh_cetak = $current->toDateString();


        if($request->datetimepicker2 == null){
            $tarikh_cetak = $current->toDateString();
        }else
        {
            $split_date = explode('-',$request->datetimepicker2);
            $tarikh_imbangan2 = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
        }


        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
        $cawangan=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];

        // $hartasemasa = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/getGlCode/' . $auth_user['cawangan'] . '/acttype/A/')->json();
        // $tanggungansemasa = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/getGlCode/' . $auth_user['cawangan'] . '/acttype/L/')->json();

        //Calculation Imbang Duga
        $imbangduga = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/CalculationPendahuluanSerahan/' . $auth_user['cawangan'],['tarikh' => $tarikh_imbangan2])->json();
        $keuntungan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/KeuntunganArRahn/' . $auth_user['cawangan'],['tarikh' => $tarikh_imbangan2])->json();
        $rebet = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/RebetCalculation/' . $auth_user['cawangan'],['tarikh' => $tarikh_imbangan2])->json();

        //Harta semasa
        $pinjamandiberi = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/PinjamanDiberi/' . $auth_user['cawangan'],['tarikh' => $tarikh_imbangan2])->json();
        $pembiayaantawarruq = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/PembiayaanDiberiTawarruq/' . $auth_user['cawangan'],['tarikh' => $tarikh_imbangan2])->json();
        $tunaiditangan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/TunaiDiTangan/' . $auth_user['cawangan'],['tarikh' => $tarikh_imbangan2])->json();
        $penghutangkaki = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/PenghutangKakitangan/' . $auth_user['cawangan'],['tarikh' => $tarikh_imbangan2])->json();
        $untungrugi = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/UntungRugiCalculation/' . $auth_user['cawangan'],['tarikh' => $tarikh_imbangan2])->json();
        $bayaranpendahuluan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/BayaranPendahuluan/' . $auth_user['cawangan'],['tarikh' => $tarikh_imbangan2])->json();

        //Tanggungan Semasa
        $panjar = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/CalculationPanjar/' . $auth_user['cawangan'],['tarikh' => $tarikh_imbangan2])->json();
        $bakilelong = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/BakiLelongCalculation/' . $auth_user['cawangan'],['tarikh' => $tarikh_imbangan2])->json();
        $wtd = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/SerahBaki/' . $auth_user['cawangan'],['tarikh' => $tarikh_imbangan2])->json();
        $cajlelong = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cajlelong/' . $auth_user['cawangan'],['tarikh' => $tarikh_imbangan2])->json();
        $keuntungansebenar = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/KeuntunganSebenar/' . $auth_user['cawangan'],['tarikh' => $tarikh_imbangan2])->json();
        $cajperkhidmatan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cajperkhidmatanterkumpul/' . $auth_user['cawangan'],['tarikh' => $tarikh_imbangan2])->json();
        $hapuskira = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/CalculationHapusKira/' . $auth_user['cawangan'],['tarikh' => $tarikh_imbangan2])->json();
        $xtra = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/Xtra/' . $auth_user['cawangan'],['tarikh' => $tarikh_imbangan2])->json();


        //TunaiDitangan Sementara Upah
        if($tarikh_imbangan2 >= '2021-04-22' && $tarikh_imbangan2 < '2021-05-05')
        {
            $advansurharini = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/advansurharini/' . $auth_user['cawangan'],['tarikh' => $tarikh_imbangan2])->json();
            $totaltunaiditangan = floatval($tunaiditangan) - floatval($advansurharini);
        }else{
            $totaltunaiditangan = $tunaiditangan;
        }

        $jualan1stop = 0;$belian1stop = 0;$utgterkumpulxtra = 0;$rugilelongxtra = 0;$bakilelongxtra = 0;$cajlelongxtra = 0;$upahsimpanxtra = 0;
        //1Stop Special Case
        if($auth_user['cawangan'] === '1STOP')
        {
            $jualan1stop = floatval(129857.00);
            $belian1stop = floatval(122446.50);
            $utgterkumpulxtra = floatval(6727.10);
            $rugilelongxtra = floatval(10079.30);
            $bakilelongxtra = floatval(123.20);
            $cajlelongxtra = floatval(25.50);
            $upahsimpanxtra = floatval(4894.40);
            $peruntukanhapuskira = ($xtra['phapuskira']);

        }else{
            $upahsimpanxtra = $xtra['upahsimpan'];
            $bakilelongxtra = $xtra['bakilelong'];
            $utgterkumpulxtra = $xtra['untungrugiterkumpul'];
            $rugilelongxtra = -($xtra['kerugianlelong']);
            $peruntukanhapuskira = ($xtra['phapuskira']);
        }

        //Terkumpul
        $untungrugiterkumpul = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/UntungRugiTerkumpul/'. $auth_user['cawangan'],['tarikh' => $tarikh_imbangan2])->json();

        $hartasemasa = [];
        // $hartasemasa = ['serahan' => $imbangduga['serahan'],'pinjamankoop' => $pinjamandiberi['pinjamankoop'],'emaspalsu' => $pinjamandiberi['emaspalsu'],'tunaiditangan' => $tunaiditangan['bakiakhir']];

        if($totaltunaiditangan > 0) array_push($hartasemasa,['glcode' => '1000/010','descpt' => 'Tunai Di Tangan', 'amaun' => $totaltunaiditangan]);
        if($pembiayaantawarruq > 0) array_push($hartasemasa,['glcode' => '1000/010','descpt' => 'Pembiayaan Di Bawah Tawarruq', 'amaun' => $pembiayaantawarruq]);
        if($penghutangkaki > 0) array_push($hartasemasa,['glcode' => '1000/011','descpt' => 'Penghutang Kakitangan', 'amaun' => $penghutangkaki]);
        //array_push($hartasemasa,['glcode' => '1000/045','descpt' => 'Serahan Bayaran Online', 'amaun' => 0.00]);
        if($keuntungan > 0) array_push($hartasemasa,['glcode' => '1000/211','descpt' => 'Keuntungan Tertangguh', 'amaun' => $keuntungan]);
        if($bayaranpendahuluan > 0) array_push($hartasemasa,['glcode' => '5000/041','descpt' => 'Bayaran Pendahuluan','amaun' => ($bayaranpendahuluan * -1)]);
        if($rebet > 0) array_push($hartasemasa,['glcode' => '5000/240','descpt' => 'Rebet 1','amaun' => ($rebet * -1)]);
        if($imbangduga['serahan'] > 0) array_push($hartasemasa,['glcode' => '1000/020','descpt' => 'Serahan Kepada HQ', 'amaun' => $imbangduga['serahan']]);
        if($pinjamandiberi['pinjamankoop'] > 0) array_push($hartasemasa,['glcode' => '1000/013','descpt' => 'Pinjaman Diberi - Koop PKB', 'amaun' => $pinjamandiberi['pinjamankoop']]);
        if($pinjamandiberi['emaspalsu'] > 0) array_push($hartasemasa,['glcode' => '1000/014','descpt' => 'Pinjaman Diberi - Emas Palsu / Kes', 'amaun' => $pinjamandiberi['emaspalsu']]);
        if($imbangduga['pindahkekpkb'] > 0) array_push($hartasemasa,['glcode' => '1000/090','descpt' => 'Pindah Ke KPKB(IP)', 'amaun' => $imbangduga['pindahkekpkb']]);
        if($peruntukanhapuskira > 0) array_push($hartasemasa,['glcode' => '1000/015','descpt' => 'Peruntukan Hapus Kira', 'amaun' => $peruntukanhapuskira]);
        if($belian1stop > 0) array_push($hartasemasa,['glcode' => '8000/501','descpt' => 'Belian 1 Stop', 'amaun' => $belian1stop]);

        $totalhartasemasa = 0;
        foreach($hartasemasa as $index => $hs)
        {
            $totalhartasemasa += $hs['amaun'];
        }

        $tanggungansemasa = [];

        // $tanggungansemasa = ['pendahuluan' => $imbangduga['pendahuluan'],'bds' => $panjar['bds'],'perubatan' => $panjar['perubatan'],'keraian' => $panjar['keraian'],'bakilelong' => $bakilelong['bakilelong'],'rugilelong' => $bakilelong['rugilelong'],'wtd' => $wtd,'cajlelong' => $cajlelong];

        if($keuntungan > 0) array_push($tanggungansemasa,['glcode' => '2000/007','descpt' => 'Keuntungan Tergantung', 'amaun' => $keuntungan]);
        if($keuntungansebenar > 0) array_push($tanggungansemasa,['glcode' => '5000/013','descpt' => 'Upah Simpan','amaun' => $keuntungansebenar]);
        if($rebet > 0) array_push($tanggungansemasa,['glcode' => '5000/241','descpt' => 'Rebet 2','amaun' => ($rebet * -1)]);
        if($imbangduga['pendahuluan'] > 0) array_push($tanggungansemasa,['glcode' => '7000/102','descpt' => 'Pendahuluan Daripada HQ','amaun' => $imbangduga['pendahuluan']]);
        if($panjar['bds'] > 0) array_push($tanggungansemasa,['glcode' => '3000/017','descpt' => 'Panjar - Baiki dan Selenggara','amaun' => -$panjar['bds']]);
        if($panjar['perubatan'] > 0) array_push($tanggungansemasa,['glcode' => '3000/018','descpt' => 'Panjar - Belanja AM','amaun' => -$panjar['perubatan']]);
        if($panjar['keraian'] > 0) array_push($tanggungansemasa,['glcode' => '3000/019','descpt' => 'Panjar - Keraian Dan Promosi','amaun' => -$panjar['keraian']]);
        if($bakilelong['bakilelong'] != 0) array_push($tanggungansemasa,['glcode' => '7000/090','descpt' => 'Baki Lelong','amaun' => $bakilelong['bakilelong']]);
        if($bakilelong['rugilelong'] != 0) array_push($tanggungansemasa,['glcode' => '3000/020','descpt' => 'Kerugian Lelong','amaun' => $bakilelong['rugilelong']]);
        if($wtd > 0) array_push($tanggungansemasa,['glcode' => '3000/021','descpt' => 'SERAHAN WANG TAK DITUNTUT','amaun' => $wtd]);
        if($cajlelong > 0) array_push($tanggungansemasa,['glcode' => '5000/213','descpt' => 'Pendapatan Caj Lelong','amaun' => $cajlelong]);
        if($untungrugiterkumpul != 0) array_push($tanggungansemasa,['glcode' => '7000/101','descpt' => 'Untung/Rugi Terkumpul','amaun' => $untungrugiterkumpul]);
        if($cajperkhidmatan > 0) array_push($tanggungansemasa,['glcode' => '5000/213','descpt' => 'Caj Perkhidmatan Terkumpul','amaun' => $cajperkhidmatan]);
        if($cajlelongxtra > 0) array_push($tanggungansemasa,['glcode' => '5000/313','descpt' => 'Pendapatan Caj Lelong Xtra ','amaun' => $cajlelongxtra]);
        if($upahsimpanxtra > 0) array_push($tanggungansemasa,['glcode' => '5000/413','descpt' => 'Upah Simpan Xtra - Koperasi PKB','amaun' => $upahsimpanxtra]);
        if($bakilelongxtra > 0) array_push($tanggungansemasa,['glcode' => '7000/190','descpt' => 'Baki Lelong Xtra','amaun' => $bakilelongxtra]);
        if($rugilelongxtra != 0) array_push($tanggungansemasa,['glcode' => '7000/191','descpt' => 'Rugi Lelong Xtra','amaun' => -$rugilelongxtra]);
        if($utgterkumpulxtra > 0) array_push($tanggungansemasa,['glcode' => '7000/201','descpt' => 'Untung/Rugi Terkumpul Xtra','amaun' => $utgterkumpulxtra]);
        if($jualan1stop > 0) array_push($tanggungansemasa,['glcode' => '9000/501','descpt' => 'Jualan 1 Stop','amaun' => $jualan1stop]);
        if($hapuskira > 0) array_push($tanggungansemasa,['glcode' => '3000/011','descpt' => 'Hapus Kira','amaun' => -$hapuskira]);

        $totaltanggungansemasa = 0;
        foreach($tanggungansemasa as $index => $ts)
        {
            $totaltanggungansemasa += $ts['amaun'];
        }

        $untungrugifield = [];
        array_push($untungrugifield,['glcode' => '2000/007','descpt' => 'Keuntungan Sebenar', 'amaun' => $untungrugi]);


        $totaluntungrugi = 0;
        $totaluntungrugi = $totaltanggungansemasa + $untungrugi;


        return view('print.laporan_imbanduga',compact('tarikh_imbangan', 'tarikh_imbangan2','hartasemasa','untungrugifield','tanggungansemasa','totalhartasemasa','totaluntungrugi','totaltanggungansemasa','tarikh_cetak','cawangan_info','cawangan','api_url','cawangan_detail'));

    }



    //KE PAGE LEJER AM
    public function data_lejer_am()
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');

        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
        return view('khazanah.laporan.datalejer_am',  compact('cawangan_info', 'api_url'));
    }

    //print lejer_AM
    public function cetak_lejer_am(Request $request)
    {

        $auth_user = Auth::user();
        $api_url = config('api.url');

        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
        $cawangan=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];

        $auth_user = Auth::user();
        $api_url = config('api.url');

        $num=$request->num;
        $current = Carbon::now();
        $current = new Carbon();
        $tarikh_cetak = $current->toDateString();

        if($request->datetimepicker1 == null){
            $tarikh_cetak = $current->toDateString();
        }else {
            $split_date = explode('-',$request->datetimepicker1);
            $tarikh_mula = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
        }

        if($request->datetimepicker2 == null){
            $tarikh_cetak = $current->toDateString();
        }
        else{
            $split_date = explode('-',$request->datetimepicker2);
            $tarikh_akhir = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
        }


        $lejeramdata = Lejeram::orderBy('glcode')->get();
        $lejeram = array();
        $len = count($lejeramdata);
        $totalkredit = 0;
        $totaldebit = 0;

        foreach($lejeramdata as $index => $lejer)
        {


            if($lejer->api_redirect != null)
            {
                ${$lejer['variable']} = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). $lejer['api_redirect'] . $auth_user['cawangan'],['mula' => $tarikh_mula, 'hingga' => $tarikh_akhir])->json();

                if($lejer->namadebit == null)
                {
                    $debit = null;
                }else{
                    if($lejer->oneVar == 1)
                    {
                        $debit = ${$lejer['variable']};
                    }else{
                        $debit = ${$lejer['variable']}[$lejer->namadebit];
                    }

                    $totaldebit += floatval($debit);
                }

                if($lejer->namakredit == null)
                {
                    $kredit = null;
                }else{
                    if($lejer->oneVar == 1)
                    {
                        $kredit = ${$lejer['variable']};
                    }else{
                        $kredit = ${$lejer['variable']}[$lejer->namakredit];
                    }

                    $totalkredit += floatval($kredit);
                }

                if(!($kredit == null && $debit == null))
                {
                    array_push($lejeram,['glcode' => $lejer->glcode, 'descpt' => $lejer->descpt,'debit' => ($debit) ? $debit : 0,'kredit' => ($kredit) ? $kredit : 0]);
                }
            }else{
                array_push($lejeram,['glcode' => $lejer->glcode, 'descpt' => $lejer->descpt,'debit' => 0,'kredit' => 0]);
            }

            if($index == $len-1)
            {
                $lejeram[0]['debit'] = $totalkredit;
                $lejeram[0]['kredit'] = $totaldebit;
            }
        }

        $totalsamadebit = 0;
        $totalsamakredit = 0;
        foreach($lejeram as $index=>$lejer)
        {
            $totalsamadebit += floatval($lejer['debit']);
            $totalsamakredit += floatval($lejer['kredit']);
        }

        return view('print.laporan_lejer_am',compact('cawangan','tarikh_cetak','cawangan_info', 'api_url','cawangan_detail','tarikh_akhir','tarikh_mula','lejeram','totalsamadebit','totalsamakredit'));
    }

    public function laporan_tuntutbaki(Request $request)
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');
        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();


        return view('khazanah.laporan.tuntutbaki', compact('cawangan_info','api_url'));
    }

    // public function cetaklaporan_tuntutbaki(Request $request){

    //     $api_url = config('api.url');
    //     $auth_user = Auth::user();
    //     $current = Carbon::now();
    //     $current = new Carbon();
    //     $tarikh_cetak = $current->toDateString();

    //     if($request->datetimepicker1 == null){
    //         $gadai_date = $current->toDateString();
    //     }else{
    //         $split_date = explode('-',$request->datetimepicker1);
    //         $gadai_date = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
    //     }

    //     $tuntutbaki = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/TuntutBakiTarikh/'. $auth_user['cawangan'] ,['tarikh' => $gadai_date])->json();

    //     $cawangan=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
    //     $cawangan_detail=$cawangan[0];

    //     return view('print.laporan_tuntutbaki', compact('tuntutbaki','cawangan_detail','tarikh_cetak','gadai_date','api_url'));
    // }

    public function cetaklaporan_tuntutbaki(Request $request){

        $api_url = config('api.url');
        $auth_user = Auth::user();
        $current = Carbon::now();
        $current = new Carbon();
        $tarikh_cetak = $current->toDateString();

        if($request->datetimepicker1 == null){
            $gadai_date = $current->toDateString();
        } else {
            $split_date = explode('-', $request->datetimepicker1);
            $gadai_date = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
        }

        // Fetching the data from API
        $tuntutbaki = Http::withOptions(['verify' => config('app.api_verify')])
            ->get(config('api.url') . '/api/TuntutBakiTarikh/' . $auth_user['cawangan'], ['tarikh' => $gadai_date])
            ->json();

        $cawangan = Http::withOptions(['verify' => config('app.api_verify')])
            ->get(config('api.url') . '/api/cawangan/detail/' . $auth_user['cawangan'])
            ->json();

        // Check if $tuntutbaki is an array and not empty before sending it to the view
        if (!is_array($tuntutbaki) || empty($tuntutbaki)) {
            $tuntutbaki = []; // If not valid, set it to an empty array
        }

        $cawangan_detail = $cawangan[0] ?? null;

        return view('print.laporan_tuntutbaki', compact('tuntutbaki', 'cawangan_detail', 'tarikh_cetak', 'gadai_date', 'api_url'));
    }





    public function laporan_Perbelanjaan(Request $request)
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');
        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();


        return view('khazanah.laporan.perbelanjaan', compact('cawangan_info','api_url'));
    }

    public function cetaklaporan_Perbelanjaan(Request $request)
    {
        $api_url = config('api.url');
        $auth_user = Auth::user();
        $current = Carbon::now();
        $current = new Carbon();
        $tarikh_cetak = $current->toDateString();

        if($request->datemula == null){
            $mula_date = '2017-01-01';
        }else{
            $split_date = explode('-', $request->datemula);
            $mula_date = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
        }

        if($request->datehingga == null){
            $hingga_date = $current->toDateString();
        }else{
            $split_date = explode('-', $request->datehingga);
            $hingga_date = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
        }

        $perbelanjaan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/SenaraiPanjar/' . $auth_user['cawangan'] . '/' . $mula_date . '/' . $hingga_date)->json();

        $cawangan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];

        return view('print.laporan_perbelanjaan', compact('perbelanjaan','cawangan_detail','tarikh_cetak','mula_date','hingga_date','api_url'));
    }

    public function cetaklaporan_Perbelanjaan_Baucer($baucer)
    {
        $api_url = config('api.url');
        $auth_user = Auth::user();
        $current = Carbon::now();
        $current = new Carbon();
        $tarikh_cetak = $current->toDateString();

        $perbelanjaan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/SenaraiPanjar/listeach/' . $auth_user['cawangan'],['nobaucer' => $baucer])->json();

        $cawangan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];

        return view('print.laporan_perbelanjaan_baucer', compact('perbelanjaan','cawangan_detail','tarikh_cetak','api_url'));
    }




    public function RequestPembatalan(Request $request)
    {
        $auth_user = Auth::user();
       $kodcaw = $request->kodcaw;
       $nombor = $request->no;
       $kodlaluan = $request->kodlaluan;

       Http::withOptions(['verify' => config('app.api_verify')])->put(config('api.url').'/api/requestTeller/batal/'.$kodcaw,[
        'no'=>$nombor,
        'kodlaluan'=>$kodlaluan
        ]);

        event(new PendahuluanSerahan("Batal","all",$auth_user['cawangan'],$kodlaluan));

        return response('Berjaya');
    }

    public function SuratAkuanGadaian(Request $request)
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');

        $cawangan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];

        $gadai = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/gadai/norujukan/' . $auth_user['cawangan'] . '/' . $request->norujukanhidden)->json();

        foreach($gadai as $index1=>$gad)
            $customers = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/customers/'.$gad['nokp'])->json();

        // $kodnegeridandaerah = $request->negeriagh .'|' . $request->daerahagh;

        // $polis = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/polis/get/',['norujukan' => $request->norujukanhidden,'kodcaw' => $auth_user['cawangan']])->json();
        $polis = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/polis/get/',['norujukan' => $request->norujukanhidden,'kodcaw' => $auth_user['cawangan'],['name' => $auth_user['name']]])->json();

        $daftar = 0;
        if(sizeof($polis) > 0)
        {
            event(new NotifikasiAGH($auth_user['cawangan'],'Tak boleh generate dah kerana sudah digenerate dah.','error',$auth_user['name']));
            $daftar = 400;
        }else{
            $daftarpolis = Http::withOptions(['verify' => config('app.api_verify')])->post(config('api.url').'/api/polis/create/',[

                'kodcaw' =>$auth_user['cawangan'],
                'norujukan' => $request->norujukanhidden,
                'komen1' => $request->alasan,
                'komen2' => 'Surat Dijana',
                'tebus' => 0,
                'teller' => $auth_user['name'],
                'caj' => 0.00,
                'kodnegeri' => $request->negeriagh,
                'koddaerah' => $request->daerahagh

            ]);

            $daftar = $daftarpolis->getStatusCode();
        }

        if($daftar == 200)
        {
            $polis = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/polis/get/',['norujukan' => $request->norujukanhidden,'kodcaw' => $auth_user['cawangan']])->json();

            event(new NotifikasiAGH($auth_user['cawangan'],'Surat Akuan Gadaian Hilang berjaya dijana.','success',$auth_user['name']));
            // return redirect()->route('khazanah.customer_info')->with('aghinformation','Surat Akuan Gadaian Hilang berjaya dijana.|success');
            return view('print.suratgadaianhilang', compact('polis','gadai','customers','cawangan_detail','api_url'));

        }else{

            event(new NotifikasiAGH($auth_user['cawangan'],'Mengalami masalah dalam menjana.','error',$auth_user['name']));
            return redirect()->route('khazanah.customer_info')->with('aghinformation','Mengalami masalah dalam menjana.|error');
        }

        return response($daftarpolis);
    }

    public function SuratAkuanGadaianUpdate(Request $request)
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');

        $cawangan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];

        $gadai = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/gadai/norujukan/' . $auth_user['cawangan'] . '/' . $request->norujukanhidden)->json();

        foreach($gadai as $index1=>$gad)
            $customers = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/customers/'.$gad['nokp'])->json();

        // $kodnegeridandaerah = $request->negeriagh .'|' . $request->daerahagh;

        $polis = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/polis/get/',['norujukan' => $request->norujukanhidden,'kodcaw' => $auth_user['cawangan']])->json();

        $daftar = 0;

            $daftarpolis = Http::withOptions(['verify' => config('app.api_verify')])->put(config('api.url').'/api/polis/edit/',[

                'kodcaw' =>$auth_user['cawangan'],
                'norujukan' => $request->norujukanhidden,
                'komen1' => $request->alasan,
                'komen2' => 'Surat dikemaskini',
                'teller' => $auth_user['name'],
                'kodnegeri' => $request->negeriagh,
                'koddaerah' => $request->daerahagh

            ]);

            $daftar = $daftarpolis->getStatusCode();

        if($daftar == 200)
        {
            $polis = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/polis/get/',['norujukan' => $request->norujukanhidden,'kodcaw' => $auth_user['cawangan']])->json();

            event(new NotifikasiAGH($auth_user['cawangan'],'Surat Akuan Gadaian Hilang berjaya dijana.','success',$auth_user['name']));
            // return redirect()->route('khazanah.customer_info')->with('aghinformation','Surat Akuan Gadaian Hilang berjaya dijana.|success');
            return view('print.suratgadaianhilang', compact('polis','gadai','customers','cawangan_detail','api_url'));

        }else{

            event(new NotifikasiAGH($auth_user['cawangan'],'Mengalami masalah dalam menjana.','error',$auth_user['name']));
            // return redirect()->route('khazanah.customer_info')->with('aghinformation','Mengalami masalah dalam menjana.|error');
        }

        return response($daftar);
    }

    public function ulangcetakSuratAkuanGadaianHilang($norujukan)
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');

        $cawangan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];

        $gadai = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/gadai/norujukan/' . $auth_user['cawangan'] . '/' . $norujukan)->json();

        foreach($gadai as $index1=>$gad)
	{
        $customers = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/customers/'.$gad['nokp'])->json();
	}
        $polis = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/polis/get/',['norujukan' => $norujukan,'kodcaw' => $auth_user['cawangan']])->json();
        $log_ulangcetakagh = Http::withOptions(['verify' => config('app.api_verify')])->put(config('api.url').'/api/polis/agh',['norujukan' => $norujukan,'kodcaw' => $auth_user['cawangan'],'teller' => $auth_user['name']])->json();

        return view('print.suratgadaianhilang', compact('polis','gadai','customers','cawangan_detail','api_url'));
    }

    public function PemilihanAnsUpah()
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');

        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();


        return view('khazanah.laporan.ansuranupah', compact('cawangan_info','api_url'));
    }

    public function LaporanUpah()
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');

        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();


        return view('khazanah.laporan.upah', compact('cawangan_info','api_url'));
    }

    public function cetaklaporan_Upah(Request $request)
    {
        $api_url = config('api.url');
        $auth_user = Auth::user();
        $current = Carbon::now();
        $current = new Carbon();
        $tarikh_cetak = $current->toDateString();

        if($request->datemula == null){
            $mula_date = '2017-01-01';
        }else{
            $split_date = explode('-', $request->datemula);
            $mula_date = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
        }

        if($request->datehingga == null){
            $hingga_date = $current->toDateString();
        }else{
            $split_date = explode('-', $request->datehingga);
            $hingga_date = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
        }

        $advansur = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/AnsuranByRangeTarikh/' . $auth_user['cawangan'],['mula' => $mula_date,'akhir' => $hingga_date])->json();

        $cawangan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];

        return view('print.laporan_upah', compact('advansur','cawangan_detail','tarikh_cetak','mula_date','hingga_date','api_url'));
    }

    public function lelongan($norujukan)
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');
        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();

        $gadaidetail = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/gadai/norujukan/'. $norujukan)->json();
        $noic = $gadaidetail[0][0]['nokp'];
        $customerdetail = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/customers/'. $noic)->json();
        $nama = $customerdetail['nama'];

        $lelong = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/lelong/cawangan/'. $auth_user['cawangan'] . '/' . $norujukan)->json();

        if(sizeof($lelong) > 0)
        {
            $lelong = $lelong[0];
        }

        $gadai = $gadaidetail[0][0];

        return view('lelongan.index-khazanah',compact('cawangan_info','api_url','gadai','nama','lelong'));
    }

    public function laporan_pendserahan()
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');

        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();


        return view('khazanah.laporan.pendahuluanserahan', compact('cawangan_info','api_url'));
    }

    public function cetaklaporan_laporan_pendserahan(Request $request)
    {

        $api_url = config('api.url');
        $auth_user = Auth::user();
        $current = Carbon::now();
        $current = new Carbon();
        $tarikh_cetak = $current->toDateString();

        if($request->datemula == null){
            $mula_date = '2017-01-01';
        }else{
            $split_date = explode('-', $request->datemula);
            $mula_date = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
        }

        if($request->datehingga == null){
            $hingga_date = $current->toDateString();
        }else{
            $split_date = explode('-', $request->datehingga);
            $hingga_date = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
        }

        $pendser = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/SenaraiPendSer/range/' . $auth_user['cawangan'],['mula' => $mula_date,'hingga' => $hingga_date,'jenis' => $request->radpendser])->json();

        $cawangan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];

        $jenisLaporan = $request->radpendser;

        return view('print.laporan_pendahuluanserahan', compact('pendser','cawangan_detail','tarikh_cetak','mula_date','hingga_date','api_url','jenisLaporan'));
    }

    public function laporanDenominasi()
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');

        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();


        return view('khazanah.laporan.denominasi', compact('cawangan_info','api_url'));
    }

    public function cetakLaporanDenominasi(Request $request)
    {

        $api_url = config('api.url');
        $auth_user = Auth::user();
        $current = Carbon::now();
        $current = new Carbon();
        $tarikh_cetak = $current->toDateString();

        if($request->datedenominasi == null){
            $date = $current->toDateString();
        }else{
            $split_date = explode('-', $request->datedenominasi);
            $date = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
        }

        $denominasi = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/DenominasiLaporan/' . $auth_user['cawangan'],['tarikh' => $date])->json();

        if(!$denominasi)
        {
            return redirect()->back()->with('errormessage', 'Tiada data pada tarikh tersebut.');
        }

        $cawangan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];

        return view('print.laporan_denominasi', compact('denominasi','cawangan_detail','tarikh_cetak','date','api_url'));
    }

    public function laporan_senaraipetibesi()
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');

        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();


        return view('khazanah.laporan.senaraipetibesi', compact('cawangan_info','api_url'));
    }

    public function cetaklaporan_senaraipetibesi(Request $request)
    {

        $api_url = config('api.url');
        $auth_user = Auth::user();
        $current = Carbon::now();
        $current = new Carbon();
        $tarikh_cetak = $current->toDateString();

        if($request->datemula == null){
            $mula_date = '2017-01-01';
        }else{
            $split_date = explode('-', $request->datemula);
            $mula_date = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
        }

        if($request->datehingga == null){
            $hingga_date = $current->toDateString();
        }else{
            $split_date = explode('-', $request->datehingga);
            $hingga_date = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
        }

        $petibesi = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/laporanpetibesirange/' . $auth_user['cawangan'],['mula' => $mula_date,'hingga' => $hingga_date])->json();

        $cawangan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];

        return view('print.laporan_senaraipetibesi', compact('petibesi','cawangan_detail','tarikh_cetak','mula_date','hingga_date','api_url'));
    }

    public function laporanhargaemas()
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');

        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();

        return view('khazanah.laporan.hargaemas', compact('cawangan_info','api_url'));
    }

    public function cetakLaporanhargaemas(Request $request)
    {

        $api_url = config('api.url');
        $auth_user = Auth::user();
        $current = Carbon::now();
        $current = new Carbon();
        $tarikh_cetak = $current->toDateString();

        if($request->datemula == null){
            $mula_date = '2017-01-01';
        }else{
            $split_date = explode('-', $request->datemula);
            $mula_date = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
        }

        if($request->datehingga == null){
            $hingga_date = $current->toDateString();
        }else{
            $split_date = explode('-', $request->datehingga);
            $hingga_date = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
        }

        $petibesi = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/harga_emas/laporan/sandaran/',['mula' => $mula_date,'hingga' => $hingga_date])->json();

        $cawangan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];

        return view('print.laporan_hargaemas', compact('petibesi','cawangan_detail','tarikh_cetak','mula_date','hingga_date','api_url'));
    }


    public function laporanAuditTrail()
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');

        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();


        return view('khazanah.laporan.audittrail', compact('cawangan_info','api_url'));
    }

    public function cetakLaporanAuditTrail(Request $request)
    {

        $api_url = config('api.url');
        $auth_user = Auth::user();
        $current = Carbon::now();
        $current = new Carbon();
        $tarikh_cetak = $current->toDateString();

        if($request->dateaudit == null){
            $date = $current->toDateString();
        }else{
            $split_date = explode('-', $request->dateaudit);
            $date = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
        }

        $audittrail = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/laporanAuditTrail/',['tarikh' => $date,'kodcaw' => Auth::user()->cawangan])->json();

        if(!$audittrail)
        {
            return redirect()->back()->with('errormessage', 'Tiada data pada tarikh tersebut.');
        }

        $cawangan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];

        return view('print.laporan_audittrail', compact('audittrail','cawangan_detail','tarikh_cetak','date','api_url'));
    }

    public function laporanWakil()
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');

        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
        $lookwakil = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/lookupwakil/')->json();

        return view('khazanah.laporan.wakil', compact('cawangan_info','api_url','lookwakil'));
    }

    public function cetakLaporanWakil(Request $request)
    {

        $api_url = config('api.url');
        $auth_user = Auth::user();
        $current = Carbon::now();
        $current = new Carbon();
        $tarikh_cetak = $current->toDateString();

        // if($request->dateaudit == null){
        //     $date = $current->toDateString();
        // }else{
        //     $split_date = explode('-', $request->dateaudit);
        //     $date = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
        // }


        $wakil = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/getwakil/' . Auth::user()->cawangan . '/status/hubungan/',['hubungan' => $request->slthubungan])->json();

        $cawangan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];

        return view('print.laporan_wakil', compact('wakil','cawangan_detail','tarikh_cetak','api_url'));
    }

    public function laporanSAGHilang()
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');

        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
        $loopalasan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/lookupalasan/')->json();

        return view('khazanah.laporan.saghilang', compact('cawangan_info','api_url','loopalasan'));
    }

    public function cetakLaporanSAGHilang(Request $request)
    {
        // return $request->all();
        // $request->validate([
        //     'mula_date' => 'required|date_format:Y-m-d',
        //     'hingga_date' => 'required|date_format:Y-m-d|after_or_equal:mula_date',
        // ]);

        $api_url = config('api.url');
        $auth_user = Auth::user();
        $current = Carbon::now();
        $current = new Carbon();
        $tarikh_cetak = $current->toDateString();

        // if($request->datesaghilang == null){
        //     $date = $current->toDateString();
        // }else{
        //     $split_date = explode('-', $request->datesaghilang);
        //     $date = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
        // }

        if($request->mula_date == null){
            $mula_date = $current->toDateString();
        }else{
            $split_date = explode('-', $request->mula_date);
            $mula_date = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
        }

        if($request->hingga_date == null){
            $hingga_date = $current->toDateString();
        }else{
            $split_date = explode('-', $request->hingga_date);
            $hingga_date = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
        }

        $polis = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/polis/laporan/',['kodcaw' => $auth_user['cawangan'], 'alasan' => $request->sltalasan, 'tarikh_mula' => $mula_date, 'tarikh_akhir' => $hingga_date])->json();

        $cawangan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];

        return view('print.laporan_saghilang', compact('polis','cawangan_detail','mula_date','hingga_date','tarikh_cetak','api_url'));
    }

    public function NotisLelong()
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');

        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();

        $cawangan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];

        // $loopalasan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/lookupalasan/')->json();

        return view('print.notislelong', compact('cawangan_info','api_url','cawangan_detail'));
    }

    public function PaparanPermohonan()
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');

        $current = Carbon::now();
        $tarikhsemasa = $current->toDateString();


        $cawangan_info = Http::get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();

        $getVault = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/TunaiDiTangan/' . $auth_user['cawangan'],['tarikh' => $tarikhsemasa])->json();



        //Dashboardall
        $dashboard=Http::withOptions(['verify' => config('app.api_verify')])->get( config('api.url').'/api/Dashboard_all/'.$auth_user['cawangan'])->json();

        $permohonan = Http::withOptions(['verify' => config('app.api_verify')])->get( config('api.url').'/admin/permohonan/get/'.$auth_user['cawangan'])->json();
        $bank = Http::withOptions(['verify' => config('app.api_verify')])->get( config('api.url').'/api/bank')->json();

        $denominasi=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/DenominasiController/'.$auth_user['cawangan'])->json();

        if(sizeof($denominasi) == 0)
        {
            $denominasi=Http::withOptions(['verify' => config('app.api_verify')])->post(config('api.url').'/api/DenominasiCreate/'.$auth_user['cawangan'])->json();
        }
        else
        {
            $denominasi=$denominasi[0];
        }
        return view('khazanah.permohonanwang', compact('bank','cawangan_info','api_url','getVault','tarikhsemasa','dashboard','permohonan','denominasi'));
    }

    public function RekodPermohonan()
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');

        $current = Carbon::now();

        $cawangan_info = Http::get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
        $permohonan = Http::withOptions(['verify' => config('app.api_verify')])->get( config('api.url').'/admin/permohonan/rekod/'.$auth_user['cawangan'])->json();

        return view('khazanah.rekodpermohonan', compact('cawangan_info','api_url','permohonan'));
    }


    public function SimpanPermohonan(Request $request){

        // dd($request->all());
        $auth_user = Auth::user();
        $api_url = config('api.url');

        $permohonan = Http::withOptions(['verify' => config('app.api_verify')])->post(config('api.url') . '/admin/permohonan/add/'. $auth_user['cawangan'],[
            'jenismohon' => $request->radwang,
            'jumlahmohon' => $request->jumlahmohon,
            'tarikhmohon' => $request->tarikhmohon,
            'bakipetibesi' => $request->bakipetibesi,
            'outstanding' => $request->outstanding,
            'namapemohon' => $auth_user['name'],
            'jawatanpemohon' => $auth_user['roles']
        ])->json();

        if($permohonan == true)
        {
            return redirect()->back()->with('message', 'Permohonan Berjaya Disimpan!');
        }else{
            return redirect()->back()->with('error', 'Permohonan Tidak Berjaya!');
        }
    }

    public function StatusPermohonan($mohonid){

        $auth_user = Auth::user();
        $api_url = config('api.url');

        $cawangan_info = Http::get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();

        $permohonan = Http::withOptions(['verify' => config('app.api_verify')])->get( config('api.url').'/admin/permohonan/get/'.$auth_user['cawangan'] .'/' . $mohonid)->json();

        $getdescription = Http::withOptions(['verify' => config('app.api_verify')])->get( config('api.url').'/admin/permohonan/description/' . $mohonid)->json();

        return view('khazanah.status', compact('cawangan_info','api_url','permohonan','getdescription'));

    }

    public function CITPermohonan(Request $request)
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');


        $resitname = time().'.'.$request->resit->extension();
        $resitdata = base64_encode($request->resit->get());

        $invoicename = time().'.'.$request->invoice->extension();
        $invoicedata = base64_encode($request->invoice->get());

        $updatelulus = Http::accept('application/'.$request->resit->extension() . ',application/'.$request->invoice->extension())->post(config('api.url') . '/admin/permohonan/tagcit',[
            'resitcit' => $request->noresit,
            'bank' => $request->selectedbank,
            'mohonid' => $request->mohonid,
            'dataresit' => $resitdata,
            'datainvoice' => $invoicedata,
            'resittype' => $request->resit->extension(),
            'invoicetype' => $request->invoice->extension(),
            'resitname' => $resitname,
            'invoicename' => $invoicename,

        ])->json();

        if($updatelulus)
            return redirect()->back()->with('message', 'Berjaya mengemaskini CIT Permohonan!');
        else
        return redirect()->back()->with('error', 'Ralat dalam menjana sebarang kemaskini!');

    }

    public function DownloadFiles(Request $request){

        $download = Http::get(config('api.url').'/admin/permohonan/viewfiles',[
            'files_id' => $request->files_id
        ])->json();

        return response(base64_decode($download['data']), 200,  [
            'Content-type' => 'application/' . $download['type'],
            'Content-Disposition' => 'inline; filename='.$download['filename']
        ]);
    }

    public function JanaPermohonan(Request $request){
        $auth_user = Auth::user();
        $api_url = config('api.url');

        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
        $cawangan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail = $cawangan[0];

        $permohonan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/admin/permohonan/get/'.$auth_user['cawangan'] . '/' . $request->mohonid)->json();

        $caw = CawanganAR::where('kodcaw',$auth_user['cawangan'])->get()->toArray();
        $jeniscawangan = $caw[0]['syarikat'];

        $current = Carbon::now();
        $tarikh_cetak = $current->toDateString();

        $ahli = '';
        $fullname = '';
        switch($jeniscawangan){
            case 'PKB' : $fullname = 'PERMODALAN KELANTAN BERHAD';
            break;
            case 'SYIRKAH' : $fullname = 'JOINT VENTURE PKB AND KRKB';
            break;
            case 'KPKB' : $fullname = 'JOINT VENTURE PKB AND KPKB';
            break;
            case 'BMMB' : $fullname = 'JOINT VENTURE MVSB AND PKB';
                          $ahli = 'Ahli JMC';
            break;
        }

        $myDateTime = DateTime::createFromFormat('Y-m-d', $tarikh_cetak);
        $tarikhcetakan = $myDateTime->format('d-m-Y');

        $ringgittext = '';
        if($permohonan['jumlah_diluluskan'] !== null)
        {

            $decones = array(
                '01' => "Satu",
                '02' => "Dua",
                '03' => "Tiga",
                '04' => "Empat",
                '05' => "Lima",
                '06' => "Enam",
                '07' => "Tujuh",
                '08' => "Lapan",
                '09' => "Sembilan",
                10 => "Sepuluh",
                11 => "Sebelas",
                12 => "Dua Belas",
                13 => "Tiga Belas",
                14 => "Empat Belas",
                15 => "Lima Belas",
                16 => "Enam Belas",
                17 => "Tujuh Belas",
                18 => "Lapan Belas",
                19 => "Sembilan Belas"
                );
            $ones = array(
                        0 => " ",
                        1 => "Satu",
                        2 => "Dua",
                        3 => "Tiga",
                        4 => "Empat",
                        5 => "Lima",
                        6 => "Enam",
                        7 => "Tujuh",
                        8 => "Lapan",
                        9 => "Sembilan",
                        10 => "Sepuluh",
                        11 => "Sebelas",
                        12 => "Dua Belas",
                        13 => "Tiga Belas",
                        14 => "Empat Belas",
                        15 => "Lima Belas",
                        16 => "Enam Belas",
                        17 => "Tujuh Belas",
                        18 => "Lapan Belas",
                        19 => "Sembilan Belas"
                        );
            $tens = array(
                        0 => "",
                        2 => "Dua Puluh",
                        3 => "Tiga Puluh",
                        4 => "Empat Puluh",
                        5 => "Lima Puluh",
                        6 => "Enam Puluh",
                        7 => "Tujuh Puluh",
                        8 => "Lapan Puluh",
                        9 => "Sembilan Puluh"
                        );
            $hundreds = array(
                        "Ratus",
                        "Ribu",
                        "Juta",
                        "bilion",
                        "trilion"
                        ); //limit t quadrillion

            $num = number_format($permohonan['jumlah_diluluskan'],2,".",",");
            // dd($num);
            $num_arr = explode(".",$num);
            $wholenum = $num_arr[0];
            $decnum = $num_arr[1];
            $whole_arr = array_reverse(explode(",",$wholenum));
            krsort($whole_arr);
            $ringgittext = "";



            foreach($whole_arr as $key => $i){
                if($i > 0 && $i < 20){
                    $ringgittext .= $ones[$i];
                }
                elseif($i < 100){
                    $ringgittext .= $tens[substr($i,0,1)];
                    $ringgittext .= " ".$ones[substr($i,1,1)];
                }
                else{
                    $ringgittext .= $ones[substr($i,0,1)]." ".$hundreds[0];
                    $ringgittext .= " ".$tens[substr($i,1,1)];
                    $ringgittext .= " ".$ones[substr($i,2,1)];
                }
                if($key > 0){
                    $ringgittext .= " ".$hundreds[$key]." ";
                }
            }

            $text = preg_replace('/\s+/', ' ', $ringgittext);
            $trimmed_str = rtrim($text);

            $ringgittext = $trimmed_str." Ringgit";

            if($decnum > 0){
                $ringgittext .= " dan ";
                if($decnum < 20){
                    $ringgittext .= $decones[$decnum];
                }
                elseif($decnum < 100){
                    $ringgittext .= $tens[substr($decnum,0,1)];
                    $ringgittext .= " ".$ones[substr($decnum,1,1)];
                }
                $ringgittext = $ringgittext." sen";
            }

        }


        return view('print.jana_permohonan', compact('cawangan_detail','api_url','permohonan','jeniscawangan','fullname','tarikhcetakan','ringgittext'));
    }

    public function ProsesPermohonan(Request $request)
    {
        $imageName = 'JanaanPC_' .time().'.'.$request->image->extension();
        $data = base64_encode($request->image->get());

        $updatelulus = Http::accept('application/'.$request->image->extension())->post(config('api.url') . '/admin/permohonan/updatejanapc',[
            'status' => $request->status,
            'dataimage' => $data,
            'imagename' => $imageName,
            'imagetype' => $request->image->extension(),
            'mohonid' => $request->mohonid
        ])->json();


        if($updatelulus)
        {
            return response('success');
        }else{
            return response('error');
        }


    }

    public function TuntutBakiByBatchNo(Request $request)
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');

        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();

        $cawangan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];

        $current = Carbon::now();
        $current = new Carbon();

        $lelong = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/senaraituntutbaki/aktif_batch/'. $auth_user['cawangan'])->json();

        return view('khazanah.tuntutbaki_notis', compact('cawangan_detail','api_url','lelong','cawangan_info'));

    }

    public function NotisTuntutBaki(Request $request)
    {

        $auth_user = Auth::user();
        $api_url = config('api.url');

        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();

        $cawangan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];

        $current = Carbon::now();
        $current = new Carbon();
        $tarikh_cetak = $current->toDateString();

        $taxation = config('app.taxation');

        $jenis = $request->jeniscetakan;
        $batchno = $request->batchno;

        $lelong = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/senaraituntutbaki/laporan/'. $auth_user['cawangan'],[
            'jenis' => $jenis,
            'batchno' => $batchno
        ])->json();

        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
        $cawangan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];

        if($jenis == 'tuntutbaki')
        {
            $pdf=PDF::loadView('print.notistuntutbaki', compact('cawangan_info','api_url','cawangan_detail','lelong','tarikh_cetak','taxation'))->setPaper('A4','potrait');
            return $pdf->download('notistuntutbaki_'.$tarikh_cetak.'_'.$auth_user['cawangan'] . '_' . $batchno.'.pdf');
        }else{
            $pdf=PDF::loadView('print.notisrugi', compact('cawangan_info','api_url','cawangan_detail','lelong','tarikh_cetak','taxation'))->setPaper('A4','potrait');
            return $pdf->download('notisrugi_'.$tarikh_cetak.'_'.$auth_user['cawangan'] . '_' . $batchno.'.pdf');
        }

    }

    public function Laporan_SerahPetiBesiGadai()
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');

        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
        // $lookwakil = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/lookupwakil/')->json();

        return view('khazanah.laporan.serahgadai', compact('cawangan_info','api_url'));
    }

    public function cetak_serahBarcodeGadai(Request $request)
    {
        $api_url = config('api.url');
        $auth_user = Auth::user();
        $current = Carbon::now();
        $current = new Carbon();
        $tarikh_cetak = $current->toDateString();

        if($request->datemula == null){
            $datemula = $current->toDateString();
        }else{
            $split_date = explode('-', $request->datemula);
            $datemula = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
        }

        if($request->datehingga == null){
            $datehingga = $current->toDateString();
        }else{
            $split_date = explode('-', $request->datehingga);
            $datehingga = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
        }


        $serahbarcode = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/admin/laporan/serahgadai/',[
            'kodcaw' => $auth_user['cawangan'],
            'mula' => $datemula,
            'hingga' => $datehingga,
        ])->json();


        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
        $cawangan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];

        return view('print.laporan_serahpetibesigadai', compact('cawangan_detail','api_url','serahbarcode','cawangan_info','datemula','datehingga','tarikh_cetak'));
    }


    public function Laporan_SerahMarhunTebus()
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');

        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
        // $lookwakil = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/lookupwakil/')->json();

        return view('khazanah.laporan.serahmarhun', compact('cawangan_info','api_url'));
    }


    public function cetak_serahMarhunTebus(Request $request)
    {
        $api_url = config('api.url');
        $auth_user = Auth::user();
        $current = Carbon::now();
        $current = new Carbon();
        $tarikh_cetak = $current->toDateString();

        if($request->datemula == null){
            $datemula = $current->toDateString();
        }else{
            $split_date = explode('-', $request->datemula);
            $datemula = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
        }

        if($request->datehingga == null){
            $datehingga = $current->toDateString();
        }else{
            $split_date = explode('-', $request->datehingga);
            $datehingga = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
        }


        $serahmarhun = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/admin/laporan/serahmarhuntebus/',[
            'kodcaw' => $auth_user['cawangan'],
            'mula' => $datemula,
            'hingga' => $datehingga,
        ])->json();

        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
        $cawangan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];

        return view('print.laporan_serahmarhun', compact('cawangan_detail','api_url','serahmarhun','cawangan_info','datemula','datehingga','tarikh_cetak'));
    }

    public function Laporan_AuditCustomer()
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');

        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();

        return view('khazanah.laporan.auditcustomer', compact('cawangan_info','api_url'));
    }

    public function cetak_AuditCustomer(Request $request)
    {
        $api_url = config('api.url');
        $auth_user = Auth::user();
        $current = Carbon::now();
        $current = new Carbon();
        $tarikh_cetak = $current->toDateString();

        if($request->datemula == null){
            $datemula = $current->toDateString();
        }else{
            $split_date = explode('-', $request->datemula);
            $datemula = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
        }

        if($request->datehingga == null){
            $datehingga = $current->toDateString();
        }else{
            $split_date = explode('-', $request->datehingga);
            $datehingga = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
        }

        $auditcustomer = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/admin/laporan/auditcustomer/',[
            'kodcaw' => $auth_user['cawangan'],
            'mula' => $datemula,
            'hingga' => $datehingga
        ])->json();

        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
        $cawangan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];

        return view('print.laporan_auditcustomer', compact('cawangan_detail','api_url','auditcustomer','cawangan_info','datemula','datehingga','tarikh_cetak'));
    }

    public function LaporanTebusBarcode(){
        $auth_user=Auth::user();
        $current = Carbon::now();
        $current = new Carbon();
        $api_url = config('api.url');
        $tarikh_cetak = $current->toDateString();

        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
        $cawangan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];

        $datemula = $current->toDateString();
        $datehingga = $current->toDateString();

        $serahmarhun = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/admin/laporan/serahmarhuntebus/',[
            'kodcaw' => $auth_user['cawangan'],
            'mula' => $datemula,
            'hingga' => $datehingga,
        ])->json();

        return view('print.laporan_serahmarhun', compact('cawangan_detail','api_url','serahmarhun','cawangan_info','datemula','datehingga','tarikh_cetak'));
    }

    public function ImbangDugaAkru(){
        $auth_user = Auth::user();
        $api_url = config('api.url');

        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();

        return view('khazanah.laporan.imbangduga_akru', compact('cawangan_info','api_url'));
    }

    public function cetakimbangdugaAkru(Request $request){

        $auth_user = Auth::user();
        $api_url = config('api.url');
        $current = Carbon::now();
        $current = new Carbon();

        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
        $cawangan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];

        $tarikh_cetak = $current->toDateString();

        $tarikh_imbangan= $current->toDateString();
        $tarikh_imbangan2= $current->toDateString();

        $tarikh_cetak = $current->toDateString();

        if($request->datetimepicker2 == null){
            $tarikh_cetak = $current->toDateString();
        }else{
            $split_date = explode('-',$request->datetimepicker2);
            $tarikh_imbangan2 = $request->datetimepicker2;
        }

        $tarikh_imbangan2 = new Carbon($tarikh_imbangan2);

        // if($tarikh_imbangan2 < $current->toDateString()){

        if($tarikh_imbangan2 < $current->toDateString()){
            $response = (new ImbanganDuga())->GetImbangDugaBackup($auth_user['cawangan'],$request->datetimepicker2);
        }else{
            $response = (new ImbanganDuga())->ViewImbangDugaAROS($auth_user['cawangan'],$tarikh_imbangan2);
        }

        if($response->getContent() == 'notfound')
        {
            return redirect()->route('laporan.imbangdugaakru')->with('error','Tiada Rekod Dijumpai Dalam Imbang Duga Backup!');
        }

        $array_ig = json_decode($response->getContent());
        $hartasemasa = $array_ig->array_harta;
        $tanggungansemasa = $array_ig->array_tanggungan;
        $totalhartasemasa = $array_ig->sum_harta;
        $totaltanggungansemasa = $array_ig->sum_tanggungan;

        if($this->floatvalue(number_format($totalhartasemasa,2)) != $this->floatvalue(number_format($totaltanggungansemasa,2))){
            return redirect()->route('laporan.imbangdugaakru')->with('error','Imbangduga tidak sama. Sila berhubung dengan pihak IT');
        }

        return view('print.laporan_imbangduga-akru',compact('tarikh_imbangan', 'tarikh_imbangan2','hartasemasa','tanggungansemasa','totalhartasemasa','totaltanggungansemasa','tarikh_cetak','cawangan_info','cawangan','api_url','cawangan_detail'));
    }

    public function laporanStokAuditCombine(){
        $auth_user = Auth::user();
        $api_url = config('api.url');

        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
        $stokaudit = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/gadai/cawangan/'. $auth_user['cawangan'])->json();

        return view('khazanah.laporan.stokauditcombine',compact('cawangan_info','api_url'));
    }

    public function cetakLaporanStokAuditCombine(Request $request){

        $api_url = config('api.url');
        $auth_user = Auth::user();
        $current = Carbon::now();
        $current = new Carbon();
        $tarikh_cetak = $current->toDateString();

        if($request->datetimepicker1 == null){
            $stokaudit_date = $current->toDateString();
        }else{
            $split_date = explode('-',$request->datetimepicker1);
            $stokaudit_date = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
        }

        $cawangan=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];

        $stokaudit_aktif = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/stokaudit/aktif/'. $auth_user['cawangan'],['tarikh' => $stokaudit_date])->json();
        $stokaudit_takaktif = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/stokaudit/takaktif/'. $auth_user['cawangan'],['tarikh' => $stokaudit_date])->json();

        // return view('print.laporan_stokauditbelumaktif', compact('stokaudit','cawangan_detail','tarikh_cetak','stokaudit_date','api_url'));

        return view('print.laporan_stokauditcombine', compact('stokaudit_aktif','stokaudit_takaktif','cawangan_detail','tarikh_cetak','stokaudit_date','api_url'));
    }

    public function laporanGadaianUjrah (){

        $auth_user = Auth::user();
        $api_url = config('api.url');

        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/' . $auth_user['cawangan'])->json();

        return view('khazanah.laporanujrah.gadaianujrah', compact('auth_user','api_url','cawangan_info'));

    }

    public function cetakLaporanGadaiUjrah(Request $request){

        $api_url = config('api.url');
        $auth_user = Auth::user();
        $current = Carbon::now();
        $current = new Carbon();
        $tarikh_cetak = $current->toDateString();

        if($request->datemula == null){
            $mula_date = $current->toDateString();
        }else{
            $split_date = explode('-', $request->datemula);
            $mula_date = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
        }

        if($request->datehingga == null){
            $hingga_date = $current->toDateString();
        }else{
            $split_date = explode('-', $request->datehingga);
            $hingga_date = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
        }


        $gadai = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/gadailaporanRangeDate/'. $auth_user['cawangan'] ,['mula' => $mula_date,'akhir' => $hingga_date])->json();
        $cawangan=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];

        return view('print.laporan_gadaian_ujrah', compact('gadai','cawangan_detail','tarikh_cetak','mula_date','api_url','hingga_date'));
    }


    public function laporanPenebusanUjrah(){

        $auth_user = Auth::user();
        $api_url = config('api.url');
        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
        return view('khazanah.laporanujrah.tebusujrah',compact('cawangan_info','api_url'));
    }

    public function cetakLaporanPenebusanUjrah(Request $request){
        // dd($request);
        $api_url = config('api.url');
        $auth_user = Auth::user();
        $current = Carbon::now();
        $current = new Carbon();

        $tarikh_cetak = $current->toDateString();

        if($request->datemula == null){
            $mula_date = $current->toDateString();
        }else{
            $split_date = explode('-', $request->datemula);
            $mula_date = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
        }

        if($request->datehingga == null){
            $hingga_date = $current->toDateString();
        }else{
            $split_date = explode('-', $request->datehingga);
            $hingga_date = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
        }

        $tebus = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/tebusujrah/laporan/'. $auth_user['cawangan']  ,['mula' => $mula_date,'akhir' => $hingga_date])->json();
        $totalkeuntunganrebet = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/tebusujrah/laporan/'. $auth_user['cawangan']  ,['mula' => $mula_date,'akhir' => $hingga_date])->json();
        $tebuspengasingan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/tebus/laporanjenistawarruqujrah/'. $auth_user['cawangan'] ,['mula' => $mula_date,'akhir' => $hingga_date])->json();
        $upahlama = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/advansurdetailsrange/'. $auth_user['cawangan'],['mula' => $mula_date,'akhir' => $hingga_date])->json();
        $ansuran = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/ansuransummaryrange/'. $auth_user['cawangan'],['mula' => $mula_date,'akhir' => $hingga_date])->json();

        $cawangan=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];
        return view('print.laporan_tebus_ujrah', compact('tebus','cawangan_detail','tarikh_cetak','mula_date','hingga_date','tebuspengasingan','api_url','upahlama','ansuran','totalkeuntunganrebet'));
    }

    public function laporanAnsuranUjrah(){
        $auth_user = Auth::user();
        $api_url = config('api.url');

        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
        return view('khazanah.laporanujrah.ansuranujrah',compact('cawangan_info','api_url'));
    }

    public function cetakLaporanAnsuranUjrah(Request $request){
        // dd($request);
        $api_url = config('api.url');
        $auth_user = Auth::user();
        $current = Carbon::now();
        $current = new Carbon();
        $tarikh_cetak = $current->toDateString();

        if($request->datemula == null){
            $mula_date = $current->toDateString();
        }else{
            $split_date = explode('-', $request->datemula);
            $mula_date = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
        }

        if($request->datehingga == null){
            $hingga_date = $current->toDateString();
        }else{
            $split_date = explode('-', $request->datehingga);
            $hingga_date = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];
        }

        $tebus = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/ansuranujrah/laporan/'. $auth_user['cawangan'],['mula' => $mula_date,'akhir' => $hingga_date])->json();
        $cawangan=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];
        return view('print.laporan_ansuran_ujrah', compact('tebus','cawangan_detail','tarikh_cetak','mula_date','hingga_date','api_url'));
    }

    public function laporanStokAuditKhas(){
        $auth_user = Auth::user();
        $api_url = config('api.url');


        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();

        return view('khazanah.laporan.stokauditkhas',compact('cawangan_info','api_url'));
    }

    public function cetakLaporanStokAuditKhas(Request $request){
        $api_url = config('api.url');
        $auth_user = Auth::user();
        $current = Carbon::now();
        $current = new Carbon();
        $tarikh_cetak = $current->toDateString();
        $daritempoh = $request->daritempoh;
        $hinggatempoh = $request->hinggatempoh;
        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();

        $cawangan=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];

        if($request->radaktif === 'aktif')
        {
            $stokaudit = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/stokauditkhas/aktif/'. $auth_user['cawangan'],
            ['daritempoh' => $daritempoh, 'hinggatempoh' => $hinggatempoh])->json();
            return view('print.laporan_stokauditkhas', compact('stokaudit','cawangan_detail','tarikh_cetak','daritempoh','hinggatempoh','api_url','cawangan_info'));
        }
    }

    public function cetakExportLaporanStokAuditKhas(Request $request){
        // dd($request->all());
        $api_url = config('api.url');
        $auth_user = Auth::user();
        $current = Carbon::now();
        $current = new Carbon();
        $tarikh_cetak = $current->toDateString();
        $daritempoh = $request->daritempoh;
        $hinggatempoh = $request->hinggatempoh;
        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();

        $cawangan=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];

        $stokaudit = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/stokauditkhas/aktif/'. $auth_user['cawangan'],['daritempoh' => $daritempoh, 'hinggatempoh' => $hinggatempoh])->json();

        if($request->type == 'excel')
        {
            return view('excel.excel_stokauditkhas', compact('stokaudit','cawangan_detail','tarikh_cetak','daritempoh','hinggatempoh','api_url','cawangan_info'));
        }
        elseif($request->type == 'whatsapp')
        {

            $blastwhatsapp = Http::withOptions(['verify' => config('app.api_verify')])->post(config('api.url').'/api/blastwasapbysag/'.$auth_user['cawangan'],
            ['daritempoh' => $daritempoh, 'hinggatempoh' => $hinggatempoh, 'namapengguna' => $auth_user['name'],])->json();
            if($blastwhatsapp == 'success'){
                return redirect()->route('laporan.stokauditkhas')
                ->with('success','Blast Whatsapp Dalam Proses.');
            }
            elseif($blastwhatsapp == 'failed'){
                return redirect()->route('laporan.stokauditkhas')
                ->with('failed','Blast Whatsapp Tidak Berjaya, Sila Semak Semula Rekod Blasted.');
            }

        }
        else
        {
            $stokaudit = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/stokauditkhas/aktif/'. $auth_user['cawangan'],['daritempoh' => $daritempoh, 'hinggatempoh' => $hinggatempoh])->json();
            return view('pdf.pdf_stokauditkhas', compact('stokaudit','cawangan_detail','tarikh_cetak','daritempoh','hinggatempoh','api_url','cawangan_info'));

        }
    }

    public function rekodblast(Request $request){
        $auth_user = Auth::user();
        $api_url = config('api.url');


        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
        $rekodblast = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/rekodblast/'. $auth_user['cawangan'])->json();
        return view('khazanah.laporan.rekodblast',compact('cawangan_info','api_url','rekodblast'));
    }

    public function rekodblastexport(Request $request){
        $auth_user = Auth::user();
        $api_url = config('api.url');
        $current = Carbon::now();
        $current = new Carbon();
        $tarikh_cetak = $current->toDateString();

        $cawangan=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];

        if($request->type == 'pdf')
        {
            $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
            $rekodblast = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/rekodblast/'. $auth_user['cawangan'])->json();
            return view('pdf.pdf_rekodblast', compact('cawangan_info','api_url','rekodblast','cawangan_detail','tarikh_cetak'));
        }
    }

    public function rekodblastsms(Request $request){
        $auth_user = Auth::user();
        $api_url = config('api.url');


            $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
            $rekodblastsms = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/rekodblastsms/'. $auth_user['cawangan'])->json();
            return view('khazanah.laporan.rekodblastsms',compact('cawangan_info','api_url','rekodblastsms'));
    }

    public function rekodblastsmsexport(Request $request){
        $auth_user = Auth::user();
        $api_url = config('api.url');
        $current = Carbon::now();
        $current = new Carbon();
        $tarikh_cetak = $current->toDateString();

        $cawangan=Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];

        if($request->type == 'pdf')
        {
            $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
            $rekodblastsms = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/rekodblastsms/'. $auth_user['cawangan'])->json();
            return view('pdf.pdf_rekodblastsms', compact('cawangan_info','api_url','rekodblastsms','cawangan_detail','tarikh_cetak'));
        }
    }

    public function notisperingatan(Request $request){
        $auth_user = Auth::user();
        $api_url = config('api.url');
        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();

        return view('callcenter.khazanah.notisperingatan',compact('cawangan_info','api_url'));
    }

    public function rekodnotisperingatanpertama()
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');

        $arrayinfo = [];

        $notis = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/cawangan/senarainotisperingatan/'. $auth_user['cawangan'])->json();
        // dd($notis );
        foreach($notis as $info)
        {
            $getcawnotis = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/cawangan/getcawnotis/'. $auth_user['cawangan'].'/'.$info['notis_id'])->json();
            // dd($getcawnotis);
            array_push($arrayinfo, ['info'=> $info,'cawnotis' => $getcawnotis]);
        }
        return view('callcenter.khazanah.rekodnotisperingatanpertama',compact('notis','arrayinfo'));
    }

    public function detailnotisperingatan()
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');
        $kodcaw = $auth_user['cawangan'];
        $senarai = [];

        $detailnotis = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/cawangan/getcawnotisall/'. $auth_user['cawangan'])->json();
        // dd($detailnotis);
        foreach($detailnotis as $info)
        {
            // $getcawnotisbycaw = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/arcc/getcawnotisbycaw', [
            //     'kodcaw' => $info[0]['kodcaw'],
            //     'notisid' => $notisid
            // ])->json();

            $data = [
                // 'detailnotis' => $info ? $info : '',
                'getcawnotisbycaw' => $info ? $info : '',
            ];

            array_push($senarai,$data);
            // dd($data);
        }
        return view('callcenter.khazanah.detailnotisperingatan',compact('detailnotis','senarai','kodcaw'));
    }

    public function bycawnotisperingatan($kodcaw,$notisid, Request $request)
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');
        $kodcaw = $auth_user['cawangan'];
        $cawangan_info = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/cawangan/'.$auth_user['cawangan'])->json();
        $bycawnotisperingatan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/cawangan/bycawnotisperingatan/'.$auth_user['cawangan'].'/'.$notisid)->json();

        return view('callcenter.khazanah.bycawdetailnotisperingatan',compact('cawangan_info','bycawnotisperingatan','kodcaw','notisid','api_url'));
    }

    function floatvalue($val){
        $val = str_replace(",",".",$val);
        $val = preg_replace('/\.(?=.*\.)/', '', $val);
        return floatval($val);
    }


    public function AutoSerahan(Request $request)
    {
        if(isset($request->do_delete) && $request->do_delete == true){
            $call_approval_v3 = Http::withOptions(['verify' => config('app.api_verify')])
            ->post(config('api.url_koop') . '/api/autoserahan/delete', [
                'recno' => $request->recno,
                'version' => 'v3',
                'kodcaw' => $request->kodcaw,
                'teller' => $request->teller,
                'amaun' => $request->amaun,
            ])->json();

            return response()->json([
                'status' => 'success',
                'message' => 'Approval process completed successfully',
                'id' => $call_approval_v3['id'],
            ]);

        }

        // Step 1: Call API v3 First
        $call_approval_v3 = Http::withOptions(['verify' => config('app.api_verify')])
            ->post(config('api.url_koop') . '/api/autoserahan/create', [
                'kodcaw' => $request->arraysend['kodcaw'],
                'teller' => $request->arraysend['teller'],
                'amaun' => $request->arraysend['amaun'],
                'txtring1' => $request->arraysend['ambilanring1'],
                'txtring5' => $request->arraysend['ambilanring5'],
                'txtring10' => $request->arraysend['ambilanring10'],
                'txtring20' => $request->arraysend['ambilanring20'],
                'txtring50' => $request->arraysend['ambilanring50'],
                'txtring100' => $request->arraysend['ambilanring100'],
                'txtsen1' => $request->arraysend['ambilansen1'],
                'txtsen5' => $request->arraysend['ambilansen5'],
                'txtsen10' => $request->arraysend['ambilansen10'],
                'txtsen20' => $request->arraysend['ambilansen20'],
                'txtsen50' => $request->arraysend['ambilansen50'],
                'jenis' => 'pendahuluan',
                'action' => 'create',
                'version' => 'v3',
            ])->json();

        // Check if API v3 was successful before proceeding
        if (!isset($call_approval_v3['id']) || $call_approval_v3['status'] !== 'success') {
            return response()->json([
                'status' => 'failed',
                'message' => 'API v3 failed. Cannot proceed to API v2.',
                'error' => $call_approval_v3
            ], 400);
        }

        // Step 2: Call API v2 using response from API v3
        $call_approval_v2 = Http::withOptions(['verify' => config('app.api_verify')])
            ->post(config('api.url_pkb') . '/api/autoserahan/create', [
                'id' => $call_approval_v3['id'],  // Use ID from API v3 response
                'kodcaw' => $request->arraysend['kodcaw'],
                'teller' => $request->arraysend['teller'],
                'amaun' => $request->arraysend['amaun'],
                'txtring1' => $request->arraysend['ambilanring1'],
                'txtring5' => $request->arraysend['ambilanring5'],
                'txtring10' => $request->arraysend['ambilanring10'],
                'txtring20' => $request->arraysend['ambilanring20'],
                'txtring50' => $request->arraysend['ambilanring50'],
                'txtring100' => $request->arraysend['ambilanring100'],
                'txtsen1' => $request->arraysend['ambilansen1'],
                'txtsen5' => $request->arraysend['ambilansen5'],
                'txtsen10' => $request->arraysend['ambilansen10'],
                'txtsen20' => $request->arraysend['ambilansen20'],
                'txtsen50' => $request->arraysend['ambilansen50'],
                'jenis' => 'serahan',
                'version' => 'v2',
            ])->json();

        $call_set_approval_v3 =  Http::withOptions(['verify' => config('app.api_verify')])
        ->put(config('api.url_koop') . '/api/autoserahan/setapproval', [
            'jenis' => 'create',
            'id' => $call_approval_v3['id'],
            'deno_id' => $call_approval_v2['denoid']
        ])->json();


        // Step 3: Check if both API v3 and API v2 were successful
        if ($call_approval_v2['status'] === 'success' && $call_approval_v3['status'] === 'success' && $call_set_approval_v3['status'] === 'success') {
            event(new AutoSerahan("Operasi",$call_approval_v3['id'],$request->arraysend['kodcaw'],$request->arraysend['teller']));
            
            return response()->json([
                'status' => 'success',
                'message' => 'Approval process completed successfully',
                'id' => $call_approval_v3['id'],
            ]);
        } else {
            return response()->json([
                'status' => 'failed',
                'message' => 'One of the approvals failed',
            ], 400);
        }
    }


}
