Option Strict Off
Option Explicit On
Imports VB = Microsoft.VisualBasic
Friend Class frmControleSegundaVia
	Inherits System.Windows.Forms.Form
	Private istrMesAn As String
	Private idblNumNf As Double
	Private iblnCobrar As Boolean
	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 ifrmQuemChama As System.Windows.Forms.Form
	
	Public Sub SetaParametros(ByRef afrmQuemChama As System.Windows.Forms.Form, ByVal astrMesAno As String, ByVal adblNumNf As Double, ByVal ablnCobrar As Boolean, ByVal astrSolicitante As String)
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		ifrmQuemChama = afrmQuemChama
		ifrmQuemChama.Enabled = False
		istrMesAn = astrMesAno
		idblNumNf = adblNumNf
		iblnCobrar = ablnCobrar
		Me.txtSolicitante.Text = Str_Retira(astrSolicitante, "~`!@#$%^&*()_-+=[{]};:'<,.>/?", " ")
		lstrSql = ""
		lstrSql = lstrSql & "SELECT * "
		lstrSql = lstrSql & "  FROM `nf` "
		lstrSql = lstrSql & " WHERE `mesano` = '" & istrMesAn & "' "
		lstrSql = lstrSql & "   AND `mesano_ref` = " & CInt(VB.Right(istrMesAn, 4) & VB.Left(istrMesAn, 2)) & " "
		lstrSql = lstrSql & "   AND `numnf` = " & idblNumNf & " "
		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"'
					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))
				End If
				Call .Close()
			End If
		End With
		With Me.txtMotivo
			.ReadOnly = iblnCobrar
			.BackColor = System.Drawing.ColorTranslator.FromOle(IIf(.ReadOnly, System.Drawing.ColorTranslator.ToOle(System.Drawing.SystemColors.Control), System.Drawing.ColorTranslator.ToOle(System.Drawing.SystemColors.Window)))
		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 CobraSegundaVia()
		Dim llngReg As Integer
		Dim lstrCod As String
		Dim ldblVlr As Double
		Dim ldteBas As Date
		Dim ldteIni As Date
		Dim ldteFin As Date
		Dim lstrSql As String
		Dim lrstAux As ADODB.Recordset
		lrstAux = New ADODB.Recordset
		If iblnCobrar Then
			ldteBas = CDate(VB6.Format(BuscaHoraBD, "DD/MM/YYYY HH:NN:SS"))
			ldteIni = System.Date.FromOADate(DateSerial(Year(ldteBas), Month(ldteBas), UltimoDiaMes(Month(ldteBas), Year(ldteBas))).ToOADate + 1)
			ldteFin = DateSerial(Year(ldteIni), Month(ldteIni), UltimoDiaMes(Month(ldteIni), Year(ldteIni)))
			'Pega valor da Segunda Via
			lstrSql = ""
			lstrSql = lstrSql & "  SELECT `codigos`.`valor`, "
			lstrSql = lstrSql & "         `codigos`.`num` "
			lstrSql = lstrSql & "    FROM `codigos` "
			lstrSql = lstrSql & "   WHERE `codigos`.`num` = '904000006" & IIf(UCase(istrGrupo) = "A", 4, IIf(istrFase = "M", 1, IIf(istrFase = "B", 2, 3))) & "' "
			lstrSql = lstrSql & "     AND `codigos`.`datanova` <= '" & VB6.Format(ldteBas, "yyyy-MM-dd") & "' "
			lstrSql = lstrSql & "ORDER BY `datanova` DESC LIMIT 0, 1"
			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"'
						ldblVlr = CDbl(Replace(IIf(IsDbNull(.Fields("valor").value), 0, .Fields("valor").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"'
						lstrCod = IIf(IsDbNull(.Fields("num").value), "", .Fields("num").value)
					End If
					Call .Close()
				End If
			End With
			lstrSql = ""
			lstrSql = lstrSql & "SELECT `id`,`zona`,`livro`,`conta`,`cons_id`,`unid_id`,`sit`,`codigo`,`dt_ini`,`dt_fim` "
			lstrSql = lstrSql & "  FROM `taxas` "
			lstrSql = lstrSql & " WHERE `zona`    = '" & istrZona & "' "
			lstrSql = lstrSql & "   AND `livro`   = '" & istrLivro & "' "
			lstrSql = lstrSql & "   AND `conta`   = '" & istrConta & "' "
			lstrSql = lstrSql & "   AND `unid_id` = " & ilngUnidId & " "
			lstrSql = lstrSql & "   AND `cons_id` = " & ilngConsId & " "
			lstrSql = lstrSql & "   AND `codigo`  = '" & lstrCod & "' "
			lstrSql = lstrSql & "   AND `dt_ini` >= '" & VB6.Format(ldteIni, "yyyy-MM-dd") & "' "
			lstrSql = lstrSql & "   AND `dt_fim` <= '" & VB6.Format(ldteFin, "yyyy-MM-dd") & "' "
			lstrSql = lstrSql & "   AND TRIM(`ref`) LIKE '%" & istrMesAn & "%' LIMIT 0, 1"
			Call Conectar(lstrSql, lrstAux)
			With lrstAux
				If .State > 0 Then
					If .RecordCount > 0 Then
						Call MsgBox("Já existe uma taxa de segunda via para esta fatura! Será cobrando novamente no valor de R$" & VB6.Format(ldblVlr, "#,##0.00") & "!", MsgBoxStyle.Information, gstrTituloMensagem)
						llngReg = .RecordCount
					End If
					Call .Close()
				End If
			End With
			lstrSql = ""
			lstrSql = lstrSql & "INSERT INTO `taxas` SET " & vbCrLf
			lstrSql = lstrSql & "            `zona`    = '" & istrZona & "', " & vbCrLf
			lstrSql = lstrSql & "            `livro`   = '" & istrLivro & "', " & vbCrLf
			lstrSql = lstrSql & "            `conta`   = '" & istrConta & "', " & vbCrLf
			lstrSql = lstrSql & "            `cons_id` = " & ilngConsId & ", " & vbCrLf
			lstrSql = lstrSql & "            `unid_id` = " & ilngUnidId & ", " & vbCrLf
			lstrSql = lstrSql & "            `codigo`  = '" & lstrCod & "', " & vbCrLf
			lstrSql = lstrSql & "            `valor`   = " & Replace(CStr(ldblVlr), ",", ".") & ", " & vbCrLf
			lstrSql = lstrSql & "            `dt_ini`  = '" & VB6.Format(ldteIni, "yyyy-MM-dd") & "', " & vbCrLf
			lstrSql = lstrSql & "            `dt_fim`  = '" & VB6.Format(ldteFin, "yyyy-MM-dd") & "', " & vbCrLf
			lstrSql = lstrSql & "            `ref`     = '" & istrMesAn & IIf(llngReg > 0, (" / " & llngReg), "") & "' "
			Call Executar(lstrSql)
		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 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 lblnAux As Boolean
		Dim lfrmImpFaturas As frmImpFaturas
		lfrmImpFaturas = New frmImpFaturas
		lblnAux = True
		If ValidaSentencaFrase(Me.txtSolicitante.Text) Then
			lblnAux = False
			Call MsgBox("Nome do solicitante inválido, favor informar um nome de solicitante válido!", MsgBoxStyle.Critical, gstrTituloMensagem)
		Else
			lblnAux = True
		End If
		If lblnAux Then
			If Not Me.txtMotivo.ReadOnly Then
				If ValidaSentencaFrase(Me.txtMotivo.Text) Then
					lblnAux = False
					Call MsgBox("Motivo inválido! Favor informar um motivo válido!", MsgBoxStyle.Critical, gstrTituloMensagem)
				Else
					lblnAux = True
				End If
			End If
			If lblnAux Then
				If iblnCobrar Then
					Call CobraSegundaVia()
				End If
				Call AtualizaHistoricoFatura(idblNumNf, istrMesAn, CInt(VB.Right(istrMesAn, 4) & VB.Left(istrMesAn, 2)), "Segunda Via", Me.txtSolicitante.Text, Me.txtMotivo.Text)
				If istrGrupo = "B" Then
					Call lfrmImpFaturas.GeraFaturaGrupoB(1, istrMesAn, idblNumNf, 0, "nf")
				Else
					Call lfrmImpFaturas.GeraFaturaGrupoA(1, istrMesAn, idblNumNf, 0, "nf")
				End If
			End If
		End If
		'UPGRADE_NOTE: Object lfrmImpFaturas 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"'
		lfrmImpFaturas = Nothing
	End Sub
	
	Private Sub frmControleSegundaVia_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		Call CenterFormQuemChama(Me, ifrmQuemChama)
	End Sub
	
	Private Sub frmControleSegundaVia_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