餐飲行業,打印池是必要的部件。
實現原理:每一台打印機都有自己的任務隊列和處理任務隊列的線程。
unit untPrintTask;
interface
uses
System.SysUtils, System.Classes,
Datasnap.DBClient, frxclass,
System.Generics.Collections;
type
TBillContent = record // 小票內容
machineNo: string; // POS機號
skyName: string; // 收款員姓名
saleNo: string; // 小票號
saleTime: TDateTime; // 銷售時間
amount: Currency; // 應收
pay: Currency; // 支付
change: Currency; // 找零
prnData: OleVariant; // 小票明細:商品名稱、單價、數量、金額。。。。。。
deskNo:string; // 台號
payType: string; // 支付方式
end;
type
TPrinterInfo = record // 打印機信息
prnNo: Integer; // 印機編號
prnName: string; // 印機名稱
prnType: string; // 結賬、廚打
prnWidth: Integer; // 50mm\76mm\80mm
remark: string; // 備注
prnModel: string; // 打印模版
end;
type
TOneTimePrint = record // 一次打印
printerInfo: TPrinterInfo; // 打印機信息
billContent: TBillContent; // 小票內容
end;
type
TPrintTaskThread = class(TThread) // 打印任務線程
private
FPrintQueue: TQueue<TOneTimePrint>; // 打印隊列
protected
procedure Execute; override;
public
constructor Create; overload;
destructor Destroy; override;
property PrintQueue: TQueue<TOneTimePrint> read FPrintQueue
write FPrintQueue;
end;
var
g_PrintTasks: TDictionary<string, TPrintTaskThread>; // <打印機名字, TPrintTask>
implementation
{ TPrintTask }
uses untFastReport, UntSysConst;
constructor TPrintTaskThread.Create;
begin
Create(False);
FreeOnTerminate := False;
// 創建打印隊列
FPrintQueue := TQueue<TOneTimePrint>.Create;
end;
destructor TPrintTaskThread.Destroy;
begin
// 釋放打印隊列
FreeAndNil(FPrintQueue);
inherited;
end;
procedure TPrintTaskThread.Execute;
var
OneTimePrint: TOneTimePrint;
dm: TdmFastReport;
c: TfrxComponent;
begin
while not Self.Terminated do
begin
if Assigned(FPrintQueue) and (FPrintQueue.Count > 0) then
begin
// 從任務隊列中提取一個任務
OneTimePrint := FPrintQueue.Dequeue;
dm := TdmFastReport.Create(nil);
try
try
// 小票明細數據
dm.cds.Data := OneTimePrint.billContent.prnData;
// 小票模板
dm.report.LoadFromFile(OneTimePrint.printerInfo.prnModel);
// 哪個打印機
dm.report.PrintOptions.Printer := OneTimePrint.printerInfo.prnName;
// 變量賦值
c:=dm.report.FindObject('mmShopName');
if c<>nil then
TfrxMemoView(c).Memo.Text := UserInfo.ShopName;
c := dm.report.FindObject('mmMachineNo');
if c<>nil then
TfrxMemoView(c).Memo.Text := OneTimePrint.billContent.machineNo;
c:=dm.report.FindObject('mmSKY');
if c<>nil then
TfrxMemoView(c).Memo.Text := OneTimePrint.billContent.skyName;
c:= dm.report.FindObject('mmBillNo');
if c<>nil then
TfrxMemoView(c).Memo.Text := OneTimePrint.billContent.saleNo;
c:=dm.report.FindObject('mmSaleTime');
if c<>nil then
TfrxMemoView(c).Memo.Text := FormatDateTime('yyyy-mm-dd hh:nn',OneTimePrint.billContent.saleTime);
c:= dm.report.FindObject('mmDeskNo');
if c<>nil then
TfrxMemoView(c).Memo.Text := OneTimePrint.billContent.deskNo; // 台號
c:= dm.report.FindObject('mmPayType');
if c<>nil then
TfrxMemoView(c).Memo.Text := OneTimePrint.billContent.payType;
c:= dm.report.FindObject('mmAmount');
if c<> nil then
TfrxMemoView(c).Memo.Text := FormatCurr('0.00', OneTimePrint.billContent.amount);
c:=dm.report.FindObject('mmPay');
if c<>nil then
TfrxMemoView(c).Memo.Text := FormatCurr('0.00', OneTimePrint.billContent.pay);
c:= dm.report.FindObject('mmGiveChange');
if c<> nil then
TfrxMemoView(c).Memo.Text := FormatCurr('0.00', OneTimePrint.billContent.change);
// 開始打印
dm.report.PrepareReport();
dm.report.Print;
except
// 打印失敗,重新加入任務隊列
Self.FPrintQueue.Enqueue(OneTimePrint);
end;
finally
FreeAndNil(dm);
end;
end;
Sleep(1);
end;
end;
end.