Option Strict Off
Option Explicit On
Imports fatd_faturamento.BLL.Repositorio
Imports fatd_faturamento.ENTIDADE
Friend Class frmEmissaoBxRJul2010
	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
		Dim lstrAux As String
		Dim lstrAux1 As String
		Dim lintPos1 As Short
		Try
			'Me.dialArqOpen.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"'
			'Me.dialArqOpen.Filter = "*." & Me.dialArqOpen.DefaultExt
			'Me.dialArqOpen.FileName = StrReverse(Mid(StrReverse(Me.txtArquivo.Text), CInt(InStr(StrReverse(Me.txtArquivo.Text), "\")))) & istrNmArq & "." & Me.dialArqOpen.DefaultExt
			'Me.dialArqOpen.Title = "Arquivo de exportação do Baixa Renda Jul2010 OF.C. 31/2013"
			'Me.dialArqOpen.ShowDialog()
			fbdRenda.ShowDialog()
			lstrAux = StrReverse(fbdRenda.SelectedPath())
			Me.txtArquivo.Text = StrReverse(Mid(lstrAux, CInt(InStr(lstrAux, "\")))) & istrNmArq & ".TXT" '& Me.dialArqOpen.DefaultExt
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: cmdArquivo_Click" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub
	
	Private Sub cmdExporta_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdExporta.Click
		Call EncheGrade()
	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 frmEmissaoBxRJul2010_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		Try
			'Call CenterForm(Me)
			Call MontaGrade()
			istrNmArq = "BR" & gstrEmpresa_CodRes89 & "_414_7_2010"
			Me.txtArquivo.Text = My.Application.Info.DirectoryPath & "\" & istrNmArq & ".TXT"
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: frmEmissaoBxRJul2010_Load" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub MontaGrade()
		Try
			With Me.mfgDados
				.Rows = 0
				.Cols = 9
				Call .Clear()
				.set_ColWidth(0, 500)
				.set_ColWidth(1, 600)
				.set_ColWidth(2, 1100)
				.set_ColWidth(3, 200)
				.set_ColWidth(4, 200)
				.set_ColWidth(5, 3000)
				.set_ColWidth(6, 3000)
				.set_ColWidth(7, 1000)
				.set_ColWidth(8, 300)
				'.ColWidth(9) = 900
			End With
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: MontaGrade" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub

	Private Sub EncheGrade()
		'Controle Arquivo
		Dim lstrLin As String
		Dim lintARQ As Short
		Dim llngRow As Integer
		Dim llngCol As Integer
		'
		Dim lstrZon As String
		Dim lstrLiv As String
		Dim lstrCon As String
		Dim llngCId As Integer
		Dim llngUId As Integer
		Dim lstrB02 As String
		Dim lstrFas As String
		Dim lstrTip As String
		Dim lstrNom As String
		Dim lstrEnd As String
		Dim lstrNum As String
		Dim lstrCom As String
		Dim lstrBai As String
		Dim lstrCid As String
		Dim lstrUf As String
		Dim lstrCCl As String
		Dim lblnTse As Boolean
		Dim lstrSql As String
		Dim dt, dt2, dt3, dt4 As DataTable
		Dim objAtualizaBancoDados As New entidadeAtualizaBancoDados
		Dim repositorioAtualizaBancoDados As New repositorioAtualizaBancoDados

		Try

			If Trim(Me.txtArquivo.Text) = "" Then
				Call MsgBox("Você deve selecionar um local que deseja gravar o arquivo TXT!", MsgBoxStyle.Critical, gstrTituloMensagem)
			Else
				Call MontaGrade()
				Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
				Call LigDes(False)
				Me.mfgDados.Redraw = False
				lstrSql = ""
				lstrSql = lstrSql & "SELECT * "
				lstrSql = lstrSql & "  FROM `uc_faturamento` "
				lstrSql = lstrSql & " WHERE `jul2010` = 'S' "

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

				If dt.Rows.Count > 0 Then
					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, dt.Rows.Count)
					For Each row As DataRow In dt.Rows

						lstrZon = Trim(IIf(IsDBNull(row.Item("zona")), "", row.Item("zona")))
						lstrLiv = Trim(IIf(IsDBNull(row.Item("livro")), "", row.Item("livro")))
						lstrCon = Trim(IIf(IsDBNull(row.Item("conta")), "", row.Item("conta")))
						llngUId = CInt(IIf(IsDBNull(row.Item("id")), "0", row.Item("id")))
						lstrB02 = Trim(IIf(IsDBNull(row.Item("bx0402")), "N", row.Item("bx0402")))
						lstrEnd = Trim(IIf(IsDBNull(row.Item("end")), "", row.Item("end")))
						lstrNum = Trim(IIf(IsDBNull(row.Item("numero")), "", row.Item("numero")))
						lstrCom = Trim(IIf(IsDBNull(row.Item("complement")), "", row.Item("complement")))
						lstrBai = Trim(IIf(IsDBNull(row.Item("bairro")), "", row.Item("bairro")))
						lstrCid = Trim(IIf(IsDBNull(row.Item("cidade")), "", row.Item("cidade")))
						lstrUf = Trim(IIf(IsDBNull(row.Item("uf")), "", row.Item("uf")))

						'Buscar dados faturados de cada época
						lstrSql = ""
						lstrSql = lstrSql & "SELECT * "
						lstrSql = lstrSql & "  FROM `nf` "
						lstrSql = lstrSql & " WHERE `zona`    = '" & lstrZon & "' "
						lstrSql = lstrSql & "   AND `livro`   = '" & lstrLiv & "' "
						lstrSql = lstrSql & "   AND `conta`   = '" & lstrCon & "' "
						lstrSql = lstrSql & "   AND `unid_id` =  " & llngUId & "  "
						lstrSql = lstrSql & "   AND `mesano`  = '07/2010' "

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

						If dt2.Rows.Count > 0 Then
							For Each row2 As DataRow In dt2.Rows
								llngCId = CInt(IIf(IsDBNull(row2.Item("cons_id")), "0", row2.Item("cons_id")))
								lstrFas = Trim(IIf(IsDBNull(row2.Item("fase")), "M", row2.Item("fase")))
								lstrCCl = Trim(IIf(IsDBNull(row2.Item("codclas")), "", row2.Item("codclas")))
							Next
						End If

						'Buscar TSEE
						lblnTse = False
						lstrSql = ""
						lstrSql = lstrSql & "SELECT * "
						lstrSql = lstrSql & "  FROM `nf` "
						lstrSql = lstrSql & " WHERE `zona`    = '" & lstrZon & "' "
						lstrSql = lstrSql & "   AND `livro`   = '" & lstrLiv & "' "
						lstrSql = lstrSql & "   AND `conta`   = '" & lstrCon & "' "
						lstrSql = lstrSql & "   AND `unid_id` =  " & llngUId & "  "
						lstrSql = lstrSql & "   AND `codclas` = '9105' "
						lstrSql = lstrSql & "   AND `mesano_ref`  > 201111 LIMIT 0, 1"

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

						lblnTse = (dt3.Rows.Count > 0)

						lstrSql = ""
						lstrSql = lstrSql & "SELECT * "
						lstrSql = lstrSql & "  FROM `consum` "
						lstrSql = lstrSql & " WHERE `consreg` = " & llngCId & " "

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

						If dt4.Rows.Count > 0 Then
							For Each row4 As DataRow In dt4.Rows
								lstrNom = Trim(IIf(IsDBNull(row4.Item("nome")), "", row4.Item("nome")))
							Next
						End If


						Select Case lstrFas
							Case Is = "M"
								lstrFas = "1"
							Case Is = "B"
								lstrFas = "2"
							Case Is = "T"
								lstrFas = "3"
							Case Else
								lstrFas = "1"
						End Select

						If lstrCCl = "9104" Then
							If lstrB02 = "S" Then
								lstrTip = "6"
							Else
								lstrTip = "2"
							End If
						ElseIf lstrCCl = "9105" Or lstrCCl = "9106" Then
							If lstrB02 = "S" Then
								lstrTip = "7"
							Else
								lstrTip = "3"
							End If
						Else
							lstrTip = "9"
						End If

						If Not lblnTse Then
							With Me.mfgDados
								.Rows = .Rows + 1
								.set_TextMatrix(.Rows - 1, 0, gstrEmpresa_CodRes89)
								.set_TextMatrix(.Rows - 1, 1, "jul2010")
								.set_TextMatrix(.Rows - 1, 2, lstrZon & lstrLiv & lstrCon)
								.set_TextMatrix(.Rows - 1, 3, lstrFas)
								.set_TextMatrix(.Rows - 1, 4, lstrTip)
								.set_TextMatrix(.Rows - 1, 5, lstrNom)
								.set_TextMatrix(.Rows - 1, 6, lstrEnd & IIf(Trim(lstrNum) <> "", ", " & lstrNum, "") & IIf(Trim(lstrCom) <> "", " - " & lstrCom, "") & IIf(Trim(lstrBai) <> "", " BAIRRO " & lstrBai, ""))
								.set_TextMatrix(.Rows - 1, 7, lstrCid)
								.set_TextMatrix(.Rows - 1, 8, lstrUf)
								'.TextMatrix(.Rows - 1, 9) = lstrCCl
							End With
						End If

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

				Me.mfgDados.Redraw = True
				Me.Cursor = System.Windows.Forms.Cursors.Default
				If Trim(Me.txtArquivo.Text) <> "" Then
					lintARQ = FreeFile()
					FileOpen(lintARQ, Me.txtArquivo.Text, OpenMode.Output)
					With Me.mfgDados
						If .Rows > 1 Then
							Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, .Rows, " Gerando Arquivo ")
							For llngRow = 0 To .Rows - 1
								lstrLin = ""
								For llngCol = 0 To .Cols - 1
									lstrLin = lstrLin & .get_TextMatrix(llngRow, llngCol) & ";"
								Next llngCol
								If CInt(Len(lstrLin)) > 0 Then
									PrintLine(lintARQ, lstrLin)
								End If
								Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1,  , " Gerando Arquivo ")
							Next llngRow
						End If
					End With
					FileClose(lintARQ)
					Call MsgBox("Arquivo gerado com sucesso!", MsgBoxStyle.Information, gstrTituloMensagem)
				End If

			End If
			Call LigDes(True)

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

	End Sub

	Private Sub LigDes(ByVal ablnModo As Boolean)
		Try
			Me.cmdArquivo.Enabled = ablnModo
			Me.cmdExporta.Enabled = ablnModo
			Me.cmdFechar.Enabled = ablnModo
		Catch ex As Exception
			MessageBox.Show("Ocorreu o Erro: " & Err.Description & " - Rotina: LigDes" & "", "Aviso de Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
			Exit Sub
		End Try
	End Sub
End Class