<?php

namespace App\Services;

use App\KadarUpah;

class OnePercentServices
{

    protected $maks_margin = 80;
    protected $one_percent = 1;

    private function kiraanUjrah(float $nilaimarhun = 0.0,float $kadarujrah = 0.0,float $margin = 0.0){
        
        //periksa adakah kadar ujrah ini diberi percentage
        if($kadarujrah >= 1){
            return;
        }

        $ujrah = ($kadarujrah/100) * $nilaimarhun;
        $formula_margin = ($this->maks_margin - $margin) / $this->maks_margin;
        $ujrah_margin = $formula_margin * $kadarujrah;
        $rebate_ujrah = $nilaimarhun*($ujrah_margin/100);
        // $ujrah_sebenar = $ujrah;
        $ujrah_sebenar = $ujrah - $rebate_ujrah;

        return $ujrah_sebenar;
    }

    private function kiraanOnePercent(float $pembiayaan = 0.0){

        //periksa adakah pembiayaan adalah null atau kosong.
        if($pembiayaan == 0 || $pembiayaan == null)
            return;
        
            $onepercent = ($this->one_percent/100) * $pembiayaan;


        return $onepercent;
    }

    public function kiraanUjrahSebenar(float $nilaimarhun = 0.0,float $kadarujrah = 0.0,float $margin = 0.0){

        if($kadarujrah >= 1){
            return;
        }

        $ujrah = ($kadarujrah/100) * $nilaimarhun;
        $formula_margin = ($this->maks_margin - $margin) / $this->maks_margin;
        $rebate_ujrah = $formula_margin * $ujrah;
        $ujrah_sebenar = $ujrah - $rebate_ujrah;

        return $ujrah_sebenar;
    }

    public function kiraanKeuntunganRebet(float $pembiayaan = 0.0,float $nilaimarhun = 0.0,float $kadarujrah = 0.0,float $margin = 0.0){

        $onepercent = $this->kiraanOnePercent($pembiayaan);
        $ujrah_sebenar = $this->kiraanUjrahSebenar($nilaimarhun,$kadarujrah,$margin);

        $onepercent = (!empty($onepercent)) ? $this->getRoundedMethod($onepercent) : 0;
        $ujrah_sebenar = (!empty($ujrah_sebenar)) ? $this->getRoundedMethod($ujrah_sebenar) : 0;

        $keuntungan = $onepercent + $ujrah_sebenar;

        return ['keuntungan' => $keuntungan,'onepercent' => $onepercent, 'ujrah' => $ujrah_sebenar];
    }

    public function kiraanKeuntunganSebenar(float $pembiayaan = 0.0,float $nilaimarhun = 0.0,float $kadarujrah = 0.0,float $margin = 0.0){

        $onepercent = $this->kiraanOnePercent($pembiayaan);
        $ujrah_sebenar = $this->kiraanUjrah($nilaimarhun,$kadarujrah,$margin);

        $onepercent = (!empty($onepercent)) ? $this->getRoundedMethod($onepercent) : 0;
        $ujrah_sebenar = (!empty($ujrah_sebenar)) ? $this->getRoundedMethod($ujrah_sebenar) : 0;

        $keuntungan = $onepercent + $ujrah_sebenar;

        return ['keuntungan' => $keuntungan,'onepercent' => $onepercent, 'ujrah' => $ujrah_sebenar];
    }

    public function getKadarUjrah(float $nilaimarhun = 0.0, $groupid) : Float{

        $kadarujrah = KadarUpah::on('mysql7')->select('kadarujrah')->where([['min','<=',$nilaimarhun],['max','>=',$nilaimarhun]])->where('group_id',1)->get()->toArray();

        return isset($kadarujrah[0]) ? $kadarujrah[0]['kadarujrah'] : 0;
    }

    public function getRoundedMethod(float $number = 0.0) : Float{
        $number_string = number_format(floor($number * 100)/100,2);

        $rounded = substr($number_string, 0, -1);
        $lastDigit = substr($number_string, -1);

        if ($lastDigit >= 0 && $lastDigit <= 2) {
            if(strpos($rounded,',')){
                $rounded = str_replace(',','',$rounded);
            }
            return $rounded;
        } elseif ($lastDigit >= 3 && $lastDigit <= 7) {
            $rounded = $rounded . '5';
            if(strpos($rounded,',')){
                $rounded = str_replace(',','',$rounded);
            }
            return $rounded;
        } else {
            return round($number,PHP_ROUND_HALF_UP);
        }
    }
}
