public static EVentasDetalles Insertar(int idVenta, int idArticulo, int cantidad, decimal precio, decimal importe) { using (var db = new VentasConexión()) { var trx = db.Database.BeginTransaction(); try { var id = db.EVentasDetalles.Any() ? db.EVentasDetalles.Max(a1 => a1.Id) + 1 : 1; var a = new EVentasDetalles { Id = id, IdEmpresa = Lib.Configuration.IdEmpresa, IdVenta = idVenta, IdArticulo = idArticulo, Importe = importe, Cantidad = cantidad, Precio = precio }; db.EVentasDetalles.Add(a); db.SaveChanges(); trx.Commit(); return(a); } catch (Exception) { trx.Rollback(); throw; } } }
public static bool Insertar(EVentasDetalles detalles) { bool proceso = true; using (var db = new VentasConexión()) { var trx = db.Database.BeginTransaction(); try { var id = db.EVentasDetalles.Any() ? db.EVentasDetalles.Max(a1 => a1.Id) + 1 : 1; detalles.IdEmpresa = Lib.Configuration.IdEmpresa; detalles.Id = id; db.EVentasDetalles.Add(detalles); db.SaveChanges(); trx.Commit(); } catch (Exception) { trx.Rollback(); proceso = false; } } return(proceso); }
private void GuardarVenta() { EVentas _venta = new EVentas(); List <EVentasDetalles> _detalleVenta = new List <EVentasDetalles>(); try { _venta.IdCliente = IdCliente; _venta.Fecha = Fecha; _venta.Importe = Subtotal; _venta.Descuento = Descuento; _venta.DescuentoPorc = DescPorc; _venta.ImporteTotal = ImporteTotal; _venta.PrecioLista = PrecioLista; _venta.IdUsuario = IdUsuario; _venta.Estado = Estado; var idVentaRegistrada = VentasRepository.Insertar(_venta); if (idVentaRegistrada == 0) { MessageBox.Show("Error al registrar Venta", "Registrar venta"); return; } for (int i = 0; i <= Convert.ToInt32(dgvDetalles.Rows.Count - 1); i++) { EVentasDetalles detalle = new EVentasDetalles(); detalle.IdArticulo = Convert.ToInt32(dgvDetalles.Rows[i].Cells[0].Value); detalle.Cantidad = Convert.ToInt32(dgvDetalles.Rows[i].Cells[3].Value); detalle.Precio = Convert.ToInt32(dgvDetalles.Rows[i].Cells[4].Value); detalle.Importe = Convert.ToInt32(dgvDetalles.Rows[i].Cells[5].Value); detalle.IdVenta = idVentaRegistrada; _detalleVenta.Add(detalle); if (!VentasDetallesRepository.Insertar(detalle)) { VentasRepository.EliminarVentaRegistradaIncorrectamente(idVentaRegistrada); VentasDetallesRepository.EliminarDetallesVentaRegistradosIncorrectamente(idVentaRegistrada); MessageBox.Show("Error al registrar Venta", "Registrar venta"); return; } } if (Configuration.ImprimeVentas) { ImprimirVenta(_venta.Id); } if (Configuration.VentaDescuentaStock) { foreach (var item in _detalleVenta) { EArticulosRepository.DescontarStockArticulo( Convert.ToDecimal(item.IdArticulo), Convert.ToDecimal(item.Cantidad)); } } if (Configuration.SoloCobroEfectivo) { } MessageBox.Show("Venta registrada correctamente", "Registrar venta"); LimpiarVentana(); } catch (Exception ex) { ShowError("Error al intentar leer los datos: \n" + ex.Message); } }