private void btnSave_Click(object sender, EventArgs e) { try { if (radioAddNew.Checked) { string code = txtItemCode.Text; string Quantity = txtQty.Text; string UnitPrice = txtUnitPrice.Text; string SellingPrice = txtSellingPrice.Text; DateTime addedDate = txtAddingDate.Value.Date; DateTime expDate = txtEDate.Value.Date; DateTime mfDate = txtMDate.Value.Date; int icode = Convert.ToInt32(code); if (code != "" && Quantity != "" && UnitPrice != "" && SellingPrice != "") { _PhamaPOSEntities = _dbConnector.getConn(); int itemId = _PhamaPOSEntities.items.Where(a => a.itemCode == icode).FirstOrDefault().itemId; stock _stock = new stock(); _stock.itemId = itemId; _stock.isAvailable = true; _stock.entryDate = DateTime.Now; _stock.entryBy = _user.userName; _stock.itemUnitPriceBuying = Convert.ToDecimal(UnitPrice); _stock.itemUnitPriceSelling = Convert.ToDecimal(SellingPrice); _stock.runningQuantity = Convert.ToInt32(Quantity); _stock.stockExpDate = expDate; _stock.stockMfdDate = mfDate; _stock.StockQuantity = Convert.ToInt32(Quantity); _stock.stockReceivedDate = addedDate; _PhamaPOSEntities.stocks.Add(_stock); _PhamaPOSEntities.SaveChanges(); ClearFields(); MessageBox.Show("Stock Successfully added.."); if (itemCode != null) { this.Dispose(); addItem i = new addItem(_user); i.Show(); } else { this.Dispose(); } } else { MessageBox.Show("Please Fill All the mandetary fields.."); } } else { string code = txtItemCode.Text; string Quantity = txtQty.Text; string UnitPrice = txtUnitPrice.Text; string SellingPrice = txtSellingPrice.Text; DateTime addedDate = txtAddingDate.Value.Date; DateTime expDate = txtEDate.Value.Date; DateTime mfDate = txtMDate.Value.Date; int batchId = Convert.ToInt32(drpBatch.SelectedValue); int icode = Convert.ToInt32(code); if (code != "" && Quantity != "" && UnitPrice != "" && SellingPrice != "") { _PhamaPOSEntities = _dbConnector.getConn(); var _stock = _PhamaPOSEntities.stocks.Where(a => a.stockId == batchId).FirstOrDefault(); _stock.stockExpDate = expDate; _stock.runningQuantity = Convert.ToInt32(Quantity); _stock.itemUnitPriceBuying = Convert.ToDecimal(UnitPrice); _stock.itemUnitPriceSelling = Convert.ToDecimal(SellingPrice); _stock.stockMfdDate = mfDate; _stock.StockQuantity = Convert.ToInt32(Quantity); _PhamaPOSEntities.stocks.Attach(_stock); var entry = _PhamaPOSEntities.Entry(_stock); entry.Property(a => a.runningQuantity).IsModified = true; entry.Property(a => a.itemUnitPriceBuying).IsModified = true; entry.Property(a => a.itemUnitPriceSelling).IsModified = true; entry.Property(a => a.stockExpDate).IsModified = true; entry.Property(a => a.stockMfdDate).IsModified = true; entry.Property(a => a.StockQuantity).IsModified = true; _PhamaPOSEntities.SaveChanges(); MessageBox.Show("Stock Successfully updated.."); ClearFields(); this.Hide(); } else { MessageBox.Show("Please Fill All the mandetary fields.."); } } } catch (Exception ex) { MessageBox.Show("Error Occured.. Please Retry.. " + ex.Message); } }
private void button1_Click(object sender, EventArgs e) { using (var contex = new PhamaPOSEntities()) { using (var dbContextTransaction = contex.Database.BeginTransaction()) { try { saleBatch sb = new saleBatch(); sb.entryBy = _user.userName; sb.entryDate = DateTime.Now; sb.saleDate = DateTime.Now; sb.discountedAmount = Convert.ToDecimal(_txtDiscAmt); sb.grandTotal = Convert.ToDecimal(_grandTotal); sb.subTotal = Convert.ToDecimal(_subTotal); contex.saleBatches.Add(sb); contex.SaveChanges(); foreach (DataRow dr in _tblItems.Rows) { Int64 batchID = Convert.ToInt64(dr["Item Sock"]); int quantity = Convert.ToInt32(dr["Quantity"]); //update stock stock s = new stock(); s = contex.stocks.Where(a => a.stockId == batchID).FirstOrDefault(); if (s.runningQuantity == quantity) { s.runningQuantity = 0; s.isAvailable = false; s.stockClearedDate = DateTime.Now; contex.stocks.Attach(s); var entry = contex.Entry(s); entry.Property(a => a.runningQuantity).IsModified = true; entry.Property(a => a.isAvailable).IsModified = true; entry.Property(a => a.stockClearedDate).IsModified = true; contex.SaveChanges(); } else { s.runningQuantity = s.runningQuantity - quantity; contex.stocks.Attach(s); var entry = contex.Entry(s); entry.Property(a => a.runningQuantity).IsModified = true; contex.SaveChanges(); } //update sale sale sale = new sale(); sale.amount = Convert.ToDecimal(dr["Total"]); sale.itemStockId = Convert.ToInt64(dr["Item Sock"]); sale.saleBatchId = sb.saleBatchId; sale.soldQuantity = Convert.ToInt64(dr["Quantity"]); sale.unitPrice = Convert.ToDecimal(dr["Unit Price"]); contex.sales.Add(sale); contex.SaveChanges(); //form.Show(); } dbContextTransaction.Commit(); MessageBox.Show("Payment Proceed Successfull.."); home form = new home(_user); form.Show(); this.Close(); } catch (Exception) { MessageBox.Show("Error Occured.. Please Retry.."); dbContextTransaction.Rollback(); } } } }