示例#1
0
        private void ItemsForm_FormClosing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            CartForm cartForm = Application.OpenForms.OfType <CartForm>().FirstOrDefault();

            cartForm.LoadData();
            cartForm.EnableAddItemButton(true);
        }
示例#2
0
        private void AddToCartButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (itemListView.CheckedItems.Count > 0)
                {
                    using (TransactionScope scope = new TransactionScope())
                    {
                        foreach (ListViewItem listViewItem in itemListView.CheckedItems)
                        {
                            Item         item         = _itemManager.GetById(listViewItem.SubItems[1].Text.ToInt(-1));
                            PurchaseItem purchaseItem = new PurchaseItem {
                                PurchaseId = _purchase.Id, ItemId = item.Id, Price = item.Price, Quantity = 1, SubTotal = item.Price
                            };
                            item.Stocks -= 1;

                            if ((purchaseItem.Id = _purchaseItemManager.Add(purchaseItem)) > 0 && _itemManager.Update(item))
                            {
                                _purchases.Add(new PurchaseDetails(purchaseItem, item));
                                LoadData();
                                CartForm cartForm = Application.OpenForms.OfType <CartForm>().FirstOrDefault();
                                cartForm.LoadData();
                            }
                            else
                            {
                                string caption = "Can't add item(s).";
                                string message = "Please try again.";
                                MessageBox.Show(message, caption, MessageBoxButtons.OK);
                                return;
                            }
                        }

                        scope.Complete();
                    }
                }
                else
                {
                    string caption = "No item selected.";
                    string message = "Please select an item to be added in your cart.";
                    MessageBox.Show(message, caption, MessageBoxButtons.OK);
                }
            }
            catch (Exception ex)
            {
                Logger.log.Error(ex.ToString());
            }
        }
        private void NewPurchaseButton_Click(object sender, EventArgs e)
        {
            try
            {
                PurchaseHistory purchaseHistory = _customer.PurchaseHistory.Find(x => x.Purchase.Status == ProfileStringConstants.PENDING);
                Purchase        purchase;

                if (purchaseHistory == null)
                {
                    purchase = new Purchase {
                        CustomerId = _customer.Info.Id, Status = ProfileStringConstants.PENDING, Date = DateTime.Now.ToString(), Total = 0
                    };

                    if ((purchase.Id = _purchaseManager.Add(purchase)) > 0)
                    {
                        LoadData();
                        EnableNewPurchaseButton(false);
                        CartForm cartForm = new CartForm(_customer.PurchaseHistory.Find(x => x.Purchase.Id == purchase.Id).PurchaseDetails, purchase);
                        cartForm.MdiParent = this.ParentForm;
                        cartForm.Show();
                    }
                    else
                    {
                        string message = "Can't create new purchase.";
                        string caption = "Please try again.";
                        MessageBox.Show(message, caption, MessageBoxButtons.OK);
                    }
                }
                else
                {
                    string       message = "You have a pending purchase. Would you like to restore it? Selecting no will delete that purchase.";
                    string       caption = string.Empty;
                    DialogResult result  = MessageBox.Show(message, caption, MessageBoxButtons.YesNo);

                    if (result == DialogResult.Yes)
                    {
                        purchase = purchaseHistory.Purchase;
                        EnableNewPurchaseButton(false);
                        CartForm cartForm = new CartForm(purchaseHistory.PurchaseDetails, purchase);
                        cartForm.MdiParent = this.ParentForm;
                        cartForm.Show();
                    }
                    else
                    {
                        int[] ids = { purchaseHistory.Purchase.Id };
                        List <PurchaseItem> purchaseItemDel = new List <PurchaseItem>()
                        {
                            new PurchaseItem {
                                PurchaseId = ids[0]
                            }
                        };
                        List <Purchase> purchaseDel = new List <Purchase>()
                        {
                            new Purchase {
                                Id = ids[0]
                            }
                        };

                        List <PurchaseItem> purchaseItems = _purchaseItemManager.GetAllWhere(purchaseItemDel[0]);

                        if (purchaseItems.Count > 0)
                        {
                            if (_customer.PurchaseHistory.Find(x => x.Purchase.Id == ids[0]).Purchase.Status == ProfileStringConstants.PENDING)
                            {
                                foreach (PurchaseItem purchaseItem in purchaseItems)
                                {
                                    Item item = _itemManager.GetById(purchaseItem.ItemId);
                                    item.Stocks += purchaseItem.Quantity;
                                    _itemManager.Update(item);
                                }
                            }

                            _purchaseItemManager.Delete(purchaseItemDel);
                        }

                        if (_purchaseManager.Delete(purchaseDel))
                        {
                            LoadData();
                            purchase = new Purchase {
                                CustomerId = _customer.Info.Id, Status = ProfileStringConstants.PENDING, Date = DateTime.Now.ToString(), Total = 0
                            };

                            if ((purchase.Id = _purchaseManager.Add(purchase)) > 0)
                            {
                                LoadData();
                                EnableNewPurchaseButton(false);
                                CartForm cartForm = new CartForm(_customer.PurchaseHistory.Find(x => x.Purchase.Id == purchase.Id).PurchaseDetails, purchase);
                                cartForm.MdiParent = this.ParentForm;
                                cartForm.Show();
                            }
                            else
                            {
                                message = "Can't create new purchase.";
                                caption = "Please try again.";
                                result  = MessageBox.Show(message, caption, MessageBoxButtons.OK);
                            }
                        }
                        else
                        {
                            message = "Can't delete.";
                            caption = "Please try again.";
                            result  = MessageBox.Show(message, caption, MessageBoxButtons.OK);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.log.Error(ex.ToString());
            }
        }