<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\HasOne;

class RingkasanMain extends Model
{
    protected $table = 'ringkasan_main';

    public function gadai(): HasOne
    {
        return $this->hasOne(RingkasanGadai::class, 'gad_id', 'gad_id');
    }

    public function ansuran(): HasOne
    {
        return $this->hasOne(RingkasanAnsuran::class, 'ans_id', 'ans_id');
    }

    public function lelong(): HasOne
    {
        return $this->hasOne(RingkasanLelong::class, 'lel_id', 'lel_id');
    }

    public function tebus(): HasOne
    {
        return $this->hasOne(RingkasanTebus::class, 'teb_id', 'teb_id');
    }

    public function scopeJumlahGadai($query,$mula,$hingga) : Float
    {
        $from = date($mula);
        $to = date($hingga);

        $gadai = $query->with('gadai')->whereBetween('date', [$from, $to])->get();

        return $gadai->sum(function ($gad){
            return $gad->gadai->jumlah_pembiayaan;
        });
    }

    public function scopeBilanganGadai($query,$mula,$hingga) : Float
    {
        $from = date($mula);
        $to = date($hingga);

        $gadai = $query->with('gadai')->whereBetween('date', [$from, $to])->get();

        return $gadai->sum(function($gad){
            return $gad->gadai->bilangan_pembiayaan;
        });
    }

    public function scopeGadai70($query,$mula,$hingga) : Float
    {
        $from = date($mula);
        $to = date($hingga);

        $gadai = $query->with('gadai')->whereBetween('date', [$from, $to])->get();

        return $gadai->sum(function($gad){
            return $gad->gadai->gadai_70;
        });
    }

    public function scopeGadai75($query,$mula,$hingga) : Float
    {
        $from = date($mula);
        $to = date($hingga);

        $gadai = $query->with('gadai')->whereBetween('date', [$from, $to])->get();

        return $gadai->sum(function($gad){
            return $gad->gadai->gadai_75;
        });
    }

    public function scopeGadai80($query,$mula,$hingga) : Float
    {
        $from = date($mula);
        $to = date($hingga);

        $gadai = $query->with('gadai')->whereBetween('date', [$from, $to])->get();

        return $gadai->sum(function($gad){
            return $gad->gadai->gadai_80;
        });
    }

    public function scopeNm70($query,$mula,$hingga) : Float
    {
        $from = date($mula);
        $to = date($hingga);

        $gadai = $query->with('gadai')->whereBetween('date', [$from, $to])->get();

        return $gadai->sum(function($gad){
            return $gad->gadai->nm_70;
        });
    }

    public function scopeNm75($query,$mula,$hingga) : Float
    {
        $from = date($mula);
        $to = date($hingga);

        $gadai = $query->with('gadai')->whereBetween('date', [$from, $to])->get();

        return $gadai->sum(function($gad){
            return $gad->gadai->nm_75;
        });
    }


    public function scopeNm80($query,$mula,$hingga) : Float
    {
        $from = date($mula);
        $to = date($hingga);

        $gadai = $query->with('gadai')->whereBetween('date', [$from, $to])->get();

        return $gadai->sum(function($gad){
            return $gad->gadai->nm_80;
        });
    }

    public function scopeBil70($query,$mula,$hingga) : Float
    {
        $from = date($mula);
        $to = date($hingga);

        $gadai = $query->with('gadai')->whereBetween('date', [$from, $to])->get();

        return $gadai->sum(function($gad){
            return $gad->gadai->bil_70;
        });
    }

    public function scopeBil75($query,$mula,$hingga) : Float
    {
        $from = date($mula);
        $to = date($hingga);

        $gadai = $query->with('gadai')->whereBetween('date', [$from, $to])->get();

        return $gadai->sum(function($gad){
            return $gad->gadai->bil_75;
        });
    }

    public function scopeBil80($query,$mula,$hingga) : Float
    {
        $from = date($mula);
        $to = date($hingga);

        $gadai = $query->with('gadai')->whereBetween('date', [$from, $to])->get();

        return $gadai->sum(function($gad){
            return $gad->gadai->bil_80;
        });
    }

    public function scopePendahuluan($query,$mula,$hingga) : Float
    {
        $from = date($mula);
        $to = date($hingga);

        return $query->whereBetween('date', [$from, $to])->sum('pendahuluan');
    }

    public function scopePelanggan($query,$mula,$hingga) : Int
    {
        $from = date($mula);
        $to = date($hingga);

        return $query->whereBetween('date', [$from, $to])->sum('pelangganbaru');
    }

    public function scopeSerahan($query,$mula,$hingga) : Float
    {
        $from = date($mula);
        $to = date($hingga);

        return $query->whereBetween('date', [$from, $to])->sum('serahan');
    }

    public function scopeSerahanOnline($query,$mula,$hingga) : Float
    {
        $from = date($mula);
        $to = date($hingga);

        return $query->whereBetween('date', [$from, $to])->sum('serahan_online');
    }

    public function scopeBelanjaAm($query,$mula,$hingga) : Float
    {
        $from = date($mula);
        $to = date($hingga);

        return $query->whereBetween('date', [$from, $to])->sum('belanjaam');
    }

    public function scopeBaikiDanSelenggara($query,$mula,$hingga) : Float
    {
        $from = date($mula);
        $to = date($hingga);

        return $query->whereBetween('date', [$from, $to])->sum('baikidanselenggara');
    }

    public function scopeKeraian($query,$mula,$hingga) : Float
    {
        $from = date($mula);
        $to = date($hingga);

        return $query->whereBetween('date', [$from, $to])->sum('keraian');
    }

    public function scopePenebusan($query,$mula,$hingga) : Float
    {
        $from = date($mula);
        $to = date($hingga);

        $tebus = $query->with('tebus')->whereBetween('date', [$from, $to])->get();
        return $tebus->sum(function ($gad){
            return $gad->tebus->pembiayaan_dibayar;
        });
    }

    public function scopeKeuntunganPenebusan($query,$mula,$hingga) : Float
    {
        $from = date($mula);
        $to = date($hingga);

        $tebus = $query->with('tebus')->whereBetween('date', [$from, $to])->get();
        return $tebus->sum(function ($gad){
            return $gad->tebus->keuntungan_dibayar;
        });
    }

    public function scopeBilanganPenebusan($query,$mula,$hingga) : Float
    {
        $from = date($mula);
        $to = date($hingga);

        $tebus = $query->with('tebus')->whereBetween('date', [$from, $to])->get();
        return $tebus->sum(function ($gad){
            return $gad->tebus->bilangan_tebus;
        });
    }

    public function scopeJumlahLelong($query,$mula,$hingga) : Float
    {
        $from = date($mula);
        $to = date($hingga);

        $lelong = $query->with('lelong')->whereBetween('date', [$from, $to])->get();
        return $lelong->sum(function ($gad){
            return $gad->lelong->lelong_current;
        });
    }

    public function scopeBakiLelong($query,$mula,$hingga) : Float
    {
        $from = date($mula);
        $to = date($hingga);

        $lelong = $query->with('lelong')->whereBetween('date', [$from, $to])->get();
        return $lelong->sum(function ($gad){
            return $gad->lelong->baki_lelong;
        });
    }

    public function scopeRugiLelong($query,$mula,$hingga) : Float
    {
        $from = date($mula);
        $to = date($hingga);

        $lelong = $query->with('lelong')->whereBetween('date', [$from, $to])->get();
        return $lelong->sum(function ($gad){
            return $gad->lelong->rugi_lelong;
        });
    }

    public function scopeBayaranRugi($query,$mula,$hingga) : Float
    {
        $from = date($mula);
        $to = date($hingga);

        $lelong = $query->with('lelong')->whereBetween('date', [$from, $to])->get();
        return $lelong->sum(function ($gad){
            return $gad->lelong->bayaran_rugi;
        });
    }

    public function scopeBayaranBaki($query,$mula,$hingga) : Float
    {
        $from = date($mula);
        $to = date($hingga);

        $lelong = $query->with('lelong')->whereBetween('date', [$from, $to])->get();
        return $lelong->sum(function ($gad){
            return $gad->lelong->bayaran_baki;
        });
    }

    public function scopeCajLelong($query,$mula,$hingga) : Float
    {
        $from = date($mula);
        $to = date($hingga);

        $lelong = $query->with('lelong')->whereBetween('date', [$from, $to])->get();
        return $lelong->sum(function ($gad){
            return $gad->lelong->caj_lelong;
        });
    }

    public function scopeBilLelong($query,$mula,$hingga) : Float
    {
        $from = date($mula);
        $to = date($hingga);

        $lelong = $query->with('lelong')->whereBetween('date', [$from, $to])->get();
        return $lelong->sum(function ($gad){
            return $gad->lelong->bil_lelong;
        });
    }

    public function scopePembiayaanAnsuran($query,$mula,$hingga) : Float
    {
        $from = date($mula);
        $to = date($hingga);

        $ansuran = $query->with('ansuran')->whereBetween('date', [$from, $to])->get();
        return $ansuran->sum(function ($gad){
            return $gad->ansuran->pembiayaan_dibayar;
        });
    }
    
    public function scopeKeuntunganAnsuran($query,$mula,$hingga) : Float
    {
        $from = date($mula);
        $to = date($hingga);

        $ansuran = $query->with('ansuran')->whereBetween('date', [$from, $to])->get();
        return $ansuran->sum(function ($gad){
            return $gad->ansuran->keuntungan_dibayar;
        });
    }

    public function scopeBilAnsuran($query,$mula,$hingga) : Float
    {
        $from = date($mula);
        $to = date($hingga);

        $ansuran = $query->with('ansuran')->whereBetween('date', [$from, $to])->get();
        return $ansuran->sum(function ($gad){
            return $gad->ansuran->bilangan_ansuran;
        });
    }

    public function scopePembiayaanOnline($query,$mula,$hingga) : Float
    {
        $from = date($mula);
        $to = date($hingga);

        $ansuran = $query->with('ansuran')->whereBetween('date', [$from, $to])->get();
        return $ansuran->sum(function ($gad){
            return $gad->ansuran->pembiayaan_dibayar_online;
        });
    }

    public function scopeKeuntunganOnline($query,$mula,$hingga) : Float
    {
        $from = date($mula);
        $to = date($hingga);

        $ansuran = $query->with('ansuran')->whereBetween('date', [$from, $to])->get();
        return $ansuran->sum(function ($gad){
            return $gad->ansuran->keuntungan_dibayar_online;
        });
    }

    public function scopeBilOnline($query,$mula,$hingga) : Float
    {
        $from = date($mula);
        $to = date($hingga);

        $ansuran = $query->with('ansuran')->whereBetween('date', [$from, $to])->get();
        return $ansuran->sum(function ($gad){
            return $gad->ansuran->bilangan_bayaran_online;
        });
    }



    public $timestamps = false;

}