<?php
class Hasil_forecast_model extends CI_model
{
   public function create($idkopi,$jenis_kopi,$jumlahkopi) {
        $this->db->where('id_alpha', 1);
        $get_alpha = $this->db->get('alpha')->row();
        $alpha = $get_alpha->alpha;
        
        $this->db->where('jenis_kopi', $jenis_kopi);
        $check_data = $this->db->get('hitung_ses')->num_rows();

        if ($check_data < 1) {
            $ft = $alpha * $jumlahkopi + ( ( 1 - $alpha ) * $jumlahkopi );
        } else {
            $this->db->where('jenis_kopi', $jenis_kopi);
            $this->db->order_by('id_hitung_ses', 'DESC');
            $get_data_last = $this->db->get('hitung_ses')->row();

            $ft = ( $alpha * $get_data_last->at ) + ( 1 - $alpha ) * $get_data_last->ft;
        }

        $se = pow($jumlahkopi - $ft, 2);
        $ad = abs($jumlahkopi - $ft);
        $ape = abs($jumlahkopi - $ft) / $jumlahkopi * 100;

        $data = [
            'id_data_kopi' => $idkopi,
            'jenis_kopi' => $jenis_kopi,
            'at' => number_format($jumlahkopi, 2),
            'ft' => number_format($ft, 2),
            'se' => number_format($se, 2),
            'ad' => number_format($ad, 2),
            'ape' => number_format($ape, 2),
        ];

        return $this->db->insert('hitung_ses', $data);
   }

   public function update($id,$jumlahkopi) {
        $this->db->where('id_alpha', 1);
        $get_alpha = $this->db->get('alpha')->row();
        $alpha = $get_alpha->alpha;

        $this->db->where('id_data_kopi', $id);
        $check_data_update = $this->db->get('hitung_ses')->row();
        

        $this->db->where('jenis_kopi', $check_data_update->jenis_kopi);
        $this->db->order_by('id_hitung_ses', 'asc');
        $get_data_first = $this->db->get('hitung_ses')->row();

        if($check_data_update->id_data_kopi == $get_data_first->id_data_kopi) {
            $ft = $alpha * $jumlahkopi + ( ( 1 - $alpha ) * $jumlahkopi );
        } else {
            $this->db->where('jenis_kopi', $check_data_update->jenis_kopi);
            $this->db->where('id_hitung_ses <', $check_data_update->id_hitung_ses);
            $this->db->order_by('id_hitung_ses', 'DESC');
            $get_data_last = $this->db->get('hitung_ses')->row();

            $ft = ( $alpha * $get_data_last->at ) + ( 1 - $alpha ) * $get_data_last->ft;
        }

        $se = pow($jumlahkopi - $ft, 2);
        $ad = abs($jumlahkopi - $ft);
        $ape = abs($jumlahkopi - $ft) / $jumlahkopi * 100;
        
        $this->db->set('at', number_format($jumlahkopi, 2));
        $this->db->set('ft', number_format($ft, 2));
        $this->db->set('se', number_format($se, 2));
        $this->db->set('ad', number_format($ad, 2));
        $this->db->set('ape', number_format($ape, 2));
        $this->db->where('id_data_kopi', $id);
        $this->db->update('hitung_ses');

        return $this->update_all($check_data_update->jenis_kopi,$check_data_update->id_hitung_ses);
   }

   public function update_all($jeniskopi, $idlastupdate){
        $this->db->where('jenis_kopi', $jeniskopi);
        $this->db->where('id_hitung_ses >', $idlastupdate);
        $get_data = $this->db->get('hitung_ses')->result();

        $this->db->where('id_alpha', 1);
        $get_alpha = $this->db->get('alpha')->row();
        $alpha = $get_alpha->alpha;

        foreach($get_data as $loop) {
            $jumlahkopi = $loop->at;

            $this->db->where('id_data_kopi', $loop->id_data_kopi);
            $check_data_update = $this->db->get('hitung_ses')->row();
            
            $this->db->where('jenis_kopi', $check_data_update->jenis_kopi);
            $this->db->order_by('id_hitung_ses', 'asc');
            $get_data_first = $this->db->get('hitung_ses')->row();

            if($check_data_update->id_data_kopi == $get_data_first->id_data_kopi) {
                $ft = $alpha * $jumlahkopi + ( ( 1 - $alpha ) * $jumlahkopi);
            } else {
                $this->db->where('jenis_kopi', $check_data_update->jenis_kopi);
                $this->db->where('id_hitung_ses <', $check_data_update->id_hitung_ses);
                $this->db->order_by('id_hitung_ses', 'DESC');
                $get_data_last = $this->db->get('hitung_ses')->row();
        
                $ft = ( $alpha * $get_data_last->at ) + ( 1 - $alpha ) * $get_data_last->ft;
            }
        
            $se = pow($jumlahkopi - $ft, 2);
            $ad = abs($jumlahkopi - $ft);
            $ape = abs($jumlahkopi - $ft) / $jumlahkopi * 100;
        
            $this->db->set('at', number_format($jumlahkopi, 2));
            $this->db->set('ft', number_format($ft, 2));
            $this->db->set('se', number_format($se, 2));
            $this->db->set('ad', number_format($ad, 2));
            $this->db->set('ape', number_format($ape, 2));
            $this->db->where('id_data_kopi', $loop->id_data_kopi);
            $this->db->update('hitung_ses');
        }
   }

   public function update_alpha($alpha) {

    $get_data = $this->db->get('hitung_ses')->result();

    foreach($get_data as $loop) {
            $jumlahkopi = $loop->at;

            $this->db->where('id_data_kopi', $loop->id_data_kopi);
            $check_data_update = $this->db->get('hitung_ses')->row();
            
            $this->db->where('jenis_kopi', $check_data_update->jenis_kopi);
            $this->db->order_by('id_hitung_ses', 'asc');
            $get_data_first = $this->db->get('hitung_ses')->row();

            if($check_data_update->id_data_kopi == $get_data_first->id_data_kopi) {
                $ft = $alpha * $jumlahkopi + ( ( 1 - $alpha ) * $jumlahkopi);
            } else {
                $this->db->where('jenis_kopi', $check_data_update->jenis_kopi);
                $this->db->where('id_hitung_ses <', $check_data_update->id_hitung_ses);
                $this->db->order_by('id_hitung_ses', 'DESC');
                $get_data_last = $this->db->get('hitung_ses')->row();
        
                $ft = ( $alpha * $get_data_last->at ) + ( 1 - $alpha ) * $get_data_last->ft;
            }
        
            $se = pow($jumlahkopi - $ft, 2);
            $ad = abs($jumlahkopi - $ft);
            $ape = abs($jumlahkopi - $ft) / $jumlahkopi * 100;
        
            $this->db->set('at', number_format($jumlahkopi, 2));
            $this->db->set('ft', number_format($ft, 2));
            $this->db->set('se', number_format($se, 2));
            $this->db->set('ad', number_format($ad, 2));
            $this->db->set('ape', number_format($ape, 2));
            $this->db->where('id_data_kopi', $loop->id_data_kopi);
            $this->db->update('hitung_ses');
        }
    }

    public function get_at($jeniskopi) {
        $this->db->select('at');
        $this->db->where('jenis_kopi', $jeniskopi);
        return $this->db->get('hitung_ses')->result_array();
    }

    public function get_ft($jeniskopi) {
        $this->db->select('ft');
        $this->db->where('jenis_kopi', $jeniskopi);
        return $this->db->get('hitung_ses')->result_array();
    }

    public function get_allses($jeniskopi) {
        $this->db->where('jenis_kopi', $jeniskopi);
        return $this->db->get('hitung_ses')->result_array();
    }

    public function get_bulan($jeniskopi) {
        $this->db->select('bulan');
        $this->db->where('jenis_kopi', $jeniskopi);
        return $this->db->get('data_kopi')->result_array();
    }

    public function mse($jeniskopi) {
        $this->db->where('jenis_kopi', $jeniskopi);
        $data = $this->db->get('hitung_ses')->result_array();

        $datarray = [];
        foreach ($data as $loop) {
            array_push($datarray, $loop['se']);
        }
        $total = array_sum($datarray);
        $count = count($datarray);

        return number_format($total / $count, 2);
    }

    public function count_mse($jeniskopi){
        $this->db->where('jenis_kopi', $jeniskopi);
        $data = $this->db->get('hitung_ses')->result_array();

        $datarray = [];
        foreach ($data as $loop) {
            array_push($datarray, $loop['se']);
        }
        $count = count($datarray);

        return $count;
    }
    public function get_mad($jeniskopi) {
        $this->db->where('jenis_kopi', $jeniskopi);
        $data = $this->db->get('hitung_ses')->result_array();

        $datarray = [];
        foreach ($data as $loop) {
            array_push($datarray, $loop['ad']);
        }
        $total = array_sum($datarray);
        $count = count($datarray);

        return number_format($total / 35, 2);
    }

    public function get_mape($jeniskopi) {
        $this->db->where('jenis_kopi', $jeniskopi);
        $data = $this->db->get('hitung_ses')->result_array();

        $datarray = [];
        foreach ($data as $loop) {
            array_push($datarray, $loop['ape']);
        }
        $total = array_sum($datarray);
        $count = count($datarray);

        return number_format($total / 35, 2);
    }

    public function bulan_berikut($jeniskopi) {

        $this->db->where('id_alpha', 1);
        $get_alpha = $this->db->get('alpha')->row();
        $alpha = $get_alpha->alpha;


        $this->db->where('jenis_kopi', $jeniskopi);
        $this->db->order_by("tahun DESC, bulan DESC");
        $data = $this->db->get('data_kopi')->row();

        $this->db->where('jenis_kopi', $jeniskopi);
        $this->db->where('id_data_kopi ', $data->id);
        $datases = $this->db->get('hitung_ses')->row();

        $hasil = ($alpha * $datases->at) + (1 - $alpha) * $datases->ft;
        return $hasil;
    }
}
