Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Friend Class frmRelMedidores
	Inherits System.Windows.Forms.Form
	
	Private Sub cmdArquivo_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdArquivo.Click
		Me.dialArqOpen.DefaultExt = "CSV"
		'UPGRADE_WARNING: Filter has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		Me.dialArqOpen.Filter = "*." & Me.dialArqOpen.DefaultExt
		Me.dialArqOpen.FileName = My.Application.Info.DirectoryPath & "\ListagemMedidores_" & VB6.Format(BuscaDataBD, "yyyymmddHHMMSS") & "." & Me.dialArqOpen.DefaultExt
		Me.dialArqOpen.Title = "Arquivo de exportação de Medidores"
		Me.dialArqOpen.ShowDialog()
		Me.txtArquivo.Text = Me.dialArqOpen.FileName
	End Sub
	
	Private Sub cmdEmitir_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdEmitir.Click
		Dim lstrCriterio As String
		Dim lstrAuxiliar As String
		Dim lstrCaminho As String
		Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
		Me.cmdEmitir.Enabled = False
		Me.cmdFechar.Enabled = False
		Me.dtpBase.Enabled = False
		Call RelMedidores()
		'On Error GoTo ErroEmitir
		lstrAuxiliar = ""
		If Me.opbSintetico.Checked Then
			lstrCaminho = My.Application.Info.DirectoryPath & "\" & LeConfig("DIR_RPT") & "RelMedidoresSintetico.rpt"
			lstrCriterio = "{temp_medidores_total.nomemaquina} = """ & gstrNmEstacao & """"
		Else
			lstrCaminho = My.Application.Info.DirectoryPath & "\" & LeConfig("DIR_RPT") & "RelMedidoresAnalitico.rpt"
			lstrCriterio = "{temp_medidores.nomemaquina} = """ & gstrNmEstacao & """"
		End If
		With crpMedidores
			.ReportFileName = lstrCaminho
			.DiscardSavedData = True
			.WindowState = Crystal.WindowStateConstants.crptMaximized
			.WindowControlBox = True
			.WindowControls = True
			.WindowShowPrintSetupBtn = True
			.WindowShowCloseBtn = True
			.WindowShowSearchBtn = True
			.SelectionFormula = lstrCriterio
			.WindowTitle = "Relatório de medidores"
			.Destination = Crystal.DestinationConstants.crptToWindow
			.set_Formulas(1, "F_Titulo = """ & "Listagem de medidores em: " & VB6.Format(Me.dtpBase.value, "dd/mm/yyyy") & " da rota: " & VB6.GetItemString(Me.cmbRotaIni, Me.cmbRotaIni.SelectedIndex) & " até " & VB6.GetItemString(Me.cmbRotaFim, Me.cmbRotaFim.SelectedIndex) & """ ")
			lstrAuxiliar = " Grupo ("
			If Me.chkGrupoA.CheckState = 1 Then
				lstrAuxiliar = lstrAuxiliar & "A"
			End If
			If Me.chkGrupoB.CheckState = 1 Then
				lstrAuxiliar = lstrAuxiliar & "B"
			End If
			lstrAuxiliar = lstrAuxiliar & ") - Situação ("
			
			If Me.chkLigados.CheckState = 1 Then
				lstrAuxiliar = lstrAuxiliar & "L,"
			End If
			If Me.chkCortados.CheckState = 1 Then
				lstrAuxiliar = lstrAuxiliar & "C,"
			End If
			If Me.chkDesligados.CheckState = 1 Then
				lstrAuxiliar = lstrAuxiliar & "D,"
			End If
			If Me.chkDesativados.CheckState = 1 Then
				lstrAuxiliar = lstrAuxiliar & "I,"
			End If
			lstrAuxiliar = VB.Left(lstrAuxiliar, Len(lstrAuxiliar) - 1) & ")"
			.set_Formulas(2, "F_Subtitulo = """ & lstrAuxiliar & """ ")
			.Action = 0
		End With
		'ErroEmitir:
		'  If Err.Number > 0 Then
		'    Call MsgBox(Err.Number & Err.Description, vbInformation, gstrTituloMensagem)
		'  End If
		Me.cmdEmitir.Enabled = True
		Me.cmdFechar.Enabled = True
		Me.dtpBase.Enabled = True
		Me.Cursor = System.Windows.Forms.Cursors.Default
	End Sub
	
	Private Sub cmdExporta_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdExporta.Click
		Dim lstrLin As String
		Dim lintARQ As Short
		Dim llngRow As Integer
		Dim llngCol As Integer
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		If Trim(Me.txtArquivo.Text) <> "" Then
			Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
			Me.cmdEmitir.Enabled = False
			Me.cmdFechar.Enabled = False
			Me.dtpBase.Enabled = False
			Call RelMedidores()
			
			lintARQ = FreeFile
			FileOpen(lintARQ, Me.txtArquivo.Text, OpenMode.Output)
			lstrSql = ""
			lstrSql = lstrSql & " SELECT "
			lstrSql = lstrSql & "       `temp_medidores`.`zona`, " & vbCrLf
			lstrSql = lstrSql & "       `temp_medidores`.`livro`, " & vbCrLf
			lstrSql = lstrSql & "       `temp_medidores`.`conta`, " & vbCrLf
			lstrSql = lstrSql & "       `temp_medidores`.`rota`, " & vbCrLf
			'
			lstrSql = lstrSql & "       `temp_medidores`.`nome`, "
			lstrSql = lstrSql & "       `temp_medidores`.`logradouro`, " & vbCrLf
			lstrSql = lstrSql & "       `temp_medidores`.`numero`, " & vbCrLf
			lstrSql = lstrSql & "       `temp_medidores`.`bairro`, " & vbCrLf
			lstrSql = lstrSql & "       `temp_medidores`.`complemento`, " & vbCrLf
			'
			lstrSql = lstrSql & "       `temp_medidores`.`tp_ligacao`, " & vbCrLf
			lstrSql = lstrSql & "       `temp_medidores`.`medidor`, " & vbCrLf
			lstrSql = lstrSql & "       `temp_medidores`.`marca`, " & vbCrLf
			lstrSql = lstrSql & "       `temp_medidores`.`tensao`, " & vbCrLf
			lstrSql = lstrSql & "       `temp_medidores`.`fase`, " & vbCrLf
			lstrSql = lstrSql & "       `temp_medidores`.`faselig`, " & vbCrLf
			lstrSql = lstrSql & "       `temp_medidores`.`carga`, " & vbCrLf
			lstrSql = lstrSql & "       `temp_medidores`.`transf`, " & vbCrLf
			lstrSql = lstrSql & "       `temp_medidores`.`dstipo_medidor`, " & vbCrLf
			lstrSql = lstrSql & "       `temp_medidores`.`regiao`, " & vbCrLf
			'
			lstrSql = lstrSql & "       `temp_medidores`.`ap_saude`, " & vbCrLf
			lstrSql = lstrSql & "       `temp_medidores`.`grupo`, " & vbCrLf
			lstrSql = lstrSql & "       `temp_medidores`.`situacao`, " & vbCrLf
			lstrSql = lstrSql & "       `temp_medidores`.`clas`, " & vbCrLf
			lstrSql = lstrSql & "       `temp_medidores`.`data_ligacao`, " & vbCrLf
			lstrSql = lstrSql & "       `temp_medidores`.`data_entrada`, " & vbCrLf & vbCrLf
			lstrSql = lstrSql & "       `temp_medidores`.`data_desligamento`, "
			lstrSql = lstrSql & "       `temp_medidores`.`data_corte` " & vbCrLf
			lstrSql = lstrSql & "  FROM `temp_medidores` " & vbCrLf
			lstrSql = lstrSql & " WHERE `temp_medidores`.`nomemaquina` = '" & gstrNmEstacao & "' "
			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)
						lstrLin = ""
						For llngCol = 0 To 26
							If .Fields(llngCol).Name = "rota" Then
								lstrLin = lstrLin & .Fields(llngCol).Name & ";"
								lstrLin = lstrLin & "sequencia" & ";"
							Else
								lstrLin = lstrLin & .Fields(llngCol).Name & ";"
							End If
						Next llngCol
						PrintLine(lintARQ, lstrLin)
						While Not .EOF
							lstrLin = ""
							For llngCol = 0 To 26
								If .Fields(llngCol).Name = "rota" Then
									lstrLin = lstrLin & " " & VB.Right(VB.Left(.Fields(llngCol).value, 4), 2) & ";"
									lstrLin = lstrLin & " " & VB.Right(.Fields(llngCol).value, 4) & ";"
								Else
									lstrLin = lstrLin & " " & .Fields(llngCol).value & ";"
								End If
							Next llngCol
							PrintLine(lintARQ, lstrLin)
							Call .MoveNext()
							Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
						End While
					End If
				End If
			End With
			FileClose(lintARQ)
			Call MsgBox("Arquivo gerado com sucesso!", MsgBoxStyle.Information, gstrTituloMensagem)
		Else
			Call MsgBox("Você precisa selecionar o caminho e o arquivo para exportação!", MsgBoxStyle.Information, gstrTituloMensagem)
		End If
		'UPGRADE_NOTE: Object lrstAux may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
		lrstAux = Nothing
		Me.cmdEmitir.Enabled = True
		Me.cmdFechar.Enabled = True
		Me.dtpBase.Enabled = True
		Me.Cursor = System.Windows.Forms.Cursors.Default
		
	End Sub
	
	Private Sub frmRelMedidores_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
		Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
		If KeyAscii = System.Windows.Forms.Keys.Escape Then
			Call cmdFechar_Click(cmdFechar, New System.EventArgs())
		ElseIf KeyAscii = System.Windows.Forms.Keys.Return Then 
			Call SendKeysA(System.Windows.Forms.Keys.Tab, True)
		End If
		eventArgs.KeyChar = Chr(KeyAscii)
		If KeyAscii = 0 Then
			eventArgs.Handled = True
		End If
	End Sub
	
	Private Sub frmRelMedidores_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		Dim llngConta As Integer
		Dim lstrSql As String
		'On Error GoTo ErroForm_Load
		
		Me.dtpBase.value = BuscaDataBD
		For llngConta = 0 To 99
			Call Me.cmbRotaIni.Items.Add(VB6.Format(llngConta, "00"))
			Call Me.cmbRotaFim.Items.Add(VB6.Format(llngConta, "00"))
		Next llngConta
		Me.cmbRotaIni.SelectedIndex = 0
		Me.cmbRotaFim.SelectedIndex = Me.cmbRotaFim.Items.Count - 1
		lstrSql = ""
		lstrSql = lstrSql & "CREATE TABLE IF NOT EXISTS `temp_medidores` (" & vbCrLf
		lstrSql = lstrSql & "  `nomemaquina` varchar(255) default NULL," & vbCrLf
		lstrSql = lstrSql & "  `zona` varchar(3) default NULL," & vbCrLf
		lstrSql = lstrSql & "  `livro` varchar(2) default NULL," & vbCrLf
		lstrSql = lstrSql & "  `conta` varchar(8) default NULL," & vbCrLf
		lstrSql = lstrSql & "  `rota` varchar(9) default NULL," & vbCrLf
		'
		lstrSql = lstrSql & "  `nome` varchar(85) default NULL," & vbCrLf
		lstrSql = lstrSql & "  `logradouro` varchar(60) default NULL," & vbCrLf
		lstrSql = lstrSql & "  `numero` varchar(10) default NULL," & vbCrLf
		lstrSql = lstrSql & "  `bairro` varchar(60) default NULL," & vbCrLf
		lstrSql = lstrSql & "  `complemento` varchar(100) default NULL," & vbCrLf
		'
		lstrSql = lstrSql & "  `tp_ligacao` varchar(15) DEFAULT 'A'," & vbCrLf
		lstrSql = lstrSql & "  `medidor` varchar(11) default NULL," & vbCrLf
		lstrSql = lstrSql & "  `marca` varchar(15) DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `tensao` varchar(10) DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `fase` varchar(10) default NULL," & vbCrLf
		lstrSql = lstrSql & "  `faselig` varchar(4) DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `carga` double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `transf` varchar(10) DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `dstipo_medidor` varchar(100) NOT NULL DEFAULT '0'," & vbCrLf
		lstrSql = lstrSql & "  `regiao` varchar(10) DEFAULT NULL," & vbCrLf
		'
		lstrSql = lstrSql & "  `ap_saude` char(1) DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `grupo` char(1) default NULL," & vbCrLf
		lstrSql = lstrSql & "  `situacao` varchar(10) default NULL," & vbCrLf
		lstrSql = lstrSql & "  `clas` varchar(30) DEFAULT NULL, " & vbCrLf
		lstrSql = lstrSql & "  `data_ligacao` date default NULL, " & vbCrLf
		lstrSql = lstrSql & "  `data_entrada` date default NULL, " & vbCrLf
		lstrSql = lstrSql & "  `data_desligamento` date DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `data_corte` date DEFAULT NULL" & vbCrLf
		lstrSql = lstrSql & ") ENGINE=InnoDB DEFAULT CHARSET=latin1"
		Call Executar(lstrSql)
		lstrSql = ""
		lstrSql = lstrSql & "CREATE TABLE IF NOT EXISTS `temp_medidores_total` (" & vbCrLf
		lstrSql = lstrSql & "  `nomemaquina` varchar(255) default NULL," & vbCrLf
		lstrSql = lstrSql & "  `grupo` varchar(1) default NULL," & vbCrLf
		lstrSql = lstrSql & "  `situacao` varchar(10) default NULL," & vbCrLf
		lstrSql = lstrSql & "  `fase` varchar(10) default NULL," & vbCrLf
		lstrSql = lstrSql & "  `tensao` varchar(10) DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `dstipo_medidor` varchar(100) NOT NULL DEFAULT '0'," & vbCrLf
		lstrSql = lstrSql & "  `marca` varchar(15) DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `qtde` int(11) default NULL" & vbCrLf
		lstrSql = lstrSql & ") ENGINE=InnoDB DEFAULT CHARSET=latin1"
		Call Executar(lstrSql)
		
		'ErroForm_Load:
		'  If myConC.Errors.Count > 0 Then
		'    Call MsgBox("Código: " & myConC.Errors.Item(myConC.Errors.Count - 1).NativeError & vbCrLf & "Descrição:" & myConC.Errors.Item(myConC.Errors.Count - 1).Description)
		'  ElseIf Err.Number > 0 Then
		'    Call MsgBox("Código: " & Err.Number & vbCrLf & "Descrição:" & Err.Description)
		'  End If
	End Sub
	
	Private Sub cmdFechar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdFechar.Click
		Call Me.Close()
	End Sub
	
	Private Sub RelMedidores()
		Dim llngRow As Integer
		'
		Dim lstrZona As String
		Dim lstrLivro As String
		Dim lstrConta As String
		Dim lstrRota As String
		'
		Dim lstrNome As String
		Dim lstrLograd As String
		Dim lstrNumero As String
		Dim lstrComple As String
		Dim lstrBairro As String
		'
		Dim lstrTpLigacao As String
		Dim lstrMedidor As String
		Dim lstrMarca As String
		Dim lstrTensao As String
		Dim lstrFase As String
		Dim lstrFaseLig As String
		Dim ldblCarga As Double
		Dim lstrTransf As String
		Dim llngTpMedidor As Integer
		Dim lstrTpMedidor As String
		Dim lstrRegiao As String
		'
		Dim lstrApSaude As String
		Dim lstrGrupo As String
		Dim lstrSituacao As String
		Dim lstrClasse As String
		Dim ldteLigacao As Date
		Dim ldteEntrada As Date
		Dim ldteDesliga As Date
		Dim ldteSuspend As Date
		'
		Dim lblnContinua As Boolean
		Dim lstrAuxiliar As String
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		'On Error GoTo ErroRelMedidores
		lstrSql = "DELETE FROM `temp_medidores` WHERE `nomemaquina` = """ & gstrNmEstacao & """ "
		Call Executar(lstrSql)
		lstrSql = "DELETE FROM `temp_medidores_total` WHERE `nomemaquina` = """ & gstrNmEstacao & """ "
		Call Executar(lstrSql)
		With Me.mfgTotais
			.Clear()
			.Rows = 0
			.Cols = 7
			.Redraw = False
		End With
		lstrAuxiliar = ""
		If Me.chkLigados.CheckState = 1 Then
			lstrAuxiliar = lstrAuxiliar & " 'L',"
		End If
		If Me.chkDesligados.CheckState = 1 Then
			lstrAuxiliar = lstrAuxiliar & " 'D',"
		End If
		If Me.chkCortados.CheckState = 1 Then
			lstrAuxiliar = lstrAuxiliar & " 'C',"
		End If
		If Me.chkDesativados.CheckState = 1 Then
			lstrAuxiliar = lstrAuxiliar & " 'I',"
		End If
		
		If Me.chkGrupoB.CheckState = 1 Then
			lstrSql = ""
			lstrSql = lstrSql & "SELECT `unidb`.`zona`, " & vbCrLf
			lstrSql = lstrSql & "       `unidb`.`livro`, " & vbCrLf
			lstrSql = lstrSql & "       `unidb`.`conta`, " & vbCrLf
			lstrSql = lstrSql & "       `unidb`.`rota`, " & vbCrLf
			'
			lstrSql = lstrSql & "       `unidb`.`nome`, " & vbCrLf
			lstrSql = lstrSql & "       `unidb`.`end`, " & vbCrLf
			lstrSql = lstrSql & "       `unidb`.`numero`, " & vbCrLf
			lstrSql = lstrSql & "       `unidb`.`complement`, " & vbCrLf
			lstrSql = lstrSql & "       `unidb`.`bairro`, " & vbCrLf
			'
			lstrSql = lstrSql & "       `unidb`.`tp_ligacao`, " & vbCrLf
			lstrSql = lstrSql & "       `unidb`.`num` AS `medidor`, " & vbCrLf
			lstrSql = lstrSql & "       `unidb`.`marca`, " & vbCrLf
			lstrSql = lstrSql & "       `unidb`.`tensao`, " & vbCrLf
			lstrSql = lstrSql & "       `unidb`.`fase`, " & vbCrLf
			lstrSql = lstrSql & "       `unidb`.`faselig`, " & vbCrLf
			lstrSql = lstrSql & "       `unidb`.`carga`, " & vbCrLf
			lstrSql = lstrSql & "       `unidb`.`transf`, " & vbCrLf
			lstrSql = lstrSql & "       `unidb`.`idtipo_medidor`, " & vbCrLf
			lstrSql = lstrSql & "       `unidb`.`regiao`, " & vbCrLf
			'
			lstrSql = lstrSql & "       `unidb`.`ap_saude`, " & vbCrLf
			lstrSql = lstrSql & "       `unidb`.`grupo`, " & vbCrLf
			lstrSql = lstrSql & "       `unidb`.`situacao`, " & vbCrLf
			lstrSql = lstrSql & "       `unidb`.`clas`, " & vbCrLf
			lstrSql = lstrSql & "       `unidb`.`data_ligacao`, " & vbCrLf
			lstrSql = lstrSql & "       `unidb`.`data_entrada`, " & vbCrLf
			lstrSql = lstrSql & "       `unidb`.`data_desligamento`, " & vbCrLf
			lstrSql = lstrSql & "       `unidb`.`data_corte` " & vbCrLf
			'
			lstrSql = lstrSql & "  FROM `unidb` " & vbCrLf
			lstrSql = lstrSql & " WHERE TRIM(`unidb`.`num`) <> '' AND `unidb`.`num` IS NOT NULL " & vbCrLf
			lstrSql = lstrSql & "   AND `unidb`.`data_ligacao` <= '" & VB6.Format(Me.dtpBase.value, "yyyy-MM-dd") & "' " & vbCrLf
			lstrSql = lstrSql & "   AND `unidb`.`data_entrada` <= '" & VB6.Format(Me.dtpBase.value, "yyyy-MM-dd") & "' "
			lstrSql = lstrSql & "   AND (`unidb`.`num` IS NOT NULL AND TRIM(`unidb`.`num`) <> '' AND `unidb`.`num` <> '0') "
			If Len(Trim(lstrAuxiliar)) > 0 Then
				lstrSql = lstrSql & " AND `unidb`.`situacao` IN( " & VB.Left(lstrAuxiliar, Len(lstrAuxiliar) - 1) & " )"
			End If
		End If
		If Me.chkGrupoB.CheckState = 1 And Me.chkGrupoA.CheckState = 1 Then
			lstrSql = lstrSql & vbCrLf & " UNION " & vbCrLf
		End If
		If Me.chkGrupoA.CheckState = 1 Then
			lstrSql = lstrSql & "SELECT `unida`.`zona`, " & vbCrLf
			lstrSql = lstrSql & "       `unida`.`livro`, " & vbCrLf
			lstrSql = lstrSql & "       `unida`.`conta`, " & vbCrLf
			lstrSql = lstrSql & "       `unida`.`rota`, " & vbCrLf
			'
			lstrSql = lstrSql & "       `unida`.`nome`, " & vbCrLf
			lstrSql = lstrSql & "       `unida`.`end`, " & vbCrLf
			lstrSql = lstrSql & "       `unida`.`numero`, " & vbCrLf
			lstrSql = lstrSql & "       `unida`.`complement`, " & vbCrLf
			lstrSql = lstrSql & "       `unida`.`bairro`, " & vbCrLf
			'
			lstrSql = lstrSql & "       `unida`.`tp_ligacao`, " & vbCrLf
			lstrSql = lstrSql & "       `unida`.`eletr` AS `medidor`, " & vbCrLf
			lstrSql = lstrSql & "       `unida`.`marca`, " & vbCrLf
			lstrSql = lstrSql & "       `unida`.`tensao`, " & vbCrLf
			lstrSql = lstrSql & "       `unida`.`fase`, " & vbCrLf
			lstrSql = lstrSql & "       'ABCN' AS `faselig`, " & vbCrLf
			lstrSql = lstrSql & "       `unida`.`carga`, " & vbCrLf
			lstrSql = lstrSql & "       '' AS `transf`, " & vbCrLf
			lstrSql = lstrSql & "       `unida`.`idtipo_medidor`, " & vbCrLf
			lstrSql = lstrSql & "       `unida`.`regiao`, " & vbCrLf
			'
			lstrSql = lstrSql & "       'N' AS `ap_saude`, " & vbCrLf
			lstrSql = lstrSql & "       `unida`.`grupo`, " & vbCrLf
			lstrSql = lstrSql & "       `unida`.`situacao`, " & vbCrLf
			lstrSql = lstrSql & "       `unida`.`clas`, " & vbCrLf
			lstrSql = lstrSql & "       `unida`.`data_ligacao`, " & vbCrLf
			lstrSql = lstrSql & "       `unida`.`data_entrada`, " & vbCrLf
			lstrSql = lstrSql & "       `unida`.`data_desligamento`, " & vbCrLf
			lstrSql = lstrSql & "       `unida`.`data_corte` " & vbCrLf
			'
			lstrSql = lstrSql & "  FROM unida " & vbCrLf
			lstrSql = lstrSql & " WHERE TRIM(`unida`.`eletr`) <> '' AND `unida`.`eletr` IS NOT NULL " & vbCrLf
			lstrSql = lstrSql & "   AND `unida`.`data_ligacao` <= '" & VB6.Format(Me.dtpBase.value, "yyyy-MM-dd") & "' " & vbCrLf
			lstrSql = lstrSql & "   AND `unida`.`data_entrada` <= '" & VB6.Format(Me.dtpBase.value, "yyyy-MM-dd") & "' "
			lstrSql = lstrSql & "   AND (`unida`.`eletr` IS NOT NULL AND TRIM(`unida`.`eletr`) <> '' AND `unida`.`eletr` <> '0') "
			If Len(Trim(lstrAuxiliar)) > 0 Then
				lstrSql = lstrSql & " AND `unida`.`situacao` IN( " & VB.Left(lstrAuxiliar, Len(lstrAuxiliar) - 1) & " )"
			End If
		End If
		lstrSql = lstrSql & " ORDER BY right(`rota`,6) ASC "
		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)
					While Not .EOF
						lblnContinua = True
						'
						'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(.Fields("zona").value), "", .Fields("zona").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"'
						lstrLivro = Trim(IIf(IsDbNull(.Fields("livro").value), "", .Fields("livro").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"'
						lstrConta = Trim(IIf(IsDbNull(.Fields("conta").value), "", .Fields("conta").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"'
						lstrRota = Trim(IIf(IsDbNull(.Fields("rota").value), "00000000", .Fields("rota").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 = Trim(IIf(IsDbNull(.Fields("nome").value), "", .Fields("nome").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"'
						lstrLograd = Trim(IIf(IsDbNull(.Fields("end").value), "", .Fields("end").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"'
						lstrNumero = Trim(IIf(IsDbNull(.Fields("numero").value), "", .Fields("numero").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"'
						lstrComple = Trim(IIf(IsDbNull(.Fields("complement").value), "", .Fields("complement").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"'
						lstrBairro = Trim(IIf(IsDbNull(.Fields("bairro").value), "", .Fields("bairro").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"'
						lstrTpLigacao = Trim(IIf(IsDbNull(.Fields("tp_ligacao").value), "A", .Fields("tp_ligacao").value))
						lstrTpLigacao = IIf(lstrTpLigacao = "A", "Aéreo", IIf(lstrTpLigacao = "S", "Subterrâneo", "--"))
						'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"'
						lstrMedidor = Trim(IIf(IsDbNull(.Fields("medidor").value), "", .Fields("medidor").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"'
						lstrMarca = Trim(IIf(IsDbNull(.Fields("marca").value), "", .Fields("marca").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"'
						lstrTensao = Trim(IIf(IsDbNull(.Fields("tensao").value), "", .Fields("tensao").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"'
						lstrFase = Trim(IIf(IsDbNull(.Fields("fase").value), "M", .Fields("fase").value))
						lstrFase = IIf(lstrFase = "M", "MONOFÁSICO", IIf(lstrFase = "B", "BIFÁSICO", IIf(lstrFase = "T", "TRIFÁSICO", "--")))
						'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"'
						lstrFaseLig = Trim(IIf(IsDbNull(.Fields("faselig").value), "ABCN", .Fields("faselig").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"'
						ldblCarga = CDbl(IIf(IsDbNull(.Fields("carga").value), 0, .Fields("carga").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"'
						lstrTransf = Trim(IIf(IsDbNull(.Fields("transf").value), "", .Fields("transf").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"'
						llngTpMedidor = CInt(IIf(IsDbNull(.Fields("idtipo_medidor").value), 0, .Fields("idtipo_medidor").value))
						lstrTpMedidor = BuscaTipoMedidor(llngTpMedidor)
						'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"'
						lstrRegiao = Trim(IIf(IsDbNull(.Fields("regiao").value), "U", .Fields("regiao").value))
						lstrRegiao = IIf(lstrRegiao = "U", "URBANA", IIf(lstrRegiao = "R", "RURAL", "--"))
						'
						'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"'
						lstrApSaude = Trim(IIf(IsDbNull(.Fields("ap_saude").value), "N", .Fields("ap_saude").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"'
						lstrGrupo = VB.Left(Trim(IIf(IsDbNull(.Fields("grupo").value), "B1", .Fields("grupo").value)), 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"'
						lstrSituacao = Trim(IIf(IsDbNull(.Fields("situacao").value), "", .Fields("situacao").value))
						lstrSituacao = IIf(lstrSituacao = "L", "LIGADO", IIf(lstrSituacao = "D", "DESLIGADO", IIf(lstrSituacao = "I", "DESTIVADO", IIf(lstrSituacao = "C", "SUSPENDIDO", "--"))))
						'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"'
						lstrClasse = Trim(IIf(IsDbNull(.Fields("clas").value), "9101", .Fields("clas").value))
						Select Case VB.Left(lstrClasse, 1)
							Case Is = "0"
								lstrClasse = "Industrial"
							Case Is = "1"
								lstrClasse = "Industrial"
							Case Is = "2"
								lstrClasse = "Industrial"
							Case Is = "3"
								lstrClasse = "Industrial"
							Case Is = "4"
								lstrClasse = "Comercial"
							Case Is = "5"
								lstrClasse = "Rural"
							Case Is = "6"
								If VB.Left(lstrClasse, 2) = "64" Then
									lstrClasse = "Iluminação Pública"
								Else
									lstrClasse = "Poder Público"
								End If
							Case Is = "7"
								lstrClasse = "Serviço Público"
							Case Is = "8"
								lstrClasse = "Consumo Próprio"
							Case Is = "9"
								lstrClasse = "Residencial"
							Case Else
								lstrClasse = "indefinido"
						End Select
						'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"'
						ldteLigacao = CDate(IIf(IsDbNull(.Fields("data_ligacao").value), "1900-01-01", .Fields("data_ligacao").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"'
						ldteEntrada = CDate(IIf(IsDbNull(.Fields("data_entrada").value), "1900-01-01", .Fields("data_entrada").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"'
						ldteDesliga = CDate(IIf(IsDbNull(.Fields("data_desligamento").value), "1900-01-01", .Fields("data_desligamento").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"'
						ldteSuspend = CDate(IIf(IsDbNull(.Fields("data_corte").value), "1900-01-01", .Fields("data_corte").value))
						'
						If gstrEmpresa_Sigla = "EFLUL" Then
							If lstrZona = "001" And lstrLivro = "12" And (lstrConta = "001717" Or lstrConta = "001718") Then
								lblnContinua = False
							End If
						End If
						If lblnContinua Then
							lstrSql = ""
							lstrSql = lstrSql & "INSERT INTO `temp_medidores` SET "
							lstrSql = lstrSql & "            `nomemaquina`  = """ & gstrNmEstacao & """, "
							'
							lstrSql = lstrSql & "            `zona`         = """ & lstrZona & """, "
							lstrSql = lstrSql & "            `livro`        = """ & lstrLivro & """, "
							lstrSql = lstrSql & "            `conta`        = """ & lstrConta & """, "
							lstrSql = lstrSql & "            `rota`         = """ & lstrRota & """, "
							'
							lstrSql = lstrSql & "            `nome`         = """ & lstrNome & """, "
							lstrSql = lstrSql & "            `logradouro`   = """ & lstrLograd & """, "
							lstrSql = lstrSql & "            `numero`       = """ & lstrNumero & """, "
							lstrSql = lstrSql & "            `bairro`       = """ & lstrBairro & """, "
							lstrSql = lstrSql & "            `complemento`  = """ & lstrComple & """, "
							'
							lstrSql = lstrSql & "            `tp_ligacao`     = """ & lstrTpLigacao & """, "
							lstrSql = lstrSql & "            `medidor`        = """ & lstrMedidor & """, "
							lstrSql = lstrSql & "            `marca`          = """ & lstrMarca & """, "
							lstrSql = lstrSql & "            `tensao`         = """ & lstrTensao & """, "
							lstrSql = lstrSql & "            `fase`           = """ & lstrFase & """, "
							lstrSql = lstrSql & "            `faselig`        = """ & lstrFaseLig & """, "
							lstrSql = lstrSql & "            `carga`          = """ & Replace(CStr(ldblCarga), ",", ".") & """, "
							lstrSql = lstrSql & "            `transf`         = """ & lstrTransf & """, "
							lstrSql = lstrSql & "            `dstipo_medidor` = """ & lstrTpMedidor & """, "
							lstrSql = lstrSql & "            `regiao`         = """ & lstrRegiao & """, "
							'
							lstrSql = lstrSql & "            `ap_saude`     = """ & lstrApSaude & """, "
							lstrSql = lstrSql & "            `grupo`        = """ & lstrGrupo & """, "
							lstrSql = lstrSql & "            `situacao`     = """ & lstrSituacao & """, "
							lstrSql = lstrSql & "            `clas`        = """ & lstrClasse & """, "
							lstrSql = lstrSql & "            `data_ligacao` = """ & VB6.Format(ldteLigacao, "yyyy-MM-dd") & """, "
							lstrSql = lstrSql & "            `data_entrada` = """ & VB6.Format(ldteEntrada, "yyyy-MM-dd") & """, "
							lstrSql = lstrSql & "            `data_desligamento` = """ & VB6.Format(ldteDesliga, "yyyy-MM-dd") & """, "
							lstrSql = lstrSql & "            `data_corte` = """ & VB6.Format(ldteSuspend, "yyyy-MM-dd") & """ "
							Call Executar(lstrSql)
							Call AdicionaGrade(lstrGrupo, lstrSituacao, lstrFase, lstrTensao, lstrTpMedidor, lstrMarca)
						End If
						Call .MoveNext()
						Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
					End While
				End If
				Call .Close()
			End If
		End With
		With Me.mfgTotais
			If .Rows > 0 Then
				For llngRow = 0 To .Rows - 1
					lstrSql = ""
					lstrSql = lstrSql & "INSERT INTO `temp_medidores_total` SET "
					lstrSql = lstrSql & "            `nomemaquina`    = """ & gstrNmEstacao & """, "
					lstrSql = lstrSql & "            `grupo`          = """ & .get_TextMatrix(llngRow, 0) & """, "
					lstrSql = lstrSql & "            `situacao`       = """ & .get_TextMatrix(llngRow, 1) & """, "
					lstrSql = lstrSql & "            `fase`           = """ & .get_TextMatrix(llngRow, 2) & """, "
					lstrSql = lstrSql & "            `tensao`         = """ & .get_TextMatrix(llngRow, 3) & """, "
					lstrSql = lstrSql & "            `dstipo_medidor` = """ & .get_TextMatrix(llngRow, 4) & """, "
					lstrSql = lstrSql & "            `marca`          = """ & .get_TextMatrix(llngRow, 5) & """, "
					lstrSql = lstrSql & "            `qtde`           = " & CInt(.get_TextMatrix(llngRow, 6)) & " "
					Call Executar(lstrSql)
				Next llngRow
			End If
			.Redraw = True
		End With
		'ErroRelMedidores:
		'  If myConC.Errors.Count > 0 Then
		'    Call MsgBox("Código: " & myConC.Errors.Item(myConC.Errors.Count - 1).NativeError & vbCrLf & "Descrição:" & myConC.Errors.Item(myConC.Errors.Count - 1).Description)
		'    Resume Next
		'  ElseIf Err.Number > 0 Then
		'    Call MsgBox("Código: " & Err.Number & vbCrLf & "Descrição:" & Err.Description)
		'    Resume Next
		'  End If
	End Sub
	
	Private Sub AdicionaGrade(ByVal astrGru As String, ByVal astrSit As String, ByVal astrFas As String, ByVal astrTen As String, ByVal astrTMd As String, ByVal astrMar As Object)
		Dim llngRow As Integer
		Dim llngPos As Integer
		Dim lblnAux As Boolean
		Dim lstrMar As String
		'UPGRADE_WARNING: Couldn't resolve default property of object astrMar. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		lstrMar = Trim(astrMar)
		lstrMar = IIf(lstrMar = "", "SEM MARCA", lstrMar)
		llngPos = 0
		lblnAux = False
		With Me.mfgTotais
			'Busca a linha da grid que tem a informacao
			If .Rows > 0 Then
				For llngRow = 0 To .Rows - 1
					If .get_TextMatrix(llngRow, 0) = astrGru And .get_TextMatrix(llngRow, 1) = astrSit And .get_TextMatrix(llngRow, 2) = astrFas And .get_TextMatrix(llngRow, 3) = astrTen And .get_TextMatrix(llngRow, 4) = astrTMd And .get_TextMatrix(llngRow, 5) = lstrMar Then
						llngPos = llngRow
						llngRow = .Rows - 1
						lblnAux = True
					End If
				Next llngRow
			End If
			If lblnAux = False Then
				.Rows = .Rows + 1
				llngPos = (.Rows - 1)
				.set_TextMatrix(llngPos, 0, astrGru) 'Grupo
				.set_TextMatrix(llngPos, 1, astrSit) 'Situacao
				.set_TextMatrix(llngPos, 2, astrFas) 'Fase
				.set_TextMatrix(llngPos, 3, astrTen) 'Tensao
				.set_TextMatrix(llngPos, 4, astrTMd) 'Tipo de Medidor
				.set_TextMatrix(llngPos, 5, lstrMar) 'Marca Mediror
			End If
			.set_TextMatrix(llngPos, 6, FormatNumber(CInt(IIf(.get_TextMatrix(llngPos, 6) = "", 0, .get_TextMatrix(llngPos, 6))) + 1, 0)) 'Qtde
		End With
	End Sub
	
	Private Function BuscaTipoMedidor(ByVal alngTipoMedidor As Integer) As String
		Dim lstrRet As String
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		lstrRet = ""
		lstrSql = ""
		lstrSql = lstrSql & "SELECT id, "
		lstrSql = lstrSql & "       descricao "
		lstrSql = lstrSql & "  FROM tipo_medidor "
		lstrSql = lstrSql & " WHERE id = " & alngTipoMedidor
		Call Conectar(lstrSql, lrstAux)
		With lrstAux
			If .State = ADODB.ObjectStateEnum.adStateOpen Then
				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"'
					lstrRet = UCase(Trim(IIf(IsDbNull(.Fields("descricao").value), "--", .Fields("descricao").value)))
				Else
					lstrRet = ""
				End If
				Call .Close()
			End If
		End With
		BuscaTipoMedidor = lstrRet
		'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
	End Function
End Class