Summary
This document lists available scripts for use as actions in EngageIP. For more information on actions see the article Extending EngageIP With Action Scripts.
Adding Action Scripts
To use these scripts, click on Action link on the Setup page and paste the code in as shown, enter a descriptive name and click Save. The action scripts can then be selected for use under Credit Rating / Payment terms for example.
Late Fee Script - Percentage of Invoice Balance
Note: For late fee scripts, be sure to update the associated Service name to match an existing service in your deployment. The Service name is required for taxing and reporting purposes.
This script will add a late fee calculated as a percentage of the invoice balance based on the value you enter in the 'Rate' field. If you need the system to add a rate like 1.5% rather then a whole number you can convert the rate profile question to a datatype of 'double'. This allows you to enter 1.5 which will be calculated down to 0.015 in the script below.
Under the action created, click add to add a profile question, configure it as below (both for percentage or flat fee). When you add the action to credit rating, it will request the percentage or flat fee you want to add according to the action you've selected in this KB.
//Name: LateFeeFlatFee //Description: //EventCode: if (context.ContainsKey("InvoiceID")) { var invoice = Invoice.GetByID(Convert.ToInt32(context["InvoiceID"])); if (Logic.Core.User.GetUserBalance(invoice.UserID) > 0) { var rate = Convert.ToDouble(context["Rate"]); var statement = Logisense.Boss.Logic.Billing.Bill.RetrieveStatement(Logisense.Boss.Logic.Billing.Bill.FindTopLevelParent(invoice.UserID)); using (var invoiceSummaryCalculator = new InvoiceSummaryCalculator(statement)) { Logisense.Boss.Logic.Billing.Bill.AddCharge(statement.ID, invoice.UserID, rate, "Late charge", DateTime.MinValue, DateTime.MinValue, false, invoiceSummaryCalculator, Service.SearchByName("Invoice Late Fee CDN")[0]); invoiceSummaryCalculator.UpdateAggregate(); } } return true; } return false;
To add a GL Code, you can insert a service name as shown in the below script:
if (context.ContainsKey("InvoiceID")) { Invoice invoice = Invoice.GetByID(Convert.ToInt32(context["InvoiceID"])); double rate = Convert.ToDouble(context["Rate"]); double total = Logisense.Boss.Logic.Invoicing.GetInvoiceTotal(invoice); int statementID = Logisense.Boss.Logic.Billing.Bill.RetrieveStatement(Logisense.Boss.Logic.Billing.Bill.FindTopLevelParent(invoice.UserID)); double amount = Logisense.Boss.Logic.Billing.Rounding.Round(total * (rate / 100), 2); Logisense.Boss.Logic.Billing.Bill.AddCharge(statementID, invoice.UserID, amount, "Late charge", DateTime.MinValue, DateTime.MinValue, true,Service.SearchByName("Invoice Late Fee CDN")[0]); return true; } else { return false; }
Late Fee Script - Percentage with No Tax Applied
This script will add a dollar amount late fee to an account as a percentage of the invoice total but will not have taxes applied to the late fee.
//Name: LateFeePercentageNoTaxApplied //Description: //EventCode: if (context.ContainsKey("InvoiceID")) { var invoice = Invoice.GetByID(Convert.ToInt32(context["InvoiceID"])); var rate = Convert.ToDouble(context["Rate"]); var total = Invoicing.GetInvoiceTotal(invoice); var totalTax = 0d; var statement = Logisense.Boss.Logic.Billing.Bill.RetrieveStatement(Logisense.Boss.Logic.Billing.Bill.FindTopLevelParent(invoice.UserID)); foreach (var statementDetail in statement.GetStatementDetailsCollection()) { foreach (var statementDetailsTax in statementDetail.GetStatementDetailsTaxCollection()) { totalTax += statementDetailsTax.Amount; } } total -= totalTax; var amount = Logisense.Boss.Logic.Billing.Rounding.Round(total * (rate / 100), 2); using (var invoiceSummaryCalculator = new InvoiceSummaryCalculator(statement)) { Logisense.Boss.Logic.Billing.Bill.AddCharge(statement.ID, invoice.UserID, amount, "Late charge", DateTime.MinValue, DateTime.MinValue, false, invoiceSummaryCalculator, Service.SearchByName("Invoice Late Fee CDN")[0]); invoiceSummaryCalculator.UpdateAggregate(); } return true; } return false;
Late Fee Script - Flat Fee
The script below adds a flat fee when credit rating runs and the invoice has an overdue balance.
//Name: LateFeeFlatFee //Description: //EventCode: if (context.ContainsKey("InvoiceID")) { var invoice = Invoice.GetByID(Convert.ToInt32(context["InvoiceID"])); if (Logic.Core.User.GetUserBalance(invoice.UserID) > 0) { var rate = Convert.ToDouble(context["Rate"]); var statement = Logisense.Boss.Logic.Billing.Bill.RetrieveStatement(Logisense.Boss.Logic.Billing.Bill.FindTopLevelParent(invoice.UserID)); using (var invoiceSummaryCalculator = new InvoiceSummaryCalculator(statement)) { Logisense.Boss.Logic.Billing.Bill.AddCharge(statement.ID, invoice.UserID, rate, "Late charge", DateTime.MinValue, DateTime.MinValue, false, invoiceSummaryCalculator, Service.SearchByName("Invoice Late Fee CDN")[0]); invoiceSummaryCalculator.UpdateAggregate(); } } return true; } return false;
Send Email Script - Standard
This script sends an email when an invoice has an overdue balance. The profile questions will take the name of the email message setup under 'email messages' on the setup page.
//Name: SendEmail //Description: //EventCode: var user = User.GetByID(Convert.ToInt32(context["UserID"])); var emails = user.GetOwner().SearchEmailMessageByName(context["EmailMessage"].ToString()); if (emails.Length > 0) { var coreEmail = (Logic.Core.EmailMessage)emails[0]; coreEmail.SendTo(user.ID); return true; } return false;
Send Email if User Status IS NOT 'Canceled'
Note: this script references the back end status, not the defined label status on the setup tab. Available back end statuses are 'Canceled', 'Active', 'Suspended' and 'Prospect'.
//Name: SendEmailUserStatusNotCanceled //Description: //EventCode: var user = User.GetByID(Convert.ToInt32(context["UserID"])); EventLogger.Log(user.ID, user.ID, "SendEmailUserStatusNotCanceled", "CustomCode", string.Format("User's current StatusType: {0}", user.GetCurrent_StatusType().Name), user.OwnerID, "User", user.ID, null); if (user.GetCurrent_StatusType().Name != "Canceled") { var emails = user.GetOwner().SearchEmailMessageByName(context["EmailMessage"].ToString()); if (emails.Length > 0) { var coreEmail = (Logic.Core.EmailMessage)emails[0]; coreEmail.SendTo(user.ID); return true; } } return false;
Set User Status where NOT 'Canceled'
Similar to above, an if statement will set the user status when the account is not in a status of 'canceled'.
Note: When setting an account status to canceled the system will cancel any packages and add a prorated refund by default. If you do not want a prorated refund you can create a status of 'Suspended' with a back end status of 'Active'. You can then track who has this status and perform administrative tasks on them as needed, enable, charge credit card manually, etc.
//Name: SetUserStatusWhereNotCanceled //Description: //EventCode: var user = User.GetByID(Convert.ToInt32(context["UserID"])); EventLogger.Log(user.ID, user.ID, "SetUserStatusWhereNotCanceled", "CustomCode", string.Format("User's current StatusType: {0}", user.GetCurrent_StatusType().Name), user.OwnerID, "User", user.ID, null); if (user.GetCurrent_StatusType().Name != "Canceled") { var userStatusType = user.GetActingOwner().SearchUserStatusTypeByName(context["UserStatus"].ToString())[0]; if (userStatusType != default(UserStatusType)) { user.UserStatusTypeID = userStatusType.ID; user.Update(); return true; } } return false;
Balance Threshold Wrappers
This script is used to include thresholds like invoice balance thresholds into the logic, see below options. For assistance on using these, please contact LogiSense customer support.
LogiSense Support | email: support@logisense.com | |
phone (Canada): 1-519-249-0508 | phone (USA): 1-917-410-3584 |
The below script checks profile question on the action called 'Threshold' and will only execute its payload (in this case an email user script) if the invoice balance is greater than x (as //configured in the profile question.
//Name: PaymentTermsWithBalanceThreshold //Description: //EventCode: var threshold = Convert.ToDouble(context["Threshold"]); if (context.ContainsKey("InvoiceID") && threshold > 0) { var invoice = ViewInvoice.GetByID(Convert.ToInt32(context["InvoiceID"])); var user = User.GetByID(Convert.ToInt32(context["UserID"])); if (invoice.Balance > threshold) { EventLogger.Log(user.ID, user.ID, "PaymentTermsWithBalanceThreshold", "PaymentTerms", "Executing Payment Term PaymentTermsWithBalanceThreshold", user.OwnerID, "Invoice", invoice.ID, null); var emails = user.GetOwner().SearchEmailMessageByName(context["EmailMessage"].ToString()); if (emails.Length > 0) { var email = (Logic.Core.EmailMessage)emails[0]; email.SendTo(user.ID); return true; } } else { EventLogger.Log(user.ID, user.ID, "PaymentTermsWithBalanceThreshold", "PaymentTerms", "Error returned from Threshold PT", user.OwnerID, "Invoice", invoice.ID, null); } } return false;
Second Example - To run the payment term IF the recurring total is greater then y, use the following script which will send an email but can be implanted with other action scripts to do things like applying a late fee or other functionality.
//Name: PaymentTermsWithInvoiceRecurringTotalThreshold //Description: //EventCode: var threshold = Convert.ToDouble(context["Threshold"]); if (context.ContainsKey("InvoiceID") && threshold > 0) { var user = User.GetByID(Convert.ToInt32(context["UserID"])); var invoice = ViewInvoice.GetByID(Convert.ToInt32(context["InvoiceID"])); var total = 0d; foreach (var statementDetail in ViewStatementdetailsInvoiceByPackageGroup.SearchByInvoiceID(Convert.ToInt32(context["InvoiceID"]))) { total += statementDetail.Amount * statementDetail.Quantity; } if (total > threshold) { EventLogger.Log(user.ID, user.ID, "PaymentTermsWithInvoiceRecurringTotalThreshold", "PaymentTerms", "Executing Payment Term PaymentTermsWithInvoiceRecurringTotalThreshold", user.OwnerID, "Invoice", invoice.ID, null); //Execute other logic here var emails = user.GetOwner().SearchEmailMessageByName(context["EmailMessage"].ToString()); if (emails.Length > 0) { var email = (Logic.Core.EmailMessage) emails[0]; email.SendTo(user.ID); return true; } } EventLogger.Log(user.ID, user.ID, "PaymentTermsWithInvoiceRecurringTotalThreshold", "PaymentTerms", context["EventLogMessageOnThresholdNotMet"].ToString(), user.OwnerID, "Invoice", invoice.ID, null); } return false;
On each action you will need to add these Profile Questions:
Name | DataType |
Threshold | Double |
EventLogMessageOnThresholdNotMet | Text |
EmailMessage | Text |
Write Off
This write off script requires two profile questions on action:
EventLogMessage - Profile question with a datatype of text. This message will be added to the Event Log report if the user is NOT canceled. Otherwise there is Event Log text in the action that can be adjusted
UserStatus - this is the status to which you would like the user to be set, i.e. you should have a status named 'Write Off' and that value should be in the profile question as well. The name needs to match the status type exactly.
This write off script is fired by payment terms for invoices that are past due by a set amount. It will set the user status on the account to that which you have selected and add a credit to the account to zero out the accounts balance effectively removing it from reports (because its balanced).
//Name: PaymentTermsWriteOff //Description: //EventCode: var user = User.GetByID(Convert.ToInt32(context["UserID"])); var userBalanceFutureQuery = new ViewUserBalanceFutureQuery { UserID = user.ID }; var balance = 0d; foreach (var userBalanceFuture in ViewUserBalanceFuture.GetCollection(ref userBalanceFutureQuery)) { balance += userBalanceFuture.Balance; } var statusType = StatusType.GetByID(user.Current_StatusTypeID); //This message is only logged to the EventLog if the user is NOT canceled var message = Convert.ToString(context["EventLogMessage"]); if (statusType.Name == "Canceled") { if (balance > 0) { balance = balance * -1; var coreUser = (Logic.Core.User) user; coreUser.refundOptionName = Logic.Core.RefundOption.NONE; Logisense.Boss.Logic.Billing.Bill.AddCredit(coreUser.ID, balance, "Write Off (Payment Terms)", DateTime.MinValue, true); var userStatusType = user.GetActingOwner().SearchUserStatusTypeByName(context["UserStatus"].ToString())[0]; coreUser.UserStatusTypeID = userStatusType.ID; coreUser.Update(); EventLogger.Log(user.ID, user.ID, "PaymentTermsWriteOff", "PaymentTerms", "User was canceled and balance was written off successfully.", user.OwnerID, "User", user.ID, null); return true; } else { EventLogger.Log(user.ID, user.ID, "PaymentTermsWriteOff", "PaymentTerms", message, user.OwnerID, "User", user.ID, null); } } else { EventLogger.Log(user.ID, user.ID, "PaymentTermsWriteOff", "PaymentTerms", message, user.OwnerID, "User", user.ID, null); } return false;