unit U_ZdyAttachGYSX;

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;

type
  TfrmZdyAttachGYSX = 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;
    zjm: TEdit;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    Tadd: TToolButton;
    TBedit: TToolButton;
    ToolButton4: TToolButton;
    Label5: TLabel;
    KHNameJC: TEdit;
    Panel2: TPanel;
    Panel3: TPanel;
    Panel4: TPanel;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    V1Column1: TcxGridDBColumn;
    cxGYSName: TcxGridDBColumn;
    cxGridDBGYSNameZC: TcxGridDBColumn;
    B1GYSFZRen: TcxGridDBColumn;
    cxGridDBGYSTel: TcxGridDBColumn;
    V1GYSCodeFW: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBKHMXName: TcxGridDBColumn;
    cxGridDBCKHMXSH: TcxGridDBColumn;
    cxGridDBKHMXLXR: TcxGridDBColumn;
    cxGridDBKHMXTel: TcxGridDBColumn;
    cxGridDBKHMXNote: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxGrid3: TcxGrid;
    TV3: TcxGridDBTableView;
    cxGridLevel3: TcxGridLevel;
    cxGridPopupMenu3: TcxGridPopupMenu;
    DS_HZ2: TDataSource;
    ADOQueryMain2: TADOQuery;
    CDS_HZ2: TClientDataSet;
    DS_HZ3: TDataSource;
    ADOQueryMain3: TADOQuery;
    CDS_HZ3: TClientDataSet;
    cxGridPopupMenu4: TcxGridPopupMenu;
    V1Column4: TcxGridDBColumn;
    V2Column4: TcxGridDBColumn;
    V1GYSType: TcxGridDBColumn;
    Label2: TLabel;
    TCK: TToolButton;
    Tchk: TToolButton;
    TNochk: TToolButton;
    Label4: TLabel;
    V1SHStatus: TcxGridDBColumn;
    SHStatus: TComboBox;
    Label1: TLabel;
    GYSCodeFW: TEdit;
    V2ZKHMXJC: TcxGridDBColumn;
    Label3: TLabel;
    YWY: TEdit;
    V3BankNo: TcxGridDBColumn;
    V3BankName: TcxGridDBColumn;
    V1FZR: 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 zjmChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure TBeditClick(Sender: TObject);
    procedure TaddClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton4Click(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 TchkClick(Sender: TObject);
    procedure TNochkClick(Sender: TObject);
    procedure TV2MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    { Private declarations }
    procedure InitGrid();
    procedure setstatus();

  public
    fDEFstr5:string;
    canshu1,FGYSType:string;
  end;

var
  frmZdyAttachGYSX: TfrmZdyAttachGYSX;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp, U_FjList_RZ,U_PBGYSInPutMain,U_PBGYSInPut;

{$R *.dfm}
procedure TfrmZdyAttachGYSX.setstatus();
begin
  tchk.Visible:=false;
  tnochk.Visible:=false;
  Tadd.Visible:=false;
  TBedit.Visible:=false;
  TBDel.Visible:=false;
  IF trim(canshu1)='高权限'then
  begin
    Tadd.Visible:=True;
    TBedit.Visible:=True;
    TBDel.Visible:=True;
    tchk.Visible:=True;
    tNochk.Visible:=True;
  end;
  if Trim(canshu1)='' then
  begin
    Tadd.Visible:=True;
    TBedit.Visible:=True;
    TBDel.Visible:=True;
  end;
end;

procedure TfrmZdyAttachGYSX.InitGrid();
begin
  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_Main ');
    sql.add(' where isnull(GYSType,'''')=''坯布'' ');
    //ShowMessage(sql.Text);
    Open;
  end;
  SCreateCDS20(ADOQueryMain,CDS_HZ);
  SInitCDSData20(ADOQueryMain,CDS_HZ);
  with ADOQueryMain2 do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from KH_Sub where KHMainid='''+Trim(CDS_HZ.fieldbyname('KHMainid').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 KH_Sub_Bank where KHSubid='''+Trim(CDS_HZ2.fieldbyname('KHSubid').asstring)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryMain3,CDS_HZ3);
  SInitCDSData20(ADOQueryMain3,CDS_HZ3);
end;

procedure TfrmZdyAttachGYSX.FormDestroy(Sender: TObject);
begin
  frmZdyAttachGYSX:=nil;
end;

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

procedure TfrmZdyAttachGYSX.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('PBGYS1',Tv1,'供应商管理');
  WriteCxGrid('PBGYS2',Tv2,'供应商管理');
  WriteCxGrid('PBGYS3',Tv3,'供应商管理');
  Close;
end;
procedure TfrmZdyAttachGYSX.TBDelClick(Sender: TObject);
begin
 if CDS_HZ.IsEmpty then exit;
  if CDS_HZ.Locate('ssel',true,[])=false then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  CDS_HZ.DisableControls;
  with CDS_HZ do
  begin
    while not eof do
    begin
      iF fieldbyname('ssel').AsBoolean=true then
      begin
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('delete KH_Main where KHMainID='''+Trim(CDS_HZ.fieldbyname('KHMainID').AsString)+'''');
          sql.Add('delete from KH_Sub where KHMainID='''+Trim(CDS_HZ.fieldbyname('KHMainID').AsString)+'''');
          sql.Add('delete from KH_Sub_Bank where KHMainID='''+Trim(CDS_HZ.fieldbyname('KHMainID').AsString)+'''');
          ExecSQL;
        end;
        CDS_HZ.Delete;
      end
      else
      next;
    end;
  end;
  CDS_HZ.EnableControls;
end;

procedure TfrmZdyAttachGYSX.FormShow(Sender: TObject);
begin
//  ReadCxGrid('供应商资料',Tv2,'客户自定义');
  IF trim(fDEFstr5)<>'' then
  begin
  end;
  setstatus();
//  InitGrid();
end;

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

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


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

procedure TfrmZdyAttachGYSX.TBeditClick(Sender: TObject);
var
 i:Integer;
 FieldName:String;
begin
  if CDS_HZ.IsEmpty then exit;
  try
    frmPBGYSInPut:=TfrmPBGYSInPut.Create(Application);
    with frmPBGYSInPut do
    begin
      FKHMainid:=Trim(Self.CDS_HZ.fieldbyname('KHMainid').AsString);
      LabGYSName.caption:=Trim(Self.CDS_HZ.fieldbyname('GYSName').AsString);
      if ShowModal=1 then
      begin
        TBRafresh.Click;
      end;
    end;
  finally
    frmPBGYSInPut.Free;
  end;
end;

procedure TfrmZdyAttachGYSX.TaddClick(Sender: TObject);
begin
  try
    frmPBGYSInPutMain:=TfrmPBGYSInPutMain.Create(Application);
    with frmPBGYSInPutMain do
    begin
      FKHMainid:='';
      if ShowModal=1 then
      begin
        TBRafresh.Click;
      end;
    end;
  finally
    frmPBGYSInPutMain.Free;
  end;
end;

procedure TfrmZdyAttachGYSX.FormCreate(Sender: TObject);
begin
  canshu1:=trim(DParameters1);
  FGYSType:=trim(DParameters2);
  ReadCxGrid('GYS1',Tv1,'客户自定义');
  ReadCxGrid('GYS2',Tv2,'客户自定义');
  ReadCxGrid('GYS3',Tv3,'客户自定义');
  
end;

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

procedure TfrmZdyAttachGYSX.ToolButton4Click(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('KHMainid').AsString);
      fType:='供应商资料';
      if ShowModal=1 then
      begin

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

procedure TfrmZdyAttachGYSX.TV1MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if CDS_HZ.IsEmpty then exit;
  with ADOQueryMain2 do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from KH_Sub where KHMainid='''+Trim(CDS_HZ.fieldbyname('KHMainid').asstring)+'''');
    sql.Add(' order by XHNO');
    Open;
  end;
  SCreateCDS20(ADOQueryMain2,CDS_HZ2);
  SInitCDSData20(ADOQueryMain2,CDS_HZ2);
end;

procedure TfrmZdyAttachGYSX.TV1CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  TCK.Click;
end;

procedure TfrmZdyAttachGYSX.TchkClick(Sender: TObject);
begin
  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_Main SET status=1,chker='''+trim(DName)+''',chkTime=getdate() ');
            sql.Add('where KHMainid='+quotedstr(trim(CDS_HZ.fieldbyname('KHMainid').AsString)));
            execsql;
          end;
          delete;
        end
        else
        next;
      end;
      First;
      EnableControls;
    end;
    application.MessageBox('审核成功！','提示信息');
  except
    CDS_HZ.EnableControls;
    application.MessageBox('审核失败！','提示信息',0);
  end;
end;

procedure TfrmZdyAttachGYSX.TNochkClick(Sender: TObject);
begin
  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_Main SET status=0,chker='''',chkTime=null');
           sql.Add('where KHMainid='+quotedstr(trim(CDS_HZ.fieldbyname('KHMainid').AsString)));
           execsql;
         end;
         delete;
       end
       else
       next;
     end;
     First;
     EnableControls;
   end;
    application.MessageBox('撤销审核成功！','提示信息');
  except
    CDS_HZ.EnableControls;
    application.MessageBox('撤销审核失败！','提示信息',0);
  end;
end;

procedure TfrmZdyAttachGYSX.TV2MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if CDS_HZ2.IsEmpty then exit;
  with ADOQueryMain3 do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from KH_Sub_Bank where KHSubid='''+Trim(CDS_HZ2.fieldbyname('KHSubid').asstring)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryMain3,CDS_HZ3);
  SInitCDSData20(ADOQueryMain3,CDS_HZ3);
end;

end.


