Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Imports fatd_faturamento.BLL.Repositorio
Imports fatd_faturamento.ENTIDADE
Friend Class frmGerarDbfModulo6
	Inherits System.Windows.Forms.Form
	'Private icnxDbf As ADODB.Connection
	Private idteBas As Date
	Private iintCtrlPasta As Short ' 0 = Pasta Destino, 1 = Pasta Indra
	
	Private Sub cmdExporta_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdExporta.Click
		Call LigDes(False)
		Me.mfgUcBtIndra.Rows = 0
		Me.mfgUcMtIndra.Rows = 0
		If Me.txtDirIndra.Text <> "" Then
			Call CarregaDbfIndra()
		End If
		If CriaDbf Then
			'Busca informações para flexgrid
			If EncheGrade Then
				If InsereDbf Then
					Call MsgBox("Gravação dos dados nos arquivos DBF concluída com sucesso.", MsgBoxStyle.Information, gstrTituloMensagem)
				Else
					Call MsgBox("Problema na gravação dos dados nos arquivos DBF!," & vbCrLf & "Favor contactar administrador do sistema.", MsgBoxStyle.Information, gstrTituloMensagem)
				End If
			Else
				Call MsgBox("Problema na carga e preparação dos dados!," & vbCrLf & "Favor contactar administrador do sistema.", MsgBoxStyle.Information, gstrTituloMensagem)
			End If
		Else
			Call MsgBox("Problemas na criação dos arquivos DBF!," & vbCrLf & "Favor contactar administrador do sistema.", MsgBoxStyle.Information, gstrTituloMensagem)
		End If
		Call LigDes(True)
	End Sub
	
	Private Sub cmdFechar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdFechar.Click
		'Call frmBdgd.Show()
		Call Me.Close()
	End Sub
	
	Private Sub cmdGerarDbfEQUMED_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdGerarDbfEQUMED.Click
		Dim lstrArq As String
		Dim lstrDbf As String
		Dim llngRow As Integer
		Dim llngCol As Integer
		Dim ldblAux As Double
		Dim dt As DataTable
		Dim objAtualizaBancoDados As New entidadeAtualizaBancoDados
		Dim repositorioAtualizaBancoDados As New repositorioAtualizaBancoDados
		On Error GoTo ErroGerarDbfEQUMED
		'
		'Abre Conexão para pastas DBF ex: C:\[Nome do Directorio]
		'
		Call AbreConexaoDBF(Me.txtDirDestino.Text)
		'
		'Valida se existe na pasta selecionada o arquivo EQU_MED.DBF
		'
		lstrArq = Me.txtDirDestino.Text & IIf(VB.Right(Trim(Me.txtDirDestino.Text), 1) <> "\", "\", "") & "EQU_MED.DBF"
		'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		If Trim(Dir(lstrArq)) <> "" Then
			If MsgBox("Deseja sobrescrever o arquivo EQU_MED?", MsgBoxStyle.YesNo, gstrTituloMensagem) = MsgBoxResult.Yes Then
				'
				'Apaga o arquivo
				'
				Call Kill(lstrArq)
			End If
		End If
		'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		If Trim(Dir(lstrArq)) = "" Then
			'
			'Cria o arquivo EQU_MED.DBF na pasta selecionada
			'
			lstrDbf = ""
			lstrDbf = lstrDbf & "create table `EQU_MED` ("
			lstrDbf = lstrDbf & "`COD_ID` char(20), "
			lstrDbf = lstrDbf & "`DIST` char(2), "
			lstrDbf = lstrDbf & "`TIP_UNID` char(2), "
			lstrDbf = lstrDbf & "`QUAN_FAS` char(4), "
			lstrDbf = lstrDbf & "`ODI` char(99), "
			lstrDbf = lstrDbf & "`TI` char(3), "
			lstrDbf = lstrDbf & "`CM` char(3), "
			lstrDbf = lstrDbf & "`TUC` char(3), "
			lstrDbf = lstrDbf & "`A1` char(2), "
			lstrDbf = lstrDbf & "`A2` char(2), "
			lstrDbf = lstrDbf & "`A3` char(2), "
			lstrDbf = lstrDbf & "`A4` char(2), "
			lstrDbf = lstrDbf & "`A5` char(2), "
			lstrDbf = lstrDbf & "`A6` char(2), "
			lstrDbf = lstrDbf & "`IDUC` char(99), "
			lstrDbf = lstrDbf & "`UAR` char(99), "
			lstrDbf = lstrDbf & "`DAT_IMO` date, "
			lstrDbf = lstrDbf & "`DESC` char(254))"

			objAtualizaBancoDados.QUERY_NAME = lstrDbf
			repositorioAtualizaBancoDados.Execute(objAtualizaBancoDados)

			'	Call icnxDbf.Execute(lstrDbf)
		End If
		'
		' Importa CSV
		'
		lstrArq = Me.txtDirIndra.Text & IIf(VB.Right(Trim(Me.txtDirIndra.Text), 1) <> "\", "\", "") & "EQU_MED.CSV"
		'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		If Trim(Dir(lstrArq)) <> "" Then
			Call CarregaArquivo(lstrArq, (Me.mfgEqu_Med))
			If mfgEqu_Med.Rows > 0 Then
				''Inserir alguns registos na Tabela Persons
				With Me.mfgEqu_Med
					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, .Rows - 1, " exportação Equ Med ")
					For llngRow = 1 To .Rows - 1
						lstrDbf = ""
						lstrDbf = lstrDbf & "insert into EQU_MED values("
						Call BarraProgresso((Me.ProgressBarHistorico), (Me.lblPercHistorico), (Me.lblInfoHistorio), 0, .Cols - 1, "")
						For llngCol = 0 To .Cols - 1
							lstrDbf = lstrDbf & "'" & .get_TextMatrix(llngRow, llngCol) & "',"
							Call BarraProgresso((Me.ProgressBarHistorico), (Me.lblPercHistorico), (Me.lblInfoHistorio), 1,  , "")
						Next llngCol
						lstrDbf = VB.Left(lstrDbf, CInt(Len(lstrDbf)) - 1) & ")"

						objAtualizaBancoDados.QUERY_NAME = lstrDbf
						repositorioAtualizaBancoDados.Execute(objAtualizaBancoDados)

						Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1,  , " exportação Equ Med ")
					Next llngRow
				End With
			End If
		End If
ErroGerarDbfEQUMED:
		'If icnxDbf.Errors.Count > 0 Then
		'Call MsgBox(icnxDbf.Errors.Item(0).Number & " - " & icnxDbf.Errors.Item(0).NativeError & " - " & icnxDbf.Errors.Item(0).Description, MsgBoxStyle.Exclamation, gstrTituloMensagem)
		'Else
		'If Err.Number > 0 Then
		'Call MsgBox(Err.Number & " - " & Err.Description, MsgBoxStyle.Exclamation, gstrTituloMensagem)
		'End If
		'End If
		'If icnxDbf.State = ADODB.ObjectStateEnum.adStateOpen Then icnxDbf.Close()
	End Sub
	
	Private Sub cmdGerarDbfEquTraMed_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdGerarDbfEquTraMed.Click
		Dim lstrArq As String
		Dim lstrDbf As String
		Dim llngRow As Integer
		Dim llngCol As Integer
		Dim ldblAux As Double
		Dim dt As DataTable
		Dim objatualizaBancoDados As New entidadeAtualizaBancoDados
		Dim repositorioAtualizaBancoDados As New repositorioAtualizaBancoDados

		On Error GoTo ErroGerarDbfEQUTRAMED
		'
		'Abre Conexão para pastas DBF ex: C:\[Nome do Directorio]
		'
		Call AbreConexaoDBF(Me.txtDirDestino.Text)
		'
		'Valida se existe na pasta selecionada o arquivo EQU_TRA.DBF
		'
		lstrArq = Me.txtDirDestino.Text & IIf(VB.Right(Trim(Me.txtDirDestino.Text), 1) <> "\", "\", "") & "EQU_TRA.DBF"
		'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		If Trim(Dir(lstrArq)) <> "" Then
			If MsgBox("Deseja sobrescrever o arquivo EQU_TRA?", MsgBoxStyle.YesNo, gstrTituloMensagem) = MsgBoxResult.Yes Then
				'
				'Apaga o arquivo
				'
				Call Kill(lstrArq)
			End If
		End If
		'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		If Trim(Dir(lstrArq)) = "" Then
			'
			'Cria o arquivo EQU_MED.DBF na pasta selecionada
			'
			lstrDbf = ""
			lstrDbf = lstrDbf & "create table `EQU_TRA` ("
			lstrDbf = lstrDbf & "`COD_ID` char(20), "
			lstrDbf = lstrDbf & "`SUB` char(20), "
			lstrDbf = lstrDbf & "`DIST` char(2), "
			lstrDbf = lstrDbf & "`TIP_UNID` char(2), "
			lstrDbf = lstrDbf & "`ODI` char(99), "
			lstrDbf = lstrDbf & "`TI` char(3), "
			lstrDbf = lstrDbf & "`CM` char(3), "
			lstrDbf = lstrDbf & "`TUC` char(3), "
			lstrDbf = lstrDbf & "`A1` char(2), "
			lstrDbf = lstrDbf & "`A2` char(2), "
			lstrDbf = lstrDbf & "`A3` char(2), "
			lstrDbf = lstrDbf & "`A4` char(2), "
			lstrDbf = lstrDbf & "`A5` char(2), "
			lstrDbf = lstrDbf & "`A6` char(2), "
			lstrDbf = lstrDbf & "`IDUC` char(99), "
			lstrDbf = lstrDbf & "`UAR` char(99), "
			lstrDbf = lstrDbf & "`DAT_IMO` date, "
			lstrDbf = lstrDbf & "`DESC` char(254))"

			objAtualizaBancoDados.QUERY_NAME = lstrDbf
			repositorioAtualizaBancoDados.Execute(objAtualizaBancoDados)
		End If
		'
		' Importa CSV
		'
		lstrArq = Me.txtDirIndra.Text & IIf(VB.Right(Trim(Me.txtDirIndra.Text), 1) <> "\", "\", "") & "EQU_TRA_MED.CSV"
		'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		If Trim(Dir(lstrArq)) <> "" Then
			Call CarregaArquivo(lstrArq, (Me.mfgEqu_Tra_Med))
			If Me.mfgEqu_Tra_Med.Rows > 0 Then
				''Inserir alguns registos na Tabela Persons
				With Me.mfgEqu_Tra_Med
					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, .Rows - 1, " exportação Equ Tra Med ")
					For llngRow = 1 To .Rows - 1
						lstrDbf = ""
						lstrDbf = lstrDbf & "insert into EQU_TRA values("
						Call BarraProgresso((Me.ProgressBarHistorico), (Me.lblPercHistorico), (Me.lblInfoHistorio), 0, .Cols - 1, "")
						For llngCol = 0 To .Cols - 1
							lstrDbf = lstrDbf & "'" & .get_TextMatrix(llngRow, llngCol) & "',"
							Call BarraProgresso((Me.ProgressBarHistorico), (Me.lblPercHistorico), (Me.lblInfoHistorio), 1,  , "")
						Next llngCol
						lstrDbf = VB.Left(lstrDbf, CInt(Len(lstrDbf)) - 1) & ")"

						objAtualizaBancoDados.QUERY_NAME = lstrDbf
						repositorioAtualizaBancoDados.Execute(objAtualizaBancoDados)

						Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1,  , " exportação Equ Tra Med ")
					Next llngRow
				End With
			End If
		End If
ErroGerarDbfEQUTRAMED:
		'If icnxDbf.Errors.Count > 0 Then
		'Call MsgBox(icnxDbf.Errors.Item(0).Number & " - " & icnxDbf.Errors.Item(0).NativeError & " - " & icnxDbf.Errors.Item(0).Description, MsgBoxStyle.Exclamation, gstrTituloMensagem)
		'Else
		'If Err.Number > 0 Then
		'Call MsgBox(Err.Number & " - " & Err.Description, MsgBoxStyle.Exclamation, gstrTituloMensagem)
		'End If
		'End If
		'If icnxDbf.State = ADODB.ObjectStateEnum.adStateOpen Then icnxDbf.Close()

	End Sub

	Private Sub cmdPasta_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdPasta.Click
		fbdgeradbf.ShowDialog()
		txtDirDestino.Text = fbdgeradbf.SelectedPath()
		'iintCtrlPasta = 0
		'With Me.cmdPasta
		'Me.Enabled = False
		'frmDir.frmQuemChama = Me
		'frmDir.intTipo = 1
		'Call frmDir.SetBounds(VB6.TwipsToPixelsX(VB6.PixelsToTwipsX(Me.Left) + VB6.PixelsToTwipsX(.Left) + 400), VB6.TwipsToPixelsY(VB6.PixelsToTwipsY(Me.Top) + VB6.PixelsToTwipsY(.Top) + 400), 0, 0, Windows.Forms.BoundsSpecified.X Or Windows.Forms.BoundsSpecified.Y)
		'Call frmDir.ShowDialog()
		'End With
	End Sub

	Private Sub cmdPastaIndra_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdPastaIndra.Click
		fbdgeradbf.ShowDialog()
		txtDirIndra.Text = fbdgeradbf.SelectedPath()
		'iintCtrlPasta = 1
		'With Me.cmdPastaIndra
		'Me.Enabled = False
		'frmDir.frmQuemChama = Me
		'frmDir.intTipo = 1
		'Call frmDir.SetBounds(VB6.TwipsToPixelsX(VB6.PixelsToTwipsX(Me.Left) + VB6.PixelsToTwipsX(.Left) + 400), VB6.TwipsToPixelsY(VB6.PixelsToTwipsY(Me.Top) + VB6.PixelsToTwipsY(.Top) + 400), 0, 0, Windows.Forms.BoundsSpecified.X Or Windows.Forms.BoundsSpecified.Y)
		'Call frmDir.ShowDialog()
		'End With
	End Sub

	Private Sub dtpAno_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles dtpAno.Leave
		'UPGRADE_WARNING: Couldn't resolve default property of object Me.dtpAno.value. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		idteBas = Me.dtpAno.value
		idteBas = CDate("31/12/" & VB6.Format(idteBas, "YYYY"))
		Me.dtpAno.value = idteBas
	End Sub
	
	Private Sub frmGerarDbfModulo6_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
		Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
		If KeyAscii = System.Windows.Forms.Keys.Escape Then
			If Me.cmdFechar.Enabled Then
				Call cmdFechar_Click(cmdFechar, New System.EventArgs())
			End If
		ElseIf KeyAscii = System.Windows.Forms.Keys.Return Then
			TabEnter(KeyAscii, eventArgs)
		End If
		eventArgs.KeyChar = Chr(KeyAscii)
		If KeyAscii = 0 Then
			eventArgs.Handled = True
		End If
	End Sub
	
	Private Sub frmGerarDbfModulo6_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		'Call CenterForm(Me)
		iintCtrlPasta = 0
		'icnxDbf = CreateObject("ADODB.Connection")
		Me.txtDir.Text = GetConfig("dir.sorteio")
		If Trim(Me.txtDir.Text) = "-1" Or Trim(Me.txtDir.Text) = "" Then Me.txtDir.Text = My.Application.Info.DirectoryPath
		idteBas = BuscaDataBD
		idteBas = CDate("31/12/" & VB6.Format(idteBas, "YYYY"))
		Me.dtpAno.value = idteBas
		Me.txtDirDestino.Text = Me.txtDir.Text
	End Sub
	
	Private Sub AbreConexaoDBF(ByVal astrCaminhoDBF As String)
		'Call icnxDbf.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & astrCaminhoDBF & ";Extended Properties=""DBASE IV;"";")
	End Sub
	
	Private Sub frmGerarDbfModulo6_FormClosed(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
		'UPGRADE_NOTE: Object icnxDbf may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
		'icnxDbf = Nothing
	End Sub
	
	Private Function CarregaDbfIndra() As Boolean
		Dim lstrArq As String
		Dim lstrCod As String
		Dim lstrRam As String
		Dim lstrDis As String
		Dim lstrFas As String
		Dim lstrGru As String
		Dim lstrSit As String
		Dim ldblPot As Double

		Dim lstrPnc As String
		Dim lstrSdm As String
		Dim lblnRet As Boolean
		Dim lstrDbf As String
		Dim llngRec As Integer
		Dim dt As DataTable
		Dim dt2 As DataTable
		Dim objatualizaBancoDados As New entidadeAtualizaBancoDados
		Dim repositorioAtualizaBancoDados As New repositorioAtualizaBancoDados

		lblnRet = False
		On Error GoTo ErroCarregaDbfIndra
		'
		'Abre Conexão para pastas DBF ex: C:\[Nome do Directorio]
		'
		Call AbreConexaoDBF(Me.txtDirIndra.Text)
		'
		'Valida se existe na pasta selecionada o arquivo UC_BT.DBF
		'
		lstrArq = Me.txtDirIndra.Text & IIf(VB.Right(Trim(Me.txtDirIndra.Text), 1) <> "\", "\", "") & "UC_BT.DBF"
		'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		If Trim(Dir(lstrArq)) <> "" Then
			lstrDbf = "SELECT * FROM [UC_BT#DBF] ORDER BY COD_ID ASC"

			objatualizaBancoDados.QUERY_NAME = lstrDbf
			dt = repositorioAtualizaBancoDados.Consultar(objatualizaBancoDados)

			If dt.Rows.Count > 0 Then
				'		'
				'		'Conta os registros pois arquivo DBF não tem suporte ao RecordCount
				'		'
				'		Call .MoveFirst()
				'		While Not .EOF
				'			llngRec = llngRec + 1
				'			Call .MoveNext()
				'		End While
				'		Call .MoveFirst()
				'		If llngRec > 0 Then
				'			With Me.mfgUcBtIndra
				'				.Redraw = False
				'				.Rows = 0
				'				.Cols = 2
				'				Call .Clear()
				'			End With
				Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, llngRec, " Carregando DBF Indra UC_BT ")
				'			While Not .EOF
				'				'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
				'				lstrCod = Trim(IIf(IsDbNull(.Fields("COD_ID").value), "", .Fields("COD_ID").value))
				'				'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
				'				lstrRam = Trim(IIf(IsDbNull(.Fields("RAMAL").value), "", .Fields("RAMAL").value))

				'				'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
				'				lstrDis = Trim(IIf(IsDbNull(.Fields("DIST").value), "", .Fields("DIST").value))
				'				'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
				'				lstrFas = Trim(IIf(IsDbNull(.Fields("FAS_CON").value), "", .Fields("FAS_CON").value))
				'				'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
				'				lstrGru = Trim(IIf(IsDbNull(.Fields("GRU_TEN").value), "", .Fields("GRU_TEN").value))
				'				'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
				'				lstrSit = Trim(IIf(IsDbNull(.Fields("SIT_ATIV").value), "", .Fields("SIT_ATIV").value))
				'				'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
				'				ldblPot = CDbl(IIf(IsDbNull(.Fields("POT_INST").value), 0, .Fields("POT_INST").value))


				With Me.mfgUcBtIndra
					.Rows = .Rows + 1
					.set_TextMatrix(.Rows - 1, 0, lstrCod)
					.set_TextMatrix(.Rows - 1, 1, lstrRam)
					.set_TextMatrix(.Rows - 1, 3, lstrDis)
					.set_TextMatrix(.Rows - 1, 5, lstrFas)
					.set_TextMatrix(.Rows - 1, 9, lstrSit)
					.set_TextMatrix(.Rows - 1, 11, ldblPot)
				End With
				'				Call .MoveNext()
				'				Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1,  , " Carregando DBF Indra UC_BT ")
				'			End While
				Me.mfgUcBtIndra.Redraw = True
				'		End If
				'	
			End If
		Else
				Call MsgBox("O arquivo UC_BT.DBF da Indra não se encontra no local especificado!", MsgBoxStyle.Critical, gstrTituloMensagem)
		End If
		'
		'Valida se existe na pasta selecionada o arquivo UC_MT.DBF
		'
		lstrArq = Me.txtDirIndra.Text & IIf(VB.Right(Trim(Me.txtDirIndra.Text), 1) <> "\", "\", "") & "UC_MT.DBF"
		'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		If Trim(Dir(lstrArq)) <> "" Then
			lstrDbf = "SELECT * FROM [UC_MT#DBF] ORDER BY COD_ID ASC"

			objatualizaBancoDados.QUERY_NAME = lstrDbf
			dt2 = repositorioAtualizaBancoDados.Consultar(objatualizaBancoDados)

			'With lrstAux
			'	If .State = ADODB.ObjectStateEnum.adStateOpen Then
			'		'
			'		'Conta os registros pois arquivo DBF não tem suporte ao RecordCount
			'		'
			'		Call .MoveFirst()
			'		While Not .EOF
			'			llngRec = llngRec + 1
			'			Call .MoveNext()
			'		End While
			'		Call .MoveFirst()
			'		If llngRec > 0 Then
			'			With Me.mfgUcMtIndra
			'				.Redraw = False
			'				.Rows = 0
			'				.Cols = 3
			'				Call .Clear()
			'			End With
			'			Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, llngRec, " Carregando DBF Indra UC_MT ")
			'			While Not .EOF
			'				'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
			'				lstrCod = Trim(IIf(IsDbNull(.Fields("COD_ID").value), "", .Fields("COD_ID").value))
			'				'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
			'				lstrPnc = Trim(IIf(IsDbNull(.Fields("PN_CON").value), "", .Fields("PN_CON").value))
			'				'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
			'				lstrSdm = Trim(IIf(IsDbNull(.Fields("SDMT_CON").value), "", .Fields("SDMT_CON").value))
			'				'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
			'				lstrDis = Trim(IIf(IsDbNull(.Fields("DIST").value), "", .Fields("DIST").value))
			'				'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
			'				lstrFas = Trim(IIf(IsDbNull(.Fields("FAS_CON").value), "", .Fields("FAS_CON").value))
			'				'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
			'				lstrGru = Trim(IIf(IsDbNull(.Fields("GRU_TEN").value), "", .Fields("GRU_TEN").value))
			'				'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
			'				lstrSit = Trim(IIf(IsDbNull(.Fields("SIT_ATIV").value), "", .Fields("SIT_ATIV").value))
			'				'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
			'				ldblPot = CDbl(IIf(IsDbNull(.Fields("POT_INST").value), 0, .Fields("POT_INST").value))
			'				With Me.mfgUcMtIndra
			'					.Rows = .Rows + 1
			'					.set_TextMatrix(.Rows - 1, 0, lstrCod)
			'					.set_TextMatrix(.Rows - 1, 1, lstrPnc)
			'					.set_TextMatrix(.Rows - 1, 2, lstrSdm)
			'					.set_TextMatrix(.Rows - 1, 7, lstrDis)
			'					.set_TextMatrix(.Rows - 1, 9, lstrFas)
			'					.set_TextMatrix(.Rows - 1, 13, lstrSit)
			'					.set_TextMatrix(.Rows - 1, 15, ldblPot)
			'				End With
			'				Call .MoveNext()
			'				Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1,  , " Carregando DBF Indra UC_MT ")
			'			End While
			'			Me.mfgUcMtIndra.Redraw = True
			'		End If
			'		Call .Close()
			'	End If
			'End With
		Else
			Call MsgBox("O arquivo UC_MT.DBF da Indra não se encontra no local especificado!", MsgBoxStyle.Critical, gstrTituloMensagem)
		End If
ErroCarregaDbfIndra:
		'If icnxDbf.Errors.Count > 0 Then
		'Call MsgBox(icnxDbf.Errors.Item(0).Number & " - " & icnxDbf.Errors.Item(0).NativeError & " - " & icnxDbf.Errors.Item(0).Description, MsgBoxStyle.Exclamation, gstrTituloMensagem)
		'lblnRet = False
		'Else
		'If Err.Number > 0 Then
		'Call MsgBox(Err.Number & " - " & Err.Description, MsgBoxStyle.Exclamation, gstrTituloMensagem)
		'lblnRet = False
		'Else
		'lblnRet = True
		'End If
		'End If
		'UPGRADE_NOTE: Object lrstAux may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
		'lrstAux = Nothing
		'If icnxDbf.State = ADODB.ObjectStateEnum.adStateOpen Then icnxDbf.Close()
		CarregaDbfIndra = lblnRet
	End Function
	
	Private Function CriaDbf() As Boolean
		Dim lblnRet As Boolean
		Dim lstrDbf As String
		Dim lstrArq As String
		Dim objAtualizaBancoDados As New entidadeAtualizaBancoDados
		Dim repositorioAtualizaBancoDados As New repositorioAtualizaBancoDados

		Try

			'On Error GoTo ErroCriaDbf
			'
			'Abre Conexão para pastas DBF ex: C:\[Nome do Directorio]
			'
			Call AbreConexaoDBF(Me.txtDirDestino.Text)
			'
			'Valida se existe na pasta selecionada o arquivo UC_BT.DBF
			'
			lblnRet = False
			lstrArq = Me.txtDirDestino.Text & IIf(VB.Right(Trim(Me.txtDirDestino.Text), 1) <> "\", "\", "") & "UC_BT.DBF"
			'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
			If Trim(Dir(lstrArq)) <> "" Then
				If MsgBox("Deseja sobrescrever o arquivo UC_BT?", MsgBoxStyle.YesNo, gstrTituloMensagem) = MsgBoxResult.Yes Then
					'
					'Apaga o arquivo
					'
					Call Kill(lstrArq)
				End If
			End If
			'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
			If Trim(Dir(lstrArq)) = "" Then
				'
				'Cria o arquivo UC_BT.DBF na pasta selecionada
				'
				lstrDbf = ""
				lstrDbf = lstrDbf & "create table `UC_BT` ("
				lstrDbf = lstrDbf & "`COD_ID` char(20), "
				lstrDbf = lstrDbf & "`RAMAL` char(20), "
				lstrDbf = lstrDbf & "`MED` char(20), "
				lstrDbf = lstrDbf & "`DIST` char(2), "
				lstrDbf = lstrDbf & "`CLA_SUB` char(4), "
				lstrDbf = lstrDbf & "`FAS_CON` char(4), "
				lstrDbf = lstrDbf & "`GRU_TEN` char(2), "
				lstrDbf = lstrDbf & "`TEN_FORN` char(2), "
				lstrDbf = lstrDbf & "`GRU_TAR` char(4), "
				lstrDbf = lstrDbf & "`SIT_ATIV` char(2), "
				lstrDbf = lstrDbf & "`DATA_CON` date, "
				lstrDbf = lstrDbf & "`POT_INST` decimal(5), "
				lstrDbf = lstrDbf & "`ENE_01` decimal(5), "
				lstrDbf = lstrDbf & "`ENE_02` decimal(5), "
				lstrDbf = lstrDbf & "`ENE_03` decimal(5), "
				lstrDbf = lstrDbf & "`ENE_04` decimal(5), "
				lstrDbf = lstrDbf & "`ENE_05` decimal(5), "
				lstrDbf = lstrDbf & "`ENE_06` decimal(5), "
				lstrDbf = lstrDbf & "`ENE_07` decimal(5), "
				lstrDbf = lstrDbf & "`ENE_08` decimal(5), "
				lstrDbf = lstrDbf & "`ENE_09` decimal(5), "
				lstrDbf = lstrDbf & "`ENE_10` decimal(5), "
				lstrDbf = lstrDbf & "`ENE_11` decimal(5), "
				lstrDbf = lstrDbf & "`ENE_12` decimal(5), "
				lstrDbf = lstrDbf & "`DESC` char(254))"

				objAtualizaBancoDados.QUERY_NAME = lstrDbf
				repositorioAtualizaBancoDados.Execute(objAtualizaBancoDados)

			End If
			'
			'Valida se existe na pasta selecionada o arquivo UC_MT.DBF
			'
			lstrArq = Me.txtDirDestino.Text & IIf(VB.Right(Trim(Me.txtDirDestino.Text), 1) <> "\", "\", "") & "UC_MT.DBF"
			'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
			If Trim(Dir(lstrArq)) <> "" Then
				If MsgBox("Deseja sobrescrever o arquivo UC_MT?", MsgBoxStyle.YesNo, gstrTituloMensagem) = MsgBoxResult.Yes Then
					'
					'Apaga o arquivo
					'
					Call Kill(lstrArq)
				End If
			End If
			'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
			If Trim(Dir(lstrArq)) = "" Then
				lstrDbf = ""
				lstrDbf = lstrDbf & "create table `UC_MT` ("
				lstrDbf = lstrDbf & "`COD_ID` char(20), "
				lstrDbf = lstrDbf & "`PN_CON` char(20), "
				lstrDbf = lstrDbf & "`SDMT_CON` char(20), "
				lstrDbf = lstrDbf & "`MED_01` char(20), "
				lstrDbf = lstrDbf & "`MED_02` char(20), "
				lstrDbf = lstrDbf & "`TP` char(20), "
				lstrDbf = lstrDbf & "`TC` char(20), "
				lstrDbf = lstrDbf & "`DIST` char(2), "
				lstrDbf = lstrDbf & "`CLA_SUB` char(4), "
				lstrDbf = lstrDbf & "`FAS_CON` char(4), "
				lstrDbf = lstrDbf & "`GRU_TEN` char(2), "
				lstrDbf = lstrDbf & "`TEN_FORN` char(2), "
				lstrDbf = lstrDbf & "`GRU_TAR` char(4), "
				lstrDbf = lstrDbf & "`SIT_ATIV` char(2), "
				lstrDbf = lstrDbf & "`DATA_CON` Date, "
				lstrDbf = lstrDbf & "`POT_INST` decimal(5), "
				lstrDbf = lstrDbf & "`LIV` char(1), "
				lstrDbf = lstrDbf & "`DEM_01` decimal(5), "
				lstrDbf = lstrDbf & "`DEM_02` decimal(5), "
				lstrDbf = lstrDbf & "`DEM_03` decimal(5), "
				lstrDbf = lstrDbf & "`DEM_04` decimal(5), "
				lstrDbf = lstrDbf & "`DEM_05` decimal(5), "
				lstrDbf = lstrDbf & "`DEM_06` decimal(5), "
				lstrDbf = lstrDbf & "`DEM_07` decimal(5), "
				lstrDbf = lstrDbf & "`DEM_08` decimal(5), "
				lstrDbf = lstrDbf & "`DEM_09` decimal(5), "
				lstrDbf = lstrDbf & "`DEM_10` decimal(5), "
				lstrDbf = lstrDbf & "`DEM_11` decimal(5), "
				lstrDbf = lstrDbf & "`DEM_12` decimal(5), "
				lstrDbf = lstrDbf & "`ENE_01` decimal(5), "
				lstrDbf = lstrDbf & "`ENE_02` decimal(5), "
				lstrDbf = lstrDbf & "`ENE_03` decimal(5), "
				lstrDbf = lstrDbf & "`ENE_04` decimal(5), "
				lstrDbf = lstrDbf & "`ENE_05` decimal(5), "
				lstrDbf = lstrDbf & "`ENE_06` decimal(5), "
				lstrDbf = lstrDbf & "`ENE_07` decimal(5), "
				lstrDbf = lstrDbf & "`ENE_08` decimal(5), "
				lstrDbf = lstrDbf & "`ENE_09` decimal(5), "
				lstrDbf = lstrDbf & "`ENE_10` decimal(5), "
				lstrDbf = lstrDbf & "`ENE_11` decimal(5), "
				lstrDbf = lstrDbf & "`ENE_12` decimal(5), "
				lstrDbf = lstrDbf & "`DESC` char(254))"

				objAtualizaBancoDados.QUERY_NAME = lstrDbf
				repositorioAtualizaBancoDados.Execute(objAtualizaBancoDados)

			End If
			'
			lblnRet = True
			CriaDbf = lblnRet

		Catch ex As Exception
			lblnRet = False
			CriaDbf = lblnRet
			Exit Function
		End Try

	End Function
	
	Private Function EncheGrade() As Boolean
		'
		Dim lstrGru As String
		Dim lstrZon As String
		Dim lstrLiv As String
		Dim lstrCon As String
		Dim llngUId As Integer
		Dim llngCId As Integer
		Dim lstrSit As String
		Dim lstrMed As String
		Dim lstrFLi As String
		Dim lstrTen As String
		Dim lstrCla As String
		Dim lstrFCl As String
		Dim ldblCrg As Double
		Dim lstrIte As String
		Dim ldteLig As Date
		Dim ldteRel As Date
		Dim ldteEnt As Date
		Dim lstrRam As String
		Dim lstrPnc As String
		Dim lstrSdm As String
		Dim lblnFat As Boolean
		Dim lblnCon As Boolean
		Dim lstrCod As String
		'UPGRADE_WARNING: Lower bound of array ldblEne was changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="0F1C9BE1-AF9D-476E-83B1-17D43BECFF20"'
		Dim ldblEne(12) As Double
		'UPGRADE_WARNING: Lower bound of array ldblDem was changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="0F1C9BE1-AF9D-476E-83B1-17D43BECFF20"'
		Dim ldblDem(12) As Double
		'
		Dim lintAux As Short
		Dim lblnRet As Boolean
		Dim lstrSql As String
		Dim dt As DataTable
		Dim dt2 As DataTable
		Dim objAtualizaBancoDados As New entidadeAtualizaBancoDados
		Dim repositorioAtualizaBancoDados As New repositorioAtualizaBancoDados

		Try

			Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
			lblnRet = False
			'
			'Grupo B
			'
			lstrSql = ""
			lstrSql = lstrSql & "SELECT * "
			lstrSql = lstrSql & "  FROM `unidb` "
			lstrSql = lstrSql & " WHERE `unidb`.`situacao` <> 'N' "

			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)
				Me.mfgUcBt.Redraw = False
				Me.mfgUcBt.Rows = 0
				Me.mfgUcBt.Cols = 25
				Call Me.mfgUcBt.Clear()
				lblnRet = True
				For Each row As DataRow In dt.Rows
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrZon = Trim(IIf(IsDBNull(row.Item("zona")), "", row.Item("zona")))
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrLiv = Trim(IIf(IsDBNull(row.Item("livro")), "", row.Item("livro")))
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrCon = Trim(IIf(IsDBNull(row.Item("conta")), "", row.Item("conta")))
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					llngUId = CInt(IIf(IsDBNull(row.Item("unid_id")), "0", row.Item("unid_id")))
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					llngCId = CInt(IIf(IsDBNull(row.Item("cons_id")), "0", row.Item("cons_id")))
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrMed = Trim(IIf(IsDBNull(row.Item("num")), "", row.Item("num")))
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrSit = Trim(IIf(IsDBNull(row.Item("situacao")), "L", row.Item("situacao")))
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrFLi = Trim(IIf(IsDBNull(row.Item("faselig")), "0", row.Item("faselig")))
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrTen = Trim(IIf(IsDBNull(row.Item("tensao")), "", row.Item("tensao")))
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrCla = Trim(IIf(IsDBNull(row.Item("clas")), "", row.Item("clas")))
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					ldblCrg = (CDbl(IIf(IsDBNull(row.Item("carga")), "0", row.Item("carga"))) / 1000)
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					ldteLig = CDate(IIf(IsDBNull(row.Item("data_ligacao")), "1900-01-01", row.Item("data_ligacao")))
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					ldteRel = CDate(IIf(IsDBNull(row.Item("data_religacao")), "1900-01-01", row.Item("data_religacao")))
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					ldteEnt = CDate(IIf(IsDBNull(row.Item("data_entrada")), "1900-01-01", row.Item("data_entrada")))
					If ldteLig > idteBas Then
						lblnFat = VerificaSeExisteFatura(lstrZon, lstrLiv, lstrCon, llngUId, "B")
					Else
						lblnFat = True
					End If
					If ldteLig <=
						idteBas Or lblnFat Then
						Call BarraProgresso((Me.ProgressBarHistorico), (Me.lblPercHistorico), (Me.lblInfoHistorio), 0, 12, " histórico ")
						For lintAux = 1 To 12
							ldblEne(lintAux) = BuscaConsumo("B", llngUId, VB6.Format(lintAux, "00") & "/" & VB6.Format(idteBas, "YYYY"),  , 0, lstrFCl)
							Call BarraProgresso((Me.ProgressBarHistorico), (Me.lblPercHistorico), (Me.lblInfoHistorio), 1,  , " histórico ")
						Next lintAux
						If gstrEmpresa_Sigla = "EFLUL" Then
							lstrCod = VB6.Format(lstrLiv & VB.Right(lstrCon, 4), "000000") 'COD_ID
						Else
							lstrCod = VB6.Format(lstrZon & lstrLiv & lstrCon, "00000000000") 'COD_ID
						End If
						lstrRam = ""
						With Me.mfgUcBtIndra
							If .Rows > 0 Then
								lblnCon = BuscaDadosIndra((Me.mfgUcBtIndra), lstrCod, "BT", lstrRam, lstrFLi)
							Else
								lblnCon = False
							End If
						End With
						If lblnCon Then
							With Me.mfgUcBt
								.Rows = .Rows + 1
								.set_TextMatrix(.Rows - 1, 0, lstrCod)
								.set_TextMatrix(.Rows - 1, 1, lstrRam) 'RAMAL
								.set_TextMatrix(.Rows - 1, 2, lstrMed) 'MED
								.set_TextMatrix(.Rows - 1, 3, BuscaTDIST) 'DIST
								.set_TextMatrix(.Rows - 1, 4, BuscaTCLA_SUBCLA(lstrCla)) 'CLA_SUB
								.set_TextMatrix(.Rows - 1, 5, lstrFLi) 'FAS_CON
								.set_TextMatrix(.Rows - 1, 6, "BT") 'GRU_TEN
								.set_TextMatrix(.Rows - 1, 7, BuscaTTEN(lstrTen)) 'TEN_FORN
								.set_TextMatrix(.Rows - 1, 8, BuscaTGRU_TAR(lstrCla, "B", lstrFCl)) 'GRU_TAR
								If lstrSit = "L" Or lstrSit = "C" Then
									.set_TextMatrix(.Rows - 1, 9, "SM") 'SIT_ATIV
								Else
									.set_TextMatrix(.Rows - 1, 9, "NO") 'SIT_ATIV
								End If
								If ldteLig > idteBas And ldteEnt <= idteBas Then
									.set_TextMatrix(.Rows - 1, 10, VB6.Format(ldteEnt, "DD/MM/YY")) 'DATA_CON
								ElseIf ldteLig > idteBas And ldteRel <= idteBas Then
									.set_TextMatrix(.Rows - 1, 10, VB6.Format(ldteRel, "DD/MM/YY")) 'DATA_CON
								Else
									.set_TextMatrix(.Rows - 1, 10, VB6.Format(ldteLig, "DD/MM/YY")) 'DATA_CON
								End If
								.set_TextMatrix(.Rows - 1, 11, Replace(VB6.Format(ldblCrg / 1000, "#,#####0.00000"), ".", "")) 'POT_INST
								.set_TextMatrix(.Rows - 1, 12, Replace(VB6.Format(ldblEne(1), "#,#####0.00000"), ".", "")) 'ENE_01
								.set_TextMatrix(.Rows - 1, 13, Replace(VB6.Format(ldblEne(2), "#,#####0.00000"), ".", "")) 'ENE_02
								.set_TextMatrix(.Rows - 1, 14, Replace(VB6.Format(ldblEne(3), "#,#####0.00000"), ".", "")) 'ENE_03
								.set_TextMatrix(.Rows - 1, 15, Replace(VB6.Format(ldblEne(4), "#,#####0.00000"), ".", "")) 'ENE_04
								.set_TextMatrix(.Rows - 1, 16, Replace(VB6.Format(ldblEne(5), "#,#####0.00000"), ".", "")) 'ENE_05
								.set_TextMatrix(.Rows - 1, 17, Replace(VB6.Format(ldblEne(6), "#,#####0.00000"), ".", "")) 'ENE_06
								.set_TextMatrix(.Rows - 1, 18, Replace(VB6.Format(ldblEne(7), "#,#####0.00000"), ".", "")) 'ENE_07
								.set_TextMatrix(.Rows - 1, 19, Replace(VB6.Format(ldblEne(8), "#,#####0.00000"), ".", "")) 'ENE_08
								.set_TextMatrix(.Rows - 1, 20, Replace(VB6.Format(ldblEne(9), "#,#####0.00000"), ".", "")) 'ENE_09
								.set_TextMatrix(.Rows - 1, 21, Replace(VB6.Format(ldblEne(10), "#,#####0.00000"), ".", "")) 'ENE_10
								.set_TextMatrix(.Rows - 1, 22, Replace(VB6.Format(ldblEne(11), "#,#####0.00000"), ".", "")) 'ENE_11
								.set_TextMatrix(.Rows - 1, 23, Replace(VB6.Format(ldblEne(12), "#,#####0.00000"), ".", "")) 'ENE_12
								.set_TextMatrix(.Rows - 1, 24, "") 'DESC
								Me.lblTotUcBt.Text = CStr(.Rows - 1)
							End With
						End If
					End If
					'				
					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1,  , " Grupo B ")
				Next
				Me.mfgUcBt.Redraw = True

			End If
			'
			'Grupo A
			'
			lstrSql = ""
			lstrSql = lstrSql & "SELECT * "
			lstrSql = lstrSql & "  FROM `unida` "
			lstrSql = lstrSql & " WHERE `unida`.`situacao` <> 'N' "

			objAtualizaBancoDados.QUERY_NAME = lstrSql
			dt2 = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

			If dt2.Rows.Count > 0 Then

				Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, dt2.Rows.Count, " Grupo A ")
				Me.mfgUcMt.Redraw = False
				Me.mfgUcMt.Rows = 0
				Me.mfgUcMt.Cols = 42
				Call Me.mfgUcMt.Clear()
				lblnRet = True

				For Each row2 As DataRow In dt2.Rows
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrZon = Trim(IIf(IsDBNull(row2.Item("zona")), "", row2.Item("zona")))
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrLiv = Trim(IIf(IsDBNull(row2.Item("livro")), "", row2.Item("livro")))
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrCon = Trim(IIf(IsDBNull(row2.Item("conta")), "", row2.Item("conta")))
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					llngUId = CInt(IIf(IsDBNull(row2.Item("unid_id")), "0", row2.Item("unid_id")))
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					llngCId = CInt(IIf(IsDBNull(row2.Item("cons_id")), "0", row2.Item("cons_id")))
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrMed = Trim(IIf(IsDBNull(row2.Item("eletr")), "", row2.Item("eletr")))
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrSit = Trim(IIf(IsDBNull(row2.Item("situacao")), "L", row2.Item("situacao")))
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrFLi = Trim(IIf(IsDBNull(row2.Item("faselig")), "0", row2.Item("faselig")))
					If lstrFLi = "" Or lstrFLi = "0" Then lstrFLi = "ABCN"
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrTen = Trim(IIf(IsDBNull(row2.Item("tensao")), "", row2.Item("tensao")))
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					lstrCla = Trim(IIf(IsDBNull(row2.Item("clas")), "", row2.Item("clas")))
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					ldblCrg = (CDbl(IIf(IsDBNull(row2.Item("carga")), "0", row2.Item("carga"))) / 1000)
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					ldteLig = CDate(IIf(IsDBNull(row2.Item("data_ligacao")), "1900-01-01", row2.Item("data_ligacao")))
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					ldteRel = CDate(IIf(IsDBNull(row2.Item("data_religacao")), "1900-01-01", row2.Item("data_religacao")))
					'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
					ldteEnt = CDate(IIf(IsDBNull(row2.Item("data_entrada")), "1900-01-01", row2.Item("data_entrada")))
					If ldteLig > idteBas Then
						lblnFat = VerificaSeExisteFatura(lstrZon, lstrLiv, lstrCon, llngUId, "A")
					Else
						lblnFat = True
					End If
					If ldteLig <= idteBas Or lblnFat Then
						Call BarraProgresso((Me.ProgressBarHistorico), (Me.lblPercHistorico), (Me.lblInfoHistorio), 0, 12, " histórico ")
						For lintAux = 1 To 12
							ldblEne(lintAux) = BuscaConsumo("A", llngUId, VB6.Format(lintAux, "00") & "/" & VB6.Format(idteBas, "YYYY"),  , 0, lstrFCl)
							ldblDem(lintAux) = BuscaConsumo("A", llngUId, VB6.Format(lintAux, "00") & "/" & VB6.Format(idteBas, "YYYY"),  , 2, lstrFCl)
							Call BarraProgresso((Me.ProgressBarHistorico), (Me.lblPercHistorico), (Me.lblInfoHistorio), 1,  , " histórico ")
						Next lintAux
						If gstrEmpresa_Sigla = "EFLUL" Then
							lstrCod = VB6.Format(lstrLiv & VB.Right(lstrCon, 4), "000000") 'COD_ID
						Else
							lstrCod = VB6.Format(lstrZon & lstrLiv & lstrCon, "00000000000") 'COD_ID
						End If
						lstrPnc = ""
						lstrSdm = ""
						With Me.mfgUcMtIndra
							If .Rows > 0 Then
								lblnCon = BuscaDadosIndra((Me.mfgUcMtIndra), lstrCod, "MT", lstrPnc, lstrSdm, lstrFLi, ldblCrg)
							Else
								lblnCon = False
							End If
						End With
						If lblnCon Then
							With Me.mfgUcMt
								.Rows = .Rows + 1
								.set_TextMatrix(.Rows - 1, 0, lstrCod)
								.set_TextMatrix(.Rows - 1, 1, lstrPnc) 'PN_CON
								.set_TextMatrix(.Rows - 1, 2, lstrSdm) 'SDMT_CON
								.set_TextMatrix(.Rows - 1, 3, lstrMed) 'MED_01
								.set_TextMatrix(.Rows - 1, 4, "") 'MED_02
								.set_TextMatrix(.Rows - 1, 5, "0") 'TP
								.set_TextMatrix(.Rows - 1, 6, "0") 'TC
								.set_TextMatrix(.Rows - 1, 7, BuscaTDIST) 'DIST
								.set_TextMatrix(.Rows - 1, 8, BuscaTCLA_SUBCLA(lstrCla)) 'CLA_SUB
								.set_TextMatrix(.Rows - 1, 9, lstrFLi) 'FAS_CON
								.set_TextMatrix(.Rows - 1, 10, "MT") 'GRU_TEN
								.set_TextMatrix(.Rows - 1, 11, BuscaTTEN(lstrTen)) 'TEN_FORN
								.set_TextMatrix(.Rows - 1, 12, BuscaTGRU_TAR(lstrCla, "A", lstrFCl)) 'GRU_TAR
								If lstrSit = "L" Or lstrSit = "C" Then
									.set_TextMatrix(.Rows - 1, 13, "SM") 'SIT_ATIV
								Else
									.set_TextMatrix(.Rows - 1, 13, "NO") 'SIT_ATIV
								End If
								If ldteLig > idteBas And ldteEnt <= idteBas Then
									.set_TextMatrix(.Rows - 1, 14, VB6.Format(ldteEnt, "DD/MM/YY")) 'DATA_CON
								ElseIf ldteLig > idteBas And ldteRel <= idteBas Then
									.set_TextMatrix(.Rows - 1, 14, VB6.Format(ldteRel, "DD/MM/YY")) 'DATA_CON
								Else
									.set_TextMatrix(.Rows - 1, 14, VB6.Format(ldteLig, "DD/MM/YY")) 'DATA_CON
								End If
								.set_TextMatrix(.Rows - 1, 15, Replace(VB6.Format(ldblCrg, "#,#####0.00000"), ".", "")) 'POT_INST
								.set_TextMatrix(.Rows - 1, 16, IIf(lstrFCl = "01AL00", "V", "F")) 'LIV
								.set_TextMatrix(.Rows - 1, 17, Replace(VB6.Format(ldblDem(1), "#,#####0.00000"), ".", "")) 'DEM_01
								.set_TextMatrix(.Rows - 1, 18, Replace(VB6.Format(ldblDem(2), "#,#####0.00000"), ".", "")) 'DEM_02
								.set_TextMatrix(.Rows - 1, 19, Replace(VB6.Format(ldblDem(3), "#,#####0.00000"), ".", "")) 'DEM_03
								.set_TextMatrix(.Rows - 1, 20, Replace(VB6.Format(ldblDem(4), "#,#####0.00000"), ".", "")) 'DEM_04
								.set_TextMatrix(.Rows - 1, 21, Replace(VB6.Format(ldblDem(5), "#,#####0.00000"), ".", "")) 'DEM_05
								.set_TextMatrix(.Rows - 1, 22, Replace(VB6.Format(ldblDem(6), "#,#####0.00000"), ".", "")) 'DEM_06
								.set_TextMatrix(.Rows - 1, 23, Replace(VB6.Format(ldblDem(7), "#,#####0.00000"), ".", "")) 'DEM_07
								.set_TextMatrix(.Rows - 1, 24, Replace(VB6.Format(ldblDem(8), "#,#####0.00000"), ".", "")) 'DEM_08
								.set_TextMatrix(.Rows - 1, 25, Replace(VB6.Format(ldblDem(9), "#,#####0.00000"), ".", "")) 'DEM_09
								.set_TextMatrix(.Rows - 1, 26, Replace(VB6.Format(ldblDem(10), "#,#####0.00000"), ".", "")) 'DEM_10
								.set_TextMatrix(.Rows - 1, 27, Replace(VB6.Format(ldblDem(11), "#,#####0.00000"), ".", "")) 'DEM_11
								.set_TextMatrix(.Rows - 1, 28, Replace(VB6.Format(ldblDem(12), "#,#####0.00000"), ".", "")) 'DEM_12
								.set_TextMatrix(.Rows - 1, 29, Replace(VB6.Format(ldblEne(1), "#,#####0.00000"), ".", "")) 'ENE_01
								.set_TextMatrix(.Rows - 1, 30, Replace(VB6.Format(ldblEne(2), "#,#####0.00000"), ".", "")) 'ENE_02
								.set_TextMatrix(.Rows - 1, 31, Replace(VB6.Format(ldblEne(3), "#,#####0.00000"), ".", "")) 'ENE_03
								.set_TextMatrix(.Rows - 1, 32, Replace(VB6.Format(ldblEne(4), "#,#####0.00000"), ".", "")) 'ENE_04
								.set_TextMatrix(.Rows - 1, 33, Replace(VB6.Format(ldblEne(5), "#,#####0.00000"), ".", "")) 'ENE_05
								.set_TextMatrix(.Rows - 1, 34, Replace(VB6.Format(ldblEne(6), "#,#####0.00000"), ".", "")) 'ENE_06
								.set_TextMatrix(.Rows - 1, 35, Replace(VB6.Format(ldblEne(7), "#,#####0.00000"), ".", "")) 'ENE_07
								.set_TextMatrix(.Rows - 1, 36, Replace(VB6.Format(ldblEne(8), "#,#####0.00000"), ".", "")) 'ENE_08
								.set_TextMatrix(.Rows - 1, 37, Replace(VB6.Format(ldblEne(9), "#,#####0.00000"), ".", "")) 'ENE_09
								.set_TextMatrix(.Rows - 1, 38, Replace(VB6.Format(ldblEne(10), "#,#####0.00000"), ".", "")) 'ENE_10
								.set_TextMatrix(.Rows - 1, 39, Replace(VB6.Format(ldblEne(11), "#,#####0.00000"), ".", "")) 'ENE_11
								.set_TextMatrix(.Rows - 1, 40, Replace(VB6.Format(ldblEne(12), "#,#####0.00000"), ".", "")) 'ENE_12
								.set_TextMatrix(.Rows - 1, 41, "") 'DESC
								Me.lblTotUcMt.Text = CStr(.Rows - 1)
							End With
						End If
					End If

					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1,  , " Grupo A ")
				Next
				Me.mfgUcMt.Redraw = True
			End If

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

		Catch ex As Exception
			lblnRet = False
			EncheGrade = lblnRet
			Exit Function
		End Try

	End Function
	
	Private Function InsereDbf() As Boolean
		Dim lblnRet As Boolean
		Dim llngRow As Integer
		Dim llngCol As Integer
		Dim lstrDbf As String
		Dim ldblAux As Double
		Dim objAtualizaBancoDados As New entidadeAtualizaBancoDados
		Dim repositorioAtualizaBancoDados As New repositorioAtualizaBancoDados

		On Error GoTo ErroInsereDbf
		''Inserir alguns registos na Tabela Persons
		Call AbreConexaoDBF(Me.txtDirDestino.Text)
		With Me.mfgUcBt
			Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, .Rows - 1, " exportação B ")
			For llngRow = 0 To .Rows - 1
				lstrDbf = ""
				lstrDbf = lstrDbf & "insert into UC_BT values("
				Call BarraProgresso((Me.ProgressBarHistorico), (Me.lblPercHistorico), (Me.lblInfoHistorio), 0, .Cols - 1, "")
				For llngCol = 0 To .Cols - 1
					If llngCol >= 11 And llngCol <= 23 Then
						'ldblAux = Arredondar(Conversion.CDbl(IIf(IsNumeric(.TextMatrix(llngRow, llngCol)), .TextMatrix(llngRow, llngCol), 0)), 5)
						lstrDbf = lstrDbf & "'" & Replace(.get_TextMatrix(llngRow, llngCol), ".", ",") & "',"
						'lstrDbf = lstrDbf & "'" & Strings.Replace(ldblAux, ".", ",") & "',"
					Else
						lstrDbf = lstrDbf & "'" & .get_TextMatrix(llngRow, llngCol) & "',"
					End If
					Call BarraProgresso((Me.ProgressBarHistorico), (Me.lblPercHistorico), (Me.lblInfoHistorio), 1,  , "")
				Next llngCol
				lstrDbf = VB.Left(lstrDbf, CInt(Len(lstrDbf)) - 1) & ")"

				objAtualizaBancoDados.QUERY_NAME = lstrDbf
				repositorioAtualizaBancoDados.Execute(objAtualizaBancoDados)

				Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1,  , " exportação B ")
			Next llngRow
		End With
		With Me.mfgUcMt
			If .Rows > 0 Then
				Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, .Rows - 1, " exportação A ")
				For llngRow = 0 To .Rows - 1
					lstrDbf = ""
					lstrDbf = lstrDbf & "insert into UC_MT values("
					Call BarraProgresso((Me.ProgressBarHistorico), (Me.lblPercHistorico), (Me.lblInfoHistorio), 0, .Cols - 1, "")
					For llngCol = 0 To .Cols - 1
						If llngCol >= 12 And llngCol <= 23 Then
							lstrDbf = lstrDbf & "'" & Replace(.get_TextMatrix(llngRow, llngCol), ".", ",") & "',"
						Else
							lstrDbf = lstrDbf & "'" & .get_TextMatrix(llngRow, llngCol) & "',"
						End If
						Call BarraProgresso((Me.ProgressBarHistorico), (Me.lblPercHistorico), (Me.lblInfoHistorio), 1,  , "")
					Next llngCol
					lstrDbf = VB.Left(lstrDbf, CInt(Len(lstrDbf)) - 1) & ")"

					objAtualizaBancoDados.QUERY_NAME = lstrDbf
					repositorioAtualizaBancoDados.Execute(objAtualizaBancoDados)

					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1,  , " exportação A ")
				Next llngRow
			End If
		End With
		lblnRet = True
ErroInsereDbf:
		'If icnxDbf.Errors.Count > 0 Then
		'	Call MsgBox(icnxDbf.Errors.Item(0).Number & " - " & icnxDbf.Errors.Item(0).NativeError & " - " & icnxDbf.Errors.Item(0).Description, MsgBoxStyle.Exclamation, gstrTituloMensagem)
		'	lblnRet = False
		'Else
		'	If Err.Number > 0 Then
		'		Call MsgBox(Err.Number & " - " & Err.Description, MsgBoxStyle.Exclamation, gstrTituloMensagem)
		'		lblnRet = False
		'	Else
		'		lblnRet = True
		'	End If
		'End If
		'If icnxDbf.State = ADODB.ObjectStateEnum.adStateOpen Then icnxDbf.Close()
		InsereDbf = lblnRet
	End Function
	
	Private Function VerificaSeExisteFatura(ByVal astrZona As String, ByVal astrLivro As String, ByVal astrConta As String, ByVal alngUnidId As Integer, ByVal astrGrupo As String) As Boolean
		Dim lblnRet As Boolean
		Dim lstrSql As String
		Dim dt As DataTable
		Dim objatualizaBancoDados As New entidadeAtualizaBancoDados
		Dim repositorioAtualizaBancoDados As New repositorioAtualizaBancoDados

		lblnRet = False
		lstrSql = ""
		lstrSql = lstrSql & "SELECT * "
		lstrSql = lstrSql & "  FROM `nf` "
		lstrSql = lstrSql & " WHERE (`mesano_ref` BETWEEN " & VB6.Format(idteBas, "YYYY") & "01" & " AND " & VB6.Format(idteBas, "YYYY") & "12)"
		lstrSql = lstrSql & "   AND `grupo`   = '" & astrGrupo & "' "
		lstrSql = lstrSql & "   AND `zona`    = '" & astrZona & "' "
		lstrSql = lstrSql & "   AND `livro`   = '" & astrLivro & "' "
		lstrSql = lstrSql & "   AND `conta`   = '" & astrConta & "' "
		lstrSql = lstrSql & "   AND `unid_id` =  " & alngUnidId & " "
		lstrSql = lstrSql & " LIMIT 0, 1 "

		Try

			objatualizaBancoDados.QUERY_NAME = lstrSql
			dt = repositorioAtualizaBancoDados.Consultar(objatualizaBancoDados)

			If dt.Rows.Count > 0 Then
				lblnRet = (dt.Rows.Count > 0)
			End If

			VerificaSeExisteFatura = lblnRet

		Catch ex As Exception
			Exit Function
		End Try
	End Function
	
	Private Function BuscaTCLA_SUBCLA(ByVal astrCodClasse As String) As String
		Dim lstrRet As String
		Dim lstrSql As String
		Dim dt As DataTable
		Dim objatualizaBancoDados As New entidadeAtualizaBancoDados
		Dim repositorioAtualizaBancoDados As New repositorioAtualizaBancoDados

		lstrRet = ""
		lstrSql = ""
		lstrSql = lstrSql & "SELECT * "
		lstrSql = lstrSql & "  FROM `classe` "
		lstrSql = lstrSql & " WHERE `codigo` = '" & astrCodClasse & "' "

		Try
			objatualizaBancoDados.QUERY_NAME = lstrSql
			dt = repositorioAtualizaBancoDados.Consultar(objatualizaBancoDados)

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

			BuscaTCLA_SUBCLA = lstrRet

		Catch ex As Exception
			Exit Function
		End Try

	End Function
	Private Function BuscaTDIST() As String
		Dim lstrAux As String
		Dim lstrRet As String
		Dim lstrDad As String
		Try
			lstrDad = "AES-SUL?1;ADESA?2;AMPLA?3;BANDEIRANTE?4;BOA VISTA ENERGIA?5;CAIUÁ?6;CEA?7;CEAL?8;CEB?9;CEEE?10;CELESC?11;CELG?12;CELPA?13;CELPE?14;CELTINS?15;CEMAR?16;CEMAT?17;CEMIG?18;CEPISA?19;CERON?20;CER?21;CFLO?22;CHESP?23;CJE?24;CLFM?25;CNEE?26;COCEL?27;COELBA?28;COELCE?29;COOPERALIANÇA?30;COPEL?31;COSERN?32;CPEE?33;CPFL SANTA CRUZ?34;CPFL PAULISTA?35;CPFL PIRATININGA?36;CSPE?37;DEMEI?38;DME-PC?39;EBO?40;EEB?41;EEVP?42;EFLJCL?43;EFLUL?44;ELEKTRO?45;ELETROACRE?46;ELETROCAR?47;ELETROPAULO?48;ELFSM?49;EMG?50;ENERSUL?51;ENF?52;EPB?53;ESCELSA?54;ESE?55;FORCEL?56;HIDROPAN?57;IENERGIA?58;LIGHT?59;MUX ENERGIA?60;RGE?61;SULGIPE?62;UHENPAL?63;CEREJ?64;CERAL?65;COORSEL?66;CERBRANORTE?67;CERAÇÁ?68;CERPALO?69;CERGRAL?70;CEJAMA?71;CERSUL?72;COOPERA?73;COOPERMILA?74;CERGAL?75;CERTAJA ENERGIA?76;CERAL-DIS?77;CERRP?78;CERNHE?79;CERES?80;CERCOS?81;CETRIL?82;CERPRO?83;CERMEC?84;CERIS?85;CERIPA?86;CERIM?87;CEDRI?88;CEDRAP?89;"
			If CInt(InStr(1, lstrDad, gstrEmpresa_Sigla)) > 0 Then
				lstrAux = Mid(lstrDad, CInt(InStr(1, lstrDad, gstrEmpresa_Sigla)))
				lstrRet = Mid(lstrAux, CInt(InStr(1, lstrAux, "?")) + 1, CInt(InStr(1, lstrAux, ";")) - (CInt(InStr(1, lstrAux, "?")) + 1))
			Else
				lstrRet = ""
			End If
			BuscaTDIST = lstrRet

		Catch ex As Exception
			Exit Function
		End Try
	End Function

	Private Function BuscaTTEN(ByVal astrTensao As Object) As String
		Dim lstrAux As String
		Dim lstrRet As String
		Dim lstrDad As String
		Try

			lstrDad = "216?47;220?48;127?49;230?50;240?51;254?52;380?53;400?54;440?55;500?56;1000?1;2200?2;3600?3;3785?4;4160?5;5000?6;6000?7;7960?8;8670?9;11400?10;12000?11;12600?12;13200?13;13800?14;14400?15;15000?16;19053?17;19919?18;21000?19;23000?20;24200?21;25000?22;27000?23;30000?24;33000?25;34500?26;36000?27;38000?28;40000?29;45000?30;45400?31;48000?32;60000?33;69000?34;72500?35;88000?36;120000?37;123000?38;138000?39;145000?40;230000?41;500000?42;750000?43;1000000?44;"
			'UPGRADE_WARNING: Couldn't resolve default property of object astrTensao. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
			If CInt(InStr(1, lstrDad, astrTensao)) > 0 Then
				'UPGRADE_WARNING: Couldn't resolve default property of object astrTensao. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
				lstrAux = Mid(lstrDad, CInt(InStr(1, lstrDad, astrTensao)))
				lstrRet = Mid(lstrAux, CInt(InStr(1, lstrAux, "?")) + 1, CInt(InStr(1, lstrAux, ";")) - (CInt(InStr(1, lstrAux, "?")) + 1))
			Else
				lstrRet = "0"
			End If
			BuscaTTEN = lstrRet

		Catch ex As Exception
			Exit Function
		End Try
	End Function
	Private Function BuscaTGRU_TAR(ByVal astrCodClasse As String, ByVal astrGrupo As String, ByVal astrCodClaBx As String) As String
		Dim lstrRet As String

		Try
			If astrGrupo = "B" Then
				If VB.Left(astrCodClasse, 2) = "64" Then
					lstrRet = "B4A"
					If VB.Left(astrCodClasse, 4) = "6402" Then
						lstrRet = "B4B"
					End If
				ElseIf VB.Left(astrCodClasse, 1) = "5" Then
					lstrRet = "B2RU"
					If VB.Left(astrCodClasse, 2) = "52" Then
						lstrRet = "B2CO"
					ElseIf VB.Left(astrCodClasse, 2) = "54" Then
						lstrRet = "B2SP"
					ElseIf VB.Left(astrCodClasse, 2) = "55" Then
						lstrRet = "B2SP"
					End If
				ElseIf VB.Left(astrCodClasse, 1) = "9" Then
					lstrRet = "B1"
					If astrCodClaBx = "9102" Or astrCodClaBx = "9104" Or astrCodClaBx = "9105" Or astrCodClaBx = "9106" Then
						lstrRet = "B1BR"
					End If
				Else
					lstrRet = "B3"
				End If
			Else
				lstrRet = "A4"
			End If
			BuscaTGRU_TAR = lstrRet

		Catch ex As Exception
			Exit Function
		End Try
	End Function

	Private Sub LigDes(ByVal ablnModo As Boolean)
		Me.cmdExporta.Enabled = ablnModo
		Me.cmdFechar.Enabled = ablnModo
		Me.cmdPasta.Enabled = ablnModo
		Me.dtpAno.Enabled = ablnModo
		Me.dtpAno.Enabled = ablnModo
	End Sub
	
	'UPGRADE_WARNING: Event txtDir.TextChanged 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 txtDir_TextChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtDir.TextChanged
		If iintCtrlPasta = 0 Then
			Me.txtDirDestino.Text = Me.txtDir.Text
			Me.txtDirIndra.Text = ""
		ElseIf iintCtrlPasta = 1 Then 
			Me.txtDirIndra.Text = Me.txtDir.Text
		End If
	End Sub
	
	Private Function BuscaDadosIndra(ByRef amfgAux As AxMSFlexGridLib.AxMSFlexGrid, ByVal astrCodId As String, ByVal astrGruTen As String, Optional ByRef astrValor1 As String = "", Optional ByRef astrValor2 As String = "", Optional ByRef astrValor3 As String = "", Optional ByRef adblValor As Double = 0) As Boolean
		Dim lblnRet As Boolean
		Dim llngRow As Integer
		Dim llngAux As Integer

		Try
			lblnRet = False
			llngRow = 0
			astrValor1 = ""
			astrValor2 = ""
			With amfgAux
				If .Rows > 0 Then
					For llngAux = 0 To .Rows - 1
						If .get_TextMatrix(llngAux, 0) = astrCodId Then
							llngRow = llngAux
							llngAux = .Rows - 1
							lblnRet = True
							If astrGruTen = "BT" Then
								astrValor1 = .get_TextMatrix(llngRow, 1) 'RAMAL
								astrValor2 = .get_TextMatrix(llngRow, 5) 'FAS_CON
								adblValor = CDbl(.get_TextMatrix(llngRow, 11)) 'POT_INST
							Else
								astrValor1 = .get_TextMatrix(llngRow, 1) 'PN_CON
								astrValor2 = .get_TextMatrix(llngRow, 2) 'SDMT_CON
								astrValor3 = .get_TextMatrix(llngRow, 9) 'FAS_CON
								adblValor = CDbl(.get_TextMatrix(llngRow, 15)) 'POT_INST
							End If
						End If
					Next llngAux
				End If
			End With
			BuscaDadosIndra = lblnRet

		Catch ex As Exception
			Exit Function
		End Try
	End Function
	
	Private Sub CarregaArquivo(ByVal astrArq As String, ByRef amfgAux As AxMSFlexGridLib.AxMSFlexGrid)
		Dim lstrLin As String
		Dim lintARQ As Short
		Dim lintAux As Short
		Dim larrIte() As String
		Dim llngTotLin As Integer
		Dim lintLin As Short

		Try

			'On Error GoTo ErrorCarregaArquivo
			llngTotLin = 0
			lintLin = 0
			'
			'Conta quantidade de linhas do arquivo
			lintARQ = FreeFile
			FileOpen(lintARQ, astrArq, OpenMode.Input)
			While Not EOF(lintARQ)
				lstrLin = LineInput(lintARQ)
				llngTotLin = llngTotLin + 1
				System.Windows.Forms.Application.DoEvents()
			End While
			FileClose(lintARQ)
			With amfgAux
				.Cols = 18
				.Rows = 0
				Call .Clear()
			End With
			lintARQ = FreeFile
			FileOpen(lintARQ, astrArq, OpenMode.Input)
			Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, llngTotLin)
			While Not EOF(lintARQ)
				lstrLin = LineInput(lintARQ)
				lintLin = lintLin + 1
				larrIte = TrataCSV(lstrLin)
				amfgAux.Rows = amfgAux.Rows + 1
				For lintAux = LBound(larrIte) To UBound(larrIte)
					With amfgAux
						.set_TextMatrix(.Rows - 1, lintAux, larrIte(lintAux))
					End With
				Next lintAux
				Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
			End While
			FileClose(lintARQ)
		Catch ex As Exception
			Exit Sub
		End Try

	End Sub
	
	Private Function TrataCSV(ByVal astrLinha As String) As String()
		Dim larrIte() As String
		Dim lstrAux As String
		Dim lintAux As Short
		Dim lblnAsp As Boolean
		Try
			lblnAsp = False
			lstrAux = ""
			larrIte = Split(astrLinha, ";")
			For lintAux = 0 To UBound(larrIte)
				If Not lblnAsp Then
					If (VB.Left(larrIte(lintAux), 1) = """" And VB.Right(larrIte(lintAux), 1) = """") Or (VB.Left(larrIte(lintAux), 1) <> """" And VB.Right(larrIte(lintAux), 1) <> """") Then
						lstrAux = lstrAux & "$$$" & Replace(larrIte(lintAux), """", "")
					ElseIf VB.Left(larrIte(lintAux), 1) = """" Then
						lblnAsp = True
						lstrAux = lstrAux & "$$$" & Replace(larrIte(lintAux), """", "")
					End If
				Else
					lstrAux = lstrAux & "," & Replace(larrIte(lintAux), """", "")
					If VB.Right(larrIte(lintAux), 1) = """" Then lblnAsp = False
				End If
			Next
			TrataCSV = Split(IIf(VB.Left(lstrAux, 3) = "$$$", VB.Right(lstrAux, Len(lstrAux) - 3), ""), "$$$")
		Catch ex As Exception
			Exit Function
		End Try
	End Function
End Class