|
| 1 | +"use strict"; |
| 2 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 | +exports.isoNsoCalculator = void 0; |
| 4 | +const read_ocf_package_1 = require("../read_ocf_package"); |
| 5 | +const vesting_schedule_generator_1 = require("../vesting_schedule_generator"); |
| 6 | +const addYearAndGrantId = (vestingSchedule, issuanceId) => { |
| 7 | + return vestingSchedule.map((entry) => { |
| 8 | + const date = new Date(entry.Date); |
| 9 | + return Object.assign(Object.assign({}, entry), { Grant: issuanceId, Year: date.getFullYear() }); |
| 10 | + }); |
| 11 | +}; |
| 12 | +// Function to sum the amounts by year |
| 13 | +const sumByYear = (vestingSchedule) => { |
| 14 | + // Create an empty object to store the sums by year |
| 15 | + const result = {}; |
| 16 | + // Iterate through the array |
| 17 | + vestingSchedule.forEach((entry) => { |
| 18 | + if (entry["Event Type"] !== "Exercise") { |
| 19 | + const year = entry.Year; |
| 20 | + const amount = entry["Event Quantity"]; |
| 21 | + // If the year is already in the result, add the amount |
| 22 | + if (result[year]) { |
| 23 | + result[year] += amount; |
| 24 | + } |
| 25 | + else { |
| 26 | + // Otherwise, set the initial amount for the year |
| 27 | + result[year] = amount; |
| 28 | + } |
| 29 | + } |
| 30 | + }); |
| 31 | + // Convert the result object into an array of objects for displaying |
| 32 | + const resultTable = Object.keys(result).map((yearString) => { |
| 33 | + const year = parseInt(yearString, 10); // Convert the key back to a number |
| 34 | + return { |
| 35 | + Grant: vestingSchedule[0].Grant, |
| 36 | + Year: year, |
| 37 | + TotalAmountVested: result[year], |
| 38 | + FMV: 0, |
| 39 | + VestedValue: 0, |
| 40 | + }; |
| 41 | + }); |
| 42 | + return resultTable; |
| 43 | +}; |
| 44 | +// Function to calculate capacity |
| 45 | +function calculateCapacity(vestingData, capacityPerYear) { |
| 46 | + const result = []; |
| 47 | + let remainingCapacity = capacityPerYear; |
| 48 | + let currentYear = vestingData[0].Year; // Initialize the current year to the year of the first row |
| 49 | + vestingData.forEach((row) => { |
| 50 | + // If the year changes, reset the remaining capacity to the full capacity for the new year |
| 51 | + if (row.Year !== currentYear) { |
| 52 | + remainingCapacity = capacityPerYear; |
| 53 | + currentYear = row.Year; // Update the current year |
| 54 | + } |
| 55 | + // Determine how much capacity is used in this row |
| 56 | + const usedCapacity = Math.min(row.VestedValue, remainingCapacity); |
| 57 | + // Update remaining capacity after usage |
| 58 | + remainingCapacity -= usedCapacity; |
| 59 | + // Add Capacity Used and Capacity Remaining to the row |
| 60 | + result.push(Object.assign(Object.assign({}, row), { CapacityUsed: usedCapacity, CapacityRemaining: remainingCapacity })); |
| 61 | + }); |
| 62 | + return result; |
| 63 | +} |
| 64 | +// Function to add ISO Shares and NSO Shares to the vesting data |
| 65 | +function addSharesColumns(vestingData) { |
| 66 | + return vestingData.map((row) => { |
| 67 | + const isoShares = Math.round(row.CapacityUsed / row.FMV); |
| 68 | + const nsoShares = Math.round((row.VestedValue - row.CapacityUsed) / row.FMV); |
| 69 | + return Object.assign(Object.assign({}, row), { ISOShares: isoShares, NSOShares: nsoShares }); |
| 70 | + }); |
| 71 | +} |
| 72 | +// Function to add ISO Used, ISO Remaining, and NSO columns |
| 73 | +function addISOColumns(vestingData, isoData) { |
| 74 | + const result = []; |
| 75 | + const isoRemainingByGrantAndYear = {}; |
| 76 | + for (let i = 0; i < vestingData.length; i++) { |
| 77 | + // Get the corresponding ISO share information for the grant and year |
| 78 | + const isoInfo = isoData.find((iso) => iso.Grant === vestingData[i].Grant && iso.Year === vestingData[i].Year); |
| 79 | + if (!isoInfo) { |
| 80 | + throw new Error(`ISO data not found for Grant ${vestingData[i].Grant} and Year ${vestingData[i].Year}`); |
| 81 | + } |
| 82 | + const grantYearKey = `${vestingData[i].Grant}-${vestingData[i].Year}`; |
| 83 | + // Initialize ISO Remaining at the start of the year |
| 84 | + if (!(grantYearKey in isoRemainingByGrantAndYear)) { |
| 85 | + isoRemainingByGrantAndYear[grantYearKey] = isoInfo.ISOShares; |
| 86 | + } |
| 87 | + // Calculate ISO Used as the minimum of Amount Vested and ISO Remaining |
| 88 | + const isoUsed = Math.min(vestingData[i]["Event Quantity"], isoRemainingByGrantAndYear[grantYearKey]); |
| 89 | + // Calculate NSO if ISO Remaining is zero |
| 90 | + const nso = isoRemainingByGrantAndYear[grantYearKey] === 0 ? vestingData[i]["Event Quantity"] : Math.max(0, vestingData[i]["Event Quantity"] - isoUsed); |
| 91 | + // Update ISO Remaining |
| 92 | + const isoRemaining = isoRemainingByGrantAndYear[grantYearKey] - isoUsed; |
| 93 | + // Store the updated ISO Remaining for the next rows in the same year |
| 94 | + isoRemainingByGrantAndYear[grantYearKey] = isoRemaining; |
| 95 | + // Add the row with the new ISO and NSO columns |
| 96 | + result.push(Object.assign(Object.assign({}, vestingData[i]), { ISO: isoUsed, NSO: nso, ISORemaining: isoRemaining })); |
| 97 | + } |
| 98 | + return result; |
| 99 | +} |
| 100 | +const isoNsoCalculator = (packagePath, stakeholderId, capacity) => { |
| 101 | + const ocfPackage = (0, read_ocf_package_1.readOcfPackage)(packagePath); |
| 102 | + const valuations = ocfPackage.valuations; |
| 103 | + const transactions = ocfPackage.transactions; |
| 104 | + const equityCompensationIssuances = transactions.filter((transaction) => transaction.stakeholder_id === stakeholderId && transaction.object_type === "TX_EQUITY_COMPENSATION_ISSUANCE"); |
| 105 | + if (equityCompensationIssuances.length === 0) { |
| 106 | + throw new Error("No equity compensation issuances found for stakeholder"); |
| 107 | + } |
| 108 | + const sortedIssuances = equityCompensationIssuances.sort((a, b) => a.date.localeCompare(b.date)); |
| 109 | + const combinedYearTable = []; |
| 110 | + const combinedGrants = []; |
| 111 | + let vestedByYearTable = []; |
| 112 | + sortedIssuances.forEach((issuance) => { |
| 113 | + const vestingSchedule = (0, vesting_schedule_generator_1.generateSchedule)(packagePath, issuance.security_id); |
| 114 | + const vestingScheduleWithYearAndGrantId = addYearAndGrantId(vestingSchedule, issuance.id); |
| 115 | + combinedGrants.push(vestingScheduleWithYearAndGrantId); |
| 116 | + vestedByYearTable = sumByYear(vestingScheduleWithYearAndGrantId); |
| 117 | + valuations.forEach((valuation) => { |
| 118 | + if (valuation.id === issuance.valuation_id) { |
| 119 | + for (let i = 0; i < vestedByYearTable.length; i++) { |
| 120 | + vestedByYearTable[i]["FMV"] = parseFloat(valuation.price_per_share.amount); |
| 121 | + vestedByYearTable[i]["VestedValue"] = vestedByYearTable[i]["FMV"] * vestedByYearTable[i]["TotalAmountVested"]; |
| 122 | + } |
| 123 | + } |
| 124 | + }); |
| 125 | + combinedYearTable.push(...vestedByYearTable); |
| 126 | + }); |
| 127 | + combinedYearTable.sort((a, b) => a.Year - b.Year); |
| 128 | + const updatedVestingData = calculateCapacity(combinedYearTable, capacity); |
| 129 | + // Add ISO and NSO shares |
| 130 | + const updatedVestingDataWithShares = addSharesColumns(updatedVestingData); |
| 131 | + const sortedByGrant = updatedVestingDataWithShares.sort((a, b) => a.Grant.localeCompare(b.Grant)); |
| 132 | + let result = []; |
| 133 | + combinedGrants.forEach((grant) => { |
| 134 | + const updatedVestingData = addISOColumns(grant, sortedByGrant); |
| 135 | + result.push(updatedVestingData); |
| 136 | + }); |
| 137 | + return result; |
| 138 | +}; |
| 139 | +exports.isoNsoCalculator = isoNsoCalculator; |
0 commit comments