Public Class AExistencia
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents CmbDeposito As System.Windows.Forms.ComboBox
    Public WithEvents BtnRegistrar As System.Windows.Forms.Button
    Public WithEvents Imagen1 As System.Windows.Forms.PictureBox
    Friend WithEvents EqtProducto As System.Windows.Forms.Label
    Friend WithEvents CmbProducto As System.Windows.Forms.ComboBox
    Friend WithEvents TexUsuario As System.Windows.Forms.TextBox
    Friend WithEvents EqtExistenciaActual As System.Windows.Forms.Label
    Friend WithEvents EqtUsuario As System.Windows.Forms.Label
    Friend WithEvents TexExistenciaActual As System.Windows.Forms.TextBox
    Friend WithEvents EqtExistenciaNueva As System.Windows.Forms.Label
    Friend WithEvents EqtDeposito As System.Windows.Forms.Label
    Friend WithEvents TexExistenciaNueva As System.Windows.Forms.TextBox
    Friend WithEvents BtnDepositoRapido As System.Windows.Forms.Button
    Friend WithEvents BtnProductoRapido As System.Windows.Forms.Button
    Friend WithEvents EqtComentario As System.Windows.Forms.Label
    Friend WithEvents Label1 As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    Me.EqtDeposito = New System.Windows.Forms.Label()
    Me.CmbDeposito = New System.Windows.Forms.ComboBox()
    Me.BtnRegistrar = New System.Windows.Forms.Button()
    Me.Imagen1 = New System.Windows.Forms.PictureBox()
    Me.EqtProducto = New System.Windows.Forms.Label()
    Me.CmbProducto = New System.Windows.Forms.ComboBox()
    Me.TexUsuario = New System.Windows.Forms.TextBox()
    Me.EqtExistenciaActual = New System.Windows.Forms.Label()
    Me.EqtUsuario = New System.Windows.Forms.Label()
    Me.TexExistenciaActual = New System.Windows.Forms.TextBox()
    Me.EqtExistenciaNueva = New System.Windows.Forms.Label()
    Me.TexExistenciaNueva = New System.Windows.Forms.TextBox()
    Me.BtnDepositoRapido = New System.Windows.Forms.Button()
    Me.BtnProductoRapido = New System.Windows.Forms.Button()
    Me.EqtComentario = New System.Windows.Forms.Label()
    Me.Label1 = New System.Windows.Forms.Label()
    CType(Me.Imagen1, System.ComponentModel.ISupportInitialize).BeginInit()
    Me.SuspendLayout()
    '
    'EqtDeposito
    '
    Me.EqtDeposito.AutoSize = True
    Me.EqtDeposito.Location = New System.Drawing.Point(16, 16)
    Me.EqtDeposito.Name = "EqtDeposito"
    Me.EqtDeposito.Size = New System.Drawing.Size(103, 13)
    Me.EqtDeposito.TabIndex = 188
    Me.EqtDeposito.Text = "Selección depósito :"
    '
    'CmbDeposito
    '
    Me.CmbDeposito.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
    Me.CmbDeposito.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
    Me.CmbDeposito.Location = New System.Drawing.Point(168, 16)
    Me.CmbDeposito.Name = "CmbDeposito"
    Me.CmbDeposito.Size = New System.Drawing.Size(224, 21)
    Me.CmbDeposito.TabIndex = 187
    '
    'BtnRegistrar
    '
    Me.BtnRegistrar.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
    Me.BtnRegistrar.BackColor = System.Drawing.SystemColors.Control
    Me.BtnRegistrar.Cursor = System.Windows.Forms.Cursors.Default
    Me.BtnRegistrar.FlatStyle = System.Windows.Forms.FlatStyle.System
    Me.BtnRegistrar.Font = New System.Drawing.Font("Arial", 8.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    Me.BtnRegistrar.ForeColor = System.Drawing.SystemColors.ControlText
    Me.BtnRegistrar.Location = New System.Drawing.Point(408, 63)
    Me.BtnRegistrar.Name = "BtnRegistrar"
    Me.BtnRegistrar.RightToLeft = System.Windows.Forms.RightToLeft.No
    Me.BtnRegistrar.Size = New System.Drawing.Size(104, 32)
    Me.BtnRegistrar.TabIndex = 185
    Me.BtnRegistrar.Text = "&Guardar datos"
    Me.BtnRegistrar.UseVisualStyleBackColor = False
    '
    'Imagen1
    '
    Me.Imagen1.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
    Me.Imagen1.Cursor = System.Windows.Forms.Cursors.Default
    Me.Imagen1.Location = New System.Drawing.Point(448, 23)
    Me.Imagen1.Name = "Imagen1"
    Me.Imagen1.Size = New System.Drawing.Size(32, 32)
    Me.Imagen1.TabIndex = 186
    Me.Imagen1.TabStop = False
    '
    'EqtProducto
    '
    Me.EqtProducto.AutoSize = True
    Me.EqtProducto.Location = New System.Drawing.Point(16, 48)
    Me.EqtProducto.Name = "EqtProducto"
    Me.EqtProducto.Size = New System.Drawing.Size(105, 13)
    Me.EqtProducto.TabIndex = 190
    Me.EqtProducto.Text = "Selección producto :"
    '
    'CmbProducto
    '
    Me.CmbProducto.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
    Me.CmbProducto.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
    Me.CmbProducto.Location = New System.Drawing.Point(168, 48)
    Me.CmbProducto.Name = "CmbProducto"
    Me.CmbProducto.Size = New System.Drawing.Size(224, 21)
    Me.CmbProducto.TabIndex = 189
    '
    'TexUsuario
    '
    Me.TexUsuario.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
    Me.TexUsuario.BackColor = System.Drawing.SystemColors.Control
    Me.TexUsuario.Location = New System.Drawing.Point(136, 192)
    Me.TexUsuario.Name = "TexUsuario"
    Me.TexUsuario.ReadOnly = True
    Me.TexUsuario.Size = New System.Drawing.Size(256, 20)
    Me.TexUsuario.TabIndex = 193
    '
    'EqtExistenciaActual
    '
    Me.EqtExistenciaActual.AutoSize = True
    Me.EqtExistenciaActual.Location = New System.Drawing.Point(16, 104)
    Me.EqtExistenciaActual.Name = "EqtExistenciaActual"
    Me.EqtExistenciaActual.Size = New System.Drawing.Size(93, 13)
    Me.EqtExistenciaActual.TabIndex = 191
    Me.EqtExistenciaActual.Text = "Existencia actual :"
    '
    'EqtUsuario
    '
    Me.EqtUsuario.AutoSize = True
    Me.EqtUsuario.Location = New System.Drawing.Point(16, 192)
    Me.EqtUsuario.Name = "EqtUsuario"
    Me.EqtUsuario.Size = New System.Drawing.Size(110, 13)
    Me.EqtUsuario.TabIndex = 194
    Me.EqtUsuario.Text = "Ultimo usuario modif. :"
    '
    'TexExistenciaActual
    '
    Me.TexExistenciaActual.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
    Me.TexExistenciaActual.BackColor = System.Drawing.SystemColors.Control
    Me.TexExistenciaActual.Location = New System.Drawing.Point(136, 104)
    Me.TexExistenciaActual.MaxLength = 150
    Me.TexExistenciaActual.Name = "TexExistenciaActual"
    Me.TexExistenciaActual.Size = New System.Drawing.Size(88, 20)
    Me.TexExistenciaActual.TabIndex = 192
    Me.TexExistenciaActual.TextAlign = System.Windows.Forms.HorizontalAlignment.Right
    '
    'EqtExistenciaNueva
    '
    Me.EqtExistenciaNueva.AutoSize = True
    Me.EqtExistenciaNueva.Location = New System.Drawing.Point(16, 144)
    Me.EqtExistenciaNueva.Name = "EqtExistenciaNueva"
    Me.EqtExistenciaNueva.Size = New System.Drawing.Size(94, 13)
    Me.EqtExistenciaNueva.TabIndex = 195
    Me.EqtExistenciaNueva.Text = "Existencia nueva :"
    '
    'TexExistenciaNueva
    '
    Me.TexExistenciaNueva.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
    Me.TexExistenciaNueva.Location = New System.Drawing.Point(136, 144)
    Me.TexExistenciaNueva.MaxLength = 150
    Me.TexExistenciaNueva.Name = "TexExistenciaNueva"
    Me.TexExistenciaNueva.Size = New System.Drawing.Size(88, 20)
    Me.TexExistenciaNueva.TabIndex = 196
    Me.TexExistenciaNueva.TextAlign = System.Windows.Forms.HorizontalAlignment.Right
    '
    'BtnDepositoRapido
    '
    Me.BtnDepositoRapido.FlatStyle = System.Windows.Forms.FlatStyle.System
    Me.BtnDepositoRapido.Location = New System.Drawing.Point(136, 15)
    Me.BtnDepositoRapido.Name = "BtnDepositoRapido"
    Me.BtnDepositoRapido.Size = New System.Drawing.Size(24, 24)
    Me.BtnDepositoRapido.TabIndex = 197
    Me.BtnDepositoRapido.Text = ">"
    '
    'BtnProductoRapido
    '
    Me.BtnProductoRapido.FlatStyle = System.Windows.Forms.FlatStyle.System
    Me.BtnProductoRapido.Location = New System.Drawing.Point(136, 47)
    Me.BtnProductoRapido.Name = "BtnProductoRapido"
    Me.BtnProductoRapido.Size = New System.Drawing.Size(24, 24)
    Me.BtnProductoRapido.TabIndex = 198
    Me.BtnProductoRapido.Text = ">"
    '
    'EqtComentario
    '
    Me.EqtComentario.Location = New System.Drawing.Point(232, 104)
    Me.EqtComentario.Name = "EqtComentario"
    Me.EqtComentario.Size = New System.Drawing.Size(168, 16)
    Me.EqtComentario.TabIndex = 214
    Me.EqtComentario.Text = "Unidades de venta del producto"
    '
    'Label1
    '
    Me.Label1.Location = New System.Drawing.Point(232, 144)
    Me.Label1.Name = "Label1"
    Me.Label1.Size = New System.Drawing.Size(168, 16)
    Me.Label1.TabIndex = 215
    Me.Label1.Text = "Unidades de venta del producto"
    '
    'AExistencia
    '
    Me.AcceptButton = Me.BtnRegistrar
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.ClientSize = New System.Drawing.Size(528, 246)
    Me.Controls.Add(Me.Label1)
    Me.Controls.Add(Me.EqtComentario)
    Me.Controls.Add(Me.BtnProductoRapido)
    Me.Controls.Add(Me.BtnDepositoRapido)
    Me.Controls.Add(Me.EqtExistenciaNueva)
    Me.Controls.Add(Me.TexExistenciaNueva)
    Me.Controls.Add(Me.TexUsuario)
    Me.Controls.Add(Me.EqtExistenciaActual)
    Me.Controls.Add(Me.EqtUsuario)
    Me.Controls.Add(Me.TexExistenciaActual)
    Me.Controls.Add(Me.EqtProducto)
    Me.Controls.Add(Me.CmbProducto)
    Me.Controls.Add(Me.EqtDeposito)
    Me.Controls.Add(Me.CmbDeposito)
    Me.Controls.Add(Me.BtnRegistrar)
    Me.Controls.Add(Me.Imagen1)
    Me.MaximizeBox = False
    Me.Name = "AExistencia"
    Me.Text = "Ajustes de productos en existencia"
    CType(Me.Imagen1, System.ComponentModel.ISupportInitialize).EndInit()
    Me.ResumeLayout(False)
    Me.PerformLayout()

  End Sub

#End Region

  Private Sub SubLimpiarTex()
    For Each c As Control In Me.Controls
      If TypeOf c Is TextBox Then c.Text = ""
    Next
    CmbDeposito.SelectedIndex = -1
    CmbProducto.SelectedIndex = -1
    TexUsuario.Text = ""
  End Sub

  Private Sub TexExistenciaNueva_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TexExistenciaNueva.KeyPress
    SubCondicionNumeroReal(TexExistenciaNueva, e.KeyChar)
  End Sub

  Private Sub AExistencia_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    e.Cancel = True
    Me.Visible = False
    CType(MdiParent, VentaTec).MenuPrincipal.MenuItems(1).MenuItems(4).Enabled = True
    CType(MdiParent, VentaTec).BarraEstado.Panels(0).Text = ""
    SubLimpiarTex()
  End Sub

  Private Sub AExistencia_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    SubIconVentana(Me, "BOOKS01")
    SubIconRegistrar(Imagen1)
  End Sub

  Private Sub AExistencia_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
    CType(MdiParent, VentaTec).BarraEstado.Panels(0).Text = ""
  End Sub

  Private Sub SubExistenciaActual()
    Try
      Cursor = Cursors.WaitCursor
      SubAbrirTblBDRgt("SELECT * FROM VExistencias WHERE NombreDeposito = '" & CmbDeposito.Text & "' AND NombreProducto = '" & CmbProducto.Text & "'")
      Dim FTblRgt As DataRow = TTblRgt.Rows(0)
      ' inicializar
      TexExistenciaActual.Text = ""
      TexUsuario.Text = ""
      TexExistenciaActual.Text = FormatNumber(FTblRgt("Existencia").ToString, 2)
      TexUsuario.Text = FTblRgt("Usuario").ToString
      Cursor = Cursors.Default
    Catch ex As Exception
      Cursor = Cursors.Default
      MessageBox.Show(ex.Message, CmtTituloMensaje, MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
  End Sub

  Private Sub CmbDeposito_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmbDeposito.SelectedIndexChanged
    CmbProducto.Enabled = True
    CmbProducto.SelectedIndex = -1
    CmbProducto.Focus()
    TexExistenciaActual.Text = ""
    TexUsuario.Text = ""
  End Sub

  Private Sub CmbProducto_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CmbProducto.SelectedIndexChanged
    If CmbProducto.Text <> "" And CmbDeposito.Text <> "" Then SubExistenciaActual()
  End Sub

  Private Sub BtnRegistrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnRegistrar.Click
    Try
      ' tabla existencia de depósitos
      If CmbDeposito.Text = "" Or _
         CmbProducto.Text = "" Or _
         TexExistenciaNueva.Text = "" Then
        MessageBox.Show(CmtFaltaDatoRequerido, CmtTituloMensaje, MessageBoxButtons.OK, MessageBoxIcon.Error)
      Else
        Cursor = Cursors.WaitCursor
        SubAbrirTblBDRgt("SELECT * FROM VExistencias WHERE NombreDeposito = '" & CmbDeposito.Text & "' AND NombreProducto = '" & CmbProducto.Text & "'")
        ' Saber si hubo error
        'If Not FnErrorADONET(Me) Then
        ' Restar en origen y sumar en destino
        ' Verificar la existencia de registro en cada depósito
        ' Depósito origen
        If TTblRgt.Rows.Count <> 0 Then
          'If Not CBool(Err.Number) Then
          ' Existe el registro
          ' Restar al existente
          Dim FTblRgt As DataRow = TTblRgt.Rows(0)
          FTblRgt("Existencia") = Val(FnFormulaNumeroComaPorPunto(TexExistenciaNueva.Text))
          ' datos a base
          BDTblRgt.Update(TTblRgt)
          TTblRgt.AcceptChanges()
          ' Igualar registros
          TexExistenciaActual.Text = TexExistenciaNueva.Text
        Else
          '' Hubo error
          ' Inicializar
          'Err.Clear()
          ' No existe registro origen
          ' Restar desde cero creando el registro
          Dim FNTblRgt As DataRow = TTblRgt.NewRow()
          FNTblRgt("NombreDeposito") = CmbDeposito.Text
          FNTblRgt("NombreProducto") = CmbProducto.Text
          FNTblRgt("Existencia") = Val(FnFormulaNumeroComaPorPunto(TexExistenciaNueva.Text))
          FNTblRgt("Usuario") = UsuarioActual
          ' nueva fila
          TTblRgt.Rows.Add(FNTblRgt)
          ' datos en la base
          BDTblRgt.Update(TTblRgt)
          TTblRgt.AcceptChanges()
        End If
        ' exito
        MessageBox.Show(CmtMoverExistencia, CmtTituloMensaje, MessageBoxButtons.OK, MessageBoxIcon.Information)
        Cursor = Cursors.Default
      End If
    Catch ex As Exception
      Cursor = Cursors.Default
      MessageBox.Show(ex.Message, CmtTituloMensaje, MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
  End Sub

  Private Sub AExistencia_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated
    Dim Ciclo As Int32
    Try
      SubAbrirTblBD("SELECT * FROM VDeposito ORDER BY NombreDeposito")
      SubAbrirTblBD2("SELECT * FROM VProducto ORDER BY NombreProducto")
      ' inicializacion
      TexExistenciaActual.Text = ""
      TexUsuario.Text = ""
      ' combinadas depósito
      CmbDeposito.Items.Clear()
      For Ciclo = 0 To TTbl.Rows.Count - 1
        Dim FTbl As DataRow = TTbl.Rows(Ciclo)
        If Not FTbl("NombreDeposito").ToString = SinDeposito Then CmbDeposito.Items.Add(FTbl("NombreDeposito").ToString)
      Next
      ' combinada producto
      CmbProducto.Items.Clear()
      For Ciclo = 0 To TTbl2.Rows.Count - 1
        Dim FTbl2 As DataRow = TTbl2.Rows(Ciclo)
        If CBool(FTbl2("LlevarExistencia").ToString) Then CmbProducto.Items.Add(FTbl2("NombreProducto").ToString)
      Next
    Catch ex As Exception
      MessageBox.Show(ex.Message, CmtTituloMensaje, MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
  End Sub

  Private Sub BtnDepositoRapido_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDepositoRapido.Click
    ' ATENCION: VERIFICAR QUE EL USUARIO TENGA PERMISO
    If FnConAutorizacionVentana("NDeposito") Then
      'Dim FrmNDeposito As New NDeposito
      frmNDeposito.MdiParent = CType(MdiParent, VentaTec)
      frmNDeposito.Visible = True
      CType(MdiParent, VentaTec).MenuPrincipal.MenuItems(4).MenuItems(9).MenuItems(1).Enabled = False
    Else
      MessageBox.Show(CmtNoIngresoVentana, CmtTituloMensaje, MessageBoxButtons.OK, MessageBoxIcon.Information)
    End If
  End Sub

  Private Sub BtnProductoRapido_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnProductoRapido.Click
    ' ATENCION: VERIFICAR QUE EL USUARIO TENGA PERMISO
    If FnConAutorizacionVentana("NProducto") Then
      'Dim FrmNProducto As New NProducto
      FrmNProducto.MdiParent = CType(MdiParent, VentaTec)
      FrmNProducto.Visible = True
      CType(MdiParent, VentaTec).MenuPrincipal.MenuItems(4).MenuItems(6).MenuItems(1).Enabled = False
      BtnProductoRapido.Enabled = False
    Else
      MessageBox.Show(CmtNoIngresoVentana, CmtTituloMensaje, MessageBoxButtons.OK, MessageBoxIcon.Information)
    End If
  End Sub

  Private Sub BtnRegistrar_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles BtnRegistrar.MouseMove
    CType(MdiParent, VentaTec).BarraEstado.Panels(0).Text = CmtPresione & "para generar el ajuste de existencia"
  End Sub

End Class
