unit U_GSList;

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, cxPC, cxDropDownEdit, Menus, BtnEdit;

type
  TfrmGSList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ToolButton2: TToolButton;
    ADOQueryMain: TADOQuery;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    Tadd: TToolButton;
    DS_HZ3: TDataSource;
    CDS_HZ3: TClientDataSet;
    cxGridPopupMenu4: TcxGridPopupMenu;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    V1Column1: TcxGridDBColumn;
    cxGYSName: TcxGridDBColumn;
    cxGridDBGYSNameZC: TcxGridDBColumn;
    B1GYSFZRen: TcxGridDBColumn;
    cxGridDBGYSTel: TcxGridDBColumn;
    V1SHStatus: TcxGridDBColumn;
    V1GYSCodeFW: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    PopupMenu1: TPopupMenu;
    MainEdit: TMenuItem;
    MainView: TMenuItem;
    TBFJUp1: TPopupMenu;
    BankEdit: TMenuItem;
    BankDel: TMenuItem;
    BankView: TMenuItem;
    cxGrid3: TcxGrid;
    TV3: TcxGridDBTableView;
    V3Column2: TcxGridDBColumn;
    V3BankName: TcxGridDBColumn;
    V3BankNo: TcxGridDBColumn;
    V3Column3: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    TBAddBank: TMenuItem;
    V3Column1: TcxGridDBColumn;
    V1Column2: TcxGridDBColumn;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    Panel1: TPanel;
    V1Column3: TcxGridDBColumn;
    V3Column4: 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 cxTabControl1Change(Sender: TObject);
    procedure TV1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure TV1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure MainEditClick(Sender: TObject);
    procedure MainViewClick(Sender: TObject);
    procedure BankEditClick(Sender: TObject);
    procedure BankDelClick(Sender: TObject);
    procedure BankViewClick(Sender: TObject);
    procedure TV3DblClick(Sender: TObject);
    procedure TBAddBankClick(Sender: TObject);
  private
    { Private declarations }
    ColorInt:Integer;
    procedure InitGrid();
    procedure setstatus();
    procedure InitGridBankMain();
    procedure InitGridBankMainID(FID:String);
  public
    canshu1,canshu2,FGYSType,XZKHTYpe:string;
  end;

//var
  //frmGSList: TfrmGSList;

implementation
uses
  U_DataLink,U_RTFun,U_ZDYHelp,U_GSInPutMain,U_GSInPutMainBank,U_FjList;

{$R *.dfm}
procedure TfrmGSList.setstatus();
begin
  canshu2:='GS';
  IF trim(Self.Caption)='公司账户资料查询*'then
  begin
    TBAddBank.Visible:=False;
    Tadd.Visible:=False;
    TBDel.Visible:=False;
    MainEdit.Visible:=False;
    BankEdit.Visible:=False;
    BankDel.Visible:=False;
  end;
end;

procedure TfrmGSList.InitGrid();
begin
  with ADOQueryMain do
  begin
    Filtered:=False;
    Close;
    SQL.Clear;
    sql.Add(' select A.* ');
    sql.Add(',SDQty=(select Count(*) from TP_File TF where TF.WBID=A.KHMainId and TF.TFType=''公司资料''  )');
    sql.Add(' from KH_Main A ');
    sql.add(' where isnull(KHFlag,'''')='''+Trim(canshu2)+''' and isnull(Valid,'''')=''Y''  ');
    Open;
  end;
  SCreateCDS20(ADOQueryMain,CDS_HZ);
  SInitCDSData20(ADOQueryMain,CDS_HZ);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from KH_Sub_Bank where KHMainid='''+Trim(CDS_HZ.fieldbyname('KHMainid').asstring)+''' and isnull(Valid,'''')=''Y''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_HZ3);
  SInitCDSData20(ADOQueryTemp,CDS_HZ3);
end;

procedure TfrmGSList.FormDestroy(Sender: TObject);
begin
  //frmGSList:=nil;
end;

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

procedure TfrmGSList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('GS1',Tv1,'公司账户管理');
  WriteCxGrid('GS3',Tv3,'公司账户管理');
  Close;
end;
procedure TfrmGSList.TBDelClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then exit;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_HZ.DisableControls;
    with CDS_HZ do
    begin
     // while Locate('SSel',True,[]) do
      begin
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('UPdate  KH_Main  Set Valid=''N'',DelTime=getdate(),Deler='''+Trim(DName)+''',DelerCode='''+Trim(DCode)+'''');
          sql.Add(' where KHMainID='''+Trim(CDS_HZ.fieldbyname('KHMainID').AsString)+'''');
          
          sql.Add('UPdate  KH_Sub_Bank  Set Valid=''N'',DelTime=getdate(),Deler='''+Trim(DName)+''',DelerCode='''+Trim(DCode)+'''');
          sql.Add(' where KHMainID='''+Trim(CDS_HZ.fieldbyname('KHMainID').AsString)+'''');
          ExecSQL;
        end;
        CDS_HZ.Delete;
      end;
    end;
    CDS_HZ.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作失败!','提示',0);
    CDS_HZ.EnableControls;
  end;
end;

procedure TfrmGSList.FormShow(Sender: TObject);
begin
  ReadCxGrid('GS1',Tv1,'公司账户管理');
  ReadCxGrid('GS3',Tv3,'公司账户管理');
  setstatus();
  InitGrid();
end;

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

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

procedure TfrmGSList.TV1MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  InitGridBankMain();
end;

procedure TfrmGSList.InitGridBankMain();
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from KH_Sub_Bank where KHMainid='''+Trim(CDS_HZ.fieldbyname('KHMainid').asstring)+'''');
    if CDS_HZ.IsEmpty then
    begin
      sql.Add(' and 1=2');
    end else
    begin
      sql.Add(' and valid=''Y'' ');
    end;
    sql.Add(' order by XH');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_HZ3);
  SInitCDSData20(ADOQueryTemp,CDS_HZ3);
end;
procedure TfrmGSList.InitGridBankMainID(FID:String);
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from KH_Sub_Bank where KHMainid='''+Trim(FID)+'''');
    if CDS_HZ.IsEmpty then
    begin
      sql.Add(' and 1=2');
    end else
    begin
      sql.Add(' and valid=''Y'' ');
    end;
    sql.Add(' order by XH');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_HZ3);
  SInitCDSData20(ADOQueryTemp,CDS_HZ3);
end;


procedure TfrmGSList.TV1CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
var
  FName:string;
begin
  if CDS_HZ.IsEmpty then Exit;
  FName:=Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName;
  if Trim(FName)='SDQty' then
  begin
    if CDS_HZ.IsEmpty then Exit;
    try
      frmFjList:=TfrmFjList.Create(Application);
      with frmFjList do
      begin
        fkeyNO:=Trim(CDS_HZ.fieldbyname('KHMainid').AsString);
        fType:='公司资料';
        if Self.Caption='公司账户资料查询*' then
        begin
          fstatus:=1;
        end else
        begin
          fstatus:=0;
        end;

        if ShowModal=1 then
        begin

        end;
      end;
    finally
      frmFjList.Free;
    end;
  end;
end;

procedure TfrmGSList.MainEditClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then Exit;
  try
    frmGSInPutMain:=TfrmGSInPutMain.Create(Application);
    with frmGSInPutMain do
    begin
      FKHMainid:=Trim(Self.CDS_HZ.fieldbyname('KHMainId').AsString);
      FKHFlag:=Trim(Self.canshu2);
      if ShowModal=1 then
      begin
        TBRafresh.Click;
        CDS_HZ.Locate('KHMainId',FKHMainid,[]);
        InitGridBankMainID(FKHMainid);
      end;
    end;
  finally
    frmGSInPutMain.Free;
  end;
end;

procedure TfrmGSList.MainViewClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then Exit;
  try
    frmGSInPutMain:=TfrmGSInPutMain.Create(Application);
    with frmGSInPutMain do
    begin
      FKHMainid:=Trim(Self.CDS_HZ.fieldbyname('KHMainId').AsString);
      frmGSInPutMain.TBSave.Visible:=False;
      if ShowModal=1 then
      begin
        TBRafresh.Click;
      end;
    end;
  finally
    frmGSInPutMain.Free;
  end;
end;

procedure TfrmGSList.BankEditClick(Sender: TObject);
begin
  if CDS_HZ3.IsEmpty then exit;
  try
    frmGSInPutMainBank:=TfrmGSInPutMainBank.Create(Application);
    with frmGSInPutMainBank do
    begin
      FKHMainid:=Trim(Self.CDS_HZ.fieldbyname('KHMainId').AsString);
      FBKID:=Trim(Self.CDS_HZ3.fieldbyname('BKID').AsString);
      PState:=1;
      if ShowModal=1 then
      begin
        Self.InitGridBankMain();
      end;
    end;
  finally
    frmGSInPutMainBank.Free;
  end;

end;

procedure TfrmGSList.BankDelClick(Sender: TObject);
begin
  IF CDS_HZ3.IsEmpty then exit;
 
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('Update KH_Sub_Bank Set Valid=''N'',DelTime=getdate(),Deler='''+Trim(DName)+''',DelerCode='''+Trim(DCode)+'''');
    sql.add('where BKID='''+Trim(CDS_HZ3.fieldbyname('BKID').AsString)+'''');
    ExecSQL;
  end;
  CDS_HZ3.Delete;
end;

procedure TfrmGSList.BankViewClick(Sender: TObject);
begin
  if CDS_HZ3.IsEmpty then Exit;
  try
    frmGSInPutMainBank:=TfrmGSInPutMainBank.Create(Application);
    with frmGSInPutMainBank do
    begin
      FKHMainid:=Trim(Self.CDS_HZ.fieldbyname('KHMainId').AsString);
      FBKID:=Trim(Self.CDS_HZ3.fieldbyname('BKID').AsString);
      PState:=2;
      if ShowModal=1 then
      begin
        Self.InitGridBankMain();
      end;
    end;
  finally
    frmGSInPutMainBank.Free;
  end;
end;

procedure TfrmGSList.TV3DblClick(Sender: TObject);
begin
  BankView.Click;
end;

procedure TfrmGSList.TBAddBankClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then exit;
  try
    frmGSInPutMainBank:=TfrmGSInPutMainBank.Create(Application);
    with frmGSInPutMainBank do
    begin
      FKHMainid:=Trim(CDS_HZ.fieldbyname('KHMainId').AsString);
      PState:=0;
      if ShowModal=1 then
      begin
        Self.InitGridBankMain();
      end;
    end;
  finally
    frmGSInPutMainBank.Free;
  end;
end;

end.


