private void CartForm_FormClosing(object sender, System.ComponentModel.CancelEventArgs e) { if (ComputeTotalPrice() <= 0) { List <Purchase> purchases = new List <Purchase>() { new Purchase { Id = _purchase.Id } }; if (!_purchaseManager.Delete(purchases)) { Logger.log.Error($"A pending purchase with id {_purchase.Id} can't be deleted."); } } ItemsForm itemsForm = Application.OpenForms.OfType <ItemsForm>().FirstOrDefault(); if (itemsForm != null) { itemsForm.Close(); } ProfileForm profileForm = Application.OpenForms.OfType <ProfileForm>().FirstOrDefault(); profileForm.LoadData(); profileForm.EnableNewPurchaseButton(true); }
private void AddButton_Click(object sender, EventArgs e) { EnableAddItemButton(false); ItemsForm itemsForm = new ItemsForm(_purchases, _purchase); itemsForm.MdiParent = this.ParentForm; itemsForm.Show(); }
private void UpdateButton_Click(object sender, EventArgs e) { if (cartListView.SelectedItems.Count > 0) { try { Item item = _itemManager.GetById(cartListView.SelectedItems[0].SubItems[1].Text.ToInt(-1)); PurchaseDetails purchaseDetails = _purchases.Find(x => x.PurchaseItem.ItemId == item.Id); using (TransactionScope scope = new TransactionScope()) { int quantity = textBoxQuantity.Text.ToInt(-1); if (quantity > 0 && quantity <= (item.Stocks + purchaseDetails.PurchaseItem.Quantity)) { item.Stocks -= (quantity - purchaseDetails.PurchaseItem.Quantity); purchaseDetails.PurchaseItem.Quantity = quantity; purchaseDetails.PurchaseItem.SubTotal = quantity * item.Price; if (!_purchaseItemManager.Update(purchaseDetails.PurchaseItem) || !_itemManager.Update(item)) { string message = "Can't update item."; string caption = "Please try again."; MessageBox.Show(message, caption, MessageBoxButtons.OK); return; } } else { string caption = "Please try again."; string message = "Can't update item for that value."; MessageBox.Show(message, caption, MessageBoxButtons.OK); } textBoxTotal.Text = ComputeTotalPrice().ToString(); LoadData(); ItemsForm itemsForm = Application.OpenForms.OfType <ItemsForm>().FirstOrDefault(); if (itemsForm != null) { itemsForm.LoadData(); } scope.Complete(); } } catch (Exception ex) { Logger.log.Error(ex.ToString()); } } else { string caption = string.Empty; string message = "Please select an item in your cart."; MessageBox.Show(message, caption, MessageBoxButtons.OK); } }
private void RemoveButton_Click(object sender, EventArgs e) { try { if (cartListView.CheckedItems.Count > 0) { List <string> failures = new List <string>(); foreach (ListViewItem listViewItem in cartListView.CheckedItems) { using (TransactionScope scope = new TransactionScope()) { Item item = _itemManager.GetById(listViewItem.SubItems[1].Text.ToInt(-1)); PurchaseDetails purchaseDetails = _purchases.Find(x => x.PurchaseItem.ItemId == item.Id); int[] id = { purchaseDetails.PurchaseItem.Id }; List <PurchaseItem> purchaseItem = new List <PurchaseItem>() { new PurchaseItem { Id = id[0] } }; item.Stocks += purchaseDetails.PurchaseItem.Quantity; if (_purchaseItemManager.Delete(purchaseItem) && _itemManager.Update(item)) { _purchases.Remove(purchaseDetails); textBoxTotal.Text = ComputeTotalPrice().ToString(); scope.Complete(); } else { failures.Add(item.Name); } } } ItemsForm itemsForm = Application.OpenForms.OfType <ItemsForm>().FirstOrDefault(); if (itemsForm != null) { itemsForm.LoadData(); } LoadData(); if (failures.Count > 0) { string items = string.Empty; foreach (string failure in failures) { items += $"{items}, {failure}"; } string message = $"Item/s {items} can't be deleted"; string caption = "Please try again."; MessageBox.Show(message, caption, MessageBoxButtons.OK); } } else { string caption = "Error"; string message = "Please toggle the checkbox for the\nitem(s) in your cart to remove."; MessageBox.Show(message, caption, MessageBoxButtons.OK); } } catch (Exception ex) { Logger.log.Error(ex.ToString()); } }