Imports System.ComponentModel

Public Class frmReqPago
    Private FinCambioCuentaBancaria As Double = False
    Private oRequisicionDePagoCE As RequisicionDePagoCE
    Private CodigoInv As String = String.Empty
    Private vCargandoRegistro As Boolean = False
    Private Transaccion As String = String.Empty
    Private TipoEstado As String = String.Empty
    Private Empresa As String = String.Empty
    Private Operaciones As New Operaciones
    Private oDAOGeneral As New DAOGeneral
    Sub New()

        ' Esta llamada es exigida por el diseñador.
        InitializeComponent()

        ' Agregue cualquier inicialización después de la llamada a InitializeComponent().

    End Sub
    Sub New(Codigo As String, Empresa As String, Estado As String)
        Me.New
        CodigoInv = Codigo
        TipoEstado = Estado
        Me.Empresa = Empresa
    End Sub



    Sub CargarCodigo()
        Dim CodigoReq As String = String.Empty
        CodigoReq = RequisicionDePagoDAO.NuevoCodigo()
        txtCodigoRequisicion.Text = CodigoReq
        CargarMonto()
    End Sub
    Sub CargarMonto()
        Dim Monto As Double = RequisicionDePagoDAO.Monto(CodigoInv)
        If Monto = 0 Then
            MsgBox("No contiene un monto o valor no válido")
            Me.Close()
        Else
            txtMontoPago.Text = Monto
        End If
    End Sub


    Private Sub frmReqPago_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        'Configurcion de Requisicion de Pago para Reportos de venta'
        ConfiguracionReportoVenta()

        'TODO: esta línea de código carga datos en la tabla 'InversionesDEVDataSet3.BAN1' Puede moverla o quitarla según sea necesario.
        Me.BAN1TableAdapter.Fill(Me.InversionesDEVDataSet3.BAN1)
        'TODO: esta línea de código carga datos en la tabla 'InversionesDataSet1.USR0' Puede moverla o quitarla según sea necesario.
        Me.USR0TableAdapter.Fill(Me.InversionesDataSet1.USR0)
        'TODO: esta línea de código carga datos en la tabla 'InversionesDataSet1.FPAG' Puede moverla o quitarla según sea necesario.
        Me.FPAGTableAdapter.Fill(Me.InversionesDataSet1.FPAG)
        'TODO: esta línea de código carga datos en la tabla 'InversionesDataSet1.BAN0' Puede moverla o quitarla según sea necesario.
        Me.BAN0TableAdapter.Fill(Me.InversionesDataSet1.BAN0)
        'TODO: esta línea de código carga datos en la tabla 'InversionesDataSet1.RPAG' Puede moverla o quitarla según sea necesario.

        CargarEmisores()
        CargarCodigo()

        inicializar()



        If RequisicionDePagoDAO.ExisteRequisicion(CodigoInv) Then

            CargarRegistro()
        Else

            PrepararNuevo()
        End If

    End Sub


    Sub ConfiguracionReportoVenta()
        If txtCodigoInversion.Text.IndexOf("REPOVENTA") > -1 Then
            btnValidar.Visible = False
        End If

    End Sub

    Sub inicializar()
        cbxCodigoBanco.SelectedIndex = -1
        cbxCuentaBancaria.SelectedIndex = -1
        cbxFormaDePago.SelectedIndex = -1
        cbxSolicitante.SelectedIndex = -1
        cboEmisor.SelectedIndex = -1
        txtSaldoContable.Text = 0
        dtpFechaPago.Value = Date.Today.Date
        If String.IsNullOrEmpty(txtMontoPago.Text) Then
            txtMontoPago.Text = 0
        End If
    End Sub
    Private Sub RPAGBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles RPAGBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.RPAGBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.InversionesDataSet1)
    End Sub
    Private Sub CargarRegistro()
        Dim CodigoInversion As String = txtCodigoInversion.Text.ToString
        oRequisicionDePagoCE = New RequisicionDePagoCE
        oRequisicionDePagoCE = RequisicionDePagoDAO.CargarRegistro(CodigoInv)

        vCargandoRegistro = True
        cbxSolicitante.SelectedValue = oRequisicionDePagoCE.UsuarioSolicitante
        cboEmisor.SelectedValue = oRequisicionDePagoCE.AFavorDe
        cbxFormaDePago.SelectedValue = oRequisicionDePagoCE.FormaDePago
        txtMontoPago.Text = oRequisicionDePagoCE.MontoDelPago.ToString
        cbxCodigoBanco.SelectedValue = oRequisicionDePagoCE.CodigoBanco
        cbxCuentaBancaria.SelectedValue = oRequisicionDePagoCE.NumeroCuenta
        txtSaldoContable.Text = oRequisicionDePagoCE.SaldoContable.ToString
        txtConcepto.Text = oRequisicionDePagoCE.Concepto
        dtpFechaPago.Value = oRequisicionDePagoCE.FechaDelPago
        txtCodigoInversion.Text = oRequisicionDePagoCE.CodigoInversion
        txtCodigoRequisicion.Text = oRequisicionDePagoCE.CodigoReq
        vCargandoRegistro = False

        Me.Transaccion = "C"
        Me.btnAccion.Text = "Aceptar"

    End Sub
    Private Sub CargarEmisores()
        Me.cboEmisor.DataSource = oDAOGeneral.ListaEmisores
        Me.cboEmisor.DisplayMember = "Descripcion"
        Me.cboEmisor.ValueMember = "Codigo"
        Me.cboEmisor.SelectedIndex = -1

    End Sub
    Private Sub PrepararNuevo()
        oRequisicionDePagoCE = New RequisicionDePagoCE
        Me.Transaccion = "N"
        Me.btnAccion.Text = "Guardar"
        txtCodigoInversion.Text = CodigoInv
        inicializar()
    End Sub
    Private Sub GuardarRequisicion()
        If Not ValidarDatos() Then
            Dim res As Integer
            oRequisicionDePagoCE_Refrescar()
            res = RequisicionDePagoDAO.GuardarNuevaRequisicion(oRequisicionDePagoCE, Transaccion)

            If res = 1 Then
                frmINV.EnviadoContaduria = False
                Dim Id As Integer = frmINV.vBindingSource.Current("DocId")
                frmINV.CargarRegistro(Id)
                frmINV.EstadoBoton()

                CargarRegistro()

            End If


        End If
    End Sub


    Private Sub CargarCuentasBancarias()

        If Not (Empresa Is Nothing) Then
            Me.cbxCuentaBancaria.SelectedIndex = -1
            Dim Banco As String = cbxCodigoBanco.SelectedValue
            Me.cbxCuentaBancaria.DataSource = BancosDAO.CuentasBancarias(Empresa, Banco)
            Me.cbxCuentaBancaria.DisplayMember = "NumCuenta"
            Me.cbxCuentaBancaria.ValueMember = "NumCuenta"
            Me.cbxCuentaBancaria.SelectedIndex = -1

        End If

    End Sub

    Private Sub cbxCuentaBancaria_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbxCuentaBancaria.SelectedIndexChanged


        CargarSaldoActualDeCuenta()

    End Sub

    Private Sub cbxCodigoBanco_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbxCodigoBanco.SelectedIndexChanged

        FinCambioCuentaBancaria = False
        CargarCuentasBancarias()
        FinCambioCuentaBancaria = True
    End Sub

    Private Function ValidaMonto(ByVal pValorTXT As String, ByRef MensajeError As String) As Boolean
        Dim rValor As Double

        If pValorTXT.Length > 0 Then
            If Not Double.TryParse(pValorTXT, rValor) Then
                MensajeError = "Valor no válido."
                Return False
            Else
                Return True
            End If
        Else
            Return True
        End If

    End Function

    Private Sub txtMontoPago_TextChanged(sender As Object, e As EventArgs) Handles txtMontoPago.TextChanged

    End Sub

    Private Sub txtMontoPago_Validating(sender As Object, e As CancelEventArgs) Handles txtMontoPago.Validating

    End Sub

    Private Sub txtMontoPago_Validated(sender As Object, e As EventArgs) Handles txtMontoPago.Validated
        Me.ErrorProvider1.SetError(txtMontoPago, "")
    End Sub

    Private Sub txtAFavorDe_TextChanged(sender As Object, e As EventArgs)

    End Sub

    Private Sub txtConcepto_TextChanged(sender As Object, e As EventArgs) Handles txtConcepto.TextChanged

    End Sub
    Private Sub cbxSolicitante_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbxSolicitante.SelectedIndexChanged

    End Sub
    Private Sub cbxFormaDePago_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbxFormaDePago.SelectedIndexChanged


    End Sub


    Private Sub btnAccion_Click(sender As Object, e As EventArgs) Handles btnAccion.Click
        Dim Err As Boolean = ValidarDatos()
        If Err Then
            MsgBox("Valores no válidos")
        Else

            If Me.Transaccion = "N" Then
                GuardarRequisicion()

            Else
                Me.Close()
            End If

        End If
    End Sub
    Function ValidarDatos()
        Dim Solicitante As String = String.Empty
        Dim Emisor As String = String.Empty
        Dim FormaPago As String = String.Empty
        Dim MontoPago As Double = 0
        Dim Banco As String = String.Empty
        Dim CuentaBanco As String = String.Empty
        Dim SaldoContable As Double = 0
        Dim Concepto As String = String.Empty
        Dim FechaPago As Date = Date.Now.Date
        Dim CodigoRequisicion As String = String.Empty
        Dim CodigoInversion As String = String.Empty
        Dim Err As Boolean = False

        Solicitante = cbxSolicitante.SelectedValue
        Emisor = cboEmisor.SelectedValue
        FormaPago = cbxFormaDePago.SelectedValue
        MontoPago = Operaciones.ConvertirDecimal(txtMontoPago.Text.ToString)
        Banco = cbxCodigoBanco.SelectedValue
        CuentaBanco = cbxCuentaBancaria.SelectedValue
        SaldoContable = Operaciones.ConvertirDecimal(txtSaldoContable.Text.ToString)
        Concepto = txtConcepto.Text
        FechaPago = dtpFechaPago.Value
        CodigoRequisicion = txtCodigoRequisicion.Text.ToString
        CodigoInversion = txtCodigoInversion.Text.ToString


        If (String.IsNullOrEmpty(Solicitante) Or Solicitante Is Nothing) Or
         (String.IsNullOrEmpty(Emisor) Or Emisor Is Nothing) Or
         (String.IsNullOrEmpty(FormaPago) Or FormaPago Is Nothing) Or
         (String.IsNullOrEmpty(Banco) Or Banco Is Nothing) Or
         (String.IsNullOrEmpty(CuentaBanco) Or CuentaBanco Is Nothing) Or
         (String.IsNullOrEmpty(Concepto) Or Concepto Is Nothing) Or
         (String.IsNullOrEmpty(CodigoRequisicion) Or CodigoRequisicion Is Nothing) Or
         (String.IsNullOrEmpty(CodigoInversion) Or CodigoInversion Is Nothing) Or
            MontoPago = 0 Or SaldoContable = 0 Then
            Err = True
        End If










        Return Err
    End Function

    Private Sub txtSaldoContable_TextChanged(sender As Object, e As EventArgs) Handles txtSaldoContable.TextChanged


    End Sub

    Private Sub dtpFechaPago_ValueChanged(sender As Object, e As EventArgs) Handles dtpFechaPago.ValueChanged


    End Sub

    Private Sub txtAFavorDe_Leave(sender As Object, e As EventArgs)

    End Sub

    Private Sub txtConcepto_Leave(sender As Object, e As EventArgs) Handles txtConcepto.Leave


    End Sub
    Private Sub oRequisicionDePagoCE_Refrescar()
        Dim Solicitante As String = String.Empty
        Dim Emisor As String = String.Empty
        Dim FormaPago As String = String.Empty
        Dim MontoPago As Double = 0
        Dim Banco As String = String.Empty
        Dim CuentaBanco As String = String.Empty
        Dim SaldoContable As Double = 0
        Dim Concepto As String = String.Empty
        Dim FechaPago As Date = Date.Now.Date
        Dim CodigoRequisicion As String = String.Empty
        Dim CodigoInversion As String = String.Empty
        Dim Err As Boolean = False

        Solicitante = cbxSolicitante.SelectedValue
        Emisor = cboEmisor.SelectedValue
        FormaPago = cbxFormaDePago.SelectedValue
        MontoPago = Operaciones.ConvertirDecimal(txtMontoPago.Text.ToString)
        Banco = cbxCodigoBanco.SelectedValue
        CuentaBanco = cbxCuentaBancaria.SelectedValue
        SaldoContable = Operaciones.ConvertirDecimal(txtSaldoContable.Text.ToString)
        Concepto = txtConcepto.Text
        FechaPago = dtpFechaPago.Value
        CodigoRequisicion = txtCodigoRequisicion.Text.ToString
        CodigoInversion = txtCodigoInversion.Text.ToString

        oRequisicionDePagoCE.CodigoInversion = CodigoInv
        oRequisicionDePagoCE.AFavorDe = Emisor
        oRequisicionDePagoCE.FormaDePago = FormaPago
        oRequisicionDePagoCE.MontoDelPago = MontoPago
        oRequisicionDePagoCE.FechaDelPago = FechaPago
        oRequisicionDePagoCE.CodigoBanco = Banco
        oRequisicionDePagoCE.NumeroCuenta = CuentaBanco
        oRequisicionDePagoCE.SaldoContable = SaldoContable
        oRequisicionDePagoCE.Concepto = Concepto
        oRequisicionDePagoCE.UsuarioSolicitante = Solicitante
        oRequisicionDePagoCE.CodigoInversion = CodigoInversion
        oRequisicionDePagoCE.CodigoReq = CodigoRequisicion
    End Sub

    Private Sub btnVistaPreliminar_Click(sender As Object, e As EventArgs) Handles btnVistaPreliminar.Click
        Impresion(True)

    End Sub

    Private Sub btnImprimir_Click(sender As Object, e As EventArgs) Handles btnImprimir.Click
        Impresion(False)
    End Sub
    Private Sub Impresion(ByVal pVistaPreliminar As Boolean)

        If RequisicionDePagoDAO.ExisteRequisicion(CodigoInv) Then
            Dim oImprimir As New frmImprimir
            oImprimir.NombreRPT = "rptRequisicionDePago.rpt"
            oImprimir.IdDocumento = CodigoInv

            If pVistaPreliminar = True Then
                oImprimir.CargarRPT()
                oImprimir.ShowDialog()

            Else
                oImprimir.VistaPrevia = False
                oImprimir.CargarRPT()

            End If
        End If

    End Sub
    Private Sub CargarSaldoActualDeCuenta()
        Dim vCuentaContable As String
        Dim vSaldo As Double
        Dim vBDSAP As String
        Dim Banco As String = cbxCodigoBanco.SelectedValue
        Dim CuentaBancaria As String = cbxCuentaBancaria.SelectedValue

        If Not String.IsNullOrEmpty(Empresa) And Not Banco Is Nothing And
        Not CuentaBancaria Is Nothing And FinCambioCuentaBancaria Then


            vCuentaContable = BancosDAO.ObtieneCuentaContable(Empresa, Banco, CuentaBancaria)
            vBDSAP = EmpresasDAO.ObtenerNombreBDSAP(Empresa)
            vSaldo = InvSAP.ObtenerSaldoActualDeCuenta(vCuentaContable, vBDSAP)

            txtSaldoContable.Text = vSaldo.ToString(Configuraciones.CodigoTXTMontos)
        End If
    End Sub
    Private Sub btnCancelar_Click(sender As Object, e As EventArgs) Handles btnCancelar.Click
        Me.Close()

    End Sub

    Private Sub txtCodigoInversion_TextChanged(sender As Object, e As EventArgs) Handles txtCodigoInversion.TextChanged

    End Sub

    Private Sub txtCodigoReq_TextChanged(sender As Object, e As EventArgs)

    End Sub

    Private Sub btnEnviar_Click(sender As Object, e As EventArgs) Handles btnValidar.Click
        frmINV.EnviadoContaduria = True
        Dim Id As Integer = frmINV.vBindingSource.Current("DocId")

        frmINV.EstadoBoton()
        frmINV.CargarRegistro(Id)
        frmINV.EstadoBoton()


    End Sub

    Private Sub btnNuevo_Click(sender As Object, e As EventArgs)
        Transaccion = "N"
        btnAccion.Text = "Nuevo"
        PrepararNuevo()
    End Sub

    Private Sub BtnModificar_Click(sender As Object, e As EventArgs)
        Transaccion = "M"
        btnAccion.Text = "Modificar"
    End Sub

    Private Sub navNuevo_Click(sender As Object, e As EventArgs)
        Transaccion = "N"
        btnAccion.Text = "Nuevo"
        PrepararNuevo()

    End Sub

    Private Sub navModificar_Click(sender As Object, e As EventArgs) Handles navModificar.Click
        Transaccion = "M"
        btnAccion.Text = "Modificar"
    End Sub

    Private Sub cboEmisor_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboEmisor.SelectedIndexChanged

    End Sub
End Class