unit U_GRYearPFList;

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 = 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;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label1: TLabel;
    ToolButton1: TToolButton;
    ToolButton4: TToolButton;
    ADOQueryPrint: TADOQuery;
    cxTabControl1: TcxTabControl;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Filltime: TcxGridDBColumn;
    v1GRName: TcxGridDBColumn;
    v1PFSJNum: TcxGridDBColumn;
    TVv1PFNote: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    lbl1: TLabel;
    lbl3: TLabel;
    GRName: TEdit;
    Dept: TEdit;
    TBAdd: TToolButton;
    TVPFSumNum: TcxGridDBColumn;
    TVPFStatus: TcxGridDBColumn;
    TBTJ: TToolButton;
    TBCXTJ: TToolButton;
    TBUpdate: TToolButton;
    TBDel: TToolButton;
    cxGrid3: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridLevel2: TcxGridLevel;
    cxSplitter1: TcxSplitter;
    Source1: TDataSource;
    Order_Sub: TClientDataSet;
    v1PFXJNum: TcxGridDBColumn;
    v1GangWei: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1PFNum: TcxGridDBColumn;
    v1Dept: TcxGridDBColumn;
    v1PFMonth: TcxGridDBColumn;
    Label3: TLabel;
    PFYear: TEdit;
    Label4: TLabel;
    PFMonth: TEdit;
    v1PFYear: TcxGridDBColumn;
    cxGrid2: TcxGrid;
    TV3: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    V2Chker: TcxGridDBColumn;
    V2ChkStatus: TcxGridDBColumn;
    PF_Sub: TClientDataSet;
    DataSource3: TDataSource;
    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);

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

var
  frmGRYearPFList: TfrmGRYearPFList;

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

{$R *.dfm}

procedure TfrmGRYearPFlist.setstatus();
begin
  TBUpdate.Visible:=False;
  TBDel.Visible:=False;
  TBTJ.Visible:=False;
  TBCXTJ.Visible:=False;
  TBAdd.Visible:=False;
  cxTabControl1.Visible:=true;
  if trim(DParameters1)<>'高权限' then
  begin
    case cxTabControl1.TabIndex of
    0:begin
        TBUpdate.Visible:=true;
        TBDel.Visible:=true;
        TBTJ.Visible:=true;
        TBAdd.Visible:=true;
      end;
    1:begin
        TBCXTJ.Visible:=true;
      end;
    2:begin

      end;
    end;
  end
  else
  begin
    cxTabControl1.Visible:=False;
  end;
end;

procedure TfrmGRYearPFList.FormDestroy(Sender: TObject);
begin
  frmGRYearPFList:=nil;
end;

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

procedure TfrmGRYearPFList.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('个人年度考核评分',Tv1,'薪酬管理');
end;

procedure TfrmGRYearPFList.InitGrid();
begin
  with ADOQueryMain DO
  begin
    Close;
    sql.Clear;
    sql.Add('select * from GRYearPF_Sub ');
    sql.Add('where Filltime>='''+formatdatetime('yyyy-MM-dd',BegDate.Time)+''' ');
    sql.Add('and Filltime<'''+formatdatetime('yyyy-MM-dd',enddate.Time+1)+''' ');
    if Trim(DParameters1)<>'高权限' then
    begin
      if cxTabControl1.TabIndex=0 then
      sql.Add('and PFStatus=''待提交'' ');
      if cxTabControl1.TabIndex=1 then
      sql.Add('and pfstatus<>''待提交'' ');
      sql.Add('and GRName='''+trim(DName)+'''');
    end
    else
    begin
      sql.Add('and PFStatus=''评分完成'' ');
    end;
    Open;
  end;
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('select * from GRYearPF_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.InitForm();
begin
  EndDate.Date:=SGetServerDate(ADOQueryTemp);
  BegDate.Date:=SGetServerDate(ADOQueryTemp)-15;
  ReadCxGrid('个人年度考核评分',Tv1,'薪酬管理');
  setstatus();
  InitGrid();
end;

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

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

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

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

procedure TfrmGRYearPFList.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.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.TBAddClick(Sender: TObject);
var FYear,FMonth:string;
begin
  frmGRYearPFList_Sub:=TfrmGRYearPFList_Sub.Create(self);
  with frmGRYearPFList_Sub do
  begin
    PFSubID:='';
    Formid:='0';
    if ShowModal=1 then
    begin
      InitGrid();
    end;
    Free;
  end;
end;

procedure TfrmGRYearPFList.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.Tv1CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('select * from GRYearPF_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.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)+'''');
    sql.add('delete from GRYearPFWB_SubMX where PFSubid='''+trim(Order_Main.fieldbyname('PFSubid').AsString)+'''');
    sql.add('delete from GRYearPFWB_Sub where PFSubid='''+trim(Order_Main.fieldbyname('PFSubid').AsString)+'''');
    ExecSQL;
  end;
  InitGrid();
end;

procedure TfrmGRYearPFList.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.TBTJClick(Sender: TObject);
var maxno: string;
begin
  if Order_Main.IsEmpty then exit;
  frmZDYHelpSel:=TfrmZDYHelpSel.Create(Application);
  with frmZDYHelpSel do
  begin
    flag:='KHPFR';
    flagname:='考核评分人';
    MainType:=Trim(DName);
    if ShowModal=1 then
    begin
      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 SY_User where UserName='''+Trim(ClientDataSet1.fieldbyname('ZdyName').AsString)+'''');
              Open;
            end;
            if ADOQueryTemp.IsEmpty then
            begin
              Application.MessageBox('评分人定义错误!','提示',0);
              Exit;
            end;
            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 ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('update GRYearPF_Sub set PFStatus=''已提交'' ');
        sql.Add('where PFSubID='''+trim(Order_Main.fieldbyname('PFSubID').asstring)+''' ');
        ExecSQL;
      end;
    end;
    Free;
  end;
  InitGrid();
end;

procedure TfrmGRYearPFList.TBCXTJClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then exit;
  if Application.MessageBox('确定要撤销吗','提示',1)=2 then Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('update GRYearPF_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;
  InitGrid();
end;

end.
