unit U_ZdyAttachmentX;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxGraphics, cxCustomData, cxStyles, cxTL, cxMaskEdit, DB, ADODB,
  cxInplaceContainer, cxDBTL, cxControls, cxTLData, ComCtrls, ToolWin,
  StdCtrls, cxFilter, cxData, cxDataStorage, cxEdit, cxDBData, DBClient,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, ExtCtrls,
  cxSplitter, cxGridLevel, cxClasses, cxGridCustomView, cxGrid,
  cxGridCustomPopupMenu, cxGridPopupMenu, RM_Dataset, RM_System, RM_Common,
  RM_Class, RM_GridReport, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdFTP,ShellAPI,IniFiles, cxCheckBox, cxCalendar,
  cxButtonEdit, cxTextEdit, cxDBLookupComboBox, cxPC;

type
  TfrmZdyAttachmentX = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    Label3: TLabel;
    zjm: TEdit;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    ToolButton3: TToolButton;
    Label1: TLabel;
    GYSNameZC: TEdit;
    TBedit: TToolButton;
    Label2: TLabel;
    GYSFZRen: TEdit;
    DEFStr7: TEdit;
    Label4: TLabel;
    CheckBox1: TCheckBox;
    Tchk: TToolButton;
    TNochk: TToolButton;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    ToolButton6: TToolButton;
    ToolButton7: TToolButton;
    Panel2: TPanel;
    Panel3: TPanel;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    cxGridDBColumn13: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridDBColumn15: TcxGridDBColumn;
    cxGridDBColumn16: TcxGridDBColumn;
    cxGridDBColumn20: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    cxGridPopupMenu3: TcxGridPopupMenu;
    CDS_HZ2: TClientDataSet;
    DS_HZ2: TDataSource;
    ADOQueryMain2: TADOQuery;
    DS_HZ3: TDataSource;
    ADOQueryMain3: TADOQuery;
    CDS_HZ3: TClientDataSet;
    cxGridPopupMenu4: TcxGridPopupMenu;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v2GYSFZRen: TcxGridDBColumn;
    v2GYSName: TcxGridDBColumn;
    v2GYSNameZC: TcxGridDBColumn;
    v2GYSAdd: TcxGridDBColumn;
    v2GYSTel: TcxGridDBColumn;
    v2KHCZ: TcxGridDBColumn;
    v2KHLXR: TcxGridDBColumn;
    v2KHPhone: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    v2Ssel: TcxGridDBColumn;
    Label5: TLabel;
    status: TComboBox;
    v2SHStatus: TcxGridDBColumn;
    cxGrid3: TcxGrid;
    TV3: TcxGridDBTableView;
    V1LXFS_BH: TcxGridDBColumn;
    cxLXFS_LXR: TcxGridDBColumn;
    cxGridDBLXFS_SJ: TcxGridDBColumn;
    V1CLXFS_DZ: TcxGridDBColumn;
    cxGridDLXFS_DH: TcxGridDBColumn;
    cxGridDBLXFS_CZ: TcxGridDBColumn;
    V1LXFS_ZW: TcxGridDBColumn;
    cxGridDBCLXFS_YX: TcxGridDBColumn;
    cxGridDBLXFS_Other: TcxGridDBColumn;
    cxGridDBLXFS_WX: TcxGridDBColumn;
    V1LXFS_BZ: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure zjmChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure TBeditClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure TchkClick(Sender: TObject);
    procedure TNochkClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure Tv2MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private

    { Private declarations }
    procedure InitGrid();
    procedure setstatus();
  public
    fkhType:string;
    canshu1:string;
  end;

var
  frmZdyAttachmentX: TfrmZdyAttachmentX;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp,U_ZdyAttInputX, U_FjList_RZ;

{$R *.dfm}

procedure TfrmZdyAttachmentX.setstatus();
begin
  TBedit.Visible:=false;
  TBdel.Visible:=false;
  tchk.Visible:=false;
  tnochk.Visible:=false;
  ToolButton7.Visible:=false;
  IF trim(canshu1)=''then
  begin
    TBedit.Visible:=true;
    TBdel.Visible:=true;
  end
  else IF trim(canshu1)='高权限'then
  begin
    ToolButton7.Visible:=True;
    TBedit.Visible:=true;
    tbdel.Visible:=True;
    tchk.Visible:=True;
    tNochk.Visible:=True;
  end;
 // InitGrid();
end;

procedure TfrmZdyAttachmentX.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      SQL.Clear;
      sql.Add(' select *,zjm=dbo.getpinyin(GYSName) ');
      sql.add(',SHStatus=(case when isnull(status,''0'')=''0'' then ''未审核'' else ''已审核'' end)');
      sql.add(' from KH_Zdy_AttachmentX where isnull(GYSType,'''')=''客户'' ');
      IF trim(canshu1)<>'高权限'then
      begin
        sql.Add(' and (GYSFZRen='+quotedstr(trim(DName)));
        SQL.Add(' or filler='+quotedstr(trim(DName))+')');
      end;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    SInitCDSData20(ADOQueryMain,CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
  end;
end;


procedure TfrmZdyAttachmentX.FormDestroy(Sender: TObject);
begin
  frmZdyAttachmentX:=nil;
end;

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

procedure TfrmZdyAttachmentX.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('客户资料11',Tv1,'客户自定义');
  WriteCxGrid('客户资料22',Tv2,'客户自定义');
  WriteCxGrid('客户资料33',Tv3,'客户自定义');
  Close;
end;
procedure TfrmZdyAttachmentX.TBDelClick(Sender: TObject);
begin
  ToolBar1.SetFocus;
  if CDS_HZ.IsEmpty then Exit;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  with CDS_HZ do
  begin
    DisableControls;
    First;
    while not eof do
    begin
      iF fieldbyname('ssel').AsBoolean=true then
      begin
        if Trim(CDS_HZ.fieldbyname('ATID').AsString)<>'' then
        begin
          with ADOQueryCmd do
          begin
            Close;
            SQL.Clear;
            sql.Add('delete KH_Zdy_AttachmentX where ATID='''+Trim(CDS_HZ.fieldbyname('ATID').AsString)+'''');
            sql.Add('delete GYS_ZHXX where ATID='''+Trim(CDS_HZ.fieldbyname('ATID').AsString)+'''');
            sql.Add('delete from GYS_LXFS where ATID='''+Trim(CDS_HZ.fieldbyname('ATID').AsString)+'''');
            ExecSQL;
          end;
        end;
        CDS_HZ.Delete;
      end
      else
      next;
    end;
    EnableControls;
  end;
end;

procedure TfrmZdyAttachmentX.FormShow(Sender: TObject);
begin
  ReadCxGrid('客户资料11',Tv1,'客户自定义');
  ReadCxGrid('客户资料22',Tv2,'客户自定义');
  ReadCxGrid('客户资料33',Tv3,'客户自定义');
  setstatus();
  InitGrid();
end;

procedure TfrmZdyAttachmentX.TBRafreshClick(Sender: TObject);
begin
  ToolBar1.SetFocus;
  InitGrid();
end;

procedure TfrmZdyAttachmentX.ToolButton2Click(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    SInitCDSData20(ADOQueryMain,CDS_HZ);
  end;
end;

procedure TfrmZdyAttachmentX.zjmChange(Sender: TObject);
begin
  ToolButton2.Click;
end;


procedure TfrmZdyAttachmentX.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  TcxGridToExcel(self.Caption,cxgrid2);
 // SelExportData(Tv2,ADOQueryMain,'客户资料列表');
end;

procedure TfrmZdyAttachmentX.TBeditClick(Sender: TObject);
begin
  IF CDS_HZ.IsEmpty then exit;
  try
    frmZdyAttInput:=TfrmZdyAttInput.Create(Application);
    with frmZdyAttInput do
    begin
      FCYID:=Trim(Self.CDS_HZ.fieldbyname('ATID').AsString);
      fkhType:=self.fkhType;
      if ShowModal=1 then
      begin
        TBRafresh.Click;
      end;
    end;
  finally
    frmZdyAttInput.Free;
  end;
end;

procedure TfrmZdyAttachmentX.ToolButton3Click(Sender: TObject);
begin
  try
    frmZdyAttInput:=TfrmZdyAttInput.Create(Application);
    with frmZdyAttInput do
    begin
      fkhType:=self.fkhType;
      FCYID:='';
      if ShowModal=1 then
      begin
        TBRafresh.Click;
      end;
    end;
  finally
    frmZdyAttInput.Free;
  end;
end;

procedure TfrmZdyAttachmentX.CheckBox1Click(Sender: TObject);
begin
  SelOKNo(CDS_HZ,CheckBox1.Checked);
end;

procedure TfrmZdyAttachmentX.TchkClick(Sender: TObject);
begin
  ToolBar1.SetFocus;
  if CDS_HZ.IsEmpty then exit;
  if CDS_HZ.Locate('ssel',true,[])=false then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;

 try
   with CDS_HZ do
   begin
     DisableControls;
     First;
     while not eof do
     begin
       iF fieldbyname('ssel').AsBoolean then
       begin
         with ADOQueryCmd do
         begin
           close;
           sql.Clear;
           sql.Add('update  KH_Zdy_AttachmentX SET status=1,chker='''+trim(DName)+''',chkTime=getdate() ');
           sql.Add('where ATID='+quotedstr(trim(CDS_HZ.fieldbyname('ATID').AsString)));
           execsql;
         end;
       end;
       next;
     end;
     First;
     EnableControls;
    end;
    application.MessageBox('审核成功！','提示信息');
    TBRafresh.Click;
  except
    CDS_HZ.EnableControls;
    application.MessageBox('审核失败！','提示信息',0);
  end;
end;

procedure TfrmZdyAttachmentX.TNochkClick(Sender: TObject);
begin
  ToolBar1.SetFocus;
  if CDS_HZ.IsEmpty then exit;
  if CDS_HZ.Locate('ssel',true,[])=false then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  try
   with CDS_HZ do
   begin
     DisableControls;
     First;
     while not eof do
     begin
       iF fieldbyname('ssel').AsBoolean then
       begin
         with ADOQueryCmd do
         begin
           close;
           sql.Clear;
           sql.Add('update  KH_Zdy_AttachmentX SET status=0,chker='''',chkTime=null ');
           sql.Add('where ATID='+quotedstr(trim(CDS_HZ.fieldbyname('ATID').AsString)));
           execsql;
         end;
       end;
       next;
     end;
     First;
     EnableControls;
    end;
    application.MessageBox('撤销审核成功！','提示信息');
    TBRafresh.Click;
  except
    CDS_HZ.EnableControls;
    application.MessageBox('撤销审核失败！','提示信息',0);
  end;
end;

procedure TfrmZdyAttachmentX.FormCreate(Sender: TObject);
begin
  canshu1:=trim(DParameters1);
end;

procedure TfrmZdyAttachmentX.cxTabControl1Change(Sender: TObject);
begin
  setstatus();
  InitGrid();
end;

procedure TfrmZdyAttachmentX.ToolButton4Click(Sender: TObject);
begin
  ToolBar1.SetFocus;
  if CDS_HZ.IsEmpty then Exit;
  try
    frmFjList_RZ:=TfrmFjList_RZ.Create(Application);
    with frmFjList_RZ do
    begin
    //  PState:=1;
   //   if Trim(DParameters1)<>'高权限'  then cxButton1.Visible:=false;
      fkeyNO:=Trim(Self.CDS_HZ.fieldbyname('ATID').AsString);
      fType:='客户资料';
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmFjList_RZ.Free;
  end;
end;

procedure TfrmZdyAttachmentX.ToolButton5Click(Sender: TObject);
begin
  ToolBar1.SetFocus;
  IF CDS_HZ.IsEmpty then exit;
  try
    frmZdyAttInput:=TfrmZdyAttInput.Create(Application);
    with frmZdyAttInput do
    begin
      TBsave.Visible:=false;
      ToolBar2.Visible:=false;
      FCYID:=Trim(Self.CDS_HZ.fieldbyname('ATID').AsString);
      fkhType:=self.fkhType;
      if ShowModal=1 then
      begin
        
      end;
    end;
  finally
    frmZdyAttInput.Free;
  end;
end;

procedure TfrmZdyAttachmentX.ToolButton6Click(Sender: TObject);
var
  fPrintFile:string;
   Txt,fImagePath:string;
   Moudle: THandle;
   Makebar:TMakebar;
   Mixtext:TMixtext;
begin
  ToolBar1.SetFocus;
  IF CDS_HZ.IsEmpty then exit;
  try
    Moudle:=LoadLibrary('MakeQRBarcode.dll');
    @Makebar:=GetProcAddress(Moudle,'Make');
    @Mixtext:=GetProcAddress(Moudle,'MixText');
    Txt:=Trim(CDS_HZ.fieldbyname('ZDYCODE').AsString);
    fImagePath:=ExtractFilePath(Application.ExeName)+'image\temp.bmp' ;
    if not DirectoryExists(pchar(ExtractFilePath(Application.ExeName)+'image')) then
    CreateDirectory(pchar(ExtractFilePath(Application.ExeName)+'image'),nil);
    if FileExists(fImagePath) then DeleteFile(fImagePath);
    Makebar(pchar(Txt),Length(Txt),3,3,0,PChar(fImagePath),3);
  except
    application.MessageBox('条形码生成失败！','提示信息',MB_ICONERROR);
   exit;
  end;

  fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\客户资料信息.rmf' ;
  if FileExists(fPrintFile) then
  begin
    RMVariables['QRBARCODE']:=fImagePath;
    RM1.LoadFromFile(fPrintFile);
 //   Rm2.DefaultCopies:=strtointdef(trim(ComboBox1.Text),1);
   // RM1.ShowReport;
    RM1.PrintReport;
  end
  else
     Application.MessageBox(PChar('没有找到文件'+fPrintFile),'提示',0);


end;

procedure TfrmZdyAttachmentX.ToolButton7Click(Sender: TObject);
var
  FuserName:string;
begin
  ToolBar1.SetFocus;
  IF CDS_HZ.IsEmpty then exit;
  frmZDYHelp:=TfrmZDYHelp.Create(self);
  with frmZDYHelp do
  begin
    flag:='YWY';
    FlagName:='业务员';
    IF showmodal=1 then
    begin
      FuserName:=ClientDataSet1.fieldbyname('zdyName').AsString;
    end;
    free;
  end;
  IF trim(FuserName)<>'' then
  begin
    with CDS_HZ do
    begin
      DisableControls;
      First;
      while not eof do
      begin
        iF fieldbyname('ssel').AsBoolean=true then
        begin
          with ADOQueryCmd do
          begin
            Close;
            SQL.Clear;
            sql.Add('update KH_Zdy_AttachmentX SET GYSFZRen='''+trim(FuserName)+''' where ATID='''+Trim(CDS_HZ.fieldbyname('ATID').AsString)+'''');
            ExecSQL;
          end;
        end;
        next;
      end;
      EnableControls;
    end;
    TBRafresh.Click;
  end;
end;

procedure TfrmZdyAttachmentX.Tv2MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  with ADOQueryMain2 do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from GYS_ZHXX where ATID='''+Trim(CDS_HZ.fieldbyname('ATID').asstring)+'''');
    sql.Add(' order by XHNo');
    Open;
  end;
  SCreateCDS20(ADOQueryMain2,CDS_HZ2);
  SInitCDSData20(ADOQueryMain2,CDS_HZ2);
  with ADOQueryMain3 do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from GYS_LXFS where ATID='''+Trim(CDS_HZ2.fieldbyname('ATID').asstring)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryMain3,CDS_HZ3);
  SInitCDSData20(ADOQueryMain3,CDS_HZ3);
end;

end.


