private void SaveButton_Click(object sender, EventArgs e) { var suppliername = selectedSupplierName; using (var dbCtx = new POSApplication.Model.posdbEntities()) { var item = dbCtx.suppliers.SingleOrDefault(x => x.SupplierName == suppliername); if (item != null && suppliername.CompareTo(item.SupplierName) == 0) { supplier c = (from x in dbCtx.suppliers where x.SupplierName == suppliername select x).First(); c.SupplierName = SupplierNameField.Text; c.SupplierAddress = SupplierAddressField.Text; c.ContactName = ContactPersonNameField.Text; c.ContactNumber = ContactPersonNumberField.Text; dbCtx.SaveChanges(); MessageBox.Show("Changes Updated Successfully."); } else if (item == null) { supplier c = (from x in dbCtx.suppliers where x.SupplierName == SupplierNameField.Text select x).SingleOrDefault(); if (c == null) { if (SupplierNameField.Text.Length > 0 && ContactPersonNameField.Text.Length > 0) { var r = new Model.supplier { SupplierName = SupplierNameField.Text, ContactName = ContactPersonNameField.Text, ContactNumber = ContactPersonNumberField.Text, SupplierAddress = SupplierAddressField.Text }; dbCtx.suppliers.Add(r); // call SaveChanges method to save student into database dbCtx.SaveChanges(); MessageBox.Show("New Supplier " + SupplierNameField.Text + " Added."); SuccessfulSupplierAddition(); } else { MessageBox.Show("Please enter New supplier name and contact person name."); } } else { MessageBox.Show("Supplier with the same name already exists."); } } } LoadExistingSuppliers(); }
public void deleteSupplier(string supplierName) { using (var dbCtx = new POSApplication.Model.posdbEntities()) { var itemToRemove = dbCtx.suppliers.First(x => x.SupplierName == supplierName); if (itemToRemove != null) { dbCtx.suppliers.Remove(itemToRemove); dbCtx.SaveChanges(); MessageBox.Show("Supplier Deleted."); } } }
public void UpdateChanges() { string categoryName = CategoryList.Text; string productName = ProductNameField.Text; string ReOrderLevel = ReOrderLevelField.Text; string SalePrice = SalePriceField.Text; if (SalePrice.Length == 0) { SalePrice = "0"; } if (ReOrderLevel.Length == 0) { ReOrderLevel = "0"; } if (productName.Length != 0) { using (var db = new POSApplication.Model.posdbEntities()) { var product = db.products.SingleOrDefault(x => x.ProductName == productName); //Model.inventory c = (from x in db.inventories // where x.ProductID == item.ProductID && x.UpdateDt == null // && x.PurchasePrice == saleprice || x.PurchasePrice == 0 // select x).FirstOrDefault(); decimal salepricedecimalvalue = Decimal.Parse(SalePrice); var items = db.inventories.Where( x => x.ProductID == product.ProductID && x.UpdateDt == null || (x.PurchasePrice == 0 && x.ProductID == product.ProductID && x.UpdateDt == null) ).ToList(); foreach (var item in items) { item.UpdateDt = DateTime.Now; db.SaveChanges(); var a = new inventory { ProductID = item.ProductID, CreateDt = DateTime.Now, SaleDate = null, PurchasePrice = item.PurchasePrice, Quantity = 0, UpdateDt = null, PackingTypeID = null, SumQuantity = item.SumQuantity, SalePrice = Decimal.Parse(SalePrice), InvoiceID = null }; db.inventories.Add(a); db.SaveChanges(); } db.SaveChanges(); //resetting the categories and products list to stop user from making changes. clearFields(); MessageBox.Show("Changes Saved"); } } else { //write something for the else condition } }
public void RecordSale(int checkoutOrHold, string modeOfPayment) { decimal discount = 0; decimal taxapplied = 0; if (AmountPaidField.Text.Length > 0) { try { var amountpaid = Decimal.Parse(AmountPaidField.Text); var total = Decimal.Parse(TotalSaleField.Text); BalanceField.Text = decimal.Round(amountpaid - total, 2).ToString(); if (total <= amountpaid) { decimal balance = amountpaid - total; //start creating db records using (var db = new POSApplication.Model.posdbEntities()) { if (TotalDiscountField.Text.Length > 0) { try { discount = Decimal.Parse(TotalDiscountField.Text); } catch (Exception e1) { MessageBox.Show(@"Enter numeric value for Discount"); } } if (TaxAppliedField.Text.Length > 0) { try { taxapplied = Decimal.Parse(TaxAppliedField.Text); } catch (Exception e1) { MessageBox.Show(@"Enter numeric value for Tax"); } } total = total + (total * (taxapplied / 100)); total = total - (total * (discount / 100)); BalanceField.Text = balance.ToString(); var so = new purchasesorder { AmountPaid = amountpaid }; currentSaleObject.AmountPaid = amountpaid; so.Balance = balance; currentSaleObject.Balance = decimal.Round((decimal)balance, 2); so.Discount = discount; currentSaleObject.Discount = discount; so.ModeOfPayment = modeOfPayment; so.PurchaseAmount = Decimal.Parse(TotalSaleField.Text); currentSaleObject.SaleAmount = decimal.Round((decimal)so.PurchaseAmount, 2); //so.SaleDate = DateTime.Now.Date; so.PurchaseDate = dateTimePicker1.Value.Date; //so.Date_dateofmonth_int = DateTime.Now.Day; so.Date_dateofmonth_int = dateTimePicker1.Value.Day; if (DateTime.Now.DayOfWeek == DayOfWeek.Sunday) { so.Date_Dayofweek_int = 7; } if (DateTime.Now.DayOfWeek == DayOfWeek.Monday) { so.Date_Dayofweek_int = 1; } if (DateTime.Now.DayOfWeek == DayOfWeek.Tuesday) { so.Date_Dayofweek_int = 2; } if (DateTime.Now.DayOfWeek == DayOfWeek.Wednesday) { so.Date_Dayofweek_int = 3; } if (DateTime.Now.DayOfWeek == DayOfWeek.Thursday) { so.Date_Dayofweek_int = 4; } if (DateTime.Now.DayOfWeek == DayOfWeek.Friday) { so.Date_Dayofweek_int = 5; } if (DateTime.Now.DayOfWeek == DayOfWeek.Saturday) { so.Date_Dayofweek_int = 6; } //so.Date_DayofWeek_string = DateTime.Now.DayOfWeek.ToString(); so.Date_DayofWeek_string = dateTimePicker1.Value.DayOfWeek.ToString(); //so.date_month_int = DateTime.Now.Month; so.date_month_int = dateTimePicker1.Value.Month; if (DateTime.Now.Month == 1) { so.date_month_string = "January"; } if (DateTime.Now.Month == 2) { so.date_month_string = "February"; } if (DateTime.Now.Month == 3) { so.date_month_string = "March"; } if (DateTime.Now.Month == 4) { so.date_month_string = "April"; } if (DateTime.Now.Month == 5) { so.date_month_string = "May"; } if (DateTime.Now.Month == 6) { so.date_month_string = "June"; } if (DateTime.Now.Month == 7) { so.date_month_string = "July"; } if (DateTime.Now.Month == 8) { so.date_month_string = "August"; } if (DateTime.Now.Month == 9) { so.date_month_string = "September"; } if (DateTime.Now.Month == 10) { so.date_month_string = "October"; } if (DateTime.Now.Month == 11) { so.date_month_string = "November"; } if (DateTime.Now.Month == 12) { so.date_month_string = "December"; } so.date_year_int = dateTimePicker1.Value.Year; so.time_hour = int.Parse(dateTimePicker1.Value.Hour.ToString()); so.PurchaseTime = dateTimePicker1.Value.TimeOfDay; so.UserName = AppConfig.loggedInUserName; //setting the status to checkout or hold sale. so.PurchaseStatus = checkoutOrHold; so.TaxPercentage = taxapplied; currentSaleObject.TaxPercentage = taxapplied; so.TerminalID = AppConfig.TerminalID; //recording the purchase as purchase on hold if (checkoutOrHold == 1) { so.HoldInvoice = 1; } else { so.HoldInvoice = 0; } so.InvoiceID = OrderIDField.Text; db.purchasesorders.Add(so); db.SaveChanges(); //Adding purchase Items int orderid = so.OrderID; foreach (var item in currentSaleObject.SaleItems) { decimal tax = item.TaxPercentage / 100; decimal purchaseprice = (item.PriceApplied * tax) + item.PriceApplied; decimal itemdiscount = item.Discount / 100; purchaseprice = (purchaseprice * itemdiscount) + purchaseprice; var s = new purchasesorderdetail { ProductID = item.ProductID, InvoiceID = OrderIDField.Text, Discount = item.Discount, PurchasePrice = item.PriceApplied, Quantity = item.Quantity }; s.PurchasePrice = purchaseprice; s.TaxPercentage = item.TaxPercentage; db.purchasesorderdetails.Add(s); db.SaveChanges(); ////////////////////////////////////////////////////////////////////////////////// //closing previous inventory record. ////////////////////////////////////////////////////////////////////////////////// Model.inventory c = (from x in db.inventories where (x.ProductID == item.ProductID && x.UpdateDt == null && x.PurchasePrice == purchaseprice || (x.ProductID == item.ProductID && x.UpdateDt == null && x.PurchasePrice == 0 && x.SalePrice == 0)) select x).SingleOrDefault(); if (c != null) { c.UpdateDt = dateTimePicker1.Value; db.SaveChanges(); decimal existingQuantity; if (c.SumQuantity != null) { existingQuantity = (decimal)c.SumQuantity; } else { existingQuantity = 0; } var inv = new inventory { ProductID = item.ProductID, CreateDt = dateTimePicker1.Value, PurchaseDate = dateTimePicker1.Value, PurchasePrice = purchaseprice, Quantity = item.Quantity, //TaxPaid = item.TaxPercentage, //ReorderValue = c.ReorderValue, UpdateDt = null, PackingTypeID = null, SumQuantity = existingQuantity + item.Quantity, SalePrice = c.SalePrice, InvoiceID = so.InvoiceID }; db.inventories.Add(inv); db.SaveChanges(); } else { decimal existingQuantity = 0; var d = (from x in db.inventories where x.ProductID == item.ProductID && x.UpdateDt == null select x).FirstOrDefault(); var inv = new inventory { ProductID = item.ProductID, CreateDt = dateTimePicker1.Value, PurchaseDate = dateTimePicker1.Value, PurchasePrice = purchaseprice, Quantity = item.Quantity, UpdateDt = null, PackingTypeID = null, SumQuantity = existingQuantity + item.Quantity, SalePrice = 0, InvoiceID = so.InvoiceID }; db.inventories.Add(inv); db.SaveChanges(); } } ///////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////// if (checkoutOrHold == 1) { MessageBox.Show(@"Purchase is put on HOLD. You can open the Purchases on hold again from Invoices Form."); } else { PrintPage(); currentSaleObject = new SaleClass(); currentSaleObject.SaleItems = new List <SaleItemClass>(); SaleItemsList.DataSource = null; SaleItemsList.Refresh(); clearFields(); } } } else { MessageBox.Show(@"Amount Paid Value should be larger or equal to Total"); } } catch (Exception e1) { MessageBox.Show(@"Enter numeric value for Amount Paid"); } } else { MessageBox.Show(@"Enter value for Amount Paid"); } }