Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Imports fatd_faturamento.BLL.Repositorio
Imports fatd_faturamento.ENTIDADE
Imports CrystalDecisions.CrystalReports.Engine
Friend Class frmRelReavisos
	Inherits System.Windows.Forms.Form
	'Private frmAuxiliar As FrmRelatorio
	Private istrCabecA As String
	Private istrCabecB As String
	Private istrTamCol As String
	Private istrCabecG As String
    Private istrTabela As String
    Private sNomeEmpresa As String
    Private sEnderecoEmpresa As String
    Private sCepEmpresa As String
    Private sCidadeEmpresa As String
    Private sUFempresa As String
    Private sBairroEmpresa As String
    Private sFoneEmpresa As String
    Private sWebEmpresa As String
    Private sNomeRelatorio As String
    Private sData As String
    Private Limagem As Byte()
    Private objAtualizaBancoDados As New entidadeAtualizaBancoDados
    Private repositorioAtualizaBancoDados As New repositorioAtualizaBancoDados
    Private dt As DataTable
    Private objExecAtualizaBancoDados As New entidadeAtualizaBancoDados
    Private repositorioExecAtualizaBancoDados As New repositorioAtualizaBancoDados
    Private Sub chkDesconsiderar_CheckStateChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles chkDesconsiderar.CheckStateChanged
        Try
            Me.dtpDesconsiderar.Enabled = (Me.chkDesconsiderar.CheckState = 1)
        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description, "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try
    End Sub
    Private Sub chkFaturas_CheckStateChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles chkFaturas.CheckStateChanged
        Try
            Me.dtpMesAno.Enabled = (Me.chkFaturas.CheckState = 1)
        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description, "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try
    End Sub
    Private Sub cmdBaixaReavisos_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdBaixaReavisos.Click
        Try
            frmBaixaReavisos.Show()
        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description, "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try
    End Sub
    Private Sub cmdImprimir_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdImprimir.Click
        Dim lstrAux As String
        Dim lstrSql As String
        Dim lblnVerifica As Boolean
        Try
            If Me.dtpInicial.Enabled Then
                If CDate(Me.dtpInicial.Value) > CDate(Me.dtpFinal.Value) Then
                    Call MsgBox("A data inicial não pode ser maior que a data final!", MsgBoxStyle.Information, gstrTituloMensagem)
                    Call dtpInicial.Focus()
                    Exit Sub
                End If
            End If
            Me.Cursor = System.Windows.Forms.Cursors.WaitCursor

            If Me.opbSemReaviso.Checked Then
                istrCabecA = "Listagem das Faturas "
            Else
                istrCabecA = "Listagem dos Reavisos com a situação "
            End If

            If Me.opbReaviso.Checked Then
                lstrSql = ""
                lstrSql = lstrSql & " WHERE `concorte`.`sit` = ""R"" "
                lstrSql = lstrSql & "   AND `concorte`.`datareav` <= """ & VB6.Format(Me.dtpFinal.Value, "yyyy-MM-dd") & """ "
                istrCabecB = Me.opbReaviso.Text & " até " & VB6.Format(Me.dtpFinal.Value, "dd/mm/yyyy")
                lstrAux = "REAVISO"
            ElseIf Me.opbEntregue.Checked Then
                lstrSql = ""
                lstrSql = lstrSql & " WHERE `concorte`.`sit` = ""E"" "
                lstrSql = lstrSql & "   AND `concorte`.`dataentrega` BETWEEN """ & VB6.Format(Me.dtpInicial.Value, "yyyy-MM-dd") & """ "
                lstrSql = lstrSql & "                                    AND """ & VB6.Format(Me.dtpFinal.Value, "yyyy-MM-dd") & """ "
                istrCabecB = Me.opbEntregue.Text & " no período de " & VB6.Format(Me.dtpInicial.Value, "dd/mm/yyyy") & " até " & VB6.Format(Me.dtpFinal.Value, "dd/mm/yyyy")
                lstrAux = "ENTREGA"
            ElseIf Me.opbCortado.Checked Then
                lstrSql = ""
                lstrSql = lstrSql & " WHERE 1 =1 " '`concorte`.`sit` = ""C"" "
                lstrSql = lstrSql & "   AND `concorte`.`datacorte` BETWEEN """ & VB6.Format(Me.dtpInicial.Value & " 00:00:01", "yyyy-MM-dd hh:mm:ss") & """ "
                lstrSql = lstrSql & "                                  AND """ & VB6.Format(Me.dtpFinal.Value & " 23:59:59", "yyyy-MM-dd  hh:mm:ss") & """ "
                istrCabecB = Me.opbCortado.Text & " no período de " & VB6.Format(Me.dtpInicial.Value, "dd/mm/yyyy") & " até " & VB6.Format(Me.dtpFinal.Value, "dd/mm/yyyy")
                lstrAux = "CORTE"
            ElseIf Me.opbPrazo.Checked Then
                lstrSql = ""
                lstrSql = lstrSql & " WHERE `concorte`.`sit` = ""E"" "
                lstrSql = lstrSql & "   AND `concorte`.`prazoreav` <= """ & VB6.Format(Me.dtpFinal.Value, "yyyy-MM-dd") & """ "
                istrCabecB = Me.opbPrazo.Text & " até " & VB6.Format(Me.dtpFinal.Value, "dd/mm/yyyy")
                lstrAux = "PRAZO"
            ElseIf Me.opbSemReaviso.Checked Then
                lstrSql = lstrSql & " WHERE `nf`.`flgst`    = """" "
                lstrSql = lstrSql & "   AND `nf`.`datavenc` <= """ & VB6.Format(Me.dtpFinal.Value, "yyyy-MM-dd") & """ "
                If Me.chkDesconsiderar.CheckState = 1 Then
                    lstrSql = lstrSql & " AND `nf`.`mesano` <> '" & VB6.Format(Me.dtpDesconsiderar.Value, "mm/yyyy") & "' "
                End If
                lstrSql = lstrSql & "   AND NOT EXISTS(SELECT * FROM `concorte` WHERE `concorte`.`numnf` = `nf`.`numnf` AND `concorte`.`mesano` = `nf`.`mesano`) "
                istrCabecB = Me.opbSemReaviso.Text & " com Vencimento até " & VB6.Format(Me.dtpFinal.Value, "dd/mm/yyyy")
                lstrAux = "SITUAÇÃO"
            End If

            If Me.chkCortados.CheckState = 1 Or Me.chkLigados.CheckState = 1 Or Me.chkDesligados.CheckState = 1 Or Me.chkDesativados.CheckState = 1 Then
                lstrSql = lstrSql & " AND IFNULL(IFNULL(`uc_faturamento`.`situacao`,`uc_faturamento`.`situacao`),'L') IN("
                If Me.chkCortados.CheckState = 1 Then
                    lstrSql = lstrSql & "'C',"
                End If
                If Me.chkLigados.CheckState = 1 Then
                    lstrSql = lstrSql & "'L',"
                End If
                If Me.chkDesligados.CheckState = 1 Then
                    lstrSql = lstrSql & "'D',"
                End If
                If Me.chkDesativados.CheckState = 1 Then
                    lstrSql = lstrSql & "'I' "
                End If
                lstrSql = VB.Left(lstrSql, Len(lstrSql) - 1) & ")"
            End If

            lblnVerifica = ImprimeRelatorio(lstrSql)

            If Not lblnVerifica Then
                Call MsgBox("Não foi encontrado nenhum dado para imprimir, favor verifique as opções!", MsgBoxStyle.Information, gstrTituloMensagem)
            End If

            Me.lblInfo.Text = Me.lblInfo.Text & "...Concluído...."
            Me.Cursor = System.Windows.Forms.Cursors.Default
            System.Windows.Forms.Application.DoEvents()

        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description, "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try
    End Sub

    Public Function CabecaRel() As Object
        'With frmAuxiliar
        '	Call Cabecalho_Relatorio(.VS, istrCabecA, istrCabecB, "3")
        '	'Call LineFeed(.VS, 1)
        '	.VS.Font = VB6.FontChangeBold(.VS.Font, True)
        '	.VS.BrushColor = System.Drawing.Color.White
        '	.VS.Font = VB6.FontChangeSize(.VS.Font, 8)
        '	.VS.Table = istrTamCol & istrCabecG
        '	.VS.Font = VB6.FontChangeBold(.VS.Font, False)
        '	Call LineFeed(.VS, 1)
        'End With
    End Function

    Private Function ImprimeRelatorio(ByVal astrSql As String) As Boolean
        Dim lstrZLC As String
        Dim llngDCId As Integer
        Dim llngUCId As Integer
        Dim lstrRota As String
        Dim lstrNome As String
        Dim ldblNumNf As Double
        Dim lstrMesAno As String
        Dim ldteReaviso As Date
        Dim ldtePrazo As Date
        Dim ldteEntrega As Date
        Dim ldteCorte As Date
        Dim ldteVecto As Date
        Dim lstrMedidor As String
        Dim ldblValor As Double
        Dim lintZebra As Short
        Dim llngQtde As Integer
        Dim ldblTVlr As Double
        Dim lstrSituacao As String
        Dim lblnContinua As Boolean
        Dim lstrSitAux As String
        Dim lblnRetorno As Boolean
        Dim lstrSql As String

        Try
            lblnContinua = True
            lstrSitAux = ""
            lstrSql = ""
            If Me.opbSemReaviso.Checked Then
                lstrSql = lstrSql & "SELECT `nf`.`zona`, "
                lstrSql = lstrSql & "       `nf`.`livro`, "
                lstrSql = lstrSql & "       `nf`.`conta`, "
                lstrSql = lstrSql & "       `nf`.`cons_id`, "
                lstrSql = lstrSql & "       `nf`.`unid_id`, "
                'lstrSql = lstrSql & "       IFNULL(IFNULL(`unida`.`cons_id`,`unidb`.`cons_id`),`consum`.`consreg`) `cons_id_uc`, "
                lstrSql = lstrSql & "       IFNULL(IFNULL(`uc_faturamento`.`cons_id`,`uc_faturamento`.`cons_id`),`consum`.`consreg`) `cons_id_uc`, "
                lstrSql = lstrSql & "       `nf`.`rota`, "
                ' lstrSql = lstrSql & "       IFNULL(IFNULL(`unida`.`nome`,`unidb`.`nome`),`consum`.`nome`) `nome`, "
                lstrSql = lstrSql & "       IFNULL(IFNULL(`uc_faturamento`.`nome`,`uc_faturamento`.`nome`),`consum`.`nome`) `nome`, "
                lstrSql = lstrSql & "       `consum`.`nome` `nome_c`, "
                lstrSql = lstrSql & "       `nf`.`numnf`, "
                lstrSql = lstrSql & "       `nf`.`mesano`, "
                lstrSql = lstrSql & "       '1900-01-01' `datareav`, "
                lstrSql = lstrSql & "       '1900-01-01' `prazoreav`, "
                lstrSql = lstrSql & "       '1900-01-01' `dataentrega`, "
                lstrSql = lstrSql & "       '1900-01-01' `datacorte`, "
                lstrSql = lstrSql & "       `nf`.`datavenc`, "
                'lstrSql = lstrSql & "       IFNULL(IFNULL(`unida`.`eletr`,`unidb`.`num`),'') `medidor`, "
                lstrSql = lstrSql & "       IFNULL(IFNULL(`uc_faturamento`.`eletr`,`uc_faturamento`.`num`),'') `medidor`, "
                lstrSql = lstrSql & "       `nf`.`valortot` `valor`, "
                'lstrSql = lstrSql & "       IFNULL(IFNULL(`unidb`.`situacao`,`unida`.`situacao`),'L') AS `situacao_uc` "
                lstrSql = lstrSql & "       IFNULL(IFNULL(`uc_faturamento`.`situacao`,`uc_faturamento`.`situacao`),'L') AS `situacao_uc` "
                lstrSql = lstrSql & "  FROM `nf` "
                lstrSql = lstrSql & " LEFT JOIN `uc_faturamento` ON(`uc_faturamento`.`zona` = `nf`.`zona` AND `uc_faturamento`.`livro` = `nf`.`livro` AND `uc_faturamento`.`conta` = `nf`.`conta` AND `nf`.`grupo` in('A','B')) "
                'lstrSql = lstrSql & " LEFT JOIN `unida` ON(`unida`.`zona` = `nf`.`zona` AND `unida`.`livro` = `nf`.`livro` AND `unida`.`conta` = `nf`.`conta` AND `nf`.`grupo` = 'A') "
                lstrSql = lstrSql & " LEFT JOIN `consum` ON(`consum`.`consreg` = `nf`.`cons_id`) "
                lstrSql = lstrSql & astrSql
                lstrSql = lstrSql & " ORDER BY right(`nf`.`rota`,6) ASC "
                Me.lblInfo.Text = " Buscando as faturas...."
            Else
                lstrSql = lstrSql & "SELECT `concorte`.`zona`, "
                lstrSql = lstrSql & "       `concorte`.`livro`, "
                lstrSql = lstrSql & "       `concorte`.`conta`, "
                lstrSql = lstrSql & "       `concorte`.`cons_id`, "
                lstrSql = lstrSql & "       `concorte`.`unid_id`, "
                'lstrSql = lstrSql & "       IFNULL(IFNULL(`unida`.`cons_id`,`unidb`.`cons_id`),`consum`.`consreg`) `cons_id_uc`, "
                lstrSql = lstrSql & "       IFNULL(IFNULL(`uc_faturamento`.`cons_id`,`uc_faturamento`.`cons_id`),`consum`.`consreg`) `cons_id_uc`, "
                lstrSql = lstrSql & "       `concorte`.`rota`, "
                lstrSql = lstrSql & "       `concorte`.`nome`, "
                lstrSql = lstrSql & "       `consum`.`nome` `nome_c`, "
                lstrSql = lstrSql & "       `concorte`.`numnf`, "
                lstrSql = lstrSql & "       `concorte`.`mesano`, "
                lstrSql = lstrSql & "       `concorte`.`datareav`, "
                lstrSql = lstrSql & "       `concorte`.`prazoreav`, "
                lstrSql = lstrSql & "       `concorte`.`dataentrega`, "
                lstrSql = lstrSql & "       `concorte`.`datacorte`, "
                lstrSql = lstrSql & "       `concorte`.`datavenc`, "
                lstrSql = lstrSql & "       `concorte`.`medidor`, "
                lstrSql = lstrSql & "       `concorte`.`valor`, "
                'lstrSql = lstrSql & "       IFNULL(IFNULL(`unidb`.`situacao`,`unida`.`situacao`),'L') AS `situacao_uc` "
                lstrSql = lstrSql & "       IFNULL(IFNULL(`uc_faturamento`.`situacao`,`uc_faturamento`.`situacao`),'L') AS `situacao_uc` "
                lstrSql = lstrSql & "  FROM `concorte` "
                lstrSql = lstrSql & " LEFT JOIN `uc_faturamento` ON(`uc_faturamento`.`zona` = `concorte`.`zona` AND `uc_faturamento`.`livro` = `concorte`.`livro` AND `uc_faturamento`.`conta` = `concorte`.`conta` AND MID(`concorte`.`ta`,3,1) in ('A','B')) "
                'lstrSql = lstrSql & " LEFT JOIN `unida` ON(`unida`.`zona` = `concorte`.`zona` AND `unida`.`livro` = `concorte`.`livro` AND `unida`.`conta` = `concorte`.`conta` AND MID(`concorte`.`ta`,3,1) = 'A') "
                lstrSql = lstrSql & " LEFT JOIN `consum` ON(`consum`.`consreg` = `concorte`.`cons_id`) "
                lstrSql = lstrSql & astrSql
                If Me.chkFaturas.CheckState = 1 Then
                    lstrSql = lstrSql & " AND `concorte`.`mesano` = '" & VB6.Format(Me.dtpMesAno.Value, "mm/yyyy") & "' "
                End If
                If Me.chkDesconsiderar.CheckState = 1 Then
                    lstrSql = lstrSql & " AND `concorte`.`mesano` <> '" & VB6.Format(Me.dtpDesconsiderar.Value, "mm/yyyy") & "' "
                End If
                lstrSql = lstrSql & " ORDER BY right(`concorte`.`rota`,6) ASC "
                Me.lblInfo.Text = " Buscando os dados dos reavisos...."
            End If
            objAtualizaBancoDados.QUERY_NAME = lstrSql
            dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

            Call Me.lblInfo.Refresh()

            lintZebra = 1

            If dt.Rows.Count > 0 Then
                lblnRetorno = True
                llngQtde = dt.Rows.Count
                Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, llngQtde)
                ldblTVlr = 0
                For Each row As DataRow In dt.Rows
                    lstrSituacao = IIf(IsDBNull(row.Item("situacao_uc")), "L", row.Item("situacao_uc"))
                    lstrSitAux = ""
                    If Me.chkLigados.CheckState = System.Windows.Forms.CheckState.Checked Then
                        lstrSitAux = lstrSitAux & "L"
                    End If
                    If Me.chkDesligados.CheckState = System.Windows.Forms.CheckState.Checked Then
                        lstrSitAux = lstrSitAux & "D"
                    End If
                    If Me.chkCortados.CheckState = System.Windows.Forms.CheckState.Checked Then
                        lstrSitAux = lstrSitAux & "C"
                    End If
                    If Me.chkDesativados.CheckState = System.Windows.Forms.CheckState.Checked Then
                        lstrSitAux = lstrSitAux & "I"
                    End If
                    lblnContinua = (CInt(InStr(1, lstrSitAux, lstrSituacao)) > 0)
                    If lblnContinua Then

                        lstrZLC = VB6.Format(IIf(IsDBNull(row.Item("zona")), "0", row.Item("zona")), "000") & "." & VB6.Format(IIf(IsDBNull(row.Item("livro")), "0", row.Item("livro")), "00") & "." & VB6.Format(IIf(IsDBNull(row.Item("conta")), "0", row.Item("conta")), "000000")
                        lstrRota = VB6.Format(IIf(IsDBNull(row.Item("rota")), "0", row.Item("rota")), "00000000")
                        lstrRota = VB.Left(lstrRota, 4) & "." & VB.Right(lstrRota, 4)
                        llngDCId = IIf(IsDBNull(row.Item("cons_id")), 0, row.Item("cons_id"))
                        llngUCId = IIf(IsDBNull(row.Item("cons_id_uc")), 0, row.Item("cons_id_uc"))
                        lstrNome = IIf(IsDBNull(row.Item("nome")), "", row.Item("nome"))

                        If Me.opbSemReaviso.Checked Then
                            If llngDCId <> llngUCId Then
                                lstrNome = "(Trans.Tit.) " & IIf(IsDBNull(row.Item("nome_c")), "", row.Item("nome_c"))
                            End If
                        End If

                        ldblNumNf = CDbl(IIf(IsDBNull(row.Item("numnf")), 0, row.Item("numnf")))
                        lstrMesAno = IIf(IsDBNull(row.Item("mesano")), "", row.Item("mesano"))
                        ldteReaviso = CDate(IIf(IsDBNull(row.Item("datareav")), "1900-01-01", row.Item("datareav")))
                        ldtePrazo = CDate(IIf(IsDBNull(row.Item("prazoreav")), "1900-01-01", row.Item("prazoreav")))
                        ldteEntrega = CDate(IIf(IsDBNull(row.Item("dataentrega")), "1900-01-01", row.Item("dataentrega")))
                        ldteCorte = CDate(IIf(IsDBNull(row.Item("datacorte")), "1900-01-01", row.Item("datacorte")))
                        ldteVecto = CDate(IIf(IsDBNull(row.Item("datavenc")), "1900-01-01", row.Item("datavenc")))
                        lstrMedidor = IIf(IsDBNull(row.Item("medidor")), "", row.Item("medidor"))
                        ldblValor = CDbl(IIf(IsDBNull(row.Item("valor")), 0, row.Item("valor")))
                        ldblTVlr = ldblTVlr + ldblValor

                        lstrSql = ""
                        lstrSql = lstrSql & "INSERT INTO AUX_REL_REAVISO_CORTADOS SET " & vbCrLf
                        lstrSql = lstrSql & "            `ZLC`    = '" & lstrZLC & "', " & vbCrLf
                        lstrSql = lstrSql & "            `ROTA`   = '" & lstrRota & "', " & vbCrLf
                        lstrSql = lstrSql & "            `NOME`   = '" & lstrNome & "', " & vbCrLf
                        lstrSql = lstrSql & "            `FATURA` = '" & ldblNumNf & "', " & vbCrLf
                        lstrSql = lstrSql & "            `REF` = '" & lstrMesAno & "', " & vbCrLf

                        If Me.opbReaviso.Checked Then
                            lstrSql = lstrSql & "            `DATA`  = '" & VB6.Format(ldteReaviso, "dd/mm/yyyy") & "', " & vbCrLf
                            sData = "Reaviso"
                        ElseIf Me.opbEntregue.Checked Then
                            lstrSql = lstrSql & "            `DATA`  = '" & VB6.Format(ldteEntrega, "dd/mm/yyyy") & "', " & vbCrLf
                            sData = "Entrega"
                        ElseIf Me.opbCortado.Checked Then
                            lstrSql = lstrSql & "            `DATA`  = '" & VB6.Format(ldteCorte, "dd/mm/yyyy") & "', " & vbCrLf
                            sData = "Corte"
                        ElseIf Me.opbPrazo.Checked Then
                            lstrSql = lstrSql & "            `DATA`  = '" & VB6.Format(ldtePrazo, "dd/mm/yyyy") & "', " & vbCrLf
                            sData = "Prazo"
                        ElseIf Me.opbSemReaviso.Checked Then
                            lstrSql = lstrSql & "            `DATA`  = '" & lstrSituacao & "', " & vbCrLf
                            sData = "Situação"
                        End If

                        lstrSql = lstrSql & "            `VECTO`   = '" & VB6.Format(ldteVecto, "dd/mm/yyyy") & "', " & vbCrLf
                        lstrSql = lstrSql & "            `MEDIDOR`  = '" & lstrMedidor & "', " & vbCrLf
                        lstrSql = lstrSql & "            `VALOR`     = '" & ldblValor & "' "
                        objExecAtualizaBancoDados.QUERY_NAME = lstrSql
                        repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)


                    End If

                    System.Windows.Forms.Application.DoEvents()
                    Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
                Next

                ' Retorno de dados da empresa
                Call sDadosEmpresa(Limagem, sNomeEmpresa, sEnderecoEmpresa, sCepEmpresa, sCidadeEmpresa, sUFempresa, sBairroEmpresa, sFoneEmpresa, sWebEmpresa)

                Dim sNomeRelatorio As String
                sNomeRelatorio = "rptReavisoCortados"

                Call lPrint(sNomeRelatorio)

            Else
                lblnRetorno = False
            End If

            ImprimeRelatorio = lblnRetorno
        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description, "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Function
        End Try

    End Function

    'Private Sub dtpInicial_Change(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles dtpInicial.Change
    '    Me.dtpFinal.Value = CDate(UltimoDiaMes((Me.dtpInicial.Month), (Me.dtpInicial.Year)) & "/" & VB6.Format(Me.dtpInicial.Value, "mm/yyyy"))
    'End Sub

    'Private Sub dtpInicial_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles dtpInicial.ClickEvent
    '    Call dtpInicial_Change(dtpInicial, New System.EventArgs())
    'End Sub

    Private Sub frmRelReavisos_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
        Try
            Me.dtpInicial.Value = CDate(VB6.Format(Date.Now, "dd/MM/yyyy"))
            Me.dtpFinal.Value = CDate(UltimoDiaMes((Month(Me.dtpInicial.Value)), (Year(Me.dtpInicial.Value))) & "/" & VB6.Format(Me.dtpInicial.Value, "mm/yyyy"))
            Me.dtpMesAno.Value = Me.dtpInicial.Value
            Me.dtpDesconsiderar.Value = Me.dtpInicial.Value
            Call LigDesDatas()
            Call sCriaTabelaAuxiliar()
        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description, "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try
    End Sub
    Private Sub opbCortado_CheckedChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles opbCortado.CheckedChanged
        Try
            If eventSender.Checked Then
                Call LigDesDatas()
            End If
        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description, "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try
    End Sub
    Private Sub opbentregue_checkedchanged(ByVal eventsender As System.Object, ByVal eventargs As System.EventArgs) Handles opbEntregue.CheckedChanged
        Try
            If eventsender.checked Then
                Call LigDesDatas()
            End If
        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description, "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try
    End Sub
    Private Sub opbPrazo_CheckedChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles opbPrazo.CheckedChanged
        Try
            If eventSender.Checked Then
                Call LigDesDatas()
            End If
        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description, "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try
    End Sub
    Private Sub opbReaviso_CheckedChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles opbReaviso.CheckedChanged
        Try
            If eventSender.Checked Then
                Call LigDesDatas()
            End If
        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description, "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try
    End Sub
    Private Sub LigDesDatas()
        Try
            If Me.opbCortado.Checked Or Me.opbEntregue.Checked Then
                Me.dtpInicial.Enabled = True
            Else
                Me.dtpInicial.Enabled = False
            End If
            If Me.opbSemReaviso.Checked Then
                Me.lblAux(2).Text = "Vecto:"
            Else
                Me.lblAux(2).Text = "Final:"
            End If
        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description, "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try
    End Sub
    Private Sub cmdFechar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdFechar.Click
        Call Me.Close()
    End Sub
    Private Sub opbSemReaviso_CheckedChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles opbSemReaviso.CheckedChanged
        Try
            If eventSender.Checked Then
                Call LigDesDatas()
            End If
        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description, "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
            End Try
    End Sub

    Private Sub dtpInicial_ValueChanged(sender As Object, e As EventArgs) Handles dtpInicial.ValueChanged
        Try
            Me.dtpFinal.Value = CDate(UltimoDiaMes((Month(Me.dtpInicial.Value)), Year(Me.dtpInicial.Value)) & "/" & VB6.Format(Me.dtpInicial.Value, "mm/yyyy"))
        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description, "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try
    End Sub
    Private Sub lPrint(ByVal vNomeRPT As String)

        Dim dt As DataTable
        Dim dt_2 As DataTable
        Dim ds As DataSet
        Dim lstrsql As String

        Try
            Dim rpt As New ReportDocument()

            If vNomeRPT = "rptReavisoCortados" Then

                ' CABEÇALHO
                dt_2 = New DataTable()
                dt_2.Columns.Add("ZLC", GetType(String))
                dt_2.Columns.Add("ROTA", GetType(String))
                dt_2.Columns.Add("NOME", GetType(String))
                dt_2.Columns.Add("FATURA", GetType(String))
                dt_2.Columns.Add("REF", GetType(String))
                dt_2.Columns.Add("DATA", GetType(String))
                dt_2.Columns.Add("VECTO", GetType(String))
                dt_2.Columns.Add("MEDIDOR", GetType(String))
                dt_2.Columns.Add("VALOR", GetType(String))
                dt_2.Columns.Add("IMAGEM", GetType(Byte()))


                ds = New DataSet

                lstrsql = ""
                lstrsql = "SELECT * FROM AUX_REL_REAVISO_CORTADOS"
                objAtualizaBancoDados.QUERY_NAME = lstrsql
                dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

                If dt.Rows.Count > 0 Then
                    For Each dr_1 As DataRow In dt.Rows

                        dt_2.Rows.Add(dr_1("ZLC"),
                                          dr_1("ROTA"),
                                          dr_1("NOME"),
                                          dr_1("FATURA"),
                                          dr_1("REF"),
                                          dr_1("DATA"),
                                          dr_1("VECTO"),
                                          dr_1("MEDIDOR"),
                                          dr_1("VALOR"),
                                          Limagem)
                    Next dr_1
                End If


                ds.Tables.Add(dt)
                ds.Tables(0).TableName = "REL_REAVISO_CORTADOS"

                If ds.Tables(0).Rows.Count = 0 Then
                    MessageBox.Show("Não há registros para o relatório escolhido! ", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Exit Sub
                Else

                    rpt = New rptReavisoCortados

                    rpt.SetDataSource(ds)
                    rpt.SetParameterValue("nomeempresa", sNomeEmpresa)
                    rpt.SetParameterValue("enderecoempresa", sEnderecoEmpresa)
                    rpt.SetParameterValue("cepempresa", sCepEmpresa)
                    rpt.SetParameterValue("ufempresa", sUFempresa)
                    rpt.SetParameterValue("cidadeempresa", sCidadeEmpresa)
                    rpt.SetParameterValue("bairroempresa", sBairroEmpresa)
                    rpt.SetParameterValue("foneempresa", sFoneEmpresa)
                    rpt.SetParameterValue("websiteempresa", sWebEmpresa)
                    rpt.SetParameterValue("sData", sData)

                    frmViewRelatorios.rpt = rpt

                    frmViewRelatorios.CRViewer.Zoom((100))
                    frmViewRelatorios.Text = "Relatório Reaviso Cortados"
                    frmViewRelatorios.ShowDialog()

                    dt = Nothing

                End If

            End If
        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description, "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try
    End Sub

    Private Sub sCriaTabelaAuxiliar()
        Dim lstrsql As String

        Try
            lstrsql = ""
            lstrsql = "DROP TABLE IF EXISTS `AUX_REL_REAVISO_CORTADOS`"
            objExecAtualizaBancoDados.QUERY_NAME = lstrsql
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

            lstrsql = ""
            lstrsql = lstrsql & "CREATE TABLE IF NOT EXISTS `AUX_REL_REAVISO_CORTADOS` (" & vbCrLf
            lstrsql = lstrsql & "  `ZLC`           varchar(20) DEFAULT NULL, " & vbCrLf
            lstrsql = lstrsql & "  `ROTA`          varchar(20) DEFAULT NULL, " & vbCrLf
            lstrsql = lstrsql & "  `NOME`          varchar(100) DEFAULT NULL,  " & vbCrLf
            lstrsql = lstrsql & "  `FATURA`        varchar(10) DEFAULT NULL,  " & vbCrLf
            lstrsql = lstrsql & "  `REF`           varchar(10) DEFAULT NULL, " & vbCrLf
            lstrsql = lstrsql & "  `DATA`         varchar(10) DEFAULT NULL, " & vbCrLf
            lstrsql = lstrsql & "  `VECTO`         varchar(10) DEFAULT NULL, " & vbCrLf
            lstrsql = lstrsql & "  `MEDIDOR`       varchar(20) DEFAULT NULL, " & vbCrLf
            lstrsql = lstrsql & "  `VALOR`          varchar(20) DEFAULT NULL) "
            objExecAtualizaBancoDados.QUERY_NAME = lstrsql
            repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

        Catch ex As Exception
            MessageBox.Show("Ocorreu o Erro: " & Err.Description, "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try
    End Sub
End Class