void DecreaseStockinHist(POSEntities p, int quanity, StockinHistory st) { st.Quantity -= quanity; if (st.Quantity <= 0) { p.StockinHistories.Remove(st); } }
private void stockinBtn_Click(object sender, EventArgs e) { if (MessageBox.Show("Are you sure you want to stock these items?", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel) { return; } using (var p = new POSEntities()) { Product product; for (int i = 0; i < inventoryTable.RowCount; i++) { var itemId = inventoryTable.Rows[i].Cells[0].Value.ToString(); var suppName = inventoryTable.Rows[i].Cells[6].Value.ToString(); var serialNum = inventoryTable.Rows[i].Cells[1].Value.ToString(); var q = Convert.ToInt32((inventoryTable.Rows[i].Cells[3].Value.ToString())); product = p.Products.FirstOrDefault(x => x.ItemId == itemId && x.Supplier.Name == suppName); InventoryItem it = new InventoryItem(); //Console.WriteLine(it.Id); if (string.IsNullOrEmpty(serialNum)) { it = p.InventoryItems.FirstOrDefault(x => x.Product.Id == product.Id && x.SerialNumber == null); if (it == null) { it = new InventoryItem(); it.Product = product; it.Quantity = q; p.InventoryItems.Add(it); } else { it.Quantity += q; } } else { it = new InventoryItem(); it.Product = product; it.Quantity = q; it.SerialNumber = serialNum; p.InventoryItems.Add(it); } //p.SaveChanges(); var stockinHist = new StockinHistory(); stockinHist.InventoryItem = it; stockinHist.ItemName = it.Product.Item.Name; stockinHist.Cost = it.Product.Cost; stockinHist.Supplier = it.Product.Supplier.Name; stockinHist.Date = DateTime.Now; stockinHist.Quantity = q; stockinHist.SerialNumber = it.SerialNumber; stockinHist.LoginUsername = p.Logins.FirstOrDefault(x => x.Username == currLogin.Username).Username; p.StockinHistories.Add(stockinHist); } p.SaveChanges(); //OnSave?.Invoke(this, null); MessageBox.Show("Saved.", "", MessageBoxButtons.OK, MessageBoxIcon.Information); this.DialogResult = DialogResult.OK; } }