unit AlertThread;
interface
uses
Windows, Messages, SysUtils, Classes, Registry, DB, ADODB,
ComObj, ActiveX, ComCtrls, DateUtils, StdCtrls, Controls, Dialogs;
type
TAlertEventType = (aeConnect, aeDisconnect, aeStart, aeFinish);
TAlertType = (atSalesMonthLast, atSalesMonthYear, atSalesQuarter, atSalesYear, atOverdueApprovals,
atUnlinkedBO, atPODueToday, atOverduePO, atPurchConsign, atPickupDelivery);
TAmountType = (amToday, amYesterday, amTodayLastWeek, amTodayLastMonth, amTodayLastYear,
amThisWeek, amLastWeek, amThisMonth, amLastMonth, amThisMonthLastYear, amThisYear, amLastYear);
TAccountBO = (abInvoiceCreated, abItemSold);
TAlertEvent = procedure(Sender: TObject; const T: TAlertType; const S: String; const ID: Integer) of object;
TAmountEvent = procedure(Sender: TObject; const T: TAmountType; const Amount: Currency) of object;
TAlertThread = class(TThread)
private
fStep: Integer;
fConnStr: String;
fDataReady: Bool;
fVal: string;
fInd: Integer;
fTyp: TAlertType;
cst, cst2, cst3: Currency;
C_Date: TDate;
C_DateTime: TDateTime;
fEventType: TAlertEventType;
fDB: TADOConnection;
Q: TADODataset;
fOnFinish: TNotifyEvent;
fOnBegin: TNotifyEvent;
fOnDisconnect: TNotifyEvent;
fOnConnect: TNotifyEvent;
fOnAlert: TAlertEvent;
fOnAmount: TAmountEvent;
procedure ThreadTerminated(Sender: TObject);
procedure DBConnect(Sender: TObject);
procedure DBDisconnect(Sender: TObject);
procedure DoEvent;
procedure DoAlerts;
function ConnectDB: Bool;
procedure UpdateAlert;
procedure DoStep;
protected
procedure Execute; override;
public
ConsThreshold, CommTimeout: Integer;
cryr: string;
pryr: string;
pryr1: string;
pryr2: string;
pryr3: string;
yr1, yr2, yr3, yr4, yr5: string;
monthC, yearC: currency;
constructor Create(const AConnStr: String);
published
property OnBegin: TNotifyEvent read fOnBegin write fOnBegin;
property OnFinish: TNotifyEvent read fOnFinish write fOnFinish;
property OnConnect: TNotifyEvent read fOnConnect write fOnConnect;
property OnDisconnect: TNotifyEvent read fOnDisconnect write fOnDisconnect;
property OnAlert: TAlertEvent read fOnAlert write fOnAlert;
property OnAmount: TAmountEvent read fOnAmount write fOnAmount;
end;
implementation
{ TAlertThread }
function TAlertThread.ConnectDB: Bool;
begin
Result:= False;
if fDB.Connected then Exit;
fDB.ConnectionString:= fConnStr;
try
fDB.Connected:= True;
Result:= True;
except
end;
end;
constructor TAlertThread.Create(const AConnStr: String);
begin
FreeOnTerminate:= True;
inherited Create(True);
OnTerminate:= ThreadTerminated;
fConnStr:= AConnStr;
CoInitialize(nil);
try
fDB:= TADOConnection.Create(nil);
fDB.LoginPrompt:= False;
fDB.ConnectionString:= fConnStr;
fDB.AfterConnect:= Self.DBConnect;
fDB.AfterDisconnect:= Self.DBDisconnect;
Q:= TADODataSet.Create(nil);
Q.Connection:= fDB;
finally
CoUninitialize;
end;
end;
procedure TAlertThread.DBConnect(Sender: TObject);
begin
if assigned(fOnConnect) then fOnConnect(Self);
end;
procedure TAlertThread.DBDisconnect(Sender: TObject);
begin
if assigned(fOnDisconnect) then fOnDisconnect(Self);
end;
procedure TAlertThread.DoEvent;
begin
case fEventType of
aeConnect: begin
if assigned(fOnConnect) then fOnConnect(Self);
end;
aeDisconnect: begin
if assigned(fOnDisconnect) then fOnDisconnect(Self);
end;
aeStart: begin
if assigned(fOnBegin) then fOnBegin(Self);
end;
aeFinish: begin
if assigned(fOnFinish) then fOnFinish(Self);
end;
end;
end;
procedure TAlertThread.DoStep;
begin
if assigned(fOnAlert) then
Self.fOnAlert(Self, fTyp, fVal, fInd);
end;
procedure TAlertThread.Execute;
begin
if Terminated then Exit;
fEventType:= aeStart;
Synchronize(DoEvent);
try
CoInitialize(nil);
try
if ConnectDB then begin
//CODE GOES HERE
DoAlerts;
end;
finally
CoUninitialize;
end;
finally
fEventType:= aeFinish;
Synchronize(DoEvent);
end;
end;
procedure TAlertThread.ThreadTerminated(Sender: TObject);
begin
if assigned(Q) then begin
Q.Close;
Q.Free;
Q:= nil;
end;
if assigned(fDB) then begin
fDB.Connected:= False;
fDB.Free;
fDB:= nil;
end;
end;
procedure TAlertThread.UpdateAlert;
begin
if assigned(fOnAlert) then begin
Self.fOnAlert(Self, fTyp, fVal, fInd);
end;
end;
procedure TAlertThread.DoAlerts;
begin
//PROCEDURE TRUNCATED BECAUSE IT IS VERY LARGE - problem for sure does not come from here
end;
end.