/// <summary>
        ///     Processes the parameters.
        /// </summary>
        public override void processParentIntention()
        {
            this.transaction = (ParentParameter as PurchaseTransactionUC)?.SelectedTransaction;
            this.loadItems();


        }
        /// <summary>
        /// Gets all items by purchase transaction.
        /// </summary>
        /// <param name="transaction">The transaction.</param>
        /// <returns>
        /// The purchase transaction's items.
        /// </returns>
        public IList<PurchaseTransaction_Item> GetAllItemsByPurchaseTransaction(PurchaseTransaction transaction)
        {
            if (transaction == null)
            {
                throw new ArgumentNullException();
            }

            return this.itemRepository.GetAllItemsByPurchaseTransaction(transaction);
        }
        /// <summary>
        /// Adds the purchase transaction.
        /// </summary>
        /// <param name="transaction">The transaction.</param>
        public void AddPurchaseTransaction(PurchaseTransaction transaction)
        {
            
            var id = this.purchaseRepository.AddOne(transaction);
            transaction.Id = id;

            try
            {
                foreach (var item in transaction.Items)
                {
                    item.PurchaseTransactionId = id;
                }

                this.purchaseItemRepository.AddList(transaction.Items);
            }
            catch (Exception)
            {
                this.deleteChangesFromDatabase(transaction.Items, id);

                throw;
            }
        }
        /// <summary>
        /// Adds one item to the database.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        public string AddOne(PurchaseTransaction item)
        {
            if (item == null)
            {
                throw new ArgumentNullException();
            }

            var id = string.Empty;

            const string statement = "INSERT INTO PurchaseTransaction (transactionTime, Customer_id, Employee_id)" +
                                     " VALUES (@TransactionTime, @CustomerId, @EmployeeId)";

            var connection = new MySqlConnection(this.CONNECTION_STRING);

            using (var command = new MySqlCommand(statement))
            {
                command.Parameters.AddWithValue("@TransactionTime", item.TransactionTime);
                command.Parameters.AddWithValue("@CustomerId", item.CustomerId);
                command.Parameters.AddWithValue("@EmployeeId", item.EmployeeId);

                command.Connection = connection;

                try
                {
                    command.Connection.Open();
                    command.ExecuteNonQuery();
                    id = command.LastInsertedId.ToString();
                }
                finally
                {
                    command.Connection.Close();
                }
            }

            return id;
        }
        /// <summary>
        /// Gets the item from the database by identifier.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public PurchaseTransaction GetById(string id)
        {
            const string query = "SELECT * " + 
                                 "FROM PurchaseTransaction " +
                                 "WHERE id = @id";

            var connection = new MySqlConnection(this.CONNECTION_STRING);

            var transaction = new PurchaseTransaction();

            using (var command = new MySqlCommand(query))
            {
                command.Connection = connection;

                command.Parameters.AddWithValue("@id", id);

                try
                {
                    command.Connection.Open();

                    var reader = command.ExecuteReader();

                    while (reader.Read())
                    {
                        
                        transaction.Id = ((int)reader["id"]).ToString();
                        transaction.TransactionTime = reader["transactionTime"] == DBNull.Value
                            ? DateTime.MinValue
                            : reader["transactionTime"] as DateTime? ?? DateTime.MinValue;
                        transaction.CustomerId = reader["Customer_id"] == DBNull.Value ? "0" : ((int) reader["Customer_id"]).ToString();
                        transaction.EmployeeId = reader["Employee_id"] == DBNull.Value ? "0" : ((int)reader["Employee_id"]).ToString();

                    }
                }
                finally
                {
                    command.Connection.Close();
                }
            }

            return transaction;
        }
        /// <summary>
        /// Gets the transactions by customer identifier.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <returns></returns>
        public IList<PurchaseTransaction> GetTransactionsByCustomerId(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentNullException(nameof(id));
            }


            var purchaseTransactions = new List<PurchaseTransaction>();

            const string query = "SELECT * FROM PurchaseTransaction WHERE Customer_id = @id";

            var connection = new MySqlConnection(this.CONNECTION_STRING);

            using (var command = new MySqlCommand(query))
            {
                command.Connection = connection;
                command.Parameters.AddWithValue("@id", id);

                try
                {
                    command.Connection.Open();

                    var reader = command.ExecuteReader();

                    while (reader.Read())
                    {

                        var theTransaction = new PurchaseTransaction
                        {
                            Id = reader["id"].ToString(),
                            TransactionTime = reader["transactionTime"] as DateTime? ?? DateTime.MinValue,
                            CustomerId = reader["Customer_id"] == DBNull.Value ? "NULL" : reader["Customer_id"].ToString(),
                            EmployeeId = reader["Employee_id"] as string ?? string.Empty,
                        };


                        purchaseTransactions.Add(theTransaction);
                    }
                }
                finally
                {
                    command.Connection.Close();
                }
            }

            return purchaseTransactions;
        }
 /// <summary>
 /// Updates the item by identifier.
 /// </summary>
 /// <param name="item">The item.</param>
 public void UpdateById(PurchaseTransaction item)
 {
     throw new NotImplementedException();
 }
 /// <summary>
 /// Deletes the specified item from the database.
 /// </summary>
 /// <param name="item">The item.</param>
 /// <exception cref="NotImplementedException"></exception>
 public void Delete(PurchaseTransaction item)
 {
     throw new NotImplementedException();
 }
        /// <summary>
        /// Gets all items by purchase transaction.
        /// </summary>
        /// <param name="transaction">The transaction.</param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public IList<PurchaseTransaction_Item> GetAllItemsByPurchaseTransaction(PurchaseTransaction transaction)
        {
            if (transaction == null)
            {
                throw new ArgumentNullException();
            }

            var items = new List<PurchaseTransaction_Item>();

            var query = "SELECT PurchaseTransaction_Item.*, Furniture.name AS FurnitureName, ReturnTransaction_Item.ReturnTransaction_id, SUM(ReturnTransaction_Item.quantity) AS ReturnedQuantity " +
                        "FROM PurchaseTransaction_Item " +
                        "INNER JOIN Furniture " +
                        "ON PurchaseTransaction_Item.Furniture_id=Furniture.id " +
                        "LEFT JOIN ReturnTransaction_Item " +
                        "ON PurchaseTransaction_Item.PurchaseTransaction_id=ReturnTransaction_Item.PurchaseTransaction_Item_PurchaseTransaction_id AND PurchaseTransaction_Item.Furniture_id = ReturnTransaction_Item.PurchaseTransaction_Item_Furniture_id " +
                        "WHERE PurchaseTransaction_id=@Id " +
                        "GROUP BY PurchaseTransaction_Item.Furniture_id, PurchaseTransaction_Item.PurchaseTransaction_id";

            var connection = new MySqlConnection(this.CONNECTION_STRING);

            using (var command = new MySqlCommand(query))
            {
                command.Parameters.AddWithValue("@Id", transaction.Id);

                command.Connection = connection;

                try
                {
                    command.Connection.Open();

                    var reader = command.ExecuteReader();

                    while (reader.Read())
                    {
                        var item = new PurchaseTransaction_Item
                        {
                            PurchaseTransactionId = transaction.Id,
                            FurnitureId =
                                reader["Furniture_id"] == DBNull.Value ? "NULL" : reader["Furniture_id"].ToString(),
                            Quantity = reader["quantity"] == DBNull.Value ? 0 : (int) reader["quantity"],
                            LeaseTime = reader["leaseTime"] == DBNull.Value ? 0 : (int) reader["leaseTime"],
                            FurnitureName = reader["FurnitureName"] == DBNull.Value ? "NULL" : reader["FurnitureName"].ToString()
                        };

                        var returnedQuantity = reader["ReturnedQuantity"] == DBNull.Value ? 0: Int32.Parse(reader["ReturnedQuantity"].ToString());


                        if (reader["ReturnTransaction_id"] != DBNull.Value)
                        {
                            item.ReturnableQuantity = item.Quantity - returnedQuantity;
                        }
                        else
                        {
                            item.ReturnableQuantity = item.Quantity;
                        }

                        items.Add(item);
                    }

                }
                finally
                {
                    command.Connection.Close();
                }
            }

            return items;
        }
Example #10
0
        private void submitTransactionButton_Click(object sender, EventArgs e)
        {
            if (this.itemsToPurchase.Count == 0)
            {
                ErrorHandler.DisplayErrorBox("Error", "Cannot submit an empty transaction.");
                return;
            }

            if (this.customerID == "null")
            {
                ErrorHandler.DisplayErrorBox("Error", "Must select a customer.");
                return;
            }

            try
            {
                var theController = new TransactionController();
                var furnitureController = new FurnitureController();

                var transaction = new PurchaseTransaction
                {
                    TransactionTime = DateTime.Now,
                    CustomerId = this.customerID,
                    EmployeeId = this.session.Id.ToString(),
                    Items = new List<PurchaseTransaction_Item>(this.itemsToPurchase)
                };


                theController.AddPurchaseTransaction(transaction);

                furnitureController.UpdateQuantitiesByIds(transaction.Items);
                MessageBox.Show(this.itemsToPurchase.Count + @" item(s) were purchased for a total of " + this.totalPriceLabel.Text + ".", @"Transaction Successful",
                    MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
                this.clearTransaction();
            }
            catch (NullReferenceException nullReference)
            {
                ErrorHandler.DisplayErrorMessageToUserAndLog("Session Error", "The login session is null.", nullReference);
            }
            catch (InvalidCastException invalidCast)
            {
                ErrorHandler.DisplayErrorMessageToUserAndLog("Session Error", "The tag cannot be cast as a login session.", invalidCast);
            }
            catch (MySqlException sqlException)
            {
                ErrorHandler.DisplayErrorMessageToUserAndLog("SQL Error",
                    "The transaction could not be added to the database.", sqlException);
            }
            catch (ArgumentOutOfRangeException rangeException)
            {
                ErrorHandler.DisplayErrorMessageToUserAndLog("Quantity Error",
                    rangeException.Message, rangeException);
            }
        }