-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathBuy.pm
231 lines (178 loc) · 5.3 KB
/
Buy.pm
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
############A Buy Trade
package Buy;
# Copyright 2012 Rareventure, LLC
# Copyright 2015,2016 Rareventure, LLC
#
# This file is part of Bitcoin Tax Calculator
# Bitcoin Tax Calculator is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# Bitcoin Tax Calculator is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with Bitcoin Tax Calculator. If not, see <http://www.gnu.org/licenses/>.
#
# If you make modifications to this software that you feel
# increases it usefulness for the rest of the community, please
# email the changes, enhancements, bug fixes as well as any and
# all ideas to me. This software is going to be maintained and
# enhanced as deemed necessary by the community.
#
# Tim Engler (engler@gmail.com)
use Trade;
@ISA = qw(Trade);
sub new
{
my ($class, $date, $shares, $price, $symbol, $refs) = @_;
$self = Trade::new($class, $date, $shares, $price, $symbol, $refs);
$self->{gets_basis} = 0;
$self;
}
sub type
{
"buy";
}
sub split
{
#sellCause may be undef, which means this call is not being split because its attached to a sell
my ($self, $trades, $newShares, $sellCause, $splitSell) = @_;
if((defined $sellCause) && (defined $self->{sell}) && $self->{sell} ne $sellCause)
{
die "why another sell cause then our sell?";
}
if((defined $sellCause) && $self ne $sellCause->{buy})
{
die "why sell cause doesn't mark us as the buy?";
}
if(!(defined $sellCause))
{
if($self->{sell})
{
die "Always split from the top of the chain";
}
}
my $otherShares = $self->{shares} - $newShares;
my $newPrice = $self->{price} * $newShares / $self->{shares};
my $otherPrice = $self->{price} * $otherShares / $self->{shares};
#create a new buy split off from this one. No charge for this buy, and give it the shares not allocated
my $splitBuy = new Buy($self->{date}, $otherShares, $otherPrice,
$self->{symbol}, $self->{refs});
if(defined $splitSell)
{
if(defined $splitSell->{buy})
{
die "trying to replace the buy?";
}
if($splitSell->{shares} != $splitBuy->{shares})
{
die "Split sell has differing shares from split buy";
}
#if there is a splitSell, we use it
$splitBuy->{sell} = $splitSell;
$splitSell->{buy} = $splitBuy;
}
$self->{shares} = $newShares;
$self->{price} = $newPrice;
#insert the new buy right after us
$trades->insertAfter($splitBuy, $self);
#now split the washSell recursively, naming us and our newly defined split cousin as the cause
if((defined $self->{washSell}) && ((!defined $cause) || $cause != $self->{washSell}))
{
$self->{washSell}->split($trades, $newShares, $self, $splitBuy);
}
return $splitBuy;
}
#allocates the shares to the given sell as its buy. If there are still some shares left, buy must split itself into two
#purchases, one allocated and one not
sub markBuyForSell
{
my ($self, $sell) = @_;
#if we weren't able to mark all the shares of the sell
if($sell->{shares} != $self->{shares})
{
die "cannot mark buy for sell if number of shares differ"
}
if(defined $self->{sell})
{
die "Trying to replace the sell?";
}
if(defined $sell->{buy})
{
die "Trying to replace the buy for the sell?";
}
$self->{sell} = $sell;
$sell->{buy} = $self;
}
#returns cost and any backup from wash sales
sub getBasis
{
my ($self) = @_;
my $basis = $self->{price};
my $washSale = $self->{washSell};
#if this is a wash buy
if(defined $washSale)
{
#add the loss from the wash sale
$basis -= $washSale->getGain();
}
return $basis;
}
sub markAsWash
{
my ($self, $sell) = @_;
if($self->{shares} != $sell->{shares})
{
die "Buy must have same number of shares as sell";
}
die if $self->{washSell};
#mark ourselves as a wash
$self->{washSell} = $sell;
$sell->{washBuy} = $self;
if($sell->{buy} eq $self)
{
die "Trying to create a loop?";
}
}
sub isWash
{
my ($self) = @_;
return 1 if defined $self->{washSell};
return 0;
}
sub isBuy
{
my ($self, $sell) = @_;
return 1 if $sell == $self->{sell};
return 0;
}
sub toWashString
{
my ($self) = @_;
if($self->isWash)
{
return &main::convertDaysToText($self->{washSell}->{date})."\t".main::format_amt($self->{washSell}->{shares});
}
return "";
}
sub toIRSString
{
return "";
}
#checks if sell date is more than a year past this buy date
sub isLongTermToDate
{
my ($self,$sell_date) = @_;
my $buy_date = main::convertDaysToText($self->{date});
my ($byear,$bmonth,$bday) = $buy_date =~ /^(\d{4})-(\d\d)-(\d\d)$/ or die;
my ($syear,$smonth,$sday) = $sell_date =~ /^(\d{4})-(\d\d)-(\d\d)$/ or die;
return 1 if($byear <= $syear - 2);
return 0 if($byear >= $syear);
return 1 if($bmonth < $smonth);
return 0 if($bmonth > $smonth);
return 1 if($bday < $sday);
return 0;
}
1;