home *** CD-ROM | disk | FTP | other *** search
/ PC Format Collection 48 / SENT14D.ISO / tech / delphi / disk15 / mastapp.pak / MAIN.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-08-24  |  9.6 KB  |  326 lines

  1. {                           MastApp Main Window.
  2.  
  3.   By default, the database component's alias is DBDEMOS, and so the
  4.   application accesses the Paradox tables. To upsize the application
  5.   to Local Interbase Server, take the following steps:
  6.  
  7.   1   Pull down the Tools menu, launch Database Desktop and select its
  8.       File|Aliases... to create a new alias. Press the NEW button and name
  9.       the alias MASTSQL. Set the alias type to INTRBASE and specify a server
  10.       name of C:\DELPHI\DEMOS\DATA\MASTSQL.GDB. Set the user name to SYSDBA
  11.       and then press KEEP NEW.
  12.  
  13.   2   Enter the password "masterkey" (it MUST be entered in lowercase) and
  14.       press CONNECT in order to validate the alias you have just created.
  15.       (If you cannot connect, make sure the IBLOCAL bin directory is on your
  16.       DOS path and you specified the correct location for the MASTSQL.GDB
  17.       file. If you still cannot connect, refer to INSTALL.TXT and README.TXT
  18.       for more information.)
  19.  
  20.       Press DISCONNECT, OK and then answer YES when asked whether to save
  21.       these changes in your configuration file.
  22.  
  23.   3   Close the Database Desktop and return to Delphi. Close the project (in
  24.       order to close all open forms) then and re-open it by selecting
  25.       MASTAPP.DPR from the pick list at the bottom of the File menu.
  26.       Select the database component in the lower left corner of the MainForm
  27.       and set its Connected property to False. Click on the AliasName property's
  28.       dropdown list and select MASTSQL. (If MASTSQL is not listed, go back to
  29.       the Database Desktop and make sure you can connect from there). Finally,
  30.       set the Database component's Connected property to True, enter the
  31.       password ("masterkey") and run.
  32.  
  33.   You've just "upsized" MASTAPP to a client-server application! To switch
  34.   between the desktop and SQL versions, close all units and forms (the easiest
  35.   way is to close and re-open the project as instructed above), use the
  36.   Object Inspector to set MainForm.Database's Connected property to False, pick
  37.   an AliasName (DBDEMOS to use the Paradox database, MASTSQL to use
  38.   the Local InterBase Server version), and set Connected to True.
  39.   Now you're ready to run.
  40. }
  41.  
  42. unit Main;
  43.  
  44. interface
  45.  
  46. uses
  47.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  48.   Forms, Dialogs, Buttons, StdCtrls, Menus, ExtCtrls, DB, Report;
  49.  
  50. type
  51.   TMainForm = class(TForm)
  52.     MainPanel: TPanel;
  53.     OrderBtn: TSpeedButton;
  54.     BrowseBtn: TSpeedButton;
  55.     PartsBtn: TSpeedButton;
  56.     CloseBtn: TSpeedButton;
  57.     ReportBtn: TSpeedButton;
  58.     Database: TDatabase;
  59.     MainMenu1: TMainMenu;
  60.     File1: TMenuItem;
  61.     PrinterSetup1: TMenuItem;
  62.     N3: TMenuItem;
  63.     N4: TMenuItem;
  64.     NewOrder2: TMenuItem;
  65.     N5: TMenuItem;
  66.     View1: TMenuItem;
  67.     Orders1: TMenuItem;
  68.     PartsInventiry1: TMenuItem;
  69.     Help1: TMenuItem;
  70.     About1: TMenuItem;
  71.     N7: TMenuItem;
  72.     StayOnTop2: TMenuItem;
  73.     PrintReport1: TMenuItem;
  74.     HelpBtn: TSpeedButton;
  75.     Report: TReport;
  76.     Report11: TMenuItem;
  77.     Report21: TMenuItem;
  78.     N6: TMenuItem;
  79.     Contents1: TMenuItem;
  80.     PrinterSetup: TPrinterSetupDialog;
  81.     Invoice: TMenuItem;
  82.     procedure BrowseCustOrd(Sender: TObject);
  83.     procedure CloseApp(Sender: TObject);
  84.     procedure FormCreate(Sender: TObject);
  85.     procedure BrowseParts(Sender: TObject);
  86.     procedure ToggleStayonTop(Sender: TObject);
  87.     procedure NewOrder(Sender: TObject);
  88.     procedure HelpBtnClick(Sender: TObject);
  89.     procedure PrinterSetupClick(Sender: TObject);
  90.     procedure AboutClick(Sender: TObject);
  91.     procedure FormDestroy(Sender: TObject);
  92.     procedure ReportBtnClick(Sender: TObject);
  93.     procedure OrderReport(Sender: TObject);
  94.     procedure CustomerReport(Sender: TObject);
  95.     procedure InvoiceReport(Sender: TObject);
  96.   private
  97.     procedure UpdateRSConnect(const Dbpath:string);
  98.     procedure InitRSRUN;
  99.     procedure SetReportPreview;
  100.   end;
  101.  
  102. var
  103.   MainForm: TMainForm;
  104.  
  105. implementation
  106.  
  107. uses CustOrd, BrParts, EdOrders, About, PickRep, Custqry, IniFiles;
  108.  
  109. {$R *.DFM}
  110.  
  111. procedure TMainForm.BrowseCustOrd(Sender: TObject);
  112. begin
  113.   BrCustOrdForm.Show;
  114. end;
  115.  
  116. procedure TMainForm.CloseApp(Sender: TObject);
  117. begin
  118.   Close;
  119. end;
  120.  
  121. procedure TMainForm.UpdateRSConnect(const Dbpath: string);
  122. const
  123.   TiniFilename = 'RPTSMITH.CON';   {ReportSmith connections file}
  124.   AppConTitle = 'MASTAPP';
  125.   SQLConTitle = 'MASTSQL';
  126.   ConnectNamesSection = 'ConnectNamesSection';
  127.   ConnectNamesKey = 'ConnectNames';
  128.   MASTAPPSection = 'MASTAPP';
  129.   MASTSQLSection = 'MASTSQL';
  130.   TypeKey = 'Type';
  131.   ServerKey = 'Server';
  132.   SQLTypeVal = 67;
  133.   SQLServerVal = 'IBLOCAL';
  134.   SQLDataFilePathKey = 'Database';
  135.   SQLUseridKey = 'USERID';
  136.   SQLUseridVal = 'SYSDBA';
  137.   TypeVal = 61;
  138.   ServerVal = 'PARADOX';
  139.   DataFilePathKey = 'DataFilePath';
  140. var
  141.   TempStr,
  142.   ConFile: string[127];
  143.   RSCON: TIniFile;
  144. begin
  145.  { the ReportSmith CON file is actually an INI file -- assumes in win dir}
  146.   RSCon := TIniFile.Create(TiniFilename);
  147.   TempStr := RSCon.ReadString(ConnectNamesSection, ConnectNamesKey, '');
  148.   if Database.IsSQLBased then { CON file contents differs for SQL connections }
  149.   begin
  150.     if Pos(SQLConTitle,TempStr) = 0 then
  151.     begin
  152.       if TempStr <> '' then
  153.         TempStr := TempStr + ',';
  154.       RSCon.WriteString(ConnectNamesSection, ConnectNamesKey, TempStr+SQLConTitle);
  155.     end;
  156.     RSCon.WriteInteger(MASTSQLSection, TypeKey, SQLTypeVal);
  157.     RSCon.WriteString(MASTSQLSection, SQLDataFilePathKey, DBpath);
  158.     RSCon.WriteString(MASTSQLSection, ServerKey, SQLServerVal);
  159.     RSCon.WriteString(MASTSQLSection, SQLUseridKey, SQLUseridVal);
  160.   end
  161.   else
  162.   begin
  163.     if Pos(AppConTitle,TempStr) = 0 then
  164.     begin
  165.       if TempStr <> '' then
  166.         TempStr := TempStr + ',';
  167.       RSCon.WriteString(ConnectNamesSection, ConnectNamesKey, TempStr+AppConTitle);
  168.     end;
  169.     RSCon.WriteInteger(MASTAPPSection, TypeKey, TypeVal);
  170.     RSCon.WriteString(MASTAPPSection, DataFilePathKey, DBpath);
  171.     RSCon.WriteString(MASTAPPSection, ServerKey, ServerVal);
  172.   end;
  173.   RSCon.Free;
  174. end;
  175.  
  176. procedure TMainForm.InitRSRUN;
  177. var
  178.   ParamList: TStringList;
  179.   DBPath: string[127];
  180. begin
  181.   Report.ReportDir := ExtractFilePath(Application.ExeName);
  182.   { get the actual location of the database from the alias,
  183.     the path is needed for the reports -- assumes alias is defined }
  184.   ParamList := TStringList.Create;
  185.   try
  186.     Session.GetAliasParams(Database.AliasName, ParamList);
  187.     if Database.IsSQLBased then
  188.       DBPath := ParamList.Values['SERVER NAME']
  189.     else
  190.       DBPath := ParamList.Values['PATH'];
  191.   finally
  192.     ParamList.Free;
  193.   end;
  194.   { set up the ReportSmith "connection" identifying the database location }
  195.   UpdateRSConnect(DBPath);
  196. end;
  197.  
  198. procedure TMainForm.FormCreate(Sender: TObject);
  199. var W: longint;
  200. begin
  201.   ClientWidth := CloseBtn.Left + CloseBtn.Width - 1;
  202.   ClientHeight := CloseBtn.Top + CloseBtn.Height - 1;
  203.   MainPanel.Align := alClient;
  204.   { position the form at the top of display }
  205.   Left := 0;
  206.   Top := 0;
  207.   { initialize ReportSmith }
  208.   InitRSRUN;
  209. end;
  210.  
  211. procedure TMainForm.BrowseParts(Sender: TObject);
  212. begin
  213.   BrPartsForm.Show;
  214. end;
  215.  
  216.  
  217. procedure TMainForm.ToggleStayonTop(Sender: TObject);
  218. begin
  219.   with Sender as TMenuItem do
  220.   begin
  221.     Checked := not Checked;
  222.     if Checked then MainForm.FormStyle := fsStayOnTop
  223.     else MainForm.FormStyle := fsNormal;
  224.   end;
  225. end;
  226.  
  227. procedure TMainForm.NewOrder(Sender: TObject);
  228. begin
  229.   OrderForm.Enter;
  230.   ClientHeight := CloseBtn.Height - 1;
  231. end;
  232.  
  233. procedure TMainForm.HelpBtnClick(Sender: TObject);
  234. begin
  235.   Application.HelpCommand(HELP_CONTENTS,0);
  236. end;
  237.  
  238. procedure TMainForm.PrinterSetupClick(Sender: TObject);
  239. begin
  240.   PrinterSetup.Execute;
  241. end;
  242.  
  243. procedure TMainForm.AboutClick(Sender: TObject);
  244. begin
  245.   AboutBox.ShowModal;
  246. end;
  247.  
  248. procedure TMainForm.FormDestroy(Sender: TObject);
  249. begin
  250.   Application.HelpCommand(HELP_QUIT,0);
  251. end;
  252.  
  253. procedure TMainForm.ReportBtnClick(Sender: TObject);
  254. begin
  255.   if PickRpt.ShowModal = mrOK then
  256.   begin
  257.     if PickRpt.OrderRadBtn.Checked then
  258.       OrderReport(nil)
  259.     else if PickRpt.CustRadBtn.Checked then
  260.       CustomerReport(nil)
  261.     else
  262.       InvoiceReport(nil);
  263.     Report.Preview := False;
  264.   end;
  265. end;
  266.  
  267. procedure TMainForm.SetReportPreview;
  268. begin
  269.   if Report.Preview then
  270.      ShowWindow(Report.ReportHandle, SW_SHOWNORMAL)
  271.   else
  272.      ShowWindow(Report.ReportHandle, SW_SHOWMINIMIZED);
  273. end;
  274.  
  275. procedure TMainForm.OrderReport(Sender: TObject);
  276. begin
  277.   with CustQueryDlg do
  278.   begin
  279.     MsgLab.Caption := 'Print all orders ranging:';
  280.     if FromDate = 0 then FromDate := Now;
  281.     if ToDate = 0 then ToDate := Now;
  282.     if ShowModal = mrOk then with Report do
  283.     begin
  284.       if Database.IsSQLBased then
  285.         ReportName := 'ORDERSQL.RPT'
  286.       else
  287.         ReportName := 'ORDERHST.RPT';
  288.       InitialValues.Clear;
  289.       InitialValues.Add('@startdate=<''' + FormatDateTime('mm"/"dd"/"yy', FromDate) + '''>');
  290.       InitialValues.Add('@enddate=<''' + FormatDateTime('mm"/"dd"/"yy', ToDate) + '''>');
  291.       SetReportPreview;
  292.       Run;
  293.     end;
  294.   end;
  295. end;
  296.  
  297. procedure TMainForm.CustomerReport(Sender: TObject);
  298. begin
  299.   with Report do
  300.   begin
  301.     InitialValues.Clear;
  302.     if Database.IsSQLBased then
  303.       ReportName := 'CUSTSQL.RPT'
  304.     else
  305.       ReportName := 'CUSTLIST.RPT';
  306.     SetReportPreview;
  307.     Run;
  308.   end;
  309. end;
  310.  
  311. procedure TMainForm.InvoiceReport(Sender: TObject);
  312. begin
  313.   with Report do
  314.   begin
  315.     InitialValues.Clear;
  316.     if Database.IsSQLBased then
  317.       ReportName := 'INVSQL.RPT'
  318.     else
  319.       ReportName := 'INVOICE.RPT';
  320.     SetReportPreview;
  321.     Run;
  322.   end;
  323. end;
  324.  
  325. end.
  326.