Skip to content

Ledger Functions

The Ledger is the array of financial entries a quote, adjustment or cancellation produces. Elements stores it with the policy for accounting and reporting.


Overview

The Quote Extension functions that calculate a price clear Ledger and rebuild it:

Function Entries written
$CalculatePremiumAdditive(quoteType) One Total entry for all items, then Tax duty, one AddOn.{name} per add-on, and Fee
$CalculateMTAPremium(mtaFee) The difference between PreviousMatrix and PricingMatrix, in the same sections, subtype Regular (or a single MTA Premium Waived entry)
$CalculateFullCancellation, $CalculateProRataCancellation, $CalculateNoRefundCancellation One refund entry per section of MTALedgerHistory, subtype Cancellation, negative amounts, plus Cancellation fee

$AddTaxDuty and $AddToLedger add one entry to the existing ledger.

Items, factors and discounts don't get entries of their own: they are combined into the Total entry.


Ledger Values

Every entry the Quote Extension writes has LedgerType "Policy".

LedgerSubType When
The quote type passed to $CalculatePremiumAdditive: "New business" or "Renewals" New business and renewal quotes
"Regular" Mid-term adjustments ($CalculateMTAPremium, $AddTaxDuty on an adjustment)
"Cancellation" Cancellations
LedgerSection Contents
"Total" All pricing matrix items, after factors and discounts
"Tax duty" Tax duty, when the matrix has one (TaxType "Tax duty")
"AddOn.{name}" One per add-on, for example "AddOn.Gadget Cover"
"Fee" Admin fee and its IPT
"MTA Premium Waived" Single zero entry when an adjustment's premium is waived
"Cancellation fee" Fee charged on cancellation

Elements matches these strings exactly. Customer Management finds a quote's entries by LedgerType "Policy" and LedgerSubType "New business", "Renewals", "Regular" or "Cancellation", so pass $CalculatePremiumAdditive exactly "New business" or "Renewals".

Customer Management also writes ledger entries outside EverSharp, which can appear in MTALedgerHistory:

LedgerType LedgerSubType
"Policy" "Adjustment" (stored adjustments), "Reinstatement" (reversals)
"Payment" "Card", "External Payment" (finance agreements), "Chargeback"

$AddToLedger

Adds one entry to Ledger. It writes the arguments as given and doesn't validate them.

Signature

$AddToLedger(ledgerType, ledgerSubType, ledgerSection, netAmount, commissionAmount, taxAmount, taxRate, taxType, grossAmount, currency, isRefundable)

Parameters

All 11 are required, in this order.

  • ledgerType (string) - "Policy" for premium entries (see Ledger Values)
  • ledgerSubType (string) - The quote type: "New business", "Renewals", "Regular" (adjustments) or "Cancellation"
  • ledgerSection (string) - Section name, for example "Total", "Fee" or "AddOn.Gadget Cover"
  • netAmount (number) - Net amount
  • commissionAmount (number) - Commission amount
  • taxAmount (number) - Tax amount
  • taxRate (number) - Tax rate as a decimal (e.g., 0.12)
  • taxType (string) - "IPT" or "Tax duty"
  • grossAmount (number) - Gross amount
  • currency (string) - ISO currency code
  • isRefundable (boolean) - Whether the entry is refunded on cancellation

Returns

  • Nothing to use in the script: read Ledger after the call

Ledger must already exist (the calling system supplies it, or set Ledger = [];), otherwise the call fails.

Example

$AddToLedger(
    "Policy",             // ledgerType
    "New business",       // ledgerSubType
    "Manual adjustment",  // ledgerSection
    50.00,                // netAmount
    7.50,                 // commissionAmount
    6.00,                 // taxAmount
    0.12,                 // taxRate (12%)
    "IPT",                // taxType
    56.00,                // grossAmount
    "GBP",                // currency
    true                  // isRefundable
);

totalGross = $Sum(Ledger, "GrossAmount");

Call it after $CalculatePremiumAdditive, $CalculateMTAPremium or a cancellation function: those clear the ledger and would remove the entry.


Ledger Structure

Entry Fields

Each ledger entry is an object with these properties:

{
    "LedgerType": "Policy",            // See Ledger Values
    "LedgerSubType": "New business",   // Quote type
    "LedgerSection": "Total",          // Section
    "NetAmount": 500.00,               // Net
    "CommissionAmount": 75.00,         // Commission
    "TaxAmount": 69.00,                // Tax
    "TaxRate": 0.12,                   // Tax rate
    "TaxType": "IPT",                  // "IPT" or "Tax duty"
    "GrossAmount": 644.00,             // Gross
    "Currency": "GBP",                 // Currency
    "IsRefundable": true               // Refunded on cancellation
}

Automatic Ledger Population

$CalculatePremiumAdditive

$SetCurrencyToPricingMatrix("GBP");
$AddItemToPricingMatrix("Base Premium", 500.00, 1.0);
$AddFactorToPricingMatrix("Age Factor", 1.20, true, 0);
$SetFeeToPricingMatrix(20.00);

$CalculatePremiumAdditive("New business");

// Ledger now contains (subtype "New business"):
// - "Total": the base premium after the age factor
// - "Fee": the admin fee
// plus "Tax duty" and one "AddOn.{name}" entry when the matrix has them

$CalculateMTAPremium

$CalculateMTAPremium(25.00);

// Ledger contains the difference from PreviousMatrix (subtype "Regular"):
// - "Total", "AddOn.{name}" and "Fee" (including the MTA fee)
// or a single "MTA Premium Waived" entry when the premium is waived

Cancellation Functions

$CalculateProRataCancellation(25.00, false);

// Ledger contains (subtype "Cancellation"):
// - One entry per section of MTALedgerHistory, with negative (refund) amounts
// - "Cancellation fee"

Ledger Analysis

Accessing Entries

// Get all entries
allEntries = Ledger;

// Filter by section
totalEntries = Ledger.Filter((e) => e.LedgerSection == "Total");
feeEntries = Ledger.Filter((e) => e.LedgerSection == "Fee");
addOnEntries = Ledger.Filter((e) => e.LedgerSection.StartsWith("AddOn."));

// Filter by subtype
cancellationEntries = Ledger.Filter((e) => e.LedgerSubType == "Cancellation");

Calculating Totals

// Total gross premium
totalGross = $Sum(Ledger, "GrossAmount");

// Total net (before commission)
totalNet = $Sum(Ledger, "NetAmount");

// Total tax
totalTax = $Sum(Ledger, "TaxAmount");

// Total commission
totalCommission = $Sum(Ledger, "CommissionAmount");

Ledger History

Cancellations use the policy's historical ledger data. MTAs do not: $CalculateMTAPremium compares PricingMatrix against PreviousMatrix instead.

MTALedgerHistory

Contains every ledger entry for the policy so far (the original sale plus any adjustments). The calling system supplies it, and $CalculateFullCancellation, $CalculateProRataCancellation and $CalculateNoRefundCancellation read it to work out the refund:

// Environment variable: MTALedgerHistory (supplied by the calling system)
paidToDate = $Sum(MTALedgerHistory, "GrossAmount");

$CalculateProRataCancellation(25.00, false);

Reporting Example

// Generate ledger summary report
function GenerateLedgerReport() {
    report = {
        "TotalGross": $Sum(Ledger, "GrossAmount"),
        "TotalNet": $Sum(Ledger, "NetAmount"),
        "TotalCommission": $Sum(Ledger, "CommissionAmount"),
        "TotalTax": $Sum(Ledger, "TaxAmount"),
        "EntryCount": Ledger.Length,
        "BySection": {}
    };

    reportIndex = 0;
    while (reportIndex < Ledger.Length) {
        reportEntry = Ledger[reportIndex];
        reportIndex++;
        $SetValue(report.BySection, reportEntry.LedgerSection, reportEntry.GrossAmount);
    }

    return report;
}

report = GenerateLedgerReport();
$Log("Ledger Report", report);

Integration with Output

The ledger data flows into the Output object:

$CalculatePremiumAdditive("New business");

// Output automatically populated from ledger totals
finalPremium = Output.Premium;  // Sum of ledger gross amounts
iptApplied = Output.IptApplied;  // IPT rate from pricing matrix

Best Practices

1. Let Functions Manage the Ledger

Prefer $CalculatePremiumAdditive and the MTA and cancellation functions to manual $AddToLedger calls. If you add entries manually, use the ledger values Elements expects.

2. Don't Overwrite Ledger History

MTALedgerHistory and PreviousMatrix are supplied by the calling system. Don't reassign them in scripts:

// ❌ Avoid - $CalculateMTAPremium ignores MTALedgerHistory and uses PreviousMatrix
MTALedgerHistory = Ledger;

// ✅ Just calculate - the calling system provides PreviousMatrix and MTALedgerHistory
$CalculateMTAPremium(15.00);

3. Validate Ledger Totals

// Ensure ledger gross matches the premium
ledgerGross = $Sum(Ledger, "GrossAmount");

if ($Abs(ledgerGross - Output.Premium) > 0.01) {
    $Log("Warning: Ledger mismatch");
}

Environment Variables

Variable Type Description
Ledger Array Current ledger entries
MTALedgerHistory Array Policy ledger history (used by cancellations)
Output Object Populated with ledger totals

Next Steps