/// <summary>
        /// Approves/Reviews/Disburses an expense.
        /// </summary>
        /// <param name="expense">An Expense object.</param>
        /// <param name="review">An ExpenseReview object.</param>
        public void ApproveExpense(Expense expense, ExpenseReview review)
        {
            // Create message contract.
               ExpenseWorkflowService.ApproverRequestMessage requestMessage =
                new ExpenseWorkflowService.ApproverRequestMessage(expense.WorkflowID, expense, review);

            ExpenseWorkflowServiceClient proxy = new ExpenseWorkflowServiceClient();
            try
            {
                // For Manager.
                if (expense.Status == ExpenseStatus.Pending)
                {
                    proxy.ReviewExpense(requestMessage);
                }

                // For Head of Department/HR Manager.
                else if (expense.Status == ExpenseStatus.Reviewed ||
                    expense.Status == ExpenseStatus.Escalated)
                {
                    proxy.ApproveExpense(requestMessage);
                }

                // For Financial Controller.
                else if (expense.Status == ExpenseStatus.Approved)
                {
                    proxy.DisburseExpense(requestMessage);
                }

                proxy.Close();
            }
            catch (FaultException<ProcessExecutionFault> ex)
            {
                throw new ApplicationException(ex.Message);
            }
        }
 /// <summary>
 /// Inserts an expense row.
 /// </summary>
 /// <param name="expense">An Expense object.</param>
 public void Create(ExpenseReview review)
 {
     using (ExpenseDataContext ctx = new ExpenseDataContext())
     {
         ctx.AddObject(ENTITY_SET_NAME, review);
         ctx.SaveChanges();
     }
 }
        /// <summary>
        /// Approves an Expense request.
        /// </summary>
        /// <param name="expense">An Expense object.</param>
        /// <param name="review">An ExpenseReview object.</param>
        public Expense Approve(Expense expense, ExpenseReview review)
        {
            Console.WriteLine("Approving...");

            expense.Status = ExpenseStatus.Approved;
            expense.IsCompleted = false;

            return UpdateExpenseRecord(expense, review);
        }
        private void ApproveExpense(bool approved)
        {
            try
            {
                foreach (DataGridViewRow row in expenseGrid.SelectedRows)
                {
                    Expense expense = (Expense)row.DataBoundItem;

                    // Create a new review object.
                    // NOTE: For simplicity, we just use Role as ApproverID.
                    ExpenseReview review = new ExpenseReview();
                    review.ExpenseID = expense.ExpenseID;
                    review.Reviewer = roleBox.Text;
                    review.IsApproved = approved;
                    review.Remarks = remarksBox.Text;

                    _upc.ApproveExpense(expense, review);

                }

            }
            catch (FaultException faultEx)
            {
                MessageBox.Show(faultEx.Message, "Error Processing Expense", MessageBoxButtons.OK,
                         MessageBoxIcon.Error);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error Processing Expense", MessageBoxButtons.OK,
                        MessageBoxIcon.Error); ;
            }

            string term = string.Compare(roleBox.Text, "Financial Controller", true) == 0 ?
                            "disbursed" : "approved";

            statusLabel.Text = string.Format(
                (expenseGrid.SelectedRows.Count <= 1 ? "Expense {0}." : "Expenses {0}."),
                approved ? term : "rejected");

            LoadExpenses();
        }
        /// <summary>
        /// Disburses an Approved Expense request.
        /// </summary>
        /// <param name="expense">An Expense object.</param>
        /// <param name="review">An ExpenseReview object.</param>
        public Expense Disburse(Expense expense, ExpenseReview review)
        {
            Console.WriteLine("Disbursing...");

            expense.Status = ExpenseStatus.Disbursed;
            expense.AssignedTo = string.Empty;
            expense.IsCompleted = true;

               return UpdateExpenseRecord(expense, review);
        }
        private Expense UpdateExpenseRecord(Expense expense, ExpenseReview review)
        {
            using (TransactionScope ts =
                new TransactionScope(TransactionScopeOption.Required))
            {
                try
                {
                    expense = UpdateExpense(expense);
                    CreateExpenseReview(review);
                    LogStatus(expense);
                    ts.Complete();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw ex;
                }
            }

            return expense;
        }
        /// <summary>
        /// Creates a new Expense Review record in the database.
        /// </summary>
        /// <param name="expense">An Expense Review object.</param>
        private void CreateExpenseReview(ExpenseReview review)
        {
            // Business logic.
            review.DateApproved = DateTime.Now;

            Console.WriteLine(review.ToString());

            // Persist data.
            ExpenseReviewDataAccess dac = new ExpenseReviewDataAccess();
            try
            {
                dac.Create(review);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw ex;
            }
        }
        private void ApproveExpense(bool approved)
        {
            int i = 0;

            try
            {
                foreach (GridViewRow row in expenseGrid.Rows)
                {
                    if (((CheckBox)row.FindControl("selectBox")).Checked)
                    {
                        // NOTE: I don't really like this part because it is reconstructing
                        //       the entity. Any ideas or advice would be appreciated.
                        Expense expense = new Expense();

                        DataKey key = expenseGrid.DataKeys[row.RowIndex];
                        expense.ExpenseID = (long)key.Values[0];
                        expense.WorkflowID = (Guid)key.Values[1];

                        expense.Employee = row.Cells[2].Text;
                        expense.Category = (ExpenseCategory)
                            Enum.Parse(typeof(ExpenseCategory), row.Cells[3].Text);
                        expense.Description = row.Cells[4].Text;
                        expense.Amount = Convert.ToDouble(row.Cells[5].Text);
                        expense.ExpenseDate = Convert.ToDateTime(row.Cells[6].Text);
                        expense.Status = (ExpenseStatus)
                            Enum.Parse(typeof(ExpenseStatus), row.Cells[7].Text);
                        expense.DateSubmitted = Convert.ToDateTime(row.Cells[8].Text);
                        expense.DateModified = Convert.ToDateTime(row.Cells[9].Text);
                        // End of Expense entity reconstruction.

                        ExpenseReview review = new ExpenseReview();
                        review.ExpenseID = expense.ExpenseID;
                        review.Reviewer = roleBox.SelectedValue;
                        review.IsApproved = approved;
                        review.Remarks = remarksBox.Text;

                        _upc.ApproveExpense(expense, review);

                    }
                    i++;
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }

            LoadExpenses();

            string term = string.Compare(roleBox.Text, "Financial Controller", true) == 0 ?
                    "disbursed. " : "approved. ";

            if (i > 0)
            {
                statusLabel.Text = (i > 1 ? "Expenses " : "Expense ") +
                    (approved ? term : " rejected.") +
                    (expenseGrid.Rows.Count > 0 ? string.Empty :
                        "There are no more pending expenses available for review or approval.");
            }
        }