unit U_YGYPInPut_YGXZDJ;

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, cxCalendar,StrUtils, cxDropDownEdit,jpeg,
  IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdFTP, cxPC,
  cxGridCustomPopupMenu, cxGridPopupMenu, Menus, ExtDlgs, cxCurrencyEdit;

type
  TfrmYGYPInPut_YGXZDJ = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    TBSave: TToolButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    Panel3: TPanel;
    Label4: TLabel;
    Label11: TLabel;
    JSDate: TDateTimePicker;
    FFDate: TDateTimePicker;
    Label6: TLabel;
    TGZFF: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1YGName: TcxGridDBColumn;
    v1GZMoney: TcxGridDBColumn;
    v1KQJMoney: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1KOtherMoney: TcxGridDBColumn;
    v1KCDMoney: TcxGridDBColumn;
    v1KGSMoney: TcxGridDBColumn;
    v1JLMoney: TcxGridDBColumn;
    ToolBar2: TToolBar;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    v1KSBMoney: TcxGridDBColumn;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    Label3: TLabel;
    Edit1: TEdit;
    v1XHNo: TcxGridDBColumn;
    v1KFKMeny: TcxGridDBColumn;
    v1FFMoney: TcxGridDBColumn;
    v1XZNote: TcxGridDBColumn;
    Label1: TLabel;
    SSYear: TEdit;
    Label2: TLabel;
    SSMonth: TComboBox;
    PriceUnit: TBtnEditA;
    v1FKSYSTEM: TcxGridDBColumn;
    v1FKType: TcxGridDBColumn;
    Label5: TLabel;
    FKTaiTou: TBtnEditA;
    Label7: TLabel;
    FKType: TComboBox;
    TSYGZ: TToolButton;
    ADOQueryMain: TADOQuery;
    Label8: TLabel;
    ADOQuery1: TADOQuery;
    ADOQuery2: TADOQuery;
    v1JKMoney: TcxGridDBColumn;
    ADOQueryJK: TADOQuery;
    ADOQuery3: TADOQuery;
    FKDept: TBtnEditA;
    v1CJBGZMoney: TcxGridDBColumn;
    v1GLFLMoney: TcxGridDBColumn;
    v1DXGZMoney: TcxGridDBColumn;
    v1XHXJMoney: TcxGridDBColumn;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure TGZFFClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure v1GZMoneyPropertiesEditValueChanged(Sender: TObject);
    procedure v1FKSYSTEMPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1FKTypePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure FKTaiTouBtnClick(Sender: TObject);
    procedure PriceUnitBtnClick(Sender: TObject);
    procedure TSYGZClick(Sender: TObject);
    procedure Tv1EditKeyDown(Sender: TcxCustomGridTableView;
      AItem: TcxCustomGridTableItem; AEdit: TcxCustomEdit; var Key: Word;
      Shift: TShiftState);
    procedure Tv1NavigatorButtonsButtonClick(Sender: TObject;
      AButtonIndex: Integer; var ADone: Boolean);
    procedure FKDeptBtnClick(Sender: TObject);
  private
    TPInt:Integer;
    procedure InitData();
    function  SaveData():Boolean;
    procedure GLAge();
    procedure saveDataSY();
    procedure JXUPDate();
    procedure YGLiZhi();
    procedure NXZZDate();
    { Private declarations }
  public
    canshu1:String;
    PState,CopyInt,FXHNO:Integer;
    FMainId,FXZMain,FXZSubid :String;
    { Public declarations }
  end;

var
  frmYGYPInPut_YGXZDJ: TfrmYGYPInPut_YGXZDJ;
implementation
uses
  U_DataLink,U_ZDYHelp,U_RTFun,U_ZDYHelpSel,U_GRYearPFList_FHDQDJ;

{$R *.dfm}

procedure TfrmYGYPInPut_YGXZDJ.NXZZDate();
var MaxNXNo: string;
    FSNXSMoney: double;
begin
  try
    if SSMonth.Text='1' then
    begin
      with ADOTemp do
      begin
        close;
        sql.Clear;
        sql.Add('select * ');
        Sql.add('from OA_YG_DangAN_NXJL ');
        sql.add('where YGYear='''+trim(SSYear.Text)+''' ');
        open;
        if isempty then
        begin
          with ADOCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update OA_YG_DangAn set ');
            sql.add(' NXFFStatus='''' ');
            sql.add('where LiZhiFlag=0 ');
            sql.add('and FHType=''年薪制'' ');
            execsql;
          end;
        end;
      end;
    end;
    with ADOCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select * ');
      Sql.add('from OA_YG_DangAn ');
      sql.add('where LiZhiFlag=0 ');
      sql.add('and FHType=''年薪制'' ');
      open;
    end;
    with ADOCmd do
    begin
      first;
      while not eof do
      begin
        with ADOTemp do
        begin
          close;
          sql.Clear;
          sql.Add('select * ');
          Sql.add('from OA_YG_DangAN_NXJL ');
          sql.add('where YGYear='''+trim(SSYear.Text)+''' ');
          sql.add('and YGID='''+trim(ADOCmd.fieldbyname('YGID').AsString)+'''');
          open;
          if isempty then
          begin
            if GetLSNo(ADOQuery1,MaxNXNo,'NX','OA_YG_DangAN_NXJL',4,1)=False then
            begin
              Application.MessageBox('取主流水号失败!','提示',0);
              Exit;
            end;
          end
          else
          MaxNXNo:=trim(ADOTemp.fieldbyname('NXId').asstring);
        end;
        with ADOTemp do
        begin
          close;
          sql.clear;
          sql.Add('select * from OA_YG_DangAN_NXJL ');
          sql.add(' where NXID='''+trim(MaxNXNo)+'''');
          open;
          if isempty then
          begin
            with ADOQueryMain do
            begin
              close;
              sql.Clear;
              sql.Add('update OA_YG_DangAn set ');
              Sql.add(' SYearGZMoney=GRNXMoney');
              sql.add(' where YGID='''+trim(ADOCmd.fieldbyname('YGID').AsString)+'''');
              execsql;
            end;
            with ADOQueryMain do
            begin
              close;
              sql.Clear;
              sql.Add('update OA_YG_DangAN_NXJL set ');
              if trim(ADOCmd.fieldbyname('GRYearPF').AsString)<>'' then
              Sql.add(' GRYearPF='''+trim(ADOCmd.fieldbyname('GRYearPF').AsString)+'''')
              else
              Sql.add(' GRYearPF=0');
              sql.add(' where YGYear='''+trim(floattostr(strtofloat(SSYear.Text)-1))+''' ');
              sql.add('and YGID='''+trim(ADOCmd.fieldbyname('YGID').AsString)+'''');
              execsql;
            end;
          end;
        end;
        with ADOTemp do
        begin
          close;
          sql.clear;
          sql.Add('select * from OA_YG_DangAN_NXJL ');
          sql.add(' where NXID='''+trim(MaxNXNo)+'''');
          open;
          if isempty then
          begin
            append;
          end
          else
          edit;
          fieldbyname('NXID').Value:=trim(MaxNXNo);
          fieldbyname('YGYear').Value:=trim(SSYear.Text);
          if ADOCmd.FieldByName('NXFFStatus').AsString='' then
          begin
            if ADOCmd.fieldbyname('NXZZType').AsString<>'年度一月' then
            begin
              if (strtocurr(uppercase(formatdatetime('yyyy',ADOCmd.fieldbyname('ZhuanZhengDate').AsDateTime)))<strtocurr(SSYear.Text)) and
                 (strtocurr(uppercase(formatdatetime('MM',ADOCmd.fieldbyname('ZhuanZhengDate').AsDateTime)))<=strtocurr(SSMonth.Text)) then
              begin
                if ADOCmd.fieldbyname('GRYearPF').AsFloat>60 then
                begin
                  if ADOCmd.fieldbyname('GRNXMoney').AsFloat<=50000 then
                  begin
                    FSNXSMoney:=ADOCmd.fieldbyname('GRNXMoney').AsFloat*1.1;
                  end
                  else if ADOCmd.fieldbyname('GRNXMoney').AsFloat<=100000 then
                  begin
                    FSNXSMoney:=ADOCmd.fieldbyname('GRNXMoney').AsFloat*1.08;
                  end
                  else if ADOCmd.fieldbyname('GRNXMoney').AsFloat<=150000 then
                  begin
                    FSNXSMoney:=ADOCmd.fieldbyname('GRNXMoney').AsFloat*1.05;
                  end
                  else
                  begin
                    FSNXSMoney:=ADOCmd.fieldbyname('GRNXMoney').AsFloat*1.03;
                  end;
                end
                else
                begin
                  FSNXSMoney:=ADOCmd.Fieldbyname('GRNXMoney').asfloat;
                end;
                with ADOQuery2 do
                begin
                  close;
                  sql.Clear;
                  sql.Add('update OA_YG_DangAn set NXFFStatus=''已增涨'' ');
                  sql.add('where YGID='''+trim(ADOCmd.fieldbyname('YGID').AsString)+'''');
                  execsql;
                end;
              end
              else
              begin
                FSNXSMoney:=ADOCmd.fieldbyname('GRNXMoney').AsFloat;
              end;
            end
            else
            begin
              if trim(SSMonth.text)='1' then
              begin
                if ADOCmd.fieldbyname('GRYearPF').AsFloat>60 then
                begin
                  if ADOCmd.fieldbyname('GRNXMoney').AsFloat<=50000 then
                  begin
                    FSNXSMoney:=ADOCmd.fieldbyname('GRNXMoney').AsFloat*1.1;
                  end
                  else if ADOCmd.fieldbyname('GRNXMoney').AsFloat<=100000 then
                  begin
                    FSNXSMoney:=ADOCmd.fieldbyname('GRNXMoney').AsFloat*1.08;
                  end
                  else if ADOCmd.fieldbyname('GRNXMoney').AsFloat<=150000 then
                  begin
                    FSNXSMoney:=ADOCmd.fieldbyname('GRNXMoney').AsFloat*1.05;
                  end
                  else
                  begin
                    FSNXSMoney:=ADOCmd.fieldbyname('GRNXMoney').AsFloat*1.03;
                  end;
                end
                else
                begin
                  FSNXSMoney:=ADOCmd.fieldbyname('GRNXMoney').AsFloat;
                end;
                with ADOQuery2 do
                begin
                  close;
                  sql.Clear;
                  sql.Add('update OA_YG_DangAn set NXFFStatus=''已增涨'' ');
                  sql.add('where YGID='''+trim(ADOCmd.fieldbyname('YGID').AsString)+'''');
                  execsql;
                end;
              end
              else
              begin
                FSNXSMoney:=ADOCmd.fieldbyname('GRNXMoney').asfloat;
              end;
            end;
          end
          else
          begin
            FSNXSMoney:=ADOCmd.fieldbyname('GRNXMoney').AsFloat;
          end;
          fieldbyname('YGNXMoney').AsFloat:=FSNXSMoney;
          fieldbyname('filltime').AsDateTime:=SGetServerDate(ADOQuery1);
          fieldbyname('YGID').Value:=trim(ADOCmd.fieldbyname('YGID').AsString);
          post;
        end;
        with ADOTemp do
        begin
          close;
          sql.Clear;
          sql.Add('update OA_YG_DangAn set GRNXMoney='''+currtostr(FSNXSMoney)+''' ');
          sql.add('where YGID='''+trim(ADOCmd.fieldbyname('YGID').AsString)+'''');
          execsql;
        end;
        next;
      end;
    end;
  finally;
    
  end;
end;

procedure TfrmYGYPInPut_YGXZDJ.YGLiZhi();
begin
  with ADOCmd do
  begin
    close;
    sql.Clear;
    sql.Add('select * from OA_YG_DangAn ');
    sql.add('where LiZhiFlag=0 and LiZhiStatus=''离职通过'' ');
    open;
  end;
  with ADOCmd do
  begin
    first;
    while not eof do
    begin
      if FieldByName('SJLiZhiDate').AsDateTime<SGetServerDate(ADOTemp) then
      begin
        with ADOQueryMain do
        begin
          close;
          sql.Clear;
          sql.add('update OA_YG_DangAn set ');
          sql.add('LiZhiFlag=1,YGType=''离职'' ');
          sql.Add(' where YGID='''+trim(ADOCmd.fieldbyname('YGID').AsString)+'''');
          execsql;
        end;
      end;
      next;
    end;
  end;
end;

procedure TfrmYGYPInPut_YGXZDJ.JXUPDate();
var FNXMoney,FNXZZMoney: double;
begin
  with ADOQueryMain do
  begin
    close;
    sql.Clear;
    sql.Add('select * from OA_YG_DangAn_JX ');
    sql.add('where JXStatus=''加薪通过'' and getdate()>SXJXdate ');
    open;
  end;
  with ADOQueryMain do
  begin
    first;
    while not eof do
    begin
      with ADOTemp do
      begin
        close;
        sql.Clear;
        sql.Add('select * from OA_YG_DangAn ');
        sql.add('where YGID='''+trim(ADOQueryMain.fieldbyname('YGID').AsString)+'''');
        open;
      end;
      FNXMoney:=ADOTemp.fieldbyname('GRNXMoney').AsFloat;
      with ADOTemp do
      begin
        close;
        sql.Clear;
        sql.Add('select * from OA_YG_DangAN_NXJL ');
        sql.add('where YGID='''+trim(ADOQueryMain.fieldbyname('YGID').AsString)+'''');
        sql.add(' and YGYear='''+trim(SSYear.Text)+'''');
        open;
      end;
      FNXZZMoney:=ADOTemp.fieldbyname('YGNXMoney').AsFloat;
      with ADOTemp do
      begin
        close;
        sql.Clear;
        sql.Add('update OA_YG_DangAn set GRNXMoney='''+floattostr(FNXMoney+ADOQueryMain.fieldbyname('JXMoney').AsFloat)+''' ');
        sql.add('where YGID='''+trim(ADOQueryMain.fieldbyname('YGID').AsString)+'''');
        execsql;
      end;
      with ADOTemp do
      begin
        close;
        sql.Clear;
        sql.Add('update OA_YG_DangAN_NXJL set YGNXMoney='''+floattostr(FNXZZMoney+ADOQueryMain.fieldbyname('JXMoney').AsFloat)+''' ');
        sql.add('where YGID='''+trim(ADOQueryMain.fieldbyname('YGID').AsString)+'''');
        sql.add(' and YGYear='''+trim(SSYear.Text)+'''');
        execsql;
      end;
      with ADOTemp do
      begin
        close;
        sql.Clear;
        sql.add('update OA_YG_DangAn_JX set ');
        sql.add('JXStatus=''已加薪'' ');
        sql.Add(' where JXID='''+trim(ADOQueryMain.fieldbyname('JXID').AsString)+'''');
        execsql;
      end;
      next;
    end;
  end;
end;

procedure TfrmYGYPInPut_YGXZDJ.saveDataSY();
var FYear: string;
begin
  {FYear:=uppercase(formatdateTime('yyyy',JSDate.datetime);
  with Order_Sub do
  begin
    first;
    while not eof do
    begin
      with ADOTemp do
      begin
        close;
        sql.Clear;
        sql.Add('select * from OA_YG_XZFF_Main ');
        open;
      end;
      next;
    end;
  end;}
end;

procedure TfrmYGYPInPut_YGXZDJ.GLAge();
var FXTGLNF,FYGGLNF,FGLYF: Double;
begin
  FXTGLNF:=strtofloat(uppercase(formatdateTime('yyyy',JSDate.datetime)));
  with ADOCmd do
  begin
    close;
    sql.Clear;
    sql.Add('select ZhuanZhengDate,YGGL from OA_YG_DangAn where YGType=''正式'' ');
    open;
  end;
  with ADOCmd do
  begin
    first;
    while not eof do
    begin
      edit;
      FYGGLNF:=strtofloat(uppercase(formatdateTime('yyyy',ADOCmd.fieldbyname('ZhuanZhengDate').AsDateTime)));
      FGLYF:=strtofloat(uppercase(formatdateTime('MM',ADOCmd.fieldbyname('ZhuanZhengDate').AsDateTime)));
      if (FGLYF>=1) and (FGLYF<=3) then
      fieldbyname('YGGL').AsFloat:=FXTGLNF-FYGGLNF+1;
      if (FGLYF>3) and (FGLYF<=9) then
      fieldbyname('YGGL').AsFloat:=FXTGLNF-FYGGLNF+0.5;
      if (FGLYF>9) and (FGLYF<=12) then
      fieldbyname('YGGL').AsFloat:=FXTGLNF-FYGGLNF;
      next;
    end;
  end;
end;

procedure TfrmYGYPInPut_YGXZDJ.TBCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmYGYPInPut_YGXZDJ.InitData();
begin
  with ADOTemp DO
  begin
    Close;
    sql.Clear;
    sql.add('select * from OA_YG_XZFF_Main ');
    sql.Add('where XZMain='''+trim(FXZMain)+''' ');
    Open;
  end;
  if ADOTemp.IsEmpty=False then
  begin
    SSYear.Text:=trim(ADOTemp.fieldbyname('SSYear').AsString);
    SSMonth.Text:=trim(ADOTemp.fieldbyname('SSMonth').AsString);
    FFDate.datetime:=ADOTemp.fieldbyname('FFDate').AsDateTime;
    PriceUnit.Text:=trim(ADOTemp.fieldbyname('PriceUnit').asstring);
    FKTaiTou.Text:=trim(ADOTemp.fieldbyname('FKTaiTou').asstring);
    FKDept.Text:=trim(ADOTemp.fieldbyname('FKDept').asstring);
  end;
  with ADOCmd do
  begin
    close;
    sql.Clear;
    sql.Add('select *,cast(0 as decimal(18,2)) SYearGZMoney from OA_YG_XZFF where XZMain='''+Trim(FXZMain)+''' ');
    sql.add('and isnull(Chkstatus,'''')=''待提交''');
    sql.add('Order By XHNo');
    open;
  end;
  SCreateCDS20(ADOCmd,Order_Sub);
  SInitCDSData20(ADOCmd,Order_Sub);
end;

procedure TfrmYGYPInPut_YGXZDJ.FormShow(Sender: TObject);
begin
  if strtofloat(uppercase(formatdatetime('MM',SGetServerDate(ADOTemp))))=1 then
  begin
    SSYear.Text:=floattostr(strtofloat(uppercase(formatdatetime('yyyy',SGetServerDate(ADOTemp))))-1);
    SSMonth.Text:='12';
  end
  else
  begin
    SSYear.Text:=trim(uppercase(formatdatetime('yyyy',SGetServerDate(ADOTemp))));
    SSMonth.Text:=floattostr(strtofloat(uppercase(formatdatetime('MM',SGetServerDate(ADOTemp))))-1);
  end;                           
  FFDate.DateTime:=SGetServerDate(ADOTemp);
  InitData();
end;

function TfrmYGYPInPut_YGXZDJ.SaveData():Boolean;
var
  maxno:String;
begin
  Result:=False;
  try
    ADOCmd.Connection.BeginTrans;
    if trim(FXZMain)='' then
    begin
      if GetLSNo(ADOCmd,FXZMain,'FF','OA_YG_XZFF_Main',4,1)=False then
      begin
        Application.MessageBox('取主流水号失败!','提示',0);
        Exit;
      end;
    end;
    with ADOCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select * from OA_YG_XZFF_Main ');
      sql.add('where XZMain='''+trim(FXZMain)+'''');
      open;
    end;
    if ADOCmd.IsEmpty then
      ADOCmd.append
    else
      ADOCmd.edit;
    ADOCmd.fieldbyname('XZMain').asstring:=trim(FXZMain);
    ADOCmd.fieldbyname('filler').asstring:=trim(DName);
    ADOCmd.fieldbyname('filltime').asdatetime:=SGetServerDate(ADOTemp);
    RTSetsavedata(ADOCmd,'OA_YG_XZFF_Main',Panel3,0);
    ADOCmd.post;
    with Order_Sub do
    begin
      first;
      while not eof do
      begin
        if fieldbyname('XZID').AsString='' then
        begin
          if GetLSNo(ADOCmd,maxno,'XZ','OA_YG_XZFF',4,1)=False then
          begin
            Application.MessageBox('取主流水号失败!','提示',0);
            Exit;
          end;
        end
        else
        begin
          Maxno:=Order_Sub.fieldbyname('XZID').AsString;
        end;
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          SQL.Add('select * from OA_YG_XZFF where XZID='''+Trim(Maxno)+'''');
          Open;
          if isempty then
          begin
            append;
            fieldbyname('Chkstatus').asstring:='待提交';
          end
          else
          begin
            edit;
            fieldbyname('Chkstatus').asstring:=trim(Order_Sub.fieldbyname('Chkstatus').AsString);
          end;
          fieldbyname('XZID').AsString:=trim(maxno);
          fieldbyname('XZMain').asstring:=trim(FXZMain);
          fieldbyname('YGID').AsString:=trim(Order_Sub.fieldbyname('YGID').AsString);
          fieldbyname('XHNo').Value:=Order_Sub.fieldbyname('XHNo').AsFloat;
          fieldbyname('GZMoney').Value:=Order_Sub.fieldbyname('GZMoney').AsFloat;
          fieldbyname('KQJMoney').Value:=Order_Sub.fieldbyname('KQJMoney').AsFloat;
          fieldbyname('KCDMoney').Value:=Order_Sub.fieldbyname('KCDMoney').AsFloat;
          fieldbyname('KOtherMoney').Value:=Order_Sub.fieldbyname('KOtherMoney').AsFloat;
          fieldbyname('KSBMoney').Value:=Order_Sub.fieldbyname('KSBMoney').AsFloat;
          fieldbyname('KGSMoney').Value:=Order_Sub.fieldbyname('KGSMoney').AsFloat;
          fieldbyname('JLMoney').Value:=Order_Sub.fieldbyname('JLMoney').AsFloat;
          fieldbyname('KFKMeny').Value:=Order_Sub.fieldbyname('KFKMeny').AsFloat;
          fieldbyname('FFMoney').Value:=Order_Sub.fieldbyname('FFMoney').AsFloat;
          fieldbyname('JKMoney').Value:=Order_Sub.fieldbyname('JKMoney').AsFloat;
          fieldbyname('DXGZMoney').Value:=Order_Sub.fieldbyname('DXGZMoney').AsFloat;
          RTSetSaveDataCDS(ADOCmd,Tv1,Order_Sub,'OA_YG_XZFF',0);
          post;
        end;
        next;
      end;
    end;
    ADOCmd.Connection.CommitTrans;
    Result:=True;
  except;
    Result:=False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;

procedure TfrmYGYPInPut_YGXZDJ.TBSaveClick(Sender: TObject);
var
  FSFNO:String;
begin
  ToolBar1.SetFocus;
  if PriceUnit.Text='' then
  begin
    application.MessageBox('币种不能为空','提示');
    exit;
  end;
  if FKTaiTou.Text='' then
  begin
    application.MessageBox('付款抬头不能为空','提示');
    exit;
  end;
  if FKType.Text='' then
  begin
    application.MessageBox('付款方式不能为空','提示');
    exit;
  end;
  //saveDataSY();
  if SaveData() then
  begin
    Application.MessageBox('保存成功！','提示',0);
    Modalresult:=1;
  end;
end;

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

procedure TfrmYGYPInPut_YGXZDJ.FormDestroy(Sender: TObject);
begin
  FrmYGYPInPut_YGXZDJ:=nil;
end;

procedure TfrmYGYPInPut_YGXZDJ.TGZFFClick(Sender: TObject);
var FGRMoney,FGZMoney,FJBGZMoney: double;
begin
  if SSYear.Text='' then
  begin
    application.MessageBox('所属年份不能为空','提示');
    exit;
  end;
  if SSMonth.Text='' then
  begin
    application.MessageBox('所属月份不能为空','提示');
    exit;
  end;
  if FKType.Text='' then
  begin
    application.MessageBox('付款方式不能为空','提示');
    exit;
  end;
  if FKTaiTou.Text='' then
  begin
    application.MessageBox('付款抬头不能为空','提示');
    exit;
  end;
  if FKDept.Text='' then
  begin
    application.MessageBox('发放部门不能为空','提示');
    exit;
  end;
  Panel3.Enabled:=False;
  YGLiZhi();
  //JXUPDate();  //加薪更新
  //NXZZDate();  //年薪增长更新
  if Order_Sub.IsEmpty then
  FXHNO:=1;
  with ADOCmd do
  begin
    close;
    sql.Clear;
    sql.Add('select A.*,B.*,MBMoney=cast(0 as decimal(18,2)) ');
    sql.Add(',XHXJMoneyM=(select Sum(C.money) from GZList C where C.YGName=A.YGName and Year(C.CRTime)='''+trim(SSYear.Text)+''' ');
    sql.Add(' and Month(C.CRTime)='''+trim(SSMonth.Text)+''') ');
    sql.Add('from OA_YG_XZFF A ');
    sql.add('inner join OA_YG_XZFF_Main B on B.XZMain=A.XZMain ');
    sql.add('where A.FKSYSTEM='''+trim(FKTaiTou.Text)+''' ');
    sql.add('and isnull(A.FKType,'''')='''+trim(FKType.Text)+''' ');
    sql.add('and isnull(B.FKDept,'''')='''+trim(FKDept.Text)+''' ');
    if trim(SSMonth.Text)='1' then
    begin
      sql.add('and B.SSYear='''+trim(floattostr(strtofloat(SSYear.Text)-1))+''' ');
      sql.add('and B.SSMonth=''12'' ');
    end
    else
    begin
      sql.add('and B.SSYear='''+trim(SSYear.Text)+''' ');
      sql.add('and B.SSMonth='''+trim(floattostr(strtofloat(SSMonth.Text)-1))+'''');
    end;
    open;
  end;
  if ADOCmd.IsEmpty then
  begin
    with ADOCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select A.*,B.* ');
      sql.Add(',XHXJMoneyM=(select Sum(C.money) from GZList C where C.YGName=A.YGName and Year(C.CRTime)='''+trim(SSYear.Text)+''' ');
      sql.Add(' and Month(C.CRTime)='''+trim(SSMonth.Text)+''') ');
      sql.Add('from OA_YG_DangAn A ');
      sql.Add('left join OA_YG_XZFF B on B.YGName=A.YGName where LiZhiFlag=0');
      open;
    end;
  end;
  with ADOCmd do
  begin
    first;
    while not eof do
    begin
      with ADOQueryJK do
      begin
        close;
        sql.Clear;
        sql.Add('select YGName,CDFKMoney=Sum(case when DATEDIFF(mi,SYDate,convert(varchar,SBDate,8))<5 then 0 else DATEDIFF(mi,SYDate,convert(varchar,SBDate,8)) end)');
        sql.Add(',QJDay=(select Sum(SQDay) from OA_YG_CLGLSW C where C.ZWName=YGName and Year(C.SQBegDate)=NDKDate and Month(C.SQBegDate)=MDKDate and C.SQType=''请假'')');
        sql.Add(',CQDay=count(MTDay)+isnull((select Sum(SQDay) from OA_YG_CLGLSW C where C.ZWName=YGName and Year(C.SQBegDate)=NDKDate and Month(C.SQBegDate)=MDKDate and C.SQType=''补卡''),0)');
        sql.Add(' from (select A.YGName,Max(DKDate) XBDate,Min(DKDate) SBDate,convert(varchar(10),DKDate,120) MTDay,Year(DKDate) NDKDate,cast(Month(DKDate) as decimal(18,0)) MDKDate ');
        sql.Add(',SYDate=(select Top 1 convert(varchar,B.SYDate,8) from OA_YG_DangAn B where B.YGName=A.YGName)');
        sql.Add(',XBTime=(select Top 1 convert(varchar,B.XBTime,8)from OA_YG_DangAn B where B.YGName=A.YGName)');
        sql.Add(' from OA_YG_KQ A where YGName='''+trim(ADOCmd.fieldbyname('YGName').AsString)+'''');
        sql.Add(' and Year(DKDate)='''+trim(SSYear.Text)+''' and cast(Month(DKDate) as decimal(18,0))='''+trim(SSMonth.Text)+'''');
        sql.Add(' group by A.YGName,convert(varchar(10),DKDate,120),Year(DKDate),cast(Month(DKDate) as decimal(18,0))) AA');
        sql.Add(' group by YGName,NDKDate,MDKDate');
        //ShowMessage(sql.Text);
        open;
      end;
      with Order_Sub do
      begin
        append;
        fieldbyname('XHNo').AsInteger:=FXHNo;
        fieldbyname('YGName').Value:=trim(ADOCmd.fieldbyname('YGName').AsString);
        fieldbyname('DXGZMoney').Value:=ADOCmd.fieldbyname('DXGZMoney').AsFloat;
        fieldbyname('KQJMoney').Value:=ADOQueryJK.fieldbyname('QJDay').AsFloat;
        fieldbyname('KSBMoney').Value:=30.5-ADOQueryJK.fieldbyname('CQDay').AsFloat-ADOQueryJK.fieldbyname('QJDay').AsFloat;
        fieldbyname('KOtherMoney').Value:=ADOQueryJK.fieldbyname('CQDay').AsFloat;
        fieldbyname('GLFLMoney').Value:=ADOCmd.fieldbyname('GLFLMoney').AsFloat;
        fieldbyname('KGSMoney').Value:=ADOCmd.fieldbyname('KGSMoney').AsFloat;
        fieldbyname('KFKMeny').Value:=ADOCmd.fieldbyname('KFKMeny').AsFloat;
        fieldbyname('XHXJMoney').Value:=ADOCmd.fieldbyname('XHXJMoneyM').AsFloat;
        if ADOCmd.fieldbyname('MBMoney').AsFloat=0 then
        begin
          fieldbyname('GZMoney').Value:=ADOCmd.fieldbyname('GZMoney').AsFloat;
          fieldbyname('JBGZMoney').Value:=Format('%.0f',[ADOCmd.fieldbyname('GZMoney').AsFloat/30.5*ADOQueryJK.fieldbyname('CQDay').AsFloat]);
          FGZMoney:=ADOCmd.fieldbyname('GZMoney').AsFloat;
          FJBGZMoney:=StrToFloat(Format('%.0f',[ADOCmd.fieldbyname('GZMoney').AsFloat/30.5*ADOQueryJK.fieldbyname('CQDay').AsFloat]));
        end
        else
        begin
          fieldbyname('GZMoney').Value:=ADOCmd.fieldbyname('MBMoney').AsFloat;
          fieldbyname('JBGZMoney').Value:=Format('%.0f',[ADOCmd.fieldbyname('MBMoney').AsFloat/30.5*ADOQueryJK.fieldbyname('CQDay').AsFloat]);
          FGZMoney:=ADOCmd.fieldbyname('MBMoney').AsFloat;
          FJBGZMoney:=StrToFloat(Format('%.0f',[ADOCmd.fieldbyname('MBMoney').AsFloat/30.5*ADOQueryJK.fieldbyname('CQDay').AsFloat]));
        end;
        fieldbyname('KCDMoney').Value:=ADOQueryJK.fieldbyname('CDFKMoney').AsFloat;
        if FKDept.Text='绣花部' then
        begin
          if ADOCmd.fieldbyname('XHXJMoneyM').AsFloat>FGZMoney then
          begin
            fieldbyname('FFMoney').Value:=ADOCmd.fieldbyname('XHXJMoneyM').AsFloat;
          end
          else
          begin
            fieldbyname('FFMoney').Value:=FJBGZMoney-ADOQueryJK.fieldbyname('CDFKMoney').AsFloat;
          end;
        end
        else
        fieldbyname('FFMoney').Value:=FJBGZMoney-ADOQueryJK.fieldbyname('CDFKMoney').AsFloat;
        fieldbyname('FKSYSTEM').Value:=trim(FKTaiTou.Text);
        fieldbyname('XZNote').Value:=trim(ADOCmd.fieldbyname('XZNote').AsString);
        fieldbyname('FKType').Value:=trim(FKType.Text);
        post;
      end;
      FXHNo:=FXHNo+1;
      next;
    end;
  end;
  TGZFF.Enabled:=False;
end;

procedure TfrmYGYPInPut_YGXZDJ.ToolButton3Click(Sender: TObject);
var i:integer;
begin
  if Order_Sub.IsEmpty then
  begin
    FXHNo:=1;
  end
  else
  begin
    Order_Sub.Last;
    FXHNo:=Order_Sub.fieldbyname('XHNo').AsInteger+1;
  end;
  with Order_Sub do
  begin
    append;
    fieldbyname('XHNo').AsInteger:=FXHNo;
    fieldbyname('FKSYSTEM').AsString:=trim(FKTaiTou.Text);
    fieldbyname('FKType').AsString:=trim(FKType.Text);
    post;
  end;
end;

procedure TfrmYGYPInPut_YGXZDJ.ToolButton4Click(Sender: TObject);
begin
  FXHNo:=Order_Sub.fieldbyname('XHNo').AsInteger;
  with ADOQueryMain do
  begin
    close;
    sql.Clear;
    sql.Add('delete from OA_YG_XZFF where XZID='''+trim(Order_Sub.fieldbyname('XZID').AsString)+'''');
    execsql;
  end;
  Order_Sub.Delete;
  with Order_Sub do
  begin
    first;
    while not eof do
    begin
      edit;
      if fieldbyname('XHNo').AsInteger>FXHNo then
      begin
        fieldbyname('XHNo').AsInteger:=fieldbyname('XHNo').AsInteger-1;
      end;
      post;
      next;
    end;
  end;
end;

procedure TfrmYGYPInPut_YGXZDJ.v1GZMoneyPropertiesEditValueChanged(
  Sender: TObject);
var
  FName,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;
  with Order_Sub do
  begin
    Edit;
    FieldByName('FFMoney').Value:=FieldByName('GZMoney').AsFloat-FieldByName('KQJMoney').AsFloat-
                                  FieldByName('KCDMoney').AsFloat-FieldByName('KOtherMoney').AsFloat-
                                  FieldByName('KSBMoney').AsFloat-FieldByName('KGSMoney').AsFloat+
                                  FieldByName('JLMoney').AsFloat-FieldByName('KFKMeny').AsFloat-
                                  fieldbyname('JKMoney').AsFloat;
    Post;
  end;
end;

procedure TfrmYGYPInPut_YGXZDJ.v1FKSYSTEMPropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  frmZDYHelp:=TfrmZDYHelp.Create(Application);
  with frmZDYHelp do
  begin
    flag:='FKSYSTEM';
    flagname:='付款抬头';
    if ShowModal=1 then
    begin
      with Order_Sub do
      begin
        edit;
        fieldbyname('FKSYSTEM').AsString:=trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        
      end;
    end;
    free;
  end;
end;

procedure TfrmYGYPInPut_YGXZDJ.v1FKTypePropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  frmZDYHelp:=TfrmZDYHelp.Create(Application);
  with frmZDYHelp do
  begin
    flag:='FKType';
    flagname:='付款方式';
    if ShowModal=1 then
    begin
      with Order_Sub do
      begin
        edit;
        fieldbyname('FKType').AsString:=trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        
      end;
    end;
    free;
  end;
end;

procedure TfrmYGYPInPut_YGXZDJ.FKTaiTouBtnClick(Sender: TObject);
begin
  frmZDYHelp:=TfrmZDYHelp.Create(Application);
  with frmZDYHelp do
  begin
    flag:='FKSYSTEM';
    flagname:='付款抬头';
    if ShowModal=1 then
    begin
      FKTaiTou.Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
    end;
    free;
  end;
end;

procedure TfrmYGYPInPut_YGXZDJ.PriceUnitBtnClick(Sender: TObject);
begin
  frmZDYHelp:=TfrmZDYHelp.Create(Application);
  with frmZDYHelp do
  begin
    flag:='priceUnit';
    flagname:='币种';
    if ShowModal=1 then
    begin
      PriceUnit.Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
    end;
    free;
  end;
end;

procedure TfrmYGYPInPut_YGXZDJ.TSYGZClick(Sender: TObject);
var FXHNO: integer;
    FGRMoney: double;
begin
  if SSYear.Text='' then
  begin
    application.MessageBox('所属年份不能为空','提示');
    exit;
  end;
  if SSMonth.Text='' then
  begin
    application.MessageBox('所属月份不能为空','提示');
    exit;
  end;
  if FKType.Text='' then
  begin
    application.MessageBox('付款方式不能为空','提示');
    exit;
  end;
  if FKTaiTou.Text='' then
  begin
    application.MessageBox('付款抬头不能为空','提示');
    exit;
  end;
  Panel3.Enabled:=False;
  FXHNO:=1;
  with ADOCmd do
  begin
    close;
    sql.Clear;
    sql.Add('select * from OA_YG_DangAn ');
    sql.add('where LiZhiFlag=0 ');
    sql.add('and FKSYSTEM='''+trim(FKTaiTou.Text)+''' ');
    sql.Add('and FHType=''年薪制'' and ZhuanZhengDate<>'''' ');
    if FKType.Text='打卡' then
    begin
      sql.add('and isnull(FKType,'''')<>''现金'' ');
    end
    else
    begin
      sql.add('and isnull(FKType,'''')<>''打卡''');
    end;
    open;
  end;
  with ADOCmd do
  begin
    first;
    while not eof do
    begin
      with Order_Sub do
      begin
        append;
        fieldbyname('XHNo').AsInteger:=FXHNo;
        fieldbyname('YGName').Value:=trim(ADOCmd.fieldbyname('YGName').AsString);
        fieldbyname('XZNo').Value:=trim(ADOCmd.fieldbyname('XZNo').AsString);
        fieldbyname('YGID').Value:=trim(ADOCmd.fieldbyname('YGID').AsString);
        fieldbyname('FKSYSTEM').Value:=trim(ADOCmd.fieldbyname('FKSYSTEM').AsString);
        fieldbyname('SYearGZMoney').Value:=ADOCmd.fieldbyname('SYearGZMoney').AsFloat;
        fieldbyname('FKType').Value:=trim(FKType.Text);
        if ADOCmd.FieldByName('SYearGZMoney').AsFloat<=50000 then
        begin
          FGRMoney:=ADOCmd.FieldByName('SYearGZMoney').AsFloat/14*2;
        end
        else
        begin
          FGRMoney:=ADOCmd.FieldByName('SYearGZMoney').AsFloat/14*0.8*2+ADOCmd.FieldByName('SYearGZMoney').AsFloat*0.2;
        end;
        FieldByName('GZMoney').AsFloat:=FGRMoney;
        post;
      end;
      FXHNo:=FXHNo+1;
      next;
    end;
  end;
end;

procedure TfrmYGYPInPut_YGXZDJ.Tv1EditKeyDown(
  Sender: TcxCustomGridTableView; AItem: TcxCustomGridTableItem;
  AEdit: TcxCustomEdit; var Key: Word; Shift: TShiftState);
begin
  inherited;
  if Key=VK_RETURN then
  begin
    if Tv1.Controller.FocusedColumnIndex=Tv1.VisibleColumnCount-1 then
    begin
      with Tv1.DataController do
      begin
        if IsEof then
        begin
          Tv1.DataController.Append;
        end else
        begin
          Tv1.DataController.MoveBy(1);
        end;
        Tv1.Controller.FocusedItemIndex:=-1;
      end;
    end;
  end;
end;

procedure TfrmYGYPInPut_YGXZDJ.Tv1NavigatorButtonsButtonClick(
  Sender: TObject; AButtonIndex: Integer; var ADone: Boolean);
begin
  ToolBar1.Refresh;
end;

procedure TfrmYGYPInPut_YGXZDJ.FKDeptBtnClick(Sender: TObject);
begin
  frmZDYHelp:=TfrmZDYHelp.Create(Application);
  with frmZDYHelp do
  begin
    flag:='FKDept';
    flagname:='付款岗位';
    if ShowModal=1 then
    begin
      FKDept.Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
    end;
    free;
  end;
end;

end.
