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 frmRelLactoCvTxDv
	Inherits System.Windows.Forms.Form

	Private objAtuatlizaBancoDados As New entidadeAtualizaBancoDados
	Private repositorioAtualizaBancoDados As New repositorioAtualizaBancoDados

	Private objExecAtuatlizaBancoDados 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 bValidacao As Boolean
	Private Sub cmbTipos_SelectedIndexChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmbTipos.SelectedIndexChanged
		If VB6.GetItemString(Me.cmbTipos, Me.cmbTipos.SelectedIndex) <> "(nenhum)" Then
			Me.txtNomeSelecao.Text = VB6.GetItemString(Me.cmbTipos, Me.cmbTipos.SelectedIndex)
		End If
	End Sub
	
	Private Sub cmdAdiciona_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdAdiciona.Click
		Dim llngConta As Integer
		Dim lstrCodigo As String
		Dim lstrDescri As String
		Dim lblnAdiciona As Boolean
		Try
			lblnAdiciona = True
			lstrCodigo = Me.lvwTipos.FocusedItem.Text
			lstrDescri = Me.lvwTipos.FocusedItem.SubItems.Item(1).Text
			With Me.lvwSelecao
				If .Items.Count > 0 Then
					For llngConta = 1 To .Items.Count - 1
						If .Items.Item(llngConta).Text = lstrCodigo Then
							lblnAdiciona = False
						End If
					Next llngConta
				End If
				If lblnAdiciona Then
					Call .Items.Add(lstrCodigo)
					Call .Items.Item(.Items.Count - 1).SubItems.Add(lstrDescri)
				End If
			End With
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: cmdAdiciona_Click" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub
	
	Private Sub cmdAdTodos_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdAdTodos.Click
		Dim llngConta As Integer
		Dim lstrCodigo As String
		Dim lstrDescri As String
		Try
			With Me.lvwTipos
				If .Items.Count > 0 Then
					For llngConta = 1 To .Items.Count - 1

						lstrCodigo = .Items.Item(llngConta).Text
						lstrDescri = .Items.Item(llngConta).SubItems(1).Text
						With Me.lvwSelecao
							Call .Items.Add(lstrCodigo)
							Call .Items.Item(.Items.Count - 1).SubItems.Add(lstrDescri)
						End With
					Next llngConta
				End If
			End With
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: cmdAdTodos_Click" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub
	
	Private Sub cmdCarregar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdCarregar.Click
		Dim lstrSql As String
		'Dim lrstConsulta As ADODB.Recordset
		Dim lstrCodigo As String
		Dim lstrDescri As String
		Try
			'lrstConsulta = New ADODB.Recordset
			With Me.lvwSelecao
				Call .Items.Clear()
				If .Columns.Count <= 1 Then
					Call .Columns.Add("", "Código", CInt(VB6.TwipsToPixelsX(1000)))
					Call .Columns.Add("", "Descrição", CInt(VB6.TwipsToPixelsX(3500)))
				End If
			End With

			If VB6.GetItemString(Me.cmbTipos, Me.cmbTipos.SelectedIndex) <> "(nenhum)" Then

				lstrSql = ""
				lstrSql = lstrSql & "SELECT `sel_lanctoscvdvtx`.`codigo`, " & vbCrLf
				lstrSql = lstrSql & "       `codigos`.`descri` " & vbCrLf
				lstrSql = lstrSql & "  FROM `sel_lanctoscvdvtx`  " & vbCrLf
				lstrSql = lstrSql & "  LEFT JOIN `codigos` ON(`codigos`.`num` = `sel_lanctoscvdvtx`.`codigo`) " & vbCrLf
				lstrSql = lstrSql & " WHERE `sel_lanctoscvdvtx`.`descricao` = '" & VB6.GetItemString(Me.cmbTipos, Me.cmbTipos.SelectedIndex) & "' " & vbCrLf
				lstrSql = lstrSql & " ORDER BY `codigo` ASC "
				objAtuatlizaBancoDados.QUERY_NAME = lstrSql
				dt = repositorioAtualizaBancoDados.Consultar(objAtuatlizaBancoDados)

				If dt.Rows.Count > 0 Then
					For Each row As DataRow In dt.Rows
						lstrCodigo = IIf(IsDBNull(row.Item("codigo")), "0", row.Item("codigo"))
						lstrDescri = IIf(IsDBNull(row.Item("descri")), "", row.Item("descri"))
						With Me.lvwSelecao
							Call .Items.Add(lstrCodigo)
							Call .Items.Item(.Items.Count - 1).SubItems.Add(lstrDescri)
						End With
					Next
				End If

				Me.txtNomeSelecao.Text = VB6.GetItemString(Me.cmbTipos, Me.cmbTipos.SelectedIndex)
			Else
				Me.txtNomeSelecao.Text = ""
			End If
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: cmdCarregar_Click" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub cmdEmitir_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdEmitir.Click
		Dim lstrZona As String
		Dim lstrLivro As String
		Dim lstrConta As String
		Dim lstrNomeA As String
		Dim lstrNomeB As String
		Dim lstrCodigo As String
		Dim lstrDescri As String
		Dim ldblValor As Double
		Dim llngConta As Integer
		Dim ldteIni As Date
		Dim ldteFim As Date
		Dim lstrRef As String
		Dim lstrCriterio As String
		Dim lblnConvenios As Boolean
		Dim lblnTaxas As Boolean
		Dim lstrAux As String
		Dim lstrSql As String

		Try
			'Dim lrstConsulta As ADODB.Recordset
			'lrstConsulta = New ADODB.Recordset
			lblnConvenios = False
			lblnTaxas = False
			'Cria a tabela temp caso ainda nao exista no banco de dados
			Me.lblInfo.Text = " Aguarde criando tabela auxiliar... "
			Call Me.lblInfo.Refresh()

			'lstrSql = ""
			'lstrSql = lstrSql & "CREATE TABLE IF NOT EXISTS `temp_lanctoscvdvtx`    (" & vbCrLf
			'lstrSql = lstrSql & "   `nomemaquina` varchar(255) default '000',        " & vbCrLf
			'lstrSql = lstrSql & "   `zona`        varchar(3)   default '000',        " & vbCrLf
			'lstrSql = lstrSql & "   `livro`       varchar(2)   default '00',         " & vbCrLf
			'lstrSql = lstrSql & "   `conta`       varchar(6)   default '000000',     " & vbCrLf
			'lstrSql = lstrSql & "   `nome`        varchar(85)  default '',           " & vbCrLf
			'lstrSql = lstrSql & "   `endcompleto` varchar(85)  default '',           " & vbCrLf
			'lstrSql = lstrSql & "   `codigo`      varchar(10)  default '000000000',  " & vbCrLf
			'lstrSql = lstrSql & "   `descri`      varchar(180) default '',           " & vbCrLf
			'lstrSql = lstrSql & "   `ref`         varchar(200) default '',           " & vbCrLf
			'lstrSql = lstrSql & "   `valor`       double(12,2) default '0.00',       " & vbCrLf
			'lstrSql = lstrSql & "   `dt_ini`      date         default '0000-00-00', " & vbCrLf
			'lstrSql = lstrSql & "   `dt_fim`      date         default '0000-00-00'  " & vbCrLf
			'lstrSql = lstrSql & " )"
			'Call Executar(lstrSql)

			'Limpa a tabela temp conforme o nome da maquina conectada
			Me.lblInfo.Text = " Aguarde limpando tabela auxiliar... "
			Call Me.lblInfo.Refresh()

			lstrSql = "DELETE FROM AUX_LANCTOSCVDVTX WHERE nomemaquina = """ & My.Computer.Name & """ "
			objExecAtuatlizaBancoDados.QUERY_NAME = lstrSql
			repositorioExecAtualizaBancoDados.Execute(objExecAtuatlizaBancoDados)

			'Seleciona os convenios e diversos
			Me.lblInfo.Text = " Aguarde selecionado convênios e diversos... "
			Call Me.lblInfo.Refresh()

			lstrSql = ""
			lstrSql = lstrSql & "SELECT " & vbCrLf
			lstrSql = lstrSql & "       `convenios`.`zona`,     " & vbCrLf
			lstrSql = lstrSql & "       `convenios`.`livro`,    " & vbCrLf
			lstrSql = lstrSql & "       `convenios`.`conta`,    " & vbCrLf
			'lstrSql = lstrSql & "       `unida`.`nome` AS `nma`," & vbCrLf
			lstrSql = lstrSql & "       `uc_faturamento`.`nome` AS `nmb`," & vbCrLf
			lstrSql = lstrSql & "       `convenios`.`convenio`, " & vbCrLf
			lstrSql = lstrSql & "       `codigos`.`descri`,     " & vbCrLf
			lstrSql = lstrSql & "       `convenios`.`valor`,    " & vbCrLf
			lstrSql = lstrSql & "       `convenios`.`dt_ini`,   " & vbCrLf
			lstrSql = lstrSql & "       `convenios`.`dt_fim`,   " & vbCrLf
			lstrSql = lstrSql & "       `convenios`.`ref`       " & vbCrLf
			lstrSql = lstrSql & "  FROM `convenios` " & vbCrLf
			'lstrSql = lstrSql & "        LEFT JOIN `unida` ON(`unida`.`zona` = `convenios`.`zona` AND `unida`.`livro` = `convenios`.`livro` AND `unida`.`conta` = `convenios`.`conta`) " & vbCrLf
			lstrSql = lstrSql & "        LEFT JOIN `uc_faturamento` ON(`uc_faturamento`.`zona` = `convenios`.`zona` AND `uc_faturamento`.`livro` = `convenios`.`livro` AND `uc_faturamento`.`conta` = `convenios`.`conta`) " & vbCrLf
			lstrSql = lstrSql & "        LEFT JOIN `codigos` ON(`codigos`.`num` = `convenios`.`convenio`) " & vbCrLf
			lstrSql = lstrSql & " WHERE ((`convenios`.`dt_ini` >= '" & VB6.Format(Me.dtpInicial.Value, "yyyy-MM-dd") & "' AND " & vbCrLf
			lstrSql = lstrSql & "         `convenios`.`dt_ini` <= '" & VB6.Format(Me.dtpFinal.Value, "yyyy-MM-dd") & "') OR " & vbCrLf
			lstrSql = lstrSql & "        (`convenios`.`dt_fim` >= '" & VB6.Format(Me.dtpInicial.Value, "yyyy-MM-dd") & "' AND " & vbCrLf
			lstrSql = lstrSql & "         `convenios`.`dt_fim` <= '" & VB6.Format(Me.dtpFinal.Value, "yyyy-MM-dd") & "') OR " & vbCrLf
			lstrSql = lstrSql & "        (`convenios`.`dt_ini` <= '" & VB6.Format(Me.dtpInicial.Value, "yyyy-MM-dd") & "' AND " & vbCrLf
			lstrSql = lstrSql & "         `convenios`.`dt_fim` >= '" & VB6.Format(Me.dtpFinal.Value, "yyyy-MM-dd") & "')) " & vbCrLf

			lstrAux = ""
			With Me.lvwSelecao
				If .Items.Count > 0 Then
					For llngConta = 1 To .Items.Count - 1
						If VB.Left(.Items.Item(llngConta).Text, 4) <> "9040" Then
							lstrAux = lstrAux & "'" & .Items.Item(llngConta).Text & "',"
							lblnConvenios = True
						End If
					Next llngConta
				End If
			End With
			If Len(Trim(lstrAux)) > 0 Then
				lstrSql = lstrSql & "   AND `convenios`.`convenio` IN(" & VB.Left(lstrAux, Len(lstrAux) - 1) & ") "
			End If

			If lblnConvenios Then
				objAtuatlizaBancoDados.QUERY_NAME = lstrSql
				dt = repositorioAtualizaBancoDados.Consultar(objAtuatlizaBancoDados)

				If dt.Rows.Count > 0 Then
					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, dt.Rows.Count, "Convênios")
					'lstrSql = "INSERT INTO AUX_LANCTOSCVDVTX(`nomemaquina`, `zona`, `livro`, `conta`, `nome`, `codigo`, `descri`, `ref`, `valor`, `dt_ini`, `dt_fim`) VALUES " & vbCrLf
					For Each row As DataRow In dt.Rows

						lstrZona = Trim(IIf(IsDBNull(row.Item("zona")), "", row.Item("zona")))
						lstrLivro = Trim(IIf(IsDBNull(row.Item("livro")), "", row.Item("livro")))
						lstrConta = Trim(IIf(IsDBNull(row.Item("conta")), "", row.Item("conta")))
						'lstrNomeA = Trim(IIf(IsDBNull(row.Item("nma")), "", row.Item("nma")))
						lstrNomeA = Trim(IIf(IsDBNull(row.Item("nmb")), "", row.Item("nmb")))
						lstrNomeB = Trim(IIf(IsDBNull(row.Item("nmb")), "", row.Item("nmb")))
						lstrCodigo = Trim(IIf(IsDBNull(row.Item("convenio")), "", row.Item("convenio")))
						lstrDescri = Trim(IIf(IsDBNull(row.Item("descri")), "", row.Item("descri")))
						ldblValor = CDbl(IIf(IsDBNull(row.Item("valor")), "0", row.Item("valor")))
						ldteIni = CDate(IIf(IsDBNull(row.Item("dt_ini")), "1900-01-01", row.Item("dt_ini")))
						ldteFim = CDate(IIf(IsDBNull(row.Item("dt_fim")), "1900-01-01", row.Item("dt_fim")))
						lstrRef = Trim(IIf(IsDBNull(row.Item("ref")), "", row.Item("ref")))

						lstrSql = "INSERT INTO AUX_LANCTOSCVDVTX(`nomemaquina`, `zona`, `livro`, `conta`, `nome`, `codigo`, `descri`, `ref`, `valor`, `dt_ini`, `dt_fim`) VALUES " & vbCrLf
						lstrSql = lstrSql & " (""" & My.Computer.Name & """, "
						lstrSql = lstrSql & " """ & lstrZona & """, "
						lstrSql = lstrSql & " """ & lstrLivro & """, "
						lstrSql = lstrSql & " """ & lstrConta & """, "
						lstrSql = lstrSql & " """ & IIf(lstrNomeB = "", lstrNomeA, lstrNomeB) & """, "
						lstrSql = lstrSql & " """ & lstrCodigo & """, "
						lstrSql = lstrSql & " """ & lstrDescri & """, "
						lstrSql = lstrSql & " """ & lstrRef & """, "
						lstrSql = lstrSql & " """ & Replace(CStr(ldblValor), ",", ".") & """, "
						lstrSql = lstrSql & " """ & VB6.Format(ldteIni, "yyyy-MM-dd") & """, "
						lstrSql = lstrSql & " """ & VB6.Format(ldteFim, "yyyy-MM-dd") & """), " & vbCrLf

						lstrSql = VB.Left(lstrSql, Len(lstrSql) - 4)
						objExecAtuatlizaBancoDados.QUERY_NAME = lstrSql
						repositorioExecAtualizaBancoDados.Execute(objExecAtuatlizaBancoDados)

						Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1,  , "Convênios")


					Next

				Else
					lblnConvenios = False
				End If


			End If

			'Seleciona as taxas
			Me.lblInfo.Text = " Aguarde selecionado taxas... "
			Call Me.lblInfo.Refresh()
			lstrSql = ""
			lstrSql = lstrSql & "SELECT "
			lstrSql = lstrSql & "       `taxas`.`zona`,     " & vbCrLf
			lstrSql = lstrSql & "       `taxas`.`livro`,    " & vbCrLf
			lstrSql = lstrSql & "       `taxas`.`conta`,    " & vbCrLf
			'lstrSql = lstrSql & "       `unida`.`nome` AS `nma`," & vbCrLf
			lstrSql = lstrSql & "       `uc_faturamento`.`nome` AS `nmb`," & vbCrLf
			lstrSql = lstrSql & "       `taxas`.`codigo`,   " & vbCrLf
			lstrSql = lstrSql & "       `codigos`.`descri`, " & vbCrLf
			lstrSql = lstrSql & "       `taxas`.`valor`,    " & vbCrLf
			lstrSql = lstrSql & "       `taxas`.`dt_ini`,   " & vbCrLf
			lstrSql = lstrSql & "       `taxas`.`dt_fim`,   " & vbCrLf
			lstrSql = lstrSql & "       `taxas`.`ref`       " & vbCrLf
			lstrSql = lstrSql & "  FROM `taxas` "
			'lstrSql = lstrSql & "        LEFT JOIN `unida` ON(`unida`.`zona` = `taxas`.`zona` AND `unida`.`livro` = `taxas`.`livro` AND `unida`.`conta` = `taxas`.`conta`) "
			lstrSql = lstrSql & "        LEFT JOIN `uc_faturamento` ON(`uc_faturamento`.`zona` = `taxas`.`zona` AND `uc_faturamento`.`livro` = `taxas`.`livro` AND `uc_faturamento`.`conta` = `taxas`.`conta`) "
			lstrSql = lstrSql & "        LEFT JOIN `codigos` ON(`codigos`.`num` = `taxas`.`codigo`) "
			lstrSql = lstrSql & " WHERE ((`taxas`.`dt_ini` >= '" & VB6.Format(Me.dtpInicial.Value, "yyyy-MM-dd") & "' AND " & vbCrLf
			lstrSql = lstrSql & "         `taxas`.`dt_ini` <= '" & VB6.Format(Me.dtpFinal.Value, "yyyy-MM-dd") & "') OR " & vbCrLf
			lstrSql = lstrSql & "        (`taxas`.`dt_fim` >= '" & VB6.Format(Me.dtpInicial.Value, "yyyy-MM-dd") & "' AND " & vbCrLf
			lstrSql = lstrSql & "         `taxas`.`dt_fim` <= '" & VB6.Format(Me.dtpFinal.Value, "yyyy-MM-dd") & "') OR " & vbCrLf
			lstrSql = lstrSql & "        (`taxas`.`dt_ini` <= '" & VB6.Format(Me.dtpInicial.Value, "yyyy-MM-dd") & "' AND " & vbCrLf
			lstrSql = lstrSql & "         `taxas`.`dt_fim` >= '" & VB6.Format(Me.dtpFinal.Value, "yyyy-MM-dd") & "')) " & vbCrLf

			lstrAux = ""
			With Me.lvwSelecao
				If .Items.Count > 0 Then
					For llngConta = 1 To .Items.Count - 1
						If VB.Left(.Items.Item(llngConta).Text, 4) = "9040" Then
							lstrAux = lstrAux & "'" & .Items.Item(llngConta).Text & "',"
							lblnTaxas = True
						End If
					Next llngConta
				End If
			End With

			If Len(Trim(lstrAux)) > 0 Then
				lstrSql = lstrSql & " AND `taxas`.`codigo` IN(" & VB.Left(lstrAux, Len(lstrAux) - 1) & ") "
			End If

			If lblnTaxas Then
				objAtuatlizaBancoDados.QUERY_NAME = lstrSql
				dt1 = repositorioAtualizaBancoDados.Consultar(objAtuatlizaBancoDados)

				If dt1.Rows.Count > 0 Then
					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, dt1.Rows.Count, "Taxas")

					For Each row1 As DataRow In dt1.Rows

						lstrZona = Trim(IIf(IsDBNull(row1.Item("zona")), "", row1.Item("zona")))
						lstrLivro = Trim(IIf(IsDBNull(row1.Item("livro")), "", row1.Item("livro")))
						lstrConta = Trim(IIf(IsDBNull(row1.Item("conta")), "", row1.Item("conta")))
						'lstrNomeA = Trim(IIf(IsDBNull(row1.Item("nma")), "", row1.Item("nma")))
						lstrNomeA = Trim(IIf(IsDBNull(row1.Item("nmb")), "", row1.Item("nmb")))
						lstrNomeB = Trim(IIf(IsDBNull(row1.Item("nmb")), "", row1.Item("nmb")))
						lstrCodigo = Trim(IIf(IsDBNull(row1.Item("codigo")), "", row1.Item("codigo")))
						lstrDescri = Trim(IIf(IsDBNull(row1.Item("descri")), "", row1.Item("descri")))
						ldblValor = CDbl(IIf(IsDBNull(row1.Item("valor")), "0", row1.Item("valor")))
						ldteIni = CDate(IIf(IsDBNull(row1.Item("dt_ini")), "1900-01-01", row1.Item("dt_ini")))
						ldteFim = CDate(IIf(IsDBNull(row1.Item("dt_fim")), "1900-01-01", row1.Item("dt_fim")))
						lstrRef = Trim(IIf(IsDBNull(row1.Item("ref")), "", row1.Item("ref")))

						lstrSql = "INSERT INTO AUX_LANCTOSCVDVTX(`nomemaquina`, `zona`, `livro`, `conta`, `nome`, `codigo`, `descri`, `ref`, `valor`, `dt_ini`, `dt_fim`) VALUES " & vbCrLf
						lstrSql = lstrSql & " (""" & My.Computer.Name & """, "
						lstrSql = lstrSql & " """ & lstrZona & """, "
						lstrSql = lstrSql & " """ & lstrLivro & """, "
						lstrSql = lstrSql & " """ & lstrConta & """, "
						lstrSql = lstrSql & " """ & IIf(lstrNomeB = "", lstrNomeA, lstrNomeB) & """, "
						lstrSql = lstrSql & " """ & lstrCodigo & """, "
						lstrSql = lstrSql & " """ & lstrDescri & """, "
						lstrSql = lstrSql & " """ & lstrRef & """, "
						lstrSql = lstrSql & " """ & Replace(CStr(ldblValor), ",", ".") & """, "
						lstrSql = lstrSql & " """ & VB6.Format(ldteIni, "yyyy-MM-dd") & """, "
						lstrSql = lstrSql & " """ & VB6.Format(ldteFim, "yyyy-MM-dd") & """), " & vbCrLf

						lstrSql = VB.Left(lstrSql, Len(lstrSql) - 4)
						objExecAtuatlizaBancoDados.QUERY_NAME = lstrSql
						repositorioExecAtualizaBancoDados.Execute(objExecAtuatlizaBancoDados)

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

				Else
					lblnTaxas = False
				End If

			End If

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

			'If lblnConvenios = True Or lblnTaxas = True Then
			'	lstrCriterio = "{temp_lanctoscvdvtx.nomemaquina} = """ & gstrNmEstacao & """ "
			'	With cryRelLactos
			'		.ReportFileName = My.Application.Info.DirectoryPath & "\" & LeConfig("DIR_RPT") & "Lancamentos.rpt"
			'		.DiscardSavedData = True
			'		.WindowState = Crystal.WindowStateConstants.crptMaximized
			'		.WindowControlBox = True
			'		.WindowControls = True
			'		.WindowShowPrintSetupBtn = True
			'		.WindowShowCloseBtn = True
			'		.WindowShowSearchBtn = True
			'		.WindowTitle = "Impressão dos Lançamentos de Convênios, Diversos e Taxas"
			'		.Destination = Crystal.DestinationConstants.crptToWindow
			'		.SelectionFormula = lstrCriterio
			'		.set_Formulas(1, "TituloA = ""Listagem dos lançamentos do período de " & VB6.Format(Me.dtpInicial.value, "dd/mm/yyyy") & " até " & VB6.Format(Me.dtpFinal.value, "dd/mm/yyyy") & """ ")
			'		.set_SortFields(0, "+{temp_lanctoscvdvtx.codigo}")
			'		If Me.opbNome.Checked Then
			'			.set_SortFields(1, "+{temp_lanctoscvdvtx.nome}")
			'		ElseIf Me.opbZlc.Checked Then 
			'			.set_SortFields(1, "+{temp_lanctoscvdvtx.zona}")
			'			.set_SortFields(2, "+{temp_lanctoscvdvtx.livro}")
			'			.set_SortFields(3, "+{temp_lanctoscvdvtx.conta}")
			'		Else
			'			.set_SortFields(1, "+{temp_lanctoscvdvtx.zona}")
			'			.set_SortFields(2, "+{temp_lanctoscvdvtx.livro}")
			'			.set_SortFields(3, "+{temp_lanctoscvdvtx.conta}")
			'			.set_SortFields(4, "+{temp_lanctoscvdvtx.dt_ini}")
			'			.set_SortFields(5, "+{temp_lanctoscvdvtx.dt_fim}")
			'		End If
			'		.Action = 0
			'	End With
			'Else
			'	Call MsgBox("Não foi encontrado nenhum registro, favor verifique seus filtros e emita novamente!", MsgBoxStyle.Information, gstrTituloMensagem)
			'End If
		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 Sub cmdExcluir_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdExcluir.Click
		Dim lstrSql As String
		Try
			lstrSql = ""
			lstrSql = lstrSql & "DELETE FROM `sel_lanctoscvdvtx` " & vbCrLf
			lstrSql = lstrSql & "      WHERE `sel_lanctoscvdvtx`.`descricao` = '" & VB6.GetItemString(Me.cmbTipos, Me.cmbTipos.SelectedIndex) & "' "
			objExecAtuatlizaBancoDados.QUERY_NAME = lstrSql
			repositorioExecAtualizaBancoDados.Execute(objExecAtuatlizaBancoDados)

			Call EnchePreDefinidos()
			Call cmdCarregar_Click(cmdCarregar, New System.EventArgs())
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: cmdExcluir_Click" & "", "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 cmdGravar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdGravar.Click
		Dim lstrSql As String
		Dim llngConta As Integer
		Dim lstrCodigo As String
		'Dim lrstConsulta As ADODB.Recordset
		'lrstConsulta = New ADODB.Recordset
		Try
			If Len(Trim(Me.txtNomeSelecao.Text)) = 0 Then
				Call MsgBox("É necessário informar um nome para gravar esta seleção!", MsgBoxStyle.Exclamation, gstrTituloMensagem)
				Call Me.txtNomeSelecao.Focus()
			Else
				Me.txtNomeSelecao.Text = Replace(Me.txtNomeSelecao.Text, """", "'")
				lstrSql = lstrSql & "SELECT * " & vbCrLf
				lstrSql = lstrSql & "  FROM `sel_lanctoscvdvtx` " & vbCrLf
				lstrSql = lstrSql & " WHERE `sel_lanctoscvdvtx`.`descricao` = '" & Trim(Me.txtNomeSelecao.Text) & "' "
				'Call Conectar(lstrSql, lrstConsulta)
				objAtuatlizaBancoDados.QUERY_NAME = lstrSql
				dt = repositorioAtualizaBancoDados.Consultar(objAtuatlizaBancoDados)

				If dt.Rows.Count > 0 Then
					lstrSql = ""
					lstrSql = lstrSql & "DELETE FROM `sel_lanctoscvdvtx` " & vbCrLf
					lstrSql = lstrSql & "      WHERE `sel_lanctoscvdvtx`.`descricao` = '" & Trim(Me.txtNomeSelecao.Text) & "' "
					objExecAtuatlizaBancoDados.QUERY_NAME = lstrSql
					repositorioExecAtualizaBancoDados.Execute(objExecAtuatlizaBancoDados)
					'Call Executar(lstrSql)
				End If

				With Me.lvwSelecao
					If .Items.Count > 0 Then
						lstrSql = ""
						lstrSql = lstrSql & "INSERT INTO `sel_lanctoscvdvtx` (`descricao`,`codigo`) VALUES"
						For llngConta = 1 To .Items.Count - 1
							lstrCodigo = .Items.Item(llngConta).Text
							lstrSql = lstrSql & " (""" & Trim(Me.txtNomeSelecao.Text) & """, "
							lstrSql = lstrSql & "'" & lstrCodigo & "'), "
						Next llngConta
						objExecAtuatlizaBancoDados.QUERY_NAME = VB.Left(lstrSql, Len(lstrSql) - 2)
						repositorioExecAtualizaBancoDados.Execute(objExecAtuatlizaBancoDados)
						'Call Executar(VB.Left(lstrSql, Len(lstrSql) - 2))
					End If
				End With
				Call EnchePreDefinidos()
				Call cmdCarregar_Click(cmdCarregar, New System.EventArgs())
			End If
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: cmdGravar_Click" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub cmdLimpar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdLimpar.Click
		Try
			With Me.lvwSelecao
				Call .Items.Clear()
				If .Columns.Count <= 1 Then
					Call .Columns.Add("", "Código", CInt(VB6.TwipsToPixelsX(1000)))
					Call .Columns.Add("", "Descrição", CInt(VB6.TwipsToPixelsX(3500)))
				End If
			End With
			Me.txtNomeSelecao.Text = ""
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: cmdLimpar_Click" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub cmdRetira_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdRetira.Click
		Try
			With Me.lvwSelecao
				If .Items.Count > 0 Then
					.Items.RemoveAt((.FocusedItem.Index))
				End If
			End With
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: cmdRetira_Click" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub cmdReTodos_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdReTodos.Click
		Try
			With Me.lvwSelecao
				Call .Items.Clear()
				If .Columns.Count <= 1 Then
					Call .Columns.Add("", "Código", CInt(VB6.TwipsToPixelsX(1000)))
					Call .Columns.Add("", "Descrição", CInt(VB6.TwipsToPixelsX(3500)))
				End If
			End With
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: cmdReTodos_Click" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub frmRelLactoCvTxDv_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		Try

			Me.dtpInicial.Value = CDate("01/" & VB6.Format(Date.Now, "mm/yyyy"))
			Me.dtpFinal.Value = CDate(VB6.Format(UltimoDiaMes(Month(Me.dtpInicial.Value), Year(Me.dtpInicial.Value)), "00") & "/" & VB6.Format(Me.dtpInicial.Value, "mm/yyyy"))
			Call EncheTipos()
			Call EnchePreDefinidos()
			If VB6.GetItemString(Me.cmbTipos, Me.cmbTipos.SelectedIndex) <> "(nenhum)" Then
				Call cmdCarregar_Click(cmdCarregar, New System.EventArgs())
			End If
			sCarregaTabelaAuxiliar()
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: frmRelLactoCvTxDv_Load" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub frmRelLactoCvTxDv_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
		Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
		Try
			If KeyAscii = System.Windows.Forms.Keys.Return Then
				TabEnter(KeyAscii, eventArgs)
			ElseIf KeyAscii = System.Windows.Forms.Keys.Escape Then
				Call cmdFechar_Click(cmdFechar, New System.EventArgs())
			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: frmRelLactoCvTxDv_KeyPress" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub
	
	Private Sub EncheTipos()
		Dim lstrCodigo As String
		Dim lstrDescri As String
		Dim lstrSql As String

		Try

			With Me.lvwTipos
				Call .Items.Clear()
				If .Columns.Count <= 1 Then
					Call .Columns.Add("", "Código", CInt(VB6.TwipsToPixelsX(1000)))
					Call .Columns.Add("", "Descrição", CInt(VB6.TwipsToPixelsX(3500)))
				End If
			End With
			With Me.lvwSelecao
				Call .Items.Clear()
				If .Columns.Count <= 1 Then
					Call .Columns.Add("", "Código", CInt(VB6.TwipsToPixelsX(1000)))
					Call .Columns.Add("", "Descrição", CInt(VB6.TwipsToPixelsX(3500)))
				End If
			End With
			lstrSql = ""
			lstrSql = lstrSql & "SELECT `codigos`.`num`, " & vbCrLf
			lstrSql = lstrSql & "       `codigos`.`descri` " & vbCrLf
			lstrSql = lstrSql & "  FROM `codigos` " & vbCrLf
			lstrSql = lstrSql & " WHERE `codigos`.`num` LIKE '9030%' " & vbCrLf
			lstrSql = lstrSql & "    OR `codigos`.`num` LIKE '9040%' " & vbCrLf
			lstrSql = lstrSql & "    OR `codigos`.`num` LIKE '9070%' " & vbCrLf
			lstrSql = lstrSql & "    OR `codigos`.`num` LIKE '9080%' " & vbCrLf
			lstrSql = lstrSql & "ORDER BY `codigos`.`num` ASC, descri ASC "
			'Call Conectar(lstrSql, lrstConsulta)
			objAtuatlizaBancoDados.QUERY_NAME = lstrSql
			dt = repositorioAtualizaBancoDados.Consultar(objAtuatlizaBancoDados)

			If dt.Rows.Count > 0 Then
				For Each row As DataRow In dt.Rows
					lstrCodigo = IIf(IsDBNull(row.Item("num")), "0", row.Item("num"))
					lstrDescri = IIf(IsDBNull(row.Item("descri")), "", row.Item("descri"))
					With Me.lvwTipos
						Call .Items.Add(lstrCodigo)
						Call .Items.Item(.Items.Count - 1).SubItems.Add(lstrDescri)
					End With
				Next
			End If

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

	End Sub

	Private Sub EnchePreDefinidos()
		Dim lintConta As Short
		Dim lintIndex As Short
		Dim lstrSql As String

		Dim lstrCodigo As String
		Dim lstrDescri As String

		Try

			Call Me.cmbTipos.Items.Clear()
			lintIndex = 0
			lstrSql = ""
			lstrSql = lstrSql & "CREATE TABLE IF NOT EXISTS `sel_lanctoscvdvtx`    (" & vbCrLf
			lstrSql = lstrSql & "   `descricao` varchar(100) default '',          " & vbCrLf
			lstrSql = lstrSql & "   `codigo`    varchar(10)  default '0000000000' " & vbCrLf
			lstrSql = lstrSql & " )"
			objExecAtuatlizaBancoDados.QUERY_NAME = lstrSql
			repositorioExecAtualizaBancoDados.Execute(objExecAtuatlizaBancoDados)

			lstrSql = ""
			lstrSql = lstrSql & "SELECT DISTINCT `sel_lanctoscvdvtx`.`descricao` " & vbCrLf
			lstrSql = lstrSql & "  FROM `sel_lanctoscvdvtx` " & vbCrLf
			lstrSql = lstrSql & " ORDER BY `descricao` ASC "
			objAtuatlizaBancoDados.QUERY_NAME = lstrSql
			dt = repositorioAtualizaBancoDados.Consultar(objAtuatlizaBancoDados)

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

					lstrDescri = IIf(IsDBNull(row.Item("descricao")), "(nenhum)", row.Item("descricao"))
					Call Me.cmbTipos.Items.Add(lstrDescri)

				Next
			Else
				Call Me.cmbTipos.Items.Add("(nenhum)")
			End If
			With Me.cmbTipos
				If Len(Trim(Me.txtNomeSelecao.Text)) > 0 Then
					For lintConta = 0 To .Items.Count - 1
						If VB6.GetItemString(Me.cmbTipos, lintConta) = Me.txtNomeSelecao.Text Then
							lintIndex = lintConta
						End If
					Next lintConta
				End If
				.SelectedIndex = lintIndex
			End With

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

	Private Sub frmRelLactoCvTxDv_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed

		Dim lstrsql As String

		Try

			objExecAtuatlizaBancoDados.QUERY_NAME = "drop table if exists AUX_LANCTOSCVDVTX"
			repositorioExecAtualizaBancoDados.Execute(objExecAtuatlizaBancoDados)


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

	Private Sub lvwSelecao_DoubleClick(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles lvwSelecao.DoubleClick
		Call cmdRetira_Click(cmdRetira, New System.EventArgs())
	End Sub
	Private Sub lvwTipos_DoubleClick(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles lvwTipos.DoubleClick
		Call cmdAdiciona_Click(cmdAdiciona, New System.EventArgs())
	End Sub

	Private Sub sCarregaTabelaAuxiliar()

		Dim lstrsql As String

		Try

			objExecAtuatlizaBancoDados.QUERY_NAME = "drop table if exists AUX_LANCTOSCVDVTX"
			repositorioExecAtualizaBancoDados.Execute(objExecAtuatlizaBancoDados)

			lstrsql = ""
			lstrsql = lstrsql & "CREATE TABLE IF NOT EXISTS `AUX_LANCTOSCVDVTX`    (" & vbCrLf
			lstrsql = lstrsql & "   `NOMEMAQUINA` varchar(100) ,        " & vbCrLf
			lstrsql = lstrsql & "   `ZONA`        varchar(10)   ,        " & vbCrLf
			lstrsql = lstrsql & "   `LIVRO`       varchar(10)   ,         " & vbCrLf
			lstrsql = lstrsql & "   `CONTA`       varchar(10)   ,     " & vbCrLf
			lstrsql = lstrsql & "   `NOME`        varchar(100)  ,           " & vbCrLf
			lstrsql = lstrsql & "   `ENDCOMPLEMENTO` varchar(100)  ,           " & vbCrLf
			lstrsql = lstrsql & "   `CODIGO`      varchar(10)  ,  " & vbCrLf
			lstrsql = lstrsql & "   `DESCRI`      varchar(255) ,           " & vbCrLf
			lstrsql = lstrsql & "   `REF`         varchar(200) ,           " & vbCrLf
			lstrsql = lstrsql & "   `VALOR`       varchar(20),       " & vbCrLf
			lstrsql = lstrsql & "   `DT_INI`      varchar(20), " & vbCrLf
			lstrsql = lstrsql & "   `DT_FIM`      varchar(20)  " & vbCrLf
			lstrsql = lstrsql & " )"
			objExecAtuatlizaBancoDados.QUERY_NAME = lstrsql
			repositorioExecAtualizaBancoDados.Execute(objExecAtuatlizaBancoDados)

		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 bytBLOBData() As Byte

		Dim objEmpresa As New entidadeEmpresa
		Dim repositorioEmpresa As New repositorioEmpresa

		Dim vlist

		Try
			Dim rpt As New ReportDocument()

			If vNomeRPT = "rptRelLancamentos" Then

				dt_3 = New DataTable()
				dt_3.Columns.Add("NOMEMAQUINA", GetType(String))
				dt_3.Columns.Add("ZONA", GetType(String))
				dt_3.Columns.Add("LIVRO", GetType(String))
				dt_3.Columns.Add("CONTA", GetType(String))
				dt_3.Columns.Add("NOME", GetType(String))
				dt_3.Columns.Add("ENDCOMPLEMENTO", GetType(String))
				dt_3.Columns.Add("CODIGO", GetType(String))
				dt_3.Columns.Add("DESCRI", GetType(String))
				dt_3.Columns.Add("REF", GetType(String))
				dt_3.Columns.Add("VALOR", GetType(String))
				dt_3.Columns.Add("DT_INI", GetType(String))
				dt_3.Columns.Add("DT_FIM", GetType(String))

				ds = New DataSet

				lstrsql = ""
				lstrsql = "SELECT * FROM AUX_LANCTOSCVDVTX  WHERE NOMEMAQUINA = '" & My.Computer.Name & "'"
				objAtuatlizaBancoDados.QUERY_NAME = lstrsql
				dt1 = repositorioAtualizaBancoDados.Consultar(objAtuatlizaBancoDados)

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

						dt_3.Rows.Add(dr_1("NOMEMAQUINA"),
										  dr_1("ZONA"),
										  dr_1("LIVRO"),
										  dr_1("CONTA"),
										  dr_1("NOME"),
										  dr_1("ENDCOMPLEMENTO"),
										  dr_1("CODIGO"),
										  dr_1("DESCRI"),
										  dr_1("REF"),
										  dr_1("VALOR"),
										  dr_1("DT_INI"),
										  dr_1("DT_FIM"))
					Next dr_1
				End If


				ds.Tables.Add(dt1)
				ds.Tables(0).TableName = "REL_LANCTOCVTX"

				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 rptRelLancamentos

					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("F_Titulo", "Listagem dos lançamentos do período de " & VB6.Format(Me.dtpInicial.Value, "dd/mm/yyyy") & " até " & VB6.Format(Me.dtpFinal.Value, "dd/mm/yyyy") & "")

					'rpt.SetParameterValue("Tipo", IIf(cmbOpcao.Text = "Geral", "Geral", "Munícipio"))
					'rpt.SetParameterValue("MesAnoRef", Month(dtpMesAno.Value) & "/" & Year(dtpMesAno.Value))
					'rpt.SetParameterValue("NomeRel", "Grupo A - Período de: " & dtpIni.Value & " Até: " & dtpFim.Value)

					frmViewRelatorios.rpt = rpt

					frmViewRelatorios.CRViewer.Zoom((100))
					frmViewRelatorios.Text = "Relatório Consumidor a Vencer"
					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
End Class