unit U_ZHKHListNew;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, ToolWin, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, ADODB,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel, cxClasses,
  cxControls, cxGridCustomView, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGrid, DBClient, cxCheckBox, cxCalendar, cxSplitter,
  RM_Dataset, RM_System, RM_Common, RM_Class, RM_GridReport, RM_e_Xls,
  Menus, cxButtonEdit, cxDropDownEdit;

type
  TfrmZHKHListNew = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    TBDel: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBDataSet1: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    PopupMenu1: TPopupMenu;
    N2: TMenuItem;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ADOQueryPrint: TADOQuery;
    CDS_Print: TClientDataSet;
    ToolButton3: TToolButton;
    PopupMenu2: TPopupMenu;
    N11: TMenuItem;
    N21: TMenuItem;
    N31: TMenuItem;
    Panel1: TPanel;
    Label1: TLabel;
    Label9: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    KHNameJC: TEdit;
    v1Column5: TcxGridDBColumn;
    CDS_Chk: TClientDataSet;
    DS_Chk: TDataSource;
    cxGridPopupMenu2: TcxGridPopupMenu;
    cxGridPopupMenu3: TcxGridPopupMenu;
    CDS_Sub: TClientDataSet;
    DS_Sub: TDataSource;
    v1Column2: TcxGridDBColumn;
    CDS_Chker: TClientDataSet;
    v1Column4: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    CheckBox1: TCheckBox;
    Label2: TLabel;
    KHCode: TEdit;
    v1Column1: TcxGridDBColumn;
    Label34: TLabel;
    KHType: TComboBox;
    v1Column3: TcxGridDBColumn;
    Label3: TLabel;
    HZEmail: TEdit;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    Label4: TLabel;
    KHLaiYuan: TComboBox;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure CustomerNoNameChange(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
  private
    Canshu1:String;
    DQdate:TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;

    { Private declarations }
  public
    FFInt,FCloth:Integer;

    { Public declarations }
  end;

var
  frmZHKHListNew: TfrmZHKHListNew;

implementation
uses
  U_DataLink,U_RTFun,U_ZDYHelp, U_ZHKHInPutTabNew, U_ZDYHelpSel;

{$R *.dfm}

procedure TfrmZHKHListNew.FormDestroy(Sender: TObject);
begin
  frmZHKHListNew:=nil;
end;

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

procedure TfrmZHKHListNew.FormCreate(Sender: TObject);
begin
  cxgrid1.Align:=alClient;
  DQdate:=SGetServerDate(ADOQueryTemp);
  Canshu1:=Trim(DParameters1);
end;

procedure TfrmZHKHListNew.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('展会客户登记N',Tv1,'客户管理');

end;

procedure TfrmZHKHListNew.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select A.*,HZEmail=dbo.F_Get_KHInfo(A.ZKID,''Email'')  ');
      sql.Add(' from ZH_KH_Info A  ');
      if CheckBox1.Checked then
      begin
        SQL.Add('where  ZKDate>='''+FormatDateTime('yyyy-MM-dd',BegDate.DateTime)+'''');
        SQL.Add('and  ZKDate<'''+FormatDateTime('yyyy-MM-dd',enddate.DateTime+1)+'''');
      end else
      begin
        SQL.Add(' where 1=1 ');
      end;
      sql.Add(' and Type=''KH'' ');
      if Trim(Canshu1)<>'高权限'  then
      begin
        if Trim(Canshu1)='组' then
        begin
          sql.Add('and Filler in(select UserName from  SY_User where DPID in');
          sql.Add('(select DPID from SY_User where UserName='''+Trim(DName)+'''))');
        end else
        begin
          sql.Add('and (A.Filler='''+Trim(DName)+'''');
          sql.Add(' or YWY='''+Trim(DName)+''')');
        end;
        sql.Add('  and Valid=''Y'' ');
      end;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;


procedure TfrmZHKHListNew.InitForm();
var
  fsj:string;
begin
  ReadCxGrid('展会客户登记N',Tv1,'客户管理');
  BegDate.DateTime:=SGetServerDate10(ADOQueryTemp)-7;
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  fsj:='select distinct(KHLaiYuan) Name,Cast('''' as varchar(20)) Code from ZH_KH_Info where isnull(KHLaiYuan,'''')<>'''' and Type=''KH'' '  ;
  SInitComBoxBySql(ADOQueryTemp,KHLaiYuan,False,fsj);
  fsj:='select distinct(KHType) Name,Cast('''' as varchar(20)) Code from ZH_KH_Info where isnull(KHType,'''')<>'''' and Type=''KH'' '  ;
  SInitComBoxBySql(ADOQueryTemp,KHType,False,fsj);
  InitGrid();
end;

procedure TfrmZHKHListNew.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active=False then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;

procedure TfrmZHKHListNew.TBEditClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  if Trim(Canshu1)='' then
  begin
    if Trim(Order_Main.fieldbyname('Filler').AsString)<>Trim(DName) then
    begin
      if Trim(Order_Main.fieldbyname('YWY').AsString)<>Trim(DName) then
      begin
        Application.MessageBox('不能操作他人的数据!','提示',0);
        Exit;
      end;
    end;
  end;

  try
    frmZHKHInPutTabNew:=TfrmZHKHInPutTabNew.Create(Application);
    with frmZHKHInPutTabNew do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('ZKId').AsString);
      FTFID:=Trim(Self.Order_Main.fieldbyname('TPID').AsString);
      TPInt:=99;
      TPIntOther:=99;
      if ShowModal=1 then
      begin
        Self.Order_Main.Locate('ZKID',Trim(FMainId),[]);
        with Self.Order_Main do
        begin
          Edit;
          FieldByName('ZKID').Value:=Trim(FMainId);
          FieldByName('ZKDate').Value:=ZKDate.Date;
          FieldByName('YWY').Value:=Trim(YWY.Text);
          FieldByName('KHCode').Value:=Trim(KHCode.Text);
          FieldByName('KHNameJC').Value:=Trim(KHNameJC.Text);
          FieldByName('KHType').Value:=Trim(KHType.Text);
          FieldByName('Filler').Value:=Trim(DName);
          Post;
        end;
      end;
    end;
  finally
    frmZHKHInPutTabNew.Free;
  end;
end;

procedure TfrmZHKHListNew.TBDelClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  if Trim(Canshu1)='' then
  begin
    if Trim(Order_Main.fieldbyname('Filler').AsString)<>Trim(DName) then
    begin
      if Trim(Order_Main.fieldbyname('YWY').AsString)<>Trim(DName) then
      begin
        Application.MessageBox('不能操作他人的数据!','提示',0);
        Exit;
      end;
    end;
  end;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  if DelData() then
  begin
    Order_Main.Delete;
  end;
end;

function TfrmZHKHListNew.DelData():Boolean;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update ZH_KH_Info Set Valid=''N'' where ZKId='''+Trim(Order_Main.fieldbyname('ZKId').AsString)+'''');
      ExecSQL;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=False;
    Application.MessageBox('数据删除异常！','提示',0);
  end;
end;

procedure TfrmZHKHListNew.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  TcxGridToExcel('客户资料',cxGrid1);
end;

procedure TfrmZHKHListNew.TBRafreshClick(Sender: TObject);
var
  fsj:String;
begin
  InitGrid();
  fsj:='select distinct(KHLaiYuan) Name,Cast('''' as varchar(20)) Code from ZH_KH_Info where isnull(KHLaiYuan,'''')<>'''' and Type=''KH'' '  ;
  SInitComBoxBySql(ADOQueryTemp,KHLaiYuan,False,fsj);
  fsj:='select distinct(KHType) Name,Cast('''' as varchar(20)) Code from ZH_KH_Info where isnull(KHType,'''')<>'''' and Type=''KH'' '  ;
  SInitComBoxBySql(ADOQueryTemp,KHType,False,fsj);
end;

procedure TfrmZHKHListNew.TBAddClick(Sender: TObject);
var
  maxno:string;
begin
  try
    frmZHKHInPutTabNew:=TfrmZHKHInPutTabNew.Create(Application);
    with frmZHKHInPutTabNew do
    begin
      PState:=0;
      FMainId:='';
      FTFID:='';
      TPInt:=99;
      TPIntOther:=99;
      if ShowModal=1 then
      begin
        with Self.Order_Main do
        begin
          Append;
          FieldByName('ZKID').Value:=Trim(FMainId);
          FieldByName('ZKDate').Value:=ZKDate.Date;
          FieldByName('YWY').Value:=Trim(YWY.Text);
          FieldByName('KHCode').Value:=Trim(KHCode.Text);
          FieldByName('KHNameJC').Value:=Trim(KHNameJC.Text);
          FieldByName('KHType').Value:=Trim(KHType.Text);
          FieldByName('Filler').Value:=Trim(DName);
          Post;
        end;
      end;
    end;
  finally
    frmZHKHInPutTabNew.Free;
  end;
end;

procedure TfrmZHKHListNew.FormShow(Sender: TObject);
begin
  InitForm();
end;

procedure TfrmZHKHListNew.CheckBox1Click(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmZHKHListNew.CheckBox2Click(Sender: TObject);
begin
  TBRafresh.Click;
end;

procedure TfrmZHKHListNew.ToolButton1Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  try
    frmZHKHInPutTabNew:=TfrmZHKHInPutTabNew.Create(Application);
    with frmZHKHInPutTabNew do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('ZKId').AsString);
      TBSave.Visible:=False;
      ToolBar2.Visible:=False;
      ToolBar3.Visible:=False;
      ToolBar5.Visible:=False;
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmZHKHInPutTabNew.Free;
  end;
end;

procedure TfrmZHKHListNew.ToolButton2Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  try
    frmZHKHInPutTabNew:=TfrmZHKHInPutTabNew.Create(Application);
    with frmZHKHInPutTabNew do
    begin
      PState:=1;
      CopyInt:=99;
      FMainId:=Trim(Self.Order_Main.fieldbyname('ZKId').AsString);
      FTFID:='';
      TPInt:=99;
      TPIntOther:=99;
      if ShowModal=1 then
      begin
        with Self.Order_Main do
        begin
          Append;
          FieldByName('ZKID').Value:=Trim(FMainId);
          FieldByName('ZKDate').Value:=ZKDate.Date;
          FieldByName('YWY').Value:=Trim(YWY.Text);
          FieldByName('KHCode').Value:=Trim(KHCode.Text);
          FieldByName('KHNameJC').Value:=Trim(KHNameJC.Text);
          FieldByName('KHType').Value:=Trim(KHType.Text);
          FieldByName('Filler').Value:=Trim(DName);
          Post;
        end;
      end;
    end;
  finally
    frmZHKHInPutTabNew.Free;
  end;
end;

procedure TfrmZHKHListNew.ToolButton3Click(Sender: TObject);
begin
  ModalResult:=1;
end;

procedure TfrmZHKHListNew.CustomerNoNameChange(Sender: TObject);
begin
  if ADOQueryMain.Active=False then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;

procedure TfrmZHKHListNew.Tv1DblClick(Sender: TObject);
begin
  ToolButton1.Click;
end;

end.
