private void BtnAdd_Click(object sender, EventArgs e) { if (!checkFieldsDEpartureDetail()) { return; } var db = new ConnectionDB(); var data = db.Inventories.FirstOrDefault(x => x.id_warehouse == idWarehouse && x.id_item == idItem); int quantity = Convert.ToInt32(tbQuantity.Text); if (data == null) { MessageBox.Show("Elementos insuficientes", "Control de inventarios", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (data.quantity < quantity) { MessageBox.Show("Elementos insuficientes", "Control de inventarios", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } var departureDetail = new DepartureDet { id_item = idItem, quantity = Convert.ToInt16(tbQuantity.Text), user = tbPerson.Text, }; departureList.Add(departureDetail); cbItem.Text = ""; tbQuantity.Text = ""; fill_dgvDepartureDetails2(); }
private void BtnSave_Click(object sender, EventArgs e) { var db = new ConnectionDB(); Departure departure = new Departure(); DepartureDet departureDet = new DepartureDet(); if (dgvDepartureDetail2.Rows.Count == 0) { MessageBox.Show("Sin artículos por entregar", "Control Inventario", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } departure.date = DateTime.Now; departure.id_cellar = idCellar; departure.id_user = idUser; departure.id_warehouse = idWarehouse; departure.observation = tbObservations.Text; departure.serie = cbSerie.Text; departure.folio = Convert.ToInt32(tbFolio.Text); db.Departures.Add(departure); db.SaveChanges(); MessageBox.Show("Registro con éxito", "Salida", MessageBoxButtons.OK, MessageBoxIcon.Information); foreach (DataGridViewRow item in dgvDepartureDetail2.Rows) { departureDet.id_departure = departure.id; departureDet.id_item = (int)item.Cells[0].Value; departureDet.quantity = (int)item.Cells[2].Value; departureDet.user = (string)item.Cells[3].Value; db.DepartureDet.Add(departureDet); db.SaveChanges(); var inventory = db.Inventories.FirstOrDefault(x => x.id_warehouse == idWarehouse && x.id_item == departureDet.id_item); if (inventory != null) { inventory.quantity = inventory.quantity - departureDet.quantity; db.Entry(inventory).State = EntityState.Modified; db.SaveChanges(); } var inventory_cellar = db.Inventories.FirstOrDefault(x => x.id_warehouse == idCellar && x.id_item == departureDet.id_item); if (inventory_cellar != null) { inventory_cellar.quantity = inventory_cellar.quantity + departureDet.quantity; db.Entry(inventory_cellar).State = EntityState.Modified; db.SaveChanges(); } else { inventory_cellar = new Inventory { id_item = departureDet.id_item, id_warehouse = idCellar, quantity = departureDet.quantity }; db.Inventories.Add(inventory_cellar); db.SaveChanges(); } } fill_dgvDepartures(""); enable_fields(false); btnNew.Visible = true; btnCancel.Visible = false; btnSave.Visible = false; clearFieldsTab2(); }