home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 1999 February / DPPCPRO0299.ISO / February / Delphi / Install / DATA.Z / DM.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-06-11  |  5.1 KB  |  171 lines

  1. unit DM;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   DBTables, DB;
  8.  
  9. type
  10.   TDM1 = class(TDataModule)
  11.     tblMaster: TTable;
  12.     tblMasterSYMBOL: TStringField;
  13.     tblMasterCO_NAME: TStringField;
  14.     tblMasterEXCHANGE: TStringField;
  15.     tblMasterIndustryLongName: TStringField;
  16.     tblMasterCUR_PRICE: TFloatField;
  17.     tblMasterYRL_HIGH: TFloatField;
  18.     tblMasterYRL_LOW: TFloatField;
  19.     tblMasterP_E_RATIO: TFloatField;
  20.     tblMasterPROJ_GRTH: TFloatField;
  21.     tblMasterINDUSTRY: TSmallintField;
  22.     tblMasterPRICE_CHG: TSmallintField;
  23.     tblMasterRATING: TStringField;
  24.     tblMasterRANK: TFloatField;
  25.     tblMasterOUTLOOK: TSmallintField;
  26.     tblMasterRCMNDATION: TStringField;
  27.     tblMasterRISK: TStringField;
  28.     dsMaster: TDataSource;
  29.     tblIndustry: TTable;
  30.     tblIndustryIND_CODE: TSmallintField;
  31.     tblIndustryIND_NAME: TStringField;
  32.     tblIndustryLONG_NAME: TStringField;
  33.     dsIndustry: TDataSource;
  34.     tblHoldings: TTable;
  35.     tblHoldingsACCT_NBR: TFloatField;
  36.     tblHoldingsSHARES: TFloatField;
  37.     tblHoldingsPUR_PRICE: TFloatField;
  38.     tblHoldingsPUR_DATE: TDateField;
  39.     tblHoldingsSYMBOL: TStringField;
  40.     tblHoldingsPUR_COST: TCurrencyField;
  41.     dsHoldings: TDataSource;
  42.     procedure tblHoldingsCalcFields(DataSet: TDataSet);
  43.     procedure tblHoldingsAfterPost(DataSet: TDataSet);
  44.     procedure CalculateTotals(Sender: TObject; Field: TField);
  45.     procedure tblHoldingsAfterOpen(DataSet: TDataSet);
  46.   private
  47.     { Private declarations }
  48.   public
  49.     { Public declarations }
  50.   end;
  51.  
  52. var
  53.   DM1: TDM1;
  54.  
  55. implementation
  56.  
  57. uses CtrlForm;
  58.  
  59. {$R *.DFM}
  60.  
  61. procedure TDM1.tblHoldingsCalcFields(DataSet: TDataSet);
  62. begin
  63.   tblHoldingsPUR_COST.AsFloat :=
  64.     tblHoldingsPUR_PRICE.AsFloat * tblHoldingsSHARES.AsFloat;
  65. end;
  66.  
  67. procedure TDM1.CalculateTotals(Sender: TObject; Field: TField);
  68. var
  69.   flTotalCost,            { Holds total share cost }
  70.   flTotalShares,          { Holds total share count }
  71.   flTotalValue,           { Holds total share value }
  72.   flDifference: Real;     { Holds difference between cost and value }
  73.   strFormatSpec: string;  { The Display Format specification }
  74. begin
  75.  
  76.   { Update the count of stock transactions }
  77.   FmCtrlGrid.lPurchase.Caption := IntToStr( tblHoldings.RecordCount );
  78.  
  79.   { See whether or not its necessary to total the holdings and
  80.     (if so) do so and update the result displays; otherwise,
  81.     clear the result displays. }
  82.   if tblHoldings.recordCount = 0 then
  83.   begin
  84.     { Clear the result displays }
  85.     FmCtrlGrid.lTotalCost.Caption   := '';
  86.     FmCtrlGrid.lTotalShares.Caption := '';
  87.     FmCtrlGrid.lDifference.Caption  := '';
  88.   end
  89.   else
  90.   begin
  91.     { let the user know something's going on }
  92.     Screen.Cursor := crHourglass;
  93.  
  94.     { Initialize the holder variables }
  95.     flTotalCost := 0.0;
  96.     flTotalShares := 0.0;
  97.     flTotalValue := 0.0;
  98.     flDifference := 0.0;
  99.  
  100.     { Calculate the total cost of these holdings. }
  101.     tblHoldings.disableControls;  { hide this process from the user }
  102.     tblHoldings.first;
  103.     while not tblHoldings.eof do
  104.     begin
  105.       flTotalCost := flTotalCost + tblHoldingsPUR_COST.AsFloat;
  106.       flTotalShares := flTotalShares + tblHoldingsSHARES.AsFloat;
  107.       tblHoldings.next;
  108.     end;
  109.     tblHoldings.first;
  110.     tblHoldings.enableControls;  { restore the display of holdings }
  111.  
  112.     { Calculate the current value of the shares (by multiplying
  113.       the current holdings by the current share price) and the
  114.       difference between the cost and the value. }
  115.  
  116.     flTotalValue := flTotalShares * tblMasterCUR_PRICE.AsFloat;
  117.     flDifference := flTotalValue - flTotalCost;
  118.  
  119.     { Use the same format specification as that being used to
  120.       display the Current Price field value so it can be used
  121.       to display the results }
  122.  
  123.     strFormatSpec := tblMasterCUR_PRICE.DisplayFormat;
  124.  
  125.     { Update the result displays }
  126.  
  127.     FmCtrlGrid.lTotalCost.Caption :=
  128.        FormatFloat( strFormatSpec, flTotalCost );
  129.     FmCtrlGrid.lTotalShares.Caption :=
  130.        FormatFloat( strFormatSpec, flTotalValue );
  131.     FmCtrlGrid.lDifference.Caption :=
  132.        FormatFloat( strFormatSpec, flDifference );
  133.  
  134.     { Update the Font Color of the Diference to
  135.       indicate the quality of the investment }
  136.     if flDifference > 0 then
  137.       FmCtrlGrid.lDifference.Font.Color := clGreen
  138.     else
  139.       FmCtrlGrid.lDifference.Font.Color := clRed;
  140.     FmCtrlGrid.lDifference.update;
  141.  
  142.     { let the user know that we're finished }
  143.     Screen.Cursor := crDefault;
  144.   end;
  145. end;
  146.  
  147.  
  148. procedure TDM1.tblHoldingsAfterPost(DataSet: TDataSet);
  149. var
  150.   bmCurrent : TBookmark;  { Holds the current position }
  151. begin
  152.   with tblHoldings do
  153.   begin
  154.     bmCurrent := getBookmark;       { save position }
  155.     try
  156.       CalculateTotals(nil, nil);  { recalc totals }
  157.       gotoBookmark(bmCurrent);     { restore position }
  158.     finally;
  159.       freeBookmark(bmCurrent);     { free memory }
  160.     end;
  161.   end;
  162. end;
  163.  
  164. procedure TDM1.tblHoldingsAfterOpen(DataSet: TDataSet);
  165. begin
  166.   {Don't want this calculation to occur until both master & detail are open}
  167.   dsMaster.OnDataChange := CalculateTotals;
  168. end;
  169.  
  170. end.
  171.