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 frmRelRetencao
	Inherits System.Windows.Forms.Form
	Private objAtualizaBancoDados As New entidadeAtualizaBancoDados
	Private repositorioAtualizaBancoDados As New repositorioAtualizaBancoDados

	Private objExecAtualizaBancoDados As New entidadeAtualizaBancoDados
	Private repositorioExecAtualizaBancoDados As New repositorioAtualizaBancoDados
	Private dt, dt1, dt2, dt3, dt4 As DataTable
	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 Sub cmdEmitir_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdEmitir.Click
		Dim lstrCabec1 As String
		Dim lstrCabec2 As String
		Dim lstrTipoRel As String
		Dim lstrMesAno As String
		Dim lstrZona As String
		Dim lstrLivro As String
		Dim lstrConta As String
		Dim llngConsId As Integer
		Dim llngUnidId As Integer
		Dim lstrNome As String
		Dim lstrCodClas As String
		Dim lstrNomClas As String
		Dim lstrGrTar As String
		Dim ldtePagto As Date
		Dim ldteVecto As Date
		Dim ldblPisKwh As Double
		Dim ldblCofKwh As Double
		Dim ldblCslKwh As Double
		Dim ldblIrpKwh As Double
		Dim ldblPisKw As Double
		Dim ldblCofKw As Double
		Dim ldblCslKw As Double
		Dim ldblIrpKw As Double
		Dim lstrSql As String
		Dim lstrOrdem As String
		Dim lstrCriterio As String
		Dim lstrComando As String


		Try
			Call LigDes(False)
			lstrComando = " " & vbCrLf
			Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
			Me.lblInfo.Text = " Buscando os dados...."
			lstrCabec1 = "RELATÓRIO DAS RETENÇÕES DE TRIBUTOS FEDERAIS"
			lstrCabec2 = "Período de " & VB6.Format(Me.dtpInicial.Value, "dd/mm/yyyy") & " até " & VB6.Format(Me.dtpFinal.Value, "dd/mm/yyyy")


			System.Windows.Forms.Application.DoEvents()
			lstrSql = "DELETE FROM `AUX_REL_RETENCAO` WHERE `AUX_REL_RETENCAO`.`nm_usuario`      = """ & My.Computer.Name & """ "
			objExecAtualizaBancoDados.QUERY_NAME = lstrSql
			repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

			lstrOrdem = ""
			lstrSql = ""
			lstrSql = lstrSql & "SELECT nf.mesano, nf.zona, nf.livro, nf.conta, nf.codclas, nf.ta, nf.datapag, nf.datavenc, nf.cons_id, nf.unid_id, "
			lstrSql = lstrSql & "       buscavlritem(nf.itensserv,""9090000001"") + 0 pis_kwh, "
			lstrSql = lstrSql & "       buscavlritem(nf.itensserv,""9090000002"") + 0 cofins_kwh, "
			lstrSql = lstrSql & "       buscavlritem(nf.itensserv,""9090000003"") + 0 csll_kwh, "
			lstrSql = lstrSql & "       buscavlritem(nf.itensserv,""9090000004"") + 0 irpj_kwh, "
			lstrSql = lstrSql & "       buscavlritem(nf.itensserv,""9090000005"") + 0 irpj_kw, "
			lstrSql = lstrSql & "       buscavlritem(nf.itensserv,""9090000006"") + 0 csll_kw, "
			lstrSql = lstrSql & "       buscavlritem(nf.itensserv,""9090000007"") + 0 cofins_kw, "
			lstrSql = lstrSql & "       buscavlritem(nf.itensserv,""9090000008"") + 0 pis_kw "
			lstrSql = lstrSql & "  FROM nf "
			If Me.opbFaturado.Checked Then
				lstrSql = lstrSql & " WHERE CONCAT(RIGHT(nf.mesano, 4),""-"",LEFT(nf.mesano,2),""-01"") BETWEEN """ & VB6.Format(Me.dtpInicial.Value, "yyyy-MM-dd") & """ AND """ & VB6.Format(Me.dtpFinal.Value, "yyyy-MM-dd") & """ "
				lstrOrdem = " ORDER BY CONCAT(RIGHT(nf.mesano,4),LEFT(nf.mesano,2)) ASC, MID(nf.ta,3,2) ASC, nf.zona ASC, nf.livro ASC, nf.conta ASC "
				lstrCabec2 = lstrCabec2 & " (Faturado) "
				lstrTipoRel = CStr(1)
			ElseIf Me.opbArrecadado.Checked Then
				lstrSql = lstrSql & " WHERE nf.datapag BETWEEN """ & VB6.Format(Me.dtpInicial.Value, "yyyy-MM-dd") & """ AND """ & VB6.Format(Me.dtpFinal.Value, "yyyy-MM-dd") & """ "
				lstrOrdem = " ORDER BY CONCAT(RIGHT(nf.mesano,4),LEFT(nf.mesano,2)) ASC, MID(nf.ta,3,2) ASC, nf.datapag ASC "
				lstrCabec2 = lstrCabec2 & " (Arrecadado) "
				lstrTipoRel = CStr(2)
			ElseIf Me.opbAberto.Checked Then
				lstrSql = lstrSql & " WHERE CONCAT(RIGHT(nf.mesano, 4),""-"",LEFT(nf.mesano,2),""-01"") BETWEEN """ & VB6.Format(Me.dtpInicial.Value, "yyyy-MM-dd") & """ AND """ & VB6.Format(Me.dtpFinal.Value, "yyyy-MM-dd") & """ "
				lstrSql = lstrSql & "   AND (nf.datapag IS NULL OR nf.datapag = ""0000-00-00"") "
				lstrOrdem = " ORDER BY CONCAT(RIGHT(nf.mesano,4),LEFT(nf.mesano,2)) ASC, MID(nf.ta,3,2) ASC, nf.datavenc ASC "
				lstrCabec2 = lstrCabec2 & " (Abertos) "
				lstrTipoRel = CStr(1)
			End If
			lstrSql = lstrSql & "   AND (   nf.itensserv LIKE ""%9090000001%"" "
			lstrSql = lstrSql & "        OR nf.itensserv LIKE ""%9090000002%"" "
			lstrSql = lstrSql & "        OR nf.itensserv LIKE ""%9090000003%"" "
			lstrSql = lstrSql & "        OR nf.itensserv LIKE ""%9090000004%"" "
			lstrSql = lstrSql & "        OR nf.itensserv LIKE ""%9090000005%"" "
			lstrSql = lstrSql & "        OR nf.itensserv LIKE ""%9090000006%"" "
			lstrSql = lstrSql & "        OR nf.itensserv LIKE ""%9090000007%"" "
			lstrSql = lstrSql & "        OR nf.itensserv LIKE ""%9090000008%"") "
			objAtualizaBancoDados.QUERY_NAME = lstrSql
			dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

			If dt.Rows.Count > 0 Then
				Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, dt.Rows.Count)
				For Each row As DataRow In dt.Rows

					lstrMesAno = IIf(IsDBNull(row.Item("mesano")), "", row.Item("mesano"))
					lstrZona = VB6.Format(IIf(IsDBNull(row.Item("zona")), "0", row.Item("zona")), "000")
					lstrLivro = VB6.Format(IIf(IsDBNull(row.Item("livro")), "0", row.Item("livro")), "00")
					lstrConta = VB6.Format(IIf(IsDBNull(row.Item("conta")), "0", row.Item("conta")), "000000")
					llngConsId = IIf(IsDBNull(row.Item("cons_id")), 0, row.Item("cons_id"))
					llngUnidId = IIf(IsDBNull(row.Item("unid_id")), 0, row.Item("unid_id"))
					lstrCodClas = VB6.Format(IIf(IsDBNull(row.Item("codclas")), "0", row.Item("codclas")), "0000")
					lstrNomClas = BuscaNMClase(lstrCodClas)
					lstrGrTar = Mid(IIf(IsDBNull(row.Item("ta")), "01B1", row.Item("ta")), 3, 2)
					lstrNome = VB.Left(BuscaNMConsumidor(llngConsId, llngUnidId, lstrGrTar), 100)
					ldtePagto = CDate(VB6.Format(IIf(IsDBNull(row.Item("datapag")), "1900-01-01", row.Item("datapag")), "dd/mm/yyyy"))
					ldteVecto = CDate(VB6.Format(IIf(IsDBNull(row.Item("datavenc")), "1900-01-01", row.Item("datavenc")), "dd/mm/yyyy"))
					ldblPisKwh = Arredondar(IIf(IsDBNull(row.Item("pis_kwh")), 0, row.Item("pis_kwh")), 2)
					ldblCofKwh = Arredondar(IIf(IsDBNull(row.Item("cofins_kwh")), 0, row.Item("cofins_kwh")), 2)
					ldblCslKwh = Arredondar(IIf(IsDBNull(row.Item("csll_kwh")), 0, row.Item("csll_kwh")), 2)
					ldblIrpKwh = Arredondar(IIf(IsDBNull(row.Item("irpj_kwh")), 0, row.Item("irpj_kwh")), 2)
					ldblPisKw = Arredondar(IIf(IsDBNull(row.Item("pis_kw")), 0, row.Item("pis_kw")), 2)
					ldblCofKw = Arredondar(IIf(IsDBNull(row.Item("cofins_kw")), 0, row.Item("cofins_kw")), 2)
					ldblCslKw = Arredondar(IIf(IsDBNull(row.Item("csll_kw")), 0, row.Item("csll_kw")), 2)
					ldblIrpKw = Arredondar(IIf(IsDBNull(row.Item("irpj_kw")), 0, row.Item("irpj_kw")), 2)

					lstrSql = ""
					lstrSql = lstrSql & "INSERT INTO `AUX_REL_RETENCAO` SET " & lstrComando
					lstrSql = lstrSql & "            `AUX_REL_RETENCAO`.`nm_usuario` = """ & My.Computer.Name & """, " & lstrComando
					lstrSql = lstrSql & "            `AUX_REL_RETENCAO`.`mesano`     = """ & VB.Right(lstrMesAno, 4) & VB.Left(lstrMesAno, 2) & """, " & lstrComando
					lstrSql = lstrSql & "            `AUX_REL_RETENCAO`.`zona`       = """ & lstrZona & """, " & lstrComando
					lstrSql = lstrSql & "            `AUX_REL_RETENCAO`.`livro`      = """ & lstrLivro & """, " & lstrComando
					lstrSql = lstrSql & "            `AUX_REL_RETENCAO`.`conta`      = """ & lstrConta & """, " & lstrComando
					lstrSql = lstrSql & "            `AUX_REL_RETENCAO`.`codclas`    = """ & lstrCodClas & """, " & lstrComando
					lstrSql = lstrSql & "            `AUX_REL_RETENCAO`.`tarifa`     = """ & lstrGrTar & """, " & lstrComando
					lstrSql = lstrSql & "            `AUX_REL_RETENCAO`.`nome`       = """ & lstrNome & """, " & lstrComando
					lstrSql = lstrSql & "            `AUX_REL_RETENCAO`.`pagto`      = """ & VB6.Format(ldtePagto, "yyyy-MM-dd") & """, " & lstrComando
					lstrSql = lstrSql & "            `AUX_REL_RETENCAO`.`vecto`      = """ & VB6.Format(ldteVecto, "yyyy-MM-dd") & """, " & lstrComando
					lstrSql = lstrSql & "            `AUX_REL_RETENCAO`.`piskwh`     = " & Replace(CStr(ldblPisKwh), ",", ".") & ", " & lstrComando
					lstrSql = lstrSql & "            `AUX_REL_RETENCAO`.`cofinskwh`  = " & Replace(CStr(ldblCofKwh), ",", ".") & ", " & lstrComando
					lstrSql = lstrSql & "            `AUX_REL_RETENCAO`.`csllkwh`    = " & Replace(CStr(ldblCslKwh), ",", ".") & ", " & lstrComando
					lstrSql = lstrSql & "            `AUX_REL_RETENCAO`.`irpjkwh`    = " & Replace(CStr(ldblIrpKwh), ",", ".") & ", " & lstrComando
					lstrSql = lstrSql & "            `AUX_REL_RETENCAO`.`piskw`      = " & Replace(CStr(ldblPisKw), ",", ".") & ", " & lstrComando
					lstrSql = lstrSql & "            `AUX_REL_RETENCAO`.`cofinskw`   = " & Replace(CStr(ldblCofKw), ",", ".") & ", " & lstrComando
					lstrSql = lstrSql & "            `AUX_REL_RETENCAO`.`csllkw`     = " & Replace(CStr(ldblCslKw), ",", ".") & ", " & lstrComando
					lstrSql = lstrSql & "            `AUX_REL_RETENCAO`.`irpJkw`     = " & Replace(CStr(ldblIrpKw), ",", ".") & ", " & lstrComando
					lstrSql = lstrSql & "            `AUX_REL_RETENCAO`.`tiporel`    = " & Replace(lstrTipoRel, ",", ".") & " "
					objExecAtualizaBancoDados.QUERY_NAME = lstrSql
					repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
				Next

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

				Me.lblInfo.Text = Me.lblInfo.Text & "...Concluído...."
			Else
				Call MsgBox("Não foi encontrado nenhum dado para imprimir, favor verifique as opções!", MsgBoxStyle.Information, gstrTituloMensagem)
				Me.lblInfo.Text = " nenhuma informação processada...."
			End If

			Call LigDes(True)

			Me.Cursor = System.Windows.Forms.Cursors.Default
			System.Windows.Forms.Application.DoEvents()
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: cmdEmitir_Click" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub
	
	Private Function BuscaNMConsumidor(ByVal alngConsId As Integer, ByVal alngUnidId As Integer, ByVal astrGrupo As String) As String
		Dim lstrNome As String
		Dim lstrCpfCnpj As String
		Dim lstrSql As String
		'Dim lrstConsumidor As ADODB.Recordset
		'lrstConsumidor = New ADODB.Recordset
		Try
			lstrSql = ""
			lstrSql = lstrSql & "SELECT nome, cpf_cnpj "
			'lstrSql = lstrSql & "  FROM unid" & LCase(VB.Left(astrGrupo, 1))
			lstrSql = lstrSql & "  FROM uc_faturamento"
			lstrSql = lstrSql & " WHERE cons_id = " & alngConsId
			lstrSql = lstrSql & "   AND id = " & alngUnidId
			'lstrSql = lstrSql & "   AND unid_id = " & alngUnidId
			objAtualizaBancoDados.QUERY_NAME = lstrSql
			dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

			If dt.Rows.Count <= 0 Then

				lstrSql = ""
				lstrSql = lstrSql & "SELECT nome, cgc "
				lstrSql = lstrSql & "  FROM consum"
				lstrSql = lstrSql & " WHERE consreg = " & alngConsId
				objAtualizaBancoDados.QUERY_NAME = lstrSql
				dt1 = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

				If dt1.Rows.Count > 0 Then
					For Each row1 As DataRow In dt1.Rows
						lstrNome = Trim(IIf(IsDBNull(row1.Item("nome")), "", row1.Item("nome")))
						lstrCpfCnpj = Trim(IIf(IsDBNull(row1.Item("cgc")), "", row1.Item("cgc")))
					Next
				Else
					lstrNome = ""
				End If
			Else
				For Each row As DataRow In dt.Rows
					lstrNome = Trim(IIf(IsDBNull(row.Item("nome")), "", row.Item("nome")))
					lstrCpfCnpj = Trim(IIf(IsDBNull(row.Item("cpf_cnpj")), "", row.Item("cpf_cnpj")))
				Next
			End If

			If Len(lstrCpfCnpj) > 11 Then
				lstrCpfCnpj = FormataCNPJ(lstrCpfCnpj)
			Else
				lstrCpfCnpj = FormataCPF(lstrCpfCnpj)
			End If

			BuscaNMConsumidor = lstrCpfCnpj & " " & lstrNome

		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: BuscaNMConsumidor" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Function
		End Try
	End Function
	
	Private Function BuscaNMClase(ByVal astrCodClas As String) As String
		Dim lstrNome As String
		Dim lstrSql As String

		Try
			lstrSql = ""
			lstrSql = lstrSql & "SELECT descricao "
			lstrSql = lstrSql & "  FROM classe "
			lstrSql = lstrSql & " WHERE codigo = " & astrCodClas
			objAtualizaBancoDados.QUERY_NAME = lstrSql
			dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

			If dt.Rows.Count > 0 Then
				For Each row As DataRow In dt.Rows
					lstrNome = Trim(IIf(IsDBNull(row.Item("descricao")), "", row.Item("descricao")))
				Next
			Else
				lstrNome = ""
			End If

			BuscaNMClase = lstrNome
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: BuscaNMClase" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Function
		End Try
	End Function
	
	Private Sub cmdFechar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdFechar.Click
		Call Me.Close()
	End Sub

	Private Sub frmRelRetencao_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
		Dim lstrsql As String

		Try

			lstrsql = ""
			lstrsql = "DROP TABLE IF EXISTS `AUX_REL_RETENCAO`"
			objAtualizaBancoDados.QUERY_NAME = lstrsql
			repositorioAtualizaBancoDados.Execute(objAtualizaBancoDados)


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

	Private Sub frmRelRetencao_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		Try
			'Me.Icon = FrmAtPrincipal.Icon
			Me.dtpInicial.Value = VB6.Format("01/" & VB6.Format(gDtBcoDados, "mm/yyyy"), "dd/mm/yyyy")
			Me.dtpFinal.Value = VB6.Format(UltimoDiaMes(Month(Me.dtpInicial.Value), Year(Me.dtpInicial.Value)) & "/" & VB6.Format(Me.dtpInicial.Value, "mm/yyyy"), "dd/mm/yyyy")
			Call sCarregaTabelaAuxiliar()
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: frmRelRetencao_Load" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub LigDes(ByVal ablnModo As Boolean)
		Try
			Me.dtpFinal.Enabled = ablnModo
			Me.dtpInicial.Enabled = ablnModo
			Me.opbAberto.Enabled = ablnModo
			Me.opbArrecadado.Enabled = ablnModo
			Me.opbFaturado.Enabled = ablnModo
			Me.cmdEmitir.Enabled = ablnModo
			Me.cmdFechar.Enabled = ablnModo
		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 sCarregaTabelaAuxiliar()
		Dim lstrsql As String

		Try
			lstrsql = ""
			lstrsql = "CREATE TABLE IF NOT EXISTS `AUX_REL_RETENCAO` ("
			lstrsql = lstrsql & " `nm_usuario`            varchar(30)  NOT NULL ,"
			lstrsql = lstrsql & " `mesano`                varchar(10)  NOT NULL ,"
			lstrsql = lstrsql & " `zona`                  varchar(10)  NOT NULL ,"
			lstrsql = lstrsql & " `livro`                 varchar(10)  NOT NULL ,"
			lstrsql = lstrsql & " `conta`                 varchar(10)  NOT NULL ,"
			lstrsql = lstrsql & " `codclas`               varchar(10)  NOT NULL ,"
			lstrsql = lstrsql & " `tarifa`                varchar(10)  NOT NULL ,"
			lstrsql = lstrsql & " `nome`                  varchar(50)  NOT NULL ,"
			lstrsql = lstrsql & " `pagto`                 varchar(10)  NOT NULL ,"
			lstrsql = lstrsql & " `vecto`                 varchar(10)  NOT NULL ,"
			lstrsql = lstrsql & " `piskwh`                varchar(20)  NOT NULL ,"
			lstrsql = lstrsql & " `cofinskwh`             varchar(20)  NOT NULL ,"
			lstrsql = lstrsql & " `csllkwh`               varchar(20)  NOT NULL ,"
			lstrsql = lstrsql & " `irpjkwh`               varchar(20)  NOT NULL ,"
			lstrsql = lstrsql & " `piskw`                 varchar(20)  NOT NULL ,"
			lstrsql = lstrsql & " `cofinskw`              varchar(20)  NOT NULL ,"
			lstrsql = lstrsql & " `csllkw`                varchar(20)  NOT NULL ,"
			lstrsql = lstrsql & " `irpJkw`                varchar(20)  NOT NULL ,"
			lstrsql = lstrsql & " `tiporel`               varchar(20)  NOT NULL ) "
			objExecAtualizaBancoDados.QUERY_NAME = lstrsql
			repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)



		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: sCarregaTabelaAuxiliar" & "", "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 sDe, sAte As String

		Dim bytBLOBData() As Byte

		Dim objEmpresa As New entidadeEmpresa
		Dim repositorioEmpresa As New repositorioEmpresa

		Dim vlist

		sDe = VB6.Format(Me.dtpInicial.Value, "dd/mm/yyyy")
		sAte = VB6.Format(Me.dtpFinal.Value, "dd/mm/yyyy")

		Try
			Dim rpt As New ReportDocument()

			If vNomeRPT = "rptRelRetencao" Then

				vlist = repositorioEmpresa.Select(objEmpresa)

				If vlist.Count <> 0 Then
					For Each objEmpresaFor As entidadeEmpresa In vlist
						bytBLOBData = objEmpresaFor.LOGOTIPO
					Next
				End If

				dt_2 = New DataTable()
				dt_2.Columns.Add("NM_USUARIO", GetType(String))
				dt_2.Columns.Add("MESANO", GetType(String))
				dt_2.Columns.Add("ZONA", GetType(String))
				dt_2.Columns.Add("LIVRO", GetType(String))
				dt_2.Columns.Add("CONTA", GetType(String))
				dt_2.Columns.Add("CODCLAS", GetType(String))
				dt_2.Columns.Add("TARIFA", GetType(String))
				dt_2.Columns.Add("NOME", GetType(String))
				dt_2.Columns.Add("PAGTO", GetType(String))
				dt_2.Columns.Add("VECTO", GetType(String))
				dt_2.Columns.Add("PISKWH", GetType(String))
				dt_2.Columns.Add("COFINSKWH", GetType(String))
				dt_2.Columns.Add("CSLLKWH", GetType(String))
				dt_2.Columns.Add("IRPJKWH", GetType(String))
				dt_2.Columns.Add("PISKW", GetType(String))
				dt_2.Columns.Add("COFINSKW", GetType(String))
				dt_2.Columns.Add("CSLLKW", GetType(String))
				dt_2.Columns.Add("IRPJKW", GetType(String))
				dt_2.Columns.Add("TIPOREL", GetType(String))
				dt_2.Columns.Add("IMAGEM", GetType(Byte()))

				ds = New DataSet

				lstrsql = ""
				lstrsql = "SELECT * FROM AUX_REL_RETENCAO " '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("NM_USUARIO"),
										  dr_1("MESANO"),
										  dr_1("ZONA"),
										  dr_1("LIVRO"),
										  dr_1("CONTA"),
										  dr_1("CODCLAS"),
										  dr_1("TARIFA"),
										  dr_1("NOME"),
										  dr_1("PAGTO"),
										  dr_1("VECTO"),
										  dr_1("PISKWH"),
										  dr_1("COFINSKWH"),
										  dr_1("CSLLKWH"),
										  dr_1("IRPJKWH"),
										  dr_1("PISKW"),
										  dr_1("COFINSKW"),
										  dr_1("CSLLKW"),
										  dr_1("IRPJKW"),
										  dr_1("TIPOREL"),
										  bytBLOBData)
					Next dr_1
				End If


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


				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 rptRelRetencao

					rpt.SetDataSource(ds)
					rpt.SetParameterValue("de", sDe)
					rpt.SetParameterValue("ate", sAte)
					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)

					frmViewRelatorios.rpt = rpt

					frmViewRelatorios.CRViewer.Zoom((100))
					frmViewRelatorios.Text = "Relatório de Renda não Faturada"
					frmViewRelatorios.ShowDialog()

				End If
			End If

		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
End Class