Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Imports fatd_faturamento.BLL.Repositorio
Imports fatd_faturamento.ENTIDADE
Friend Class frmImportaLinkCenter
	Inherits System.Windows.Forms.Form
	Private ilngIdUsuarioLC As Integer
	Private idteIni As Date
	Private idteFim As Date
	Private iFrmQuemChama As System.Windows.Forms.Form
	Private dt, dt1, dt2, dt3 As DataTable
	Private strTrataUC As String
	Private objAtualizaBancoDados As New entidadeAtualizaBancoDados
	Private repositorioAtualizaBancoDados As New repositorioAtualizaBancoDados
	Private objExecAtualizaBancoDados As New entidadeAtualizaBancoDados
	Private repositorioExecAtualizaBancoDados As New repositorioAtualizaBancoDados
	Private bdataSugerida As Boolean

	'UPGRADE_WARNING: Event chkLogradouro.CheckStateChanged may fire when form is initialized. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="88B12AE1-6DE0-48A0-86F1-60C0686C026A"'
	Private Sub chkLogradouro_CheckStateChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles chkLogradouro.CheckStateChanged
		Try
			With Me.txtSequenciasOs
				If Me.chkLogradouro.CheckState = System.Windows.Forms.CheckState.Checked Then
					Me.chkMedidores.CheckState = System.Windows.Forms.CheckState.Unchecked
					Me.chkConsumidor.CheckState = System.Windows.Forms.CheckState.Unchecked
					.Enabled = False
					.Text = ""
				Else
					.Enabled = True
				End If
				.BackColor = System.Drawing.ColorTranslator.FromOle(IIf(.Enabled, System.Drawing.ColorTranslator.ToOle(System.Drawing.SystemColors.Window), System.Drawing.ColorTranslator.ToOle(System.Drawing.SystemColors.Control)))
			End With
		Catch ex As Exception
			Throw New Exception("Ocorreu o Erro: " & Err.Description & " - Rotina: chkLogradouro_CheckStateChanged" & "", ex)
			'MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: chkLogradouro_CheckStateChanged" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	'UPGRADE_WARNING: Event chkMedidores.CheckStateChanged may fire when form is initialized. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="88B12AE1-6DE0-48A0-86F1-60C0686C026A"'
	Private Sub chkMedidores_CheckStateChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles chkMedidores.CheckStateChanged
		Try
			With Me.txtSequenciasOs
				If Me.chkMedidores.CheckState = System.Windows.Forms.CheckState.Checked Then
					Me.chkConsumidor.CheckState = System.Windows.Forms.CheckState.Unchecked
					Me.chkLogradouro.CheckState = System.Windows.Forms.CheckState.Unchecked
					.Enabled = False
					.Text = ""
				Else
					.Enabled = True
				End If
				.BackColor = System.Drawing.ColorTranslator.FromOle(IIf(.Enabled, System.Drawing.ColorTranslator.ToOle(System.Drawing.SystemColors.Window), System.Drawing.ColorTranslator.ToOle(System.Drawing.SystemColors.Control)))
			End With
		Catch ex As Exception
			Throw New Exception("Ocorreu o Erro: " & Err.Description & " - Rotina: chkLogradouro_CheckStateChanged" & "", ex)

			'MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: chkMedidores_CheckStateChanged" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub cmdLeituras_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdLeituras.Click
		Try
			Call frmAtualizaLeituras.SetaParametros(Me)
			Call frmAtualizaLeituras.Show()
		Catch ex As Exception
			Throw New Exception("Ocorreu o Erro: " & Err.Description & " - Rotina:  cmdLeituras_Click" & "", ex)
			'MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: cmdLeituras_Click" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub IniciarMesFaturamento()
		Try
			Dim lblnContinua As Boolean
			lblnContinua = True

			If Not VerificaDadosObrigatorios() Then
				Exit Sub
			End If

			If lblnContinua Then
				idteIni = dtpInicio.Value
				idteFim = dtpFimMesAno.Value
				Call Processa()
			End If

			SetConfig("iniciou.faturamento", "Boolean", "True", "False")
		Catch ex As Exception
			Throw New Exception("Ocorreu o Erro: " & Err.Description & " - Rotina: IniciarMesFaturamento" & "", ex)
			Exit Sub
		End Try
	End Sub


	Private Sub cmdProcessar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdProcessar.Click
		Try
			Dim lblnContinua As Boolean
			lblnContinua = True
			If Trim(Me.txtSequenciasOs.Text) = "" And Me.chkConsumidor.CheckState = System.Windows.Forms.CheckState.Unchecked And Me.chkLogradouro.CheckState = System.Windows.Forms.CheckState.Unchecked And Me.chkMedidores.CheckState = System.Windows.Forms.CheckState.Unchecked Then
				If MsgBox("Tem certeza que deseja importar tudo do Link Center, alterações, transferencias, etc..?", MsgBoxStyle.YesNo + MsgBoxStyle.DefaultButton2, gstrTituloMensagem) = MsgBoxResult.Yes Then
					lblnContinua = True
				Else
					lblnContinua = False
				End If
			End If

			If Not VerificaDadosObrigatorios() Then
				Exit Sub
			End If

			If lblnContinua Then
				Me.dtpFimMesAno.Enabled = False
				Me.cmdProcessar.Enabled = False
				Me.cmdFechar.Enabled = False
				Me.chkLogradouro.Enabled = False
				Me.chkMedidores.Enabled = False
				Me.chkConsumidor.Enabled = False

				idteIni = dtpInicio.Value
				idteFim = dtpFimMesAno.Value
				'idteIni = CDate(CDate("01/" & VB6.Format(Me.dtpFimMesAno.Value, "MM/YYYY")) & " 00:00:01")
				'idteFim = CDate(CDate(UltimoDiaMes(Month(Me.dtpFimMesAno.Value), Year(Me.dtpFimMesAno.Value)) & "/" & VB6.Format(Me.dtpFimMesAno.Value, "MM/YYYY")) & " 23:59:59")
				Call Processa()
				Me.chkConsumidor.Enabled = True
				Me.chkLogradouro.Enabled = True
				Me.chkMedidores.Enabled = True
				Me.dtpFimMesAno.Enabled = True
				Me.cmdProcessar.Enabled = True
				Me.cmdFechar.Enabled = True
				Call MsgBox("Finalizado", MsgBoxStyle.Information, gstrTituloMensagem)
			End If
		Catch ex As Exception
			Throw New Exception("Ocorreu o Erro: " & Err.Description & " - Rotina: cmdProcessar_Click" & "", ex)
			'MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: cmdProcessar_Click" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub frmImportaLinkCenter_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		Try
			Dim lstrSql As String

			If Not bdataSugerida Then
				Me.dtpInicio.Value = VB6.Format(CDate(GetConfig("mes.ano.faturamento")).AddMonths(-1), "dd/MM/yyyy")
				Me.dtpFimMesAno.Value = CDate(VB6.Format(Date.Now, "dd/MM/yyyy"))
			End If

			dtpInicio.Enabled = False
			dtpFimMesAno.Enabled = False

			strTrataUC = GetConfig("tratamento.uc")
			'
			lstrSql = ""
			lstrSql = lstrSql & "CREATE TABLE IF NOT EXISTS `consum_lc_auditoria` ("
			lstrSql = lstrSql & "  `consreg` int(11) DEFAULT 0,"
			lstrSql = lstrSql & "  `matricula` int(11) DEFAULT NULL,"
			lstrSql = lstrSql & "  `nome` varchar(85) DEFAULT NULL,"
			lstrSql = lstrSql & "  `cgc` varchar(18) DEFAULT NULL,"
			lstrSql = lstrSql & "  `insc` varchar(25) DEFAULT NULL,"
			lstrSql = lstrSql & "  `rg` varchar(25) DEFAULT NULL,"
			lstrSql = lstrSql & "  `dddtel` char(2) DEFAULT NULL,"
			lstrSql = lstrSql & "  `telefone` varchar(20) DEFAULT NULL,"
			lstrSql = lstrSql & "  `dddfax` char(2) DEFAULT NULL,"
			lstrSql = lstrSql & "  `fax` varchar(10) DEFAULT NULL,"
			lstrSql = lstrSql & "  `dddcel` char(2) DEFAULT NULL,"
			lstrSql = lstrSql & "  `cel` varchar(10) DEFAULT NULL,"
			lstrSql = lstrSql & "  `email` varchar(40) DEFAULT NULL,"
			lstrSql = lstrSql & "  `nomepai` varchar(60) DEFAULT NULL,"
			lstrSql = lstrSql & "  `nomemae` varchar(60) DEFAULT NULL,"
			lstrSql = lstrSql & "  `idestado_civil` int(11) DEFAULT NULL,"
			lstrSql = lstrSql & "  `idprofissao` int(11) DEFAULT NULL,"
			lstrSql = lstrSql & "  `idnacionalidade` int(11) DEFAULT NULL,"
			lstrSql = lstrSql & "  `dt_cadastro` date DEFAULT '0000-00-00',"
			lstrSql = lstrSql & "  `dt_nascto` date DEFAULT '0000-00-00',"
			lstrSql = lstrSql & "  `icms` char(1) DEFAULT NULL,"
			lstrSql = lstrSql & "  `cip` char(1) DEFAULT NULL,"
			lstrSql = lstrSql & "  `et` int(3) DEFAULT '0',"
			lstrSql = lstrSql & "  `observacao` text,"
			lstrSql = lstrSql & "  `itenshist` text,"
			lstrSql = lstrSql & "  `movimento` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',"
			lstrSql = lstrSql & "  `evento` varchar(10) NOT NULL DEFAULT '',"
			lstrSql = lstrSql & "  `idauditoria` int(11) NOT NULL AUTO_INCREMENT,"
			lstrSql = lstrSql & "  `bairro` varchar(60) DEFAULT '',"
			lstrSql = lstrSql & "  `cep` varchar(10) DEFAULT '',"
			lstrSql = lstrSql & "  `cidade` varchar(60) DEFAULT NULL,"
			lstrSql = lstrSql & "  `codend` varchar(15) DEFAULT '',"
			lstrSql = lstrSql & "  `complement` varchar(100) DEFAULT '',"
			lstrSql = lstrSql & "  `ctps` varchar(11) DEFAULT '',"
			lstrSql = lstrSql & "  `distrito` varchar(60) DEFAULT '',"
			lstrSql = lstrSql & "  `dtautodec` date DEFAULT '0000-00-00',"
			lstrSql = lstrSql & "  `end` varchar(60) DEFAULT '',"
			lstrSql = lstrSql & "  `estado_civil` char(1) DEFAULT '',"
			lstrSql = lstrSql & "  `lote` varchar(6) DEFAULT '',"
			lstrSql = lstrSql & "  `nacionalidade` varchar(100) DEFAULT '',"
			lstrSql = lstrSql & "  `nis_data` date DEFAULT '0000-00-00',"
			lstrSql = lstrSql & "  `nis_faturado` date DEFAULT '0000-00-00',"
			lstrSql = lstrSql & "  `nis_numer` varchar(15) DEFAULT '',"
			lstrSql = lstrSql & "  `nis_uf` char(2) DEFAULT '',"
			lstrSql = lstrSql & "  `nomecontato` varchar(40) DEFAULT '',"
			lstrSql = lstrSql & "  `numero` varchar(10) DEFAULT '',"
			lstrSql = lstrSql & "  `profissao` varchar(40) DEFAULT '',"
			lstrSql = lstrSql & "  `quadra` varchar(6) DEFAULT '',"
			lstrSql = lstrSql & "  `rg_orgao` varchar(6) DEFAULT '',"
			lstrSql = lstrSql & "  `rg_uf` varchar(2) DEFAULT '',"
			lstrSql = lstrSql & "  `telcontato` varchar(13) DEFAULT '',"
			lstrSql = lstrSql & "  `titulo_eleitor` varchar(11) DEFAULT '',"
			lstrSql = lstrSql & "  `uf` varchar(2) DEFAULT '',  "
			lstrSql = lstrSql & "  PRIMARY KEY (`idauditoria`)"
			lstrSql = lstrSql & ") ENGINE=InnoDB DEFAULT CHARSET=latin1  "
			objExecAtualizaBancoDados.QUERY_NAME = lstrSql
			repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)
			'
			lstrSql = ""
			lstrSql = lstrSql & "CREATE TABLE IF NOT EXISTS `unidb_lc_auditoria` ("
			lstrSql = lstrSql & "  `unid_id` int(11) DEFAULT NULL,"
			lstrSql = lstrSql & "  `cons_id` int(11) DEFAULT NULL,"
			lstrSql = lstrSql & "  `pagto_duvidoso` tinyint(1) NOT NULL DEFAULT '0',"
			lstrSql = lstrSql & "  `banco` varchar(3) DEFAULT NULL,"
			lstrSql = lstrSql & "  `agenc` varchar(4) DEFAULT NULL,"
			lstrSql = lstrSql & "  `cc` varchar(12) DEFAULT NULL,"
			lstrSql = lstrSql & "  `grupo` char(2) DEFAULT NULL,"
			lstrSql = lstrSql & "  `ta` varchar(6) DEFAULT NULL,"
			lstrSql = lstrSql & "  `clas` varchar(4) DEFAULT NULL,"
			lstrSql = lstrSql & "  `fase` char(1) DEFAULT NULL,"
			lstrSql = lstrSql & "  `situacao` char(1) DEFAULT NULL,"
			lstrSql = lstrSql & "  `ilum` char(1) DEFAULT NULL,"
			lstrSql = lstrSql & "  `tensao` varchar(10) DEFAULT NULL,"
			lstrSql = lstrSql & "  `faselig` varchar(4) DEFAULT NULL,"
			lstrSql = lstrSql & "  `codend` varchar(15) DEFAULT NULL,"
			lstrSql = lstrSql & "  `uf` varchar(2) DEFAULT NULL,"
			lstrSql = lstrSql & "  `cep` varchar(10) DEFAULT NULL,"
			lstrSql = lstrSql & "  `cidade` varchar(60) DEFAULT NULL,"
			lstrSql = lstrSql & "  `distrito` varchar(60) DEFAULT NULL,"
			lstrSql = lstrSql & "  `bairro` varchar(60) DEFAULT NULL,"
			lstrSql = lstrSql & "  `end` varchar(60) DEFAULT NULL,"
			lstrSql = lstrSql & "  `numero` varchar(10) DEFAULT NULL,"
			lstrSql = lstrSql & "  `complement` varchar(100) DEFAULT NULL,"
			lstrSql = lstrSql & "  `codend_entrega` varchar(15) DEFAULT NULL,"
			lstrSql = lstrSql & "  `uf_entrega` varchar(2) DEFAULT NULL,"
			lstrSql = lstrSql & "  `cep_entrega` varchar(10) DEFAULT NULL,"
			lstrSql = lstrSql & "  `cid_entrega` varchar(60) DEFAULT NULL,"
			lstrSql = lstrSql & "  `dis_entrega` varchar(60) DEFAULT NULL,"
			lstrSql = lstrSql & "  `bai_entrega` varchar(60) DEFAULT NULL,"
			lstrSql = lstrSql & "  `end_entrega` varchar(60) DEFAULT NULL,"
			lstrSql = lstrSql & "  `num_entrega` varchar(10) DEFAULT NULL,"
			lstrSql = lstrSql & "  `compl_entrega` varchar(100) DEFAULT NULL,"
			lstrSql = lstrSql & "  `diavenc` char(2) NOT NULL DEFAULT '00',"
			lstrSql = lstrSql & "  `cpf_cnpj` varchar(15) DEFAULT NULL,"
			lstrSql = lstrSql & "  `rg` varchar(25) DEFAULT NULL,"
			lstrSql = lstrSql & "  `insc` varchar(25) DEFAULT NULL,"
			lstrSql = lstrSql & "  `pessoa` char(1) DEFAULT NULL,"
			lstrSql = lstrSql & "  `nome` varchar(85) DEFAULT NULL,"
			lstrSql = lstrSql & "  `circuito` varchar(10) DEFAULT NULL,"
			lstrSql = lstrSql & "  `lacre` varchar(100) DEFAULT NULL,"
			lstrSql = lstrSql & "  `ap_saude` char(1) DEFAULT 'N',"
			lstrSql = lstrSql & "  `tp_ligacao` varchar(15) DEFAULT 'A',"
			lstrSql = lstrSql & "  `regiao` varchar(1) NOT NULL DEFAULT 'U',"
			lstrSql = lstrSql & "  `cnae` varchar(4) DEFAULT NULL,"
			lstrSql = lstrSql & "  `data_ligacao` date DEFAULT NULL,"
			lstrSql = lstrSql & "  `data_religacao` date DEFAULT NULL,"
			lstrSql = lstrSql & "  `data_desligamento` date DEFAULT NULL,"
			lstrSql = lstrSql & "  `data_entrada` date DEFAULT NULL,"
			lstrSql = lstrSql & "  `data_corte` date DEFAULT NULL,"
			lstrSql = lstrSql & "  `data_contrato_demanda` date DEFAULT NULL,"
			lstrSql = lstrSql & "  `ultleit` double DEFAULT NULL,"
			lstrSql = lstrSql & "  `final` double DEFAULT NULL,"
			lstrSql = lstrSql & "  `dem_contr_fp` double DEFAULT NULL,"
			lstrSql = lstrSql & "  `dem_contr_ponta` double DEFAULT NULL,"
			lstrSql = lstrSql & "  `demanda_contr` double DEFAULT NULL,"
			lstrSql = lstrSql & "  `agrupa` int(11) NOT NULL DEFAULT '1',"
			lstrSql = lstrSql & "  `instal` varchar(10) DEFAULT NULL,"
			lstrSql = lstrSql & "  `const` double DEFAULT NULL,"
			lstrSql = lstrSql & "  `const_consumo` double DEFAULT NULL,"
			lstrSql = lstrSql & "  `const_demanda` double DEFAULT NULL,"
			lstrSql = lstrSql & "  `const_kvarh` double DEFAULT NULL,"
			lstrSql = lstrSql & "  `const_cons_p` double DEFAULT NULL,"
			lstrSql = lstrSql & "  `const_cons_fp` double DEFAULT NULL,"
			lstrSql = lstrSql & "  `const_demanda_fp` double DEFAULT NULL,"
			lstrSql = lstrSql & "  `const_kvarh_p` double DEFAULT NULL,"
			lstrSql = lstrSql & "  `const_kvarh_fp` double DEFAULT NULL,"
			lstrSql = lstrSql & "  `dtafabmedidor` date DEFAULT NULL,"
			lstrSql = lstrSql & "  `idtipo_medidor` int(11) NOT NULL DEFAULT '0',"
			lstrSql = lstrSql & "  `marca` varchar(15) DEFAULT NULL,"
			lstrSql = lstrSql & "  `num` varchar(11) DEFAULT '',"
			lstrSql = lstrSql & "  `medkvarh` varchar(11) DEFAULT NULL,"
			lstrSql = lstrSql & "  `eletr` varchar(9) DEFAULT NULL,"
			lstrSql = lstrSql & "  `medkw` double DEFAULT NULL,"
			lstrSql = lstrSql & "  `idmedidor` int(11) DEFAULT '0',"
			lstrSql = lstrSql & "  `movimento` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',"
			lstrSql = lstrSql & "  `evento` varchar(10) NOT NULL DEFAULT '',"
			lstrSql = lstrSql & "  `idauditoria` int(11) NOT NULL AUTO_INCREMENT,"
			lstrSql = lstrSql & "  PRIMARY KEY (`idauditoria`)"
			lstrSql = lstrSql & ") ENGINE=InnoDB DEFAULT CHARSET=latin1  "
			objExecAtualizaBancoDados.QUERY_NAME = lstrSql
			repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

			lstrSql = ""
			lstrSql = lstrSql & "CREATE TABLE IF NOT EXISTS `unida_lc_auditoria` ("
			lstrSql = lstrSql & "  `unid_id` int(11) DEFAULT NULL,"
			lstrSql = lstrSql & "  `cons_id` int(11) DEFAULT NULL,"
			lstrSql = lstrSql & "  `pagto_duvidoso` tinyint(1) NOT NULL DEFAULT '0',"
			lstrSql = lstrSql & "  `banco` varchar(3) DEFAULT NULL,"
			lstrSql = lstrSql & "  `agenc` varchar(4) DEFAULT NULL,"
			lstrSql = lstrSql & "  `cc` varchar(12) DEFAULT NULL,"
			lstrSql = lstrSql & "  `grupo` char(2) DEFAULT NULL,"
			lstrSql = lstrSql & "  `ta` varchar(6) DEFAULT NULL,"
			lstrSql = lstrSql & "  `clas` varchar(4) DEFAULT NULL,"
			lstrSql = lstrSql & "  `fase` char(1) DEFAULT NULL,"
			lstrSql = lstrSql & "  `situacao` char(1) DEFAULT NULL,"
			lstrSql = lstrSql & "  `ilum` char(1) DEFAULT NULL,"
			lstrSql = lstrSql & "  `tensao` varchar(10) DEFAULT NULL,"
			lstrSql = lstrSql & "  `faselig` varchar(4) DEFAULT NULL,"
			lstrSql = lstrSql & "  `codend` varchar(15) DEFAULT NULL,"
			lstrSql = lstrSql & "  `uf` varchar(2) DEFAULT NULL,"
			lstrSql = lstrSql & "  `cep` varchar(10) DEFAULT NULL,"
			lstrSql = lstrSql & "  `cidade` varchar(60) DEFAULT NULL,"
			lstrSql = lstrSql & "  `distrito` varchar(60) DEFAULT NULL,"
			lstrSql = lstrSql & "  `bairro` varchar(60) DEFAULT NULL,"
			lstrSql = lstrSql & "  `end` varchar(60) DEFAULT NULL,"
			lstrSql = lstrSql & "  `numero` varchar(10) DEFAULT NULL,"
			lstrSql = lstrSql & "  `complement` varchar(100) DEFAULT NULL,"
			lstrSql = lstrSql & "  `codend_entrega` varchar(15) DEFAULT NULL,"
			lstrSql = lstrSql & "  `uf_entrega` varchar(2) DEFAULT NULL,"
			lstrSql = lstrSql & "  `cep_entrega` varchar(10) DEFAULT NULL,"
			lstrSql = lstrSql & "  `cid_entrega` varchar(60) DEFAULT NULL,"
			lstrSql = lstrSql & "  `dis_entrega` varchar(60) DEFAULT NULL,"
			lstrSql = lstrSql & "  `bai_entrega` varchar(60) DEFAULT NULL,"
			lstrSql = lstrSql & "  `end_entrega` varchar(60) DEFAULT NULL,"
			lstrSql = lstrSql & "  `num_entrega` varchar(10) DEFAULT NULL,"
			lstrSql = lstrSql & "  `compl_entrega` varchar(100) DEFAULT NULL,"
			lstrSql = lstrSql & "  `diavenc` char(2) NOT NULL DEFAULT '00',"
			lstrSql = lstrSql & "  `cpf_cnpj` varchar(15) DEFAULT NULL,"
			lstrSql = lstrSql & "  `rg` varchar(25) DEFAULT NULL,"
			lstrSql = lstrSql & "  `insc` varchar(25) DEFAULT NULL,"
			lstrSql = lstrSql & "  `pessoa` char(1) DEFAULT NULL,"
			lstrSql = lstrSql & "  `nome` varchar(85) DEFAULT NULL,"
			lstrSql = lstrSql & "  `circuito` varchar(10) DEFAULT NULL,"
			lstrSql = lstrSql & "  `lacre` varchar(100) DEFAULT NULL,"
			lstrSql = lstrSql & "  `ap_saude` char(1) DEFAULT 'N',"
			lstrSql = lstrSql & "  `tp_ligacao` varchar(15) DEFAULT 'A',"
			lstrSql = lstrSql & "  `regiao` varchar(1) NOT NULL DEFAULT 'U',"
			lstrSql = lstrSql & "  `cnae` varchar(4) DEFAULT NULL,"
			lstrSql = lstrSql & "  `data_ligacao` date DEFAULT NULL,"
			lstrSql = lstrSql & "  `data_religacao` date DEFAULT NULL,"
			lstrSql = lstrSql & "  `data_desligamento` date DEFAULT NULL,"
			lstrSql = lstrSql & "  `data_entrada` date DEFAULT NULL,"
			lstrSql = lstrSql & "  `data_corte` date DEFAULT NULL,"
			lstrSql = lstrSql & "  `data_contrato_demanda` date DEFAULT NULL,"
			lstrSql = lstrSql & "  `ultleit` double DEFAULT NULL,"
			lstrSql = lstrSql & "  `final` double DEFAULT NULL,"
			lstrSql = lstrSql & "  `dem_contr_fp` double DEFAULT NULL,"
			lstrSql = lstrSql & "  `dem_contr_ponta` double DEFAULT NULL,"
			lstrSql = lstrSql & "  `demanda_contr` double DEFAULT NULL,"
			lstrSql = lstrSql & "  `agrupa` int(11) NOT NULL DEFAULT '1',"
			lstrSql = lstrSql & "  `instal` varchar(10) DEFAULT NULL,"
			lstrSql = lstrSql & "  `const` double DEFAULT NULL,"
			lstrSql = lstrSql & "  `const_consumo` double DEFAULT NULL,"
			lstrSql = lstrSql & "  `const_demanda` double DEFAULT NULL,"
			lstrSql = lstrSql & "  `const_kvarh` double DEFAULT NULL,"
			lstrSql = lstrSql & "  `const_cons_p` double DEFAULT NULL,"
			lstrSql = lstrSql & "  `const_cons_fp` double DEFAULT NULL,"
			lstrSql = lstrSql & "  `const_demanda_fp` double DEFAULT NULL,"
			lstrSql = lstrSql & "  `const_kvarh_p` double DEFAULT NULL,"
			lstrSql = lstrSql & "  `const_kvarh_fp` double DEFAULT NULL,"
			lstrSql = lstrSql & "  `dtafabmedidor` date DEFAULT NULL,"
			lstrSql = lstrSql & "  `idtipo_medidor` int(11) NOT NULL DEFAULT '0',"
			lstrSql = lstrSql & "  `marca` varchar(15) DEFAULT NULL,"
			lstrSql = lstrSql & "  `num` varchar(11) DEFAULT '',"
			lstrSql = lstrSql & "  `medkvark` varchar(11) DEFAULT NULL,"
			lstrSql = lstrSql & "  `eletr` varchar(9) DEFAULT NULL,"
			lstrSql = lstrSql & "  `medkw` double DEFAULT NULL,"
			lstrSql = lstrSql & "  `idmedidor` int(11) DEFAULT '0',"
			lstrSql = lstrSql & "  `movimento` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',"
			lstrSql = lstrSql & "  `evento` varchar(10) NOT NULL DEFAULT '',"
			lstrSql = lstrSql & "  `idauditoria` int(11) NOT NULL AUTO_INCREMENT,"
			lstrSql = lstrSql & "  PRIMARY KEY (`idauditoria`)"
			lstrSql = lstrSql & ") ENGINE=InnoDB DEFAULT CHARSET=latin1  "
			objExecAtualizaBancoDados.QUERY_NAME = lstrSql
			repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)
			'
			' Verifica se existe usuário LinkCenter
			'
			ilngIdUsuarioLC = 0
			lstrSql = ""
			lstrSql = lstrSql & "SELECT * "
			lstrSql = lstrSql & "  FROM `usuarios`"
			lstrSql = lstrSql & " WHERE `usuario` = 'LinkCenter' "
			objAtualizaBancoDados.QUERY_NAME = lstrSql
			dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

			If dt.Rows.Count > 0 Then
				For Each row As DataRow In dt.Rows
					ilngIdUsuarioLC = CInt(IIf(IsDBNull(row.Item("id")), 0, row.Item("id")))
				Next
			End If
			'	
			If ilngIdUsuarioLC = 0 Then

				lstrSql = "INSERT INTO `usuarios` (`nome`,`usuario`,`senha`,`matricula`,`idgrupos`,`opcao`,`recman`,`consumidor_criar_novo`,`consumidor_salvar`,`unidade_consumidora_criarnovo`,`unidade_consumidora_excluir`,`unidade_consumidora_historico`,`unidade_consumidora_salvar`,`faturas_demonstrativo`,`faturas_segunda_via`,`faturas_pedido_prazo`,`faturas_todos_aberto`,`faturas_aberto_cortado`,`faturas_reserva_judice`,`faturas_pagas`,`faturas_ultimos_consumos`,`servicos_convenios`,`servicos_extrato`,`servicos_historico`,`servicos_nis`,`servicos_ordem_servico`,`servicos_reclamacao`,`servicos_ultimas_ordens_servicos`,`servicos_ultimas_reclamacoes`,`atendimento_comercial_salvar`,`situacao`,`caixa_numero`) "
				lstrSql = lstrSql & "VALUES ('Link Center','LinkCenter','cbb918c884c0295a76ef4557f5402847','',4,b'1',b'0',b'0',b'0',b'0',b'0',b'0',b'0',b'0',b'0',b'0',b'0',b'0',b'0',b'0',b'0',b'0',b'0',b'0',b'0',b'0',b'0',b'0',b'0',b'0','Ativo',0);"
				objExecAtualizaBancoDados.QUERY_NAME = lstrSql
				repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

				lstrSql = ""
				lstrSql = lstrSql & "SELECT * "
				lstrSql = lstrSql & "  FROM `usuarios`"
				lstrSql = lstrSql & " WHERE `usuario` = 'LinkCenter' "
				objAtualizaBancoDados.QUERY_NAME = lstrSql
				dt1 = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

				If dt1.Rows.Count > 0 Then
					For Each row1 As DataRow In dt1.Rows
						ilngIdUsuarioLC = CInt(IIf(IsDBNull(row1.Item("id")), 0, row1.Item("id")))
					Next
				End If
			End If
		Catch ex As Exception
			Throw New Exception("Ocorreu o Erro: " & Err.Description & " - Rotina:  frmImportaLinkCenter_Load" & "", ex)

			'MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: frmImportaLinkCenter_Load" & "", "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 Function VerificaDadosObrigatorios() As Boolean
		'
		Dim lstrSql As String
		Dim lstrMensagem As String = "Antes de realizar a importação dos dados do Link Center, é necessário que você verifique alguns dados inconsistentes: " & vbCrLf

		Try
			Me.lblInfo.Text = " Verificando Informações de Consumidores e Unidades Consumidoras"
			Call Me.lblInfo.Refresh()

			' Busca no Link Center Unidades Consumidoras que tem a ROTA = null ou 0

			lstrSql = ""
			lstrSql = lstrSql & " SELECT " & vbCrLf
			lstrSql = lstrSql & " `zona`, " & vbCrLf
			lstrSql = lstrSql & " `livro`, " & vbCrLf
			lstrSql = lstrSql & " `conta`, " & vbCrLf
			lstrSql = lstrSql & " IF(IFNULL(`rota`, 0) = 0, 'Rota', '') AS `campoInvalido` " & vbCrLf
			lstrSql = lstrSql & " FROM " & gstrBDCLinkCenter & ".`uc`" & vbCrLf
			lstrSql = lstrSql & " WHERE "
			lstrSql = lstrSql & " (IFNULL(`rota`, 0) = 0 OR `rota` = '') " & vbCrLf

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

			If dt.Rows.Count > 0 Then
				Dim i = 0
				For Each row As DataRow In dt.Rows
					i += 1
					lstrMensagem &= "Campo: " & row("campoInvalido") & " - " & "ZLC: " & row("zona") & "." & row("livro") & "." & row("conta") & vbCrLf
				Next
				MessageBox.Show(lstrMensagem, "Aviso de Inconsistencia de Dados", MessageBoxButtons.OK, MessageBoxIcon.Information)
				Return False
			Else
				Return True
			End If
		Catch ex As Exception
			Throw New Exception("Ocorreu o Erro: " & Err.Description & " - Rotina: Etapa001" & "", ex)
			Exit Function
		End Try

	End Function


	Private Sub Processa()
		Try
			Me.lblRegEtapa(0).Text = ""
			Me.lblRegEtapa(1).Text = ""
			Me.lblRegEtapa(2).Text = ""
			Me.lblRegEtapa(3).Text = ""
			Me.lblRegEtapa(4).Text = ""
			Me.lblRegEtapa(5).Text = ""
			Me.lblRegEtapa(6).Text = ""
			Me.lblRegEtapa(7).Text = ""


			If Me.chkMedidores.CheckState = System.Windows.Forms.CheckState.Unchecked Then
				Me.lblEtapas.Text = " Etapa 001 - Inclusão de novos consumidores"
				Call Etapa001() 'Importa novos consumidores e atualiza o LC com o consreg_fatwin
				If Me.chkConsumidor.CheckState = System.Windows.Forms.CheckState.Unchecked Then
					Me.lblEtapas.Text = " Etapa 002 - Atualização dos dados dos consumidores"
					Call Etapa002() 'Atualiza consumidores do LC para o FW
				End If
			End If

			If Me.chkLogradouro.CheckState = System.Windows.Forms.CheckState.Unchecked Then
				If Me.chkConsumidor.CheckState = System.Windows.Forms.CheckState.Unchecked Then
					Me.lblEtapas.Text = " Etapa 003  - inclusão de novos medidores"
					Call Etapa003() 'Importa novos medidores
					Me.lblEtapas.Text = " Etapa 004 - atualização dos dados dos medidores" 'verifica troca de medidores
					Call Etapa004(0) 'Atualiza medidores do LC para FW
				End If
			End If

			If Me.chkMedidores.CheckState = System.Windows.Forms.CheckState.Unchecked Then
				If Me.chkConsumidor.CheckState = System.Windows.Forms.CheckState.Unchecked Then
					Me.lblEtapas.Text = " Etapa 005 - Inclusão de novas unidades consumidoras"
					Call Etapa005() 'Importa novas unidade consumidoras e atualiza o LC com o unid_id_fatwin
					Me.lblEtapas.Text = " Etapa 006 - Atualização dos dados das unidades consumidoras"
					Call Etapa006() 'Atualiza unidade consumidora
					If Me.chkLogradouro.CheckState = System.Windows.Forms.CheckState.Unchecked Then
						Me.lblEtapas.Text = " Etapa 007 - Movimentação dos medidores nas unidades consumidoras"
						Call Etapa007()
						If gstrEmpresa_CapSocialAutomatic = "S" Then
							Me.lblEtapas.Text = " Etapa 008 - Capital Social"
							Call Etapa008()
						End If
					End If
				End If
			End If

		Catch ex As Exception
			Throw New Exception("Ocorreu o Erro: " & Err.Description & " - Rotina: Processa" & "", ex)
			Exit Sub
		End Try
	End Sub

	Private Sub Etapa001()
		Dim ldblRowAfec As Double
		Dim ldteBase As Date
		Dim lblnExi As Boolean
		Dim llngFW_ConsReg As Integer
		'
		Dim bintid_antigo As Long
		Dim llngLC_Id As Integer
		Dim lstrLC_Nome As String
		Dim lstrLC_Tipo As String
		Dim lstrLC_Cpf_cnpj As String
		Dim lstrLC_Rg As String
		Dim lstrLC_Ie As String
		Dim lstrLC_Email As String
		Dim lstrLC_DDDTelef As String
		Dim lstrLC_Telefone As String
		Dim lstrLC_DDDCelu As String
		Dim lstrLC_Celular As String
		Dim lstrLC_DDDFax As String
		Dim lstrLC_Fax As String
		Dim lstrLC_Observacao As String
		Dim lstrLC_Nome_pai As String
		Dim lstrLC_Nome_mae As String
		Dim lstrLC_Conta_corrente As String
		Dim lstrLC_Agencia As String
		Dim llngLC_Id_banco As Integer
		Dim lstrLC_Descricao_banco As String
		Dim ldteLC_Data_nascimento As Date
		Dim lstrLC_Uf_nascimento As String
		Dim llngLC_Matricula As Integer
		Dim lstrLC_Profissao As String
		Dim lstrLC_Estado_civil As String
		Dim llngLC_Estado_civil As Integer
		Dim ldteLC_Data_cadastro As Date
		Dim llngLC_Consreg_fatwin As Integer
		Dim llngLC_Id_endereco As Double
		Dim lstrLC_Endereco As String
		Dim lstrLC_Numero As String
		Dim lstrLC_Complemento As String
		Dim lstrLC_Quadra As String
		Dim lstrLC_Lote As String
		Dim lstrLC_Bairro As String
		Dim lstrLC_Distrito As String
		Dim lstrLC_Cidade As String
		Dim lstrLC_Cep As String
		Dim lstrLC_Uf As String
		Dim intid_bairro_antigo As Integer
		Dim intid_cidade_antigo As Integer
		'
		Dim lstrSql As String

		Try

			ldteBase = Date.Now
			Me.lblInfo.Text = " Buscando novos consumidores no L.C. aguarde..."
			Call Me.lblInfo.Refresh()
			'
			'Busca os consumidores no sistema link CENTER
			'
			lstrSql = ""
			lstrSql = lstrSql & "SELECT " & vbCrLf
			lstrSql = lstrSql & "       `lc_c`.`id_antigo`, `lc_c`.`id`, `lc_c`.`nome`, `lc_c`.`tipo`, `lc_c`.`cpf_cnpj`, `lc_c`.`rg`, `lc_c`.`ie`, `lc_c`.`email`, `lc_c`.`telefone`, `lc_c`.`celular`, `lc_c`.`fax`, `lc_c`.`observacao`, `lc_c`.`nome_pai`, `lc_c`.`nome_mae`, `lc_c`.`data_nascimento`, `lc_c`.`uf_nascimento`, `lc_c`.`matricula`, " & vbCrLf
			lstrSql = lstrSql & "       `lc_c`.`id_banco`, `lc_c`.`descricao_banco`, `lc_c`.`agencia`, `lc_c`.`conta_corrente`, `lc_c`.`profissao`, `lc_c`.`estado_civil`, `lc_c`.`data_cadastro`, `lc_c`.`consreg_fatwin`, " & vbCrLf
			lstrSql = lstrSql & "       `lc_c`.`id_endereco`, `lc_c`.`endereco`, `lc_c`.`numero`, `lc_c`.`complemento`, `lc_c`.`quadra`, `lc_c`.`lote`, `lc_c`.`bairro`, `lc_c`.`distrito`, `lc_c`.`municipio`, `lc_c`.`cep`, `lc_c`.`uf`, `lc_c`.`id_bairro_antigo`, `lc_c`.`id_cidade_antigo` " & vbCrLf
			lstrSql = lstrSql & "  FROM " & gstrBDCLinkCenter & ".`consumidor` `lc_c` " & vbCrLf
			lstrSql = lstrSql & " WHERE NOT EXISTS(SELECT * FROM `consum` `fw_c` WHERE `fw_c`.`consreg` = `lc_c`.`consreg_fatwin`)"
			'lstrSql = lstrSql & "And `data_cadastro` BETWEEN '2022-10-01' AND '2022-11-30'"

			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, " NOVOS CONSUMIDORES ")
				For Each row As DataRow In dt.Rows

					bintid_antigo = CLng(IIf(IsDBNull(row.Item("id_antigo")), "0", row.Item("id_antigo")))
					llngLC_Id = CInt(IIf(IsDBNull(row.Item("id")), "0", row.Item("id")))
					lstrLC_Nome = VB.Left(Trim(UCase(IIf(IsDBNull(row.Item("nome")), "", row.Item("nome")))), 85)
					lstrLC_Tipo = Trim(IIf(IsDBNull(row.Item("tipo")), "F", row.Item("tipo")))
					lstrLC_Cpf_cnpj = Trim(IIf(IsDBNull(row.Item("cpf_cnpj")), "", row.Item("cpf_cnpj")))
					lstrLC_Rg = VB.Left(Trim(IIf(IsDBNull(row.Item("rg")), "", row.Item("rg"))), 25)
					lstrLC_Ie = VB.Left(Trim(IIf(IsDBNull(row.Item("ie")), "", row.Item("ie"))), 25)
					lstrLC_Email = VB.Left(Trim(IIf(IsDBNull(row.Item("email")), "", row.Item("email"))), 40)
					lstrLC_Telefone = Trim(IIf(IsDBNull(row.Item("telefone")), "", row.Item("telefone")))
					lstrLC_DDDTelef = Trim(Str_Retira(VB.Left(lstrLC_Telefone, CInt(InStr(1, lstrLC_Telefone, ")"))), "()"))
					lstrLC_Telefone = Trim(Str_Retira(Mid(lstrLC_Telefone, CInt(InStr(1, lstrLC_Telefone, ")")) + 1), "().-/"))
					lstrLC_Celular = Trim(IIf(IsDBNull(row.Item("celular")), "", row.Item("celular")))
					lstrLC_DDDCelu = Trim(Str_Retira(VB.Left(lstrLC_Celular, CInt(InStr(1, lstrLC_Celular, ")"))), "()"))
					lstrLC_Celular = Trim(Str_Retira(Mid(lstrLC_Celular, CInt(InStr(1, lstrLC_Celular, ")")) + 1), "().-/"))
					lstrLC_Fax = Trim(IIf(IsDBNull(row.Item("fax")), "", row.Item("fax")))
					lstrLC_DDDFax = Trim(Str_Retira(VB.Left(lstrLC_Fax, CInt(InStr(1, lstrLC_Fax, ")"))), "()"))
					lstrLC_Fax = Trim(Str_Retira(Mid(lstrLC_Fax, CInt(InStr(1, lstrLC_Fax, ")")) + 1), "().-/"))
					lstrLC_Observacao = Trim(IIf(IsDBNull(row.Item("observacao")), "", row.Item("observacao")))
					lstrLC_Nome_pai = VB.Left(Trim(UCase(IIf(IsDBNull(row.Item("nome_pai")), "", row.Item("nome_pai")))), 60)
					lstrLC_Nome_mae = VB.Left(Trim(UCase(IIf(IsDBNull(row.Item("nome_mae")), "", row.Item("nome_mae")))), 60)
					lstrLC_Conta_corrente = Trim(IIf(IsDBNull(row.Item("conta_corrente")), "", row.Item("conta_corrente")))
					lstrLC_Agencia = Trim(IIf(IsDBNull(row.Item("agencia")), "", row.Item("agencia")))
					llngLC_Id_banco = CInt(IIf(IsDBNull(row.Item("id_banco")), "0", row.Item("id_banco")))
					lstrLC_Descricao_banco = Trim(IIf(IsDBNull(row.Item("descricao_banco")), "", row.Item("descricao_banco")))
					ldteLC_Data_nascimento = CDate(IIf(IsDBNull(row.Item("data_nascimento")), "1900-01-01", row.Item("data_nascimento")))
					lstrLC_Uf_nascimento = Trim(IIf(IsDBNull(row.Item("uf_nascimento")), "", row.Item("uf_nascimento")))
					llngLC_Matricula = CInt(VB.Left(IIf(IsDBNull(row.Item("matricula")), 0, row.Item("matricula")), 9))
					lstrLC_Profissao = Trim(IIf(IsDBNull(row.Item("profissao")), "", row.Item("profissao")))
					lstrLC_Estado_civil = Trim(IIf(IsDBNull(row.Item("estado_civil")), "", row.Item("estado_civil")))
					llngLC_Estado_civil = BuscaCodigoEstadoCivil(lstrLC_Estado_civil)
					ldteLC_Data_cadastro = CDate(IIf(IsDBNull(row.Item("data_cadastro")), "1900-01-01", row.Item("data_cadastro")))
					llngLC_Consreg_fatwin = CInt(IIf(IsDBNull(row.Item("consreg_fatwin")), "0", row.Item("consreg_fatwin")))
					llngLC_Id_endereco = CInt(IIf(IsDBNull(row.Item("id_endereco")), "0", row.Item("id_endereco")))
					lstrLC_Uf = Trim(IIf(IsDBNull(row.Item("uf")), "", row.Item("uf")))
					lstrLC_Cep = Trim(IIf(IsDBNull(row.Item("cep")), "", row.Item("cep")))
					lstrLC_Cidade = Trim(IIf(IsDBNull(row.Item("municipio")), "", row.Item("municipio")))
					lstrLC_Distrito = Trim(IIf(IsDBNull(row.Item("distrito")), "", row.Item("distrito")))
					lstrLC_Bairro = Trim(IIf(IsDBNull(row.Item("bairro")), "", row.Item("bairro")))
					lstrLC_Endereco = Trim(IIf(IsDBNull(row.Item("endereco")), "", row.Item("endereco")))
					lstrLC_Numero = Trim(IIf(IsDBNull(row.Item("numero")), "", row.Item("numero")))
					lstrLC_Complemento = Trim(IIf(IsDBNull(row.Item("complemento")), "", row.Item("complemento")))
					lstrLC_Quadra = Trim(IIf(IsDBNull(row.Item("quadra")), "", row.Item("quadra")))
					lstrLC_Lote = Trim(IIf(IsDBNull(row.Item("lote")), "", row.Item("lote")))

					intid_bairro_antigo = CInt(IIf(IsDBNull(row.Item("id_bairro_antigo")), "0", row.Item("id_bairro_antigo")))
					intid_cidade_antigo = CInt(IIf(IsDBNull(row.Item("id_cidade_antigo")), "0", row.Item("id_cidade_antigo")))

					lblnExi = False

					lstrLC_Cpf_cnpj = Str_Retira(Trim(IIf(IsDBNull(row.Item("cpf_cnpj")), "", row.Item("cpf_cnpj"))), ".-/")
					'
					'Busca consumidor pelo CPF/CNPJ
					'
					If CLng(lstrLC_Cpf_cnpj) <> 0 Then
						lstrSql = ""
						lstrSql = lstrSql & "SELECT * "
						lstrSql = lstrSql & "  FROM `consum` "
						lstrSql = lstrSql & " WHERE `cgc` = '" & lstrLC_Cpf_cnpj & "' "
						objAtualizaBancoDados.QUERY_NAME = lstrSql
						dt1 = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

						If dt1.Rows.Count > 0 Then
							For Each row1 As DataRow In dt1.Rows
								llngFW_ConsReg = CInt(IIf(IsDBNull(row1.Item("consreg")), 0, row1.Item("consreg")))
							Next
							lblnExi = True
						Else
							llngFW_ConsReg = 0
							lblnExi = False
						End If
					End If

					dt1 = Nothing

					If Not lblnExi Then

						lstrSql = ""
						lstrSql = lstrSql & "matricula   =  " & llngLC_Matricula & ", "
						lstrSql = lstrSql & "nome        = '" & Replace(lstrLC_Nome, "'", "\'") & "', "
						lstrSql = lstrSql & "cgc         = '" & lstrLC_Cpf_cnpj & "', "
						lstrSql = lstrSql & "cpf_cnpj    = '" & lstrLC_Cpf_cnpj & "', "
						lstrSql = lstrSql & "insc        = '" & lstrLC_Ie & "', "
						lstrSql = lstrSql & "rg          = '" & lstrLC_Rg & "', "
						lstrSql = lstrSql & "dddtel      = '" & lstrLC_DDDTelef & "', "
						lstrSql = lstrSql & "telefone    = '" & lstrLC_Telefone & "', "
						lstrSql = lstrSql & "dddfax      = '" & lstrLC_DDDFax & "', "
						lstrSql = lstrSql & "fax         = '" & lstrLC_Fax & "', "
						lstrSql = lstrSql & "dddcel      = '" & lstrLC_DDDCelu & "', "
						lstrSql = lstrSql & "cel         = '" & lstrLC_Celular & "', "
						lstrSql = lstrSql & "email       = '" & lstrLC_Email & "', "
						lstrSql = lstrSql & "icms        = NULL, "
						lstrSql = lstrSql & "cip         = NULL, "
						lstrSql = lstrSql & "nomepai     = '" & Replace(lstrLC_Nome_pai, "'", "\'") & "', "
						lstrSql = lstrSql & "nomemae     = '" & Replace(lstrLC_Nome_mae, "'", "\'") & "', "
						lstrSql = lstrSql & "et          = 0, "
						lstrSql = lstrSql & "idestado_civil  =  " & llngLC_Estado_civil & ", "
						lstrSql = lstrSql & "estado_civil    = '" & UCase(VB.Left(lstrLC_Estado_civil, 1)) & "', "
						lstrSql = lstrSql & "idprofissao     = NULL, "
						lstrSql = lstrSql & "profissao       = '" & Replace(UCase(lstrLC_Profissao), "'", "\'") & "', "
						lstrSql = lstrSql & "idnacionalidade = NULL, "

						If ldteLC_Data_cadastro <> CDate("01/01/1900") Then
							lstrSql = lstrSql & "dt_cadastro = '" & VB6.Format(ldteLC_Data_cadastro, "yyyy-MM-dd") & "', "
						Else
							lstrSql = lstrSql & "dt_cadastro = '" & VB6.Format(ldteBase, "yyyy-MM-dd") & "', "
						End If
						If ldteLC_Data_nascimento <> CDate("01/01/1900") Then
							lstrSql = lstrSql & "dt_nascto   = '" & VB6.Format(ldteLC_Data_nascimento, "yyyy-MM-dd") & "', "
						Else
							lstrSql = lstrSql & "dt_nascto   = NULL, "
						End If

						lstrSql = lstrSql & "codend     = '" & llngLC_Id_endereco & "', "
						lstrSql = lstrSql & "id_antigo  = " & bintid_antigo & ", "
						lstrSql = lstrSql & "uf         = '" & Replace(lstrLC_Uf, "'", "\'") & "', "
						lstrSql = lstrSql & "cep        = '" & lstrLC_Cep & "', "

						Dim rdescrCidade As String = fBuscaBairroCidade_CEPNSS(intid_cidade_antigo, intid_bairro_antigo, "C", "D")
						lstrSql = lstrSql & "cidade     = '" & IIf(rdescrCidade = "", lstrLC_Cidade, rdescrCidade) & "', "
						'
						Dim rCodCidade As Integer = fBuscaBairroCidade_CEPNSS(intid_cidade_antigo, intid_bairro_antigo, "C", "C")
						lstrSql = lstrSql & "cod_cidade     = " & IIf(rCodCidade = 0, intid_cidade_antigo, rCodCidade) & ", "

						lstrSql = lstrSql & "distrito   = '" & Replace(lstrLC_Distrito, "'", "\'") & "', "

						Dim rdescrBairro As String = fBuscaBairroCidade_CEPNSS(intid_cidade_antigo, intid_bairro_antigo, "B", "D")
						lstrSql = lstrSql & "bairro     = '" & IIf(rdescrBairro = "", lstrLC_Bairro, rdescrBairro) & "', "

						Dim rCodBairro As Integer = fBuscaBairroCidade_CEPNSS(intid_cidade_antigo, intid_bairro_antigo, "B", "C")
						lstrSql = lstrSql & "cod_bairro     = " & IIf(rCodBairro = 0, intid_bairro_antigo, rCodBairro) & ", "

						lstrSql = lstrSql & "end        = '" & Replace(lstrLC_Endereco, "'", "\'") & "', "
						lstrSql = lstrSql & "numero     = '" & Replace(lstrLC_Numero, "'", "\'") & "', "
						lstrSql = lstrSql & "complement = '" & Replace(lstrLC_Complemento, "'", "\'") & "', "
						lstrSql = lstrSql & "quadra     = '" & Replace(lstrLC_Quadra, "'", "\'") & "', "
						lstrSql = lstrSql & "lote       = '" & Replace(lstrLC_Lote, "'", "\'") & "', "
						lstrSql = lstrSql & "observacao  = CONVERT('" & Replace(lstrLC_Observacao, "'", "\'") & "' USING latin1), "


						objExecAtualizaBancoDados.QUERY_NAME = "INSERT INTO `consum` SET " & lstrSql & "itenshist   = CONVERT('IMPORTADO PELO L.C. EM " & VB6.Format(ldteBase, "yyyy-MM-dd HH:NN:SS") & "' USING latin1) "
						repositorioAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

						Me.lblRegEtapa(0).Text = VB6.Format(CDbl(IIf(IsNumeric(Me.lblRegEtapa(0).Text), Me.lblRegEtapa(0).Text, 0)) + ldblRowAfec, "#0")

						lstrSql = ""
						lstrSql = "SELECT LAST_INSERT_ID()"
						objAtualizaBancoDados.QUERY_NAME = lstrSql
						dt2 = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

						If dt2.Rows.Count > 0 Then
							For Each row2 As DataRow In dt2.Rows
								llngFW_ConsReg = CInt(IIf(IsDBNull(row2.Item(0)), 0, row2.Item(0)))
								Call AtualizaConsRegLC(llngFW_ConsReg, llngLC_Id)
								'Call AtualizaConsRegLC(llngLC_Id, llngLC_Id)
							Next
						End If

						'Call InsereAuditoriaConsumidor(llngFW_ConsReg, "INCLUIDO")
						Call InsereAuditoriaConsumidor(llngLC_Id, "INCLUIDO")

					Else
						'Call AtualizaConsRegLC(llngFW_ConsReg, llngLC_Id)
						'Call AtualizaConsRegLC(llngLC_Id, llngLC_Id)
					End If

					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1,  , " NOVOS CONSUMIDORES ")
				Next
			End If
		Catch ex As Exception
			Throw New Exception("Ocorreu o Erro: " & Err.Description & " - Rotina: Etapa001" & "", ex)
			'MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: Etapa001" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try

	End Sub

	Private Sub Etapa002()
		Dim ldblRowAfec As Double
		Dim ldteBase As Date
		'
		'Fatwin
		'
		Dim llngFW_ConsReg As Integer
		Dim lstrFW_Nome As String
		Dim lstrFW_Cpf_cnpj As String
		Dim lstrFW_Rg As String
		Dim lstrFW_Ie As String
		Dim lstrFW_Email As String
		Dim lstrFW_DDDTelef As String
		Dim lstrFW_Telefone As String
		Dim lstrFW_DDDCelu As String
		Dim lstrFW_Celular As String
		Dim lstrFW_DDDFax As String
		Dim lstrFW_Fax As String
		Dim lstrFW_Nome_pai As String
		Dim lstrFW_Nome_mae As String
		Dim ldteFW_Data_nascimento As Date
		Dim llngFW_Matricula As Integer
		Dim llngFW_Idestado_civil As Integer
		Dim lstrLC_Id_endereco_fatwin As String
		Dim lstrFW_Codend As String
		Dim lstrFW_Uf As String
		Dim lstrFW_Cep As String
		Dim lstrFW_Cidade As String
		Dim lstrFW_Distrito As String
		Dim lstrFW_Bairro As String
		Dim lstrFW_End As String
		Dim lstrFW_Numero As String
		Dim lstrFW_Complement As String
		Dim lstrFW_Quadra As String
		Dim lstrFW_Lote As String
		'
		'Link Center
		'
		Dim llngLC_Id As Integer
		Dim lstrLC_Nome As String
		Dim lstrLC_Tipo As String
		Dim lstrLC_Cpf_cnpj As String
		Dim lstrLC_Rg As String
		Dim lstrLC_Ie As String
		Dim lstrLC_Email As String
		Dim lstrLC_DDDTelef As String
		Dim lstrLC_Telefone As String
		Dim lstrLC_DDDCelu As String
		Dim lstrLC_Celular As String
		Dim lstrLC_DDDFax As String
		Dim lstrLC_Fax As String
		Dim lstrLC_Observacao As String
		Dim lstrLC_Nome_pai As String
		Dim lstrLC_Nome_mae As String
		Dim lstrLC_Conta_corrente As String
		Dim lstrLC_Agencia As String
		Dim llngLC_Id_banco As Integer
		Dim lstrLC_Descricao_banco As String
		Dim ldteLC_Data_nascimento As Date
		Dim lstrLC_Uf_nascimento As String
		Dim llngLC_Matricula As Integer
		Dim lstrLC_Profissao As String
		Dim lstrLC_Estado_civil As String
		Dim llngLC_Estado_civil As Integer
		Dim ldteLC_Data_cadastro As Date
		Dim llngLC_Consreg_fatwin As Integer
		Dim llngLC_Id_endereco As Double
		Dim lstrLC_Endereco As String
		Dim lstrLC_Numero As String
		Dim lstrLC_Complemento As String
		Dim lstrLC_Quadra As String
		Dim lstrLC_Lote As String
		Dim lstrLC_Bairro As String
		Dim lstrLC_Distrito As String
		Dim lstrLC_Cidade As String
		Dim lstrLC_Cep As String
		Dim lstrLC_Uf As String
		'
		Dim lstrSql As String
		Try

			'ldteBase = Date.Now
			ldteBase = BuscaDataBD()

			Me.lblInfo.Text = " Buscando todos consumidores no L.C. aguarde..."
			Call Me.lblInfo.Refresh()

			lstrSql = ""
			lstrSql = lstrSql & "SELECT " & vbCrLf
			lstrSql = lstrSql & "       `lc_c`.`id`, `lc_c`.`nome`, `lc_c`.`tipo`, `lc_c`.`cpf_cnpj`, `lc_c`.`rg`, `lc_c`.`ie`, `lc_c`.`email`, `lc_c`.`telefone`, `lc_c`.`celular`, `lc_c`.`fax`, `lc_c`.`observacao`, `lc_c`.`nome_pai`, `lc_c`.`nome_mae`, `lc_c`.`data_nascimento`, `lc_c`.`uf_nascimento`, `lc_c`.`matricula`, " & vbCrLf
			lstrSql = lstrSql & "       `lc_c`.`id_banco`, `lc_c`.`descricao_banco`, `lc_c`.`agencia`, `lc_c`.`conta_corrente`, `lc_c`.`profissao`, `lc_c`.`estado_civil`, `lc_c`.`data_cadastro`, `lc_c`.`consreg_fatwin`, " & vbCrLf
			lstrSql = lstrSql & "       `lc_c`.`id_endereco`, `lc_c`.`endereco`, `lc_c`.`numero`, `lc_c`.`complemento`, `lc_c`.`quadra`, `lc_c`.`lote`, `lc_c`.`bairro`, `lc_c`.`distrito`, `lc_c`.`municipio`, `lc_c`.`cep`, `lc_c`.`uf` " & vbCrLf
			lstrSql = lstrSql & "  FROM " & gstrBDCLinkCenter & ".`consumidor` `lc_c` " & vbCrLf
			lstrSql = lstrSql & " WHERE (`lc_c`.`consreg_fatwin` > 0 AND `lc_c`.`consreg_fatwin` IS NOT NULL) " & vbCrLf

			If Me.chkLogradouro.CheckState = System.Windows.Forms.CheckState.Unchecked Then
				lstrSql = lstrSql & "   AND EXISTS(SELECT * " & vbCrLf
				lstrSql = lstrSql & "                FROM " & gstrBDCLinkCenter & ".`os` `lc_o` " & vbCrLf
				lstrSql = lstrSql & "               WHERE `lc_o`.`id_consumidor` = `lc_c`.`id` " & vbCrLf


				If Trim(Me.txtSequenciasOs.Text) <> "" Then
					lstrSql = lstrSql & "                 AND `lc_o`.`sequencia_os` in(" & Me.txtSequenciasOs.Text & ") " & vbCrLf
				Else
					lstrSql = lstrSql & "				AND `lc_o`.`id_class_comercial` = '30.04'" 'Classificação 30.04 é classificação para Alteração Cadastral
					lstrSql = lstrSql & "               AND (DATE(`lc_o`.`data_conclusao`) BETWEEN '" & VB6.Format(idteIni, "yyyy-MM-dd") & "' AND '" & VB6.Format(idteFim, "yyyy-MM-dd") & "') " & vbCrLf
				End If

				lstrSql = lstrSql & "             )"
			End If

			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, " CONSUMIDORES ")
				For Each row As DataRow In dt.Rows

					llngLC_Id = CInt(IIf(IsDBNull(row.Item("id")), "0", row.Item("id")))
					lstrLC_Nome = VB.Left(Trim(UCase(IIf(IsDBNull(row.Item("nome")), "", row.Item("nome")))), 85)
					lstrLC_Tipo = Trim(IIf(IsDBNull(row.Item("tipo")), "F", row.Item("tipo")))
					lstrLC_Cpf_cnpj = Str_Retira(Trim(IIf(IsDBNull(row.Item("cpf_cnpj")), "", row.Item("cpf_cnpj"))), ".-/")
					lstrLC_Rg = VB.Left(Trim(IIf(IsDBNull(row.Item("rg")), "", row.Item("rg"))), 25)
					lstrLC_Ie = VB.Left(Trim(IIf(IsDBNull(row.Item("ie")), "", row.Item("ie"))), 25)
					lstrLC_Email = VB.Left(Trim(IIf(IsDBNull(row.Item("email")), "", row.Item("email"))), 40)
					lstrLC_Telefone = Trim(IIf(IsDBNull(row.Item("telefone")), "", row.Item("telefone")))
					lstrLC_DDDTelef = Str_Retira(VB.Left(lstrLC_Telefone, CInt(InStr(1, lstrLC_Telefone, ")"))), "()")
					lstrLC_Telefone = Trim(Str_Retira(Mid(lstrLC_Telefone, CInt(InStr(1, lstrLC_Telefone, ")")) + 1), "().-/"))
					lstrLC_Celular = Trim(IIf(IsDBNull(row.Item("celular")), "", row.Item("celular")))
					lstrLC_DDDCelu = Str_Retira(VB.Left(lstrLC_Celular, CInt(InStr(1, lstrLC_Celular, ")"))), "()")
					lstrLC_Celular = Trim(Str_Retira(Mid(lstrLC_Celular, CInt(InStr(1, lstrLC_Celular, ")")) + 1), "().-/"))
					lstrLC_Fax = Trim(IIf(IsDBNull(row.Item("fax")), "", row.Item("fax")))
					lstrLC_DDDFax = Str_Retira(VB.Left(lstrLC_Fax, CInt(InStr(1, lstrLC_Fax, ")"))), "()")
					lstrLC_Fax = Trim(Str_Retira(Mid(lstrLC_Fax, CInt(InStr(1, lstrLC_Fax, ")")) + 1), "().-/"))
					lstrLC_Observacao = Trim(IIf(IsDBNull(row.Item("observacao")), "", row.Item("observacao")))
					lstrLC_Nome_pai = VB.Left(Trim(UCase(IIf(IsDBNull(row.Item("nome_pai")), "", row.Item("nome_pai")))), 60)
					lstrLC_Nome_mae = VB.Left(Trim(UCase(IIf(IsDBNull(row.Item("nome_mae")), "", row.Item("nome_mae")))), 60)
					lstrLC_Conta_corrente = Trim(IIf(IsDBNull(row.Item("conta_corrente")), "", row.Item("conta_corrente")))
					lstrLC_Agencia = Trim(IIf(IsDBNull(row.Item("agencia")), "", row.Item("agencia")))
					llngLC_Id_banco = CInt(IIf(IsDBNull(row.Item("id_banco")), "0", row.Item("id_banco")))
					lstrLC_Descricao_banco = Trim(IIf(IsDBNull(row.Item("descricao_banco")), "", row.Item("descricao_banco")))
					ldteLC_Data_nascimento = CDate(IIf(IsDBNull(row.Item("data_nascimento")), "1900-01-01", row.Item("data_nascimento")))
					lstrLC_Uf_nascimento = Trim(IIf(IsDBNull(row.Item("uf_nascimento")), "", row.Item("uf_nascimento")))
					llngLC_Matricula = CInt(VB.Left(IIf(IsDBNull(row.Item("matricula")), "0", row.Item("matricula")), 9))
					lstrLC_Profissao = Trim(IIf(IsDBNull(row.Item("profissao")), "", row.Item("profissao")))
					lstrLC_Estado_civil = Trim(IIf(IsDBNull(row.Item("estado_civil")), "", row.Item("estado_civil")))
					llngLC_Estado_civil = BuscaCodigoEstadoCivil(lstrLC_Estado_civil)
					ldteLC_Data_cadastro = CDate(IIf(IsDBNull(row.Item("data_cadastro")), "1900-01-01", row.Item("data_cadastro")))
					llngLC_Consreg_fatwin = CInt(IIf(IsDBNull(row.Item("consreg_fatwin")), "0", row.Item("consreg_fatwin")))
					llngLC_Id_endereco = CInt(IIf(IsDBNull(row.Item("id_endereco")), "0", row.Item("id_endereco")))
					lstrLC_Uf = Trim(IIf(IsDBNull(row.Item("uf")), "", row.Item("uf")))
					lstrLC_Cep = Str_Retira(Trim(IIf(IsDBNull(row.Item("cep")), "", row.Item("cep"))), ".-/")
					lstrLC_Cidade = Trim(IIf(IsDBNull(row.Item("municipio")), "", row.Item("municipio")))
					lstrLC_Distrito = Trim(IIf(IsDBNull(row.Item("distrito")), "", row.Item("distrito")))
					lstrLC_Bairro = Trim(IIf(IsDBNull(row.Item("bairro")), "", row.Item("bairro")))
					lstrLC_Endereco = Trim(IIf(IsDBNull(row.Item("endereco")), "", row.Item("endereco")))
					lstrLC_Numero = Trim(IIf(IsDBNull(row.Item("numero")), "", row.Item("numero")))
					lstrLC_Complemento = Trim(IIf(IsDBNull(row.Item("complemento")), "", row.Item("complemento")))
					lstrLC_Quadra = Trim(IIf(IsDBNull(row.Item("quadra")), "", row.Item("quadra")))
					lstrLC_Lote = Trim(IIf(IsDBNull(row.Item("lote")), "", row.Item("lote")))
					'
					'Busca consumidor pelo consreg
					'
					lstrSql = ""
					lstrSql = lstrSql & "SELECT * "
					lstrSql = lstrSql & "  FROM `consum` "
					lstrSql = lstrSql & " WHERE `consreg` = " & llngLC_Consreg_fatwin & " "
					objAtualizaBancoDados.QUERY_NAME = lstrSql
					dt1 = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

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

							llngFW_ConsReg = CInt(IIf(IsDBNull(row1.Item("consreg")), "0", row1.Item("consreg")))
							lstrFW_Nome = Trim(UCase(IIf(IsDBNull(row1.Item("nome")), "", row1.Item("nome"))))
							lstrFW_Cpf_cnpj = Str_Retira(Trim(IIf(IsDBNull(row1.Item("cgc")), "", row1.Item("cgc"))), ".-/")
							lstrFW_Rg = Trim(IIf(IsDBNull(row1.Item("rg")), "", row1.Item("rg")))
							lstrFW_Ie = Trim(IIf(IsDBNull(row1.Item("insc")), "", row1.Item("insc")))
							lstrFW_Email = Trim(IIf(IsDBNull(row1.Item("email")), "", row1.Item("email")))
							lstrFW_DDDTelef = Str_Retira(Trim(IIf(IsDBNull(row1.Item("dddtel")), "", row1.Item("dddtel"))), "().-/")
							lstrFW_Telefone = Str_Retira(Trim(IIf(IsDBNull(row1.Item("telefone")), "", row1.Item("telefone"))), "().-/")
							lstrFW_DDDCelu = Str_Retira(Trim(IIf(IsDBNull(row1.Item("dddcel")), "", row1.Item("dddcel"))), "().-/")
							lstrFW_Celular = Str_Retira(Trim(IIf(IsDBNull(row1.Item("cel")), "", row1.Item("cel"))), "().-/")
							lstrFW_DDDFax = Str_Retira(Trim(IIf(IsDBNull(row1.Item("dddfax")), "", row1.Item("dddfax"))), "().-/")
							lstrFW_Fax = Str_Retira(Trim(IIf(IsDBNull(row1.Item("fax")), "", row1.Item("fax"))), "().-/")
							lstrFW_Nome_pai = Trim(UCase(IIf(IsDBNull(row1.Item("nomepai")), "", row1.Item("nomepai"))))
							lstrFW_Nome_mae = Trim(UCase(IIf(IsDBNull(row1.Item("nomemae")), "", row1.Item("nomemae"))))
							ldteFW_Data_nascimento = CDate(IIf(IsDBNull(row1.Item("dt_nascto")), "1900-01-01", row1.Item("dt_nascto")))
							llngFW_Matricula = CInt(IIf(IsDBNull(row1.Item("matricula")), "0", row1.Item("matricula")))
							llngFW_Idestado_civil = CInt(IIf(IsDBNull(row1.Item("idestado_civil")), "0", row1.Item("idestado_civil")))
							lstrFW_Codend = Trim(IIf(IsDBNull(row1.Item("codend")), "", row1.Item("codend")))
							lstrFW_Uf = Trim(IIf(IsDBNull(row1.Item("uf")), "", row1.Item("uf")))
							lstrFW_Cep = Trim(IIf(IsDBNull(row1.Item("cep")), "", row1.Item("cep")))
							lstrFW_Cidade = Trim(IIf(IsDBNull(row1.Item("cidade")), "", row1.Item("cidade")))
							lstrFW_Distrito = Trim(IIf(IsDBNull(row1.Item("distrito")), "", row1.Item("distrito")))
							lstrFW_Bairro = Trim(IIf(IsDBNull(row1.Item("bairro")), "", row1.Item("bairro")))
							lstrFW_End = Trim(IIf(IsDBNull(row1.Item("end")), "", row1.Item("end")))
							lstrFW_Numero = Trim(IIf(IsDBNull(row1.Item("numero")), "", row1.Item("numero")))
							lstrFW_Complement = Trim(IIf(IsDBNull(row1.Item("complement")), "", row1.Item("complement")))
							lstrFW_Quadra = Trim(IIf(IsDBNull(row1.Item("quadra")), "", row1.Item("quadra")))
							lstrFW_Lote = Trim(IIf(IsDBNull(row1.Item("lote")), "", row1.Item("lote")))

							lstrSql = ""
							If Me.chkLogradouro.CheckState = System.Windows.Forms.CheckState.Unchecked Then
								If llngFW_Matricula <> llngLC_Matricula Then
									If llngLC_Matricula > 0 Then
										lstrSql = lstrSql & "matricula   =  " & llngLC_Matricula & ", "
									End If
								End If
								If lstrFW_Nome <> lstrLC_Nome Then
									If lstrLC_Nome <> "" Then
										lstrSql = lstrSql & "nome        = '" & Replace(lstrLC_Nome, "'", "\'") & "', "
									End If
								End If
								If lstrFW_Cpf_cnpj <> lstrLC_Cpf_cnpj Then
									If lstrLC_Cpf_cnpj <> "" Then
										lstrSql = lstrSql & "cgc         = '" & lstrLC_Cpf_cnpj & "', "
									End If
								End If
								If lstrFW_Ie <> lstrLC_Ie Then
									If lstrLC_Ie <> "" Then
										lstrSql = lstrSql & "insc        = '" & lstrLC_Ie & "', "
									End If
								End If
								If lstrFW_Rg <> lstrLC_Rg Then
									If lstrLC_Rg <> "" Then
										lstrSql = lstrSql & "rg          = '" & lstrLC_Rg & "', "
									End If
								End If
								If lstrFW_DDDTelef <> lstrLC_DDDTelef Then
									If lstrLC_DDDTelef <> "" Then
										lstrSql = lstrSql & "dddtel      = '" & lstrLC_DDDTelef & "', "
									End If
								End If
								If lstrFW_Telefone <> lstrLC_Telefone Then
									If lstrLC_Telefone <> "" Then
										lstrSql = lstrSql & "telefone    = '" & lstrLC_Telefone & "', "
									End If
								End If
								If lstrFW_DDDFax <> lstrLC_DDDFax Then
									If lstrLC_DDDFax <> "" Then
										lstrSql = lstrSql & "dddfax      = '" & lstrLC_DDDFax & "', "
									End If
								End If
								If lstrFW_Fax <> lstrLC_Fax Then
									If lstrLC_Fax <> "" Then
										lstrSql = lstrSql & "fax         = '" & lstrLC_Fax & "', "
									End If
								End If
								If lstrFW_DDDCelu <> lstrLC_DDDCelu Then
									If lstrLC_DDDCelu <> "" Then
										lstrSql = lstrSql & "dddcel      = '" & lstrLC_DDDCelu & "', "
									End If
								End If
								If lstrFW_Celular <> lstrLC_Celular Then
									If lstrLC_Celular <> "" Then
										lstrSql = lstrSql & "cel         = '" & lstrLC_Celular & "', "
									End If
								End If
								If lstrFW_Email <> lstrLC_Email Then
									If lstrLC_Email <> "" Then
										lstrSql = lstrSql & "email       = '" & lstrLC_Email & "', "
									End If
								End If
								If lstrFW_Nome_pai <> lstrLC_Nome_pai Then
									If lstrLC_Nome_pai <> "" Then
										lstrSql = lstrSql & "nomepai     = '" & Replace(lstrLC_Nome_pai, "'", "\'") & "', "
									End If
								End If
								If lstrFW_Nome_mae <> lstrLC_Nome_mae Then
									If lstrLC_Nome_mae <> "" Then
										lstrSql = lstrSql & "nomemae     = '" & Replace(lstrLC_Nome_mae, "'", "\'") & "', "
									End If
								End If
								If llngFW_Idestado_civil <> llngLC_Estado_civil Then
									If llngLC_Estado_civil > 0 Then
										lstrSql = lstrSql & "idestado_civil  = " & llngLC_Estado_civil & ", "
									End If
								End If
								If ldteFW_Data_nascimento <> ldteLC_Data_nascimento Then
									If ldteLC_Data_nascimento <> CDate("01/01/1900") Then
										lstrSql = lstrSql & "dt_nascto   = '" & VB6.Format(ldteLC_Data_nascimento, "yyyy-MM-dd") & "', "
									End If
								End If
							End If

							lstrLC_Id_endereco_fatwin = BuscaAtualizaCodEndereco(llngLC_Id_endereco)

							If lstrFW_Codend <> lstrLC_Id_endereco_fatwin Then
								If lstrLC_Id_endereco_fatwin <> "" Then
									lstrSql = lstrSql & "codend     = '" & lstrLC_Id_endereco_fatwin & "', "
								End If
							End If
							If lstrFW_Uf <> lstrLC_Uf Then
								If lstrLC_Uf <> "" Then
									lstrSql = lstrSql & "uf         = '" & Replace(lstrLC_Uf, "'", "\'") & "', "
								End If
							End If
							If lstrFW_Cep <> Replace(Replace(lstrLC_Cep, "-", ""), ".", "") Then
								If lstrLC_Cep <> "" Then
									lstrSql = lstrSql & "cep        = '" & lstrLC_Cep & "', "
								End If
							End If
							If lstrFW_Cidade <> lstrLC_Cidade Then
								If lstrLC_Cidade <> "" Then
									lstrSql = lstrSql & "cidade     = '" & Replace(lstrLC_Cidade, "'", "\'") & "', "
								End If
							End If
							If lstrFW_Distrito <> lstrLC_Distrito Then
								If lstrLC_Distrito <> "" Then
									lstrSql = lstrSql & "distrito   = '" & Replace(lstrLC_Distrito, "'", "\'") & "', "
								End If
							End If
							If lstrFW_Bairro <> lstrLC_Bairro Then
								If lstrLC_Bairro <> "" Then
									lstrSql = lstrSql & "bairro     = '" & Replace(lstrLC_Bairro, "'", "\'") & "', "
								End If
							End If
							If lstrFW_End <> lstrLC_Endereco Then
								If lstrLC_Endereco <> "" Then
									lstrSql = lstrSql & "end        = '" & Replace(lstrLC_Endereco, "'", "\'") & "', "
								End If
							End If
							If lstrFW_Numero <> lstrLC_Numero Then
								If lstrLC_Numero <> "" Then
									lstrSql = lstrSql & "numero     = '" & Replace(lstrLC_Numero, "'", "\'") & "', "
								End If
							End If
							If lstrFW_Complement <> lstrLC_Complemento Then
								If lstrLC_Complemento <> "" Then
									lstrSql = lstrSql & "complement = '" & Replace(lstrLC_Complemento, "'", "\'") & "', "
								End If
							End If
							If lstrFW_Quadra <> lstrLC_Quadra Then
								If lstrLC_Quadra <> "" Then
									lstrSql = lstrSql & "quadra = '" & Replace(lstrLC_Quadra, "'", "\'") & "', "
								End If
							End If
							If lstrFW_Lote <> lstrLC_Lote Then
								If lstrLC_Lote <> "" Then
									lstrSql = lstrSql & "lote = '" & Replace(lstrLC_Lote, "'", "\'") & "', "
								End If
							End If

							lstrSql = Trim(lstrSql)
							If Trim(lstrSql) <> "" Then
								lstrSql = VB.Left(lstrSql, CInt(Len(lstrSql)) - 1)
								Call InsereAuditoriaConsumidor(llngLC_Consreg_fatwin, "ORIGEM")

								objExecAtualizaBancoDados.QUERY_NAME = "UPDATE `consum` SET " & lstrSql & " WHERE `consreg` = " & llngLC_Consreg_fatwin & " "
								repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

								Me.lblRegEtapa(1).Text = VB6.Format(CDbl(IIf(IsNumeric(Me.lblRegEtapa(1).Text), Me.lblRegEtapa(1).Text, 0)) + ldblRowAfec, "#0")
								Call InsereAuditoriaConsumidor(llngLC_Consreg_fatwin, "ALTERADO")
							End If

						Next
					End If

					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1,  , " CONSUMIDORES ")
				Next
			End If

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

	End Sub

	Private Sub Etapa003()
		Dim ldblRowAfec As Double
		'
		'Link Center
		'
		Dim llngLC_Id As Integer
		Dim lstrLC_Numero As String
		Dim llngLC_Id_tipo_medidor As Integer
		Dim lstrLC_Descricao_tipo_medidor As String
		Dim llngLC_Id_transformador As Integer
		Dim lstrLC_Descricao_transformador As String
		Dim llngLC_Id_tipo_fase As Integer
		Dim lstrLC_Descricao_tipo_fase As String
		Dim llngLC_Id_tipo_grandeza_medida As Integer
		Dim lstrLC_Descricao_tipo_grandeza_medida As String
		Dim llngLC_Id_tipo_padrao_medicao As Integer
		Dim lstrLC_Descricao_tipo_padrao_medicao As String
		Dim llngLC_Id_fabricante As Integer
		Dim lstrLC_Descricao_fabricante As String
		Dim llngLC_Id_disjuntor As Integer
		Dim lstrLC_Descricao_disjuntor As String
		Dim llngLC_Id_tensao As Integer
		Dim lstrLC_Descricao_tensao As String
		Dim llngLC_Id_corrente As Integer
		Dim lstrLC_Descricao_corrente As String
		Dim lstrLC_Fase_ligacao As String
		Dim lstrLC_Modelo As String
		Dim ldteLC_Data_fabricacao As Date
		Dim llngLC_Numero_digitos As Integer
		Dim ldblLC_Const_kwh As Double
		Dim ldblLC_Const_kw As Double
		Dim ldblLC_Const_kvarh As Double
		Dim ldblLC_Const_kwh_p As Double
		Dim ldblLC_Const_kwh_fp As Double
		Dim ldblLC_Const_kw_p As Double
		Dim ldblLC_Const_kw_fp As Double
		Dim ldblLC_Const_kvarh_p As Double
		Dim ldblLC_Const_kvarh_fp As Double
		Dim llngLC_Id_situacao As Integer
		Dim lstrLC_Situacao As String
		'
		Dim lstrSql As String
		Try

			Me.lblInfo.Text = " Buscando novos medidores no L.C. aguarde..."
			Call Me.lblInfo.Refresh()
			lstrSql = ""
			lstrSql = lstrSql & "SELECT "
			lstrSql = lstrSql & " `lc_m`.`id`, "
			lstrSql = lstrSql & " `lc_m`.`numero`, "
			lstrSql = lstrSql & " `lc_m`.`id_tipo_medidor`, "
			lstrSql = lstrSql & " `lc_m`.`descricao_tipo_medidor`, "
			lstrSql = lstrSql & " `lc_m`.`id_transformador`, "
			lstrSql = lstrSql & " `lc_m`.`descricao_transformador`, "
			lstrSql = lstrSql & " `lc_m`.`id_tipo_fase`, "
			lstrSql = lstrSql & " `lc_m`.`descricao_tipo_fase`, "
			lstrSql = lstrSql & " `lc_m`.`id_tipo_grandeza_medida`, "
			lstrSql = lstrSql & " `lc_m`.`descricao_tipo_grandeza_medida`, "
			lstrSql = lstrSql & " `lc_m`.`id_tipo_padrao_medicao`, "
			lstrSql = lstrSql & " `lc_m`.`descricao_tipo_padrao_medicao`, "
			lstrSql = lstrSql & " `lc_m`.`id_fabricante`, "
			lstrSql = lstrSql & " `lc_m`.`descricao_fabricante`, "
			lstrSql = lstrSql & " `lc_m`.`id_disjuntor`, "
			lstrSql = lstrSql & " `lc_m`.`descricao_disjuntor`, "
			lstrSql = lstrSql & " `lc_m`.`id_tensao`, "
			lstrSql = lstrSql & " `lc_m`.`descricao_tensao`, "
			lstrSql = lstrSql & " `lc_m`.`id_corrente`, "
			lstrSql = lstrSql & " `lc_m`.`descricao_corrente`, "
			lstrSql = lstrSql & " `lc_m`.`fase_ligacao`, "
			lstrSql = lstrSql & " `lc_m`.`modelo`, "
			lstrSql = lstrSql & " `lc_m`.`data_fabricacao`, "
			lstrSql = lstrSql & " `lc_m`.`numero_digitos`, "
			lstrSql = lstrSql & " `lc_m`.`const_kwh`, "
			lstrSql = lstrSql & " `lc_m`.`const_kw`, "
			lstrSql = lstrSql & " `lc_m`.`const_kvarh`, "
			lstrSql = lstrSql & " `lc_m`.`const_kwh_p`, "
			lstrSql = lstrSql & " `lc_m`.`const_kwh_fp`, "
			lstrSql = lstrSql & " `lc_m`.`const_kw_p`, "
			lstrSql = lstrSql & " `lc_m`.`const_kw_fp`, "
			lstrSql = lstrSql & " `lc_m`.`const_kvarh_p`, "
			lstrSql = lstrSql & " `lc_m`.`const_kvarh_fp`, "
			lstrSql = lstrSql & " `lc_m`.`situacao` "
			lstrSql = lstrSql & "  FROM " & gstrBDMLinkCenter & ".`medidor` `lc_m` "
			lstrSql = lstrSql & " WHERE NOT EXISTS(SELECT * FROM `medidores` `fw_m` WHERE `fw_m`.`id` = `lc_m`.`id`)"
			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, " NOVOS MEDIDORES ")
				For Each row As DataRow In dt.Rows

					llngLC_Id = CInt(IIf(IsDBNull(row.Item("id")), "0", row.Item("id")))
					lstrLC_Numero = Trim(IIf(IsDBNull(row.Item("numero")), "", row.Item("numero")))
					llngLC_Id_tipo_medidor = CInt(IIf(IsDBNull(row.Item("id_tipo_medidor")), "0", row.Item("id_tipo_medidor")))
					lstrLC_Descricao_tipo_medidor = Trim(IIf(IsDBNull(row.Item("descricao_tipo_medidor")), "", row.Item("descricao_tipo_medidor")))
					'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"'
					llngLC_Id_transformador = CInt(IIf(IsDBNull(row.Item("id_transformador")), "1", row.Item("id_transformador")))
					If llngLC_Id_transformador = 0 Then llngLC_Id_transformador = 1
					'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"'
					lstrLC_Descricao_transformador = Trim(IIf(IsDBNull(row.Item("descricao_transformador")), "", row.Item("descricao_transformador")))
					'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"'
					llngLC_Id_tipo_fase = CInt(IIf(IsDBNull(row.Item("id_tipo_fase")), "0", row.Item("id_tipo_fase")))
					'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"'
					lstrLC_Descricao_tipo_fase = Trim(IIf(IsDBNull(row.Item("descricao_tipo_fase")), "", row.Item("descricao_tipo_fase")))
					'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"'
					llngLC_Id_tipo_grandeza_medida = CInt(IIf(IsDBNull(row.Item("id_tipo_grandeza_medida")), "0", row.Item("id_tipo_grandeza_medida")))
					'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"'
					lstrLC_Descricao_tipo_grandeza_medida = Trim(IIf(IsDBNull(row.Item("descricao_tipo_grandeza_medida")), "", row.Item("descricao_tipo_grandeza_medida")))
					'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"'
					llngLC_Id_tipo_padrao_medicao = CInt(IIf(IsDBNull(row.Item("id_tipo_padrao_medicao")), "0", row.Item("id_tipo_padrao_medicao")))
					'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"'
					lstrLC_Descricao_tipo_padrao_medicao = Trim(IIf(IsDBNull(row.Item("descricao_tipo_padrao_medicao")), "", row.Item("descricao_tipo_padrao_medicao")))
					'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"'
					llngLC_Id_fabricante = CInt(IIf(IsDBNull(row.Item("id_fabricante")), "0", row.Item("id_fabricante")))
					'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"'
					lstrLC_Descricao_fabricante = Trim(IIf(IsDBNull(row.Item("descricao_fabricante")), "", row.Item("descricao_fabricante")))
					'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"'
					llngLC_Id_disjuntor = CInt(IIf(IsDBNull(row.Item("id_disjuntor")), "0", row.Item("id_disjuntor")))
					'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"'
					lstrLC_Descricao_disjuntor = Trim(IIf(IsDBNull(row.Item("descricao_disjuntor")), "", row.Item("descricao_disjuntor")))
					'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"'
					llngLC_Id_tensao = CInt(IIf(IsDBNull(row.Item("id_tensao")), "0", row.Item("id_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"'
					lstrLC_Descricao_tensao = Trim(IIf(IsDBNull(row.Item("descricao_tensao")), "", row.Item("descricao_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"'
					llngLC_Id_corrente = CInt(IIf(IsDBNull(row.Item("id_corrente")), "0", row.Item("id_corrente")))
					'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"'
					lstrLC_Descricao_corrente = Trim(IIf(IsDBNull(row.Item("descricao_corrente")), "", row.Item("descricao_corrente")))
					'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"'
					lstrLC_Fase_ligacao = IIf(IsDBNull(row.Item("fase_ligacao")), "", row.Item("fase_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"'
					lstrLC_Modelo = Trim(IIf(IsDBNull(row.Item("modelo")), "", row.Item("modelo")))
					'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"'
					ldteLC_Data_fabricacao = CDate(IIf(IsDBNull(row.Item("data_fabricacao")), "1900-01-01", row.Item("data_fabricacao")))
					'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"'
					llngLC_Numero_digitos = CInt(IIf(IsDBNull(row.Item("numero_digitos")), "0", row.Item("numero_digitos")))
					'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"'
					ldblLC_Const_kwh = CDbl(IIf(IsDBNull(row.Item("const_kwh")), "0", row.Item("const_kwh")))
					'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"'
					ldblLC_Const_kw = CDbl(IIf(IsDBNull(row.Item("const_kw")), "0", row.Item("const_kw")))
					'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"'
					ldblLC_Const_kvarh = CDbl(IIf(IsDBNull(row.Item("const_kvarh")), "0", row.Item("const_kvarh")))
					'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"'
					ldblLC_Const_kwh_p = CDbl(IIf(IsDBNull(row.Item("const_kwh_p")), "0", row.Item("const_kwh_p")))
					'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"'
					ldblLC_Const_kwh_fp = CDbl(IIf(IsDBNull(row.Item("const_kwh_fp")), "0", row.Item("const_kwh_fp")))
					'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"'
					ldblLC_Const_kw_p = CDbl(IIf(IsDBNull(row.Item("const_kw_p")), "0", row.Item("const_kw_p")))
					'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"'
					ldblLC_Const_kw_fp = CDbl(IIf(IsDBNull(row.Item("const_kw_fp")), "0", row.Item("const_kw_fp")))
					'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"'
					ldblLC_Const_kvarh_p = CDbl(IIf(IsDBNull(row.Item("const_kvarh_p")), "0", row.Item("const_kvarh_p")))
					'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"'
					ldblLC_Const_kvarh_fp = CDbl(IIf(IsDBNull(row.Item("const_kvarh_fp")), "0", row.Item("const_kvarh_fp")))
					'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"'
					lstrLC_Situacao = Trim(IIf(IsDBNull(row.Item("situacao")), "", row.Item("situacao")))
					If lstrLC_Situacao = "I" Then
						llngLC_Id_situacao = 1
					ElseIf lstrLC_Situacao = "S" Then
						llngLC_Id_situacao = 6
					ElseIf lstrLC_Situacao = "R" Then
						llngLC_Id_situacao = 8
					ElseIf lstrLC_Situacao = "V" Or lstrLC_Situacao = "A" Then
						llngLC_Id_situacao = 7
					End If
					If llngLC_Id_tipo_medidor > 0 Then
						Call AtualizaTipoMedidor(llngLC_Id_tipo_medidor, lstrLC_Descricao_tipo_medidor)
					End If
					If llngLC_Id_transformador > 0 Then
						Call AtualizaTransformador(llngLC_Id_transformador, lstrLC_Descricao_transformador)
					End If
					If llngLC_Id_tipo_fase > 0 Then
						Call AtualizaTipoFases(llngLC_Id_tipo_fase, lstrLC_Descricao_tipo_fase)
					End If
					If llngLC_Id_tipo_grandeza_medida > 0 Then
						'Call AtualizaTipoGrandezaMedida(llngLC_Id_tipo_grandeza_medida, lstrLC_Descricao_tipo_grandeza_medida)
					End If
					If llngLC_Id_tipo_padrao_medicao > 0 Then
						Call AtualizaTipoPadraoMedicao(llngLC_Id_tipo_padrao_medicao, lstrLC_Descricao_tipo_padrao_medicao)
					End If
					If llngLC_Id_fabricante > 0 Then
						Call AtualizaFabricantes(llngLC_Id_fabricante, lstrLC_Descricao_fabricante)
					End If
					If llngLC_Id_disjuntor > 0 Then
						Call AtualizaDisjuntor(llngLC_Id_disjuntor, lstrLC_Descricao_disjuntor)
					End If
					If llngLC_Id_tensao > 0 Then
						Call AtualizaBobinaTensao(llngLC_Id_tensao, lstrLC_Descricao_tensao)
					End If
					If llngLC_Id_corrente > 0 Then
						Call AtualizaBobinaCorrente(llngLC_Id_corrente, lstrLC_Descricao_corrente)
					End If
					lstrSql = ""
					lstrSql = lstrSql & "id                     =  " & llngLC_Id & ","
					lstrSql = lstrSql & "numero                 = '" & lstrLC_Numero & "',"
					lstrSql = lstrSql & "idtipo_medidor         =  " & llngLC_Id_tipo_medidor & ", "
					lstrSql = lstrSql & "idtransformador        =  " & llngLC_Id_transformador & ", "
					lstrSql = lstrSql & "idtipo_fases           =  " & llngLC_Id_tipo_fase & ", "
					lstrSql = lstrSql & "idtipo_grandeza_medida =  " & llngLC_Id_tipo_grandeza_medida & ", "
					lstrSql = lstrSql & "idtipo_padrao_medicao  =  " & llngLC_Id_tipo_padrao_medicao & ", "
					lstrSql = lstrSql & "idfabricantes          =  " & llngLC_Id_fabricante & ", "
					lstrSql = lstrSql & "iddisjuntor            =  " & llngLC_Id_disjuntor & ", "
					lstrSql = lstrSql & "idbobina_tensao        =  " & llngLC_Id_tensao & ", "
					lstrSql = lstrSql & "idbobina_corrente      =  " & llngLC_Id_corrente & ", "
					lstrSql = lstrSql & "idsituacao             =  " & llngLC_Id_situacao & ", "
					lstrSql = lstrSql & "fase_ligacao           = '" & lstrLC_Fase_ligacao & "', "
					lstrSql = lstrSql & "modelo                 = '" & Replace(lstrLC_Modelo, "'", "\'") & "', "
					If ldteLC_Data_fabricacao <> CDate("01/01/1900") Then
						lstrSql = lstrSql & "fabricacao             = '" & VB6.Format(ldteLC_Data_fabricacao, "yyyy-MM-dd") & "', "
					End If
					lstrSql = lstrSql & "numero_digitos         =  " & llngLC_Numero_digitos & ", "
					If ldblLC_Const_kwh > 0 Then
						lstrSql = lstrSql & "const_kwh              =  " & Replace(CStr(ldblLC_Const_kwh), ",", ".") & ", "
					End If
					If ldblLC_Const_kw > 0 Then
						lstrSql = lstrSql & "const_kw               =  " & Replace(CStr(ldblLC_Const_kw), ",", ".") & ", "
					End If
					If ldblLC_Const_kvarh > 0 Then
						lstrSql = lstrSql & "const_kvarh            =  " & Replace(CStr(ldblLC_Const_kvarh), ",", ".") & ", "
					End If
					If ldblLC_Const_kwh_p > 0 Then
						lstrSql = lstrSql & "const_kwh_p            =  " & Replace(CStr(ldblLC_Const_kwh_p), ",", ".") & ", "
					End If
					If ldblLC_Const_kwh_fp > 0 Then
						lstrSql = lstrSql & "const_kwh_fp           =  " & Replace(CStr(ldblLC_Const_kwh_fp), ",", ".") & ", "
					End If
					If ldblLC_Const_kw_p > 0 Then
						lstrSql = lstrSql & "const_kw_p             =  " & Replace(CStr(ldblLC_Const_kw_p), ",", ".") & ", "
					End If
					If ldblLC_Const_kw_fp > 0 Then
						lstrSql = lstrSql & "const_kw_fp            =  " & Replace(CStr(ldblLC_Const_kw_fp), ",", ".") & ", "
					End If
					If ldblLC_Const_kvarh_p > 0 Then
						lstrSql = lstrSql & "const_kvarh_p          =  " & Replace(CStr(ldblLC_Const_kvarh_p), ",", ".") & ", "
					End If
					If ldblLC_Const_kvarh_fp > 0 Then
						lstrSql = lstrSql & "const_kvarh_fp         =  " & Replace(CStr(ldblLC_Const_kvarh_fp), ",", ".") & ", "
					End If
					lstrSql = lstrSql & "situacao               = 'Ativo', "
					lstrSql = lstrSql & "idusuarios             =  " & ilngIdUsuarioLC & " "

					objExecAtualizaBancoDados.QUERY_NAME = "INSERT INTO `medidores` SET " & lstrSql
					repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

					Me.lblRegEtapa(2).Text = VB6.Format(CDbl(IIf(IsNumeric(Me.lblRegEtapa(2).Text), Me.lblRegEtapa(2).Text, 0)) + ldblRowAfec, "#0")
					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1,  , " NOVOS MEDIDORES ")
				Next
			End If
		Catch ex As Exception
			Throw New Exception("Ocorreu o Erro: " & Err.Description & " - Rotina: Etapa003" & "", ex)
			'MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: Etapa003" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try

	End Sub

	Private Sub Etapa004(Optional ByVal adblId_Medidor As Double = 0)
		Dim ldblRowAfec As Double
		'
		'Fatwin
		'
		Dim llngFW_Id As Integer
		Dim lstrFW_Numero As String
		Dim llngFW_Idtipo_medidor As Integer
		Dim llngFW_Idtransformador As Integer
		Dim llngFW_Idtipo_fases As Integer
		Dim llngFW_Idtipo_grandeza_medida As Integer
		Dim llngFW_Idtipo_padrao_medicao As Integer
		Dim llngFW_Idfabricantes As Integer
		Dim llngFW_Iddisjuntor As Integer
		Dim llngFW_Idbobina_tensao As Integer
		Dim llngFW_Idbobina_corrente As Integer
		Dim llngFW_Idsituacao As Integer
		Dim lstrFW_Fase_ligacao As String
		Dim lstrFW_Modelo As String
		Dim ldteFW_Fabricacao As Date
		Dim llngFW_Numero_digitos As Integer
		Dim ldblFW_Const_kwh As Double
		Dim ldblFW_Const_kw As Double
		Dim ldblFW_Const_kvarh As Double
		Dim ldblFW_Const_kwh_p As Double
		Dim ldblFW_Const_kwh_fp As Double
		Dim ldblFW_Const_kw_p As Double
		Dim ldblFW_Const_kw_fp As Double
		Dim ldblFW_Const_kvarh_p As Double
		Dim ldblFW_Const_kvarh_fp As Double
		Dim lstrFW_Situacao As String
		Dim llngFW_Idusuarios As Integer
		'
		' Link Center
		'
		Dim llngLC_Id As Integer
		Dim lstrLC_Numero As String
		Dim llngLC_Id_tipo_medidor As Integer
		Dim lstrLC_Descricao_tipo_medidor As String
		Dim llngLC_Id_transformador As Integer
		Dim lstrLC_Descricao_transformador As String
		Dim llngLC_Id_tipo_fase As Integer
		Dim lstrLC_Descricao_tipo_fase As String
		Dim llngLC_Id_tipo_grandeza_medida As Integer
		Dim lstrLC_Descricao_tipo_grandeza_medida As String
		Dim llngLC_Id_tipo_padrao_medicao As Integer
		Dim lstrLC_Descricao_tipo_padrao_medicao As String
		Dim llngLC_Id_fabricante As Integer
		Dim lstrLC_Descricao_fabricante As String
		Dim llngLC_Id_disjuntor As Integer
		Dim lstrLC_Descricao_disjuntor As String
		Dim llngLC_Id_tensao As Integer
		Dim lstrLC_Descricao_tensao As String
		Dim llngLC_Id_corrente As Integer
		Dim lstrLC_Descricao_corrente As String
		Dim lstrLC_Fase_ligacao As String
		Dim lstrLC_Modelo As String
		Dim ldteLC_Data_fabricacao As Date
		Dim llngLC_Numero_digitos As Integer
		Dim ldblLC_Const_kwh As Double
		Dim ldblLC_Const_kw As Double
		Dim ldblLC_Const_kvarh As Double
		Dim ldblLC_Const_kwh_p As Double
		Dim ldblLC_Const_kwh_fp As Double
		Dim ldblLC_Const_kw_p As Double
		Dim ldblLC_Const_kw_fp As Double
		Dim ldblLC_Const_kvarh_p As Double
		Dim ldblLC_Const_kvarh_fp As Double
		Dim llngLC_Id_situacao As Integer
		Dim lstrLC_Situacao As String
		'
		Dim lstrSql As String

		Try
			Me.lblInfo.Text = " Buscando todos medidores no L.C. aguarde..."
			Call Me.lblInfo.Refresh()
			lstrSql = ""
			lstrSql = lstrSql & "SELECT "
			lstrSql = lstrSql & " `lc_m`.`id`, "
			lstrSql = lstrSql & " `lc_m`.`numero`, "
			lstrSql = lstrSql & " `lc_m`.`id_tipo_medidor`, "
			lstrSql = lstrSql & " `lc_m`.`descricao_tipo_medidor`, "
			lstrSql = lstrSql & " `lc_m`.`id_transformador`, "
			lstrSql = lstrSql & " `lc_m`.`descricao_transformador`, "
			lstrSql = lstrSql & " `lc_m`.`id_tipo_fase`, "
			lstrSql = lstrSql & " `lc_m`.`descricao_tipo_fase`, "
			lstrSql = lstrSql & " `lc_m`.`id_tipo_grandeza_medida`, "
			lstrSql = lstrSql & " `lc_m`.`descricao_tipo_grandeza_medida`, "
			lstrSql = lstrSql & " `lc_m`.`id_tipo_padrao_medicao`, "
			lstrSql = lstrSql & " `lc_m`.`descricao_tipo_padrao_medicao`, "
			lstrSql = lstrSql & " `lc_m`.`id_fabricante`, "
			lstrSql = lstrSql & " `lc_m`.`descricao_fabricante`, "
			lstrSql = lstrSql & " `lc_m`.`id_disjuntor`, "
			lstrSql = lstrSql & " `lc_m`.`descricao_disjuntor`, "
			lstrSql = lstrSql & " `lc_m`.`id_tensao`, "
			lstrSql = lstrSql & " `lc_m`.`descricao_tensao`, "
			lstrSql = lstrSql & " `lc_m`.`id_corrente`, "
			lstrSql = lstrSql & " `lc_m`.`descricao_corrente`, "
			lstrSql = lstrSql & " `lc_m`.`fase_ligacao`, "
			lstrSql = lstrSql & " `lc_m`.`modelo`, "
			lstrSql = lstrSql & " `lc_m`.`data_fabricacao`, "
			lstrSql = lstrSql & " `lc_m`.`numero_digitos`, "
			lstrSql = lstrSql & " `lc_m`.`const_kwh`, "
			lstrSql = lstrSql & " `lc_m`.`const_kw`, "
			lstrSql = lstrSql & " `lc_m`.`const_kvarh`, "
			lstrSql = lstrSql & " `lc_m`.`const_kwh_p`, "
			lstrSql = lstrSql & " `lc_m`.`const_kwh_fp`, "
			lstrSql = lstrSql & " `lc_m`.`const_kw_p`, "
			lstrSql = lstrSql & " `lc_m`.`const_kw_fp`, "
			lstrSql = lstrSql & " `lc_m`.`const_kvarh_p`, "
			lstrSql = lstrSql & " `lc_m`.`const_kvarh_fp`, "
			lstrSql = lstrSql & " `lc_m`.`situacao` "
			lstrSql = lstrSql & "  FROM " & gstrBDMLinkCenter & ".`medidor` `lc_m` " 'MEDICAO_CERES

			If Me.chkMedidores.CheckState = System.Windows.Forms.CheckState.Unchecked Then
				If adblId_Medidor = 0 Then
					lstrSql = lstrSql & " WHERE EXISTS(SELECT * "
					lstrSql = lstrSql & "                FROM " & gstrBDCLinkCenter & ".`os` `lc_o` "
					lstrSql = lstrSql & "               WHERE `lc_o`.`id_medidor` = `lc_m`.`id` "

					If Trim(Me.txtSequenciasOs.Text) <> "" Then
						lstrSql = lstrSql & "                 AND `lc_o`.`sequencia_os` in(" & Me.txtSequenciasOs.Text & ") "
					Else
						lstrSql = lstrSql & "                 AND (DATE(`lc_o`.`data_conclusao`) BETWEEN '" & VB6.Format(idteIni, "yyyy-MM-dd") & "' AND '" & VB6.Format(idteFim, "yyyy-MM-dd") & "')"
						lstrSql = lstrSql & "				  AND `lc_o`.`id_class_comercial` = '30.10'"
					End If
					lstrSql = lstrSql & "              ) "
				Else
					lstrSql = lstrSql & " WHERE `lc_m`.`id` = " & adblId_Medidor
				End If
			End If

			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, " MEDIDORES ")
				For Each row As DataRow In dt.Rows

					llngLC_Id = CInt(IIf(IsDBNull(row.Item("id")), "0", row.Item("id")))

					lstrLC_Numero = Trim(IIf(IsDBNull(row.Item("numero")), "", row.Item("numero")))

					llngLC_Id_tipo_medidor = CInt(IIf(IsDBNull(row.Item("id_tipo_medidor")), "0", row.Item("id_tipo_medidor")))

					lstrLC_Descricao_tipo_medidor = Trim(IIf(IsDBNull(row.Item("descricao_tipo_medidor")), "", row.Item("descricao_tipo_medidor")))

					llngLC_Id_transformador = CInt(IIf(IsDBNull(row.Item("id_transformador")), "1", row.Item("id_transformador")))
					If llngLC_Id_transformador = 0 Then llngLC_Id_transformador = 1

					lstrLC_Descricao_transformador = Trim(IIf(IsDBNull(row.Item("descricao_transformador")), "", row.Item("descricao_transformador")))

					llngLC_Id_tipo_fase = CInt(IIf(IsDBNull(row.Item("id_tipo_fase")), "0", row.Item("id_tipo_fase")))

					lstrLC_Descricao_tipo_fase = Trim(IIf(IsDBNull(row.Item("descricao_tipo_fase")), "", row.Item("descricao_tipo_fase")))

					llngLC_Id_tipo_grandeza_medida = CInt(IIf(IsDBNull(row.Item("id_tipo_grandeza_medida")), "0", row.Item("id_tipo_grandeza_medida")))

					lstrLC_Descricao_tipo_grandeza_medida = Trim(IIf(IsDBNull(row.Item("descricao_tipo_grandeza_medida")), "", row.Item("descricao_tipo_grandeza_medida")))

					llngLC_Id_tipo_padrao_medicao = CInt(IIf(IsDBNull(row.Item("id_tipo_padrao_medicao")), "0", row.Item("id_tipo_padrao_medicao")))

					lstrLC_Descricao_tipo_padrao_medicao = Trim(IIf(IsDBNull(row.Item("descricao_tipo_padrao_medicao")), "", row.Item("descricao_tipo_padrao_medicao")))

					llngLC_Id_fabricante = CInt(IIf(IsDBNull(row.Item("id_fabricante")), "0", row.Item("id_fabricante")))

					lstrLC_Descricao_fabricante = Trim(IIf(IsDBNull(row.Item("descricao_fabricante")), "", row.Item("descricao_fabricante")))

					llngLC_Id_disjuntor = CInt(IIf(IsDBNull(row.Item("id_disjuntor")), "0", row.Item("id_disjuntor")))

					lstrLC_Descricao_disjuntor = Trim(IIf(IsDBNull(row.Item("descricao_disjuntor")), "", row.Item("descricao_disjuntor")))

					llngLC_Id_tensao = CInt(IIf(IsDBNull(row.Item("id_tensao")), "0", row.Item("id_tensao")))

					lstrLC_Descricao_tensao = Trim(IIf(IsDBNull(row.Item("descricao_tensao")), "", row.Item("descricao_tensao")))

					llngLC_Id_corrente = CInt(IIf(IsDBNull(row.Item("id_corrente")), "0", row.Item("id_corrente")))

					lstrLC_Descricao_corrente = Trim(IIf(IsDBNull(row.Item("descricao_corrente")), "", row.Item("descricao_corrente")))

					lstrLC_Fase_ligacao = IIf(IsDBNull(row.Item("fase_ligacao")), "", row.Item("fase_ligacao"))

					lstrLC_Modelo = Trim(IIf(IsDBNull(row.Item("modelo")), "", row.Item("modelo")))

					ldteLC_Data_fabricacao = CDate(IIf(IsDBNull(row.Item("data_fabricacao")), "1900-01-01", row.Item("data_fabricacao")))

					llngLC_Numero_digitos = CInt(IIf(IsDBNull(row.Item("numero_digitos")), "0", row.Item("numero_digitos")))

					ldblLC_Const_kwh = CDbl(IIf(IsDBNull(row.Item("const_kwh")), "0", row.Item("const_kwh")))

					ldblLC_Const_kw = CDbl(IIf(IsDBNull(row.Item("const_kw")), "0", row.Item("const_kw")))

					ldblLC_Const_kvarh = CDbl(IIf(IsDBNull(row.Item("const_kvarh")), "0", row.Item("const_kvarh")))

					ldblLC_Const_kwh_p = CDbl(IIf(IsDBNull(row.Item("const_kwh_p")), "0", row.Item("const_kwh_p")))

					ldblLC_Const_kwh_fp = CDbl(IIf(IsDBNull(row.Item("const_kwh_fp")), "0", row.Item("const_kwh_fp")))
					ldblLC_Const_kw_p = CDbl(IIf(IsDBNull(row.Item("const_kw_p")), "0", row.Item("const_kw_p")))
					ldblLC_Const_kw_fp = CDbl(IIf(IsDBNull(row.Item("const_kw_fp")), "0", row.Item("const_kw_fp")))
					ldblLC_Const_kvarh_p = CDbl(IIf(IsDBNull(row.Item("const_kvarh_p")), "0", row.Item("const_kvarh_p")))
					ldblLC_Const_kvarh_fp = CDbl(IIf(IsDBNull(row.Item("const_kvarh_fp")), "0", row.Item("const_kvarh_fp")))
					lstrLC_Situacao = Trim(IIf(IsDBNull(row.Item("situacao")), "", row.Item("situacao")))
					If lstrLC_Situacao = "I" Then
						llngLC_Id_situacao = 1
					ElseIf lstrLC_Situacao = "S" Then
						llngLC_Id_situacao = 6
					ElseIf lstrLC_Situacao = "R" Then
						llngLC_Id_situacao = 8
					ElseIf lstrLC_Situacao = "V" Then
						llngLC_Id_situacao = 7
					End If
					'
					'Verifica se existe e se teve alguma modificação
					'
					lstrSql = ""
					lstrSql = lstrSql & "SELECT * "
					lstrSql = lstrSql & "  FROM `medidores` "
					lstrSql = lstrSql & " WHERE `id` = " & llngLC_Id
					objAtualizaBancoDados.QUERY_NAME = lstrSql
					dt1 = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

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

							llngFW_Id = CInt(IIf(IsDBNull(row1.Item("id")), "0", row1.Item("id")))
							lstrFW_Numero = Trim(IIf(IsDBNull(row1.Item("numero")), "", row1.Item("numero")))
							llngFW_Idtipo_medidor = CInt(IIf(IsDBNull(row1.Item("idtipo_medidor")), "0", row1.Item("idtipo_medidor")))
							llngFW_Idtransformador = CInt(IIf(IsDBNull(row1.Item("idtransformador")), "0", row1.Item("idtransformador")))
							llngFW_Idtipo_fases = CInt(IIf(IsDBNull(row1.Item("idtipo_fases")), "0", row1.Item("idtipo_fases")))
							llngFW_Idtipo_grandeza_medida = CInt(IIf(IsDBNull(row1.Item("idtipo_grandeza_medida")), "0", row1.Item("idtipo_grandeza_medida")))
							llngFW_Idtipo_padrao_medicao = CInt(IIf(IsDBNull(row1.Item("idtipo_padrao_medicao")), "0", row1.Item("idtipo_padrao_medicao")))
							llngFW_Idfabricantes = CInt(IIf(IsDBNull(row1.Item("idfabricantes")), "0", row1.Item("idfabricantes")))
							llngFW_Iddisjuntor = CInt(IIf(IsDBNull(row1.Item("iddisjuntor")), "0", row1.Item("iddisjuntor")))
							llngFW_Idbobina_tensao = CInt(IIf(IsDBNull(row1.Item("idbobina_tensao")), "0", row1.Item("idbobina_tensao")))
							llngFW_Idbobina_corrente = CInt(IIf(IsDBNull(row1.Item("idbobina_corrente")), "0", row1.Item("idbobina_corrente")))
							llngFW_Idsituacao = CInt(IIf(IsDBNull(row1.Item("idsituacao")), "0", row1.Item("idsituacao")))
							lstrFW_Fase_ligacao = Trim(IIf(IsDBNull(row1.Item("fase_ligacao")), "", row1.Item("fase_ligacao")))
							lstrFW_Modelo = Trim(IIf(IsDBNull(row1.Item("modelo")), "", row1.Item("modelo")))
							ldteFW_Fabricacao = IIf(IsDBNull(row1.Item("fabricacao")), "1900-01-01", row1.Item("fabricacao"))
							llngFW_Numero_digitos = CInt(IIf(IsDBNull(row1.Item("numero_digitos")), "0", row1.Item("numero_digitos")))
							ldblFW_Const_kwh = CDbl(IIf(IsDBNull(row1.Item("const_kwh")), "0", row1.Item("const_kwh")))
							ldblFW_Const_kw = CDbl(IIf(IsDBNull(row1.Item("const_kw")), "0", row1.Item("const_kw")))
							ldblFW_Const_kvarh = CDbl(IIf(IsDBNull(row1.Item("const_kvarh")), "0", row1.Item("const_kvarh")))
							ldblFW_Const_kwh_p = CDbl(IIf(IsDBNull(row1.Item("const_kwh_p")), "0", row1.Item("const_kwh_p")))
							ldblFW_Const_kwh_fp = CDbl(IIf(IsDBNull(row1.Item("const_kwh_fp")), "0", row1.Item("const_kwh_fp")))
							ldblFW_Const_kw_p = CDbl(IIf(IsDBNull(row1.Item("const_kw_p")), "0", row1.Item("const_kw_p")))
							ldblFW_Const_kw_fp = CDbl(IIf(IsDBNull(row1.Item("const_kw_fp")), "0", row1.Item("const_kw_fp")))
							ldblFW_Const_kvarh_p = CDbl(IIf(IsDBNull(row1.Item("const_kvarh_p")), "0", row1.Item("const_kvarh_p")))
							ldblFW_Const_kvarh_fp = CDbl(IIf(IsDBNull(row1.Item("const_kvarh_fp")), "0", row1.Item("const_kvarh_fp")))
							lstrFW_Situacao = Trim(IIf(IsDBNull(row1.Item("situacao")), "Ativo", row1.Item("situacao")))
							llngFW_Idusuarios = CInt(IIf(IsDBNull(row1.Item("idusuarios")), IIf(vUsuario.ID = 0, 1, vUsuario.ID), row1.Item("idusuarios")))
							lstrSql = ""
							If lstrFW_Numero <> lstrLC_Numero Then
								If lstrLC_Numero <> "" Then
									lstrSql = lstrSql & "numero                 = '" & lstrLC_Numero & "',"
								End If
							End If
							If llngFW_Idtipo_medidor <> llngLC_Id_tipo_medidor Then
								If llngLC_Id_tipo_medidor > 0 Then
									lstrSql = lstrSql & "idtipo_medidor         =  " & llngLC_Id_tipo_medidor & ", "
								End If
							End If
							If llngFW_Idtransformador <> llngLC_Id_transformador Then
								If llngLC_Id_transformador > 0 Then
									lstrSql = lstrSql & "idtransformador        =  " & llngLC_Id_transformador & ", "
								End If
							End If
							If llngFW_Idtipo_fases <> llngLC_Id_tipo_fase Then
								If llngLC_Id_tipo_fase > 0 Then
									lstrSql = lstrSql & "idtipo_fases           =  " & llngLC_Id_tipo_fase & ", "
								End If
							End If
							If llngFW_Idtipo_grandeza_medida <> llngLC_Id_tipo_grandeza_medida Then
								If llngLC_Id_tipo_grandeza_medida > 0 Then
									lstrSql = lstrSql & "idtipo_grandeza_medida =  " & llngLC_Id_tipo_grandeza_medida & ", "
								End If
							End If
							If llngFW_Idtipo_padrao_medicao <> llngLC_Id_tipo_padrao_medicao Then
								If llngLC_Id_tipo_padrao_medicao > 0 Then
									lstrSql = lstrSql & "idtipo_padrao_medicao  =  " & llngLC_Id_tipo_padrao_medicao & ", "
								End If
							End If
							If llngFW_Idfabricantes <> llngLC_Id_fabricante Then
								If llngLC_Id_fabricante > 0 Then
									lstrSql = lstrSql & "idfabricantes          =  " & llngLC_Id_fabricante & ", "
								End If
							End If
							If llngFW_Iddisjuntor <> llngLC_Id_disjuntor Then
								If llngLC_Id_disjuntor > 0 Then
									lstrSql = lstrSql & "iddisjuntor            =  " & llngLC_Id_disjuntor & ", "
								End If
							End If
							If llngFW_Idbobina_tensao <> llngLC_Id_tensao Then
								If llngLC_Id_tensao > 0 Then
									lstrSql = lstrSql & "idbobina_tensao        =  " & llngLC_Id_tensao & ", "
								End If
							End If
							If llngFW_Idbobina_corrente <> llngLC_Id_corrente Then
								If llngLC_Id_corrente > 0 Then
									lstrSql = lstrSql & "idbobina_corrente      =  " & llngLC_Id_corrente & ", "
								End If
							End If
							If llngFW_Idsituacao <> llngLC_Id_situacao Then
								If llngLC_Id_situacao > 0 Then
									lstrSql = lstrSql & "idsituacao             =  " & llngLC_Id_situacao & ", "
								End If
							End If
							If lstrFW_Fase_ligacao <> lstrLC_Fase_ligacao Then
								If lstrLC_Fase_ligacao <> "" Then
									lstrSql = lstrSql & "fase_ligacao           = '" & lstrLC_Fase_ligacao & "', "
								End If
							End If
							If lstrFW_Modelo <> lstrLC_Modelo Then
								If lstrLC_Modelo <> "" Then
									lstrSql = lstrSql & "modelo                 = '" & Replace(lstrLC_Modelo, "'", "\'") & "', "
								End If
							End If
							If ldteFW_Fabricacao <> ldteLC_Data_fabricacao Then
								If ldteLC_Data_fabricacao <> CDate("01/01/1900") Then
									lstrSql = lstrSql & "fabricacao             = '" & VB6.Format(ldteLC_Data_fabricacao, "yyyy-MM-dd") & "', "
								End If
							End If
							If llngFW_Numero_digitos <> llngLC_Numero_digitos Then
								If llngLC_Numero_digitos > 0 Then
									lstrSql = lstrSql & "numero_digitos         =  " & llngLC_Numero_digitos & ", "
								End If
							End If
							If ldblFW_Const_kwh <> ldblLC_Const_kwh Then
								If ldblLC_Const_kwh > 0 Then
									lstrSql = lstrSql & "const_kwh              =  " & Replace(CStr(ldblLC_Const_kwh), ",", ".") & ", "
								End If
							End If
							If ldblFW_Const_kw <> ldblLC_Const_kw Then
								If ldblLC_Const_kw > 0 Then
									lstrSql = lstrSql & "const_kw               =  " & Replace(CStr(ldblLC_Const_kw), ",", ".") & ", "
								End If
							End If
							If ldblFW_Const_kvarh <> ldblLC_Const_kvarh Then
								If ldblLC_Const_kvarh > 0 Then
									lstrSql = lstrSql & "const_kvarh            =  " & Replace(CStr(ldblLC_Const_kvarh), ",", ".") & ", "
								End If
							End If
							If ldblFW_Const_kwh_p <> ldblLC_Const_kwh_p Then
								If ldblLC_Const_kwh_p > 0 Then
									lstrSql = lstrSql & "const_kwh_p            =  " & Replace(CStr(ldblLC_Const_kwh_p), ",", ".") & ", "
								End If
							End If
							If ldblFW_Const_kwh_fp <> ldblLC_Const_kwh_fp Then
								If ldblLC_Const_kwh_fp > 0 Then
									lstrSql = lstrSql & "const_kwh_fp           =  " & Replace(CStr(ldblLC_Const_kwh_fp), ",", ".") & ", "
								End If
							End If
							If ldblFW_Const_kw_p <> ldblLC_Const_kw_p Then
								If ldblLC_Const_kw_p > 0 Then
									lstrSql = lstrSql & "const_kw_p             =  " & Replace(CStr(ldblLC_Const_kw_p), ",", ".") & ", "
								End If
							End If
							If ldblFW_Const_kw_fp <> ldblLC_Const_kw_fp Then
								If ldblLC_Const_kw_fp > 0 Then
									lstrSql = lstrSql & "const_kw_fp            =  " & Replace(CStr(ldblLC_Const_kw_fp), ",", ".") & ", "
								End If
							End If
							If ldblFW_Const_kvarh_p <> ldblLC_Const_kvarh_p Then
								If ldblLC_Const_kvarh_p > 0 Then
									lstrSql = lstrSql & "const_kvarh_p          =  " & Replace(CStr(ldblLC_Const_kvarh_p), ",", ".") & ", "
								End If
							End If
							If ldblFW_Const_kvarh_fp <> ldblLC_Const_kvarh_fp Then
								If ldblLC_Const_kvarh_fp > 0 Then
									lstrSql = lstrSql & "const_kvarh_fp         =  " & Replace(CStr(ldblLC_Const_kvarh_fp), ",", ".") & ", "
								End If
							End If
							lstrSql = Trim(lstrSql)
							If Trim(lstrSql) <> "" Then
								If llngLC_Id_tipo_medidor > 0 Then
									Call AtualizaTipoMedidor(llngLC_Id_tipo_medidor, lstrLC_Descricao_tipo_medidor)
								End If
								If llngLC_Id_transformador > 0 Then
									'Call AtualizaTransformador(llngLC_Id_transformador, lstrLC_Descricao_transformador)
								End If
								If llngLC_Id_tipo_fase > 0 Then
									Call AtualizaTipoFases(llngLC_Id_tipo_fase, lstrLC_Descricao_tipo_fase)
								End If
								If llngLC_Id_tipo_grandeza_medida > 0 Then
									'Call AtualizaTipoGrandezaMedida(llngLC_Id_tipo_grandeza_medida, lstrLC_Descricao_tipo_grandeza_medida)
								End If
								If llngLC_Id_tipo_padrao_medicao > 0 Then
									Call AtualizaTipoPadraoMedicao(llngLC_Id_tipo_padrao_medicao, lstrLC_Descricao_tipo_padrao_medicao)
								End If
								If llngLC_Id_fabricante > 0 Then
									Call AtualizaFabricantes(llngLC_Id_fabricante, lstrLC_Descricao_fabricante)
								End If
								If llngLC_Id_disjuntor > 0 Then
									Call AtualizaDisjuntor(llngLC_Id_disjuntor, lstrLC_Descricao_disjuntor)
								End If
								If llngLC_Id_tensao > 0 Then
									Call AtualizaBobinaTensao(llngLC_Id_tensao, lstrLC_Descricao_tensao)
								End If
								If llngLC_Id_corrente > 0 Then
									Call AtualizaBobinaCorrente(llngLC_Id_corrente, lstrLC_Descricao_corrente)
								End If

								lstrSql = lstrSql & "situacao               = 'Ativo', "
								lstrSql = lstrSql & "idusuarios             =  " & ilngIdUsuarioLC & " "
								objExecAtualizaBancoDados.QUERY_NAME = "UPDATE `medidores` SET " & lstrSql & " WHERE `id` =  " & llngLC_Id
								repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

								Me.lblRegEtapa(3).Text = VB6.Format(CDbl(IIf(IsNumeric(Me.lblRegEtapa(3).Text), Me.lblRegEtapa(3).Text, 0)) + ldblRowAfec, "#0")
							End If
						Next
					End If

					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1,  , " MEDIDORES ")
				Next
			End If
		Catch ex As Exception
			Throw New Exception("Ocorreu o Erro: " & Err.Description & " - Rotina: Etapa004" & "", ex)
			'MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: Etapa004" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try

	End Sub

	Private Sub Etapa005()
		Dim ldblRowAfec As Double
		Dim lblnDeuCerto As Boolean
		'LinkCenter
		Dim llngLC_Id As Integer
		Dim llngLC_Id_consumidor As Integer
		Dim llngLC_Rota As Integer
		Dim llngLC_Rota20 As Integer
		Dim llngLC_Sequencia As Integer
		Dim llngLC_Id_banco As Integer
		Dim llngLC_Id_endereco As Integer
		Dim llngLC_Id_cidade As Integer
		Dim lintLC_Entrega As Short
		Dim llngLC_Id_endereco_entrega As Integer
		Dim llngLC_Id_uc_agrupamento As Integer
		Dim llngLC_Aparelho_saude As Integer
		Dim llngLC_Pagamento_governo As Integer
		Dim llngLC_Vip_comercial As Integer
		Dim llngLC_Vip_tecnico As Integer
		Dim llngLC_Qh As Integer
		Dim llngLC_Vencimento_prox_mes As Integer
		Dim llngLC_Pagamento_duvidoso As Integer
		Dim llngLC_Isento_iluminacao As Integer
		Dim llngLC_Aliquota_diferente As Integer
		Dim llngLC_Unid_id_fatwin As Integer
		Dim llngLC_Agrupa_id_uc As Integer
		Dim llngLC_Agrupa_rota_uc As Integer
		Dim llngLC_Agrupa_sequencia_uc As Integer
		Dim lstrLC_Zlc As String
		Dim lstrLC_Descricao_banco As String
		Dim lstrLC_Agencia As String
		Dim lstrLC_Conta_corrente As String
		Dim lstrLC_Grupo As String
		Dim llngLC_Id_classe As Integer
		Dim lstrLC_Classe As String
		Dim lstrLC_Sub_classe As String
		Dim lstrLC_Nome As String
		Dim lstrLC_Cpf_cnpj As String
		Dim lstrLC_Ie As String
		Dim lstrLC_Rg As String
		Dim lstrLC_Situacao As String
		Dim lstrLC_Tipo As String
		Dim lstrLC_Endereco As String
		Dim lstrLC_Cidade As String
		Dim lstrLC_Cep As String
		Dim lstrLC_Numero As String
		Dim lstrLC_Bairro As String
		Dim lstrLC_Distrito As String
		Dim lstrLC_Uf As String
		Dim lstrLC_Complemento As String
		Dim lstrLC_Quadra As String
		Dim lstrLC_Lote As String
		Dim lstrLC_Endereco_entrega As String
		Dim lstrLC_Cidade_entrega As String
		Dim lstrLC_Cep_entrega As String
		Dim lstrLC_Numero_entrega As String
		Dim lstrLC_Bairro_entrega As String
		Dim lstrLC_Distrito_entrega As String
		Dim lstrLC_Uf_entrega As String
		Dim lstrLC_Complemento_entrega As String
		Dim lstrLC_Quadra_entrega As String
		Dim lstrLC_Lote_entrega As String
		Dim lstrLC_Conjunto As String
		Dim lstrLC_Tipo_ligacao As String
		Dim lstrLC_Fase_ligacao As String
		Dim lstrLC_Tipo_fase As String
		Dim lstrLC_Tensao As String
		Dim lstrLC_Regiao As String
		Dim lstrLC_Dia_vencimento As String
		Dim lstrLC_Id_agrupamento As String
		Dim lstrLC_Telefone As String
		Dim lstrLC_Fax As String
		Dim lstrLC_Celular As String
		Dim lstrLC_Historico As String
		Dim lstrLC_Cnae As String
		Dim lstrLC_Agrupa_nome_uc As String
		Dim lstrLC_Lacre As String
		Dim ldteLC_Data_cadastro As Date
		Dim ldteLC_Data_ligacao As Date
		Dim ldteLC_Data_contrato_demanda As Date
		Dim ldteLC_Data_religacao As Date
		Dim ldteLC_Data_corte As Date
		Dim ldteLC_Data_desligamento As Date
		Dim ldblLC_Id_ramal As Double
		Dim ldblLC_Id_medidor As Double
		Dim ldblLC_numero_medidor As Double
		Dim ldblLC_Demanda_contratada As Double
		Dim ldblLC_Demanda_contratada_ponta As Double
		Dim ldblLC_Demanda_contratada_fp As Double
		Dim ldblLC_Perdas As Double
		Dim ldblLC_Ultima_leitura_kwh As Double
		Dim ldblLC_Conjuge_Fatura As Double
		Dim bintid_antigo As Long
		Dim lstrLC_latitude As String
		Dim lstrLC_longitude As String
		Dim lstrLC_caixa_numero As String

		Dim lstrSql As String
		Try
			lstrSql = ""
			lstrSql = lstrSql & "SELECT * "
			lstrSql = lstrSql & "  FROM " & gstrBDCLinkCenter & ".`uc` `lc_uc` "
			lstrSql = lstrSql & " WHERE NOT EXISTS(SELECT * FROM `unidade_consumidora` `fw_uc` WHERE `fw_uc`.`unid_id` = `lc_uc`.`unid_id_fatwin`)"
			'lstrSql = lstrSql & " WHERE NOT EXISTS(SELECT * FROM `unidade_consumidora` `fw_uc` WHERE `fw_uc`.`id` = `lc_uc`.`unid_id_fatwin`)"
			lstrSql = lstrSql & " AND ZLC <> '9999999999'"
			If strTrataUC = "N" Then
				lstrSql = lstrSql & "   AND (`lc_uc`.`id_consumidor` > 0 AND `lc_uc`.`id_consumidor` IS NOT NULL) "
			End If
			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, " NOVAS UNIDADES CONSUMIDORAS ")
				For Each row As DataRow In dt.Rows

					bintid_antigo = CLng(IIf(IsDBNull(row.Item("id_antigo")), "0", row.Item("id_antigo")))
					llngLC_Id_consumidor = CInt(IIf(IsDBNull(row.Item("id_consumidor")), "0", row.Item("id_consumidor")))
					llngLC_Id = CInt(IIf(IsDBNull(row.Item("id")), "0", row.Item("id")))
					llngLC_Rota = CInt(IIf(IsDBNull(row.Item("rota")), "9999", row.Item("rota")))
					If llngLC_Rota = 0 Then llngLC_Rota = 9999
					llngLC_Sequencia = CInt(IIf(IsDBNull(row.Item("sequencia")), "0", row.Item("sequencia")))
					llngLC_Id_banco = CInt(IIf(IsDBNull(row.Item("id_banco")), "0", row.Item("id_banco")))
					llngLC_Id_endereco = CInt(IIf(IsDBNull(row.Item("id_endereco")), "0", row.Item("id_endereco")))
					llngLC_Id_cidade = CInt(IIf(IsDBNull(row.Item("id_cidade")), "0", row.Item("id_cidade")))
					llngLC_Id_endereco_entrega = CInt(IIf(IsDBNull(row.Item("id_endereco_entrega")), "0", row.Item("id_endereco_entrega")))
					llngLC_Id_uc_agrupamento = CInt(IIf(IsDBNull(row.Item("id_uc_agrupamento")), "0", row.Item("id_uc_agrupamento")))
					llngLC_Aparelho_saude = CInt(IIf(IsDBNull(row.Item("aparelho_saude")), "0", row.Item("aparelho_saude")))
					llngLC_Pagamento_governo = CInt(IIf(IsDBNull(row.Item("pagamento_governo")), "0", row.Item("pagamento_governo")))
					llngLC_Vip_comercial = CInt(IIf(IsDBNull(row.Item("vip_comercial")), "0", row.Item("vip_comercial")))
					llngLC_Vip_tecnico = CInt(IIf(IsDBNull(row.Item("vip_tecnico")), "0", row.Item("vip_tecnico")))
					llngLC_Qh = CInt(IIf(IsDBNull(row.Item("qh")), "0", row.Item("qh")))
					llngLC_Vencimento_prox_mes = CInt(IIf(IsDBNull(row.Item("vencimento_prox_mes")), "0", row.Item("vencimento_prox_mes")))
					llngLC_Pagamento_duvidoso = CInt(IIf(IsDBNull(row.Item("pagamento_duvidoso")), "0", row.Item("pagamento_duvidoso")))
					llngLC_Isento_iluminacao = CInt(IIf(IsDBNull(row.Item("isento_iluminacao")), "0", row.Item("isento_iluminacao")))
					llngLC_Aliquota_diferente = CInt(IIf(IsDBNull(row.Item("aliquota_diferente")), "0", row.Item("aliquota_diferente")))
					llngLC_Unid_id_fatwin = CInt(IIf(IsDBNull(row.Item("unid_id_fatwin")), "0", row.Item("unid_id_fatwin")))
					llngLC_Agrupa_id_uc = CInt(IIf(IsDBNull(row.Item("agrupa_id_uc")), "0", row.Item("agrupa_id_uc")))
					llngLC_Agrupa_rota_uc = CInt(IIf(IsDBNull(row.Item("agrupa_rota_uc")), "0", row.Item("agrupa_rota_uc")))
					llngLC_Agrupa_sequencia_uc = CInt(IIf(IsDBNull(row.Item("agrupa_sequencia_uc")), "0", row.Item("agrupa_sequencia_uc")))
					lstrLC_Zlc = VB6.Format(row.Item("zlc"), "00000000000")
					If lstrLC_Zlc = "" Then lstrLC_Zlc = "99999999999"
					lstrLC_Descricao_banco = Trim(IIf(IsDBNull(row.Item("descricao_banco")), "", row.Item("descricao_banco")))
					lstrLC_Agencia = VB.Left(Str_Retira(Trim(IIf(IsDBNull(row.Item("agencia")), "", row.Item("agencia"))), "-./"), 4)
					lstrLC_Conta_corrente = VB.Left(Trim(IIf(IsDBNull(row.Item("conta_corrente")), "", row.Item("conta_corrente"))), 12)
					lstrLC_Grupo = Trim(IIf(IsDBNull(row.Item("grupo")), "", row.Item("grupo")))
					llngLC_Id_classe = CInt(IIf(IsDBNull(row.Item("id_classe")), "0", row.Item("id_classe")))
					lstrLC_Classe = Trim(IIf(IsDBNull(row.Item("descricao_classe")), "", row.Item("descricao_classe")))
					lstrLC_Sub_classe = Trim(IIf(IsDBNull(row.Item("descricao_classe_tar")), "", row.Item("descricao_classe_tar")))
					lstrLC_Nome = Trim(IIf(IsDBNull(row.Item("nome")), "", row.Item("nome")))
					lstrLC_Cpf_cnpj = Str_Retira(Trim(IIf(IsDBNull(row.Item("cpf_cnpj")), "", row.Item("cpf_cnpj"))), ".-/")
					lstrLC_Ie = Trim(IIf(IsDBNull(row.Item("ie")), "", row.Item("ie")))
					lstrLC_Rg = Trim(IIf(IsDBNull(row.Item("rg")), "", row.Item("rg")))
					lstrLC_Situacao = Trim(IIf(IsDBNull(row.Item("situacao")), "", row.Item("situacao")))
					lstrLC_Tipo = Trim(IIf(IsDBNull(row.Item("tipo")), "", row.Item("tipo")))
					lstrLC_Endereco = Trim(IIf(IsDBNull(row.Item("endereco")), "", row.Item("endereco")))
					lstrLC_Cidade = Trim(IIf(IsDBNull(row.Item("cidade")), "", row.Item("cidade")))
					lstrLC_Cep = Str_Retira(Trim(IIf(IsDBNull(row.Item("cep")), "", row.Item("cep"))), ".-/")
					lstrLC_Numero = Trim(IIf(IsDBNull(row.Item("numero")), "", row.Item("numero")))
					lstrLC_Bairro = Trim(IIf(IsDBNull(row.Item("bairro")), "", row.Item("bairro")))
					lstrLC_Distrito = Trim(IIf(IsDBNull(row.Item("distrito")), "", row.Item("distrito")))
					lstrLC_Uf = Trim(IIf(IsDBNull(row.Item("uf")), "", row.Item("uf")))
					lstrLC_Complemento = Trim(IIf(IsDBNull(row.Item("complemento")), "", row.Item("complemento")))
					lstrLC_Quadra = Trim(IIf(IsDBNull(row.Item("quadra")), "", row.Item("quadra")))
					lstrLC_Lote = Trim(IIf(IsDBNull(row.Item("lote")), "", row.Item("lote")))
					lstrLC_Endereco_entrega = Trim(IIf(IsDBNull(row.Item("endereco_entrega")), "", row.Item("endereco_entrega")))
					lstrLC_Cidade_entrega = Trim(IIf(IsDBNull(row.Item("cidade_entrega")), "", row.Item("cidade_entrega")))
					lstrLC_Cep_entrega = Str_Retira(Trim(IIf(IsDBNull(row.Item("cep_entrega")), "", row.Item("cep_entrega"))), ".-/")
					lstrLC_Numero_entrega = Trim(IIf(IsDBNull(row.Item("numero_entrega")), "", row.Item("numero_entrega")))
					lstrLC_Bairro_entrega = Trim(IIf(IsDBNull(row.Item("bairro_entrega")), "", row.Item("bairro_entrega")))
					lstrLC_Distrito_entrega = Trim(IIf(IsDBNull(row.Item("distrito_entrega")), "", row.Item("distrito_entrega")))
					lstrLC_Uf_entrega = Trim(IIf(IsDBNull(row.Item("uf_entrega")), "", row.Item("uf_entrega")))
					lstrLC_Complemento_entrega = Trim(IIf(IsDBNull(row.Item("complemento_entrega")), "", row.Item("complemento_entrega")))
					lstrLC_Quadra_entrega = Trim(IIf(IsDBNull(row.Item("quadra_entrega")), "", row.Item("quadra_entrega")))
					lstrLC_Lote_entrega = Trim(IIf(IsDBNull(row.Item("lote_entrega")), "", row.Item("lote_entrega")))
					lstrLC_Conjunto = Trim(IIf(IsDBNull(row.Item("conjunto")), "", row.Item("conjunto")))
					lstrLC_Fase_ligacao = Trim(IIf(IsDBNull(row.Item("fase_ligacao")), "ABCN", row.Item("fase_ligacao")))
					lstrLC_Tipo_fase = Trim(IIf(IsDBNull(row.Item("tipo_fase")), "T", row.Item("tipo_fase")))
					lstrLC_Tensao = Trim(IIf(IsDBNull(row.Item("tensao")), "380", row.Item("tensao")))
					lstrLC_Regiao = Trim(IIf(IsDBNull(row.Item("regiao")), "U", row.Item("regiao")))
					lstrLC_Dia_vencimento = Trim(IIf(IsDBNull(row.Item("dia_vencimento")), "", row.Item("dia_vencimento")))
					If gstrEmpresa_Sigla = "EFLUL" Then
						If lstrLC_Dia_vencimento = "" Then lstrLC_Dia_vencimento = "20"
					End If
					lstrLC_Id_agrupamento = CStr(Val(Trim(IIf(IsDBNull(row.Item("id_agrupamento")), "1", row.Item("id_agrupamento")))))
					lstrLC_Telefone = Trim(IIf(IsDBNull(row.Item("telefone")), "", row.Item("telefone")))
					lstrLC_Fax = Trim(IIf(IsDBNull(row.Item("fax")), "", row.Item("fax")))
					lstrLC_Celular = Trim(IIf(IsDBNull(row.Item("celular")), "", row.Item("celular")))
					lstrLC_Historico = Trim(IIf(IsDBNull(row.Item("historico")), "", row.Item("historico")))
					lstrLC_Cnae = VB.Left(Str_Retira(Trim(IIf(IsDBNull(row.Item("codigo_cnae")), "", row.Item("codigo_cnae"))), "./- "), 4)
					lstrLC_Agrupa_nome_uc = Trim(IIf(IsDBNull(row.Item("agrupa_nome_uc")), "", row.Item("agrupa_nome_uc")))
					lstrLC_Lacre = Trim(IIf(IsDBNull(row.Item("lacre")), "", row.Item("lacre")))
					ldteLC_Data_cadastro = CDate(IIf(IsDBNull(row.Item("data_cadastro")), "1900-01-01", row.Item("data_cadastro")))
					ldteLC_Data_ligacao = CDate(IIf(IsDBNull(row.Item("data_ligacao")), "1900-01-01", row.Item("data_ligacao")))
					ldteLC_Data_contrato_demanda = CDate(IIf(IsDBNull(row.Item("data_contrato_demanda")), "1900-01-01", row.Item("data_contrato_demanda")))
					ldteLC_Data_religacao = CDate(IIf(IsDBNull(row.Item("data_religacao")), "1900-01-01", row.Item("data_religacao")))
					ldteLC_Data_corte = CDate(IIf(IsDBNull(row.Item("data_corte")), "1900-01-01", row.Item("data_corte")))
					ldteLC_Data_desligamento = CDate(IIf(IsDBNull(row.Item("data_desligamento")), "1900-01-01", row.Item("data_desligamento")))
					ldblLC_Id_ramal = CDbl(IIf(IsDBNull(row.Item("id_ramal")), "0", row.Item("id_ramal")))

					'lstrLC_Tipo_ligacao = BuscaTipoLigacaoRamal(ldblLC_Id_ramal)

					ldblLC_Id_medidor = CDbl(IIf(IsDBNull(row.Item("id_medidor")), "0", row.Item("id_medidor")))
					ldblLC_numero_medidor = CDbl(IIf(IsDBNull(row.Item("numero_medidor")), "0", row.Item("id_medidor")))
					ldblLC_Demanda_contratada = CDbl(IIf(IsDBNull(row.Item("demanda_contratada")), "0", row.Item("demanda_contratada")))
					ldblLC_Demanda_contratada_ponta = CDbl(IIf(IsDBNull(row.Item("demanda_contratada_ponta")), "0", row.Item("demanda_contratada_ponta")))
					ldblLC_Demanda_contratada_fp = CDbl(IIf(IsDBNull(row.Item("demanda_contratada_fp")), "0", row.Item("demanda_contratada_fp")))
					ldblLC_Perdas = CDbl(IIf(IsDBNull(row.Item("perdas")), "0", row.Item("perdas")))
					ldblLC_Ultima_leitura_kwh = CDbl(IIf(IsDBNull(row.Item("ultima_leitura_kwh")), "0", row.Item("ultima_leitura_kwh")))

					'ldblLC_Ultima_leitura_kwh = BuscaUltimaLeituraMED(ldblLC_Id_medidor)

					ldblLC_Conjuge_Fatura = CDbl(IIf(IsDBNull(row.Item("conjuge_fatura")), "0", row.Item("conjuge_fatura")))
					lstrLC_latitude = CDbl(IIf(IsDBNull(row.Item("latitude")), "0", row.Item("latitude")))
					lstrLC_longitude = CDbl(IIf(IsDBNull(row.Item("longitude")), "0", row.Item("longitude")))
					lstrLC_caixa_numero = Trim(IIf(IsDBNull(row.Item("numero_caixa")), "", row.Item("numero_caixa")))

					lstrSql = ""
					lstrSql = lstrSql & "id = " & llngLC_Id & ", "
					lstrSql = lstrSql & "unid_id = " & bintid_antigo & ", "
					lstrSql = lstrSql & "cons_id = " & llngLC_Id_consumidor & ", "

					If VB.Left(lstrLC_Zlc, 3) <> "999" Then
						lstrSql = lstrSql & "zona = '" & VB.Left(lstrLC_Zlc, 3) & "', "
					Else
						lstrSql = lstrSql & "zona = '999', "
					End If
					If VB.Right(VB.Left(lstrLC_Zlc, 5), 2) <> "99" Then
						lstrSql = lstrSql & "livro = '" & VB.Right(VB.Left(lstrLC_Zlc, 5), 2) & "', "
					Else
						lstrSql = lstrSql & "livro = '99', "
					End If
					If VB.Right(lstrLC_Zlc, 6) <> "999999" Then
						lstrSql = lstrSql & "conta = '" & VB.Right(lstrLC_Zlc, 6) & "', "
					Else
						lstrSql = lstrSql & "conta = '999999', "
					End If

					If llngLC_Rota <> CDbl("9999") Then
						If gstrEmpresa_Sigla = "EFLUL" Then
							''If lstrLC_Dia_vencimento = 15 Or lstrLC_Dia_vencimento = 25 Then
							llngLC_Rota20 = 20
							lstrSql = lstrSql & "rota = '" & VB6.Format(llngLC_Rota20, "00") & VB6.Format(VB.Right(CStr(llngLC_Rota), 2), "00") & VB6.Format(llngLC_Sequencia, "0000") & "', "
							''Else
							''  lstrSql = lstrSql & "rota = '" & Strings.Format(lstrLC_Dia_vencimento, "00") & Strings.Format(Right(llngLC_Rota, 2), "00") & Strings.Format(llngLC_Sequencia, "0000") & "', "
							'
						Else
							lstrSql = lstrSql & "caderno = '" & llngLC_Rota & "',"
							lstrSql = lstrSql & "rota = '" & llngLC_Rota & "',"
							lstrSql = lstrSql & "sequencia ='" & llngLC_Sequencia & "',"
							'lstrSql = lstrSql & "rota = '" & VB6.Format(llngLC_Rota, "0000") & VB6.Format(llngLC_Sequencia, "0000") & "', "
						End If
					Else
						lstrSql = lstrSql & "rota = '99999999', "
					End If

					lstrSql = lstrSql & "luz_todos = 'N', "
					lstrSql = lstrSql & "pagto_duvidoso = " & llngLC_Pagamento_duvidoso & ", "
					lstrSql = lstrSql & "banco    = '" & BuscaIdBancoLinkCenter(llngLC_Id_banco) & "', "
					lstrSql = lstrSql & "agenc    = '" & lstrLC_Agencia & "', "
					lstrSql = lstrSql & "cc       = '" & lstrLC_Conta_corrente & "', "

					If lstrLC_Grupo = "B" Then
						lstrSql = lstrSql & "grupo    = '" & VB.Left(Trim(lstrLC_Sub_classe), 2) & "', "
						Select Case VB.Left(Trim(lstrLC_Sub_classe), 2)
							Case Is = "B3"
								If InStr(1, lstrLC_Sub_classe, "municipal") > 0 Then
									lstrSql = lstrSql & "ta       = '01" & VB.Left(Trim(lstrLC_Sub_classe), 2) & "34', "
								ElseIf InStr(1, lstrLC_Sub_classe, "estadual") > 0 Then
									lstrSql = lstrSql & "ta       = '01" & VB.Left(Trim(lstrLC_Sub_classe), 2) & "35', "
								ElseIf InStr(1, lstrLC_Sub_classe, "federal") > 0 Then
									lstrSql = lstrSql & "ta       = '01" & VB.Left(Trim(lstrLC_Sub_classe), 2) & "36', "
								ElseIf InStr(1, lstrLC_Sub_classe, "Tração elétrica") > 0 Then
									lstrSql = lstrSql & "ta       = '01" & VB.Left(Trim(lstrLC_Sub_classe), 2) & "37', "
								ElseIf InStr(1, lstrLC_Sub_classe, "Água, esgoto e saneamento") > 0 Then
									lstrSql = lstrSql & "ta       = '01" & VB.Left(Trim(lstrLC_Sub_classe), 2) & "38', "
								Else
									lstrSql = lstrSql & "ta       = '01" & VB.Left(Trim(lstrLC_Sub_classe), 2) & "00', "
								End If
							Case Else
								lstrSql = lstrSql & "ta       = '01" & VB.Left(Trim(lstrLC_Sub_classe), 2) & "00', "
						End Select
					ElseIf lstrLC_Grupo = "A" Then
						lstrSql = lstrSql & "grupo    = 'A4', "
						lstrSql = lstrSql & "ta       = '01A400', "
					End If

					lstrSql = lstrSql & "clas = '" & VB6.Format(llngLC_Id_classe, "0000") & "', " 'BuscaClasse(llngLC_Id_classe, lstrLC_Classe, lstrLC_Sub_classe) & "', "
					lstrSql = lstrSql & "fase     = '" & lstrLC_Tipo_fase & "', "
					lstrSql = lstrSql & "situacao = '" & lstrLC_Situacao & "', "
					lstrSql = lstrSql & "ilum     = '" & IIf(llngLC_Isento_iluminacao = 0, "S", "N") & "', "
					lstrSql = lstrSql & "tensao   = '" & lstrLC_Tensao & "', "
					lstrSql = lstrSql & "faselig  = '" & lstrLC_Fase_ligacao & "', "
					'Parte de logradouro vai ser pedreira
					'lstrSql = lstrSql & "codend     = '" & BuscaAtualizaCodEndereco(llngLC_Id_endereco) & "', "
					lstrSql = lstrSql & "codend     = " & llngLC_Id_endereco & ", "
					lstrSql = lstrSql & "uf         = '" & Replace(lstrLC_Uf, "'", "\'") & "', "
					lstrSql = lstrSql & "cep        = '" & lstrLC_Cep & "', "
					lstrSql = lstrSql & "codcidade  = " & llngLC_Id_cidade & ", "
					lstrSql = lstrSql & "cidade     = '" & Replace(lstrLC_Cidade, "'", "\'") & "', "
					lstrSql = lstrSql & "distrito   = '" & Replace(lstrLC_Distrito, "'", "\'") & "', "
					lstrSql = lstrSql & "bairro     = '" & Replace(lstrLC_Bairro, "'", "\'") & "', "
					lstrSql = lstrSql & "end        = '" & Replace(lstrLC_Endereco, "'", "\'") & "', "
					lstrSql = lstrSql & "numero     = '" & Replace(lstrLC_Numero, "'", "\'") & "', "
					lstrSql = lstrSql & "complement = '" & Replace(lstrLC_Complemento, "'", "\'") & "', "
					lstrSql = lstrSql & "quadra     = '" & Replace(lstrLC_Quadra, "'", "\'") & "', "
					lstrSql = lstrSql & "lote       = '" & Replace(lstrLC_Lote, "'", "\'") & "', "
					lintLC_Entrega = 0

					If llngLC_Id_endereco_entrega > 0 Then
						If (lstrLC_Cidade_entrega & lstrLC_Bairro_entrega & lstrLC_Endereco_entrega & lstrLC_Numero_entrega & lstrLC_Complemento_entrega) <> (lstrLC_Cidade & lstrLC_Bairro & lstrLC_Endereco & lstrLC_Numero & lstrLC_Complemento) Then
							lintLC_Entrega = 1
						Else
							lintLC_Entrega = 0
						End If
					Else
						lintLC_Entrega = 0
					End If


					'If CInt(IIf(IsNumeric(lstrLC_Id_agrupamento), lstrLC_Id_agrupamento, 0)) = 2 Then lintLC_Entrega = 1 'Agrupamento CORREIOS sempre tem que ser = 2 em todos os clientes

					lstrSql = lstrSql & "entrega = " & lintLC_Entrega & ", "
					'lstrSql = lstrSql & "codend_entrega = '" & BuscaAtualizaCodEndereco(llngLC_Id_endereco_entrega) & "', "
					lstrSql = lstrSql & "codend_entrega = " & llngLC_Id_endereco_entrega & ", "
					lstrSql = lstrSql & "uf_entrega     = '" & Replace(lstrLC_Uf_entrega, "'", "\'") & "', "
					lstrSql = lstrSql & "cep_entrega    = '" & lstrLC_Cep_entrega & "', "
					lstrSql = lstrSql & "cid_entrega    = '" & Replace(lstrLC_Cidade_entrega, "'", "\'") & "', "
					lstrSql = lstrSql & "dis_entrega    = '" & Replace(lstrLC_Distrito_entrega, "'", "\'") & "', "
					lstrSql = lstrSql & "bai_entrega    = '" & Replace(lstrLC_Bairro_entrega, "'", "\'") & "', "
					lstrSql = lstrSql & "end_entrega    = '" & Replace(lstrLC_Endereco_entrega, "'", "\'") & "', "
					lstrSql = lstrSql & "num_entrega    = '" & Replace(lstrLC_Numero_entrega, "'", "\'") & "', "
					lstrSql = lstrSql & "compl_entrega  = '" & Replace(lstrLC_Complemento_entrega, "'", "\'") & "', "
					lstrSql = lstrSql & "qua_entrega    = '" & Replace(lstrLC_Quadra_entrega, "'", "\'") & "', "
					lstrSql = lstrSql & "lot_entrega    = '" & Replace(lstrLC_Lote_entrega, "'", "\'") & "', "
					lstrSql = lstrSql & "diavenc        = '" & VB6.Format(lstrLC_Dia_vencimento, "00") & "', "
					lstrSql = lstrSql & "cpf_cnpj       = '" & lstrLC_Cpf_cnpj & "', "
					lstrSql = lstrSql & "rg             = '" & lstrLC_Rg & "', "
					lstrSql = lstrSql & "insc           = '" & lstrLC_Ie & "', "
					lstrSql = lstrSql & "pessoa         = '" & lstrLC_Tipo & "', "
					lstrSql = lstrSql & "nome           = '" & Replace(lstrLC_Nome, "'", "\'") & "', "
					lstrSql = lstrSql & "circuito       = '" & lstrLC_Conjunto & "', "
					lstrSql = lstrSql & "lacre          = '" & lstrLC_Lacre & "', "
					lstrSql = lstrSql & "ap_saude       = '" & IIf(llngLC_Aparelho_saude = 1, "S", "N") & "', "
					lstrSql = lstrSql & "tp_ligacao     = '" & lstrLC_Tipo_ligacao & "', "
					lstrSql = lstrSql & "regiao         = '" & lstrLC_Regiao & "', "
					lstrSql = lstrSql & "cnae           = '" & lstrLC_Cnae & "', "
					lstrSql = lstrSql & "num            = '" & ldblLC_numero_medidor & "', "
					lstrSql = lstrSql & "data_ligacao   = '" & VB6.Format(ldteLC_Data_ligacao, "yyyy-MM-dd") & "', "
					lstrSql = lstrSql & "data_religacao = '" & VB6.Format(ldteLC_Data_religacao, "yyyy-MM-dd") & "', "
					lstrSql = lstrSql & "data_desligamento = '" & VB6.Format(ldteLC_Data_desligamento, "yyyy-MM-dd") & "', "
					lstrSql = lstrSql & "data_entrada = '" & VB6.Format(ldteLC_Data_ligacao, "yyyy-MM-dd") & "', "
					lstrSql = lstrSql & "data_corte = '" & VB6.Format(ldteLC_Data_corte, "yyyy-MM-dd") & "', "
					lstrSql = lstrSql & "data_contrato_demanda = '" & VB6.Format(ldteLC_Data_contrato_demanda, "yyyy-MM-dd") & "', "
					lstrSql = lstrSql & "ultleit = " & Replace(CStr(ldblLC_Ultima_leitura_kwh), ",", ".") & ", "
					lstrSql = lstrSql & "final = " & Replace(CStr(ldblLC_Perdas), ",", ".") & ", "
					lstrSql = lstrSql & "dem_contr_fp = " & Replace(CStr(ldblLC_Demanda_contratada_fp), ",", ".") & ", "
					lstrSql = lstrSql & "dem_contr_ponta = " & Replace(CStr(ldblLC_Demanda_contratada_ponta), ",", ".") & ", "
					lstrSql = lstrSql & "demanda_contr = " & Replace(CStr(ldblLC_Demanda_contratada_ponta), ",", ".") & ", "
					lstrSql = lstrSql & "res694 = 'N', "
					lstrSql = lstrSql & "isento = 'N', "
					lstrSql = lstrSql & "sgdenvio = 'P',"
					lstrSql = lstrSql & "bx0402 = 'N', "
					lstrSql = lstrSql & "comprovante_rural = 'N', "
					lstrSql = lstrSql & "jul2010 = 'N', "
					lstrSql = lstrSql & "estufa = 'N', "
					lstrSql = lstrSql & "encargo = 'N', "
					lstrSql = lstrSql & "flg_sazonal = 'N',"
					lstrSql = lstrSql & "icms_demanda_lida = 'N', "
					lstrSql = lstrSql & "icms_diferido = 'N', "
					lstrSql = lstrSql & "icms_isento_demanda = 'N', "
					lstrSql = lstrSql & "agrupa = '" & BuscaIdAgrupaLinkCenter(lstrLC_Id_agrupamento) & "', "
					lstrSql = lstrSql & "Conjuge_Fatura = " & Replace(CStr(ldblLC_Conjuge_Fatura), ",", ".") & ", "

					If lstrLC_latitude = 0 Then
						lstrSql = lstrSql & "Latitude = '0.000000', "
					Else
						lstrSql = lstrSql & "Latitude = " & Replace(CStr(lstrLC_latitude), ",", ".") & ", "
					End If

					If lstrLC_longitude = 0 Then
						lstrSql = lstrSql & "Longitude = '0.000000', "
					Else
						lstrSql = lstrSql & "Longitude = " & Replace(CStr(lstrLC_longitude), ",", ".") & ", "
					End If

					lstrSql = lstrSql & "numero_caixa = '" & lstrLC_caixa_numero & "' "

					lblnDeuCerto = True

					objExecAtualizaBancoDados.QUERY_NAME = "START TRANSACTION"
					repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

					objExecAtualizaBancoDados.QUERY_NAME = "INSERT INTO `uc_faturamento` SET " & lstrSql
					repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

					Me.lblRegEtapa(4).Text = VB6.Format(CDbl(IIf(IsNumeric(Me.lblRegEtapa(4).Text), Me.lblRegEtapa(4).Text, 0)) + ldblRowAfec, "#0")

					'Busca o consreg e atualiza no LC
					objAtualizaBancoDados.QUERY_NAME = "SELECT LAST_INSERT_ID() "
					dt1 = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

					If dt1.Rows.Count > 0 Then

						llngLC_Unid_id_fatwin = CInt(IIf(IsDBNull(row.Item(0)), 0, row.Item(0)))

						lstrSql = ""
						lstrSql = lstrSql & "INSERT INTO `unidade_consumidora` SET "
						'lstrSql = lstrSql & "id         =  " & llngLC_Id & ", "
						lstrSql = lstrSql & "grupo      = '" & lstrLC_Grupo & "', "
						lstrSql = lstrSql & "unid_id    =  " & llngLC_Unid_id_fatwin & ", "
						lstrSql = lstrSql & "cons_id    =  " & llngLC_Id_consumidor & ", "
						lstrSql = lstrSql & "nome       = '" & Replace(lstrLC_Nome, "'", "\'") & "', "
						lstrSql = lstrSql & "cpf_cnpj   = '" & lstrLC_Cpf_cnpj & "', "
						lstrSql = lstrSql & "situacao   = '" & lstrLC_Situacao & "', "
						'lstrSql = lstrSql & "codend     = '" & BuscaAtualizaCodEndereco(llngLC_Id_endereco) & "', "
						lstrSql = lstrSql & "codend     = " & llngLC_Id_endereco & ", "
						lstrSql = lstrSql & "uf         = '" & Replace(lstrLC_Uf, "'", "\'") & "', "
						lstrSql = lstrSql & "cep        = '" & lstrLC_Cep & "', "
						lstrSql = lstrSql & "cidade     = '" & Replace(lstrLC_Cidade, "'", "\'") & "', "
						lstrSql = lstrSql & "distrito   = '" & Replace(lstrLC_Distrito, "'", "\'") & "', "
						lstrSql = lstrSql & "bairro     = '" & Replace(lstrLC_Bairro, "'", "\'") & "', "
						lstrSql = lstrSql & "end        = '" & Replace(lstrLC_Endereco, "'", "\'") & "', "
						lstrSql = lstrSql & "numero     = '" & Replace(lstrLC_Numero, "'", "\'") & "', "
						lstrSql = lstrSql & "complement = '" & Replace(lstrLC_Complemento, "'", "\'") & "', "
						lstrSql = lstrSql & "quadra     = '" & Replace(lstrLC_Quadra, "'", "\'") & "', "
						lstrSql = lstrSql & "lote       = '" & Replace(lstrLC_Lote, "'", "\'") & "', "
						lstrSql = lstrSql & "diavenc    = '" & VB6.Format(lstrLC_Dia_vencimento, "00") & "', "
						lstrSql = lstrSql & "regiao     = '" & lstrLC_Regiao & "', "
						lstrSql = lstrSql & "dt_ligacao   = '" & VB6.Format(ldteLC_Data_ligacao, "yyyy-MM-dd") & "', "
						lstrSql = lstrSql & "dt_religacao = '" & VB6.Format(ldteLC_Data_religacao, "yyyy-MM-dd") & "', "
						lstrSql = lstrSql & "dt_desligamento = '" & VB6.Format(ldteLC_Data_desligamento, "yyyy-MM-dd") & "', "
						lstrSql = lstrSql & "dt_entrada = '" & VB6.Format(ldteLC_Data_ligacao, "yyyy-MM-dd") & "', "
						lstrSql = lstrSql & "dt_corte = '" & VB6.Format(ldteLC_Data_corte, "yyyy-MM-dd") & "', "

						If llngLC_Rota <> CDbl("9999") Then
							lstrSql = lstrSql & "rota_numero    = '" & VB.Right(VB6.Format(llngLC_Rota, "0000"), 2) & "', "
							lstrSql = lstrSql & "rota_sequencia = '" & VB6.Format(llngLC_Sequencia, "0000") & "', "
						Else
							lstrSql = lstrSql & "rota_numero    = '9999', "
							lstrSql = lstrSql & "rota_sequencia = '9999', "
						End If
						If VB.Left(lstrLC_Zlc, 3) <> "999" Then
							lstrSql = lstrSql & "zona = '" & VB.Left(lstrLC_Zlc, 3) & "', "
						Else
							lstrSql = lstrSql & "zona = '999', "
						End If
						If VB.Right(VB.Left(lstrLC_Zlc, 5), 2) <> "99" Then
							lstrSql = lstrSql & "livro = '" & VB.Right(VB.Left(lstrLC_Zlc, 5), 2) & "', "
						Else
							lstrSql = lstrSql & "livro = '99', "
						End If
						If VB.Right(lstrLC_Zlc, 6) <> "999999" Then
							lstrSql = lstrSql & "conta = '" & VB.Right(lstrLC_Zlc, 6) & "' "
						Else
							lstrSql = lstrSql & "conta = '999999' "
						End If
						objExecAtualizaBancoDados.QUERY_NAME = lstrSql
						repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

						Call AtualizaUnidIdLC(llngLC_Unid_id_fatwin, llngLC_Id)

						'Else
						'Call myConC.Execute("ROLLBACK")
						'lblnDeuCerto = False

					End If

					Call InsereAuditoriaUnidadeConsumidora(llngLC_Unid_id_fatwin, lstrLC_Grupo, "INCLUIDO")

					objExecAtualizaBancoDados.QUERY_NAME = "COMMIT"
					repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1,  , " NOVAS UNIDADES CONSUMIDORAS ")
				Next
			End If
		Catch ex As Exception

			If UCase(Err.Source) = "MYSQL.DATA" Then
				objExecAtualizaBancoDados.QUERY_NAME = "ROLLBACK"
				repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)
			End If
			Throw New Exception("Ocorreu o Erro: " & Err.Description & " - Rotina: Etapa005" & "", ex)
			'MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: Etapa005" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try

	End Sub

	Private Sub Etapa006()
		Dim ldblRowAfec As Double
		'Fatwin
		Dim ldblConstante As Double
		Dim ldblResiduo As Double
		Dim lstrFW_Zona As String
		Dim lstrFW_Livro As String
		Dim lstrFW_Conta As String
		Dim llngFW_Cons_id As Integer
		Dim llngFW_Pagto_duvidoso As Integer
		Dim lstrFW_Banco As String
		Dim lstrFW_Agenc As String
		Dim lstrFW_Cc As String
		Dim lstrFW_Grupo As String
		Dim lstrFW_Ta As String
		Dim lstrFW_Clas As String
		Dim lstrFW_Bxrenda As String
		Dim lstrFW_Fase As String
		Dim lstrFW_Situacao As String
		Dim lstrFW_Ilum As String
		Dim lstrFW_Tensao As String
		Dim lstrFW_Faselig As String
		Dim lstrFW_Codend As String
		Dim lstrFW_Uf As String
		Dim lstrFW_Cep As String
		Dim lstrFW_Cidade As String
		Dim lstrFW_Distrito As String
		Dim lstrFW_Bairro As String
		Dim lstrFW_End As String
		Dim lstrFW_Numero As String
		Dim lstrFW_Complement As String
		Dim lstrFW_Quadra As String
		Dim lstrFW_Lote As String
		Dim llngFW_Entrega As Integer
		Dim lstrFW_Codend_entrega As String
		Dim lstrFW_Uf_entrega As String
		Dim lstrFW_Cep_entrega As String
		Dim lstrFW_Cid_entrega As String
		Dim lstrFW_Dis_entrega As String
		Dim lstrFW_Bai_entrega As String
		Dim lstrFW_End_entrega As String
		Dim lstrFW_Num_entrega As String
		Dim lstrFW_Compl_entrega As String
		Dim lstrFW_Qua_entrega As String
		Dim lstrFW_Lot_entrega As String
		Dim lstrFW_Diavenc As String
		Dim lstrFW_Cpf_cnpj As String
		Dim lstrFW_Rg As String
		Dim lstrFW_Insc As String
		Dim lstrFW_Pessoa As String
		Dim lstrFW_Nome As String
		Dim lstrFW_Circuito As String
		Dim lstrFW_Lacre As String
		Dim lstrFW_Ap_saude As String
		Dim lstrFW_Tp_ligacao As String
		Dim lstrFW_Regiao As String
		Dim lstrFW_Cnae As String
		Dim ldteFW_Data_ligacao As Date
		Dim ldteFW_Data_religacao As Date
		Dim ldteFW_Data_desligamento As Date
		Dim ldteFW_Data_entrada As Date
		Dim ldteFW_Data_corte As Date
		Dim ldteFW_Data_contrato_demanda As Date
		Dim ldblFW_Ultleit As Double
		Dim ldblFW_UltleitDesligado As Double
		Dim ldblFW_Final As Double
		Dim ldblFW_Dem_contr_fp As Double
		Dim ldblFW_Dem_contr_ponta As Double
		Dim ldblFW_Demanda_contr As Double
		Dim llngFW_Agrupa As Integer
		Dim ldblFW_Id_medidor As Double
		Dim lstrFW_Numero_medidor As String
		Dim ldblFW_Conjuge_Fatura As Double
		Dim lstrFW_Latitude As String
		Dim lstrFW_Longitude As String
		Dim lstrFW_Caixa_Numero As String

		'LinkCenter
		Dim iOSCod As Integer
		Dim llngLC_Id As Integer
		Dim llngLC_Id_consumidor As Integer
		Dim llngLC_Rota As Integer
		Dim llngLC_Sequencia As Integer
		Dim llngLC_Id_banco As Integer
		Dim llngLc_Id_banco_fatwin As Integer
		Dim llngLc_Id_agrupa_fatwin As Integer
		Dim llngLC_Id_endereco As Integer
		Dim lstrLC_Id_endereco_fatwin As String
		Dim llngLC_Id_cidade As Integer
		Dim lintLC_Entrega As Short
		Dim llngLC_Id_endereco_entrega As Integer
		Dim lstrLC_Id_endereco_entrega_fatwin As String
		Dim llngLC_Id_uc_agrupamento As Integer
		Dim llngLC_Aparelho_saude As Integer
		Dim llngLC_Pagamento_governo As Integer
		Dim llngLC_Vip_comercial As Integer
		Dim llngLC_Vip_tecnico As Integer
		Dim llngLC_Qh As Integer
		Dim llngLC_Vencimento_prox_mes As Integer
		Dim llngLC_Pagamento_duvidoso As Integer
		Dim llngLC_Isento_iluminacao As Integer
		Dim llngLC_Aliquota_diferente As Integer
		Dim llngLC_Unid_id_fatwin As Integer
		Dim llngLC_Agrupa_id_uc As Integer
		Dim llngLC_Agrupa_rota_uc As Integer
		Dim llngLC_Agrupa_sequencia_uc As Integer
		Dim lstrLC_Zlc As String
		Dim lstrLC_Descricao_banco As String
		Dim lstrLC_Agencia As String
		Dim lstrLC_Conta_corrente As String
		Dim lstrLC_Grupo As String
		Dim llngLC_Id_classe As String
		Dim lstrLC_Classe As String
		Dim lstrLC_Sub_classe As String
		Dim lstrLC_Nome As String
		Dim lstrLC_Cpf_cnpj As String
		Dim lstrLC_Ie As String
		Dim lstrLC_Rg As String
		Dim lstrLC_Situacao As String
		Dim lstrLC_Tipo As String
		Dim lstrLC_Endereco As String
		Dim lstrLC_Cidade As String
		Dim lstrLC_Cep As String
		Dim lstrLC_Numero As String
		Dim lstrLC_Bairro As String
		Dim lstrLC_Distrito As String
		Dim lstrLC_Uf As String
		Dim lstrLC_Complemento As String
		Dim lstrLC_Quadra As String
		Dim lstrLC_Lote As String
		Dim lstrLC_Endereco_entrega As String
		Dim lstrLC_Cidade_entrega As String
		Dim lstrLC_Cep_entrega As String
		Dim lstrLC_Numero_entrega As String
		Dim lstrLC_Bairro_entrega As String
		Dim lstrLC_Distrito_entrega As String
		Dim lstrLC_Uf_entrega As String
		Dim lstrLC_Complemento_entrega As String
		Dim lstrLC_Quadra_entrega As String
		Dim lstrLC_Lote_entrega As String
		Dim lstrLC_Conjunto As String
		Dim lstrLC_Tipo_ligacao As String
		Dim lstrLC_Fase_ligacao As String
		Dim lstrLC_Tipo_fase As String
		Dim lstrLC_Tensao As String
		Dim lstrLC_Regiao As String
		Dim lstrLC_Dia_vencimento As String
		Dim lstrLC_Id_agrupamento As String
		Dim lstrLC_Telefone As String
		Dim lstrLC_Fax As String
		Dim lstrLC_Celular As String
		Dim lstrLC_Historico As String
		Dim lstrLC_Cnae As String
		Dim lstrLC_Agrupa_nome_uc As String
		Dim lstrLC_Lacre As String
		Dim ldteLC_Data_cadastro As Date
		Dim ldteLC_Data_ligacao As Date
		Dim ldteLC_Data_contrato_demanda As Date
		Dim ldteLC_Data_religacao As Date
		Dim ldteLC_Data_corte As Date
		Dim ldteLC_Data_desligamento As Date
		Dim ldblLC_Id_ramal As Double
		Dim ldblLC_Id_medidor As Double
		Dim lStrLC_Numero_medidor As String
		Dim ldblLC_Demanda_contratada As Double
		Dim ldblLC_Demanda_contratada_ponta As Double
		Dim ldblLC_Demanda_contratada_fp As Double
		Dim ldblLC_Perdas As Double
		Dim ldblLC_Ultima_leitura_kwh As Double
		Dim ldblLC_Ultima_leitant_kwh As Double
		Dim ldblLC_Conjuge_Fatura As Double
		Dim lstrLC_Latitude As String
		Dim lstrLC_Longitude As String
		Dim lstrLC_numero_caixa As String
		'
		Dim lstrSql As String

		Try

			lstrSql = ""
			If Me.chkLogradouro.CheckState = System.Windows.Forms.CheckState.Unchecked Then
				lstrSql = lstrSql & "SELECT * " & vbCrLf
				lstrSql = lstrSql & "  FROM " & gstrBDCLinkCenter & ".`uc` `lc_uc` INNER JOIN " & gstrBDCLinkCenter & ".`os` `lc_o` ON(`lc_o`.`id_uc` = `lc_uc`.`id`) " & vbCrLf
				lstrSql = lstrSql & "  WHERE (`lc_uc`.`unid_id_fatwin` > 0 AND `lc_uc`.`unid_id_fatwin` IS NOT NULL) " & vbCrLf
				lstrSql = lstrSql & "   AND  (`lc_uc`.`id_consumidor` > 0 AND `lc_uc`.`id_consumidor` IS NOT NULL) " & vbCrLf
				If Trim(Me.txtSequenciasOs.Text) <> "" Then
					lstrSql = lstrSql & "                 AND `lc_o`.`sequencia_os` in(" & Me.txtSequenciasOs.Text & ") " & vbCrLf
				Else
					lstrSql = lstrSql & "   AND  (DATE(`lc_o`.`data_conclusao`) BETWEEN '" & VB6.Format(idteIni, "yyyy-MM-dd") & "' AND '" & VB6.Format(idteFim, "yyyy-MM-dd") & "')" & vbCrLf
				End If

				'lstrSql = lstrSql & "   AND EXISTS(SELECT * " & vbCrLf
				'lstrSql = lstrSql & "                FROM " & gstrBDCLinkCenter & ".`os` `lc_o` " & vbCrLf
				'lstrSql = lstrSql & "               WHERE `lc_o`.`id_uc` = `lc_uc`.`id` " & vbCrLf
				'lstrSql = lstrSql & "                 AND `lc_o`.`id_consumidor` = `lc_uc`.`id_consumidor` " & vbCrLf
				'lstrSql = lstrSql & "                 AND (`lc_o`.`data_conclusao` BETWEEN '" & VB6.Format(idteIni, "yyyy-MM-dd HH:MM:SS") & "' AND '" & VB6.Format(idteFim, "yyyy-MM-dd HH:MM:SS") & "')" & vbCrLf
				'If Trim(Me.txtSequenciasOs.Text) <> "" Then
				'    lstrSql = lstrSql & "                 AND `lc_o`.`sequencia_os` in(" & Me.txtSequenciasOs.Text & ") " & vbCrLf

				'lstrSql = lstrSql & ") "

			Else

				lstrSql = lstrSql & "SELECT * " & vbCrLf
				lstrSql = lstrSql & "  FROM " & gstrBDCLinkCenter & ".`uc` `lc_uc` " & vbCrLf
				lstrSql = lstrSql & " WHERE (`lc_uc`.`unid_id_fatwin` > 0 AND `lc_uc`.`unid_id_fatwin` IS NOT NULL) " & vbCrLf
				lstrSql = lstrSql & "   AND (`lc_uc`.`id_consumidor` > 0 AND `lc_uc`.`id_consumidor` IS NOT NULL) " & vbCrLf
			End If
			'lstrSql = lstrSql & "   AND `lc_uc`.`id` = '1082'"

			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, " UNIDADES CONSUMIDORAS ")
				For Each row As DataRow In dt.Rows

					iOSCod = CInt(IIf(IsDBNull(row.Item("sequencia_os")), "0", row.Item("sequencia_os")))

					llngLC_Id_consumidor = CInt(IIf(IsDBNull(row.Item("id_consumidor")), "0", row.Item("id_consumidor")))
					llngLC_Id_consumidor = BuscaConsReg(llngLC_Id_consumidor)

					llngLC_Unid_id_fatwin = CInt(IIf(IsDBNull(row.Item("unid_id_fatwin")), "0", row.Item("unid_id_fatwin")))
					If llngLC_Id_consumidor > 0 And llngLC_Unid_id_fatwin > 0 Then

						llngLC_Id = CInt(IIf(IsDBNull(row.Item("id")), "0", row.Item("id")))
						llngLC_Rota = CInt(IIf(IsDBNull(row.Item("rota")), "0", row.Item("rota")))
						llngLC_Sequencia = CInt(IIf(IsDBNull(row.Item("sequencia")), "0", row.Item("sequencia")))
						llngLC_Id_banco = CInt(IIf(IsDBNull(row.Item("id_banco")), "0", row.Item("id_banco")))
						llngLC_Id_endereco = CInt(IIf(IsDBNull(row.Item("id_endereco")), "0", row.Item("id_endereco")))
						llngLC_Id_cidade = CInt(IIf(IsDBNull(row.Item("id_cidade")), "0", row.Item("id_cidade")))
						llngLC_Id_endereco_entrega = CInt(IIf(IsDBNull(row.Item("id_endereco_entrega")), "0", row.Item("id_endereco_entrega")))
						llngLC_Id_uc_agrupamento = CInt(IIf(IsDBNull(row.Item("id_uc_agrupamento")), "0", row.Item("id_uc_agrupamento")))
						llngLC_Aparelho_saude = CInt(IIf(IsDBNull(row.Item("aparelho_saude")), "0", row.Item("aparelho_saude")))
						llngLC_Pagamento_governo = CInt(IIf(IsDBNull(row.Item("pagamento_governo")), "0", row.Item("pagamento_governo")))
						llngLC_Vip_comercial = CInt(IIf(IsDBNull(row.Item("vip_comercial")), "0", row.Item("vip_comercial")))
						llngLC_Vip_tecnico = CInt(IIf(IsDBNull(row.Item("vip_tecnico")), "0", row.Item("vip_tecnico")))
						llngLC_Qh = CInt(IIf(IsDBNull(row.Item("qh")), "0", row.Item("qh")))
						llngLC_Vencimento_prox_mes = CInt(IIf(IsDBNull(row.Item("vencimento_prox_mes")), "0", row.Item("vencimento_prox_mes")))
						llngLC_Pagamento_duvidoso = CInt(IIf(IsDBNull(row.Item("pagamento_duvidoso")), "0", row.Item("pagamento_duvidoso")))
						llngLC_Isento_iluminacao = CInt(IIf(IsDBNull(row.Item("isento_iluminacao")), "0", row.Item("isento_iluminacao")))
						llngLC_Aliquota_diferente = CInt(IIf(IsDBNull(row.Item("aliquota_diferente")), "0", row.Item("aliquota_diferente")))
						llngLC_Agrupa_id_uc = CInt(IIf(IsDBNull(row.Item("agrupa_id_uc")), "0", row.Item("agrupa_id_uc")))
						llngLC_Agrupa_rota_uc = CInt(IIf(IsDBNull(row.Item("agrupa_rota_uc")), "0", row.Item("agrupa_rota_uc")))
						llngLC_Agrupa_sequencia_uc = CInt(IIf(IsDBNull(row.Item("agrupa_sequencia_uc")), "0", row.Item("agrupa_sequencia_uc")))
						lstrLC_Zlc = Trim(IIf(IsDBNull(row.Item("zlc")), "", row.Item("zlc")))
						lstrLC_Descricao_banco = Trim(IIf(IsDBNull(row.Item("descricao_banco")), "", row.Item("descricao_banco")))
						lstrLC_Agencia = VB.Left(Str_Retira(Trim(IIf(IsDBNull(row.Item("agencia")), "", row.Item("agencia"))), "-./"), 4)
						lstrLC_Conta_corrente = VB.Left(Trim(IIf(IsDBNull(row.Item("conta_corrente")), "", row.Item("conta_corrente"))), 12)
						lstrLC_Grupo = Trim(IIf(IsDBNull(row.Item("grupo")), "", row.Item("grupo")))
						llngLC_Id_classe = CStr(CInt(IIf(IsDBNull(row.Item("id_classe")), "0", row.Item("id_classe"))))
						lstrLC_Classe = Trim(IIf(IsDBNull(row.Item("descricao_classe")), "", row.Item("descricao_classe")))
						lstrLC_Sub_classe = Trim(IIf(IsDBNull(row.Item("descricao_classe_tar")), "", row.Item("descricao_classe_tar")))
						lstrLC_Nome = UCase(Trim(IIf(IsDBNull(row.Item("nome")), "", row.Item("nome"))))
						lstrLC_Cpf_cnpj = Str_Retira(Trim(IIf(IsDBNull(row.Item("cpf_cnpj")), "", row.Item("cpf_cnpj"))), ".-/")
						lstrLC_Ie = Trim(IIf(IsDBNull(row.Item("ie")), "", row.Item("ie")))
						lstrLC_Rg = Trim(IIf(IsDBNull(row.Item("rg")), "", row.Item("rg")))
						lstrLC_Situacao = Trim(IIf(IsDBNull(row.Item("situacao")), "", row.Item("situacao")))
						lstrLC_Tipo = Trim(IIf(IsDBNull(row.Item("tipo")), "", row.Item("tipo")))
						lstrLC_Endereco = Trim(IIf(IsDBNull(row.Item("endereco")), "", row.Item("endereco")))
						lstrLC_Cidade = Trim(IIf(IsDBNull(row.Item("cidade")), "", row.Item("cidade")))
						lstrLC_Cep = Str_Retira(Trim(IIf(IsDBNull(row.Item("cep")), "", row.Item("cep"))), ".-/")
						lstrLC_Numero = Trim(IIf(IsDBNull(row.Item("numero")), "", row.Item("numero")))
						lstrLC_Bairro = Trim(IIf(IsDBNull(row.Item("bairro")), "", row.Item("bairro")))
						lstrLC_Distrito = Trim(IIf(IsDBNull(row.Item("distrito")), "", row.Item("distrito")))
						lstrLC_Uf = Trim(IIf(IsDBNull(row.Item("uf")), "", row.Item("uf")))
						lstrLC_Complemento = Trim(IIf(IsDBNull(row.Item("complemento")), "", row.Item("complemento")))
						lstrLC_Quadra = Trim(IIf(IsDBNull(row.Item("quadra")), "", row.Item("quadra")))
						lstrLC_Lote = Trim(IIf(IsDBNull(row.Item("lote")), "", row.Item("lote")))
						lstrLC_Endereco_entrega = Trim(IIf(IsDBNull(row.Item("endereco_entrega")), "", row.Item("endereco_entrega")))
						lstrLC_Cidade_entrega = Trim(IIf(IsDBNull(row.Item("cidade_entrega")), "", row.Item("cidade_entrega")))
						lstrLC_Cep_entrega = Str_Retira(Trim(IIf(IsDBNull(row.Item("cep_entrega")), "", row.Item("cep_entrega"))), ".-/")
						lstrLC_Numero_entrega = Trim(IIf(IsDBNull(row.Item("numero_entrega")), "", row.Item("numero_entrega")))
						lstrLC_Bairro_entrega = Trim(IIf(IsDBNull(row.Item("bairro_entrega")), "", row.Item("bairro_entrega")))
						lstrLC_Distrito_entrega = Trim(IIf(IsDBNull(row.Item("distrito_entrega")), "", row.Item("distrito_entrega")))
						lstrLC_Uf_entrega = Trim(IIf(IsDBNull(row.Item("uf_entrega")), "", row.Item("uf_entrega")))
						lstrLC_Complemento_entrega = Trim(IIf(IsDBNull(row.Item("complemento_entrega")), "", row.Item("complemento_entrega")))
						lstrLC_Quadra_entrega = Trim(IIf(IsDBNull(row.Item("quadra_entrega")), "", row.Item("quadra_entrega")))
						lstrLC_Lote_entrega = Trim(IIf(IsDBNull(row.Item("lote_entrega")), "", row.Item("lote_entrega")))
						lstrLC_Conjunto = Trim(IIf(IsDBNull(row.Item("conjunto")), "", row.Item("conjunto")))
						lstrLC_Fase_ligacao = Trim(IIf(IsDBNull(row.Item("fase_ligacao")), "ABCN", row.Item("fase_ligacao")))
						lstrLC_Tipo_fase = Trim(IIf(IsDBNull(row.Item("tipo_fase")), "T", row.Item("tipo_fase")))
						lstrLC_Tensao = Trim(IIf(IsDBNull(row.Item("tensao")), "380", row.Item("tensao")))
						lstrLC_Regiao = Trim(IIf(IsDBNull(row.Item("regiao")), "U", row.Item("regiao")))
						lstrLC_Dia_vencimento = VB6.Format(Trim(IIf(IsDBNull(row.Item("dia_vencimento")), "0", row.Item("dia_vencimento"))), "00")
						lstrLC_Id_agrupamento = CStr(Val(Trim(IIf(IsDBNull(row.Item("id_agrupamento")), "1", row.Item("id_agrupamento")))))
						lstrLC_Telefone = Trim(IIf(IsDBNull(row.Item("telefone")), "", row.Item("telefone")))
						lstrLC_Fax = Trim(IIf(IsDBNull(row.Item("fax")), "", row.Item("fax")))
						lstrLC_Celular = Trim(IIf(IsDBNull(row.Item("celular")), "", row.Item("celular")))
						lstrLC_Historico = Trim(IIf(IsDBNull(row.Item("historico")), "", row.Item("historico")))
						lstrLC_Cnae = VB.Left(Str_Retira(Trim(IIf(IsDBNull(row.Item("codigo_cnae")), "", row.Item("codigo_cnae"))), "./- "), 4)
						lstrLC_Agrupa_nome_uc = Trim(IIf(IsDBNull(row.Item("agrupa_nome_uc")), "", row.Item("agrupa_nome_uc")))
						lstrLC_Lacre = Trim(IIf(IsDBNull(row.Item("lacre")), "", row.Item("lacre")))
						ldteLC_Data_cadastro = CDate(IIf(IsDBNull(row.Item("data_cadastro")), "1900-01-01", row.Item("data_cadastro")))
						ldteLC_Data_ligacao = CDate(IIf(IsDBNull(row.Item("data_ligacao")), "1900-01-01", row.Item("data_ligacao")))
						ldteLC_Data_contrato_demanda = CDate(IIf(IsDBNull(row.Item("data_contrato_demanda")), "1900-01-01", row.Item("data_contrato_demanda")))
						ldteLC_Data_religacao = CDate(IIf(IsDBNull(row.Item("data_religacao")), "1900-01-01", row.Item("data_religacao")))
						ldteLC_Data_corte = CDate(IIf(IsDBNull(row.Item("data_corte")), "1900-01-01", row.Item("data_corte")))
						ldteLC_Data_desligamento = CDate(IIf(IsDBNull(row.Item("data_desligamento")), "1900-01-01", row.Item("data_desligamento")))
						ldblLC_Id_ramal = CDbl(IIf(IsDBNull(row.Item("id_ramal")), "0", row.Item("id_ramal")))
						lstrLC_Tipo_ligacao = BuscaTipoLigacaoRamal(ldblLC_Id_ramal)
						ldblLC_Id_medidor = CDbl(IIf(IsDBNull(row.Item("id_medidor")), "0", row.Item("id_medidor")))
						lStrLC_Numero_medidor = IIf(IsDBNull(row.Item("numero_medidor")), "0", row.Item("numero_medidor"))
						ldblLC_Demanda_contratada = CDbl(IIf(IsDBNull(row.Item("demanda_contratada")), "0", row.Item("demanda_contratada")))
						ldblLC_Demanda_contratada_ponta = CDbl(IIf(IsDBNull(row.Item("demanda_contratada_ponta")), "0", row.Item("demanda_contratada_ponta")))
						ldblLC_Demanda_contratada_fp = CDbl(IIf(IsDBNull(row.Item("demanda_contratada_fp")), "0", row.Item("demanda_contratada_fp")))
						ldblLC_Perdas = CDbl(IIf(IsDBNull(row.Item("perdas")), "0", row.Item("perdas")))
						ldblLC_Ultima_leitura_kwh = CDbl(IIf(IsDBNull(row.Item("ultima_leitura_kwh")), "0", row.Item("ultima_leitura_kwh")))
						ldblLC_Conjuge_Fatura = CDbl(IIf(IsDBNull(row.Item("conjuge_fatura")), "0", row.Item("conjuge_fatura")))
						lstrLC_Latitude = CDbl(IIf(IsDBNull(row.Item("latitude")), "0", row.Item("latitude")))
						lstrLC_Longitude = CDbl(IIf(IsDBNull(row.Item("longitude")), "0", row.Item("longitude")))
						lstrLC_numero_caixa = Trim(IIf(IsDBNull(row.Item("numero_caixa")), "", row.Item("numero_caixa")))

						'
						lstrSql = ""
						lstrSql = lstrSql & "SELECT * "
						lstrSql = lstrSql & "  FROM `uc_faturamento` "
						lstrSql = lstrSql & " WHERE `id` = " & llngLC_Unid_id_fatwin
						objAtualizaBancoDados.QUERY_NAME = lstrSql
						dt1 = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

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

								lstrFW_Zona = CStr(CInt(IIf(IsDBNull(row1.Item("zona")), "", row1.Item("zona"))))
								lstrFW_Livro = CStr(CInt(IIf(IsDBNull(row1.Item("livro")), "", row1.Item("livro"))))
								lstrFW_Conta = CStr(CInt(IIf(IsDBNull(row1.Item("conta")), "", row1.Item("conta"))))
								llngFW_Cons_id = CInt(IIf(IsDBNull(row1.Item("cons_id")), "0", row1.Item("cons_id")))
								llngFW_Pagto_duvidoso = CInt(IIf(IsDBNull(row1.Item("pagto_duvidoso")), "0", row1.Item("pagto_duvidoso")))
								lstrFW_Banco = Trim(IIf(IsDBNull(row1.Item("banco")), "", row1.Item("banco")))
								lstrFW_Agenc = Trim(IIf(IsDBNull(row1.Item("agenc")), "", row1.Item("agenc")))
								lstrFW_Cc = Trim(IIf(IsDBNull(row1.Item("cc")), "", row1.Item("cc")))
								lstrFW_Grupo = Trim(IIf(IsDBNull(row1.Item("grupo")), "", row1.Item("grupo")))
								lstrFW_Ta = Trim(IIf(IsDBNull(row1.Item("ta")), "", row1.Item("ta")))
								lstrFW_Clas = Trim(IIf(IsDBNull(row1.Item("clas")), "", row1.Item("clas")))
								lstrFW_Bxrenda = Trim(IIf(IsDBNull(row1.Item("bxrenda")), "", row1.Item("bxrenda")))
								lstrFW_Fase = Trim(IIf(IsDBNull(row1.Item("fase")), "", row1.Item("fase")))
								lstrFW_Situacao = Trim(IIf(IsDBNull(row1.Item("situacao")), "L", row1.Item("situacao")))
								lstrFW_Ilum = Trim(IIf(IsDBNull(row1.Item("ilum")), "", row1.Item("ilum")))
								lstrFW_Tensao = Trim(IIf(IsDBNull(row1.Item("tensao")), "", row1.Item("tensao")))
								lstrFW_Faselig = Trim(IIf(IsDBNull(row1.Item("faselig")), "", row1.Item("faselig")))
								lstrFW_Codend = Trim(IIf(IsDBNull(row1.Item("codend")), "", row1.Item("codend")))
								lstrFW_Uf = Trim(IIf(IsDBNull(row1.Item("uf")), "", row1.Item("uf")))
								lstrFW_Cep = Trim(IIf(IsDBNull(row1.Item("cep")), "", row1.Item("cep")))
								lstrFW_Cidade = Trim(IIf(IsDBNull(row1.Item("cidade")), "", row1.Item("cidade")))
								lstrFW_Distrito = Trim(IIf(IsDBNull(row1.Item("distrito")), "", row1.Item("distrito")))
								lstrFW_Bairro = Trim(IIf(IsDBNull(row1.Item("bairro")), "", row1.Item("bairro")))
								lstrFW_End = Trim(IIf(IsDBNull(row1.Item("end")), "", row1.Item("end")))
								lstrFW_Numero = Trim(IIf(IsDBNull(row1.Item("numero")), "", row1.Item("numero")))
								lstrFW_Complement = Trim(IIf(IsDBNull(row1.Item("complement")), "", row1.Item("complement")))
								lstrFW_Quadra = Trim(IIf(IsDBNull(row1.Item("quadra")), "", row1.Item("quadra")))
								lstrFW_Lote = Trim(IIf(IsDBNull(row1.Item("lote")), "", row1.Item("lote")))
								llngFW_Entrega = CInt(IIf(IsDBNull(row1.Item("entrega")), "0", row1.Item("entrega")))
								lstrFW_Codend_entrega = Trim(IIf(IsDBNull(row1.Item("codend_entrega")), "", row1.Item("codend_entrega")))
								lstrFW_Uf_entrega = Trim(IIf(IsDBNull(row1.Item("uf_entrega")), "", row1.Item("uf_entrega")))
								lstrFW_Cep_entrega = Trim(IIf(IsDBNull(row1.Item("cep_entrega")), "", row1.Item("cep_entrega")))
								lstrFW_Cid_entrega = Trim(IIf(IsDBNull(row1.Item("cid_entrega")), "", row1.Item("cid_entrega")))
								lstrFW_Dis_entrega = Trim(IIf(IsDBNull(row1.Item("dis_entrega")), "", row1.Item("dis_entrega")))
								lstrFW_Bai_entrega = Trim(IIf(IsDBNull(row1.Item("bai_entrega")), "", row1.Item("bai_entrega")))
								lstrFW_End_entrega = Trim(IIf(IsDBNull(row1.Item("end_entrega")), "", row1.Item("end_entrega")))
								lstrFW_Num_entrega = Trim(IIf(IsDBNull(row1.Item("num_entrega")), "", row1.Item("num_entrega")))
								lstrFW_Compl_entrega = Trim(IIf(IsDBNull(row1.Item("compl_entrega")), "", row1.Item("compl_entrega")))
								lstrFW_Qua_entrega = Trim(IIf(IsDBNull(row1.Item("qua_entrega")), "", row1.Item("qua_entrega")))
								lstrFW_Lot_entrega = Trim(IIf(IsDBNull(row1.Item("lot_entrega")), "", row1.Item("lot_entrega")))
								lstrFW_Diavenc = Trim(IIf(IsDBNull(row1.Item("diavenc")), "", row1.Item("diavenc")))
								lstrFW_Cpf_cnpj = Trim(IIf(IsDBNull(row1.Item("cpf_cnpj")), "", row1.Item("cpf_cnpj")))
								lstrFW_Rg = Trim(IIf(IsDBNull(row1.Item("rg")), "", row1.Item("rg")))
								lstrFW_Insc = Trim(IIf(IsDBNull(row1.Item("insc")), "", row1.Item("insc")))
								lstrFW_Pessoa = Trim(IIf(IsDBNull(row1.Item("pessoa")), "", row1.Item("pessoa")))
								lstrFW_Nome = Trim(IIf(IsDBNull(row1.Item("nome")), "", row1.Item("nome")))
								lstrFW_Circuito = Trim(IIf(IsDBNull(row1.Item("circuito")), "", row1.Item("circuito")))
								lstrFW_Lacre = Trim(IIf(IsDBNull(row1.Item("lacre")), "", row1.Item("lacre")))
								lstrFW_Ap_saude = Trim(IIf(IsDBNull(row1.Item("ap_saude")), "", row1.Item("ap_saude")))
								lstrFW_Tp_ligacao = Trim(IIf(IsDBNull(row1.Item("tp_ligacao")), "", row1.Item("tp_ligacao")))
								lstrFW_Regiao = Trim(IIf(IsDBNull(row1.Item("regiao")), "", row1.Item("regiao")))
								lstrFW_Cnae = Trim(IIf(IsDBNull(row1.Item("cnae")), "", row1.Item("cnae")))
								ldteFW_Data_ligacao = CDate(IIf(IsDBNull(row1.Item("data_ligacao")), "1900-01-01", row1.Item("data_ligacao")))
								ldteFW_Data_religacao = CDate(IIf(IsDBNull(row1.Item("data_religacao")), "1900-01-01", row1.Item("data_religacao")))
								ldteFW_Data_desligamento = CDate(IIf(IsDBNull(row1.Item("data_desligamento")), "1900-01-01", row1.Item("data_desligamento")))
								ldteFW_Data_entrada = CDate(IIf(IsDBNull(row1.Item("data_entrada")), "1900-01-01", row1.Item("data_entrada")))
								ldteFW_Data_corte = CDate(IIf(IsDBNull(row1.Item("data_corte")), "1900-01-01", row1.Item("data_corte")))
								ldteFW_Data_contrato_demanda = CDate(IIf(IsDBNull(row1.Item("data_contrato_demanda")), "1900-01-01", row1.Item("data_contrato_demanda")))
								ldblFW_Ultleit = CDbl(IIf(IsDBNull(row1.Item("ultleit")), "0", row1.Item("ultleit")))
								ldblFW_UltleitDesligado = CDbl(IIf(IsDBNull(row1.Item("ultleit_desligamento")), "0", row1.Item("ultleit_desligamento")))
								ldblFW_Final = CDbl(IIf(IsDBNull(row1.Item("final")), "0", row1.Item("final")))
								ldblFW_Dem_contr_fp = CDbl(IIf(IsDBNull(row1.Item("dem_contr_fp")), "0", row1.Item("dem_contr_fp")))
								ldblFW_Dem_contr_ponta = CDbl(IIf(IsDBNull(row1.Item("dem_contr_ponta")), "0", row1.Item("dem_contr_ponta")))
								ldblFW_Demanda_contr = CDbl(IIf(IsDBNull(row1.Item("demanda_contr")), "0", row1.Item("demanda_contr")))
								llngFW_Agrupa = CInt(IIf(IsDBNull(row1.Item("agrupa")), "0", row1.Item("agrupa")))
								ldblFW_Id_medidor = CDbl(IIf(IsDBNull(row1.Item("idmedidor")), "0", row1.Item("idmedidor")))
								lstrFW_Numero_medidor = IIf(IsDBNull(row1.Item("num")), "0", row1.Item("num"))
								ldblFW_Conjuge_Fatura = CDbl(IIf(IsDBNull(row1.Item("conjuge_fatura")), "0", row1.Item("conjuge_fatura")))
								lstrFW_Latitude = CDbl(IIf(IsDBNull(row.Item("latitude")), "0", row.Item("latitude")))
								lstrFW_Longitude = CDbl(IIf(IsDBNull(row.Item("longitude")), "0", row.Item("longitude")))
								lstrFW_Caixa_Numero = Trim(IIf(IsDBNull(row1.Item("numero_caixa")), "", row1.Item("numero_caixa")))

								lstrSql = ""
								If Me.chkLogradouro.CheckState = System.Windows.Forms.CheckState.Unchecked Then
									If llngFW_Cons_id <> llngLC_Id_consumidor Then
										If llngLC_Id_consumidor > 0 Then
											lstrSql = lstrSql & "cons_id = " & llngLC_Id_consumidor & ", "
										End If
									End If
									If llngFW_Pagto_duvidoso <> llngLC_Pagamento_duvidoso Then
										If llngLC_Pagamento_duvidoso > 0 Then
											lstrSql = lstrSql & "pagto_duvidoso = " & llngLC_Pagamento_duvidoso & ", "
										End If
									End If


									llngLc_Id_banco_fatwin = BuscaIdBancoLinkCenter(llngLC_Id_banco)
									If lstrFW_Banco <> llngLc_Id_banco_fatwin Then
										If llngLc_Id_banco_fatwin > 0 Then
											lstrSql = lstrSql & "banco    = '" & llngLc_Id_banco_fatwin & "', "
										End If
									End If


									If lstrFW_Agenc <> lstrLC_Agencia Then
										lstrSql = lstrSql & "agenc    = '" & lstrLC_Agencia & "', "
									End If

									If lstrFW_Cc <> lstrLC_Conta_corrente Then
										lstrSql = lstrSql & "cc       = '" & lstrLC_Conta_corrente & "', "
									End If

									If lstrLC_Grupo = "B" Then
										If lstrFW_Grupo <> Strings.Left(Strings.Trim(lstrLC_Sub_classe), 2) Then
											If Strings.Left(Strings.Trim(lstrLC_Sub_classe), 2) <> "" Then
												lstrSql = lstrSql & "grupo    = '" & Strings.Left(Strings.Trim(lstrLC_Sub_classe), 2) & "', "
												Select Case Strings.Left(Strings.Trim(lstrLC_Sub_classe), 2)
													Case Is = "B3"
														If Strings.InStr(1, lstrLC_Sub_classe, "municipal") > 0 Then
															lstrSql = lstrSql & "ta       = '01" & Strings.Left(Strings.Trim(lstrLC_Sub_classe), 2) & "34', "
														ElseIf Strings.InStr(1, lstrLC_Sub_classe, "estadual") > 0 Then
															lstrSql = lstrSql & "ta       = '01" & Strings.Left(Strings.Trim(lstrLC_Sub_classe), 2) & "35', "
														ElseIf Strings.InStr(1, lstrLC_Sub_classe, "federal") > 0 Then
															lstrSql = lstrSql & "ta       = '01" & Strings.Left(Strings.Trim(lstrLC_Sub_classe), 2) & "36', "
														ElseIf Strings.InStr(1, lstrLC_Sub_classe, "Tração elétrica") > 0 Then
															lstrSql = lstrSql & "ta       = '01" & Strings.Left(Strings.Trim(lstrLC_Sub_classe), 2) & "37', "
														ElseIf Strings.InStr(1, lstrLC_Sub_classe, "Água, esgoto e saneamento") > 0 Then
															lstrSql = lstrSql & "ta       = '01" & Strings.Left(Strings.Trim(lstrLC_Sub_classe), 2) & "38', "
														Else
															lstrSql = lstrSql & "ta       = '01" & Strings.Left(Strings.Trim(lstrLC_Sub_classe), 2) & "00', "
														End If
													Case Else
														lstrSql = lstrSql & "ta       = '01" & Strings.Left(Strings.Trim(lstrLC_Sub_classe), 2) & "00', "
												End Select
											End If
										End If
									End If


									llngLC_Id_classe = CStr(BuscaClasse(CInt(llngLC_Id_classe), lstrLC_Classe, lstrLC_Sub_classe))
									If lstrFW_Clas <> llngLC_Id_classe Then
										If CDbl(llngLC_Id_classe) > 0 Then
											lstrSql = lstrSql & "clas = '" & BuscaClasse(CInt(llngLC_Id_classe), lstrLC_Classe, lstrLC_Sub_classe) & "', "
										End If
									End If
									If lstrFW_Fase <> lstrLC_Tipo_fase Then
										If lstrLC_Tipo_fase <> "" Then
											lstrSql = lstrSql & "fase     = '" & lstrLC_Tipo_fase & "', "
										End If
									End If
									If lstrFW_Situacao <> lstrLC_Situacao Then
										If lstrLC_Situacao <> "" Then
											lstrSql = lstrSql & "situacao = '" & lstrLC_Situacao & "', "
											If lstrLC_Situacao = "D" Or lstrLC_Situacao = "I" Or lstrLC_Situacao = "C" Then
												If ldblLC_Id_ramal = 0 Then
													'Fazer limpar o ramal da U.C.
												End If
											End If
										End If
									End If


									If lstrFW_Ilum <> IIf(llngLC_Isento_iluminacao = 0, "S", "N") Then
										lstrSql = lstrSql & "ilum     = '" & IIf(llngLC_Isento_iluminacao = 0, "S", "N") & "', "
									End If


									If lstrFW_Tensao <> lstrLC_Tensao Then
										If lstrLC_Tensao <> "" Then
											lstrSql = lstrSql & "tensao   = '" & lstrLC_Tensao & "', "
										End If
									End If
									If lstrFW_Faselig <> lstrLC_Fase_ligacao Then
										If lstrLC_Fase_ligacao <> "" Then
											lstrSql = lstrSql & "faselig  = '" & lstrLC_Fase_ligacao & "', "
										End If
									End If
								End If

								lstrLC_Id_endereco_fatwin = BuscaAtualizaCodEndereco(llngLC_Id_endereco)
								If lstrFW_Codend <> lstrLC_Id_endereco_fatwin Then
									If lstrLC_Id_endereco_fatwin <> "" Then
										lstrSql = lstrSql & "codend     = '" & BuscaAtualizaCodEndereco(llngLC_Id_endereco) & "', "
									End If
								End If

								If lstrFW_Uf <> lstrLC_Uf Then
									lstrSql = lstrSql & "uf         = '" & Replace(lstrLC_Uf, "'", "\'") & "', "
								End If

								If lstrFW_Cep <> lstrLC_Cep Then
									lstrSql = lstrSql & "cep        = '" & lstrLC_Cep & "', "
								End If

								If lstrFW_Cidade <> lstrLC_Cidade Then
									lstrSql = lstrSql & "cidade     = '" & Replace(lstrLC_Cidade, "'", "\'") & "', "
								End If

								If lstrFW_Distrito <> lstrLC_Distrito Then
									lstrSql = lstrSql & "distrito   = '" & Replace(lstrLC_Distrito, "'", "\'") & "', "
								End If

								If lstrFW_Bairro <> lstrLC_Bairro Then
									lstrSql = lstrSql & "bairro     = '" & Replace(lstrLC_Bairro, "'", "\'") & "', "
								End If

								If lstrFW_End <> lstrLC_Endereco Then
									lstrSql = lstrSql & "end        = '" & Replace(lstrLC_Endereco, "'", "\'") & "', "
								End If

								If lstrFW_Numero <> lstrLC_Numero Then
									lstrSql = lstrSql & "numero     = '" & Replace(lstrLC_Numero, "'", "\'") & "', "
								End If

								If lstrFW_Complement <> lstrLC_Complemento Then
									lstrSql = lstrSql & "complement = '" & Replace(lstrLC_Complemento, "'", "\'") & "', "
								End If

								If lstrFW_Quadra <> lstrLC_Quadra Then
									lstrSql = lstrSql & "quadra = '" & Replace(lstrLC_Quadra, "'", "\'") & "', "
								End If

								If lstrFW_Lote <> lstrLC_Lote Then
									lstrSql = lstrSql & "lote = '" & Replace(lstrLC_Lote, "'", "\'") & "', "
								End If

								'lintLC_Entrega = 0

								'If llngLC_Id_endereco_entrega > 0 Then
								'	If (lstrLC_Cidade_entrega & lstrLC_Bairro_entrega & lstrLC_Endereco_entrega & lstrLC_Numero_entrega & lstrLC_Complemento_entrega) <> (lstrLC_Cidade & lstrLC_Bairro & lstrLC_Endereco & lstrLC_Numero & lstrLC_Complemento) Then
								'		lintLC_Entrega = 1
								'	Else
								'		lintLC_Entrega = 0
								'	End If
								'Else
								'	lintLC_Entrega = 0
								'End If

								'If CInt(IIf(IsNumeric(lstrLC_Id_agrupamento), lstrLC_Id_agrupamento, 0)) = 2 Then
								'	lintLC_Entrega = 1 'Agrupamento CORREIOS sempre tem que ser = 2 em todos os clientes
								'End If

								'If llngFW_Entrega <> lintLC_Entrega Then
								'	lstrSql = lstrSql & "entrega = " & lintLC_Entrega & ", "
								'End If

								lstrLC_Id_endereco_entrega_fatwin = BuscaAtualizaCodEndereco(llngLC_Id_endereco_entrega)

								If lstrFW_Codend_entrega <> lstrLC_Id_endereco_entrega_fatwin Then
									lstrSql = lstrSql & "codend_entrega = '" & BuscaAtualizaCodEndereco(llngLC_Id_endereco_entrega) & "', "
								End If

								If lstrFW_Uf_entrega <> lstrLC_Uf_entrega Then
									lstrSql = lstrSql & "uf_entrega     = '" & Replace(lstrLC_Uf_entrega, "'", "\'") & "', "
								End If

								If lstrFW_Cep_entrega <> lstrLC_Cep_entrega Then
									lstrSql = lstrSql & "cep_entrega    = '" & lstrLC_Cep_entrega & "', "
								End If

								If lstrFW_Cid_entrega <> lstrLC_Cidade_entrega Then
									lstrSql = lstrSql & "cid_entrega    = '" & Replace(lstrLC_Cidade_entrega, "'", "\'") & "', "
								End If

								If lstrFW_Dis_entrega <> lstrLC_Distrito_entrega Then
									lstrSql = lstrSql & "dis_entrega    = '" & Replace(lstrLC_Distrito_entrega, "'", "\'") & "', "
								End If

								If lstrFW_Bai_entrega <> lstrLC_Bairro_entrega Then
									lstrSql = lstrSql & "bai_entrega    = '" & Replace(lstrLC_Bairro_entrega, "'", "\'") & "', "
								End If

								If lstrFW_End_entrega <> lstrLC_Endereco_entrega Then
									lstrSql = lstrSql & "end_entrega    = '" & Replace(lstrLC_Endereco_entrega, "'", "\'") & "', "
								End If

								If lstrFW_Num_entrega <> lstrLC_Numero_entrega Then
									lstrSql = lstrSql & "num_entrega    = '" & Replace(lstrLC_Numero_entrega, "'", "\'") & "', "
								End If

								If lstrFW_Compl_entrega <> lstrLC_Complemento_entrega Then
									lstrSql = lstrSql & "compl_entrega  = '" & Replace(lstrLC_Complemento_entrega, "'", "\'") & "', "
								End If

								If lstrFW_Qua_entrega <> lstrLC_Quadra_entrega Then
									lstrSql = lstrSql & "qua_entrega = '" & Replace(lstrLC_Quadra_entrega, "'", "\'") & "', "
								End If

								If lstrFW_Lot_entrega <> lstrLC_Lote_entrega Then
									lstrSql = lstrSql & "lot_entrega = '" & Replace(lstrLC_Lote_entrega, "'", "\'") & "', "
								End If

								If Me.chkLogradouro.CheckState = System.Windows.Forms.CheckState.Unchecked Then
									If lstrFW_Diavenc <> lstrLC_Dia_vencimento Then
										If VB6.Format(lstrLC_Dia_vencimento) <> "00" And VB6.Format(lstrLC_Dia_vencimento) <> "" Then
											lstrSql = lstrSql & "diavenc        = '" & VB6.Format(lstrLC_Dia_vencimento, "00") & "', "
										End If
									End If

									If lstrFW_Cpf_cnpj <> lstrLC_Cpf_cnpj Then
										If lstrLC_Cpf_cnpj <> "" Then
											lstrSql = lstrSql & "cpf_cnpj       = '" & lstrLC_Cpf_cnpj & "', "
										End If
									End If

									If lstrFW_Rg <> lstrLC_Rg Then
										If lstrLC_Rg <> "" Then
											lstrSql = lstrSql & "rg             = '" & lstrLC_Rg & "', "
										End If
									End If

									If lstrFW_Insc <> lstrLC_Ie Then
										If lstrLC_Ie <> "" Then
											lstrSql = lstrSql & "insc           = '" & lstrLC_Ie & "', "
										End If
									End If

									If lstrFW_Pessoa <> lstrLC_Tipo Then
										If lstrLC_Tipo <> "" Then
											lstrSql = lstrSql & "pessoa         = '" & lstrLC_Tipo & "', "
										End If
									End If

									If lstrFW_Nome <> lstrLC_Nome Then
										If lstrLC_Nome <> "" Then
											lstrSql = lstrSql & "nome           = '" & Replace(lstrLC_Nome, "'", "\'") & "', "
										End If
									End If

									If lstrFW_Circuito <> lstrLC_Conjunto Then
										If lstrLC_Conjunto <> "" Then
											lstrSql = lstrSql & "circuito       = '" & lstrLC_Conjunto & "', "
										End If
									End If

									If lstrFW_Lacre <> lstrLC_Lacre Then
										If lstrLC_Lacre <> "" Then
											lstrSql = lstrSql & "lacre          = '" & lstrLC_Lacre & "', "
										End If
									End If

									If lstrFW_Ap_saude <> IIf(llngLC_Aparelho_saude = 1, "S", "N") Then
										lstrSql = lstrSql & "ap_saude       = '" & IIf(llngLC_Aparelho_saude = 1, "S", "N") & "', "
									End If

									If lstrFW_Tp_ligacao <> lstrLC_Tipo_ligacao Then
										If lstrLC_Tipo_ligacao <> "" Then
											lstrSql = lstrSql & "tp_ligacao     = '" & lstrLC_Tipo_ligacao & "', "
										End If
									End If

									If lstrFW_Regiao <> lstrLC_Regiao Then
										If lstrLC_Regiao <> "" Then
											lstrSql = lstrSql & "regiao         = '" & lstrLC_Regiao & "', "
										End If
									End If

									If lstrFW_Cnae <> lstrLC_Cnae Then
										If lstrLC_Cnae <> "" Then
											lstrSql = lstrSql & "cnae           = '" & lstrLC_Cnae & "', "
										End If
									End If

									If ldteFW_Data_ligacao <> ldteLC_Data_ligacao Then
										If ldteLC_Data_ligacao <> CDate("01/01/1900") Then
											lstrSql = lstrSql & "data_ligacao   = '" & VB6.Format(ldteLC_Data_ligacao, "yyyy-MM-dd") & "', "
										End If
									End If

									If ldteFW_Data_religacao <> ldteLC_Data_religacao Then
										If ldteLC_Data_religacao <> CDate("01/01/1900") Then
											lstrSql = lstrSql & "data_religacao = '" & VB6.Format(ldteLC_Data_religacao, "yyyy-MM-dd") & "', "
										End If
									End If

									If ldteFW_Data_desligamento <> ldteLC_Data_desligamento Then
										If ldteLC_Data_desligamento <> CDate("01/01/1900") Then
											lstrSql = lstrSql & "data_desligamento = '" & VB6.Format(ldteLC_Data_desligamento, "yyyy-MM-dd") & "', "
										End If
									End If

									If ldteFW_Data_entrada <> ldteLC_Data_ligacao Then
										If ldteLC_Data_ligacao <> CDate("01/01/1900") Then
											lstrSql = lstrSql & "data_entrada = '" & VB6.Format(ldteLC_Data_ligacao, "yyyy-MM-dd") & "', "
										End If
									End If

									If ldteFW_Data_corte <> ldteLC_Data_corte Then
										If ldteLC_Data_corte <> CDate("01/01/1900") Then
											lstrSql = lstrSql & "data_corte = '" & VB6.Format(ldteLC_Data_corte, "yyyy-MM-dd") & "', "
										End If
									End If

									If ldteFW_Data_contrato_demanda <> ldteLC_Data_contrato_demanda Then
										If ldteLC_Data_contrato_demanda <> CDate("01/01/1900") Then
											lstrSql = lstrSql & "data_contrato_demanda = '" & VB6.Format(ldteLC_Data_contrato_demanda, "yyyy-MM-dd") & "', "
										End If
									End If

									Dim aux = ldblLC_Ultima_leitura_kwh
									Dim aux2 = ldblLC_Ultima_leitant_kwh

									If iOSCod > 0 Then
										'If ldblLC_Ultima_leitura_kwh = 0 And Trim(Me.txtSequenciasOs.Text) <> "" Then
										'Call LetUltimaLeituraOsConclusao(CInt(Me.txtSequenciasOs.Text), ldblLC_Ultima_leitura_kwh, ldblLC_Ultima_leitant_kwh, ldblConstante)
										Call LetUltimaLeituraOsConclusao(CInt(iOSCod), ldblLC_Ultima_leitura_kwh, ldblLC_Ultima_leitant_kwh, ldblConstante)
									End If


									'Alterado por nader, a pedido do arlindo.. if's anteriores estão comentados.
									If ldblFW_Ultleit <> ldblLC_Ultima_leitant_kwh Then 'If ldblFW_Ultleit <> ldblLC_Ultima_leitura_kwh Then
										If ldblLC_Ultima_leitant_kwh > 0 Then 'If ldblLC_Ultima_leitura_kwh > 0 Then
											'If VB.Left(lstrLC_Grupo, 1) = "B" Then
											'If ldblFW_Ultleit < ldblLC_Ultima_leitant_kwh Then
											'ldblResiduo = (ldblLC_Ultima_leitant_kwh - ldblFW_Ultleit) * ldblConstante
											'lstrSql = lstrSql & "residuo_consumo = " & Replace(CStr(ldblResiduo), ",", ".") & ", "
											'End If
											'End If

											Call GravaMovimentoUltimaLeitura(lstrFW_Zona, lstrFW_Livro, lstrFW_Conta, llngLC_Unid_id_fatwin, llngFW_Cons_id, VB.Left(lstrLC_Grupo, 1), lstrLC_Situacao, ldblFW_Ultleit, ldblFW_UltleitDesligado, ldblLC_Ultima_leitura_kwh, ldblLC_Ultima_leitant_kwh, ldblResiduo, IIf(IsNumeric(Me.txtSequenciasOs.Text), Me.txtSequenciasOs.Text, 0))
											lstrSql = lstrSql & "ultleit = " & Replace(CStr(ldblLC_Ultima_leitura_kwh), ",", ".") & ", "

										Else
											If lstrLC_Situacao <> "L" And lstrLC_Situacao <> "C" Then
												lstrSql = lstrSql & "ultleit_desligamento = " & Replace(CStr(ldblLC_Ultima_leitura_kwh), ",", ".") & ", "
											Else
												lstrSql = lstrSql & "ultleit = " & Replace(CStr(ldblLC_Ultima_leitura_kwh), ",", ".") & ", "
											End If
										End If
									End If

								End If

								If ldblFW_Final <> ldblLC_Perdas Then
									If ldblLC_Perdas > 0 Then
										lstrSql = lstrSql & "final = " & Replace(CStr(ldblLC_Perdas), ",", ".") & ", "
									End If
								End If

								If ldblFW_Dem_contr_fp <> ldblLC_Demanda_contratada_fp Then
									If ldblLC_Demanda_contratada_fp > 0 Then
										lstrSql = lstrSql & "dem_contr_fp = " & Replace(CStr(ldblLC_Demanda_contratada_fp), ",", ".") & ", "
									End If
								End If

								If ldblFW_Dem_contr_ponta <> ldblLC_Demanda_contratada_ponta Then
									If ldblLC_Demanda_contratada_ponta > 0 Then
										lstrSql = lstrSql & "dem_contr_ponta = " & Replace(CStr(ldblLC_Demanda_contratada_ponta), ",", ".") & ", "
									End If
								End If

								If ldblFW_Demanda_contr <> ldblLC_Demanda_contratada_ponta Then
									If ldblLC_Demanda_contratada_ponta > 0 Then
										lstrSql = lstrSql & "demanda_contr = " & Replace(CStr(ldblLC_Demanda_contratada_ponta), ",", ".") & ", "
									End If
								End If

								llngLc_Id_agrupa_fatwin = BuscaIdAgrupaLinkCenter(lstrLC_Id_agrupamento)
								If llngFW_Agrupa <> llngLc_Id_agrupa_fatwin Then
									lstrSql = lstrSql & "agrupa = '" & llngLc_Id_agrupa_fatwin & "', "
								End If

								If ldblFW_Conjuge_Fatura <> ldblLC_Conjuge_Fatura Then
									If ldblLC_Conjuge_Fatura < 2 Then
										lstrSql = lstrSql & "conjuge_fatura = " & Replace(CStr(ldblLC_Conjuge_Fatura), ",", ".") & ", "
									End If
								End If

								If lstrFW_Numero_medidor <> lStrLC_Numero_medidor Then
									lstrSql = lstrSql & "num = '" & Replace(lStrLC_Numero_medidor, ",", ".") & "', "
								End If

								If lstrFW_Latitude <> lstrLC_Latitude Then
									lstrSql = lstrSql & "latitude = " & Replace(CStr(lstrLC_Latitude), ",", ".") & ", "
								End If

								If lstrFW_Longitude <> lstrLC_Longitude Then
									lstrSql = lstrSql & "longitude = " & Replace(CStr(lstrLC_Longitude), ",", ".") & ", "
								End If

								If lstrFW_Caixa_Numero <> lstrLC_numero_caixa Then
									'lstrSql = lstrSql & "numero_caixa = '" & lstrLC_numero_caixa & "',"
								End If

								lstrSql = Trim(lstrSql)

								If Trim(lstrSql) <> "" Then
									lstrSql = VB.Left(lstrSql, Len(lstrSql) - 1)
									Call InsereAuditoriaUnidadeConsumidora(llngLC_Unid_id_fatwin, VB.Left(lstrLC_Grupo, 1), "ORIGEM")

									objExecAtualizaBancoDados.QUERY_NAME = "UPDATE `uc_faturamento` SET " & lstrSql & " WHERE `id` = " & llngLC_Unid_id_fatwin
									repositorioExecAtualizaBancoDados.Consultar(objExecAtualizaBancoDados)

									Call CancelaConvenioTransfTitularidade(llngFW_Cons_id, llngLC_Id, llngLC_Unid_id_fatwin, VB6.Format(lstrFW_Zona, "000"), VB6.Format(lstrFW_Livro, "00"), VB6.Format(lstrFW_Conta, "000000"))
									Me.lblRegEtapa(5).Text = VB6.Format(CDbl(IIf(IsNumeric(Me.lblRegEtapa(5).Text), Me.lblRegEtapa(5).Text, 0)) + ldblRowAfec, "#0")
									Call InsereAuditoriaUnidadeConsumidora(llngLC_Unid_id_fatwin, VB.Left(lstrLC_Grupo, 1), "ALTERADO")
									If Me.chkLogradouro.CheckState = System.Windows.Forms.CheckState.Unchecked Then
										Call AtualizaTabelasTransfTitularidade(UCase(VB.Left(lstrLC_Grupo, 1)), llngFW_Cons_id, llngLC_Id_consumidor, llngLC_Unid_id_fatwin)
									End If
								End If

								'If Me.chkLogradouro.CheckState = System.Windows.Forms.CheckState.Unchecked Then
								'If ldblFW_Id_medidor > 0 Then
								'Call Etapa004(ldblFW_Id_medidor)
								'If ldblLC_Id_medidor > 0 And ldblFW_Id_medidor <> ldblLC_Id_medidor Then
								'Call Etapa004(ldblLC_Id_medidor)

								'ElseIf ldblLC_Id_medidor > 0 Then
								'Call Etapa004(ldblLC_Id_medidor)

							Next
						End If

					End If

					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1,  , " UNIDADES CONSUMIDORAS ")
				Next
			End If
		Catch ex As Exception

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

	End Sub

	Private Sub LetUltimaLeituraOsConclusao(ByVal adblSequenciaOs As Integer, ByRef leitura As Double, ByRef LeituraAnterior As Double, ByRef Constante As Double)

		Dim llngIdMedAt As Integer
		Dim llngIdMedAn As Integer
		Dim lstrSql As String
		Try

			lstrSql = "SELECT c.* FROM " & gstrBDCLinkCenter & ".os o, " & gstrBDCLinkCenter & ".os_conclusao c "
			lstrSql = lstrSql & " WHERE c.id = o.id AND c.id_sub = o.id_sub AND o.sequencia_os = " & adblSequenciaOs & " "
			objAtualizaBancoDados.QUERY_NAME = lstrSql
			dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

			If dt.Rows.Count > 0 Then
				For Each row As DataRow In dt.Rows
					llngIdMedAt = IIf(IsDBNull(row.Item("id_medidor")), 0, row.Item("id_medidor"))
					llngIdMedAn = IIf(IsDBNull(row.Item("id_medidor_anterior")), 0, row.Item("id_medidor_anterior"))
					leitura = IIf(IsDBNull(row.Item("medidor_leitura_kwh")), 0, row.Item("medidor_leitura_kwh"))
					LeituraAnterior = IIf(IsDBNull(row.Item("medidor_anterior_leitura_kwh")), 0, row.Item("medidor_anterior_leitura_kwh"))
					Constante = GetConstanteMedidor(llngIdMedAn)
				Next
			End If
		Catch ex As Exception
			Throw New Exception("Ocorreu o Erro: " & Err.Description & " - Rotina: LetUltimaLeituraOsConclusao" & "", ex)
			'MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: LetUltimaLeituraOsConclusao" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try

	End Sub

	Private Function GetConstanteMedidor(ByVal alngIdMedidor As Integer) As Double
		Dim ldblCon As Double
		Dim lstrSql As String
		Try
			lstrSql = "SELECT const_kwh FROM " & gstrBDMLinkCenter & ".medidor WHERE id = " & alngIdMedidor
			objAtualizaBancoDados.QUERY_NAME = lstrSql
			dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

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

			GetConstanteMedidor = ldblCon
		Catch ex As Exception
			Throw New Exception("Ocorreu o Erro: " & Err.Description & " - Rotina: GetConstanteMedidor" & "", ex)
			'MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: GetConstanteMedidor" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Function
		End Try
	End Function

	Private Sub GravaMovimentoUltimaLeitura(ByVal astrZona As String, ByVal astrLivro As String, ByVal astrConta As String, ByVal alngUnidId As Integer, ByVal alngConsId As Integer, ByVal astrGrupo As String, ByVal astrSituacao As String, ByVal adblUltLeit As Double, ByVal adblUltLeitDesligado As Double, ByVal adblLeituraLc As Double, ByVal adblLeitAntLc As Double, ByVal adblResiduo As Double, ByVal adblSequenciaOs As Double)
		Dim ldblProtocolo As Double
		Dim ldblProtocoloSub As Short
		Dim lstrIdClasComercial As String
		Dim lstrDsClasComercial As String
		Dim lstrLAt As String
		Dim lstrLAn As String
		Dim lstrSql As String

		Try
			lstrSql = ""
			lstrSql = "SELECT * FROM `leitmensal` WHERE `unid_id` = " & alngUnidId & " LIMIT 0, 1"
			objAtualizaBancoDados.QUERY_NAME = lstrSql
			dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

			If dt.Rows.Count > 0 Then
				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"'
					lstrLAt = IIf(IsDBNull(row.Item("leit_atual")), "", row.Item("leit_atual"))
					'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"'
					lstrLAn = IIf(IsDBNull(row.Item("leit_ant")), "", row.Item("leit_ant"))
				Next
			End If

			ldblProtocolo = 0
			ldblProtocoloSub = 0
			lstrIdClasComercial = ""
			lstrDsClasComercial = ""

			If adblSequenciaOs > 0 Then
				lstrSql = ""
				lstrSql = "SELECT * FROM " & gstrBDCLinkCenter & ".`os` WHERE `sequencia_os` = " & adblSequenciaOs
				objAtualizaBancoDados.QUERY_NAME = lstrSql
				dt1 = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

				If dt1.Rows.Count > 0 Then
					For Each row1 As DataRow In dt1.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"'
						ldblProtocolo = IIf(IsDBNull(row1.Item("id")), 0, row1.Item("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"'
						ldblProtocoloSub = IIf(IsDBNull(row1.Item("id_sub")), 0, row1.Item("id_sub"))
						'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"'
						lstrIdClasComercial = IIf(IsDBNull(row1.Item("id_class_comercial")), "", row1.Item("id_class_comercial"))
						'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"'
						lstrDsClasComercial = IIf(IsDBNull(row1.Item("descr_class_comercial")), "", row1.Item("descr_class_comercial"))
					Next
				End If

			End If
			lstrSql = "INSERT INTO `leituras_unidb_lc` SET "
			lstrSql = lstrSql & "`zona` = '" & VB.Right("000" & astrZona, 3) & "', "
			lstrSql = lstrSql & "`livro` = '" & VB.Right("00" & astrLivro, 2) & "', "
			lstrSql = lstrSql & "`conta` = '" & VB.Right("000000" & astrConta, 6) & "', "
			lstrSql = lstrSql & "`unid_id` = " & alngUnidId & ", "
			lstrSql = lstrSql & "`cons_id` = " & alngConsId & ", "
			lstrSql = lstrSql & "`grupo` = '" & astrGrupo & "', "
			lstrSql = lstrSql & "`situacao` = '" & astrSituacao & "', "
			lstrSql = lstrSql & "`caderno_leit_atual` = '" & lstrLAt & "', "
			lstrSql = lstrSql & "`caderno_leit_anterior` = '" & lstrLAn & "', "
			lstrSql = lstrSql & "`ultleit` = " & adblUltLeit & ", "
			lstrSql = lstrSql & "`ultleit_desligamento` = " & adblUltLeitDesligado & ", "
			lstrSql = lstrSql & "`leitura_lc` = " & adblLeituraLc & ", "
			lstrSql = lstrSql & "`ultimaleit_lc` = " & adblLeitAntLc & ", "
			lstrSql = lstrSql & "`residuo` = " & Replace(adblResiduo, ",", ".") & ", "
			lstrSql = lstrSql & "`sequencia_os` = " & adblSequenciaOs & ", "
			lstrSql = lstrSql & "`protocolo` = " & ldblProtocolo & ", "
			lstrSql = lstrSql & "`protocolo_sub` = " & ldblProtocoloSub & ", "
			lstrSql = lstrSql & "`id_class_comercial` = '" & lstrIdClasComercial & "', "
			lstrSql = lstrSql & "`descr_class_comercial` = '" & Replace(lstrDsClasComercial, "'", "\'") & "', "
			lstrSql = lstrSql & "`movimento` = '" & VB6.Format(Date.Now, "yyyy-mm-dd hh:mm:ss") & "', " 'CONVERT(DATE_FORMAT(localtime(),'%Y-%m-%d %T') USING Latin1), "
			lstrSql = lstrSql & "`atualizacao` = '0000-00-00 00:00:00', "
			lstrSql = lstrSql & "`usa_desligamento` = 0, "
			lstrSql = lstrSql & "`atualiza_uc` = 0, "
			lstrSql = lstrSql & "`atualiza_caderno_lat` = 0, "
			lstrSql = lstrSql & "`atualiza_caderno_lan` = 0"
			objExecAtualizaBancoDados.QUERY_NAME = lstrSql
			repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)
		Catch ex As Exception
			Throw New Exception("Ocorreu o Erro: " & Err.Description & " - Rotina: GravaMovimentoUltimaLeitura" & "", ex)
			'MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: GravaMovimentoUltimaLeitura" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try

	End Sub

	Private Sub Etapa007()
		Dim ldblRowAfec As Double
		Dim lintSitInstalado As Short
		Dim lintSitReligado As Short
		Dim lintSitRetirado As Short
		Dim lintSitDesligado As Short
		Dim lintSitDesativado As Short
		Dim lintSitSucata As Short
		Dim lintSitLiberado As Short
		Dim lintSitManutencao As Short
		Dim llngFW_Idmedidor As Integer
		Dim llngFw_IdUnidadeConsumidora As Integer
		Dim lintLC_SitMedidor As Short
		Dim llngLC_Id_medidor As Integer
		Dim lstrLC_Grupo As String
		Dim lstrLC_Situacao As String
		Dim llngLC_Unid_id_fatwin As Integer
		Dim lstrSql As String

		Try

			lintSitInstalado = 1
			lintSitReligado = 2
			lintSitRetirado = 3
			lintSitDesligado = 4
			lintSitDesativado = 5
			lintSitSucata = 6
			lintSitLiberado = 7
			lintSitManutencao = 8
			'Primeiro Passo Desativar os Medidores se for nulo retirar se não desligar
			lstrSql = ""
			lstrSql = lstrSql & "SELECT * " & vbCrLf
			lstrSql = lstrSql & "  FROM " & gstrBDCLinkCenter & ".`uc` `lc_uc` " & vbCrLf
			lstrSql = lstrSql & " WHERE  `lc_uc`.`situacao` IN( 'I', 'D') " & vbCrLf
			lstrSql = lstrSql & "   AND `lc_uc`.`unid_id_fatwin` > 0 " & vbCrLf
			lstrSql = lstrSql & "   AND EXISTS(SELECT * " & vbCrLf
			lstrSql = lstrSql & "                FROM " & gstrBDCLinkCenter & ".`os` `lc_o` " & vbCrLf
			lstrSql = lstrSql & "               WHERE `lc_o`.`id_uc` = `lc_uc`.`id` " & vbCrLf

			If Trim(Me.txtSequenciasOs.Text) <> "" Then
				lstrSql = lstrSql & "                 AND `lc_o`.`sequencia_os` in(" & Me.txtSequenciasOs.Text & ") " & vbCrLf
			Else
				lstrSql = lstrSql & "                 AND (DATE(`lc_o`.`data_conclusao`) BETWEEN '" & VB6.Format(idteIni, "yyyy-MM-dd") & "' AND '" & VB6.Format(idteFim, "yyyy-MM-dd") & "')" & vbCrLf
			End If
			lstrSql = lstrSql & " )"

			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, " MOVIMENTO MEDIDORES ")
				For Each row As DataRow In dt.Rows
					llngLC_Id_medidor = CInt(IIf(IsDBNull(row.Item("id_medidor")), 0, row.Item("id_medidor")))
					llngLC_Unid_id_fatwin = CInt(IIf(IsDBNull(row.Item("unid_id_fatwin")), 0, row.Item("unid_id_fatwin")))
					lstrLC_Grupo = Trim(IIf(IsDBNull(row.Item("grupo")), "B", row.Item("grupo")))
					lstrLC_Situacao = Trim(IIf(IsDBNull(row.Item("situacao")), "I", row.Item("situacao")))

					lstrSql = ""
					lstrSql = lstrSql & "SELECT * "
					lstrSql = lstrSql & "  FROM `uc_faturamento` "
					lstrSql = lstrSql & " WHERE `id` = " & llngLC_Unid_id_fatwin

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

					If dt1.Rows.Count > 0 Then
						For Each row1 As DataRow In dt1.Rows
							llngFW_Idmedidor = CInt(IIf(IsDBNull(row1.Item("idmedidor")), 0, row1.Item("idmedidor")))
							llngFw_IdUnidadeConsumidora = BuscaIdUnidadeConsumidora(lstrLC_Grupo, llngLC_Unid_id_fatwin)
							'1
							If llngLC_Id_medidor = 0 Then
								If llngFW_Idmedidor > 0 Then
									lintLC_SitMedidor = BuscaSituacaoMedidorLC(llngFW_Idmedidor)
									If lintLC_SitMedidor = lintSitSucata Then
										Call AtualizaMedidor(llngFW_Idmedidor, lintSitSucata)
										Call AtualizaMedidorUnidadeConsumidora(llngFW_Idmedidor, llngFw_IdUnidadeConsumidora, lintSitSucata, "00000000000000", 0, "Atualizado pelo LinkCenter")
										Call AtualizaMedicaoUnidadeConsumidora(lstrLC_Grupo, llngLC_Unid_id_fatwin, 0)
										Me.lblRegEtapa(6).Text = VB6.Format(CDbl(IIf(IsNumeric(Me.lblRegEtapa(6).Text), Me.lblRegEtapa(6).Text, 0)) + 1, "#0")
									Else
										Call AtualizaMedidor(llngFW_Idmedidor, lintSitLiberado)
										Call AtualizaMedidorUnidadeConsumidora(llngFW_Idmedidor, llngFw_IdUnidadeConsumidora, lintSitRetirado, "00000000000000", 0, "Atualizado pelo LinkCenter")
										If lstrLC_Situacao = "I" Then
											Call AtualizaMedidorUnidadeConsumidora(llngFW_Idmedidor, llngFw_IdUnidadeConsumidora, lintSitDesativado, "00000000000000", 0, "Atualizado pelo LinkCenter")
										End If
										Call AtualizaMedicaoUnidadeConsumidora(lstrLC_Grupo, llngLC_Unid_id_fatwin, 0)
										Me.lblRegEtapa(6).Text = VB6.Format(CDbl(IIf(IsNumeric(Me.lblRegEtapa(6).Text), Me.lblRegEtapa(6).Text, 0)) + 1, "#0")
									End If
								End If
							Else
								If llngLC_Id_medidor = llngFW_Idmedidor Then
									lintLC_SitMedidor = BuscaSituacaoMedidorLC(llngFW_Idmedidor)
									If lintLC_SitMedidor = lintSitSucata Then
										Call AtualizaMedidor(llngFW_Idmedidor, lintSitSucata)
										Call AtualizaMedidorUnidadeConsumidora(llngFW_Idmedidor, llngFw_IdUnidadeConsumidora, lintSitSucata, "00000000000000", 0, "Atualizado pelo LinkCenter")
										Call AtualizaMedicaoUnidadeConsumidora(lstrLC_Grupo, llngLC_Unid_id_fatwin, 0)
										Me.lblRegEtapa(6).Text = VB6.Format(CDbl(IIf(IsNumeric(Me.lblRegEtapa(6).Text), Me.lblRegEtapa(6).Text, 0)) + 1, "#0")
									Else
										If lintLC_SitMedidor <> 1 Then
											Call AtualizaMedidor(llngFW_Idmedidor, lintSitLiberado)
											Call AtualizaMedidorUnidadeConsumidora(llngFW_Idmedidor, llngFw_IdUnidadeConsumidora, lintSitRetirado, "00000000000000", 0, "Atualizado pelo LinkCenter")
										End If
										If lstrLC_Situacao = "I" Then
											Call AtualizaMedidorUnidadeConsumidora(llngFW_Idmedidor, llngFw_IdUnidadeConsumidora, lintSitDesativado, "00000000000000", 0, "Atualizado pelo LinkCenter")
										End If
										Call AtualizaMedicaoUnidadeConsumidora(lstrLC_Grupo, llngLC_Unid_id_fatwin, IIf(lintLC_SitMedidor = 1, llngLC_Id_medidor, 0))
										Me.lblRegEtapa(6).Text = VB6.Format(CDbl(IIf(IsNumeric(Me.lblRegEtapa(6).Text), Me.lblRegEtapa(6).Text, 0)) + 1, "#0")
									End If
								Else
									If llngFW_Idmedidor > 0 Then
										lintLC_SitMedidor = BuscaSituacaoMedidorLC(llngFW_Idmedidor)
										If lintLC_SitMedidor = lintSitSucata Then
											Call AtualizaMedidor(llngFW_Idmedidor, lintSitSucata)
											Call AtualizaMedidorUnidadeConsumidora(llngFW_Idmedidor, llngFw_IdUnidadeConsumidora, lintSitSucata, "00000000000000", 0, "Atualizado pelo LinkCenter")
											Call AtualizaMedicaoUnidadeConsumidora(lstrLC_Grupo, llngLC_Unid_id_fatwin, 0)
											Me.lblRegEtapa(6).Text = VB6.Format(CDbl(IIf(IsNumeric(Me.lblRegEtapa(6).Text), Me.lblRegEtapa(6).Text, 0)) + 1, "#0")
										Else
											Call AtualizaMedidor(llngFW_Idmedidor, lintSitLiberado)
											Call AtualizaMedidorUnidadeConsumidora(llngFW_Idmedidor, llngFw_IdUnidadeConsumidora, lintSitRetirado, "00000000000000", 0, "Atualizado pelo LinkCenter")
											If lstrLC_Situacao = "I" Then
												Call AtualizaMedidorUnidadeConsumidora(llngFW_Idmedidor, llngFw_IdUnidadeConsumidora, lintSitDesativado, "00000000000000", 0, "Atualizado pelo LinkCenter")
											End If
											Call AtualizaMedicaoUnidadeConsumidora(lstrLC_Grupo, llngLC_Unid_id_fatwin, 0)
											Me.lblRegEtapa(6).Text = VB6.Format(CDbl(IIf(IsNumeric(Me.lblRegEtapa(6).Text), Me.lblRegEtapa(6).Text, 0)) + 1, "#0")
										End If
									End If
								End If
							End If
						Next
					End If

					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1,  , " MOVIMENTO MEDIDORES ")
				Next
			End If


			'Segunda regra
			lstrSql = ""
			lstrSql = lstrSql & "SELECT * " & vbCrLf
			lstrSql = lstrSql & "  FROM " & gstrBDCLinkCenter & ".`uc` `lc_uc` " & vbCrLf
			lstrSql = lstrSql & " WHERE  `situacao` IN( 'L','C') " & vbCrLf
			lstrSql = lstrSql & "   AND `unid_id_fatwin` > 0 " & vbCrLf
			lstrSql = lstrSql & "   AND EXISTS(SELECT * " & vbCrLf
			lstrSql = lstrSql & "                FROM " & gstrBDCLinkCenter & ".`os` `lc_o` " & vbCrLf
			lstrSql = lstrSql & "               WHERE `lc_o`.`id_uc` = `lc_uc`.`id` " & vbCrLf
			If Trim(Me.txtSequenciasOs.Text) <> "" Then
				lstrSql = lstrSql & "                 AND `lc_o`.`sequencia_os` in(" & Me.txtSequenciasOs.Text & ") " & vbCrLf
			Else
				lstrSql = lstrSql & "                 AND (DATE(`lc_o`.`data_conclusao`) BETWEEN '" & VB6.Format(idteIni, "yyyy-MM-dd") & "' AND '" & VB6.Format(idteFim, "yyyy-MM-dd") & "')" & vbCrLf

			End If
			lstrSql = lstrSql & "             )"
			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, " MOVIMENTO MENDIDORES ")
				For Each row2 As DataRow In dt2.Rows
					llngLC_Id_medidor = CInt(IIf(IsDBNull(row2.Item("id_medidor")), 0, row2.Item("id_medidor")))
					llngLC_Unid_id_fatwin = CInt(IIf(IsDBNull(row2.Item("unid_id_fatwin")), 0, row2.Item("unid_id_fatwin")))
					lstrLC_Grupo = Trim(IIf(IsDBNull(row2.Item("grupo")), "B", row2.Item("grupo")))
					lstrLC_Situacao = Trim(IIf(IsDBNull(row2.Item("situacao")), "I", row2.Item("situacao")))

					lstrSql = ""
					lstrSql = lstrSql & "SELECT * "
					lstrSql = lstrSql & "  FROM `uc_faturamento` "
					lstrSql = lstrSql & " WHERE `id` = " & llngLC_Unid_id_fatwin
					objAtualizaBancoDados.QUERY_NAME = lstrSql
					dt3 = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

					If dt3.Rows.Count > 0 Then
						For Each row3 As DataRow In dt3.Rows
							llngFW_Idmedidor = CInt(IIf(IsDBNull(row3.Item("idmedidor")), 0, row3.Item("idmedidor")))
							llngFw_IdUnidadeConsumidora = BuscaIdUnidadeConsumidora(lstrLC_Grupo, llngLC_Unid_id_fatwin)
							'1
							If llngLC_Id_medidor > 0 Then
								If llngFW_Idmedidor = 0 Then
									Call AtualizaMedidor(llngLC_Id_medidor, lintSitInstalado)
									Call AtualizaMedidorUnidadeConsumidora(llngLC_Id_medidor, llngFw_IdUnidadeConsumidora, lintSitInstalado, "00000000000000", 0, "Atualizado pelo LinkCenter")
									'''Call AtualizaMedicaoUnidadeConsumidora(lstrLC_Grupo, llngLC_Unid_id_fatwin, llngLC_Id_medidor)
									Me.lblRegEtapa(7).Text = VB6.Format(CDbl(IIf(IsNumeric(Me.lblRegEtapa(7).Text), Me.lblRegEtapa(7).Text, 0)) + 1, "#0")
								Else
									lintLC_SitMedidor = BuscaSituacaoMedidorLC(llngFW_Idmedidor)
									If lintLC_SitMedidor = lintSitSucata Then
										Call AtualizaMedidor(llngFW_Idmedidor, lintSitSucata)
										Call AtualizaMedidorUnidadeConsumidora(llngFW_Idmedidor, llngFw_IdUnidadeConsumidora, lintSitSucata, "00000000000000", 0, "Atualizado pelo LinkCenter")
										Call AtualizaMedicaoUnidadeConsumidora(lstrLC_Grupo, llngLC_Unid_id_fatwin, 0)
										Me.lblRegEtapa(7).Text = VB6.Format(CDbl(IIf(IsNumeric(Me.lblRegEtapa(7).Text), Me.lblRegEtapa(7).Text, 0)) + 1, "#0")
									Else
										Call AtualizaMedidor(llngFW_Idmedidor, lintSitLiberado)
										Call AtualizaMedidorUnidadeConsumidora(llngFW_Idmedidor, llngFw_IdUnidadeConsumidora, lintSitRetirado, "00000000000000", 0, "Atualizado pelo LinkCenter")
										Call AtualizaMedicaoUnidadeConsumidora(lstrLC_Grupo, llngLC_Unid_id_fatwin, 0)
										Me.lblRegEtapa(7).Text = VB6.Format(CDbl(IIf(IsNumeric(Me.lblRegEtapa(7).Text), Me.lblRegEtapa(7).Text, 0)) + 1, "#0")
									End If
									Call AtualizaMedidor(llngLC_Id_medidor, lintSitInstalado)
									Call AtualizaMedidorUnidadeConsumidora(llngLC_Id_medidor, llngFw_IdUnidadeConsumidora, lintSitInstalado, "00000000000000", 0, "Atualizado pelo LinkCenter")
									Call AtualizaMedicaoUnidadeConsumidora(lstrLC_Grupo, llngLC_Unid_id_fatwin, llngLC_Id_medidor)
									Me.lblRegEtapa(7).Text = VB6.Format(CDbl(IIf(IsNumeric(Me.lblRegEtapa(7).Text), Me.lblRegEtapa(7).Text, 0)) + 1, "#0")
								End If
							End If
						Next
					End If

					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1,  , " MOVIMENTO MEDIDORES ")
				Next
			End If

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

	Private Sub Etapa008()
		'busca o capital social
		Dim lstrGrupo As String
		Dim llngUnidId As Integer
		Dim ldblIdCodigo As Double
		Dim lstrDescricao As String
		Dim lstrReferencia As String
		Dim ldblValor As Double
		Dim ldteInicio As Date
		Dim ldteTermino As Date
		Dim ldblProtocolo As Double
		Dim ldteBas As Date
		Dim lstrSql As String

		Try

			ldteBas = Date.Now
			lstrSql = ""
			lstrSql = lstrSql & "SELECT `uc`.`unid_id_fatwin`, `uc`.`id`, `uc`.`grupo`, `convenios_taxas_diversos`.* "
			lstrSql = lstrSql & "  FROM " & gstrBDCLinkCenter & ".`convenios_taxas_diversos`, "
			lstrSql = lstrSql & "       " & gstrBDCLinkCenter & ".`uc` "
			lstrSql = lstrSql & " WHERE `convenios_taxas_diversos`.`data_inicio` >= '" & VB6.Format(ldteBas, "yyyy-MM-dd") & "' "
			lstrSql = lstrSql & "   AND `convenios_taxas_diversos`.`id_uc` = `uc`.`id` "
			lstrSql = lstrSql & "   AND `convenios_taxas_diversos`.`id_codigo` IN(9080000007,9080000008) "
			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, " CAPITAL SOCIAL ")
				For Each row As DataRow In dt.Rows

					lstrGrupo = Trim(IIf(IsDBNull(row.Item("grupo")), "B", row.Item("grupo")))
					llngUnidId = CInt(IIf(IsDBNull(row.Item("unid_id_fatwin")), 0, row.Item("unid_id_fatwin")))
					ldblIdCodigo = CDbl(IIf(IsDBNull(row.Item("id_codigo")), 0, row.Item("id_codigo")))
					lstrDescricao = Trim(IIf(IsDBNull(row.Item("descricao")), "", row.Item("descricao")))
					lstrReferencia = Trim(IIf(IsDBNull(row.Item("referencia")), "", row.Item("referencia")))
					ldblValor = CDbl(IIf(IsDBNull(row.Item("valor")), 0, row.Item("valor")))
					ldteInicio = CDate(IIf(IsDBNull(row.Item("data_inicio")), "1951-01-01", row.Item("data_inicio")))
					ldteTermino = CDate(IIf(IsDBNull(row.Item("data_termino")), "1951-01-01", row.Item("data_termino")))
					ldblProtocolo = CDbl(IIf(IsDBNull(row.Item("protocolo")), 0, row.Item("protocolo")))
					Call VerificaInsereConvenios(llngUnidId, lstrGrupo, ldblIdCodigo, lstrDescricao, ldteInicio, ldteTermino, lstrReferencia, ldblValor, ldteBas)

					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1,  , " CAPITAL SOCIAL ")
				Next
			End If

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

	End Sub

	Private Sub Etapa009()
		Dim lblnExiste As Boolean
		Dim ldblconsreg As Double
		Dim lstrnome_consumidor As String
		Dim lstrtipo_pessoa As String
		Dim lstrcpf_cnpj As String
		Dim lstrRg As String
		Dim ldtedata_nascimento As Date
		Dim ldblmatricula As Double
		Dim lintlocal_votacao As Short
		Dim lintlocal_votacao2 As Short
		Dim lstrdescricao_local_votacao As String
		Dim ldtedata_inclusao As Date
		Dim ldtedata_alteracao As Date
		Dim llngAux As Integer
		Dim lstrSql As String
		Try

			lstrSql = ""
			lstrSql = lstrSql & "SELECT " & vbCrLf
			lstrSql = lstrSql & " `consumidor`.`consreg_fatwin` `consreg`, " & vbCrLf
			lstrSql = lstrSql & " IFNULL(`eleicao_local`.`nome_consumidor`, `consumidor`.`nome`) `nome_consumidor`, " & vbCrLf
			lstrSql = lstrSql & " IFNULL(`eleicao_local`.`tipo_pessoa`,  `consumidor`.`tipo`) `tipo_pessoa`, " & vbCrLf
			lstrSql = lstrSql & " IFNULL(`eleicao_local`.`cpf_cnpj`,  `consumidor`.`cpf_cnpj`) `cpf_cnpj`, " & vbCrLf
			lstrSql = lstrSql & " IFNULL(`eleicao_local`.`rg`,  `consumidor`.`rg`) `rg`, " & vbCrLf
			lstrSql = lstrSql & " IFNULL(`eleicao_local`.`data_nascimento`,  `consumidor`.`data_nascimento`) `data_nascimento`, " & vbCrLf
			lstrSql = lstrSql & " IFNULL(`eleicao_local`.`matricula`,  `consumidor`.`matricula`) `matricula`, " & vbCrLf
			lstrSql = lstrSql & " `eleicao_local`.`local_votacao`, " & vbCrLf
			lstrSql = lstrSql & " `eleicao_local`.`descricao_local_votacao`, " & vbCrLf
			lstrSql = lstrSql & " `eleicao_local`.`data_inclusao`, " & vbCrLf
			lstrSql = lstrSql & " `eleicao_local`.`data_alteracao` " & vbCrLf
			lstrSql = lstrSql & "  FROM " & gstrBDCLinkCenter & ".`consumidor` " & vbCrLf
			lstrSql = lstrSql & " LEFT JOIN " & gstrBDCLinkCenter & ".`eleicao_local` ON(`eleicao_local`.`id_consumidor` = `consumidor`.`id`) " & vbCrLf
			lstrSql = lstrSql & " WHERE `consumidor`.`consreg_fatwin` > 0 " & vbCrLf
			'Call Conectar(lstrSql, lrstAux)
			'With lrstAux
			'	If .State = ADODB.ObjectStateEnum.adStateOpen Then
			'		If .RecordCount > 0 Then
			'			Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, .RecordCount, " Eleição ")
			'			While Not .EOF
			'				lintlocal_votacao2 = 0
			'				'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"'
			'				ldblconsreg = IIf(IsDbNull(.Fields("consreg").value), 0, .Fields("consreg").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"'
			'				lstrnome_consumidor = IIf(IsDbNull(.Fields("nome_consumidor").value), "", .Fields("nome_consumidor").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"'
			'				lstrtipo_pessoa = IIf(IsDbNull(.Fields("tipo_pessoa").value), "", .Fields("tipo_pessoa").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"'
			'				lstrcpf_cnpj = IIf(IsDbNull(.Fields("cpf_cnpj").value), "", .Fields("cpf_cnpj").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"'
			'				lstrRg = IIf(IsDbNull(.Fields("rg").value), "", .Fields("rg").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"'
			'				ldtedata_nascimento = IIf(IsDbNull(.Fields("data_nascimento").value), "1900-01-01", .Fields("data_nascimento").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"'
			'				ldblmatricula = IIf(IsDbNull(.Fields("matricula").value), 0, .Fields("matricula").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"'
			'				lintlocal_votacao = IIf(IsDbNull(.Fields("local_votacao").value), 0, .Fields("local_votacao").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"'
			'				lstrdescricao_local_votacao = IIf(IsDbNull(.Fields("descricao_local_votacao").value), "", .Fields("descricao_local_votacao").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"'
			'				ldtedata_inclusao = IIf(IsDbNull(.Fields("data_inclusao").value), "1900-01-01", .Fields("data_inclusao").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"'
			'				ldtedata_alteracao = IIf(IsDbNull(.Fields("data_alteracao").value), "1900-01-01", .Fields("data_alteracao").value)
			'				lstrSql = ""
			'				lstrSql = lstrSql & "SELECT * "
			'				lstrSql = lstrSql & "  FROM `eleicao_local` "
			'				lstrSql = lstrSql & " WHERE `consreg` = " & ldblconsreg & " "
			'				Call Conectar(lstrSql, lrstEle)
			'				With lrstEle
			'					If .RecordCount > 0 Then
			'						'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"'
			'						lintlocal_votacao2 = IIf(IsDbNull(.Fields("local_votacao").value), 0, .Fields("local_votacao").value)
			'						lblnExiste = True
			'					Else
			'						lblnExiste = False
			'					End If
			'				End With
			'				'          If Not lblnContinua Then
			'				'            Call Executar("DELETE FROM `eleicao_local` WHERE `consreg` = " & ldblconsreg) ', False, , False, False
			'				'          End If
			'				lstrSql = ""
			'				If lblnExiste Then
			'					lstrSql = lstrSql & "UPDATE "
			'				Else
			'					lstrSql = lstrSql & "INSERT INTO "
			'				End If
			'				lstrSql = lstrSql & gstrDataBase & ".`eleicao_local` SET " & vbCrLf
			'				lstrSql = lstrSql & "`consreg` = " & ldblconsreg & ", " & vbCrLf
			'				lstrSql = lstrSql & "`nome_consumidor` = '" & Replace(lstrnome_consumidor, "'", "\'") & "'," & vbCrLf
			'				lstrSql = lstrSql & "`tipo_pessoa` = '" & lstrtipo_pessoa & "'," & vbCrLf
			'				lstrSql = lstrSql & "`cpf_cnpj` = '" & lstrcpf_cnpj & "'," & vbCrLf
			'				lstrSql = lstrSql & "`rg` = '" & lstrRg & "'," & vbCrLf
			'				If CDate(ldtedata_nascimento) = CDate("01/01/1900") Then
			'					lstrSql = lstrSql & "`data_nascimento` = null," & vbCrLf
			'				Else
			'					lstrSql = lstrSql & "`data_nascimento` = '" & VB6.Format(ldtedata_nascimento, "yyyy-MM-dd") & "'," & vbCrLf
			'				End If
			'				lstrSql = lstrSql & "`matricula` = " & ldblmatricula & ", " & vbCrLf
			'				lstrSql = lstrSql & "`local_votacao` = " & lintlocal_votacao & ", " & vbCrLf
			'				lstrSql = lstrSql & "`descricao_local_votacao` = '" & lstrdescricao_local_votacao & "'," & vbCrLf
			'				If CDate(ldtedata_inclusao) = CDate("01/01/1900") Then
			'					lstrSql = lstrSql & "`data_inclusao` = null," & vbCrLf
			'				Else
			'					lstrSql = lstrSql & "`data_inclusao` = '" & VB6.Format(ldtedata_inclusao, "yyyy-MM-dd hh:mm:ss") & "'," & vbCrLf
			'				End If
			'				If CDate(ldtedata_alteracao) = CDate("01/01/1900") Then
			'					lstrSql = lstrSql & "`data_alteracao` = null" & vbCrLf
			'				Else
			'					lstrSql = lstrSql & "`data_alteracao` = '" & VB6.Format(ldtedata_alteracao, "yyyy-MM-dd hh:mm:ss") & "' " & vbCrLf
			'				End If
			'				If lblnExiste Then
			'					lstrSql = lstrSql & " WHERE `consreg` = " & ldblconsreg & " " & vbCrLf
			'					If lintlocal_votacao2 <> lintlocal_votacao Then
			'						Call Executar(lstrSql) ', False, , False, False
			'					End If
			'				Else
			'					Call Executar(lstrSql) ', False, , False, False
			'				End If
			'				Call .MoveNext()
			'				Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1,  , " Eleição ")
			'			End While
			'		End If
			'		Call .Close()
			'	End If
			'End With
			''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
		Catch ex As Exception
			Throw New Exception("Ocorreu o Erro: " & Err.Description & " - Rotina: lNewC" & "", ex)
			'MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: lNewC" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub VerificaInsereConvenios(ByVal alngUnidId As Integer, ByVal astrGrupo As String, ByVal adblIdCodigo As Double, ByVal astrDescricao As String, ByVal adteInicio As Date, ByVal adteTermino As Date, ByVal astrReferencia As String, ByVal adblValor As Double, ByVal adteCadastro As Date)
		Dim llngConsId As Integer
		Dim lstrZona As String
		Dim lstrLivro As String
		Dim lstrConta As String
		Dim lblnExi As Boolean
		Dim ldblRowAfetados As Double
		Dim lstrSql As String
		Try
			'Pega chave atual da unidb ou unida
			If astrGrupo = "B" Or astrGrupo = "A" Then
				If alngUnidId > 0 Then
					lstrSql = ""
					lstrSql = lstrSql & "SELECT * "
					lstrSql = lstrSql & "  FROM `uc_faturamento` "
					lstrSql = lstrSql & " WHERE `id` = " & alngUnidId
					objAtualizaBancoDados.QUERY_NAME = lstrSql
					dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

					If dt.Rows.Count > 0 Then
						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"'
							llngConsId = 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"'
							lstrZona = 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"'
							lstrLivro = 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"'
							lstrConta = Trim(IIf(IsDBNull(row.Item("conta")), "", row.Item("conta")))
						Next
					End If


					lblnExi = False
					If llngConsId > 0 Then
						lstrSql = ""
						lstrSql = lstrSql & "SELECT * "
						lstrSql = lstrSql & "  FROM `convenios` "
						lstrSql = lstrSql & " WHERE `unid_id` = " & alngUnidId & " "
						lstrSql = lstrSql & "   AND `cons_id` = " & llngConsId & " "
						lstrSql = lstrSql & "   AND `zona` = '" & lstrZona & "' "
						lstrSql = lstrSql & "   AND `livro` = '" & lstrLivro & "' "
						lstrSql = lstrSql & "   AND `conta` = '" & lstrConta & "' "
						lstrSql = lstrSql & "   AND `convenio` = '" & CStr(adblIdCodigo) & "' "
						If Len(Trim(astrReferencia)) > 0 Then
							lstrSql = lstrSql & "   AND `ref` = '" & Trim(astrReferencia) & "' "
						End If
						lstrSql = lstrSql & "   AND `dt_ini` = '" & VB6.Format(adteInicio, "yyyy-MM-dd") & "' "
						lstrSql = lstrSql & "   AND `dt_fim` = '" & VB6.Format(adteTermino, "yyyy-MM-dd") & "' "
						objAtualizaBancoDados.QUERY_NAME = lstrSql
						dt1 = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)


						If dt1.Rows.Count = 0 Then
							'For Each row1 As DataRow In dt1.Rows
							lstrSql = ""
							lstrSql = lstrSql & "INSERT INTO `convenios` SET " & vbCrLf
							lstrSql = lstrSql & "       `zona`     = '" & lstrZona & "', " & vbCrLf
							lstrSql = lstrSql & "       `livro`    = '" & lstrLivro & "', " & vbCrLf
							lstrSql = lstrSql & "       `conta`    = '" & lstrConta & "', " & vbCrLf
							lstrSql = lstrSql & "       `cons_id`  =  " & llngConsId & ", " & vbCrLf
							lstrSql = lstrSql & "       `unid_id`  =  " & alngUnidId & ", " & vbCrLf
							lstrSql = lstrSql & "       `convenio` = '" & CStr(adblIdCodigo) & "', " & vbCrLf
							lstrSql = lstrSql & "       `valor`    =  " & Replace(CStr(adblValor), ",", ".") & ", " & vbCrLf
							lstrSql = lstrSql & "       `dt_ini`   = '" & VB6.Format(adteInicio, "yyyy-MM-dd") & "', " & vbCrLf
							lstrSql = lstrSql & "       `dt_fim`   = '" & VB6.Format(adteTermino, "yyyy-MM-dd") & "', " & vbCrLf
							lstrSql = lstrSql & "       `ref`      = '" & Trim(astrReferencia) & "', " & vbCrLf
							lstrSql = lstrSql & "       `sit`      = 'Novo', " & vbCrLf
							lstrSql = lstrSql & "       `dt_cadastro` = '" & VB6.Format(adteCadastro, "yyyy-MM-dd") & "'; "

							objExecAtualizaBancoDados.QUERY_NAME = lstrSql
							repositorioExecAtualizaBancoDados.Consultar(objExecAtualizaBancoDados)

							If ldblRowAfetados > 0 Then
								Call AtualizaHistoricoUnidade(alngUnidId, astrGrupo, "[" & VB6.Format(adteCadastro, "DD/MM/YYYY") & "]: " & astrDescricao & " REF: " & Trim(astrReferencia) & " VALOR: " & VB6.Format(adblValor, "#,##0.00"))
							End If
							'Next
						End If


					End If
				End If
			End If
		Catch ex As Exception
			Throw New Exception("Ocorreu o Erro: " & Err.Description & " - Rotina: VerificaInsereConvenios" & "", ex)

			'MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: VerificaInsereConvenios" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub AtualizaMedicaoUnidadeConsumidora(ByVal astrGrupo As String, ByVal alngUnidId As Integer, ByVal alngIdMedidor As Integer)
		Dim ldblRowAfec As Double
		Dim llngIdTransformador As Integer
		Dim lstrFaseLigacao As String
		Dim lstrNumMedidor As String
		Dim ldblConstanteKWH As Double
		Dim ldblConstanteKW As Double
		Dim ldblConstanteKVa As Double
		Dim ldblConstanteKWHp As Double
		Dim ldblConstanteKWHfp As Double
		Dim ldblConstanteKWp As Double
		Dim ldblConstanteKWfp As Double
		Dim ldblConstanteKVap As Double
		Dim ldblConstanteKVafp As Double
		Dim ldteFabricacao As Date
		Dim llngIdTipoMedidor As Integer
		Dim lstrDsFabricante As String
		Dim lstrTFa As String
		Dim lstrSql As String
		Try

			lstrSql = ""
			lstrSql = lstrSql & "SELECT * "
			lstrSql = lstrSql & "  FROM `medidores` "
			lstrSql = lstrSql & " WHERE `id` = " & alngIdMedidor
			objAtualizaBancoDados.QUERY_NAME = lstrSql
			dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

			If dt.Rows.Count > 0 Then
				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"'
					lstrTFa = IIf(IsDBNull(row.Item("idtipo_fases")), "1", row.Item("idtipo_fases"))
					If lstrTFa = "1" Then lstrTFa = "M"
					If lstrTFa = "2" Then lstrTFa = "B"
					If lstrTFa = "3" Then lstrTFa = "T"
					If lstrTFa = "4" Then lstrTFa = "T"
					'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"'
					llngIdTransformador = CInt(IIf(IsDBNull(row.Item("idtransformador")), "0", row.Item("idtransformador")))
					'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"'
					lstrFaseLigacao = Trim(IIf(IsDBNull(row.Item("fase_ligacao")), "AN", row.Item("fase_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"'
					lstrNumMedidor = Trim(IIf(IsDBNull(row.Item("numero")), "", row.Item("numero")))
					'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"'
					ldblConstanteKWH = CDbl(IIf(IsDBNull(row.Item("const_kwh")), "1", row.Item("const_kwh")))
					'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"'
					ldblConstanteKW = CDbl(IIf(IsDBNull(row.Item("const_kw")), "1", row.Item("const_kw")))
					'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"'
					ldblConstanteKVa = CDbl(IIf(IsDBNull(row.Item("const_kvarh")), "1", row.Item("const_kvarh")))
					'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"'
					ldblConstanteKWHp = CDbl(IIf(IsDBNull(row.Item("const_kwh_p")), "1", row.Item("const_kwh_p")))
					'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"'
					ldblConstanteKWHfp = CDbl(IIf(IsDBNull(row.Item("const_kwh_fp")), "1", row.Item("const_kwh_fp")))
					'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"'
					ldblConstanteKWp = CDbl(IIf(IsDBNull(row.Item("const_kw_p")), "1", row.Item("const_kw_p")))
					'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"'
					ldblConstanteKWfp = CDbl(IIf(IsDBNull(row.Item("const_kw_fp")), "1", row.Item("const_kw_fp")))
					'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"'
					ldblConstanteKVap = CDbl(IIf(IsDBNull(row.Item("const_kvarh_p")), "1", row.Item("const_kvarh_p")))
					'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"'
					ldblConstanteKVafp = CDbl(IIf(IsDBNull(row.Item("const_kvarh_fp")), "1", row.Item("const_kvarh_fp")))
					'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"'
					ldteFabricacao = CDate(IIf(IsDBNull(row.Item("fabricacao")), "1900-01-01", row.Item("fabricacao")))
					'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"'
					llngIdTipoMedidor = CInt(IIf(IsDBNull(row.Item("idtipo_medidor")), "1", row.Item("idtipo_medidor")))
					'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"'
					'UPGRADE_WARNING: Couldn't resolve default property of object BuscaCampoDescricaoTabelaPadrao(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
					lstrDsFabricante = BuscaCampoDescricaoTabelaPadrao("fabricantes", "descricao", CInt(IIf(IsDBNull(row.Item("idfabricantes")), "1", row.Item("idfabricantes"))))
				Next
			End If

			If alngIdMedidor = 0 Then lstrNumMedidor = "0"
			lstrSql = ""
			lstrSql = lstrSql & "UPDATE `uc_faturamento` SET " & vbCrLf
			If alngIdMedidor > 0 Then
				lstrSql = lstrSql & "       `instal`       = " & VB6.Format(llngIdTransformador, "0000") & ", " & vbCrLf
				If astrGrupo = "B" Then
					If ldblConstanteKWH > 0 Then
						lstrSql = lstrSql & "       `const`           = " & Replace(CStr(ldblConstanteKWH), ",", ".") & ", " & vbCrLf
					End If
				Else
					If ldblConstanteKWH > 0 Then
						lstrSql = lstrSql & "       `const_consumo`     = " & Replace(CStr(ldblConstanteKWH), ",", ".") & ", " & vbCrLf
					End If
					If ldblConstanteKW > 0 Then
						lstrSql = lstrSql & "       `const_demanda`     = " & Replace(CStr(ldblConstanteKW), ",", ".") & ", " & vbCrLf
					End If
					If ldblConstanteKVa > 0 Then
						lstrSql = lstrSql & "       `const_kvarh`       = " & Replace(CStr(ldblConstanteKVa), ",", ".") & ", " & vbCrLf
					End If
					If ldblConstanteKWHp > 0 Then
						lstrSql = lstrSql & "       `const_cons_p`      = " & Replace(CStr(ldblConstanteKWHp), ",", ".") & ", " & vbCrLf
					End If
					If ldblConstanteKWHfp > 0 Then
						lstrSql = lstrSql & "       `const_cons_fp`     = " & Replace(CStr(ldblConstanteKWHfp), ",", ".") & ", " & vbCrLf
					End If
					If ldblConstanteKWp > 0 Then
						lstrSql = lstrSql & "       `const_demanda`     = " & Replace(CStr(ldblConstanteKWp), ",", ".") & ", " & vbCrLf
					End If
					If ldblConstanteKWfp > 0 Then
						lstrSql = lstrSql & "       `const_demanda_fp`  = " & Replace(CStr(ldblConstanteKWfp), ",", ".") & ", " & vbCrLf
					End If
					If ldblConstanteKVap > 0 Then
						lstrSql = lstrSql & "       `const_kvarh_p`     = " & Replace(CStr(ldblConstanteKVap), ",", ".") & ", " & vbCrLf
					End If
					If ldblConstanteKVafp > 0 Then
						lstrSql = lstrSql & "       `const_kvarh_fp`    = " & Replace(CStr(ldblConstanteKVafp), ",", ".") & ", " & vbCrLf
					End If
				End If
				lstrSql = lstrSql & "       `dtafabmedidor`   =  '" & VB6.Format(ldteFabricacao, "yyyy-MM-dd") & "', " & vbCrLf
				lstrSql = lstrSql & "       `idtipo_medidor`  = '" & llngIdTipoMedidor & "', "
				lstrSql = lstrSql & "       `marca`           = """ & UCase(lstrDsFabricante) & """, " & vbCrLf
			End If
			If astrGrupo = "B" Then
				lstrSql = lstrSql & "       `num`             = """ & lstrNumMedidor & """, " & vbCrLf
				lstrSql = lstrSql & "       `medkvarh`        = """ & lstrNumMedidor & """, " & vbCrLf
			Else
				lstrSql = lstrSql & "       `eletr`             = """ & lstrNumMedidor & """, " & vbCrLf
				lstrSql = lstrSql & "       `medkw`             = """ & lstrNumMedidor & """, " & vbCrLf
				lstrSql = lstrSql & "       `medkvark`          = """ & lstrNumMedidor & """, " & vbCrLf
			End If
			lstrSql = lstrSql & "       `idmedidor`       = " & alngIdMedidor & " " & vbCrLf
			lstrSql = lstrSql & " WHERE `uc_faturamento`.`id` = " & alngUnidId & " "

			objExecAtualizaBancoDados.QUERY_NAME = lstrSql
			repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

			Me.lblRegEtapa(5).Text = VB6.Format(CDbl(IIf(IsNumeric(Me.lblRegEtapa(5).Text), Me.lblRegEtapa(5).Text, 0)) + ldblRowAfec, "#0")
		Catch ex As Exception
			Throw New Exception("Ocorreu o Erro: " & Err.Description & " - Rotina: AtualizaMedicaoUnidadeConsumidora" & "", ex)
			'MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: AtualizaMedicaoUnidadeConsumidora" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Function BuscaSituacaoMedidorLC(ByVal alngIdMedidor As Integer) As Short
		Dim lintRet As Short
		Dim lstrSit As String
		Dim lstrSql As String
		Try
			lintRet = 1
			lstrSql = ""
			lstrSql = lstrSql & "SELECT * "
			lstrSql = lstrSql & "  FROM " & gstrBDMLinkCenter & ".`medidor` "
			lstrSql = lstrSql & " WHERE `id` = " & alngIdMedidor
			objAtualizaBancoDados.QUERY_NAME = lstrSql
			dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

			If dt.Rows.Count > 0 Then
				For Each row As DataRow In dt.Rows
					lstrSit = Trim(IIf(IsDBNull(row.Item("situacao")), "L", row.Item("situacao")))
					If lstrSit = "I" Then lintRet = 1 'Instalado
					If lstrSit = "S" Then lintRet = 6 'Sucata
					If lstrSit = "R" Then lintRet = 8 'Reparo
					If lstrSit = "V" Then lintRet = 7 'Liberado
				Next
			End If

			BuscaSituacaoMedidorLC = lintRet

		Catch ex As Exception
			Throw New Exception("Ocorreu o Erro: " & Err.Description & " - Rotina: BuscaSituacaoMedidorLC" & "", ex)

			'MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: BuscaSituacaoMedidorLC" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Function
		End Try

	End Function

	Private Function BuscaIdUnidadeConsumidora(ByVal astrGrupo As String, ByRef alngUnidId As Integer) As Integer
		Dim llngRet As Integer
		Dim lstrSql As String

		Try

			lstrSql = ""
			lstrSql = lstrSql & "SELECT * "
			lstrSql = lstrSql & "  FROM `unidade_consumidora` "
			lstrSql = lstrSql & " WHERE `grupo` = '" & astrGrupo & "' "
			lstrSql = lstrSql & "   AND `unid_id` = " & alngUnidId
			objAtualizaBancoDados.QUERY_NAME = lstrSql
			dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

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

			BuscaIdUnidadeConsumidora = llngRet

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

	End Function

	Private Function BuscaClasse(ByVal alngIdClasse As Integer, ByVal astrDsClasse As String, ByVal astrDsSubCla As String) As Integer
		Dim llngRet As Integer
		Dim lintIni As Short
		Dim lintFim As Short
		Dim lstrSql As String

		Try

			llngRet = alngIdClasse
			If InStr(1, astrDsClasse, "RESIDENCIAL") > 0 Then
				lintIni = 9101
				lintFim = 9199
			ElseIf InStr(1, astrDsClasse, "COMERCIAL") > 0 Then
				lintIni = 4000
				lintFim = 4999
			ElseIf InStr(1, astrDsClasse, "INDUSTRIAL") > 0 Then
				lintIni = 0
				lintFim = 3999
			ElseIf InStr(1, astrDsClasse, "RURAL") > 0 Then
				lintIni = 5000
				lintFim = 5999
			ElseIf InStr(1, astrDsClasse, "PODER PÚBLICO") > 0 Then
				If InStr(1, astrDsSubCla, "federal") > 0 Then
					lintIni = 6300
					lintFim = 6399
				ElseIf InStr(1, astrDsSubCla, "estadual") > 0 Then
					lintIni = 6200
					lintFim = 6299
				ElseIf InStr(1, astrDsSubCla, "municipal") > 0 Then
					lintIni = 6100
					lintFim = 6399
				End If
			ElseIf InStr(1, astrDsClasse, "ILUMINAÇÃO PÚBLICA") > 0 Then
				lintIni = 6400
				lintFim = 6499
			ElseIf InStr(1, astrDsClasse, "SERVIÇO PÚBLICO") > 0 Then
				lintIni = 7000
				lintFim = 7999
				If InStr(1, astrDsSubCla, "Tração elétrica") > 0 Then
					lintIni = 7100
					lintFim = 7199
				ElseIf InStr(1, astrDsSubCla, "Água, esgoto e saneamento") > 0 Then
					lintIni = 7200
					lintFim = 7299
				End If
			ElseIf InStr(1, astrDsClasse, "CONSUMO PRÓPRIO") > 0 Then
				lintIni = 8000
				lintFim = 8999
			End If
			'lstrSql = ""
			'lstrSql = lstrSql & "SELECT * "
			'lstrSql = lstrSql & "  FROM `classe` "
			'lstrSql = lstrSql & " WHERE codigo BETWEEN " & lintIni & " AND " & lintFim
			'objAtualizaBancoDados.QUERY_NAME = lstrSql
			'dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

			'If dt.Rows.Count > 0 Then
			'For Each row As DataRow In dt.Rows
			'llngRet = CLng(IIf(IsDBNull(row.Item("codigo")), 0, row.Item("codigo")))
			'Next


			BuscaClasse = llngRet

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

	Private Function BuscaConsReg(ByVal alngLC_Id_consumidor As Integer) As Integer
		Dim llngRet As Integer
		Dim lstrSql As String
		Try
			llngRet = 0
			lstrSql = ""
			lstrSql = lstrSql & "SELECT "
			lstrSql = lstrSql & "       id, nome, tipo, cpf_cnpj, rg, ie, email, telefone, celular, fax, observacao, nome_pai, nome_mae, data_nascimento, uf_nascimento, matricula, "
			lstrSql = lstrSql & "       id_banco, descricao_banco, agencia, conta_corrente, profissao, estado_civil, data_cadastro, consreg_fatwin "
			lstrSql = lstrSql & "  FROM " & gstrBDCLinkCenter & ".`consumidor` "
			lstrSql = lstrSql & " WHERE `id` = " & alngLC_Id_consumidor & " "
			objAtualizaBancoDados.QUERY_NAME = lstrSql
			dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

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

			BuscaConsReg = llngRet
		Catch ex As Exception
			Throw New Exception("Ocorreu o Erro: " & Err.Description & " - Rotina: BuscaConsReg" & "", ex)
			'MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: BuscaConsReg" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Function
		End Try
	End Function

	Private Function BuscaAtualizaCodEndereco(ByVal alngLC_Id_endereco As Integer) As String
		Dim lstrRet As String
		'
		Dim llngFw_Id_Uf As Integer
		'
		Dim llngLC_Id As Integer
		Dim llngLC_Id_bairro As Integer
		Dim llngLC_Id_distrito As Integer
		Dim llngLC_Id_municipio As Integer
		Dim lstrLC_Descricao As String
		Dim lstrLC_Cep As String
		Dim lstrLC_Descricao_bairro As String
		Dim lstrLC_Descricao_distrito As String
		Dim lstrLC_Descricao_municipio As String
		Dim lstrLC_Descricao_Uf As String
		'
		Dim lblnExi As Boolean
		Dim lstrSql As String
		Try
			lstrSql = ""
			lstrSql = lstrSql & "SELECT * "
			lstrSql = lstrSql & "  FROM " & gstrBDCLinkCenter & ".`logra_rua` "
			lstrSql = lstrSql & " WHERE `id`  = " & alngLC_Id_endereco
			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("id_fatwin")), "", row.Item("id_fatwin")))
					llngLC_Id = CInt(IIf(IsDBNull(row.Item("id")), "0", row.Item("id")))
					llngLC_Id_bairro = CInt(IIf(IsDBNull(row.Item("id_bairro")), "0", row.Item("id_bairro")))
					llngLC_Id_distrito = CInt(IIf(IsDBNull(row.Item("id_distrito")), "0", row.Item("id_distrito")))
					llngLC_Id_municipio = CInt(IIf(IsDBNull(row.Item("id_municipio")), "0", row.Item("id_municipio")))
					lstrLC_Descricao = Trim(IIf(IsDBNull(row.Item("descricao")), "", row.Item("descricao")))
					lstrLC_Cep = Trim(IIf(IsDBNull(row.Item("cep")), "", row.Item("cep")))
					lstrLC_Descricao_bairro = Trim(IIf(IsDBNull(row.Item("descricao_bairro")), "", row.Item("descricao_bairro")))
					lstrLC_Descricao_distrito = Trim(IIf(IsDBNull(row.Item("descricao_distrito")), "", row.Item("descricao_distrito")))
					lstrLC_Descricao_municipio = Trim(IIf(IsDBNull(row.Item("descricao_municipio")), "", row.Item("descricao_municipio")))
					lstrLC_Descricao_Uf = Trim(IIf(IsDBNull(row.Item("descricao_uf")), "", row.Item("descricao_uf")))
				Next
			End If

			If VerificaExisteRegistroRet("logra", "cod = '" & lstrRet & "' ") = False Then
				'If VerificaExisteRegistro(gstrDataBase & ".logra", "cod = '" & lstrRet & "' ") = False Then
				'If lstrRet = "" Then 'agora vem a pedreira
				lblnExi = False
				lstrSql = ""
				'lstrSql = lstrSql & "SELECT * "
				'lstrSql = lstrSql & "  FROM `uf` "
				'lstrSql = lstrSql & " WHERE `uf` = '" & lstrLC_Descricao_Uf & "' "

				lstrSql = lstrSql & "SELECT * "
				lstrSql = lstrSql & "  FROM " & gstrBDCLinkCenter & ".`logra_municipio` "
				lstrSql = lstrSql & " WHERE `Descricao_uf` = '" & lstrLC_Descricao_Uf & "' "
				objAtualizaBancoDados.QUERY_NAME = lstrSql
				dt1 = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

				If dt1.Rows.Count > 0 Then
					'For Each row1 As DataRow In dt1.Rows
					'llngFw_Id_Uf = CInt(IIf(IsDBNull(row1.Item("codnum")), 0, row1.Item("codnum")))
					lblnExi = True
					'Next
				End If

				If lblnExi Then
					llngLC_Id_municipio = VerificaInsereMunic(llngFw_Id_Uf, llngLC_Id_municipio, lstrLC_Descricao_municipio)
					llngLC_Id_distrito = VerificaInsereDistrito(llngFw_Id_Uf, llngLC_Id_municipio, llngLC_Id_distrito, lstrLC_Descricao_distrito)
					llngLC_Id_bairro = VerificaInsereBairro(llngFw_Id_Uf, llngLC_Id_municipio, llngLC_Id_distrito, llngLC_Id_bairro, lstrLC_Descricao_bairro)
					lstrRet = VerificaInsereLogra(llngFw_Id_Uf, llngLC_Id_municipio, llngLC_Id_distrito, llngLC_Id_bairro, lstrLC_Descricao, lstrLC_Cep, alngLC_Id_endereco)
					'Atualiza o id_fatwin no L.C.
					lstrSql = ""
					lstrSql = lstrSql & "  UPDATE " & gstrBDCLinkCenter & ".`logra_rua` SET `id_fatwin` = '" & lstrRet & "' "
					lstrSql = lstrSql & " WHERE `id`  = " & alngLC_Id_endereco
					objExecAtualizaBancoDados.QUERY_NAME = lstrSql
					repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)
				End If
			End If
			BuscaAtualizaCodEndereco = lstrRet

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

	End Function

	Private Function VerificaInsereLogra(ByVal alngFw_Id_Uf As Integer, ByVal alngLC_Id_municipio As Integer, ByVal alngLC_Id_distrito As Integer, ByVal alngLC_Id_bairro As Integer, ByVal astrLC_Descricao As String, ByVal astrLC_Cep As String, Optional ByVal alngId_Endereco As Long = 0) As String
		Dim llngId As Integer
		Dim lstrRet As String
		Dim lblnExi As Boolean
		Dim lstrSql As String
		Try
			lblnExi = False
			lstrRet = alngId_Endereco
			lstrSql = ""
			lstrSql = lstrSql & "SELECT * "
			lstrSql = lstrSql & "  FROM `logra` "
			lstrSql = lstrSql & " WHERE `cod`   = '" & alngId_Endereco & "' "
			'lstrSql = lstrSql & " WHERE `nome`   = '" & Replace(RetiraAcentuacao(astrLC_Descricao), "'", "\'") & "' "
			'lstrSql = lstrSql & "   AND `codcid` = " & alngLC_Id_municipio & " "
			'lstrSql = lstrSql & "   AND `codbai` = " & alngLC_Id_bairro & " "
			'lstrSql = lstrSql & "   AND LEFT(`cod`,11) = '" & VB6.Format(alngFw_Id_Uf, "00") & VB6.Format(alngLC_Id_municipio, "0000") & VB6.Format(alngLC_Id_distrito, "00") & VB6.Format(alngLC_Id_bairro, "000") & "' "
			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("cod")), "", row.Item("cod")))
					lblnExi = True
				Next
			End If


			If Not lblnExi Then
				'lstrSql = ""
				'lstrSql = lstrSql & "SELECT MAX(`codnum`) `codnum`"
				'lstrSql = lstrSql & "  FROM `logra` "
				'lstrSql = lstrSql & " WHERE `codcid` = " & alngLC_Id_municipio & " "
				'lstrSql = lstrSql & "   AND `codbai` = " & alngLC_Id_bairro & " "
				'lstrSql = lstrSql & "   AND LEFT(`cod`,11) = '" & VB6.Format(alngFw_Id_Uf, "00") & VB6.Format(alngLC_Id_municipio, "0000") & VB6.Format(alngLC_Id_distrito, "00") & VB6.Format(alngLC_Id_bairro, "000") & "' "
				'objAtualizaBancoDados.QUERY_NAME = lstrSql
				'dt1 = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

				'If dt1.Rows.Count > 0 Then
				'	For Each row1 As DataRow In dt1.Rows
				'		llngId = CInt(IIf(IsDBNull(row1.Item("codnum")), 0, row1.Item("codnum"))) + 1
				'	Next


				'lstrRet = VB6.Format(alngFw_Id_Uf, "00") & VB6.Format(alngLC_Id_municipio, "0000") & VB6.Format(alngLC_Id_distrito, "00") & VB6.Format(alngLC_Id_bairro, "000") & VB6.Format(llngId, "0000")
				lstrSql = ""
				lstrSql = lstrSql & "INSERT INTO `logra` SET "
				lstrSql = lstrSql & "            `cod`    = '" & alngId_Endereco & "', "
				lstrSql = lstrSql & "            `nome`   = '" & Replace(RetiraAcentuacao(astrLC_Descricao), "'", "\'") & "', "
				lstrSql = lstrSql & "            `cep`    = '" & Str_Retira(astrLC_Cep, ".-/") & "', "
				'lstrSql = lstrSql & "            `codnum` =  " & llngId & ", "
				lstrSql = lstrSql & "            `codcid` =  " & alngLC_Id_municipio & ", "
				lstrSql = lstrSql & "            `codbai` =  " & alngLC_Id_bairro & " "
				objExecAtualizaBancoDados.QUERY_NAME = lstrSql
				repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

			End If

			VerificaInsereLogra = lstrRet

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

	End Function

	Private Function VerificaInsereMunic(ByVal alngFw_Id_Uf As Integer, ByVal alngLC_Id_municipio As Integer, ByVal astrLC_Descricao_municipio As String) As Integer
		Dim llngRet As Integer
		Dim lblnExi As Boolean
		Dim lstrSql As String

		Try
			llngRet = alngLC_Id_municipio
			lblnExi = False
			lstrSql = ""
			lstrSql = lstrSql & "SELECT * "
			lstrSql = lstrSql & "  FROM `munic` "
			lstrSql = lstrSql & " WHERE `cod` = '" & alngLC_Id_municipio & "' "
			'lstrSql = lstrSql & " WHERE `nome` = '" & RetiraAcentuacao(astrLC_Descricao_municipio) & "' "
			'lstrSql = lstrSql & "   AND `coduf`  = " & alngFw_Id_Uf & " "
			objAtualizaBancoDados.QUERY_NAME = lstrSql
			dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

			If dt.Rows.Count > 0 Then
				'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"'
				'llngRet = IIf(IsDBNull(row.Item("codnum")), 1, row.Item("codnum"))
				lblnExi = True
				'Next
			End If


			If Not lblnExi Then
				'lstrSql = ""
				'lstrSql = lstrSql & "SELECT MAX(`codnum`) `codnum` "
				'lstrSql = lstrSql & "  FROM `munic` "
				'lstrSql = lstrSql & " WHERE `coduf`  = " & alngFw_Id_Uf & " "
				'objAtualizaBancoDados.QUERY_NAME = lstrSql
				'dt1 = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

				'If dt1.Rows.Count > 0 Then
				'	For Each row1 As DataRow In dt1.Rows
				'		llngRet = (IIf(IsDBNull(row1.Item("codnum")), 0, row1.Item("codnum"))) + 1
				'	Next


				'If llngRet > 0 Then
				lstrSql = ""
				lstrSql = lstrSql & "INSERT INTO `munic` SET "
				lstrSql = lstrSql & "            `cod`    = '" & alngLC_Id_municipio & "', "
				'lstrSql = lstrSql & "            `cod`    = '" & VB6.Format(alngFw_Id_Uf, "00") & VB6.Format(llngRet, "0000") & "', "
				lstrSql = lstrSql & "            `nome`   = '" & Replace(RetiraAcentuacao(astrLC_Descricao_municipio), "'", "\'") & "', "
				'lstrSql = lstrSql & "            `codnum` =  " & llngRet & ", "
				'lstrSql = lstrSql & "            `coduf`  =  " & alngFw_Id_Uf & ", "
				lstrSql = lstrSql & "            `zona`   = '', "
				lstrSql = lstrSql & "            `ibge`   = '' "
				objExecAtualizaBancoDados.QUERY_NAME = lstrSql
				repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)


			End If

			VerificaInsereMunic = llngRet

		Catch ex As Exception
			Throw New Exception("Ocorreu o Erro: " & Err.Description & " - Rotina: VerificaInsereMunic" & "", ex)

			'MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: VerificaInsereMunic" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Function
		End Try

	End Function

	Private Function VerificaInsereDistrito(ByVal alngFw_Id_Uf As Integer, ByVal alngLC_Id_municipio As Integer, ByVal alngLC_Id_distrito As Integer, ByVal astrLC_Descricao_distrito As String) As Integer
		Dim llngRet As Integer
		Dim lblnExi As Boolean
		Dim lstrSql As String

		Try
			llngRet = alngLC_Id_distrito
			lblnExi = False
			lstrSql = ""
			lstrSql = lstrSql & "SELECT * "
			lstrSql = lstrSql & "  FROM `distrito` "
			lstrSql = lstrSql & " WHERE `cod` = '" & alngLC_Id_distrito & "' "
			'lstrSql = lstrSql & " WHERE `nome` = '" & RetiraAcentuacao(astrLC_Descricao_distrito) & "' "
			'lstrSql = lstrSql & "   AND `codcid` = " & alngLC_Id_municipio & " "
			'lstrSql = lstrSql & "   AND LEFT(`cod`,2) = '" & VB6.Format(alngFw_Id_Uf, "00") & "' "
			objAtualizaBancoDados.QUERY_NAME = lstrSql
			dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

			If dt.Rows.Count > 0 Then
				'For Each row As DataRow In dt.Rows
				'llngRet = IIf(IsDBNull(row.Item("codnum")), 1, row.Item("codnum"))
				lblnExi = True
				'Next
			End If

			If Not lblnExi Then
				'lstrSql = ""
				'lstrSql = lstrSql & "SELECT MAX(`codnum`) `codnum` "
				'lstrSql = lstrSql & "  FROM `distrito` "
				'lstrSql = lstrSql & " WHERE `codcid` = " & alngLC_Id_municipio & " "
				'lstrSql = lstrSql & "   AND LEFT(`cod`,2) = '" & VB6.Format(alngFw_Id_Uf, "00") & "' "
				'objAtualizaBancoDados.QUERY_NAME = lstrSql
				'dt1 = repositorioExecAtualizaBancoDados.Consultar(objExecAtualizaBancoDados)

				'If dt1.Rows.Count > 0 Then
				'	For Each row1 As DataRow In dt1.Rows
				'		llngRet = (IIf(IsDBNull(row1.Item("codnum")), 0, row1.Item("codnum"))) + 1
				'	Next


				'If llngRet > 0 Then
				lstrSql = ""
				lstrSql = lstrSql & "INSERT INTO `distrito` SET "
				lstrSql = lstrSql & "            `cod`    = '" & alngLC_Id_distrito & "', "
				'lstrSql = lstrSql & "            `cod`    = '" & VB6.Format(alngFw_Id_Uf, "00") & VB6.Format(alngLC_Id_municipio, "0000") & VB6.Format(llngRet, "00") & "', "
				lstrSql = lstrSql & "            `nome`   = '" & Replace(RetiraAcentuacao(astrLC_Descricao_distrito), "'", "\'") & "', "
				'lstrSql = lstrSql & "            `codnum` =  " & llngRet & ", "
				lstrSql = lstrSql & "            `codcid`  =  " & alngLC_Id_municipio & " "
				objExecAtualizaBancoDados.QUERY_NAME = lstrSql
				repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)


			End If

			VerificaInsereDistrito = llngRet
		Catch ex As Exception
			Throw New Exception("Ocorreu o Erro: " & Err.Description & " - Rotina: VerificaInsereDistrito" & "", ex)
			'MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: VerificaInsereDistrito" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Function
		End Try
	End Function

	Private Function VerificaInsereBairro(ByVal alngFw_Id_Uf As Integer, ByVal alngLC_Id_municipio As Integer, ByVal alngLC_Id_distrito As Integer, ByVal alngLC_Id_bairro As Integer, ByVal astrLC_Descricao_bairro As String) As Integer
		Dim llngRet As Integer
		Dim lblnExi As Boolean
		Dim lstrSql As String
		Try
			llngRet = alngLC_Id_bairro
			lblnExi = False
			lstrSql = ""
			lstrSql = lstrSql & "SELECT * "
			lstrSql = lstrSql & "  FROM `bairro` "
			lstrSql = lstrSql & " WHERE `cod` = '" & alngLC_Id_bairro & "' "
			'lstrSql = lstrSql & " WHERE `nome` = '" & RetiraAcentuacao(astrLC_Descricao_bairro) & "' "
			'lstrSql = lstrSql & "   AND `coddist` = " & alngLC_Id_distrito & " "
			'lstrSql = lstrSql & "   AND LEFT(`cod`,6) = '" & VB6.Format(alngFw_Id_Uf, "00") & VB6.Format(alngLC_Id_municipio, "0000") & "' "
			objAtualizaBancoDados.QUERY_NAME = lstrSql
			dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

			If dt.Rows.Count > 0 Then
				'For Each row As DataRow In dt.Rows
				'llngRet = IIf(IsDBNull(row.Item("codnum")), 1, row.Item("codnum"))
				lblnExi = True
				'Next
			End If

			If Not lblnExi Then
				'lstrSql = ""
				'lstrSql = lstrSql & "SELECT MAX(`codnum`) `codnum` "
				'lstrSql = lstrSql & "  FROM `bairro` "
				'lstrSql = lstrSql & " WHERE `coddist` = " & alngLC_Id_distrito & " "
				'lstrSql = lstrSql & "   AND LEFT(`cod`,6) = '" & VB6.Format(alngFw_Id_Uf, "00") & VB6.Format(alngLC_Id_municipio, "0000") & "' "

				'objAtualizaBancoDados.QUERY_NAME = lstrSql
				'dt1 = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

				'If dt1.Rows.Count > 0 Then
				'	For Each row1 As DataRow In dt1.Rows
				'		llngRet = (IIf(IsDBNull(row1.Item("codnum")), 0, row1.Item("codnum"))) + 1
				'	Next


				'If llngRet > 0 Then
				lstrSql = ""
				lstrSql = lstrSql & "INSERT INTO `bairro` SET "
				lstrSql = lstrSql & "            `cod`     = '" & alngLC_Id_bairro & "', "
				'lstrSql = lstrSql & "            `cod`     = '" & VB6.Format(alngFw_Id_Uf, "00") & VB6.Format(alngLC_Id_municipio, "0000") & VB6.Format(alngLC_Id_distrito, "00") & VB6.Format(llngRet, "000") & "', "
				lstrSql = lstrSql & "            `nome`    = '" & Replace(RetiraAcentuacao(astrLC_Descricao_bairro), "'", "\'") & "', "
				'lstrSql = lstrSql & "            `codnum`  =  " & llngRet & ", "
				lstrSql = lstrSql & "            `coddist` =  " & alngLC_Id_distrito & " "
				objExecAtualizaBancoDados.QUERY_NAME = lstrSql
				repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)


			End If

			VerificaInsereBairro = llngRet

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

	Private Function BuscaIdAgrupaLinkCenter(ByVal alngLC_Id_agrupa As Integer) As Integer
		Dim llngRet As Integer
		Dim lstrSql As String
		Try
			llngRet = 0
			lstrSql = ""
			lstrSql = lstrSql & "SELECT * "
			lstrSql = lstrSql & "  FROM " & gstrBDCLinkCenter & ".`agrupamento` "
			lstrSql = lstrSql & " WHERE `id`  = " & alngLC_Id_agrupa
			objAtualizaBancoDados.QUERY_NAME = lstrSql
			dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

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

			'If llngRet = 0 Then llngRet = CInt(GetConfig("cod.banco.principal"))
			BuscaIdAgrupaLinkCenter = llngRet
		Catch ex As Exception
			Throw New Exception("Ocorreu o Erro: " & Err.Description & " - Rotina: BuscaIdBancoLinkCenter" & "", ex)
			'MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: BuscaIdBancoLinkCenter" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Function
		End Try

	End Function
	Private Function BuscaIdBancoLinkCenter(ByVal alngLC_Id_banco As Integer) As Integer
		Dim llngRet As Integer
		Dim lstrSql As String
		Try
			llngRet = 0
			lstrSql = ""
			lstrSql = lstrSql & "SELECT * "
			lstrSql = lstrSql & "  FROM " & gstrBDCLinkCenter & ".`banco` "
			lstrSql = lstrSql & " WHERE `id`  = " & alngLC_Id_banco
			objAtualizaBancoDados.QUERY_NAME = lstrSql
			dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

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

			'If llngRet = 0 Then llngRet = CInt(GetConfig("cod.banco.principal"))
			BuscaIdBancoLinkCenter = llngRet
		Catch ex As Exception
			Throw New Exception("Ocorreu o Erro: " & Err.Description & " - Rotina: BuscaIdBancoLinkCenter" & "", ex)
			'MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: BuscaIdBancoLinkCenter" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Function
		End Try

	End Function

	Private Sub AtualizaBobinaCorrente(ByVal alngLC_Id_corrente As Integer, ByVal astrLC_Descricao_corrente As String)
		Dim lblnExi As Boolean
		Dim lstrSql As String
		Try
			lblnExi = False
			lstrSql = ""
			lstrSql = lstrSql & "SELECT * "
			lstrSql = lstrSql & "  FROM `bobina_corrente` "
			lstrSql = lstrSql & " WHERE `id` = " & alngLC_Id_corrente
			objAtualizaBancoDados.QUERY_NAME = lstrSql
			dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

			lblnExi = (dt.Rows.Count > 0)

			If Not lblnExi Then
				lstrSql = ""
				lstrSql = lstrSql & "INSERT INTO `bobina_corrente` "
				lstrSql = lstrSql & " (`id`                       , `descricao`                        , `situacao`, `idusuarios`) VALUES"
				lstrSql = lstrSql & " (" & alngLC_Id_corrente & ",'" & astrLC_Descricao_corrente & "','Ativo'    ," & ilngIdUsuarioLC & ");"
				objExecAtualizaBancoDados.QUERY_NAME = lstrSql
				repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)
			End If
		Catch ex As Exception
			Throw New Exception("Ocorreu o Erro: " & Err.Description & " - Rotina: AtualizaBobinaCorrente" & "", ex)
			'MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: AtualizaBobinaCorrente" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub AtualizaBobinaTensao(ByVal alngLC_Id_tensao As Integer, ByVal astrLC_Descricao_tensao As String)
		Dim lblnExi As Boolean
		Dim lstrSql As String

		Try
			lblnExi = False
			lstrSql = ""
			lstrSql = lstrSql & "SELECT * "
			lstrSql = lstrSql & "  FROM `bobina_tensao` "
			lstrSql = lstrSql & " WHERE `id` = " & alngLC_Id_tensao
			objAtualizaBancoDados.QUERY_NAME = lstrSql
			dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

			lblnExi = (dt.Rows.Count > 0)

			If Not lblnExi Then
				lstrSql = ""
				lstrSql = lstrSql & "INSERT INTO `bobina_tensao` "
				lstrSql = lstrSql & " (`id`                       , `descricao`                        , `situacao`, `idusuarios`) VALUES"
				lstrSql = lstrSql & " (" & alngLC_Id_tensao & ",'" & astrLC_Descricao_tensao & "','Ativo'    ," & ilngIdUsuarioLC & ");"
				objExecAtualizaBancoDados.QUERY_NAME = lstrSql
				repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)
			End If

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

	End Sub

	Private Sub AtualizaDisjuntor(ByVal alngLC_Id_disjuntor As Integer, ByVal astrLC_Descricao_disjuntor As String)
		Dim lblnExi As Boolean
		Dim lstrSql As String

		Try
			lblnExi = False
			lstrSql = ""
			lstrSql = lstrSql & "SELECT * "
			lstrSql = lstrSql & "  FROM `disjuntor` "
			lstrSql = lstrSql & " WHERE `id` = " & alngLC_Id_disjuntor
			objAtualizaBancoDados.QUERY_NAME = lstrSql
			dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

			lblnExi = (dt.Rows.Count > 0)

			If Not lblnExi Then
				lstrSql = ""
				lstrSql = lstrSql & "INSERT INTO `disjuntor` "
				lstrSql = lstrSql & " (`id`                       , `descricao`                        , `situacao`, `idusuarios`) VALUES"
				lstrSql = lstrSql & " (" & alngLC_Id_disjuntor & ",'" & astrLC_Descricao_disjuntor & "','Ativo'    ," & ilngIdUsuarioLC & ");"
				objExecAtualizaBancoDados.QUERY_NAME = lstrSql
				repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)
			End If

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

	End Sub

	Private Sub AtualizaFabricantes(ByVal alngLC_Id_fabricante As Integer, ByVal astrLC_Descricao_fabricante As String)
		Dim lblnExi As Boolean
		Dim lstrSql As String
		Try
			lblnExi = False
			lstrSql = ""
			lstrSql = lstrSql & "SELECT * "
			lstrSql = lstrSql & "  FROM `fabricantes` "
			lstrSql = lstrSql & " WHERE `id` = " & alngLC_Id_fabricante
			objAtualizaBancoDados.QUERY_NAME = lstrSql
			dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

			lblnExi = (dt.Rows.Count > 0)

			If Not lblnExi Then
				lstrSql = ""
				lstrSql = lstrSql & "INSERT INTO `fabricantes` "
				lstrSql = lstrSql & " (`id`                        , `descricao`                         , `situacao`, `idusuarios`) VALUES"
				lstrSql = lstrSql & " (" & alngLC_Id_fabricante & ",'" & astrLC_Descricao_fabricante & "','Ativo'    ," & ilngIdUsuarioLC & ");"
				objExecAtualizaBancoDados.QUERY_NAME = lstrSql
				repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)
			End If
		Catch ex As Exception
			Throw New Exception("Ocorreu o Erro: " & Err.Description & " - Rotina: AtualizaFabricantes" & "", ex)
			'MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: AtualizaFabricantes" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try

	End Sub

	Private Sub AtualizaTipoPadraoMedicao(ByVal alngLC_Id_tipo_padrao_medicao As Integer, ByVal astrLC_Descricao_tipo_padrao_medicao As String)
		Dim lblnExi As Boolean
		Dim lstrSql As String
		Try
			lblnExi = False
			lstrSql = ""
			lstrSql = lstrSql & "SELECT * "
			lstrSql = lstrSql & "  FROM `tipo_padrao_medicao` "
			lstrSql = lstrSql & " WHERE `id` = " & alngLC_Id_tipo_padrao_medicao
			objAtualizaBancoDados.QUERY_NAME = lstrSql
			dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

			lblnExi = (dt.Rows.Count > 0)

			If Not lblnExi Then
				lstrSql = ""
				lstrSql = lstrSql & "INSERT INTO `tipo_padrao_medicao` "
				lstrSql = lstrSql & " (`id`                       , `descricao`                        , `situacao`, `idusuarios`) VALUES"
				lstrSql = lstrSql & " (" & alngLC_Id_tipo_padrao_medicao & ",'" & astrLC_Descricao_tipo_padrao_medicao & "','Ativo'    ," & ilngIdUsuarioLC & ");"
				objExecAtualizaBancoDados.QUERY_NAME = lstrSql
				repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)
			End If
		Catch ex As Exception
			Throw New Exception("Ocorreu o Erro: " & Err.Description & " - Rotina: AtualizaTipoPadraoMedicao" & "", ex)
			'MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: AtualizaTipoPadraoMedicao" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try

	End Sub

	Private Sub AtualizaTipoGrandezaMedida(ByVal alngLC_Id_tipo_grandeza_medida As Integer, ByVal astrLC_Descricao_tipo_grandeza_medida As String)
		Dim lblnExi As Boolean
		Dim lstrSql As String
		Try
			lblnExi = False
			lstrSql = ""
			lstrSql = lstrSql & "SELECT * "
			lstrSql = lstrSql & "  FROM `tipo_grandeza_medida` "
			lstrSql = lstrSql & " WHERE `id` = " & alngLC_Id_tipo_grandeza_medida
			objAtualizaBancoDados.QUERY_NAME = lstrSql
			dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

			lblnExi = (dt.Rows.Count > 0)

			If Not lblnExi Then
				lstrSql = ""
				lstrSql = lstrSql & "INSERT INTO `tipo_grandeza_medida` "
				lstrSql = lstrSql & " (`id`                       , `descricao`                        , `situacao`, `idusuarios`) VALUES"
				lstrSql = lstrSql & " (" & alngLC_Id_tipo_grandeza_medida & ",'" & astrLC_Descricao_tipo_grandeza_medida & "','Ativo'    ," & ilngIdUsuarioLC & ");"
				objExecAtualizaBancoDados.QUERY_NAME = lstrSql
				repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)
			End If

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

	End Sub

	Private Sub AtualizaTipoFases(ByVal alngLC_Id_tipo_fase As Integer, ByVal astrLC_Descricao_tipo_fase As String)
		Dim lblnExi As Boolean
		Dim lstrSql As String
		Try

			lblnExi = False
			lstrSql = ""
			lstrSql = lstrSql & "SELECT * "
			lstrSql = lstrSql & "  FROM `tipo_fases` "
			lstrSql = lstrSql & " WHERE `id` = " & alngLC_Id_tipo_fase
			objAtualizaBancoDados.QUERY_NAME = lstrSql
			dt = repositorioExecAtualizaBancoDados.Consultar(objAtualizaBancoDados)

			lblnExi = (dt.Rows.Count > 0)

			If Not lblnExi Then
				lstrSql = ""
				lstrSql = lstrSql & "INSERT INTO `tipo_fases` "
				lstrSql = lstrSql & " (`id`                       , `descricao`                        , `situacao`, `idusuarios`) VALUES"
				lstrSql = lstrSql & " (" & alngLC_Id_tipo_fase & ",'" & astrLC_Descricao_tipo_fase & "','Ativo'    ," & ilngIdUsuarioLC & ");"
				objExecAtualizaBancoDados.QUERY_NAME = lstrSql
				repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)
			End If
		Catch ex As Exception
			Throw New Exception("Ocorreu o Erro: " & Err.Description & " - Rotina: AtualizaTipoFases" & "", ex)
			'MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: AtualizaTipoFases" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try

	End Sub

	Private Sub AtualizaTransformador(ByVal alngLC_Id_transformador As Integer, ByVal astrLC_Descricao_transformador As String)
		Dim lblnExi As Boolean
		Dim lstrSql As String
		Try
			lblnExi = False
			lstrSql = ""
			lstrSql = lstrSql & "SELECT * "
			lstrSql = lstrSql & "  FROM `transformador` "
			lstrSql = lstrSql & " WHERE `id` = " & alngLC_Id_transformador

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

			lblnExi = (dt.Rows.Count > 0)
			'	
			If Not lblnExi Then
				lstrSql = ""
				lstrSql = lstrSql & "INSERT INTO `transformador` "
				lstrSql = lstrSql & " (`id`, `descricao`, `situacao`, `idusuarios`) VALUES"
				lstrSql = lstrSql & " (" & alngLC_Id_transformador & ",'" & astrLC_Descricao_transformador & "','Ativo'," & ilngIdUsuarioLC & ");"
				objExecAtualizaBancoDados.QUERY_NAME = lstrSql
				repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)
			End If

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

	Private Sub AtualizaTipoMedidor(ByVal alngLC_Id_tipo_medidor As Integer, ByVal astrLC_Descricao_tipo_medidor As String)
		Dim lblnExi As Boolean
		Dim lstrSql As String
		Try
			lblnExi = False
			lstrSql = ""
			lstrSql = lstrSql & "SELECT * "
			lstrSql = lstrSql & "  FROM `tipo_medidor` "
			lstrSql = lstrSql & " WHERE `id` = " & alngLC_Id_tipo_medidor
			objAtualizaBancoDados.QUERY_NAME = lstrSql
			dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

			lblnExi = (dt.Rows.Count > 0)

			If Not lblnExi Then
				lstrSql = ""
				lstrSql = lstrSql & "INSERT INTO `tipo_medidor` "
				lstrSql = lstrSql & " (`id`                         , `descricao`                           , `situacao`, `idusuarios`) VALUES"
				lstrSql = lstrSql & " (" & alngLC_Id_tipo_medidor & ",'" & astrLC_Descricao_tipo_medidor & "','Ativo'    ," & ilngIdUsuarioLC & ");"
				objExecAtualizaBancoDados.QUERY_NAME = lstrSql
				repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)
			End If

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

	End Sub

	Private Function AtualizaConsRegLC(ByVal alngFW_ConsReg As Integer, ByVal alngLC_ConsReg As Integer) As Boolean
		Try
			'On Error Resume Next
			'AtualizaConsRegLC = Executar("UPDATE " & gstrBDCLinkCenter & ".`consumidor` SET `consreg_fatwin` = " & alngFW_ConsReg & " WHERE `id` = " & alngLC_ConsReg, True)
			objExecAtualizaBancoDados.QUERY_NAME = "UPDATE " & gstrBDCLinkCenter & ".`consumidor` SET `consreg_fatwin` = " & alngFW_ConsReg & " WHERE `id` = " & alngLC_ConsReg
			repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

			AtualizaConsRegLC = True

		Catch ex As Exception
			Throw New Exception("Ocorreu o Erro: " & Err.Description & " - Rotina: AtualizaConsRegLC" & "", ex)
			'MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: AtualizaConsRegLC" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			AtualizaConsRegLC = False
			Exit Function
		End Try
	End Function

	Private Function AtualizaUnidIdLC(ByVal alngLC_Unid_id_fatwin As Integer, ByVal alngLC_Id As Integer) As Boolean
		Try
			'On Error Resume Next
			'AtualizaUnidIdLC = Executar("UPDATE " & gstrBDCLinkCenter & ".`uc` SET `unid_id_fatwin` = " & alngLC_Unid_id_fatwin & " WHERE `id` = " & alngLC_Id, True)
			objExecAtualizaBancoDados.QUERY_NAME = "UPDATE " & gstrBDCLinkCenter & ".`uc` SET `unid_id_fatwin` = " & alngLC_Unid_id_fatwin & " WHERE `id` = " & alngLC_Id
			repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

			AtualizaUnidIdLC = True

		Catch ex As Exception
			Throw New Exception("Ocorreu o Erro: " & Err.Description & " - Rotina: AtualizaUnidIdLC" & "", ex)
			'MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: AtualizaUnidIdLC" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			AtualizaUnidIdLC = False
			Exit Function
		End Try

	End Function

	Private Sub InsereAuditoriaConsumidor(ByVal alngConsReg As Integer, ByVal astrEvento As String)
		Dim lstrSql As String
		Try
			lstrSql = ""
			lstrSql = lstrSql & "INSERT INTO `consum_lc_auditoria`( "
			lstrSql = lstrSql & "`consreg`,`matricula`,`nome`,`cgc`,`insc`,`rg`,`dddtel`,`telefone`,`dddfax`,`fax`,`dddcel`,`cel`,`email`,"
			lstrSql = lstrSql & "`nomepai`,`nomemae`,`idestado_civil`,`idprofissao`,`idnacionalidade`,`dt_cadastro`,`dt_nascto`,`icms`,`cip`,`et`,"
			lstrSql = lstrSql & "`observacao`,`itenshist`,`movimento`,`evento`) ("
			lstrSql = lstrSql & "SELECT `consreg`,`matricula`,`nome`,`cgc`,`insc`,`rg`,`dddtel`,`telefone`,`dddfax`,`fax`,`dddcel`,`cel`,`email`,"
			lstrSql = lstrSql & "`nomepai`,`nomemae`,`idestado_civil`,`idprofissao`,`idnacionalidade`,`dt_cadastro`,`dt_nascto`,`icms`,`cip`,`et`,`observacao`,`itenshist`,"
			lstrSql = lstrSql & "'" & VB6.Format(Date.Now, "yyyy-mm-dd hh:mm:ss") & "'  `movimento`,'" & astrEvento & "' `evento` "
			lstrSql = lstrSql & "FROM `consum`"
			lstrSql = lstrSql & "WHERE `consreg` = " & alngConsReg & ")"
			objExecAtualizaBancoDados.QUERY_NAME = lstrSql
			repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

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

	Private Sub InsereAuditoriaUnidadeConsumidora(ByVal alngUnidId As Integer, ByVal astrGrupo As String, ByVal astrEvento As String)
		Dim lstrSql As String
		Try
			'lstrSql = ""
			'lstrSql = lstrSql & "INSERT INTO `uc_lc_auditoria` ("
			'lstrSql = lstrSql & "`unid_id`,`cons_id`,`pagto_duvidoso`,`banco`,`agenc`,`cc`,`grupo`,`ta`,`clas`,`fase`,`situacao`,`ilum`,`tensao`,"
			'lstrSql = lstrSql & "`faselig`,`codend`,`uf`,`cep`,`cidade`,`distrito`,`bairro`,`end`,`numero`,`complement`,`codend_entrega`,"
			'lstrSql = lstrSql & "`uf_entrega`,`cep_entrega`,`cid_entrega`,`dis_entrega`,`bai_entrega`,`end_entrega`,`num_entrega`,`compl_entrega`,"
			'lstrSql = lstrSql & "`diavenc`,`cpf_cnpj`,`rg`,`insc`,`pessoa`,`nome`,`circuito`,`lacre`,`ap_saude`,`tp_ligacao`,`regiao`,`cnae`,"
			'lstrSql = lstrSql & "`data_ligacao`,`data_religacao`,`data_desligamento`,`data_entrada`,`data_corte`,`data_contrato_demanda`,`ultleit`,"
			'lstrSql = lstrSql & "`final`,`dem_contr_fp`,`dem_contr_ponta`,`demanda_contr`,`agrupa`,`instal`,`const`,`const_consumo`,`const_demanda`,"
			'lstrSql = lstrSql & "`const_kvarh`,`const_cons_p`,`const_cons_fp`,`const_demanda_fp`,`const_kvarh_p`,`const_kvarh_fp`,`dtafabmedidor`,"
			'lstrSql = lstrSql & "`idtipo_medidor`,`marca`,`num`,`" & IIf(astrGrupo = "A", "medkvark", "medkvarh") & "`,`eletr`,`medkw`,`idmedidor`,`movimento`,`evento`, `id`) ("
			'lstrSql = lstrSql & "SELECT "
			''lstrSql = lstrSql & "`unid_id`,`cons_id`,`pagto_duvidoso`,`banco`,`agenc`,`cc`,`grupo`,`ta`,`clas`,`fase`,`situacao`,`ilum`,`tensao`,"
			'lstrSql = lstrSql & "`id`,`cons_id`, CASE WHEN pagto_duvidoso IS NULL THEN 0 ELSE pagto_duvidoso END,`banco`,left(agenc,4),`cc`,`grupo`,`ta`,`clas`,`fase`,`situacao`,`ilum`,`tensao`,"
			'lstrSql = lstrSql & "`faselig`,`codend`,`uf`,`cep`,`cidade`,`distrito`,`bairro`,`end`,`numero`,`complement`,`codend_entrega`,"
			'lstrSql = lstrSql & "`uf_entrega`,`cep_entrega`,`cid_entrega`,`dis_entrega`,`bai_entrega`,`end_entrega`,`num_entrega`,`compl_entrega`,"
			'lstrSql = lstrSql & "CASE WHEN diavenc IS NULL THEN '1900-01-01' ELSE diavenc END,`cpf_cnpj`,`rg`,`insc`,`pessoa`,`nome`,`circuito`,`lacre`,`ap_saude`,`tp_ligacao`,`regiao`,`cnae`,"
			'lstrSql = lstrSql & "`data_ligacao`,`data_religacao`,`data_desligamento`,`data_entrada`,`data_corte`,`data_contrato_demanda`,`ultleit`,"
			'lstrSql = lstrSql & "`final`,`dem_contr_fp`,`dem_contr_ponta`,`demanda_contr`,CASE WHEN agrupa IS NULL THEN 0 ELSE agrupa END,`instal`,`const`,`const_consumo`,`const_demanda`,"
			'lstrSql = lstrSql & "`const_kvarh`,`const_cons_p`,`const_cons_fp`,`const_demanda_fp`,`const_kvarh_p`,`const_kvarh_fp`,`dtafabmedidor`,"
			'lstrSql = lstrSql & "CASE WHEN idtipo_medidor IS NULL THEN 0 ELSE idtipo_medidor END,`marca`,`num`,`" & IIf(astrGrupo = "A", "medkvark", "medkvarh") & "`,`eletr`,`medkw`,CASE WHEN idmedidor IS NULL THEN 0 ELSE idmedidor END,"
			'lstrSql = lstrSql & "'" & VB6.Format(Date.Now, "yyyy-mm-dd hh:mm:ss") & "' `movimento`,'" & astrEvento & "' `evento`, `id` "
			'lstrSql = lstrSql & "FROM `uc_faturamento`"
			'lstrSql = lstrSql & "WHERE `id` = " & alngUnidId & ")"
			'objExecAtualizaBancoDados.QUERY_NAME = lstrSql
			'repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

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

	End Sub

	Public Sub SetaDados(ByVal astrSequencia_OS As String, ByVal adteReferencia As Date, ByRef afrmQuemChama As System.Windows.Forms.Form)
		Try
			Me.txtSequenciasOs.Text = astrSequencia_OS
			Me.dtpFimMesAno.Value = "01/" & VB6.Format(adteReferencia, "MM/YYYY")
			bdataSugerida = True
			iFrmQuemChama = afrmQuemChama
			iFrmQuemChama.Enabled = False
			Me.chkMedidores.Enabled = False
		Catch ex As Exception
			Throw New Exception("Ocorreu o Erro: " & Err.Description & " - Rotina: SetaDados" & "", ex)

			'MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: SetaDados" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub AtualizaTabelasTransfTitularidade(ByVal astrGrupo As String, ByVal alngConsIdOrig As Integer, ByVal alngConsIdDest As Integer, ByVal alngUnidId As Integer)
		Dim ldteBas As Date
		Dim lstrSql As String
		Try


			ldteBas = CDate(VB6.Format(VB6.Format(BuscaDataBD, "YYYY-MM") & "-01", "yyyy-MM-dd"))
			'
			'Atualiza Taxas
			'
			lstrSql = ""
			lstrSql = lstrSql & "UPDATE `taxas` "
			lstrSql = lstrSql & "   SET `cons_id` = " & alngConsIdDest & " "
			lstrSql = lstrSql & " WHERE `dt_ini`  > '" & VB6.Format(ldteBas, "yyyy-MM-dd") & "' "
			lstrSql = lstrSql & "   AND `unid_id` =  " & alngUnidId & " "
			lstrSql = lstrSql & "   AND `cons_id` =  " & alngConsIdOrig
			objExecAtualizaBancoDados.QUERY_NAME = lstrSql
			repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)
			'
			'Atualiza caderno de leitura, faturas e taxas
			'
			lstrSql = ""
			If astrGrupo = "B" Then
				lstrSql = ""
				lstrSql = lstrSql & "SELECT * "
				lstrSql = lstrSql & "  FROM `leitmensal` "
				lstrSql = lstrSql & " WHERE `dt_leit` >= '" & VB6.Format(ldteBas, "yyyy-MM-dd") & "' "
				lstrSql = lstrSql & "   AND `unid_id`  =  " & alngUnidId & " "
				lstrSql = lstrSql & " LIMIT 0, 1 "
			ElseIf astrGrupo = "A" Then
				lstrSql = ""
				lstrSql = lstrSql & "SELECT * "
				lstrSql = lstrSql & "  FROM `leitura` "
				lstrSql = lstrSql & " WHERE `dt_leit_atual` >= '" & VB6.Format(ldteBas, "yyyy-MM-dd") & "' "
				lstrSql = lstrSql & "   AND `id_unid`        =  " & alngUnidId & " "
				lstrSql = lstrSql & " LIMIT 0, 1 "
			End If

			If Len(lstrSql) > 0 Then
				objAtualizaBancoDados.QUERY_NAME = lstrSql
				dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

				If dt.Rows.Count > 0 Then

					lstrSql = ""
					lstrSql = lstrSql & "SELECT * "
					lstrSql = lstrSql & "  FROM `nf` "
					lstrSql = lstrSql & " WHERE `mesano_ref` >= " & VB6.Format(ldteBas, "YYYYMM") & " "
					lstrSql = lstrSql & "   AND `unid_id`     = " & alngUnidId & " "
					lstrSql = lstrSql & "   AND `cons_id`     = " & alngConsIdOrig & " "
					lstrSql = lstrSql & " LIMIT 0, 1 "
					objAtualizaBancoDados.QUERY_NAME = lstrSql
					dt1 = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)


					If dt1.Rows.Count < 1 Then

						lstrSql = ""
						lstrSql = lstrSql & "UPDATE `taxas` "
						lstrSql = lstrSql & "   SET `cons_id` = " & alngConsIdDest & " "
						lstrSql = lstrSql & " WHERE `dt_ini`  = '" & VB6.Format(ldteBas, "yyyy-MM-dd") & "' "
						lstrSql = lstrSql & "   AND `unid_id` = " & alngUnidId & " "
						lstrSql = lstrSql & "   AND `cons_id` = " & alngConsIdOrig & " "
						objExecAtualizaBancoDados.QUERY_NAME = lstrSql
						repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

						lstrSql = ""
						If astrGrupo = "B" Then
							lstrSql = ""
							lstrSql = lstrSql & "UPDATE `leitmensal` "
							lstrSql = lstrSql & "   SET `cons_id`  =  " & alngConsIdDest & " "
							lstrSql = lstrSql & " WHERE `dt_leit` >= '" & VB6.Format(ldteBas, "yyyy-MM-dd") & "' "
							lstrSql = lstrSql & "   AND `unid_id`  =  " & alngUnidId & " "
						ElseIf astrGrupo = "A" Then
							lstrSql = ""
							lstrSql = lstrSql & "UPDATE `leitura` "
							lstrSql = lstrSql & "   SET `id_consumidor`  =  " & alngConsIdDest & " "
							lstrSql = lstrSql & " WHERE `dt_leit_atual` >= '" & VB6.Format(ldteBas, "yyyy-MM-dd") & "' "
							lstrSql = lstrSql & "   AND `id_unid`        =  " & alngUnidId & " "
						End If

						If Len(lstrSql) > 0 Then
							objExecAtualizaBancoDados.QUERY_NAME = lstrSql
							repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)
						End If

						lstrSql = ""
						lstrSql = lstrSql & "UPDATE `nftransicao` "
						lstrSql = lstrSql & "   SET `cons_id` = " & alngConsIdDest & " "
						lstrSql = lstrSql & " WHERE `mesano_ref` >= " & VB6.Format(ldteBas, "YYYYMM") & " "
						lstrSql = lstrSql & "   AND `unid_id`     = " & alngUnidId & " "
						lstrSql = lstrSql & "   AND `cons_id`     = " & alngConsIdOrig & " "
						objExecAtualizaBancoDados.QUERY_NAME = lstrSql
						repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)

					End If
				End If
			End If
		Catch ex As Exception
			Throw New Exception("Ocorreu o Erro: " & Err.Description & " - Rotina: AtualizaTabelasTransfTitularidade" & "", ex)

			'MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: AtualizaTabelasTransfTitularidade" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try

	End Sub

	Private Function BuscaTipoLigacaoRamal(ByVal adblIdRamal As Double) As String
		Dim lstrRet As String
		Dim lstrSql As String
		Try
			lstrRet = "A"
			lstrSql = ""
			lstrSql = lstrSql & "SELECT * "
			lstrSql = lstrSql & "  FROM " & gstrBDMLinkCenter & ".`ramal` "
			lstrSql = lstrSql & " WHERE id = " & adblIdRamal
			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("tipo_ligacao")), "A", row.Item("tipo_ligacao")))
				Next
			End If

			BuscaTipoLigacaoRamal = lstrRet

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

	Private Sub chkConsumidor_CheckStateChanged(sender As Object, e As EventArgs) Handles chkConsumidor.CheckStateChanged

		Try
			With Me.txtSequenciasOs
				If Me.chkConsumidor.CheckState = System.Windows.Forms.CheckState.Checked Then
					Me.chkMedidores.CheckState = System.Windows.Forms.CheckState.Unchecked
					Me.chkLogradouro.CheckState = System.Windows.Forms.CheckState.Unchecked
					.Enabled = False
					.Text = ""
				Else
					.Enabled = True
				End If
				.BackColor = System.Drawing.ColorTranslator.FromOle(IIf(.Enabled, System.Drawing.ColorTranslator.ToOle(System.Drawing.SystemColors.Window), System.Drawing.ColorTranslator.ToOle(System.Drawing.SystemColors.Control)))
			End With
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: chkLogradouro_CheckStateChanged" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try

	End Sub

	Public Sub btnProcessar_Click(sender As Object, e As EventArgs) Handles btnProcessar.Click
		Dim reportaProgresso As New ReportaProgresso()
		Dim acao As Action = Sub() IniciarMesFaturamento()

		Dim frmBarraProgresso As New FrmBarraProgressoGenerica(acao)
		frmBarraProgresso.Label2.Text = "Importando dados do Link Center para Iniciar mês de Faturamento..."

		'AddHandler frmBarraProgresso.FormClosed, AddressOf frmBarraProgresso_FormClosed
		frmBarraProgresso.ShowDialog()
	End Sub

	Private Sub frmImportaLinkCenter_FormClosed(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
		If iFrmQuemChama Is Nothing Then
		Else
			iFrmQuemChama.Enabled = True
			'UPGRADE_ISSUE: Control CarregaDadosGerecial_LC could not be resolved because it was within the generic namespace Form. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="084D22AD-ECB1-400F-B4C7-418ECEC5E36E"'
			'Call iFrmQuemChama.CarregaDadosGerecial_LC()
		End If
	End Sub

	Private Function BuscaUltimaLeituraMED(ByVal adblLC_Id_medidor As Integer) As Integer
		Dim llngRet As Integer
		Dim lstrSql As String
		Try
			lstrSql = ""
			lstrSql = lstrSql & "SELECT * "
			lstrSql = lstrSql & "  FROM " & gstrBDMLinkCenter & ".`medidor` "
			lstrSql = lstrSql & " WHERE `id`  = " & adblLC_Id_medidor
			objAtualizaBancoDados.QUERY_NAME = lstrSql
			dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

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

			BuscaUltimaLeituraMED = llngRet
		Catch ex As Exception
			Throw New Exception("Ocorreu o Erro: " & Err.Description & " - Rotina: BuscaUltimaLeituraMED" & "", ex)

			'MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: BuscaUltimaLeituraMED" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Function
		End Try

	End Function

	Private Sub CancelaConvenioTransfTitularidade(ByVal llngFW_Cons_id As Integer, ByVal llngLC_Id As Integer, ByVal llngLC_Unid_id_fatwin As Integer, ByVal astrZona As String, ByVal astrLivro As String, ByVal astrConta As String)

		Dim lstrSql As String
		Dim llngProtocolo As String
		Dim ldteData_conclusao As Date
		Try
			llngProtocolo = ""

			lstrSql = ""
			lstrSql = lstrSql & "SELECT * "
			lstrSql = lstrSql & "FROM " & gstrBDCLinkCenter & ".`os`"
			lstrSql = lstrSql & "WHERE `id_uc` = " & llngLC_Id
			lstrSql = lstrSql & " AND `id_consumidor` = " & llngFW_Cons_id
			lstrSql = lstrSql & " AND `descr_class_comercial` = 'Transf. Titularidade'"
			lstrSql = lstrSql & " AND `status` = 'Concluída'"
			lstrSql = lstrSql & " AND `status_sub` = 'Aprovada'"
			lstrSql = lstrSql & " AND (DATE(`data_conclusao`) BETWEEN '" & VB6.Format(idteIni, "yyyy-MM-dd") & "' AND '" & VB6.Format(idteFim, "yyyy-MM-dd") & "') " & vbCrLf
			objAtualizaBancoDados.QUERY_NAME = lstrSql
			dt = repositorioAtualizaBancoDados.Consultar(objAtualizaBancoDados)

			If dt.Rows.Count > 0 Then
				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"'
					llngProtocolo = IIf(IsDBNull(row.Item("id")), "0", row.Item("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"'
					ldteData_conclusao = IIf(IsDBNull(row.Item("data_conclusao")), "1900-01-01", row.Item("data_conclusao"))
				Next
			End If

			If llngProtocolo <> "" Then
				lstrSql = ""
				lstrSql = lstrSql & "UPDATE `convenios` "
				lstrSql = lstrSql & " SET `sit` = 'Cancelado', `dt_cancelado` = '" & VB6.Format(ldteData_conclusao, "yyyy-MM-dd") & "', `motivo` = 'Transf. Titularidade protocolo: " & llngProtocolo & "'"
				lstrSql = lstrSql & " WHERE `cons_id`  = " & llngFW_Cons_id & " "
				lstrSql = lstrSql & " AND `unid_id` = " & llngLC_Unid_id_fatwin & " "
				lstrSql = lstrSql & " AND `zona` = " & astrZona & " "
				lstrSql = lstrSql & " AND `livro` = " & astrLivro & " "
				lstrSql = lstrSql & " AND `conta` = " & astrConta & " "
				lstrSql = lstrSql & " AND LEFT(`convenio`, 4) = '9030' "
				objExecAtualizaBancoDados.QUERY_NAME = lstrSql
				repositorioExecAtualizaBancoDados.Execute(objExecAtualizaBancoDados)
			End If

		Catch ex As Exception
			Throw New Exception("Ocorreu o Erro: " & Err.Description & " - Rotina: CancelaConvenioTransfTitularidade" & "", ex)

			'MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: CancelaConvenioTransfTitularidade" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try

	End Sub
End Class