' 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/>.
Imports System.Xml
Imports System.Drawing.Printing

Public Class CERFA
    Public debile As Boolean
    Public num_facture_SV As Integer
    Public num_facture_VD As Integer
    Public Type_Papier_CERFA As Boolean
    Private drawstring As String
    Public duplicata As Boolean
    Public selected_printer As String

    Private fact_SV As officine.PharmaDataSet.FacturesDataTable
    Private fact_VD As officine.PharmaDataSet.Facture_VenteDirecteDataTable
    Private lignes_fact As officine.PharmaDataSet.Facture_LigneFactureDataTable
    Private actes_fact As officine.PharmaDataSet.ActesDataTable

    Public cerfa_médecin As Boolean


    Private Sub ecrit_drawstring()
        drawstring = En_tête_TextBox.Text + Chr(10) + _
            "N° Facture: " + G1110C1_textbox.Text + Chr(10) + _
            "Date Facture: " + G1110C3_TextBox.Text + Chr(10) + _
            "Personne recevant les soins: " + Bénéficiaire_TextBox.Text + Chr(10) + G1130C2_textbox.Text + G1130C3_textbox.Text + Chr(10) + " né le " + G1130C4_textbox.Text + Chr(10)
        If Assuré_TextBox.Text <> "" Then
            drawstring = drawstring + "Assuré: " + Assuré_TextBox.Text + Chr(10) + G1130C2_assuré_textbox.Text + G1130C3_assuré_textbox.Text + Chr(10)
        End If
        If Ident_Pharmacien_TextBox.Text <> "" Then
            drawstring = drawstring + Chr(10) + Ident_Pharmacien_TextBox.Text + Chr(10)
        End If
        If Ident_Structure_TextBox.Text <> "" Then
            drawstring = drawstring + Chr(10) + Ident_Structure_TextBox.Text + Chr(10)
        End If
        If rempl_id_TextBox.Text <> "" Then
            drawstring = drawstring + Chr(10) + "remplaçant: " + rempl_id_TextBox.Text + rempl_clé_TextBox.Text + Chr(10)
        End If
        drawstring = drawstring + Chr(10) + "Prescripteur: " + Prescr_nom_TextBox.Text + Chr(10) + Prescr_id_TextBox.Text + Chr(10) + "Struct: " + Prescr_num_structTextBox.Text + Chr(10)
        drawstring = drawstring + "date prescr: " + Prescr_Date_TextBox.Text + Chr(10)
        If G1840C3_TextBox.Text <> "" Then
            drawstring = drawstring + "date ep: " + G1840C3_TextBox.Text + Chr(10)
        End If
        If Maladie_CheckBox.Checked = True Then
            drawstring = drawstring + Chr(10) + "MALADIE" + Chr(10)
        End If
        If art155_CheckBox.Checked = True Then
            drawstring = drawstring + Chr(10) + "SOINS dispensés au titre de l'art. L. 115" + Chr(10)
        End If
        If ALD_CheckBox.Checked = True Then
            drawstring = drawstring + Chr(10) + "acte conforme au protocole ALD" + Chr(10)
        End If
        If Prev_CheckBox.Checked = True Then
            drawstring = drawstring + Chr(10) + "action de prévention" + Chr(10)
        End If
        If Autre_exo_CheckBox.Checked = True Then
            drawstring = drawstring + Chr(10) + "éxo TM" + Chr(10)
        End If
        If Accident_tiers_oui_CheckBox.Checked = True Then
            drawstring = drawstring + Chr(10) + "accident causé par un tiers " + Accident_tiers_Date_TextBox.Text + Chr(10)
        End If
        If AT_CheckBox.Checked = True Then
            drawstring = drawstring + Chr(10) + "AT/MP" + G1513C2_TextBox.Text + G1513C3_TextBox.Text + " " + G1513C1_TextBox.Text + Chr(10)
        End If
        drawstring = drawstring + Chr(10) + Chr(10) + "Produits et Prestations:" + Chr(10)
        For r = 0 To Prestations_ListBox.Items.Count - 1
            drawstring = drawstring + Prestations_ListBox.Items(r).ToString + Chr(10)
        Next
        drawstring = drawstring + Chr(10) + "Montant Total:" + Montant_Total_TextBox.Text.ToString + "€" + Chr(10)
        If TP_AMO_CheckBox.Checked = True Then
            drawstring = drawstring + "l'assuré n'a pas payé la part obligatoire" + Chr(10)
        End If
        If TP_AMC_CheckBox.Checked = True Then
            drawstring = drawstring + "l'assuré n'a pas payé la part complémentaire" + Chr(10)
        End If
    End Sub

    Public Sub écrit_CERFA(ByVal seize_actes_fact As officine.PharmaDataSet.ActesDataTable)
        If duplicata = True Then
            En_tête_TextBox.Text = "DUPLICATA"
            Pied_page_TextBox.Text = "DUPLICATA"
        End If


        Dim nature_assurance As String = ""
        If num_facture_SV <> 0 Then


            G1110C1_textbox.Text = ecrit_espacé(fact_SV(0).G1110C1.ToString)
            G1110C3_TextBox.Text = ecrit_date(fact_SV(0).G1110C3.ToString)

            Bénéficiaire_TextBox.Text = fact_SV(0).Bénéficiaire.ToString


            G1130C2_textbox.Text = ecrit_espacé(fact_SV(0).G1130C2.ToString)
            G1130C3_textbox.Text = ecrit_espacé(fact_SV(0).G1130C3.ToString)
            G1130C4_textbox.Text = ecrit_date(fact_SV(0).G1130C4.ToString)
            G1310C1_TextBox.Text = ecrit_espacé(fact_SV(0).G1310C1.ToString)
            G1310C2_TextBox.Text = ecrit_espacé(fact_SV(0).G1310C2.ToString)
            G1310C3_TextBox.Text = ecrit_espacé(fact_SV(0).G1310C3.ToString)
            Assuré_TextBox.Text = ecrit_espacé(fact_SV(0).Assuré.ToString)
            Dim prescripteur = Me.PrescripteursTableAdapter1.GetPrescripteurDataByIDinterne(fact_SV(0).IDPrescripteur.ToString)
            If prescripteur.Rows.Count > 0 Then
                Prescr_nom_TextBox.Text = Me.PrescripteursTableAdapter1.GetPrescripteurDataByIDinterne(fact_SV(0).IDPrescripteur.ToString).Item(0).NomPrénom()
                Prescr_id_TextBox.Text = ecrit_espacé(prescripteur.Item(0).Numidentification)
                Prescr_num_structTextBox.Text = ecrit_espacé(prescripteur.Item(0).numStructure)
            Else
                MyMsg_Box.Presentation("Officine !", "Le prescripteur pour cette facture n'a pas été trouvé." & vbCrLf & vbCrLf & "La partie prescripteur du documment sera laissée vide.", False, "", False, "", True, "OK")
                MyMsg_Box.ShowDialog()
            End If

            If fact_SV(0).G1211C3.ToString <> "" Then
                Prescr_Date_TextBox.Text = ecrit_date(fact_SV(0).G1211C3.ToString)
            ElseIf fact_SV(0).G1213C4.ToString <> "" Then
                Prescr_Date_TextBox.Text = ecrit_date(fact_SV(0).G1213C4.ToString)
            ElseIf fact_SV(0).G1214C4.ToString <> "" Then
                Prescr_Date_TextBox.Text = ecrit_date(fact_SV(0).G1214C4.ToString)
            End If
            nature_assurance = fact_SV(0).Nature_Assurance.ToString
            If fact_SV(0).G1511C1.ToString <> "" Then
                Accident_tiers_oui_CheckBox.Checked = True
                Accident_tiers_Date_TextBox.Text = ecrit_date(fact_SV(0).G1511C1.ToString)
            Else
                Accident_tiers_non_CheckBox.Checked = True
            End If
            G1513C1_TextBox.Text = ecrit_date(fact_SV(0).G1513C1.ToString)
            G1513C2_TextBox.Text = ecrit_espacé(fact_SV(0).G1513C2.ToString)
            G1513C3_TextBox.Text = ecrit_espacé(fact_SV(0).G1513C3.ToString)
            Montant_Total_TextBox.Text = ecrit_espacé((Convert.ToDouble(fact_SV(0).G1910C2) / 100).ToString)
            TP_AMO_CheckBox.Checked = fact_SV(0).TP_AMO
            TP_AMC_CheckBox.Checked = fact_SV(0).TP_AMC


            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            For r = 0 To lignes_fact.Rows.Count - 1
                If Not lignes_fact(0).IsG1840C3Null Then
                    If lignes_fact(0).G1840C3.ToString <> "" Then
                        G1840C3_TextBox.Text = ecrit_date(lignes_fact(0).G1840C3.ToString)
                    End If
                End If

                If Not lignes_fact(0).IsG1820C2Null Then
                    If Val(lignes_fact(0).G1820C2.ToString) = 4 Then
                        ALD_CheckBox.Checked = True
                    ElseIf Val(lignes_fact(0).G1820C2.ToString) = 7 Then
                        Prev_CheckBox.Checked = True
                    ElseIf Val(lignes_fact(0).G1820C2.ToString) > 0 Then
                        Autre_exo_CheckBox.Checked = True
                    End If
                End If

            Next
            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            'conditions de prise en charge
            If nature_assurance = "SMG" And G1310C1_TextBox.Text = "08" Then
                art155_CheckBox.Checked = True
            End If
            If nature_assurance = "MALADIE" And ALD_CheckBox.Checked = False And Prev_CheckBox.Checked = False And Autre_exo_CheckBox.Checked = False Then
                Maladie_CheckBox.Checked = True

            End If
            If nature_assurance = "ACCIDENT DU TRAVAIL" Then
                AT_CheckBox.Checked = True
            End If
            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            'Bénéficiaire/assuré
            If UCase(Bénéficiaire_TextBox.Text) = UCase(Assuré_TextBox.Text) Then
                Assuré_TextBox.Text = ""
            Else
                G1130C2_assuré_textbox.Text = G1130C2_textbox.Text
                G1130C2_textbox.Text = ""
                G1130C3_assuré_textbox.Text = G1130C3_textbox.Text
                G1130C3_textbox.Text = ""
            End If
            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            'identité pharmacien structure
            Dim ConfigXml As New XmlDocument
            ConfigXml.Load(Mid(Application.ExecutablePath, 1, InStrRev(Application.ExecutablePath, "\")) + "InfoPro.xml")
            Dim nodeliste As XmlNodeList
            nodeliste = ConfigXml.GetElementsByTagName("CERFA")
            Ident_Pharmacien_TextBox.Text = nodeliste.Item(0).Item("Pharmacien_ID").InnerText
            Ident_Structure_TextBox.Text = nodeliste.Item(0).Item("Structure_ID").InnerText
            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            'remplaçant
            If Config.Activer_Session_Remplacement = True Then
                rempl_id_TextBox.Text = ecrit_espacé(Config.Remplacant_Num_Id_Nat)
                rempl_clé_TextBox.Text = ecrit_espacé(Config.Remplacant_Cle_Num_Id_Nat)
            End If

        ElseIf fact_VD.Rows.Count > 0 Then

            G1110C1_textbox.Text = "VD" & ecrit_espacé(fact_VD(0).Num.ToString)
            G1110C3_TextBox.Text = ecrit_date(fact_VD(0).Date_.ToString)
            Bénéficiaire_TextBox.Text = fact_VD(0).Nom_Complet
            If CurrentBenéficiaire.Num <> 0 Then


                If CurrentBenéficiaire.G101C8 <> "" And CurrentBenéficiaire.G101C9 <> "" Then
                    G1130C2_textbox.Text = ecrit_espacé(CurrentBenéficiaire.G101C8)
                    G1130C3_textbox.Text = ecrit_espacé(CurrentBenéficiaire.G101C9)
                End If
                If CurrentBenéficiaire.G104C12 <> "" Then
                    G1130C4_textbox.Text = ecrit_date(CurrentBenéficiaire.G104C12)
                End If

                G1310C1_TextBox.Text = ecrit_espacé(CurrentBenéficiaire.G101C10.ToString)
                G1310C2_TextBox.Text = ecrit_espacé(CurrentBenéficiaire.G101C11.ToString)
                G1310C3_TextBox.Text = ecrit_espacé(CurrentBenéficiaire.G101C12.ToString)
            End If
            '''''''''''''''''''''''''''''''''''''''''''''''''''''''

            Prescr_nom_TextBox.Text = COMPTOIR_MAIN.PrescripteurMyCombobox.Text
            Prescr_id_TextBox.Text = COMPTOIR_MAIN.TextBox_Prescripteur_NumID.Text

            nature_assurance = COMPTOIR_MAIN.NatureAssurance_Button.Text
            If nature_assurance = "Choix Nature Assurance" Then
                nature_assurance = ""
            End If

            Montant_Total_TextBox.Text = ecrit_espacé(Convert.ToDouble(fact_VD(0).Total).ToString)
            TP_AMO_CheckBox.Checked = False
            TP_AMC_CheckBox.Checked = False


            'identité pharmacien structure
            Dim ConfigXml As New XmlDocument
            ConfigXml.Load(Mid(Application.ExecutablePath, 1, InStrRev(Application.ExecutablePath, "\")) + "InfoPro.xml")
            Dim nodeliste As XmlNodeList
            nodeliste = ConfigXml.GetElementsByTagName("CERFA")
            Ident_Pharmacien_TextBox.Text = nodeliste.Item(0).Item("Pharmacien_ID").InnerText
            Ident_Structure_TextBox.Text = nodeliste.Item(0).Item("Structure_ID").InnerText
            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            'remplaçant
            If Config.Activer_Session_Remplacement = True Then
                rempl_id_TextBox.Text = ecrit_espacé(Config.Remplacant_Num_Id_Nat)
                rempl_clé_TextBox.Text = ecrit_espacé(Config.Remplacant_Cle_Num_Id_Nat)
            End If

        End If

        '_______________________________________________________________________________________________________
        'PRODUITS ET PRESTATIONS
        Prestations_ListBox.Items.Clear()
        Prestations_ListBox.Items.Add("Qté|PrixUnit|T.Remb|Prest|TxRb|Cip Ean13    |LPP            |Désignation         |Ex")
        For r = 0 To seize_actes_fact.Rows.Count - 1
            Dim Quantité As String = ""
            Dim Prix_Unitaire As String = ""
            Dim Tarif_Remboursement As String = ""
            Dim Prest As String = ""
            Dim TauxRmbrsmnt As String = ""
            Dim cip_ean13 As String = ""
            Dim LPP_Type_prestation As String = ""
            Dim LPP_Ref As String = ""
            Dim LPP_Quantité As String = ""
            Dim Désignation As String = ""
            Dim NumLigne_FactSV As Integer = 0
            Dim codeExo As String = ""

            For c = 0 To seize_actes_fact.Columns.Count - 1
                Select Case seize_actes_fact.Columns.Item(c).ColumnName
                    Case "Quantité"
                        Quantité = seize_actes_fact(r).Item(c).ToString
                    Case "Prix_Unitaire"
                        Prix_Unitaire = seize_actes_fact(r).Item(c).ToString
                    Case "Tarif_Remboursement"
                        Tarif_Remboursement = seize_actes_fact(r).Item(c).ToString
                    Case "CodeActe"
                        Prest = seize_actes_fact(r).Item(c).ToString
                    Case "TauxRmbrsmnt"
                        TauxRmbrsmnt = seize_actes_fact(r).Item(c).ToString
                    Case "CIP"
                        If cip_ean13 = "" Then 'pour ne pas ecraser ean13
                            cip_ean13 = seize_actes_fact(r).Item(c).ToString
                        End If
                    Case "EAN13"
                        If seize_actes_fact(r).Item(c).ToString <> "" Then 'pour ne pas ecraser cip si ean13=""
                            cip_ean13 = seize_actes_fact(r).Item(c).ToString
                        End If
                    Case "LPP_Type_prestation"
                        LPP_Type_prestation = seize_actes_fact(r).Item(c).ToString
                    Case "LPP_Ref"
                        LPP_Ref = seize_actes_fact(r).Item(c).ToString
                    Case "LPP_Quantité"
                        LPP_Quantité = seize_actes_fact(r).Item(c).ToString
                    Case "Désignation"
                        Désignation = Mid(seize_actes_fact(r).Item(c).ToString, 1, 20)
                    Case "Ligne_Facture_SV"
                        NumLigne_FactSV = seize_actes_fact(r).Item(c)

                End Select
            Next
            'taille des string pour presentation
            While Quantité.Length < 3
                Quantité = Quantité + " "
            End While
            While Prix_Unitaire.Length < 8
                Prix_Unitaire = Prix_Unitaire + " "
            End While
            While Tarif_Remboursement.Length < 6
                Tarif_Remboursement = Tarif_Remboursement + " "
            End While
            While Prest.Length < 5
                Prest = Prest + " "
            End While
            While TauxRmbrsmnt.Length < 4
                TauxRmbrsmnt = TauxRmbrsmnt + " "
            End While
            While cip_ean13.Length < 13
                cip_ean13 = cip_ean13 + " "
            End While
            While LPP_Type_prestation.Length < 1
                LPP_Type_prestation = LPP_Type_prestation + " "
            End While
            While LPP_Ref.Length < 8
                LPP_Ref = LPP_Ref + " "
            End While
            LPP_Quantité = "(" + LPP_Quantité + ")"
            While LPP_Quantité.Length < 4
                LPP_Quantité = LPP_Quantité + " "
            End While
            While Désignation.Length < 20
                Désignation = Désignation + " "
            End While

            For r2 = 0 To lignes_fact.Rows.Count - 1
                For c2 = 0 To lignes_fact.Columns.Count - 1
                    If lignes_fact.Columns.Item(c2).ColumnName = "NumLigne" Then
                        If Val(NumLigne_FactSV) = Val(lignes_fact(r2).Item(c2)) Then
                            For c3 = 0 To lignes_fact.Columns.Count - 1
                                If lignes_fact.Columns.Item(c3).ColumnName = "G1820C2" Then
                                    codeExo = lignes_fact(r2).Item(c3).ToString
                                    Exit For
                                End If
                            Next
                        End If
                        Exit For
                    End If
                Next
            Next

            Prestations_ListBox.Items.Add(Quantité + "|" + Prix_Unitaire + "|" + Tarif_Remboursement + "|" + Prest + "|" + TauxRmbrsmnt + "|" + cip_ean13 + "|" + LPP_Type_prestation + " " + LPP_Ref + " " + LPP_Quantité + "|" + Désignation + "|" + codeExo)
        Next



    End Sub

    Public Sub écrit_CERFA_médecin()
        If duplicata = True Then
            En_tête_TextBox.Text = "DUPLICATA"
            Pied_page_TextBox.Text = "DUPLICATA"
        End If

        Bénéficiaire_TextBox.Text = fact_SV(0).Bénéficiaire.ToString
        G1130C2_textbox.Text = ecrit_espacé(fact_SV(0).G1130C2.ToString)
        G1130C3_textbox.Text = ecrit_espacé(fact_SV(0).G1130C3.ToString)
        G1130C4_textbox.Text = ecrit_date(fact_SV(0).G1130C4.ToString)
        G1310C1_TextBox.Text = ecrit_espacé(fact_SV(0).G1310C1.ToString)
        G1310C2_TextBox.Text = ecrit_espacé(fact_SV(0).G1310C2.ToString)
        G1310C3_TextBox.Text = ecrit_espacé(fact_SV(0).G1310C3.ToString)
        Assuré_TextBox.Text = ecrit_espacé(fact_SV(0).Assuré.ToString)

        Accident_tiers_oui_CheckBox.Visible = False
        Accident_tiers_non_CheckBox.Visible = False
        TP_AMO_CheckBox.Visible = False
        TP_AMC_CheckBox.Visible = False
        ALD_CheckBox.Visible = False
        Prev_CheckBox.Visible = False
        Autre_exo_CheckBox.Visible = False
        art155_CheckBox.Visible = False
        Maladie_CheckBox.Visible = False
        AT_CheckBox.Visible = False


    End Sub
    Private Function ecrit_espacé(ByVal text_in As String) As String
        Dim text_out As String = ""
        For i = 1 To text_in.Length
            text_out = text_out + Mid(text_in, i, 1) + " "
        Next
        Return text_out
    End Function

    Private Function ecrit_date(ByRef Date_Vitale_In As String) As String
        If Date_Vitale_In <> "" Then
            Return Mid(Date_Vitale_In, 7, 1) + " " + Mid(Date_Vitale_In, 8, 1) + "|" + Mid(Date_Vitale_In, 5, 1) + " " + Mid(Date_Vitale_In, 6, 1) + "|" + _
                   Mid(Date_Vitale_In, 1, 1) + " " + Mid(Date_Vitale_In, 2, 1) + " " + Mid(Date_Vitale_In, 3, 1) + " " + Mid(Date_Vitale_In, 4, 1)
        Else
            Return ""
        End If
    End Function

    Private Sub CERFA_Load(sender As Object, e As EventArgs) Handles Me.Load
        fact_SV = COMPTOIR_MAIN.FacturesTableAdapter.GetFactureDataByNum1(num_facture_SV)
        fact_VD = COMPTOIR_MAIN.Facture_VenteDirecteTableAdapter1.GetDataBy_Num(num_facture_VD)
        lignes_fact = COMPTOIR_MAIN.Facture_LigneFactureTableAdapter1.GetDataByNumFacture(num_facture_SV)
        If num_facture_SV <> 0 Then
            actes_fact = COMPTOIR_MAIN.ActesTableAdapter.GetDataByNumFacturesSV(num_facture_SV)
        Else
            actes_fact = COMPTOIR_MAIN.ActesTableAdapter.GetDataBy_NumFactureVenteDirecte(num_facture_VD)
        End If

        If debile Then
            Type_Papier_CERFA = True
            cerfa_médecin = False
        End If
        If Type_Papier_CERFA = True And cerfa_médecin = False Then
            While actes_fact.Rows.Count > 0
                If debile = False Then
                    MyMsg_Box.Presentation("Impression feuille de soins:", "Introduisez document CERFA dans l'imprimante" & vbCrLf & selected_printer, False, "", False, "", True, "IMPR")
                    MyMsg_Box.ShowDialog()
                End If

                Dim seize_actes_fact As New officine.PharmaDataSet.ActesDataTable
                If actes_fact.Rows.Count <= 16 Then
                    For r = 0 To actes_fact.Rows.Count - 1
                        seize_actes_fact.ImportRow(actes_fact(r))
                    Next
                    actes_fact.Rows.Clear()
                Else
                    For r = 0 To 15
                        seize_actes_fact.ImportRow(actes_fact(r))
                    Next
                    For r = 0 To 15
                        actes_fact.RemoveActesRow(actes_fact(0))
                    Next
                End If
                écrit_CERFA(seize_actes_fact)
                For Each ctrl In Controls
                    If (ctrl.GetType() Is GetType(CheckBox)) Then
                        If ctrl.Checked = False Then
                            ctrl.visible = False
                        End If
                    End If
                Next
                '  MsgBox("debile=" & debile.ToString & vbCrLf & "Bénéficiaire_TextBox.Text =" & Bénéficiaire_TextBox.Text)
                If debile = False Then

                    Me.BackgroundImage = Nothing
                    PrintDocument1.PrinterSettings.DefaultPageSettings.Margins.Left = 0
                    PrintDocument1.PrinterSettings.DefaultPageSettings.Margins.Right = 0
                    PrintDocument1.PrinterSettings.DefaultPageSettings.Margins.Bottom = 0
                    PrintDocument1.PrinterSettings.DefaultPageSettings.Margins.Top = 0
                    Me.BackgroundImage = Nothing
                    Me.BackColor = Color.White
                    Me.Refresh()
                    PrintDocument1.PrinterSettings.PrinterName = selected_printer
                    PrintDocument1.Print()
                Else
                    'Me.BackgroundImage = Nothing
                    Me.BackgroundImage = Global.officine.My.Resources.Resources.cerfa_nb
                    Me.BackColor = Color.White
                    'Bénéficiaire_TextBox.ForeColor = Color.Black
                    'Bénéficiaire_TextBox.BackColor = Color.Red
                    'Bénéficiaire_TextBox.Visible = True
                    ' MsgBox(Bénéficiaire_TextBox.Text)
                    Me.Visible = True
                    Me.BringToFront()
                    Me.TopMost = True
                    VenteForm.TopMost = False
                    Application.DoEvents()

                    Dim b As New Bitmap(Me.Width, Me.Height)
                    Me.DrawToBitmap(b, Me.ClientRectangle)
                    Dim scor As New ScanOrdo
                    scor.Alter_Mode_Fonctionement = "debile"
                    scor.PictureBox1.Image = b
                    scor.A10_1_ListBox.SelectedItem = "Feuille de soins papier FSNC"
                    scor.ShowDialog()



                End If

            End While

            Me.Close()

        ElseIf Type_Papier_CERFA = True And cerfa_médecin = True Then
            MyMsg_Box.Presentation("Impression feuille de soins médecin:", "Introduisez document CERFA dans l'imprimante" & vbCrLf & selected_printer, False, "", False, "", True, "IMPR")
            MyMsg_Box.ShowDialog()
            écrit_CERFA_médecin()

            Me.BackgroundImage = Nothing
            PrintDocument1.PrinterSettings.DefaultPageSettings.Margins.Left = 0
            PrintDocument1.PrinterSettings.DefaultPageSettings.Margins.Right = 0
            PrintDocument1.PrinterSettings.DefaultPageSettings.Margins.Bottom = 0
            PrintDocument1.PrinterSettings.DefaultPageSettings.Margins.Top = 0
            Me.BackgroundImage = Nothing
            Me.BackColor = Color.White
            Me.Refresh()
            PrintDocument1.PrinterSettings.PrinterName = selected_printer
            PrintDocument1.Print()
            Me.Close()
        Else
            ecrit_drawstring()
            PrintDocument1.Print()
            Me.Close()
        End If

    End Sub

    Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        If Type_Papier_CERFA = True Then

            Me.Visible = True

            Dim b As New Bitmap(Me.Width, Me.Height)
            Me.DrawToBitmap(b, Me.ClientRectangle)

            Dim destRect As New Rectangle(0, 0, 827, 1169)
            e.Graphics.DrawImage(b, destRect)

        Else
            Dim drawFont As New Font("Lucida Console", 10)
            Dim drawBrush As New SolidBrush(Color.Black)
            e.Graphics.DrawString(drawstring, drawFont, drawBrush, 10, 10)
        End If


    End Sub
End Class