Option Strict Off
Option Explicit On
Friend Class frmReorganizaRotas
	Inherits System.Windows.Forms.Form
	Private ilngRotaRow As Integer
	
	Private Sub mfgDados_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles mfgDados.ClickEvent
		With Me.mfgDados
			'Só permite a alteração na coluna de nova sequencia
			.Col = 3
			'Salva a linha selecionada em uma variavel global
			ilngRotaRow = .Row
			'Passa o valor da célula para o txtEntrada
			Me.txtEntrada.Text = .get_TextMatrix(.Row, 3)
			'Posiciona o txtEntreda em cima da célula selecionada para edição
			Call Me.txtEntrada.SetBounds(VB6.TwipsToPixelsX(.CellLeft + VB6.PixelsToTwipsX(.Left)), VB6.TwipsToPixelsY(.CellTop + VB6.PixelsToTwipsY(.Top)), VB6.TwipsToPixelsX(.CellWidth - 10), VB6.TwipsToPixelsY(.CellHeight - 10))
			Me.txtEntrada.Visible = True
			System.Windows.Forms.Application.DoEvents()
			Me.txtEntrada.Focus()
			Me.txtEntrada.SelectionStart = 0
			Me.txtEntrada.SelectionLength = Len(Me.txtEntrada.Text)
		End With
	End Sub
	
	Private Sub mfgDados_Scroll(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles mfgDados.Scroll
		With Me.mfgDados
			If ilngRotaRow > 0 Then
				Call Me.txtEntrada.SetBounds(VB6.TwipsToPixelsX(.CellLeft + VB6.PixelsToTwipsX(.Left)), VB6.TwipsToPixelsY(.CellTop + VB6.PixelsToTwipsY(.Top)), VB6.TwipsToPixelsX(.CellWidth - 10), VB6.TwipsToPixelsY(.CellHeight - 10))
			End If
		End With
	End Sub
	
	Private Sub txtEntrada_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtEntrada.KeyPress
		Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
		If KeyAscii = System.Windows.Forms.Keys.Return Then
			Me.mfgDados.Focus()
		ElseIf KeyAscii <> System.Windows.Forms.Keys.Back And (KeyAscii < System.Windows.Forms.Keys.D0 Or KeyAscii > System.Windows.Forms.Keys.D9) Then 
			KeyAscii = 0
		End If
		eventArgs.KeyChar = Chr(KeyAscii)
		If KeyAscii = 0 Then
			eventArgs.Handled = True
		End If
	End Sub
	
	Private Sub txtEntrada_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtEntrada.Leave
		Dim lstrAuxiliar As String
		
		With Me.txtEntrada
			lstrAuxiliar = VB6.Format(.Text, "0000")
			.Visible = False
		End With
		With Me.mfgDados
			.Redraw = False
			.set_TextMatrix(ilngRotaRow, 3, lstrAuxiliar)
			Call VerificaAlteracao()
			.Redraw = True
		End With
		ilngRotaRow = -1
	End Sub
	
	Private Sub cmdBuscar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdBuscar.Click
		Dim lstrZon As String
		Dim lstrLiv As String
		Dim lstrCon As String
		Dim lstrNom As String
		Dim llngUId As Integer
		Dim llngSeq As Integer
		Dim lstrSql As String
		Dim llngRow As Integer
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		
		Call CabecalhoGrid()
		Call LigDes(False)
		If Len(VB6.GetItemString(Me.cmbSetor, Me.cmbSetor.SelectedIndex)) > 0 Then
			Me.mfgDados.Redraw = False
			lstrSql = ""
			lstrSql = lstrSql & "SELECT `uc`.`zona`, "
			lstrSql = lstrSql & "       `uc`.`livro`, "
			lstrSql = lstrSql & "       `uc`.`conta`, "
			lstrSql = lstrSql & "       `uc`.`nome`, "
			lstrSql = lstrSql & " RIGHT(`uc`.`rota`, 4) AS `sequencia`, "
			lstrSql = lstrSql & "       `uc`.`unid_id` "
			lstrSql = lstrSql & "  FROM `unid" & IIf(Me.opbGrupoB.Checked, "b", "a") & "` AS `uc` "
			lstrSql = lstrSql & " WHERE RIGHT(LEFT(`uc`.`rota`, 4), 2) = """ & VB6.GetItemString(Me.cmbSetor, Me.cmbSetor.SelectedIndex) & """ "
			lstrSql = lstrSql & " ORDER BY 5 ASC"
			Call Conectar(lstrSql, lrstAux)
			With lrstAux
				If .State > 0 Then
					If .RecordCount > 0 Then
						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"'
							lstrZon = IIf(IsDbNull(.Fields("zona").value), "", .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"'
							lstrLiv = IIf(IsDbNull(.Fields("livro").value), "", .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"'
							lstrCon = IIf(IsDbNull(.Fields("conta").value), "", .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"'
							lstrNom = 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"'
							llngSeq = CInt(IIf(IsDbNull(.Fields("sequencia").value), 0, .Fields("sequencia").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"'
							llngUId = CInt(IIf(IsDbNull(.Fields("unid_id").value), 0, .Fields("unid_id").value))
							With Me.mfgDados
								.Rows = .Rows + 1
								.set_TextMatrix(.Rows - 1, 0, lstrZon & "." & lstrLiv & "." & lstrCon)
								.set_TextMatrix(.Rows - 1, 1, lstrNom)
								.set_TextMatrix(.Rows - 1, 2, VB6.Format(llngSeq, "0000"))
								.set_TextMatrix(.Rows - 1, 4, llngUId)
							End With
							Call .MoveNext()
							Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
						End While
					End If
					Call .Close()
				End If
			End With
			With Me.mfgDados
				For llngRow = 1 To .Rows - 1
					.set_TextMatrix(llngRow, 3, VB6.Format(llngRow, "0000"))
				Next llngRow
				.HighLight = MSFlexGridLib.HighLightSettings.flexHighlightWithFocus
			End With
			Me.mfgDados.Redraw = True
		End If
		Call LigDes(True)
		Call VerificaAlteracao()
		'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 cmdReordenar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdReordenar.Click
		Dim llngCount As Integer
		
		With Me.mfgDados
			If .Rows > 1 Then
				.Redraw = False
				.Col = 3
				.ColSel = 3
				.Row = 0
				.RowSel = 0
				.Sort = MSFlexGridLib.SortSettings.flexSortGenericAscending
				For llngCount = 1 To (.Rows - 1)
					.set_TextMatrix(llngCount, 3, VB6.Format(llngCount, "0000"))
				Next 
				.Redraw = True
			End If
		End With
	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 lstrSeq As String
		Dim lstrOld As String
		Dim lstrZon As String
		Dim lstrLiv As String
		Dim lstrCon As String
		Dim llngUId As Integer
		Dim lstrSql As String
		Dim llngRow As Integer
		
		Call LigDes(False)
		With Me.mfgDados
			If .Rows > 1 Then
				Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 0, .Rows - 1)
				For llngRow = 1 To .Rows - 1
					lstrSeq = .get_TextMatrix(llngRow, 3)
					lstrOld = VB6.Format(.get_TextMatrix(llngRow, 2), "0000")
					lstrZon = Mid(.get_TextMatrix(llngRow, 0), 1, 3)
					lstrLiv = Mid(.get_TextMatrix(llngRow, 0), 5, 2)
					lstrCon = Mid(.get_TextMatrix(llngRow, 0), 8, 6)
					llngUId = CInt(.get_TextMatrix(llngRow, 4))
					If lstrSeq <> lstrOld Then
						lstrSql = ""
						lstrSql = lstrSql & "UPDATE `unid" & IIf(Me.opbGrupoB.Checked, "b", "a") & "` "
						lstrSql = lstrSql & "   SET `rota` = CONCAT(LEFT(`rota`, 4),'" & lstrSeq & "') "
						lstrSql = lstrSql & "WHERE RIGHT(LEFT(`rota`, 4), 2) = '" & VB6.GetItemString(Me.cmbSetor, Me.cmbSetor.SelectedIndex) & "' "
						lstrSql = lstrSql & "  AND `zona`    = '" & lstrZon & "' "
						lstrSql = lstrSql & "  AND `livro`   = '" & lstrLiv & "' "
						lstrSql = lstrSql & "  AND `conta`   = '" & lstrCon & "' "
						lstrSql = lstrSql & "  AND `unid_id` = " & llngUId & " "
						Call Executar(lstrSql)
						If Me.opbGrupoB.Checked Then
							lstrSql = ""
							lstrSql = lstrSql & "UPDATE `leitmensal` "
							lstrSql = lstrSql & "   SET `rota` = CONCAT(LEFT(`rota`, 4),'" & lstrSeq & "') "
							lstrSql = lstrSql & "WHERE RIGHT(LEFT(`rota`, 4), 2) = '" & VB6.GetItemString(Me.cmbSetor, Me.cmbSetor.SelectedIndex) & "' "
							lstrSql = lstrSql & "  AND `zona`    = '" & lstrZon & "' "
							lstrSql = lstrSql & "  AND `livro`   = '" & lstrLiv & "' "
							lstrSql = lstrSql & "  AND `conta`   = '" & lstrCon & "' "
							lstrSql = lstrSql & "  AND `unid_id` = " & llngUId & " "
							Call Executar(lstrSql)
						End If
						
						lstrSql = ""
						lstrSql = lstrSql & "UPDATE " & gstrBDCLinkCenter & ".`uc` "
						lstrSql = lstrSql & "   SET `rota`           = " & CShort(VB6.GetItemString(Me.cmbSetor, Me.cmbSetor.SelectedIndex)) & ", `sequencia` = " & CShort(lstrSeq) & "  "
						lstrSql = lstrSql & " WHERE `zlc`            = '" & lstrZon & lstrLiv & lstrCon & "'"
						lstrSql = lstrSql & "   AND `unid_id_fatwin` = " & llngUId & " "
						Call Executar(lstrSql)
						
						
					End If
					Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
				Next llngRow
				Call MsgBox("Rota " & VB6.GetItemString(Me.cmbSetor, Me.cmbSetor.SelectedIndex) & " foi processada!", MsgBoxStyle.Information, gstrTituloMensagem)
			End If
		End With
		Call LigDes(True)
		Call CabecalhoGrid()
	End Sub
	
	Private Sub frmReorganizaRotas_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 cmdFechar_Click(cmdFechar, 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 frmReorganizaRotas_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		Dim lstrSql As String
		Dim lstrSet As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		
		Call cmbSetor.Items.Clear()
		lstrSql = ""
		lstrSql = lstrSql & "  SELECT DISTINCT RIGHT(LEFT(`rota`, 4), 2) AS `setor` "
		lstrSql = lstrSql & "    FROM `unidb` USE INDEX (`keyrota`) "
		lstrSql = lstrSql & "   UNION ALL "
		lstrSql = lstrSql & "  SELECT DISTINCT RIGHT(LEFT(`rota`, 4), 2) AS `setor` "
		lstrSql = lstrSql & "    FROM `unida` USE INDEX (`keyrota`) "
		lstrSql = lstrSql & "ORDER BY 1 ASC "
		Call Conectar(lstrSql, lrstAux)
		With lrstAux
			If .State > 0 Then
				If .RecordCount > 0 Then
					While Not lrstAux.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"'
						lstrSet = VB6.Format(Trim(IIf(IsDbNull(.Fields("setor").value), "0", .Fields("setor").value)), "00")
						If Len(lstrSet) > 0 Then
							Call Me.cmbSetor.Items.Add(lstrSet)
						Else
							Call Me.cmbSetor.Items.Add("00")
						End If
						Call .MoveNext()
					End While
				End If
				Call .Close()
			End If
		End With
		Me.cmbSetor.SelectedIndex = 0
		'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
		Call CabecalhoGrid()
	End Sub
	
	Private Sub CabecalhoGrid()
		With Me.mfgDados
			.Rows = 1
			Call .Clear()
			.set_ColWidth(0, 1500)
			.set_ColWidth(1, 5800)
			.set_ColWidth(2, 1000)
			.set_ColWidth(3, 1000)
			.set_ColWidth(4, 0)
			.set_ColAlignment(0, MSFlexGridLib.AlignmentSettings.flexAlignCenterCenter)
			.set_ColAlignment(1, MSFlexGridLib.AlignmentSettings.flexAlignLeftCenter)
			.set_ColAlignment(2, MSFlexGridLib.AlignmentSettings.flexAlignRightCenter)
			.set_ColAlignment(3, MSFlexGridLib.AlignmentSettings.flexAlignRightCenter)
			.set_TextMatrix(0, 0, "Identificação")
			.set_TextMatrix(0, 1, "Nome")
			.set_TextMatrix(0, 2, "Sequência")
			.set_TextMatrix(0, 3, "Nova Seq.")
			.set_TextMatrix(0, 4, "Unid_id")
		End With
	End Sub
	
	Private Sub LigDes(ByRef abModo As Boolean)
		Me.cmbSetor.Enabled = abModo
		Me.cmdBuscar.Enabled = abModo
		Me.mfgDados.Enabled = abModo
		Me.cmdProcessar.Enabled = abModo
		Me.cmdFechar.Enabled = abModo
		Me.cmdReordenar.Enabled = abModo
		Me.fmeAux.Enabled = Not (Me.mfgDados.Rows > 1)
	End Sub
	
	Private Sub VerificaAlteracao()
		Dim llngRow As Integer
		Dim llngAux As Integer
		
		With Me.mfgDados
			llngAux = .Row
			If .Rows > 1 Then
				'UPGRADE_ISSUE: VBControlExtender method mfgDados.FillStyle was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
				.FillStyle = MSFlexGridLib.FillStyleSettings.flexFillRepeat
				'.Redraw = False
				For llngRow = 1 To (.Rows - 1)
					If .get_TextMatrix(llngRow, 2) <> .get_TextMatrix(llngRow, 3) Then
						'Pinta de amarelo a linha quando a nova sequencia for diferente
						.Row = llngRow
						.Col = 0
						.ColSel = .Cols - 1
						.CellBackColor = System.Drawing.ColorTranslator.FromOle(&H80FFFF)
					Else
						.Row = llngRow
						.Col = 0
						.ColSel = .Cols - 1
						.CellBackColor = System.Drawing.SystemColors.Window
					End If
				Next 
				'.Redraw = True
				'UPGRADE_ISSUE: VBControlExtender method mfgDados.FillStyle was not upgraded. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="CC4C7EC0-C903-48FC-ACCC-81861D12DA4A"'
				.FillStyle = MSFlexGridLib.FillStyleSettings.flexFillSingle
			End If
			.Row = llngAux
			.Col = 3
		End With
	End Sub
End Class