unit U_YGXQAPList;

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;

type
  TfrmYGXQAPList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    TBView: TToolButton;
    Panel1: TPanel;
    Label9: TLabel;
    Dept: TEdit;
    v1Column2: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    Label34: TLabel;
    v1Column7: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    GangWei: TEdit;
    v1Column4: TcxGridDBColumn;
    TBSChk: TToolButton;
    v1Column5: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N2: TMenuItem;
    N1: TMenuItem;
    TBSCancel: TToolButton;
    v1Column12: TcxGridDBColumn;
    BegDate: TDateTimePicker;
    Label3: TLabel;
    EndDate: TDateTimePicker;
    Label4: TLabel;
    cxTabControl1: TcxTabControl;
    v1Column3: TcxGridDBColumn;
    ToolButton1: TToolButton;
    v1Column8: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column14: 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 TBSChkClick(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure TBSCancelClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
  private
    canshu1:string;
    DQdate:TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    { Private declarations }
  public
    FFInt,FCloth:Integer;

    { Public declarations }
  end;

var
  frmYGXQAPList: TfrmYGXQAPList;

implementation
uses
  U_DataLink,U_RTFun,U_ZDYHelp, U_YGXQInPut;

{$R *.dfm}

procedure TfrmYGXQAPList.FormDestroy(Sender: TObject);
begin
  frmYGXQAPList:=nil;
end;

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

procedure TfrmYGXQAPList.FormCreate(Sender: TObject);
begin
  canshu1:=Trim(DParameters1);
  cxgrid1.Align:=alClient;
end;

procedure TfrmYGXQAPList.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('用人申请列表AP',Tv1,'OA管理');
end;

procedure TfrmYGXQAPList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select A.* ');
      sql.Add(' from OA_YG_XQ A  ');
      sql.Add(' where OAType=''用人申请'' ');
      if cxTabControl1.TabIndex<>0 then
      begin
        sql.Add(' and SQDate>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.Date))+'''');
        sql.Add(' and SQDate<='''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.Date+1))+'''');
      end;
      sql.Add(' and isnull(ChkStatus,'''')=''审批通过'' ');
      if cxTabControl1.TabIndex=0 then
      begin
        sql.Add(' and isnull(APPerson,'''')='''' ');
      end else
      if cxTabControl1.TabIndex=1 then
      begin
        sql.Add(' and isnull(APPerson,'''')<>'''' ');
      end;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;


procedure TfrmYGXQAPList.InitForm();
begin
  EndDate.Date:=SGetServerDate(ADOQueryTemp);
  BegDate.Date:=EndDate.Date-7;
  ReadCxGrid('用人申请列表AP',Tv1,'OA管理');
  InitGrid();
end;

procedure TfrmYGXQAPList.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;

function TfrmYGXQAPList.DelData():Boolean;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Delete OA_YG_XQ  where YXId='''+Trim(Order_Main.fieldbyname('YXId').AsString)+'''');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=False;
    Application.MessageBox('数据删除异常！','提示',0);
  end;
end;

procedure TfrmYGXQAPList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  TcxGridToExcel('用人需求安排列表',cxGrid1);
end;

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

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

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

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

procedure TfrmYGXQAPList.TBViewClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  try
    frmYGXQInPut:=TfrmYGXQInPut.Create(Application);
    with frmYGXQInPut do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('YXId').AsString);
      TBSave.Visible:=False;
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmYGXQInPut.Free;
  end;
end;

procedure TfrmYGXQAPList.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 TfrmYGXQAPList.TBSChkClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  if cxTabControl1.TabIndex<>0 then Exit;
  if Order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要执行此操作吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    Order_Main.DisableControls;
    with Order_Main do
    begin
      First;
      while Order_Main.Locate('SSel',True,[]) do
      begin
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('Update OA_YG_XQ Set ');
          sql.Add(' APPerson='''+Trim(DName)+''',APTime=getdate(),APStatus=''已安排'' ');
          sql.Add(' where YXID='''+Order_Main.fieldbyname('YXID').AsString+'''');
          ExecSQL;
        end;
        Delete
      end;
    end;
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    Exit;
  except
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!','提示',0);
  end;
end;

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

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

procedure TfrmYGXQAPList.TBSCancelClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  if cxTabControl1.TabIndex<>1 then Exit;
  if Order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  {Order_Main.DisableControls;
  with Order_Main do
  begin
    First;
    while not Eof do
    begin
      if Order_Main.FieldByName('SSel').AsBoolean=True then
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add(' select * from OA_YG_XQ where YXID='''+Trim(Order_Main.fieldbyname('YXID').AsString)+'''');
          sql.Add(' and isnull(APStatus,'''')=''已安排'' ');
          Open;
        end;
        if ADOQueryTemp.IsEmpty=False then
        begin
          Order_Main.EnableControls;
          Application.MessageBox('已安排，不能撤销!','提示',0);
          Exit;
        end;
      end;
      Next;
    end;
  end;

  Order_Main.EnableControls;}
  if Application.MessageBox('确定要执行撤销操作吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    Order_Main.DisableControls;
    with Order_Main do
    begin
      First;
      while Order_Main.Locate('SSel',True,[]) do
      begin
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('Update OA_YG_XQ Set ');
          sql.Add(' APPerson=Null,APTime=Null,APStatus=Null');
          sql.Add(' where YXID='''+Order_Main.fieldbyname('YXID').AsString+'''');
          ExecSQL;
        end;
        Delete;
      end;
    end;
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    Exit;
  except
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!','提示',0);
  end;
end;

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

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

end.
