unit U_LabelPrint;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, ADODB, RM_e_Graphic, RM_e_bmp, RM_BarCode, RM_System,
  RM_Common, RM_Class, RM_GridReport, RM_e_Xls, RM_Dataset, StdCtrls,
  ExtCtrls;

type
  TfrmLabelPrint = class(TForm)
    ADOQueryMain: TADOQuery;
    ADOConnection1: TADOConnection;
    RMXLSExport1: TRMXLSExport;
    RMGridReport1: TRMGridReport;
    RMBarCodeObject1: TRMBarCodeObject;
    RMBMPExport1: TRMBMPExport;
    RMDS_Main: TRMDBDataSet;
    ADOQueryTmp: TADOQuery;
    Button1: TButton;
    ADOQueryCust: TADOQuery;
    Panel1: TPanel;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    procedure InitDataSetDictionary();
//    procedure InitVarDictionary();
    procedure OpenLabel();
  public
    fLabelId:string;     //标签记录Id;
    fKeyNo:string;       //
    fCustomNo:string;    //客户编号
    fLabelCopys:integer; //打印份数
    fIsPreviewPrint:Boolean;
    FFCDFlag:String;
    procedure DoPrintLabel();
  end;

var
  frmLabelPrint: TfrmLabelPrint;

implementation
uses
  U_DataLink;
{$R *.dfm}
////////////////////////////////////////////////////////////////////
  //函数：打印标签加载
////////////////////////////////////////////////////////////////////
procedure TfrmLabelPrint.DoPrintLabel();
begin
  InitDataSetDictionary();
  OpenLabel();
end;

procedure TfrmLabelPrint.FormCreate(Sender: TObject);
begin
  try
    with ADOConnection1 do
    begin
      Connected:=false;
      ConnectionString:=DConString;
      Connected:=true;
    end;
  Except
    application.MessageBox('数据库连接失败！','错误',mb_Ok+ MB_ICONERROR);
  end;
end;
/////////////////////////////////////////////////
  //函数功能：加载标签数据
/////////////////////////////////////////////////
procedure TfrmLabelPrint.InitDataSetDictionary();
begin
  try
    with ADOQueryMain do
    begin
      close;
      sql.Clear ;
      sql.Add('select RTrim(AA.XJID) XJID,RTrim(Cast(AA.XJSJKZ as varchar(20))) XJSJKZ,RTrim(cast(Cast(AA.XJFK*10 as int) as varchar(20))) XJFK,RTrim(B.OrderNo)+'''+Trim(FFCDFlag)+''' OrderNo');
      sql.Add(',Rtrim(C.SWFBColor) SWFBColor, YLPB=dbo.F_Get_WFBOrder_SubStr(C.SubId,''YLPBSZ'')');
      sql.Add(',Rtrim(Cast(AA.XJlen as varchar(20))) XJlen,Rtrim(Cast(AA.XJMaoZ as varchar(20))) XJMaoZ');
      sql.Add(',Rtrim(Cast(AA.XJFree as varchar(20))) XJFree,Rtrim(Cast( Cast(AA.XJlen*AA.XJFK/100 as int) as varchar(20))) XJPFM');
      sql.Add(',Rtrim(Cast(Cast(C.SWFBKZ as int) as varchar(20))) SWFBKZ');
      sql.Add(',Rtrim(Cast(Cast(AA.XJJt as int) as varchar(20))) XJJt');
      sql.Add(',RTrim(B.LbEngName) LbEngName');
      sql.Add(',RTrim(C.SWFBCodeName) SWFBCodeName from WFB_XJJY AA ');
      sql.add(' inner join WFB_MJJY A on AA.MJID=A.MJID') ;
      sql.add(' inner join WFBOrder_Main B on A.MainId=B.MainId');
      sql.Add(' inner join WFBOrder_Sub C on A.SubId=C.SubId');
      sql.Add(' where AA.XJID='''+Trim(fKeyNo)+'''');
      OPen;
    end;
    with RMGridReport1 do
    begin
      Dictionary.FieldAliases.Clear;
      Dictionary.FieldAliases['RMDS_Main']:= '标签数据';
   //   Dictionary.FieldAliases['RMDS_Main."BarCodeNo"']:='标签条码';
    end;
  except
    application.MessageBox('加载标签数据时发生错误!','警告信息',0);
  end;
end;
//////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
procedure TfrmLabelPrint.OpenLabel();
begin
  with ADOQueryTmp do
  begin
    close;
    sql.Clear ;
    sql.Add('select labelfile from P_Label A');
    sql.Add('where labelId='''+fLabelId+'''');
    Open;
    if fieldbyname('labelFile').IsNull then
    begin
      application.MessageBox('未找到要打印的标签信息记录!','提示信息',0);
      self.Close ;
      exit;
    end;
    RMGridReport1.LoadFromBlobField(tblobfield(fieldbyname('labelFile')));
 //   InitVarDictionary();
    //fIsPreviewPrint:=true;
    if fIsPreviewPrint then
      RMGridReport1.ShowReport
    else
      RMGridReport1.PrintReport ;
    close;
  end;

end;
procedure TfrmLabelPrint.Button1Click(Sender: TObject);
begin
  DoPrintLabel();
end;

procedure TfrmLabelPrint.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action:=caFree;
end;

end.
