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

  1. {
  2.                      MastApp Main Window.
  3.  
  4.   By default, the database component's alias is DBDEMOS, and so the
  5.   application accesses the Paradox tables. You can upsize the
  6.   application to use Local InterBase data by choosing View | Remote
  7.   Data from the application's main menu.  For this to work, however,
  8.   the Local InterBase Server must be running.  The application checks
  9.   for this and raises an exception if there's a problem.
  10.  
  11.   The application also creates the MASTSQL alias if it doesn't
  12.   already exist.  This code for this appears in the MastData module.
  13.   
  14. }
  15.  
  16. unit Main;
  17.  
  18. interface
  19.  
  20. uses
  21.   SysUtils, Windows, Messages, Classes, Graphics, Controls,
  22.   Forms, Dialogs, Buttons, StdCtrls, Menus, ExtCtrls, DB, Report;
  23.  
  24. type
  25.   TMainForm = class(TForm)
  26.     MainPanel: TPanel;
  27.     Report: TReport;
  28.     PrinterSetup: TPrinterSetupDialog;
  29.     OrderBtn: TSpeedButton;
  30.     BrowseBtn: TSpeedButton;
  31.     PartsBtn: TSpeedButton;
  32.     CloseBtn: TSpeedButton;
  33.     ReportBtn: TSpeedButton;
  34.     HelpBtn: TSpeedButton;
  35.     MainMenu: TMainMenu;
  36.     FileMenu: TMenuItem;
  37.     FilePrinterSetup: TMenuItem;
  38.     FileExit: TMenuItem;
  39.     FileNewOrder: TMenuItem;
  40.     FilePrintReport: TMenuItem;
  41.     PrintCustList: TMenuItem;
  42.     PrintOrders: TMenuItem;
  43.     PrintInvoice: TMenuItem;
  44.     ViewMenu: TMenuItem;
  45.     ViewOrders: TMenuItem;
  46.     ViewPartsInventory: TMenuItem;
  47.     ViewStayOnTop: TMenuItem;
  48.     ViewLocal: TMenuItem;
  49.     ViewRemote: TMenuItem;
  50.     HelpMenu: TMenuItem;
  51.     HelpAbout: TMenuItem;
  52.     HelpContents: TMenuItem;
  53.     procedure BrowseCustOrd(Sender: TObject);
  54.     procedure CloseApp(Sender: TObject);
  55.     procedure FormCreate(Sender: TObject);
  56.     procedure BrowseParts(Sender: TObject);
  57.     procedure ToggleStayonTop(Sender: TObject);
  58.     procedure NewOrder(Sender: TObject);
  59.     procedure HelpBtnClick(Sender: TObject);
  60.     procedure PrinterSetupClick(Sender: TObject);
  61.     procedure AboutClick(Sender: TObject);
  62.     procedure FormDestroy(Sender: TObject);
  63.     procedure ReportBtnClick(Sender: TObject);
  64.     procedure OrderReport(Sender: TObject);
  65.     procedure CustomerReport(Sender: TObject);
  66.     procedure InvoiceReport(Sender: TObject);
  67.     procedure ViewLocalClick(Sender: TObject);
  68.     procedure ViewRemoteClick(Sender: TObject);
  69.     procedure ViewMenuClick(Sender: TObject);
  70.   private
  71.     procedure CloseAllWindows;
  72.     procedure UpdateRSConnect(const Dbpath:string);
  73.     procedure InitRSRUN;
  74.     procedure SetReportPreview;
  75.   end;
  76.  
  77. var
  78.   MainForm: TMainForm;
  79.  
  80. implementation
  81.  
  82. uses
  83.    DataMod,  { Data Module }
  84.    BrCstOrd, { The Browse Orders by Customer form }
  85.    BrParts,  { The Browse Parts form }
  86.    EdOrders, { The Edit Orders form }
  87.    QryCust,  { The Specify Date Range form }
  88.    PickRep,  { The Report Selection form }
  89.    About,    { The About dialog box }
  90.    IniFiles; { Delphi Unit for INI file support }
  91.  
  92. {$R *.DFM}
  93.  
  94. procedure TMainForm.BrowseCustOrd(Sender: TObject);
  95. begin
  96.   BrCustOrdForm.Show;
  97. end;
  98.  
  99. procedure TMainForm.CloseApp(Sender: TObject);
  100. begin
  101.   Close;
  102. end;
  103.  
  104. procedure TMainForm.UpdateRSConnect(const Dbpath: string);
  105. const
  106.   TiniFilename = 'RPTSMITH.CON';   {ReportSmith connections file}
  107.   AppConTitle = 'MASTAPP';
  108.   SQLConTitle = 'MASTSQL';
  109.   ConnectNamesSection = 'ConnectNamesSection';
  110.   ConnectNamesKey = 'ConnectNames';
  111.   MASTAPPSection = 'MASTAPP';
  112.   MASTSQLSection = 'MASTSQL';
  113.   TypeKey = 'Type';
  114.   ServerKey = 'Server';
  115.   SQLTypeVal = 67;
  116.   SQLServerVal = 'MASTSQL';
  117.   SQLDataFilePathKey = 'Database';
  118.   SQLUseridKey = 'USERID';
  119.   SQLUseridVal = 'SYSDBA';
  120.   TypeVal = 61;
  121.   ServerVal = 'PARADOX';
  122.   DataFilePathKey = 'DataFilePath';
  123. var
  124.   TempStr,
  125.   ConFile: string;
  126.   RSCON: TIniFile;
  127. begin
  128.  { the ReportSmith CON file is actually an INI file -- assumes in win dir}
  129.   RSCon := TIniFile.Create(TiniFilename);
  130.   TempStr := RSCon.ReadString(ConnectNamesSection, ConnectNamesKey, '');
  131.   { CON file contents differs for SQL connections }
  132.   if MastData.Database.IsSQLBased then
  133.   begin
  134.     if Pos(SQLConTitle,TempStr) = 0 then
  135.     begin
  136.       if TempStr <> '' then
  137.         TempStr := TempStr + ',';
  138.       RSCon.WriteString(ConnectNamesSection, ConnectNamesKey, TempStr+SQLConTitle);
  139.     end;
  140.     RSCon.WriteInteger(MASTSQLSection, TypeKey, SQLTypeVal);
  141.     RSCon.WriteString(MASTSQLSection, SQLDataFilePathKey, DBpath);
  142.     RSCon.WriteString(MASTSQLSection, ServerKey, SQLServerVal);
  143.     RSCon.WriteString(MASTSQLSection, SQLUseridKey, SQLUseridVal);
  144.   end
  145.   else
  146.   begin
  147.     if Pos(AppConTitle,TempStr) = 0 then
  148.     begin
  149.       if TempStr <> '' then
  150.         TempStr := TempStr + ',';
  151.       RSCon.WriteString(ConnectNamesSection, ConnectNamesKey, TempStr+AppConTitle);
  152.     end;
  153.     RSCon.WriteInteger(MASTAPPSection, TypeKey, TypeVal);
  154.     RSCon.WriteString(MASTAPPSection, DataFilePathKey, DBpath);
  155.     RSCon.WriteString(MASTAPPSection, ServerKey, ServerVal);
  156.   end;
  157.   RSCon.Free;
  158. end;
  159.  
  160. procedure TMainForm.InitRSRUN;
  161. var
  162.   DBPath: string;
  163.   ParamList: TStringList;
  164. begin
  165.   Report.ReportDir := ExtractFilePath(Application.ExeName);
  166.   { get the actual location of the database from the alias,
  167.     the path is needed for the reports -- assumes alias is defined }
  168.   ParamList := TStringList.Create;
  169.   try
  170.     Session.GetAliasParams(MastData.Database.AliasName, ParamList);
  171.     if MastData.Database.IsSQLBased then
  172.       DBPath := ParamList.Values['SERVER NAME']
  173.     else
  174.       DBPath := ParamList.Values['PATH'];
  175.   finally
  176.     ParamList.Free;
  177.   end;
  178.   { set up the ReportSmith "connection" identifying the database location }
  179.   UpdateRSConnect(DBPath);
  180. end;
  181.  
  182. procedure TMainForm.FormCreate(Sender: TObject);
  183. var
  184.   W: Longint;
  185. begin
  186.   ClientWidth := CloseBtn.Left + CloseBtn.Width + 1;
  187.   ClientHeight := CloseBtn.Top + CloseBtn.Height;
  188.   MainPanel.Align := alClient;
  189.   { position the form at the top of display }
  190.   Left := 0;
  191.   Top := 0;
  192.   { initialize ReportSmith }
  193.   InitRSRUN;
  194. end;
  195.  
  196. procedure TMainForm.BrowseParts(Sender: TObject);
  197. begin
  198.   BrPartsForm.Show;
  199. end;
  200.  
  201.  
  202. procedure TMainForm.ToggleStayonTop(Sender: TObject);
  203. begin
  204.   with Sender as TMenuItem do
  205.   begin
  206.     Checked := not Checked;
  207.     if Checked then MainForm.FormStyle := fsStayOnTop
  208.     else MainForm.FormStyle := fsNormal;
  209.   end;
  210. end;
  211.  
  212. procedure TMainForm.NewOrder(Sender: TObject);
  213. begin
  214.   EdOrderForm.Enter;
  215. end;
  216.  
  217. procedure TMainForm.HelpBtnClick(Sender: TObject);
  218. begin
  219.   Application.HelpCommand(HELP_CONTENTS, 0);
  220. end;
  221.  
  222. procedure TMainForm.PrinterSetupClick(Sender: TObject);
  223. begin
  224.   PrinterSetup.Execute;
  225. end;
  226.  
  227. procedure TMainForm.AboutClick(Sender: TObject);
  228. begin
  229.   AboutBox.ShowModal;
  230. end;
  231.  
  232. procedure TMainForm.FormDestroy(Sender: TObject);
  233. begin
  234.   Application.HelpCommand(HELP_QUIT,0);
  235. end;
  236.  
  237. procedure TMainForm.ReportBtnClick(Sender: TObject);
  238. begin
  239.   if PickRpt.ShowModal = mrOK then
  240.   begin
  241.     if PickRpt.OrderRadBtn.Checked then
  242.       OrderReport(nil)
  243.     else if PickRpt.CustRadBtn.Checked then
  244.       CustomerReport(nil)
  245.     else
  246.       InvoiceReport(nil);
  247.     Report.Preview := False;
  248.   end;
  249. end;
  250.  
  251. procedure TMainForm.SetReportPreview;
  252. begin
  253.   if Report.Preview then
  254.      ShowWindow(Report.ReportHandle, SW_SHOWNORMAL)
  255.   else
  256.      ShowWindow(Report.ReportHandle, SW_SHOWMINIMIZED);
  257. end;
  258.  
  259. procedure TMainForm.OrderReport(Sender: TObject);
  260. begin
  261.   with QueryCustDlg do
  262.   begin
  263.     MsgLab.Caption := 'Print all orders ranging:';
  264.     if FromDate = 0 then FromDate := EncodeDate(95, 01, 01);
  265.     if ToDate = 0 then ToDate := Now;
  266.     if ShowModal = mrOk then with Report do
  267.     begin
  268.       if MastData.Database.IsSQLBased then
  269.         ReportName := 'ORDERSQL.RPT'
  270.       else
  271.         ReportName := 'ORDERHST.RPT';
  272.       InitialValues.Clear;
  273.       InitialValues.Add('@startdate=<''' + FormatDateTime('mm"/"dd"/"yy',
  274.         FromDate) + '''>');
  275.       InitialValues.Add('@enddate=<''' + FormatDateTime('mm"/"dd"/"yy',
  276.         ToDate) + '''>');
  277.       SetReportPreview;
  278.       Run;
  279.     end;
  280.   end;
  281. end;
  282.  
  283. procedure TMainForm.CustomerReport(Sender: TObject);
  284. begin
  285.   with Report do
  286.   begin
  287.     InitialValues.Clear;
  288.     if MastData.Database.IsSQLBased then
  289.       ReportName := 'CUSTSQL.RPT'
  290.     else
  291.       ReportName := 'CUSTLIST.RPT';
  292.     SetReportPreview;
  293.     Run;
  294.   end;
  295. end;
  296.  
  297. procedure TMainForm.InvoiceReport(Sender: TObject);
  298. begin
  299.   with Report do
  300.   begin
  301.     InitialValues.Clear;
  302.     if MastData.Database.IsSQLBased then
  303.       ReportName := 'INVSQL.RPT'
  304.     else
  305.       ReportName := 'INVOICE.RPT';
  306.     SetReportPreview;
  307.     Run;
  308.   end;
  309. end;
  310.  
  311. procedure TMainForm.CloseAllWindows;
  312. var
  313.   I: Integer;
  314.   F: TForm;
  315. begin
  316.   for I := 0 to Application.ComponentCount - 1 do
  317.   begin
  318.     if Application.Components[I] is TForm then
  319.     begin
  320.       F := TForm(Application.Components[I]);
  321.       if (F <> Self) and (F.Visible) then F.Close;
  322.     end;
  323.   end;
  324. end;
  325.  
  326. procedure TMainForm.ViewLocalClick(Sender: TObject);
  327. begin
  328.   CloseAllWindows;
  329.   MastData.UseLocalData;
  330.   ViewLocal.Checked := True;
  331.   Caption := Application.Title + ' (Paradox Data)';
  332. end;
  333.  
  334. procedure TMainForm.ViewRemoteClick(Sender: TObject);
  335. begin
  336.   CloseAllWindows;
  337.   MastData.UseRemoteData;
  338.   ViewRemote.Checked := True;
  339.   Caption := Application.Title + ' (Local Interbase)';
  340. end;
  341.  
  342. procedure TMainForm.ViewMenuClick(Sender: TObject);
  343. begin
  344.   { Enable the Remote data menu item only if local Interbase is running }
  345.   ViewRemote.Enabled := FindWindow(NIL, 'InterBase Server') <> 0;
  346. end;
  347.  
  348. end.
  349.