1 <?xml version=
"1.0" encoding=
"UTF-8"?>
2 <!DOCTYPE script:module PUBLIC
"-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
4 * This file is part of the LibreOffice project.
6 * This Source Code Form is subject to the terms of the Mozilla Public
7 * License, v. 2.0. If a copy of the MPL was not distributed with this
8 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 * This file incorporates work covered by the following license notice:
12 * Licensed to the Apache Software Foundation (ASF) under one or more
13 * contributor license agreements. See the NOTICE file distributed
14 * with this work for additional information regarding copyright
15 * ownership. The ASF licenses this file to you under the Apache
16 * License, Version 2.0 (the "License"); you may not use this file
17 * except in compliance with the License. You may obtain a copy of
18 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 <script:module xmlns:
script=
"http://openoffice.org/2000/script" script:
name=
"Depot" script:
language=
"StarBasic">Option Explicit
23 Sub Initialize(Optional bChooseMarketPlace as Boolean)
24 Dim bEnableHistory as Boolean
25 GlobalScope.BasicLibraries.LoadLibrary(
"Tools
")
26 ' oMarketModel = GetControlModel(oDocument.Sheets(
0),
"CmdHistory
")
27 ' bEnableHistory = oMarketModel.Enabled
31 oDocument = ThisComponent
32 oController = oDocument.GetCurrentController
33 oSheets = oDocument.Sheets
34 oFirstSheet = oSheets(
0)
35 oMovementSheet = oSheets(
1)
36 oBankSheet = oSheets(
2)
37 oDocFormats = oDocument.NumberFormats
38 oNumberFormatter = CreateUnoService(
"com.sun.star.util.NumberFormatter
")
39 oNumberFormatter.AttachNumberFormatsSupplier(oDocument)
40 oDocLocale = oDocument.CharLocale
41 sDocLanguage = oDocLocale.Language
42 sDocCountry = oDocLocale.Country
45 ' oMarketModel.Enabled = bEnableHistory
46 If Not IsMissing(bChooseMarketPlace) Then
47 If bChoosemarketPlace Then
53 If Not IsMissing(bChooseMarketPlace) Then
54 If bChooseMarketPlace Then
55 oMarketModel.Enabled = bEnableMarket
56 oInternetModel.Enabled = bEnableInternet
64 FillListbox(DlgTransaction.GetControl(
"lstBuyStocks
"), TransactTitle(SBDIALOGBUY), False)
65 SetupTransactionControls(SBDIALOGBUY)
66 EnableTransactionControls(False)
67 DlgTransaction.Execute()
73 If FillListbox(DlgTransaction.GetControl(
"lstSellStocks
"), TransactTitle(SBDIALOGSELL), True) Then
74 SetupTransactionControls(SBDIALOGSELL)
75 EnableTransactionControls(False)
76 DlgTransaction.Execute()
82 Dim TransactionCount as Integer
83 Dim StockCount, iStartRow, i as Integer
84 Dim oRows, oRange as Object
85 Dim StockName as String
87 ' Delete transactions and reset overview
88 If MsgBox(sMsgDeleteAll, SBMSGYESNO+SBMSGQUESTION+SBMSGDEFAULTBTN2, sMsgAuthorization) =
6 Then
89 ' Assumption: If and only if there is an overview, then there are transactions, too
90 UnprotectSheets(oSheets)
91 StockCount = GetStocksCount(iStartRow)
93 For i =
1 To StockCount
94 StockName = oFirstSheet.GetCellbyPosition(SBCOLUMNNAME1, iStartRow + i).String
95 If oSheets.HasbyName(StockName) Then
96 oSheets.RemoveByName(StockName)
99 oDocument.AddActionLock
100 RemoveStockRows(oFirstSheet, iStartRow +
1, StockCount)
101 TransactionCount = GetTransactionCount(iStartRow)
102 RemoveStockRows(oMovementSheet, iStartRow +
2, TransactionCount)
103 ProtectSheets(oSheets)
104 oDocument.RemoveActionLock
111 Dim RestQuantity, Value, PartialValue, Profit
112 Dim iNewRow as Integer, iRow as Integer
113 Dim iStockRow as Long, iRestQuantity as Long
114 Dim oNameCell as Object
115 Dim CellStockName as String, SelStockName as String
116 Dim CurRate as Double
117 Dim TransactDate as Date
118 Dim LocStockName as String
119 ' Check for rate entered
120 If TransactModel.txtRate.Value =
0 Then
121 If TransactModel.Step = SBDIALOGBUY Then
122 If MsgBox(sMsgFreeStock, SBMSGYESNO+SBMSGQUESTION, sMsgConfirm)=
7 Then
126 If MsgBox(sMsgTotalLoss, SBMSGYESNO+SBMSGQUESTION, sMsgConfirm)=
7 Then
131 CurRate = TransactModel.txtRate.Value
132 TransactDate = CDateFromUNODate(TransactModel.txtDate.Date)
133 DlgTransaction.EndExecute()
134 UnprotectSheets(oSheets)
136 iNewRow = DuplicateRow(oMovementSheet,
"HiddenRow3
")
138 If TransactModel.Step = SBDIALOGBUY Then
139 CellStockName = TransactModel.lstBuyStocks.Text
140 If Instr(
1,CellStockName,
"$
")
<> 0 Then
141 CellStockName =
"'" & CellStockName
& "'"
143 oMovementSheet.GetCellByPosition(SBCOLUMNNAME2, iNewRow).String = CellStockName
144 oMovementSheet.GetCellByPosition(SBCOLUMNQUANTITY2, iNewRow).Value = TransactModel.txtQuantity.Value
146 CellStockName = DlgTransaction.GetControl(
"lstSellStocks
").GetSelectedItem()
147 oMovementSheet.GetCellByPosition(SBCOLUMNNAME2, iNewRow).String = CellStockName
148 oMovementSheet.GetCellByPosition(SBCOLUMNQUANTITY2, iNewRow).Value = -TransactModel.txtQuantity.Value
151 oMovementSheet.GetCellByPosition(SBCOLUMNDATE2, iNewRow).Value = CDateFromUNODate(TransactModel.txtDate.Date)
152 oMovementSheet.GetCellByPosition(SBCOLUMNRATE2, iNewRow).Value = TransactModel.txtRate.Value
153 oMovementSheet.GetCellByPosition(SBCOLUMNPROVPERCENT2, iNewRow).Value = TransactModel.txtCommission.EffectiveValue
154 oMovementSheet.GetCellByPosition(SBCOLUMNPROVMIN2, iNewRow).Value = TransactModel.txtMinimum.Value
155 oMovementSheet.GetCellByPosition(SBCOLUMNPROVFIX2, iNewRow).Value = TransactModel.txtFix.Value
157 ' Buy stocks: Update overview for new stocks
158 If TransactModel.Step = SBDIALOGBUY Then
159 iStockRow = GetStockRowIndex(CellStockName)
160 If iStockRow = -
1 Then
161 iNewRow = DuplicateRow(oFirstSheet,
"HiddenRow2
")
162 oFirstSheet.GetCellByPosition(SBCOLUMNNAME1, iNewRow).String = CellStockName
163 oFirstSheet.GetCellByPosition(SBCOLUMNID1, iNewRow).String = TransactModel.txtStockID.Text
164 iStockRow = GetStockRowIndex(CellStockName)
166 ' Sell stocks: Get transaction value, then update Transaction sheet
167 ElseIf TransactModel.Step = SBDIALOGSELL Then
168 Profit = oMovementSheet.GetCellByPosition(SBCOLUMNPROCEEDS2, iNewRow).Value
170 Sold = TransactModel.txtQuantity.Value
171 SelStockName = DlgTransaction.GetControl(
"lstSellStocks
").GetSelectedItem()
172 ' Go to first name
173 If TransactMode = FIFO Then
174 iRow = SBROWFIRSTTRANSACT2
179 ' Check that no transaction after split date exists else cancel split
181 oNameCell = oMovementSheet.GetCellByPosition(SBCOLUMNNAME2, iRow)
182 CellStockName = oNameCell.String
183 If CellStockName = SelStockName Then
184 ' Update transactions: Note quantity sold
185 RestQuantity = oMovementSheet.GetCellByPosition(SBCOLUMNQTYREST2, iRow).Value
186 ' If there still is a rest left ...
187 If RestQuantity
> 0 Then
188 If RestQuantity
< Sold Then
189 ' Recalculate profit of new transaction
190 Profit = Profit - oMovementSheet.GetCellByPosition(SBCOLUMNPRCREST2, iRow).Value
191 AddValueToCellContent(SBCOLUMNQTYSOLD2, iRow, RestQuantity)
192 PartialValue = RestQuantity / Sold * Value
193 AddValueToCellContent(SBCOLUMNREALPROC2, iRow, PartialValue)
194 Sold = Sold - RestQuantity
195 Value = Value - PartialValue
197 ' Recalculate profit of neTransactModel.lstBuyStocks.Textw transaction
198 PartialValue = oMovementSheet.GetCellByPosition(SBCOLUMNPRCREST2, iRow).Value
199 Profit = Profit - PartialValue/RestQuantity * Sold
200 ' Update sold shares cell
201 AddValueToCellContent(SBCOLUMNQTYSOLD2, iRow, Sold)
202 ' Update sales turnover cell
203 AddValueToCellContent(SBCOLUMNREALPROC2, iRow, Value)
204 ' Update variables for rest of transaction
210 iRow = iRow + TransactMode
212 oMovementSheet.GetCellByPosition(SBCOLUMNREALPROFIT2,iNewRow).Value = Profit
213 iStockRow = GetStockRowIndex(SelStockName)
214 iRestQuantity = oFirstSheet.GetCellbyPosition(SBCOLUMNQUANTITY1, iStockRow).Value
215 ' If iRestQuantity =
0 Then
216 ' If oSheets.HasbyName(SelStockName) Then
217 ' oSheets.RemoveByName(SelStockName)
223 InsertCurrentValue(CurRate, iStockRow,TransactDate)
224 ProtectSheets(oSheets)
228 Sub SelectStockname(aEvent as Object)
229 Dim iCurRow as Integer
230 Dim CurStockName as String
232 ' Find row with stock name
233 If TransactModel.Step = SBDIALOGBUY Then
234 CurStockName = .lstBuyStocks.Text
235 iCurRow = GetStockRowIndex(CurStockName)
236 .txtQuantity.ValueMax =
10000000
238 Dim ListBoxList() as String
239 ListBoxList() = GetSelectedListboxItems(aEvent.Source.getModel())
240 CurStockName = ListBoxList(
0)
241 ' CurStockName = DlgTransaction.GetControl(aEvent.Source.getModel.Name).GetSelectedItem()
242 iCurRow = GetStockRowIndex(CurStockName)
243 Dim fdouble as Double
244 fdouble = oFirstSheet.GetCellByPosition(SBCOLUMNQUANTITY1, iCurRow).Value
245 .txtQuantity.Value = fdouble
246 .txtQuantity.ValueMax = oFirstSheet.GetCellByPosition(SBCOLUMNQUANTITY1, iCurRow).Value
247 .txtRate.Value = oFirstSheet.GetCellbyPosition(SBCOLUMNRATE1, iCurRow).Value
249 .txtStockID.Enabled = .Step = SBDIALOGBUY
250 .lblStockID.Enabled = .Step = SBDIALOGBUY
251 ' Default settings for quantity and rate
252 .txtStockID.Text = GetStockID(CurStockName, iCurRow)
254 EnableTransactionControls(CurStockName
<> "")
255 TransactModel.cmdGoOn.DefaultButton = True
260 Sub HandleStocks(Mode as Integer, oDialog as Object)
261 Dim DividendPerShare, DividendTotal, RestQuantity, OldValue
262 Dim SelStockName, CellStockName as String
263 Dim oNameCell as Object, oDateCell as Object
265 Dim oDividendCell as Object
267 Dim OldNumber, NewNumber as Integer
268 Dim NoteText as String
269 Dim TotalStocksCount as Long
271 oDocument.AddActionLock
273 oModel = oDialog.Model
274 SelStockName = DlgStockRates.GetControl(
"lstStockNames
").GetSelectedItem()
277 Dim bTakeTotal as Boolean
278 ' Update transactions: Enter dividend paid for all Buy transactions not sold completely
279 bTakeTotal = oModel.optTotal.State =
1
281 DividendTotal = oModel.txtDividend.Value
282 iRow = GetStockRowIndex(SelStockName)
283 TotalStocksCount = oFirstSheet.GetCellByPosition(SBCOLUMNQUANTITY1,iRow).Value
284 DividendPerShare = DividendTotal/TotalStocksCount
286 DividendPerShare = oModel.txtDividend.Value
290 ' Store entered values in variables
291 OldNumber = oModel.txtOldRate.Value
292 NewNumber = oModel.txtNewRate.Value
293 SplitDate = CDateFromUNODate(oModel.txtDate.Date)
294 iRow = SBROWFIRSTTRANSACT2
295 NoteText = cSplit
& SplitDate
& ",
" & oModel.txtOldRate.Value
& oModel.lblColon.Label
& oModel.txtNewRate.Value
297 oNameCell = oMovementSheet.GetCellByPosition(SBCOLUMNNAME2, iRow)
298 CellStockName = oNameCell.String
299 If CellStockName = SelStockName Then
300 oDateCell = oMovementSheet.GetCellByPosition(SBCOLUMNDATE2, iRow)
301 If oDateCell.Value
>= SplitDate Then
302 MsgBox sMsgWrongExchangeDate, SBMSGOK + SBMSGSTOP, sMsgError
307 Loop Until CellStockName =
""
309 iRow = SBROWFIRSTTRANSACT2
310 UnprotectSheets(oSheets)
312 oNameCell = oMovementSheet.GetCellByPosition(SBCOLUMNNAME2, iRow)
313 CellStockName = oNameCell.String
314 If CellStockName = SelStockName Then
317 RestQuantity = oMovementSheet.GetCellByPosition(SBCOLUMNQTYREST2, iRow).Value
318 If RestQuantity
> 0 Then
319 oDividendCell = oMovementSheet.GetCellByPosition(SBCOLUMNDIVIDEND2, iRow)
320 OldValue = oDividendCell.Value
321 oDividendCell.Value = OldValue + RestQuantity * DividendPerShare
324 oDateCell = oMovementSheet.GetCellByPosition(SBCOLUMNDATE2, iRow)
325 SplitCellValue(oMovementSheet, NewNumber, OldNumber, SBCOLUMNQUANTITY2, iRow, NoteText)
326 SplitCellValue(oMovementSheet, OldNumber, NewNumber, SBCOLUMNRATE2, iRow,
"")
327 SplitCellValue(oMovementSheet, NewNumber, OldNumber, SBCOLUMNQTYSOLD2, iRow,
"")
331 Loop Until CellStockName =
""
332 If Mode = HANDLESPLIT Then
333 CalculateChartafterSplit(SelStockName, NewNumber, OldNumber, NoteText, SplitDate)
335 oDocument.CalculateAll()
336 ProtectSheets(oSheets)
337 oDocument.RemoveActionLock
341 Sub CancelStockRate()
342 DlgStockRates.EndExecute()
346 Sub CancelTransaction()
347 DlgTransaction.EndExecute()
351 Sub CommitStockRate()
352 Dim CurStep as Integer
353 CurStep = StockRatesModel.Step
356 ' Check for quantity entered
357 If StockRatesModel.txtDividend.Value =
0 Then
358 MsgBox sMsgNoDividend, SBMSGSTOP+SBMSGSTOP, sMsgError
361 HandleStocks(HANDLEDIVIDEND, DlgStockRates)
363 HandleStocks(HANDLESPLIT, DlgStockRates)
365 InsertCompanyHistory()
370 Sub EnableTransactionControls(bEnable as Boolean)
372 .lblQuantity.Enabled = bEnable
373 .txtQuantity.Enabled = bEnable
374 .lblRate.Enabled = bEnable
375 .txtRate.Enabled = bEnable
376 .lblDate.Enabled = bEnable
377 .txtDate.Enabled = bEnable
378 .lblCommission.Enabled = bEnable
379 .txtCommission.Enabled = bEnable
380 .lblMinimum.Enabled = bEnable
381 .txtMinimum.Enabled = bEnable
382 .lblFix.Enabled = bEnable
383 .txtFix.Enabled = bEnable
384 If TransactModel.Step = SBDIALOGSELL Then
385 .cmdGoOn.Enabled = Ubound(TransactModel.lstSellStocks.SelectedItems())
> -
1
386 DlgTransaction.GetControl(
"lstSellStocks
").SetFocus()
388 .cmdGoOn.Enabled = TransactModel.lstBuyStocks.Text
<> ""
389 DlgTransaction.GetControl(
"lstBuyStocks
").SetFocus()
392 TransactModel.cmdGoOn.DefaultButton = True
398 Sub SetupTransactionControls(CurStep as Integer)
399 DlgReference = DlgTransaction
401 .txtDate.Date = CDateToUNODate(Date())
402 .txtDate.DateMax = CDateToUNODate(Date())
403 .txtStockID.Enabled = False
404 .lblStockID.Enabled = False
405 .lblStockID.Label = sCurStockIDLabel
406 .txtRate.CurrencySymbol = sCurCurrency
407 .txtFix.CurrencySymbol = sCurCurrency
410 DlgTransaction.Title = TransactTitle(CurStep)
411 CellValuetoControl(oBankSheet, TransactModel.txtCommission,
"ProvisionPercent
")
412 CellValuetoControl(oBankSheet, TransactModel.txtMinimum,
"ProvisionMinimum
")
413 CellValuetoControl(oBankSheet, TransactModel.txtFix,
"ProvisionFix
")
417 Sub AddShortCuttoControl()
418 Dim SelCompany as String
419 Dim iRow, SelIndex as Integer
420 SelIndex = DlgTransaction.GetControl(
"lstBuyStocks
").GetSelectedItemPos()
421 If SelIndex
<> -
1 Then
422 SelCompany = TransactModel.lstBuyStocks.StringItemList(SelIndex)
423 iRow = GetStockRowIndex(SelCompany)
424 If iRow
<> -
1 Then
425 TransactModel.txtStockID.Text = oFirstSheet.GetCellByPosition(SBCOLUMNID1,iRow).String
426 TransactModel.txtRate.Value = oFirstSheet.GetCellByPosition(SBCOLUMNRATE1,iRow).Value
428 TransactModel.txtStockID.Text =
""
429 TransactModel.txtRate.Value =
0
432 TransactModel.txtStockID.Text =
""
433 TransactModel.txtRate.Value =
0
438 Sub OpenStockRatePage(aEvent)
439 Dim CurStep as Integer
441 CurStep = aEvent.Source.Model.Tag
442 If FillListbox(DlgStockRates.GetControl(
"lstStockNames
"), StockRatesTitle(CurStep), True) Then
443 StockRatesModel.Step = CurStep
444 ToggleStockRateControls(False, CurStep)
445 InitializeStockRatesControls(CurStep)
446 DlgStockRates.Execute()
451 Sub SelectStockNameForRates()
452 Dim StockName as String
453 StockName = DlgStockRates.GetControl(
"lstStockNames
").GetSelectedItem()
454 If StockName
<> "" Then
455 StockRatesModel.txtStockID.Text = GetStockID(StockName)
456 ToggleStockRateControls(True, StockRatesModel.Step)
458 StockRatesModel.cmdGoOn.DefaultButton = True
462 Sub ToggleStockRateControls(bDoEnable as Boolean, CurStep as Integer)
464 .lblStockID.Enabled = False
465 .txtStockID.Enabled = False
466 .cmdGoOn.Enabled = Ubound(StockRatesModel.lstStockNames.SelectedItems())
<> -
1
469 .optPerShare.Enabled = bDoEnable
470 .optTotal.Enabled = bDoEnable
471 .lblDividend.Enabled = bDoEnable
472 .txtDividend.Enabled = bDoEnable
474 .lblExchangeRate.Enabled = bDoEnable
475 .lblDate.Enabled = bDoEnable
476 .lblColon.Enabled = bDoEnable
477 .txtOldRate.Enabled = bDoEnable
478 .txtNewRate.Enabled = bDoEnable
479 .txtDate.Enabled = bDoEnable
481 .lblStartDate.Enabled = bDoEnable
482 .lblEndDate.Enabled = bDoEnable
483 .txtStartDate.Enabled = bDoEnable
484 .txtEndDate.Enabled = bDoEnable
485 .hlnInterval.Enabled = bDoEnable
486 .optDaily.Enabled = bDoEnable
487 .optWeekly.Enabled = bDoEnable
493 Sub InitializeStockRatesControls(CurStep as Integer)
494 DlgReference = DlgStockRates
495 DlgStockRates.Title = StockRatesTitle(CurStep)
497 .txtStockID.Text =
""
498 .lblStockID.Label = sCurStockIDLabel
501 .txtDividend.Value =
0
502 .optPerShare.State =
1
503 .txtDividend.CurrencySymbol = sCurCurrency
505 .txtOldRate.Value =
1
506 .txtNewRate.Value =
1
507 .txtDate.Date = CDateToUNODate(Date())
509 .txtStartDate.DateMax = CDateToUNODate(CDate(Date())-
1)
510 .txtEndDate.DateMax = CDateToUNODate(CDate(Date())-
1)
511 .txtStartDate.Date = CDateToUNODate(CDate(Date())-
8)
512 .txtEndDate.Date = CDateToUNODate(CDate(Date())-
1)