﻿Imports Microsoft.VisualBasic
Imports System
Imports System.Data
Imports System.Collections.Generic
Imports System.Web.UI
Imports System.Linq
Imports DevExpress.Web
Imports DevExpress.Web.Data

Public Class ClientiPrezziNetti
    Inherits System.Web.UI.Page

    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
        '********************************************************

        '--- imposta la connessione al database sui datasource
        dsAziende.ConnectionString = Session("sConnectionString")
        dsClienti.ConnectionString = Session("sConnectionString")
        'dsClientiAziende.ConnectionString = Session("sConnectionString")

        '--- considera il filtro sull'IDCliente, se è stato passato...
        If Val(Session("sIDCliente")) > 0 Then
            FiltraDataSource_ClientiPrezziNetti(Session("sIDCliente"))
        End If


        'AggiornavstClientiAziende()
        If (lstClienti.SelectedIndex < 1) Then
            Dim sqlCmd = "SELECT * from clienti order by RagioneSociale"
            dsClienti.SelectCommand = sqlCmd
            dsClienti.DataBind()
            Try
                lstClienti.DataBind()
                lstClienti.Items.Insert(0, "Nessun Cliente Selezionato")
                lstClientiAziende.Items.Clear()
                lstClientiAziende.Items.Insert(0, "Nessuna Azienda Selezionata")
                lstClientiAziende.Enabled = False
            Catch
            End Try
        End If

        AggiornadsClientiPrezziNetti()

    End Sub
    Sub AggiornavstClientiAziende()
        Dim sqlCmd

        lstClientiAziende.Enabled = True
        lstClientiAziende.Items.Clear()
        If (lstClienti.SelectedIndex > 0) Then

            sqlCmd = "SELECT * from vstclientiAziende where idcliente =" & lstClienti.SelectedValue.ToString & " order by Azienda"
            vstClientiAziende.SelectCommand = sqlCmd
            vstClientiAziende.DataBind()
            lstClientiAziende.DataBind()
            lstClientiAziende.Items.Insert(0, "Nessuna Azienda Selezionata")
        Else
            lstClientiAziende.Items.Insert(0, "Nessuna Azienda Selezionata")
        End If



    End Sub
    Sub FiltraDataSource_ClientiPrezziNetti(pIDCliente)

        ''--- filtra i dati in base al parametro, SE IL PARAMETRO > 0 ---
        'Dim sqlCmd = "SELECT * from ClientiAziende"

        'If Val(pIDCliente) > 0 Then
        '    sqlCmd += " WHERE IDCliente = " & pIDCliente
        'End If

        ''dsClientiAziende.SelectCommand = sqlCmd
        ''dsClientiAziende.DataBind()
        'ASPxGridView1.DataBind()

        ''--- Aggiorna il Titolo...
        'If Val(pIDCliente) > 0 Then
        '    titoloLabel.Text = "Scheda Aziende del Cliente " & Session("sDescrizioneCliente").ToString.Trim
        'Else
        '    titoloLabel.Text = "Scheda Generale Clienti-Aziende"
        'End If

    End Sub

    Private Sub ASPxGridView1_InitNewRow(sender As Object, e As ASPxDataInitNewRowEventArgs) Handles ASPxGridView1.InitNewRow

        '--- se è stato passato l'IDCliente da un'altra pagina, propone quello in inserimento nuova riga
        'If Val(Session("sIDCliente")) > 0 Then
        '    Dim xIDCliente As Integer = Val(Session("sIDCliente"))
        '    e.NewValues("IDCliente") = xIDCliente
        'End If

    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
        e.NewValues("IDCliente") = lstClienti.SelectedValue
        e.NewValues("IDAzienda") = lstClientiAziende.SelectedValue
    End Sub

    Private Sub ASPxGridView1_RowUpdating(sender As Object, e As ASPxDataUpdatingEventArgs) Handles ASPxGridView1.RowUpdating

        '-------------------------------------------------------
        '--- aggiorna i dati in fase di modifica di una riga ---
        '-------------------------------------------------------
        Dim xDateTime = DateTime.Now
        e.NewValues("Upd_DataOra") = xDateTime
        e.NewValues("Upd_Utente") = Session("sUtente_UserName")        '--- mettere la variabile di sessione
        e.NewValues("LivelloAutorizzazione") = Session("sUtente_Livello")    '--- mettere la variabile di sessione
        e.NewValues("DataAgg") = xDateTime

    End Sub

    'Protected Sub RefreshButton_Click(sender As Object, e As EventArgs) Handles RefreshButton.Click

    '    '--- azzera la variabile di sessione per non filtrare più i dati
    '    Session("sIDCliente") = ""
    '    Session("sDescrizioneCliente") = ""
    '    FiltraDataSource_ClientiPrezziNetti("")

    'End Sub

    Private Sub ASPxGridView1_SelectionChanged(sender As Object, e As EventArgs) Handles ASPxGridView1.SelectionChanged

    End Sub

    Protected Sub ClientiAziende_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstClientiAziende.SelectedIndexChanged
    End Sub

    Private Sub ClientiAziende_Load(sender As Object, e As EventArgs) Handles lstClientiAziende.Load

    End Sub

    Protected Sub ClientiAziendeButton_Click(sender As Object, e As EventArgs) Handles ClientiAziendeButton.Click

        AggiornadsClientiPrezziNetti()
        AggiornadsProdotti()


    End Sub
    Sub AggiornadsProdotti()
        Dim sqlCmd
        If (lstClientiAziende.SelectedIndex > 0) And (lstClienti.SelectedIndex > 0) Then
            sqlCmd = "SELECT * from Prodotti where idAzienda =" & lstClientiAziende.SelectedValue.ToString & " order by descrizione"
            dsProdotti.SelectCommand = sqlCmd
            dsProdotti.DataBind()
        Else
            sqlCmd = "SELECT * from Prodotti where idAzienda = 9999999 order by descrizione"
            dsProdotti.SelectCommand = sqlCmd
            dsProdotti.DataBind()

        End If

    End Sub
    Sub AggiornadsClientiPrezziNetti()
        Dim sqlCmd
        If (lstClientiAziende.SelectedIndex > 0) And (lstClienti.SelectedIndex > 0) Then
            sqlCmd = "SELECT * from clientiPrezziNetti where idAzienda = " & lstClientiAziende.SelectedValue.ToString & " and idCliente = " & lstClienti.SelectedValue.ToString & " order by DataAgg"
            dsClientiPrezziNetti.SelectCommand = sqlCmd
            dsClientiPrezziNetti.DataBind()
            ASPxGridView1.DataBind()
        Else
            sqlCmd = "SELECT * from clientiPrezziNetti where idclientiPrezziNetti = 9999999 order by DataAgg"
            dsClientiPrezziNetti.SelectCommand = sqlCmd
            dsClientiPrezziNetti.DataBind()
            ASPxGridView1.DataBind()
        End If

    End Sub

    Private Sub lstClienti_Load(sender As Object, e As EventArgs) Handles lstClienti.Load
        'lstClienti.Items.Insert(0, "Nessun Cliente Selezionato")
    End Sub

    Private Sub lstClienti_DataBinding(sender As Object, e As EventArgs) Handles lstClienti.DataBinding
        'lstClienti.Items.Insert(0, "Nessun Cliente Selezionato")
    End Sub

    Private Sub lstClienti_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstClienti.SelectedIndexChanged
        AggiornavstClientiAziende()
    End Sub

    Private Sub dsClientiPrezziNetti_Load(sender As Object, e As EventArgs) Handles dsClientiPrezziNetti.Load

    End Sub
End Class