' Copyright (C) 2025  P4pillon.org
'
' This program is free software: you can redistribute it and/or modify
' it under the terms of the GNU Affero General Public License as
' published by the Free Software Foundation, either version 3 of the
' License, or (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU Affero General Public License for more details.
'
' You should have received a copy of the GNU Affero General Public License
' along with this program.  If not, see <https://www.gnu.org/licenses/>.
Public Class ScanOrdo

    Declare Function TWAIN_AcquireToClipboard Lib "EZTW32.DLL" (ByVal hwndApp&, ByVal wPixTypes&) As Long
    Declare Function TWAIN_SelectImageSource Lib "EZTW32.DLL" (ByVal hwndApp&) As Long
    Private Sub scan()
        Dim r As Long
        r = TWAIN_SelectImageSource(Me.Handle.ToInt32)
        r = TWAIN_AcquireToClipboard(Me.Handle.ToInt32, 0)
        PictureBox1.Image = Clipboard.GetDataObject.GetData(DataFormats.Bitmap)
    End Sub

    Private Sub PictureBoxScanOrdo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBoxScanOrdo.Click, Label_ScanOrdo.Click
        scan()
    End Sub

    Private Sub PictureBox_PDF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox_PDF.Click, LabelPDF.Click
        '--------------------------------------------------
        'PostScript file
        PrintDocument_Bitmap.PrinterSettings.PrinterName = "Postscript"
        If My.Computer.FileSystem.FileExists(Application.StartupPath + "\GS\scantemp.ps") Then
            My.Computer.FileSystem.DeleteFile(Application.StartupPath + "\GS\scantemp.ps")
        End If
        PrintDocument_Bitmap.PrinterSettings.PrintFileName = Application.StartupPath + "\GS\scantemp.ps"
        PrintDocument_Bitmap.PrinterSettings.PrintToFile = True
        PrintDocument_Bitmap.Print()
        '--------------------------------------------------
        'numfacture
        Dim FileName As String = CurrentFactureVitale.G1110C1
        '--------------------------------------------------
        'compteur
        Dim compteur As Integer = 1
        While Dir(Application.StartupPath + "\scanordo\doc\" + FileName + "_" + compteur.ToString + "*.pdf") <> ""
            compteur = compteur + 1
        End While
        FileName = FileName + "_" + compteur.ToString
        '--------------------------------------------------
        'type document
        If RadioButton_ORDN.Checked = True Then
            FileName = FileName + "_ORDN"
        ElseIf RadioButton_ORDE.Checked = True Then
            FileName = FileName + "_ORDE"
        ElseIf RadioButton_FSNC.Checked = True Then
            FileName = FileName + "_FNSC"
        ElseIf RadioButton_FSEC.Checked = True Then
            FileName = FileName + "_FSEC"
        ElseIf RadioButton_TKVT.Checked = True Then
            FileName = FileName + "_TKVT"
        ElseIf RadioButton_PREV.Checked = True Then
            FileName = FileName + "_PREV"
        ElseIf RadioButton_AUTP.Checked = True Then
            FileName = FileName + "_AUTP"
        End If
        '--------------------------------------------------
        FileName = FileName + ".pdf"
        'convert PostScript to PDF
        Teletransmission_MAIN.ShellandWait(Application.StartupPath + "\GS", "gswin32c.exe", "-sDEVICE=pdfwrite -o " + FileName + " scantemp.ps")
        '--------------------------------------------------
        'move PDF file
        My.Computer.FileSystem.MoveFile(Application.StartupPath + "\GS\" + FileName, Application.StartupPath + "\scanordo\doc\" + FileName)
        '--------------------------------------------------
        'save to Bdd
        Me.SCOR_PJTableAdapter.Insert(FileName, ID_PJ(compteur.ToString), "A FINALISER", CurrentFactureVitale.G1110C1)
        VenteForm.ListBox_scanordo.Items.Add(FileName)
        Me.Close()
    End Sub

    Private Sub PrintDocument_Bitmap_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument_Bitmap.PrintPage
        e.Graphics.DrawImage(PictureBox1.Image, 0, 0)
    End Sub

    Private Sub ScanOrdo_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        scan()
    End Sub

    Private Sub PictureBox_Abandonner_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox_Abandonner.Click, LabelAbandonner.Click
        Me.Close()
    End Sub
    Private Function ID_PJ(ByVal compteur As String) As String
        '[RG_TT057]
        Dim out As String = ""
        '--------------------------------------------------
        'N° de facturation du PS responsable de la PJ + Nature_PJ
        If RadioButton_ORDN.Checked = True Then
            out = CurrentFactureVitale.G1211C1 + "." + "ORDN"
        ElseIf RadioButton_ORDE.Checked = True Then
            out = CurrentFactureVitale.G1211C1 + "." + "ORDE"
        ElseIf RadioButton_FSNC.Checked = True Then
            out = CurrentFactureVitale.G1120C1 + "." + "FSNC"
        ElseIf RadioButton_FSEC.Checked = True Then
            out = CurrentFactureVitale.G1120C1 + "." + "FSEC"
        ElseIf RadioButton_TKVT.Checked = True Then
            out = CurrentFactureVitale.G1120C1 + "." + "TKVT"
        ElseIf RadioButton_PREV.Checked = True Then
            out = CurrentFactureVitale.G1120C1 + "." + "PREV"
        ElseIf RadioButton_AUTP.Checked = True Then
            out = CurrentFactureVitale.G1120C1 + "." + "AUTP"
        End If
        '--------------------------------------------------
        'N° de facturation de PS émetteur de la PJ + Code_régime_Code_caisse_gestionnaire_Code_centre_gestionnaire
        out = out + "." + CurrentFactureVitale.G1120C1 + "." + CurrentFactureVitale.G1310C1 + CurrentFactureVitale.G1310C2 + CurrentFactureVitale.G1310C3
        '--------------------------------------------------
        'Date_constitution_PJ (AAAAMMJJHHMMSS)
        out = out + "." + Format(Now, "yyyyMMddHHmmss")
        '--------------------------------------------------
        'Compteur
        While compteur.Length < 9
            compteur = "0" + compteur
        End While
        out = out + "." + compteur

        Return out

    End Function

    
End Class