home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 9 / CDACTUAL9.iso / progs / CB / DATA.Z / DM.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-15  |  4.4 KB  |  131 lines

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