Imports IntraSell_DLL
Imports IntraSell_Net_Base
Module ModuleReportsAutomatisch
    Public Sub SendeStatistik()
        Try
            If VarValue(Variablen.STATISTIKEN_SENDE_TAGESUMSATZ) Then
                Dim subject = "Tagesumsatz " & Today
                SendeAuswertung("ReportVorgangStatistik.rdlc", subject, Today, Now)
            End If

            If VarValue(Variablen.STATISTIKEN_SENDE_MONATSUMSATZ) Then

                Dim lastDay = Today.AddMonths(-1)
                lastDay = lastDay.AddDays(-1 * (Today.Day - 1))
                Dim DatumVon = lastDay
                Dim DatumBis = lastDay.AddMonths(1).AddMinutes(-1)

                Dim subject = "Monatsumsatz " & DatumVon & " bis " & DatumBis

                SendeAuswertung("ReportVorgangStatistik.rdlc", subject, DatumVon, DatumBis)
            End If

            If VarValue(Variablen.STATISTIKEN_SENDE_CHARTS) Then
                Dim subject = "Auswertung letzte 30 Tage "
                SendeAuswertung("ReportVorgangStatistikCharts.rdlc", subject, Now.AddDays(-30), Now)
            End If
        Catch ex As Exception
            HandleAppError(ex, Silent:=True)
        End Try
    End Sub

    Private Sub SendeAuswertung(reportName As String, reportSubject As String, DatumVon As Date, DatumUntil As Date)

        Dim filter = Nothing
        Dim exportPDF = ModuleReports.ShowVerkaufsstatistik(DatumVon, DatumUntil, filter, reportName, vorgangTyp:="AR", exportAsPDF:=True)
        'Send report 
        'Mail

        Dim mail = New Email
        mail.FilePath = exportPDF
        mail.EmailFrom = VarValue(Variablen.FIRMA_EMAIL)
        mail.EmailTo = VarValue(Variablen.STATISTIKEN_EMAIL_TO)
        Dim filekey = BerechneFileKey(mail.FilePath)
        mail.Mailtext = reportSubject & " " & VarValue(Variablen.FIRMA) & vbNewLine & "<br>" & MailTextVorbereiten(filekey)
        mail.SofortSenden = True
        mail.Subject = reportSubject & " " & VarValue(Variablen.FIRMA)
        ModuleMailGriton.DateiEinemKundenSenden(mail)
    End Sub

    Friend Sub ReportTaskAnlegen()
        Dim wann = Now.AddDays(1)
        If ExistsVarValue(Variablen.STATISTIKEN_UHRZEIT) Then
            Dim uhrzeit = VarValue(Variablen.STATISTIKEN_UHRZEIT)
            wann = Today.AddDays(1).AddHours(uhrzeit.Split(":")(0)).AddMinutes(uhrzeit.Split(":")(1))
        End If
        Dim reportCommand As New IntraSellReportCommand
        ModuleGlobal.RequestCommandMitZeitpunktDerAusfuehrung(reportCommand, wann)
    End Sub
End Module
