unit U_YGYPInPut_YGFHDJ;

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_YGFHDJ = 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;
    QSDate: TDateTimePicker;
    WCLv: TEdit;
    Label6: TLabel;
    ToolButton1: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column7: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1RuZhiDate: TcxGridDBColumn;
    v1YGGL: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1MBMoney: TcxGridDBColumn;
    v1FHType: TcxGridDBColumn;
    v1YMBMoney: TcxGridDBColumn;
    v1FHMoney: TcxGridDBColumn;
    ToolBar2: TToolBar;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    v1PriceUnit: TcxGridDBColumn;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    YMBMoney: TEdit;
    ADOQuery1: TADOQuery;
    ADOQuery2: TADOQuery;
    v1GRYearPF: TcxGridDBColumn;
    v1GRNXMoney: 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 ToolButton1Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
  private
    TPInt:Integer;
    procedure InitData();
    function  SaveData():Boolean;
    procedure GLAge();
    procedure GRYearPF();
    { Private declarations }
  public
    canshu1:String;
    PState,CopyInt:Integer;
    FMainId,FFHId,FFHYear :String;
    { Public declarations }
  end;

var
  frmYGYPInPut_YGFHDJ: TfrmYGYPInPut_YGFHDJ;
implementation
uses
  U_DataLink,U_ZDYHelp,U_RTFun,U_ZDYHelpSel,U_GRYearPFList_FHDQDJ;

{$R *.dfm}
procedure TfrmYGYPInPut_YGFHDJ.GRYearPF();
begin

end;

procedure TfrmYGYPInPut_YGFHDJ.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=''正式'' ');
    sql.add('and year(ZhuanZhengDate)<='''+trim(floattostr(FXTGLNF-2))+''' ');
    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_YGFHDJ.TBCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmYGYPInPut_YGFHDJ.InitData();
begin
  {with ADOTemp DO
  begin
    Close;
    sql.Clear;
    sql.Add('select D.* ');
    sql.add('from GRYearFH_Main D ');
    sql.Add('where D.FHID='''+trim(FFHID)+''' ');
    Open;
  end;
  if ADOTemp.IsEmpty=False then
  begin
    QSDate.DateTime:=ADOTemp.fieldbyname('QSDate').AsDateTime;
    JSDate.datetime:=ADOTemp.fieldbyname('JSDate').AsDateTime;
  end;}
  with ADOCmd do
  begin
    close;
    sql.Clear;
    sql.Add('select *,cast(0 as decimal(18,2)) GRNXMoney,cast(0 as decimal(18,2)) SYearGZMoney,cast(0 as decimal(18,2)) GRYearPF');
    sql.add(',cast(0 as decimal(18,2))  GRNXMoney');
    sql.add(' from GRYearFH_Sub where FHID='''+Trim(FFHID)+'''');
    open;
  end;
  SCreateCDS20(ADOCmd,Order_Sub);
  SInitCDSData20(ADOCmd,Order_Sub);
end;

procedure TfrmYGYPInPut_YGFHDJ.FormShow(Sender: TObject);
begin
  QSDate.DateTime:=SGetServerDate(ADOTemp);
  JSDate.DateTime:=SGetServerDate(ADOTemp);
  InitData();
end;

function TfrmYGYPInPut_YGFHDJ.SaveData():Boolean;
var
  maxno,MaxNXNo:String;
  FSNXSMoney: double;
begin
  try
    with ADOtemp do
    begin
      close;
      sql.Clear;
      sql.Add('select * from GRYearFH_Sub ');
      sql.Add('where FHID='''+trim(FFHID)+'''');
      open;
    end;
    if ADOTemp.IsEmpty=False then
    begin
      application.MessageBox('该档期已存在','提示');
      exit;
    end;
    with Order_Sub do
    begin
      first;
      while not eof do
      begin
        if order_sub.FieldByName('FHSubid').AsString='' then
        begin
          if GetLSNo(ADOCmd,maxno,'FSH','GRYearFH_Sub',4,1)=False then
          begin
            Application.MessageBox('取主流水号失败!','提示',0);
            Exit;
          end;
        end
        else
        begin
          Maxno:=Order_Sub.fieldbyname('FHSubid').AsString;
        end;
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          SQL.Add('select * from GRYearFH_Sub where FHSubid='''+Trim(Maxno)+'''');
          Open;
          if isempty then
          begin
            append;
          end
          else
          edit;
          fieldbyname('FHID').AsString:=trim(FFHID);
          fieldbyname('FHSubid').asstring:=trim(maxno);
          fieldbyname('YGID').AsString:=trim(Order_Sub.fieldbyname('YGID').AsString);
          fieldbyname('filler').asstring:=trim(DName);
          fieldbyname('filltime').value:=SGetServerDate(ADOTemp);
          fieldbyname('NFYear').Value:=uppercase(formatdateTime('yyyy',JSDate.datetime));
          fieldbyname('GRYearPF').Value:=Order_Sub.fieldbyname('GRYearPF').AsFloat;
          fieldbyname('GRNXMoney').Value:=Order_Sub.fieldbyname('GRNXMoney').AsFloat;
          RTSetSaveDataCDS(ADOCmd,Tv1,Order_Sub,'GRYearFH_Sub',0);
          post;
        end;
        with ADOTemp do
        begin
          close;
          sql.Clear;
          sql.Add('update OA_YG_DangAn set YWSNXSMoney='''+trim(ADOCmd.fieldbyname('YMBMoney').AsString)+''' ');
          sql.add('where YGName='''+trim(Order_Sub.fieldbyname('YGName').AsString)+'''');
          execsql;
        end;
        next;
      end;
    end;
    Application.MessageBox('保存成功！','提示',0);
    Modalresult:=1;
  except;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;

procedure TfrmYGYPInPut_YGFHDJ.TBSaveClick(Sender: TObject);
var
  FSFNO:String;
begin
  ToolBar1.SetFocus;
  SaveData();
end;

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

procedure TfrmYGYPInPut_YGFHDJ.FormDestroy(Sender: TObject);
begin
  FrmYGYPInPut_YGFHDJ:=nil;
end;

procedure TfrmYGYPInPut_YGFHDJ.ToolButton1Click(Sender: TObject);
var FHZMoney,FGSZMoney,FWCL,FTSMoney,FZWCL: double;
begin
  frmGRYearPFList_FHDQDJ:=TfrmGRYearPFList_FHDQDJ.create(self);
  with frmGRYearPFList_FHDQDJ do
  begin
    formid:='99';
    if showmodal=1 then
    begin
      QSDate.DateTime:=Order_Main.fieldbyname('QSDate').asdatetime;
      JSDate.Datetime:=order_Main.fieldbyname('JSDate').asdatetime;
      WCLv.Text:=order_Main.fieldbyname('WCLv').asstring;
      FFHID:=Order_Main.fieldbyname('FHID').asstring;
      FFHYear:=Order_Main.fieldbyname('FHYear').AsString;
      FGSZMoney:=order_Main.fieldbyname('YXSMoney').AsFloat;        //公司总销售金额
      GLAge();       //分红工龄
      if Order_Main.fieldbyname('WCLv').AsFloat>=100 then
      begin
        FHZMoney:=1000000;
      end
      else if Order_Main.fieldbyname('WCLv').AsFloat>=50 then
      begin
        FHZMoney:=Order_Main.fieldbyname('WCLv').AsFloat*10000;
      end
      else
      FHZMoney:=0;
      with ADOCmd do
      begin
        close;
        sql.Clear;
        sql.Add('exec P_FH_GRWCMoney '''+trim(formatdatetime('yyyy-MM-dd',QSDate.DateTime))+'''');
        sql.Add(','''+trim(formatdatetime('yyyy-MM-dd',JSDate.DateTime+1))+'''');
        open;
      end;
      with ADOCmd do
      begin
        first;
        FZWCL:=0;
        while not eof do
        begin
          if fieldbyname('FHType').AsString<>'年薪制' then
          begin
            if fieldbyname('MBMoney').AsFloat=0 then
            FZWCL:=FZWCL+0
            else
            FZWCL:=FZWCL+fieldbyname('YMBMoney').AsFloat/fieldbyname('MBMoney').AsFloat;
          end;
          next;
        end;
      end;
      with ADOCmd do
      begin
        first;
        while not eof do
        begin
          with order_Sub do
          begin
            append;
            fieldbyname('YGName').AsString:=trim(ADOCmd.fieldbyname('YGName').AsString);
            fieldbyname('Dept').Value:=trim(ADOCmd.fieldbyname('Dept').AsString);
            fieldbyname('GangWei').Value:=trim(ADOCmd.fieldbyname('GangWei').AsString);
            fieldbyname('YGID').AsString:=trim(ADOCmd.fieldbyname('YGID').AsString);
            fieldbyname('RuZhiDate').Value:=ADOCmd.fieldbyname('ZhuanZhengDate').asdatetime;
            fieldbyname('YGGL').Value:=ADOCmd.fieldbyname('YGGL').AsFloat;
            fieldbyname('GRNXMoney').Value:=ADOCmd.fieldbyname('GRNXMoney').AsFloat;
            fieldbyname('FHType').Value:=trim(ADOCmd.fieldbyname('FHType').AsString);
            fieldbyname('MBMoney').Value:=ADOCmd.fieldbyname('MBMoney').AsFloat;
            fieldbyname('PriceUnit').Value:=trim(ADOCmd.fieldbyname('PriceUnit').AsString);
            fieldbyname('YMBMoney').Value:=ADOCmd.fieldbyname('YMBMoney').AsFloat;
            fieldbyname('SYearGZMoney').Value:=ADOCmd.fieldbyname('SYearGZMoney').AsFloat;
            fieldbyname('GRYearPF').Value:=ADOCmd.fieldbyname('GRYearPF').AsFloat;
            fieldbyname('GRNXMoney').Value:=ADOCmd.fieldbyname('GRNXMoney').AsFloat;
            if ADOCmd.fieldbyname('FHType').AsString<>'个人提成' then
            begin
              if (ADOCmd.fieldbyname('ZNXMoney').AsFloat=0) or (ADOCmd.fieldbyname('ZYearPF').AsFloat=0) then
              begin
                FTSMoney:=0;
              end
              else
              begin
                FTSMoney:=ADOCmd.fieldbyname('ZNXRY').AsFloat/ADOCmd.fieldbyname('ZRY').AsFloat*FHZMoney*
                                          (ADOCmd.fieldbyname('YGGL').AsFloat/ADOCmd.fieldbyname('ZNXGL').AsFloat*0.3+
                                           ADOCmd.fieldbyname('GRNXMoney').AsFloat/ADOCmd.fieldbyname('ZNXMoney').AsFloat*0.2+
                                           ADOCmd.fieldbyname('GRYearPF').AsFloat/ADOCmd.fieldbyname('ZYearPF').asfloat*0.5);
              end;
              {if ADOCmd.fieldbyname('FHType').AsString='0.5%提成' then
              begin
                if ADOCmd.fieldbyname('YMBMoney').AsFloat*0.005*ADOCmd.fieldbyname('HuiLV').AsFloat>FTSMoney then
                begin
                  FTSMoney:=ADOCmd.fieldbyname('YMBMoney').AsFloat*0.005*ADOCmd.fieldbyname('HuiLV').AsFloat;
                end;
              end;
              if ADOCmd.fieldbyname('FHType').AsString='1%提成' then
              begin
                if ADOCmd.fieldbyname('YMBMoney').AsFloat*0.01*ADOCmd.fieldbyname('HuiLV').AsFloat>FTSMoney then
                begin
                  FTSMoney:=ADOCmd.fieldbyname('YMBMoney').AsFloat*0.01*ADOCmd.fieldbyname('HuiLV').AsFloat;
                end;
              end;
              if ADOCmd.fieldbyname('FHType').AsString='0.3/米提成' then
              begin
                if ADOCmd.fieldbyname('YMBQty').AsFloat*0.3>FTSMoney then
                begin
                  FTSMoney:=ADOCmd.fieldbyname('YMBQty').AsFloat*0.3;
                end;
              end;}
              fieldbyname('FHMoney').AsFloat:=FTSMoney;
            end
            else
            begin
              if ADOCmd.fieldbyname('MBMoney').AsFloat<>0 then
              begin
                if ADOCmd.FieldByName('PriceUnit').AsString='￥' then
                FWCL:=ADOCmd.fieldbyname('YMBMoney').AsFloat/ADOCmd.fieldbyname('HuiLV').AsFloat*2
                else
                FWCL:=ADOCmd.fieldbyname('YMBMoney').AsFloat;
                fieldbyname('FHMoney').Value:=ADOCmd.fieldbyname('ZTCRY').AsFloat/ADOCmd.fieldbyname('ZRY').AsFloat*FHZMoney*
                                          (ADOCmd.fieldbyname('YGGL').AsFloat/ADOCmd.fieldbyname('ZTCGL').AsFloat*0.3+
                                           FWCL/FGSZMoney*0.2+fieldbyname('YMBMoney').AsFloat/fieldbyname('MBMoney').AsFloat/FZWCL*0.5);
              end;
            end;
            post;
          end;
          next;
        end;
      end;
    end;
    free;
  end;
end;

procedure TfrmYGYPInPut_YGFHDJ.ToolButton3Click(Sender: TObject);
begin
  with Order_Sub do
  begin
    append;
    
    post;
  end;
end;

procedure TfrmYGYPInPut_YGFHDJ.ToolButton4Click(Sender: TObject);
begin
  Order_Sub.Delete;
end;

end.
