home *** CD-ROM | disk | FTP | other *** search
/ Delphi 5 for Professionals / DELPHI5.iso / Runimage / Delphi50 / Demos / Db / Mastapp / main.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-08-11  |  10.4 KB  |  384 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, DBTables;
  23.  
  24. type
  25.   TDateOrder = (doMDY, doDMY, doYMD);
  26.  
  27.   TMainForm = class(TForm)
  28.     MainPanel: TPanel;
  29.     PrinterSetup: TPrinterSetupDialog;
  30.     OrderBtn: TSpeedButton;
  31.     BrowseBtn: TSpeedButton;
  32.     PartsBtn: TSpeedButton;
  33.     CloseBtn: TSpeedButton;
  34.     ReportBtn: TSpeedButton;
  35.     HelpBtn: TSpeedButton;
  36.     MainMenu: TMainMenu;
  37.     FileMenu: TMenuItem;
  38.     FilePrinterSetup: TMenuItem;
  39.     FileExit: TMenuItem;
  40.     FileNewOrder: TMenuItem;
  41.     FilePrintReport: TMenuItem;
  42.     PrintCustList: TMenuItem;
  43.     PrintOrders: TMenuItem;
  44.     PrintInvoice: TMenuItem;
  45.     ViewMenu: TMenuItem;
  46.     ViewOrders: TMenuItem;
  47.     ViewPartsInventory: TMenuItem;
  48.     ViewStayOnTop: TMenuItem;
  49.     ViewLocal: TMenuItem;
  50.     ViewRemote: TMenuItem;
  51.     HelpMenu: TMenuItem;
  52.     HelpAbout: TMenuItem;
  53.     HelpContents: TMenuItem;
  54.     procedure BrowseCustOrd(Sender: TObject);
  55.     procedure CloseApp(Sender: TObject);
  56.     procedure FormCreate(Sender: TObject);
  57.     procedure BrowseParts(Sender: TObject);
  58.     procedure ToggleStayonTop(Sender: TObject);
  59.     procedure NewOrder(Sender: TObject);
  60.     procedure HelpBtnClick(Sender: TObject);
  61.     procedure PrinterSetupClick(Sender: TObject);
  62.     procedure AboutClick(Sender: TObject);
  63.     procedure FormDestroy(Sender: TObject);
  64.     procedure ReportBtnClick(Sender: TObject);
  65.     procedure OrderReport(Sender: TObject);
  66.     procedure CustomerReport(Sender: TObject);
  67.     procedure InvoiceReport(Sender: TObject);
  68.     procedure ViewLocalClick(Sender: TObject);
  69.     procedure ViewRemoteClick(Sender: TObject);
  70.     procedure ViewMenuClick(Sender: TObject);
  71.   private
  72.     procedure PrintOrderReport(Preview: Boolean);
  73.     procedure PrintInvoiceReport(Preview: Boolean);
  74.     procedure PrintCustomerReport(Preview: Boolean);
  75.     procedure CloseAllWindows;
  76.     procedure UpdateRSConnect(const Dbpath:string);
  77.     procedure InitRSRUN;
  78.   end;
  79.  
  80. var
  81.   MainForm: TMainForm;
  82.  
  83. implementation
  84.  
  85. uses
  86.    DataMod,  { Data Module }
  87.    BrCstOrd, { The Browse Orders by Customer form }
  88.    BrParts,  { The Browse Parts form }
  89.    EdOrders, { The Edit Orders form }
  90.    QryCust,  { The Specify Date Range form }
  91.    PickRep,  { The Report Selection form }
  92.    About,    { The About dialog box }
  93.    IniFiles, { Delphi Unit for INI file support }
  94.    CustRpt,  { The customer by invoice report }
  95.    OrderRpt, { The orders by ship date report }
  96.    InvcRpt,  { The invoice report }
  97.    PickInvc; { The invoice number selection dialog }
  98.  
  99. {$R *.DFM}
  100.  
  101. function GetDateOrder(const DateFormat: string): TDateOrder;
  102. var
  103.   I: Integer;
  104. begin
  105.   Result := doMDY;
  106.   I := 1;
  107.   while I <= Length(DateFormat) do
  108.   begin
  109.     case Chr(Ord(DateFormat[I]) and $DF) of
  110.       'Y': Result := doYMD;
  111.       'M': Result := doMDY;
  112.       'D': Result := doDMY;
  113.     else
  114.       Inc(I);
  115.       Continue;
  116.     end;
  117.     Exit;
  118.   end;
  119.   Result := doMDY;
  120. end;
  121.  
  122. procedure TMainForm.BrowseCustOrd(Sender: TObject);
  123. begin
  124.   case GetDateOrder(ShortDateFormat) of
  125.     doYMD: ShortDateFormat := 'yy/mm/dd';
  126.     doMDY: ShortDateFormat := 'mm/dd/yy';
  127.     doDMY: ShortDateFormat := 'dd/mm/yy';
  128.   end;
  129.   BrCustOrdForm.Show;
  130. end;
  131.  
  132. procedure TMainForm.CloseApp(Sender: TObject);
  133. begin
  134.   Close;
  135. end;
  136.  
  137. procedure TMainForm.UpdateRSConnect(const Dbpath: string);
  138. const
  139.   TiniFilename = 'RPTSMITH.CON';   {ReportSmith connections file}
  140.   AppConTitle = 'MASTAPP';
  141.   SQLConTitle = 'MASTSQL';
  142.   ConnectNamesSection = 'ConnectNamesSection';
  143.   ConnectNamesKey = 'ConnectNames';
  144.   MASTAPPSection = 'MASTAPP';
  145.   MASTSQLSection = 'MASTSQL';
  146.   TypeKey = 'Type';
  147.   ServerKey = 'Server';
  148.   SQLTypeVal = 67;
  149.   SQLServerVal = 'MASTSQL';
  150.   SQLDataFilePathKey = 'Database';
  151.   SQLUseridKey = 'USERID';
  152.   SQLUseridVal = 'SYSDBA';
  153.   TypeVal = 61;
  154.   ServerVal = 'PARADOX';
  155.   DataFilePathKey = 'DataFilePath';
  156. var
  157.   TempStr: string;
  158.   RSCON: TIniFile;
  159. begin
  160.  { the ReportSmith CON file is actually an INI file -- assumes in win dir}
  161.   RSCon := TIniFile.Create(TiniFilename);
  162.   TempStr := RSCon.ReadString(ConnectNamesSection, ConnectNamesKey, '');
  163.   { CON file contents differs for SQL connections }
  164.   if MastData.Database.IsSQLBased then
  165.   begin
  166.     if AnsiPos(SQLConTitle,TempStr) = 0 then
  167.     begin
  168.       if TempStr <> '' then
  169.         TempStr := TempStr + ',';
  170.       RSCon.WriteString(ConnectNamesSection, ConnectNamesKey, TempStr+SQLConTitle);
  171.     end;
  172.     RSCon.WriteInteger(MASTSQLSection, TypeKey, SQLTypeVal);
  173.     RSCon.WriteString(MASTSQLSection, SQLDataFilePathKey, DBpath);
  174.     RSCon.WriteString(MASTSQLSection, ServerKey, SQLServerVal);
  175.     RSCon.WriteString(MASTSQLSection, SQLUseridKey, SQLUseridVal);
  176.   end
  177.   else
  178.   begin
  179.     if AnsiPos(AppConTitle,TempStr) = 0 then
  180.     begin
  181.       if TempStr <> '' then
  182.         TempStr := TempStr + ',';
  183.       RSCon.WriteString(ConnectNamesSection, ConnectNamesKey, TempStr+AppConTitle);
  184.     end;
  185.     RSCon.WriteInteger(MASTAPPSection, TypeKey, TypeVal);
  186.     RSCon.WriteString(MASTAPPSection, DataFilePathKey, DBpath);
  187.     RSCon.WriteString(MASTAPPSection, ServerKey, ServerVal);
  188.   end;
  189.   RSCon.Free;
  190. end;
  191.  
  192. procedure TMainForm.InitRSRUN;
  193. var
  194.   DBPath: string;
  195.   ParamList: TStringList;
  196. begin
  197.   { get the actual location of the database from the alias,
  198.     the path is needed for the reports -- assumes alias is defined }
  199.   ParamList := TStringList.Create;
  200.   try
  201.     Session.GetAliasParams(MastData.Database.AliasName, ParamList);
  202.     if MastData.Database.IsSQLBased then
  203.       DBPath := ParamList.Values['SERVER NAME']
  204.     else
  205.       DBPath := ParamList.Values['PATH'];
  206.   finally
  207.     ParamList.Free;
  208.   end;
  209.   { set up the ReportSmith "connection" identifying the database location }
  210.   UpdateRSConnect(DBPath);
  211. end;
  212.  
  213. procedure TMainForm.FormCreate(Sender: TObject);
  214. begin
  215.   ClientWidth := CloseBtn.Left + CloseBtn.Width + 1;
  216.   ClientHeight := CloseBtn.Top + CloseBtn.Height;
  217.   MainPanel.Align := alClient;
  218.   { position the form at the top of display }
  219.   Left := 0;
  220.   Top := 0;
  221.   { initialize ReportSmith }
  222.   InitRSRUN;
  223. end;
  224.  
  225. procedure TMainForm.BrowseParts(Sender: TObject);
  226. begin
  227.   BrPartsForm.Show;
  228. end;
  229.  
  230.  
  231. procedure TMainForm.ToggleStayonTop(Sender: TObject);
  232. begin
  233.   with Sender as TMenuItem do
  234.   begin
  235.     Checked := not Checked;
  236.     if Checked then MainForm.FormStyle := fsStayOnTop
  237.     else MainForm.FormStyle := fsNormal;
  238.   end;
  239. end;
  240.  
  241. procedure TMainForm.NewOrder(Sender: TObject);
  242. begin
  243.   EdOrderForm.Enter;
  244. end;
  245.  
  246. procedure TMainForm.HelpBtnClick(Sender: TObject);
  247. begin
  248.   Application.HelpCommand(HELP_CONTENTS, 0);
  249. end;
  250.  
  251. procedure TMainForm.PrinterSetupClick(Sender: TObject);
  252. begin
  253.   PrinterSetup.Execute;
  254. end;
  255.  
  256. procedure TMainForm.AboutClick(Sender: TObject);
  257. begin
  258.   AboutBox.ShowModal;
  259. end;
  260.  
  261. procedure TMainForm.FormDestroy(Sender: TObject);
  262. begin
  263.   Application.HelpCommand(HELP_QUIT,0);
  264. end;
  265.  
  266. procedure TMainForm.CloseAllWindows;
  267. var
  268.   I: Integer;
  269.   F: TForm;
  270. begin
  271.   for I := 0 to Application.ComponentCount - 1 do
  272.   begin
  273.     if Application.Components[I] is TForm then
  274.     begin
  275.       F := TForm(Application.Components[I]);
  276.       if (F <> Self) and (F.Visible) then F.Close;
  277.     end;
  278.   end;
  279. end;
  280.  
  281. procedure TMainForm.ViewLocalClick(Sender: TObject);
  282. begin
  283.   CloseAllWindows;
  284.   MastData.UseLocalData;
  285.   ViewLocal.Checked := True;
  286.   Caption := Application.Title + ' (Paradox Data)';
  287. end;
  288.  
  289. procedure TMainForm.ViewRemoteClick(Sender: TObject);
  290. begin
  291.   CloseAllWindows;
  292.   MastData.UseRemoteData;
  293.   ViewRemote.Checked := True;
  294.   Caption := Application.Title + ' (Local Interbase)';
  295. end;
  296.  
  297. procedure TMainForm.ViewMenuClick(Sender: TObject);
  298. begin
  299.   { Enable the Remote data menu item only if local Interbase is running }
  300.   ViewRemote.Enabled := FindWindow(NIL, 'InterBase Server') <> 0;
  301. end;
  302.  
  303. procedure TMainForm.ReportBtnClick(Sender: TObject);
  304. begin
  305.   with PickRpt do
  306.   if ShowModal = mrOK then
  307.     Case ReportType.ItemIndex of
  308.       0: PrintCustomerReport( Preview );
  309.       1: PrintOrderReport( Preview );
  310.       2: PrintInvoiceReport( Preview );
  311.     end;
  312. end;
  313.  
  314. procedure TMainForm.PrintCustomerReport(Preview: Boolean);
  315. begin
  316.   with MastData.CustByLastInvQuery do
  317.   begin
  318.     Open;
  319.     if Preview then
  320.        CustomerByInvoiceReport.Preview
  321.     else
  322.        CustomerByInvoiceReport.Print;
  323.     Close;
  324.   end;
  325. end;
  326.  
  327. procedure TMainForm.PrintOrderReport(Preview: Boolean);
  328. const
  329.   FromToHeading = 'From ''%s'' To ''%s''';
  330. begin
  331.   // Request the 'From' and 'To' dates from the user.
  332.   with QueryCustDlg do
  333.   begin
  334.     MsgLab.Caption := 'Print all orders ranging:';
  335.     if FromDate = 0 then FromDate := EncodeDate(95, 01, 01);
  336.     if ToDate = 0 then ToDate := Now;
  337.  
  338.     if ShowModal = mrOk then
  339.     with MastData.OrdersByDateQuery do
  340.     begin
  341.       Close;
  342.       PArams.ParamByName('FromDate').AsDate := FromDate;
  343.       PArams.ParamByName('ToDate').AsDate := ToDate;
  344.       Open;
  345.  
  346.       // Format the From To header with the user's dates entered
  347.       OrdersByDateReport.FromToHeading.Caption :=
  348.         Format(FromToHeading, [DateToStr(FromDate), DateToStr(ToDate)]);
  349.  
  350.       if Preview then
  351.          OrdersByDateReport.Preview
  352.       else
  353.          OrdersByDateReport.Print;
  354.       Close;
  355.      end;
  356.    end;
  357. end;
  358.  
  359. procedure TMainForm.PrintInvoiceReport(Preview: Boolean);
  360. begin
  361.   if PickOrderNoDlg.ShowModal = mrOk then
  362.      if Preview then
  363.         InvoiceByOrderNoReport.Preview
  364.      else
  365.         InvoiceByOrderNoReport.Print;
  366. end;
  367.  
  368. procedure TMainForm.OrderReport(Sender: TObject);
  369. begin
  370.   PrintOrderReport(False); // print-no preview
  371. end;
  372.  
  373. procedure TMainForm.CustomerReport(Sender: TObject);
  374. begin
  375.   PrintCustomerReport(False); // print-no preview
  376. end;
  377.  
  378. procedure TMainForm.InvoiceReport(Sender: TObject);
  379. begin
  380.   PrintInvoiceReport(False); // print-no preview
  381. end;
  382.  
  383. end.
  384.