Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Friend Class frmRelPee
	Inherits System.Windows.Forms.Form
	
	'UPGRADE_WARNING: Event chkFaturadoEm.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 chkFaturadoEm_CheckStateChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles chkFaturadoEm.CheckStateChanged
		Me.dtpFaturadoEm.Enabled = (Me.chkFaturadoEm.CheckState = System.Windows.Forms.CheckState.Checked)
		Me.chkSomenteMesAno.Enabled = (Me.chkFaturadoEm.CheckState = System.Windows.Forms.CheckState.Checked)
	End Sub
	
	'UPGRADE_WARNING: Event chkMesAno.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 chkMesAno_CheckStateChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles chkMesAno.CheckStateChanged
		Me.dtpMesAno.Enabled = (Me.chkMesAno.CheckState = System.Windows.Forms.CheckState.Checked)
	End Sub
	
	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 & "\PEE_" & VB6.Format(BuscaDataBD, "YYYYMMDDHHMMSS") & "." & Me.dialArqOpen.DefaultExt
		Me.dialArqOpen.Title = "Arquivo de exportação de P.E.E."
		Me.dialArqOpen.ShowDialog()
		Me.txtArquivo.Text = Me.dialArqOpen.FileName
	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
			Call LigDes(False)
			lstrSql = ""
			lstrSql = lstrSql & "SELECT "
			lstrSql = lstrSql & "       `bairro`, `identificacao`, `caderno`, `sequencia`, `classe`, `nome`, "
			lstrSql = lstrSql & "       `end_log`, `end_num`, `end_com`, `end_cid`, `end_dis`, "
			lstrSql = lstrSql & "       `medidor`, `regiao`, `fase`, `situacao`, `grupo`, `nis`, "
			lstrSql = lstrSql & "       `mes_01`, `mes_02`, `mes_03`, `mes_04`, `mes_05`, `mes_06`, `mes_07`, `mes_08`, `mes_09`, `mes_10`, `mes_11`, `mes_12`, "
			lstrSql = lstrSql & "       `vlr_01`, `vlr_02`, `vlr_03`, `vlr_04`, `vlr_05`, `vlr_06`, `vlr_07`, `vlr_08`, `vlr_09`, `vlr_10`, `vlr_11`, `vlr_12`, "
			lstrSql = lstrSql & "       `icms_01`, `icms_02`, `icms_03`, `icms_04`, `icms_05`, `icms_06`, `icms_07`, `icms_08`, `icms_09`, `icms_10`, `icms_11`, `icms_12`, "
			lstrSql = lstrSql & "       `pis_01`, `pis_02`, `pis_03`, `pis_04`, `pis_05`, `pis_06`, `pis_07`, `pis_08`, `pis_09`, `pis_10`, `pis_11`, `pis_12`, "
			lstrSql = lstrSql & "       `cofins_01`, `cofins_02`, `cofins_03`, `cofins_04`, `cofins_05`, `cofins_06`, `cofins_07`, `cofins_08`, `cofins_09`, `cofins_10`, `cofins_11`, `cofins_12`, "
			lstrSql = lstrSql & "       `ene_01`, `ene_02`, `ene_03`, `ene_04`, `ene_05`, `ene_06`, `ene_07`, `ene_08`, `ene_09`, `ene_10`, `ene_11`, `ene_12`, "
			lstrSql = lstrSql & "       `tra_01`, `tra_02`, `tra_03`, `tra_04`, `tra_05`, `tra_06`, `tra_07`, `tra_08`, `tra_09`, `tra_10`, `tra_11`, `tra_12`, "
			lstrSql = lstrSql & "       `dis_01`, `dis_02`, `dis_03`, `dis_04`, `dis_05`, `dis_06`, `dis_07`, `dis_08`, `dis_09`, `dis_10`, `dis_11`, `dis_12`, "
			lstrSql = lstrSql & "       `enc_01`, `enc_02`, `enc_03`, `enc_04`, `enc_05`, `enc_06`, `enc_07`, `enc_08`, `enc_09`, `enc_10`, `enc_11`, `enc_12`, "
			lstrSql = lstrSql & "       `per_01`, `per_02`, `per_03`, `per_04`, `per_05`, `per_06`, `per_07`, `per_08`, `per_09`, `per_10`, `per_11`, `per_12`, "
			lstrSql = lstrSql & "       `tic_01`, `tic_02`, `tic_03`, `tic_04`, `tic_05`, `tic_06`, `tic_07`, `tic_08`, `tic_09`, `tic_10`, `tic_11`, `tic_12`, "
			lstrSql = lstrSql & "       `tpc_01`, `tpc_02`, `tpc_03`, `tpc_04`, `tpc_05`, `tpc_06`, `tpc_07`, `tpc_08`, `tpc_09`, `tpc_10`, `tpc_11`, `tpc_12` "
			lstrSql = lstrSql & "  FROM `temp_relpee` "
			lstrSql = lstrSql & " WHERE `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)
						lintARQ = FreeFile
						FileOpen(lintARQ, Me.txtArquivo.Text, OpenMode.Output)
						lstrLin = ""
						For llngCol = 0 To .Fields.Count - 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"'
							lstrLin = lstrLin & IIf(IsDbNull(.Fields(llngCol).Name), "", .Fields(llngCol).Name) & ";"
						Next llngCol
						PrintLine(lintARQ, lstrLin)
						While Not .EOF
							lstrLin = ""
							For llngCol = 0 To .Fields.Count - 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"'
								lstrLin = lstrLin & IIf(IsDbNull(.Fields(llngCol).value), "", .Fields(llngCol).value) & ";"
							Next llngCol
							PrintLine(lintARQ, lstrLin)
							Call .MoveNext()
							Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
						End While
						FileClose(lintARQ)
					End If
					Call .Close()
				End If
			End With
			Call LigDes(True)
			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
	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 cmdImprimir_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdImprimir.Click
		Call LigDes(False)
		Call ImprimirPee()
		Call LigDes(True)
	End Sub
	
	Private Sub cmdProcessar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdProcessar.Click
		Call LigDes(False)
		Call Executar("DELETE FROM `temp_relpee` WHERE `nomemaquina`   = '" & gstrNmEstacao & "' ")
		Call EncheGrade()
		Call LigDes(True)
	End Sub
	
	Private Sub frmRelPee_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		Dim lstrSql As String
		Call CenterForm(Me)
		Call CarregaRotas()
		Call CarregaClasses((Me.lsvClasses))
		Me.dtpMesAno.value = BuscaDataBD
		'UPGRADE_WARNING: Couldn't resolve default property of object Me.dtpMesAno.value. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		Me.dtpFaturadoEm.value = Me.dtpMesAno.value
		Me.dtpMesAno.Enabled = False
		Me.dtpFaturadoEm.Enabled = False
		lstrSql = ""
		lstrSql = lstrSql & "CREATE TABLE IF NOT EXISTS `temp_relpee` (" & vbCrLf
		lstrSql = lstrSql & "  `nomemaquina`   varchar(255) NOT NULL DEFAULT ''," & vbCrLf
		lstrSql = lstrSql & "  `bairro`        varchar(60) DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `identificacao` varchar(13) DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `caderno`       varchar(4) DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `sequencia`     varchar(4) DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `classe`        varchar(60) DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `nome`          varchar(85) DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `end_log`       varchar(60) DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `end_num`       varchar(10) DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `end_com`       varchar(100) DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `end_cid`       varchar(60) DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `end_dis`       varchar(60) DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `medidor`       varchar(11) DEFAULT ''," & vbCrLf
		lstrSql = lstrSql & "  `regiao`        varchar(10) DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `fase`          varchar(10) DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `situacao`      varchar(10) DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `grupo`         char(2) DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `nis`           varchar(15) DEFAULT NULL," & vbCrLf
		'
		lstrSql = lstrSql & "  `mes_01`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `mes_02`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `mes_03`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `mes_04`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `mes_05`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `mes_06`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `mes_07`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `mes_08`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `mes_09`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `mes_10`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `mes_11`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `mes_12`        double DEFAULT NULL," & vbCrLf
		'
		lstrSql = lstrSql & "  `vlr_01`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `vlr_02`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `vlr_03`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `vlr_04`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `vlr_05`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `vlr_06`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `vlr_07`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `vlr_08`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `vlr_09`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `vlr_10`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `vlr_11`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `vlr_12`        double DEFAULT NULL," & vbCrLf
		'
		lstrSql = lstrSql & "  `icms_01`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `icms_02`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `icms_03`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `icms_04`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `icms_05`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `icms_06`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `icms_07`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `icms_08`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `icms_09`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `icms_10`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `icms_11`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `icms_12`        double DEFAULT NULL," & vbCrLf
		'
		lstrSql = lstrSql & "  `pis_01`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `pis_02`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `pis_03`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `pis_04`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `pis_05`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `pis_06`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `pis_07`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `pis_08`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `pis_09`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `pis_10`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `pis_11`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `pis_12`        double DEFAULT NULL," & vbCrLf
		'
		lstrSql = lstrSql & "  `cofins_01`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `cofins_02`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `cofins_03`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `cofins_04`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `cofins_05`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `cofins_06`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `cofins_07`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `cofins_08`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `cofins_09`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `cofins_10`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `cofins_11`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `cofins_12`        double DEFAULT NULL," & vbCrLf
		'
		lstrSql = lstrSql & "  `ene_01`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `ene_02`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `ene_03`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `ene_04`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `ene_05`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `ene_06`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `ene_07`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `ene_08`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `ene_09`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `ene_10`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `ene_11`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `ene_12`        double DEFAULT NULL," & vbCrLf
		'
		lstrSql = lstrSql & "  `tra_01`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `tra_02`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `tra_03`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `tra_04`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `tra_05`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `tra_06`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `tra_07`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `tra_08`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `tra_09`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `tra_10`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `tra_11`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `tra_12`        double DEFAULT NULL," & vbCrLf
		'
		lstrSql = lstrSql & "  `dis_01`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `dis_02`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `dis_03`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `dis_04`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `dis_05`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `dis_06`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `dis_07`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `dis_08`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `dis_09`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `dis_10`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `dis_11`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `dis_12`        double DEFAULT NULL," & vbCrLf
		'
		lstrSql = lstrSql & "  `enc_01`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `enc_02`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `enc_03`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `enc_04`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `enc_05`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `enc_06`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `enc_07`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `enc_08`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `enc_09`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `enc_10`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `enc_11`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `enc_12`        double DEFAULT NULL," & vbCrLf
		'
		lstrSql = lstrSql & "  `per_01`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `per_02`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `per_03`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `per_04`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `per_05`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `per_06`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `per_07`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `per_08`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `per_09`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `per_10`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `per_11`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `per_12`        double DEFAULT NULL," & vbCrLf
		'
		lstrSql = lstrSql & "  `tic_01`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `tic_02`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `tic_03`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `tic_04`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `tic_05`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `tic_06`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `tic_07`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `tic_08`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `tic_09`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `tic_10`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `tic_11`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `tic_12`        double DEFAULT NULL," & vbCrLf
		'
		lstrSql = lstrSql & "  `tpc_01`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `tpc_02`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `tpc_03`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `tpc_04`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `tpc_05`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `tpc_06`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `tpc_07`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `tpc_08`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `tpc_09`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `tpc_10`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `tpc_11`        double DEFAULT NULL," & vbCrLf
		lstrSql = lstrSql & "  `tpc_12`        double DEFAULT NULL," & vbCrLf
		'
		lstrSql = lstrSql & "  PRIMARY KEY (`nomemaquina`,`identificacao`)," & vbCrLf
		lstrSql = lstrSql & "  KEY `keyrota` (`caderno`)" & vbCrLf
		lstrSql = lstrSql & ") ENGINE=InnoDB DEFAULT CHARSET=latin1"
		Call Executar(lstrSql)
	End Sub
	
	Private Sub EncheGrade()
		Dim lstrZona As String
		Dim lstrLivro As String
		Dim lstrConta As String
		Dim lstrRota As String
		Dim lstrRegiao As String
		Dim lstrFase As String
		Dim lstrSituacao As String
		Dim lstrGrupo As String
		Dim lstrClas As String
		Dim lstrNome As String
		Dim lstrEndCid As String
		Dim lstrEndDis As String
		Dim lstrEndBai As String
		Dim lstrEndLog As String
		Dim lstrEndNum As String
		Dim lstrEndCom As String
		Dim lstrMedidor As String
		Dim llngConsId As Integer
		Dim llngUnidId As Integer
		Dim ldblNumNf As Double
		Dim llngMesano As Integer
		Dim ldteLeitura As Date
		Dim lstrGruTar As String
		Dim lstrClasFat As String
		Dim lstrItens As String
		Dim lstrNrCpf As String
		Dim lstrNrNis As String
		'UPGRADE_WARNING: Lower bound of array ldblkWhFat was changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="0F1C9BE1-AF9D-476E-83B1-17D43BECFF20"'
		Dim ldblkWhFat(12) As Double
		'UPGRADE_WARNING: Lower bound of array ldblVlrkWhFat was changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="0F1C9BE1-AF9D-476E-83B1-17D43BECFF20"'
		Dim ldblVlrkWhFat(12) As Double
		'UPGRADE_WARNING: Lower bound of array ldblIcms was changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="0F1C9BE1-AF9D-476E-83B1-17D43BECFF20"'
		Dim ldblIcms(12) As Double
		'UPGRADE_WARNING: Lower bound of array ldblPis was changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="0F1C9BE1-AF9D-476E-83B1-17D43BECFF20"'
		Dim ldblPis(12) As Double
		'UPGRADE_WARNING: Lower bound of array ldblCofins was changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="0F1C9BE1-AF9D-476E-83B1-17D43BECFF20"'
		Dim ldblCofins(12) As Double
		'UPGRADE_WARNING: Lower bound of array ldblEnergia was changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="0F1C9BE1-AF9D-476E-83B1-17D43BECFF20"'
		Dim ldblEnergia(12) As Double
		'UPGRADE_WARNING: Lower bound of array ldblTransimissao was changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="0F1C9BE1-AF9D-476E-83B1-17D43BECFF20"'
		Dim ldblTransimissao(12) As Double
		'UPGRADE_WARNING: Lower bound of array ldblDistribuicao was changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="0F1C9BE1-AF9D-476E-83B1-17D43BECFF20"'
		Dim ldblDistribuicao(12) As Double
		'UPGRADE_WARNING: Lower bound of array ldblEncSet was changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="0F1C9BE1-AF9D-476E-83B1-17D43BECFF20"'
		Dim ldblEncSet(12) As Double
		'UPGRADE_WARNING: Lower bound of array ldblPerdas was changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="0F1C9BE1-AF9D-476E-83B1-17D43BECFF20"'
		Dim ldblPerdas(12) As Double
		'UPGRADE_WARNING: Lower bound of array ldblTriIcms was changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="0F1C9BE1-AF9D-476E-83B1-17D43BECFF20"'
		Dim ldblTriIcms(12) As Double
		'UPGRADE_WARNING: Lower bound of array ldblTriPiCo was changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="0F1C9BE1-AF9D-476E-83B1-17D43BECFF20"'
		Dim ldblTriPiCo(12) As Double
		Dim lintAux As Short
		Dim lblnContinua As Boolean
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		Dim lrstFat As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		lrstFat = New ADODB.Recordset
		'
		lstrSql = ""
		If Me.chkGrupoB.CheckState = System.Windows.Forms.CheckState.Checked Then
			lstrSql = lstrSql & "SELECT " & vbCrLf
			lstrSql = lstrSql & "       `cons_id`, " & vbCrLf
			lstrSql = lstrSql & "       `unid_id`, " & vbCrLf
			lstrSql = lstrSql & "       `zona`, " & vbCrLf
			lstrSql = lstrSql & "       `livro`, " & vbCrLf
			lstrSql = lstrSql & "       `conta`, " & vbCrLf
			lstrSql = lstrSql & "       `rota`, " & vbCrLf
			lstrSql = lstrSql & "       `regiao`, " & vbCrLf
			lstrSql = lstrSql & "       `fase`, " & vbCrLf
			lstrSql = lstrSql & "       `situacao`, " & vbCrLf
			lstrSql = lstrSql & "       `grupo`, " & vbCrLf
			lstrSql = lstrSql & "       `clas`, " & vbCrLf
			lstrSql = lstrSql & "       `nome`, " & vbCrLf
			lstrSql = lstrSql & "       `cidade`, " & vbCrLf
			lstrSql = lstrSql & "       `distrito`, " & vbCrLf
			lstrSql = lstrSql & "       `bairro`, " & vbCrLf
			lstrSql = lstrSql & "       `end`, " & vbCrLf
			lstrSql = lstrSql & "       `numero`, " & vbCrLf
			lstrSql = lstrSql & "       `complement`, " & vbCrLf
			lstrSql = lstrSql & "       `num` AS `medidor`, " & vbCrLf
			lstrSql = lstrSql & "       `cpf_cnpj` " & vbCrLf
			lstrSql = lstrSql & "  FROM `unidb` " & vbCrLf
			lstrSql = lstrSql & " WHERE 1 = 1 " & vbCrLf
			If Me.chkLigados.CheckState = System.Windows.Forms.CheckState.Checked Or Me.chkDesligados.CheckState = System.Windows.Forms.CheckState.Checked Or Me.chkCortados.CheckState = System.Windows.Forms.CheckState.Checked Or Me.chkDesativados.CheckState = System.Windows.Forms.CheckState.Checked Then
				lstrSql = lstrSql & "  AND `situacao` IN("
				If Me.chkLigados.CheckState = System.Windows.Forms.CheckState.Checked Then
					lstrSql = lstrSql & "'L',"
				End If
				If Me.chkDesligados.CheckState = System.Windows.Forms.CheckState.Checked Then
					lstrSql = lstrSql & "'D',"
				End If
				If Me.chkCortados.CheckState = System.Windows.Forms.CheckState.Checked Then
					lstrSql = lstrSql & "'C',"
				End If
				If Me.chkDesativados.CheckState = System.Windows.Forms.CheckState.Checked Then
					lstrSql = lstrSql & "'I',"
				End If
				lstrSql = VB.Left(lstrSql, Len(lstrSql) - 1) & ")" & vbCrLf
			End If
			lstrSql = lstrSql & MontaFiltroRotas & vbCrLf
			lstrSql = lstrSql & MontaFiltroClasse & vbCrLf
		End If
		If Me.chkGrupoB.CheckState = System.Windows.Forms.CheckState.Checked And Me.chkGrupoA.CheckState = System.Windows.Forms.CheckState.Checked Then
			lstrSql = lstrSql & "UNION " & vbCrLf
		End If
		If Me.chkGrupoA.CheckState = System.Windows.Forms.CheckState.Checked Then
			lstrSql = lstrSql & "SELECT " & vbCrLf
			lstrSql = lstrSql & "       `cons_id`, " & vbCrLf
			lstrSql = lstrSql & "       `unid_id`, " & vbCrLf
			lstrSql = lstrSql & "       `zona`, " & vbCrLf
			lstrSql = lstrSql & "       `livro`, " & vbCrLf
			lstrSql = lstrSql & "       `conta`, " & vbCrLf
			lstrSql = lstrSql & "       `rota`, " & vbCrLf
			lstrSql = lstrSql & "       `regiao`, " & vbCrLf
			lstrSql = lstrSql & "       `fase`, " & vbCrLf
			lstrSql = lstrSql & "       `situacao`, " & vbCrLf
			lstrSql = lstrSql & "       `grupo`, " & vbCrLf
			lstrSql = lstrSql & "       `clas`, " & vbCrLf
			lstrSql = lstrSql & "       `nome`, " & vbCrLf
			lstrSql = lstrSql & "       `cidade`, " & vbCrLf
			lstrSql = lstrSql & "       `distrito`, " & vbCrLf
			lstrSql = lstrSql & "       `bairro`, " & vbCrLf
			lstrSql = lstrSql & "       `end`, " & vbCrLf
			lstrSql = lstrSql & "       `numero`, " & vbCrLf
			lstrSql = lstrSql & "       `complement`, " & vbCrLf
			lstrSql = lstrSql & "       `eletr` AS `medidor`, " & vbCrLf
			lstrSql = lstrSql & "       `cpf_cnpj` " & vbCrLf
			lstrSql = lstrSql & "  FROM `unida` " & vbCrLf
			lstrSql = lstrSql & " WHERE 1 = 1 " & vbCrLf
			If Me.chkLigados.CheckState = System.Windows.Forms.CheckState.Checked Or Me.chkDesligados.CheckState = System.Windows.Forms.CheckState.Checked Or Me.chkCortados.CheckState = System.Windows.Forms.CheckState.Checked Or Me.chkDesativados.CheckState = System.Windows.Forms.CheckState.Checked Then
				lstrSql = lstrSql & "  AND `situacao` IN("
				If Me.chkLigados.CheckState = System.Windows.Forms.CheckState.Checked Then
					lstrSql = lstrSql & "'L',"
				End If
				If Me.chkDesligados.CheckState = System.Windows.Forms.CheckState.Checked Then
					lstrSql = lstrSql & "'D',"
				End If
				If Me.chkCortados.CheckState = System.Windows.Forms.CheckState.Checked Then
					lstrSql = lstrSql & "'C',"
				End If
				If Me.chkDesativados.CheckState = System.Windows.Forms.CheckState.Checked Then
					lstrSql = lstrSql & "'I',"
				End If
				lstrSql = VB.Left(lstrSql, Len(lstrSql) - 1) & ")" & vbCrLf
			End If
			lstrSql = lstrSql & MontaFiltroRotas & vbCrLf
			lstrSql = lstrSql & MontaFiltroClasse & vbCrLf
		End If
		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
						'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), "", .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"'
						lstrRegiao = Trim(IIf(IsDbNull(.Fields("regiao").value), "U", .Fields("regiao").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))
						'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), "L", .Fields("situacao").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 = Trim(IIf(IsDbNull(.Fields("grupo").value), "B", .Fields("grupo").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"'
						lstrClas = Trim(IIf(IsDbNull(.Fields("clas").value), "0000", .Fields("clas").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"'
						lstrEndCid = Trim(IIf(IsDbNull(.Fields("cidade").value), "", .Fields("cidade").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"'
						lstrEndDis = Trim(IIf(IsDbNull(.Fields("distrito").value), "", .Fields("distrito").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"'
						lstrEndBai = 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"'
						lstrEndLog = 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"'
						lstrEndNum = 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"'
						lstrEndCom = 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"'
						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"'
						llngConsId = CInt(IIf(IsDbNull(.Fields("cons_id").value), 0, .Fields("cons_id").value))
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						llngUnidId = CInt(IIf(IsDbNull(.Fields("unid_id").value), 0, .Fields("unid_id").value))
						lblnContinua = True
						If Me.chkEmDia.CheckState = System.Windows.Forms.CheckState.Checked Then
							lblnContinua = (Pedente(llngConsId) = False) 'se ele for false é por que não tem nada em aberto, dai coloca na grade
						End If
						'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"'
						lstrNrCpf = Trim(IIf(IsDbNull(.Fields("cpf_cnpj").value), "", .Fields("cpf_cnpj").value))
						If VB.Left(lstrGrupo, 1) = "B" Then
							lstrNrNis = BuscaNis(lstrZona, lstrLivro, lstrConta, llngUnidId, lstrNrCpf)
						Else
							lstrNrNis = ""
						End If
						If Me.chkFaturadoEm.CheckState = System.Windows.Forms.CheckState.Checked And Me.chkSomenteMesAno.CheckState = System.Windows.Forms.CheckState.Unchecked Then
							lblnContinua = VerificaFaturamento(lstrGrupo, lstrZona, lstrLivro, lstrConta, llngUnidId)
						End If
						If lblnContinua Then
							lstrSql = ""
							lstrSql = lstrSql & "SELECT "
							lstrSql = lstrSql & "       nf.numnf, "
							lstrSql = lstrSql & "       nf.mesano_ref, "
							lstrSql = lstrSql & "       nf.cons_id, "
							lstrSql = lstrSql & "       nf.unid_id, "
							lstrSql = lstrSql & "       nf.zona, "
							lstrSql = lstrSql & "       nf.livro, "
							lstrSql = lstrSql & "       nf.conta, "
							lstrSql = lstrSql & "       nf.dataleit, "
							lstrSql = lstrSql & "       nf.ta, "
							lstrSql = lstrSql & "       nf.codclas, "
							lstrSql = lstrSql & "       nf.itensserv "
							lstrSql = lstrSql & "  FROM nf "
							lstrSql = lstrSql & " WHERE nf.zona    = '" & lstrZona & "' "
							lstrSql = lstrSql & "   AND nf.livro   = '" & lstrLivro & "' "
							lstrSql = lstrSql & "   AND nf.conta   = '" & lstrConta & "' "
							lstrSql = lstrSql & "   AND nf.cons_id =  " & llngConsId & " "
							lstrSql = lstrSql & "   AND nf.unid_id =  " & llngUnidId & " "
							lstrSql = lstrSql & "   AND nf.grupo   = '" & VB.Left(lstrGrupo, 1) & "' "
							If Me.chkMesAno.CheckState = System.Windows.Forms.CheckState.Checked Then
								lstrSql = lstrSql & "   AND nf.mesano_ref <= " & CInt(VB6.Format(Me.dtpMesAno.value, "yyyyMM"))
							ElseIf Me.chkSomenteMesAno.CheckState = System.Windows.Forms.CheckState.Checked Then 
								lstrSql = lstrSql & "   AND nf.mesano_ref = " & CInt(VB6.Format(Me.dtpFaturadoEm.value, "yyyyMM"))
							End If
							lstrSql = lstrSql & " ORDER BY nf.mesano_ref DESC LIMIT 0, 12 "
							Call Conectar(lstrSql, lrstFat)
							With lrstFat
								If .State = ADODB.ObjectStateEnum.adStateOpen Then
									If .RecordCount > 0 Then
										lintAux = 1
										While Not .EOF
											'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
											ldblNumNf = CDbl(IIf(IsDbNull(.Fields("numnf").value), 0, .Fields("numnf").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"'
											llngMesano = CInt(IIf(IsDbNull(.Fields("mesano_ref").value), 0, .Fields("mesano_ref").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"'
											ldteLeitura = CDate(IIf(IsDbNull(.Fields("dataleit").value), "1900-01-01", .Fields("dataleit").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"'
											lstrGruTar = Trim(IIf(IsDbNull(.Fields("ta").value), "01B100", .Fields("ta").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"'
											lstrClasFat = Trim(IIf(IsDbNull(.Fields("codclas").value), "9101", .Fields("codclas").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"'
											lstrItens = Trim(IIf(IsDbNull(.Fields("itensserv").value), "", .Fields("itensserv").value))
											If VB.Left(lstrGrupo, 1) = "B" Then
												ldblkWhFat(lintAux) = BuscaVlrItem(lstrItens, "030201009")
												ldblVlrkWhFat(lintAux) = BuscaVlrItem(lstrItens, "030202009")
											Else
												If Mid(lstrGruTar, 3, 2) = "A4" Or Mid(lstrGruTar, 3, 2) = "AS" Then
													ldblkWhFat(lintAux) = BuscaVlrItem(lstrItens, "0302010009")
													ldblVlrkWhFat(lintAux) = BuscaVlrItem(lstrItens, "0302020009")
												Else
													ldblkWhFat(lintAux) = BuscaVlrItem(lstrItens, "0302010010") + BuscaVlrItem(lstrItens, "0302010011")
													ldblVlrkWhFat(lintAux) = BuscaVlrItem(lstrItens, "0302020010") + BuscaVlrItem(lstrItens, "0302020011")
												End If
											End If
											ldblIcms(lintAux) = BuscaVlrItem(lstrItens, "0302030009") + BuscaVlrItem(lstrItens, "0302030010")
											ldblPis(lintAux) = BuscaVlrItem(lstrItens, "0302020029")
											ldblCofins(lintAux) = BuscaVlrItem(lstrItens, "0302020030")
											Call ComponentesFatura(llngMesano, lstrGruTar, lstrClasFat, lstrItens, ldteLeitura, ldblEnergia(lintAux), ldblTransimissao(lintAux), ldblDistribuicao(lintAux), ldblEncSet(lintAux), ldblPerdas(lintAux), ldblTriIcms(lintAux), ldblTriPiCo(lintAux))
											lintAux = lintAux + 1
											Call .MoveNext()
										End While
									End If
									Call .Close()
								End If
							End With
							lstrSql = ""
							lstrSql = lstrSql & "INSERT INTO `temp_relpee` SET " & vbCrLf
							lstrSql = lstrSql & "  `nomemaquina`   = '" & gstrNmEstacao & "'," & vbCrLf
							lstrSql = lstrSql & "  `bairro`        = '" & Replace(lstrEndBai, "'", "\'") & "'," & vbCrLf
							lstrSql = lstrSql & "  `identificacao` = '" & lstrZona & "." & lstrLivro & "." & lstrConta & "'," & vbCrLf
							lstrSql = lstrSql & "  `caderno`       = '" & VB.Left(lstrRota, 4) & "'," & vbCrLf
							lstrSql = lstrSql & "  `sequencia`     = '" & VB.Right(lstrRota, 4) & "'," & vbCrLf
							lstrSql = lstrSql & "  `classe`        = '" & Replace(BuscaClasse(lstrClas), "'", "\'") & "'," & vbCrLf
							lstrSql = lstrSql & "  `nome`          = '" & Replace(lstrNome, "'", "\'") & "'," & vbCrLf
							lstrSql = lstrSql & "  `end_log`       = '" & Replace(lstrEndLog, "'", "\'") & "'," & vbCrLf
							lstrSql = lstrSql & "  `end_num`       = '" & Replace(lstrEndNum, "'", "\'") & "'," & vbCrLf
							lstrSql = lstrSql & "  `end_com`       = '" & Replace(lstrEndCom, "'", "\'") & "'," & vbCrLf
							lstrSql = lstrSql & "  `end_cid`       = '" & Replace(lstrEndCid, "'", "\'") & "'," & vbCrLf
							lstrSql = lstrSql & "  `end_dis`       = '" & Replace(lstrEndDis, "'", "\'") & "'," & vbCrLf
							lstrSql = lstrSql & "  `medidor`       = '" & lstrMedidor & "'," & vbCrLf
							lstrSql = lstrSql & "  `regiao`        = '" & IIf(lstrRegiao = "R", "Rural", "Urbana") & "'," & vbCrLf
							lstrSql = lstrSql & "  `fase`          = '" & IIf(lstrFase = "M", "Monofásico", IIf(lstrFase = "B", "Bifásico", "Trifásico")) & "'," & vbCrLf
							lstrSql = lstrSql & "  `situacao`      = '" & IIf(lstrSituacao = "L", "Ligado", IIf(lstrSituacao = "D", "Desligado", IIf(lstrSituacao = "C", "Suspenso", IIf(lstrSituacao = "I", "Desativado", "Não Ligado")))) & "'," & vbCrLf
							lstrSql = lstrSql & "  `grupo`         = '" & lstrGrupo & "'," & vbCrLf
							For lintAux = 1 To 12
								lstrSql = lstrSql & "  `mes_" & VB6.Format(lintAux, "00") & "`    = " & Replace(CStr(ldblkWhFat(lintAux)), ",", ".") & "," & vbCrLf
								lstrSql = lstrSql & "  `vlr_" & VB6.Format(lintAux, "00") & "`    = " & Replace(CStr(ldblVlrkWhFat(lintAux)), ",", ".") & "," & vbCrLf
								lstrSql = lstrSql & "  `icms_" & VB6.Format(lintAux, "00") & "`   = " & Replace(CStr(ldblIcms(lintAux)), ",", ".") & "," & vbCrLf
								lstrSql = lstrSql & "  `pis_" & VB6.Format(lintAux, "00") & "`    = " & Replace(CStr(ldblPis(lintAux)), ",", ".") & "," & vbCrLf
								lstrSql = lstrSql & "  `cofins_" & VB6.Format(lintAux, "00") & "` = " & Replace(CStr(ldblCofins(lintAux)), ",", ".") & "," & vbCrLf
								lstrSql = lstrSql & "  `ene_" & VB6.Format(lintAux, "00") & "`    = " & Replace(CStr(ldblEnergia(lintAux)), ",", ".") & "," & vbCrLf
								lstrSql = lstrSql & "  `tra_" & VB6.Format(lintAux, "00") & "`    = " & Replace(CStr(ldblTransimissao(lintAux)), ",", ".") & "," & vbCrLf
								lstrSql = lstrSql & "  `dis_" & VB6.Format(lintAux, "00") & "`    = " & Replace(CStr(ldblDistribuicao(lintAux)), ",", ".") & "," & vbCrLf
								lstrSql = lstrSql & "  `enc_" & VB6.Format(lintAux, "00") & "`    = " & Replace(CStr(ldblEncSet(lintAux)), ",", ".") & "," & vbCrLf
								lstrSql = lstrSql & "  `per_" & VB6.Format(lintAux, "00") & "`    = " & Replace(CStr(ldblPerdas(lintAux)), ",", ".") & "," & vbCrLf
								lstrSql = lstrSql & "  `tic_" & VB6.Format(lintAux, "00") & "`    = " & Replace(CStr(ldblTriIcms(lintAux)), ",", ".") & "," & vbCrLf
								lstrSql = lstrSql & "  `tpc_" & VB6.Format(lintAux, "00") & "`    = " & Replace(CStr(ldblTriPiCo(lintAux)), ",", ".") & "," & vbCrLf
							Next lintAux
							lstrSql = lstrSql & "  `nis`           = '" & lstrNrNis & "';" & vbCrLf
							Call Executar(lstrSql)
						End If
						Call .MoveNext()
						Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
					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
		'UPGRADE_NOTE: Object lrstFat 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"'
		lrstFat = Nothing
	End Sub
	
	Private Sub ComponentesFatura(ByVal alngMesAno As Integer, ByVal astrGruTar As String, ByVal astrClasse As String, ByVal astrItens As String, ByVal adteLeitura As Date, ByRef adblVlrEnergia As Double, ByRef adblVlrTransmissao As Double, ByRef adblVlrDistribuicao As Double, ByRef adblVlrEncSetoriais As Double, ByRef adblVlrPerdas As Double, ByRef adblVlrTributosIcms As Double, ByRef adblVlrTributosPisCof As Double)
		
		
		'Revisado OK
		Dim lstrGruTar As String
		Dim lstrItens As String
		Dim lstrRet As String
		Dim lstrCodigo As String
		Dim lstrMesAnoAux As String
		Dim lstrMesAnoOld As String
		'
		Dim ldblPerEnergia As Double 'Percentual
		Dim ldblPerTransmissao As Double 'Percentual
		Dim ldblPerDistribuicao As Double 'Percentual
		Dim ldblPerEncSetoriais As Double 'Percentual
		Dim ldblPerPerdas As Double 'Percentual
		Dim ldblPerTributosIcms As Double 'Percentual
		Dim ldblPerTributosPisCof As Double 'Percentual
		'
		Dim ldblVlrEnergia As Double 'valor
		Dim ldblVlrTransmissao As Double 'valor
		Dim ldblVlrDistribuicao As Double 'valor
		Dim ldblVlrEncSetoriais As Double 'valor
		Dim ldblVlrPerdas As Double 'valor
		Dim ldblVlrTributosIcms As Double 'valor
		Dim ldblVlrTributosPisCof As Double 'valor
		'
		Dim ldblItenValor As Double
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		ldblVlrEnergia = 0
		ldblVlrTransmissao = 0
		ldblVlrDistribuicao = 0
		ldblVlrEncSetoriais = 0
		ldblVlrPerdas = 0
		ldblVlrTributosIcms = 0
		ldblVlrTributosPisCof = 0
		'
		Call GetTarifasComposicao(adteLeitura, ldblPerEnergia, ldblPerTransmissao, ldblPerDistribuicao, ldblPerEncSetoriais, ldblPerPerdas, ldblPerTributosIcms, ldblPerTributosPisCof)
		'
		lstrMesAnoAux = ""
		lstrMesAnoOld = ""
		lstrRet = ""
		lstrGruTar = astrGruTar
		lstrItens = astrItens
		If CDbl(VB.Left(astrClasse, 1)) = 9 And CDbl(VB.Right(astrClasse, 1)) <> 1 Then
			lstrGruTar = VB.Left(lstrGruTar, 5) & 2
		End If
		lstrSql = ""
		lstrSql = lstrSql & "  SELECT * "
		lstrSql = lstrSql & "    FROM `composicao` "
		lstrSql = lstrSql & "   WHERE `referencia` <= " & alngMesAno & " "
		lstrSql = lstrSql & "     AND `ta` = '" & lstrGruTar & "' "
		lstrSql = lstrSql & "ORDER BY `referencia` DESC, `codigo` ASC "
		Call Conectar(lstrSql, lrstAux)
		With lrstAux
			If .State = ADODB.ObjectStateEnum.adStateOpen Then
				If .RecordCount > 0 Then
					While Not .EOF
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						lstrMesAnoAux = IIf(IsDbNull(.Fields("mesano").value), "", .Fields("mesano").value)
						If Len(Trim(lstrMesAnoOld)) = 0 Then
							lstrMesAnoOld = lstrMesAnoAux
						End If
						If lstrMesAnoAux = lstrMesAnoOld 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"'
							lstrCodigo = Trim(IIf(IsDbNull(.Fields("codigo").value), "", .Fields("codigo").value))
							'
							'Calculo do rateio
							'
							ldblItenValor = BuscaVlrItem(lstrItens, lstrCodigo)
							If ldblItenValor <> 0 Then
								ldblVlrEnergia = ldblVlrEnergia + (ldblItenValor * (ldblPerEnergia / 100))
								ldblVlrTransmissao = ldblVlrTransmissao + (ldblItenValor * (ldblPerTransmissao / 100))
								ldblVlrDistribuicao = ldblVlrDistribuicao + (ldblItenValor * (ldblPerDistribuicao / 100))
								ldblVlrEncSetoriais = ldblVlrEncSetoriais + (ldblItenValor * (ldblPerEncSetoriais / 100))
								ldblVlrPerdas = ldblVlrPerdas + (ldblItenValor * (ldblPerPerdas / 100))
								ldblVlrTributosIcms = ldblVlrTributosIcms + (ldblItenValor * (ldblPerTributosIcms / 100))
								ldblVlrTributosPisCof = ldblVlrTributosPisCof + (ldblItenValor * (ldblPerTributosPisCof / 100))
							End If
							Call .MoveNext()
						Else
							Call .MoveLast()
							Call .MoveNext()
						End If
					End While
				End If
				Call .Close()
			End If
		End With
		If (ldblVlrEnergia + ldblVlrTransmissao + ldblVlrDistribuicao + ldblVlrEncSetoriais + ldblVlrPerdas + ldblVlrTributosIcms + ldblVlrTributosPisCof) <> 0 Then
			adblVlrEnergia = ldblVlrEnergia
			adblVlrTransmissao = ldblVlrTransmissao
			adblVlrDistribuicao = ldblVlrDistribuicao
			adblVlrEncSetoriais = ldblVlrEncSetoriais
			adblVlrPerdas = ldblVlrPerdas
			adblVlrTributosIcms = ldblVlrTributosIcms
			adblVlrTributosPisCof = ldblVlrTributosPisCof
		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
	End Sub
	
	Private Sub CarregaClasses(ByRef alsvAuxiliar As System.Windows.Forms.ListView)
		Dim lstrNome As String
		Dim lstrZona As String
		With Me.lsvClasses
			Call .Items.Clear()
			If .Columns.Count <= 1 Then
				Call .Columns.Add("", "Descrição", CInt(VB6.TwipsToPixelsX(5490)))
				Call .Columns.Add("", "Condição", CInt(VB6.TwipsToPixelsX(0)))
			End If
			Call .Items.Add("Marcar Todas")
			'UPGRADE_WARNING: Lower bound of collection Me.lsvClasses.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
			Call .Items.Item(.Items.Count).SubItems.Add(" = 0")
			Call .Items.Add("Residencial")
			'UPGRADE_WARNING: Lower bound of collection Me.lsvClasses.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
			Call .Items.Item(.Items.Count).SubItems.Add(" = 9101")
			Call .Items.Add("Industrial")
			'UPGRADE_WARNING: Lower bound of collection Me.lsvClasses.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
			Call .Items.Item(.Items.Count).SubItems.Add(" < 4")
			Call .Items.Add("Livre")
			'UPGRADE_WARNING: Lower bound of collection Me.lsvClasses.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
			Call .Items.Item(.Items.Count).SubItems.Add(" = 0100")
			Call .Items.Add("Comercial")
			'UPGRADE_WARNING: Lower bound of collection Me.lsvClasses.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
			Call .Items.Item(.Items.Count).SubItems.Add(" = 4")
			Call .Items.Add("Rural")
			'UPGRADE_WARNING: Lower bound of collection Me.lsvClasses.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
			Call .Items.Item(.Items.Count).SubItems.Add(" = 5")
			Call .Items.Add("Poder Público Municipal")
			'UPGRADE_WARNING: Lower bound of collection Me.lsvClasses.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
			Call .Items.Item(.Items.Count).SubItems.Add(" = 61")
			Call .Items.Add("Poder Público Estadual")
			'UPGRADE_WARNING: Lower bound of collection Me.lsvClasses.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
			Call .Items.Item(.Items.Count).SubItems.Add(" = 62")
			Call .Items.Add("Poder Público Federal")
			'UPGRADE_WARNING: Lower bound of collection Me.lsvClasses.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
			Call .Items.Item(.Items.Count).SubItems.Add(" = 63")
			Call .Items.Add("Iluminação Pública")
			'UPGRADE_WARNING: Lower bound of collection Me.lsvClasses.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
			Call .Items.Item(.Items.Count).SubItems.Add(" = 64")
			Call .Items.Add("Serviço Púplico")
			'UPGRADE_WARNING: Lower bound of collection Me.lsvClasses.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
			Call .Items.Item(.Items.Count).SubItems.Add(" = 7")
			Call .Items.Add("Consumo Próprio")
			'UPGRADE_WARNING: Lower bound of collection Me.lsvClasses.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
			Call .Items.Item(.Items.Count).SubItems.Add(" = 8")
			'UPGRADE_WARNING: Lower bound of collection Me.lsvClasses.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
			.Items.Item(1).Font = VB6.FontChangeBold(.Items.Item(1).Font, True)
			'UPGRADE_WARNING: Lower bound of collection Me.lsvClasses.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
			.Items.Item(1).Checked = True
			'UPGRADE_WARNING: Lower bound of collection Me.lsvClasses.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
			Call lsvClasses_ItemCheck(lsvClasses, New System.Windows.Forms.ItemCheckEventArgs(.Items.Item(1).Index, System.Windows.Forms.CheckState.Indeterminate, .Items.Item(1).Checked))
		End With
		System.Windows.Forms.Application.DoEvents()
	End Sub
	
	Private Sub frmRelPee_FormClosed(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
		Call Executar("DELETE FROM `temp_relpee` WHERE `nomemaquina`   = '" & gstrNmEstacao & "' ")
	End Sub
	
	Private Sub lsvRotas_ItemCheck(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.ItemCheckEventArgs) Handles lsvRotas.ItemCheck
		Dim aItem As System.Windows.Forms.ListViewItem = lsvRotas.Items(eventArgs.Index)
		Dim lintIdx As Short
		With Me.lsvRotas
			If aItem.Text = "Marcar Todas" Then
				For lintIdx = 1 To .Items.Count
					'UPGRADE_WARNING: Lower bound of collection Me.lsvRotas.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
					.Items.Item(lintIdx).Checked = .Items.Item(1).Checked
				Next lintIdx
			Else
				If aItem.Checked = False Then
					'UPGRADE_WARNING: Lower bound of collection Me.lsvRotas.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
					.Items.Item(1).Checked = False
				End If
			End If
		End With
	End Sub
	
	Private Sub lsvClasses_ItemCheck(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.ItemCheckEventArgs) Handles lsvClasses.ItemCheck
		Dim aItem As System.Windows.Forms.ListViewItem = lsvClasses.Items(eventArgs.Index)
		Dim lintIdx As Short
		With Me.lsvClasses
			If aItem.Text = "Marcar Todas" Then
				For lintIdx = 1 To .Items.Count
					'UPGRADE_WARNING: Lower bound of collection Me.lsvClasses.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
					.Items.Item(lintIdx).Checked = .Items.Item(1).Checked
				Next lintIdx
			Else
				If aItem.Checked = False Then
					'UPGRADE_WARNING: Lower bound of collection Me.lsvClasses.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
					.Items.Item(1).Checked = False
				End If
			End If
		End With
	End Sub
	
	Private Sub CarregaRotas()
		Dim lstrRota As String
		Dim llngLigados As Integer
		Dim llngNaoLigados As Integer
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		With Me.lsvRotas
			Call .Items.Clear()
			Call .Columns.Clear()
			If .Columns.Count <= 1 Then
				Call .Columns.Add("", "Rota", CInt(VB6.TwipsToPixelsX(2100)))
				Call .Columns.Add("Ligados", CInt(VB6.TwipsToPixelsX(1700)), System.Windows.Forms.HorizontalAlignment.Right)
				Call .Columns.Add("Não Ligados", CInt(VB6.TwipsToPixelsX(1700)), System.Windows.Forms.HorizontalAlignment.Right)
			End If
		End With
		lstrSql = ""
		lstrSql = lstrSql & "SELECT LEFT(`unidb`.`rota`, 4) AS `rotaunid`, " & vbCrLf
		lstrSql = lstrSql & "       SUM(IF(`unidb`.`situacao` = 'L', 1, 0)) AS `ligados`, " & vbCrLf
		lstrSql = lstrSql & "       SUM(IF(`unidb`.`situacao` <> 'L', 1, 0)) AS `naoligados` " & vbCrLf
		lstrSql = lstrSql & "  FROM `unidb` " & vbCrLf
		lstrSql = lstrSql & " WHERE TRIM(`unidb`.`rota`) <> '' " & vbCrLf
		lstrSql = lstrSql & "   AND LEFT(`unidb`.`rota`, 4) <> '9999' " & vbCrLf
		lstrSql = lstrSql & " GROUP BY 1 " & vbCrLf
		lstrSql = lstrSql & "UNION " & vbCrLf
		lstrSql = lstrSql & "SELECT LEFT(`unida`.`rota`, 4) AS `rotaunid`, " & vbCrLf
		lstrSql = lstrSql & "       SUM(IF(`unida`.`situacao` = 'L', 1, 0)) AS `ligados`, " & vbCrLf
		lstrSql = lstrSql & "       SUM(IF(`unida`.`situacao` <> 'L', 1, 0)) AS `naoligados` " & vbCrLf
		lstrSql = lstrSql & "  FROM `unida` " & vbCrLf
		lstrSql = lstrSql & " WHERE TRIM(`unida`.`rota`) <> '' " & vbCrLf
		lstrSql = lstrSql & "   AND LEFT(`unida`.`rota`, 4) <> '9999' " & vbCrLf
		lstrSql = lstrSql & " GROUP BY 1 " & vbCrLf
		lstrSql = lstrSql & " ORDER BY 1 ASC"
		Call Conectar(lstrSql, lrstAux)
		' ----- Insere os dados na ListView de rotas
		With lrstAux
			If .State > 0 Then
				If .RecordCount > 0 Then
					With Me.lsvRotas
						Call .Items.Add("Marcar Todas")
						'UPGRADE_WARNING: Lower bound of collection Me.lsvRotas.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
						Call .Items.Item(.Items.Count).SubItems.Add("")
						'UPGRADE_WARNING: Lower bound of collection Me.lsvRotas.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
						Call .Items.Item(.Items.Count).SubItems.Add("")
					End With
					While Not .EOF
						'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
						lstrRota = VB6.Format(IIf(IsDbNull(.Fields("rotaunid").value), "0", .Fields("rotaunid").value), "0000")
						'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"'
						llngLigados = IIf(IsDbNull(.Fields("ligados").value), 0, .Fields("ligados").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"'
						llngNaoLigados = IIf(IsDbNull(.Fields("naoligados").value), 0, .Fields("naoligados").value)
						With Me.lsvRotas
							Call .Items.Add(lstrRota)
							'UPGRADE_WARNING: Lower bound of collection Me.lsvRotas.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
							Call .Items.Item(.Items.Count).SubItems.Add(CStr(llngLigados))
							'UPGRADE_WARNING: Lower bound of collection Me.lsvRotas.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
							Call .Items.Item(.Items.Count).SubItems.Add(CStr(llngNaoLigados))
						End With
						Call .MoveNext()
					End While
				End If
				Call .Close()
			End If
		End With
		'UPGRADE_WARNING: Lower bound of collection Me.lsvRotas.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
		Me.lsvRotas.Items.Item(1).Font = VB6.FontChangeBold(Me.lsvRotas.Items.Item(1).Font, True)
		'UPGRADE_WARNING: Lower bound of collection Me.lsvRotas.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
		Me.lsvRotas.Items.Item(1).Checked = True
		'UPGRADE_WARNING: Lower bound of collection Me.lsvRotas.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
		Call lsvRotas_ItemCheck(lsvRotas, New System.Windows.Forms.ItemCheckEventArgs(Me.lsvRotas.Items.Item(1).Index, System.Windows.Forms.CheckState.Indeterminate, Me.lsvRotas.Items.Item(1).Checked))
		'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
		System.Windows.Forms.Application.DoEvents()
	End Sub
	
	Private Function MontaFiltroRotas() As String
		Dim lstrAux As String
		Dim lintIdx As Short
		lstrAux = ""
		With Me.lsvRotas
			'UPGRADE_WARNING: Lower bound of collection Me.lsvRotas.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
			If .Items.Item(1).Checked = False Then
				For lintIdx = 2 To .Items.Count
					'UPGRADE_WARNING: Lower bound of collection Me.lsvRotas.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
					If .Items.Item(lintIdx).Checked = True Then
						'UPGRADE_WARNING: Lower bound of collection Me.lsvRotas.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
						lstrAux = lstrAux & "'" & .Items.Item(lintIdx).Text & "',"
					End If
				Next lintIdx
				If Len(Trim(lstrAux)) > 0 Then
					lstrAux = " (" & VB.Left(lstrAux, Len(lstrAux) - 1) & ") "
				End If
				If Len(Trim(lstrAux)) > 0 Then
					lstrAux = " AND LEFT(`rota`, 4) IN " & lstrAux & " "
				End If
			End If
		End With
		MontaFiltroRotas = lstrAux
	End Function
	
	Private Function MontaFiltroClasse() As String
		Dim lstrAux1 As String
		Dim lstrAux2 As String
		Dim lintIdx As Short
		lstrAux1 = ""
		lstrAux2 = ""
		With Me.lsvClasses
			'UPGRADE_WARNING: Lower bound of collection Me.lsvClasses.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
			If .Items.Item(1).Checked = False Then
				lstrAux1 = " AND ("
				For lintIdx = 2 To .Items.Count
					'UPGRADE_WARNING: Lower bound of collection Me.lsvClasses.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
					If .Items.Item(lintIdx).Checked = True Then
						'UPGRADE_WARNING: Lower bound of collection Me.lsvClasses.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
						'UPGRADE_WARNING: Lower bound of collection Me.lsvClasses.ListItems.Item() has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
						lstrAux1 = lstrAux1 & " LEFT(`clas`," & Len(Trim(Str_Retira(.Items.Item(lintIdx).SubItems(1).Text, "<="))) & ")" & .Items.Item(lintIdx).SubItems(1).Text & " OR "
						'UPGRADE_WARNING: Lower bound of collection Me.lsvClasses.ListItems has changed from 1 to 0. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A3B628A0-A810-4AE2-BFA2-9E7A29EB9AD0"'
						If Trim(.Items.Item(lintIdx).Text) = "Industrial" And .Items.Item(4).Checked = False Then
							lstrAux2 = " AND `clas` <> 0100 "
						Else
							lstrAux2 = ""
						End If
					End If
				Next lintIdx
				If lstrAux1 = " AND (" Then
					lstrAux1 = ""
					lstrAux2 = ""
				Else
					lstrAux1 = VB.Left(lstrAux1, Len(lstrAux1) - 3) & ") "
				End If
			End If
		End With
		MontaFiltroClasse = lstrAux1 & lstrAux2
	End Function
	
	Private Function BuscaClasse(ByVal astrClasse As String) As String
		Select Case CInt(VB.Left(astrClasse, 1))
			Case Is = 9
				BuscaClasse = "Residencial"
			Case Is < 4
				If astrClasse = "0100" Then
					BuscaClasse = "Livre"
				Else
					BuscaClasse = "Industrial"
				End If
			Case Is = 4
				BuscaClasse = "Comercial"
			Case Is = 5
				BuscaClasse = "Rural"
			Case Is = 6
				If CInt(VB.Left(astrClasse, 2)) = 61 Then
					BuscaClasse = "Poder Público Municipal"
				ElseIf CInt(VB.Left(astrClasse, 2)) = 62 Then 
					BuscaClasse = "Poder Público Estadual"
				ElseIf CInt(VB.Left(astrClasse, 2)) = 63 Then 
					BuscaClasse = "Poder Público Federal"
				ElseIf CInt(VB.Left(astrClasse, 2)) = 64 Then 
					BuscaClasse = "Iluminação Pública"
				End If
			Case Is = 7
				BuscaClasse = "Serviço Púplico"
			Case Is = 8
				BuscaClasse = "Consumo Próprio"
			Case Else
				BuscaClasse = "----------"
		End Select
	End Function
	
	Private Sub ImprimirPee()
		Dim lstrSql As String
		Dim llngRow As Integer
		Dim lstrAux As String
		Dim lstrCri As String
		'
		lstrCri = "{temp_relpee.nomemaquina} = '" & gstrNmEstacao & "' "
		lstrAux = " Grupo ("
		If Me.chkGrupoA.CheckState = System.Windows.Forms.CheckState.Checked Then
			lstrAux = lstrAux & "A"
		End If
		If Me.chkGrupoB.CheckState = 1 Then
			lstrAux = lstrAux & "B"
		End If
		lstrAux = lstrAux & ") - Situação ("
		If Me.chkLigados.CheckState = System.Windows.Forms.CheckState.Checked Then
			lstrAux = lstrAux & "L,"
		End If
		If Me.chkCortados.CheckState = System.Windows.Forms.CheckState.Checked Then
			lstrAux = lstrAux & "C,"
		End If
		If Me.chkDesligados.CheckState = System.Windows.Forms.CheckState.Checked Then
			lstrAux = lstrAux & "D,"
		End If
		If Me.chkDesativados.CheckState = System.Windows.Forms.CheckState.Checked Then
			lstrAux = lstrAux & "I,"
		End If
		lstrAux = VB.Left(lstrAux, Len(lstrAux) - 1) & ")"
		With Me.crpAux
			.ReportFileName = My.Application.Info.DirectoryPath & "\" & LeConfig("DIR_RPT") & "RelPee.rpt"
			.DiscardSavedData = True
			.WindowState = Crystal.WindowStateConstants.crptMaximized
			.WindowControlBox = True
			.WindowControls = True
			.WindowShowPrintSetupBtn = True
			.WindowShowCloseBtn = True
			.WindowShowSearchBtn = True
			.SelectionFormula = lstrCri
			.WindowTitle = "Relatório dos em Aberto"
			.Destination = Crystal.DestinationConstants.crptToWindow
			.set_Formulas(1, "fn_titulo = """ & lstrAux & """ ")
			.Action = 0
		End With
	End Sub
	
	Private Function BuscaNis(ByVal astrZona As String, ByVal astrLivro As String, ByVal astrConta As String, ByVal alngUnidId As Integer, ByVal astrCpf As String) As String
		Dim lstrRet As String
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		lstrRet = ""
		lstrSql = ""
		lstrSql = lstrSql & "SELECT " & vbCrLf
		lstrSql = lstrSql & "       `nis`.`cpf`, " & vbCrLf
		lstrSql = lstrSql & "       `nis`.`zona`, " & vbCrLf
		lstrSql = lstrSql & "       `nis`.`livro`, " & vbCrLf
		lstrSql = lstrSql & "       `nis`.`conta`, " & vbCrLf
		lstrSql = lstrSql & "       `nis`.`unid_id`, " & vbCrLf
		lstrSql = lstrSql & "       `nis`.`nis`, " & vbCrLf
		lstrSql = lstrSql & "       `nis`.`evento` " & vbCrLf
		lstrSql = lstrSql & "  FROM `nis` " & vbCrLf
		lstrSql = lstrSql & " WHERE `nis`.`cpf`     = '" & astrCpf & "' " & vbCrLf
		lstrSql = lstrSql & "   AND `nis`.`zona`    = '" & astrZona & "' " & vbCrLf
		lstrSql = lstrSql & "   AND `nis`.`livro`   = '" & astrLivro & "' " & vbCrLf
		lstrSql = lstrSql & "   AND `nis`.`conta`   = '" & astrConta & "' " & vbCrLf
		lstrSql = lstrSql & "   AND `nis`.`unid_id` = " & alngUnidId & vbCrLf
		lstrSql = lstrSql & "   AND `nis`.`evento` <> 'D' " & vbCrLf
		lstrSql = lstrSql & "   AND `nis`.`nis` <> '' " & vbCrLf
		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 = Trim(IIf(IsDbNull(.Fields("nis").value), "", .Fields("nis").value))
				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
		BuscaNis = lstrRet
	End Function
	
	Private Function Pedente(ByVal alngConsId As Object) As Boolean
		Dim lblnRet As Boolean
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		lblnRet = False
		lstrSql = ""
		lstrSql = lstrSql & "SELECT numnf, mesano, mesano_ref, zona, livro, conta, cons_id, unid_id, flgst, datavenc "
		lstrSql = lstrSql & "  FROM nf "
		'UPGRADE_WARNING: Couldn't resolve default property of object alngConsId. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		lstrSql = lstrSql & " WHERE nf.cons_id = " & alngConsId
		lstrSql = lstrSql & "   AND nf.flgst = '' "
		lstrSql = lstrSql & "   AND nf.datavenc < '" & VB6.Format(BuscaDataBD, "yyyy-MM-dd") & "' "
		lstrSql = lstrSql & "  LIMIT 0, 1 "
		Call Conectar(lstrSql, lrstAux)
		With lrstAux
			If .State = ADODB.ObjectStateEnum.adStateOpen Then
				lblnRet = (.RecordCount > 0)
				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
		Pedente = lblnRet
	End Function
	
	Private Sub LigDes(ByVal ablnModo As Boolean)
		Me.chkGrupoA.Enabled = ablnModo
		Me.chkGrupoB.Enabled = ablnModo
		Me.chkLigados.Enabled = ablnModo
		Me.chkDesligados.Enabled = ablnModo
		Me.chkCortados.Enabled = ablnModo
		Me.chkDesativados.Enabled = ablnModo
		Me.chkMesAno.Enabled = ablnModo
		Me.chkFaturadoEm.Enabled = ablnModo
		Me.dtpMesAno.Enabled = ablnModo
		Me.chkSomenteMesAno.Enabled = IIf(Me.chkFaturadoEm.Enabled And Me.chkFaturadoEm.CheckState = System.Windows.Forms.CheckState.Checked, True, False)
		If Me.dtpMesAno.Enabled Then
			Me.dtpMesAno.Enabled = (Me.chkMesAno.CheckState = System.Windows.Forms.CheckState.Checked)
		End If
		Me.dtpFaturadoEm.Enabled = ablnModo
		If Me.dtpMesAno.Enabled Then
			Me.dtpFaturadoEm.Enabled = (Me.chkFaturadoEm.CheckState = System.Windows.Forms.CheckState.Checked)
		End If
		Me.chkEmDia.Enabled = ablnModo
		Me.lsvRotas.Enabled = ablnModo
		Me.lsvClasses.Enabled = ablnModo
		Me.cmdProcessar.Enabled = ablnModo
		Me.cmdExporta.Enabled = ablnModo
		Me.cmdImprimir.Enabled = ablnModo
		Me.cmdFechar.Enabled = ablnModo
		Me.cmdArquivo.Enabled = ablnModo
	End Sub
	
	Private Function VerificaFaturamento(ByVal astrGrupo As String, ByVal astrZona As String, ByVal astrLivro As String, ByVal astrConta As String, ByVal alngUnidId As Integer) As Boolean
		Dim lblnRet As Boolean
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		lblnRet = False
		lstrSql = ""
		lstrSql = lstrSql & "   SELECT * " & vbCrLf
		lstrSql = lstrSql & "     FROM `nf` " & vbCrLf
		lstrSql = lstrSql & "    WHERE `nf`.`zona`       = '" & astrZona & "' " & vbCrLf
		lstrSql = lstrSql & "      AND `nf`.`livro`      = '" & astrLivro & "' " & vbCrLf
		lstrSql = lstrSql & "      AND `nf`.`conta`      = '" & astrConta & "' " & vbCrLf
		lstrSql = lstrSql & "      AND `nf`.`unid_id`    = '" & alngUnidId & "' " & vbCrLf
		lstrSql = lstrSql & "      AND `nf`.`grupo`      = '" & VB.Left(astrGrupo, 1) & "' " & vbCrLf
		lstrSql = lstrSql & "      AND `nf`.`mesano_ref` = " & CInt(VB6.Format(Me.dtpFaturadoEm.value, "YYYYMM")) & " " & vbCrLf
		lstrSql = lstrSql & "    LIMIT 0, 1 "
		Call Conectar(lstrSql, lrstAux)
		With lrstAux
			If .State = ADODB.ObjectStateEnum.adStateOpen Then
				lblnRet = (.RecordCount > 0)
				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
		VerificaFaturamento = lblnRet
	End Function
End Class