home *** CD-ROM | disk | FTP | other *** search
/ Chip 1997 April / Chip_1997-04_cd.bin / prezent / cb / data.z / DM.CPP < prev    next >
C/C++ Source or Header  |  1997-01-16  |  5KB  |  135 lines

  1. //----------------------------------------------------------------------------
  2. //Borland C++ Builder
  3. //Copyright (c) 1987 Borland International Inc. All Rights Reserved.
  4. //----------------------------------------------------------------------------
  5. //---------------------------------------------------------------------------
  6. #include <vcl.h>
  7. #pragma hdrstop
  8.  
  9. #include "DM.h"
  10. #include "CtrlForm.h"
  11. //---------------------------------------------------------------------------
  12. #pragma resource "*.dfm"
  13. TDM1 *DM1;
  14. //---------------------------------------------------------------------------
  15. __fastcall TDM1::TDM1(TComponent* Owner)
  16.   : TDataModule(Owner)
  17. {
  18. }
  19. //---------------------------------------------------------------------------
  20. void __fastcall TDM1::tblHoldingsAfterOpen(TDataSet *DataSet)
  21. {
  22.   dsMaster->OnDataChange = CalculateTotals;
  23. }
  24. //---------------------------------------------------------------------
  25. void __fastcall TDM1::tblHoldingsAfterPost(TDataSet *DataSet)
  26. {
  27. //    TBookmark bmCurrent;
  28.  
  29. //    bmCurrent = tblHoldings->getBookmark();       { save position }
  30. //    try
  31. //        {
  32.           CalculateTotals(0, 0);  /* recalc totals */
  33. //          tblHoldings->gotoBookmark(bmCurrent);     { restore position }
  34. //       }
  35. //    finally;
  36. //        {
  37. //          tblHoldings->freeBookmark(bmCurrent);     { free memory }
  38.  //     }
  39.  
  40. }
  41. //---------------------------------------------------------------------
  42. void __fastcall TDM1::tblHoldingsCalcFields(TDataSet *DataSet)
  43. {
  44.     tblHoldingsPUR_COST->AsFloat =
  45.     tblHoldingsPUR_PRICE->AsFloat * tblHoldingsSHARES->AsFloat;
  46. }
  47. //---------------------------------------------------------------------
  48.  
  49. #pragma warn -aus
  50. void __fastcall TDM1::CalculateTotals(TObject * Sender, TField * Field)
  51.     {
  52.    float flTotalCost = 0;            /* Holds total share cost */
  53.       float flTotalShares = 0;          /* Holds total share count */
  54.       float flTotalValue = 0;           /* Holds total share value */
  55.       float flDifference = 0;             /* Holds difference between cost and value */
  56.       AnsiString strFormatSpec;    /* The Display Format specification */
  57.  
  58.   /* Update the count of stock transactions */
  59.       FmCtrlGrid->lPurchase->Caption = IntToStr( tblHoldings->RecordCount );
  60.  
  61.   /* See whether or not its necessary to total the holdings and
  62.     (if so) do so and update the result displays; otherwise,
  63.     clear the result displays. */
  64.       if (tblHoldings->RecordCount == 0)
  65.           {
  66.     /* Clear the result displays */
  67.         FmCtrlGrid->lTotalCost->Caption   = "";
  68.         FmCtrlGrid->lTotalShares->Caption = "";
  69.         FmCtrlGrid->lDifference->Caption  = "";
  70.           }
  71.       else
  72.           {
  73.     /* let the user know something's going on */
  74. //        Screen->Cursor = crHourglass;
  75.  
  76.     /* Initialize the holder variables */
  77.     /*
  78.         flTotalCost = 0.0;
  79.         flTotalShares = 0.0;
  80.         flTotalValue = 0.0;
  81.         flDifference = 0.0;
  82.     */
  83.  
  84.     /* Calculate the total cost of these holdings. */
  85.         tblHoldings->DisableControls();  /* hide this process from the user */
  86.         tblHoldings->First();
  87.         while (!tblHoldings->Eof)
  88.            {
  89.               flTotalCost = flTotalCost + tblHoldingsPUR_COST->AsFloat;
  90.               flTotalShares = flTotalShares + tblHoldingsSHARES->AsFloat;
  91.               tblHoldings->Next();
  92.             };
  93.         tblHoldings->First();
  94.         tblHoldings->EnableControls();  /* restore the display of holdings */
  95.  
  96.     /* Calculate the current value of the shares (by multiplying
  97.       the current holdings by the current share price) and the
  98.       difference between the cost and the value. */
  99.  
  100.         flTotalValue = flTotalShares * tblMasterCUR_PRICE->AsFloat;
  101.         flDifference = flTotalValue - flTotalCost;
  102.  
  103.     /* Use the same format specification as that being used to
  104.       display the Current Price field value so it can be used
  105.       to display the results */
  106.  
  107.         strFormatSpec = tblMasterCUR_PRICE->DisplayFormat;
  108.  
  109.     /* Update the result displays */
  110.  
  111.         FmCtrlGrid->lTotalCost->Caption =
  112.            FormatFloat( strFormatSpec, flTotalCost );
  113.         FmCtrlGrid->lTotalShares->Caption =
  114.            FormatFloat( strFormatSpec, flTotalValue );
  115.         FmCtrlGrid->lDifference->Caption =
  116.            FormatFloat( strFormatSpec, flDifference );
  117.  
  118.     /* Update the Font Color of the Diference to
  119.       indicate the quality of the investment */
  120.         if (flDifference > 0)
  121.             {
  122.               FmCtrlGrid->lDifference->Font->Color = clGreen;
  123.            }
  124.         else
  125.             {
  126.               FmCtrlGrid->lDifference->Font->Color = clRed;
  127.            }
  128.         FmCtrlGrid->lDifference->Update();
  129.  
  130.     /* let the user know that we're finished */
  131. //        Screen->Cursor = crDefault;
  132.           }
  133.     }
  134. #pragma warn .aus
  135. //---------------------------------------------------------------------