{*******************************************************} { } { Responsive Software http://www.responsive.co.nz } { } { Copyright (c) 2003-2006 Responsive Software Limited } { } {*******************************************************} unit SalesReportFrameUnit; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, BaseFrameUnit, ComCtrls, StdCtrls, ExtCtrls, ImgList, BusinessObjects; type TSalesReportFrame = class(TBaseFrame) HeadingLabel: TLabel; HeadingShape: TShape; Label2: TLabel; Bevel1: TBevel; Label5: TLabel; Label8: TLabel; Label1: TLabel; Label11: TLabel; Label3: TLabel; BeginSaleDateDateTimePicker: TDateTimePicker; EndSaleDateDateTimePicker: TDateTimePicker; PrintButton: TButton; RadioGroup1: TRadioGroup; OrderBySaleDateRadioButton: TRadioButton; OrderBySalespersonRadioButton: TRadioButton; OrderByItemRadioButton: TRadioButton; OrderByPaymentTypeRadioButton: TRadioButton; SalespersonComboBox: TComboBox; ItemComboBox: TComboBox; TotalsOnlyCheckBox: TCheckBox; Label4: TLabel; PaymentTypeComboBox: TComboBox; EmailButton: TButton; procedure PrintButtonClick(Sender: TObject); procedure BeginSaleDateDateTimePickerKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); procedure BeginSaleDateDateTimePickerEnter(Sender: TObject); procedure BeginSaleDateDateTimePickerCloseUp(Sender: TObject); procedure BeginSaleDateDateTimePickerChange(Sender: TObject); procedure EndSaleDateDateTimePickerKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); procedure EndSaleDateDateTimePickerEnter(Sender: TObject); procedure EndSaleDateDateTimePickerCloseUp(Sender: TObject); procedure EndSaleDateDateTimePickerChange(Sender: TObject); procedure EmailButtonClick(Sender: TObject); private { Private declarations } IgnoreChangeEvents : boolean; // set this to true when updating controls in code FirstChangeEvent : boolean; // used to workaround bug in TDateTimePicker procedure PrintReport (Email : boolean); public { Public declarations } procedure Setup; override; procedure UpdateComboBoxes (Full : boolean); override; end; var SalesReportFrame: TSalesReportFrame; implementation uses GeneralUtilities, DatabaseObjects, DatabaseManager, Globals, Utilities, Main, Base; {$R *.dfm} procedure TSalesReportFrame.Setup; begin IgnoreChangeEvents := true; UpdateComboBoxes(true); // load parameters from workstation configuration if WorkstationConfiguration.SalesReportParameters.OrderBy = diSaleDate then OrderBySaleDateRadioButton.Checked := true else if WorkstationConfiguration.SalesReportParameters.OrderBy = diSalesperson then OrderBySalespersonRadioButton.Checked := true else if WorkstationConfiguration.SalesReportParameters.OrderBy = diItem then OrderByItemRadioButton.Checked := true else if WorkstationConfiguration.SalesReportParameters.OrderBy = diPaymentType then OrderByPaymentTypeRadioButton.Checked := true; TotalsOnlyCheckBox.Checked := WorkstationConfiguration.SalesReportParameters.TotalsOnly; BeginSaleDateDateTimePicker.Date := Date + WorkstationConfiguration.SalesReportParameters.BeginSaleDate; EndSaleDateDateTimePicker.Date := Date + WorkstationConfiguration.SalesReportParameters.EndSaleDate; SalespersonComboBox.ItemIndex := SalespersonComboBox.Items.IndexOf(ComboBoxAllIndicator); ItemComboBox.ItemIndex := ItemComboBox.Items.IndexOf(ComboBoxAllIndicator); PaymentTypeComboBox.ItemIndex := PaymentTypeComboBox.Items.IndexOf(ComboBoxAllIndicator); IgnoreChangeEvents := false; end; procedure TSalesReportFrame.UpdateComboBoxes; var i : integer; begin // populate salesperson combo box SalespersonComboBox.Items.Clear; SalespersonComboBox.Items.Add(ComboBoxAllIndicator); for i := 0 to Salespersons.Count - 1 do SalespersonComboBox.Items.Add(TSalesperson(Salespersons[i]).Name); // populate item combo box ItemComboBox.Items.Clear; ItemComboBox.Items.Add(ComboBoxAllIndicator); for i := 0 to Items.Count - 1 do ItemComboBox.Items.Add(TItem(Items[i]).Name); // populate payment type combo box PaymentTypeComboBox.Items.Clear; PaymentTypeComboBox.Items.Add(ComboBoxAllIndicator); for i := 0 to PaymentTypes.Count - 1 do PaymentTypeComboBox.Items.Add(TPaymentType(PaymentTypes[i]).Name); end; procedure TSalesReportFrame.PrintReport (Email : boolean); begin // save parameters in workstation configuration if OrderBySaleDateRadioButton.Checked then WorkstationConfiguration.SalesReportParameters.OrderBy := diSaleDate else if OrderBySalespersonRadioButton.Checked then WorkstationConfiguration.SalesReportParameters.OrderBy := diSalesperson else if OrderByItemRadioButton.Checked then WorkstationConfiguration.SalesReportParameters.OrderBy := diItem else if OrderByPaymentTypeRadioButton.Checked then WorkstationConfiguration.SalesReportParameters.OrderBy := diPaymentType; WorkstationConfiguration.SalesReportParameters.SalespersonId := SalespersonIdFromName(SalespersonComboBox.Text); WorkstationConfiguration.SalesReportParameters.ItemId := ItemIdFromName(ItemComboBox.Text); WorkstationConfiguration.SalesReportParameters.PaymentTypeId := PaymentTypeIdFromName(PaymentTypeComboBox.Text); WorkstationConfiguration.SalesReportParameters.TotalsOnly := TotalsOnlyCheckBox.Checked; WorkstationConfiguration.SalesReportParameters.BeginSaleDate := Trunc(BeginSaleDateDateTimePicker.Date) - Trunc(Date); WorkstationConfiguration.SalesReportParameters.EndSaleDate := Trunc(EndSaleDateDateTimePicker.Date) - Trunc(Date); UpdateComboBoxes(true); SaveWorkstationConfiguration; if WorkstationConfiguration.SalesReportParameters.NoSelectionCriteria then MessageDlg('Please enter selection criteria',mtWarning,[mbOk],0) else PrintSalesReport(WorkstationConfiguration.SalesReportParameters,Email); end; {******************************************************************************} procedure TSalesReportFrame.PrintButtonClick(Sender: TObject); begin PrintReport(false); end; procedure TSalesReportFrame.EmailButtonClick(Sender: TObject); begin PrintReport(true); end; {***** BeginSaleDateDateTimePicker event handling *****************************} procedure TSalesReportFrame.BeginSaleDateDateTimePickerKeyDown( Sender: TObject; var Key: Word; Shift: TShiftState); begin if Key = VK_RETURN then begin TBaseForm(Parent).GoToNextControl; Key := 0; end; FirstChangeEvent := true; end; procedure TSalesReportFrame.BeginSaleDateDateTimePickerEnter( Sender: TObject); begin FirstChangeEvent := true; end; procedure TSalesReportFrame.BeginSaleDateDateTimePickerCloseUp( Sender: TObject); begin keybd_event(VK_LEFT,0,0,0); keybd_event(VK_RIGHT,0,0,0); end; procedure TSalesReportFrame.BeginSaleDateDateTimePickerChange( Sender: TObject); var BeginDate, EndDate : TDateTime; EndDateDateTimePicker : TDateTimePicker; begin if IgnoreChangeEvents then Exit; // there is a bug in the DateTimePicker where change events // fire twice so ignore the second one FirstChangeEvent := not FirstChangeEvent; if FirstChangeEvent then Exit; EndDateDateTimePicker := EndSaleDateDateTimePicker; BeginDate := Trunc(TDateTimePicker(Sender).Date); EndDate := EndDateDateTimePicker.Date; // adjust end date if necessary if EndDate < BeginDate then begin EndDate := BeginDate; EndDateDateTimePicker.Date := EndDate; end; end; {***** EndSaleDateDateTimePicker event handling *******************************} procedure TSalesReportFrame.EndSaleDateDateTimePickerKeyDown( Sender: TObject; var Key: Word; Shift: TShiftState); begin if Key = VK_RETURN then begin TBaseForm(Parent).GoToNextControl; Key := 0; end; FirstChangeEvent := true; end; procedure TSalesReportFrame.EndSaleDateDateTimePickerEnter( Sender: TObject); begin FirstChangeEvent := true; end; procedure TSalesReportFrame.EndSaleDateDateTimePickerCloseUp( Sender: TObject); begin keybd_event(VK_LEFT,0,0,0); keybd_event(VK_RIGHT,0,0,0); end; procedure TSalesReportFrame.EndSaleDateDateTimePickerChange( Sender: TObject); var BeginDate, EndDate : TDateTime; BeginDateDateTimePicker : TDateTimePicker; begin if IgnoreChangeEvents then Exit; // there is a bug in the DateTimePicker where change events // fire twice so ignore the second one FirstChangeEvent := not FirstChangeEvent; if FirstChangeEvent then Exit; BeginDateDateTimePicker := BeginSaleDateDateTimePicker; BeginDate := BeginDateDateTimePicker.Date; EndDate := Trunc(TDateTimePicker(Sender).Date); // adjust begin date if necessary if EndDate < BeginDate then begin BeginDate := EndDate; BeginDateDateTimePicker.Date := BeginDate; end; end; {******************************************************************************} end.