/// <summary> /// Load a DataTable object with a row from the tDiscountType table /// </summary> /// <param name="discountTypeID"></param> /// <returns></returns> private tDiscountType.fGetDiscountTypeDataTable LoadDiscountType(int discountTypeID) { tDiscountType.fGetDiscountTypeDataTable discountTypeDataTable = new tDiscountType.fGetDiscountTypeDataTable(); tDiscountTypeTableAdapters.fGetDiscountTypeTableAdapter fGetDiscountTypeTableAdapter = new tDiscountTypeTableAdapters.fGetDiscountTypeTableAdapter(); // Don't use the connection configured in the DataSet designer. Use the one we like. fGetDiscountTypeTableAdapter.Connection = Config.myConnection; fGetDiscountTypeTableAdapter.Fill(discountTypeDataTable, discountTypeID); return(discountTypeDataTable); }
/// <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) { tDiscountType discountType = new tDiscountType(); tCouponDetail.fGetCouponDetailDataTable couponDetailDataTable = LoadCouponDetail(tp.couponDetailID); int discountTypeID = couponDetailDataTable.Rows[0].Field <int>("DiscountTypeID"); 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); } }