unit U_LabelHelp10;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxCheckBox, cxGridLevel,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxClasses,
  cxControls, cxGridCustomView, cxGrid, ImgList, ComCtrls, ToolWin, ADODB,
  RM_Common, RM_Preview, StdCtrls, ExtCtrls, RM_System, RM_Class,
  RM_GridReport, cxSplitter;

type
  TfrmLabelHelp10 = class(TForm)
    ToolBar2: TToolBar;
    TOk: TToolButton;
    TBClose: TToolButton;
    ImageList1: TImageList;
    cxGrid1: TcxGrid;
    tv1: TcxGridDBTableView;
    tv1sel: TcxGridDBColumn;
    tv1labelClass: TcxGridDBColumn;
    tv1labelCaption: TcxGridDBColumn;
    tv1packLabelCaption: TcxGridDBColumn;
    tv1Column1: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    DS_Label: TDataSource;
    ADOQueryLabel: TADOQuery;
    tv1labelUnit: TcxGridDBColumn;
    Tload: TToolButton;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    Panel4: TPanel;
    GroupBox1: TGroupBox;
    RMPreview2: TRMPreview;
    GroupBox2: TGroupBox;
    RMPreview1: TRMPreview;
    RMGridReport1: TRMGridReport;
    RMGridReport2: TRMGridReport;
    ADOQueryPackLabel: TADOQuery;
    ADOConnection1: TADOConnection;
    cxSplitter1: TcxSplitter;
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TOkClick(Sender: TObject);
    procedure tv1DblClick(Sender: TObject);
    procedure TloadClick(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure tv1FocusedRecordChanged(Sender: TcxCustomGridTableView;
      APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
  private
    isLoad:Boolean;
    procedure IntLabelGrid();
    function InsertPlanLabel(mLabel:string):Boolean;
    function IsHasPlanLabel():boolean;
    procedure OpenLabel();
    procedure OpenPackLabel();
  public
    fKeyType:integer;
    fDept:string;
    fPlanNo:string;
    fseqNo:string;
    fKeyNo:string;
    fcustomNo:string;
    fSysType:string;
  end;

var
  frmLabelHelp10: TfrmLabelHelp10;

implementation
uses
  U_global,U_formPas;
{$R *.dfm}
////////////////////////////////////////////////////////////////
  //
////////////////////////////////////////////////////////////////
procedure TfrmLabelHelp10.IntLabelGrid();
begin
  try
    isLoad:=false;
    ADOQueryLabel.DisableControls ;
    with ADOQueryLabel do
    begin
      close;
      sql.clear;
      sql.Add('select B.*,');
      sql.Add('packLabelCaption=(select labelCaption from jd_label_sub x where x.labelId= B.packlabelId)');
      sql.Add('from jd_label A');
      sql.Add('inner join jd_label_sub B');
      sql.Add('on A.customNo=B.customNo ');
      //if trim(fCustomNo)<>'' then
        sql.Add('and (A.customNo='+quotedStr(trim(fCustomNo))+' or A.labelFlag=1)');
      sql.Add('and B.labelClass=''卷标签''');
      if trim(fDept)<>'' then
      begin
        sql.Add('and (B.labelDept='+quotedStr(trim(fDept))+' or B.labelDept='+quotedStr('全部')+')');
      end;
      sql.Add('order by A.labelFlag');
      Open;
    end;
  finally
    ADOQueryLabel.EnableControls ;
    isLoad:=true;
    OpenLabel();
  end;
end;
procedure TfrmLabelHelp10.FormShow(Sender: TObject);
begin
  IntLabelGrid();
end;

procedure TfrmLabelHelp10.FormCreate(Sender: TObject);
begin
  cxGrid1.Align :=alClient;
  with ADOConnection1 do
  begin
    Connected:=false;
    ConnectionString:=gConString;
    Connected:=true;
  end;  
end;

procedure TfrmLabelHelp10.TBCloseClick(Sender: TObject);
begin
  close;
end;

procedure TfrmLabelHelp10.TOkClick(Sender: TObject);
begin
  if ADOQueryLabel.IsEmpty then exit;
  if not IsHasPlanLabel() then
  begin
    if not InsertPlanLabel(ADOQueryLabel.fieldByName('labelID').AsString) then
    begin
      exit;
    end;
  end;
  ModalResult:=1;
end;

procedure TfrmLabelHelp10.tv1DblClick(Sender: TObject);
begin
  TOk.Click ;
end;
////////////////////////////////////////////////////////////////////
  //
////////////////////////////////////////////////////////////////////
function TfrmLabelHelp10.InsertPlanLabel(mLabel:string):Boolean;
begin
  try
    with ADOQueryCmd do
    begin
      close;
      sql.clear;
      sql.Add('exec P_DO_CopyLabel');
      sql.Add('@LabelFlag='+intTostr(fKeyType));
      sql.Add(',@LabelId='+mLabel);
      sql.Add(',@SysCode='+quotedStr(trim(fSysType)));
      sql.Add(',@mplanNO='+quotedStr(trim(fPlanNo)));
      if fKeyType=1 then
         sql.Add(',@mpseqNo='+quotedStr(trim(fKeyNo)))
      else
        sql.Add(',@mpseqNo='+quotedStr(trim(fseqNo)));
      sql.Add(',@customNo='+quotedStr(trim(fcustomNo)));
      sql.Add(',@userid='+quotedStr(trim(gUserId)));
      execSql;
    end;
    result:=true;
  except
    result:=false;
    application.MessageBox('保存当前标签信息时发生错误!','提示信息',0); 
  end;
end;

procedure TfrmLabelHelp10.TloadClick(Sender: TObject);
begin
  if ADOQueryLabel.IsEmpty then exit;
  if InsertPlanLabel(ADOQueryLabel.fieldByName('labelID').AsString) then
  begin
    ModalResult:=1;
  end;
end;
///////////////////////////////////////////////////////
  //
///////////////////////////////////////////////////////
function TfrmLabelHelp10.IsHasPlanLabel():boolean;
begin
  result:=false;
  try
    with ADOQueryTmp do
    begin
      close;
      sql.clear;
      sql.Add('select count(*) as cnt');
      sql.Add('from JD_Label_Plan');
      sql.Add('where PlanNO='+quotedStr(fPlanNo));
      //if trim(fseqNo)<>'' then
      //  sql.Add('and P_seqNo='+quotedStr(fSeqNo));
      Open;
      if fieldByName('cnt').asinteger>0 then
      begin
        result:=true;
      end;
    end;
  except
    application.MessageBox('检查是否有订单标签时发生错误!','提示信息',0);
  end;
end;

procedure TfrmLabelHelp10.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  //关闭窗口
  if key= vk_Escape then
  begin
    Close ;
  end;
  if key=VK_return  then
    Tok.Click ;
  if key=VK_f1  then
    Tload.Click ;
end;
////////////////////////////////////////////////////////
  //函数功能：打开标签文件
////////////////////////////////////////////////////////
procedure TfrmLabelHelp10.OpenLabel();
begin
  if ADOQueryLabel.IsEmpty then exit;
  with RMGridReport1 do
  begin
    try
      if ADOQueryLabel.fieldbyname('labelFile').IsNull then exit;
      LoadFromBlobField(tblobfield(ADOQueryLabel.fieldbyname('labelFile')));
      Preview :=RMPreview1;
      //ShowReport ;
      PrepareReport;
      ShowPreparedReport;
    except
    end;
  end;
  OpenPackLabel();
end;
////////////////////////////////////////////////////////
  //函数功能：打开标签文件
////////////////////////////////////////////////////////
procedure TfrmLabelHelp10.OpenPackLabel();
begin
  if ADOQueryLabel.IsEmpty then exit;
  if ADOQueryLabel.fieldByName('packLabelId').AsInteger <=0 then
  begin
    RMGridReport2.Preview:=nil;
    exit;
  end;
  /////////////////////////////////////////
  with ADOQueryPackLabel do
  begin
    close;
    sql.Add('select * from jd_label_sub');
    sql.Add('where labelId='+ADOQueryLabel.fieldByName('packLabelId').AsString);
    Open;
  end;
  ///////////////////////////////////////
  with RMGridReport2 do
  begin
    try
      if ADOQueryPackLabel.fieldbyname('labelFile').IsNull then exit;
      LoadFromBlobField(tblobfield(ADOQueryPackLabel.fieldbyname('labelFile')));
      Preview :=RMPreview2;
      //ShowReport ;
      PrepareReport;
      ShowPreparedReport;
    except
    end;
  end;
end;

procedure TfrmLabelHelp10.tv1FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
begin
  if isLoad then
    OpenLabel();
end;

end.
