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 frmRelIluminacao
	Inherits System.Windows.Forms.Form
	'Private ifrmRelCIP As FrmRelatorio
	Private istrCabec(2) As String
	Private dt, dt1, dt2, dt3, dt4 As DataTable
	Private objAtualizaBancoDados As New entidadeAtualizaBancoDados
	Private repositorioAtualizaBancoDados As New repositorioAtualizaBancoDados

	Private objExecAtualizaBancoDados As New entidadeAtualizaBancoDados
	Private repositorioExecAtualizaBancoDados As New repositorioAtualizaBancoDados
	Private LImagem() As Byte
	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 bValidacao As Boolean
	Private dtBase As Date
	Private sCabec As String
	Private sCabec2 As String

	'UPGRADE_WARNING: Event chkAberto.CheckStateChanged may fire when form is initialized. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="88B12AE1-6DE0-48A0-86F1-60C0686C026A"'
	Private Sub chkAberto_CheckStateChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles chkAberto.CheckStateChanged
		Try
			If Me.chkAberto.CheckState = 0 Then
				If Me.chkFaturado.CheckState = 0 And Me.chkArrecadado.CheckState = 0 Then
					Me.chkAberto.CheckState = System.Windows.Forms.CheckState.Checked
				End If
			End If
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: chkAberto_CheckStateChanged" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	'UPGRADE_WARNING: Event chkArrecadado.CheckStateChanged may fire when form is initialized. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="88B12AE1-6DE0-48A0-86F1-60C0686C026A"'
	Private Sub chkArrecadado_CheckStateChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles chkArrecadado.CheckStateChanged
		Try
			If Me.chkArrecadado.CheckState = 0 Then
				If Me.chkAberto.CheckState = 0 And Me.chkFaturado.CheckState = 0 Then
					Me.chkArrecadado.CheckState = System.Windows.Forms.CheckState.Checked
				End If
			End If
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: chkArrecadado_CheckStateChanged" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	'UPGRADE_WARNING: Event chkFaturado.CheckStateChanged may fire when form is initialized. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="88B12AE1-6DE0-48A0-86F1-60C0686C026A"'
	Private Sub chkFaturado_CheckStateChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles chkFaturado.CheckStateChanged
		Try
			If Me.chkFaturado.CheckState = 0 Then
				If Me.chkAberto.CheckState = 0 And Me.chkArrecadado.CheckState = 0 Then
					Me.chkFaturado.CheckState = System.Windows.Forms.CheckState.Checked
				End If
			End If
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: chkFaturado_CheckStateChanged" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	'UPGRADE_WARNING: Event chkReserva.CheckStateChanged may fire when form is initialized. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="88B12AE1-6DE0-48A0-86F1-60C0686C026A"'
	Private Sub chkReserva_CheckStateChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles chkReserva.CheckStateChanged
		Try
			If Me.chkReserva.CheckState = System.Windows.Forms.CheckState.Checked Then
				Me.chkArrecadado.CheckState = System.Windows.Forms.CheckState.Checked
				Me.chkArrecadado.Enabled = False
				Me.chkAberto.CheckState = System.Windows.Forms.CheckState.Unchecked
				Me.chkAberto.Enabled = False
				Me.chkFaturado.CheckState = System.Windows.Forms.CheckState.Unchecked
				Me.chkFaturado.Enabled = False
			Else
				Me.chkArrecadado.Enabled = True
				Me.chkAberto.Enabled = True
				Me.chkFaturado.Enabled = True
			End If
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: chkReserva_CheckStateChanged" & "", "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 lstrSql As String

		Try
			Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
			Call LigDes(False)

			Me.mfgDados.Rows = 1
			Me.lblInfo.Text = " Aguarde.... "
			Call Me.lblInfo.Refresh()
			If Me.chkArrecadado.CheckState = 1 Then
				'
				'--- Arrecadado no período Informado
				lstrSql = ""
				lstrSql = lstrSql & "   AND `nf`.`datapag` BETWEEN '" & VB6.Format(Me.dtpInicial.Value, "yyyy-MM-dd") & "' AND '" & VB6.Format(Me.dtpFinal.Value, "yyyy-MM-dd") & "' "
				lstrSql = lstrSql & "   AND `nf`.`datapag` <> '0000-00-00' "

				If chkSomenteMiicrogeradoras.Checked = True Then
					lstrSql = lstrSql & " AND (nf.MICRO_GERACAO = 1"
					lstrSql = lstrSql & " OR nf.MICRO_GERACAO_DISTRIBUIDA = 1)"
					'Else
					'	lstrSql = lstrSql & " AND nf.MICRO_GERACAO = 0"
					'	lstrSql = lstrSql & " AND nf.MICRO_GERACAO_DISTRIBUIDA = 0"
				End If

				If Me.chkReserva.CheckState = System.Windows.Forms.CheckState.Unchecked Then
					lstrSql = lstrSql & "   AND `nf`.`flgst` = 'P' "
					istrCabec(0) = "Resumo de custeio de iluminação pública arrecadado no período de " & VB6.Format(Me.dtpInicial.Value, "dd/mm/yyyy") & " até " & VB6.Format(Me.dtpFinal.Value, "dd/mm/yyyy")
				Else
					lstrSql = lstrSql & "   AND `nf`.`flgst` = 'R' "
					istrCabec(0) = "Baixados Reserva iluminação pública no período de " & VB6.Format(Me.dtpInicial.Value, "dd/mm/yyyy") & " até " & VB6.Format(Me.dtpFinal.Value, "dd/mm/yyyy")
				End If

				Call MontaDados(lstrSql, 0)
			ElseIf Me.chkFaturado.CheckState = 1 Then
				'
				'--- Faturados no período Informado
				lstrSql = ""
				'lstrSql = lstrSql & "   AND `nf`.`mesano_ref` BETWEEN " & CLng(Format(Me.dtpInicial.Value, "yyyymm")) & " AND " & CLng(Format(Me.dtpFinal.Value, "yyyymm"))
				lstrSql = lstrSql & "   AND `nf`.`dataem` BETWEEN '" & VB6.Format(Me.dtpInicial.Value, "yyyy-MM-dd") & "' AND '" & VB6.Format(Me.dtpFinal.Value, "yyyy-MM-dd") & "' "
				'lstrSql = lstrSql & "   AND `nf`.`mesano_ref` = " & CLng(Format(Me.dtpFinal.Value, "yyyymm"))
				'lstrSql = lstrSql & "   AND `nf`.`flgst` <> 'C' "
				lstrSql = lstrSql & " AND ((mesano_ref BETWEEN " & CInt(VB6.Format(Me.dtpInicial.Value, "yyyymm")) & " AND " & CInt(VB6.Format(Me.dtpFinal.Value, "yyyymm")) & ") OR" & vbCrLf
				lstrSql = lstrSql & "      (flgst='C' AND mesano_ref <> REPLACE(LEFT(datast, 7),'-','') AND datast BETWEEN '" & VB6.Format(Me.dtpInicial.Value, "yyyy-MM-dd") & "' AND '" & VB6.Format(Me.dtpFinal.Value, "yyyy-MM-dd") & "'))" & vbCrLf
				istrCabec(1) = "Resumo de custeio de iluminação pública faturado com emissão entre " & VB6.Format(Me.dtpInicial.Value, "dd/mm/yyyy") & " até " & VB6.Format(Me.dtpFinal.Value, "dd/mm/yyyy")
				Call MontaDados(lstrSql, 1)
			ElseIf Me.chkAberto.CheckState = 1 Then
				'
				'--- Em Aberto até a data final informada
				lstrSql = ""
				lstrSql = lstrSql & "   AND `nf`.`mesano_ref` <= " & CInt(VB6.Format(Me.dtpFinal.Value, "yyyymm")) & " "
				lstrSql = lstrSql & "   AND `nf`.`dataem` <= '" & VB6.Format(Me.dtpFinal.Value, "yyyy-MM-dd") & "' "
				lstrSql = lstrSql & "   AND (`nf`.`datapag` > '" & VB6.Format(Me.dtpFinal.Value, "yyyy-MM-dd") & "' OR `nf`.`datapag` = '0000-00-00' OR `nf`.`datapag` = '1900-01-01' )"
				lstrSql = lstrSql & "   AND (`nf`.`flgst` <> 'C' OR " & vbCrLf
				lstrSql = lstrSql & "        ( (`nf`.`flgst`='C' AND `nf`.`datast` > '" & VB6.Format(Me.dtpFinal.Value, "yyyy-MM-dd") & "') OR " & vbCrLf
				lstrSql = lstrSql & "          (`nf`.`flgst`='C' AND `nf`.`mesano_ref` <> REPLACE(LEFT(`nf`.`datast`, 7),'-','') AND `nf`.`datast` BETWEEN '" & VB6.Format(Me.dtpInicial.Value, "yyyy-MM-dd") & "' AND '" & VB6.Format(Me.dtpFinal.Value, "yyyy-MM-dd") & "')" & vbCrLf
				lstrSql = lstrSql & "         )" & vbCrLf
				lstrSql = lstrSql & "        )" & vbCrLf
				lstrSql = lstrSql & "   AND NOT EXISTS(SELECT * " & vbCrLf
				lstrSql = lstrSql & "                    FROM reserva " & vbCrLf
				lstrSql = lstrSql & "                   WHERE reserva.numnf    = nf.numnf  " & vbCrLf
				lstrSql = lstrSql & "                     AND reserva.mesano   = nf.mesano " & vbCrLf
				lstrSql = lstrSql & "                     AND reserva.zona     = nf.zona   " & vbCrLf
				lstrSql = lstrSql & "                     AND reserva.livro    = nf.livro  " & vbCrLf
				lstrSql = lstrSql & "                     AND reserva.conta    = nf.conta  " & vbCrLf
				lstrSql = lstrSql & "                     AND reserva.datares <= '" & VB6.Format(Me.dtpFinal.Value, "yyyy-MM-dd") & "')"
				istrCabec(2) = "Resumo de custeio de iluminação pública em aberto até o dia " & VB6.Format(Me.dtpFinal.Value, "dd/mm/yyyy")
				Call MontaDados(lstrSql, 2)
			End If
			Call RelIluminacao()
			'
			'With ifrmRelCIP
			'	Call .VS.EndDoc()
			'	Call .Show()
			'End With
			Me.lblInfo.Text = " Pronto!"
			Call Me.lblInfo.Refresh()
			Call LigDes(True)
			Me.Cursor = System.Windows.Forms.Cursors.Default

		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: cmdImprimir_Click" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub
	
	Private Sub MontaDados(ByVal astrSql As String, ByVal aintTipo As Short)
		Dim lstrZona As String
		Dim lstrClas As String
		Dim lstrItem As String
		Dim ldblIlum As Double
		Dim lstrCdes As String
		Dim lstrSql As String
		'Dim lrstAux As New ADODB.Recordset
		'lrstAux = New ADODB.Recordset
		Try
			Me.lblInfo.Text = " Buscando faturas " & IIf(aintTipo = 0, "(Arrecadado)", IIf(aintTipo = 1, "(Faturado)", "(Abertos)"))
			Call Me.lblInfo.Refresh()
			lstrSql = ""
			lstrSql = lstrSql & "SELECT "
			lstrSql = lstrSql & "       `nf`.`codclas`, "
			lstrSql = lstrSql & "       `nf`.`itensserv`, "
			lstrSql = lstrSql & "       `nf`.`numnf`, "
			lstrSql = lstrSql & "       `nf`.`zona`, "
			lstrSql = lstrSql & "       `nf`.`datapag`, "
			lstrSql = lstrSql & "       `nf`.`flgst`, "
			lstrSql = lstrSql & "       `nf`.`mesano_ref`, "
			lstrSql = lstrSql & "       `nf`.`mesano` "
			lstrSql = lstrSql & "  FROM `nf` "
			lstrSql = lstrSql & " WHERE `nf`.`itensserv` LIKE '%0301000003%' "
			objAtualizaBancoDados.QUERY_NAME = lstrSql & astrSql
			dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)


			If dt.Rows.Count > 0 Then
				Me.mfgDados.Redraw = False
				Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, dt.Rows.Count, IIf(aintTipo = 0, "(Arrecadado)", IIf(aintTipo = 1, "(Faturado)", "(Abertos)")))
				For Each row As DataRow In dt.Rows
					lstrZona = Trim(IIf(IsDBNull(row.Item("zona")), "000", row.Item("zona")))
					lstrClas = Trim(IIf(IsDBNull(row.Item("codclas")), "0000", row.Item("codclas")))
					lstrItem = Trim(IIf(IsDBNull(row.Item("itensserv")), "", row.Item("itensserv")))
					ldblIlum = BuscaVlrItem(lstrItem, "0301000003")
					Select Case CShort(VB.Left(lstrClas, 1))
						Case Is = 9
							lstrCdes = "0 RESIDENCIAL"
						Case Is < 4
							lstrCdes = "1 INDUSTRIAL"
						Case Is = 4
							lstrCdes = "2 COMERCIAL"
						Case Is = 5
							lstrCdes = "3 RURAL"
						Case Is = 6
							lstrCdes = "4 PODERES PÚBLICOS"
						Case Is = 7
							lstrCdes = "5 SERVIÇO PÚBLICO"
						Case Is = 8
							lstrCdes = "6 CONSUMO PRÓPRIO"
						Case Else
							lstrCdes = "7 INDEFINIDO"
					End Select

					Call AdicionaGrid(aintTipo, lstrZona, lstrCdes, ldblIlum)

					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1,  , IIf(aintTipo = 0, "(Arrecadado)", IIf(aintTipo = 1, "(Faturado)", "(Abertos)")))
				Next
			End If

			With Me.mfgDados
				.Col = 5
				.Sort = MSFlexGridLib.SortSettings.flexSortGenericAscending
				.Redraw = True
			End With

			Call sCarregaTabelaAux()

			Call sDadosEmpresa(LImagem, sNomeEmpresa, sEnderecoEmpresa, sCepEmpresa, sCidadeEmpresa, sUFempresa, sBairroEmpresa, sFoneEmpresa, sWebEmpresa)
			Call lPrint("rptRelIluminacaoPublica")

		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: MontaDados" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub
	
	Private Sub RelIluminacao()
		Dim llngRow As Integer
		Dim lstrTam As String
		Dim lstrTab As String
		Dim lstrAux As String
		Dim lintAux As Short
		
		Dim lintTipo As Short
		Dim lstrZona As String
		Dim lstrCdes As String
		Dim llngQtde As Integer
		Dim ldblIlum As Double
		Dim llntSTQt As Integer
		Dim ldblSTot As Double
		Dim llntGTQt As Integer
		Dim ldblGTot As Double

		Try

			With Me.mfgDados
				If .Rows > 1 Then
					lintTipo = CShort(.get_TextMatrix(1, 0))
				End If
				'With ifrmRelCIP
				'	.VS.CurrentLine = 1
				'	Call Cabecalho_Relatorio(.VS, istrCabec(lintTipo), "", "2")
				'	Call LineFeed(.VS, 3)
				'	.VS.Font = VB6.FontChangeBold(.VS.Font, True)
				'	.VS.BrushColor = System.Drawing.Color.White
				'	.VS.Font = VB6.FontChangeSize(.VS.Font, 8)
				'	lstrTam = "<10mm|<20mm|<70mm|>35mm|>60mm;"
				'	lstrTab = "|ZONA|CLASSE|QTD|Valor"
				'	.VS.Table = lstrTam & lstrTab
				'	.VS.Font = VB6.FontChangeBold(.VS.Font, False)
				'	Call LineFeed(.VS, 1)
				'End With
				For llngRow = 1 To .Rows - 1
					lintTipo = CShort(.get_TextMatrix(llngRow, 0))
					lstrZona = Trim(.get_TextMatrix(llngRow, 1))
					lstrCdes = Trim(.get_TextMatrix(llngRow, 2))
					llngQtde = CInt(.get_TextMatrix(llngRow, 3))
					ldblIlum = CDbl(.get_TextMatrix(llngRow, 4))
					If llngRow = 1 Then
						lstrAux = lstrZona
						lintAux = lintTipo
					End If
					'With ifrmRelCIP
					'	If lstrZona <> lstrAux Or lintTipo <> lintAux Then
					'		.VS.Font = VB6.FontChangeBold(.VS.Font, True)
					'		lstrTab = ""
					'		lstrTab = lstrTab & "|"
					'		lstrTab = lstrTab & "TOTAL" & "|"
					'		lstrTab = lstrTab & "|"
					'		lstrTab = lstrTab & FormatNumber(llntSTQt, 0) & "|"
					'		lstrTab = lstrTab & FormatNumber(ldblSTot, 2)
					'		.VS.Table = lstrTam & lstrTab
					'		.VS.Font = VB6.FontChangeBold(.VS.Font, False)
					'		Call LineFeed(.VS, 1)
					'		llntSTQt = 0
					'		ldblSTot = 0
					'	End If
					'	'If lintTipo <> lintAux Then
					'	'	.VS.NewPage()
					'	'	.VS.CurrentLine = 1
					'	'	Call Cabecalho_Relatorio(.VS, istrCabec(lintTipo), "", "2")
					'	'	Call LineFeed(.VS, 3)
					'	'	.VS.Font = VB6.FontChangeBold(.VS.Font, True)
					'	'	.VS.BrushColor = System.Drawing.Color.White
					'	'	.VS.Font = VB6.FontChangeSize(.VS.Font, 8)
					'	'	lstrTam = "<10mm|<20mm|<70mm|>35mm|>60mm;"
					'	'	lstrTab = "|ZONA|CLASSE|QTD|Valor"
					'	'	.VS.Table = lstrTam & lstrTab
					'	'	.VS.Font = VB6.FontChangeBold(.VS.Font, False)
					'	'	Call LineFeed(.VS, 1)
					'	'End If
					'	llntSTQt = llntSTQt + llngQtde
					'	ldblSTot = ldblSTot + ldblIlum
					'	llntGTQt = llntGTQt + llngQtde
					'	ldblGTot = ldblGTot + ldblIlum
					'	lstrTab = ""
					'	lstrTab = lstrTab & "|"
					'	lstrTab = lstrTab & lstrZona & "|"
					'	lstrTab = lstrTab & Mid(lstrCdes, 3) & "|"
					'	lstrTab = lstrTab & FormatNumber(llngQtde, 0) & "|"
					'	lstrTab = lstrTab & FormatNumber(ldblIlum, 2)
					'	.VS.Table = lstrTam & lstrTab
					'	'If .VS.CurrentLine > 402 Then
					'	'	.VS.NewPage()
					'	'	.VS.CurrentLine = 1
					'	'	Call Cabecalho_Relatorio(.VS, istrCabec(lintTipo), "", "2")
					'	'	Call LineFeed(.VS, 3)
					'	'	.VS.Font = VB6.FontChangeBold(.VS.Font, True)
					'	'	.VS.BrushColor = System.Drawing.Color.White
					'	'	.VS.Font = VB6.FontChangeSize(.VS.Font, 8)
					'	'	lstrTam = "<10mm|<20mm|<70mm|>35mm|>60mm;"
					'	'	lstrTab = "|ZONA|CLASSE|QTD|Valor"
					'	'	.VS.Table = lstrTam & lstrTab
					'	'	.VS.Font = VB6.FontChangeBold(.VS.Font, False)
					'	'	Call LineFeed(.VS, 1)
					'	'End If
					'	lstrAux = lstrZona
					'	lintAux = lintTipo
					'End With
				Next llngRow
				'With ifrmRelCIP
				'	.VS.Font = VB6.FontChangeBold(.VS.Font, True)
				'	lstrTab = ""
				'	lstrTab = lstrTab & "|"
				'	lstrTab = lstrTab & "TOTAL" & "|"
				'	lstrTab = lstrTab & "|"
				'	lstrTab = lstrTab & FormatNumber(llntSTQt, 0) & "|"
				'	lstrTab = lstrTab & FormatNumber(ldblSTot, 2)
				'	.VS.Table = lstrTam & lstrTab
				'	.VS.Font = VB6.FontChangeBold(.VS.Font, False)
				'	'      Call LineFeed(.VS, 2)
				'	'      .VS.FontBold = True
				'	'      lstrTab = ""
				'	'      lstrTab = lstrTab & "|"
				'	'      lstrTab = lstrTab & "TOTAL GERAL" & "|"
				'	'      lstrTab = lstrTab & "|"
				'	'      lstrTab = lstrTab & FormatNumber(llntGTQt, 0) & "|"
				'	'      lstrTab = lstrTab & FormatNumber(ldblGTot, 2)
				'	'      .VS.Table = lstrTam & lstrTab
				'	'      .VS.FontBold = False
				'End With
			End With
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: RelIluminacao" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub LigDes(ByVal abModo As Boolean)
		Try
			Me.dtpInicial.Enabled = abModo
			Me.dtpFinal.Enabled = abModo
			Me.cmdImprimir.Enabled = abModo
			Me.cmdFechar.Enabled = abModo
			Me.chkReserva.Enabled = abModo
			If Me.chkReserva.CheckState = System.Windows.Forms.CheckState.Checked Then
				Me.chkArrecadado.Enabled = False
				Me.chkAberto.Enabled = False
				Me.chkFaturado.Enabled = False
			Else
				Me.chkArrecadado.Enabled = abModo
				Me.chkAberto.Enabled = abModo
				Me.chkFaturado.Enabled = abModo
			End If
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: LigDes" & "", "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 frmRelIluminacao_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load

		Try

			Me.dtpInicial.Value = Date.Now()
			Me.dtpFinal.Value = CDate(UltimoDiaMes(Month(dtpInicial.Value), Year(dtpInicial.Value)) & "/" & Month(dtpInicial.Value) & "/" & Year(dtpInicial.Value))
			Call sCriaTabelaAuxiliar()
			'ifrmRelCIP = New FrmRelatorio
			With Me.mfgDados
				.Rows = 1
				Call .Clear()
				.set_TextMatrix(0, 0, "Tipo")
				.set_TextMatrix(0, 1, "Zona")
				.set_TextMatrix(0, 2, "Classe")
				.set_TextMatrix(0, 3, "Qtde")
				.set_TextMatrix(0, 4, "Valor")
				.set_TextMatrix(0, 5, "Ordenação")
				.set_ColWidth(0, 500)
				.set_ColWidth(1, 500)
				.set_ColWidth(2, 2000)
				.set_ColWidth(3, 800)
				.set_ColWidth(4, 1000)
				.set_ColWidth(5, 2800)
				.set_ColAlignment(0, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
				.set_ColAlignment(1, MSFlexGridLib.AlignmentSettings.flexAlignLeftCenter)
				.set_ColAlignment(2, MSFlexGridLib.AlignmentSettings.flexAlignLeftCenter)
				.set_ColAlignment(3, MSFlexGridLib.AlignmentSettings.flexAlignRightCenter)
				.set_ColAlignment(4, MSFlexGridLib.AlignmentSettings.flexAlignRightCenter)
				.set_ColAlignment(5, MSFlexGridLib.AlignmentSettings.flexAlignLeftCenter)
			End With

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

	Private Sub frmRelIluminacao_FormClosed(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
		Try
			Dim lstrsql As String


			objExecAtualizaBancoDados.QUERY_NAME = "drop table if exists AUX_ILUMINACAO_PUBLICA"
			repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

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

	Private Sub AdicionaGrid(ByVal aintTipo As Short, ByVal astrZona As String, ByVal astrCdes As String, ByVal adblIlum As Double)
		Dim llngRow As Integer
		Dim llngPos As Integer
		llngPos = 0
		'Verifica se jé tem na grade

		Try
			With Me.mfgDados
				If .Rows > 1 Then
					For llngRow = 1 To .Rows - 1
						If CDbl(.get_TextMatrix(llngRow, 0)) = aintTipo Then
							If .get_TextMatrix(llngRow, 1) = astrZona Then
								If .get_TextMatrix(llngRow, 2) = astrCdes Then
									llngPos = llngRow
									llngRow = .Rows - 1
								End If
							End If
						End If
					Next llngRow
				End If
				If llngPos = 0 Then
					.Rows = .Rows + 1
					llngPos = (.Rows - 1)
					.set_TextMatrix(llngPos, 0, aintTipo) 'Tipo relatório
					.set_TextMatrix(llngPos, 1, astrZona) 'Cidade
					.set_TextMatrix(llngPos, 2, astrCdes) 'Classe Código/Descrição
					.set_TextMatrix(llngPos, 5, aintTipo & astrZona & astrCdes)
				End If
				.set_TextMatrix(llngPos, 3, FormatNumber(CInt(IIf(.get_TextMatrix(llngPos, 3) = "", 0, .get_TextMatrix(llngPos, 3))) + 1, 0)) 'Qtde
				.set_TextMatrix(llngPos, 4, FormatNumber(CDbl(IIf(.get_TextMatrix(llngPos, 4) = "", 0, .get_TextMatrix(llngPos, 4))) + adblIlum, 2)) 'valor
			End With

		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: AdicionaGrid" & "", "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_1 As DataTable
		Dim dt_2 As DataTable
		Dim dt_3 As DataTable
		Dim dc As DataColumn
		Dim ds As DataSet
		Dim lstrsql As String
		Dim Filtro As String

		Dim bytBLOBData() As Byte

		Dim objEmpresa As New entidadeEmpresa
		Dim repositorioEmpresa As New repositorioEmpresa

		Dim vlist

		Filtro = "Por: "
		If chkArrecadado.Checked = True Then
			Filtro = Filtro & "Arrecadados" & ", "
		End If

		If chkFaturado.Checked = True Then
			Filtro = Filtro & "Faturados " & ", "
		End If

		If chkAberto.Checked = True Then
			Filtro = Filtro & "Em Aberto " & ", "
		End If

		If chkReserva.Checked = True Then
			Filtro = Filtro & "Enviado Para Reserva " & ", "
		End If

		If chkSomenteMiicrogeradoras.Checked = True Then
			Filtro = Filtro & "Somente Micro Geradoras " & ", "
		End If

		Filtro = Mid(Filtro, 1, Len(Filtro) - 2)

		'Dim vlist

		'Dim objEmpresa As New entidadeEmpresa
		'Dim repositorioEmpresa As New repositorioEmpresa


		'vlist = repositorioEmpresa.Select(objEmpresa)

		'If vlist.Count <> 0 Then

		'	For Each objEmpresaFor As entidadeEmpresa In vlist

		'		bytBLOBData = objEmpresaFor.LOGOTIPO

		'	Next

		'End If

		Try
			Dim rpt As New ReportDocument()

			'rptConsumidorAVencer
			If vNomeRPT = "rptRelIluminacaoPublica" Then


				dt_2 = New DataTable()
				dt_2.Columns.Add("TIPO", GetType(String))
				dt_2.Columns.Add("ZONA", GetType(String))
				dt_2.Columns.Add("CLASSE", GetType(String))
				dt_2.Columns.Add("QTDE", GetType(String))
				dt_2.Columns.Add("VALOR", GetType(String))
				dt_2.Columns.Add("IMAGEM", GetType(Byte()))

				ds = New DataSet

				lstrsql = ""
				lstrsql = "SELECT * FROM AUX_ILUMINACAO_PUBLICA " 'WHERE nm_usuario = '" & My.Computer.Name & "'"
				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("TIPO"),
										  dr_1("ZONA"),
										  dr_1("CLASSE"),
										  dr_1("QTDE"),
										  dr_1("VALOR"),
										  bytBLOBData)
					Next dr_1
				End If


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


				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 rptRelIluminacaoPublica

					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("Filtros", "Período de: " & Format(dtpInicial.Value, "dd/MM/yyyy") & " até: " & Format(dtpFinal.Value, "dd/MM/yyyy"))
					rpt.SetParameterValue("Filtros2", Filtro)
					' rpt.SetParameterValue("NomeRel", "Grupo A - Período de: " & dtpIni.Value & " Até: " & dtpFim.Value)

					frmViewRelatorios.rpt = rpt

					frmViewRelatorios.CRViewer.Zoom((100))
					frmViewRelatorios.Text = "Relatório Iluminação Publica"
					frmViewRelatorios.ShowDialog()

				End If

			End If

			Me.Cursor = System.Windows.Forms.Cursors.Default

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

	End Sub

	Private Sub sCriaTabelaAuxiliar()

		Try
			Dim lstrsql As String


			objExecAtualizaBancoDados.QUERY_NAME = "drop table if exists AUX_ILUMINACAO_PUBLICA"
			repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

			lstrsql = ""
			lstrsql = "create table AUX_ILUMINACAO_PUBLICA ("
			lstrsql = lstrsql & "TIPO VARCHAR(10),"
			lstrsql = lstrsql & "ZONA VARCHAR(10),"
			lstrsql = lstrsql & "CLASSE VARCHAR(50),"
			lstrsql = lstrsql & "QTDE VARCHAR(10),"
			lstrsql = lstrsql & "VALOR  VARCHAR(20))"
			objExecAtualizaBancoDados.QUERY_NAME = lstrsql
			repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

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

	End Sub

	Private Sub sCarregaTabelaAux()
		Dim i As Integer
		Dim lstrsql As String

		lstrsql = "delete from AUX_ILUMINACAO_PUBLICA"
		objExecAtualizaBancoDados.QUERY_NAME = lstrsql
		repositorioAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

		For i = 1 To mfgDados.Rows - 1

			lstrsql = "INSERT INTO AUX_ILUMINACAO_PUBLICA SET "
			lstrsql = lstrSql & "TIPO = '" & mfgDados.get_TextMatrix(i, 0) & "' ,"
			lstrSql = lstrSql & "ZONA = '" & mfgDados.get_TextMatrix(i, 1) & "' ,"
			lstrSql = lstrSql & "CLASSE = '" & mfgDados.get_TextMatrix(i, 2) & "' ,"
			lstrSql = lstrSql & "QTDE = '" & mfgDados.get_TextMatrix(i, 3) & "',"
			lstrsql = lstrsql & "VALOR = '" & mfgDados.get_TextMatrix(i, 4) & "'"
			objExecAtualizaBancoDados.QUERY_NAME = lstrsql
			repositorioAtualizaBancoDados.Execute(objExecAtualizaBancoDados)


		Next

	End Sub
End Class