Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

View file
nameSalesforce Voucher Fields.zip

Pre-requisites

  1. Create a custom voucher field in Salesforce on either the Reservation Contact or Reservation object, using this article Add custom fields to the widget as a guideline.

  2. Add the fields and object attached to this page to the Salesforce environment.

  3. Follow the instructions for implementing custom pricing, laid out in this article Apply custom price calculations

Example Implementation

Code Block
global class GoMeddoCustomPricingCalculations implements B25.Util_PluginManager.ReservationPrice {

    public void calculate(B25.Util_PluginManager.ReservationPriceData data) {
		B25.Util_PluginManager.applyDefaultPriceCalculation(data);

		if (data.relatedRecords == null && data.reservation.Contact_Voucher_Code__c == null) {
			return;
		}

		String voucherCode;
		if (data.relatedRecords != null && data.relatedRecords.get('B25__ReservationContacts__r') != null) {
			// reservation contacts configured
			if (!data.relatedRecords.get('B25__ReservationContacts__r').isEmpty()) {
				B25__ReservationContact__c reservationContact = (B25__ReservationContact__c) data.relatedRecords.get('B25__ReservationContacts__r')[0];
				voucherCode = reservationContact.Voucher_Code__c;
			}
		} else if(data.reservation.Contact_Voucher_Code__c != null) {
			// regular contacts configured
			voucherCode = data.reservation.Contact_Voucher_Code__c;
		}

		if (voucherCode == null) {
			return;
		}

		List<Configured_Voucher__c> matchingVouchers = [
			SELECT
				Id,
				Name,
				Discount_Amount__c,
				Discount_Percent__c,
				Is_Leading_Up_Voucher__c,
				Leading_Up_Voucher_Validity__c,
				Valid_From__c,
				Valid_Until__c
			FROM
				Configured_Voucher__c
			WHERE
				Name = :voucherCode
			AND Is_Active__c = true
			AND (Discount_Amount__c != NULL OR Discount_Percent__c != NULL)
			AND (Valid_From__c <= TODAY AND Valid_Until__c > TODAY)
			WITH SECURITY_ENFORCED
		];

		if (matchingVouchers.isEmpty()) {
			return;
		}

		Configured_Voucher__c matchingVoucher = matchingVouchers[0];
		if (matchingVoucher.Is_Leading_Up_Voucher__c && Date.today().daysBetween(data.reservation.B25__Start__c.date()) > matchingVoucher.Leading_Up_Voucher_Validity__c) {
			return;
		}

		performSubtotalUpdate(data, matchingVoucher);
	}

	private static void performSubtotalUpdate(B25.Util_PluginManager.ReservationPriceData data, Configured_Voucher__c matchingVoucher) {
		if (matchingVoucher.Discount_Amount__c != null) {
			Decimal updatedSubtotal = data.reservation.B25__Subtotal__c - matchingVoucher.Discount_Amount__c;
			data.reservation.B25__Subtotal__c = updatedSubtotal < 0 ? 0 : updatedSubtotal;
		} else {
			Decimal amountToSubtract = data.reservation.B25__Subtotal__c - (data.reservation.B25__Subtotal__c * (matchingVoucher.Discount_Percent__c/100));
			data.reservation.B25__Subtotal__c = amountToSubtract;
		}

		data.reservation.Voucher_Code__c = matchingVoucher.Name;
	}
}

...