unit U_YGYPZZChkList_YGFJSH;

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, cxPC, ActnMan, ActnColorMaps,ShellAPI,IniFiles,
  IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdFTP;

type
  TfrmYGYPZZChkList_YGFJSH = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    TBView: TToolButton;
    Panel1: TPanel;
    PopupMenu1: TPopupMenu;
    N2: TMenuItem;
    N1: TMenuItem;
    TCXTJ: TToolButton;
    BegDate: TDateTimePicker;
    Label3: TLabel;
    EndDate: TDateTimePicker;
    DataSource2: TDataSource;
    order_Sub: TClientDataSet;
    Label2: TLabel;
    TTiJiao: TToolButton;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    ADOQuery1: TADOQuery;
    Label4: TLabel;
    SQType: TEdit;
    cxTabControl1: TcxTabControl;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    V2Chker: TcxGridDBColumn;
    V2Chktime: TcxGridDBColumn;
    V2Chkstatus: TcxGridDBColumn;
    V2chkNote: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Label5: TLabel;
    SQName: TEdit;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1filltime: TcxGridDBColumn;
    v1SQName: TcxGridDBColumn;
    v1SQBegDate: TcxGridDBColumn;
    v1SQEndDate: TcxGridDBColumn;
    v1SQDay: TcxGridDBColumn;
    v1TaiTou: TcxGridDBColumn;
    v1SQType: TcxGridDBColumn;
    v1SJEndDate: TcxGridDBColumn;
    v1SQNote: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1Ssel: TcxGridDBColumn;
    v1SQXH: TcxGridDBColumn;
    cxStyle2: TcxStyle;
    Label1: TLabel;
    Label6: TLabel;
    TTPCK: TToolButton;
    IdFTP1: TIdFTP;
    ODPat: TOpenDialog;
    SaveDialog1: TSaveDialog;
    Label7: TLabel;
    ZWName: TEdit;
    v1ZWName: TcxGridDBColumn;
    v1SQStatus: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure TBViewClick(Sender: TObject);
    procedure CustomerNoNameChange(Sender: TObject);
    procedure TCXTJClick(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure TTiJiaoClick(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure CLNameChange(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure TTPCKClick(Sender: TObject);
    procedure ZWNameKeyPress(Sender: TObject; var Key: Char);
    procedure SQNameKeyPress(Sender: TObject; var Key: Char);
  private
    canshu1:string;
    DQdate:TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    procedure setstatus();
    procedure ReadINIFile();
    { Private declarations }
  public
    FFInt,FCloth:Integer;
    { Public declarations }
  end;

var
  frmYGYPZZChkList_YGFJSH: TfrmYGYPZZChkList_YGFJSH;

implementation
uses
  U_DataLink,U_RTFun,U_ZDYHelp, U_YGYPInPut,
  U_ZDYHelpSel,U_YGYPInPut_YGFJDJ;

{$R *.dfm}
procedure TfrmYGYPZZChkList_YGFJSH.ReadINIFile();
var
  programIni:Tinifile;  //配置文件名
  FileName:string;
begin
  FileName:=ExtractFilePath(Paramstr(0))+'SYSTEMSET.INI';
  programIni:=Tinifile.create(FileName);
  server:=programIni.ReadString('SERVER','服务器地址','127.0.0.1');
  programIni.Free;
end;


procedure TfrmYGYPZZChkList_YGFJSH.setstatus();
begin
  TTiJiao.Visible:=False;
  case cxTabControl1.TabIndex of
    0:begin
        TTiJiao.Visible:=True;
      end;
    1:begin
      end;
    2:begin

      end;
  end;
end;

procedure TfrmYGYPZZChkList_YGFJSH.FormDestroy(Sender: TObject);
begin
  frmYGYPZZChkList_YGFJSH:=nil;
end;

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

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

procedure TfrmYGYPZZChkList_YGFJSH.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('公司事务审核列表',Tv1,'OA管理');
end;

procedure TfrmYGYPZZChkList_YGFJSH.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select A.*,B.* ');
      sql.Add(' from OA_YG_CLGLSW A ');
      sql.add(' inner join OA_Chk B on B.Mainid=A.SWID ');
      sql.Add(' where B.Chker='''+trim(DName)+''' ');
      if cxTabControl1.tabindex<>0 then
      begin
        sql.add(' and A.filltime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.Date))+'''');
        sql.Add(' and A.filltime<='''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.Date+1))+'''');
      end;
      if cxTabControl1.TabIndex=0 then
      begin
        sql.Add(' and isnull(B.Chkstatus,'''')='''' ');
      end else
      if cxTabControl1.TabIndex=1 then
      begin
        sql.add(' and isnull(B.Chkstatus,'''')=''申请通过''');
      end;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
    with ADOQueryCmd do
    begin
      close;
      sql.clear;
      sql.add('select A.* from OA_Chk A ');
      sql.add('where A.Mainid='''+trim(Order_Main.fieldbyname('SWID').AsString)+''' ');
      sql.Add('and isnull(OAType,'''')=''事务管理''');
      open;
    end;
    SCreateCDS20(ADOQueryCmd,Order_Sub);
    SInitCDSData20(ADOQueryCmd,Order_Sub);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmYGYPZZChkList_YGFJSH.InitForm();
begin
  EndDate.Date:=SGetServerDate(ADOQueryTemp);
  BegDate.Date:=EndDate.Date-7;
  ReadCxGrid('公司事务审核列表',Tv1,'OA管理');
  InitGrid();
end;

procedure TfrmYGYPZZChkList_YGFJSH.TBFindClick(Sender: TObject);
begin
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;

function TfrmYGYPZZChkList_YGFJSH.DelData():Boolean;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Delete OA_YG_DangAn where YGId='''+Trim(Order_Main.fieldbyname('YGId').AsString)+'''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Delete OA_YG_DangAn_Other  where YGId='''+Trim(Order_Main.fieldbyname('YGId').AsString)+'''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Delete TP_File  where WBId='''+Trim(Order_Main.fieldbyname('YGId').AsString)+'''');
      sql.Add(' and TFType=''YG'' ');
      ExecSQL;
    end;

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

procedure TfrmYGYPZZChkList_YGFJSH.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  TcxGridToExcel('员工事务列表',cxGrid1);
end;

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

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

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

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

procedure TfrmYGYPZZChkList_YGFJSH.TBViewClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then exit;
  frmYGYPInPut_YGFJDJ:=TfrmYGYPInPut_YGFJDJ.create(self);
  with frmYGYPInPut_YGFJDJ do
  begin
    ToolBar1.Visible:=False;
    FCLID:=trim(Order_Main.fieldbyname('SWID').AsString);
    if showModal=1 then
    begin

    end;
    free;
  end;
end;

procedure TfrmYGYPZZChkList_YGFJSH.CustomerNoNameChange(Sender: TObject);
begin
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;

procedure TfrmYGYPZZChkList_YGFJSH.TCXTJClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  toolbar1.SetFocus;
  if Application.MessageBox('确定要执行撤销操作吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    if Order_Main.FieldByName('SQStatus').AsString='已回归' then
    begin
      application.MessageBox('已回归不能审核','提示');
      exit;
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('Update OA_YG_CLGLSW Set SQStatus=''不通过''');
      sql.Add(' where SWID='''+trim(Order_Main.fieldbyname('SWID').AsString)+'''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('delete from OA_Chk ');
      sql.Add(' where Mainid='''+Order_Main.fieldbyname('SWID').AsString+'''');
      sql.Add(' and isnull(OAType,'''')=''事务管理'' ');
      ExecSQL;
    end;
    initGrid();
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!','提示',0);
  end;
end;

procedure TfrmYGYPZZChkList_YGFJSH.Tv1CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  TBView.Click;
end;

procedure TfrmYGYPZZChkList_YGFJSH.TTiJiaoClick(Sender: TObject);
var maxno: string;
begin
  if Order_Main.IsEmpty then exit;
  toolbar1.SetFocus;
  if order_Main.Locate('Ssel',true,[])=False then
  begin
    application.MessageBox('没有选择数据','提示');
    exit;
  end;
  try
    with Order_Main do
    begin
      first;
      while not eof do
      begin
        if fieldbyname('Ssel').AsBoolean=True then
        begin
          if trim(DParameters1)='高权限' then
          begin
            with ADOQueryCmd do
            begin
              Close;
              SQL.Clear;
              sql.Add('Update OA_Chk Set ');
              sql.Add('ChkTime=getdate(),Chkstatus=''申请通过'' ');
              sql.Add('where Mainid='''+trim(Order_Main.fieldbyname('SWID').AsString)+''' ');
              sql.add('and OAType=''事务管理''');
              ExecSQL;
            end;
          end
          else
          begin
            with ADOQueryCmd do
            begin
              Close;
              SQL.Clear;
              sql.Add('Update OA_Chk Set ');
              sql.Add(' ChkTime=getdate(),Chkstatus=''申请通过'' ');
              sql.Add(' where CKID='''+trim(Order_Main.fieldbyname('CKID').AsString)+''' ');
              ExecSQL;
            end;
          end;
          with ADOQueryCmd do
          begin
            close;
            sql.clear;
            sql.add('select A.* from OA_Chk A ');
            sql.add('where A.Mainid='''+trim(Order_Main.fieldbyname('SWID').AsString)+''' ');
            sql.add('and isnull(A.Chkstatus,'''')<>''申请通过'' ');
            sql.add('and isnull(A.OAType,'''')=''事务管理''');
            open;
          end;
          if ADOQueryCmd.IsEmpty then
          begin
            with ADOQueryMain do
            begin
              close;
              sql.Clear;
              sql.add('update OA_YG_CLGLSW set ');
              sql.add('SQStatus=''申请通过'' ');
              sql.Add(' where SWID='''+trim(Order_Main.fieldbyname('SWID').AsString)+'''');
              execsql;
            end;
          end;
        end;
        next;
      end;
    end;
    application.MessageBox('审核成功','提示');
    initGrid();
  except;
    application.MessageBox('审核失败','提示');
    exit;
  end;
end;

procedure TfrmYGYPZZChkList_YGFJSH.N2Click(Sender: TObject);
begin
  SelOKNo(Order_Main,True);
end;

procedure TfrmYGYPZZChkList_YGFJSH.N1Click(Sender: TObject);
begin
  SelOKNo(Order_Main,False);
end;

procedure TfrmYGYPZZChkList_YGFJSH.CLNameChange(Sender: TObject);
begin
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;

procedure TfrmYGYPZZChkList_YGFJSH.cxTabControl1Change(Sender: TObject);
begin
  setstatus();
  initGrid();
end;

procedure TfrmYGYPZZChkList_YGFJSH.Tv1CellClick(
  Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  with ADOQueryCmd do
  begin
    close;
    sql.clear;
    sql.add('select A.* from OA_Chk A ');
    sql.add('where A.Mainid='''+trim(Order_Main.fieldbyname('SWID').AsString)+''' ');
    sql.Add('and isnull(OAType,'''')=''事务管理''');
    open;
  end;
  SCreateCDS20(ADOQueryCmd,Order_Sub);
  SInitCDSData20(ADOQueryCmd,Order_Sub);
  Label1.Caption:=trim(Order_Main.fieldbyname('SQNote').AsString);
end;

procedure TfrmYGYPZZChkList_YGFJSH.TTPCKClick(Sender: TObject);
var
  fHandle:THandle;
  FInt:Integer;
  FFName,FPath:String;
begin
  with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from XD_File where CYNO='''+Trim(Order_Main.fieldbyname('SWID').AsString)+'''');
    Open;
    if IsEmpty then
    begin
      Application.MessageBox('凭证图片未上传！','提示',0);
      Exit;
    end;
  end;
  try
    ReadINIFile();
    server:=ReadINIFileStr('SYSTEMSET.INI','SERVER','服务器地址','127.0.0.1');
    if Length(server)<6 then
    begin
      server:='127.0.0.1';
    end;
    IdFTP1.Host :=server;//PicSvr;
    IdFTP1.Username := 'three';
    IdFTP1.Password := '641010';
    IdFTP1.Connect();
  except
    IdFTP1.Quit;
    Application.MessageBox('无法连接到文件服务器，请检查！', '提示', MB_ICONWARNING);
    Exit;
  end;
  FPath:='D:\Right1209\';
  if not DirectoryExists(ExtractFileDir(FPath)) then
    CreateDir(ExtractFileDir(FPath));
  FFName:=Trim(ADOQueryTemp.fieldbyname('FileName').AsString);
  FFName:=FPath+FFName;
  if FileExists(FFName) then
  begin
    FInt:=1;
  end;
  if FInt<>1 then
    IdFTP1.Get('PZ\'+Trim(ADOQueryTemp.fieldbyname('FileName').AsString),
                   FPath+Trim(ADOQueryTemp.fieldbyname('FileName').AsString));
  if IdFTP1.Connected then IdFTP1.Quit;
  ShellExecute(Handle, 'open',PChar(FPath+Trim(ADOQueryTemp.fieldbyname('FileName').AsString)),'', '', SW_SHOWNORMAL);

end;

procedure TfrmYGYPZZChkList_YGFJSH.ZWNameKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key=#13 then
  begin
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select * ');
      sql.Add(' from OA_YG_CLGLSW ');
      sql.Add(' where 1=1');
      sql.Add(' and ZWName='''+trim(ZWName.Text)+''' ');
      if cxTabControl1.TabIndex=0 then
      begin
        sql.Add(' and isnull(SQStatus,'''')<>''申请通过'' ');
      end else
      if cxTabControl1.TabIndex=1 then
      begin
        sql.add(' and isnull(SQStatus,'''')=''申请通过''');
      end;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
    with ADOQueryCmd do
    begin
      close;
      sql.clear;
      sql.add('select A.* from OA_Chk A ');
      sql.add('where A.Mainid='''+trim(Order_Main.fieldbyname('SWID').AsString)+''' ');
      sql.Add('and isnull(OAType,'''')=''事务管理''');
      open;
    end;
    SCreateCDS20(ADOQueryCmd,Order_Sub);
    SInitCDSData20(ADOQueryCmd,Order_Sub);
  end;
end;

procedure TfrmYGYPZZChkList_YGFJSH.SQNameKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key=#13 then
  begin
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select * ');
      sql.Add(' from OA_YG_CLGLSW ');
      sql.Add(' where 1=1');
      sql.Add(' and SQName='''+trim(SQName.Text)+''' ');
      if cxTabControl1.TabIndex=0 then
      begin
        sql.Add(' and isnull(SQStatus,'''')<>''申请通过'' ');
      end else
      if cxTabControl1.TabIndex=1 then
      begin
        sql.add(' and isnull(SQStatus,'''')=''申请通过''');
      end;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
    with ADOQueryCmd do
    begin
      close;
      sql.clear;
      sql.add('select A.* from OA_Chk A ');
      sql.add('where A.Mainid='''+trim(Order_Main.fieldbyname('SWID').AsString)+''' ');
      sql.Add('and isnull(OAType,'''')=''事务管理''');
      open;
    end;
    SCreateCDS20(ADOQueryCmd,Order_Sub);
    SInitCDSData20(ADOQueryCmd,Order_Sub);
  end;
end;

end.
