-
Notifications
You must be signed in to change notification settings - Fork 2
/
captable.js
1967 lines (1691 loc) · 77.9 KB
/
captable.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* An object representing a captable.
* it gets used by the AA-SG-SPA.xml:
*
* <numbered_2_firstbold>Capitalization</numbered_2_firstbold>
*
* in future it will be something like data.capTable.getCurrentRound().by_security_type(...)
* and it will know what the currentRound is from the name of the ...getActiveSheet()
*
* <numbered_3_para>Immediately prior to the Initial Closing, the fully diluted capital of the Company will consist of <?= digitCommas_(data.capTable.getRound("Bridge Round").by_security_type["Ordinary Shares"].TOTAL) ?> ordinary shares, <?= digitCommas_(data.capTable.getRound("Bridge Round").by_security_type["Class F Shares"].TOTAL) ?> Class F Redeemable Convertible Preference Shares both issued and reserved, and <?= digitCommas_(data.capTable.getRound("Bridge Round").by_security_type["Series AA Shares"].TOTAL) ?> YC-AA Preferred Shares. These shares shall have the rights, preferences, privileges and restrictions set forth in <xref to="articles_of_association" />.</numbered_3_para>
* <numbered_3_para>The outstanding shares have been duly authorized and validly issued in compliance with applicable laws, and are fully paid and nonassessable.</numbered_3_para>
* <numbered_3_para>The Company's ESOP consists of a total of <?= data.parties.esop[0].num_shares ?> shares, of which <?= digitCommas_(data.parties.esop[0]._orig_num_shares - data.capTable.getRound("Bridge Round").old_investors["ESOP"].shares) ?> have been issued and <?= digitCommas_(data.capTable.getRound("Bridge Round").old_investors["ESOP"].shares)?> remain reserved.</numbered_3_para>
*
* the above hardcodes the name of the round into the XML. this is clearly undesirable.
* we need a better way to relate the active round with the relevant terms spreadsheet.
*
* How does this work?
* First we go off and parse the cap table into a data structure
* then we set up a bunch of methods which interpret the data structure as needed for the occasion.
*
* Maybe in future the argument to the constructor should be a readRows_ object, not a sheet.
* but the format of the cap table is different enough from everything else that we can easily justify implementing a totally different parser in this module.
*
* @constructor
* @param {Sheet} termsheet - the currently active sheet which we're filling templates for
* @param {Sheet} [captablesheet=sheet named "Cap Table"] - the sheet containing a well-formed cap table
* @return {capTable}
*/
var DEFAULT_TERM_TEMPLATE = "https://docs.google.com/spreadsheets/d/1rBuKOWSqRE7QgKgF6uVWR9www4LoLho4UjOCHPQplhw/edit#gid=1632229599";
var DEFAULT_CAPTABLE_TEMPLATE = "https://docs.google.com/spreadsheets/d/1rBuKOWSqRE7QgKgF6uVWR9www4LoLho4UjOCHPQplhw/edit#gid=827871932";
function Holder(round, holderName, readrows) {
ctLog(["new Holder object %s, round %s", holderName, round.name], 9);
this.round = round;
this.name = holderName;
this.readrows = readrows;
}
function eligible_securities(securityName) {
// here we list ineligible securities; if not found, then we assume the input security name is eligible.
// this logic properly belongs in each security's term sheet, where we can pull it out as an attribute of the security.
// TODO refactor so we refer to each sheet's readrows terms to extract characteristic.
return (["Class F Shares", "Convertible Note", "Class F-NV", "SAFE", "Simple Agreement for Future Equity"]
.indexOf(securityName) < 0);
}
Holder.prototype.origEntity = function() {
return this.readrows.entitiesByName[this.name];
}
Holder.prototype.preemptiveShares = function() {
ctLog(["Holder(): %s has %s shares. how many are eligible for preemptive calculation?", this.name, this.shares], 8);
var myeligible = this.round.preemptivelyEligibleSecurities(this.name);
var myunrestricted = this.origEntity()._orig_remaining_unrestricted || 0;
var toreturn = myeligible + myunrestricted;
ctLog(["Holder(): existingly eligible shares=%s; unrestricted shares=%s; total=%s",myeligible,myunrestricted,toreturn], 8);
return toreturn;
}
function capTable_(termsheet, captablesheet, readrows) {
ctLog(["instantiating capTable object"], 6);
termsheet = termsheet || SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
captablesheet = captablesheet || termsheet.getParent().getSheetByName("Cap Table");
this.termsheet = termsheet;
this.captablesheet = captablesheet;
this.readrows = readrows;
if (captablesheet == undefined) {
ctLog(["there is no Cap Table sheet for %s.%s, returning .isValid=false", termsheet.getParent().getName(), termsheet.getSheetName()],3);
this.isValid = false;
return;
}
this.isValid = true;
this.getUrl = function() { return captablesheet.getParent().getUrl() + "#gid=" + captablesheet.getSheetId() };
ctLog(["parsing captablesheet %s.%s, active round being %s.%s",
captablesheet.getParent().getName(), captablesheet.getSheetName(),
termsheet.getParent().getName(), termsheet.getSheetName()],
6);
/**
* @method
* @return {object} captablesheet
*/
this.getSheet = function(){
var activatedCapSheet = new capTableSheet_(captablesheet);
return activatedCapSheet;
};
/**
* @member {string}
*/
this.activeRound = termsheet.getSheetName();
ctLog(["initializer: set this.activeRound = %s", this.activeRound], 6);
/**
* @member
*/
this.rounds = this.parseCaptable();
this.rounds.captable = this;
this.getAllRounds = function(){
return this.rounds
};
ctLog("initializer: .getAllRounds returns %s elements", this.getAllRounds().length);
/**
* @method
* @param {string} roundName - the name of the round you're interested in
* @return {object} round - a round
*/
this.getRound = function(roundName) {
var toreturn;
for (var ri = 0; ri < this.rounds.length; ri++) {
if (this.rounds[ri].name == roundName) {
toreturn = this.rounds[ri];
break;
}
}
return toreturn;
};
/**
* @method
* @return {object} round - the round corresponding to the active spreadsheet
*/
this.getActiveRound = function() {
ctLog(["captable.getActiveRound(): starting. activeRound = %s", this.activeRound], 9);
return this.getRound(this.activeRound);
};
ctLog("initializer: this.activeRound = %s", this.activeRound);
if (! this.getActiveRound()) {
// throw "cap table has no column named " + termsheet.getSheetName();
}
/**
* @method
* @return {Array<string>} column names - all the major columns in the cap table
*/
this.columnNames = function() {
for (var cn = 0; cn < this.rounds.length; cn++) {
ctLog("capTable.columnNames: column %s is named %s", cn, this.rounds[cn].name);
}
};
// for each major column we compute the pre/post, old/new investor values.
// we want to know:
// - which investors put in how much money for what securities in this round
// new_investors: { investorName: { shares, money, percentage } }
// - who the existing shareholders are before the round:
// old_investors: { investorName: { shares, money, percentage } }
// - how many total shares exist at the start of the round:
// shares_pre
// - how many shares of different types exist after the round
// by_security_type = { "Class F Shares" : { "Investor Name" : nnn, "TOTAL" : mmm }, ... }
// - how many shares of different types a given investor holds
// - TODO: keep a running total to carry forward from round to round. see issue 84
// - we define brand_new_investors as someone who has not been been an investor before. this is basically new_investors minus old_investors.
var totals = { shares_pre: 0,
money_pre: 0,
all_investors: {},
by_security_type: {},
ESOP: {},
};
for (var cn = 0; cn < this.rounds.length; cn++) {
var round = this.rounds[cn];
if (round.name == "TOTAL") { continue }
ctLog("capTable.new(): embroidering column %s", round.name);
// if only we had some sort of native deepcopy method... oh well.
round.old_investors = {}; // indexed by name
for (var ai in totals.all_investors) {
round.old_investors[ai] = new Holder(round, ai, readrows);
// handle the _orig_ first, then formatify to produce the actual attribute
for (var attr in totals.all_investors[ai]) {
if (! attr.match(/^_orig_/)) continue;
round.old_investors[ai][attr] = totals.all_investors[ai][attr];
}
for (var attr in totals.all_investors[ai]) {
if ( attr.match(/^_orig_/)) continue;
round.old_investors[ai][attr] = formatify_(totals.all_investors[ai]["_format_" + attr], totals.all_investors[ai]["_orig_" + attr], termsheet, attr);
}
}
// ctLog("capTable.new(): %s.old_investors = %s", round.name, round.old_investors);
totals.by_security_type[round.security_type] = totals.by_security_type[round.security_type] || {};
round.shares_pre = totals.shares_pre;
var new_shares = 0, new_money = 0;
for (var ni in round.new_investors) {
if (! round.new_investors[ni]._orig_shares && ! round.new_investors[ni]._orig_money) {
ctLog(["deleting new_investor %s from round %s because no money or shares; only attrs are %s",
ni, round.name, Object.keys(round.new_investors[ni])],9);
delete round.new_investors[ni];
continue;
}
// handle the _orig_ first, then formatify to produce the actual attribute
// TODO: turn this into a proper Holder.copy() method.
if (round.new_investors[ni]._orig_money) new_money += round.new_investors[ni]._orig_money;
if (round.new_investors[ni]._orig_shares) {
new_shares += round.new_investors[ni]._orig_shares;
totals.by_security_type[round.security_type][ni] = totals.by_security_type[round.security_type][ni] || 0; // js lacks autovivication, sigh
totals.by_security_type[round.security_type][ni] += round.new_investors[ni]._orig_shares;
}
// if it's not something directly measureable in shares we assume it's debt, which is measured in money
else if (round.new_investors[ni]._orig_money) {
totals.by_security_type[round.security_type][ni] = totals.by_security_type[round.security_type][ni] || 0; // js lacks autovivication, sigh
totals.by_security_type[round.security_type][ni] += round.new_investors[ni]._orig_money;
}
for (var attr in round.new_investors[ni]) {
if (round.new_investors[ni] == undefined) { continue } // sometimes an old investor doesn't re-up, so they're excused from action.
if (attr == "percentage") { continue } // percentages don't need to add
if (round.new_investors[ni][attr] == undefined) { continue } // money and shares do, but we don't always get new ones of those.
totals.all_investors[ni] = totals.all_investors[ni] || {}; // TODO: turn this into a proper Holder.copy() method.
totals.all_investors[ni][attr] = totals.all_investors[ni][attr] || 0;
if (attr.match(/^_orig_/)) {
totals.all_investors[ni][attr] += round.new_investors[ni][attr];
} else {
totals.all_investors[ni][attr] = round.new_investors[ni][attr];
}
}
}
round.by_security_type = {};
for (var bst in totals.by_security_type) {
round.by_security_type[bst] = { TOTAL: 0 };
for (var inv in totals.by_security_type[bst]) {
round.by_security_type[bst][inv] = totals.by_security_type[bst][inv];
round.by_security_type[bst].TOTAL += totals.by_security_type[bst][inv];
ctLog(["round %s: %s contributes %s %s to total, which is now %s",
round.name, inv, totals.by_security_type[bst][inv], bst, round.by_security_type[bst].TOTAL],8);
}
}
ctLog("round.by_security_type = %s", JSON.stringify(round.by_security_type));
// TODO: consider a company that has both Class F and Class F-NV shares. sigh.
// also, only unrestricted shares count toward their premptive rights so we need to compute those separately.
if (round.new_investors["ESOP"] != undefined && round.new_investors["ESOP"].shares) {
ctLog("ESOP: round %s has a new_investor ESOP with shares=%s", round.name, round.new_investors["ESOP"].shares);
round.ESOP = round.ESOP || new ESOP_(round.security_type, 0);
// TODO: if an ESOP was previously instantiated for the given security type, then reuse that object instead of doing a new instance.
ctLog(["ESOP: establishing ESOP object for round %s", round.name],7);
var seen_ESOP_investor = false;
for (var oi in round.ordered_investors) {
var inv = round.ordered_investors[oi];
ctLog(["ESOP: considering investor %s", inv],9);
if (round.new_investors[inv] == undefined
||
! round.new_investors[inv].shares
) {
ctLog(["ESOP: %s doesn't seem to be involved in the round; skipping.", inv],9);
continue;
}
if (inv == "ESOP") { seen_ESOP_investor = true;
var esopHolder = round.ESOP.createHolder(inv);
// TODO: set detail params for the holder so we can later compute their vesting schedule
ctLog(["ESOP: we have access to a bunch of role information for the esop holder."],8);
ctLog(["ESOP: but is that information available under round.new_investors?..."],8);
ctLog(["ESOP: ... or do we have to go look under data.parties to find f_restricted and f_unrestricted?"],8);
round.ESOP.holderGains(inv, round.new_investors[inv]._orig_shares);
// how to get back to the parties and role attributes from here?
// esopHolder
// .set(initial_f_unrestricted, readrows.roles2parties.new_investor[inv].f_unrestricted)
continue;
}
else if ( seen_ESOP_investor ) {
ctLog("ESOP: we take it that %s is a participant in the ESOP, because it appears below the ESOP line", inv);
round.ESOP.createHolder(inv);
round.ESOP.holderGains(inv, round.new_investors[inv].shares);
// TODO: readRows the ESOP round and load in the ESOP particulars so we can compute the restricted/unrestricted etc dynamically.
}
else {
ctLog("in constructing the ESOP object for round %s we ignore any rows above the ESOP line -- %s", round.name, inv);
}
//preliminary attempt at ESOP running total.
round.ESOPtotals = {};
for (var esp in totals.ESOP) {
round.ESOPtotals[esp] = { TOTAL: O};
for (var ike in totals.ESOP[esp]) {
round.ESOPtotals[esp][ike] = totals.ESOP[esp][ike];
round.ESOPtotals[esp].TOTAL += totals.ESOP[esp][ike];
}
}
// TODO: in future add a running total, similar to the rest of how we manage shares by type above.
// if we don't do this, then multiple columns which deal with ESOP will not do the right thing.
}
ctLog("capTable: created an ESOP object for round %s: %s", round.name, JSON.stringify(round.ESOP.holders));
}
// any new investor who is not already also an old investor
round.brand_new_investors = {};
for (var ni in round.new_investors) {
if (! round.new_investors[ni]._orig_shares && ! round.new_investors[ni]) continue;
if (round.old_investors[ni] != undefined) continue;
round.brand_new_investors[ni] = round.new_investors[ni];
// TODO: turn this into a proper Holder.copy() method.
}
["new_investors", "old_investors", "brand_new_investors"].map(function (itype) {
ctLog("capTable.new(%s): %s = %s", round.name, itype, Object.keys(round[itype]));
});
// ctLog("capTable.new(): we calculate that round \"%s\" has %s new shares", round.name, new_shares);
// ctLog("capTable.new(): the sheet says that we should have %s new shares", round.amount_raised.shares);
// TODO: we should probably raise a stink if those values are not the same.
// ctLog("capTable.new(): we calculate that round \"%s\" has %s new money", round.name, new_money);
// ctLog("capTable.new(): the sheet says that we should have %s new money", round.amount_raised.money);
}
// ctLog("capTable.new(): embroidered rounds to %s", this.rounds);
/**
* @method
* @return {undefined}
*/
this.rewireAllRounds = function(){
for (var cn = 0; cn < this.rounds.length; cn++) {
var round = this.rounds[cn];
round.rewire();
}
};
/**
* @member {object}
*/
this.byInvestorName = {}; // investorName to Investor object
// what does an Investor object look like?
// { name: someName,
// rounds: [ { name: roundName,
// price_per_share: whatever,
// shares: whatever,
// money: whatever,
// percentage: whatever,
// }, ...
// ]
/** all the investors
* @method
* @return {Array<object>} investors - ordered list of investor objects
*/
this.allInvestors = function() {
var toreturn = [];
// walk through each round and add the investor to toreturn
for (var cn = 0; cn < this.rounds.length; cn++) {
var round = this.rounds[cn];
if (round.name == "TOTAL") { continue; }
var new_investors = round.new_investors;
for (var investorName in new_investors) {
var investorInRound = new_investors[investorName];
var investor;
if (this.byInvestorName[investorName] == undefined) {
investor = this.byInvestorName[investorName] = { name: investorName };
toreturn.push(investor);
} else {
investor = this.byInvestorName[investorName];
}
if (investor.rounds == undefined) {
investor.rounds = [];
}
ctLog(["allInvestors: investorInRound %s in %s = %s", investorName, round.name, investorInRound],9);
var topush = {name: round.name };
if (round.price_per_share != undefined) { topush.price_per_share = round.price_per_share.shares }
topush.shares = investorInRound.shares; topush._orig_shares = investorInRound._orig_shares;
topush.money = investorInRound.money; topush._orig_money = investorInRound._orig_money;
topush.percentage = investorInRound.percentage;
investor.rounds.push(topush);
}
}
ctLog(["i have built allInvestors: %s", JSON.stringify(toreturn), 8]);
return toreturn;
};
/**
* @method
* @return [Object] holdings - a list
*/
this.investorHoldingsInRound_raw = function(investorName, round) {
ctLog([".investorHoldingsInRound_raw(%s,%s): starting", investorName, round == undefined ? "<undefined round>" : round.getName()], 9);
round = round || this.getActiveRound();
ctLog([".investorHoldingsInRound_raw: resolved round = %s", round == undefined ? "<undefined round>" : round.getName()], 9);
var pre = [];
for (var bst in round.by_security_type) {
ctLog(["investorHoldingsInRound_raw: trying to singularize shares to share: in bst=%s", bst], 9);
if (round.by_security_type[bst][investorName]) { pre.push([round.by_security_type[bst][investorName],
bst.replace(/(share)(s)/i,function(match,p1,p2){return p1})]
) }
}
return pre;
};
/**
* @method
* @return {String} holdings - "3 Ordinary Shares and 200 Class F Shares"
*/
this.investorHoldingsInRound = function(investorName, round) {
round = round || this.getActiveRound();
return round.inWords(this.investorHoldingsInRound_raw(investorName, round));
};
/** return new roles for imputation by readRows_.handleNewRoles()
* @method
*/
this.newRoles = function() {
ctLog([".newRoles(): starting. calling getActiveRound()"], 6);
var round = this.getActiveRound();
// all the old investors are given "shareholder" roles
// all the new investors are given "new_investor" roles.
// all the brand new investors are given "brand_new_investor" roles.
// sit_out_investors are those old investors who are not new_investors -- they are sitting out of the current round.
var toreturn = [];
if (! round) { return toreturn }
for (var ni in round.new_investors) {
if (ni == "ESOP" || // special case
round.new_investors[ni].money == undefined &&
round.new_investors[ni].shares == undefined
) continue;
var newRole = { relation:"new_investor", entityname:ni };
newRole.attrs = { new_commitment: round.new_investors[ni].money, num_new_shares: round.new_investors[ni].shares,
_orig_new_commitment: round.new_investors[ni]._orig_money, orig_num_new_shares: round.new_investors[ni]._orig_shares };
toreturn.push(newRole);
ctLog(["newRoles(%s): pushing from new_investors: %s", round.name, ni],8);
}
for (var ni in round.brand_new_investors) {
if (ni == "ESOP" || // special case
round.brand_new_investors[ni].money == undefined &&
round.brand_new_investors[ni].shares == undefined
) continue;
var newRole = { relation:"brand_new_investor", entityname:ni };
newRole.attrs = { new_commitment: round.brand_new_investors[ni].money, num_new_shares: round.brand_new_investors[ni].shares,
_orig_new_commitment: round.brand_new_investors[ni]._orig_money, orig_num_new_shares: round.brand_new_investors[ni]._orig_shares };
toreturn.push(newRole);
}
var tentative_shareholders = [];
for (var oi in round.old_investors) {
if (oi == "ESOP" || // special case
round.old_investors[oi].shares == 0 ||
round.old_investors[oi].money == undefined &&
round.old_investors[oi].shares == undefined
) continue;
var newRole = { relation:"shareholder", entityname:oi, attrs:{} };
tentative_shareholders.push(newRole);
}
// shareholders should exclude convertible noteholders.
ctLog(["newRoles(%s): role shareholder (before) = %s", round.name, tentative_shareholders.map(function(e) { return e.entityname })], 6);
var allInvestors_ = this.allInvestors();// side effect -- decorates investor with a .rounds attribute
var allInvestorsByName = {};
for (var ai_i in allInvestors_) {
allInvestorsByName[allInvestors_[ai_i].name] = allInvestors_[ai_i].rounds;
}
var chosen_shareholders = [];
for (var tsi in tentative_shareholders) {
var sh_name = tentative_shareholders[tsi].entityname;
if (sh_name == "ESOP") continue;
var has_actual_shares = false;
if (allInvestorsByName[sh_name].filter(function(deets) { return deets._orig_shares }).length) {
has_actual_shares = true;
chosen_shareholders.push(tentative_shareholders[tsi]);
}
}
toreturn = toreturn.concat(chosen_shareholders);
ctLog(["newRoles(%s): role shareholder (after) = %s", round.name, chosen_shareholders.map(function(e) { return e.entityname })], 6);
//
// sit_out_shareholders are those old investors who are not new_investors -- they are sitting out of the current round.
//
ctLog(["newRoles(%s): constructing sitout_shareholders.", round.name],8);
ctLog(["newRoles(%s): that should be old investors minus new investors, and also excluding anyone who's not got unrestricted at the time", round.name],8);
var sitout_old_names = chosen_shareholders.map(function(e) { return e.entityname });
ctLog(["newRoles(%s): sitout old investors = %s", round.name, sitout_old_names],8);
var sitout_new_names = toreturn.filter(function(tr){return tr.relation == "new_investor"})
.map(function(tr){return tr.entityname});
ctLog(["newRoles(%s): sitout new investors = %s", round.name, sitout_new_names],8);
if (sitout_new_names.length == 0) {
ctLog(["newRoles(%s): wtf. toreturn=%s", round.name, JSON.stringify(toreturn)],8);
}
var sitout_shareholders = chosen_shareholders
.filter(function(old){ return ( sitout_new_names.indexOf(old.entityname) < 0)} ) // not new_investor
.filter(function(old){
ctLog(["newRoles(%s): computing sitout shareholders: %s is of type %s",
round.name, old.entityname, round.old_investors[old.entityname].constructor.name, round.old_investors[old.entityname]]
,6);
return (round.old_investors[old.entityname].preemptiveShares() >= 1 ) } ) // has some kind of preemptive eligibility
.map (function(old){ return { relation:"sitout_shareholder", entityname:old.entityname, attrs:old.attrs } });
ctLog(["newRoles(%s): sitout shareholders = %s", round.name, sitout_shareholders.map(function(e){return e.entityname})],6);
toreturn = toreturn.concat(sitout_shareholders);
round.sitout_shareholders = {};
for (var siti in sitout_shareholders) {
round.sitout_shareholders[sitout_shareholders[siti].entityname] = round.old_investors[sitout_shareholders[siti].entityname];
}
//
// we want to distinguish voting_shareholders and nonvoting_shareholders.
// Some classes of shares are voting (ordinary, etc)
// Some classes of shares are specifically designated as nonvoting (nonvoting ordinary, F-NV)
//
for (var ni in round.old_investors) {
if (ni == "ESOP" || // special case
round.old_investors[ni].money == undefined &&
round.old_investors[ni].shares == undefined
) continue;
var pre = this.investorHoldingsInRound_raw(ni, round);
var security_classifications = pre.map(function(pp){
var num_securities = pp[0];
var security_type = pp[1];
if (security_type.match(/non-?voting|\bnv\b/i)) { return "nonvoting share" }
else if (security_type.match(/class f|esop/i)) { return "esop share" } // may be voting depending on vesting
else if (security_type.match(/share/i)) { return "voting share" }
else { return "nonshare" }
// XXX: it would be good to go to the original sheet and look at the ~security_essential~ term, but for now we reinvent the wheel. this violates DRY.
});
var newRoleRelation =
// If an investor (prior to the current round) owns voting shares,
// then they're a ~voting_shareholder~.
(security_classifications.indexOf("voting share") >= 0) ? "voting_shareholder" :
// If an investor (prior to the current round) owns shares,
// and all the shares they own are nonvoting,
// then they're a ~nonvoting_shareholder~.
(security_classifications.every(function(sc){return sc == "nonvoting share"})) ? "nonvoting_shareholder" :
// If an investor (prior to the current round) owns only esop shares,
// we look deeper into the Holder's OrigEntity object to compute the remaining unrestricted f.
(security_classifications.every(function(sc){return sc == "esop share"})) ? "esop_shareholder" :
// If an investor (prior to the current round) owns securities that are not shares,
// then they are neither a voting nor a nonvoting shareholder;
// they are a ~nonshare_investor~.
"nonshare_investor";
if (newRoleRelation == "esop_shareholder") {
// look deeper into the Holder's OrigEntity object to compute the remaining unrestricted f.
// can we reuse some existing method to do this?
if (round.old_investors[ni].preemptiveShares() > 0) {
ctLog(["established that esop shareholder %s has preemptive eligibility", ni],8);
newRoleRelation = "voting_shareholder";
}
else {
ctLog(["established that esop shareholder %s does not have preemptive eligibility", ni],8);
newRoleRelation = "nonvoting_shareholder";
}
}
var newRole = { relation: newRoleRelation,
entityname:ni,
attrs: {}
};
ctLog(["determined that %s, who has %s, is a %s", ni, this.investorHoldingsInRound(ni, round), newRoleRelation],8);
toreturn.push(newRole);
}
ctLog(["capTable.newRoles(): imputing %s roles: %s", toreturn.length, toreturn.map(function(nr){return nr.relation + ":" + nr.entityname})],7);
return toreturn;
};
};
/**
* a Round object
* @class
* @param {string} Round.name - the name of the round
* @param {object} Round.new_investors - dictionary of new investors who are participating in the round
* @param {Array} Round.ordered_investors - new investors, listed in order shown in the spreadsheet
* @param {sheet} Round.sheet - the google activeSheet()
*/
function Round(params) {
this.name = params.name;
this.new_investors = params.new_investors;
this.ordered_investors = params.ordered_investors;
this.sheet = params.sheet;
this.captable = params.captable;
};
Round.prototype.preemptivelyEligibleSecurities = function(holderName) {
var eligibleSecurities = Object.keys(this.by_security_type).filter(eligible_securities);
var that = this;
// how many non-class F shares do i have?
var myeligible = eligibleSecurities
.map(function(es){
ctLog(["preemptivelyEligibleSecurities: --- %s has %s %s securities, which are eligible",
holderName, that.by_security_type[es][holderName], es],8);
return that.by_security_type[es][holderName] || 0})
.reduce(function(a, b) { return a + b; }, 0);
ctLog(["preemptivelyEligibleSecurities: subtotal: %s has %s eligible securities", holderName, myeligible],8);
return myeligible;
}
/**
* @method
* @return {string} name - round name
*/
Round.prototype.getName = function(){
return this.name
};
/**
* @method
* @return {string} name - $N worth of convertible notes and N ordinary shares
*/
// holdings is an array of arrays: [ [ N, round.security_type ], ... ]
Round.prototype.inWords = function(holdings) {
var that = this;
ctLog(["inWords(%s): starting. this round = %s", holdings, that.getName()],9);
return commaAnd(holdings.map(function(bst_count){
ctLog(["inWords(): bst_count = %s", bst_count],9);
if (bst_count[1].match(/note|debt|kiss|safe/i)) {
return asCurrency_(that.getCurrency(), bst_count[0]) + " of " + plural(2, bst_count[1]);
} else {
return digitCommas_(bst_count[0],0) + " " + plural(bst_count[0], bst_count[1]);
}
}));
};
/**
* @method
* @return {sheet} the sheet corresponding to the round -- the tab with the same name
*/
Round.prototype.getTermSheet = function() {
ctLog("round.getTermSheet: returning %s", this.sheet);
return this.sheet;
};
/**
* @method
* @param {string} category
* @return {Array} Cell - [moneyCell, sharesCell, percentageCell]
*/
Round.prototype.getCategoryCellRange = function(category){
var roundColumn = this.captablesheet.getRoundColumnByName(this.name);
var categoryRow;
try {
categoryRow = this.captablesheet.getCategoryRowCaptable(category);
} catch (e) {
throw("you need to be on a Cap Table tab to run this menu item -- " + e);
};
var categoryCell;
try {
categoryCell = [this.captablesheet.getCapsheet().getRange(categoryRow, roundColumn), this.captablesheet.getCapsheet().getRange(categoryRow, roundColumn+1), this.captablesheet.getCapsheet().getRange(categoryRow, roundColumn+2)];
} catch (e) {
throw("unable to getRange(" + categoryRow + ", " + roundColumn + ") -- " + e);
};
};
/**
* @method
* @param {string} category
* @return {Array} reference -- A1 Notation for each cell [moneyA1, sharesA1, percentageA1]
*/
Round.prototype.getCategoryCellA1Notation = function(category){
var range = this.getCategoryCellRange(category);
var reference = []
for (var i; i < range.length; i++){
reference[i] = range[i].getA1Notation();
}
return reference;
};
/**
* @method
* @param {string} category
* @return {Array} value - [moneyValue, sharesValue, percentageValue]
*/
Round.prototype.getCategoryCellValue = function(category){
var range = this.getCategoryCellRange(category);
var value = []
for (var i; i < range.length; i++){
reference[i] = range[i].getValue();
}
return value;
};
/**
* @method
* @return {object} toreturn - previous round
*/
Round.prototype.getPreviousRound = function(){
ctLog("getPreviousRound trying to return round previous to %s, from %s", this.getName(), this.captable.getUrl());
var roundList = this.captable.getAllRounds();
var toreturn;
for (var ri = 0; ri < roundList.length; ri++) {
if (roundList[ri].name == this.getName()) {
toreturn = roundList[ri - 1];
break;
}
}
if (toreturn)
ctLog("getPreviousRound returning round named %s", toreturn.getName());
return toreturn;
};
/**
* @method
* @return {undefinied} resets pre/post money/shares
*/
Round.prototype.rewire = function(){
if (this.getPreviousRound() == "undefined"){
postRange[0].setFormula("=" + this.getAmountRaisedCell()[0]);
postRange[1].setFormula("=" + this.getAmountRaisedCell()[1]);
postRange[2].setValue("");
//percentage should be an empty cell
//set all premoney cells to empty cell
premoneyRange[0].setValue("");
premoneyRange[1].setValue("");
premoneyRange[2].setValue("");
}
else{
postA1Notation = this.getCategoryCellA1Notation("post");
prevRoundPostA1Notation = this.getPreviousRound().getCategoryCellA1Notation("post");
premoneyA1Notation = this.getCategoryCellA1Notation("pre-money");
amountraisedA1Notation = this.getCategoryCellA1Notation("amount raised");
postRange[0].setFormula("=" + amountraisedA1Notation[0] + "+" + prevRoundPostA1Notation[0]);
postRange[1].setFormula("=" + amountraisedA1Notation[1] + "+" + prevRoundPostA1Notation[1]);
//percentage should be an empty cell
//postRange[2].setFormula("=" + this.getAmountRaisedCell()[2] + "+" + this.previousRound().getpostRange()[2]);
premoneyRange[0].setFormula("=" + prevRoundPostA1Notation[0]);
premoneyRange[1].setFormula("=" + prevRoundPostA1Notation[1]);
premoneyRange[2].setValue("");
}
};
// this is our "DOM":
// a captable object contains an array of Round objects
// a Round object contains an array of Shareholders and an array of NewInvestor objects
// Shareholder and NewInvestors belong to the same class, "Investor"
// an Investor object contains a dictionary of share class names to money/shares
// or whatever ...
/**
* getNewInvestors
* @method
* @return {Array} array of Investor objects (who participate in this round)
* see also getNewIssues()
*/
/**
* getOldInvestors
* @method
* @return {Object}.TOTAL.shares - number of shares, as a string, prior to the round
* @return {Object}.TOTAL._orig_shares - number of shares, as a number, prior to the round
* @return {Object}.old_investors[investorName].shares - number of shares, as a string
* @return {Object}.old_investors[investorName]._orig_shares - number of shares, as a number
* @return {Object}.old_investors[investorName].money - price paid, as a string with currency in front
* @return {Object}.old_investors[investorName]._orig_money - price paid, as a number
*/
Round.prototype.getOldInvestors = function(){
var toreturn = { TOTAL: { _orig_shares: 0, _orig_money: 0 },
holders: { },
};
var currency;
ctLog("Round.getOldInvestors(%s): this.old_investors has keys %s", this.name, Object.keys(this.old_investors));
for (var oi in this.old_investors) {
if (oi == "ESOP") { continue }
toreturn.holders[oi] = this.old_investors[oi];
currency = currency || this.old_investors[oi]._format_money || this.getCurrency();
}
if (currency == undefined) { ctLog("Round.getOldInvestors(%s): amount_raised = %s", this.name, this.amount_raised);
ctLog("Round.getOldInvestors(%s): post = %s", this.name, this.post);
ctLog("Round.getOldInvestors(%s): currency is %s, returning null", this.name, currency);
return null }
ctLog("Round.getOldInvestors(%s): TOTAL._orig_money = %s", this.name, toreturn.TOTAL._orig_money);
toreturn.TOTAL.money = asCurrency_(currency, toreturn.TOTAL._orig_money);
toreturn.TOTAL.shares = formatify_("#,##0", toreturn.TOTAL._orig_shares);
return toreturn;
};
/**
* getPreMoney
* @method
* @return {Object} pre-money object of money/shares
*/
/**
* getPostShares
* @method
* @return {Number} post-money number of shares
*/
/**
* getPostMoney
* @method
* @return {Number} post-money valuation
*/
/**
* getPostMoney
* @method
* @return {Number} post-money valuation
*/
/**
* getSecurityType
* @method
* @return {String} type of security this round deals with
*/
Round.prototype.getSecurityType = function(){
return this.security_type;
}
Round.prototype.getCurrency = function(){
ctLog(["getCurrency: this.amount_raised = %s", this.amount_raised],7);
return ((this.amount_raised && this.amount_raised._format_money) ? this.amount_raised._format_money : this.post._format_money);
}
/**
* @method
* @return {Object}.TOTAL.shares - number of shares, as a string
* @return {Object}.TOTAL._orig_shares - number of shares, as a number
* @return {Object}.new_investors[investorName].shares - number of shares, as a string
* @return {Object}.new_investors[investorName]._orig_shares - number of shares, as a number
* @return {Object}.new_investors[investorName].money - price paid, as a string with currency in front
* @return {Object}.new_investors[investorName]._orig_money - price paid, as a number
*/
Round.prototype.getNewIssues = function(){
var toreturn = { TOTAL: { _orig_shares: 0, _orig_money: 0 },
holders: { },
};
var currency;
ctLog("Round.getNewIssues(%s): this.new_investors has keys %s", this.name, Object.keys(this.new_investors));
for (var ni in this.new_investors) {
if (ni == "ESOP") { continue }
toreturn.holders[ni] = this.new_investors[ni];
var number_of_things = undefined;
if (this.new_investors[ni]._orig_shares > 0) {
number_of_things = this.new_investors[ni]._orig_shares;
toreturn.TOTAL._orig_shares = toreturn.TOTAL._orig_shares + this.new_investors[ni]._orig_shares;
}
if (this.new_investors[ni]._orig_money > 0) {
number_of_things = number_of_things || this.new_investors[ni]._orig_money;
toreturn.TOTAL._orig_money = toreturn.TOTAL._orig_money + this.new_investors[ni]._orig_money;
}
currency = currency || this.new_investors[ni]._format_money || this.getCurrency();
ctLog(["Round.getNewIssues: calling inWords()"],9);
this.new_investors[ni]._inWords = this.inWords([ [ number_of_things, this.security_type ] ]);
ctLog(["Round.getNewIssues: back from inWords()"],9);
}
if (currency == undefined) { ctLog("Round.getNewIssues(%s): amount_raised = %s", this.name, this.amount_raised);
ctLog("Round.getNewIssues(%s): post = %s", this.name, this.post);
ctLog("Round.getNewIssues(%s): currency is %s, returning null", this.name, currency);
return null }
ctLog("Round.getNewIssues(%s): TOTAL._orig_money = %s", this.name, toreturn.TOTAL._orig_money);
toreturn.TOTAL.money = asCurrency_(currency, toreturn.TOTAL._orig_money);
toreturn.TOTAL.shares = formatify_("#,##0", toreturn.TOTAL._orig_shares);
return toreturn;
};
/**
* @method
* @return {Object}.TOTAL.shares - number of shares, as a string
* @return {Object}.TOTAL._orig_shares - number of shares, as a number
* @return {Object}.holders[investorName].shares - number of shares, as a string
* @return {Object}.holders[investorName]._orig_shares - number of shares, as a number
* @return {Object}.holders[investorName].money - price paid, as a string with currency in front
* @return {Object}.holders[investorName]._orig_money - price paid, as a number
*/
Round.prototype.getRedemptions = function(){
var toreturn = { TOTAL: { _orig_shares: 0, _orig_money: 0 },
holders: { },
};
var currency;
for (var ni in this.new_investors) {
if (this.new_investors[ni]._orig_shares < 0) {
if (ni == "ESOP") { continue }
toreturn.holders[ni] = this.new_investors[ni];
toreturn.TOTAL._orig_shares = toreturn.TOTAL._orig_shares - this.new_investors[ni]._orig_shares;
toreturn.TOTAL._orig_money = toreturn.TOTAL._orig_money - this.new_investors[ni]._orig_money;
}
currency = currency || this.new_investors[ni]._format_money || this.getCurrency();
}
if (currency == undefined) { return null }
toreturn.TOTAL.money = asCurrency_(currency, toreturn.TOTAL._orig_money);
toreturn.TOTAL.shares = formatify_("#,##0", toreturn.TOTAL._orig_shares);
return toreturn;
};
// parseCaptable
// previously known as "Show Me The Money!"
// returns an array (all the rounds) of hashes (each round) with hashes (each investor).
// ECMAscript 6 specifies that hashes maintain key ordering, so i did that at first,
// but some random guy on the Internet said that hashes are unordered, so I used an array lor.
// [
// {
// security_type: string,
// approximate_date: Date,
// pre_money: NNN,
// price_per_share: P,
// discount: D,
// new_investors:
// { investorName: {
// shares: N,
// money: D,
// percentage: P,
// },
// investorName: {
// shares: N,
// money: D,
// percentage: P,
// },
// },
// amount_raised: NNN,
// shares_post: NNN,
// post_money: NNN,
// },
// ... // another round
// ... // another round
// { name: "TOTAL", ... }
// ]
capTable_.prototype.parseCaptable = function() {
var sheet = this.captablesheet;
ctLog("parseCaptable: running on sheet %s", sheet.getSheetName());
var captableRounds = [];
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();
var formulas = rows.getFormulas();
var formats = rows.getNumberFormats();
var display = rows.getDisplayValues();
var section = null;
var majorToRound = {};
var majorByName = {}; // round_name: column_index
var majorByNum = {}; // column_index: round_name
var minorByName = {}; // money / shares / percentage is in column N
var minorByNum = {}; // column N is a money column, or whatever
for (var i = 0; i <= numRows - 1; i++) {
var row = values[i];
if (row[0] == "CAP TABLE") { section = row[0]; continue }
if (section == "CAP TABLE") {
// INITIALIZE A NEW ROUND
// each major column is on a 3-column repeat.
var asvar0 = asvar_(row[0]);
// asvar_("my Cool String (draft)") is "my_cool_string_draft".
// other people might do myCoolStringDraft, but I don't like camelCase.
if (row[0] == "round name") {
for (var j = 1; j<= row.length; j++) {
if (! row[j]) { continue }