Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Friend Class frmRelReativosDemandaUltrap
	Inherits System.Windows.Forms.Form
	
	Private Sub cmdEmitir_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdEmitir.Click
		Dim lstrCriterio As String
		Dim lstrCaminho As String
		Dim lstrAuxNF As String
		Dim lstrGrTar As String
		Dim lstrCdAux As String
		Dim lstrDsAux As String
		Dim lstrCdClas As String
		Dim lstrDsClas As String
		Dim lstrItens As String
		Dim ldblkWhPonta As Double
		Dim ldblkWhFponta As Double
		Dim ldblkWPonta As Double
		Dim ldblkWFponta As Double
		Dim ldblQtdkWUPonta As Double
		Dim ldblQtdkWUFponta As Double
		Dim ldblVlrkWUPonta As Double
		Dim ldblVlrkWUFponta As Double
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		Me.dtpMesAno.Enabled = False
		Me.cmdEmitir.Enabled = False
		Me.cmdFechar.Enabled = False
		lstrCaminho = My.Application.Info.DirectoryPath & "\" & LeConfig("DIR_RPT") & "RelReativosDemandaUltrap.rpt"
		lstrCriterio = "{temp_reativos.nomemaquina} = """ & gstrNmEstacao & """"
		Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
		Me.lblInfo.Text = " Aguarde buscando as faturas..."
		Call Me.lblInfo.Refresh()
		Call Executar("DELETE FROM temp_reativos WHERE nomemaquina = '" & gstrNmEstacao & "' ")
		lstrSql = ""
		lstrSql = lstrSql & "SELECT numnf, mesano, mesano_ref, zona, livro, conta, cons_id, unid_id, codclas, ta, itensserv "
		lstrSql = lstrSql & "  FROM nf "
		lstrSql = lstrSql & " WHERE mesano_ref =  " & CInt(VB6.Format(Me.dtpMesAno.value, "yyyymm")) & " "
		lstrSql = lstrSql & " ORDER BY codclas ASC "
		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"'
					'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
					lstrCdAux = Trim(IIf(IsDbNull(.Fields("codclas").value), "0000", .Fields("codclas").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"'
					'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
					lstrGrTar = Trim(IIf(IsDbNull(.Fields("ta").value), "", .Fields("ta").value))
					Call BuscaCdDsClasse(lstrCdAux, lstrDsAux, lstrGrTar)
					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"'
						'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
						lstrCdClas = Trim(IIf(IsDbNull(.Fields("codclas").value), "0000", .Fields("codclas").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"'
						'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
						lstrItens = Trim(IIf(IsDbNull(.Fields("itensserv").value), "", .Fields("itensserv").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"'
						'UPGRADE_WARNING: Couldn't resolve default property of object Interaction.IIf(). Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
						lstrGrTar = Trim(IIf(IsDbNull(.Fields("ta").value), "", .Fields("ta").value))
						Call BuscaCdDsClasse(lstrCdClas, lstrDsClas, lstrGrTar)
						Call .MoveNext()
						If (lstrCdClas <> lstrCdAux Or .EOF) Then
							If (ldblkWhPonta + ldblkWhFponta + ldblkWPonta + ldblkWFponta + ldblVlrkWUPonta + ldblVlrkWUFponta) <> 0 Then
								lstrSql = ""
								lstrSql = lstrSql & "INSERT INTO `temp_reativos` SET " & vbCrLf
								lstrSql = lstrSql & "  `nomemaquina` = '" & gstrNmEstacao & "', "
								lstrSql = lstrSql & "  `cdclas` = '" & lstrCdAux & "', "
								lstrSql = lstrSql & "  `dsclas` = '" & lstrDsAux & "', "
								lstrSql = lstrSql & "  `vlr_consumo_reativo_ponta` = " & Replace(CStr(ldblkWhPonta), ",", ".") & ", "
								lstrSql = lstrSql & "  `vlr_consumo_reativo_fponta` = " & Replace(CStr(ldblkWhFponta), ",", ".") & ", "
								lstrSql = lstrSql & "  `vlr_demanda_reativo_ponta` = " & Replace(CStr(ldblkWPonta), ",", ".") & ", "
								lstrSql = lstrSql & "  `vlr_demanda_reativo_fponta` = " & Replace(CStr(ldblkWFponta), ",", ".") & ", "
								lstrSql = lstrSql & "  `qtd_demanda_ultrap_ponta` = " & Replace(CStr(ldblQtdkWUPonta), ",", ".") & ", "
								lstrSql = lstrSql & "  `qtd_demanda_ultrap_fponta` = " & Replace(CStr(ldblQtdkWUFponta), ",", ".") & ", "
								lstrSql = lstrSql & "  `vlr_demanda_ultrap_ponta` = " & Replace(CStr(ldblVlrkWUPonta), ",", ".") & ", "
								lstrSql = lstrSql & "  `vlr_demanda_ultrap_fponta` = " & Replace(CStr(ldblVlrkWUFponta), ",", ".") & " "
								Call Executar(lstrSql)
							End If
							ldblkWhPonta = 0
							ldblkWhFponta = 0
							ldblkWPonta = 0
							ldblkWFponta = 0
							ldblQtdkWUPonta = 0
							ldblQtdkWUFponta = 0
							ldblVlrkWUPonta = 0
							ldblVlrkWUFponta = 0
							lstrCdAux = lstrCdClas
							lstrDsAux = lstrDsClas
						End If
						'Consumo Reativo
						ldblkWhPonta = ldblkWhPonta + BuscaVlrItem(lstrItens, "0302020013")
						ldblkWhFponta = ldblkWhFponta + BuscaVlrItem(lstrItens, "0302020014") + BuscaVlrItem(lstrItens, "0302020012")
						'Demanda Reativa
						ldblkWPonta = ldblkWPonta + BuscaVlrItem(lstrItens, "0302020027")
						ldblkWFponta = ldblkWFponta + BuscaVlrItem(lstrItens, "0302020028") + BuscaVlrItem(lstrItens, "0302020031")
						'Demanda de Ultrapassagem
						ldblQtdkWUPonta = ldblQtdkWUPonta + BuscaVlrItem(lstrItens, "0302010022")
						ldblVlrkWUPonta = ldblVlrkWUPonta + BuscaVlrItem(lstrItens, "0302020022")
						ldblQtdkWUFponta = ldblQtdkWUFponta + BuscaVlrItem(lstrItens, "0302010026") + BuscaVlrItem(lstrItens, "0302010018")
						ldblVlrkWUFponta = ldblVlrkWUFponta + BuscaVlrItem(lstrItens, "0302020026") + BuscaVlrItem(lstrItens, "0302020018")
						Call BarraProgresso((Me.ProgressBar), (Me.lblPerc), (Me.lblInfo), 1)
					End While
					If (ldblkWhPonta + ldblkWhFponta + ldblkWPonta + ldblkWFponta + ldblVlrkWUPonta + ldblVlrkWUFponta) <> 0 Then
						lstrSql = ""
						lstrSql = lstrSql & "INSERT INTO `temp_reativos` SET " & vbCrLf
						lstrSql = lstrSql & "  `nomemaquina` = '" & gstrNmEstacao & "', "
						lstrSql = lstrSql & "  `cdclas` = '" & lstrCdClas & "', "
						lstrSql = lstrSql & "  `dsclas` = '" & lstrDsClas & "', "
						lstrSql = lstrSql & "  `vlr_consumo_reativo_ponta` = " & Replace(CStr(ldblkWhPonta), ",", ".") & ", "
						lstrSql = lstrSql & "  `vlr_consumo_reativo_fponta` = " & Replace(CStr(ldblkWhFponta), ",", ".") & ", "
						lstrSql = lstrSql & "  `vlr_demanda_reativo_ponta` = " & Replace(CStr(ldblkWPonta), ",", ".") & ", "
						lstrSql = lstrSql & "  `vlr_demanda_reativo_fponta` = " & Replace(CStr(ldblkWFponta), ",", ".") & ", "
						lstrSql = lstrSql & "  `qtd_demanda_ultrap_ponta` = " & Replace(CStr(ldblQtdkWUPonta), ",", ".") & ", "
						lstrSql = lstrSql & "  `qtd_demanda_ultrap_fponta` = " & Replace(CStr(ldblQtdkWUFponta), ",", ".") & ", "
						lstrSql = lstrSql & "  `vlr_demanda_ultrap_ponta` = " & Replace(CStr(ldblVlrkWUPonta), ",", ".") & ", "
						lstrSql = lstrSql & "  `vlr_demanda_ultrap_fponta` = " & Replace(CStr(ldblVlrkWUFponta), ",", ".") & " "
						Call Executar(lstrSql)
						ldblkWhPonta = 0
						ldblkWhFponta = 0
						ldblkWPonta = 0
						ldblkWFponta = 0
						ldblQtdkWUPonta = 0
						ldblQtdkWUFponta = 0
						ldblVlrkWUPonta = 0
						ldblVlrkWUFponta = 0
						lstrCdAux = lstrCdClas
						lstrDsAux = lstrDsClas
					End If
					Me.lblInfo.Text = Me.lblInfo.Text & " - Pronto!"
					Call Me.lblInfo.Refresh()
					With crpReativos
						.ReportFileName = lstrCaminho
						.DiscardSavedData = True
						.WindowState = Crystal.WindowStateConstants.crptMaximized
						.WindowControlBox = True
						.WindowControls = True
						.WindowShowPrintSetupBtn = True
						.WindowShowCloseBtn = True
						.WindowShowSearchBtn = True
						.SelectionFormula = lstrCriterio
						.WindowTitle = "Relatório dos reativos e demanda de ultrapassagem"
						.Destination = Crystal.DestinationConstants.crptToWindow
						.set_Formulas(1, "fn_cabecalho = """ & "Listagem Reativos e Demanda de Ultrapassagem em " & VB6.Format(Me.dtpMesAno.value, "mm/yyyy") & """ ")
						.Action = 0
					End With
				Else
					Me.lblInfo.Text = " Não foi encontrado nenhuma informação!"
					Call Me.lblInfo.Refresh()
				End If
				Call .Close()
			End If
		End With
		Me.Cursor = System.Windows.Forms.Cursors.Arrow
		'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
		Me.dtpMesAno.Enabled = True
		Me.cmdEmitir.Enabled = True
		Me.cmdFechar.Enabled = True
	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 frmRelReativosDemandaUltrap_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 frmRelReativosDemandaUltrap_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		Dim lstrSql As String
		Me.dtpMesAno.value = BuscaDataBD
		lstrSql = ""
		lstrSql = lstrSql & "CREATE TABLE IF NOT EXISTS `temp_reativos` ( " & vbCrLf
		lstrSql = lstrSql & "  `nomemaquina` varchar(255) DEFAULT NULL, " & vbCrLf
		lstrSql = lstrSql & "  `cdclas` varchar(5) DEFAULT NULL, " & vbCrLf
		lstrSql = lstrSql & "  `dsclas` varchar(100) DEFAULT NULL, " & vbCrLf
		lstrSql = lstrSql & "  `vlr_consumo_reativo_ponta` double(14,2) DEFAULT 0, " & vbCrLf
		lstrSql = lstrSql & "  `vlr_consumo_reativo_fponta` double(14,2) DEFAULT 0, " & vbCrLf
		lstrSql = lstrSql & "  `vlr_demanda_reativo_ponta` double(14,2) DEFAULT 0, " & vbCrLf
		lstrSql = lstrSql & "  `vlr_demanda_reativo_fponta` double(14,2) DEFAULT 0, " & vbCrLf
		lstrSql = lstrSql & "  `qtd_demanda_ultrap_ponta` double(14,5) DEFAULT 0, " & vbCrLf
		lstrSql = lstrSql & "  `qtd_demanda_ultrap_fponta` double(14,5) DEFAULT 0, " & vbCrLf
		lstrSql = lstrSql & "  `vlr_demanda_ultrap_ponta` double(14,2) DEFAULT 0, " & vbCrLf
		lstrSql = lstrSql & "  `vlr_demanda_ultrap_fponta` double(14,2) DEFAULT 0" & vbCrLf
		lstrSql = lstrSql & ") ENGINE=InnoDB DEFAULT CHARSET=latin1 " & vbCrLf
		Call Executar(lstrSql)
	End Sub
	
	Private Sub frmRelReativosDemandaUltrap_FormClosed(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
		'
	End Sub
	
	Private Sub BuscaCdDsClasse(ByRef astrCdClas As String, ByRef astrDsClas As String, ByVal astrGrTar As String)
		If Mid(astrGrTar, 3, 2) = "AL" Then
			astrDsClas = "Livre"
			astrCdClas = "11"
		Else
			Select Case VB.Left(astrCdClas, 1)
				Case Is < CStr(4)
					astrDsClas = "Industrial"
					astrCdClas = "10"
				Case Is = CStr(4)
					astrDsClas = "Comercial"
					astrCdClas = "40"
				Case Is = CStr(5)
					astrDsClas = "Rural"
					astrCdClas = "50"
				Case Is = CStr(6)
					If CDbl(VB.Left(astrCdClas, 2)) = 61 Then
						astrDsClas = "Poder Publico Municipal"
						astrCdClas = "61"
					ElseIf CDbl(VB.Left(astrCdClas, 2)) = 62 Then 
						astrDsClas = "Poder Publico Estadual"
						astrCdClas = "62"
					ElseIf CDbl(VB.Left(astrCdClas, 2)) = 63 Then 
						astrDsClas = "Poder Publico Federal"
						astrCdClas = "63"
					ElseIf CDbl(VB.Left(astrCdClas, 2)) = 64 Then 
						astrDsClas = "Iluminação Publica"
						astrCdClas = "64"
					End If
				Case Is = CStr(7)
					astrDsClas = "Serviço Público"
					astrCdClas = "70"
				Case Is = CStr(8)
					astrDsClas = "Consumo Próprio"
					astrCdClas = "80"
				Case Is = CStr(9)
					astrDsClas = "Residencial"
					astrCdClas = "90"
				Case Else
					astrDsClas = "Indefinido"
					astrCdClas = "00"
			End Select
		End If
	End Sub
End Class