unit U_LabelList;

interface

uses                                
  Windows, Messages, SysUtils, StrUtils,Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ToolWin, StdCtrls, BtnEdit, ExtCtrls, DB, ADODB,
  RM_System, RM_Common, RM_Class, RM_GridReport, Buttons, FTComboBox,
  RM_Preview, RM_e_Xls, RM_e_Graphic, RM_e_bmp, RM_BarCode,
  RM_DsgGridReport, RM_Dataset, cxStyles, cxCustomData, cxGraphics,
  cxFilter, cxData, cxDataStorage, cxEdit, cxDBData, cxGridLevel,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxClasses,
  cxControls, cxGridCustomView, cxGrid, DBClient, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator;

type
  TfrmLabelList = class(TForm)
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    OpenDialog1: TOpenDialog;
    RMGridReport1: TRMGridReport;
    ADOQueryTmp: TADOQuery;
    RMPreview1: TRMPreview;
    ADOQuery1: TADOQuery;
    Panel2: TPanel;
    cxGrid1: TcxGrid;
    tv1: TcxGridDBTableView;
    tv1labeltype: TcxGridDBColumn;
    tv1labelCaption: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    tv1labelFile: TcxGridDBColumn;
    DS_Label: TDataSource;
    ADOQueryLabel10: TADOQuery;
    Panel3: TPanel;
    Note: TMemo;
    EngAddress: TEdit;
    ChnAddress: TEdit;
    engFactory: TEdit;
    ChnFactory: TEdit;
    TelePhone: TEdit;
    ywy: TEdit;
    customNo: TBtnEditA;
    Label1: TLabel;
    Label2: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label3: TLabel;
    tv1labelId: TcxGridDBColumn;
    ToolBar2: TToolBar;
    Tadd: TToolButton;
    Tupd: TToolButton;
    Tdel: TToolButton;
    TOK: TToolButton;
    Tclose: TToolButton;
    Panel4: TPanel;
    ToolButton1: TToolButton;
    Label9: TLabel;
    Label10: TLabel;
    LabelCaption: TEdit;
    LabelType: TFTComboBox;
    ADOQueryLabel: TClientDataSet;
    procedure TcloseClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure TsaveClick(Sender: TObject);
    procedure customNoBtnClick(Sender: TObject);
    procedure BtOpenClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure RMPreview1DblClick(Sender: TObject);
    procedure TaddClick(Sender: TObject);
    procedure TupdClick(Sender: TObject);
    procedure tv1FocusedRecordChanged(Sender: TcxCustomGridTableView;
      APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
    procedure TdelClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure TOkClick(Sender: TObject);
    procedure tv1DblClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure LabelTypeChange(Sender: TObject);
    procedure tv1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
  private
    isLoad:Boolean;
    function SaveData():Boolean;
    function EditData():Boolean;
    function IsCheckCustOk():Boolean;
    function DeleteData():Boolean;
    procedure InitWinData();
    procedure InitVarDictionary();
    procedure InitDataSetDictionary();
    procedure InitGrid();
    procedure OpenLabel();
    procedure SetWinStatus();
    procedure DoFilter();
  public
    fSelLabelId,LBName,LBInt,SLBName:String;
    fKeyNo:string;
    fchg:Boolean;
    fIsShowModal:Boolean;
    fWinStatus:integer;
  end;

var
  frmLabelList: TfrmLabelList;

implementation
uses
  U_DataLink, U_LabelAdd,U_RTFun;
{$R *.dfm}
procedure TfrmLabelList.DoFilter();
var
  filterStr:string;
begin
  filterStr:='';

  if trim(LabelType.Text) <>'' then
  begin
    filterStr:=' and LabelType like '+quotedStr('%'+trim(LabelType.Text)+'%');
  end;
  //名称
  if trim(LabelCaption.Text)<>'' then
  begin
    filterStr:=filterStr+' and LabelCaption like '+quotedStr('%'+trim(LabelCaption.Text)+'%');
  end;
  try
    ADOQueryLabel10.DisableControls ;
    if trim(filterStr)='' then
    begin
      ADOQueryLabel.Filtered:=false;
      ADOQueryLabel.EnableControls;
      exit;
    end;
    filterStr:=trim(RightBStr(filterStr,length(filterStr)-4));
    with ADOQueryLabel do
    begin
      filtered:=false;
      filter:=filterStr;
      filtered:=true;
    end;
    SDofilter(ADOQueryLabel10,filterStr);
    SCreateCDS20(ADOQueryLabel10,ADOQueryLabel);
    SInitCDSData20(ADOQueryLabel10,ADOQueryLabel);
  finally
    ADOQueryLabel10.EnableControls;
  end;
end;
procedure TfrmLabelList.TcloseClick(Sender: TObject);
begin
  close;
end;

procedure TfrmLabelList.FormCreate(Sender: TObject);
begin
  panel1.Align :=alClient;
 // ClearWinData(panel3);
  fSelLabelId := '';



end;

procedure TfrmLabelList.TsaveClick(Sender: TObject);
begin
  if trim(customNO.Text)='' then
  begin
    application.MessageBox('客户名称不能为空,请选择客户！','提示');
    customNo.SetFocus;
    exit;
  end;
  if application.MessageBox('确定要保存吗?','提示信息',1)=2 then exit; 
  if fWinStatus=0 then
  begin
    if not IsCheckCustOk() then exit;
    if SaveData() then
    begin
      fWinStatus:=1;
      fchg:=true;
      SetWinStatus();
    end;
  end
  else
  begin
    if EditData() then
    begin
      fchg:=true;
      application.MessageBox('保存成功!','提示信息',0) 
    end;
  end;
end;
/////////////////////////////////////////////////////////////////////
  //函数功能：保存数据
/////////////////////////////////////////////////////////////////////
function TfrmLabelList.SaveData():Boolean;
begin
  try
     with ADOQueryCmd do
     begin
        close;
        sql.Clear;
        sql.Add('select * from JD_Label');
        sql.Add('where 1<>1');
        Open;

        Append;
        fieldByName('customno').value:=trim(customno.txtCode);
        fieldByName('ChnFactory').value:=trim(ChnFactory.text);
        fieldByName('engFactory').value:=trim(engFactory.text);
        fieldByName('TelePhone').value:=trim(TelePhone.text);
        fieldByName('ChnAddress').value:=trim(ChnAddress.text);
        fieldByName('EngAddress').value:=trim(EngAddress.text);
        fieldByName('filler').value:=Dname;
        fieldByName('filltime').value:=DServerDate;
        fieldByName('note').value:= trim(Note.text);
        Post;
    end;
    result:=true;
  except
    Result:=false;
    application.MessageBox('保存标签模板出错!','警告信息',0);
  end;
end;

/////////////////////////////////////////////////////////////////////
  //函数功能：保存数据
/////////////////////////////////////////////////////////////////////
function TfrmLabelList.EditData():Boolean;
begin
  try
     with ADOQueryCmd do
     begin
        close;
        sql.Clear;
        sql.Add('select * from JD_Label');
        sql.Add('where customNo='+fKeyNo);
        Open;

        Edit;
        fieldByName('customno').value:=trim(customno.txtCode);
        fieldByName('ChnFactory').value:=trim(ChnFactory.text);
        fieldByName('engFactory').value:=trim(engFactory.text);
        fieldByName('TelePhone').value:=trim(TelePhone.text);
        fieldByName('ChnAddress').value:=trim(ChnAddress.text);
        fieldByName('EngAddress').value:=trim(EngAddress.text);
        fieldByName('note').value:= trim(Note.text);
        Post;
    end;
    result:=true;
  except
    Result:=false;
    application.MessageBox('保存标签模板出错!','警告信息',0);
  end;
end;

procedure TfrmLabelList.customNoBtnClick(Sender: TObject);
 begin
 { FormGetCust:=TFormGetCust.Create(self);

  if FormGetCust.ShowModal=mrok then
  begin
    customNo.TxtCode:=trim(FormGetCust.ADOQuery1.Fieldbyname('customno').AsString);
    customNo.Text:=trim(FormGetCust.ADOQuery1.Fieldbyname('shortname').AsString);
  end;
  FormGetCust.Free;                         }
{  frmCustHelp:=TfrmCustHelp.create(self);
  with frmCustHelp do
  begin
    if showModal=1 then
    begin
      customNo.TxtCode:=trim(ADOQueryHelp.Fieldbyname('customno').AsString);
      customNo.Text:=trim(ADOQueryHelp.Fieldbyname('shortname').AsString);
    end;
    free;
  end;
 }
end;

procedure TfrmLabelList.BtOpenClick(Sender: TObject);
begin

end;
////////////////////////////////////////////////////////////
  //初始化窗口数据
////////////////////////////////////////////////////////////
procedure TfrmLabelList.InitWinData();
begin
  try
    with ADOQueryTmp do
    begin
      close;
      sql.Clear ;
      sql.Add('select A.* ,B.customName as customNoName');
      sql.Add('from JD_Label A');
      sql.Add('INNER JOIN BC_customer B ON A.customNO=B.customNo');
      sql.Add('WHERE B.customNo='''+fkeyNo+'''');
      Open;
      if isEmpty then
      begin
        close;
        exit;
      end;

     // SetWinData(ADOQueryTmp,panel3);
      {
      RMGridReport1.LoadFromBlobField(tblobfield(fieldbyname('labelFile')));
      RMGridReport1.Preview :=RMPreview1;
      RMGridReport1.PrepareReport;
      RMGridReport1.ShowReport ;
      }
    end;
  except
  end;
end;

procedure TfrmLabelList.FormShow(Sender: TObject);
begin
  if fWinStatus=1 then tok.Visible:=false;
  InitGrid();
  if Trim(SLBName)<>'' then
  begin
    ADOQueryLabel.Locate('labelCaption',SLBName,[]);
  end;
end;

procedure TfrmLabelList.RMPreview1DblClick(Sender: TObject);
begin
  //btOpen.Click ;
end;
////////////////////////////////////////////////////////////
  //
////////////////////////////////////////////////////////////
procedure TfrmLabelList.InitVarDictionary();
var
  TmpList:Tstrings;
  mm:string;
  i:integer;
begin
  try
    TmpList:=TstringList.Create();
    with ADOQueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('select distinct ItemType from JC_LabelSetItems ');
      sql.Add('where valid=''Y''');
      Open;
      TmpList.Clear ;
      while not Eof do
      begin
        TmpList.Add(trim(fieldByName('ItemType').AsString));
        Next;
      end;
    end;
  finally
    TmpList.Free ;
  end;
end;
/////////////////////////////////////////////////
  //
/////////////////////////////////////////////////
procedure TfrmLabelList.InitDataSetDictionary();
begin
{  with ADOQuery1 do
  begin
    close;
    sql.Clear ;
    sql.Add('exec P_Get_LabelPrintData');
    sql.Add(quotedStr(''));
    sql.Add(','+quotedStr(''));
    sql.Add(','+quotedStr(''));
    OPen;
  end; }
end;

procedure TfrmLabelList.TaddClick(Sender: TObject);
begin
  try
    frmLabelAdd:=TfrmLabelAdd.create(self);
    with frmLabelAdd do
    begin
      if showModal =1 then
      begin
        fchg:=true;
        InitGrid();
      end;
    end;
  finally
    frmLabelAdd.Free;
  end;
end;

procedure TfrmLabelList.TupdClick(Sender: TObject);
begin
  if ADOQueryLabel.IsEmpty then exit;
  try
    frmLabelAdd:=TfrmLabelAdd.create(self);
    with frmLabelAdd do
    begin
      fKeyNo:=ADOQueryLabel.fieldByName('LabelId').AsString ;
      fWinstatus:=1;
      if showModal =1 then
      begin
        fchg:=true;
        InitGrid();
      end;
    end;
  finally
    frmLabelAdd.Free;
  end;

end;
/////////////////////////////////////////////
  //
/////////////////////////////////////////////
procedure TfrmLabelList.InitGrid();
begin
  try
    isLoad:=false;
    ADOQueryLabel10.DisableControls ;
    with ADOQueryLabel10 do
    begin
      close;
      sql.Clear ;
      sql.Add('select * from P_Label');
      sql.Add('where valid=''Y''');
      sql.Add(' order by labelCaption');
      Open;
    end;
    SCreateCDS20(ADOQueryLabel10,ADOQueryLabel);
    SInitCDSData20(ADOQueryLabel10,ADOQueryLabel);
  finally
    ADOQueryLabel10.EnableControls;
    isLoad:=true;
    //DoFilter();
    //OpenLabel();
  end;
end;
////////////////////////////////////////////////////////
  //函数功能：打开标签文件
////////////////////////////////////////////////////////
procedure TfrmLabelList.OpenLabel();
begin
  if ADOQueryLabel.IsEmpty then exit;
  with RMGridReport1 do
  begin
    LoadFromBlobField(tblobfield(ADOQueryLabel.fieldbyname('labelFile')));
    //Preview :=RMPreview1;
    ShowReport ;
  end;
end;
procedure TfrmLabelList.tv1FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
begin
  
end;
//////////////////////////////////////////////////////////
  //函数功能：检查该客户的标签是否已存在
/////////////////////////////////////////////////////////
function TfrmLabelList.IsCheckCustOk():Boolean;
begin
  try
    with ADOQueryTmp do
    begin
      close;
      sql.Clear ;
      sql.Add('select count(customNO)as cnt from P_Label');
      sql.Add('where customNO='''+trim(customNO.TxtCode)+'''');
      Open;

      if fieldByName('cnt').AsInteger>0 then
      begin
        Result:=false ;
        application.MessageBox('该客户标签信息已存!','警告信息',0);
      end
      else
        Result:=true;
    end;
  except
    result:=false;
    application.MessageBox('检查该客户标签信息是否已存在时发生错误!','警告信息',0);
  end;
end;
/////////////////////////////////////////////////////////////
  //
/////////////////////////////////////////////////////////////
function TfrmLabelList.DeleteData():Boolean;
begin
  try
    with ADOQueryCmd do
    begin
      close;
      sql.clear;
      sql.Add('delete P_Label');
      sql.Add('where labelId='+ADOQueryLabel.fieldByName('LabelID').asString);
      execSql;
    end;
    result:=true;
  except
    result:=false;
    application.MessageBox('删除失败!','警告信息',0);
  end;
end;
procedure TfrmLabelList.TdelClick(Sender: TObject);
begin
  if ADOQueryLabel.IsEmpty then exit;
  if application.MessageBox('确定要删除此标签吗？','警告信息',1)=2 then exit;
  if  DeleteData() then
  begin
    fchg:=true;
    InitGrid();
  end;
end;
//////////////////////////////////////////////////////////
  //
/////////////////////////////////////////////////////////
procedure TfrmLabelList.SetWinStatus();
begin
  case fWinStatus of
    0:
      begin
     //   ToolBar2.Visible :=false;
       // tsave.Visible :=true;
        customNo.Enabled :=true;
        panel3.Enabled :=true;
      end;
    1:
      begin
    //    ToolBar2.Visible :=true;
      //  tsave.Visible :=false;
        customNo.Enabled :=false;
        panel3.Enabled :=false;
        TOK.Visible:=false;
      end;
    5:
      begin
     //   ToolBar2.Visible :=false;
      //  tsave.Visible :=false;
        panel1.Enabled :=false;
        panel3.Enabled :=false;
      end;
  end ;
end;

procedure TfrmLabelList.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  {if fIsShowModal then
    Application:=MainApplication ; }
  Action:=caFree;
end;

procedure TfrmLabelList.FormDestroy(Sender: TObject);
begin
  frmLabelList:=nil;
end;

procedure TfrmLabelList.TOkClick(Sender: TObject);
begin
  if ADOQueryLabel.IsEmpty then exit;
  LBName:=Trim(ADOQueryLabel.fieldbyname('labelCaption').AsString);
  ModalResult:=1;
end;

procedure TfrmLabelList.tv1DblClick(Sender: TObject);
begin
  TOk.Click ;
end;

procedure TfrmLabelList.ToolButton1Click(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmLabelList.LabelTypeChange(Sender: TObject);
begin
  DoFilter();
end;

procedure TfrmLabelList.tv1CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  if isLoad then
    OpenLabel();
end;

end.
