unit U_ZdyAttachGYS;

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

type
  TfrmZdyAttachGYS = 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;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    ToolButton3: TToolButton;
    v2Column6: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column7: TcxGridDBColumn;
    v2Column9: TcxGridDBColumn;
    Label1: TLabel;
    ZdyCode: TEdit;
    ToolButton4: TToolButton;
    v2Column8: TcxGridDBColumn;
    Label2: TLabel;
    v2Column10: TcxGridDBColumn;
    v2Column11: TcxGridDBColumn;
    v2Column12: TcxGridDBColumn;
    v2Column13: TcxGridDBColumn;
    DEFStr7: TEdit;
    Label4: TLabel;
    v2Column14: TcxGridDBColumn;
    defstr5: TEdit;
    Panel2: TPanel;
    ToolBar2: TToolBar;
    ToolButton7: TToolButton;
    ToolButton8: TToolButton;
    ToolButton9: TToolButton;
    cxGrid1: TcxGrid;
    TV3: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    TV3Column1: TcxGridDBColumn;
    TV3Column2: TcxGridDBColumn;
    DSsub: TDataSource;
    CDSsub: TClientDataSet;
    ADOQuerysub: TADOQuery;
    ToolButton5: TToolButton;
    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 ToolButton8Click(Sender: TObject);
    procedure ToolButton9Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure cxGridDBColumn1PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ToolButton5Click(Sender: TObject);
    procedure Tv2CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
  private
    { Private declarations }
    procedure InitGrid();
    procedure Initsub();

  public
    fDEFstr5:string;
  end;

var
  frmZdyAttachGYS: TfrmZdyAttachGYS;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp,U_ZdyAttInputGYS, U_ZdyAttachCP10_help;

{$R *.dfm}
procedure TfrmZdyAttachGYS.Initsub();
begin
  if cdssub.Active then CDSsub.EmptyDataSet;
  IF CDS_hz.IsEmpty then exit;
  try
    with ADOQuerysub do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select * from KH_Zdy_CPCB where ATID='''+trim(cds_hz.fieldbyname('ATID').AsString)+''' and CBType=''GYSYSCP'' ');
      Open;
    end;
    SCreateCDS20(ADOQuerysub,CDSsub);
    SInitCDSData20(ADOQuerysub,CDSsub);
    tv3.OptionsData.Editing:=false;
  finally
  end;
end;



procedure TfrmZdyAttachGYS.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      SQL.Clear;
      sql.Add(' select * from KH_Zdy_Attachment where Type=''GYS'' ');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    SInitCDSData20(ADOQueryMain,CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
  end;
  ToolButton2.Click;
  Initsub();
end;


procedure TfrmZdyAttachGYS.FormDestroy(Sender: TObject);
begin
  frmZdyAttachGYS:=nil;
end;

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

procedure TfrmZdyAttachGYS.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('供应商资料',Tv2,'客户自定义');
  Close;
end;
procedure TfrmZdyAttachGYS.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 TfrmZdyAttachGYS.FormShow(Sender: TObject);
begin
  ReadCxGrid('供应商资料',Tv2,'客户自定义');
  IF trim(fDEFstr5)<>'' then
  begin
    DEFstr5.Text:=trim(fDEFstr5);
    DEFstr5.Enabled:=false;
 end;
  InitGrid();
end;

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

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

procedure TfrmZdyAttachGYS.ZdyNameChange(Sender: TObject);
begin
  ToolButton2.Click;
end;


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

procedure TfrmZdyAttachGYS.ToolButton4Click(Sender: TObject);
begin
  try
    frmZdyAttInputGYS:=TfrmZdyAttInputGYS.Create(Application);
    with frmZdyAttInputGYS do
    begin
      FCYID:=Trim(Self.CDS_HZ.fieldbyname('ATID').AsString);
      fDEFstr5:=self.fDEFstr5;
      if ShowModal=1 then
      begin
        TBRafresh.Click;
      end;
    end;
  finally
    frmZdyAttInputGYS.Free;
  end;
end;

procedure TfrmZdyAttachGYS.ToolButton3Click(Sender: TObject);
begin
  try
    frmZdyAttInputGYS:=TfrmZdyAttInputGYS.Create(Application);
    with frmZdyAttInputGYS do
    begin
      FCYID:='';
      fDEFstr5:=self.fDEFstr5;
      if ShowModal=1 then
      begin
        TBRafresh.Click;
      end;
    end;
  finally
    frmZdyAttInputGYS.Free;
  end;
end;

procedure TfrmZdyAttachGYS.ToolButton8Click(Sender: TObject);
begin
  IF cdssub.IsEmpty then exit;
  tv3.OptionsData.Editing:=true;
end;

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

procedure TfrmZdyAttachGYS.ToolButton7Click(Sender: TObject);
begin
  tv3.OptionsData.Editing:=true;
  CDSsub.Append;
  cdsSub.Post;
end;

procedure TfrmZdyAttachGYS.cxGridDBColumn1PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  frmZdyAttachCP10_help:=TfrmZdyAttachCP10_help.create(self);
  with frmZdyAttachCP10_help do
  begin
    if showmodal=1 then
    begin
      cdssub.Edit;
      cdssub.FieldByName('zdyCode').Value:=trim(cds_hz.fieldbyname('zdyCode').AsString);
      cdssub.FieldByName('zdyName').Value:=trim(cds_hz.fieldbyname('zdyName').AsString);
      cdssub.FieldByName('defstr1').Value:=trim(cds_hz.fieldbyname('defstr1').AsString);
      cdssub.FieldByName('defstr2').Value:=trim(cds_hz.fieldbyname('defstr2').AsString);
      cdssub.Post;
    end;
    free;
  end;
  tv3.Controller.EditingController.ShowEdit();
end;

procedure TfrmZdyAttachGYS.ToolButton5Click(Sender: TObject);
var
  maxId:string;
begin
  if CDSsub.IsEmpty then exit;
  try
    with CDSsub do
    begin
      first;
      while not eof do
      begin
        IF trim(fieldbyname('CBID').AsString)='' then
        begin
          if GetLSNo(ADOQueryCmd,maxId,'CY','KH_Zdy_CPCB',4,1)=False then
          begin
            Application.MessageBox('取最大号失败！','提示',0);
            Exit;
          end;
        end
        else
          maxId:=trim(fieldbyname('CBID').AsString);
        with  ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.Add('select * from KH_Zdy_CPCB where CBID='''+Trim(maxId)+'''');
          open;
          if  ADOQueryCmd.IsEmpty then
          begin
            Append;
            FieldByName('Filler').Value:=Trim(DName);
          end
          else
          begin
            Edit;
            FieldByName('Editer').Value:=Trim(DName);
            FieldByName('Edittime').Value:=now();
          end;
           FieldByName('CBID').Value:=Trim(maxId);
          FieldByName('ATID').Value:=Trim(CDS_HZ.fieldbyname('ATID').AsString);
          FieldByName('ZdyCode').Value:=Trim(cdssub.fieldbyname('ZdyCode').AsString);
          FieldByName('ZdyName').Value:=Trim(cdssub.fieldbyname('ZdyName').AsString);
          FieldByName('defstr1').Value:=Trim(cdssub.fieldbyname('defstr1').AsString);
          FieldByName('defstr2').Value:=Trim(cdssub.fieldbyname('defstr2').AsString);
          FieldByName('note').Value:=Trim(cdssub.fieldbyname('note').AsString);
          FieldByName('CBType').Value:='GYSYSCP';
          post;
        end;
        next;
      end;
    end;
    application.MessageBox('数据保存成功！','提示信息');
    tv3.OptionsData.Editing:=false;
  except
    application.MessageBox('数据保存失败！','提示信息',MB_ICONERROR);
  end;
end;

procedure TfrmZdyAttachGYS.Tv2CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  Initsub();
end;

end.


