{*******************************************************} { } { Responsive Software http://www.responsive.co.nz } { } { Copyright (c) 2003-2006 Responsive Software Limited } { } {*******************************************************} unit HTTPResponder; interface uses IBDatabase, Classes; function GetHTTPResponse (Method : string; URI : string; Version : string; MessageHeaders : TStringList; MessageBody : string) : string; implementation uses SyncObjs, Forms, Windows, Graphics, Chart, Series, TeEngine, Jpeg, SysUtils, Utilities, HTTPUtilities, HTTPServerCommunicatorUnit, GeneralUtilities, Globals, DatabaseObjects, DatabaseManager; {***** General formatting utilities *******************************************} var // set this global variable to false before calling a procedure which // modifies the contents of a file by replacing tags // and then test to see whether or not any were actually replaced TagReplaced : boolean; procedure ReplaceTag (Tag : string; Value : string; var Content : string); begin if Pos(Tag,Content) <> 0 then begin Content := ReplaceSubstring(Tag,Value,Content); TagReplaced := true; end; end; function GetFileName (URI : string; Host : string) : string; var QuestionMarkPos : integer; begin QuestionMarkPos := Pos('?',URI); if QuestionMarkPos > 0 then Result := Copy(URI,1,QuestionMarkPos-1) else Result := URI; Result := AppendBackslash(GlobalConfiguration.WebsiteContentDirectory) + Host + Result; Result := ReplaceChar('/','\',Result); Result := ReplaceSubstring('%20',' ',Result); end; {***** Methods that must be synchronized with the main VCL thread *************} function ConvertChartToJPG (Chart : TChart) : string; var StringStream : TStringStream; Bitmap : TBitmap; JPEGImage : TJPEGImage; begin StringStream := TStringStream.Create(''); Bitmap := TBitmap.Create; JPEGImage := TJPEGImage.Create; try JPEGImage.CompressionQuality := 65; Bitmap.Width := Chart.Width; Bitmap.Height := Chart.Height; Chart.Draw(Bitmap.Canvas,Rect(0,0,Chart.Width,Chart.Height)); JPEGImage.Assign(Bitmap); Bitmap.Dormant; // transfer jpeg image data to string JPEGImage.SaveToStream(StringStream); // extract result from data string Result := StringStream.DataString; finally JPEGImage.Free; Bitmap.Free; StringStream.Free; end; end; {***** Original HTML Response routines ****************************************} const Spacer = '  '; {***** Main Screen ************************************************************} function CreateMainScreen : string; begin // header Result := HTMLPageHeader; // details if GlobalConfiguration.POS then Result := Result + '

Items

' + '

Sales

'; if GlobalConfiguration.Accounts then Result := Result + '

Accounts

'; // footer Result := Result + HTMLPageFooter; end; {***** Items Screen ***********************************************************} function CreateItemsScreen : string; var SelectionString : string; Items : TDatabaseObjectCollection; Item : TItem; i : integer; ImageStr : string; begin // header Result := HTMLPageHeader; // load collection of items SelectionString := 'ORDER BY ' + DelimitSQLFieldName('Name'); Items := nil; LoadSomeDatabaseObjects(Items,TItem,SelectionString); try Result := Result + '

Items

'; Result := Result + ''; for i := 0 to Items.Count - 1 do begin Item := TItem(Items[i]); if Item.Active then begin if Item.Picture.AttachmentId = 0 then ImageStr := '' else ImageStr := ''; Result := Result + ''; end; end; Result := Result + '
' + Item.Name + '' + ConvertDoubleToString(Item.Price) + '' + ImageStr + '
'; finally Items.Free; end; // footer Result := Result + HTMLPageFooter; end; {***** Sales Screens **********************************************************} function CreateSalesSelectScreen : string; begin // header Result := HTMLPageHeader; // details Result := Result + '
' + '
' + '
' + 'Salesperson' + Spacer + '' + '
' + '
' + 'Begin Period' + Spacer + '' + '' + '' + '
' + '
' + 'End Period' + Spacer + '' + '' + '' + '
' + '
' + '
' + '
'; // footer Result := Result + HTMLPageFooter; end; function CreateSalesResultScreen (Parameters : TStringList) : string; var SelectionString : string; Sales : TDatabaseObjectCollection; Sale : TSale; i : integer; BeginDate, EndDate : TDateTime; SalespersonId : int64; TotalTotalPrice : int64; begin // header Result := HTMLPageHeader; // load collection of sales try BeginDate := EncodeDate( StrToInt(Parameters.Values['beginyear']), StrToInt(Parameters.Values['beginmonth']), StrToInt(Parameters.Values['beginday'])); except BeginDate := Date; end; try EndDate := EncodeDate( StrToInt(Parameters.Values['endyear']), StrToInt(Parameters.Values['endmonth']), StrToInt(Parameters.Values['endday'])); except EndDate := Date; end; if Firebird then SelectionString := 'WHERE ' + DelimitSQLFieldName('Date') + ' >= ' + DelimitSQLStringValue(ConvertDateToDatabaseString(BeginDate)) + ' AND ' + DelimitSQLFieldName('Date') + ' <= ' + DelimitSQLStringValue(ConvertDateToDatabaseString(EndDate)) else SelectionString := 'WHERE Sale."Date" >= ' + DelimitSQLStringValue(ConvertDateToDatabaseString(BeginDate)) + ' AND Sale."Date" <= ' + DelimitSQLStringValue(ConvertDateToDatabaseString(EndDate)); SalespersonId := StrToIntDef(Parameters.Values['salesperson'],0); if SalespersonId <> 0 then SelectionString := SelectionString + ' AND ' + DelimitSQLFieldName('SalespersonId') + ' = ' + IntToStr(SalespersonId); if Firebird then SelectionString := SelectionString + ' ORDER BY ' + DelimitSQLFieldName('Date') + ', ' + DelimitSQLFieldName('Time') else SelectionString := SelectionString + ' ORDER BY Sale."Date", Sale."Time"'; Sales := nil; LoadSomeDatabaseObjects(Sales,TSale,SelectionString); try Result := Result + '

Sales for period ' + FormatDate(BeginDate) + ' to ' + FormatDate(EndDate) + '

'; if SalespersonId <> 0 then Result := Result + '

Salesperson: ' + SalespersonName(SalespersonId) + '

'; Result := Result + ''; Result := Result + '' + '' + '' + '' + '' + '' + '' + ''; TotalTotalPrice := 0; for i := Sales.Count - 1 downto 0 do begin Sale := TSale(Sales[i]); Sale.LoadDetails; Result := Result + '' + '' + '' + '' + '' + '' + '' + ''; TotalTotalPrice := TotalTotalPrice + Sale.TotalPrice; end; Result := Result + '' + '' + '' + '' + '' + '' + '' + ''; Result := Result + '
DateTimeSalespersonTotal PriceItemsPayment Type
' + Format('%-8.8s',[ShortFormatDate(Sale.Date)]) + '' + FormatDateTime('hh:mm:ss',Sale.Time) + '' + Format('%-15.15s ',[SalespersonName(Sale.SalespersonId)]) + '' + Format(' %15.15s',[FormatCurrencyForDisplay(Sale.TotalPrice)]) + '' + Format(' %-35.35s ',[Sale.SaleItemsAsString]) + '' + Format(' %-25.25s ',[Sale.PaymentItemsAsString]) + '
TOTAL' + Format(' %15.15s',[FormatCurrencyForDisplay(TotalTotalPrice)]) + '
'; finally Sales.Free; end; // footer Result := Result + HTMLPageFooter; end; {***** Accounts Screens *******************************************************} function CreateAccountsSelectScreen : string; function ReportTypeOptions : string; begin Result := ''; Result := Result + '