Imports System.Data.Odbc
Public Class frmHitungAkurasi
    Dim cId_Sem, cId_Ts, cAkurasi_AWal, cAkurasi_Akhir As String
    Dim Akurasi As Boolean
    Sub CekSettingDataset()
        cmd = New OdbcCommand("Select * from tbsetting_dataset ", Conn)
        rd = cmd.ExecuteReader
        rd.Read()
        If rd.HasRows Then
            cAkurasi_AWal = rd.Item("akurasi_awal")
            cAkurasi_Akhir = rd.Item("akurasi_akhir")
            Call CekDataPrediksi()
        Else
            dg1.Columns.Clear()
            txtMape.Text = ""
            MsgBox("Maaf, Setting Dataset Belum Diatur...", , "Perhatian...!")
            btnCetak.Enabled = False
            Exit Sub
        End If
    End Sub

    Sub Hitung_MSE()
        Dim TotSelisih, nMAPE As Single
        Dim nBaris, nData As Integer

        For brs As Integer = 0 To dg1.RowCount - 2
            'Hitung Error MAPE (%)
            dg1.Item(4, brs).Value = (Math.Abs(dg1.Item(2, brs).Value - dg1.Item(3, brs).Value) / dg1.Item(2, brs).Value) * 100
            TotSelisih = TotSelisih + dg1.Item(4, brs).Value
        Next

        nBaris = dg1.RowCount - 1
        rd.Close()
        nData = dg1.RowCount - 1
        dg1.Rows.Add("")
        dg1.Rows.Add("Total")
        dg1.Item(2, nBaris + 1).Value = "n = " + Microsoft.VisualBasic.Str(nData)
        dg1.Item(4, nBaris + 1).Value = TotSelisih
        nMAPE = TotSelisih / nData
        txtMape.Text = FormatNumber(nMAPE, 2) & "%"

    End Sub
    Sub CekDataPrediksi()
        Dim cTahun As String
        dg1.Columns.Clear()
        dg1.Columns.Add("kode", "Tahun")
        dg1.Columns.Add("kode", "Bulan")
        dg1.Columns.Add("kode", "Data Aktual (y)")
        dg1.Columns.Add("kode", "Data Prediksi (y')")
        dg1.Columns.Add("kode", "Error MAPE (%)")

        Dim sql As String = "Select tahun,id_bulan,jml_pendapatan from tbdataset  " & _
                   "where no_indeks >='" & cAkurasi_AWal & "' and no_indeks <='" & cAkurasi_Akhir & "' " & _
                   " order by tahun,id_bulan "

        cmd = New OdbcCommand(sql, Conn)
        rd = cmd.ExecuteReader
        Dim i As Integer = 0
        While rd.Read
            dg1.Rows.Add()
            dg1.Item(0, i).Value = rd(0)
            dg1.Item(1, i).Value = NamaBulan(rd(1) - 1)
            dg1.Item(2, i).Value = rd(2) 'Data Aktual (y)
            i += 1
        End While
        rd.Close()
        For j As Integer = 0 To dg1.RowCount - 2
            cTahun = dg1.Rows(j).Cells(0).Value
            cId_Bulan = NilaiBulan(Trim(dg1.Rows(j).Cells(1).Value))
            sql = "select prediksi_y from  tbprediksi where tahun ='" & cTahun & "' and id_bulan ='" & cId_Bulan & "'  "
            cmd = New OdbcCommand(sql, Conn)
            rd = cmd.ExecuteReader
            rd.Read()
            If rd.HasRows = True Then
                dg1.Item(3, j).Value = rd.Item(0) 'Data Prediksi (y')
            Else
                dg1.Item(3, j).Value = ""
                Akurasi = False
            End If
        Next
        rd.Close()
        dg1.ReadOnly = True
        dg1.Columns(0).Width = 75
        dg1.Columns(1).Width = 100
        dg1.Columns(2).Width = 100
        dg1.Columns(3).Width = 100
        dg1.GridColor = Color.Blue
        dg1.DefaultCellStyle.ForeColor = Color.Blue
        dg1.RowHeadersDefaultCellStyle.BackColor = Color.DeepPink
        dg1.AlternatingRowsDefaultCellStyle.BackColor = Color.Yellow
        dg1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        dg1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        dg1.Columns(0).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        dg1.Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        dg1.Columns(3).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        dg1.Columns(4).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        dg1.Columns(2).DefaultCellStyle.Format = "#,##0"
        dg1.Columns(3).DefaultCellStyle.Format = "#,##0"
        dg1.Columns(4).DefaultCellStyle.Format = "###.#0"
        If Akurasi = True Then
            btnHitungPersamaan.Enabled = True
            btnHitungPersamaan.Focus()
        Else
            MsgBox("Maaf, Hasil Prediksi untuk Perhitungan Akurasi Belum Lengkap", , "Perhatian...!")
            btnHitungPersamaan.Enabled = False
            btnTutup.Focus()
            Exit Sub
        End If
    End Sub



    Private Sub btnCetak_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCetak.Click
        Dim frmCetakHasil As New frmLaporan
        Dim Cetakdata As CrystalDecisions.CrystalReports.Engine.ReportDocument
        cmd = New OdbcCommand("Select * from tbsetting_dataset ", Conn)
        rd = cmd.ExecuteReader
        rd.Read()
        If rd.HasRows Then
            cAkurasi_AWal = rd.Item("akurasi_awal")
            cAkurasi_Akhir = rd.Item("akurasi_akhir")
            Cetakdata = New rptAkurasi
            frmCetakHasil.CRV.ReportSource = Nothing
            frmCetakHasil.CRV.SelectionFormula = "{tbdataset1.no_indeks} >= " & cAkurasi_AWal & " And {tbdataset1.no_indeks} <= " & cAkurasi_Akhir & ""
            frmCetakHasil.CRV.ReportSource = Cetakdata
            frmCetakHasil.CRV.Refresh()
            frmCetakHasil.CRV.RefreshReport()
            frmCetakHasil.Show(Me)
        End If
    End Sub



    Private Sub frmHitungMSE_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        If Asc(e.KeyChar) = Keys.Enter Then
            SendKeys.Send("{tab}")
        End If
    End Sub





    Private Sub btnTutup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTutup.Click
        Me.Dispose()
    End Sub

    Private Sub btnHitungPersamaan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHitungPersamaan.Click
        Call Hitung_MSE()
        btnCetak.Enabled = True
    End Sub



    Private Sub cboObat_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        btnTutup.Focus()
    End Sub


    Private Sub frmHitungAkurasi_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Akurasi = True
        btnHitungPersamaan.Enabled = False
        btnCetak.Enabled = False
        dg1.Columns.Clear()

        Call CekSettingDataset()
    End Sub

    Private Sub dg1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dg1.CellContentClick

    End Sub
End Class