Files
SDL/SDL/kunden/frmKundenUebersichtZollRgDetails.vb
2026-04-27 17:10:01 +02:00

308 lines
13 KiB
VB.net

Imports System.Globalization
Imports System.IO
Imports VERAG_PROG_ALLGEMEIN
Public Class frmKundenUebersichtZollRgDetails
Dim RK_ID As Integer = -1
Dim SQL As New SQL
Dim sammelrechnungskopie As Boolean
Dim filialeTmp As String
Dim abfertigungsNrTmp As String
Sub New(RK_ID)
' Dieser Aufruf ist für den Designer erforderlich.
InitializeComponent()
Me.RK_ID = RK_ID
' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.
End Sub
Sub New(RK_ID, Kopie)
' Dieser Aufruf ist für den Designer erforderlich.
InitializeComponent()
Me.RK_ID = RK_ID
Me.sammelrechnungskopie = Kopie
' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.
End Sub
Private Sub frmKundenUebersichtZollRg_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim dt As DataRow = SQL.loadDgvBySql("SELECT *, /*[SteuerpflichtigerGesamtbetrag]*[Steuersatz %]*/ SteuerpflichtigerGesamtbetrag/(1+[Steuersatz %])*[Steuersatz %] as ErgSteuerbetrag , [SteuerpflichtigerGesamtbetrag]+[SteuerfreierGesamtbetrag] as RgBetrag FROM Rechnungsausgang WHERE RK_ID = " & RK_ID & "", "FMZOLL").Rows(0)
lblRgNr.Text = dt("RechnungsNr").ToString
lblRgBetrag.Text = CDbl(dt("RgBetrag")).ToString("N2", CultureInfo.CurrentCulture)
lblRgDat.Text = dt("RechnungsDatum").ToString
lblKunde.Text = dt("RechnungsName 1").ToString
lblKdNr.Text = dt("RechnungsKundenNr").ToString
lblOrt.Text = dt("RechnungsOrt").ToString
lblUID.Text = dt("RechnungsUstIdKz").ToString & " " & dt("RechnungsUstIdNr").ToString
lblSteuerbetrag.Text = CDbl(dt("ErgSteuerbetrag")).ToString("N2", CultureInfo.CurrentCulture)
lblAbfDat.Text = dt("Abfertigungsdatum").ToString
lblAbfNr.Text = dt("FilialenNr").ToString & " " & dt("AbfertigungsNr").ToString
filialeTmp = dt("FilialenNr").ToString
abfertigungsNrTmp = dt("AbfertigungsNr").ToString
lblSteuersatz.Text = (CDbl(dt("Steuersatz %")) * 100).ToString("N2", CultureInfo.CurrentCulture)
cbxLastschrift.Checked = CBool(dt("Lastschrift").ToString)
lblBelegartNr.Text = dt("BelegartenNr").ToString
lblWaerung.Text = dt("Währungscode").ToString
If RK_ID > 0 Then initDGV()
End Sub
Sub initDGV()
With DataGridView
.Columns.Clear()
.DataSource = SQL.loadDgvBySql("SELECT LeistungsNr, [Steuerpflichtig], Anzahl ,[LeistungsBez],Preis as Einzelpreis, ([SteuerpflichtigerBetrag]+[SteuerfreierBetrag]) AS Betrag " &
" FROM RechnungsausgangPositionen " &
" WHERE RK_ID = " & RK_ID & "", "FMZOLL")
' " ORDER BY Rechnungsausgang.RechnungsDatum DESC , Rechnungsausgang.RechnungsNr, Rechnungsausgang.Abfertigungsdatum DESC , Rechnungsausgang.FilialenNr, Rechnungsausgang.AbfertigungsNr, Rechnungsausgang.UnterNr ", "FMZOLL")
.Columns("LeistungsNr").Width = 85
.Columns("LeistungsNr").HeaderText = "LeistungsNr"
.Columns("LeistungsNr").DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter
.Columns("Steuerpflichtig").Width = 60
.Columns("Steuerpflichtig").HeaderText = "Steuerpfl."
.Columns("Steuerpflichtig").DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter
.Columns("Anzahl").Width = 50
.Columns("LeistungsBez").MinimumWidth = 150
.Columns("LeistungsBez").HeaderText = "LeistungsNr"
.Columns("LeistungsBez").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
.Columns("Einzelpreis").Width = 80
.Columns("Einzelpreis").DefaultCellStyle.Format = "N2"
.Columns("Einzelpreis").DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopRight
.Columns("Betrag").Width = 100
.Columns("Betrag").HeaderText = "Betrag"
.Columns("Betrag").DefaultCellStyle.Format = "N2"
.Columns("Betrag").DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopRight
End With
End Sub
Private Sub Button15_Click(sender As Object, e As EventArgs) Handles Button15.Click
Select Case VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA
Case "ATILLA" : Exit Sub
Case "IMEX" 'Imex nur mit Berechtigungsgruppe FAKTURIERUNG im SDL
If Not VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG_bool("FAKTURIERUNG", "SDL") Then
Exit Sub
End If
End Select
Try
If DataGridView.SelectedRows.Count = 0 Then Exit Sub
If Not IsNumeric(filialeTmp) Then Exit Sub
If Not IsNumeric(abfertigungsNrTmp) Then Exit Sub
cProgramFunctions.openThereforeNavigator(filialeTmp.ToString & abfertigungsNrTmp.ToString, filialeTmp)
Catch ex As Exception
MsgBox("Fehler beim Öffnen!" & vbNewLine & ex.Message & ex.StackTrace)
End Try
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
cFakturierung.doRechnungsDruck_SRorER(RK_ID,, False, 3,,,,, sammelrechnungskopie)
End Sub
Public Shared Function ConvertToPdfA3_WithMustang(inputPdf As String) As String
Dim outputPdf As String = inputPdf & ".a3.pdf"
Dim mustangJar As String = "C:\Users\d.breimaier\Downloads\Mustang-CLI-2.21.0.jar" ' Pfad anpassen!
Dim psi As New ProcessStartInfo()
psi.FileName = "C:\Program Files\Eclipse Adoptium\jre-11.0.28.6-hotspot\bin\java.exe"
psi.Arguments = $"-Xmx1G -Dfile.encoding=UTF-8 -jar ""{mustangJar}"" --action a3only --source ""{inputPdf}"" --out ""{outputPdf}"""
psi.UseShellExecute = False
psi.RedirectStandardOutput = True
psi.RedirectStandardError = True
psi.CreateNoWindow = True
Using proc As Process = Process.Start(psi)
Dim stdout As String = proc.StandardOutput.ReadToEnd()
Dim stderr As String = proc.StandardError.ReadToEnd()
proc.WaitForExit()
If proc.ExitCode <> 0 Then
Throw New Exception("Mustang Fehler: " & stderr)
End If
End Using
Return outputPdf
End Function
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim Path_temp As String = ""
cFakturierung.doRechnungsDruck_SRorER(RK_ID,, False, 3, Path_temp,,,, sammelrechnungskopie)
If Path_temp <> "" Then
Try
'ConvertToPdfA3_WithMustang(Path_temp)
Dim psi As New ProcessStartInfo()
psi.FileName = "C:\Program Files\Eclipse Adoptium\jre-11.0.28.6-hotspot\bin\java.exe"
psi.Arguments = "-jar ""C:\Users\d.breimaier\Downloads\Mustang-CLI-2.21.0.jar"" --no-notices --action validate --source " & Path_temp & ""
psi.RedirectStandardOutput = True
psi.RedirectStandardError = True
psi.UseShellExecute = False
psi.CreateNoWindow = True
Using p As Process = Process.Start(psi)
Dim output = p.StandardOutput.ReadToEnd()
Dim err = p.StandardError.ReadToEnd()
p.WaitForExit()
Console.WriteLine("ExitCode: " & p.ExitCode)
Console.WriteLine(output)
Console.WriteLine(err)
If output <> "" Then
Dim tmpPath_Report = VERAG_PROG_ALLGEMEIN.DATENVERVER_OPTIONS.getTMPPath("MUSTANG-REPORT", ".pdf", , False)
CreateValidationPdf(output, Path_temp, RK_ID, tmpPath_Report)
If tmpPath_Report <> "" Then Process.Start(tmpPath_Report)
End If
End Using
Catch ex As Exception
VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name)
End Try
End If
End Sub
Private Function ExtractMustangResult(xmlLog As String, invoiceFile As String, RK_ID As Integer) As String
Dim doc As New Xml.XmlDocument()
doc.LoadXml(xmlLog)
Dim fileInfo As New FileInfo(invoiceFile)
Dim invoice As New cRechnungsausgang(RK_ID)
' --- PDF Status ---
Dim pdfNode = doc.SelectSingleNode("//pdf/summary")
Dim pdfStatus As String = If(pdfNode?.Attributes("status")?.Value = "valid", "VALID", "INVALID")
' --- XML Status ---
Dim xmlNode = doc.SelectSingleNode("//xml/summary")
Dim xmlStatus As String = If(xmlNode?.Attributes("status")?.Value = "valid", "VALID", "INVALID")
' --- Profile ---
Dim profileNode = doc.SelectSingleNode("//xml/info/profile")
Dim profile As String = If(profileNode IsNot Nothing, profileNode.InnerText, "UNKNOWN")
' --- XML Version ---
Dim xmlVersionNode = doc.SelectSingleNode("//xml/info/version")
Dim xmlVersion As String = If(xmlVersionNode IsNot Nothing, xmlVersionNode.InnerText, "-")
' --- Validator Version ---
Dim validatorNode = doc.SelectSingleNode("//xml/info/validator")
Dim validatorVersion As String = validatorNode?.Attributes("version")?.Value
' --- Duration ---
Dim pdfDuration = doc.SelectSingleNode("//pdf/info/duration")?.InnerText
Dim xmlDuration = doc.SelectSingleNode("//xml/info/duration")?.InnerText
Dim duration As String = ""
If pdfDuration IsNot Nothing AndAlso xmlDuration IsNot Nothing Then
duration = $"{pdfDuration} ms (PDF) / {xmlDuration} ms (XML)"
End If
' --- Tests ---
Dim firedNode = doc.SelectSingleNode("//xml/info/rules/fired")
Dim failedNode = doc.SelectSingleNode("//xml/info/rules/failed")
Dim fired As Integer = If(firedNode IsNot Nothing, Integer.Parse(firedNode.InnerText), 0)
Dim failed As Integer = If(failedNode IsNot Nothing, Integer.Parse(failedNode.InnerText), 0)
Dim passed As Integer = fired - failed
' --- PDF Errors ---
Dim failedAssertions = doc.SelectNodes("//pdf//TestAssertion[@status='failed']")
Dim errorCount As Integer = If(failedAssertions IsNot Nothing, failedAssertions.Count, 0)
' --- Top Errors ---
Dim errorDetails As New List(Of String)
If failedAssertions IsNot Nothing Then
For i = 0 To Math.Min(2, failedAssertions.Count - 1)
Dim msg = failedAssertions(i).Attributes("message")?.Value
If msg IsNot Nothing Then errorDetails.Add("- " & msg)
Next
End If
' --- Output ---
Dim result As New Text.StringBuilder()
result.AppendLine("ZUGFeRD VALIDATION RESULT")
result.AppendLine("================================")
result.AppendLine($"PDF File : {fileInfo.Name}")
result.AppendLine($"Invoice No : {invoice.RechnungsNr}")
result.AppendLine("--------------------------------")
result.AppendLine($"PDF Status : {pdfStatus}")
result.AppendLine($"XML Status : {xmlStatus}")
result.AppendLine($"Profile : {profile}")
result.AppendLine($"XML Version : {xmlVersion}")
result.AppendLine($"Validator : {validatorVersion}")
result.AppendLine("--------------------------------")
result.AppendLine($"Tests Passed : {passed} / {fired}")
result.AppendLine($"XML Failed : {failed}")
result.AppendLine($"PDF Errors : {errorCount}")
result.AppendLine($"Duration : {duration}")
If errorDetails.Count > 0 Then
result.AppendLine("--------------------------------")
result.AppendLine("Top Errors:")
For Each err_ In errorDetails
result.AppendLine(err_)
Next
End If
result.AppendLine("================================")
Return result.ToString()
End Function
Public Sub CreateValidationPdf(log As String, Invoice_file As String, RK_ID As Integer, outputPath As String)
Dim resultText = ExtractMustangResult(log, Invoice_file, RK_ID)
Using fs As New FileStream(outputPath, FileMode.Create)
Using doc As New itextsharp.text.Document()
Dim writer = itextsharp.text.pdf.PdfWriter.GetInstance(doc, fs)
doc.Open()
Dim titleFont = itextsharp.text.FontFactory.GetFont("Arial", 14, itextsharp.text.Font.BOLD)
Dim bodyFont = itextsharp.text.FontFactory.GetFont("Arial", 10)
doc.Add(New itextsharp.text.Paragraph("ZUGFeRD VALIDATION REPORT", titleFont))
doc.Add(New itextsharp.text.Paragraph(" "))
Dim lines = resultText.Split(vbCrLf)
For Each line In lines
doc.Add(New itextsharp.text.Paragraph(line, bodyFont))
Next
doc.Close()
End Using
End Using
End Sub
End Class