private void btn_Save_Click(object sender, EventArgs e)
        {
            if (_itemOUT == false && _suppDAO.CheckSupplierByID(this.cBoxID.Text) == false)
            {
                MessageBox.Show("Sorry, Supplier not found, please insert another supplier ID.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.cBoxID.Focus();
            }
            else if (_itemOUT == true && _custDAO.CheckCustomerByID(this.cBoxID.Text) == false)
            {
                MessageBox.Show("Sorry, Customer not found, please insert another supplier ID.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.cBoxID.Focus();
            }
            else
            {
                if (MessageBox.Show("Are you sure to add this data ? ", this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    string             transNumber = _transDAO.GetTransactionNumberNext();
                    List <Transaction> listTrans   = new List <Transaction>();
                    foreach (DataGridViewRow row in this.dgvTransaction.Rows)
                    {
                        if (Convert.ToInt32(row.Cells[3].Value) > 0)
                        {
                            if (_itemOUT)
                            {
                                listTrans.Add(new Transaction
                                {
                                    transactionID     = this.txtBox_TransID.Text,
                                    transactionNumber = transNumber,
                                    transactionDate   = this.dtp_TransDate.Value.ToShortDateString(),
                                    itemData          = _itemDAO.GetItemByID(row.Cells[0].Value.ToString()),
                                    suppData          = null,
                                    custData          = _custDAO.GetCustomerByID(this.cBoxID.Text),
                                    qtyBefore         = Convert.ToInt32(row.Cells[2].Value),
                                    qtyTrans_IN       = 0,
                                    qtyTrans_OUT      = Convert.ToInt32(row.Cells[3].Value),
                                    qtyAfter          = Convert.ToInt32(row.Cells[4].Value)
                                });
                            }
                            else
                            {
                                listTrans.Add(new Transaction
                                {
                                    transactionID     = this.txtBox_TransID.Text,
                                    transactionNumber = transNumber,
                                    transactionDate   = this.dtp_TransDate.Value.ToShortDateString(),
                                    itemData          = _itemDAO.GetItemByID(row.Cells[0].Value.ToString()),
                                    suppData          = _suppDAO.GetSupplierByID(this.cBoxID.Text),
                                    custData          = null,
                                    qtyBefore         = Convert.ToInt32(row.Cells[2].Value),
                                    qtyTrans_IN       = Convert.ToInt32(row.Cells[3].Value),
                                    qtyTrans_OUT      = 0,
                                    qtyAfter          = Convert.ToInt32(row.Cells[4].Value)
                                });
                            }
                        }
                        transNumber = $"{(int.Parse(transNumber) + 1).ToString("00000")}";

                        _itemDAO.UpdateQuantity(row.Cells[0].Value.ToString(), Convert.ToInt32(row.Cells[4].Value));
                    }
                    _transDAO.AddTransaction(listTrans);

                    _itemDAO.UpdateAllItem();

                    this.Close();
                }
            }
        }
Exemple #2
0
        public ActionResult CheckOut()
        {
            Cart cart = (Cart)Session[Constants.CART];

            if (cart == null)
            {
                return(RedirectToAction("Index"));
            }
            Dictionary <MobileItem, int> items = cart.items;

            MobileItem[] keys      = items.Keys.ToArray <MobileItem>();
            MobileDAO    MobileDao = new MobileDAO();
            bool         check     = true;

            for (int i = 0; i < keys.Length; i++)
            {
                int quantity = MobileDao.GetQuantity(keys[i].MobileId);
                if (quantity == -1)
                {
                    TempData["NotAvailable"] += string.Format("{0} is not available,  \n", keys[i].Name);
                    check = false;
                }
                else if (quantity < items[keys[i]])
                {
                    TempData["OutOfStock"] += string.Format("{0} is out of the stock,  \n", keys[i].Name);
                    check = false;
                }
            }
            if (!check)
            {
                return(RedirectToAction("Index"));
            }
            else
            {
                double?amountOfMoney = 0;
                for (int i = 0; i < keys.Length; i++)
                {
                    amountOfMoney += keys[i].Price * items[keys[i]];
                }
                string      email       = ((AccountLogin)Session[Constants.USER_SESSION]).Email;
                string      payment     = "cash";
                Transaction transaction = new Transaction();
                transaction.Email           = email;
                transaction.AmountOfMoney   = amountOfMoney;
                transaction.Payment         = payment;
                transaction.TransactionTime = DateTime.Now;
                TransactionDAO transactionDAO = new TransactionDAO();
                int            transactionId  = transactionDAO.AddTransaction(transaction);

                MobileDAO MobileDAO = new MobileDAO();
                for (int i = 0; i < keys.Length; i++)
                {
                    int MobileId       = keys[i].MobileId;
                    int quantityInDB   = MobileDao.GetQuantity(MobileId);
                    int quantityInCart = items[keys[i]];
                    int remain         = quantityInDB - quantityInCart;
                    MobileDAO.SetQuantity(MobileId, remain);
                }
                OrderDAO orderDAO = new OrderDAO();
                for (int i = 0; i < keys.Length; i++)
                {
                    Order order = new Order();
                    order.Quantity      = items[keys[i]];
                    order.MobileId      = keys[i].MobileId;
                    order.MobileName    = keys[i].Name;
                    order.OrderTime     = DateTime.Now;
                    order.TransactionId = transactionId;
                    order.Email         = email;
                    orderDAO.SaveOrder(order);
                }
                Session.Remove(Constants.CART);
            }
            return(RedirectToAction("Index"));
        }