/// <summary>
 /// Load a DataTable object with a row from the tCouponDetail table
 /// </summary>
 /// <param name="couponDetailID"></param>
 /// <returns></returns>
 private Simulator.tCouponDetail.fGetCouponDetailDataTable LoadCouponDetail(int couponDetailID)
 {
     Simulator.tCouponDetail.fGetCouponDetailDataTable couponDetailDataTable = new Simulator.tCouponDetail.fGetCouponDetailDataTable();
     try {
         Simulator.tCouponDetailTableAdapters.fGetCouponDetailTableAdapter fGetCouponDetailTableAdapter = new Simulator.tCouponDetailTableAdapters.fGetCouponDetailTableAdapter();
         // Don't use the connection configured in the DataSet designer. Use the one we like.
         fGetCouponDetailTableAdapter.Connection = Config.myConnection;
         fGetCouponDetailTableAdapter.Fill(couponDetailDataTable, couponDetailID);
     } catch (Exception ex) {
         // Something went wrong.
         txtResults.AppendText(Environment.NewLine + "AddTransactions.LoadCouponDetail: " + ex.Message);
     }
     return(couponDetailDataTable);
 }
 /// <summary>
 /// Process the coupon, if any, relative to the transaction taking place
 /// </summary>
 /// <param name="tp">Transaction Paramaters for the current transaction</param>
 private void ProcessTransactionDetailAndCoupon(TransactionParameters tp)
 {
     try {
         if (tp.couponDetailID > 0)
         {
             Simulator.tDiscountType discountType = new Simulator.tDiscountType();
             Simulator.tCouponDetail.fGetCouponDetailDataTable couponDetailDataTable = LoadCouponDetail(tp.couponDetailID);
             int discountTypeID = couponDetailDataTable.Rows[0].Field <int>("DiscountTypeID");
             Simulator.tDiscountType.fGetDiscountTypeDataTable discountTypeDataTable = LoadDiscountType(discountTypeID);
             // Check the quantity being purhased in the transaction to make sure it's not too many or too few to use the coupon.
             if (tp.qty < couponDetailDataTable.Rows[0].Field <int>("MinQtyToPurchase"))
             {
                 tp.qty = couponDetailDataTable.Rows[0].Field <int>("MinQtyToPurchase");
             }
             if (tp.qty > couponDetailDataTable.Rows[0].Field <int>("MaxQtyToPurchase"))
             {
                 tp.qty = couponDetailDataTable.Rows[0].Field <int>("MaxQtyToPurchase");
             }
             if (discountTypeDataTable.Rows[0].Field <bool>("isFree") == true)
             {
                 // It's a free coupon! Make the cost in the transaction detail zero
                 tp.pricePerSellableUnitToTheCustomer = 0;
             }
             else
             {
                 if (discountTypeDataTable.Rows[0].Field <bool>("isPercentageDiscount") == true)
                 {
                     // It's a percentage off coupon! Reduce the cost in the transaction detail
                     double percentageDiscount = ((double)(100 - couponDetailDataTable.Rows[0].Field <int>("PercentageDiscount"))) / 100;
                     tp.pricePerSellableUnitToTheCustomer *= percentageDiscount;
                 }
                 else
                 {
                     if (discountTypeDataTable.Rows[0].Field <bool>("isBOGO") == true)
                     {
                         // It's buy one / get one free.
                         tp.pricePerSellableUnitToTheCustomer /= 2;      // Reduce the purchase price of each unit by 1/2 to acheive BOGO
                         // round it up because we don't sell fractions of a cent
                         tp.pricePerSellableUnitToTheCustomer = Math.Round(tp.pricePerSellableUnitToTheCustomer, 2);
                         tp.qty *= 2;        // They will get twice as many as they purchased. Woo Hoo.
                     }
                     else
                     {
                         if (discountTypeDataTable.Rows[0].Field <bool>("isAmountOff") == true)
                         {
                             // It's a specific amount off the purchase price.
                             double amountOff = Double.Parse(couponDetailDataTable.Rows[0].Field <decimal>("AmountOff").ToString());
                             tp.pricePerSellableUnitToTheCustomer -= amountOff;      // Reduce the purchase price of each unit by the amount on the coupon
                         }
                         else
                         {
                             if (discountTypeDataTable.Rows[0].Field <bool>("isGiftCard") == true)
                             {
                                 // It's a gift card.
                                 double amountOff = Double.Parse(couponDetailDataTable.Rows[0].Field <decimal>("AmountOff").ToString());
                                 tp.pricePerSellableUnitToTheCustomer -= amountOff / tp.qty;      // Reduce the purchase price of each unit
                                 // round it up because we don't sell fractions of a cent
                                 tp.pricePerSellableUnitToTheCustomer = Math.Round(tp.pricePerSellableUnitToTheCustomer, 2);
                             }
                         }
                     }
                 }
             }
         }
     } catch (Exception ex) {
         // Something went wrong.
         Write(Environment.NewLine + "AddTransactions.ProcessTransactionDetailAndCoupon: " + ex.Message);
     }
 }