Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Friend Class frmControlePedidoPrazo
	Inherits System.Windows.Forms.Form
	Private ifrmQuemChama As System.Windows.Forms.Form
	Private icrpPedidoPrazo As AxCrystal.AxCrystalReport
	Private ilngConsId As Integer
	Private ilngUnidId As Integer
	Private istrZona As String
	Private istrLivro As String
	Private istrConta As String
	Private istrFase As String
	Private istrGrupo As String
	Private istrNome As String
	Private istrEnde As String
	Private istrNume As String
	
	Public Sub SetaParametros(ByRef afrmQuemChama As System.Windows.Forms.Form, ByVal astrMesAno As String, ByVal adblNumNf As Double, ByVal astrSolicitante As String, ByRef acrpPedidoPrazo As AxCrystal.AxCrystalReport)
		Dim ldtePrazo As Date
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		ifrmQuemChama = afrmQuemChama
		icrpPedidoPrazo = acrpPedidoPrazo
		ifrmQuemChama.Enabled = False
		Me.lblMesAno.Text = astrMesAno
		Me.lblNumNf.Text = VB6.Format(adblNumNf, "#,##0")
		Me.dtpPrazo.value = VB6.Format(BuscaDataBD, "DD/MM/YYYY")
		Me.txtSolicitante.Text = Str_Retira(astrSolicitante, "~`!@#$%^&*()_-+=[{]};:'<,.>/?", " ")
		lstrSql = ""
		lstrSql = lstrSql & "SELECT `nf`.*, "
		lstrSql = lstrSql & "       IFNULL(IFNULL(`unida`.`nome`,`unidb`.`nome`),`consum`.`nome`) `nome`, "
		lstrSql = lstrSql & "       IFNULL(IFNULL(`unida`.`end`,`unidb`.`end`),`consum`.`end`) `end`, "
		lstrSql = lstrSql & "       IFNULL(IFNULL(`unida`.`numero`,`unidb`.`numero`),`consum`.`numero`) `numero` "
		lstrSql = lstrSql & "  FROM `nf` "
		lstrSql = lstrSql & "  LEFT JOIN `unida` ON(`unida`.`unid_id` = `nf`.`unid_id` AND `unida`.`cons_id` = `nf`.`cons_id` AND `nf`.`grupo` = 'A') "
		lstrSql = lstrSql & "  LEFT JOIN `unidb` ON(`unidb`.`unid_id` = `nf`.`unid_id` AND `unidb`.`cons_id` = `nf`.`cons_id` AND `nf`.`grupo` = 'B') "
		lstrSql = lstrSql & "  LEFT JOIN `consum` ON(`consum`.`consreg` = `nf`.`cons_id`) "
		lstrSql = lstrSql & " WHERE `mesano` = '" & Me.lblMesAno.Text & "' "
		lstrSql = lstrSql & "   AND `mesano_ref` = " & CInt(VB.Right(Me.lblMesAno.Text, 4) & VB.Left(Me.lblMesAno.Text, 2)) & " "
		lstrSql = lstrSql & "   AND `numnf` = " & CDbl(Me.lblNumNf.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"'
					istrNome = 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"'
					ilngConsId = 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"'
					ilngUnidId = 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"'
					istrZona = Trim(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"'
					istrLivro = Trim(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"'
					istrConta = Trim(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"'
					istrFase = Trim(IIf(IsDbNull(.Fields("fase").value), "M", .Fields("fase").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"'
					istrGrupo = 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"'
					istrEnde = 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"'
					istrNume = 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"'
					ldtePrazo = CDate(IIf(IsDbNull(.Fields("dataprazo").value), "1900-01-01", .Fields("dataprazo").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.lblVencimento.Text = VB6.Format(CDate(IIf(IsDbNull(.Fields("datavenc").value), "1900-01-01", .Fields("datavenc").value)), "DD/MM/YYYY")
					'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.lblValor.Text = VB6.Format(CDbl(IIf(IsDbNull(.Fields("valortot").value), "0", .Fields("valortot").value)), "#,##0.00")
				End If
				Call .Close()
			End If
		End With
		If ldtePrazo = CDate("01/01/1900") Then
			Me.labelStatusPrazo.Text = "Status: prazo não definido."
		Else
			Me.labelStatusPrazo.Text = "Status: o prazo para esta fatura já foi definido para " & VB6.Format(ldtePrazo, "DD/MM/YYYY") & "."
		End If
		'Call Me.dtpPrazo.SetFocus
		'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
		ifrmQuemChama.Enabled = True
		Call Me.Close()
	End Sub
	
	Private Sub cmdSalvar_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdSalvar.Click
		Dim lstrSql As String
		Dim lstrCriterio As String
		'----- Atualiza a data de prazo na tabela NF -------------------------------------------------------------
		lstrSql = ""
		lstrSql = lstrSql & "UPDATE `nf` "
		lstrSql = lstrSql & "   SET `dataprazo`   = '" & VB6.Format(Me.dtpPrazo.value, "yyyy-MM-dd") & "' "
		lstrSql = lstrSql & " WHERE `nf`.`numnf`  = " & CDbl(Me.lblNumNf.Text) & " "
		lstrSql = lstrSql & "   AND `nf`.`mesano` = '" & Trim(Me.lblMesAno.Text) & "' "
		Call Executar(lstrSql)
		lstrSql = ""
		lstrSql = lstrSql & "UPDATE `concorte` "
		lstrSql = lstrSql & "   SET `dataprazo` = '" & VB6.Format(Me.dtpPrazo.value, "yyyy-MM-dd") & "' "
		lstrSql = lstrSql & " WHERE `concorte`.`numnf`  = " & CDbl(Me.lblNumNf.Text) & " "
		lstrSql = lstrSql & "   AND `concorte`.`mesano` = '" & Trim(Me.lblMesAno.Text) & "' "
		Call Executar(lstrSql)
		'---------------------------------------------------------------------------------------------------------
		'----- Atualiza histórico da unidade consumidora ---------------------------------------------------------
		Call AtualizaHistoricoUnidade(ilngUnidId, istrGrupo, "PRORROGADO PRAZO DA NOTA " & Me.lblNumNf.Text & " " & Me.lblMesAno.Text & " PARA " & VB6.Format(Me.dtpPrazo.value, "dd/mm/yyyy") & " EM " & VB6.Format(BuscaDataBD, "dd/mm/yyyy") & " POR " & UCase(Trim(Me.txtSolicitante.Text)) & ".")
		Call AtualizaHistoricoFatura(CDbl(Me.lblNumNf.Text), Trim(Me.lblMesAno.Text), CInt(VB.Right(Me.lblMesAno.Text, 4) & VB.Left(Me.lblMesAno.Text, 2)), "Pedido Prazo", Me.txtSolicitante.Text, Me.txtMotivo.Text)
		'---------------------------------------------------------------------------------------------------------
		Me.labelStatusPrazo.Text = "Status: Gravação concluída!"
		'----- Chamada do .rpt -----------------------------------------------------------------------------------
		lstrCriterio = "{unidb.unid_id} = " & ilngUnidId & " AND {nf.numnf} = " & Me.lblNumNf.Text & " AND {nf.mesano} = '" & Me.lblMesAno.Text & "'"
		With icrpPedidoPrazo
			.ReportFileName = My.Application.Info.DirectoryPath & "\" & LeConfig("DIR_RPT") & LeConfig("REL_PEDIDO_DE_PRAZO")
			.DiscardSavedData = True
			.WindowState = Crystal.WindowStateConstants.crptMaximized
			.WindowControlBox = True
			.WindowControls = True
			.SelectionFormula = lstrCriterio
			.set_Formulas(1, "NmSolicitante = """ & UCase(Me.txtSolicitante.Text) & """ ")
			.set_Formulas(2, "Nome = """ & istrNome & """ ")
			.set_Formulas(3, "Endereco = """ & istrEnde & IIf(istrNume <> "", ", " & istrNume, "") & """ ")
			.set_Formulas(4, "Identificacao = """ & istrZona & "." & istrLivro & "." & istrConta & """ ")
			.set_Formulas(5, "MesAnoFaturamento = """ & Trim(Me.lblMesAno.Text) & """ ")
			.set_Formulas(6, "Valor = """ & Me.lblValor.Text & """ ")
			.set_Formulas(7, "Vencimento = """ & Me.lblVencimento.Text & """ ")
			.WindowShowPrintSetupBtn = True
			.WindowShowCloseBtn = True
			.WindowShowSearchBtn = True
			.WindowTitle = "Pedido de Prazo Para Pagamento de Conta de Energia Elétrica"
			.Destination = Crystal.DestinationConstants.crptToWindow
			.Action = 0
		End With
	End Sub
	
	Private Sub frmControlePedidoPrazo_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		Call CenterFormQuemChama(Me, ifrmQuemChama)
	End Sub
	
	Private Sub frmControlePedidoPrazo_FormClosed(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
		If ifrmQuemChama Is Nothing Then
			'
		Else
			ifrmQuemChama.Enabled = True
		End If
	End Sub
	
	Private Sub txtMotivo_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtMotivo.Enter
		With Me.txtMotivo
			.SelectionStart = 0
			.SelectionLength = CInt(Len(.Text))
		End With
	End Sub
	
	Private Sub txtMotivo_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtMotivo.KeyPress
		Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
		Dim lintAux As Short
		Dim lstrAux As String
		lstrAux = UCase(Chr(KeyAscii))
		lintAux = AscW(lstrAux)
		If lintAux <> System.Windows.Forms.Keys.Back Then
			If lintAux <> System.Windows.Forms.Keys.Space Then
				If (lintAux < System.Windows.Forms.Keys.A Or lintAux > System.Windows.Forms.Keys.Z) Then
					If InStr(1, "ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜàáâãäåçèéêëìíîïòóôõöùúûü", lstrAux) = 0 Then
						KeyAscii = 0
					End If
				End If
			End If
		End If
		eventArgs.KeyChar = Chr(KeyAscii)
		If KeyAscii = 0 Then
			eventArgs.Handled = True
		End If
	End Sub
	
	Private Sub txtMotivo_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtMotivo.Leave
		Me.txtMotivo.Text = Str_Arruma(UCase(Trim(Me.txtMotivo.Text)))
	End Sub
	
	Private Sub txtSolicitante_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtSolicitante.Enter
		With Me.txtSolicitante
			.SelectionStart = 0
			.SelectionLength = CInt(Len(.Text))
		End With
	End Sub
	
	Private Sub txtSolicitante_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtSolicitante.KeyPress
		Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
		Dim lintAux As Short
		Dim lstrAux As String
		lstrAux = UCase(Chr(KeyAscii))
		lintAux = AscW(lstrAux)
		If lintAux <> System.Windows.Forms.Keys.Back Then
			If lintAux <> System.Windows.Forms.Keys.Space Then
				If (lintAux < System.Windows.Forms.Keys.A Or lintAux > System.Windows.Forms.Keys.Z) Then
					If InStr(1, "ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜàáâãäåçèéêëìíîïòóôõöùúûü", lstrAux) = 0 Then
						KeyAscii = 0
					End If
				End If
			End If
		End If
		eventArgs.KeyChar = Chr(KeyAscii)
		If KeyAscii = 0 Then
			eventArgs.Handled = True
		End If
	End Sub
	
	Private Sub txtSolicitante_Leave(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtSolicitante.Leave
		Me.txtSolicitante.Text = UCase(Me.txtSolicitante.Text)
	End Sub
End Class