home *** CD-ROM | disk | FTP | other *** search
/ Delphi 5 for Professionals / DELPHI5.iso / Runimage / Delphi50 / Demos / Db / Ctrlgrid / dm.pas < prev   
Encoding:
Pascal/Delphi Source File  |  1999-08-11  |  5.1 KB  |  169 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.  
  98.     { Calculate the total cost of these holdings. }
  99.     tblHoldings.disableControls;  { hide this process from the user }
  100.     tblHoldings.first;
  101.     while not tblHoldings.eof do
  102.     begin
  103.       flTotalCost := flTotalCost + tblHoldingsPUR_COST.AsFloat;
  104.       flTotalShares := flTotalShares + tblHoldingsSHARES.AsFloat;
  105.       tblHoldings.next;
  106.     end;
  107.     tblHoldings.first;
  108.     tblHoldings.enableControls;  { restore the display of holdings }
  109.  
  110.     { Calculate the current value of the shares (by multiplying
  111.       the current holdings by the current share price) and the
  112.       difference between the cost and the value. }
  113.  
  114.     flTotalValue := flTotalShares * tblMasterCUR_PRICE.AsFloat;
  115.     flDifference := flTotalValue - flTotalCost;
  116.  
  117.     { Use the same format specification as that being used to
  118.       display the Current Price field value so it can be used
  119.       to display the results }
  120.  
  121.     strFormatSpec := tblMasterCUR_PRICE.DisplayFormat;
  122.  
  123.     { Update the result displays }
  124.  
  125.     FmCtrlGrid.lTotalCost.Caption :=
  126.        FormatFloat( strFormatSpec, flTotalCost );
  127.     FmCtrlGrid.lTotalShares.Caption :=
  128.        FormatFloat( strFormatSpec, flTotalValue );
  129.     FmCtrlGrid.lDifference.Caption :=
  130.        FormatFloat( strFormatSpec, flDifference );
  131.  
  132.     { Update the Font Color of the Diference to
  133.       indicate the quality of the investment }
  134.     if flDifference > 0 then
  135.       FmCtrlGrid.lDifference.Font.Color := clGreen
  136.     else
  137.       FmCtrlGrid.lDifference.Font.Color := clRed;
  138.     FmCtrlGrid.lDifference.update;
  139.  
  140.     { let the user know that we're finished }
  141.     Screen.Cursor := crDefault;
  142.   end;
  143. end;
  144.  
  145.  
  146. procedure TDM1.tblHoldingsAfterPost(DataSet: TDataSet);
  147. var
  148.   bmCurrent : TBookmark;  { Holds the current position }
  149. begin
  150.   with tblHoldings do
  151.   begin
  152.     bmCurrent := getBookmark;       { save position }
  153.     try
  154.       CalculateTotals(nil, nil);  { recalc totals }
  155.       gotoBookmark(bmCurrent);     { restore position }
  156.     finally;
  157.       freeBookmark(bmCurrent);     { free memory }
  158.     end;
  159.   end;
  160. end;
  161.  
  162. procedure TDM1.tblHoldingsAfterOpen(DataSet: TDataSet);
  163. begin
  164.   {Don't want this calculation to occur until both master & detail are open}
  165.   dsMaster.OnDataChange := CalculateTotals;
  166. end;
  167.  
  168. end.
  169.