﻿Imports DevExpress.Web
Imports DevExpress.Web.Data
Imports System
Imports System.Data
Imports System.Web.UI
Imports System.Data.SqlClient

Public Class Societa
    Inherits System.Web.UI.Page
    Dim tmpSQL As String
    Dim connection

    Private copiedValues As Hashtable = Nothing
    Private copiedFields() As String = {"Descrizione", "Indirizzo", "Citta", "Cap", "IDProvincia", "IDNazione", "Telefono", "Telefono2", "Fax", "Email"}

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        '********************************************************
        '--- Controllo Autenticazione per accesso alla pagina ---
        If Not Session("Autenticato") Then
            Response.Redirect("LoginUser.aspx")
        End If
        '********************************************************

        connection = New SqlConnection(ConfigurationManager.ConnectionStrings("pa_data_userConnectionString").ConnectionString)

        '--- imposta la connessione al database sui datasource
        dsSocieta.ConnectionString = Session("sConnectionString")

        dsNazioni.ConnectionString = Session("sConnectionString")
        dsProvincie.ConnectionString = Session("sConnectionString")
        dsPagamenti.ConnectionString = Session("sConnectionString")
        dsProvincie.ConnectionString = Session("sConnectionString")

        '-- La tabella societa deve contenere solo un record, quindi controlliamo ce se il record con la select
        '-- Se il record non esiste viene creato vuoto, se invece esiste si prosegue perche la form apre solo in edit.
        Try
            connection.Open()
            tmpSQL = "SELECT * FROM societa"
            Dim SqlCommand = New SqlCommand(tmpSQL, connection)
            Dim dr = SqlCommand.ExecuteReader()

            If dr.HasRows Then
                '--- esiste già il record società
                dr.Read()
            Else
                '--- il record società non esiste allora viene creato automaticamente
                connection.Close()
                Try
                    Dim g_ins_dataora = "'" + Date.Now.ToString("yyyy/MM/dd HH:mm:ss").Replace(".", ":") + "'"
                    Dim g_ins_utente = "'" + Session("sUtente_UserName") + "'"
                    Dim g_ins_societa = "'" + "Ragione sociale societa" + "'"
                    connection.Open()

                    tmpSQL = "Insert into societa (RagioneSociale, ins_dataora, ins_utente)"
                    tmpSQL = tmpSQL & " VALUES ("
                    tmpSQL = tmpSQL & g_ins_societa + ","
                    tmpSQL = tmpSQL & g_ins_dataora + "," + g_ins_utente + ")"

                    '--- esegue il comando ---
                    Dim SqlCommand1 = New SqlCommand(tmpSQL, connection)
                    Dim numeroRecords = SqlCommand1.ExecuteNonQuery
                Catch ex As Exception
                    Response.Write("Errore Insert: " & ex.Message)
                End Try
            End If

            connection.Close()
        Catch ex As Exception
            Response.Write("Errore CL1: " & ex.Message)
            connection.Close()
        End Try
        connection.Close()


    End Sub

    Private Sub ASPxGridView1_RowInserting(sender As Object, e As ASPxDataInsertingEventArgs) Handles ASPxGridView1.RowInserting

        '----------------------------------------------------------------
        '--- aggiorna i dati in fase di inserimento di una nuova riga ---
        '----------------------------------------------------------------
        Dim xDateTime = DateTime.Now
        e.NewValues("Ins_DataOra") = xDateTime
        e.NewValues("Upd_DataOra") = xDateTime
        e.NewValues("Ins_Utente") = Session("sUtente_UserName")       '--- mettere la variabile di sessione
        e.NewValues("Upd_Utente") = Session("sUtente_UserName")        '--- mettere la variabile di sessione
        e.NewValues("LivelloAutorizzazione") = Session("sUtente_Livello")    '--- mettere la variabile di sessione

    End Sub

    Private Sub ASPxGridView1_RowUpdating(sender As Object, e As ASPxDataUpdatingEventArgs) Handles ASPxGridView1.RowUpdating

        '----------------------------------------------------------------
        '--- aggiorna i dati in fase di inserimento di una nuova riga ---
        '----------------------------------------------------------------
        Dim xDateTime = DateTime.Now
        e.NewValues("Upd_DataOra") = xDateTime
        e.NewValues("Upd_Utente") = Session("sUtente_UserName")        '--- mettere la variabile di sessione
    End Sub

    Protected Sub grid_CustomButtonCallback(ByVal sender As Object, ByVal e As ASPxGridViewCustomButtonCallbackEventArgs)
        If e.ButtonID <> "Clone" Then
            Return
        End If
        copiedValues = New Hashtable()
        For Each fieldName As String In copiedFields
            copiedValues(fieldName) = ASPxGridView1.GetRowValues(e.VisibleIndex, fieldName)
        Next fieldName
        ASPxGridView1.AddNewRow()

    End Sub


    Protected Sub grid_InitNewRow(ByVal sender As Object, ByVal e As DevExpress.Web.Data.ASPxDataInitNewRowEventArgs)
        If copiedValues Is Nothing Then
            Return
        End If
        For Each fieldName As String In copiedFields
            e.NewValues(fieldName) = copiedValues(fieldName)
        Next fieldName
    End Sub

End Class