Option Strict Off
Option Explicit On

Imports fatd_faturamento.BLL.Repositorio
Imports fatd_faturamento.ENTIDADE
Imports VB = Microsoft.VisualBasic
Friend Class frmEnvioSGD
	Inherits System.Windows.Forms.Form
	Private Sub cmdFechar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdFechar.Click
		Call Me.Close()
	End Sub
	
	Private Sub cmdGerarCliente_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdGerarCliente.Click
		Dim strArquivo As String
		Dim intARQ As Short
		Dim strLinha As String

		Dim strSql As String
		Dim strZona As String
		Dim strLivro As String
		Dim strConta As String
		Dim lngConsId As Integer
		Dim lngUnidId As Integer
		Dim strNome As String
		Dim strNomeBairro As String
		Dim strNomeEnd As String
		Dim strNumero As String
		Dim strCep As String
		Dim strComplement As String
		Dim strMedidor As String
		Dim strRota As String
		Dim strTensao As String
		Dim strFase As String
		Dim strSituacao As String
		Dim strClasse As String
		Dim strSubClas As String
		Dim strRamal As String
		Dim strGrupo As String
		Dim lstrGrTar As String
		Dim strCodBairro As String
		Dim strCodLogra As String
		Dim iCodLogra As Integer
		Dim strClassif As String
		Dim strMes1 As String
		Dim strMes2 As String
		Dim strMes3 As String
		Dim strMes4 As String
		Dim strMes5 As String
		Dim strMes6 As String
		Dim dtAux As Date
		Dim dt As New DataTable
		Dim dtLogra As New DataTable
		Dim dtBairro As New DataTable
		Dim dtnf As New DataTable
		Dim objatualizaBancoDados As New entidadeAtualizaBancoDados
		Dim repositorioAtualizaBancoDados As New repositorioAtualizaBancoDados

		Try

			dtAux = System.DateTime.FromOADate(DateSerial(CInt(VB.Right(Me.txtMesAno.Text, 4)), CInt(VB.Left(Me.txtMesAno.Text, 2)), 1).ToOADate - 1)
			strMes1 = VB6.Format(Month(dtAux), "00") & "/" & VB6.Format(Year(dtAux), "0000")
			dtAux = System.DateTime.FromOADate(DateSerial(Year(dtAux), Month(dtAux), 1).ToOADate - 1)
			strMes2 = VB6.Format(Month(dtAux), "00") & "/" & VB6.Format(Year(dtAux), "0000")
			dtAux = System.DateTime.FromOADate(DateSerial(Year(dtAux), Month(dtAux), 1).ToOADate - 1)
			strMes3 = VB6.Format(Month(dtAux), "00") & "/" & VB6.Format(Year(dtAux), "0000")
			dtAux = System.DateTime.FromOADate(DateSerial(Year(dtAux), Month(dtAux), 1).ToOADate - 1)
			strMes4 = VB6.Format(Month(dtAux), "00") & "/" & VB6.Format(Year(dtAux), "0000")
			dtAux = System.DateTime.FromOADate(DateSerial(Year(dtAux), Month(dtAux), 1).ToOADate - 1)
			strMes5 = VB6.Format(Month(dtAux), "00") & "/" & VB6.Format(Year(dtAux), "0000")
			dtAux = System.DateTime.FromOADate(DateSerial(Year(dtAux), Month(dtAux), 1).ToOADate - 1)
			strMes6 = VB6.Format(Month(dtAux), "00") & "/" & VB6.Format(Year(dtAux), "0000")

			If Len(Trim(Me.txtArqCliente.Text)) > 0 Then

				strArquivo = Me.txtArqCliente.Text
				Me.Cursor = System.Windows.Forms.Cursors.WaitCursor

				lblInfo.Text = " Buscando os dados... "
				lblInfo.Refresh()

				strSql = "SELECT zona, "
				strSql = strSql & " livro, "
				strSql = strSql & " conta, "
				strSql = strSql & " nome, "
				strSql = strSql & " bairro, "
				strSql = strSql & " end, "
				strSql = strSql & " numero, "
				strSql = strSql & " cep, "
				strSql = strSql & " complement, "
				strSql = strSql & " num, "
				strSql = strSql & " rota, "
				strSql = strSql & " tensao, "
				strSql = strSql & " fase, "
				strSql = strSql & " situacao, "
				strSql = strSql & " clas, "
				strSql = strSql & " grupo, "
				strSql = strSql & " ta, "
				strSql = strSql & " cons_id, "
				'strSql = strSql & " unid_id, "
				strSql = strSql & " id, "
				strSql = strSql & " tp_ligacao "
				strSql = strSql & " FROM UC_Faturamento "
				'strSql = strSql & " UNION " & "SELECT zona, "
				'strSql = strSql & " livro, "
				'strSql = strSql & " conta, "
				'strSql = strSql & " nome, "
				'strSql = strSql & " bairro, "
				'strSql = strSql & " End, "
				'strSql = strSql & " numero, "
				'strSql = strSql & " cep, "
				'strSql = strSql & " complement, "
				'strSql = strSql & " eletr, "
				'strSql = strSql & " rota, "
				'strSql = strSql & " tensao, "
				'strSql = strSql & " fase, "
				'strSql = strSql & " situacao, "
				'strSql = strSql & " clas, "
				'strSql = strSql & " grupo, "
				'strSql = strSql & " ta, "
				'strSql = strSql & " cons_id, "
				'strSql = strSql & " unid_id, "
				'strSql = strSql & " tp_ligacao "
				'strSql = strSql & " FROM unida "
				'*Call Conectar(strSql, rsUnidade)

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

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

					intARQ = FreeFile()
					FileOpen(intARQ, strArquivo, OpenMode.Output)

					For Each row As DataRow In dt.Rows

						strZona = IIf(IsDBNull(row.Item("Zona")), "", row.Item("Zona"))
						strLivro = IIf(IsDBNull(row.Item("Livro")), "", row.Item("Livro"))
						strConta = IIf(IsDBNull(row.Item("Conta")), "", row.Item("Conta"))
						lngConsId = IIf(IsDBNull(row.Item("cons_id")), "", row.Item("cons_id"))
						'lngUnidId = IIf(IsDBNull(row.Item("unid_id")), "", row.Item("unid_id"))
						lngUnidId = IIf(IsDBNull(row.Item("id")), "", row.Item("id"))

						strSql = "SELECT *  FROM nf USE INDEX(keyzona, keylivro, keyconta) "
						strSql = strSql & "          WHERE nf.zona = """ & strZona & """ "
						strSql = strSql & "		     AND nf.livro = """ & strLivro & """ "
						strSql = strSql & "		     AND nf.conta = """ & strConta & """ "
						'strSql = strSql & "		     AND nf.cons_id = " & lngConsId & " "
						'strSql = strSql & "		     AND nf.unid_id = " & lngUnidId & " "
						strSql = strSql & "		     AND nf.mesano in(""" & Me.txtMesAno.Text & """,""" & strMes1 & """,""" & strMes2 & """,""" & strMes3 & """,""" & strMes4 & """,""" & strMes5 & """,""" & strMes6 & """) " & "  LIMIT 1"

						objatualizaBancoDados.QUERY_NAME = strSql
						dtnf = repositorioAtualizaBancoDados.Consultar(objatualizaBancoDados)

						If dtnf.Rows.Count > 0 Then
							strNome = IIf(IsDBNull(row.Item("nome")), "", row.Item("nome"))
							strNomeBairro = IIf(IsDBNull(row.Item("bairro")), "", row.Item("bairro"))
							strNomeEnd = IIf(IsDBNull(row.Item("End")), "", row.Item("End"))
							strNumero = IIf(IsDBNull(row.Item("numero")), "", row.Item("numero"))
							strCep = IIf(IsDBNull(row.Item("cep")), "", row.Item("cep"))
							strComplement = IIf(IsDBNull(row.Item("complement")), "", row.Item("complement"))
							strMedidor = IIf(IsDBNull(row.Item("Num")), "", row.Item("Num"))
							strRota = IIf(IsDBNull(row.Item("ROTA")), "", row.Item("ROTA"))
							strTensao = Trim(IIf(IsDBNull(row.Item("Tensao")), "", row.Item("Tensao")))

							If VB.Left(strTensao, 3) = "127" Then
								strTensao = "01"
							ElseIf VB.Left(strTensao, 3) = "220" Then
								strTensao = "02"
							ElseIf VB.Left(strTensao, 3) = "380" Then
								strTensao = "03"
							ElseIf Trim(VB.Left(strTensao, 6)) = "11400" Then
								strTensao = "04"
							ElseIf Trim(VB.Left(strTensao, 6)) = "13800" Then
								strTensao = "05"
							ElseIf Trim(VB.Left(strTensao, 6)) = "34500" Then
								strTensao = "06"
							ElseIf Trim(VB.Left(strTensao, 6)) = "69000" Then
								strTensao = "07"
							ElseIf Trim(VB.Left(strTensao, 6)) = "88000" Then
								strTensao = "08"
							ElseIf Trim(VB.Left(strTensao, 6)) = "138000" Then
								strTensao = "09"
							End If

							strFase = Trim(IIf(IsDBNull(row.Item("fase")), "M", row.Item("fase")))
							Select Case strFase
								Case "M"
									strFase = "01"
								Case "B"
									strFase = "02"
								Case "T"
									strFase = "03"
							End Select

							strSituacao = Trim(IIf(IsDBNull(row.Item("situacao")), "L", row.Item("situacao")))
							Select Case strSituacao
								Case "L", "C"
									strSituacao = "0"
								Case "D"
									strSituacao = "2"
								Case "I"
									strSituacao = "3"
							End Select

							strClasse = Trim(IIf(IsDBNull(row.Item("clas")), "", row.Item("clas")))
							strSubClas = VB.Right(strClasse, 2)
							Select Case VB.Left(strClasse, 1)
								Case CStr(9)
									strClasse = "1"
								Case CStr(4)
									strClasse = "2"
								Case CStr(0), CStr(1), CStr(2), CStr(3)
									strClasse = "3"
								Case CStr(5)
									strClasse = "4"
								Case CStr(6)
									strClasse = "5"
								Case CStr(8)
									strClasse = "6"
								Case CStr(7)
									strClasse = "7"
							End Select

							strGrupo = VB.Left(Trim(IIf(IsDBNull(row.Item("grupo")), "", row.Item("grupo"))), 1)
							strRamal = Trim(IIf(IsDBNull(row.Item("tp_ligacao")), "A", row.Item("tp_ligacao")))
							lstrGrTar = Mid(IIf(IsDBNull(row.Item("ta")), "", row.Item("ta")), 3, 2)
							lstrGrTar = IIf(VB.Left(lstrGrTar, 1) = "A", "A4", lstrGrTar)
							strClassif = VB6.Format(strClasse, "00") & VB6.Format(strSubClas, "00") & VB6.Format(strFase, "00")

							' BAIRRO
							strSql = "SELECT codnum FROM bairro where nome = """ & strNomeBairro & """ LIMIT 1 "
							objatualizaBancoDados.QUERY_NAME = strSql
							dtBairro = repositorioAtualizaBancoDados.Consultar(objatualizaBancoDados)


							If dtBairro.Rows.Count > 0 Then
								For Each rowbairro As DataRow In dtBairro.Rows
									strCodBairro = IIf(IsDBNull(rowbairro.Item("codnum")), "0", rowbairro.Item("codnum"))
								Next
							Else
								strCodBairro = "0"
							End If

							' LOGRA
							strSql = "SELECT codnum FROM logra where nome = """ & strNomeEnd & """ LIMIT 1 "
							objatualizaBancoDados.QUERY_NAME = strSql
							dtLogra = repositorioAtualizaBancoDados.Consultar(objatualizaBancoDados)

							If dtLogra.Rows.Count > 0 Then
								For Each rowLogra As DataRow In dtLogra.Rows
									strCodLogra = VB6.Format(IIf(IsDBNull(rowLogra.Item("codnum")), "0", rowLogra.Item("codnum")), "0000")
									'iCodLogra = VB6.Format(IIf(IsDBNull(rowLogra.Item("codnum")), "0", rowLogra.Item("codnum")), "0000")
								Next
							Else
								'iCodLogra = 0
								strCodLogra = "0"
							End If

							'PREENCHE LINHA
							strLinha = ""
							strLinha = strLinha & PreencheCampo(Trim(strCodBairro), 3, "0", "E") 'Código do Bairro
							strLinha = strLinha & PreencheCampo(PreencheCampo(Trim(strLivro), 2, "0", "E") & PreencheCampo(VB.Right(Trim(strConta), 4), 4, "0", "E"), 20, " ", "D") 'Conta
							strLinha = strLinha & PreencheCampo(Trim(strNome), 70, " ", "D") 'Nome da unidade consumidora
							strLinha = strLinha & PreencheCampo(Trim(strCodLogra), 20, " ", "D") 'Codigo do logradouro
							strLinha = strLinha & PreencheCampo(" ", 6, " ", "D") 'Tipo de Logradouro
							strLinha = strLinha & PreencheCampo(Trim(strNomeEnd), 80, " ", "D") 'Nome do logradouro
							strLinha = strLinha & PreencheCampo(" ", 6, " ", "D") 'Titulo do logradouro
							strLinha = strLinha & PreencheCampo(Trim(strNumero), 10, " ", "E") 'Numero do endereco
							strLinha = strLinha & PreencheCampo(Trim(strCep), 10, " ", "D") 'Numero do Cep
							strLinha = strLinha & PreencheCampo(Trim(strComplement), 40, " ", "D") 'Complemento do endereco
							strLinha = strLinha & PreencheCampo(Trim(strNomeBairro), 40, " ", "D") 'Nome do bairro
							strLinha = strLinha & PreencheCampo(Trim(strMedidor), 20, " ", "E") 'Medidor da unidade
							strLinha = strLinha & PreencheCampo(" ", 6, " ", "D") 'ddd tel
							strLinha = strLinha & PreencheCampo(" ", 30, " ", "D") ' telefone
							strLinha = strLinha & PreencheCampo(" ", 6, " ", "D") 'ddd cel
							strLinha = strLinha & PreencheCampo(" ", 30, " ", "D") 'celular
							strLinha = strLinha & PreencheCampo(" ", 40, " ", "D") 'email
							strLinha = strLinha & PreencheCampo(VB.Left(Trim(strRota), 4), 5, "0", "E") 'rota
							strLinha = strLinha & PreencheCampo(VB.Right(Trim(strRota), 3), 3, "0", "E") 'sequencia
							strLinha = strLinha & PreencheCampo(" ", 2, " ", "D") 'Razao
							strLinha = strLinha & PreencheCampo(" ", 4, " ", "D") 'tipo
							strLinha = strLinha & PreencheCampo("0", 1, "0", "E") 'vip
							strLinha = strLinha & PreencheCampo(" ", 1, " ", "D") 'vip
							strLinha = strLinha & PreencheCampo(" ", 1, " ", "D") 'vip
							strLinha = strLinha & PreencheCampo(" ", 1, " ", "D") 'entrega conta
							strLinha = strLinha & PreencheCampo(" ", 2, " ", "D") 'programa
							strLinha = strLinha & PreencheCampo(strTensao, 2, "0", "E") 'tensao
							strLinha = strLinha & PreencheCampo(strSituacao, 1, "0", "E") 'status ligacao
							strLinha = strLinha & PreencheCampo(strClasse, 1, "0", "E") 'classe
							strLinha = strLinha & PreencheCampo(strGrupo, 1, " ", "E") 'grupo
							strLinha = strLinha & PreencheCampo(strRamal, 1, " ", "E") 'ramal
							strLinha = strLinha & PreencheCampo(lstrGrTar, 4, " ", "D") 'grupo tarifario
							strLinha = strLinha & PreencheCampo(strClassif, 6, " ", "D") 'ramal
							PrintLine(intARQ, strLinha)
						End If

						Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
						System.Windows.Forms.Application.DoEvents()

					Next
					FileClose(intARQ)
				Else
					Call MsgBox("Sem dados para o arquivo de cadastro!", MsgBoxStyle.Information, gstrTituloMensagem)
				End If

				Me.Cursor = System.Windows.Forms.Cursors.Default
			Else
				Call MsgBox("Favor informe o arquivo de cadastro!", MsgBoxStyle.Information, gstrTituloMensagem)
				Me.txtArqCliente.Focus()
			End If

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

	Private Sub cmdGerarConsumo_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdGerarConsumo.Click
		Dim strArquivo As String
		Dim intARQ As Short
		Dim strLinha As String

		Dim strZona As String
		Dim strLivro As String
		Dim strConta As String
		Dim lstrGrTar As String
		Dim strBairro As String
		Dim strCodBairro As String
		Dim dblConsumo As Double
		Dim dblDemanda As Double
		Dim dblFatorPt As Double
		Dim dblDemCont As Double
		Dim strSql As String
		Dim dt As DataTable
		Dim dtBairro As DataTable
		Dim dtbairroaux As DataTable
		Dim objentidadeatualizabanco As New entidadeAtualizaBancoDados
		Dim repositorioatualizabanco As New repositorioAtualizaBancoDados

		Try

			If Len(Trim(txtArqConsum.Text)) > 0 Then
				strArquivo = txtArqConsum.Text
				Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
				lblInfo.Text = " Buscando os dados... "
				lblInfo.Refresh()

				'strSql = "SELECT zona, livro, conta, ta, " & "       IF(MID(ta,3,1)='B',buscavlritemfatura(numnf,mesano,'030201009'),0) consumob, " & "       IF(MID(ta,3,2)='A4',buscavlritemfatura(numnf,mesano,'0302010009'),0) consumoa, " & "       IF(MID(ta,3,1)='A' AND MID(ta,4,1)<>'4',buscavlritemfatura(numnf,mesano,'0302010010'),0) consumoap, " & "       IF(MID(ta,3,1)='A' AND MID(ta,4,1)<>'4',buscavlritemfatura(numnf,mesano,'0302010011'),0) consumoafp, " & "       IF(MID(ta,3,2)='A4',buscavlritemfatura(numnf,mesano,'0302010017'),0) demanda, " & "       IF(MID(ta,3,1)='A' AND MID(ta,4,1)<>'4',buscavlritemfatura(numnf,mesano,'0302010021'),0) demandap, " & "       IF(MID(ta,3,1)='A' AND MID(ta,4,1)<>'4',buscavlritemfatura(numnf,mesano,'0302010025'),0) demandafp, " & "       (IF(MID(ta,3,1)='A',buscavlritemfatura(numnf,mesano,'0302030099'),0)*100) fatorpot, " & "       (SELECT IF(MID(ta,3,2) = 'A4', demanda_contr, dem_contr_fp) contrato FROM unida USE INDEX(keyzona,keylivro,keyconta)  " & "         WHERE unida.zona = nf.zona " & "           AND unida.livro = nf.livro " & "           AND unida.conta = nf.conta) demcontr " & "  FROM nf USE INDEX(keyzona,keylivro,keyconta) " & " WHERE mesano = '" & txtMesAno.Text & "' " & "ORDER BY LEFT(rota,4) ASC, RIGHT(rota,4) ASC "
				strSql = "SELECT zona, livro, conta, ta, " & "       IF(MID(ta,3,1)='B',buscavlritemfatura(numnf,mesano,'030201009'),0) consumob, " & "       IF(MID(ta,3,2)='A4',buscavlritemfatura(numnf,mesano,'0302010009'),0) consumoa, " & "       IF(MID(ta,3,1)='A' AND MID(ta,4,1)<>'4',buscavlritemfatura(numnf,mesano,'0302010010'),0) consumoap, " & "       IF(MID(ta,3,1)='A' AND MID(ta,4,1)<>'4',buscavlritemfatura(numnf,mesano,'0302010011'),0) consumoafp, " & "       IF(MID(ta,3,2)='A4',buscavlritemfatura(numnf,mesano,'0302010017'),0) demanda, " & "       IF(MID(ta,3,1)='A' AND MID(ta,4,1)<>'4',buscavlritemfatura(numnf,mesano,'0302010021'),0) demandap, " & "       IF(MID(ta,3,1)='A' AND MID(ta,4,1)<>'4',buscavlritemfatura(numnf,mesano,'0302010025'),0) demandafp, " & "       (IF(MID(ta,3,1)='A',buscavlritemfatura(numnf,mesano,'0302030099'),0)*100) fatorpot, " & "       (SELECT IF(MID(ta,3,2) = 'A4', demanda_contr, dem_contr_fp) contrato FROM UC_Faturamento USE INDEX(idx_zona,idx_livro,idx_conta)   " & "         WHERE UC_Faturamento.zona = nf.zona " & "           AND UC_Faturamento.livro = nf.livro " & "           AND UC_Faturamento.conta = nf.conta) demcontr " & "  FROM nf USE INDEX(keyzona,keylivro,keyconta) " & " WHERE mesano = '" & txtMesAno.Text & "' " & "ORDER BY LEFT(rota,4) ASC, RIGHT(rota,4) ASC "

				objentidadeatualizabanco.QUERY_NAME = strSql
				dt = repositorioatualizabanco.Consultar(objentidadeatualizabanco)

				If dt.Rows.Count > 0 Then
					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, dt.Rows.Count)
					intARQ = FreeFile()
					FileOpen(intARQ, strArquivo, OpenMode.Output)

					For Each row As DataRow In dt.Rows

						strZona = IIf(IsDBNull(row.Item("Zona")), "", row.Item("Zona"))
						strLivro = IIf(IsDBNull(row.Item("Livro")), "", row.Item("Livro"))
						strConta = IIf(IsDBNull(row.Item("Conta")), "", row.Item("Conta"))
						lstrGrTar = LCase(Mid(IIf(IsDBNull(row.Item("ta")), "01B1", row.Item("ta")), 3, 1))

						'BAIRRO
						'strSql = "SELECT bairro FROM unid" & lstrGrTar & " WHERE zona = """ & strZona & """ " & "   AND livro = """ & strLivro & """ " & "   AND conta = """ & strConta & """ " & " LIMIT 1 "
						strSql = "SELECT bairro FROM UC_Faturamento WHERE zona = """ & strZona & """ " & "   AND livro = """ & strLivro & """ " & "   AND conta = """ & strConta & """ " & " LIMIT 1 "

						objentidadeatualizabanco.QUERY_NAME = strSql
						dtbairroaux = repositorioatualizabanco.Consultar(objentidadeatualizabanco)

						If dtbairroaux.Rows.Count > 0 Then
							For Each rowbairroaux As DataRow In dtbairroaux.Rows
								strBairro = IIf(IsDBNull(rowbairroaux.Item("bairro")), "", rowbairroaux.Item("bairro"))
							Next
						Else
							strBairro = ""
						End If


						' CODBAIRRO
						strSql = "SELECT codnum FROM bairro where nome = """ & strBairro & """ LIMIT 1 "

						objentidadeatualizabanco.QUERY_NAME = strSql
						dtBairro = repositorioatualizabanco.Consultar(objentidadeatualizabanco)

						If dtBairro.Rows.Count > 0 Then
							For Each rowbairro As DataRow In dtBairro.Rows
								strCodBairro = IIf(IsDBNull(rowbairro.Item("codnum")), "0", rowbairro.Item("codnum"))
							Next
						Else
							strCodBairro = "0"
						End If

						dblConsumo = IIf(IsDBNull(row.Item("consumob")), 0, row.Item("consumob"))
						dblConsumo = dblConsumo + IIf(IsDBNull(row.Item("consumoa")), 0, row.Item("consumoa"))
						dblConsumo = dblConsumo + IIf(IsDBNull(row.Item("consumoap")), 0, row.Item("consumoap"))
						dblConsumo = dblConsumo + IIf(IsDBNull(row.Item("consumoafp")), 0, row.Item("consumoafp"))

						dblDemanda = IIf(IsDBNull(row.Item("demanda")), 0, row.Item("demanda"))
						dblDemanda = dblDemanda + IIf(IsDBNull(row.Item("demandap")), 0, row.Item("demandap"))
						dblDemanda = dblDemanda + IIf(IsDBNull(row.Item("demandafp")), 0, row.Item("demandafp"))
						dblDemanda = Fix(dblDemanda)

						dblFatorPt = Fix(IIf(IsDBNull(row.Item("fatorpot")), 0, row.Item("fatorpot")))
						dblDemCont = IIf(IsDBNull(row.Item("demcontr")), 0, row.Item("demcontr"))

						strLinha = ""
						strLinha = strLinha & PreencheCampo(PreencheCampo(Trim(strCodBairro), 3, "0", "E") & PreencheCampo(Trim(strLivro), 2, "0", "E") & PreencheCampo(VB.Right(Trim(strConta), 4), 4, "0", "E"), 20, " ", "D") 'Conta
						strLinha = strLinha & PreencheCampo(Trim(CStr(dblConsumo)), 9, " ", "E") 'KWH
						strLinha = strLinha & PreencheCampo(Trim(CStr(dblDemanda)), 5, " ", "E") 'kW
						strLinha = strLinha & PreencheCampo(Trim(CStr(dblFatorPt)), 3, " ", "E") 'fator potencia
						strLinha = strLinha & PreencheCampo(Trim(CStr(dblDemCont)), 5, " ", "E") 'fator potencia

						PrintLine(intARQ, strLinha)
						'Call rsFatura.MoveNext()
						Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
						System.Windows.Forms.Application.DoEvents()
					Next
					FileClose(intARQ)
				Else
					Call MsgBox("Não existe dados neste mês!", MsgBoxStyle.Information, gstrTituloMensagem)
				End If

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

			Else
				Call MsgBox("Favor informe o arquivo de consumo!", MsgBoxStyle.Information, gstrTituloMensagem)
				Me.txtArqConsum.Focus()
			End If

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

	End Sub
	Private Sub cmdProcuraCliente_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdProcuraCliente.Click
		Try
			fbdSgd.ShowDialog()
			fbdSgd.Description = "Geração de arquivo SGD"
			txtArqCliente.Text = fbdSgd.SelectedPath & "\SGD.TXT"
			'Me.dialArqOpen.ShowDialog()
			'Me.txtArqCliente.Text = Me.dialArqOpen.FileName
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: cmdProcuraCliente_Click" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub
	Private Sub cmdProcuraConsumo_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdProcuraConsumo.Click
		Try
			fbdSgd.ShowDialog()
			fbdSgd.Description = "Geração de arquivo SGD"
			txtArqConsum.Text = fbdSgd.SelectedPath & "\CONSGD.TXT"
			'Me.dialArqOpen.ShowDialog()
			'Me.txtArqConsum.Text = Me.dialArqOpen.FileName
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: cmdProcuraConsumo_Click" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub
	Private Sub frmEnvioSGD_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		Try
			'Me.Icon = FrmAtPrincipal.Icon
			Me.txtArqCliente.Text = My.Application.Info.DirectoryPath & "\SGD.TXT"
			Me.txtArqConsum.Text = My.Application.Info.DirectoryPath & "\CONSGD.TXT"
			Me.txtMesAno.Text = GetConfig("mes.ano.faturamento")
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: frmEnvioSGD_Load" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub
End Class