Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Friend Class frmEnviarDadosLivreSef
	Inherits System.Windows.Forms.Form
	Private istrNmArq As String
	
	Private Sub cmdArquivo_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdArquivo.Click
		Me.dialArqOpen.FileName = My.Application.Info.DirectoryPath & "\" & istrNmArq
		Me.dialArqOpen.ShowDialog()
		Call MontaNomeArquivo()
	End Sub
	
	Private Sub cmdExporta_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdExporta.Click
		Dim lstrCpfCnpj As String
		Dim lstrInscEst As String
		Dim lstrRazSoci As String
		Dim lstrCgcCons As String
		Dim lstrLogrEnd As String
		Dim lstrLogrBai As String
		Dim lstrLogrCid As String
		Dim lstrLogrCep As String
		Dim lstrLogrU_F As String
		Dim lstrLogrNum As String
		Dim lstrLogrCom As String
		Dim lstrLogrado As String
		'
		Dim lstrZona As String
		Dim lstrLivro As String
		Dim lstrConta As String
		Dim llngIdCCEE As Integer
		Dim lstrMedidor As String
		Dim lstrItensNf As String
		Dim ldblkWhLido As Double
		'Controle Arquivo
		Dim lstrLin As String
		Dim lintARQ As Short
		'
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		If Trim(Me.txtArquivo.Text) = "" Then
			Call MsgBox("Você deve selecionar um local e o nome do arquivo que deseja gravar!" & vbCrLf & "Ex: C:\" & gstrEmpresa_CNPJ & "_" & VB6.Format(Me.dtpPeriodo.value, "YYMM") & Me.dialArqOpen.DefaultExt, MsgBoxStyle.Critical, gstrTituloMensagem)
		Else
			Call LigDes(False)
			Me.lblInfo.Text = " Selecionando as faturas do mês informado! Aguarde..."
			Call Me.lblInfo.Refresh()
			lstrSql = ""
			lstrSql = lstrSql & "    SELECT "
			lstrSql = lstrSql & "           `nf`.`numnf`, "
			lstrSql = lstrSql & "           `nf`.`mesano`, "
			lstrSql = lstrSql & "           `nf`.`mesano_ref`, "
			lstrSql = lstrSql & "           `nf`.`cons_id`, "
			lstrSql = lstrSql & "           `nf`.`unid_id`, "
			lstrSql = lstrSql & "           `nf`.`grupo`, "
			lstrSql = lstrSql & "           `nf`.`codclas`, "
			lstrSql = lstrSql & "           `nf`.`ta`, "
			lstrSql = lstrSql & "           `nf`.`datavenc`, "
			lstrSql = lstrSql & "           `nf`.`datapag`, "
			lstrSql = lstrSql & "           `nf`.`datast`, "
			lstrSql = lstrSql & "           `nf`.`rota`, "
			lstrSql = lstrSql & "           `nf`.`zona`, "
			lstrSql = lstrSql & "           `nf`.`livro`, "
			lstrSql = lstrSql & "           `nf`.`conta`, "
			lstrSql = lstrSql & "           `nf`.`flgst`, "
			lstrSql = lstrSql & "           `nf`.`cpf_cnpj`, "
			lstrSql = lstrSql & "           `nf`.`num_med`, "
			lstrSql = lstrSql & "           `nf`.`itensserv`, "
			lstrSql = lstrSql & "           `consum`.`nome`, "
			lstrSql = lstrSql & "           `consum`.`cgc`, "
			lstrSql = lstrSql & "           `consum`.`insc`, "
			lstrSql = lstrSql & "           `unida`.`uf`, "
			lstrSql = lstrSql & "           `unida`.`cep`, "
			lstrSql = lstrSql & "           `unida`.`cidade`, "
			lstrSql = lstrSql & "           `unida`.`bairro`, "
			lstrSql = lstrSql & "           `unida`.`end`, "
			lstrSql = lstrSql & "           `unida`.`numero`, "
			lstrSql = lstrSql & "           `unida`.`complement`, "
			lstrSql = lstrSql & "           `unida`.`eletr`, "
			lstrSql = lstrSql & "           `unida`.`idccee` "
			lstrSql = lstrSql & "      FROM ((`nf` "
			lstrSql = lstrSql & " LEFT JOIN `unida` ON((`unida`.`unid_id` = `nf`.`unid_id` AND `unida`.`cons_id` = `nf`.`cons_id`))) "
			lstrSql = lstrSql & " LEFT JOIN `consum` ON((`consum`.`consreg` = `nf`.`cons_id`))) "
			lstrSql = lstrSql & "     WHERE `nf`.`mesano_ref` = " & CInt(VB6.Format(Me.dtpPeriodo.value, "YYYYMM")) & " "
			lstrSql = lstrSql & "       AND `nf`.`ta` = '01AL00' "
			lstrSql = lstrSql & "       AND `nf`.`flgst` <> 'C' "
			Call Conectar(lstrSql, lrstAux)
			With lrstAux
				If .State = ADODB.ObjectStateEnum.adStateOpen Then
					If .RecordCount > 0 Then
						lintARQ = FreeFile
						FileOpen(lintARQ, Me.txtArquivo.Text, OpenMode.Output)
						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"'
							lstrCgcCons = Trim(IIf(IsDbNull(.Fields("cgc").value), "", .Fields("cgc").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"'
							lstrCpfCnpj = Trim(IIf(IsDbNull(.Fields("cpf_cnpj").value), "", .Fields("cpf_cnpj").value))
							'UPGRADE_WARNING: Use of Null/IsNull() detected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="2EED02CB-5C0E-4DC1-AE94-4FAA3A30F51A"'
							lstrInscEst = Trim(IIf(IsDbNull(.Fields("insc").value), "", .Fields("insc").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"'
							lstrRazSoci = 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"'
							lstrLogrEnd = 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"'
							lstrLogrBai = 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"'
							lstrLogrCid = 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"'
							lstrLogrCep = Trim(IIf(IsDbNull(.Fields("cep").value), "", .Fields("cep").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"'
							lstrLogrU_F = Trim(IIf(IsDbNull(.Fields("uf").value), "", .Fields("uf").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"'
							lstrLogrNum = 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"'
							lstrLogrCom = Trim(IIf(IsDbNull(.Fields("complement").value), "", .Fields("complement").value))
							lstrLogrado = lstrLogrEnd
							If lstrLogrNum <> "" Then
								lstrLogrado = lstrLogrado & ", " & lstrLogrNum
							End If
							If lstrLogrCom <> "" Then
								lstrLogrado = lstrLogrado & ", " & lstrLogrCom
							End If
							If lstrLogrBai <> "" Then
								lstrLogrado = lstrLogrado & ", " & lstrLogrBai
							End If
							If lstrLogrCid <> "" Then
								lstrLogrado = lstrLogrado & ", " & lstrLogrCid
							End If
							If lstrLogrCep <> "" Then
								lstrLogrado = lstrLogrado & ", " & lstrLogrCep
							End If
							If lstrLogrU_F <> "" Then
								lstrLogrado = lstrLogrado & ", " & lstrLogrU_F
							End If
							lstrLogrado = VB.Left(lstrLogrado, 250)
							'
							If lstrCpfCnpj = "" Then lstrCpfCnpj = lstrCgcCons
							'
							'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 = IIf(IsDbNull(.Fields("zona").value), "0", .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 = IIf(IsDbNull(.Fields("livro").value), "0", .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 = IIf(IsDbNull(.Fields("conta").value), "0", .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"'
							llngIdCCEE = CInt(IIf(IsDbNull(.Fields("idccee").value), 0, .Fields("idccee").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("num_med").value), "0", .Fields("num_med").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"'
							If lstrMedidor = "0" Or lstrMedidor = "" Then lstrMedidor = Trim(IIf(IsDbNull(.Fields("eletr").value), "0", .Fields("eletr").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"'
							lstrItensNf = Trim(IIf(IsDbNull(.Fields("itensserv").value), "", .Fields("itensserv").value))
							ldblkWhLido = BuscaVlrItem(lstrItensNf, "0302010010")
							ldblkWhLido = ldblkWhLido + BuscaVlrItem(lstrItensNf, "0302010011")
							ldblkWhLido = ldblkWhLido / 1000
							'
							lstrLin = ""
							lstrLin = lstrLin & VB6.Format(lstrCgcCons, New String("0", 14)) '014, 001 até 014, Númerico alinhado a direita
							lstrLin = lstrLin & VB6.Format(lstrInscEst, New String("0", 9)) '009, 015 até 023, Númerico alinhado a direita
							lstrLin = lstrLin & VB.Left(lstrRazSoci & New String(" ", 50), 50) '050, 024 até 073, Alfanúmerico alinhado a esquerda
							lstrLin = lstrLin & VB.Left(lstrLogrado & New String(" ", 250), 250) '250, 074 até 323, Alfanúmerico alinhado a esquerda
							lstrLin = lstrLin & VB.Left(lstrZona & lstrLivro & lstrConta & New String(" ", 14), 14) '014, 324 até 337, Alfanúmerico alinhado a esquerda
							lstrLin = lstrLin & VB6.Format(llngIdCCEE, New String("0", 12)) '012, 338 até 349, Numérico alinhado a direita
							lstrLin = lstrLin & VB.Left(lstrMedidor & New String(" ", 12), 12) '012, 350 até 361, Alfanumérico alinhado a esquerda
							lstrLin = lstrLin & VB6.Format(Replace(Replace(VB6.Format(ldblkWhLido, "#,###0.000"), ",", ""), ".", ""), New String("0", 12)) '012, 350 até 361, Alfanumérico alinhado a esquerda
							'
							PrintLine(lintARQ, lstrLin)
							Call .MoveNext()
							Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
						End While
					End If
					Call .Close()
					FileClose(lintARQ)
					Call MsgBox("Arquivo gerado com sucesso!", MsgBoxStyle.Information, gstrTituloMensagem)
				End If
			End With
			Call LigDes(True)
		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 cmdFechar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdFechar.Click
		Call Me.Close()
	End Sub
	
	Private Sub dtpPeriodo_Change(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles dtpPeriodo.Change
		Call MontaNomeArquivo()
	End Sub
	
	Private Sub dtpPeriodo_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles dtpPeriodo.ClickEvent
		Call dtpPeriodo_Change(dtpPeriodo, New System.EventArgs())
	End Sub
	
	Private Sub frmEnviarDadosLivreSef_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		Call CenterForm(Me)
		istrNmArq = VB6.Format(gstrEmpresa_CNPJ, New String("0", 14)) & "_" & VB6.Format(Me.dtpPeriodo.value, "YYMM") & "." & Me.dialArqOpen.DefaultExt
		'UPGRADE_WARNING: CommonDialog variable was not upgraded Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="671167DC-EA81-475D-B690-7A40C7BF4A23"'
		With Me.dialArq
			.DefaultExt = "TXT"
			'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"'
			.Filter = "*." & .DefaultExt
			.Title = "Arquivo de exportação da Portaria SEF nº 342/12, Art. 5º, inciso II"
			.FileName = My.Application.Info.DirectoryPath & "\" & istrNmArq
			Me.txtArquivo.Text = .FileName
		End With
		Me.dtpPeriodo.value = BuscaDataBD
		Call MontaNomeArquivo()
	End Sub
	
	Private Sub MontaNomeArquivo()
		Dim lstrAux As String
		istrNmArq = VB6.Format(gstrEmpresa_CNPJ, New String("0", 14)) & "_" & VB6.Format(Me.dtpPeriodo.value, "YYMM") & "." & Me.dialArqOpen.DefaultExt
		lstrAux = StrReverse(Me.dialArqOpen.FileName)
		lstrAux = Mid(lstrAux, CInt(InStr(1, lstrAux, "\")))
		lstrAux = StrReverse(lstrAux) & istrNmArq
		Me.dialArqOpen.FileName = lstrAux
		Me.txtArquivo.Text = Me.dialArqOpen.FileName
	End Sub
	
	Private Sub LigDes(ByVal ablnModo As Boolean)
		Me.dtpPeriodo.Enabled = ablnModo
		Me.cmdArquivo.Enabled = ablnModo
		Me.cmdExporta.Enabled = ablnModo
		Me.cmdFechar.Enabled = ablnModo
	End Sub
End Class