<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Services\CustomerService;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Auth;
use Carbon\Carbon;


class AliranTunaiController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index(){
        $harini = Carbon::now();
        $harini = new Carbon;

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

        if(sizeof($alirantunai) == 0)
        {

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


            if(sizeof($latesttunai) > 0)
            {

                if($latesttunai[0]['baki'] != 0)
                {
                    $alirantunai = Http::withOptions(['verify' => config('app.api_verify')])->post(config('api.url') . '/api/tunai/daftarTunai/' . $auth_user['cawangan'], [
                        'kodcaw' => $auth_user['cawangan'],
                        'teller' => $auth_user['name'],
                        'bakiawal' => $latesttunai[0]['baki'],
                        'masuk' => 0.00,
                        'yuran' => 0.00,
                        'keluar' => 0.00,
                        'baki' => $latesttunai[0]['baki'],
                        'tambah' => 0.00,
                        'kurang' => 0.00
                    ])->json();
                }
                else
                {
                    $alirantunai = Http::withOptions(['verify' => config('app.api_verify')])->post(config('api.url') . '/api/tunai/daftarTunai/' . $auth_user['cawangan'], [
                        'kodcaw' => $auth_user['cawangan'],
                        'teller' => $auth_user['name'],
                        'bakiawal' => 0.00,
                        'masuk' => 0.00,
                        'yuran' => 0.00,
                        'keluar' => 0.00,
                        'baki' => 0.00,
                        'tambah' => 0.00,
                        'kurang' => 0.00
                    ])->json();
                }


            }
            else{
                $alirantunai = Http::withOptions(['verify' => config('app.api_verify')])->post(config('api.url') . '/api/tunai/daftarTunai/' . $auth_user['cawangan'], [
                    'kodcaw' => $auth_user['cawangan'],
                    'teller' => $auth_user['name'],
                    'bakiawal' => 0.00,
                    'masuk' => 0.00,
                    'yuran' => 0.00,
                    'keluar' => 0.00,
                    'baki' => 0.00,
                    'tambah' => 0.00,
                    'kurang' => 0.00
                ])->json();
            }
        }

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

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

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

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


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

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

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

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

        $requestkodlaluan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/get/pengguna/'.$auth_user['cawangan'],["namateller" => Auth::user()->name ])->json();
        $advansur = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/AnsuranByTarikh/'.$auth_user['cawangan'],['kodlaluan' => Auth::user()->name])->json();

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

        $proses_modal = Http::withOptions(['verify' => config('app.api_verify')])->get( config('api.url') .'/api/requestTeller/checkproses',['kodlaluan' => Auth::user()->name,'kodcaw' => $auth_user['cawangan']])->json();
        
        $countproses = sizeof($proses_modal);

        return view('aliran_tunai.index',compact('cawangan_info','alirantunai','api_url','GT','denominasi','gadai_tebus','tuntutbaki','advansur','addtunai','tunaipast','countproses'));
    }

    public function alirantunaiGadai(Request $request,$teller){

        $auth_user = Auth::user();

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

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

        $keluar = $getTunai2['keluar'];
        $bakiakhir = $getTunai2['baki'];
        $masuk = $getTunai2['masuk'];

        $bakiakhir -= $request->pinjaman;


        //masuk dalam tunai aliran
        $updateTunai = Http::withOptions(['verify' => config('app.api_verify')])->put( config('api.url').'/api/updateTunaiGT/'.$auth_user['cawangan'], [
            'kodlaluan' => $teller,
            'keluar' => $keluar,
            'masuk' => $masuk,
            'bakiakhir' => $bakiakhir
        ]);

        return response()->json($updateTunai,201);

    }

    public function alirantunaiTebus(Request $request){

        $auth_user = Auth::user();

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

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

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

        $keluar=$getTunai2['keluar'];
        $baki=$getTunai2['baki'];
        $masuk=$getTunai2['masuk'];
        $bakiakhir = $baki;

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

             $totalkeluar = $tebus_detail['addpinjaman'] + $keluar;
             $totalmasuk = $tebus_detail['bakiupah'] + $masuk;

            //  $bakiakhir = $bakiawal-$totalkeluar+$totalmasuk+$pendahuluan-$serahan;
                $bakiakhir = $bakiakhir + $tebus_detail['bakiupah'] - $tebus_detail['addpinjaman'];


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

        }else{

            if($tebus_detail['jenistebus'] == 'S')
            {
                $totalmasuk = ($tebus_detail['bakipjm'] - $request['pinjaman']) + $masuk;
                $bakiakhir = $bakiakhir + ($tebus_detail['bakipjm'] - $request['pinjaman']);
            }else{
                $totalmasuk = $tebus_detail['bakipjm'] + $masuk;
                $bakiakhir = $bakiakhir + $tebus_detail['bakipjm'];
            }

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

    }

    public function alirantunaiTebusAutoTawarruq(Request $request){

        $auth_user = Auth::user();

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

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

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

        $keluar=$getTunai2['keluar'];
        $baki=$getTunai2['baki'];
        $masuk=$getTunai2['masuk'];
        $bakiakhir = $baki;

        $bakiakhir = $bakiakhir + $request->totalbayaran;

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

    public function alirantunaiUpahBknTawarruq(Request $request){

        $auth_user = Auth::user();

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

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

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

        $keluar=$getTunai2['keluar'];
        $baki=$getTunai2['baki'];
        $masuk=$getTunai2['masuk'];
        $bakiawal=$getTunai2['bakiawal'];
        $pendahuluan=$getTunai2['tambah'];
        $serahan=$getTunai2['kurang'];
        $totalmasuk = 0;
        $bakiakhir = $baki;
        $totalkeluar = 0;

        $totalmasuk = $advansur['upahDibayar'] + $masuk;
        $bakiakhir = $bakiakhir + $advansur['upahDibayar'];

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

    }

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

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

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

        $tarikh_cetak = $current->toDateString();

        $array_requestteller = [];
       //wang Syilling Initialize
       $rekodsen1 = 0; $rekodsen5 = 0; $rekodsen10 = 0;
       $rekodsen20 = 0;  $rekodsen50 = 0;

       //Wang Ringgit Initialize
       $rekodring1 = 0;  $rekodring5 = 0;  $rekodring10 = 0;
       $rekodring20 = 0; $rekodring50 = 0; $rekodring100 = 0;

        $requestteller = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/requestTeller/getlaporan/'.$auth_user['cawangan'],["teller" => Auth::user()->name ])->json();

        foreach($requestteller as $index => $reqtel)
        {
            $rekodsen1 += $reqtel['txtsen1'];
            $rekodsen5 += $reqtel['txtsen5'];
            $rekodsen10 += $reqtel['txtsen10'];
            $rekodsen20 += $reqtel['txtsen20'];
            $rekodsen50 += $reqtel['txtsen50'];

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

        array_push($array_requestteller,[ "rekodsen1" => $rekodsen1,"rekodsen5" => $rekodsen5,"rekodsen10" => $rekodsen10,"rekodsen20" => $rekodsen20,"rekodsen50" => $rekodsen50
                                        ,"rekodring1" => $rekodring1,"rekodring5" => $rekodring5,"rekodring10" => $rekodring10,"rekodring20" => $rekodring20,"rekodring50" => $rekodring50,"rekodring100" => $rekodring100]);

        return view('aliran_tunai.laporan.laporanserahan',compact('tarikh_cetak','cawangan_info','cawangan','api_url','cawangan_detail','array_requestteller'));
    }

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

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

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

        $tarikh_cetak = $current->toDateString();

        // $array_requestteller = [];
       //wang Syilling Initialize
       $rekodsen1 = 0; $rekodsen5 = 0; $rekodsen10 = 0;
       $rekodsen20 = 0;  $rekodsen50 = 0;

       //Wang Ringgit Initialize
       $rekodring1 = 0;  $rekodring5 = 0;  $rekodring10 = 0;
       $rekodring20 = 0; $rekodring50 = 0; $rekodring100 = 0;

        $requestteller = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/requestTeller/getlaporan/'.$auth_user['cawangan'],["teller" => Auth::user()->name ])->json();

        if(sizeof($requestteller) > 0)
        {
            $size = sizeof($requestteller) - 1;
            $array_requestteller = $requestteller[$size];
        }

        return view('aliran_tunai.laporan.laporanserahanberkala',compact('tarikh_cetak','cawangan_info','cawangan','api_url','cawangan_detail','array_requestteller'));
    }

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

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

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

        $keluar = $alirantunai['keluar'];
        $baki = $alirantunai['baki'];
        $masuk = $alirantunai['masuk'];
        $bakiawal = $alirantunai['bakiawal'];
        $pendahuluan = $alirantunai['tambah'];
        $serahan = $alirantunai['kurang'];

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

        $gadai = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/gadai/laporan/'. $auth_user['cawangan'] .'/'. $current->toDateString())->json();
        $tebus = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/tebus/laporan/'. $auth_user['cawangan'] .'/'. $current->toDateString())->json();
        $ansuran = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/ansuran/laporan/'. $auth_user['cawangan'],['tarikh' => $current->toDateString()])->json();
        $tuntutbaki = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url'). '/api/TuntutBaki/'. $auth_user['cawangan'] . '/teller/',['teller' => Auth::user()->name])->json();
        $advansur = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/AnsuranByTarikh/'.$auth_user['cawangan'],['kodlaluan' => Auth::user()->name ])->json();

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

        $lelongan = 0;
        if(strtoupper(Auth::user()->name) === 'LELONG')
        {
            $lelongan = floatval($lelong);
        }else{
            $lelongan = 0;
        }

        $totalserahan = 0;
        $totalpendahuluan = 0;
        foreach($addtunai as $index1=>$at)
        {
            if($at['tambah'] > 0)
            {
                $totalpendahuluan += $at['tambah'];
            }

            if($at['kurang'] > 0)
            {
                $totalserahan += $at['kurang'];
            }
        }

        $totalgadai = 0;
        $totalgadaibarcodescanned = 0;
        foreach($gadai as $index1=>$gad)
        {
            if($gad['teller'] === Auth::user()->name)
            {
                if($gad['semakbarcode'] == 'Lulus')
                    $totalgadaibarcodescanned += $gad['pinjaman'];

                $totalgadai += $gad['pinjaman'];
            }
        }

        $totaltebus = 0;
        $totaltebusbarcodescanned = 0;

        foreach($tebus as $index1=>$teb)
        {
            if($teb['teller'] === Auth::user()->name)
            {
                if($teb['tagterima'] === 1){
                    if($teb['jenistebus'] === 'S')
                    {
                        $totaltebusbarcodescanned += $teb['gadaibakipjm'] + $teb['bakiupah'];
                    }else{
                        $totaltebusbarcodescanned += $teb['bakipjm'];
                    }
                }

                if($teb['jenistebus'] === 'S')
                {
                    $totaltebus += $teb['gadaibakipjm'] + $teb['bakiupah'];
                }else{
                    $totaltebus += $teb['bakipjm'];
                }
            }

        }

        $totalansuran = 0;
        foreach($ansuran as $index1=>$ans)
        {
            if($ans['teller'] === Auth::user()->name)
                $totalansuran += $ans['duit_masuk'];
        }

        $totalupahlama = 0;
        foreach($advansur as $index1=>$upahlama)
        {
            $totalupahlama += $upahlama['upahDibayar'];
        }

        $totaltuntutbaki = 0;
        $bakilelong = 0;
        $rugilelong = 0;
        foreach($tuntutbaki as $index1=>$tb)
        {
            if($tb['jenis'] == 'T')
            {
                $bakilelong += $tb['jumlah'];
            }else{
                $rugilelong += -($tb['jumlah']);
            }

            $totaltuntutbaki += $tb['jumlah'];
        }

        $penebusan = floatval($totaltebus) + floatval($totalansuran) + floatval($totalupahlama);
        $penebusanbarcodescanned = floatval($totaltebusbarcodescanned) + floatval($totalansuran) + floatval($totalupahlama);
        $gadaian = floatval($totalgadai);

        if(strtoupper(Auth::user()->name) === 'LELONG'){
            $penebusanbarcodescanned = floatval($lelongan);
            $bakisemasa = floatval($bakiawal) + floatval($totalpendahuluan) + floatval($lelongan) - floatval($totalserahan);
        }else{
            $bakisemasa = floatval($bakiawal) + floatval($totalpendahuluan) + floatval($penebusan) - (floatval($gadaian) + floatval($totalserahan)) - floatval($totaltuntutbaki);
        }

        $errorpendahuluan = false; $errorserahan = false; $errorpenebusan = false; $errorgadaian = false; $errorbaki = false;

        if(number_format($penebusanbarcodescanned,2) !== number_format($masuk,2))
            $errorpenebusan = true;

        if(number_format(round($bakisemasa,2),2) !== number_format(round($baki,2),2))
            $errorbaki = true;

        if(number_format($totalpendahuluan,2) != number_format($pendahuluan,2))
            $errorpendahuluan = true;

        if(number_format($totalserahan,2) != number_format($serahan,2))
            $errorserahan = true;

        if(number_format($totalgadaibarcodescanned,2) != number_format($keluar,2))
            $errorgadaian = true;

        if($errorpenebusan === false && $errorgadaian === false && $errorbaki === false)
        {
            return response('sama');
        }else{
            $arraykemaskini = ['pendahuluan' => round($totalpendahuluan,2),'serahan' => round($totalserahan,2), 'penebusan' => round($penebusanbarcodescanned,2), 'gadaian' => round($totalgadaibarcodescanned,2), 'bakisemasa' => round($bakisemasa,2)];
            return response()->json($arraykemaskini);
        }
    }

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


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

        $keluar = $alirantunai['keluar'];
        $baki = $alirantunai['baki'];
        $masuk = $alirantunai['masuk'];
        $bakiawal = $alirantunai['bakiawal'];
        $pendahuluan = $alirantunai['tambah'];
        $serahan = $alirantunai['kurang'];

        $updateTunai = Http::withOptions(['verify' => config('app.api_verify')])->put( config('api.url').'/api/updateTunaiGT/'.$auth_user['cawangan'], [
                        'kodlaluan' => $auth_user['name'],
                        'keluar'=> $request->gadaian,
                        'masuk'=> $request->penebusan,
                        'bakiakhir'=> $request->bakiakhir
        ]);

        if($updateTunai->status() == 200)
        {
            return response('berjaya');
        }else{
            return response('gagal');
        }
    }

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

        $requestkodlaluan = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/get/pengguna/'.$auth_user['cawangan'],["namateller" => Auth::user()->name ])->json();

        $perbezaan = 0;

        if(Session::has('upahsemasa'))
        {
            $upah = Session::get('upahsemasa');

            $requestupah = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/ansuranupah/' . $auth_user['cawangan'],['kodlaluan' => $requestkodlaluan[0]['kodlaluan']])->json();


            if($upah !== $requestupah)
            {
                $perbezaan = $requestupah - $upah;

                Session::put('upahsemasa', $requestupah);

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

                $masuk = $requestalirantunai['masuk'];
                $keluar = $requestalirantunai['keluar'];
                $baki = $requestalirantunai['baki'];

                $total = floatval($baki) + $perbezaan;

                // masuk dalam tunai aliran
                $updateTunai=Http::withOptions(['verify' => config('app.api_verify')])->put( config('api.url').'/api/updateTunaiGT/'.$auth_user['cawangan'], [
                    'kodlaluan' => Auth::user()->name,
                    'keluar' => $keluar,
                    'masuk' => $masuk,
                    'bakiakhir' => $total
                ])->json();

                $updateTunai = 1;
            }else{
                $updateTunai = 0;
            }

        }else{
            $requestupah = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/ansuranupah/' . $auth_user['cawangan'],['kodlaluan' => $requestkodlaluan[0]['kodlaluan']])->json();

            if($requestupah !== 0)
            {
                Session::put('upahsemasa', $requestupah);

                $requestalirantunai = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/GetCurrentTunai/' . $auth_user['cawangan'],['kodlaluan' => Auth::user()->name])->json();
                $masuk = $requestalirantunai['masuk'];
                $keluar = $requestalirantunai['keluar'];

                $baki = $requestalirantunai['baki'];

                $total = floatval($baki) + floatval($requestupah);

                // masuk dalam tunai aliran
                $updateTunai=Http::withOptions(['verify' => config('app.api_verify')])->put( config('api.url').'/api/updateTunaiGT/'.$auth_user['cawangan'], [
                    'kodlaluan' => Auth::user()->name,
                    'keluar' => $keluar,
                    'masuk' => $masuk,
                    'bakiakhir' => $total
                ])->json();

                $updateTunai = 1;
            }else{
                $updateTunai = 0;
            }
        }

        return response($updateTunai);
    }

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

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

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

        $tarikh_cetak = $current->toDateString();

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

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

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

        $teller = $auth_user['name'];

        return view('aliran_tunai.laporan.laporanalirantunai',compact('tarikh_cetak','cawangan_info','cawangan','api_url','cawangan_detail','addtunai','alirantunai','teller'));
    }

    public function LaporanTunaiTeller(Request $request){


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

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

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

        $tarikh_cetak = $current->toDateString();

        $array_requestteller = [];
       //wang Syilling Initialize
       $rekodsen1 = 0; $rekodsen5 = 0; $rekodsen10 = 0;
       $rekodsen20 = 0;  $rekodsen50 = 0;

       //Wang Ringgit Initialize
       $rekodring1 = 0;  $rekodring5 = 0;  $rekodring10 = 0;
       $rekodring20 = 0; $rekodring50 = 0; $rekodring100 = 0;

        $requestteller = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url').'/api/requestTeller/getrekod/'.$auth_user['cawangan'],["teller" => Auth::user()->name,"recno" => $request->recno ])->json();
        if($requestteller[0]['modal'] != 0){
            $jenisserah = 'pendahuluan';
        }else{
            $jenisserah = 'serahan';
        }

        foreach($requestteller as $index => $reqtel)
        {
            $rekodsen1 += $reqtel['txtsen1'];
            $rekodsen5 += $reqtel['txtsen5'];
            $rekodsen10 += $reqtel['txtsen10'];
            $rekodsen20 += $reqtel['txtsen20'];
            $rekodsen50 += $reqtel['txtsen50'];

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

        array_push($array_requestteller,[ "rekodsen1" => $rekodsen1,"rekodsen5" => $rekodsen5,"rekodsen10" => $rekodsen10,"rekodsen20" => $rekodsen20,"rekodsen50" => $rekodsen50
                                        ,"rekodring1" => $rekodring1,"rekodring5" => $rekodring5,"rekodring10" => $rekodring10,"rekodring20" => $rekodring20,"rekodring50" => $rekodring50,"rekodring100" => $rekodring100]);

        return view('aliran_tunai.laporan.laporanrekodteller',compact('tarikh_cetak','cawangan_info','cawangan','api_url','cawangan_detail','array_requestteller','jenisserah'));
    }

    public function LaporanAccStatement(Request $request){

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

        $tarikh_cetak = $current->toDateString();

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

        $tarikh = date("Y-m-d", $request->tarikh);

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

        return view('aliran_tunai.laporan.account_statement',compact('tarikh_cetak','tarikh','cawangan_info','cawangan','api_url','cawangan_detail','acc_statement'));
    }


}
