/// <summary> /// Modify the regular collection day of a weekly contract in EazyCustomerManager /// </summary> /// /// <remarks> /// It is important to note that if the contract is already within the cut-off date for the next collection, the regular collection day will not be amended until the following week. /// </remarks> /// /// /// <param name="Contract">The GUID of an existing contract within EazyCustomerManager</param> /// <param name="NewPaymentDay">The new regular collection day of the existing contract</param> /// <param name="Comment">A comment to describe the amendment made to the contract</param> /// <param name="AmmendNextPayment">Whether or not the next payment should be amended to account for the change in contract day</param> /// <param name="NextPaymentAmount">If the next payment amount is to be amended, dictate what the next payment amount should be</param> /// <returns></returns> public string ContractDayWeekly(string Contract, string NewPaymentDay, string Comment, bool AmmendNextPayment, string NextPaymentAmount = "") { if (NewPaymentDay == "" || Comment == "") { throw new Exceptions.EmptyRequiredParameterException( "One or more required parameters are empty. Please double check all required parameters are filled and re-submit." ); } ContractChecks = new Utilities.ContractPostChecks(); // Perform several basic checks to ensure the information provided for the customer is fit for use ContractChecks.CheckPaymentDayInWeekIsValid(NewPaymentDay); // Create a new dictionary of parameters Parameters = new Dictionary <string, string>(); // Add method arguments to the parameters only if they are not empty try { Parameters.Add("day", NewPaymentDay); Parameters.Add("comment", Comment); Parameters.Add("patchNextPayment", AmmendNextPayment.ToString()); } catch (ArgumentException) { throw new Exceptions.InvalidParameterException("There was an error adding one or more parameters to the call. Please try again, or contact [email protected]"); } if (AmmendNextPayment) { Parameters.Add("nextPaymentPatchAmount", NextPaymentAmount); } var CreateRequest = Handler.Session(Settings); var SendRequest = CreateRequest.Patch(string.Format("contract/{0}/weekly", Contract), Parameters); // Pass the return string to the handler. This will throw an exception if it is not what we expect Handler.GenericExceptionCheck(SendRequest); if (SendRequest.Contains("Contract updated")) { return(string.Format("The contract {0} has been updated with the new regular collection day of {1}", Contract, NewPaymentDay)); } else if (SendRequest.Contains("Contract not found")) { throw new Exceptions.ResourceNotFoundException(string.Format("The contract {0} could not be found within EazyCustomerManager", Contract)); } else { return(SendRequest); } }
/// <summary> /// Create a new contract in EazyCustomerManager /// </summary> /// /// <param name="Customer">The GUID of the customer the new contract will belong to</param> /// <param name="ScheduleName">The schedule name the new contract will belong to</param> /// <param name="StartDate">The desired start date of the new contract. This must be X working days in the future, where X is the number of working days agreed with EazyCollect.</param> /// <param name="GiftAid">Whether the new contract will be eligible for GiftAid or not</param> /// <param name="TerminationType">The method of termination for the new contract</param> /// <param name="AtTheEnd">What happens to the new contract after the termination event have been triggered</param> /// /// <OptionalParams> /// <param name="NumberofDebits">The number of debits to be taken, this is mandatory if TerminationType has been set to "Collect certain number of debits"</param> /// <param name="Frequency">Mandatory if the new contract is not ad-hoc. This parameter allows you to skip periods (Ex. a value of 2 would collect every second month)</param> /// <param name="InitialAmount">Used if the first collection differs from the regular collection amount. Not to be used with ad-hoc contracts</param> /// <param name="ExtraInitialAmount">Used if there are any additional charges (Ex. Gym joining fee)</param> /// <param name="PaymentAmount">Mandatory if the contract is not ad-hoc. The regular collection amount of the new contract</param> /// <param name="FinalAmount">Used if the final collection amount differs from the regular collection amount. Not to be used with ad-hoc contracts</param> /// <param name="PaymentMonthInYear">Mandatory for annual contracts. The collection month for annual payments (1-12)</param> /// <param name="PaymentDayInMonth">Mandatory for annual and monthly contracts. The collection day of the month (1-28 || Last day of month)</param> /// <param name="PaymentDayInWeek">Mandatory for weekly contracts. The collection day of the week for weekly schedules (Monday .. Friday)</param> /// <param name="TerminationDate">Mandatory if termination type is set to "End on exact date". The termination date of a contract</param> /// <param name="AdditionalReference">An additional, search-able reference for the newly created contract</param> /// <param name="CustomDDReference">A custom DD reference for the new contract. This option is only available upon request to Eazy Collect </param> /// </OptionalParams> /// /// <example> /// Contract("ab09362d-f88e-4ee8-be85-e27e1a6ce06a", "test_schedule", "2019-06-20", "False", "Until further notice", "Switch to further notice") /// </example> /// /// <returns> /// Contract JSON formatted as string /// </returns> public string Contract(string Customer, string ScheduleName, string StartDate, bool GiftAid, string TerminationType, string AtTheEnd, string NumberofDebits = "", string Frequency = "", string InitialAmount = "", string ExtraInitialAmount = "", string PaymentAmount = "", string FinalAmount = "", string PaymentMonthInYear = "", string PaymentDayInMonth = "", string PaymentDayInWeek = "", string TerminationDate = "", string AdditionalReference = "", string CustomDDReference = "") { if (Customer == "" || ScheduleName == "" || StartDate == "" || TerminationType == "" || AtTheEnd == "") { throw new Exceptions.EmptyRequiredParameterException( "One or more required parameters are empty. Please double check all required parameters are filled and re-submit." ); } ContractChecks = new Utilities.ContractPostChecks(); // Perform several basic checks to ensure the information provided for the customer is fit for use ContractChecks.CheckScheduleNameIsAvailable(ScheduleName, Settings); int TerminationTypeInt = ContractChecks.CheckTerminationTypeIsValid(TerminationType); int AtTheEndInt = ContractChecks.CheckAtTheEndIsValid(AtTheEnd); string StartDateDateString = ContractChecks.CheckStartDateIsValid(StartDate, Settings); bool AdHocBool = ContractChecks.CheckScheduleAdHocStatus(ScheduleName, Settings); if (AdHocBool) { if (bool.Parse(Settings.GetSection("contracts")["AutoFixTerminationTypeAdHoc"])) { if (TerminationTypeInt != 1) { TerminationType = "Until further notice"; } else { } } else { if (TerminationTypeInt != 1) { throw new Exceptions.InvalidParameterException(string.Format("Termination type must be set to 'Until further notice' on ad-hoc contracts")); } } if (bool.Parse(Settings.GetSection("contracts")["AutoFixAtTheEndAdHoc"])) { if (AtTheEndInt != 1) { AtTheEnd = "Switch to further notice"; } else { } } else { if (AtTheEndInt != 1) { throw new Exceptions.InvalidParameterException(string.Format("AtTheEnd must be set to 'Switch to further notice' on ad-hoc contracts")); } } if (InitialAmount != "") { throw new Exceptions.ParameterNotAllowedException(string.Format("InitialAmount is not allowed on ad-hoc contracts")); } if (ExtraInitialAmount != "") { throw new Exceptions.ParameterNotAllowedException(string.Format("ExtraInitialAmount is not allowed on ad-hoc contracts")); } if (FinalAmount != "") { throw new Exceptions.ParameterNotAllowedException(string.Format("FinalAmount is not allowed on ad-hoc contracts")); } } else { if (Frequency == "") { throw new Exceptions.EmptyRequiredParameterException(string.Format("Frequency is mandatory on non-ad-hoc contracts")); } if (PaymentAmount == "") { throw new Exceptions.EmptyRequiredParameterException(string.Format("Payment amount is mandatory on non-ad-hoc contracts")); } int FrequencyType = ContractChecks.CheckFrequency(ScheduleName, Settings); if (FrequencyType == 0) { if (PaymentDayInWeek == "") { throw new Exceptions.EmptyRequiredParameterException(string.Format("Payment day in week is mandatory on weekly contracts")); } else { ContractChecks.CheckPaymentDayInWeekIsValid(PaymentDayInWeek); } } else if (FrequencyType == 1) { if (PaymentDayInMonth == "") { throw new Exceptions.EmptyRequiredParameterException(string.Format("Payment day in month is mandatory on monthly contracts")); } else { if (StartDateDateString.Substring(8, 2) != PaymentDayInMonth.PadLeft(2, '0')) { if (bool.Parse(Settings.GetSection("contracts")["AutoFixPaymentDayInMonth"])) { PaymentDayInMonth = StartDateDateString.Substring(8, 2); } else { PaymentDayInMonth = StartDateDateString.Substring(8, 2); throw new Exceptions.InvalidParameterException(string.Format("PaymentDayInMonth must be set to {0} if the start date is {1}.", PaymentDayInMonth, StartDateDateString)); } } ContractChecks.CheckPaymentDayInMonthIsValid(PaymentDayInMonth); } } else if (FrequencyType == 2) { if (PaymentDayInMonth == "") { throw new Exceptions.EmptyRequiredParameterException(string.Format("Payment day in month is mandatory on annual contracts")); } else if (PaymentMonthInYear == "") { throw new Exceptions.EmptyRequiredParameterException(string.Format("Payment month in year is mandatory on annual contracts")); } else { if (StartDateDateString.Substring(5, 2) != PaymentMonthInYear.PadLeft(2, '0')) { if (bool.Parse(Settings.GetSection("contracts")["AutoFixPaymentMonthInYear"])) { PaymentMonthInYear = StartDateDateString.Substring(5, 2); } else { throw new Exceptions.InvalidParameterException(string.Format("PaymentMonthInYear must be set to {0} if the start date is {1}.", StartDateDateString.Substring(5, 2), StartDateDateString)); } } if (StartDateDateString.Substring(8, 2) != PaymentDayInMonth.PadLeft(2, '0')) { if (bool.Parse(Settings.GetSection("contracts")["AutoFixPaymentDayInMonth"])) { PaymentDayInMonth = StartDateDateString.Substring(8, 2); } else { throw new Exceptions.InvalidParameterException(string.Format("PaymentDayInMonth must be set to {0} if the start date is {1}.", StartDateDateString.Substring(8, 2), StartDateDateString)); } } ContractChecks.CheckPaymentDayInMonthIsValid(PaymentDayInMonth); ContractChecks.CheckPaymentMonthInYearIsValid(PaymentMonthInYear); } } if (TerminationTypeInt == 0) { if (NumberofDebits == "") { throw new Exceptions.EmptyRequiredParameterException(string.Format("NumberOfDebits is mandatory if TerminationType is set to 'Take certain number of debits'.")); } else { ContractChecks.CheckNumberOfDebitsIsValid(NumberofDebits); } } else if (TerminationTypeInt == 1) { if (AtTheEndInt != 1) { throw new Exceptions.InvalidParameterException(string.Format("AtTheEnd must be set to 'Switch to further notice' if TerminationType is set to 'Until further notice'.")); } } else if (TerminationTypeInt == 2) { if (TerminationDate == "") { throw new Exceptions.EmptyRequiredParameterException(string.Format("TerminationDate is mandatory if TerminationType set to 'End on exact date'.")); } else { ContractChecks.CheckTerminationDateIsAfterStartDate(TerminationDate, StartDate); } } } // Create a new dictionary of parameters Parameters = new Dictionary <string, string>(); // Add method arguments to the parameters only if they are not empty try { Parameters.Add("scheduleName", ScheduleName); Parameters.Add("start", StartDateDateString); Parameters.Add("isGiftAid", GiftAid.ToString()); Parameters.Add("terminationType", TerminationType); Parameters.Add("atTheEnd", AtTheEnd); } catch (ArgumentException) { throw new Exceptions.InvalidParameterException("There was an error adding one or more parameters to the call. Please try again, or contact [email protected]"); } if (NumberofDebits != "") { Parameters.Add("numberOfDebits", NumberofDebits); } if (Frequency != "") { Parameters.Add("every", Frequency); } if (InitialAmount != "") { Parameters.Add("initialAmount", InitialAmount); } if (ExtraInitialAmount != "") { Parameters.Add("extraInitialAmount", ExtraInitialAmount); } if (PaymentAmount != "") { Parameters.Add("amount", PaymentAmount); } if (FinalAmount != "") { Parameters.Add("finalAmount", FinalAmount); } if (PaymentMonthInYear != "") { Parameters.Add("paymentMonthInYear", PaymentMonthInYear); } if (PaymentDayInMonth != "") { Parameters.Add("paymentDayInMonth", PaymentDayInMonth); } if (PaymentDayInWeek != "") { Parameters.Add("paymentDayInWeek", PaymentDayInWeek); } if (TerminationDate != "") { Parameters.Add("terminationDate", TerminationDate); } if (AdditionalReference != "") { Parameters.Add("additionalReference", AdditionalReference); } if (CustomDDReference != "") { Parameters.Add("customDirectDebitReference", CustomDDReference); } var CreateRequest = Handler.Session(Settings); var SendRequest = CreateRequest.Post(string.Format("customer/{0}/contract", Customer), Parameters); // Pass the return string to the handler. This will throw an exception if it is not what we expect Handler.GenericExceptionCheck(SendRequest); // Get the JSON returned from EazyCustomerManager JObject SendRequestAsJson = JObject.Parse(SendRequest); // Get the list of Contracts JSON objects JToken NewContract = SendRequestAsJson; return(NewContract.ToString()); }