{*******************************************************} { } { Responsive Software http://www.responsive.co.nz } { } { Copyright (c) 2003-2006 Responsive Software Limited } { } {*******************************************************} unit Sales; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, BaseFrameUnit, StdCtrls, Grids, ComCtrls, ExtCtrls, DatabaseObjects; type TSalesFrame = class(TBaseFrame) SalesStringGrid: TStringGrid; HeadingLabel: TLabel; HeadingShape: TShape; TotalTodayHeadingLabel: TLabel; TotalTodayLabel: TLabel; ReceiptButton: TButton; procedure SalesStringGridDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); procedure ReceiptButtonClick(Sender: TObject); procedure SalesStringGridDblClick(Sender: TObject); private { Private declarations } Sales : TDatabaseObjectCollection; // Sale refers to the sale currently being viewed Sale : TSale; procedure SetSale; procedure UpdateControls; procedure UpdateSalesStringGrid; procedure UpdateControlStates; function TotalToday : int64; public { Public declarations } procedure UpdateDisplay; procedure UpdateComboBoxes (Full : boolean); override; procedure Setup; override; procedure HandleF6; override; procedure UpdateSale (Sale : TSale); end; implementation uses GeneralUtilities, DatabaseManager, Globals, Base, Utilities, Main, Entries; {$R *.dfm} function TSalesFrame.TotalToday : int64; var i : integer; Sale : TSale; begin Result := 0; for i := Sales.Count - 1 downto 0 do begin Sale := TSale(Sales[i]); if Sale.Date < Date then Exit; Result := Result + Sale.TotalPrice; end; end; procedure TSalesFrame.HandleF6; begin MainForm.SwitchToFrame('Items'); end; procedure TSalesFrame.Setup; var SelectionString : string; BeginDate : TDateTime; begin SetUpStringGrid(SalesStringGrid,[ 'Date', 'Time', 'Salesperson', 'Total Price', 'Items' ],[ 12, // Date 10, // Time 15, // Salesperson 15, // Total Price 50 // Items ]); // create and initialise objects BeginDate := Date - WorkstationConfiguration.NoOfDaysSales; if Firebird then SelectionString := 'WHERE ' + DelimitSQLFieldName('Date') + ' >= ' + DelimitSQLStringValue(ConvertDateToDatabaseString(BeginDate)) + ' ORDER BY ' + DelimitSQLFieldName('Date') + ', ' + DelimitSQLFieldName('Time') else SelectionString := 'WHERE Sale."Date" >= ' + DelimitSQLStringValue(ConvertDateToDatabaseString(BeginDate)) + ' ORDER BY Sale."Date", Sale."Time"'; LoadSomeDatabaseObjects(Sales,TSale,SelectionString); SalesStringGrid.Row := 1; UpdateComboBoxes(false); UpdateDisplay; end; procedure TSalesFrame.SetSale; var Index : integer; begin if Sales = nil then begin Sale := nil; Exit; end; Index := Sales.Count - SalesStringGrid.Row; if Index < Sales.Count then Sale := TSale(Sales[Index]) else Sale := nil; end; procedure TSalesFrame.UpdateComboBoxes (Full : boolean); begin end; procedure TSalesFrame.UpdateControls; begin end; procedure TSalesFrame.UpdateSalesStringGrid; var i : integer; begin // display sales in string grid if Sales.Count = 0 then begin SalesStringGrid.RowCount := 2; // clear details on last row for i := 0 to SalesStringGrid.ColCount - 1 do SalesStringGrid.Cells[i,1] := ''; end else SalesStringGrid.RowCount := Sales.Count + 1; // don't update string grid here but display info as required // directly from draw cell event to save time SalesStringGrid.Repaint; // update balance labels TotalTodayLabel.Caption := FormatCurrencyForDisplay(TotalToday); end; procedure TSalesFrame.UpdateControlStates; begin end; procedure TSalesFrame.UpdateDisplay; begin UpdateSalesStringGrid; UpdateControls; UpdateControlStates; end; procedure TSalesFrame.UpdateSale (Sale : TSale); begin Sales.Update(Sale); UpdateDisplay; end; {******************************************************************************} procedure TSalesFrame.SalesStringGridDblClick(Sender: TObject); begin SetSale; if Sale <> nil then Utilities.PrintReceipt(Sale,true); end; procedure TSalesFrame.SalesStringGridDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); var Sale : TSale; Text : string; function NewHour : boolean; var PreviousSale : TSale; ThisHour, ThisMin, ThisSec, ThisMSec : word; PreviousHour, PreviousMin, PreviousSec, PreviousMSec : word; begin PreviousSale := TSale(Sales[Sales.Count - ARow - 1]); if PreviousSale = nil then Result := true else begin DecodeTime(Sale.Time,ThisHour,ThisMin,ThisSec,ThisMSec); DecodeTime(PreviousSale.Time,PreviousHour,PreviousMin,PreviousSec,PreviousMSec); if (ThisHour <> PreviousHour) or (Sale.Date <> PreviousSale.Date) then Result := true else Result := false; end; end; begin Sale := TSale(Sales[Sales.Count - ARow]); if Sale <> nil then begin // set the brush color if gdSelected in State then SalesStringGrid.Canvas.Brush.Color := clHighlight else if NewHour then SalesStringGrid.Canvas.Brush.Color := DarkenColor(SalesStringGrid.Color) else SalesStringGrid.Canvas.Brush.Color := SalesStringGrid.Color; // format the text if ACol = 0 then Text := FormatDate(Sale.Date) else if ACol = 1 then Text := FormatTime(Sale.Time) else if ACol = 2 then Text := SalespersonName(Sale.SalespersonId) else if ACol = 3 then Text := FormatCurrencyForDisplay(Sale.TotalPrice) else if ACol = 4 then Text := Sale.SaleItemsAsString; // output the text SalesStringGrid.Canvas.TextRect(Rect,Rect.Left+2,Rect.Top+2,Text); end; end; {******************************************************************************} procedure TSalesFrame.ReceiptButtonClick(Sender: TObject); begin SetSale; if Sale <> nil then Utilities.PrintReceipt(Sale,true); end; {******************************************************************************} end.