-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create at_update_total_share_commitments.js
- Loading branch information
1 parent
e763008
commit df6277c
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Update the total share counts for each share type | ||
let subscribersTable = base.getTable('Subscribers'); | ||
let subscriptionsTable = base.getTable('Subscriptions'); | ||
let subscribersView = subscribersTable.getView('Grid view'); | ||
let subscriptionsView = subscriptionsTable.getView('Grid view'); | ||
let shareTypesTable = base.getTable('Share Types and Price List'); | ||
let shareTypesView = shareTypesTable.getView('Grid view'); | ||
let subscribersResults = await subscribersView.selectRecordsAsync({fields: ['Name','Reserved']}); | ||
let subscriptionsResults = await subscriptionsView.selectRecordsAsync({fields: ['Share','Share Price','Frequency (in weeks)','Number of Shares','Subscribers']}); | ||
let shareTypesResults = await shareTypesView.selectRecordsAsync({fields: ['Name']}); | ||
|
||
let occurrences = [52,26,17,13,10,9,7,6]; | ||
|
||
// For each shareType, go through all the subscriptions and sum up the total shares for | ||
// the year = sub.quantity * (occurrences[sub.frequency-1]) | ||
for(let shareType of shareTypesResults.records){ | ||
let runningTotal = 0; | ||
for(let subscriptionRecord of subscriptionsResults.records){ | ||
if(subscriptionRecord.getCellValueAsString('Share') == shareType.getCellValueAsString('Name')){ | ||
|
||
let currentSubscriber = subscriptionRecord.getCellValue("Subscribers")[0].name; | ||
for(let subscriberRecord of subscribersResults.records){ | ||
if(subscriberRecord.getCellValueAsString("Name") == currentSubscriber && subscriberRecord.getCellValueAsString("Reserved") == "Yes"){ | ||
runningTotal += subscriptionRecord.getCellValue('Number of Shares') * occurrences[parseInt(subscriptionRecord.getCellValueAsString('Frequency (in weeks)'))-1]; | ||
continue; | ||
} | ||
} | ||
|
||
} | ||
} | ||
|
||
await shareTypesTable.updateRecordAsync(shareType.id, { | ||
"Shares Allocated" : runningTotal, | ||
}) | ||
|
||
} |