unit U_ZdyAttachment;

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, cxDropDownEdit, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator;

type
  TfrmZdyAttachment = 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;
    ZdyName: TEdit;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    ToolButton3: TToolButton;
    Label1: TLabel;
    ZdyCode: TEdit;
    ToolButton4: TToolButton;
    Label2: TLabel;
    DEFstr5: TEdit;
    DEFStr7: TEdit;
    Label4: TLabel;
    TBuser: TToolButton;
    Label5: TLabel;
    KeFu: TEdit;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v2Column15: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v2Column14: TcxGridDBColumn;
    v2ZdyNameYW: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v2Column11: TcxGridDBColumn;
    v2Column12: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column10: TcxGridDBColumn;
    v2Column16: TcxGridDBColumn;
    v2Column7: TcxGridDBColumn;
    v2Column8: TcxGridDBColumn;
    v2Column9: TcxGridDBColumn;
    v2DefFlt1: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v2Column13: TcxGridDBColumn;
    v2QQ: TcxGridDBColumn;
    v2WeiXin: TcxGridDBColumn;
    v2Wangwang: TcxGridDBColumn;
    v2Email: TcxGridDBColumn;
    v2Note: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Panel2: TPanel;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    v2KHLXR: TcxGridDBColumn;
    v2LXZWu: TcxGridDBColumn;
    v2LXTel: TcxGridDBColumn;
    v2LXPhone: TcxGridDBColumn;
    v2LXFax: TcxGridDBColumn;
    v2LXEmail: TcxGridDBColumn;
    v2LXNote: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    ToolBar2: TToolBar;
    TAdd: TToolButton;
    TDel: TToolButton;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    TSave: TToolButton;
    ADOQuery1: TADOQuery;
    ToolButton5: TToolButton;
    V1MXQQ: TcxGridDBColumn;
    V1MXWeiXin: TcxGridDBColumn;
    V1WangWang: TcxGridDBColumn;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyleRepository2: TcxStyleRepository;
    cxStyle2: TcxStyle;
    cxStyleRepository3: TcxStyleRepository;
    cxStyle3: TcxStyle;
    v2Column1: TcxGridDBColumn;
    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 ZdyNameChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure TBuserClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure TDelClick(Sender: TObject);
    procedure TAddClick(Sender: TObject);
    procedure TSaveClick(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure Tv2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure TV1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
  private

    { Private declarations }
    canshu1: string;
    procedure InitGrid();

  public
    fkhType: string;
  end;

var
  frmZdyAttachment: TfrmZdyAttachment;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp, U_ZdyAttInput, U_ZdyInput, U_FjList_RZ;

{$R *.dfm}

procedure TfrmZdyAttachment.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add(' select * from KH_Zdy_Attachment where Type=''KHName'' ');
      if Trim(canshu1) <> '高权限' then
      begin
        sql.Add(' and( Filler=' + quotedstr(trim(DName)));
        sql.Add(' Or DEFstr5=' + quotedstr(trim(DName)));
        sql.Add(' Or KeFu=' + quotedstr(trim(DName)));
        sql.Add(')');
      end;
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_HZ);
    SInitCDSData20(ADOQueryMain, CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmZdyAttachment.FormDestroy(Sender: TObject);
begin
  frmZdyAttachment := nil;
end;

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

procedure TfrmZdyAttachment.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('客户资料FM', Tv2, '客户自定义');
  Close;
end;

procedure TfrmZdyAttachment.TBDelClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    Exit;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  if Trim(CDS_HZ.fieldbyname('ATID').AsString) <> '' then
  begin
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('delete KH_Zdy_Attachment where ATID=''' + Trim(CDS_HZ.fieldbyname('ATID').AsString) + '''');
      sql.Add('delete KH_Zdy where ZdyNo=''' + Trim(CDS_HZ.fieldbyname('ATID').AsString) + '''');
      ExecSQL;
    end;
  end;
  CDS_HZ.Delete;
end;

procedure TfrmZdyAttachment.FormShow(Sender: TObject);
begin
  ReadCxGrid('客户资料FM', Tv2, '客户自定义');
  InitGrid();
  if trim(canshu1) <> '高权限' then
  begin
    TBuser.Visible := false;
    TBDel.Visible := false;
    ToolButton4.Visible := false;
    ToolButton1.Visible := false;
    TDel.Visible := false;
  end;

end;

procedure TfrmZdyAttachment.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmZdyAttachment.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 TfrmZdyAttachment.ZdyNameChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

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

procedure TfrmZdyAttachment.ToolButton4Click(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 TfrmZdyAttachment.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 TfrmZdyAttachment.TBuserClick(Sender: TObject);
var
  FuserName: string;
begin
  if CDS_HZ.IsEmpty then
    exit;
  frmZDYHelp := TfrmZDYHelp.Create(self);
  with frmZDYHelp do
  begin
    flag := 'UserName';
    FlagName := '操作员';
    if showmodal = 1 then
    begin
      FuserName := ClientDataSet1.fieldbyname('zdyName').AsString;
    end;
    free;
  end;
  if trim(FuserName) <> '' then
  begin
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('update  KH_Zdy_Attachment SET filler=''' + trim(FuserName) + ''' where ATID=''' + Trim(CDS_HZ.fieldbyname('ATID').AsString) + '''');
      ExecSQL;
    end;
    TBRafresh.Click;
  end;
end;

procedure TfrmZdyAttachment.FormCreate(Sender: TObject);
begin
  canshu1 := Trim(DParameters1);
end;

procedure TfrmZdyAttachment.TDelClick(Sender: TObject);
begin
  if Order_Sub.IsEmpty then
    exit;
  if Order_Sub.FieldByName('ATSID').AsString <> '' then
  begin
    if application.MessageBox('确定要删行吗', '提示', 1) = 2 then
      exit;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.add('delete from KH_Zdy_Attachment_Sub ');
      sql.add('where ATSID=''' + trim(Order_Sub.fieldbyname('ATSID').AsString) + '''');
      execsql;
    end;
  end;
  Order_Sub.Delete;
end;

procedure TfrmZdyAttachment.TAddClick(Sender: TObject);
begin
  with Order_Sub do
  begin
    append;
    post;
  end;
end;

procedure TfrmZdyAttachment.TSaveClick(Sender: TObject);
var
  MaxLX: string;
begin
  if Order_Sub.IsEmpty then
    exit;
  if Application.MessageBox('确定要保存吗', '提示', 1) = 2 then
    exit;
  with Order_Sub do
  begin
    first;
    while not eof do
    begin
      if fieldbyname('ATSID').AsString = '' then
      begin
        if GetLSNo(ADOQueryCmd, MaxLX, 'AS', 'KH_Zdy_Attachment_Sub', 4, 1) = False then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('取最大号失败！', '提示', 0);
          Exit;
        end;
      end
      else
        MaxLX := trim(fieldbyname('ATSID').AsString);
      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('select * from KH_Zdy_Attachment_Sub ');
        sql.add('where ATSID=''' + Trim(MaxLX) + '''');
        open;
      end;
      with ADOQuerycmd do
      begin
        if IsEmpty then
          append
        else
          edit;
        fieldbyname('ATSID').Value := trim(MaxLX);
        fieldbyname('ATID').Value := trim(CDS_HZ.fieldbyname('ATID').AsString);
        fieldbyname('filler').Value := trim(DName);
        SSetSaveDataCDS(ADOQuerycmd, TV1, Order_Sub, 'KH_Zdy_Attachment_Sub', 0);
        post;
      end;
      with Order_Sub do
      begin
        Edit;
        FieldByName('ATSID').Value := Trim(MaxLX);
      end;
      next;
    end;
  end;
  Application.MessageBox('保存成功', '提示');
end;

procedure TfrmZdyAttachment.ToolButton5Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    Exit;
  try
    frmFjList_RZ := TfrmFjList_RZ.Create(Application);
    with frmFjList_RZ do
    begin
      fkeyNO := Trim(Self.CDS_HZ.fieldbyname('ATID').AsString);
      fType := '合同';
      if ShowModal = 1 then
      begin

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

procedure TfrmZdyAttachment.Tv2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  with ADOQuery1 do
  begin
    close;
    sql.Clear;
    sql.Add('select * from KH_Zdy_Attachment_Sub ');
    sql.Add('where ATID=''' + Trim(CDS_HZ.fieldbyname('ATID').AsString) + '''');
    open;
  end;
  SCreateCDS20(ADOQuery1, Order_Sub);
  SInitCDSData20(ADOQuery1, Order_Sub);
end;

procedure TfrmZdyAttachment.TV1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.add('select * from KH_Zdy_Attachment_Sub ');
    sql.add('where ATSID=''' + trim(Order_Sub.fieldbyname('ATSID').AsString) + '''');
    Open;
  end;
  if not ADOQueryTemp.IsEmpty then
  begin
    TV1.OptionsSelection.CellSelect := False;
  end
  else
  begin
    TV1.OptionsSelection.CellSelect := True;
  end;
end;

end.

