Capital Gains

Created by: James Pattie

updated: $Id: CapitalGains.html,v 1.1.1.1 2001/08/09 19:02:50 moreejt Exp $


Capital Gains in Capital Gains object to use similar display routines as portfolio object

Specifications:

Structure for output Showing all data defaults to sorting by

Stocks then Assets

Sub Accounts by Alpha

Lots by Purchased, Sold

Short then long



Purchased

Sold

Cost

Market

Shares

Gain/Loss

LT/ST

    Stock Sub Acct 1








        Lot 1

1/1/01

2/2/02

1000

2000

1

1000

L

        Lot 2

1/1/01

2/1/01

1000

1

1

-999

S

    Stock Sub Acct



2000

2001


1


Stock Total



2000

2001


1


    Asset SubAcct

1/1/99

6/6/01

225

226


1

L

Asset Total



225

226


1




Grand Totals:

Total Cost:

Total Market Value

Capital G/L:     $2.00


It should also be able to output by

Short term then long term

Stocks then Assets

Gains then losses

Sub account by Alpha

Lots by Purchased, Sold


Purchased

Sold

Cost

Market

Shares

Gain/Loss

LT/ST

        Lot 2

1/1/01

2/1/01

1000

1

1

-999

S

    Stock Sub Acct



1000

1


-999


ST Total



2000

1


-999


        Lot 1

1/1/01

2/2/02

1000

2000

1

1000

L

    Stock Sub Acct



1000

2000


1000


    Asset SubAcct

1/1/99

6/6/01

225

226


1

L

LT Total



1225

2226


1001



Total Cost:

Total Market Value

Total G/L:


Structure for gathering customization.

Radio button: Order by: () ST/LT () Sub Account



Implementation:

This should be able to be accomplished by the way the data is retrieved from the database.

To get the Purchased date a SQL Function should be implemented or a couple of them?



Data Structures needed:

CGLDisplayObject - This is the top level object that we build up to and the display routine starts with.

  @cglObjects - CGLObject (Must contain at least 1 entry)

  cost - GrandTotals for these items.

  market

  gainLoss


CGLObject

  cglStocks - CGLStocks (object must be defined based upon type value)

  cglAssets - CGLAssets (object must be defined based upon type value)

  order - stock|assets (Display Stocks first then Assets or vice versa)

  type - both|ST|LT (Are we displaying ST and LT together or is this just a ST or LT object?)

  state - o|c (open or closed; Are we displaying all the items in this Object or just the Total)


CGLStocks

  @cglStockObjects - CGLStockObject (This can be empty)

  cost, market, gainLoss - (Holds Grand Totals for all entries in the cglStockObject array)

  state - o|c (open or closed; Are the Stocks being displayed or just the Stocks Total)


CGLAssets

  @cglAssetObjects - CGLAssetObject (This can be empty)

  cost, market, gainLoss - (Holds Grand Totals for all entries in the cglAssetObject array)

  state - o|c (open or closed; Are the Assets being displayed or just the Assets Total)


CGLStockObject

  name - SubAcct name

  number - SubAcct number

  cost - Totals of all Lots

  market - "

  gainLoss - "

  @cglStockLotObjects - CGLStockLotObject (All the Lots for this stock)

  state - o|c (open or closed; Show the Lots or Hide them)


CGLStockLotObject

  purchased - date stock purchased

  sold - date stock sold

  cost -

  market -

  gainLoss -

  shares - number of shares sold

  type - L|S


CGLAssetObject

  purchased

  sold

  name

  number

  cost

  market

  gainLoss

  type - L|S


Display Code overview:

If there is only 1 entry in the cglObjects array, then we display it and let it display the grandTotals.

If there is more than 1 entry in the cglObjects array, then we display each item and let them display their mini-Grand Total.

We then display the global GrandTotal based upon the value in cglTotalsObject.


The CGLDisplayObject will have a display routine which takes the document to display into. It then will loop over all entries in the cglObjects array and call their display methods to do the actual displaying.

The CGLObject will have a display routine which takes the document to display into and whether or not it should display a GrandTotal or just a Total.

The display routine will determine the order to display the data in (Stocks, Assets or Assets, Stocks) and if it is doing both LT and ST values or just one of them.


Gathering Data:

Use 1 select statement from the debitcredit_tb


Getting Stock entries -- have to do a join between debitcredit_tb and lot_node_tb

select d.trans_date as sell_date, dorcify(d.amount, d.dorc, debit_increases_account_num(d.account_num)) as amount, d.account_num, d.sub_account_num, get_asset_data(d.parent_num, d.account_num, d.sub_account_num, false, true) as cost, get_asset_data(d.parent_num, d.account_num, -1, true, true) as market, stock_purchased(l.lot) as purchase_date, l.lot, get_shares_data(l.lot, d.parent_num) as shares from debitcredit_tb as d, lot_node_tb as l where d.account_num = capital gains/ loss accounts and d.sub_account_num = stock sub accounts in and d.trans_date between '$startDate' AND '$endDate' and d.parent_num = l.journal_ref order by account_num, sell_date, sub_account_num, lot


Getting Assets entries

select trans_date as sell_date, dorcify(amount, dorc, debit_increases_account_num(account_num)) as amount, account_num, sub_account_num, get_asset_data(parent_num, account_num, sub_account_num, false, true) as cost, get_asset_data(parent_num, account_num, -1, true, true) as market, asset_purchased(sub_account_num, ledger_code) as purchase_date from debitcredit_tb where account_num = capital gains/loss accounts and sub_account_num = asset sub accounts and trans_date between '$startDate' AND '$endDate' order by account_num, sell_date, sub_account_num


need to create sql Functions:

get_asset_data(parent_num, account_num, sub_account_num, dorc, (negate account - y/n)) - Returns the dorc-ified value of amount for the cost value. (Which since it is always a positive value is not converted at all.)

        CREATE FUNCTION get_asset_data(int4, int4, int4, bool, bool) RETURNS NUMERIC AS

        'select case

          when $5 = true then (select amount from debitcredit_tb where parent_num = $1 and account_num <> $2 and sub_account_num = $3 and dorc =           $4)

          else (select amount from debitcredit_tb where parent_num = $1 and account_num = $2 and sub_account_num = $3 and dorc = $4)

          end;' LANGUAGE 'SQL';


get_shares_data(lot, parent_num)

       CREATE FUNCTION get_shares_data(text, int4) RETURNS NUMERIC AS

       ' select ns from sec_event_tb where event_code = 20 and l = $1 and event_ref = (select event_ref from lot_node_tb where journal_ref = $2);'        LANGUAGE 'SQL';


dorcify(amount, dorc, debit_increases_flag)

        CREATE FUNCTION dorcify(numeric, boolean, boolean) RETURNS NUMERIC AS

        'select case when $2 = true and $3 = true then $1

         when $2 = false and $3 = true then 0::numeric - $1

         when $2 = true and $3 = false then 0::numeric - $1

         when $2 = false and $3 = false then $1 end;' LANGUAGE 'SQL';


stock_purchased(lot) -- Takes the lot interested in and returns the purchased date for the stock in question.

        CREATE FUNCTION stock_purchased(int4) RETURNS DATE AS

        'select event_date from lot_node_tb where lot = (select buy_lot from lot_parent_tb where lot = $1) and parent_id = -1;' LANGUAGE 'SQL';


asset_purchased(sub_acct, ledger_code) -- Select date for when asset was bought

        CREATE FUNCTION asset_purchased(int4, text) RETURNS DATE AS

        'select purchase_date from asset_tb where ledger_code = $2 and sub_acct = $1;' LANGUAGE 'SQL';



*************This section has been put on hold in lieu of the above section of this

document which will be easier to implement for now************************


The Investment Reporting system will provide a third Report Type of investment. The equivalent of Regular Reports are the portfolio and capital gains and losses reports. A new detailed view report that knows it is working with the investment data will need to be created. A select box will be provided that allows the creator of a detailed view report to specify the data output/gathering format (regular, investment).


When creating investment reports, the user will provide the name of the report, the type (portfolio or capital gains and losses) and any default color scheme. When the report is run, the customization screen is displayed and then the specified data is gathered and displayed. When the user clicks on a Lot the Investment Detailed view report will be run with the specified sql string necessary to drill in on that Account, Stock, Lot combination. The number assigned to the Investment Detailed View report should be hard coded as 10 for now (I would like to make it System report 2 eventually).


The portfolio report will provide the user with a customization screen that prompts for the date and style of report to run.

The style could be by account(s) or stock(s) with the data presented in account->stock->lot or stock->lot->account layout. The user can than select the account(s) or stock(s) they wish to see. A date selection box should be provided.


The capital gains and losses report will provide the user with a customization screen that prompts for the date range and the style of report to run. The style could be by account(s) or stock(s). The user can than select the account(s) or stock(s) they wish to see. A date selection box should be provided.


The customization of running portfolio's and capital gains and losses reports will provide for color changes, printing, etc.


The investment detailed view report will provide a customization screen similar to the Regular Detailed View report except for it will be limited to the Investment Accounts and Sub Accounts. The Lot number will be the main drill down access point, but the event_ref column may also be drillable for global events, etc. so that you can get to an edit screen for a split, etc. for that stock. The customization of the run report will provide for color changes, column hiding/re-ordering, printing, etc. Each column will be sortable and can also display a sub total? (of what: stocks, cost)

*******************************************************************************


Copyright (c) 2001 HLR
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1; A copy of the license is available at http://www.gnu.org/licenses/licenses.html#FDL.

Updated: $Id: CapitalGains.html,v 1.1.1.1 2001/08/09 19:02:50 moreejt Exp $