public void Guardar(Ingreso ingreso) { _conexion = new ConexionBd(); SqlTransaction tran = null; try { SqlConnection cn = _conexion.AbrirConexion(); tran = cn.BeginTransaction(); _repositorioIngresos = new RepositorioIngresos(cn, tran); _repositorioProductos = new RepositorioProductos(cn, tran); _repositorioDetalleIngresos = new RepositorioDetalleIngresos(cn, tran); _repositorioKardex = new RepositorioKardex(cn, tran); _repositorioIngresos.Guardar(ingreso); foreach (var detalleIngreso in ingreso.DetalleIngresos) { detalleIngreso.Ingreso = ingreso; Kardex kardex = _repositorioKardex.GetUltimoKardex(detalleIngreso.Producto); if (kardex == null) { kardex = new Kardex(); kardex.Producto = detalleIngreso.Producto; kardex.Fecha = ingreso.Fecha; kardex.Movimiento = $"Ingreso Nro {ingreso.IngresoId}"; kardex.Entrada = detalleIngreso.Cantidad; kardex.Salida = 0; kardex.Saldo = detalleIngreso.Cantidad; } else { kardex.Fecha = ingreso.Fecha; kardex.Movimiento = $"Ingreso Nro {ingreso.IngresoId}"; kardex.Entrada = detalleIngreso.Cantidad; kardex.Salida = 0; kardex.Saldo += detalleIngreso.Cantidad; } _repositorioKardex.Guardar(kardex); detalleIngreso.Kardex = kardex; _repositorioDetalleIngresos.Guardar(detalleIngreso); _repositorioProductos.ActualizarStock(detalleIngreso.Producto, detalleIngreso.Cantidad); } tran.Commit(); } catch (Exception e) { tran.Rollback(); throw new Exception(e.Message); } }
public void Guardar(Venta venta) { conexion = new ConexionBd(); SqlTransaction transaction = null; try { SqlConnection cn = conexion.AbrirConexion(); transaction = cn.BeginTransaction(); repositorioVentas = new RepositorioVentas(cn, transaction); repositorioDetalleVentas = new RepositorioDetalleVentas(cn, transaction); repositorioProductos = new RepositorioProductos(cn, transaction); repositorioKardex = new RepositorioKardex(transaction, cn); repositorioVentas.Guardar(venta); foreach (var detalleVenta in venta.DetalleVentas) { detalleVenta.Venta = venta; Kardex kardex = repositorioKardex.GetUltimoKardex(detalleVenta.Producto); if (kardex == null) { kardex = new Kardex(); kardex.Producto = detalleVenta.Producto; kardex.Fecha = venta.Fecha; kardex.Movimiento = $"Venta numero {venta.VentaId}"; kardex.Entrada = 0; kardex.Salida = detalleVenta.Cantidad; kardex.Saldo = detalleVenta.Cantidad; } else { kardex.Fecha = venta.Fecha; kardex.Movimiento = $"Venta numero {venta.VentaId}"; kardex.Entrada = 0; kardex.Salida = detalleVenta.Cantidad; kardex.Saldo -= detalleVenta.Cantidad; } repositorioKardex.Guardar(kardex); detalleVenta.Kardex = kardex; repositorioDetalleVentas.Guardar(detalleVenta); repositorioProductos.ActualizarStock(detalleVenta.Producto, -detalleVenta.Cantidad); } transaction.Commit(); } catch (Exception e) { transaction.Rollback(); throw new Exception(e.Message); } }
public void Guardar(VentaEditDto ventaDto) { #region Pasar de Dto a Entidad var listaDetalles = new List <DetalleVenta>(); foreach (var itemDto in ventaDto.DetalleVentas) { var item = new DetalleVenta() { DetalleVentaId = itemDto.DetalleVentaId, Producto = new Producto { ProductoId = itemDto.Producto.ProductoId, NombreProducto = itemDto.Producto.NombreProducto }, Cantidad = itemDto.Cantidad, Precio = itemDto.Precio }; listaDetalles.Add(item); } var venta = new Venta { VentaId = ventaDto.VentaId, Cliente = new Cliente { ClienteId = ventaDto.Cliente.ClienteId, NombreCompania = ventaDto.Cliente.NombreCompania }, FechaVenta = ventaDto.FechaVenta, DetalleVentas = listaDetalles }; #endregion #region Guardar la Venta _conexionBd = new ConexionBd(); SqlTransaction tran = null; try { SqlConnection cn = _conexionBd.AbrirConexion(); tran = cn.BeginTransaction();//Comienza la transacción _repositorio = new RepositorioVentas(cn, tran); _repositorioProductos = new RepositorioProductos(cn, tran); _repositorioDetalle = new RepositorioDetalleVentas(cn, tran); _repositorio.Guardar(venta); #region Recorrer los detalles de la venta foreach (var detalleVenta in venta.DetalleVentas) { detalleVenta.Venta = venta; _repositorioDetalle.Guardar(detalleVenta); _repositorioProductos.ActualizarStock(detalleVenta.Producto, -detalleVenta.Cantidad); } #endregion tran.Commit();//Confirma la persistencia de los datos ventaDto.VentaId = venta.VentaId; } catch (Exception e) { tran.Rollback();//Tira para atrás toda la transacción si no se completa throw new Exception(e.Message); } #endregion }