Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Friend Class frmAcertaFatura
	Inherits System.Windows.Forms.Form
	Private ifrmLocalizaUnidade As frmLocalizaUnidade
	
	Private Sub cmdBuscaIndi_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdBuscaIndi.Click
		Dim lstrZon As String
		Dim lstrLiv As String
		Dim lstrCon As String
		Dim lstrRot As String
		Dim llngUid As Integer
		Dim lstrTar As String
		Dim lstrGru As String
		Dim lstrNom As String
		Dim ldblNNf As Double
		Dim llngMAn As Integer
		Dim lblnCor As Boolean
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		
		Me.txtZona.Text = ""
		Me.txtLivro.Text = ""
		Me.txtConta.Text = ""
		Me.txtNome.Text = ""
		Me.txtGrupo.Text = ""
		Me.txtRota.Text = ""
		Me.txtNewZona.Text = ""
		Me.txtNewLivro.Text = ""
		Me.txtNewConta.Text = ""
		Me.txtNewNome.Text = ""
		Me.txtNewUnidId.Text = ""
		Me.txtNewGrupo.Text = ""
		Me.txtNewRota.Text = ""
		If IsNumeric(Me.txtNumNf.Text) Then
			ldblNNf = CDbl(Me.txtNumNf.Text)
			llngMAn = CInt(VB.Right(Me.txtMesAno.Text, 4) & VB.Left(Me.txtMesAno.Text, 2))
			llngUid = 0
			lstrTar = ""
			lstrGru = ""
			lstrZon = ""
			lstrLiv = ""
			lstrCon = ""
			lstrSql = ""
			lstrSql = lstrSql & "SELECT * " & vbCrLf
			lstrSql = lstrSql & "  FROM `nftransicao` " & vbCrLf
			lstrSql = lstrSql & " WHERE `mesano_ref` = " & llngMAn & vbCrLf
			lstrSql = lstrSql & "   AND `numnf`      = " & ldblNNf
			lstrSql = lstrSql & " LIMIT 0, 1"
			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"'
						llngUid = CInt(IIf(IsDbNull(.Fields("unid_id").value), 0, .Fields("unid_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"'
						lstrTar = Mid(Trim(IIf(IsDbNull(.Fields("ta").value), "01B1", .Fields("ta").value)), 3, 2)
						'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"'
						lstrGru = 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"'
						lstrZon = VB6.Format(Trim(IIf(IsDbNull(.Fields("zona").value), "0", .Fields("zona").value)), "000")
						'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"'
						lstrLiv = VB6.Format(Trim(IIf(IsDbNull(.Fields("livro").value), "0", .Fields("livro").value)), "00")
						'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"'
						lstrCon = VB6.Format(Trim(IIf(IsDbNull(.Fields("conta").value), "0", .Fields("conta").value)), "000000")
						'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"'
						lstrRot = VB6.Format(Trim(IIf(IsDbNull(.Fields("rota").value), "0", .Fields("rota").value)), "00000000")
					End If
					Call .Close()
				End If
				If Len(Trim(lstrGru)) > 0 Then
					lstrSql = ""
					lstrSql = lstrSql & "SELECT * "
					lstrSql = lstrSql & "  FROM `unid" & LCase(lstrGru) & "` "
					lstrSql = lstrSql & " WHERE `unid_id` = " & llngUid
					Call Conectar(lstrSql, 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"'
							lstrNom = IIf(IsDbNull(.Fields("nome").value), "", .Fields("nome").value)
						Else
							lstrNom = ""
						End If
						Call .Close()
					End If
				Else
					lstrNom = ""
				End If
				If llngUid = 0 Then
					Call MsgBox("Esta fatura não existe em faturamento!", MsgBoxStyle.Information, gstrTituloMensagem)
				Else
					Me.txtZona.Text = lstrZon
					Me.txtLivro.Text = lstrLiv
					Me.txtConta.Text = lstrCon
					Me.txtNome.Text = lstrNom
					Me.txtGrupo.Text = lstrGru
					Me.txtRota.Text = lstrRot
				End If
			End With
		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 cmdBuscaZlc_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdBuscaZlc.Click
		Call ifrmLocalizaUnidade.Show()
		Call ifrmLocalizaUnidade.SetaLocalizaUnidade("U", Me)
	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 cmdProcessar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdProcessar.Click
		Dim llngCId As Integer
		Dim lstrRot As String
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		On Error GoTo ErroProcessar
		If MsgBox("Deseja trocar a fatura selecionada do ZLC: " & Me.txtZona.Text & "." & Me.txtLivro.Text & "." & Me.txtConta.Text & " para o ZLC: " & Me.txtNewZona.Text & "." & Me.txtNewLivro.Text & "." & Me.txtNewConta.Text & "?", MsgBoxStyle.YesNo + MsgBoxStyle.DefaultButton2, gstrTituloMensagem) = MsgBoxResult.Yes Then
			lstrSql = ""
			lstrSql = lstrSql & "SELECT * "
			lstrSql = lstrSql & "  FROM `unid" & LCase(Me.txtNewGrupo.Text) & "` "
			lstrSql = lstrSql & " WHERE `unid_id` = " & Me.txtNewUnidId.Text & " "
			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"'
						llngCId = 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"'
						lstrRot = Trim(IIf(IsDbNull(.Fields("rota").value), "", .Fields("rota").value))
					End If
					Call .Close()
				End If
			End With
			If llngCId > 0 Then
				lstrSql = ""
				lstrSql = lstrSql & "UPDATE `nftransicao` SET "
				lstrSql = lstrSql & "       `cons_id` =  " & llngCId & ","
				lstrSql = lstrSql & "       `unid_id` =  " & Me.txtNewUnidId.Text & ","
				lstrSql = lstrSql & "       `zona`    = '" & Me.txtNewZona.Text & "',"
				lstrSql = lstrSql & "       `livro`   = '" & Me.txtNewLivro.Text & "',"
				lstrSql = lstrSql & "       `conta`   = '" & Me.txtNewConta.Text & "',"
				lstrSql = lstrSql & "       `rota`    = '" & lstrRot & "', "
				lstrSql = lstrSql & "       `grupo`   = '" & Me.txtNewGrupo.Text & "' "
				lstrSql = lstrSql & " WHERE `mesano` = '" & Me.txtMesAno.Text & "' " & vbCrLf
				lstrSql = lstrSql & "   AND `numnf`  = " & Me.txtNumNf.Text
				Call Executar(lstrSql)
				Call MsgBox("Processo de troca realizado com sucesso!", MsgBoxStyle.Information, gstrTituloMensagem)
				Call MsgBox("Favor reprocessar a fatura.", MsgBoxStyle.Information, gstrTituloMensagem)
				Me.txtZona.Text = ""
				Me.txtLivro.Text = ""
				Me.txtConta.Text = ""
				Me.txtNome.Text = ""
				Me.txtGrupo.Text = ""
				Me.txtRota.Text = ""
				Me.txtNewZona.Text = ""
				Me.txtNewLivro.Text = ""
				Me.txtNewConta.Text = ""
				Me.txtNewNome.Text = ""
				Me.txtNewUnidId.Text = ""
				Me.txtNewGrupo.Text = ""
				Me.txtNewRota.Text = ""
				Me.txtNumNf.Text = ""
				Me.cmdProcessar.Enabled = False
				System.Windows.Forms.Application.DoEvents()
				If Me.txtNewGrupo.Text = "B" Then
					If frmVerifContaGB.Visible = False Then
						Call CenterForm(frmVerifContaGB)
						Call frmVerifContaGB.Show()
					End If
				Else
					If frmFatGrupoA.Visible = False Then
						Call CenterForm(frmFatGrupoA)
						Call frmFatGrupoA.Show()
					End If
				End If
			Else
				Call MsgBox("Problema com o consumidor favor selecionar o novo consumidor novamente!", MsgBoxStyle.Information, gstrTituloMensagem)
			End If
		End If
ErroProcessar: 
		With myConC
			If .Errors.Count > 0 Then
				Call MsgBox("Código: " & .Errors.Item(.Errors.Count - 1).NativeError & vbCrLf & "Descrição:" & .Errors.Item(.Errors.Count - 1).Description, MsgBoxStyle.Exclamation, gstrTituloMensagem)
			ElseIf Err.Number > 0 Then 
				Call MsgBox(Err.Number & " - " & Err.Description, MsgBoxStyle.Exclamation, gstrTituloMensagem)
			End If
		End With
	End Sub
	
	Private Sub frmAcertaFatura_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		Dim lstrMesAno As String
		Call CenterForm(Me)
		lstrMesAno = Trim(LeConfig("MESANOFATURAMENTO"))
		If lstrMesAno = "" Or lstrMesAno = "-1" Then
			Call MsgBox("Não foi definido o mês e ano de faturamento!", MsgBoxStyle.Information, gstrTituloMensagem)
			Call Me.Close()
		End If
		Me.txtMesAno.Text = lstrMesAno
		ifrmLocalizaUnidade = New frmLocalizaUnidade
		Me.cmdProcessar.Enabled = False
	End Sub
	
	Private Sub frmAcertaFatura_FormClosed(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
		'UPGRADE_NOTE: Object ifrmLocalizaUnidade 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"'
		ifrmLocalizaUnidade = Nothing
	End Sub
	
	Public Sub SetaCodigo(ByVal alngUnidId As Integer, ByVal astrZona As String, ByVal astrLivro As String, ByVal astrConta As String, ByVal astrGrupo As String)
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		Me.txtNewZona.Text = astrZona
		Me.txtNewLivro.Text = astrLivro
		Me.txtNewConta.Text = astrConta
		Me.txtNewGrupo.Text = astrGrupo
		Me.txtNewUnidId.Text = CStr(alngUnidId)
		lstrSql = ""
		lstrSql = lstrSql & "SELECT * "
		lstrSql = lstrSql & "  FROM `unid" & LCase(astrGrupo) & "` "
		lstrSql = lstrSql & " WHERE `unid_id` = " & alngUnidId
		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"'
					Me.txtNewNome.Text = 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"'
					Me.txtNewRota.Text = Trim(IIf(IsDbNull(.Fields("rota").value), "", .Fields("rota").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
		'If Me.txtNewGrupo.Text <> Me.txtGrupo.Text Then
		'  Call MsgBox("O Grupo não pode ser diferente!", vbInformation, gstrTituloMensagem)
		'  Me.cmdProcessar.Enabled = False
		'Else
		Call HabilitaProcesso()
		'End If
	End Sub
	
	Private Sub HabilitaProcesso()
		If Trim(Me.txtNewZona.Text) <> "" And Trim(Me.txtNewLivro.Text) <> "" And Trim(Me.txtNewConta.Text) <> "" And Trim(Me.txtNewUnidId.Text) <> "" And Trim(Me.txtNewGrupo.Text) <> "" Then
			Me.cmdProcessar.Enabled = True
		Else
			Me.cmdProcessar.Enabled = False
		End If
	End Sub
End Class