Option Strict Off
Option Explicit On
Friend Class frmAcertaUnidade
	Inherits System.Windows.Forms.Form
	
	Private Sub cmdSair_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdSair.Click
		Call Me.Close()
	End Sub
	
	Private Sub BuscaTabelas()
		Dim lstrTab1 As String
		Dim lstrCol1 As String
		Dim lstrTab2 As String
		Dim lstrCol2 As String
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		
		Me.mfgTabelas.set_ColWidth(0, 2000)
		Me.mfgTabelas.set_ColWidth(1, 950)
		Me.mfgTabelas.set_ColWidth(2, 950)
		Me.mfgTabelas.set_ColWidth(3, 300)
		Call Me.mfgTabelas.Clear()
		Me.mfgTabelas.Rows = 0
		lstrSql = ""
		lstrSql = lstrSql & "SELECT DISTINCT table_name, column_name "
		lstrSql = lstrSql & "  FROM information_schema.Columns "
		lstrSql = lstrSql & " WHERE information_schema.columns.TABLE_SCHEMA = 'fatwin' "
		lstrSql = lstrSql & "   AND (information_schema.columns.COLUMN_NAME IN('cons_id','unid_id') OR "
		lstrSql = lstrSql & "        information_schema.columns.COLUMN_NAME IN('cons_id_new','unid_id_new') OR "
		lstrSql = lstrSql & "        information_schema.columns.COLUMN_NAME IN('id_consumidor','id_unid') OR "
		lstrSql = lstrSql & "        information_schema.columns.COLUMN_NAME IN('consid','unidid') OR "
		lstrSql = lstrSql & "        information_schema.columns.COLUMN_NAME IN('cons_id_old','unid_id_old')) "
		Call Conectar(lstrSql, lrstAux)
		With lrstAux
			If .State > 0 Then
				If .RecordCount > 0 Then
					Me.mfgTabelas.Redraw = False
					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"'
						lstrTab1 = IIf(IsDbNull(.Fields("table_name").value), "", .Fields("table_name").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"'
						lstrCol1 = IIf(IsDbNull(.Fields("column_name").value), "", .Fields("column_name").value)
						Call .MoveNext()
						If Not .EOF 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"'
							lstrTab2 = IIf(IsDbNull(.Fields("table_name").value), "", .Fields("table_name").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"'
							lstrCol2 = IIf(IsDbNull(.Fields("column_name").value), "", .Fields("column_name").value)
						Else
							lstrTab2 = ""
							lstrCol2 = ""
						End If
						If lstrTab1 = lstrTab2 And lstrCol1 <> lstrCol2 Then
							With Me.mfgTabelas
								.Rows = .Rows + 1
								.set_TextMatrix(.Rows - 1, 0, lstrTab1)
								.set_TextMatrix(.Rows - 1, 1, lstrCol1)
								.set_TextMatrix(.Rows - 1, 2, lstrCol2)
							End With
						End If
						Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
					End While
					Me.mfgTabelas.Redraw = True
				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
	End Sub
	
	Private Sub cmdTransferir_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdTransferir.Click
		Dim lstrSql As String
		Dim lstrAux As String
		Dim llngRow As Integer
		Dim lstrTab As String
		Dim lstrCo1 As String
		Dim lstrCo2 As String
		
		If Len(Trim(Me.txtConsumOld.Text)) = 0 Then
			Call MsgBox("Favor informar a unidade de Origem para transferencia", MsgBoxStyle.Information, gstrTituloMensagem)
			Call Me.txtConsumOld.Focus()
		ElseIf Len(Trim(txtConsumNew.Text)) = 0 Then 
			Call MsgBox("Favor informar o consumidor de Destinho para receber a transferencia", MsgBoxStyle.Information, gstrTituloMensagem)
			Call Me.txtConsumNew.Focus()
		Else
			Call BuscaQtdeUc(CInt(Me.txtConsumOld.Text), (Me.lblNmConsumOld), (Me.lblQtdeUCOld))
			Call BuscaQtdeUc(CInt(Me.txtConsumNew.Text), (Me.lblNmConsumNew), (Me.lblQtdeUCNew))
			Call BuscaTabelas()
			With Me.mfgTabelas
				Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, .Rows)
				For llngRow = 0 To .Rows - 1
					lstrTab = .get_TextMatrix(llngRow, 0)
					lstrCo1 = .get_TextMatrix(llngRow, 1)
					lstrCo2 = .get_TextMatrix(llngRow, 2)
					lstrSql = "UPDATE " & lstrTab & " SET " & lstrCo1 & " = " & CInt(Me.txtConsumNew.Text) & " WHERE " & lstrCo1 & " = " & CInt(Me.txtConsumOld.Text) & " AND " & lstrCo2 & " = " & CInt(Me.txtUnidadeOld.Text)
					Call ExecutaSQL(lstrSql)
					.set_TextMatrix(llngRow, 3, "X")
					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
				Next llngRow
			End With
			Call MsgBox("Transferencia completada", MsgBoxStyle.Information, gstrTituloMensagem)
			Call Me.txtConsumOld.Focus()
		End If
	End Sub
	
	Private Sub ExecutaSQL(ByRef astrAux As String)
		On Error GoTo ErroExecucao
		
		Call Executar(astrAux)
		Exit Sub
		
ErroExecucao: 
		MsgBox(Err.Number & " - " & Err.Description)
	End Sub
	
	Private Sub frmAcertaUnidade_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
		Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
		If KeyAscii = System.Windows.Forms.Keys.Escape Then
			Call cmdSair_Click(cmdSair, New System.EventArgs())
		ElseIf KeyAscii = System.Windows.Forms.Keys.Return Then 
			Call SendKeysA(System.Windows.Forms.Keys.Tab, True)
		End If
		eventArgs.KeyChar = Chr(KeyAscii)
		If KeyAscii = 0 Then
			eventArgs.Handled = True
		End If
	End Sub
	
	Private Sub txtConsumNew_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtConsumNew.Enter
		With txtConsumNew
			.SelectionStart = 0
			.SelectionLength = Len(.Text)
		End With
	End Sub
	
	Private Sub txtConsumOld_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtConsumOld.Enter
		With txtConsumOld
			.SelectionStart = 0
			.SelectionLength = Len(.Text)
		End With
	End Sub
	
	Private Sub txtUnidadeOld_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtUnidadeOld.Enter
		With txtUnidadeOld
			.SelectionStart = 0
			.SelectionLength = Len(.Text)
		End With
	End Sub
	
	Private Sub BuscaQtdeUc(ByVal alngConsId As Integer, ByRef alblNome As System.Windows.Forms.Label, ByRef alblQtde As System.Windows.Forms.Label)
		Dim llngQtd As Integer
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		alblNome.Text = ""
		alblQtde.Text = CStr(0)
		If Len(alngConsId) > 0 Then
			lstrSql = ""
			lstrSql = lstrSql & "SELECT `consum`.`nome` "
			lstrSql = lstrSql & "  FROM `consum` "
			lstrSql = lstrSql & " WHERE `consum`.`consreg` = " & alngConsId
			Call Conectar(lstrSql, lrstAux)
			With lrstAux
				If .State > 0 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"'
						alblNome.Text = IIf(IsDbNull(.Fields("nome").value), "", .Fields("nome").value)
					End If
					Call .Close()
				End If
			End With
			lstrSql = ""
			lstrSql = lstrSql & "SELECT COUNT(*) AS `qtde` "
			lstrSql = lstrSql & "  FROM `unidb` "
			lstrSql = lstrSql & " WHERE `unidb`.`cons_id` = " & alngConsId
			Call Conectar(lstrSql, lrstAux)
			With lrstAux
				If .State > 0 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"'
						alblQtde.Text = CStr(CInt(IIf(IsDbNull(.Fields("qtde").value), 0, .Fields("qtde").value)))
					End If
					Call .Close()
				End If
			End With
			lstrSql = ""
			lstrSql = lstrSql & "SELECT COUNT(*) AS `qtde` "
			lstrSql = lstrSql & "  FROM `unida` "
			lstrSql = lstrSql & " WHERE `unida`.`cons_id` = " & alngConsId
			Call Conectar(lstrSql, lrstAux)
			With lrstAux
				If .State > 0 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"'
						alblQtde.Text = CStr(CDbl(alblQtde.Text) + CInt(IIf(IsDbNull(.Fields("qtde").value), 0, .Fields("qtde").value)))
					End If
					Call .Close()
				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
End Class