unit U_GRYearPFList_WB;

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, cxCalendar, cxButtonEdit, cxSplitter,
  RM_Common, RM_Class, RM_e_Xls, RM_Dataset, RM_System, RM_GridReport,
  cxTextEdit, cxPC, cxCheckBox, Menus;


type
  TfrmGRYearPFList_WB = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    CDS_PRT: TClientDataSet;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label1: TLabel;
    ToolButton1: TToolButton;
    ToolButton4: TToolButton;
    ADOQueryPrint: TADOQuery;
    cxTabControl1: TcxTabControl;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Filltime: TcxGridDBColumn;
    v1GRName: TcxGridDBColumn;
    v1XZNum: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    lbl1: TLabel;
    lbl3: TLabel;
    GRName: TEdit;
    Dept: TEdit;
    TBAdd: TToolButton;
    TVWPFSumNum: TcxGridDBColumn;
    TVPFStatus: TcxGridDBColumn;
    TBTJ: TToolButton;
    TBCXTJ: TToolButton;
    TBUpdate: TToolButton;
    TBDel: TToolButton;
    cxGrid3: TcxGrid;
    TV2: TcxGridDBTableView;
    TPFYDian: TcxGridDBColumn;
    TPFMNum: TcxGridDBColumn;
    TPFYWNum: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    cxSplitter1: TcxSplitter;
    TPFXZNum: TcxGridDBColumn;
    TPFSCNum: TcxGridDBColumn;
    TWPFSumNum: TcxGridDBColumn;
    Source1: TDataSource;
    Order_Sub: TClientDataSet;
    v1SCNum: TcxGridDBColumn;
    v1GangWei: TcxGridDBColumn;
    v1RZDate: TcxGridDBColumn;
    v1YWNum: TcxGridDBColumn;
    v1Dept: TcxGridDBColumn;
    v1CWNum: TcxGridDBColumn;
    V2FPCWNum: TcxGridDBColumn;
    v1Ssel: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    v1Column1: TcxGridDBColumn;
    Label3: TLabel;
    PFYear: TEdit;
    Label4: TLabel;
    PFMonth: TEdit;
    v1PFYear: TcxGridDBColumn;
    Label2: TLabel;
    ZDept: TEdit;
    v1ZDept: TcxGridDBColumn;
    cxGrid2: TcxGrid;
    TV3: TcxGridDBTableView;
    V2Chker: TcxGridDBColumn;
    V2ChkStatus: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    DataSource2: TDataSource;
    PF_Sub: TClientDataSet;
    ADOQuery1: TADOQuery;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure TBUpdateClick(Sender: TObject);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure TBDelClick(Sender: TObject);
    procedure GRNameChange(Sender: TObject);
    procedure TBTJClick(Sender: TObject);
    procedure TBCXTJClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);

  private
    FInt,PFInt:Integer;
    canshu1,canshu2:string;
    FNowDate:TDateTime;
    procedure InitGrid();
    procedure InitForm();
    procedure setstatus();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmGRYearPFList_WB: TfrmGRYearPFList_WB;

implementation
uses
  U_DataLink,U_RTFun,U_KDInPut,U_ModuleNote, U_ZDYHelp,U_GRYearPFList_Sub,
  U_ZDYHelpSel;

{$R *.dfm}

procedure TfrmGRYearPFList_WB.setstatus();
var FDPParent: string;
    Fint: integer;
begin
  with ADOQueryMain do
  begin
    close;
    sql.Clear;
    sql.Add('select A.*,B.DPlevel,B.DPParent from SY_User A ');
    sql.add('inner join SY_Dept B on B.DPID=A.DPID ');
    sql.add('where A.UserName='''+trim(DName)+''' ');
    open;
  end;
  if ADOQueryMain.IsEmpty=False then
  begin
    FDPParent:=ADOQueryMain.fieldbyname('DPParent').AsString;
    for Fint:=2 to ADOQueryMain.fieldbyname('DPlevel').AsInteger-1 do
    begin
      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('select * from SY_Dept where DPID='''+trim(FDPParent)+'''');
        open;
      end;
      FDPParent:=Trim(ADOQueryCmd.fieldbyname('DPParent').AsString);
    end;
    TBTJ.Visible:=False;
    TBCXTJ.Visible:=False;
    v1XZNum.Visible:=True;
    v1YWNum.Visible:=True;
    v1SCNum.Visible:=true;
    v1CWNum.Visible:=true;
    TPFYWNum.Visible:=true;
    TPFXZNum.Visible:=true;
    TPFSCNum.Visible:=true;
    V2FPCWNum.Visible:=true;
    if ADOQueryCmd.FieldByName('DPName').AsString='业务部' then
    begin
      v1YWNum.Visible:=False;
      TPFYWNum.Visible:=false;
    end;
    if ADOQueryCmd.FieldByName('DPName').AsString='总经办' then
    begin
      v1XZNum.Visible:=false;
      TPFXZNum.Visible:=false;
    end;
    if ADOQueryCmd.FieldByName('DPName').AsString='生产部' then
    begin
      v1SCNum.Visible:=false;
      TPFSCNum.Visible:=false;
    end;
    if ADOQueryCmd.FieldByName('DPName').AsString='财务部' then
    begin
      V2FPCWNum.Visible:=false;
      v1CWNum.Visible:=false;
    end;
  end;
  case cxTabControl1.TabIndex of
    0:begin
        TBTJ.Visible:=true;
      end;
    1:begin
        TBCXTJ.Visible:=true;
      end;
    2:begin

      end;
  end;
end;

procedure TfrmGRYearPFList_WB.FormDestroy(Sender: TObject);
begin
  frmGRYearPFList_WB:=nil;
end;

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

procedure TfrmGRYearPFList_WB.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('个人年度考核外部1',Tv1,'薪酬管理');
end;

procedure TfrmGRYearPFList_WB.InitGrid();
begin
  with ADOQueryMain DO
  begin
    Close;
    sql.Clear;
    sql.Add('select * from GRYearPFWB_Sub ');
    sql.Add('where Filltime>='''+formatdatetime('yyyy-MM-dd',BegDate.Time)+''' ');
    sql.Add('and Filltime<'''+formatdatetime('yyyy-MM-dd',enddate.Time+1)+''' ');
    if cxTabControl1.TabIndex=0 then
    sql.Add('and PFStatus=''待提交'' ');
    if cxTabControl1.TabIndex=1 then
    sql.Add('and pfstatus<>''待提交'' ');
    //if Trim(DParameters1)<>'高权限' then
    //begin
      //sql.Add('and GRName='''+trim(DName)+'''');
    //end;
    Open;
  end;
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from OA_Chk where MainId='''+Trim(Order_Main.fieldbyname('PFSubID').AsString)+'''');
    sql.Add(' and isnull(OAType,'''')=''考核外部评分'' ');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,PF_Sub);
  SInitCDSData20(ADOQueryTemp,PF_Sub);
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('select * from GRYearPFWB_SubMX ');
    sql.Add('where PFSubID='''+trim(Order_Main.fieldbyname('PFSubID').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryCmd,Order_Sub);
  SInitCDSData20(ADOQueryCmd,Order_Sub);
end;

procedure TfrmGRYearPFList_WB.InitForm();
begin
  EndDate.Date:=SGetServerDate(ADOQueryTemp);
  BegDate.Date:=SGetServerDate(ADOQueryTemp)-7;
  ReadCxGrid('个人年度考核外部1',Tv1,'薪酬管理');
  cxtabcontrol1.TabIndex:=0;
  SetStatus();
  InitGrid();
end;

procedure TfrmGRYearPFList_WB.TBExportClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  TcxGridToExcel('个人年度评分',cxGrid1);
end;

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

procedure TfrmGRYearPFList_WB.FormShow(Sender: TObject);
var
  i:Integer;
begin
  InitForm();
end;

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

procedure TfrmGRYearPFList_WB.ToolButton1Click(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 TfrmGRYearPFList_WB.ToolButton4Click(Sender: TObject);
var
  fPrintFile:String;
begin
  if Order_Main.IsEmpty then Exit;
  if Order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据！','提示',0);
    Exit;
  end;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\检验指示单标签.rmf' ;
  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 JYOrder_Main_MD where MDID='''+Trim(Order_Main.fieldbyname('MDID').AsString)+'''');
          Open;
        end;
        if ADOQueryTemp.FieldByName('NotPrint').AsBoolean=True then
        begin
          Order_Main.EnableControls;
          Application.MessageBox('不能打印《不打印》的缸条码!','提示',0);
          Exit;
        end;  }
        if FileExists(fPrintFile) then
        begin
          with ADOQueryPrint do
          begin
            Close;
            sql.Clear;
            sql.Add('select B.OrderNo,B.MPRTCodeName,B.MPRTMF,B.MPRTKZ,C.PRTHX,C.PRTColorNo,C.PRTColor,GangNo=A.GangNo,A.PS,A.Qty,A.QtyUnit,A.MDID ');
            SQL.Add(',B.MPRTCode,B.MPRTGY,C.PRTColorEng,A.SelfGangNo');
            sql.Add('from JYOrder_Main_MD A');
            sql.Add('inner join JYOrder_Main B on A.OrdMainid=B.Mainid');
            sql.Add(' inner join JYOrder_Sub C on A.OrdSubid=C.SubId');
            sql.Add(' where A.MDID='''+Trim(Order_Main.fieldbyname('MDID').AsString)+'''');
            Open;
          end;
          RM1.LoadFromFile(fPrintFile);
          //RM1.ShowReport;
          RM1.PrintReport;
        end else
        begin
          Order_Main.DisableControls;
          Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\检验指示单标签.rmf'),'提示',0);
          Exit;
        end;
      end;

      Next;
    end;
  end;
  Order_Main.EnableControls;
end;

procedure TfrmGRYearPFList_WB.TBAddClick(Sender: TObject);
begin
  if application.MessageBox('确定要新增外部评分吗','提示',1)=2 then exit;
  
end;

procedure TfrmGRYearPFList_WB.TBUpdateClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then exit;
  frmGRYearPFList_Sub:=TfrmGRYearPFList_Sub.Create(self);
  with frmGRYearPFList_Sub do
  begin
    PFSubID:=Trim(Order_Main.fieldbyname('PFSubID').AsString);
    Formid:='1';
    if ShowModal=1 then
    begin
      InitGrid();
    end;
    Free;
  end;
end;

procedure TfrmGRYearPFList_WB.Tv1CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('select * from GRYearPFWB_SubMX ');
    sql.Add('where PFSubID='''+trim(Order_Main.fieldbyname('PFSubID').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryCmd,Order_Sub);
  SInitCDSData20(ADOQueryCmd,Order_Sub);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from OA_Chk where MainId='''+Trim(Order_Main.fieldbyname('PFSubID').AsString)+'''');
    sql.Add(' and isnull(OAType,'''')=''考核外部评分'' ');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,PF_Sub);
  SInitCDSData20(ADOQueryTemp,PF_Sub);
end;

procedure TfrmGRYearPFList_WB.TBDelClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then exit;
  if Application.MessageBox('确定要删除吗','提示',1)=2 then exit;
  with ADOQueryMain do
  begin
    Close;
    sql.Clear;
    sql.Add('delete from GRYearPF_Sub where PFSubid='''+trim(Order_Main.fieldbyname('PFSubid').AsString)+''' ');
    SQL.Add('delete from GRYearPF_SubMX where PFSubid='''+trim(Order_Main.fieldbyname('PFSubid').AsString)+'''');
    ExecSQL;
  end;
  InitGrid();
end;

procedure TfrmGRYearPFList_WB.GRNameChange(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 TfrmGRYearPFList_WB.TBTJClick(Sender: TObject);
var maxno,FDEPT,ZDept: string;
    i,Fint:integer;
begin
  if Order_Main.IsEmpty then exit;
  if Order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  frmZDYHelpSel:=TfrmZDYHelpSel.Create(Application);
  with frmZDYHelpSel do
  begin
    flag:='KHPFR';
    flagname:='考核评分人';
    MainType:=Trim(DName);
    if ShowModal=1 then
    begin
      with Order_Main do
      begin
        first;
        while not eof do
        begin
          if fieldbyname('Ssel').AsBoolean=true then
          begin
            with CLientDataSet1 do
            begin
              first;
              i:=0;
              while not eof do
              begin
                if fieldbyname('Ssel').AsBoolean=true then
                begin
                  i:=i+1;
                  if i>3 then
                  begin
                    application.MessageBox('评分人不能超过3个','提示');
                    exit;
                  end;
                  with ADOQueryTemp do
                  begin
                    Close;
                    sql.Clear;
                    sql.Add('select * from SY_User where UserName='''+Trim(ClientDataSet1.fieldbyname('ZdyName').AsString)+'''');
                    Open;
                  end;
                  if ADOQueryTemp.IsEmpty then
                  begin
                    Application.MessageBox('评分人定义错误!','提示',0);
                    Exit;
                  end;
                  with ADOQuery1 do
                  begin
                    Close;
                    sql.Clear;
                    sql.Add('select A.*,B.* from OA_YG_DangAn A ');
                    sql.Add('inner join SY_Dept B on A.DPID=B.DPID');
                    SQL.Add('where isnull(A.YGEName,YGName)='''+Trim(ClientDataSet1.fieldbyname('ZdyName').AsString)+'''');
                    Open;
                  end;
                  FDEPT:=ADOQuery1.fieldbyname('DPParent').AsString;
                  for Fint:=2 to ADOQuery1.fieldbyname('DPlevel').AsInteger-1 do
                  begin
                    with ADOQueryCmd do
                    begin
                      close;
                      sql.Clear;
                      sql.Add('select * from SY_Dept where DPID='''+trim(FDEPT)+'''');
                      open;
                    end;
                    FDEPT:=Trim(ADOQueryCmd.fieldbyname('DPParent').AsString);
                  end;
                  if ADOQueryCmd.IsEmpty=False then
                  ZDept:=trim(ADOQueryCmd.fieldbyname('DPName').asstring);
                  if Order_Main.FieldByName('ZDept').AsString=trim(ZDept) then
                  begin
                    application.MessageBox('不能提交给被考核人自己部门','提示');
                    exit;
                  end;
                end;
                next;
              end;
            end;
            with ClientDataSet1 do
            begin
              first;
              while not Eof do
              begin
                if fieldbyname('Ssel').AsBoolean=true then
                begin
                  with ADOQueryTemp do
                  begin
                    Close;
                    sql.Clear;
                    sql.Add('select * from OA_Chk where MainId='''+Trim(Order_Main.fieldbyname('PFSubID').AsString)+'''');
                    sql.Add(' and isnull(OAType,'''')=''考核外部评分'' ');
                    sql.Add(' and isnull(Chker,'''')='''+Trim(ClientDataSet1.fieldbyname('ZdyName').AsString)+'''');
                    Open;
                  end;
                  if ADOQueryTemp.IsEmpty then
                  begin
                    if GetLSNo(ADOQueryCmd,maxno,'PF','OA_Chk',4,1)=False then
                    begin
                      Application.MessageBox('取最大号失败!','提示',0);
                      Exit;
                    end;
                    with ADOQueryCmd do
                    begin
                      Close;
                      sql.Clear;
                      sql.Add('select * from OA_Chk where 1=2');
                      Open;
                      append;
                      FieldByName('CKID').Value:=Trim(maxno);
                      FieldByName('Mainid').Value:=Trim(Order_Main.fieldbyname('PFSubID').AsString);
                      FieldByName('OAType').value:='考核外部评分';
                      FieldByName('Chker').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
                      Post;
                    end;
                  end;
                end;
                Next;
              end;
            end;
            with ADOQueryTemp do
            begin
              Close;
              sql.Clear;
              sql.Add('update GRYearPFWB_Sub set PFStatus=''已提交'' ');
              sql.Add('where PFSubID='''+trim(Order_Main.fieldbyname('PFSubID').asstring)+''' ');
              ExecSQL;
            end;
          end;
          next;
        end;
      end;
    end;
    Free;
  end;
  InitGrid();
end;

procedure TfrmGRYearPFList_WB.TBCXTJClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then exit;
  if Order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要撤销吗','提示',1)=2 then Exit;
  with Order_Main do
  begin
    first;
    while not eof do
    begin
      if fieldbyname('Ssel').AsBoolean=True then
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('update GRYearPFWB_Sub set PFStatus=''待提交'' ');
          sql.Add('where PFSubID='''+trim(Order_Main.fieldbyname('PFSubID').asstring)+''' ');
          ExecSQL;
        end;
        with ADOQueryTemp do
        begin
          close;
          sql.Clear;
          sql.Add('delete from OA_Chk where MainId='''+trim(Order_Main.fieldbyname('PFSubID').AsString)+'''');
          sql.Add(' and isnull(OAType,'''')=''考核外部评分'' ');
          ExecSQL;
        end;
      end;
      next;
    end;
  end;
  InitGrid();
end;

procedure TfrmGRYearPFList_WB.N1Click(Sender: TObject);
begin
  SelOKNo(Order_Main,True);
end;

procedure TfrmGRYearPFList_WB.N2Click(Sender: TObject);
begin
  SelOKNo(Order_Main,False); 
end;

end.
