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
|
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
<script:module xmlns:script="http://openoffice.org/2000/script" script:name="Depot" script:language="StarBasic">Option Explicit
Sub Initialize(Optional bChooseMarketPlace as Boolean)
Dim bEnableHistory as Boolean
GlobalScope.BasicLibraries.LoadLibrary("Tools")
' oMarketModel = GetControlModel(oDocument.Sheets(0), "CmdHistory")
' bEnableHistory = oMarketModel.Enabled
ToggleWindow(False)
Today = Date()
bDebugmode = False
oDocument = ThisComponent
oController = oDocument.GetCurrentController
oSheets = oDocument.Sheets
oFirstSheet = oSheets(0)
oMovementSheet = oSheets(1)
oBankSheet = oSheets(2)
oDocFormats = oDocument.NumberFormats
oNumberFormatter = CreateUnoService("com.sun.star.util.NumberFormatter")
oNumberFormatter.AttachNumberFormatsSupplier(oDocument)
oDocLocale = oDocument.CharLocale
sDocLanguage = oDocLocale.Language
sDocCountry = oDocLocale.Country
LoadLanguage()
ToggleWindow(True)
' oMarketModel.Enabled = bEnableHistory
If Not IsMissing(bChooseMarketPlace) Then
If bChoosemarketPlace Then
ChooseMarket()
End If
Else
ChooseMarket()
End If
If Not IsMissing(bChooseMarketPlace) Then
If bChooseMarketPlace Then
oMarketModel.Enabled = bEnableMarket
oInternetModel.Enabled = bEnableInternet
End If
End If
End Sub
Sub Buy()
Initialize(True)
FillListbox(DlgTransaction.GetControl("lstBuyStocks"), TransactTitle(SBDIALOGBUY), False)
SetupTransactionControls(SBDIALOGBUY)
EnableTransactionControls(False)
DlgTransaction.Execute()
End Sub
Sub Sell()
Initialize(True)
If FillListbox(DlgTransaction.GetControl("lstSellStocks"), TransactTitle(SBDIALOGSELL), True) Then
SetupTransactionControls(SBDIALOGSELL)
EnableTransactionControls(False)
DlgTransaction.Execute()
End If
End Sub
Sub Reset()
Dim TransactionCount as Integer
Dim StockCount, iStartRow, i as Integer
Dim oRows, oRange as Object
Dim StockName as String
Initialize(True)
' Delete transactions and reset overview
If MsgBox(sMsgDeleteAll, SBMSGYESNO+SBMSGQUESTION+SBMSGDEFAULTBTN2, sMsgAuthorization) = 6 Then
' Assumption: If and only if there is an overview, then there are transactions, too
UnprotectSheets(oSheets)
StockCount = GetStocksCount(iStartRow)
For i = 1 To StockCount
StockName = oFirstSheet.GetCellbyPosition(SBCOLUMNNAME1, iStartRow + i).String
If oSheets.HasbyName(StockName) Then
oSheets.RemoveByName(StockName)
End If
Next
oDocument.AddActionLock
RemoveStockRows(oFirstSheet, iStartRow + 1, StockCount)
TransactionCount = GetTransactionCount(iStartRow)
RemoveStockRows(oMovementSheet, iStartRow + 2, TransactionCount)
ProtectSheets(oSheets)
oDocument.RemoveActionLock
End If
End Sub
Sub TransactionOk
Dim Sold as Long
Dim RestQuantity, Value, PartialValue, Profit
Dim iNewRow as Integer, iRow as Integer
Dim iStockRow as Long, iRestQuantity as Long
Dim oNameCell as Object
Dim CellStockName as String, SelStockName as String
Dim CurRate as Double
Dim TransactDate as Date
Dim LocStockName as String
' Check for rate entered
If TransactModel.txtRate.Value = 0 Then
If TransactModel.Step = SBDIALOGBUY Then
If MsgBox(sMsgFreeStock, SBMSGYESNO+SBMSGQUESTION, sMsgConfirm)=7 Then
Exit Sub
End If
Else
If MsgBox(sMsgTotalLoss, SBMSGYESNO+SBMSGQUESTION, sMsgConfirm)=7 Then
Exit Sub
End If
End If
End If
CurRate = TransactModel.txtRate.Value
TransactDate = CDateFromISO(TransactModel.txtDate.Date)
DlgTransaction.EndExecute()
UnprotectSheets(oSheets)
iNewRow = DuplicateRow(oMovementSheet, "HiddenRow3")
If TransactModel.Step = SBDIALOGBUY Then
CellStockName = TransactModel.lstBuyStocks.Text
If Instr(1,CellStockName,"$") <> 0 Then
CellStockName = "'" & CellStockName & "'"
End If
oMovementSheet.GetCellByPosition(SBCOLUMNNAME2, iNewRow).String = CellStockName
oMovementSheet.GetCellByPosition(SBCOLUMNQUANTITY2, iNewRow).Value = TransactModel.txtQuantity.Value
Else
CellStockName = DlgTransaction.GetControl("lstSellStocks").GetSelectedItem()
oMovementSheet.GetCellByPosition(SBCOLUMNNAME2, iNewRow).String = CellStockName
oMovementSheet.GetCellByPosition(SBCOLUMNQUANTITY2, iNewRow).Value = -TransactModel.txtQuantity.Value
End If
oMovementSheet.GetCellByPosition(SBCOLUMNDATE2, iNewRow).Value = CDateFromISO(TransactModel.txtDate.Date)
oMovementSheet.GetCellByPosition(SBCOLUMNRATE2, iNewRow).Value = TransactModel.txtRate.Value
oMovementSheet.GetCellByPosition(SBCOLUMNPROVPERCENT2, iNewRow).Value = TransactModel.txtCommission.EffectiveValue
oMovementSheet.GetCellByPosition(SBCOLUMNPROVMIN2, iNewRow).Value = TransactModel.txtMinimum.Value
oMovementSheet.GetCellByPosition(SBCOLUMNPROVFIX2, iNewRow).Value = TransactModel.txtFix.Value
' Buy stocks: Update overview for new stocks
If TransactModel.Step = SBDIALOGBUY Then
iStockRow = GetStockRowIndex(CellStockName)
If iStockRow = -1 Then
iNewRow = DuplicateRow(oFirstSheet, "HiddenRow2")
oFirstSheet.GetCellByPosition(SBCOLUMNNAME1, iNewRow).String = CellStockName
oFirstSheet.GetCellByPosition(SBCOLUMNID1, iNewRow).String = TransactModel.txtStockID.Text
iStockRow = GetStockRowIndex(CellStockName)
End If
' Sell stocks: Get transaction value, then update Transaction sheet
ElseIf TransactModel.Step = SBDIALOGSELL Then
Profit = oMovementSheet.GetCellByPosition(SBCOLUMNPROCEEDS2, iNewRow).Value
Value = Profit
Sold = TransactModel.txtQuantity.Value
SelStockName = DlgTransaction.GetControl("lstSellStocks").GetSelectedItem()
' Go to first name
If TransactMode = FIFO Then
iRow = SBROWFIRSTTRANSACT2
Else
iRow = iNewRow-1
End If
' Check that no transaction after split date exists else cancel split
Do While Sold > 0
oNameCell = oMovementSheet.GetCellByPosition(SBCOLUMNNAME2, iRow)
CellStockName = oNameCell.String
If CellStockName = SelStockName Then
' Update transactions: Note quantity sold
RestQuantity = oMovementSheet.GetCellByPosition(SBCOLUMNQTYREST2, iRow).Value
' If there still is a rest left ...
If RestQuantity > 0 Then
If RestQuantity < Sold Then
' Recalculate profit of new transaction
Profit = Profit - oMovementSheet.GetCellByPosition(SBCOLUMNPRCREST2, iRow).Value
AddValueToCellContent(SBCOLUMNQTYSOLD2, iRow, RestQuantity)
PartialValue = RestQuantity / Sold * Value
AddValueToCellContent(SBCOLUMNREALPROC2, iRow, PartialValue)
Sold = Sold - RestQuantity
Value = Value - PartialValue
Else
' Recalculate profit of neTransactModel.lstBuyStocks.Textw transaction
PartialValue = oMovementSheet.GetCellByPosition(SBCOLUMNPRCREST2, iRow).Value
Profit = Profit - PartialValue/RestQuantity * Sold
' Update sold shares cell
AddValueToCellContent(SBCOLUMNQTYSOLD2, iRow, Sold)
' Update sales turnover cell
AddValueToCellContent(SBCOLUMNREALPROC2, iRow, Value)
' Update variables for rest of transaction
Sold = 0
Value = 0
End If
End If
End If
iRow = iRow + TransactMode
Loop
oMovementSheet.GetCellByPosition(SBCOLUMNREALPROFIT2,iNewRow).Value = Profit
iStockRow = GetStockRowIndex(SelStockName)
iRestQuantity = oFirstSheet.GetCellbyPosition(SBCOLUMNQUANTITY1, iStockRow).Value
' If iRestQuantity = 0 Then
' If oSheets.HasbyName(SelStockName) Then
' oSheets.RemoveByName(SelStockName)
' End If
' Else
' End If
End If
InsertCurrentValue(CurRate, iStockRow,TransactDate)
ProtectSheets(oSheets)
End Sub
Sub SelectStockname(aEvent as Object)
Dim iCurRow as Integer
Dim CurStockName as String
With TransactModel
' Find row with stock name
If TransactModel.Step = SBDIALOGBUY Then
CurStockName = .lstBuyStocks.Text
iCurRow = GetStockRowIndex(CurStockName)
.txtQuantity.ValueMax = 10000000
Else
Dim ListBoxList() as String
ListBoxList() = GetSelectedListboxItems(aEvent.Source.getModel())
CurStockName = ListBoxList(0)
' CurStockName = DlgTransaction.GetControl(aEvent.Source.getModel.Name).GetSelectedItem()
iCurRow = GetStockRowIndex(CurStockName)
Dim fdouble as Double
fdouble = oFirstSheet.GetCellByPosition(SBCOLUMNQUANTITY1, iCurRow).Value
.txtQuantity.Value = fdouble
.txtQuantity.ValueMax = oFirstSheet.GetCellByPosition(SBCOLUMNQUANTITY1, iCurRow).Value
.txtRate.Value = oFirstSheet.GetCellbyPosition(SBCOLUMNRATE1, iCurRow).Value
End If
.txtStockID.Enabled = .Step = SBDIALOGBUY
.lblStockID.Enabled = .Step = SBDIALOGBUY
' Default settings for quantity and rate
.txtStockID.Text = GetStockID(CurStockName, iCurRow)
End With
EnableTransactionControls(CurStockName <> "")
TransactModel.cmdGoOn.DefaultButton = True
End Sub
Sub HandleStocks(Mode as Integer, oDialog as Object)
Dim DividendPerShare, DividendTotal, RestQuantity, OldValue
Dim SelStockName, CellStockName as String
Dim oNameCell as Object, oDateCell as Object
Dim iRow as Integer
Dim oDividendCell as Object
Dim Amount
Dim OldNumber, NewNumber as Integer
Dim NoteText as String
Dim TotalStocksCount as Long
Dim oModel as Object
oDocument.AddActionLock
oDialog.EndExecute()
oModel = oDialog.Model
SelStockName = DlgStockRates.GetControl("lstStockNames").GetSelectedItem()
Select Case Mode
Case HANDLEDIVIDEND
Dim bTakeTotal as Boolean
' Update transactions: Enter dividend paid for all Buy transactions not sold completely
bTakeTotal = oModel.optTotal.State = 1
If bTakeTotal Then
DividendTotal = oModel.txtDividend.Value
iRow = GetStockRowIndex(SelStockName)
TotalStocksCount = oFirstSheet.GetCellByPosition(SBCOLUMNQUANTITY1,iRow).Value
DividendPerShare = DividendTotal/TotalStocksCount
Else
DividendPerShare = oModel.txtDividend.Value
End If
Case HANDLESPLIT
' Store entered values in variables
OldNumber = oModel.txtOldRate.Value
NewNumber = oModel.txtNewRate.Value
SplitDate = CDateFromISO(oModel.txtDate.Date)
iRow = SBROWFIRSTTRANSACT2
NoteText = cSplit & SplitDate & ", " & oModel.txtOldRate.Value & oModel.lblColon.Label & oModel.txtNewRate.Value
Do
oNameCell = oMovementSheet.GetCellByPosition(SBCOLUMNNAME2, iRow)
CellStockName = oNameCell.String
If CellStockName = SelStockName Then
oDateCell = oMovementSheet.GetCellByPosition(SBCOLUMNDATE2, iRow)
If oDateCell.Value >= SplitDate Then
MsgBox sMsgWrongExchangeDate, SBMSGOK + SBMSGSTOP, sMsgError
Exit Sub
End If
End If
iRow = iRow + 1
Loop Until CellStockName = ""
End Select
iRow = SBROWFIRSTTRANSACT2
UnprotectSheets(oSheets)
Do
oNameCell = oMovementSheet.GetCellByPosition(SBCOLUMNNAME2, iRow)
CellStockName = oNameCell.String
If CellStockName = SelStockName Then
Select Case Mode
Case HANDLEDIVIDEND
RestQuantity = oMovementSheet.GetCellByPosition(SBCOLUMNQTYREST2, iRow).Value
If RestQuantity > 0 Then
oDividendCell = oMovementSheet.GetCellByPosition(SBCOLUMNDIVIDEND2, iRow)
OldValue = oDividendCell.Value
oDividendCell.Value = OldValue + RestQuantity * DividendPerShare
End If
Case HANDLESPLIT
oDateCell = oMovementSheet.GetCellByPosition(SBCOLUMNDATE2, iRow)
SplitCellValue(oMovementSheet, NewNumber, OldNumber, SBCOLUMNQUANTITY2, iRow, NoteText)
SplitCellValue(oMovementSheet, OldNumber, NewNumber, SBCOLUMNRATE2, iRow, "")
SplitCellValue(oMovementSheet, NewNumber, OldNumber, SBCOLUMNQTYSOLD2, iRow, "")
End Select
End If
iRow = iRow + 1
Loop Until CellStockName = ""
If Mode = HANDLESPLIT Then
CalculateChartafterSplit(SelStockName, NewNumber, OldNumber, NoteText, SplitDate)
End If
oDocument.CalculateAll()
ProtectSheets(oSheets)
oDocument.RemoveActionLock
End Sub
Sub CancelStockRate()
DlgStockRates.EndExecute()
End Sub
Sub CancelTransaction()
DlgTransaction.EndExecute()
End Sub
Sub CommitStockRate()
Dim CurStep as Integer
CurStep = StockRatesModel.Step
Select Case CurStep
Case 1
' Check for quantity entered
If StockRatesModel.txtDividend.Value = 0 Then
MsgBox sMsgNoDividend, SBMSGSTOP+SBMSGSTOP, sMsgError
Exit Sub
End If
HandleStocks(HANDLEDIVIDEND, DlgStockRates)
Case 2
HandleStocks(HANDLESPLIT, DlgStockRates)
Case 3
InsertCompanyHistory()
End Select
End Sub
Sub EnableTransactionControls(bEnable as Boolean)
With TransactModel
.lblQuantity.Enabled = bEnable
.txtQuantity.Enabled = bEnable
.lblRate.Enabled = bEnable
.txtRate.Enabled = bEnable
.lblDate.Enabled = bEnable
.txtDate.Enabled = bEnable
.lblCommission.Enabled = bEnable
.txtCommission.Enabled = bEnable
.lblMinimum.Enabled = bEnable
.txtMinimum.Enabled = bEnable
.lblFix.Enabled = bEnable
.txtFix.Enabled = bEnable
If TransactModel.Step = SBDIALOGSELL Then
.cmdGoOn.Enabled = Ubound(TransactModel.lstSellStocks.SelectedItems()) > -1
DlgTransaction.GetControl("lstSellStocks").SetFocus()
Else
.cmdGoOn.Enabled = TransactModel.lstBuyStocks.Text <> ""
DlgTransaction.GetControl("lstBuyStocks").SetFocus()
End If
If bEnable Then
TransactModel.cmdGoOn.DefaultButton = True
End If
End With
End Sub
Sub SetupTransactionControls(CurStep as Integer)
DlgReference = DlgTransaction
With TransactModel
.txtDate.Date = CDateToISO(Date())
.txtDate.DateMax = CDateToISO(Date())
.txtStockID.Enabled = False
.lblStockID.Enabled = False
.lblStockID.Label = sCurStockIDLabel
.txtRate.CurrencySymbol = sCurCurrency
.txtFix.CurrencySymbol = sCurCurrency
.Step = CurStep
End With
DlgTransaction.Title = TransactTitle(CurStep)
CellValuetoControl(oBankSheet, TransactModel.txtCommission, "ProvisionPercent")
CellValuetoControl(oBankSheet, TransactModel.txtMinimum, "ProvisionMinimum")
CellValuetoControl(oBankSheet, TransactModel.txtFix, "ProvisionFix")
End Sub
Sub AddShortCuttoControl()
Dim SelCompany as String
Dim iRow, SelIndex as Integer
SelIndex = DlgTransaction.GetControl("lstBuyStocks").GetSelectedItemPos()
If SelIndex <> -1 Then
SelCompany = TransactModel.lstBuyStocks.StringItemList(SelIndex)
iRow = GetStockRowIndex(SelCompany)
If iRow <> -1 Then
TransactModel.txtStockID.Text = oFirstSheet.GetCellByPosition(SBCOLUMNID1,iRow).String
TransactModel.txtRate.Value = oFirstSheet.GetCellByPosition(SBCOLUMNRATE1,iRow).Value
Else
TransactModel.txtStockID.Text = ""
TransactModel.txtRate.Value = 0
End If
Else
TransactModel.txtStockID.Text = ""
TransactModel.txtRate.Value = 0
End If
End Sub
Sub OpenStockRatePage(aEvent)
Dim CurStep as Integer
Initialize(True)
CurStep = aEvent.Source.Model.Tag
If FillListbox(DlgStockRates.GetControl("lstStockNames"), StockRatesTitle(CurStep), True) Then
StockRatesModel.Step = CurStep
ToggleStockRateControls(False, CurStep)
InitializeStockRatesControls(CurStep)
DlgStockRates.Execute()
End If
End Sub
Sub SelectStockNameForRates()
Dim StockName as String
StockName = DlgStockRates.GetControl("lstStockNames").GetSelectedItem()
If StockName <> "" Then
StockRatesModel.txtStockID.Text = GetStockID(StockName)
ToggleStockRateControls(True, StockRatesModel.Step)
End If
StockRatesModel.cmdGoOn.DefaultButton = True
End Sub
Sub ToggleStockRateControls(bDoEnable as Boolean, CurStep as Integer)
With StockRatesModel
.lblStockID.Enabled = False
.txtStockID.Enabled = False
.cmdGoOn.Enabled = Ubound(StockRatesModel.lstStockNames.SelectedItems()) <> -1
Select Case CurStep
Case 1
.optPerShare.Enabled = bDoEnable
.optTotal.Enabled = bDoEnable
.lblDividend.Enabled = bDoEnable
.txtDividend.Enabled = bDoEnable
Case 2
.lblExchangeRate.Enabled = bDoEnable
.lblDate.Enabled = bDoEnable
.lblColon.Enabled = bDoEnable
.txtOldRate.Enabled = bDoEnable
.txtNewRate.Enabled = bDoEnable
.txtDate.Enabled = bDoEnable
Case 3
.lblStartDate.Enabled = bDoEnable
.lblEndDate.Enabled = bDoEnable
.txtStartDate.Enabled = bDoEnable
.txtEndDate.Enabled = bDoEnable
.hlnInterval.Enabled = bDoEnable
.optDaily.Enabled = bDoEnable
.optWeekly.Enabled = bDoEnable
End Select
End With
End Sub
Sub InitializeStockRatesControls(CurStep as Integer)
DlgReference = DlgStockRates
DlgStockRates.Title = StockRatesTitle(CurStep)
With StockRatesModel
.txtStockID.Text = ""
.lblStockID.Label = sCurStockIDLabel
Select Case CurStep
Case 1
.txtDividend.Value = 0
.optPerShare.State = 1
.txtDividend.CurrencySymbol = sCurCurrency
Case 2
.txtOldRate.Value = 1
.txtNewRate.Value = 1
.txtDate.Date = CDateToISO(Date())
Case 3
.txtStartDate.DateMax = CDateToISO(CDate(Date())-1)
.txtEndDate.DateMax = CDateToISO(CDate(Date())-1)
.txtStartDate.Date = CDateToISO(CDate(Date())-8)
.txtEndDate.Date = CDateToISO(CDate(Date())-1)
.optDaily.State = 1
End Select
End With
End Sub
</script:module>
|