unit U_WorkPayInPutB;

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, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator;

type
  TfrmWorkPayInPutB = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    DataSource2: TDataSource;
    ADOZDY: TADOQuery;
    CDS_ZDY: TClientDataSet;
    ADOQuery1: TADOQuery;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ClientDataSet1: TClientDataSet;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column5: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1PRTSpec: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    v1Column8: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    v1Column22: TcxGridDBColumn;
    v1Column25: TcxGridDBColumn;
    v1Column27: TcxGridDBColumn;
    v1Column29: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    Tv1Column12: TcxGridDBColumn;
    Tv1Column13: TcxGridDBColumn;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure v1Column2PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    FXS: Integer;
    procedure InitData();
    function SaveData(): Boolean;
    { Private declarations }
  public
    PState, PCopyInt: Integer;
    FMainId, FPayType, FDate: string;

    { Public declarations }
  end;

var
  frmWorkPayInPutB: TfrmWorkPayInPutB;

implementation

uses
  U_DataLink, U_ZDYHelp, U_RTFun;

{$R *.dfm}

procedure TfrmWorkPayInPutB.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('员工工资录入B', Tv1, '财务1');
end;

procedure TfrmWorkPayInPutB.InitData();
begin
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select A.* ');
    SQL.Add(' from Worker_Pay A where ');
    SQL.Add(' Convert(varchar(7),WPDate,120)=''' + Trim(FDate) + '''');
    SQL.Add(' and isnull(DataType,'''')=''B'' ');
    Open;
  end;
  SCreateCDS20(ADOQuery1, Order_Sub);
  SInitCDSData20(ADOQuery1, Order_Sub);

end;

procedure TfrmWorkPayInPutB.FormShow(Sender: TObject);
begin
  ReadCxGrid('员工工资录入B', Tv1, '财务1');
  InitData();

end;

function TfrmWorkPayInPutB.SaveData(): Boolean;
var
  maxno, FDept, XS: string;
begin
  try
    Result := False;
    ADOCmd.Connection.BeginTrans;
    //保存主表
    Order_Sub.DisableControls;
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        if Trim(Order_Sub.fieldbyname('WPID').AsString) = '' then
        begin
          if GetLSNo(ADOCmd, maxno, 'WP', 'Worker_Pay', 4, 1) = False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取规则流水号失败!', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          maxno := Trim(Order_Sub.fieldbyname('WPID').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          SQL.Add('select * from Worker_Pay where WPID=''' + Trim(Order_Sub.fieldbyname('WPID').AsString) + '''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(Order_Sub.fieldbyname('WPID').AsString) = '' then
          begin
            Append;
          end
          else
          begin
            Edit;
          end;
          FieldByName('WPID').Value := Trim(maxno);
          FieldByName('DataType').Value := 'B';
          RTSetSaveDataCDS(ADOCmd, Tv1, Order_Sub, 'Worker_Pay', 0);
          FieldByName('WPDate').Value := Trim(FDate) + '-01';

          if Trim(FMainId) = '' then
          begin
            FieldByName('Filler').Value := Trim(DName);
          end
          else
          begin
            FieldByName('Editer').Value := Trim(DName);
            FieldByName('EditTime').Value := SGetServerDateTime(ADOTemp);
          end;
          Post;
        end;
       { with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update Worker_Pay Set YFHZMoney=ROUND(YFHZMoney,0) where WPID='''+Trim(maxno)+'''');
          ExecSQL;
        end; }
        Edit;
        FieldByName('WPId').Value := Trim(maxno);
        Next;
      end;
    end;
    Order_Sub.EnableControls;
    ADOCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end;
end;

procedure TfrmWorkPayInPutB.TBSaveClick(Sender: TObject);
begin
  ToolBar1.SetFocus;
  if Order_Sub.IsEmpty then
  begin
    Application.MessageBox('明细不能为空！', '提示', 0);
    exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！', '提示', 0);
    ModalResult := 1;
  end;

end;

procedure TfrmWorkPayInPutB.v1Column2PropertiesEditValueChanged(Sender: TObject);
var
  FName, mvalue, BasePay, BuTie, WeiSheng, ZhiLiangKou, KouKuan, YFHZMoney, MonMoney, DayQty, BuTie2, BuTie3, XiuJia, JBGZ, BXBT, CXBT, BDBT, QTBT, QQJ, CPKK, JJRDay, QTFL, KQKK: string;
  SheBao, SheBaoXSGR, SheBaoGR, JiXiao, SheBaoGS, GW: string;
begin
  FName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  mvalue := TcxTextEdit(Sender).EditingText;
  with Order_Sub do
  begin
    Edit;
    if Trim(mvalue) <> '' then
    begin
      FieldByName(FName).Value := mvalue;
    end
    else
    begin
      FieldByName(FName).Value := Null;
    end;
    Post;
  end;
  GW := Order_Sub.fieldbyname('GW').AsString;   //岗位

  BDBT := Order_Sub.fieldbyname('BDBT').AsString;   //保底补贴
  if Trim(BDBT) = '' then
    BDBT := '0';
  QTBT := Order_Sub.fieldbyname('QTBT').AsString;   //其他补贴
  if Trim(QTBT) = '' then
    QTBT := '0';
  QQJ := Order_Sub.fieldbyname('QQJ').AsString;   //全勤奖
  if Trim(QQJ) = '' then
    QQJ := '0';
  CPKK := Order_Sub.fieldbyname('CPKK').AsString;   //次品扣款
  if Trim(CPKK) = '' then
    CPKK := '0';
  KQKK := Order_Sub.fieldbyname('KQKK').AsString;   //考勤扣款
  if Trim(KQKK) = '' then
    KQKK := '0';
  JJRDay := Order_Sub.fieldbyname('JJRDay').AsString;   //节假日休息天数
  if Trim(JJRDay) = '' then
    JJRDay := '0';
  QTFL := Order_Sub.fieldbyname('QTFL').AsString;   //其他福利
  if Trim(QTFL) = '' then
    QTFL := '0';
//  JBGZ := Order_Sub.fieldbyname('JBGZ').AsString;   //加班产量工资
//  if Trim(JBGZ) = '' then
//    JBGZ := '0';
  DayQty := Order_Sub.fieldbyname('DayQty').AsString;      //上班天数
  if Trim(DayQty) = '' then
    DayQty := '0';

  if GW <> '办公室' then
  begin
    with Order_Sub do
    begin
      Edit;
      FieldByName('BXBT').Value := FloatToStr(strtofloatdef(Trim(Order_Sub.fieldbyname('BXBT').AsString), 1100));
      FieldByName('WeiSheng').Value := FloatToStr(strtofloatdef(Trim(Order_Sub.fieldbyname('WeiSheng').AsString), 1000));
      FieldByName('MonMoney').Value := FloatToStr(StrToFloat(DayQty) * 75);
      Post;
    end;
  end;
  MonMoney := Order_Sub.fieldbyname('MonMoney').AsString;   //基本工资
  if Trim(MonMoney) = '' then
    MonMoney := '0';

  WeiSheng := Order_Sub.fieldbyname('WeiSheng').AsString;    //工龄补贴
  if Trim(WeiSheng) = '' then
    WeiSheng := '0';
  BXBT := Order_Sub.fieldbyname('BXBT').AsString;   //保险补贴
  if Trim(BXBT) = '' then
    BXBT := '0';

  XiuJia := Order_Sub.fieldbyname('XiuJia').AsString;
  if Trim(XiuJia) = '' then
    XiuJia := '0';
  BuTie := Order_Sub.fieldbyname('BuTie').AsString;
  if Trim(BuTie) = '' then
    BuTie := '0';
  KouKuan := Order_Sub.fieldbyname('KouKuan').AsString;
  if Trim(KouKuan) = '' then
    KouKuan := '0';
  SheBao := Order_Sub.fieldbyname('SheBao').AsString;
  if Trim(SheBao) = '' then
    SheBao := '0';
  SheBaoXSGR := Order_Sub.fieldbyname('SheBaoXSGR').AsString;
  if Trim(SheBaoXSGR) = '' then
    SheBaoXSGR := '0';
  BuTie3 := Order_Sub.fieldbyname('BuTie3').AsString;
  if Trim(BuTie3) = '' then
    BuTie3 := '0';
  if GW = '办公室' then
  begin
    CXBT := Order_Sub.fieldbyname('CXBT').AsString;   //厂休补贴
    if Trim(CXBT) = '' then
      CXBT := '0';
  end
  else
  begin
    CXBT := FloatToStr(StrToFloat(XiuJia) * 60);
  end;
  if GW <> '办公室' then
  begin
    with Order_Sub do
    begin
      Edit;
      FieldByName('CXBT').Value := CXBT;
      Post;
    end;
  end;

  if GW <> '办公室' then
  begin
    JiXiao := Order_Sub.fieldbyname('JiXiao').AsString;
    if Trim(JiXiao) = '' then
      JiXiao := '0';
  end;
  with Order_Sub do
  begin
    Edit;
    if GW = '办公室' then
    begin
      FieldByName('BuTie2').Value := StrToFloat(XiuJia) * (StrToFloat(MonMoney) + StrToFloat(WeiSheng) + StrToFloat(BuTie)) * 1.00 / 30.5;
    end
    else
    begin
//      FieldByName('BuTie2').Value := StrToFloat(XiuJia) * (StrToFloat(JiXiao) + StrToFloat(WeiSheng) + StrToFloat(BuTie)) * 1.00 / 30.5;
    end;

    FieldByName('SheBaoGR').Value := StrToFloat(SheBao) * StrToFloat(SheBaoXSGR) * 0.01;
    FieldByName('SheBaoGS').Value := StrToFloat(SheBao) - StrToFloat(SheBao) * StrToFloat(SheBaoXSGR) * 0.01;
    Post;
  end;
  BuTie2 := Order_Sub.fieldbyname('BuTie2').AsString;
  if Trim(BuTie2) = '' then
    BuTie2 := '0';
  SheBaoGR := Order_Sub.fieldbyname('SheBaoGR').AsString;
  if Trim(SheBaoGR) = '' then
    SheBaoGR := '0';
  SheBaoGS := Order_Sub.fieldbyname('SheBaoGS').AsString;
  if Trim(SheBaoGS) = '' then
    SheBaoGS := '0';

  if GW = '办公室' then
  begin
    JiXiao := FloatToStr(StrToFloat(MonMoney) + StrToFloat(WeiSheng) + StrToFloat(BuTie) + StrToFloat(SheBaoGS));
  end;
//  else
//  begin
//    JiXiao := Order_Sub.fieldbyname('JiXiao').AsString;
//    if Trim(JiXiao) = '' then
//      JiXiao := '0';
//  end;
//  BasePay := FloatToStr((StrToFloat(MonMoney) + StrToFloat(WeiSheng) + StrToFloat(BuTie)) * 1.00 * StrToFloat(DayQty) / 30.5 + StrToFloat(BuTie2) + StrToFloat(BuTie3) - StrToFloat(SheBaoGR));

  if GW = '办公室' then                                    //保险补贴1100
  begin                                //  基本  75*天数     工龄福利  1000         岗位补贴                     上班天数            节假日                休假天数
    BasePay := FloatToStr((StrToFloat(MonMoney) + StrToFloat(WeiSheng) + StrToFloat(BuTie)) / 30.5 * (StrToFloat(DayQty) + StrToFloat(JJRDay) + StrToFloat(XiuJia)));
     //应发   加班产量=应发-基本*75- 工龄福利  1000 - 保险补贴1100
  end
  else
  begin
//    BasePay := FloatToStr((StrToFloat(JiXiao) + StrToFloat(WeiSheng) + StrToFloat(BuTie)) / 30.5 * (StrToFloat(DayQty) + StrToFloat(JJRDay)) + StrToFloat(CXBT));
    BasePay := FloatToStr(StrToFloat(JiXiao) / 30.5 * StrToFloat(DayQty) + StrToFloat(CXBT));
  end;

  if GW <> '办公室' then
  begin
    JBGZ := FloatToStr(StrToFloat(BasePay) - StrToFloat(DayQty) * 75 - StrToFloat(WeiSheng) - StrToFloat(BXBT));
  end;


  //  YFHZMoney := FloatToStr(StrToFloat(BasePay) - StrToFloat(KouKuan));     

  if GW = '办公室' then
  begin
    YFHZMoney := FloatToStr(StrToFloat(BasePay) + StrToFloat(CXBT) + StrToFloat(BDBT) + StrToFloat(QQJ) + StrToFloat(QTBT) - StrToFloat(CPKK) - StrToFloat(KQKK) - StrToFloat(SheBaoGR));    //+ StrToFloat(BuTie2)
  end
  else
  begin
    YFHZMoney := FloatToStr(StrToFloat(BasePay) + StrToFloat(BDBT)  + StrToFloat(QQJ) + StrToFloat(QTBT) - StrToFloat(CPKK) - StrToFloat(KQKK) - StrToFloat(SheBaoGR));
  end;

//  YFHZMoney := FloatToStr(StrToFloat(BasePay) + StrToFloat(CXBT) + StrToFloat(BDBT) + StrToFloat(QTBT) + StrToFloat(QQJ) - StrToFloat(CPKK));
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select YFHZMoney=ROUND(:YFHZMoney,0)');
    Parameters.ParamByName('YFHZMoney').Value := YFHZMoney;
    Open;
  end;
  YFHZMoney := Trim(ADOTemp.fieldbyname('YFHZMoney').AsString);
  with Order_Sub do
  begin
    Edit;
    FieldByName('BasePay').Value := BasePay;
    FieldByName('YFHZMoney').Value := YFHZMoney;
    FieldByName('JiXiao').Value := JiXiao;
//    FieldByName('JiXiao').Value := StrToFloat(MonMoney) + StrToFloat(WeiSheng) + StrToFloat(BuTie) + StrToFloat(SheBaoGS);
    if GW <> '办公室' then
    begin
      FieldByName('JBGZ').Value := JBGZ;
    end;
    Post;
  end;
end;

procedure TfrmWorkPayInPutB.ToolButton1Click(Sender: TObject);
var
  i: Integer;
begin
  CopyAddRow(Tv1, Order_Sub);
  i := Order_Sub.RecordCount;
  with Order_Sub do
  begin
    Edit;
    FieldByName('XHInt').Value := i;
    FieldByName('Worker').Value := null;
    FieldByName('XiuJia').Value := null;
    FieldByName('QingJia').Value := null;
    FieldByName('DayQty').Value := null;
    FieldByName('BuTie2').Value := null;
    FieldByName('BasePay').Value := null;
    FieldByName('KouKuan').Value := null;
    FieldByName('YFHZMoney').Value := null;
    FieldByName('Note').Value := null;
    Post;
  end;
end;

procedure TfrmWorkPayInPutB.ToolButton2Click(Sender: TObject);
begin
  if Order_Sub.IsEmpty then
    Exit;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  with ADOCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete Worker_Pay where WPID=''' + Trim(Order_Sub.fieldbyname('WPID').AsString) + '''');
    ExecSQL;
  end;
  Order_Sub.Delete;
end;

procedure TfrmWorkPayInPutB.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  WriteCxGrid('员工工资录入', Tv1, '财务');
end;

end.

