Files
SDL/VERAG_PROG_ALLGEMEIN/Classes/cSMS.vb
2021-09-14 13:00:45 +02:00

342 lines
14 KiB
VB.net

Imports com.esendex.sdk.messaging
Imports com.esendex.sdk.sent
Imports com.esendex.sdk.core
Imports System.Data.SqlClient
Imports System.Reflection
Public Class cSMS
Property sms_id As Integer
Property sms_Datum As Date = Now
Property sms_Firma As Object = Nothing
Property sms_Account As Object = Nothing
Property sms_Fahrername As Object = Nothing
Property sms_KundenNr As Object = Nothing
Property sms_Kunde As Object = Nothing
Property sms_Vorwahl As Object = Nothing
Property sms_Handynummer As Object = Nothing
Property sms_Nachricht As Object = Nothing
Property sms_BatchId As Object = Nothing
Property sms_MessageIds As Object = Nothing
Property sms_Uris As Object = Nothing
Property sms_Sachbearbeiter As String = Nothing
Property sms_SachbearbeiterId As Integer
Property sms_AvisoId As Object = Nothing
Property sms_Status As Integer = -1
Property sms_Queued As Object = Nothing
Property sms_Sent As Object = Nothing
Property sms_Delivered As Object = Nothing
Property sms_Failure As Object = Nothing
Dim ESENDEX_USER As String = "al@verag.ag"
Dim ESENDEX_PWD As String = "Developer#2"
Dim SQL As New SQL
Sub New()
sms_Sachbearbeiter = If(sms_Sachbearbeiter, VERAG_PROG_ALLGEMEIN.cAllgemein.USRKURZNAME)
If sms_SachbearbeiterId <= 0 Then sms_SachbearbeiterId = VERAG_PROG_ALLGEMEIN.cAllgemein.USRID
End Sub
Sub New(sms_id)
Me.sms_id = sms_id
LOAD()
End Sub
Public Function getParameterList() As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable)
Dim list As New List(Of VERAG_PROG_ALLGEMEIN.SQLVariable)
list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sms_id", sms_id, , True, True))
list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sms_Datum", sms_Datum))
list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sms_Firma", sms_Firma))
list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sms_Account", sms_Account))
list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sms_Fahrername", sms_Fahrername))
list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sms_KundenNr", sms_KundenNr))
list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sms_Kunde", sms_Kunde))
list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sms_Vorwahl", sms_Vorwahl))
list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sms_Handynummer", sms_Handynummer))
list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sms_Nachricht", sms_Nachricht))
list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sms_BatchId", sms_BatchId))
list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sms_MessageIds", sms_MessageIds))
list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sms_Uris", sms_Uris))
list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sms_Sachbearbeiter", sms_Sachbearbeiter))
list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sms_SachbearbeiterId", sms_SachbearbeiterId))
list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sms_AvisoId", sms_AvisoId))
list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sms_Status", sms_Status))
list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sms_Queued", sms_Queued))
list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sms_Sent", sms_Sent))
list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sms_Delivered", sms_Delivered))
list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("sms_Failure", sms_Failure))
Return list
End Function
Public Function SAVE() As Boolean
Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()
Dim sqlstr = " BEGIN TRAN IF EXISTS(SELECT * FROM tblSMS WITH(updlock,serializable) WHERE sms_id=@sms_id) " &
" BEGIN " & getUpdateCmd() & " END " &
" Else " &
" BEGIN " & getInsertCmd() & " END " &
" commit tran "
sms_id = SQL.doSQLVarListID(sms_id, sqlstr, "FMZOLL", , list)
Return sms_id > 0
End Function
Public Sub LOAD()
Try
Using conn As SqlConnection = SQL.GetNewOpenConnectionFMZOLL()
Using cmd As New SqlCommand("SELECT * FROM tblSMS WHERE sms_id=@sms_id ", conn)
cmd.Parameters.AddWithValue("@sms_id", sms_id)
Dim dr = cmd.ExecuteReader()
If dr.Read Then
For Each i In getParameterList()
Dim propInfo As PropertyInfo = Me.GetType.GetProperty(i.Scalarvariable)
If dr.Item(i.Text) Is DBNull.Value Then
propInfo.SetValue(Me, Nothing)
Else
propInfo.SetValue(Me, dr.Item(i.Text))
End If
Next
End If
dr.Close()
End Using
End Using
Catch ex As Exception
MsgBox("Fehler in der Funktion '" & System.Reflection.MethodInfo.GetCurrentMethod.Name & "'" & vbNewLine & vbNewLine & ex.Message & vbNewLine & vbNewLine & ex.StackTrace)
End Try
End Sub
Public Function getUpdateCmd() As String
Try
Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()
Dim str As String = ""
For Each i In list
If Not i.isPrimaryParam Then
str &= "[" & i.Text & "] = @" & i.Scalarvariable & "," '.Replace("-", "").Replace(" ", "") & ","
End If
Next
str = str.Substring(0, str.Length - 1) 'wg. ','
Return (" UPDATE [tblSMS] SET " & str & " WHERE sms_id=@sms_id ")
Catch ex As Exception
MsgBox("Fehler in der Funktion '" & System.Reflection.MethodInfo.GetCurrentMethod.Name & "'" & vbNewLine & vbNewLine & ex.Message & vbNewLine & vbNewLine & ex.StackTrace)
End Try
Return ""
End Function
Public Function getInsertCmd() As String
Try
Dim list As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable) = getParameterList()
Dim str As String = ""
Dim values As String = ""
For Each i In list
If Not i.isPrimaryParam Then
str &= "[" & i.Text & "],"
values &= "@" & i.Scalarvariable & "," '.Replace("-", "").Replace(" ", "") & ","
End If
Next
str = str.Substring(0, str.Length - 1) 'wg. ','
values = values.Substring(0, values.Length - 1) 'wg. ','
Return (" INSERT INTO tblSMS (" & str & ") VALUES(" & values & ") ")
Catch ex As Exception
MsgBox("Fehler in der Funktion '" & System.Reflection.MethodInfo.GetCurrentMethod.Name & "'" & vbNewLine & vbNewLine & ex.Message & vbNewLine & vbNewLine & ex.StackTrace)
End Try
Return ""
End Function
Public Function DELETE() As Boolean 'obj As Object, tablename As String, where As String) As Boolean
Dim sqlstr = " DELETE FROM [tblSMS] WITH(updlock,serializable) WHERE sms_id=" & Me.sms_id
Return SQL.doSQL(sqlstr, "FMZOLL")
End Function
Function sendSMS() As Boolean
Try
Dim messagingService As New MessagingService(ESENDEX_USER, ESENDEX_PWD)
'SENDEN DER SMS
Dim result = messagingService.SendMessage(New SmsMessage(Me.sms_Handynummer, Me.sms_Nachricht, Me.sms_Account))
Dim Uris As String = ""
Dim msgIDs As String = ""
Try
Me.sms_BatchId = result.BatchId
If result.MessageIds IsNot Nothing Then
For Each msg In result.MessageIds
Uris &= msg.Uri & ";;"
msgIDs &= msg.Id.ToString & ";;"
Next
End If
Catch ex As Exception
End Try
While Uris.EndsWith(";;")
Uris = Uris.Substring(0, Uris.Length - 2)
End While
While msgIDs.EndsWith(";;")
msgIDs = msgIDs.Substring(0, msgIDs.Length - 2)
End While
Me.sms_MessageIds = msgIDs
Me.sms_Uris = Uris
If Not Me.SAVE() Then
MsgBox("Fehler: SMS Daten wurden nicht gespeichert!")
End If
Return True
Catch ex As Exception
MsgBox("Fehler beim Senden der SMS!" & vbNewLine & ex.Message & ex.StackTrace)
Return False
End Try
End Function
Function checkSMSStatus(Optional silent = False, Optional insertVermerkAviso = True) As String
Try
Select Case Me.sms_Status
Case MessageStatus.Submitted, MessageStatus.Sent, MessageStatus.Scheduled, MessageStatus.Connecting, -1
'Weiter...
Case Else
' Bereits finaler Status, keine Abfrage nötig
Return Me.sms_Status
End Select
If If(Me.sms_MessageIds, "") = "" Then
If Not silent Then MsgBox("Keine MessageId gefunden!")
Return False ' Keine MsgId
End If
Dim sendService As New SentService(ESENDEX_USER, ESENDEX_PWD)
'Send a message and retrieve the message ID
Dim MSG = sendService.GetMessage(New Guid(Me.sms_MessageIds.ToString))
Dim msgStatus = MSG.Status
' MsgBox(MSG.Status)
'Status in DB Ubdaten
Me.sms_Status = msgStatus
Me.sms_Sent = MSG.SentAt
Me.sms_Delivered = MSG.DeliveredAt
'If IsDate(MSG.DeliveredAt) Then
' Me.sms_Status = 2 ': MsgBox("SENT")
'Else
' 'MsgBox("NOT SENT")
'End If
If MSG.FailureReason IsNot Nothing Then Me.sms_Failure = MSG.FailureReason.Description
Me.SAVE()
If True Then
Try
If Me.sms_AvisoId IsNot Nothing AndAlso IsNumeric(Me.sms_AvisoId) Then
Select Case MSG.Status
Case MessageStatus.Expired, MessageStatus.Failed, MessageStatus.FailedAuthorisation, MessageStatus.Cancelled, MessageStatus.Rejected
'Keine Übermittlung.
Dim MaId As Integer = SQL.DLookup("LetzterMitarbeiterId", "Aviso", "AvisoId='" & Me.sms_AvisoId & "' ", "AVISO", -1)
If MaId > 0 Then
VERAG_PROG_ALLGEMEIN.cAvisoBenachrichtigungen.INSERT_BENACHRICHTIGUNG(Me.sms_AvisoId, Nothing, 3, "B", MaId, "*SMS Fehler*", 0, "", "Fehler bei der SMS Übermittlung: " & If(Me.sms_Failure, ""))
End If
End Select
End If
Catch ex As Exception
VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodBase.GetCurrentMethod.Name)
End Try
End If
Return msgStatus
Catch ex As Exception
If Not silent Then MsgBox("Fehler beim Prüfen des SMS Status!" & vbNewLine & ex.Message & ex.StackTrace)
End Try
Return ""
End Function
Shared Function SEND_StatusSMS(AVISOId As Integer) As Boolean
Dim AVISO = cAviso.getAvisoById(AVISOId, "")
Return SEND_StatusSMS(AVISO)
End Function
Shared Function SEND_StatusSMS(AVISO As cAviso) As Boolean
If If(AVISO.FahrerHandy, "") <> "" AndAlso IsNumeric(AVISO.FahrerHandy.trim) Then
Dim absenderAccount = ""
Dim absenderFirma = ""
VERAG_PROG_ALLGEMEIN.cSMS.GET_AccountInfo(VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA, absenderAccount, absenderFirma)
' If vbYes = MsgBox("Soll die Nachricht gesendet werden?" & vbNewLine & "Handy-Nummer: " & nr, MsgBoxStyle.YesNo) Then
Dim SMS = New VERAG_PROG_ALLGEMEIN.cSMS
SMS.sms_Vorwahl = ""
SMS.sms_Handynummer = "00436644178557" 'AVISO.FahrerHandy.trim 'txtHandyNummer.Text.Replace(" ", "").Replace("-", "").Replace("/", "")
SMS.sms_Nachricht = "Statusmeldung: " & vbNewLine &
"https://login.verag.ag/status.aspx?AvisoId=" & VERAG_PROG_ALLGEMEIN.cCryptography3.Encrypt(AVISO.AvisoID)
SMS.sms_Account = absenderAccount
SMS.sms_Firma = absenderFirma
SMS.sms_KundenNr = If(AVISO.Frächter_KdNr > 0, AVISO.Frächter_KdNr, Nothing)
SMS.sms_Kunde = If(AVISO.Frächter <> "", AVISO.Frächter, Nothing)
SMS.sms_Fahrername = Nothing 'If(AVISO.fahrern <> "", FahrerName, Nothing)
SMS.sms_AvisoId = AVISO.AvisoID
If SMS.sendSMS Then
Return True
End If
End If
Return False
End Function
Shared Sub GET_AccountInfo(Firma, ByRef absenderAccount, ByRef absenderFirma)
Select Case Firma'cboFirma._value
Case "VERAG" ', "BEIDE"
Select Case VERAG_PROG_ALLGEMEIN.cAllgemein.MITARBEITER.mit_abteilung
Case "ZOLL", "QS" : absenderAccount = "EX0252513" : absenderFirma = "VERAG Spedition"
Case "MDM" : absenderAccount = "EX0252747" : absenderFirma = "VERAG 360"
Case Else : absenderAccount = "EX0252513" : absenderFirma = "VERAG Spedition"
'Case Else : MsgBox("Fehler bei der Datenprüfung: Abteilung.") : Exit Sub
End Select
Case "ATILLA" : absenderAccount = "EX0252746" : absenderFirma = "ATILLA Spedition"
Case "IMEX" : absenderAccount = "EX0272082" : absenderFirma = "IMEX Spedition"
Case "UNISPED" : absenderAccount = "EX0300174" : absenderFirma = "UNISPED Spedition"
Case "FRONTOFFICE" : absenderAccount = "EX0300173" : absenderFirma = "FRONT-OFFICE SUBEN"
Case "AMBAR" : absenderAccount = "EX0315223" : absenderFirma = "AMBAR"
Case Else : MsgBox("Fehler bei der Datenprüfung: Firmendaten.") : Exit Sub
End Select
End Sub
End Class