' 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 Gestion_Caisse

    Private drawString As String

    Private Sub Gestion_Caisse_Load(sender As Object, e As EventArgs) Handles Me.Load
        Encaissements_type_ComboBox.Items.Clear()
        Encaissements_type_ComboBox.Items.Add("")
        Dim types = BDD.Type_PaiementTableAdapter.GetData
        For r = 0 To types.Rows.Count - 1
            Encaissements_type_ComboBox.Items.Add(types(r).Type)
        Next
        Dim parameters As String = "&Début=" & Format(Now, "yyyy-MM-dd") & "T00:00:00" & "&Fin=" & Format(Now, "yyyy-MM-dd") & "T23:59:59"
        Synthèse_WebBrowser.Navigate("http://" & Replace(Config.LocalNetwork_ServerName, "\", "") & "/ReportServer_SQLEXPRESS/Pages/ReportViewer.aspx?%2fAdministratif%2fSynth%c3%a8se+comptable&rs:Command=Render" & parameters)
        DataGridView1.DataSource = BDD.CaisseTableAdapter.GetDataBy_Top(30)
    End Sub


    Private Sub afficher_encaissements_LinkLabel1_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles afficher_encaissements_LinkLabel1.LinkClicked
        Dim encaissements = BDD.Encaissements_BorderauTableAdapter.Borderau(Encaissements_type_ComboBox.Text, _
                                                               New DateTime(DateTimePicker1.Value.Year, DateTimePicker1.Value.Month, DateTimePicker1.Value.Day, 0, 0, 0, 0), _
                                                               New DateTime(DateTimePicker2.Value.Year, DateTimePicker2.Value.Month, DateTimePicker2.Value.Day, 23, 59, 59, 999))
        encaissements_DataGridView.Rows.Clear()
        For r = 0 To encaissements.Rows.Count - 1
            encaissements_DataGridView.Rows.Add()
        Next
        For r = 0 To encaissements.Rows.Count - 1
            encaissements_DataGridView.Item(encaissements_montant.Index, r).Value = encaissements(r).Montant
            encaissements_DataGridView.Item(encaissements_Date.Index, r).Value = encaissements(r).Encaissement_sur_Crédit_Date
            encaissements_DataGridView.Item(encaissements_patient.Index, r).Value = encaissements(r).Nom_Complet
            encaissements_DataGridView.Item(encaissements_Edition_borderau.Index, r).Value = vbTrue
        Next
    End Sub

    Private Sub borderau_LinkLabel_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles borderau_LinkLabel.LinkClicked
        drawString = "Bordereau " & Encaissements_type_ComboBox.Text & Chr(10) & Chr(10)
        Dim nombre As Integer = 0
        Dim montant As Decimal = 0
        For r = 0 To encaissements_DataGridView.Rows.Count - 1
            If encaissements_DataGridView.Item(encaissements_Edition_borderau.Index, r).Value = vbTrue Then
                drawString = drawString + CDate(encaissements_DataGridView.Item(encaissements_Date.Index, r).Value).ToShortDateString + " " + encaissements_DataGridView.Item(encaissements_patient.Index, r).Value.ToString + " " _
                    + encaissements_DataGridView.Item(encaissements_montant.Index, r).Value.ToString + Chr(10)
                nombre = nombre + 1
                montant = montant + CDec(encaissements_DataGridView.Item(encaissements_montant.Index, r).Value)
            End If
        Next
        drawString = drawString + Chr(10) + "Nombre" + Encaissements_type_ComboBox.Text + ": " + nombre.ToString
        drawString = drawString + Chr(10) + "montant total: " + montant.ToString
        PrintDialog1.Document = PrintDocument1
        If PrintDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            PrintDocument1.Print()
        End If
    End Sub
    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage


        Dim drawFont As New Font("Courier New", 10)
        Dim drawBrush As New SolidBrush(Color.Black)
        e.Graphics.DrawString(drawString, drawFont, drawBrush, 50, 50)


    End Sub





    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If Trim(IntituléTextBox.Text) = "" Then
            MyMsg_Box.Presentation("Caisse", "Intitulé obligatoire", False, "", False, "", True, "Ok")
            MyMsg_Box.ShowDialog()
            IntituléTextBox.Focus()
            Exit Sub
        End If
        Try
            BDD.CaisseTableAdapter.Insert(BDD.CaisseTableAdapter.SelectMaxNum + 1, DateDateTimePicker.Value, current_User.ID, CDec(MontantTextBox.Text), IntituléTextBox.Text, CDec(Fond_caisseTextBox.Text))
            BDD.EncaissementsTableAdapter.Insert(BDD.EncaissementsTableAdapter.Select_Max_Num + 1, "Espèces", CDec(MontantTextBox.Text), 0, 0, Now, Now, "", "Mouvement manuel", 0, False, 0, IntituléTextBox.Text, False, 0)
            TextBox1_Validated(Nothing, Nothing)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
        If e.KeyCode = Keys.Enter Then
            TextBox1_Validated(Nothing, Nothing)
        End If
    End Sub



    Private Sub TextBox1_Validated(sender As Object, e As EventArgs) Handles TextBox1.Validated
        Try
            DataGridView1.DataSource = BDD.CaisseTableAdapter.GetDataBy_Top(CInt(TextBox1.Text))
        Catch ex As Exception

        End Try
    End Sub



    Private Sub MontantTextBox_Validated(sender As Object, e As EventArgs) Handles MontantTextBox.Validated
        Try
            Fond_caisseTextBox.Text = BDD.CaisseTableAdapter.GetDataBy_Num(BDD.CaisseTableAdapter.SelectMaxNum).Item(0).Fond_caisse.ToString + CDec(MontantTextBox.Text)
        Catch ex As Exception
            Fond_caisseTextBox.Text = "0"
        End Try
    End Sub

 


    Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
        If RadioButton1.Checked Then
            MontantTextBox.Enabled = True
            Fond_caisseTextBox.Enabled = False
            MontantTextBox.Text = 0
            Fond_caisseTextBox.Text = 0
            IntituléTextBox.Text = ""
        ElseIf RadioButton2.Checked Then
            Fond_caisseTextBox.Enabled = True
            MontantTextBox.Enabled = False
            MontantTextBox.Text = 0
            Fond_caisseTextBox.Text = 0
            IntituléTextBox.Text = "point caisse"
        End If
    End Sub

    Private Sub Encaissements_TabControl_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Encaissements_TabControl.SelectedIndexChanged
        If Encaissements_TabControl.SelectedIndex = Encaissements_TabControl.TabPages.IndexOf(TabPage_EncUser) Then
            EncUserWebBrowser.Navigate("http://" & Replace(Config.LocalNetwork_ServerName, "\", "") & "/ReportServer_SQLEXPRESS/Pages/ReportViewer.aspx?%2fAdministratif%2fEncaissements_User&rs:Command=Render")
        End If
    End Sub
End Class