<?php

namespace App\Http\Controllers;

//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\User;
use App\Cawangan;

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;

class KhazanahController extends Controller
{


    public function __construct()
    {
        $this->middleware('auth');
    }
    
    public function index()
    {
        $search = false;

        $api_url = config('api.url');
        $auth_user = Auth::user();
        $cawangan_info = Http::get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
        $bank = Http::get(config('api.url') . '/api/bank')->json();
        return view('khazanah.index',compact('search','cawangan_info','bank','api_url'));
    }

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

        $request->session()->forget('page_reload');
        $request->session()->forget('norujukan');
        return view('khazanah.info',compact('cawangan_info','api_url','customer_info'));
    }

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

        $array_reqteller = [];

        $reqTeller = Http::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::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()
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');

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

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

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

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

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

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

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

        //Hari Ini
        $ringkasanharian = Http::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::get(config('api.url'). '/api/KeuntunganSebenar/'. $auth_user['cawangan'],['tarikh' => $harini->toDateString()])->json();
        $keuntungan = Http::get(config('api.url'). '/api/UntungRugiCalculation/'. $auth_user['cawangan'],['tarikh' => $harini->toDateString()])->json();

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

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

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

        //Check Komuditi
        $komuditicheck = Http::get( config('api.url').'/api/komuditi/check')->json();

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

        //Marhun Terkumpul
        $marhun = Http::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::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 alirantunai()
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');

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

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

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

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

            $gadai_tebus = Http::get(config('api.url').'/api/gadaitebus/listharini/'.$auth_user['cawangan'].'/teller/',['tarikh' => $harini->toDateString(),'teller' => $ls->name])->json();
            $tuntutbaki = Http::get(config('api.url').'/api/TuntutBaki/'.$auth_user['cawangan'].'/teller/',['teller' => $ls->name])->json();
            $addtunai = Http::get(config('api.url').'/api/getAddTunai/'.$auth_user['cawangan'].'/kodlaluan/',['kodlaluan' => $ls->name])->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::get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();

        $petibesi = Http::get(config('api.url') . '/api/VaultController/all/'. $auth_user['cawangan'])->json();
        
        return view('khazanah.alirantunai',compact('cawangan_info','api_url','arraytunaiharini','arraytunai','listUser','petibesi','arraygadaitebus','arraytuntutbaki','arrayaddtunai'));
    }


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

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



        $cawangan_info = Http::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::get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
        $listserah = Http::get(config('api.url').'/api/listSerahMarhunController/'.$auth_user['cawangan'])->json();
        $senaraitebus = Http::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();

 
        $gadai = Http::get(config('api.url') . '/api/gadai/norujukan/'. $auth_user['cawangan'] .'/' .$request->norujukan_barcode)->json();
        $gadai_detail = $gadai[0];
        $hargajual=$gadai[0]['hargajualan'];
        $upah12=$gadai[0]['upah12'];
        $tarikh_gadai=$gadai[0]['t_gadai'];
        $tebus = Http::get(config('api.url') . '/api/listpenebusanhistory/'. $auth_user['cawangan'],['norujukan' => $request->norujukan_barcode])->json();
        //dd($tebus);
        $tebus_detail = $tebus[0];
        $pinjaman=$tebus[0]['pinjaman'];
        $jenis_tebus=$tebus[0]['jenistebus'];   
        $bakipjm=$tebus[0]['bakipjm'];
        $bakiupah=$tebus[0]['bakiupah'];

 
 
        $getTunai2=Http::get(config('api.url').'/api/GetCurrentTunai/'.$auth_user['cawangan'],['kodlaluan' => $tebus_detail['teller']])->json();
 
        $keluar=$getTunai2['keluar'];
        $baki=$getTunai2['baki'];
        $masuk=$getTunai2['masuk'];
        $bakiawal=$getTunai2['bakiawal'];
        $pendahuluan=$getTunai2['tambah'];
        $serahan=$getTunai2['kurang'];
        $totalmasuk = 0;
        $bakiakhir = $getTunai2['baki'];
        $totalkeluar = 0;
 
          // initialized value glcode lejer_am
          $KOD_tunai='1000/010'; 
          $KOD_Pembiayaan='1000/017';
          $KOD_BP='1000/019';
          $Rebet_A='1000/021';
          $Rebet_B='5000/021';
          $Untung_Semasa='5000/017';
          $U_TG='1000/211';
          $U_HG='2000/007';
          $KOD_Pinjam='1000/013';
          $Upah_Simpan='5000/013';
 
 
          $jum_bayaran=Http::get(config('api.url').'/api/transaction/one/'.$request->norujukan_barcode)->json();
          $jumlah_bayaran=$jum_bayaran['duit_masuk'];

   
          
       //tarikh tawawruqq
       $tarikh_gadai_tawaruq_start='2021-04-14';
       if($upah12==null && $hargajual==null)
       {
 
 
 
        $get_lejerPinjam=Http::get(config('api.url').'/api/getGlejer/'.$auth_user['cawangan'],['glcode'=>$KOD_Pinjam])->json();

        $hariIni_Pinjaman=$get_lejerPinjam[0]['hariini'];
        $kelmarin_Pinjaman=$get_lejerPinjam[0]['kelmarin'];
        $jumlah_Pinjaman=$get_lejerPinjam[0]['jumlah'];
        $kredit_Pinjaman=$get_lejerPinjam[0]['kredit'];
        $debit_Pinjaman=$get_lejerPinjam[0]['debit'];
        $recno_Pinjaman=$get_lejerPinjam[0]['recno'];  

        
          if($jenis_tebus=='T')
           {
                
            $kredit_Pinjaman=$kredit_Pinjaman+$pinjaman;
            
           }
           else
           {

       
   
           }
           $hariIni_Pinjaman=$debit_Pinjaman-$kredit_Pinjaman;
           $kelmarin_Pinjaman=$jumlah_Pinjaman-$hariIni_Pinjaman;
           $updateGlejer_Pinjaman=Http::put(config('api.url').'/api/kemasKiniGlejer/'.$auth_user['cawangan'],[
            'recno'=>$recno_Pinjaman,
            'debit'=>$debit_Pinjaman,
            'kredit'=>$kredit_Pinjaman,
            'hariini'=>$hariIni_Pinjaman,
            'kelmarin' =>$kelmarin_Pinjaman,
        
            ]);


            $get_latest_bayarUntung=Http::get(config('api.url').'/api/latest_transaksi_untung/data/'.$request->norujukan_barcode)->json();
            $bayaran_latest_untung=$get_latest_bayarUntung[0]['bayaran_keuntungan'];
            

            $get_lejerTunai=Http::get(config('api.url').'/api/getGlejer/'.$auth_user['cawangan'],['glcode'=>$KOD_tunai])->json();
            $hariIni_Tunai=$get_lejerTunai[0]['hariini'];
            $kelmarin_Tunai=$get_lejerTunai[0]['kelmarin'];
            $jumlah_Tunai=$get_lejerTunai[0]['jumlah'];
            $kredit_Tunai=$get_lejerTunai[0]['kredit'];
            $debit_Tunai=$get_lejerTunai[0]['debit'];
            $recno_Tunai=$get_lejerTunai[0]['recno'];  
             

            if($jenis_tebus='T')
            {

             $debit_Tunai=$debit_Tunai+$bakipjm;

            }
             else
             {
              $debit_Tunai=$debit_Tunai+$jumlah_bayaran;
            
            }

               $hariIni_Tunai=$debit_Tunai-$kredit_Tunai;
                $updateGlejer_Tunai=Http::put(config('api.url').'/api/kemasKiniGlejer/'.$auth_user['cawangan'],[
                'recno'=>$recno_Tunai,
                'debit'=>$debit_Tunai,
                'kredit'=>$kredit_Tunai,
                'hariini'=>$hariIni_Tunai,
                'kelmarin' =>$kelmarin_Tunai,
            
                ]);

                $get_latest_bayarUntung=Http::get(config('api.url').'/api/latest_transaksi_untung/data/'.$request->norujukan_barcode)->json();
                $bayaran_latest_untung=$get_latest_bayarUntung[0]['bayaran_keuntungan'];

               // $get_lejer_UntungS=Http::get(config('api.url').'/api/getGlejer/'.$request->kodcaw,['glcode'=>$Upah_Simpan])->json();
            
               $get_lejer_UntungS=Http::get(config('api.url').'/api/getGlejer/'.$auth_user['cawangan'],['glcode'=>$Upah_Simpan])->json();

                $hariini_untungS=$get_lejer_UntungS[0]['hariini'];
                $kelmarin_untungS=$get_lejer_UntungS[0]['kelmarin'];
                $jumlah_untungS=$get_lejer_UntungS[0]['jumlah'];
                $debit_untungS=$get_lejer_UntungS[0]['debit'];
                $recno_untungS=$get_lejer_UntungS[0]['recno'];
                $kredit_untungS=$get_lejer_UntungS[0]['kredit']; 


                $kredit_untungS=$kredit_untungS+$bayaran_latest_untung;
                $hariini_untungS=$debit_untungS-$kredit_untungS;
                $kelmarin_untungS=$jumlah_untungS-$hariini_untungS;

            
                  $updateGlejer_UntungS=Http::put(config('api.url').'/api/kemasKiniGlejer/'.$auth_user['cawangan'],[
                    'recno'=>$recno_untungS,
                    'debit'=>$debit_untungS,
                    'kredit'=>$kredit_untungS,
                    'hariini'=>$hariini_untungS,
                    'kelmarin' =>$kelmarin_untungS,    
                
                    ]);

            if($tebus_detail['jenistebus'] == 'P')
            {

                $totalmasuk = $tebus_detail['pinjaman'] + $tebus_detail['bakiupah'] + $masuk;

                $updateTunai=Http::put( config('api.url').'/api/updateTunaiGT/'.$auth_user['cawangan'], [
                    'kodlaluan' => $tebus_detail['teller'],   
                    'keluar'=>$keluar,
                    'masuk'=>$totalmasuk,
                    'bakiakhir'=>$bakiakhir
                ]);

                if($updateTunai)
                {
                    $update=Http::put(config('api.url').'/api/updatetagterima/'.$auth_user['cawangan'].'/'.$request->norujukan_barcode)->json();
                }
                else
                {
                    dd('x masuk aliran tunai');
                }

            }else{

                if($tebus_detail['jenistebus'] == 'S')
                {
                    $totalmasuk = $tebus_detail['pinjaman'] + $tebus_detail['bakiupah'] + $masuk;

                    $updateTunai=Http::put( config('api.url').'/api/updateTunaiGT/'.$auth_user['cawangan'], [
                       'kodlaluan' => $tebus_detail['teller'],   
                       'keluar'=>$keluar,
                       'masuk'=>$totalmasuk,
                       'bakiakhir'=>$bakiakhir
                    ]);
       
                    if($updateTunai)
                    {
                       $update=Http::put(config('api.url').'/api/updatetagterima/'.$auth_user['cawangan'].'/'.$request->norujukan_barcode)->json();
                    }
                    else
                    {
                        dd('x masuk aliran tunai');
                    }
                }else{
                    $totalmasuk = $tebus_detail['bakipjm'] + $masuk;

                    $updateTunai=Http::put( config('api.url').'/api/updateTunaiGT/'.$auth_user['cawangan'], [
                        'kodlaluan' => $tebus_detail['teller'],   
                        'keluar'=>$keluar,
                        'masuk'=>$totalmasuk,
                        'bakiakhir'=>$bakiakhir
                     ]);
        
                     if($updateTunai)
                     {
                        $update=Http::put(config('api.url').'/api/updatetagterima/'.$auth_user['cawangan'].'/'.$request->norujukan_barcode)->json();
                     }
                     else
                     {
                         dd('x masuk aliran tunai');
                     }
                }
            }
                    
       }
       else
       {
       
       // dd(request()->all());

        if($tebus_detail['jenistebus'] == 'P')
        {
              
             // $totalkeluar = $tebus_detail['addpinjaman'] + $keluar;
             $totalmasuk = $tebus_detail['pinjaman'] + $tebus_detail['bakiupah'] + $masuk;
             // $bakiakhir = $bakiawal-$totalkeluar+$totalmasuk+$pendahuluan-$serahan;
 
 
            //KIRAAN LEJER AM PENEBUSAN P BERMULA
             $get_GLCode_Tunai=Http::get(config('api.url').'/api/getGlCode/'.$auth_user['cawangan'],['glcode'=>$KOD_tunai])->json();
             $keterangan_Tunai=$get_GLCode_Tunai[0]['descpt'];
             $acctype_Tunai=$get_GLCode_Tunai[0]['acttype'];
             $get_lejerTunai=Http::get(config('api.url').'/api/getGlejer/'.$auth_user['cawangan'],['glcode'=>$KOD_tunai])->json();
             $hariIni_Tunai=$get_lejerTunai[0]['hariini'];
             $kelmarin_Tunai=$get_lejerTunai[0]['kelmarin'];
             $jumlah_Tunai=$get_lejerTunai[0]['jumlah'];
             $kredit_Tunai=$get_lejerTunai[0]['kredit'];
             $debit_Tunai=$get_lejerTunai[0]['debit'];
             $recno_Tunai=$get_lejerTunai[0]['recno'];  
 
 
                 $debit_Tunai=$debit_Tunai+$jumlah_bayaran;
                 $hariIni_Tunai=$debit_Tunai-$kredit_Tunai;
                 $kelmarin_Tunai=$jumlah_Tunai-$hariIni_Tunai;
 
                     $updateGlejer_Tunai=Http::put(config('api.url').'/api/kemasKiniGlejer/'.$auth_user['cawangan'],[
                     'recno'=>$recno_Tunai,
                     'debit'=>$debit_Tunai,
                     'kredit'=>$kredit_Tunai,
                     'hariini'=>$hariIni_Tunai,
                     'kelmarin' =>$kelmarin_Tunai,
                 
                     ]);
 
                     $get_lejerPinjaman=Http::get(config('api.url').'/api/getGlejer/'.$auth_user['cawangan'],['glcode'=>$KOD_Pembiayaan])->json();
 
                     $hariIni_Pinjaman=$get_lejerPinjaman[0]['hariini'];
                     $kelmarin_Pinjaman=$get_lejerPinjaman[0]['kelmarin'];
                     $jumlah_Pinjaman=$get_lejerPinjaman[0]['jumlah'];
                     $kredit_Pinjaman=$get_lejerPinjaman[0]['kredit'];
                     $debit_Pinjaman=$get_lejerPinjaman[0]['debit'];
                     $recno_Pinjaman=$get_lejerPinjaman[0]['recno'];  
                     
                     $kredit_Pinjaman=$kredit_Pinjaman+$pinjaman;            
                     $hariIni=$debit_Pinjaman-$kredit_Pinjaman;
                     $kelmarin_Pinjaman=$jumlah_Pinjaman-$hariIni_Pinjaman;
 
                     $updateGlejer_Pinjaman=Http::put(config('api.url').'/api/kemasKiniGlejer/'.$auth_user['cawangan'],[
                     'recno'=>$recno_Pinjaman,
                     'debit'=>$debit_Pinjaman,
                     'kredit'=>$kredit_Pinjaman,
                     'hariini'=>$hariIni_Pinjaman,
                     'kelmarin' =>$kelmarin_Pinjaman,
 
                     ]);
 
                          $get_latest_bayarUntung=Http::get(config('api.url').'/api/latest_transaksi_untung/data/'.$request->norujukan_barcode)->json();
                         $bayaran_latest_untung=$get_latest_bayarUntung[0]['bayaran_keuntungan'];
                         $get_lejer_Untung=Http::get(config('api.url').'/api/getGlejer/'.$auth_user['cawangan'],['glcode'=>$Untung_Semasa])->json();
                     
                         $hariini_untung=$get_lejer_Untung[0]['hariini'];
                         $kelmarin_untung=$get_lejer_Untung[0]['kelmarin'];
                         $jumlah_untung=$get_lejer_Untung[0]['jumlah'];
                         $debit_untung=$get_lejer_Untung[0]['debit'];
                         $recno_untung=$get_lejer_Untung[0]['recno'];
                         $kredit_untung=$get_lejer_Untung[0]['kredit']; 
 
                         $kredit_untung=$kredit_untung+$bayaran_latest_untung;
                         $hariini_untung=$debit_untung-$kredit_untung;
                         $kelmarin_untung=$jumlah_untung-$hariini_untung;
 
                         $updateGlejer_Untung=Http::put(config('api.url').'/api/kemasKiniGlejer/'.$auth_user['cawangan'],[
                         'recno'=>$recno_untung,
                         'debit'=>$debit_untung,
                         'kredit'=>$kredit_untung,
                         'hariini'=>$hariini_untung,
                         'kelmarin' =>$kelmarin_untung,     
 
                        ]);
 
                         $get_bayaranPD=Http::get(config('api.url').'/api/jumlah_bayaran_pendahuluan/'.$request->norujukan_barcode)->json();
                         $jumlah_bp=$get_bayaranPD[0]['total_byr_pendahuluan'];
 
                         $get_lejerBayaran_Pendahuluan=Http::get(config('api.url').'/api/getGlejer/'.$auth_user['cawangan'],['glcode'=>$KOD_BP])->json();    
                         $hariini_bp= $get_lejerBayaran_Pendahuluan[0]['hariini'];
                         $kelmarin_bp=$get_lejerBayaran_Pendahuluan[0]['kelmarin'];
                         $jumlah_bp= $get_lejerBayaran_Pendahuluan[0]['jumlah'];
                         $debit_bp=$get_lejerBayaran_Pendahuluan[0]['debit'];
                         $recno_bp=$get_lejerBayaran_Pendahuluan[0]['recno'];
                         $kredit_bp=$get_lejerBayaran_Pendahuluan[0]['kredit']; 
 
                         $debit_bp=$debit_bp+$jumlah_bp;
                         $hariini_bp=$debit_bp-$kredit_bp;
                         $kelmarin_bp=$jumlah_bp-$hariini_bp;
 
                                 $updateGlejer_BP=Http::put(config('api.url').'/api/kemasKiniGlejer/'.$auth_user['cawangan'],[
                                 'recno'=>$recno_bp,
                                 'debit'=>$debit_bp,
                                 'kredit'=>$kredit_bp,
                                 'hariini'=>$hariini_bp,
                                 'kelmarin' =>$kelmarin_bp,     
 
                                 ]);
                //KIRAAN LEJER AM PENEBUSAN P BERAKHIR
 
                $updateTunai=Http::put( config('api.url').'/api/updateTunaiGT/'.$auth_user['cawangan'], [
                    'kodlaluan' => $tebus_detail['teller'],   
                    'keluar'=>$keluar,
                    'masuk'=>$totalmasuk,
                    'bakiakhir'=>$bakiakhir
                ]);

                if($updateTunai)
                {
                    $update=Http::put(config('api.url').'/api/updatetagterima/'.$auth_user['cawangan'].'/'.$request->norujukan_barcode)->json();
                }
                else
                {
                    dd('x masuk aliran tunai');
                }
 
             }else{
 
             if($tebus_detail['jenistebus'] == 'S')
             {
                 $totalmasuk = $tebus_detail['pinjaman'] + $tebus_detail['bakiupah'] + $masuk;

                 $updateTunai=Http::put( config('api.url').'/api/updateTunaiGT/'.$auth_user['cawangan'], [
                    'kodlaluan' => $tebus_detail['teller'],   
                    'keluar'=>$keluar,
                    'masuk'=>$totalmasuk,
                    'bakiakhir'=>$bakiakhir
                 ]);
    
                 if($updateTunai)
                 {
                    $update=Http::put(config('api.url').'/api/updatetagterima/'.$auth_user['cawangan'].'/'.$request->norujukan_barcode)->json();
                 }
                 else
                 {
                     dd('x masuk aliran tunai');
                 }
 
 
                //KIRAAN LEJER AM UNTUK PENEBUSAN S
 
                 $get_GLCode_Tunai=Http::get(config('api.url').'/api/getGlCode/'.$auth_user['cawangan'],['glcode'=>$KOD_tunai])->json();
                 $keterangan_Tunai=$get_GLCode_Tunai[0]['descpt'];
                 $acctype_Tunai=$get_GLCode_Tunai[0]['acttype'];
 
                 $get_lejerTunai=Http::get(config('api.url').'/api/getGlejer/'.$auth_user['cawangan'],['glcode'=>$KOD_tunai])->json();
                 $hariIni_Tunai=$get_lejerTunai[0]['hariini'];
                 $kelmarin_Tunai=$get_lejerTunai[0]['kelmarin'];
                 $jumlah_Tunai=$get_lejerTunai[0]['jumlah'];
                 $kredit_Tunai=$get_lejerTunai[0]['kredit'];
                 $debit_Tunai=$get_lejerTunai[0]['debit'];
                 $recno_Tunai=$get_lejerTunai[0]['recno'];  
 
 
                 $debit_Tunai=$debit_Tunai+$jumlah_bayaran;
                 $hariIni_Tunai=$debit_Tunai-$kredit_Tunai;
                 $kelmarin_Tunai=$jumlah_Tunai-$hariIni_Tunai;
             
 
                 $updateGlejer_Tunai=Http::put(config('api.url').'/api/kemasKiniGlejer/'.$auth_user['cawangan'],[
                 'recno'=>$recno_Tunai,
                 'debit'=>$debit_Tunai,
                 'kredit'=>$kredit_Tunai,
                 'hariini'=>$hariIni_Tunai,
                 'kelmarin' =>$kelmarin_Tunai,
 
                 ]);
 
 
                 $get_lejerPinjaman=Http::get(config('api.url').'/api/getGlejer/'.$auth_user['cawangan'],['glcode'=>$KOD_Pembiayaan])->json();
 
                 $hariIni_Pinjaman=$get_lejerPinjaman[0]['hariini'];
                 $kelmarin_Pinjaman=$get_lejerPinjaman[0]['kelmarin'];
                 $jumlah_Pinjaman=$get_lejerPinjaman[0]['jumlah'];
                 $kredit_Pinjaman=$get_lejerPinjaman[0]['kredit'];
                 $debit_Pinjaman=$get_lejerPinjaman[0]['debit'];
                 $recno_Pinjaman=$get_lejerPinjaman[0]['recno'];  
 
                 $kredit_Pinjaman=$kredit_Pinjaman+$pinjaman;            
                 $hariIni_Pinjaman=$debit_Pinjaman-$kredit_Pinjaman;
                 $kelmarin_Pinjaman=$jumlah_Pinjaman-$hariIni_Pinjaman;
 
                 $updateGlejer_Pinjaman=Http::put(config('api.url').'/api/kemasKiniGlejer/'.$auth_user['cawangan'],[
                 'recno'=>$recno_Pinjaman,
                 'debit'=>$debit_Pinjaman,
                 'kredit'=>$kredit_Pinjaman,
                 'hariini'=>$hariIni_Pinjaman,
                 'kelmarin' =>$kelmarin_Pinjaman,
 
                 ]);
 
             $get_latest_bayarUntung=Http::get(config('api.url').'/api/latest_transaksi_untung/data/'.$request->norujukan_barcode)->json();
             $bayaran_latest_untung=$get_latest_bayarUntung[0]['bayaran_keuntungan'];
             $get_lejer_Untung=Http::get(config('api.url').'/api/getGlejer/'.$auth_user['cawangan'],['glcode'=>$Untung_Semasa])->json();
 
             $hariini_untung=$get_lejer_Untung[0]['hariini'];
             $kelmarin_untung=$get_lejer_Untung[0]['kelmarin'];
             $jumlah_untung=$get_lejer_Untung[0]['jumlah'];
             $debit_untung=$get_lejer_Untung[0]['debit'];
             $recno_untung=$get_lejer_Untung[0]['recno'];
             $kredit_untung=$get_lejer_Untung[0]['kredit']; 
 
             $kredit_untung=$kredit_untung+$bayaran_latest_untung;
             $hariini_untung=$debit_untung-$kredit_untung;
            $kelmarin_untung=$jumlah_untung-$hariini_untung;
 
           $updateGlejer_Untung=Http::put(config('api.url').'/api/kemasKiniGlejer/'.$auth_user['cawangan'],[
             'recno'=>$recno_untung,
             'debit'=>$debit_untung,
             'kredit'=>$kredit_untung,
             'hariini'=>$hariini_untung,
             'kelmarin' =>$kelmarin_untung,     
 
         ]);
  
 
            $get_bayaranPD=Http::get(config('api.url').'/api/jumlah_bayaran_pendahuluan/'.$request->norujukan_barcode)->json();
            $jumlah_bp=$get_bayaranPD[0]['total_byr_pendahuluan'];
    
            $get_lejerBayaran_Pendahuluan=Http::get(config('api.url').'/api/getGlejer/'.$auth_user['cawangan'],['glcode'=>$KOD_BP])->json();
    
            $hariini_bp=$get_lejerBayaran_Pendahuluan[0]['hariini'];
            $kelmarin_bp=$get_lejerBayaran_Pendahuluan[0]['kelmarin'];
            $jumlah_bp=$get_lejerBayaran_Pendahuluan[0]['jumlah'];
            $debit_bp=$get_lejerBayaran_Pendahuluan[0]['debit'];
            $recno_bp=$get_lejerBayaran_Pendahuluan[0]['recno'];
            $kredit_bp=$get_lejerBayaran_Pendahuluan[0]['kredit']; 
    
            $debit_bp=$debit_bp+$jumlah_bp;
            $hariini_bp=$debit_bp-$kredit_bp;
            $kelmarin_bp=$jumlah_bp-$hariini_bp;
    
            $updateGlejer_BP=Http::put(config('api.url').'/api/kemasKiniGlejer/'.$auth_user['cawangan'],[
            'recno'=>$recno_bp,
            'debit'=>$debit_bp,
            'kredit'=>$kredit_bp,
            'hariini'=>$hariini_bp,
            'kelmarin' =>$kelmarin_bp,     
    
            ]);
 
         //KIRAAN LEJER AM PENEBUSAN S BERAKHIR
             }else{
 
                 $totalmasuk = $tebus_detail['bakipjm'] + $masuk;
                 // KIRAAN LEJER AM BERMULA UNYTUK PENEBUSAN JENIS  'T'

              
 
                 $get_GLCode_Tunai=Http::get(config('api.url').'/api/getGlCode/'.$auth_user['cawangan'],['glcode'=>$KOD_tunai])->json();
                 $keterangan_Tunai=$get_GLCode_Tunai[0]['descpt'];
                 $acctype_Tunai=$get_GLCode_Tunai[0]['acttype'];
                 $get_lejerTunai=Http::get(config('api.url').'/api/getGlejer/'.$auth_user['cawangan'],['glcode'=>$KOD_tunai])->json();
                 $hariIni_Tunai=$get_lejerTunai[0]['hariini'];
                 $kelmarin_Tunai=$get_lejerTunai[0]['kelmarin'];
                 $jumlah_Tunai=$get_lejerTunai[0]['jumlah'];
                 $kredit_Tunai=$get_lejerTunai[0]['kredit'];
                 $debit_Tunai=$get_lejerTunai[0]['debit'];
                 $recno_Tunai=$get_lejerTunai[0]['recno'];  
             
 
                    $debit_Tunai=$debit_Tunai+$jumlah_bayaran;
                    $hariIni_Tunai=$debit_Tunai-$kredit_Tunai;
                    $kelmarin_Tunai=$jumlah_Tunai-$hariIni_Tunai;
                  
        
            $updateGlejer_Tunai=Http::put(config('api.url').'/api/kemasKiniGlejer/'.$auth_user['cawangan'],[
            'recno'=>$recno_Tunai,
            'debit'=>$debit_Tunai,
            'kredit'=>$kredit_Tunai,
            'hariini'=>$hariIni_Tunai,
            'kelmarin' =>$kelmarin_Tunai,
          
              ]);
              
          
              $get_lejerPinjaman=Http::get(config('api.url').'/api/getGlejer/'.$auth_user['cawangan'],['glcode'=>$KOD_Pembiayaan])->json();
 
              $hariIni_Pinjaman=$get_lejerPinjaman[0]['hariini'];
              $kelmarin_Pinjaman=$get_lejerPinjaman[0]['kelmarin'];
              $jumlah_Pinjaman=$get_lejerPinjaman[0]['jumlah'];
              $kredit_Pinjaman=$get_lejerPinjaman[0]['kredit'];
              $debit_Pinjaman=$get_lejerPinjaman[0]['debit'];
              $recno_Pinjaman=$get_lejerPinjaman[0]['recno'];  
             
               $kredit_Pinjaman=$kredit_Pinjaman+$pinjaman;            
               $hariIni_Pinjaman=$debit_Pinjaman-$kredit_Pinjaman;
               $kelmarin_Pinjaman=$jumlah_Pinjaman-$hariIni_Pinjaman;
 
              $updateGlejer_Pinjaman=Http::put(config('api.url').'/api/kemasKiniGlejer/'.$auth_user['cawangan'],[
              'recno'=>$recno_Pinjaman,
              'debit'=>$debit_Pinjaman,
              'kredit'=>$kredit_Pinjaman,
              'hariini'=>$hariIni_Pinjaman,
              'kelmarin' =>$kelmarin_Pinjaman,
      
               ]); 
  
                       $get_latest_bayarUntung=Http::get(config('api.url').'/api/latest_transaksi_untung/data/'.$request->norujukan_barcode)->json();
                       $bayaran_latest_untung=$get_latest_bayarUntung[0]['bayaran_keuntungan'];
                       $get_lejer_Untung=Http::get(config('api.url').'/api/getGlejer/'.$auth_user['cawangan'],['glcode'=>$Untung_Semasa])->json();
                         
                             $hariini_untung=$get_lejer_Untung[0]['hariini'];
                             $kelmarin_untung=$get_lejer_Untung[0]['kelmarin'];
                             $jumlah_untung=$get_lejer_Untung[0]['jumlah'];
                             $debit_untung=$get_lejer_Untung [0]['debit'];
                             $recno_untung=$get_lejer_Untung [0]['recno'];
                             $kredit_untung=$get_lejer_Untung [0]['kredit']; 
                             $kredit_untung=$kredit_untung+$bayaran_latest_untung;
                             $hariini_untung=$debit_untung-$kredit_untung;
                             $kelmarin_untung=$jumlah_untung-$hariini_untung;
              
                             $updateGlejer_Untung=Http::put(config('api.url').'/api/kemasKiniGlejer/'.$auth_user['cawangan'],[
                             'recno'=>$recno_untung,
                             'debit'=>$debit_untung,
                             'kredit'=>$kredit_untung,
                             'hariini'=>$hariini_untung,
                             'kelmarin' =>$kelmarin_untung,     
                         
                  ]);
                  
                  $get_bayaranPD=Http::get(config('api.url').'/api/jumlah_bayaran_pendahuluan/'.$request->norujukan_barcode)->json();
                  $jumlah_bp=$get_bayaranPD[0]['total_byr_pendahuluan'];
                  $get_lejerBayaran_Pendahuluan=Http::get(config('api.url').'/api/getGlejer/'.$auth_user['cawangan'],['glcode'=>   $KOD_BP])->json();
                    
                  $hariini_bp= $get_lejerBayaran_Pendahuluan[0]['hariini'];
                  $kelmarin_bp=$get_lejerBayaran_Pendahuluan[0]['kelmarin'];
                  $jumlah_bp= $get_lejerBayaran_Pendahuluan[0]['jumlah'];
                  $debit_bp=$get_lejerBayaran_Pendahuluan[0]['debit'];
                 $recno_bp=$get_lejerBayaran_Pendahuluan[0]['recno'];
                  $kredit_bp=$get_lejerBayaran_Pendahuluan[0]['kredit']; 
            
                  $debit_bp=$debit_bp+$jumlah_bp;
                  $hariini_bp=$debit_bp-$kredit_bp;
                  $kelmarin_bp=$jumlah_bp-$hariini_bp;
 
                    $updateGlejer_BP=Http::put(config('api.url').'/api/kemasKiniGlejer/'.$auth_user['cawangan'],[
                    'recno'=>$recno_bp,
                    'debit'=>$debit_bp,
                    'kredit'=>$kredit_bp,
                    'hariini'=>$hariini_bp,
                    'kelmarin' =>$kelmarin_bp,     
                    ]);  
                    //  KIRAAN LEJER AM PENEBUSAN T BERAKHIR
             }
             // $bakiakhir = $bakiawal-$keluar+$totalmasuk+$pendahuluan-$serahan;
            $updateTunai=Http::put( config('api.url').'/api/updateTunaiGT/'.$auth_user['cawangan'], [
                'kodlaluan' => $tebus_detail['teller'],   
                'keluar'=>$keluar,
                'masuk'=>$totalmasuk,
                'bakiakhir'=>$bakiakhir
             ]);

             if($updateTunai)
             {
                $update=Http::put(config('api.url').'/api/updatetagterima/'.$auth_user['cawangan'].'/'.$request->norujukan_barcode)->json();
             }
             else
             {
                //
                
                //dd('x masuk aliran tunai');
             }
 
        }
        
     }
          //end tawaruq tebus
         // $bakiakhir = $bakiawal-$keluar+$totalmasuk+$pendahuluan-$serahan;
        //masuk dalam tunai aliran
   
 
        // return $this->tebus();

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




       
       
    }

    //END KHAZANAH FARIS

    public function pendahuluanserah($id)
    {

        $array_reqteller = [];

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

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

        $reqTeller = Http::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::get(config('api.url').'/api/DenominasiController/'.$auth_user['cawangan'])->json();

        if(sizeof($denominasi) == 0)
        {
            $denominasi=Http::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::get( config('api.url') .'/api/requestTeller/get/'. $auth_user['cawangan'])->json();

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

      $listUser=User::where('cawangan',$auth_user['cawangan'] )->get();
      $denominasi=Http::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::get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();

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

        $kelulusan = Http::get(config('api.url') . '/api/SenaraiKelulusan/' . $auth_user['cawangan'])->json();
        $kelulusanmohonid = Http::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'],'siri' => $k['siri'], 'namateller' => $k['teller'], 'pinjaman' => $k['jumpinj'], 'nilaimarhun' => ($komen[1] ?? '-'), 'berat' => ($komen[2] ?? '-'),'margin' => ($komen[3] ?? '-'), 'komen' => ($komen[0] ?? '-'), 'pelulus' => $k['pelulus']]);
        }

        foreach($kelulusanmohonid as $index=>$km)
        {
            $komen = explode("|",$km['katalaluan']);

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

            array_push($arraykelulusanmohonid,['norujukan' => $km['norujukan'], 'teller' => $km['teller'], 'mohon' => $km['mohon'], 'pinjaman' => ($komen[0] ?? '-'), 'nilaimarhun' => ($komen[1] ?? '-'), 'berat' => ($komen[2] ?? '-'), 'margin' => ($komen[3] ?? '-')]);
        }

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

    }

    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::get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();

        //declare array baru
        $arraykelulusan = [];

        $kelulusan = Http::get(config('api.url') . '/api/RekodDahLepas/' . $auth_user['cawangan'])->json();
        $kelulusanmohonid = Http::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] ?? '-'), 'berat' => ($komen[2] ?? '-'),'margin' => ($komen[3] ?? '-'), 'komen' => ($komen[0] ?? '-'), 'pelulus' => $k['pelulus'],]);
        }

        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::get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();

        $kelulusan = Http::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::get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();


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

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

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

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

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

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

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

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

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

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

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

                }else{

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

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

                }

                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::get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();

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

        if(isset($getsiri['siri']))
        {
            $siri = $getsiri['siri'] + 1;
        }else{
            $siri = 1; 
        }
        
        $createlulus = Http::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",
            '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::get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();

        $bakiperubatan = 0;

       
        switch ($request->jenismodal) {
            case "selenggara":
                $kategori = "Baiki dan Selenggara";
                $tag = 2;
               break;
            case "keraian":
                $kategori = "Keraian & Promosi";
                $tag = 3;
               break;
            case "belanjaam":
                $kategori = "Belanja AM";
                $tag = 1;
               break;
            case "perubatan":
                $kategori = "Belanja AM - Perubatan";
                $tag = 1;
               break;
            case "dapur":
                $kategori = "Belanja AM - Barangan Dapur";
                $tag = 1;
               break;
            case "pos":
                $kategori = "Belanja AM - Postage";
                $tag = 1;
               break;
          }

        $denominasilist = Http::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 
        $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::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"
          ]);

        $panjar = Http::post(config('api.url') . '/api/CreatePanjar/'. $auth_user['cawangan'],[

            'deskripsi' => $request->deskripsi,
            'bayaran' => $request->bayaran,
            'tag' => $tag,
            'kategori' => $kategori,
            'id' => $auth_user['name'],
            'bakiperubatan' => $bakiperubatan,
            'nobaucer' => $request->baucer,
            'nobil' => "3213231",
            'tuntutoleh' => $request->tuntutan    
            
        ])->json();


        //Lejer AM untuk Panjar bermula
    

       $PANJAR_AM='3000/018';
       $KERAIAN='3000/019';
       $SELENGGARA='3000/017';

       $TUNAI='1000/010';
            
           if($kategori=="Belanja AM - Perubatan"||$kategori== "Belanja AM" || $kategori=="Belanja AM - Barangan Dapur" || $kategori=="Belanja AM - Postage")
            {

                $get_glCode_Details=Http::get(config('api.url').'/api/getGlCode/'. $auth_user['cawangan'],['glcode'=>$PANJAR_AM])->json();
                $gl_cdetails=$get_glCode_Details[0];
                $keterangan=$get_glCode_Details[0]['descpt'];  //keterangan glcode
                $actype=$get_glCode_Details[0]['acttype'];     //keterangan acctype 
 
           $get_Panjar_AM=Http::get(config('api.url').'/api/getGlejer/'. $auth_user['cawangan'],['glcode'=>$PANJAR_AM])->json();
            if(sizeof($get_Panjar_AM)==0)
            {
                    $jumlah=0.0;
                    $getGLedjer_Semalam=Http::get(config('api.url').'/api/getSemalam_Glejer/'.$auth_user['cawangan'],['glcode'=>$PANJAR_AM])->json();
                      if(sizeof($getGLedjer_Semalam)==0)
                      {
                              $jumlah=0.0;
                      } 
                      else
                      {

                      $jumlah_semalam=$getGLedjer_Semalam[0]['jumlah'];
                      $hariIniSemalam=$getGLedjer_Semalam[0]['hariini'];
                      $jumlah=$jumlah_semalam+$hariIniSemalam;
                      }    
                      $jumlahBulan_ini=0.0;
                      $get_GlejerBulan_Tahun=Http::get(config('api.url').'/api/getLejerMonthYear/'.$auth_user['cawangan'],['glcode'=>$PANJAR_AM])->json();
                 
                       if( sizeof($get_GlejerBulan_Tahun)==0)
                       {
                           $jumlahBulan_ini=0.0;
                       }
                      else
                      {
                         $jumlahBulan_ini=$get_GlejerBulan_Tahun[0]['jumlah'];
                         $jumlahBulan_ini=$jumlah-$jumlahBulan_ini;
         
                      }//end bulan tahun
                      $Jumlah_TahunIni=0.0;
                      $get_Glejer_Tahun=Http::get(config('api.url').'/api/getLejerYear/'.$auth_user['cawangan'],['glcode'=>$PANJAR_AM])->json();
                      if(sizeof($get_Glejer_Tahun)==0)
                      {
                           $Jumlah_TahunIni=0.0;
                      }
                      else
                      {
            
                      $JumTahunIni=$get_Glejer_Tahun[0]['jumlah'];
                      $Jumlah_TahunIni=$jumlah-$JumTahunIni;
              
                      }

                      $HariIni=0.0-$request->bayaran;;
                      $kelmarin=$jumlah-$HariIni;
                    
                      //insert glejer untuk current date /hari ini
                   $daftarGlejer=Http::post(config('api.url').'/api/daftarGlejer/'.$auth_user['cawangan'],[
                    'kodcaw'=>$auth_user['cawangan'],
                    'glcode'=>$PANJAR_AM,
                    'descpt'=>$keterangan,
                    'debit'=>$request->bayaran,
                    'kredit'=>0.0,
                    'hariini'=>$HariIni,
                    'kelmarin'=>$kelmarin,
                    'jumlah'=>$jumlah,
                    'bulanini'=>$jumlahBulan_ini,
                    'tahunini'=>$Jumlah_TahunIni,
                    'acttype'=> $actype

                ]);


                $LejerTunaiDitangan=Http::get(config('api.url').'/api/getGlejer/'.$auth_user['cawangan'],['glcode'=>$TUNAI])->json();   
                $hariini_Tunai=$LejerTunaiDitangan[0]['hariini'];
                $kelmarin_Tunai=$LejerTunaiDitangan[0]['kelmarin'];
                $jumlah_Tunai=$LejerTunaiDitangan[0]['jumlah'];
                $debit_Tunai=$LejerTunaiDitangan[0]['debit'];
                $recno_Tunai=$LejerTunaiDitangan[0]['recno'];
                $kredit_Tunai=$LejerTunaiDitangan[0]['kredit'];
            
                $kredit_Tunai=$kredit_Tunai+$request->bayaran;
                $hariini_Tunai=$debit_Tunai-$kredit_Tunai;
                $kelmarin_Tunai=$jumlah_Tunai-$hariini_Tunai;


                $update_TunaiDiTangan=Http::put(config('api.url').'/api/kemasKiniGlejer/'.$auth_user['cawangan'],[
                 'recno'=>$recno_Tunai,
                 'debit'=>$debit_Tunai,
                 'kredit'=>$kredit_Tunai,
                 'hariini'=>$hariini_Tunai,
                'kelmarin'=>$kelmarin_Tunai,
               
              ]);




            }
            else
            {

                    $hariini_panjar= $get_Panjar_AM[0]['hariini'];
                    $kelmarin_panjar= $get_Panjar_AM[0]['kelmarin'];
                    $jumlah_panjar= $get_Panjar_AM[0]['jumlah'];
                    $debit_panjar= $get_Panjar_AM[0]['debit'];
                    $recno_panjar= $get_Panjar_AM[0]['recno'];
                    $kredit_panjar= $get_Panjar_AM[0]['kredit']; 


                    $debit_panjar=$debit_panjar+$request->bayaran;
                    $harini_panjar=$debit_panjar-$kredit_panjar;
                    $kelmarin_panjar=$jumlah_panjar-$harini_panjar;

                      $update_PanjarAM=Http::put(config('api.url').'/api/kemasKiniGlejer/'.$auth_user['cawangan'],[
                        'recno'=>$recno_panjar,
                        'debit'=>$debit_panjar,
                        'kredit'=>$kredit_panjar,
                        'hariini'=>$hariini_panjar,
                        'kelmarin'=>$kelmarin_panjar,

                    
                                   ]);   

                                   $LejerTunaiDitangan=Http::get(config('api.url').'/api/getGlejer/'.$auth_user['cawangan'],['glcode'=>$TUNAI])->json();   
                                   $hariini_Tunai=$LejerTunaiDitangan[0]['hariini'];
                                   $kelmarin_Tunai=$LejerTunaiDitangan[0]['kelmarin'];
                                   $jumlah_Tunai=$LejerTunaiDitangan[0]['jumlah'];
                                   $debit_Tunai=$LejerTunaiDitangan[0]['debit'];
                                   $recno_Tunai=$LejerTunaiDitangan[0]['recno'];
                                   $kredit_Tunai=$LejerTunaiDitangan[0]['kredit'];
                               
                                   $kredit_Tunai=$kredit_Tunai+$request->bayaran;
                                   $hariini_Tunai=$debit_Tunai-$kredit_Tunai;
                                   $kelmarin_Tunai=$jumlah_Tunai-$hariini_Tunai;

                                   $update_TunaiDiTangan=Http::put(config('api.url').'/api/kemasKiniGlejer/'.$auth_user['cawangan'],[
                                    'recno'=>$recno_Tunai,
                                    'debit'=>$debit_Tunai,
                                    'kredit'=>$kredit_Tunai,
                                    'hariini'=>$hariini_Tunai,
                                   'kelmarin'=>$kelmarin_Tunai,
                                  
                                 ]);


            }

            } //end belanja AM
            else if(  $kategori=="Keraian & Promosi")
            {

                $get_glCode_Details=Http::get(config('api.url').'/api/getGlCode/'. $auth_user['cawangan'],['glcode'=>$KERAIAN])->json();
                $gl_cdetails=$get_glCode_Details[0];
                $keterangan=$get_glCode_Details[0]['descpt'];  //keterangan glcode
                $actype=$get_glCode_Details[0]['acttype'];     //keterangan acctype 
 
           $get_Panjar_AM=Http::get(config('api.url').'/api/getGlejer/'. $auth_user['cawangan'],['glcode'=>$KERAIAN])->json();
            if(sizeof($get_Panjar_AM)==0)
            {
                    $jumlah=0.0;
                    $getGLedjer_Semalam=Http::get(config('api.url').'/api/getSemalam_Glejer/'.$auth_user['cawangan'],['glcode'=>$KERAIAN])->json();
                      if(sizeof($getGLedjer_Semalam)==0)
                      {
                              $jumlah=0.0;
                      } 
                      else
                      {

                      $jumlah_semalam=$getGLedjer_Semalam[0]['jumlah'];
                      $hariIniSemalam=$getGLedjer_Semalam[0]['hariini'];
                      $jumlah=$jumlah_semalam+$hariIniSemalam;
                      }    
                      $jumlahBulan_ini=0.0;
                      $get_GlejerBulan_Tahun=Http::get(config('api.url').'/api/getLejerMonthYear/'.$auth_user['cawangan'],['glcode'=>$KERAIAN])->json();
                 
                       if( sizeof($get_GlejerBulan_Tahun)==0)
                       {
                           $jumlahBulan_ini=0.0;
                       }
                      else
                      {
                         $jumlahBulan_ini=$get_GlejerBulan_Tahun[0]['jumlah'];
                         $jumlahBulan_ini=$jumlah-$jumlahBulan_ini;
         
                      }//end bulan tahun
                      $Jumlah_TahunIni=0.0;
                      $get_Glejer_Tahun=Http::get(config('api.url').'/api/getLejerYear/'.$auth_user['cawangan'],['glcode'=>$KERAIAN])->json();
                      if(sizeof($get_Glejer_Tahun)==0)
                      {
                           $Jumlah_TahunIni=0.0;
                      }
                      else
                      {
            
                      $JumTahunIni=$get_Glejer_Tahun[0]['jumlah'];
                      $Jumlah_TahunIni=$jumlah-$JumTahunIni;
              
                      }

                      $HariIni=0.00-$request->bayaran;
                      $kelmarin=$jumlah-$HariIni;
                    
                      //insert glejer untuk current date /hari ini
                   $daftarGlejer=Http::post(config('api.url').'/api/daftarGlejer/'.$auth_user['cawangan'],[
                    'kodcaw'=>$auth_user['cawangan'],
                    'glcode'=>$KERAIAN,
                    'descpt'=>$keterangan,
                    'debit'=>$request->bayaran,
                    'kredit'=>0.0,
                    'hariini'=>$HariIni,
                    'kelmarin'=>$kelmarin,
                    'jumlah'=>$jumlah,
                    'bulanini'=>$jumlahBulan_ini,
                    'tahunini'=>$Jumlah_TahunIni,
                    'acttype'=> $actype

                ]);

                $LejerTunaiDitangan=Http::get(config('api.url').'/api/getGlejer/'.$auth_user['cawangan'],['glcode'=>$TUNAI])->json();   
                $hariini_Tunai=$LejerTunaiDitangan[0]['hariini'];
                $kelmarin_Tunai=$LejerTunaiDitangan[0]['kelmarin'];
                $jumlah_Tunai=$LejerTunaiDitangan[0]['jumlah'];
                $debit_Tunai=$LejerTunaiDitangan[0]['debit'];
                $recno_Tunai=$LejerTunaiDitangan[0]['recno'];
                $kredit_Tunai=$LejerTunaiDitangan[0]['kredit'];
            
                $kredit_Tunai=$kredit_Tunai+$request->bayaran;
                $hariini_Tunai=$debit_Tunai-$kredit_Tunai;
                $kelmarin_Tunai=$jumlah_Tunai-$hariini_Tunai;

                $update_TunaiDiTangan=Http::put(config('api.url').'/api/kemasKiniGlejer/'.$auth_user['cawangan'],[
                 'recno'=>$recno_Tunai,
                 'debit'=>$debit_Tunai,
                 'kredit'=>$kredit_Tunai,
                 'hariini'=>$hariini_Tunai,
                'kelmarin'=>$kelmarin_Tunai,
               
              ]);



            }
            else
            {

                    $hariini_panjar= $get_Panjar_AM[0]['hariini'];
                    $kelmarin_panjar= $get_Panjar_AM[0]['kelmarin'];
                    $jumlah_panjar= $get_Panjar_AM[0]['jumlah'];
                    $debit_panjar= $get_Panjar_AM[0]['debit'];
                    $recno_panjar= $get_Panjar_AM[0]['recno'];
                    $kredit_panjar= $get_Panjar_AM[0]['kredit']; 


                    $debit_panjar=$debit_panjar+$request->bayaran;
                    $harini_panjar=$debit_panjar-$kredit_panjar;
                    $kelmarin_panjar=$jumlah_panjar-$harini_panjar;

                      $update_PanjarAM=Http::put(config('api.url').'/api/kemasKiniGlejer/'.$auth_user['cawangan'],[
                        'recno'=>$recno_panjar,
                        'debit'=>$debit_panjar,
                        'kredit'=>$kredit_panjar,
                        'hariini'=>$hariini_panjar,
                        'kelmarin'=>$kelmarin_panjar,

                    
                                   ]);   

                                   $LejerTunaiDitangan=Http::get(config('api.url').'/api/getGlejer/'.$auth_user['cawangan'],['glcode'=>$TUNAI])->json();   
                                   $hariini_Tunai=$LejerTunaiDitangan[0]['hariini'];
                                   $kelmarin_Tunai=$LejerTunaiDitangan[0]['kelmarin'];
                                   $jumlah_Tunai=$LejerTunaiDitangan[0]['jumlah'];
                                   $debit_Tunai=$LejerTunaiDitangan[0]['debit'];
                                   $recno_Tunai=$LejerTunaiDitangan[0]['recno'];
                                   $kredit_Tunai=$LejerTunaiDitangan[0]['kredit'];
                               
                                   $kredit_Tunai=$kredit_Tunai+$request->bayaran;
                                   $hariini_Tunai=$debit_Tunai-$kredit_Tunai;
                                   $kelmarin_Tunai=$jumlah_Tunai-$hariini_Tunai;

                                   $update_TunaiDiTangan=Http::put(config('api.url').'/api/kemasKiniGlejer/'.$auth_user['cawangan'],[
                                    'recno'=>$recno_Tunai,
                                    'debit'=>$debit_Tunai,
                                    'kredit'=>$kredit_Tunai,
                                    'hariini'=>$hariini_Tunai,
                                   'kelmarin'=>$kelmarin_Tunai,
                                  
                                 ]);


            }

            }//emd keraian Promosi
           else
           {

            $get_glCode_Details=Http::get(config('api.url').'/api/getGlCode/'. $auth_user['cawangan'],['glcode'=>$SELENGGARA])->json();
            $gl_cdetails=$get_glCode_Details[0];
            $keterangan=$get_glCode_Details[0]['descpt'];  //keterangan glcode
            $actype=$get_glCode_Details[0]['acttype'];     //keterangan acctype 

       $get_Panjar_AM=Http::get(config('api.url').'/api/getGlejer/'. $auth_user['cawangan'],['glcode'=>$SELENGGARA])->json();
        if(sizeof($get_Panjar_AM)==0)
        {
                $jumlah=0.0;
                $getGLedjer_Semalam=Http::get(config('api.url').'/api/getSemalam_Glejer/'.$auth_user['cawangan'],['glcode'=>$SELENGGARA])->json();
                  if(sizeof($getGLedjer_Semalam)==0)
                  {
                          $jumlah=0.0;
                  } 
                  else
                  {

                  $jumlah_semalam=$getGLedjer_Semalam[0]['jumlah'];
                  $hariIniSemalam=$getGLedjer_Semalam[0]['hariini'];
                  $jumlah=$jumlah_semalam+$hariIniSemalam;
                  }    
                  $jumlahBulan_ini=0.0;
                  $get_GlejerBulan_Tahun=Http::get(config('api.url').'/api/getLejerMonthYear/'.$auth_user['cawangan'],['glcode'=>$SELENGGARA])->json();
             
                   if( sizeof($get_GlejerBulan_Tahun)==0)
                   {
                       $jumlahBulan_ini=0.0;
                   }
                  else
                  {
                     $jumlahBulan_ini=$get_GlejerBulan_Tahun[0]['jumlah'];
                     $jumlahBulan_ini=$jumlah-$jumlahBulan_ini;
     
                  }//end bulan tahun
                  $Jumlah_TahunIni=0.0;
                  $get_Glejer_Tahun=Http::get(config('api.url').'/api/getLejerYear/'.$auth_user['cawangan'],['glcode'=>$SELENGGARA])->json();
                  if(sizeof($get_Glejer_Tahun)==0)
                  {
                       $Jumlah_TahunIni=0.0;
                  }
                  else
                  {
        
                  $JumTahunIni=$get_Glejer_Tahun[0]['jumlah'];
                  $Jumlah_TahunIni=$jumlah-$JumTahunIni;
          
                  }

                  $HariIni=0.0-$request->bayaran;
                  $kelmarin=$jumlah-$HariIni;
                
                  //insert glejer untuk current date /hari ini
               $daftarGlejer=Http::post(config('api.url').'/api/daftarGlejer/'.$auth_user['cawangan'],[
                'kodcaw'=>$auth_user['cawangan'],
                'glcode'=>$SELENGGARA,
                'descpt'=>$keterangan,
                'debit'=>$request->bayaran,
                'kredit'=>0.0,
                'hariini'=>$HariIni,
                'kelmarin'=>$kelmarin,
                'jumlah'=>$jumlah,
                'bulanini'=>$jumlahBulan_ini,
                'tahunini'=>$Jumlah_TahunIni,
                'acttype'=> $actype

            ]);



             
            $LejerTunaiDitangan=Http::get(config('api.url').'/api/getGlejer/'.$auth_user['cawangan'],['glcode'=>$TUNAI])->json();   
            $hariini_Tunai=$LejerTunaiDitangan[0]['hariini'];
            $kelmarin_Tunai=$LejerTunaiDitangan[0]['kelmarin'];
            $jumlah_Tunai=$LejerTunaiDitangan[0]['jumlah'];
            $debit_Tunai=$LejerTunaiDitangan[0]['debit'];
            $recno_Tunai=$LejerTunaiDitangan[0]['recno'];
            $kredit_Tunai=$LejerTunaiDitangan[0]['kredit'];
        
            $kredit_Tunai=$kredit_Tunai+$request->bayaran;
            $hariini_Tunai=$debit_Tunai-$kredit_Tunai;
            $kelmarin_Tunai=$jumlah_Tunai-$hariini_Tunai;

            $update_TunaiDiTangan=Http::put(config('api.url').'/api/kemasKiniGlejer/'.$auth_user->kodcaw,[
             'recno'=>$recno_Tunai,
             'debit'=>$debit_Tunai,
             'kredit'=>$kredit_Tunai,
             'hariini'=>$hariini_Tunai,
            'kelmarin'=>$kelmarin_Tunai,
           
          ]);



        }
        else
        {

                            $hariini_panjar= $get_Panjar_AM[0]['hariini'];
                            $kelmarin_panjar= $get_Panjar_AM[0]['kelmarin'];
                            $jumlah_panjar= $get_Panjar_AM[0]['jumlah'];
                            $debit_panjar= $get_Panjar_AM[0]['debit'];
                            $recno_panjar= $get_Panjar_AM[0]['recno'];
                            $kredit_panjar= $get_Panjar_AM[0]['kredit']; 
                              $debit_panjar=$debit_panjar+$request->bayaran;
                              $harini_panjar=$debit_panjar-$kredit_panjar;
                              $kelmarin_panjar=$jumlah_panjar-$harini_panjar;
                              $update_PanjarAM=Http::put(config('api.url').'/api/kemasKiniGlejer/'.$auth_user['cawangan'],[
                            'recno'=>$recno_panjar,
                            'debit'=>$debit_panjar,
                            'kredit'=>$kredit_panjar,
                            'hariini'=>$hariini_panjar,
                            'kelmarin'=>$kelmarin_panjar,

                        
                               ]);   

                               $LejerTunaiDitangan=Http::get(config('api.url').'/api/getGlejer/'.$auth_user['cawangan'],['glcode'=>$TUNAI])->json();   
                               $hariini_Tunai=$LejerTunaiDitangan[0]['hariini'];
                               $kelmarin_Tunai=$LejerTunaiDitangan[0]['kelmarin'];
                               $jumlah_Tunai=$LejerTunaiDitangan[0]['jumlah'];
                               $debit_Tunai=$LejerTunaiDitangan[0]['debit'];
                               $recno_Tunai=$LejerTunaiDitangan[0]['recno'];
                               $kredit_Tunai=$LejerTunaiDitangan[0]['kredit'];
                           
                               $kredit_Tunai=$kredit_Tunai+$request->bayaran;
                               $hariini_Tunai=$debit_Tunai-$kredit_Tunai;
                               $kelmarin_Tunai=$jumlah_Tunai-$hariini_Tunai;

                               $update_TunaiDiTangan=Http::put(config('api.url').'/api/kemasKiniGlejer/'.$auth_user->kodcaw,[
                                'recno'=>$recno_Tunai,
                                'debit'=>$debit_Tunai,
                                'kredit'=>$kredit_Tunai,
                                'hariini'=>$hariini_Tunai,
                               'kelmarin'=>$kelmarin_Tunai,
                              
                             ]);
        }
           }//end perbelanjaan Selanggara


   
            // lejer am panjar tamat...


        $updateVault = Http::put( config('api.url').'/api/UpdateVault/Panjar/'.$auth_user['cawangan'],[
            'totpanjar'=>$request->bayaran
        ]); 

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

        if($upVault == 200 && empty($panjar) == false && $upDeno == 200)
        {
            return redirect('/panjar')->with('messageinformation','Panjar Berjaya Disimpan.');
        }else{
            return redirect('/panjar')->with('messageinformation','Panjar Mengalami Masalah UntukDisimpan.');
        }

        return response($updateVault);
    }

    public function pendahuluan()
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');
        $cawangan_info = Http::get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
        $tunai=Http::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::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::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();
        $gadai = Http::put(config('api.url').'/api/gadai/serah_marhun/khazanah/'. $auth_user['cawangan'] .'/' . $request->norujukan_barcode)->json();

        $gadai = Http::get(config('api.url') . '/api/gadai/norujukan/'. $auth_user['cawangan'] .'/' .$request->norujukan_barcode)->json();
        $gadai_detail = $gadai[0];

        $getTunai2=Http::get(config('api.url').'/api/GetCurrentTunai/'.$auth_user['cawangan'],['kodlaluan' => $gadai_detail['teller']])->json();

        $keluar=$getTunai2['keluar'];
        $baki=$getTunai2['baki'];
        $masuk=$getTunai2['masuk'];
        $bakiawal = $getTunai2['bakiawal'];
        $pendahuluan=$getTunai2['tambah'];
        $serahan=$getTunai2['kurang'];
        $totalmasuk = 0;
        $bakiakhir = $getTunai2['baki'];
        $totalkeluar = 0;
        $upah12=$gadai[0]['upah12'];
        $totalkeluar = $gadai_detail['pinjaman'] + $keluar;
    
        //masuk dalam tunai aliran
        $updateTunai=Http::put( config('api.url').'/api/updateTunaiGT/'.$auth_user['cawangan'], [
            'kodlaluan' => $gadai_detail['teller'],
            'keluar' => $totalkeluar,
            'masuk' => $masuk,
            'bakiakhir' => $bakiakhir
        ]);
            
        //************KIRAAN LEJER AM BERMULA ***/
        $pembiayaan_tawaruq='1000/017';
        $GL_Untung_Tangguh='1000/211';
        $GL_Untung_Suspense='2000/007';

        $getProduct=Http::get(config('api.url').'/api/getProduct/'.$auth_user['cawangan'])->json();     
        $glcode=$getProduct[0]['glcode'];
        $kodGL=$getProduct[0]['kodgl'];
        $get_GLCode=Http::get(config('api.url').'/api/getGlCode/'.$auth_user['cawangan'],['glcode'=>$glcode])->json();
        $keterangan=$get_GLCode[0]['descpt'];
        $acctype=$get_GLCode[0]['acttype'];


      $get_lejer=Http::get(config('api.url').'/api/getGlejer/'.$auth_user['cawangan'],['glcode'=>$glcode])->json();
      if(sizeof($get_lejer)>0)
       {
       $hariIni=$get_lejer[0]['hariini'];
       $jumlah=$get_lejer[0]['kelmarin'];
       $kredit=$get_lejer[0]['kredit'];
       $debit=$get_lejer[0]['debit'];
       $recno=$get_lejer[0]['recno'];

       $kredit=$kredit+$gadai_detail['pinjaman'];
       $Hari_Ini=$debit-$kredit;
       $Kelmarin=$jumlah-$Hari_Ini;     
 
       $updateGlejer=Http::put(config('api.url').'/api/kemasKiniGlejer/'.$auth_user['cawangan'],[
            'recno'=>$recno,
            'debit'=>$debit,
            'kredit'=>$kredit,
            'hariini'=>$Hari_Ini,
            'kelmarin'=>$Kelmarin,          
            ]);

           }
           else  
           {
        

           }
        
           $get_lejerPinj=Http::get(config('api.url').'/api/getGlejer/'.$auth_user['cawangan'],['glcode'=>$pembiayaan_tawaruq])->json();
           $hariIni=$get_lejerPinj[0]['hariini'];
           $Kelmarin=$get_lejerPinj[0]['kelmarin'];
           $jumlah=$get_lejerPinj[0]['jumlah'];
           $kredit=$get_lejerPinj[0]['kredit'];
           $debit=$get_lejerPinj[0]['debit'];
           $recno=$get_lejerPinj[0]['recno'];   
 
           $debit=$debit+$gadai_detail['pinjaman'];
           $hariIni=$debit-$kredit;
           $Kelmarin=$jumlah-$hariIni;

           $updateGlejer=Http::put(config('api.url').'/api/kemasKiniGlejer/'.$auth_user['cawangan'],[
               'recno'=>$recno,
               'kredit'=>$kredit,
               'debit'=>$debit,
               'hariini'=>$hariIni,
               'kelmarin' =>$Kelmarin,
               
               ]);

           $get_lejerPinj2=Http::get(config('api.url').'/api/getGlejer/'.$auth_user['cawangan'],['glcode'=>$GL_Untung_Tangguh])->json();
           $hariIni_UT=$get_lejerPinj2[0]['hariini'];
           $kelmarin_UT=$get_lejerPinj2[0]['kelmarin'];
           $jumlah_UT=$get_lejerPinj2[0]['jumlah'];
           $kredit_UT=$get_lejerPinj2[0]['kredit'];
           $debit_UT=$get_lejerPinj2[0]['debit'];
           $recno_UT=$get_lejerPinj2[0]['recno'];   
             
           $debit_UT=$debit_UT+$upah12;
           $hariIni_UT=$debit_UT-$kredit_UT;
           $kelmarin_UT=$jumlah_UT-$hariIni_UT;

           $updateGlejer_UT=Http::put(config('api.url').'/api/kemasKiniGlejer/'.$auth_user['cawangan'],[
           'recno'=>$recno_UT,
           'kredit'=>$kredit_UT,
           'debit'=>$debit_UT,
           'hariini'=>$hariIni_UT,
           'kelmarin' =>$kelmarin_UT,

           ]);
           $get_lejerPinj3=Http::get(config('api.url').'/api/getGlejer/'.$auth_user['cawangan'],['glcode'=>$GL_Untung_Suspense])->json();
           $hari_ini_US=$get_lejerPinj3[0]['hariini'];
           $kelmarin_US=$get_lejerPinj3[0]['kelmarin'];
           $jumlah_US=$get_lejerPinj3[0]['jumlah'];
           $kredit_US=$get_lejerPinj3[0]['kredit'];
           $debit_US=$get_lejerPinj3[0]['debit'];
           $recno_US=$get_lejerPinj3[0]['recno'];
               
           $kredit_US=$kredit_US+$upah12;
           $hari_ini_US=$debit_US-$kredit_US;
           $kelmarin_US=$jumlah_US-$hari_ini_US;

           $updateGlejer_US=Http::put(config('api.url').'/api/kemasKiniGlejer/'.$auth_user['cawangan'],[
           'recno'=>$recno_US,
           'kredit'=>$kredit_US,
           'debit'=>$debit_US,
           'hariini'=>$hari_ini_US,
           'kelmarin' =>$kelmarin_US,

           ]);

      //********TAMAT****************/
        event(new NotifikasiPembayaran('TellerkepadaKhazanah'));

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

    public function LaporanSemakBarcode(){
        $auth_user=Auth::user();
        $marhun_serah_petibesi=Http::get(config('api.url').'/api/gadai/serah_marhun/khazanah/petibesi/'.$auth_user['cawangan'])->json();
        $cawangan=Http::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::get(config('api.url').'/api/gadai/serah_marhun/khazanah/petibesi/'.$auth_user['cawangan'])->json();
        $cawangan=Http::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::get( config('api.url') .'/api/gadai/serah_marhun/khazanah/'. $auth_user['cawangan'])->json();
        $cawangan_info = Http::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::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::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"));

          $updaterequest=Http::put(config('api.url').'/api/requestTeller/update/'. $auth_user['cawangan'],[
            'kodlaluan' => $request->teller,    
            'no' => $request->no
      
        ]);

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

    }

    public function denominasicalculationserahan($request)
    {

        
        $auth_user = Auth::user();

        $denominasilist = Http::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::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"));

          $updaterequest=Http::put(config('api.url').'/api/requestTeller/update/'. $auth_user['cawangan'],[
            'kodlaluan' => $request->teller,   
            'no' => $request->no
      
        ]);

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

    }

    public function kemaskiniPendahuluan(Request $request)       //Daftar Pendahuluan kepada Kaunter
    {
       
    //   return $this->denominasicalculationpendahuluan($request);

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

      if(sizeof($peti_besi_terkini) == 0)
      {
          $peti_besi_terkini=Http::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)
      {
        //WI ERROR MESSAGE SKIT
        // dd($baki_awal);
        return redirect()->route('khazanah.serahan')->with('message', 'Jumlah yang dipinta melebihi baki dalam peti besi!.');
      }
     else
     {

      $tarikh=$request->tarikh;
      $kodlaluan=$request->teller;
      $tambah=$request->amt;
      $kurang=0.0;
      $nota=$request->nota;
      $kodcaw=$auth_user['cawangan'];

      $addTunai=Http::post(config('api.url').'/api/add/addTunai/'.$auth_user['cawangan'],[
       'kodcaw'=>$kodcaw,
       'kodlaluan'=>$kodlaluan,
       'tambah'=>$tambah,
       'kurang'=>$kurang,
       'nota'=>$nota 
       ]);

       $getTunai=Http::get(config('api.url').'/api/GetCurrentTunai/'.$auth_user['cawangan'],['kodlaluan' => $kodlaluan])->json();
      
       if(sizeof($getTunai) == 0)
       {
            dd($getTunai);
       }
       else
       {
            $bakiawal=0;$masuk=0;$yuran=0;$keluar=0;$baki=0;$tmbh=0;$kurang=0;

             $BakiAkhirT=0;$BakiAwalT=0;$tambahT=0;

             $getTunai2=Http::get(config('api.url').'/api/GetCurrentTunai/'.$auth_user['cawangan'],['kodlaluan' => $kodlaluan])->json();
             $bakiAwal=$getTunai2['bakiawal'];
             $keluar=$getTunai2['keluar'];
             $baki=$getTunai2['baki'];
             $kurangT=$getTunai2['kurang'];
             $tam=$getTunai2['tambah'];
             $masukT=$getTunai2['masuk'];

              if($bakiAwal==0)
              {
                $BakiAwalT=0;
              }
              else
              {
                $BakiAwalT=$bakiAwal;
              }

            $tambahT=$tambah + $tam;

            $kurang=$kurangT;
            $keluarT = $keluar;
            // $bakiAkhirT= $bakiAwal + $tambahT + $masukT - $keluarT - $kurang;
            $bakiAkhirT = $baki + $tambah;

            $updateTunai=Http::put( config('api.url').'/api/updateTunai/'.$auth_user['cawangan'], [
            'kodlaluan'=>$kodlaluan,
            'tambah'=>$tambahT,
            'kurang'=>$kurang,
            'bakiakhir'=>$bakiAkhirT
             ]);
             
            $latestVault=Http::get(config('api.url').'/api/VaultController/'.$auth_user['cawangan'])->json();
            $masukV=$latestVault[0]['masuk'];
            $keluarV=$latestVault[0]['keluar'];
            $pendahuluanV=$latestVault[0]['pendahuluan'];
            $serahanV=$latestVault[0]['serahan'];
            $bakiAwalV=$latestVault[0]['bakiawal'];
            $totpanjar = $latestVault[0]['totpanjar'];
                
            // return;
            //  dd($tambahT);
            //  $keluarV=$tambahT;
            $keluarV = $tambah + $keluarV;
             
             $GetLatestBakiTunai=Http::get( config('api.url').'/api/getLatestBaki/'.$auth_user['cawangan'])->json();
             
             $bakiTerkini=$GetLatestBakiTunai;
             $bakiAkhirV=$bakiAwalV - $keluarV + $masukV + $pendahuluanV - $serahanV - $totpanjar; 
            //  $bakiEOD=$bakiAkhirV+$GetLatestBakiTunai; //try test
            $bakiEOD = $bakiAkhirV;

            //  dd($bakiEOD);
            //  $createTempVault=Http::post(config('api.url').'/api/daftarVault/'.$auth_user['cawangan']);
            
            //  dd($bakiAkhirV);
            //get temporary latest vault/ petibesi



            $updateVault=Http::put( config('api.url').'/api/UpdateVault/'.$auth_user['cawangan'],[
                
                'masuk'=>$masukV,
                'keluar'=>$keluarV,
                'bakiawal'=>$bakiAwalV,
                'bakiakhir'=>$bakiAkhirV,
                'bakieod'=>$bakiEOD
          
            ]); 

        //-----PROCESS SELESAI-----------------------------------
      //display final result

      return $this->denominasicalculationpendahuluan($request);
      //dd($updateVault);  


       } //endif

     } //end 

    } //End

    public function KemaskiniSerahan(Request $request)
    {

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

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

      if(sizeof($peti_besi_terkini) == 0)
      {
          $peti_besi_terkini=Http::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.serahan')->with('message', 'Jumlah yang dipinta melebihi baki dalam peti besi!.');
      }else{
        $tarikh=$request->tarikh;
        $kodlaluan=$request->teller;
    
        $kurang=$request->amt;
        $nota=$request->nota;
        $kodcaw=$auth_user['cawangan'];
        $tambahT = 0;
        $kurangvault = $request->amt;
                    
        $addTunai=Http::post(config('api.url').'/api/add/addTunai/'.$auth_user['cawangan'],[
            'kodcaw'=>$kodcaw,
            'kodlaluan'=>$kodlaluan,
            'tambah'=>$tambahT,
            'kurang'=>$kurang,
            'nota'=>$nota]);

        $getTunai=Http::get(config('api.url').'/api/GetCurrentTunai/'.$auth_user['cawangan'],['kodlaluan' => $kodlaluan])->json();
    
        if(sizeof($getTunai) == 0)
        {
            // dd($getTunai);
        }
        else
        {
            $getTunai2=Http::get(config('api.url').'/api/GetCurrentTunai/'.$auth_user['cawangan'],['kodlaluan' => $kodlaluan])->json();
            $bakiAwalT=$getTunai2['bakiawal'];
            $keluarT=$getTunai2['keluar'];
            $baki=$getTunai2['baki'];
            $tambahT=$getTunai2['tambah'];
            $kurangT=$getTunai2['kurang'];
            $masukT=$getTunai2['masuk'];
            
            $serahanakhir = $kurang;
            $tambah_T=0;$kurang_T=0;$baki_AwalT=0.0;
        
            if($tambahT==0)
                $tambah_T=0;
              
            else
                $tambah_T=$tambahT;
    
    
              if($kurangT==0)
                $kurang_T=$kurang;
    
              else
                {
                 $kurang=$kurang+$kurangT;
                 $kurang_T=$kurang;
                }
               
               if($bakiAwalT==0)
                $baki_AwalT=0;
    
               else
                $baki_AwalT=$bakiAwalT;
    
                //$bakiAkhirT=$baki_AwalT+$tambah_T+$masukT-$keluarT-$kurang_T; //untuk kegunaan urgent
                $bakiAkhirT = $baki - $serahanakhir;

               $updateTunai=Http::put( config('api.url').'/api/updateTunai/'.$auth_user['cawangan'], [
                    'kodlaluan'=>$kodlaluan,
                    'tambah'=>$tambah_T,
                    'kurang'=>$kurang_T,
                    'bakiakhir'=>$bakiAkhirT
                 ]);
                 
               
               $latestVault=Http::get(config('api.url').'/api/VaultController/'.$auth_user['cawangan'])->json();

               $masukV=$latestVault[0]['masuk'];
               $keluarV=$latestVault[0]['keluar'];
               $pendahuluanV=$latestVault[0]['pendahuluan'];
               $serahanV=$latestVault[0]['serahan'];
               $bakiAwalV=$latestVault[0]['bakiawal'];
               $totpanjar = $latestVault[0]['totpanjar'];

    
               $masuk_V = 0;$pendahuluan_V = 0;$keluar_V = 0;$serahan_V = 0;
               
                if($masukV == 0)
                {
                    $masuk_V = 0 + $kurangvault;
                }
                else
                {
                    $masuk_V = $masukV + $kurangvault;
                }
                    
                if($keluarV == 0)
                    $keluar_V = 0;
    
                else
                    $keluar_V=$keluarV;
            
            
             if($pendahuluanV == 0)
                $pendahuluan_V = 0;
    
              else
                $pendahuluan_V = $pendahuluanV;
    
                
              if($serahanV == 0)
                $serahan_V = 0;
    
             else
                $serahan_V = $serahanV;

        }    

      }

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


      $bakiAkhirV = $bakiAwalV - $keluar_V + $masuk_V + $pendahuluan_V - $serahan_V - $totpanjar;

      $BakiEOD=$bakiAkhirV;
    //$BakiEOD=$bakiAkhirV+$sumBaki;

     $updateVault=Http::put( config('api.url').'/api/UpdateVault/'.$auth_user['cawangan'],[
         'masuk'=>$masuk_V,
         'keluar'=>$keluar_V,
         'bakiawal'=>$bakiAwalV,
         'bakiakhir'=>$bakiAkhirV,
         'bakieod'=>$BakiEOD
     ]); 
        
    //--FINAL RESULT...KENA RIDERECT KE PAGE SEBELUMNYA..

       return $this->denominasicalculationserahan($request);
    }

    

    public function laporanGadaian(){
        $auth_user = Auth::user();
        $api_url = config('api.url');
        $cawangan_info = Http::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->datetimepicker1 == null){
            $gadai_date = $current->toDateString();
        }else{
            $split_date = explode('-',$request->datetimepicker1);
            $gadai_date = $split_date[2] . '-' . $split_date[1] . '-' . $split_date[0];        
        }
        $gadai = Http::get(config('api.url'). '/api/gadai/laporan/'. $auth_user['cawangan'] .'/'. $gadai_date)->json();
        $cawangan=Http::get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];
        // dd($gadai);
        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::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->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::get(config('api.url'). '/api/tebus/laporan/'. $auth_user['cawangan'] .'/'. $tebus_date)->json();
        $tebuspengasingan = Http::get(config('api.url'). '/api/tebus/laporanjenistawarruq/'. $auth_user['cawangan'],['tarikh' => $tebus_date])->json();
        $upahlama = Http::get(config('api.url'). '/api/advansurdetails/'. $auth_user['cawangan'],['tarikh' => $tebus_date])->json();

        $cawangan=Http::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','api_url','upahlama'));
    }

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

    public function cetaklaporanLelongan(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::get(config('api.url'). '/api/lelongan/laporan/'. $auth_user['cawangan'] .'/'. $tebus_date)->json();
        $cawangan=Http::get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];

        return view('print.laporan_lelongan', compact('tebus','cawangan_detail','tarikh_cetak','tebus_date','api_url'));
    }

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

        $cawangan_info = Http::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::get(config('api.url'). '/api/ansuran/laporan/'. $auth_user['cawangan'],['tarikh' => $tebus_date])->json();
        $cawangan=Http::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::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::get(config('api.url'). '/api/customers/laporan/'. $auth_user['cawangan'] .'/'. $tebus_date)->json();
        $cawangan=Http::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::get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();

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

      // $data_3=Http::get(config('api.url').'/api/gadai/aging/test/'.$auth_user['cawangan'].'/'.$tarikh_aging.'/'.$num3)->json();
       $data=Http::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::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::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::get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();

       $cawangan=Http::get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
       $cawangan_detail=$cawangan[0];
      // $data_3=Http::get(config('api.url').'/api/gadai/aging/test/'.$auth_user['cawangan'].'/'.$tarikh_aging.'/'.$num3)->json();
       $data=Http::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::get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
        $stokaudit = Http::get(config('api.url'). '/api/gadai/cawangan/'. $auth_user['cawangan'])->json();

        return view('khazanah.laporan.stokaudit',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::get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];

        if($request->radaktif === 'aktif')
        {
            $stokaudit = Http::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::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 laporanBatalSuratGadai(){
        $auth_user = Auth::user();
        $api_url = config('api.url');

        
        $cawangan_info = Http::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::get(config('api.url'). '/api/batalgadai/laporan/'. $auth_user['cawangan'] .'/'. $tebus_date)->json();
        $cawangan=Http::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::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::get(config('api.url'). '/api/laporanpetibesi/'. $auth_user['cawangan'] .'/'. $tebus_date)->json();
        $cawangan=Http::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::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::get(config('api.url'). '/api/marhun/laporan/'. $auth_user['cawangan'] .'/'. $tebus_date)->json();
        $cawangan=Http::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::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::get(config('api.url'). '/api/marhunterkumpul/laporan/'. $auth_user['cawangan'] .'/'. $tebus_date)->json();
        $cawangan=Http::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 laporanRingkasanHarian(){
        $auth_user = Auth::user();
        $api_url = config('api.url');

        
        $cawangan_info = Http::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){
        // dd($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::get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];

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

        $laporan = $laporan_ringkasan[0];

        $kawalanstok = Http::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; echo number_format($totalbilhapuskira); 
    
        $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; echo number_format($totalpinjhapuskira,2); 

        $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 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::get(config('api.url'). '/api/gadai/laporan/ringkasan/'. $auth_user['cawangan'] .'/'. $tebus_date)->json();
        $marhun = $marhun[0];

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

        $cawangan=Http::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 laporanKawalanStok(){
        $auth_user = Auth::user();
        $api_url = config('api.url');

        
        $cawangan_info = Http::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::get(config('api.url'). '/api/kawalanstok/laporan/'. $auth_user['cawangan'] .'/'. $tebus_date)->json();

        $cawangan=Http::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::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();

        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::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::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::get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();

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

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

        if(sizeof($denominasi) == 0)
        {
            $denominasi=Http::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 unjuran()
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');
        $cawangan_info = Http::get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
        return view('khazanah.unjuran',compact('cawangan_info','api_url'));
    }


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

        
        $cawangan_info = Http::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::get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();

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

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



         $data2=Http::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::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();


                            




                

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

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

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

        }else{

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

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

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

        $denominasilist = Http::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::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::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"
    
              ]);
        }

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




    public function laporanImbangduga()
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');
        $cawangan_info = Http::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::get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
        $cawangan=Http::get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
        $cawangan_detail=$cawangan[0];

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

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

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

        //Tanggungan Semasa
        $panjar = Http::get(config('api.url').'/api/CalculationPanjar/' . $auth_user['cawangan'],['tarikh' => $tarikh_imbangan2])->json();
        $bakilelong = Http::get(config('api.url').'/api/BakiLelongCalculation/' . $auth_user['cawangan'],['tarikh' => $tarikh_imbangan2])->json();
        $wtd = Http::get(config('api.url').'/api/WTD/' . $auth_user['cawangan'],['tarikh' => $tarikh_imbangan2])->json();
        $cajlelong = Http::get(config('api.url').'/api/cajlelong/' . $auth_user['cawangan'],['tarikh' => $tarikh_imbangan2])->json();
        $keuntungansebenar = Http::get(config('api.url').'/api/KeuntunganSebenar/' . $auth_user['cawangan'],['tarikh' => $tarikh_imbangan2])->json();
        $cajperkhidmatan = Http::get(config('api.url').'/api/cajperkhidmatanterkumpul/' . $auth_user['cawangan'],['tarikh' => $tarikh_imbangan2])->json();
        
        //TunaiDitangan Sementara Upah
        if($tarikh_imbangan2 > '2021-04-22' && $tarikh_imbangan2 < '2021-05-05' && $cawangan_detail['kodcaw'] == 'KB2')
        {
            $advansurharini = Http::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);
            
        }

        //Terkumpul
        $untungrugiterkumpul = Http::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/241','descpt' => 'Rebet','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($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','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' => '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]);

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

        $untungrugifield = [];
        array_push($untungrugifield,['glcode' => '2000/007','descpt' => 'Keuntungan', '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::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)
    {

            //new glcode  2021-02-22
            $GL_Code_BayaranOnline="1000/210";
            $GL_CodeUntungSebenar="1000/213";
            $GL_CodeHutangArrahn="1000/211";
            $GL_CodeHutangAkru="2000/007";


            $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();

            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];   
            }

            //KIRAAN UNTUK LEJER_AM  BAYARAN ONLINE & TRANSAKSI BAYARAN UPAH

            $getProduct=Http::get(config('api.url').'/api/getProduct/'.$auth_user['cawangan'])->json();
            $glcode=$getProduct[0]['glcode'];
            $kodGL=$getProduct[0]['kodgl'];
            $glcode_UP=$getProduct[0]['glcode_up'];

            $get_GLCode=Http::get(config('api.url').'/api/getGlCode/'.$auth_user['cawangan'],['glcode'=>$kodGL])->json();

            $keterangan=$get_GLCode[0]['descpt'];
            $acctype=$get_GLCode[0]['acttype'];

            $total_bayaranOnline=0.0;


            // $getTotalBayaranOnline=Http::get(config('api.url').'/api/ansuranByrOnline/'.$auth_user['cawangan'])->json();
            // if(sizeof($getTotalBayaranOnline)!=0)
            // {

            // $total_bayaranOnline=$getTotalBayaranOnline[0]['cash_in'];

            // }
            // else
            // {

            // }

            //START UNTUK GLEJER

            //carian_GLejer untuk Bayaran Online  (Update Bayaran)
            $getBayarOnline_GL=Http::get(config('api.url').'/api/getGlCode/'. $auth_user['cawangan'],['glcode'=>$GL_Code_BayaranOnline])->json();    
            $keteranganBayaranOnlie=$getBayarOnline_GL[0]['descpt'];  //keterangan glcode
            $actypeBayaranOnline=$getBayarOnline_GL[0]['acttype'];     //keterangan acctype 

            $get_current_GLejer_Details_BayaranOnline=Http::get(config('api.url').'/api/getGlejer/'. $auth_user['cawangan'],['glcode'=>$GL_Code_BayaranOnline])->json(); 
            if(sizeof($get_current_GLejer_Details_BayaranOnline)==0)
            {  //start kalau xdak data
            $jumlah=0.0;

            $getGLedjer=Http::get(config('api.url').'/api/getSemalam_Glejer/'.$auth_user['cawangan'],['glcode'=>$GL_Code_BayaranOnline])->json();

            if(sizeof($getGLedjer)==0)
            {
                $jumlah=0.0;
            }
            else
            {

                //$glejer=$getGLedjer[0];
                $jumlahSemalam=$getGLedjer[0]['jumlah'];

                $hariIniSemalam=$getGLedjer[0]['hariini'];
                $jumlah=$jumlahSemalam+$hariIniSemalam;


            }



            $jumlahBulan_ini=0.0;
            $get_GlejerBulan_Tahun=Http::get(config('api.url').'/api/getLejerMonthYear/'.$auth_user['cawangan'],['glcode'=>$GL_Code_BayaranOnline])->json();

            if( sizeof($get_GlejerBulan_Tahun)==0)
            {
                $jumlahBulan_ini=0.0;
            }
            else
            {
                $jumlahBulan_ini=$get_GlejerBulan_Tahun[0]['jumlah'];
                $jumlahBulan_ini=$jumlah-$jumlahBulan_ini;
            }//end bulan tahun
            $Jumlah_TahunIni=0.0;
            $get_Glejer_Tahun=Http::get(config('api.url').'/api/getLejerYear/'.$auth_user['cawangan'],['glcode'=>$GL_Code_BayaranOnline])->json();
            if(sizeof($get_Glejer_Tahun)==0)
            {
                $Jumlah_TahunIni=0.0;
            }
            else
            {


                $JumTahunIni=$get_Glejer_Tahun[0]['jumlah'];        
                $Jumlah_TahunIni=$jumlah-$JumTahunIni;

            }

            $HariIni=0.0;
            $kelmarin=$jumlah-$HariIni;
            //insert glejer untuk current date /hari ini
            $daftarGlejer=Http::post(config('api.url').'/api/daftarGlejer/'.$auth_user['cawangan'],[

                'kodcaw' =>$auth_user['cawangan'],
                'glcode' =>$GL_Code_BayaranOnline,
                'descpt' =>$keteranganBayaranOnlie,
                'debit'=>0.0,
                'kredit'=>0.0,
                'hariini'=>$HariIni,
                'kelmarin'=>$kelmarin,
                'jumlah'=>$jumlah,
                'bulanini'=>$jumlahBulan_ini,
                'tahunini'=>$Jumlah_TahunIni,
                'acttype'=> $actypeBayaranOnline

            ]);

            }
            else
            {

                $get_lejer=Http::get(config('api.url').'/api/getGlejer/'.$auth_user['cawangan'],['glcode'=>$glcode])->json();
                
                if(sizeof($get_lejer)>0)
                {
                    $hariIni=$get_lejer[0]['hariini'];
                    $jumlah=$get_lejer[0]['kelmarin'];
                    $kredit=$get_lejer[0]['kredit'];
                    $debit=$get_lejer[0]['debit'];
                    $recno=$get_lejer[0]['recno'];
                    $kredit=$kredit+ $total_bayaranOnline;
                    $Hari_Ini=$debit-$kredit;
                    $Kelmarin=$jumlah-$Hari_Ini;       
                    $updateGlejer=Http::put(config('api.url').'/api/kemasKiniGlejer/'.$auth_user['cawangan'],[
                    'recno'=>$recno,
                    'debit'=>$debit,
                    'kredit'=>$kredit,
                    'hariini'=>$Hari_Ini,
                    'kelmarin'=>$Kelmarin,
                    ]);

                }

                $cawangan_info = Http::get(config('api.url') . '/api/cawangan/'. $auth_user['cawangan'])->json();
                $cawangan=Http::get(config('api.url').'/api/cawangan/detail/'.$auth_user['cawangan'])->json();
                $cawangan_detail=$cawangan[0];
                $debit=Http::get(config('api.url').'/api/glejer/sumDebit/'.$tarikh_mula.'/'.$tarikh_akhir.'/'.$auth_user['cawangan'])->json();
                $kredit=Http::get(config('api.url').'/api/glejer/sumKredit/'.$tarikh_mula.'/'.$tarikh_akhir.'/'.$auth_user['cawangan'])->json();
                $data=Http::get(config('api.url').'/api/glejer/getdata/'.$tarikh_mula.'/'.$tarikh_akhir.'/'.$auth_user['cawangan'])->json();
            }

            //return view('print.laporan_imbanduga',compact('tarikh_imbangan','tarikh_cetak','cawangan_info','cawangan','api_url','cawangan_detail'));
            return view( 'print.laporan_lejer_am',compact('cawangan','cawangan_info', 'api_url','cawangan_detail','tarikh_akhir','tarikh_mula','data','debit','kredit'));


    }


    public function laporan_Perbelanjaan(Request $request)
    {
        $auth_user = Auth::user();
        $api_url = config('api.url');
        $cawangan_info = Http::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::get(config('api.url'). '/api/SenaraiPanjar/' . $auth_user['cawangan'] . '/' . $mula_date . '/' . $hingga_date)->json();

        $cawangan = Http::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::get(config('api.url'). '/api/SenaraiPanjar/listeach/' . $auth_user['cawangan'],['nobaucer' => $baucer])->json();

        $cawangan = Http::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)
    {
       $kodcaw = $request->kodcaw;
       $nombor = $request->no;
       $kodlaluan = $request->kodlaluan;

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

        event(new PendahuluanSerahan("Batal"));

        return response('Berjaya');
    }

}
