Exemple #1
0
        public List<NotaIS> BuscarNotas(string a = null)
        {
            List<NotaIS> lstNotaIs = new List<NotaIS>();

            AlmacenSQL asql = new AlmacenSQL();
            if (a != null)
            {
            if (string.Compare("Entrada", a, true) == 0)
            {
                string where = " WHERE tipo=1 ORDER BY fechaReg Desc";
                db.cmd.CommandText = "SELECT * FROM NotaIS" + where;

            }
            if (string.Compare("Salida", a, true) == 0)
            {
                string where = " WHERE tipo=2 ORDER BY fechaReg Desc";
                db.cmd.CommandText = "SELECT * FROM NotaIS" + where;

            }
            if (string.Compare("MovimientoInterno", a, true) == 0)
            {
                string where = " WHERE tipo=3 ORDER BY fechaReg Desc";
                db.cmd.CommandText = "SELECT * FROM NotaIS" + where;

            }
            }
            else {
            db.cmd.CommandText = "SELECT * FROM NotaIS ORDER BY fechaReg Desc";

            }
            try
            {
                if (tipo)  db.conn.Open();
                SqlDataReader reader = db.cmd.ExecuteReader();

                while (reader.Read())
                {
                    NotaIS nota = new NotaIS();

                    nota.FechaReg = reader.IsDBNull(reader.GetOrdinal("fechaReg")) ? DateTime.MinValue : DateTime.Parse(reader["fechaReg"].ToString());
                    nota.IdAlmacen = reader.IsDBNull(reader.GetOrdinal("idAlmacen")) ? -1 : int.Parse(reader["idAlmacen"].ToString());
                    nota.IdDoc = reader.IsDBNull(reader.GetOrdinal("idDoc")) ? -1 : int.Parse(reader["idDoc"].ToString());
                    nota.IdMotivo = reader.IsDBNull(reader.GetOrdinal("idMotivo")) ? -1 : int.Parse(reader["idMotivo"].ToString());
                    nota.IdNota = reader.IsDBNull(reader.GetOrdinal("idNota")) ? -1 : int.Parse(reader["idNota"].ToString());
                    nota.IdResponsable = reader.IsDBNull(reader.GetOrdinal("responsable")) ? -1 : int.Parse(reader["responsable"].ToString());
                    nota.Observaciones = reader.IsDBNull(reader.GetOrdinal("observaciones")) ? "" : reader["observaciones"].ToString();
                    nota.Tipo = reader.IsDBNull(reader.GetOrdinal("tipo")) ? -1 : int.Parse(reader["tipo"].ToString());
                    nota.IdNotaString = "Nota00000" + nota.IdNota.ToString();
                    nota.IdAlmacenString = asql.BuscarAlmacen(nota.IdAlmacen, -1, -1).Nombre;
                    if (nota.Tipo == 1) nota.TipoString = "Entrada";
                    if (nota.Tipo == 2) nota.TipoString = "Salida";
                    if (nota.Tipo == 3) nota.TipoString = "Movimiento Interno";
                    nota.LstProducto = BuscarNotas(nota.IdNota);
                    lstNotaIs.Add(nota);

                }
                db.cmd.Parameters.Clear();
                if (tipo)  db.conn.Close();
            }
            catch (SqlException e)
            {
                Console.WriteLine(e);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace.ToString());
            }

            return lstNotaIs;
        }
Exemple #2
0
        public int AgregarNota(NotaIS p,int sector=-1)
        {
            int retorno=-1;
            db.cmd.CommandText = "INSERT INTO NotaIS(tipo,fechaReg,observaciones,responsable,idDoc,idMotivo,idAlmacen) " +
            "VALUES (@tipo,GETDATE(),@observaciones,@responsable,@idDoc,@idMotivo,@idAlmacen); SELECT CAST(scope_identity() AS int)";
            db.cmd.Parameters.AddWithValue("@tipo", p.Tipo);
            db.cmd.Parameters.AddWithValue("@observaciones", p.Observaciones);
            db.cmd.Parameters.AddWithValue("@responsable", p.IdResponsable);
            db.cmd.Parameters.AddWithValue("@idDoc", p.IdDoc);
            db.cmd.Parameters.AddWithValue("@idMotivo", p.IdMotivo);
            db.cmd.Parameters.AddWithValue("@idAlmacen", p.IdAlmacen);

            try
            {
                if (tipo) db.conn.Open();
                retorno = (Int32)db.cmd.ExecuteScalar();
                db.cmd.Parameters.Clear();
                if (tipo) db.conn.Close();

            }
            catch (SqlException e)
            {
                Console.WriteLine(e);
                return -1;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace.ToString());
                return -1;
            }

            if (sector == -1)
            {
                //Agregamos en ProductoxNotaIS
                db.cmd.CommandText = "INSERT INTO ProductoxNotaIS(idProducto,idNota,idAlmacen,cantidad,idUbicacion)" +
                "VALUES (@idProducto,@idNota,@idAlmacen,@cantidad,@idUbicacion)";
                try
                {
                   if(tipo)  db.conn.Open();
                    for (int i = 0; i < p.LstProducto.Count; i++)
                    {

                        for (int j = 0; j < p.LstProducto.ElementAt(i).Ubicaciones.Count; j++)
                        {
                            db.cmd.Parameters.AddWithValue("@idProducto", p.LstProducto.ElementAt(i).IdProducto);
                            db.cmd.Parameters.AddWithValue("@idNota", retorno);
                            db.cmd.Parameters.AddWithValue("@idAlmacen", p.IdAlmacen);
                            db.cmd.Parameters.AddWithValue("@cantidad", p.LstProducto.ElementAt(i).Ubicaciones.ElementAt(j).Cantidad);
                            db.cmd.Parameters.AddWithValue("@idUbicacion", p.LstProducto.ElementAt(i).Ubicaciones.ElementAt(j).IdUbicacion);
                            db.cmd.ExecuteNonQuery();
                            db.cmd.Parameters.Clear();

                        }

                    }
                    if(tipo) db.conn.Close();
                }
                catch (SqlException e)
                {
                    Console.WriteLine(e);
                    return -1;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.StackTrace.ToString());
                    return -1;
                }
            }

            return retorno;
        }
Exemple #3
0
        public void AgregarNotaxSector(NotaIS nota)
        {
            DBConexion db1 = new DBConexion();
            for (int i = 0; i < nota.LstProducto.Count; i++)
            {
                int retorno = 0;
                db1.cmd.CommandText = "SELECT idSector from Sector WHERE idAlmacen=@idAlmacen AND idProducto=@idProducto ";

                try
                {
                    db1.conn.Open();

                    db1.cmd.Parameters.AddWithValue("@idProducto", nota.LstProducto.ElementAt(i).IdProducto);
                    db1.cmd.Parameters.AddWithValue("@idAlmacen", nota.IdAlmacen);

                    SqlDataReader reader = db1.cmd.ExecuteReader();

                    while (reader.Read())
                    {
                        retorno = Convert.ToInt32(reader["idSector"].ToString());
                    }

                    db1.cmd.Parameters.Clear();
                    db1.conn.Close();

                }
                catch (SqlException e)
                {
                    Console.WriteLine(e);
                    return;

                }
                catch (Exception e)
                {
                    Console.WriteLine(e.StackTrace.ToString());
                    return;
                }

                //Agregamos en SectorxMovimiento
                db1.cmd.CommandText = "INSERT INTO SectorxMovimiento (idSector,idNota,cantidad) " +
                    "VALUES (@idSector,@idNota,@cantidad)";
                    try
                    {
                        db1.conn.Open();

                        db1.cmd.Parameters.AddWithValue("@idSector", retorno);
                                db1.cmd.Parameters.AddWithValue("@idNota", nota.IdNota);
                                db1.cmd.Parameters.AddWithValue("@cantidad",nota.LstProducto.ElementAt(i).CanAtender);
                                db1.cmd.ExecuteNonQuery();
                                db1.cmd.Parameters.Clear();

                        db1.conn.Close();
                    }
                    catch (SqlException e)
                    {
                        Console.WriteLine(e);

                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.StackTrace.ToString());

                    }

            }
        }
        public void AgregarNota()
        {
            NotaISSQL ntgw = new NotaISSQL();
            NotaIS nota = new NotaIS();
            nota.IdAlmacen = Almacen.ElementAt(0).IdAlmacen;
            // Logica de  Referencia de documento
            if (SelectedOrden!=null || SelectedGuia!=null || SelectedDevolucion!=null || SelectedSolicitud !=null)
            {
                // si hay documento de referencia colocar id;
                nota.IdDoc = TxtDocId;
            }
            else
            {
                //Si no existe documento de referencia colocar 0
                nota.IdDoc = 0;
            }
            if (String.IsNullOrEmpty(SelectedMotivo))
            {
                _windowManager.ShowDialog(new AlertViewModel(_windowManager, "No se pudo guardar"));
                return;
            }

            nota.IdMotivo = DataObjects.Almacen.MotivoSQL.BuscarMotivo(SelectedMotivo).Id;
            nota.IdResponsable = Responsable.ElementAt(0).IdUsuario;
            nota.Observaciones = Observaciones;
            nota.Tipo = 1;

            nota.LstProducto = this.LstProductos;

            nota.IdNota = ntgw.AgregarNota(nota);

            if (nota.IdNota > 0)
            {

                ProductoxTiendaSQL ptgw = new ProductoxTiendaSQL();
                ProductoSQL pgw = new ProductoSQL();

                List<ProductoCant> list = ntgw.BuscarNotas(nota.IdNota);
                if (u.IdTienda != 0) ptgw.ActualizarStockEntrada(list, u.IdTienda);
                else pgw.ActualizarStockEntrada(list);

                //Actualizar Documentos de Referencia para darlos por Terminados! :)

                if (SelectedOrden != null)
                {
                    guardarOrden(list);
                }

                if (SelectedGuia != null)
                {

                    CambiarEstadoGuia(SelectedGuia);

                }

                if (SelectedDevolucion != null)
                {

                    CambiarEstadoDevolucion(SelectedDevolucion);

                }

                if (SelectedSolicitud != null) {
                    CambiarEstadoSolicitud(SelectedSolicitud);
                }
                _windowManager.ShowDialog(new AlertViewModel(_windowManager, "Nota Creada"));
                //1: Agregar, 2: Editar, 3: Eliminar, 4: Recuperar, 5: Desactivar
                DataObjects.Seguridad.LogSQL.RegistrarActividad("Registrar Nota de Ingreso",  nota.IdNota.ToString(), 1);

            }
            else
            {
                _windowManager.ShowDialog(new AlertViewModel(_windowManager, "No se pudo guardar"));
            }
        }
        public void GuardarVenta(string cmbTipoVenta)
        {
            int numFilas = LstVenta.Count();
            if (numFilas > 0)
            {
                Venta v = new Venta();
                v.LstDetalle = new List<DetalleVenta>();
                v.LstPagos = new List<VentaPago>();
                v.LstDetalleServicio = new List<DetalleVentaServicio>();
                //guardar datos de la venta
                //completar
                if (tipoVenta[cmbTipoVenta] == 0)
                    v.TipoDocPago = "Boleta";
                else
                {
                    v.TipoDocPago = "Factura";
                    //validar que los datos de ruc y razon social
                    if (!string.IsNullOrEmpty(TxtRuc) && !string.IsNullOrEmpty(TxtRazonSocial))
                    {
                        v.Ruc = TxtRuc;
                        v.RazonSocial = TxtRazonSocial;
                    }
                    else
                    {
                        MessageBox.Show("Falta ingresar Ruc o Razón Social", "AVISO", MessageBoxButton.OK, MessageBoxImage.Error);
                        return;
                    }
                }

                v.NumDocPago = null;
                v.TipoVenta = "Tienda";
                v.Estado = 1;
                v.FechaReg = System.DateTime.Now;
                v.IdUsuario = Convert.ToInt32(Thread.CurrentPrincipal.Identity.Name);
                //idCliente desde la tarjeta de este si es que hay
                if (!string.IsNullOrEmpty(TxtCliente))
                {
                    v.IdCliente = Convert.ToInt32(cliente.Id);
                    v.CodTarjeta = Convert.ToInt32(TxtCliente);
                }
                else
                {
                    v.IdCliente = -1;
                    v.CodTarjeta = -1;
                }

                //guardar detalle de la venta
                foreach (DetalleVenta dv in lstVenta)
                {
                    v.LstDetalle.Add(dv);
                }
                v.Monto = total;
                v.Descuento = desc;
                v.Igv = igv_total;

                v.PtosGanados = Convert.ToInt32(v.Monto / PUNTO);

                foreach (VentaPago vp in lstPagos)
                {
                    if (vp.Nombre.Equals("Efectivo"))
                    {
                        vp.Monto -= Double.Parse(txtVuelto);
                    }
                    v.LstPagos.Add(vp);
                }

                //guardar detalle de servicios de la venta, si es que hay
                if (LstVentaServicios.Count() > 0)
                {
                    foreach (DetalleVentaServicio dvs in LstVentaServicios)
                    {
                        v.LstDetalleServicio.Add(dvs);
                    }
                }

                //insertar en la base de datos
                DBConexion db = new DBConexion();
                db.conn.Open();
                SqlTransaction trans = db.conn.BeginTransaction(IsolationLevel.Serializable);
                db.cmd.Transaction = trans;
                VentaSQL vsql = new VentaSQL(db);
                if (v.IdCliente == -1)
                {
                    int k = vsql.AgregarSinCliente(v);
                    if (k != 0)
                    {

                        NotaISSQL ntgw = new NotaISSQL();
                        NotaIS nota = new NotaIS();
                        AlmacenSQL asql = new AlmacenSQL();
                        nota.IdAlmacen = asql.BuscarAlmacen(-1, idTienda, 2).IdAlmacen;
                        // Logica de  Referencia de documento

                        //Si existe documento de referencia colocar el ID
                        nota.IdDoc = v.IdVenta;

                        nota.IdMotivo = 9;
                        nota.IdResponsable = v.IdUsuario;
                        nota.Observaciones = "Venta en Cajero";
                        nota.Tipo = 2;
                        List<ProductoCant> LstProductos = new List<ProductoCant>();
                        List<ProductoCant> lpcan = new List<ProductoCant>();

                        for (int i = 0; i < v.LstDetalle.Count; i++)
                        {
                            Producto p = new ProductoSQL().Buscar_por_CodigoProducto(v.LstDetalle.ElementAt(i).IdProducto);
                            ProductoCant pcan = new ProductoCant();
                            pcan.IdProducto = p.IdProducto;
                            pcan.CodigoProd = p.CodigoProd;
                            pcan.Nombre = p.Nombre;
                            pcan.CanAtender = v.LstDetalle.ElementAt(i).Cantidad.ToString();
                            lpcan.Add(pcan);

                        }
                        LstProductos = new List<ProductoCant>(lpcan);

                        nota.LstProducto = LstProductos;

                        nota.IdNota = ntgw.AgregarNota(nota);

                        trans.Commit();
                        ntgw.AgregarNotaxSector(nota);
                        MessageBox.Show("Venta Realizada con Exito");
                        Limpiar();
                    }
                    else
                    {
                        trans.Rollback();
                        MessageBox.Show("Ocurrio un Error en el proceso");
                    }
                }
                else
                {
                    int k = vsql.Agregar(v);
                    if (k != 0)
                    {

                        NotaISSQL ntgw = new NotaISSQL();
                        NotaIS nota = new NotaIS();
                        AlmacenSQL asql = new AlmacenSQL();
                        nota.IdAlmacen = asql.BuscarAlmacen(-1, idTienda, 2).IdAlmacen;
                        // Logica de  Referencia de documento

                        //Si existe documento de referencia colocar el ID
                        nota.IdDoc = v.IdVenta;

                        nota.IdMotivo = 9;
                        nota.IdResponsable = v.IdUsuario;
                        nota.Observaciones = "Venta en Cajero";
                        nota.Tipo = 2;
                        List<ProductoCant> LstProductos = new List<ProductoCant>();
                        List<ProductoCant> lpcan = new List<ProductoCant>();

                        for (int i = 0; i < v.LstDetalle.Count; i++)
                        {
                            Producto p = new ProductoSQL().Buscar_por_CodigoProducto(v.LstDetalle.ElementAt(i).IdProducto);
                            ProductoCant pcan = new ProductoCant();
                            pcan.IdProducto = p.IdProducto;
                            pcan.CodigoProd = p.CodigoProd;
                            pcan.Nombre = p.Nombre;
                            pcan.CanAtender = v.LstDetalle.ElementAt(i).Cantidad.ToString();
                            lpcan.Add(pcan);

                        }
                        LstProductos = new List<ProductoCant>(lpcan);

                        nota.LstProducto = LstProductos;

                        nota.IdNota = ntgw.AgregarNota(nota);

                        trans.Commit();

                        ntgw.AgregarNotaxSector(nota);
                        MessageBox.Show("Venta Realizada con Exito");
                        Limpiar();
                    }
                    else
                    {
                        trans.Rollback();
                        MessageBox.Show("Ocurrio un Error en el proceso");
                    }
                }

                if (v.TipoDocPago.Equals("Boleta"))
                {
                    GenerarPDFBoletaProductos(v);
                    if (v.LstDetalleServicio.Count() > 0)
                        GenerarPDFBoletaServicios(v);
                }
                else
                {
                    GenerarPDFFacturaProductos(v);
                    if (v.LstDetalleServicio.Count() > 0)
                        GenerarPDFFacturaServicios(v);
                }

            }
            else
            {
                MessageBox.Show("Debe ingreasar datos de la venta");
                return;
            }
        }
        public void GuardarMovimiento(DynamicGrid anaquel, DynamicGrid deposito)
        {
            int exito;

            /*Inicializacion de la transacción*/
            DBConexion db = new DBConexion();
            try
            {
                db.conn.Open();
                SqlTransaction trans = db.conn.BeginTransaction(IsolationLevel.ReadCommitted);

                db.cmd.Transaction = trans;

                SectorSQL sectorSQL = new SectorSQL(db);
                UbicacionSQL ubicacionSQL = new UbicacionSQL(db);

                /*Tablas temporales*/
                DataTable sectoresDT = sectorSQL.CrearSectoresDT();
                DataTable ubicacionesDT = ubicacionSQL.CrearUbicacionesDT();

                List<Sector> lstSectores = new List<Sector>();
                List<Ubicacion> ubicacionesModificadas = new List<Ubicacion>();

                /*Agrupo todos los sectores y ubicaciones modificadas*/
                for (int i = 0; i < anaquel.lstZonas.Count; i++)
                {
                    lstSectores.AddRange(anaquel.lstZonas[i].LstSectores);
                    for (int j = 0; j < anaquel.lstZonas[i].LstSectores.Count; j++)
                    {
                        ubicacionesModificadas.AddRange(anaquel.lstZonas[i].LstSectores[j].LstUbicaciones);
                    }

                }

                /*Agrego las filas a los DT*/
                sectorSQL.AgregarFilasToSectoresDT(sectoresDT, lstSectores);
                ubicacionSQL.AgregarFilasToUbicacionesDT(ubicacionesDT, ubicacionesModificadas);

                /*empieza el guardado en la bd*/
                exito = sectorSQL.AgregarMasivo(sectoresDT, trans); //insertados en TemporalSector
                if (exito > 0)
                {
                    exito = sectorSQL.ActualizarSectorMasivo(); //actualizados en tabla Sector
                    if (exito > 0)
                    {

                        exito = sectorSQL.ActualizarIdSector(); //actualizo idSector en tabla sector
                        if (exito > 0)
                        {
                            /*Agrego las ubicaciones de almacen de salida*/
                            ubicacionSQL.AgregarFilasToUbicacionesDT(ubicacionesDT, deposito.Ubicaciones, deposito.Ubicaciones[0][0][0].IdAlmacen);
                            exito = ubicacionSQL.AgregarMasivo(ubicacionesDT, trans);
                            if (exito > 0)
                            {
                                exito = ubicacionSQL.ActualizarIdSector();
                                if (exito > 0)
                                {
                                    exito = ubicacionSQL.ActualizarUbicacionMasivo();
                                    if (exito > 0)
                                    {
                                        /*Agrego el movimiento como un "todo" a la bd*/
                                        NotaISSQL notaSQL = new NotaISSQL(db);

                                        /*Salida para el depósito*/
                                        NotaIS nota = new NotaIS();
                                        nota.IdMotivo = 8;
                                        nota.Tipo = 2;
                                        nota.Observaciones = "";
                                        nota.IdDoc = 0;
                                        nota.IdAlmacen = idDeposito;
                                        nota.IdResponsable = idResponsable;

                                        /*Entrada para el anaquel*/
                                        NotaIS notaAn = new NotaIS();
                                        notaAn.IdMotivo = 8;
                                        notaAn.Tipo = 1;
                                        notaAn.Observaciones = "";
                                        notaAn.IdDoc = 0;
                                        notaAn.IdAlmacen = idAnaquel;
                                        notaAn.IdResponsable = idResponsable;

                                        int idNotaDeposito = notaSQL.AgregarNota(nota, 1);
                                        int idNotaAnaquel = notaSQL.AgregarNota(notaAn, 1);

                                        if (idNotaDeposito > 0 && idNotaAnaquel> 0)
                                        {
                                            exito = sectorSQL.ActualizarTemporalSector(idNotaDeposito);

                                            if (exito > 0)
                                            {
                                                exito = notaSQL.AgregarNotaxSector();

                                                if (exito > 0)
                                                {
                                                    exito = sectorSQL.ActualizarTemporalSector(idNotaAnaquel);

                                                    if (exito > 0)
                                                    {
                                                        exito = notaSQL.AgregarNotaxSector();
                                                        if (exito > 0)
                                                        {
                                                            UtilesSQL util = new UtilesSQL(db);
                                                            util.LimpiarTabla("TemporalUbicacion");
                                                            util.LimpiarTabla("TemporalSector");

                                                            trans.Commit();
                                                            _windowManager.ShowDialog(new AlertViewModel(_windowManager, "Los productos fueron transferidos correctamente"));
                                                            return;
                                                        }

                                                    }

                                                }
                                            }

                                        }

                                    }
                                }
                            }
                        }
                    }
                }
                _windowManager.ShowDialog(new AlertViewModel(_windowManager, "Lo sentimos , se produjo un error"));
                trans.Rollback();
            }
            catch (SqlException e)
            {
                _windowManager.ShowDialog(new AlertViewModel(_windowManager, "Se fue la conexión"));
            }
        }
        public Boolean EstaEnGuia(NotaIS nota)
        {
            List<GuiaRemision> list = new GuiaDeRemisionSQL().BuscarGuiaDeRemision(null, 0, null);
            for (int i=0; i<list.Count; i++)
            {
                if (list[i].Nota != null)
                {
                    if (list[i].Nota.IdNota == nota.IdNota)
                        return true;
                }
            }

            return false;
        }