unit U_YGSel;

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, cxTL, cxMaskEdit, cxInplaceContainer, cxDBTL, cxTLData,
  cxLookAndFeels, cxLookAndFeelPainters, cxTLdxBarBuiltInMenu, dxBarBuiltInMenu,
  cxNavigator, U_BaseHelp, dxSkinsCore, dxSkinsDefaultPainters, dxDateRanges,
  RM_e_Graphic, RM_e_Jpeg, RM_E_llPDF, cxGridBandedTableView,
  cxGridDBBandedTableView, U_BaseList, System.ImageList, Vcl.ImgList;

type
  TfrmYGSel = class(TfrmBaseHelp)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Order_Main: TClientDataSet;
    Panel1: TPanel;
    PopupMenu1: TPopupMenu;
    N2: TMenuItem;
    N1: TMenuItem;
    Label1: TLabel;
    YGName: TEdit;
    Label2: TLabel;
    YGSex: TComboBox;
    Label5: TLabel;
    GangWei: TEdit;
    cxDBTreeList1: TcxDBTreeList;
    cxDBTreeList1cxDBTreeListColumn2: TcxDBTreeListColumn;
    DataSource2: TDataSource;
    CDS_Tree: TClientDataSet;
    ADOQueryTree: TADOQuery;
    cxTabControl1: TcxTabControl;
    ADOQuery1: TADOQuery;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    Label4: TLabel;
    Dept: TEdit;
    Label7: TLabel;
    YGNo: TEdit;
    RMDB_1: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    RM1: TRMGridReport;
    ADOQueryPrint: TADOQuery;
    RMJPEGExport1: TRMJPEGExport;
    RMllPDFExport1: TRMllPDFExport;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBBandedTableView;
    cxGridDBBandedColumn35: TcxGridDBBandedColumn;
    cxGridDBBandedColumn42: TcxGridDBBandedColumn;
    cxGridDBBandedColumn51: TcxGridDBBandedColumn;
    cxGridDBBandedColumn52: TcxGridDBBandedColumn;
    cxGridDBBandedColumn56: TcxGridDBBandedColumn;
    cxGridDBBandedColumn57: TcxGridDBBandedColumn;
    cxGridDBBandedColumn59: TcxGridDBBandedColumn;
    cxGridDBBandedColumn61: TcxGridDBBandedColumn;
    cxGridDBBandedColumn63: TcxGridDBBandedColumn;
    cxGridDBBandedColumn77: TcxGridDBBandedColumn;
    cxGridDBBandedColumn79: TcxGridDBBandedColumn;
    cxGridDBBandedColumn81: TcxGridDBBandedColumn;
    cxGridDBBandedColumn87: TcxGridDBBandedColumn;
    cxGridDBBandedColumn91: TcxGridDBBandedColumn;
    V1Column6: TcxGridDBBandedColumn;
    V1Column4: TcxGridDBBandedColumn;
    V1Column5: TcxGridDBBandedColumn;
    V1Column7: TcxGridDBBandedColumn;
    V1Column3: TcxGridDBBandedColumn;
    V1Column8: TcxGridDBBandedColumn;
    V1Column9: TcxGridDBBandedColumn;
    V1Column10: TcxGridDBBandedColumn;
    V1Column11: TcxGridDBBandedColumn;
    cxGridDBBandedColumn3: TcxGridDBBandedColumn;
    cxGridDBBandedColumn4: TcxGridDBBandedColumn;
    cxGridDBBandedColumn5: TcxGridDBBandedColumn;
    TV1Column1: TcxGridDBBandedColumn;
    TV1Column2: TcxGridDBBandedColumn;
    TV1Column3: TcxGridDBBandedColumn;
    cxgrdbndtblvw1Column4: TcxGridDBBandedColumn;
    cxgrdbndtblvw1Column5: TcxGridDBBandedColumn;
    cxgrdbndtblvw1Column6: TcxGridDBBandedColumn;
    TV1Column7: TcxGridDBBandedColumn;
    TV1Column8: TcxGridDBBandedColumn;
    TV1Column9: TcxGridDBBandedColumn;
    TV1Column10: TcxGridDBBandedColumn;
    TV1Column11: TcxGridDBBandedColumn;
    TV1Column12: TcxGridDBBandedColumn;
    TV1Column13: TcxGridDBBandedColumn;
    TV1Column14: TcxGridDBBandedColumn;
    TV1Column15: TcxGridDBBandedColumn;
    TV1Column16: TcxGridDBBandedColumn;
    TV1Column17: TcxGridDBBandedColumn;
    TV1Column18: TcxGridDBBandedColumn;
    TV1Column19: TcxGridDBBandedColumn;
    cxGridLevel2: TcxGridLevel;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure CustomerNoNameChange(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure YGNameChange(Sender: TObject);
    procedure cxDBTreeList1DblClick(Sender: TObject);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    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;
    procedure InitTree();
    procedure GLAge();
    procedure SetStatus();
    procedure PrintIC(MICK: string);
    { Private declarations }
  public
    FFInt, FCloth: Integer;

    { Public declarations }
  end;

var
  frmYGSel: TfrmYGSel;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp;

{$R *.dfm}
procedure TfrmYGSel.PrintIC(MICK: string);
var
  fPrintFile, LBName: string;
begin
  if Order_Main.IsEmpty then
    Exit;
  LBName := Trim(Order_Main.fieldbyname('YGType').AsString) + MICK;

  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + LBName + '.rmf';
  ExportFtErpFile(LBName + '.rmf', ADOQueryTemp);
  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*');
    sql.Add(',FilesOther=(select top 1 FilesOther from TP_File    where   TFType=''员工档案'' and	WBID=A.YGID)  ');
    sql.Add(' from OA_YG_DangAn A       ');
    sql.Add('where    A.YGId=''' + Trim(Order_Main.fieldbyname('YGId').AsString) + '''');

    Open;
  end;
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
  end;
end;

procedure TfrmYGSel.SetStatus();
begin

end;

procedure TfrmYGSel.FormDestroy(Sender: TObject);
begin
  inherited;
  frmYGSel := nil;
end;

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

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

procedure TfrmYGSel.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxBandedGrid('员工档案列表2', Tv1, 'OA管理');
end;

procedure TfrmYGSel.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add('select * from OA_YG_DangAn where Dept in(''国际一部'',''国内一部'')');
//      sql.Add(' exec P_View_YGList @DPID=' + quotedstr(Trim(CDS_Tree.fieldbyname('DPID').AsString)));
//      sql.Add(',@LiZhiFlag=' + quotedstr(inttostr(cxTabControl1.TabIndex)));
//      showmessage(sql.text);
      Open;
    end;
    SCreateCDS(ADOQueryMain, Order_Main);
    SInitCDSData(ADOQueryMain, Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmYGSel.GLAge();
var
  FXTGLNF, FYGGLNF, FGLYF: Double;
begin
  FXTGLNF := strtofloat(uppercase(formatdateTime('yyyy', SGetServerDate(ADOQueryTemp))));
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('select RuZhiDate,YGYearGL from OA_YG_DangAn where YGType=''正式'' ');
    open;
  end;
  with ADOQueryCmd do
  begin
    first;
    while not eof do
    begin
      edit;
      if ADOQueryCmd.fieldbyname('RuZhiDate').asstring = '' then
      begin
        fieldbyname('YGYearGL').AsFloat := 0;
      end
      else
      begin
        FYGGLNF := strtofloat(uppercase(formatdateTime('yyyy', ADOQueryCmd.fieldbyname('RuZhiDate').AsDateTime)));
        FGLYF := strtofloat(uppercase(formatdateTime('MM', ADOQueryCmd.fieldbyname('RuZhiDate').AsDateTime)));
        if (FGLYF >= 1) and (FGLYF <= 3) then
          fieldbyname('YGYearGL').AsFloat := FXTGLNF - FYGGLNF + 1;
        if (FGLYF > 3) and (FGLYF <= 9) then
          fieldbyname('YGYearGL').AsFloat := FXTGLNF - FYGGLNF + 0.5;
        if (FGLYF > 9) and (FGLYF <= 12) then
          fieldbyname('YGYearGL').AsFloat := FXTGLNF - FYGGLNF;
      end;
      next;
    end;
  end;
end;

procedure TfrmYGSel.InitTree();
var
  i: Integer;
begin
  try
    ADOQueryTree.DisableControls;
    with ADOQueryTree do
    begin
      Close;
      SQL.Clear;
      SQL.Add('select * from SY_Dept order by DPlevel,DPOrder,DPName');
      Open;
    end;
    SCreateCDS(ADOQueryTree, CDS_Tree);
    SInitCDSData(ADOQueryTree, CDS_Tree);
    cxDBTreeList1.Items[0].Expand(True);
    CDS_Tree.Locate('DPName', '销售部', []);
  finally
    ADOQueryTree.EnableControls;
  end;
end;

procedure TfrmYGSel.InitForm();
begin
  ReadCxBandedGrid('员工档案列表2', Tv1, 'OA管理');
  InitTree();
  GLAge();
  InitGrid();
end;

procedure TfrmYGSel.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS(ADOQueryMain, Order_Main);
  SInitCDSData(ADOQueryMain, Order_Main);
end;

function TfrmYGSel.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;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('EXEC P_OAYG_in_ZhangHu @DataID=''' + Trim(Order_Main.fieldbyname('YGID').AsString) + '''');
      Open;
    end;
    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox(PChar(ADOQueryCmd.fieldbyname('ShowMsg').AsString), '提示', 0);
      exit;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result := True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result := False;
    Application.MessageBox('数据删除异常！', '提示', 0);
  end;
end;

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

procedure TfrmYGSel.FormShow(Sender: TObject);
begin
  inherited;
//  v1SJLiZhiDate.Visible := false;
//  if cxTabControl1.TabIndex <> 0 then
//    v1SJLiZhiDate.Visible := true;
//  canshu1 := self.fParameters1;
  SetStatus();
  InitForm();
end;

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

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

procedure TfrmYGSel.CustomerNoNameChange(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS(ADOQueryMain, Order_Main);
  SInitCDSData(ADOQueryMain, Order_Main);
end;

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

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

procedure TfrmYGSel.cxTabControl1Change(Sender: TObject);
begin
//  v1SJLiZhiDate.Visible := false;
//  if cxTabControl1.TabIndex <> 0 then
//    v1SJLiZhiDate.Visible := true;
  InitGrid();
end;

procedure TfrmYGSel.YGNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmYGSel.cxDBTreeList1DblClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmYGSel.Tv1CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  if Order_Main.IsEmpty then
    Exit;
  CDS_Tree.Locate('DPID', Trim(Order_Main.fieldbyname('DPID').AsString), []);
end;

procedure TfrmYGSel.TV1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  if Order_Main.IsEmpty then
    Exit;
  ModalResult := 1;
end;

end.

