<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\AliranTunaiController;

use Carbon\Carbon;

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

    }

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

        $gadaidetail = Http::get(config('api.url') . '/api/gadai/norujukan/'. $norujukan)->json();
        $noic = $gadaidetail[0][0]['nokp'];
        $customerdetail = Http::get(config('api.url') . '/api/customers/'. $noic)->json();
        $marhundetail = Http::get(config('api.url') . '/api/marhun/'. $norujukan)->json();

        $marhun = $marhundetail[0];
        $nama = $customerdetail[0]['nama'];

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

        $nilaihargasemasa = $this->tukaranhargaemas($norujukan);

        return view('penebusan.index',compact('cawangan_info','api_url','gadai','marhun','nama','nilaihargasemasa'));
    }

    public function tebuspenuh(Request $request)
    {
        $api_url = config('api.url');

        $updatetebus = Http::put(config('api.url').'/api/updatetebus/'. $request->kodcaw .'/' . $request->norujukan,[
            "nopelanggan" => $request->nopelanggan,
            "jenistebus" => $request->jenistebus,
            "jbg" => $request->jbg,
            "kadarupah" => $request->kadarupah,
            "nilai_marhun" => $request->nilai_marhun,
            "pinjaman" => $request->pinjaman,
            "ansuran" => $request->ansuran,
            "addpinjaman" => $request->addpinjaman,
            "bakipinjaman" => $request->bakipinjaman,
            "bakiupah" => $request->bakiupah,
            "historygadaian" => $request->historygadaian,
            "marhun" => $request->marhun,
            "nowakil" => $request->nowakil,
            "jenisbayarantebus" => $request->jenisbayarantebus,
            "teller" => $request->teller
        ])->json();

        if($updatetebus)
        {
            return response('success');
        }else{
            return response('fail');
        }
    }

    public function bayaransuransTawarruq(Request $request)
    {
        $api_url = config('api.url');

        $updategadai = Http::put(config('api.url').'/api/gadaikaunter/'. $request->kodcaw .'/norujukan/' . $request->norujukan,[
            'bayaran' => $request->bayaran,
            'teller' => $request->teller
        ])->json();

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

        $keluar=$getTunai2['keluar'];
        $baki=$getTunai2['baki'];
        $masuk=$getTunai2['masuk'];
        $bakiawal=$getTunai2['bakiawal'];
        $pendahuluan=$getTunai2['tambah'];
        $serahan=$getTunai2['kurang'];
        $totalmasuk = 0;
        $bakiakhir = 0;
 
        $totalmasuk = $request->bayaran + $masuk;
        
        // $bakiakhir = $bakiawal-$keluar+$totalmasuk+$pendahuluan-$serahan;
        $bakiakhir = $baki + $request->bayaran;
        
        // Add on 20210411
        $gadaidetail = Http::get(config('api.url') . '/api/gadai/norujukan/'. $request->kodcaw.'/'.$request->norujukan)->json();
        $tempohGadai=$gadaidetail[0]['tempoh'];
        $upah12bulan=$gadaidetail[0]['upah12'];
        $bakiPinjaman=$gadaidetail[0]['bakipjm'];
        $teller=$gadaidetail[0]['teller'];
        $upah6bulan= $upah12bulan/2;                   //add on 202104011
        $tempoh_bayar=$gadaidetail[0]['tempohbayar']; //add on 202104011
        $tarikhgadai=$gadaidetail[0]['t_gadai'];//Add on 20210411

        //masuk dalam tunai aliran
        $updateTunai=Http::put( config('api.url').'/api/updateTunaiGT/'.$request->kodcaw, [
            'kodlaluan' => $request->teller,
            'keluar' => $keluar,
            'masuk' => $totalmasuk,
            'bakiakhir' => $bakiakhir
        ]);

        //************** KIRAAN LEJER AM BERMULA ****************


        $Bayaran_Pendahuluan='1000/019';
        $GL_CODE='1000/010';
        $KOD_PINJAMAN='1000/017';
        $KOD_UT='1000/211';
        $KOD_US='2000/007';
        $Untung_Semasa='5000/017';
        $KOD_ANS='5000/511';

        

        $jumlahBayaran_ansuran=Http::get(config('api.url').'/api/ansuranByrKaunter/'.$request->kodcaw.'/'.$request->norujukan)->json();
        $jumlah_telah_bayaransuran=$jumlahBayaran_ansuran[0]['cash_in'];
        $jumbayaran=$request->bayaran;

        $Transaksi_keuntungan=Http::get(config('api.url').'/api/transaksi_keuntungan/'.$request->norujukan)->json();
        $bayaran_keuntungan=$Transaksi_keuntungan[0]['bayaran_keuntungan'];
        $bayaran_pembiayaan=$Transaksi_keuntungan[0]['bayaran_pembiayaan'];
        $tarikh_bayaran=$Transaksi_keuntungan[0]['tarikh_bayaran'];

            //tahun gadaian
        $tahun_gadai=substr($tarikhgadai,0,4);
        $month_gadai=substr($tarikhgadai, 5,2);
        // bulan gadaian
        $year_bayar=substr($tarikh_bayaran, 0, 4);
        $month_bayar=substr($tarikh_bayaran, 5,2);


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

  
        
              //ansuran tawaruq
         //check sama ada jumlah bayaran melebihi keuntungan semasa..kalau lebih dia akan masuk dalam bayaran pendahuluan...
         if($jumbayaran<$bayaran_keuntungan)
          {

            $LejerTunaiDitangan=Http::get(config('api.url').'/api/getGlejer/'.$request->kodcaw,['glcode'=> $GL_CODE])->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'];

            
            $debit_Tunai=$debit_Tunai+$jumbayaran+$bakiPinjaman;
            $kredit_Tunai=$kredit_Tunai+$bakiPinjaman;

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

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

          $hariini_Pinj=$get_Pinj[0]['hariini'];
          $kelmarin_Pinj=$get_Pinj[0]['kelmarin'];
          $jumlah_Pinj=$get_Pinj[0]['jumlah'];
          $debit_Pinj=$get_Pinj[0]['debit'];
          $recno_Pinj=$get_Pinj[0]['recno'];
          $kredit_Pinj=$get_Pinj[0]['kredit'];

          $kredit_Pinj=$kredit_Pinj+$bakiPinjaman;
           $debit_Pinj=$debit_Pinj+$bakiPinjaman;

          $hariini_Pinj=$debit_Pinj-$kredit_Pinj;
          $kelmarin_Pinj=$jumlah_Pinj-$hariini_Pinj;


          $updateGlejer_Pinj=Http::put(config('api.url').'/api/kemasKiniGlejer/'.$request->kodcaw,[
            'recno'=>$recno_Pinj,
            'debit'=>$debit_Pinj,
            'kredit'=>$kredit_Pinj,
            'hariini'=>$hariini_Pinj,
            'kelmarin' =>$kelmarin_Pinj,     

        ]);


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

            $hariini_UT=$get_lejerUT[0]['hariini'];
            $kelmarin_UT=$get_lejerUT[0]['kelmarin'];
            $jumlah_UT=$get_lejerUT[0]['jumlah'];
            $debit_UT= $get_lejerUT[0]['debit'];
            $recno_UT= $get_lejerUT[0]['recno'];
            $kredit_UT= $get_lejerUT[0]['kredit'];


            $debit_UT=$debit_UT-$bayaran_latest_untung;
            $hariini_UT=$debit_UT-$kredit_UT;
            $kelmarin_UT=$jumlah_UT-$hariini_UT;

            $updateGlejer_UT=Http::put(config('api.url').'/api/kemasKiniGlejer/'.$request->kodcaw,[
                'recno'=>$recno_UT,
                'debit'=>$debit_UT,
                'kredit'=>$kredit_UT,
                'hariini'=>$hariini_UT,
                'kelmarin' =>$kelmarin_UT,     

            ]);

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

            $hariini_US=$get_lejerUS[0]['hariini'];
            $kelmarin_US=$get_lejerUS[0]['kelmarin'];
            $jumlah_US=$get_lejerUS[0]['jumlah'];
            $debit_US= $get_lejerUS[0]['debit'];
            $recno_US= $get_lejerUS[0]['recno'];
            $kredit_US= $get_lejerUS[0]['kredit'];


            // $kredit_US=$kredit_US-$bayaran_keuntungan;
            $kredit_US=$kredit_US-$bayaran_latest_untung;
            $hariini_US=$debit_US-$kredit_US;
            $kelmarin_US=$jumlah_US-$hariini_US;

            $updateGlejer_US=Http::put(config('api.url').'/api/kemasKiniGlejer/'.$request->kodcaw,[
            'recno'=>$recno_US,
            'debit'=>$debit_US,
            'kredit'=>$kredit_US,
            'hariini'=>$hariini_US,
            'kelmarin' =>$kelmarin_US,     

            ]);

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

            $get_lejer_Untung=Http::get(config('api.url').'/api/getGlejer/'.$request->kodcaw,['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/'.$request->kodcaw,[
                'recno'=>$recno_untung,
                'debit'=>$debit_untung,
                'kredit'=>$kredit_untung,
                'hariini'=>$hariini_untung,
                'kelmarin' =>$kelmarin_untung,     

        ]);

        }
            
        else
        {
            $LejerTunaiDitangan=Http::get(config('api.url').'/api/getGlejer/'.$request->kodcaw,['glcode'=> $GL_CODE])->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'];

            $debit_Tunai=$debit_Tunai+$jumbayaran+$bakiPinjaman;
            $kredit_Tunai=$kredit_Tunai+$bakiPinjaman;

            $hariini_Tunai=$debit_Tunai-$kredit_Tunai;
            $kelmarin_Tunai=$jumlah_Tunai-$hariini_Tunai;

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

            ]);



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

            $hariini_Pinj=$get_Pinj[0]['hariini'];
            $kelmarin_Pinj=$get_Pinj[0]['kelmarin'];
            $jumlah_Pinj=$get_Pinj[0]['jumlah'];
            $debit_Pinj=$get_Pinj[0]['debit'];
            $recno_Pinj=$get_Pinj[0]['recno'];
            $kredit_Pinj=$get_Pinj[0]['kredit'];
  

            $kredit_Pinj=$kredit_Pinj+$bakiPinjaman;
            $debit_Pinj=$debit_Pinj+$bakiPinjaman;
            $hariini_Pinj=$debit_Pinj-$kredit_Pinj;
            $kelmarin_Pinj=$jumlah_Pinj-$hariini_Pinj;

            $updateGlejer_Pinj=Http::put(config('api.url').'/api/kemasKiniGlejer/'.$request->kodcaw,[
              'recno'=>$recno_Pinj,
              'debit'=>$debit_Pinj,
              'kredit'=>$kredit_Pinj,
              'hariini'=>$hariini_Pinj,
              'kelmarin' =>$kelmarin_Pinj,     
  
          ]);
  

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

            $hariini_UT=$get_lejerUT[0]['hariini'];
            $kelmarin_UT=$get_lejerUT[0]['kelmarin'];
            $jumlah_UT=$get_lejerUT[0]['jumlah'];
            $debit_UT= $get_lejerUT[0]['debit'];
            $recno_UT= $get_lejerUT[0]['recno'];
            $kredit_UT= $get_lejerUT[0]['kredit'];

            $debit_UT=$debit_UT-$bayaran_latest_untung;
            $hariini_UT=$debit_UT-$kredit_UT;
            $kelmarin_UT=$jumlah_UT-$hariini_UT;

            $updateGlejer_UT=Http::put(config('api.url').'/api/kemasKiniGlejer/'.$request->kodcaw,[
            'recno'=>$recno_UT,
            'debit'=>$debit_UT,
            'kredit'=>$kredit_UT,
            'hariini'=>$hariini_UT,
            'kelmarin' =>$kelmarin_UT,     

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

            $hariini_US=$get_lejerUS[0]['hariini'];
            $kelmarin_US=$get_lejerUS[0]['kelmarin'];
            $jumlah_US=$get_lejerUS[0]['jumlah'];
            $debit_US= $get_lejerUS[0]['debit'];
            $recno_US= $get_lejerUS[0]['recno'];
            $kredit_US= $get_lejerUS[0]['kredit'];

            $kredit_US=$kredit_US- $bayaran_latest_untung;
            $hariini_US=$debit_US-$kredit_US;
            $kelmarin_US=$jumlah_US-$hariini_US;

            $updateGlejer_US=Http::put(config('api.url').'/api/kemasKiniGlejer/'.$request->kodcaw,[
            'recno'=>$recno_US,
            'debit'=>$debit_US,
            'kredit'=>$kredit_US,
            'hariini'=>$hariini_US,
            'kelmarin' =>$kelmarin_US,     

        ]);


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

            $hariini_BP=$get_lejerBP[0]['hariini'];
            $kelmarin_BP=$get_lejerBP[0]['kelmarin'];
            $jumlah_BP=$get_lejerBP[0]['jumlah'];
            $debit_BP= $get_lejerBP[0]['debit'];
            $recno_BP= $get_lejerBP[0]['recno'];
            $kredit_BP= $get_lejerBP[0]['kredit'];


            $kredit_BP=$debit_BP+$bayaran_pembiayaan;
            $hariini_BP=$debit_BP-$kredit_BP;
            $kelmarin_BP=$jumlah_BP-$hariini_BP;

            $updateGlejer_BP=Http::put(config('api.url').'/api/kemasKiniGlejer/'.$request->kodcaw,[
            'recno'=>$recno_BP,
            'debit'=>$debit_BP,
            'kredit'=>$kredit_BP,
            'hariini'=>$hariini_BP,
            'kelmarin' =>$kelmarin_BP,     

        ]);

            $get_latest_bayarUntung=Http::get(config('api.url').'/api/latest_transaksi_untung/data/'.$request->norujukan)->json();
            $bayaran_latest_untung=$get_latest_bayarUntung[0]['bayaran_keuntungan'];
            $get_lejer_Untung=Http::get(config('api.url').'/api/getGlejer/'.$request->kodcaw,['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/'.$request->kodcaw,[
            'recno'=>$recno_untung,
            'debit'=>$debit_untung,
            'kredit'=>$kredit_untung,
            'hariini'=>$hariini_untung,
            'kelmarin' =>$kelmarin_untung,     

            ]);

            }

        // end kiraan lejer am untuk tawaruq


        //***************KIRAAN LEJER AM TAMAT*******************************

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

    public function bayaransurans(Request $request)
    {
        $api_url = config('api.url');
        $current = Carbon::now();
        $current = new Carbon();
        $curent_year = substr($current->year,2,2);
        $curent_month = sprintf("%02d", $current->month);
        $current_day = sprintf("%02d", $current->day);

        $auth_user = Auth::user();

        ////////////////////////
       /// UPDATE TEBUS API////
       ///////////////////////
       $updatetebus = Http::put(config('api.url').'/api/updatetebus/'. $auth_user['cawangan'] .'/' . $request->norujukan,[
            'nopelanggan' => $request->nopelanggan,
            'jenistebus' => $request->jenistebus,
            'jbg' => $request->jbg,
            'kadarupah' => $request->kadarupah,
            'nilai_marhun' => $request->nilai_marhun,
            'pinjaman' => $request->pinjaman,
            'ansuran' => $request->ansuran,
            'addpinjaman'=> $request->addpinjaman,
            'bakipinjaman' => $request->bakipinjaman,
            'bakiupah' => $request->bakiupah,
            'nowakil' => $request->nowakil,
            'historygadaian' => $request->historygadaian,
            'marhun' => $request->marhun,
            'jenisbayarantebus' => $request->jenisbayarantebus,
            'teller' => $request->teller
        ])->json();

        //////////////////////
       /// MAKING NEW SAG////
       /////////////////////
       $gadai_transaction = Http::get(config('api.url') . '/api/gadai/'. $auth_user['cawangan'] . '/' . $current->toDateString())->json();
       if($gadai_transaction){
           $new_sirig = $gadai_transaction['sirig'] + 1;
           
       }else{
           $new_sirig = 1;
       }

       $new_transaction = sprintf("%04d",$new_sirig);
       $norujukanbaru = strtoupper($auth_user['cawangan']) . $curent_year . $curent_month . $current_day . '-' .$new_transaction;

       // dd($norujukan);
       $gadai = Http::post(config('api.url').'/api/gadai/'.$auth_user['cawangan'],[
           'norujukan' => $norujukanbaru,
           'nopelanggan' => $request->nopelanggan,
           'nokp' => $request->noic,
           'sirig' => $new_sirig
       ]);

      //////////////////////
      ////Update SAG BARU///
      /////////////////////

      $total_nilai_marhun = $request->nilai_marhun;
      $margin_pinjaman = $request->pinjaman / $total_nilai_marhun;
      $kadarupah = $request->kadarkeuntungan * 100;
      $keuntungan6bulan = $request->keuntungan_6bln;
      $keuntungan12bulan = $request->keuntungan_sebulan*12;
      $hargajualan = $request->pinjaman + $keuntungan12bulan;


      $gadai = Http::put(config('api.url').'/api/gadai/norujukan/'. $auth_user['cawangan'] .'/' . $norujukanbaru,[
          'kodcaw' => $auth_user['cawangan'],
          'nilaimarhun' => round($total_nilai_marhun, 2),
          'berat' => round($request->total_berat, 2),
          'pinjaman' => round($request->pinjaman, 2),
          'kadarupah' => round($kadarupah, 2),
          'marhun' => $request->marhun,
          'percentpinj' => $margin_pinjaman,
          'teller'=> $auth_user['name'],
          'upah6' => $keuntungan6bulan,
          'upah12' => $keuntungan12bulan,
          'hargajualan' => $hargajualan,
          'kuantiti_marhun' => $request->kuantiti_marhun
      ])->json();


        ////////////////////////////
       ////Update History Gadaian///
       ////////////////////////////

       $updatehistorygadaian = Http::put(config('api.url').'/api/gadai/updatehistory/'. $auth_user['cawangan'] .'/' . $norujukanbaru,[
        'historygadaian' => $request->norujukan
        ])->json();


        ///////////////////////////
       /// MARHUN UPDATE TABLE////
       //////////////////////////


       $arraymarhunbaru = [];

       $marhunsediada = Http::get(config('api.url') . '/api/marhun/'. $request->norujukan)->json();

       $marhunyangdiambil = $marhunsediada[0];
       foreach($marhunyangdiambil as $index=>$marhunloop)
       {
            array_push($arraymarhunbaru, ["bil" => $marhunloop['bil'],"marhun" => $marhunloop['marhun'],"nota" => $marhunloop['nota'],"karat" => $marhunloop['karat'], "berat" => $marhunloop['berat'], "harga" => $marhunloop['harga'], "n_marhun" => $marhunloop['n_marhun']]);
       }

       foreach($arraymarhunbaru as $array1)
        {
            $updatemarhun = Http::post(config('api.url').'/api/marhun/'. $auth_user['cawangan'],[
            'norujukan' => $norujukanbaru,
            'marhun' => $array1['marhun'],
            'nota' => $array1['nota'],
            'karat' => $array1['karat'],
            'berat_emas' => $array1['berat'],
            'harga' => $array1['harga'],
            'nilai_marhun' => $array1['n_marhun']
        ])->json();
   
        }

    }

    public function tambahpinjaman(Request $request)
    {
        $api_url = config('api.url');
        $current = Carbon::now();
        $current = new Carbon();
        $curent_year = substr($current->year,2,2);
        $curent_month = sprintf("%02d", $current->month);
        $current_day = sprintf("%02d", $current->day);

        $auth_user = Auth::user();


        ////////////////////////
       /// UPDATE TEBUS API////
       ///////////////////////
        $updatetebus = Http::put(config('api.url').'/api/updatetebus/'. $auth_user['cawangan'] .'/' . $request->norujukan,[
            'nopelanggan' => $request->nopelanggan,
            'jenistebus' => $request->jenistebus,
            'jbg' => $request->jbg,
            'kadarupah' => $request->kadarupah,
            'nilai_marhun' => $request->nilai_marhun,
            'pinjaman' => $request->pinjaman_lama,
            'ansuran' => $request->ansuran,
            'nowakil' => $request->nowakil,
            'addpinjaman'=> $request->addpinjaman,
            'bakipinjaman' => $request->bakipinjaman,
            'bakiupah' => $request->bakiupah,
            'historygadaian' => $request->historygadaian,
            'marhun' => $request->marhun,
            'jenisbayarantebus' => $request->jenisbayarantebus,
            'teller' => $request->teller
        ])->json();

        //////////////////////
        /// MAKING NEW SAG////
        /////////////////////

        $gadai_transaction = Http::get(config('api.url') . '/api/gadai/'. $auth_user['cawangan'] . '/' . $current->toDateString())->json();
        if($gadai_transaction){
            $new_sirig = $gadai_transaction['sirig'] + 1;
            
        }else{
            $new_sirig = 1;
        }

        $new_transaction = sprintf("%04d",$new_sirig);
        $norujukanbaru = strtoupper($auth_user['cawangan']) . $curent_year . $curent_month . $current_day . '-' .$new_transaction;

        $gadai = Http::post(config('api.url').'/api/gadai/'.$auth_user['cawangan'],[
            'norujukan' => $norujukanbaru,
            'nopelanggan' => $request->nopelanggan,
            'nokp' => $request->noic,
            'sirig' => $new_sirig
        ]);

       
       //////////////////////
      ////Update SAG BARU///
      /////////////////////

        $keuntungan_sebln = $request->keuntungan_sebulan;
        $keuntungan_sebln = str_replace(',','',$keuntungan_sebln);

        $total_nilai_marhun = $request->nilai_marhun;
        $margin_pinjaman = $request->pinjaman / $total_nilai_marhun;
        $kadarupah = $request->kadarkeuntungan * 100;
        $keuntungan6bulan = $keuntungan_sebln * 6;
        $keuntungan12bulan = $keuntungan_sebln * 12;
        $hargajualan = $request->pinjaman + $keuntungan12bulan;


        $gadai = Http::put(config('api.url').'/api/gadai/norujukan/'. $auth_user['cawangan'] .'/' . $norujukanbaru,[
            'kodcaw' => $auth_user['cawangan'],
            'nilaimarhun' => round($total_nilai_marhun, 2),
            'berat' => round($request->total_berat, 2),
            'pinjaman' => round($request->pinjaman, 2),
            'kadarupah' => round($kadarupah, 2),
            'marhun' => $request->marhun,
            'percentpinj' => $margin_pinjaman,
            'teller'=> $auth_user['name'],
            'upah6' => $keuntungan6bulan,
            'upah12' => $keuntungan12bulan,
            'hargajualan' => $hargajualan,
            'kuantiti_marhun' => $request->kuantiti_marhun
        ])->json();

        $komuditi = Http::put(config('api.url').'/api/komuditi/transaksi',[
            'unit_komuditi' => round($request->pinjaman, 2),
            'norujukan' => $norujukanbaru,
            'kodcaw' => $auth_user['cawangan'],
            'teller' => $auth_user['name']
        ]);

        ////////////////////////////
        ////Update History Gadaian///
        ////////////////////////////

        $updatehistorygadaian = Http::put(config('api.url').'/api/gadai/updatehistory/'. $auth_user['cawangan'] .'/' . $norujukanbaru,[
            'historygadaian' => $request->norujukan
        ])->json();


        ///////////////////////////
        /// MARHUN UPDATE TABLE////
        //////////////////////////


        $arraymarhunbaru = [];

        $marhunsediada = Http::get(config('api.url') . '/api/marhun/'. $request->norujukan)->json();

        $marhunyangdiambil = $marhunsediada[0];
        foreach($marhunyangdiambil as $index=>$marhunloop)
        {
            $karatbaru = str_replace('.','-',$marhunloop['karat']);
            $hargabaru = Http::get(config('api.url') . '/api/harga_emas/'. $karatbaru)->json();
            
            $nilaimarhun = (floatval($hargabaru['harga']) * floatval($marhunloop['berat']));

            array_push($arraymarhunbaru, ["bil" => $marhunloop['bil'],"marhun" => $marhunloop['marhun'],"nota" => $marhunloop['nota'],"karat" => $marhunloop['karat'], "berat" => $marhunloop['berat'], "harga" => $hargabaru['harga'], "n_marhun" => $nilaimarhun]);
        }

        foreach($arraymarhunbaru as $array1){
            $updatemarhun = Http::post(config('api.url').'/api/marhun/'. $auth_user['cawangan'],[
                'norujukan' => $norujukanbaru,
                'marhun' => $array1['marhun'],
                'nota' => $array1['nota'],
                'karat' => $array1['karat'],
                'berat_emas' => $array1['berat'],
                'harga' => $array1['harga'],
                'nilai_marhun' => $array1['n_marhun']
            ])->json();
        }

        $this->AliranTunaiController->alirantunaiTebus($request);

    }


    public function tebusseparuh(Request $request)
    {
        $api_url = config('api.url');
        $current = Carbon::now();
        $current = new Carbon();
        $curent_year = substr($current->year,2,2);
        $curent_month = sprintf("%02d", $current->month);
        $current_day = sprintf("%02d", $current->day);

        $auth_user = Auth::user();

        ////////////////////////
       /// UPDATE TEBUS API////
       ///////////////////////
        $updatetebus = Http::put(config('api.url').'/api/updatetebus/'. $auth_user['cawangan'] .'/' . $request->norujukan,[
            'nopelanggan' => $request->nopelanggan,
            'jenistebus' => $request->jenistebus,
            'jbg' => $request->jbg,
            'kadarupah' => $request->kadarupah,
            'nilai_marhun' => $request->nilai_marhun,
            'pinjaman' => $request->pinjaman_lama,
            'ansuran' => $request->ansuran,
            'addpinjaman'=> $request->addpinjaman,
            'bakipinjaman' => $request->bakipinjaman,
            'bakiupah' => $request->bakiupah,
            'nowakil' => $request->nowakil,
            'historygadaian' => $request->historygadaian,
            'marhun' => $request->marhun,
            'jenisbayarantebus' => $request->jenisbayarantebus,
            'teller' => $request->teller
        ])->json();


        //////////////////////
       /// MAKING NEW SAG////
       /////////////////////
        $gadai_transaction = Http::get(config('api.url') . '/api/gadai/'. $auth_user['cawangan'] . '/' . $current->toDateString())->json();
        if($gadai_transaction){
            $new_sirig = $gadai_transaction['sirig'] + 1;
            
        }else{
            $new_sirig = 1;
        }

        $new_transaction = sprintf("%04d",$new_sirig);
        $norujukanbaru = strtoupper($auth_user['cawangan']) . $curent_year . $curent_month . $current_day . '-' .$new_transaction;

        $gadai = Http::post(config('api.url').'/api/gadai/'.$auth_user['cawangan'],[
            'norujukan' => $norujukanbaru,
            'nopelanggan' => $request->nopelanggan,
            'nokp' => $request->noic,
            'sirig' => $new_sirig
        ]);

            
        //////////////////////
       ////Update SAG BARU///
       /////////////////////
        $keuntungan_sebln = $request->keuntungan_sebulan;
        $keuntungan_sebln = str_replace(',','',$keuntungan_sebln);

        $total_nilai_marhun = $request->total_nilai_marhun;
        $margin_pinjaman = $request->pinjaman / $total_nilai_marhun;
        $kadarupah = $request->kadarkeuntungan * 100;
        $keuntungan6bulan = $keuntungan_sebln * 6;
        $keuntungan12bulan = $keuntungan_sebln * 12;
        $hargajualan = $request->pinjaman + $keuntungan12bulan;


        $gadai = Http::put(config('api.url').'/api/gadai/norujukan/'. $auth_user['cawangan'] .'/' . $norujukanbaru,[
            'kodcaw' => $auth_user['cawangan'],
            'nilaimarhun' => round($total_nilai_marhun, 2),
            'berat' => round($request->total_berat, 2),
            'pinjaman' => round($request->pinjaman, 2),
            'kadarupah' => round($kadarupah, 2),
            'marhun' => $request->marhun_detail,
            'percentpinj' => $margin_pinjaman,
            'teller'=> $auth_user['name'],
            'upah6' => $keuntungan6bulan,
            'upah12' => $keuntungan12bulan,
            'hargajualan' => $hargajualan,
            'kuantiti_marhun' => $request->kuantiti_marhun
        ])->json();

        $komuditi = Http::put(config('api.url').'/api/komuditi/transaksi',[
            'unit_komuditi' => round($request->pinjaman, 2),
            'norujukan' => $norujukanbaru,
            'kodcaw' => $auth_user['cawangan'],
            'teller' => $auth_user['name']
        ]);

        ////////////////////////////
       ////Update History Gadaian///
       ////////////////////////////

        $updatehistorygadaian = Http::put(config('api.url').'/api/gadai/updatehistory/'. $auth_user['cawangan'] .'/' . $norujukanbaru,[
            'historygadaian' => $request->norujukan
        ])->json();
    
        ///////////////////////////
       /// MARHUN UPDATE TABLE////
       //////////////////////////


       $arraymarhunbaru = [];

       $arraydiola = $request->arrayrecno;

       $marhunsediada = Http::get(config('api.url') . '/api/marhun/'. $request->norujukan)->json();

       $marhunyangdiambil = $marhunsediada[0];
       foreach($marhunyangdiambil as $index=>$marhunloop)
       {
            if(!in_array($marhunloop['recno'], $arraydiola))
            {
                $karatbaru = str_replace('.','-',$marhunloop['karat']);
                $hargabaru = Http::get(config('api.url') . '/api/harga_emas/'. $karatbaru)->json();

                $nilaimarhun = (floatval($hargabaru['harga']) * floatval($marhunloop['berat']));

                array_push($arraymarhunbaru, ["bil" => $marhunloop['bil'],"marhun" => $marhunloop['marhun'],"nota" => $marhunloop['nota'],"karat" => $marhunloop['karat'], "berat" => $marhunloop['berat'], "harga" => $hargabaru['harga'], "n_marhun" => $nilaimarhun]);
            }
       }

       foreach($arraymarhunbaru as $array1)
        {
            $updatemarhun = Http::post(config('api.url').'/api/marhun/'. $auth_user['cawangan'],[
                'norujukan' => $norujukanbaru,
                'marhun' => $array1['marhun'],
                'nota' => $array1['nota'],
                'karat' => $array1['karat'],
                'berat_emas' => $array1['berat'],
                'harga' => $array1['harga'],
                'nilai_marhun' => $array1['n_marhun']
            ])->json();
   
        }

        $this->AliranTunaiController->alirantunaiTebus($request);


        return response()->json($updatemarhun,200);

    }


    //Wakil Function - Jangan kacau saya nak makan kacang ni!!

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

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

        if(sizeof($getfirstwakil) != 0)
            return $getfirstwakil[0]['nowakil'];
        else
            return $getfirstwakil;
    }


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

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

        if(sizeof($getwakil) > 0)
            return $getwakil[0];
        else
            return 'no-data';
    }

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

        $createwakil = Http::post(config('api.url') . '/api/createwakil/'. $auth_user['cawangan'] . '/' . $request->icno,[ 
    
            'nopelanggan' => $request->nopelanggan,
            'nowakil' => $request->nowakil,
            'nama' => $request->nama,
            'hubungan' => $request->hubungan,
            'alamat' => $request->alamat,
            'telefon' => $request->telefon,
            'nota' => $request->nota

        ])->json();

        if($createwakil)
            return 1;
        else
            return 0;
    }

    public function tukaranhargaemas($norujukan)
    {

       $marhunsaglama = Http::get(config('api.url') . '/api/marhun/'. $norujukan)->json();

       $marhunsaglama = $marhunsaglama[0];
       $totalnilaimarhun = 0;

       foreach($marhunsaglama as $index=>$marhunloop)
       {
            $karatbaru = str_replace('.','-',$marhunloop['karat']);
            $hargabaru = Http::get(config('api.url') . '/api/harga_emas/'. $karatbaru)->json();

            $nilaimarhun = (floatval($hargabaru['harga']) * floatval($marhunloop['berat']));

            $totalnilaimarhun += $nilaimarhun;
       }

       return $totalnilaimarhun;
    }

}
