<?php

namespace App\Services;

use App\Advansur;
use App\Cawangan;
use App\Customer;
use App\Gadai;
use App\GlDetail;
use App\Lelong;
use App\Panjar;
use App\RingkasanAnsuran;
use App\RingkasanGadai;
use App\RingkasanLelong;
use App\RingkasanMain;
use App\RingkasanTebus;
use App\Tebus;
use App\Transaction;
use App\TransaksiKeuntungan;
use App\TuntutBaki;
use Illuminate\Support\Facades\Http;

use Carbon\Carbon;
use Carbon\CarbonPeriod;
use Illuminate\Support\Facades\DB;

class RingkasanHarianServices
{

    // protected float $tunaiditangan = 0;
   

    private function getTransaksiHarian($date,$kodcaw,$connection) : Array {
        $total_gadai = Gadai::on($connection)->where('t_gadai','=',$date)->sum('pinjaman');
        $total_tebus = Tebus::on($connection)->where('t_tebus','=',$date)->sum('pinjaman');
        $bayaranonlinequery = Transaction::on('mysql7')->whereDate('created_at',$date)->where('teller','=','Online')->where('kodcaw',$kodcaw);

        $bayaranonline = $bayaranonlinequery->sum('duit_masuk');
        $bilbayaranonline = $bayaranonlinequery->count();
        
        $ansurankaunter = TransaksiKeuntungan::on('mysql7') 
                        ->leftJoin('transaction as trans', function($join)
                        {
                            $join->on('transaksi_keuntungan.norujukan', '=', 'trans.norujukan');
                            $join->on('transaksi_keuntungan.tarikh_bayaran', '=', 'trans.created_at');
                        })
                        ->whereRaw('DATE(transaksi_keuntungan.tarikh_bayaran) = ?',[$date])
                        ->where([['trans.trans_type','=','A'],['teller','<>','Online'],['trans.kodcaw','=',$kodcaw]]);
           
        $ansuranonline = TransaksiKeuntungan::on('mysql7') 
                        ->leftJoin('transaction as trans', function($join)
                        {
                            $join->on('transaksi_keuntungan.norujukan', '=', 'trans.norujukan');
                            $join->on('transaksi_keuntungan.tarikh_bayaran', '=', 'trans.created_at');
                        })
                        ->whereRaw('DATE(transaksi_keuntungan.tarikh_bayaran) = ?',[$date])
                        ->where([['trans.trans_type','=','A'],['teller','=','Online'],['trans.kodcaw','=',$kodcaw]]);

        $total_ansuran = $ansurankaunter->sum('transaksi_keuntungan.bayaran_pembiayaan');
        $total_ansuranonline = $ansuranonline->sum('transaksi_keuntungan.bayaran_pembiayaan');

        $total_upah = Advansur::on($connection)->where('tarikh','=',$date)->sum('upahdibayar');
        $total_keuntungantebus = Tebus::on($connection)->where('t_tebus','=',$date)->sum('bakiupah');

        $total_keuntunganansuran_data_kaunter = TransaksiKeuntungan::on('mysql7')
                    ->leftJoin('transaction as trans', function($join)
                         {
                             $join->on('transaksi_keuntungan.norujukan', '=', 'trans.norujukan');
                             $join->on('transaksi_keuntungan.tarikh_bayaran', '=', 'trans.created_at');
                         })
                    ->whereDate('trans.created_at','=',$date)
                    ->where('trans.teller','<>','Online')
                    ->where('trans.trans_type','=','A')
                    ->where('trans.kodcaw',$kodcaw)
                    ->pluck('transaksi_keuntungan.norujukan');
    
        $gadaiDataKaunter = Gadai::on($connection)->whereIn('norujukan',$total_keuntunganansuran_data_kaunter)->whereNotNull('hargajualan')->pluck('norujukan');

        $keuntunganansuran_kaunter_query = TransaksiKeuntungan::on('mysql7')
                                            ->leftJoin('transaction as trans', function($join)
                                            {
                                                $join->on('transaksi_keuntungan.norujukan', '=', 'trans.norujukan');
                                                $join->on('transaksi_keuntungan.tarikh_bayaran', '=', 'trans.created_at');
                                            })
                                            ->where('trans.teller','<>','Online')
                                            ->where('trans.trans_type','=','A')
                                            ->whereIn('transaksi_keuntungan.norujukan',$gadaiDataKaunter)
                                            ->whereDate('transaksi_keuntungan.tarikh_bayaran','=',$date);
                                            
                                            
        $total_keuntunganansuran_kaunter = $keuntunganansuran_kaunter_query->sum('bayaran_keuntungan');
        $bil_keuntunganansuran_kaunter = $keuntunganansuran_kaunter_query->count();

        $total_keuntunganansuran_data_online = TransaksiKeuntungan::on('mysql7')
        ->leftJoin('transaction as trans', function($join)
             {
                 $join->on('transaksi_keuntungan.norujukan', '=', 'trans.norujukan');
                 $join->on('transaksi_keuntungan.tarikh_bayaran', '=', 'trans.created_at');
             })
        ->whereDate('trans.created_at','=',$date)
        ->where('trans.teller','=','Online')
        ->where('trans.trans_type','=','A')
        ->where('trans.kodcaw',$kodcaw)
        ->pluck('transaksi_keuntungan.norujukan');

        $gadaiDataOnline = Gadai::on($connection)->whereIn('norujukan',$total_keuntunganansuran_data_online)->whereNotNull('hargajualan')->pluck('norujukan');

        $keuntunganansuran_online_query = TransaksiKeuntungan::on('mysql7')
                                        ->leftJoin('transaction as trans', function($join)
                                        {
                                            $join->on('transaksi_keuntungan.norujukan', '=', 'trans.norujukan');
                                            $join->on('transaksi_keuntungan.tarikh_bayaran', '=', 'trans.created_at');
                                        })
                                        ->whereDate('trans.created_at','=',$date)
                                        ->where('trans.teller','=','Online')
                                        ->where('trans.trans_type','=','A')
                                        ->where('trans.kodcaw',$kodcaw)
                                        ->whereIn('trans.norujukan',$gadaiDataOnline)
                                        ->whereDate('transaksi_keuntungan.tarikh_bayaran','=',$date);
                                        
        $total_keuntunganansuran_online = $keuntunganansuran_online_query->sum('bayaran_keuntungan');
        $bil_keuntunganansuran_online = $keuntunganansuran_online_query->count();

        $total_keuntunganbukanansuran_data_kaunter = TransaksiKeuntungan::on('mysql7')
                    ->leftJoin('transaction as trans', function($join)
                    {
                        $join->on('transaksi_keuntungan.norujukan', '=', 'trans.norujukan');
                        $join->on('transaksi_keuntungan.tarikh_bayaran', '=', 'trans.created_at');
                    })
                    ->where('trans.trans_type','!=','A')
                    ->where('trans.teller','<>','Online')
                    ->whereDate('trans.created_at','=',$date)
                    ->where('trans.kodcaw','=',$kodcaw);
        
        $total_keuntunganbukanansuran_kaunter = $total_keuntunganbukanansuran_data_kaunter->sum('keuntungan_rebet');
        $bil_keuntunganbukanansuran_kaunter = $total_keuntunganbukanansuran_data_kaunter->count();

        $total_keuntunganbukanansuran_data_online = TransaksiKeuntungan::on('mysql7')
            ->leftJoin('transaction as trans', function($join)
            {
                $join->on('transaksi_keuntungan.norujukan', '=', 'trans.norujukan');
                $join->on('transaksi_keuntungan.tarikh_bayaran', '=', 'trans.created_at');
            })
            ->where('trans.trans_type','!=','A')
            ->where('trans.teller','=','Online')
            ->whereDate('trans.created_at','=',$date)
            ->where('trans.kodcaw','=',$kodcaw)
            ->pluck('transaksi_keuntungan.norujukan');

        $gadaiDataOnline = Gadai::on($connection)->whereIn('norujukan',$total_keuntunganbukanansuran_data_online)->whereNotNull('hargajualan')->pluck('norujukan');

        $keuntunganbukanansuran_online_query = TransaksiKeuntungan::on('mysql7')
                                ->whereIn('norujukan',$gadaiDataOnline)
                                ->whereDate('tarikh_bayaran','=',$date);
                                

        $total_keuntunganbukanansuran_online = $keuntunganbukanansuran_online_query->sum('keuntungan_rebet');
        $bil_keuntunganbukanansuran_online = $keuntunganbukanansuran_online_query->count();

        $keuntungan_data_kaunter = TransaksiKeuntungan::on('mysql7')
                    ->leftJoin('transaction as trans', function($join)
                    {
                        $join->on('transaksi_keuntungan.norujukan', '=', 'trans.norujukan');
                        $join->on('transaksi_keuntungan.tarikh_bayaran', '=', 'trans.created_at');
                    })
                    ->whereDate('transaksi_keuntungan.tarikh_bayaran' ,'=', $date)
                    ->where('trans.teller','<>','Online')
                    ->pluck('transaksi_keuntungan.norujukan');

        $gadaiDataKaunter = Gadai::on($connection)->whereIn('norujukan',$keuntungan_data_kaunter)->whereNull('hargajualan')->pluck('norujukan');

        $keuntungan_kaunter_query = TransaksiKeuntungan::on('mysql7')
                            ->whereIn('norujukan',$gadaiDataKaunter);
                               
        
        $keuntungan_kaunter = $keuntungan_kaunter_query->sum('bayaran_keuntungan'); 
        $bil_keuntungan_kaunter = $keuntungan_kaunter_query->count(); 


        $upahtebus = Tebus::on($connection)
                    ->where('t_tebus','=',$date)
                    ->sum('upahdibayar');

        $upahlelong = Lelong::on($connection)
                    ->where('t_jual','=',$date)
                    ->sum('bakiupah');

        $upahansurKaunter = Advansur::on($connection)
                    ->where('teller','<>','Online')
                    ->where('tarikh','=',$date)
                    ->sum('upahdibayar');

        $upahansurOnlineQuery = Advansur::on($connection)
                    ->where('teller','=','Online')
                    ->where('tarikh','=',$date);

        $upahansurOnline = $upahansurOnlineQuery->sum('upahdibayar');
        $bilupahansurOnline = $upahansurOnlineQuery->count();

        
        $upahsimpankaunter = $keuntungan_kaunter + $upahtebus + $upahlelong + $upahansurKaunter + $total_keuntunganbukanansuran_kaunter;

        $keuntungansebenarkaunter = $total_keuntunganansuran_kaunter;

        $upahsimpanonline = $upahansurOnline;
        $keuntungansebenaronline = $total_keuntunganansuran_online + $total_keuntunganbukanansuran_online;

        $total_keuntungan_kaunter = floatval($upahsimpankaunter) + floatval($keuntungansebenarkaunter);
        $total_keuntungan_online = floatval($upahsimpanonline) + floatval($keuntungansebenaronline);

        $totalbayaranonline = floatval($bayaranonline) + floatval($upahansurOnline);
        $biltotalbayaranonline =  $bilbayaranonline + $bilupahansurOnline;

        $tebus = Tebus::on($connection)
                ->select('tebus.t_tebus', 'gad.t_gadai','gad.hargajualan','gad.bakihargajualan','gad.bakipjm as gadaibakipjm','tebus.nilaimarhun','tebus.tagterima', 'tebus.teller','tebus.jenistebus','tebus.pinjaman','tebus.bakiupah','tebus.bakipjm','gad.norujukan','gad.nokp','tebus.jbg')
                ->join('gadai as gad','gad.norujukan','=','tebus.norujukan')
                ->where('tebus.t_tebus','=',$date)
                ->get();

        $pembiayaanlelong = Lelong::on($connection)
                ->where('t_jual','=',$date)
                ->sum('pinjaman');

        $totalpenebusan = 0;

        foreach($tebus as $index1=>$teb)
        {
            if($teb['gad.hargajualan'] != $teb['bakihargajualan'])
            {
                $totalpenebusan += $teb['gadaibakipjm'];
            }else{
                $totalpenebusan += $teb['pinjaman'];
            }
        }

        //Lelongan Pembiayaan
        $totalpenebusan += $pembiayaanlelong;


        //b
        $bilangan_gadai = Gadai::on($connection)->where('t_gadai','=',$date)->where('pinjaman','>',0)->count();
        $bilangan_tebus = Tebus::on($connection)->where('t_tebus','=',$date)->count();
        $bilangan_ansuran = $ansurankaunter->count();
        $bilangan_ansuranonline = $ansuranonline->count();
        $bilangan_customer_baru = Customer::on('mysql7')->where([['tarikhdaftar','=',$date],['kodcaw',$kodcaw]])->count();

        return [
            'total_gadai'=>$total_gadai,
            'total_tebus'=>$totalpenebusan,
            'total_ansuran'=>$total_ansuran,
            'total_ansuranonline'=>$total_ansuranonline,
            'bilangan_gadai'=>$bilangan_gadai,
            'bilangan_tebus'=>$bilangan_tebus,
            'bilangan_ansuran'=>$bilangan_ansuran,
            'bilangan_ansuranonline'=>$bilangan_ansuranonline,
            'bilangan_customer_baru'=>$bilangan_customer_baru,
            'total_keuntungan_kaunter'=>floatval($total_keuntungan_kaunter),
            'total_keuntungan_online'=>floatval($total_keuntungan_online),
            'totalbayaranonline' => floatval($totalbayaranonline),
            'biltotalbayaranonline' => $biltotalbayaranonline,
            'totalbayaranonline' => floatval($totalbayaranonline),
            'upahsimpankaunter' => floatval($upahsimpankaunter),
            'keuntungansebenarkaunter' => floatval($keuntungansebenarkaunter),
        ];
    }

    private function getPendahuluanSerahanHQ($date,$kodcaw,$connection) : Array{
        $serahanpkb = '1000/020';
        $pendahuluanpkb = '7000/102';

        $totalserahan = GlDetail::on($connection)
            ->where('ktacct','=',$serahanpkb)
            ->where('descpt','not like', 'SERAHAN BAYAR ONLINE%')
            ->where('tarikh','=', $date)
            ->where('tarikh','>','2021-03-24')
            ->sum('serahan');

        $serahanbayaranOnline = GlDetail::on($connection)
            ->where('ktacct','=',$serahanpkb)
            ->where('descpt','like', 'SERAHAN BAYAR ONLINE%')
            ->where('tarikh','=', $date)
            ->where('tarikh','>','2021-03-24')
            ->sum('serahan');

        $totalpendahuluan = GlDetail::on($connection)
            ->where('ktacct','=',$pendahuluanpkb)
            ->where([['tarikh','=', $date],['tarikh','>','2021-03-24']])
            ->sum('pendahuluan');
        
        return [
            'totalserahan' => $totalserahan,
            'serahanonline' => $serahanbayaranOnline,
            'totalpendahuluan' => $totalpendahuluan
        ];
    }

    private function getPerbelanjaan($date,$kodcaw,$connection) : Array{
        $belanjaam = Panjar::on($connection)->where('tag','=', 1)->where('tarikh','=', $date)->sum('bayaran');
        $baikidanselenggara = Panjar::on($connection)->where('tag','=', 2)->where('tarikh','=', $date)->sum('bayaran');
        $keraian = Panjar::on($connection)->where('tag','=', 3)->where('tarikh','=', $date)->sum('bayaran');

        return [
            'belanja' => $belanjaam,
            'baikidanselenggara' => $baikidanselenggara,
            'keraian' => $keraian
        ];
    }

    private function getLelong($date,$kodcaw,$connection) : Array{


        $data_lelong = Lelong::on($connection)->where('t_jual','=',$date);
        $data_lelong_baki = clone $data_lelong;
        $data_lelong_baki->where('baki','>=',0);
        $data_lelong_rugi = clone $data_lelong;
        $data_lelong_rugi->where('baki','<',0);

        $bilangan_lelong = $data_lelong->count();
        $cas_lelong = $data_lelong->sum('cajlelong');
        $jumlah_lelong = $data_lelong->sum('hargajual');
        
        $baki_lelong = $data_lelong_baki->sum('baki');
        $rugi_lelong = $data_lelong_rugi->sum('baki');

        $tuntut_baki_rugi = TuntutBaki::on($connection)->where('tarikh','=',$date)->where('jumlah','<',0);
        $tuntut_baki_bayar = TuntutBaki::on($connection)->where('tarikh','=',$date)->where('jumlah','>',0);

        $bayar_rugi_lelong = $tuntut_baki_rugi->sum('jumlah');
        $bayar_baki_lelong = $tuntut_baki_bayar->sum('jumlah');

        return [
            'cas_lelong' => $cas_lelong,
            'jumlah_lelong' => $jumlah_lelong,
            'baki_lelong' => $baki_lelong,
            'rugi_lelong' => $rugi_lelong,
            'bayar_rugi_lelong' => $bayar_rugi_lelong,
            'bayar_baki_lelong' => $bayar_baki_lelong,
            'bilangan' => $bilangan_lelong
        ];
    }

    private function getPeratusan($date,$kodcaw,$connection){
        $gadai_70 = Gadai::on($connection)->where([['t_gadai','=',$date],['percentpinj','<=',70]])->sum('pinjaman');
        $gadai_75 = Gadai::on($connection)->where([['t_gadai','=',$date],['percentpinj','>',70],['percentpinj','<=',75]])->sum('pinjaman');
        $gadai_80 = Gadai::on($connection)->where([['t_gadai','=',$date],['percentpinj','>',75],['percentpinj','<=',80]])->sum('pinjaman');

        $total_gadaian = floatval($gadai_70) + floatval($gadai_75) + floatval($gadai_80);

        $nilaimarhun_70 = Gadai::on($connection)->where([['t_gadai','=',$date],['percentpinj','<=',70]])->sum('nilaimarhun');
        $nilaimarhun_75 = Gadai::on($connection)->where([['t_gadai','=',$date],['percentpinj','>',70],['percentpinj','<=',75]])->sum('nilaimarhun');
        $nilaimarhun_80 = Gadai::on($connection)->where([['t_gadai','=',$date],['percentpinj','>',75],['percentpinj','<=',80]])->sum('nilaimarhun');

        $total_nm = floatval($nilaimarhun_70) + floatval($nilaimarhun_75) + floatval($nilaimarhun_80);

        $bilangan_70 = Gadai::on($connection)->where([['t_gadai','=',$date],['percentpinj','<=',70]])->count();
        $bilangan_75 = Gadai::on($connection)->where([['t_gadai','=',$date],['percentpinj','>',70],['percentpinj','<=',75]])->count();
        $bilangan_80 = Gadai::on($connection)->where([['t_gadai','=',$date],['percentpinj','>',75],['percentpinj','<=',80]])->count();

        $total_bilangan = intval($bilangan_70) + intval($bilangan_75) + intval($bilangan_80);

        return [
            'gadai_70'=>$gadai_70,
            'gadai_75'=>$gadai_75,
            'gadai_80'=>$gadai_80,
            'total_gadaian'=>$total_gadaian,
            'nilaimarhun_70'=>$nilaimarhun_70,
            'nilaimarhun_75'=>$nilaimarhun_75,
            'nilaimarhun_80'=>$nilaimarhun_80,
            'total_nm'=>$total_nm,
            'bilangan_70'=>$bilangan_70,
            'bilangan_75'=>$bilangan_75,
            'bilangan_80'=>$bilangan_80,
            'total_bilangan'=>$total_bilangan,
        ];

    }

    public function StoreRingkasanHarian($mula,$hingga,$zone){
        $period = CarbonPeriod::create($mula, $hingga);
        $cawangan = Cawangan::on('mysql7')->where('zone',$zone)->get()->toArray();
        foreach($cawangan as $index=>$caw){
            foreach($period as $current){
                DB::connection($caw['mysql'])->beginTransaction();
                try{
                    $getTransaksi = $this->getTransaksiHarian($current->toDateString(),$caw['kodcaw'],$caw['mysql']);                
                    $getPendser = $this->getPendahuluanSerahanHQ($current->toDateString(),$caw['kodcaw'],$caw['mysql']);
                    $getPerbelanjaan = $this->getPerbelanjaan($current->toDateString(),$caw['kodcaw'],$caw['mysql']);
                    $getLelong = $this->getLelong($current->toDateString(),$caw['kodcaw'],$caw['mysql']);
                    $getPeratusan = $this->getPeratusan($current->toDateString(),$caw['kodcaw'],$caw['mysql']);
                    $bilangan_tebus = $getLelong['bilangan'] + $getTransaksi['bilangan_tebus'];
                    $condition = RingkasanMain::on($caw['mysql'])->where('date',$current->toDateString())->first();
                    if($condition){
                        $gadai = RingkasanGadai::on($caw['mysql'])->find($condition['gad_id']);
                        $tebus = RingkasanTebus::on($caw['mysql'])->find($condition['teb_id']);
                        $ansuran = RingkasanAnsuran::on($caw['mysql'])->find($condition['ans_id']);
                        $lelong = RingkasanLelong::on($caw['mysql'])->find($condition['lel_id']);
                        $main = RingkasanMain::on($caw['mysql'])->where('date',$current->toDateString())->first();
                    }else{
                        $main = new RingkasanMain();
                        $gadai = new RingkasanGadai();
                        $tebus = new RingkasanTebus();
                        $ansuran = new RingkasanAnsuran();
                        $lelong = new RingkasanLelong();
                    }

                        $gadai->setConnection($caw['mysql']);
                        $ansuran->setConnection($caw['mysql']);
                        $tebus->setConnection($caw['mysql']);
                        $lelong->setConnection($caw['mysql']);
                        $main->setConnection($caw['mysql']);

                    /* main */
                    $main->pelangganbaru = ($getTransaksi['bilangan_customer_baru']) ? $getTransaksi['bilangan_customer_baru'] : 0;
                    $main->pendahuluan = ($getPendser['totalpendahuluan']) ? $getPendser['totalpendahuluan'] : 0;
                    $main->serahan = ($getPendser['totalserahan']) ? $getPendser['totalserahan'] : 0;
                    $main->serahan_online = ($getPendser['serahanonline']) ? $getPendser['serahanonline'] : 0;
                    $main->belanjaam = ($getPerbelanjaan['belanja']) ? $getPerbelanjaan['belanja'] : 0;
                    $main->baikidanselenggara = ($getPerbelanjaan['baikidanselenggara']) ? $getPerbelanjaan['baikidanselenggara'] : 0;
                    $main->keraian = ($getPerbelanjaan['keraian']) ? $getPerbelanjaan['keraian'] : 0;
                    $main->date = $current->toDateTimeString();

                    /* gadai */
                    $gadai->jumlah_pembiayaan = ($getTransaksi['total_gadai']) ? $getTransaksi['total_gadai'] : 0;
                    $gadai->bilangan_pembiayaan = ($getTransaksi['bilangan_gadai']) ? $getTransaksi['bilangan_gadai'] : 0;
                    $gadai->gadai_70 = ($getPeratusan['gadai_70']) ? $getPeratusan['gadai_70'] : 0;
                    $gadai->gadai_75 = ($getPeratusan['gadai_75']) ? $getPeratusan['gadai_75'] : 0;
                    $gadai->gadai_80 = ($getPeratusan['gadai_80']) ? $getPeratusan['gadai_80'] : 0;
                    $gadai->nm_70 = ($getPeratusan['nilaimarhun_70']) ? $getPeratusan['nilaimarhun_70'] : 0;
                    $gadai->nm_75 = ($getPeratusan['nilaimarhun_75']) ? $getPeratusan['nilaimarhun_75'] : 0;
                    $gadai->nm_80 = ($getPeratusan['nilaimarhun_80']) ? $getPeratusan['nilaimarhun_80'] : 0;
                    $gadai->bil_70 = ($getPeratusan['bilangan_70']) ? $getPeratusan['bilangan_70'] : 0;
                    $gadai->bil_75 = ($getPeratusan['bilangan_75']) ? $getPeratusan['bilangan_75'] : 0;
                    $gadai->bil_80 = ($getPeratusan['bilangan_80']) ? $getPeratusan['bilangan_80'] : 0;

                    /* tebus */
                    $tebus->pembiayaan_dibayar = ($getTransaksi['total_tebus']) ? $getTransaksi['total_tebus'] : 0;
                    $tebus->keuntungan_dibayar = ($getTransaksi['upahsimpankaunter']) ? $getTransaksi['upahsimpankaunter'] : 0;
                    $tebus->bilangan_tebus = ($bilangan_tebus) ? $bilangan_tebus : 0;

                    /* ansuran */
                    $ansuran->pembiayaan_dibayar = ($getTransaksi['total_ansuran']) ? $getTransaksi['total_ansuran'] : 0;
                    $ansuran->keuntungan_dibayar = ($getTransaksi['keuntungansebenarkaunter']) ? $getTransaksi['keuntungansebenarkaunter'] : 0;
                    $ansuran->bilangan_ansuran = ($getTransaksi['bilangan_ansuran']) ? $getTransaksi['bilangan_ansuran'] : 0;
                    $ansuran->pembiayaan_dibayar_online = ($getTransaksi['total_ansuranonline']) ? $getTransaksi['total_ansuranonline'] : 0;
                    $ansuran->keuntungan_dibayar_online = ($getTransaksi['total_keuntungan_online']) ? $getTransaksi['total_keuntungan_online'] : 0;
                    $ansuran->bilangan_bayaran_online = ($getTransaksi['bilangan_ansuranonline']) ? $getTransaksi['bilangan_ansuranonline'] : 0;

                    /* lelong */
                    $lelong->lelong_current = ($getLelong['jumlah_lelong']) ? $getLelong['jumlah_lelong'] : 0;
                    $lelong->baki_lelong = ($getLelong['baki_lelong']) ? $getLelong['baki_lelong'] : 0;
                    $lelong->rugi_lelong = ($getLelong['rugi_lelong']) ? $getLelong['rugi_lelong'] : 0;
                    $lelong->bayaran_rugi = ($getLelong['bayar_rugi_lelong']) ? $getLelong['bayar_rugi_lelong'] : 0;
                    $lelong->bayaran_baki = ($getLelong['bayar_baki_lelong']) ? $getLelong['bayar_baki_lelong'] : 0;
                    $lelong->caj_lelong = ($getLelong['cas_lelong']) ? $getLelong['cas_lelong'] : 0;
                    $lelong->bil_lelong = ($getLelong['bilangan']) ? $getLelong['bilangan'] : 0;

                    if(!$condition){
                        $main->gadai()->save($gadai);
                        $main->ansuran()->save($ansuran);
                        $main->tebus()->save($tebus);
                        $main->lelong()->save($lelong);
                        
                        $main->gad_id = $gadai->gad_id;
                        $main->teb_id = $tebus->teb_id;
                        $main->ans_id = $ansuran->ans_id;
                        $main->lel_id = $lelong->lel_id;
                    }else{
                        $ansuran->save();
                        $tebus->save();
                        $lelong->save();
                        $gadai->save();
                    }
                    $main->save();
                    

                    
                    DB::connection($caw['mysql'])->commit();
            }catch (\Exception $e) {
                    dd($e);
                    DB::connection($caw['mysql'])->rollback();
                    return response()->json($e->getMessage(),400);
                }
            }
        }

        return response('success');
    }

    public function getRingkasanHarian($date1,$date2,$kodcaw) : Array{

        $cawangan = Cawangan::on('mysql7')->where('kodcaw',$kodcaw)->get()->toArray();
        $cawangan = (sizeof($cawangan) > 0) ? $cawangan[0] : null;
        
        if($cawangan != null){
            //gadai
            $totalgadai = RingkasanMain::on($cawangan['mysql'])->JumlahGadai($date1,$date2);
            $bilangangadai = RingkasanMain::on($cawangan['mysql'])->BilanganGadai($date1,$date2);
            $gadai70 = RingkasanMain::on($cawangan['mysql'])->Gadai70($date1,$date2);
            $gadai75 = RingkasanMain::on($cawangan['mysql'])->Gadai75($date1,$date2);
            $gadai80 = RingkasanMain::on($cawangan['mysql'])->Gadai80($date1,$date2);
            $nilaimarhun70 = RingkasanMain::on($cawangan['mysql'])->Nm70($date1,$date2);
            $nilaimarhun75 = RingkasanMain::on($cawangan['mysql'])->Nm75($date1,$date2);
            $nilaimarhun80 = RingkasanMain::on($cawangan['mysql'])->Nm80($date1,$date2);
            $bilangan70 = RingkasanMain::on($cawangan['mysql'])->Bil70($date1,$date2);
            $bilangan75 = RingkasanMain::on($cawangan['mysql'])->Bil75($date1,$date2);
            $bilangan80 = RingkasanMain::on($cawangan['mysql'])->Bil80($date1,$date2);

            $totalbilperatusan = intval($bilangan70) + intval($bilangan75) + intval($bilangan80);
            $totalnmperatusan = floatval($nilaimarhun70) + floatval($nilaimarhun75) + floatval($nilaimarhun80);
            $totalgadperatusan = floatval($gadai70) + floatval($gadai75) + floatval($gadai80);

            //main        
            $pendahuluan = RingkasanMain::on($cawangan['mysql'])->pendahuluan($date1,$date2);
            $pelanggan = RingkasanMain::on($cawangan['mysql'])->pelanggan($date1,$date2);
            $serahan = RingkasanMain::on($cawangan['mysql'])->Serahan($date1,$date2);
            $serahanonline = RingkasanMain::on($cawangan['mysql'])->SerahanOnline($date1,$date2);
            $belanja = RingkasanMain::on($cawangan['mysql'])->BelanjaAm($date1,$date2);
            $baiki = RingkasanMain::on($cawangan['mysql'])->BaikiDanSelenggara($date1,$date2);
            $keraian = RingkasanMain::on($cawangan['mysql'])->Keraian($date1,$date2);

            //tebus
            $tebus = RingkasanMain::on($cawangan['mysql'])->penebusan($date1,$date2);
            $tebus_keuntungan = RingkasanMain::on($cawangan['mysql'])->KeuntunganPenebusan($date1,$date2);
            $bilangan_tebus = RingkasanMain::on($cawangan['mysql'])->BilanganPenebusan($date1,$date2);

            //lelong
            $jumlahlelong = RingkasanMain::on($cawangan['mysql'])->JumlahLelong($date1,$date2);
            $bakilelong = RingkasanMain::on($cawangan['mysql'])->BakiLelong($date1,$date2);
            $rugilelong = RingkasanMain::on($cawangan['mysql'])->RugiLelong($date1,$date2);
            $bayaranrugi = RingkasanMain::on($cawangan['mysql'])->BayaranRugi($date1,$date2);
            $bayaranbaki = RingkasanMain::on($cawangan['mysql'])->BayaranBaki($date1,$date2);
            $cajlelong = RingkasanMain::on($cawangan['mysql'])->CajLelong($date1,$date2);
            $billelong = RingkasanMain::on($cawangan['mysql'])->BilLelong($date1,$date2);


            //ansuran
            $pembiayaanansuran = RingkasanMain::on($cawangan['mysql'])->PembiayaanAnsuran($date1,$date2);
            $keuntunganansuran = RingkasanMain::on($cawangan['mysql'])->KeuntunganAnsuran($date1,$date2);
            $bilansuran = RingkasanMain::on($cawangan['mysql'])->BilAnsuran($date1,$date2);
            $pembiayaanonline = RingkasanMain::on($cawangan['mysql'])->PembiayaanOnline($date1,$date2);
            $keuntunganonline = RingkasanMain::on($cawangan['mysql'])->KeuntunganOnline($date1,$date2);
            $bilonline = RingkasanMain::on($cawangan['mysql'])->BilOnline($date1,$date2);


            return [
                'total_gadai' => $totalgadai,
                'bilangan_gadai' => $bilangangadai,
                'totalpendahuluan' => $pendahuluan,
                'bilangan_customer_baru' => $pelanggan,
                'totalserahan' => $serahan,
                'serahanbayaranOnline' => $serahanonline,
                'belanjaam' => $belanja,
                'baikidanselenggara' => $baiki,
                'keraian' => $keraian,
                'total_tebus' => $tebus,
                'upahsimpankaunter' => $tebus_keuntungan,
                'bilangan_tebus' => $bilangan_tebus,
                'jumlah_lelong' => $jumlahlelong,
                'baki_lelong' => $bakilelong,
                'rugi_lelong' => $rugilelong,
                'bayar_rugi_lelong' => $bayaranrugi,
                'bayar_baki_lelong' => $bayaranbaki,
                'cas_lelong' => $cajlelong,
                'bilangan_lelong' => $billelong,
                'total_ansuran' => $pembiayaanansuran,
                'keuntungansebenarkaunter' => $keuntunganansuran,
                'bilangan_ansuran' => $bilansuran,
                'total_ansuranonline' => $pembiayaanonline,
                'total_keuntungan_online' => $keuntunganonline,
                'bilangan_ansuranonline' => $bilonline,
                'gadai_70' => $gadai70,
                'gadai_75' => $gadai75,
                'gadai_80' => $gadai80,
                'nilaimarhun_70' => $nilaimarhun70,
                'nilaimarhun_75' => $nilaimarhun75,
                'nilaimarhun_80' => $nilaimarhun80,
                'bilangan_70' => $bilangan70,
                'bilangan_75' => $bilangan75,
                'bilangan_80' => $bilangan80,
                'total_bilangan' => $totalbilperatusan,
                'total_nm' => $totalnmperatusan,
                'total_gadaian' => $totalgadperatusan,
            ];
        }else{
            return 'error';
        }

    }

}
