unit U_GRYearPFList_Sub;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls,
  cxGridCustomView, cxGrid, cxMemo, cxRichEdit, ComCtrls, cxContainer,
  cxTextEdit, cxMaskEdit, cxButtonEdit, StdCtrls, ToolWin, DBClient, ADODB,
  ExtCtrls, BtnEdit, cxDropDownEdit, cxCalendar, cxGridCustomPopupMenu,
  cxGridPopupMenu;

type
  TfrmGRYearPFList_Sub = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ScrollBox1: TScrollBox;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    ADOQuery1: TADOQuery;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Panel1: TPanel;
    Label1: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    PFDate: TDateTimePicker;
    Dept: TBtnEditA;
    GRName: TEdit;
    Label6: TLabel;
    RZDate: TDateTimePicker;
    lbl1: TLabel;
    v1PFYDian: TcxGridDBColumn;
    v1PFMNum: TcxGridDBColumn;
    v1PFZPNum: TcxGridDBColumn;
    Label5: TLabel;
    PFNote: TEdit;
    ZDept: TBtnEditA;
    GangWei: TBtnEditA;
    ToolBar2: TToolBar;
    ToolButton2: TToolButton;
    PFMonth: TComboBox;
    Label7: TLabel;
    Label2: TLabel;
    PFYear: TEdit;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure WorkerBtnClick(Sender: TObject);
    procedure v1Column3PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1Column3PropertiesEditValueChanged(Sender: TObject);
    procedure v1Column9PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1Column1PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1priceUnitPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure DeptBtnClick(Sender: TObject);
    procedure v1ZhaiYaoPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1PFZPNumPropertiesEditValueChanged(Sender: TObject);
    procedure ZDeptBtnClick(Sender: TObject);
    procedure GangWeiBtnClick(Sender: TObject);
    procedure Tv1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure ToolButton2Click(Sender: TObject);
  private
    FXS:Integer;
    procedure InitData();
    function  SaveData():Boolean;
    procedure InitDataXG();
    { Private declarations }
  public
    PState:Integer;
    FormId,PFSubID:String;

    { Public declarations }
  end;

var
  frmGRYearPFList_Sub: TfrmGRYearPFList_Sub;

implementation
uses
  U_DataLink,U_ZDYHelp,U_RTFun,U_GYSList,U_ZDYHelpSel,U_YGCXList,
  U_SYDeptUserView, U_SYDeptView;

{$R *.dfm}

procedure TfrmGRYearPFList_Sub.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('考核评分登记',Tv1,'薪酬管理');
end;

procedure TfrmGRYearPFList_Sub.InitData();
var FDPID,FDEPT: string;
    Fint: integer;
begin
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select dateadd(month,-1,getdate()) PFfilltime,A.*,B.* ');
    sql.add('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(DName)+'''');
    Open;
  end;
  Dept.Text:=Trim(ADOQuery1.fieldbyname('Dept').AsString);
  RZDate.DateTime:=ADOQuery1.fieldbyname('ZhuanZhengDate').AsDateTime;
  GangWei.text:=ADOQuery1.fieldbyname('GangWei').AsString;
  PFDate.DateTime:=ADOQuery1.fieldbyname('PFfilltime').AsDateTime;
  PFMonth.Text:=uppercase(formatdatetime('MM',ADOQuery1.fieldbyname('PFfilltime').AsDateTime));
  PFYear.Text:=uppercase(formatdatetime('yyyy',ADOQuery1.fieldbyname('PFfilltime').AsDateTime));
  GRName.Text:=Trim(DName);
  FDPID:=Trim(ADOQuery1.fieldbyname('DPID').AsString);
  FDEPT:=Trim(ADOQuery1.fieldbyname('DPParent').AsString);
  for Fint:=2 to ADOQuery1.fieldbyname('DPlevel').AsInteger-1 do
  begin
    with ADOCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select * from SY_Dept where DPID='''+trim(FDEPT)+'''');
      open;
    end;
    FDEPT:=Trim(ADOCmd.fieldbyname('DPParent').AsString);
  end;
  if ADOCmd.IsEmpty=False then
  ZDept.Text:=trim(ADOCmd.fieldbyname('DPName').asstring);
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from GRYearPF_SubMX ');
    sql.Add(' where PFSubID='''+Trim(PFSubID)+'''');
    Open;
  end;
  SCreateCDS20(ADOQuery1,Order_Sub);
  SInitCDSData20(ADOQuery1,Order_Sub);
  with ADOQuery1 do
  begin
    close;
    sql.Clear;
    SQL.Add('select * from PF_User where DPID='''+trim(FDPID)+'''');
    Open;
  end;
  with ADOQuery1 do
  begin
    First;
    while not eof do
    begin
      with Order_Sub do
      begin
        Append;
        FieldByName('PFYDian').Value:=ADOQuery1.fieldbyname('PFYDian').AsString;
        FieldByName('PFMNum').Value:=ADOQuery1.fieldbyname('PFMNum').AsFloat;
        post;
      end;
      Next;
    end;
  end;
end;

procedure TfrmGRYearPFList_Sub.InitDataXG();
begin
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add('select A.* from GRYearPF_Sub A ');
    sql.Add(' where A.PFSubID='''+Trim(PFSubID)+'''');
    Open;
  end;
  SCSHDataNew(ADOQuery1,Panel1,0);
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from GRYearPF_SubMX ');
    sql.Add(' where PFSubID='''+Trim(PFSubID)+'''');
    Open;
  end;
  SCreateCDS20(ADOQuery1,Order_Sub);
  SInitCDSData20(ADOQuery1,Order_Sub);
end;

procedure TfrmGRYearPFList_Sub.FormShow(Sender: TObject);
begin
  ReadCxGrid('考核评分登记',Tv1,'薪酬管理');
  if Trim(FormId)='0' then
  begin
    InitData();
  end
  else
  begin
    InitDataXG();
  end;
end;

function TfrmGRYearPFList_Sub.SaveData():Boolean;
var
  maxno,maxSubNo:String;
begin
  try
    Result:=False;
    ADOCmd.Connection.BeginTrans;
    //保存主表
    if Trim(PFSubID)='' then
    begin
      if GetLSNo(ADOCmd,maxno,'PF','GRYearPF_Sub',3,1)=False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('取主流水号失败!','提示',0);
        Exit;
      end;
    end else
    begin
      maxno:=Trim(PFSubID);
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from GRYearPF_Sub where PFSubID='''+Trim(PFSubID)+'''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(PFSubID)='' then
      begin
        Append;
      end
      else begin
        Edit;
      end;
      FieldByName('PFSubID').Value:=Trim(maxno);
      RTSetsavedata(ADOCmd,'GRYearPF_Sub',Panel1,0);
      fieldbyname('filltime').Value:=SGetServerDate(ADOQuery1);
      fieldbyname('PFType').Value:='内部';
      FieldByName('PFStatus').Value:='待提交';
      Post;
    end;
    with ADOCmd DO
    begin
      Close;
      sql.Clear;
      sql.Add('select * from GRYearPF_Sub ');
      sql.add('where PFYear='''+trim(PFYear.Text)+''' ');
      sql.add('and PFMonth='''+trim(PFMonth.Text)+''' ');
      sql.add('and GRName='''+trim(DName)+'''');
      open;
    end;
    if ADOCmd.RecordCount>1 then
    begin
      ADOCmd.Connection.RollbackTrans;
      application.MessageBox('当月已存在评分不能新增','提示');
      exit;
    end;
    //外部
    with ADOCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select * from GRYearPFWB_Sub ');
      sql.Add('where PFSubID='''+Trim(PFSubID)+'''');
      open;
      if isempty then
      begin
        append;
      end
      else
      edit;
      fieldbyname('PFSubID').AsString:=trim(maxno);
      RTSetsavedata(ADOCmd,'GRYearPFWB_Sub',Panel1,0);
      fieldbyname('filltime').Value:=SGetServerDate(ADOQuery1);
      fieldbyname('PFType').value:='外部';
      FieldByName('PFStatus').Value:='待提交';
      post;
    end;
    with ADOCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select A.*,B.* from PF_User A ');
      SQL.Add('Inner join SY_Dept B on A.DPID=B.DPID ');
      sql.add('where B.DPName=''生产部''');
      open;
    end;
    with ADOQuery1 do
    begin
      close;
      sql.Clear;
      sql.Add('select * from GRYearPFWB_SubMX ');
      sql.add('where PFSubID='''+trim(PFSubID)+'''');
      open;
      if isempty then
      begin
        with ADOCmd do
        begin
          first;
          while not eof do
          begin
            if GetLSNo(ADOTemp,maxSubno,'WPF','GRYearPFWB_SubMX',4,1)=False then
            begin   
              ADOCmd.Connection.RollbackTrans;
              Application.MessageBox('取流水号失败!','提示',0);
              Exit;
            end;
            with ADOQuery1 do
            begin
              append;
              fieldbyname('PFSubMXID').asstring:=trim(MaxSubno);
              fieldbyname('PFSubid').AsString:=trim(maxNo);
              fieldbyname('PFYDian').AsString:=trim(ADOCmd.fieldbyname('PFYDian').AsString);
              fieldbyname('PFMNum').asfloat:=ADOCmd.fieldbyname('PFMNum').AsFloat;
              post;
            end;
            next;
          end;
        end;
      end;
    end;

    ///保存子表
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        if Trim(Order_Sub.fieldbyname('PFSubMXID').AsString)='' then
        begin
          if GetLSNo(ADOCmd,maxSubNo,'PX','GRYearPF_SubMX',4,1)=False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('生成子表流水号异常！','提示',0);
            exit;
          end;
        end else
        begin
          maxSubNo:=Trim(Order_Sub.fieldbyname('PFSubMXID').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from GRYearPF_SubMX where ');
          sql.Add(' PFSubMXID='''+Trim(maxSubNo)+'''');
          Open;
        end;
        with ADOCmd do
        begin
          if isempty then
            Append
          else
            Edit;
          FieldByName('PFSubID').Value:=Trim(maxno);
          FieldByName('PFSubMXID').Value:=Trim(maxSubNo);
          RTSetSaveDataCDS(ADOCmd,Tv1,Order_Sub,'GRYearPF_SubMX',0);
          fieldbyname('PFZPNum').asfloat:=Order_Sub.fieldbyname('PFZPNum').AsFloat;
          Post;
        end;
        Order_Sub.Edit;
        Order_Sub.FieldByName('PFSubMXID').Value:=Trim(maxSubNo);
        Next;
      end;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update GRYearPF_Sub Set PFNum=');
      sql.Add('(select isnull(Sum(PFZPNum),0) from GRYearPF_SubMX A where A.PFSubID=GRYearPF_Sub.PFSubID)');
      sql.Add(' where PFSubID='''+Trim(maxno)+'''');
      ExecSQL;
    end;
    ADOCmd.Connection.CommitTrans;
    PFSubID:=Trim(maxno);
    Result:=True;
  except
    Result:=False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;

procedure TfrmGRYearPFList_Sub.TBSaveClick(Sender: TObject);
var
  FReal:Double;
  FInt:Integer;
  FPPrice,PRTPrice,BCMoney,FPQty,ChaMoney,FYear,FMonth:string;
begin
  ToolBar1.SetFocus;
  if trystrtoInt(PFYear.Text,Fint)=False then
  begin
    application.MessageBox('年份非法数字','提示');
    exit;
  end;
  if trystrtoInt(PFMonth.Text,Fint)=False then
  begin
    application.MessageBox('月份非法数字','提示');
    exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！','提示',0);
    ModalResult:=1;
  end;
end;

procedure TfrmGRYearPFList_Sub.WorkerBtnClick(Sender: TObject);
begin
  frmYGCXList:=TfrmYGCXList.Create(self);
  with frmYGCXList do
  begin
    FFInt:=100;
    TSel.Visible:=True;
    if showModal=1 then
    begin
      Dept.Text:=trim(CDS_Tree.fieldbyname('DPName').AsString);
    end;
    free;
  end;
end;

procedure TfrmGRYearPFList_Sub.v1Column3PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='KeMu';
      flagname:='科目';
      if ShowModal=1 then
      begin
        with Self.Order_Sub do
        begin
          Edit;
          FieldByName('KeMu').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
         // Post;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmGRYearPFList_Sub.v1Column3PropertiesEditValueChanged(
  Sender: TObject);
var
  FName,FQty,FPrice,mvalue:string;
begin
  FName:=Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName;
  mvalue:=TcxTextEdit(Sender).EditingText;

  with Order_Sub do
  begin
    Edit;
    if Trim(mvalue)='' then
      FieldByName(FName).Value:=null
    else
      FieldByName(FName).Value:=mvalue;
    Post;
  end;
  FQty:=Trim(Order_Sub.fieldbyname('YCLQty').AsString);
  FPrice:=Trim(Order_Sub.fieldbyname('YCLPrice').AsString);
  if Trim(FQty)='' then FQty:='0';
  if Trim(FPrice)='' then FPrice:='0';
  with Order_Sub do
  begin
    Edit;
    FieldByName('Money').Value:=StrToFloat(FQty)*StrToFloat(FPrice);
    Post;
  end;
end;

procedure TfrmGRYearPFList_Sub.v1Column9PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  frmZDYHelp:=TfrmZDYHelp.Create(self);
  with frmZDYHelp do
  begin
    flag:='YCLUnit';
    flagname:='数量单位';
    if showModal=1 then
    begin
      with Order_Sub do
      begin
        edit;
        fieldbyname('YCLUnit').asstring:=trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
    free;
  end;
end;

procedure TfrmGRYearPFList_Sub.v1Column1PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  frmZDYHelp:=TfrmZDYHelp.Create(self);
  with frmZDYHelp do
  begin
    flag:='ZhaiYao';
    flagname:='付款单位';
    if showModal=1 then
    begin
      with Order_Sub do
      begin
        edit;
        fieldbyname('ZhaiYao').asstring:=trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
    free;
  end;
end;

procedure TfrmGRYearPFList_Sub.v1priceUnitPropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  frmZDYHelp:=TfrmZDYHelp.Create(self);
  with frmZDYHelp do
  begin
    flag:='priceUnit';
    flagname:='币种';
    if showModal=1 then
    begin
      with Order_Sub do
      begin
        edit;
        fieldbyname('priceUnit').asstring:=trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
    free;
  end;
end;

procedure TfrmGRYearPFList_Sub.DeptBtnClick(Sender: TObject);
begin
  try
    frmSYDeptView:=TfrmSYDeptView.Create(Application);
    with frmSYDeptView do
    begin
      if ShowModal=1 then
      begin
        Self.Dept.Text:=Trim(frmSYDeptView.CDS_Tree.fieldbyname('DPName').AsString);
        Self.Dept.TxtCode:=Trim(frmSYDeptView.CDS_Tree.fieldbyname('DPID').AsString);
        with ADOQuery1 do
        begin
          close;
          sql.Clear;
          SQL.Add('select * from PF_User where DPID='''+trim(Self.Dept.TxtCode)+'''');
          Open;
        end;
        with ADOQuery1 do
        begin
          First;
          while not eof do
          begin
            with Order_Sub do
            begin
              Append;
              FieldByName('PFYDian').Value:=ADOQuery1.fieldbyname('PFYDian').AsString;
              FieldByName('PFMNum').Value:=ADOQuery1.fieldbyname('PFMNum').AsFloat;
              post;
            end;
            Next;
          end;
        end;
      end;
    end;
  finally
    frmSYDeptView.Free;
  end;
end;

procedure TfrmGRYearPFList_Sub.v1ZhaiYaoPropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  frmZDYHelp:=TfrmZDYHelp.Create(self);
  with frmZDYHelp do
  begin
    flag:='ZhaiYao';
    flagname:='付款单位';
    if showModal=1 then
    begin
      Order_Sub.Edit;
      Order_Sub.FieldByName('ZhaiYao').AsString:=trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
    end;
    free;
  end;
end;

procedure TfrmGRYearPFList_Sub.v1PFZPNumPropertiesEditValueChanged(
  Sender: TObject);
var mavlue: string;
begin
  mavlue:=TcxTextEdit(Sender).EditingText;
  if strtofloat(mavlue)>Order_Sub.fieldbyname('PFMNum').asfloat then
  begin
    application.messagebox('评分不能超过满分','提示');
    mavlue:='';
  end;
  with Order_Sub do
  begin
    edit;
    fieldbyname('PFZPNum').AsString:=trim(mavlue);
    post;
  end;
end;

procedure TfrmGRYearPFList_Sub.ZDeptBtnClick(Sender: TObject);
begin
  try
    frmSYDeptView:=TfrmSYDeptView.Create(Application);
    with frmSYDeptView do
    begin
      if ShowModal=1 then
      begin
        Self.ZDept.Text:=Trim(frmSYDeptView.CDS_Tree.fieldbyname('DPName').AsString);
      end;
    end;
  finally;
    frmSYDeptView.Free;
  end;
end;

procedure TfrmGRYearPFList_Sub.GangWeiBtnClick(Sender: TObject);
begin
  try
    frmSYDeptView:=TfrmSYDeptView.Create(Application);
    with frmSYDeptView do
    begin
      if ShowModal=1 then
      begin
        Self.GangWei.Text:=Trim(frmSYDeptView.CDS_Tree.fieldbyname('DPName').AsString);
      end;
    end;
  finally;
    frmSYDeptView.Free;
  end;
end;

procedure TfrmGRYearPFList_Sub.Tv1KeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  TV1.DataController.GotoNext;
end;

procedure TfrmGRYearPFList_Sub.ToolButton2Click(Sender: TObject);
begin
  Order_Sub.Delete;
end;

end.
