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; //打印份数
    fplanNO:string;
    fIsPreviewPrint:Boolean;
    procedure DoPrintLabel();
  end;

var
  frmLabelPrint: TfrmLabelPrint;

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

procedure TfrmLabelPrint.FormCreate(Sender: TObject);
begin
  try
    with ADOConnection1 do
    begin
      Connected:=false;
      ConnectionString:=gConString;
      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('exec P_Label_ClothPrintData');
      sql.Add(quotedStr(fKeyNo));
      sql.Add(','+quotedStr(fPlanNO));
      //sql.Add(','+quotedStr(''));
      //sql.Add(','+quotedStr(''));
      //sql.Add(','+quotedStr(''));
      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.InitVarDictionary();
var
  mName:string;
  i:integer;
begin
  try
    with ADOQueryCust do
    begin
      close;
      sql.Clear ;
      sql.Add('exec P_Label_CustPrintData');
      sql.Add(quotedStr(fCustomNo));
      Open;

      if isEmpty then exit;
    end;
    with RMGridReport1 do
    begin
      for i:=0 to Dictionary.Variables.Count-1 do
      begin
        mName:=Dictionary.Variables.Name[i];
        if (trim(mName)<>'客户单位信息') and (trim(mName)<>'单位信息') then
          Dictionary.Variables.AsString[mName]:=trim(ADOQueryCust.fieldByName(mName).AsString);
      end;
    end;
  except

  end;
end;
////////////////////////////////////////////////////////
procedure TfrmLabelPrint.OpenLabel();
begin
  with ADOQueryTmp do
  begin
    close;
    sql.Clear ;
    sql.Add('select labelfile from jd_label_plan A');
    sql.Add('where planNo='+quotedStr(fPlanNO));
    sql.Add('and labelId='+quotedStr(fLabelId));
    Open;
    if  fieldbyname('labelFile').IsNull then
    begin
      close;
      sql.Clear ;
      sql.Add('select labelfile from jd_label_sub A');
      sql.Add('where labelId='''+fLabelId+'''');
      Open;
    end;
    if  fieldbyname('labelFile').IsNull then
    begin
      close;
      sql.Clear ;
      sql.Add('select labelfile from jd_label_plan A');
      sql.Add('where planNo='''+fPlanNO+'''');
      Open;
    end;
    if fieldbyname('labelFile').IsNull then
    begin
      close;
      sql.Clear ;
      sql.Add('select labelfile from jd_label_sub A');
      sql.Add('where customNo=''客户公用标签''');
      Open;
    end;    
    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.
