unit U_WorkerPayListB; //

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

type
  TfrmWorkerPayListB = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBEdit: TToolButton;
    TBDel: 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;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Panel2: TPanel;
    CSDate: TDateTimePicker;
    Label4: TLabel;
    CDS_Worker: TClientDataSet;
    ToolButton4: TToolButton;
    Worker: TEdit;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    Label8: TLabel;
    WPDateMonth: TEdit;
    TBAdd: TToolButton;
    TBUpdate: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column22: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column27: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1PRTSpec: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column29: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column25: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Panel3: TPanel;
    v1SSel: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    ToolButton1: TToolButton;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    Tv1Column12: TcxGridDBColumn;
    ToolButton2: TToolButton;
    ADOQuery1: TADOQuery;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure cxPageControl1Change(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure WorkerChange(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure TBUpdateClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
  private
    FInt, PFInt: Integer;
    canshu1, canshu2: string;
    procedure InitGrid();
    procedure InitForm();
    function SaveGZData(): Boolean;
    function DelData(): Boolean;
    procedure InitGridWsql(fsj: string);
    function SaveData(): Boolean;
    { Private declarations }
  public
    { Public declarations }
  end;

//var
 // frmWorkerPayListB: TfrmWorkerPayListB;

implementation

uses
  U_DataLink, U_RTFun, U_WorkPayInPutB, U_ZDYHelp;

{$R *.dfm}
function TfrmWorkerPayListB.SaveGZData(): Boolean;
var
  maxId, CRID, FMainId, X: string;
  FDate: TDateTime;
begin
  FMainId := '';
  X := trim(Order_Main.fieldbyname('WPDateMonth').AsString) + '-01';
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
//    sql.Add('SELECT DATEADD(ms,-3,DATEADD(mm, DATEDIFF(m,0,:FDate)+1, 0)) as dt');
    sql.Add('SELECT DATEADD(ms,-3,DATEADD(mm, DATEDIFF(m,0,''' + X + ''')+1, 0)) as dt');
//    Parameters.ParamByName('FDate').Value := trim(Order_Main.fieldbyname('WPDateMonth').AsString) + '-01';
//    ShowMessage(SQL.text);
    Open;
  end;
  FDate := ADOQueryTemp.fieldbyname('dt').AsDateTime;
//  FDate := Trim(ADOQueryTemp.fieldbyname('dt').AsString);
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from YS_Money_CR where YFID=''' + Trim(FMainId) + '''');
      Open;
    end;
    FMainId := Trim(ADOQueryCmd.fieldbyname('YFID').AsString);
    if Trim(FMainId) = '' then
    begin
      if GetLSNo(ADOQueryCmd, maxId, 'SK', 'YS_Money_CR', 4, 1) = False then
      begin
//        Result := False;
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败！', '提示', 0);
        Exit;
      end;
    end
    else
    begin
      maxId := FMainId;
    end;

    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from YS_Money_KC where FactoryName=''' + Trim(Order_Main.fieldbyname('Worker').AsString) + '''');
      sql.Add(' and KCType=''GYS'' ');
      Open;
    end;
    if ADOQueryCmd.IsEmpty = False then
    begin
      CRID := ADOQueryCmd.fieldbyname('CRID').AsString;
    end
    else
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update YS_Money_CRID set CRID=CRID+1');
        sql.Add('select * from YS_Money_CRID ');
        Open;
      end;
      CRID := ADOQueryCmd.fieldbyname('CRID').AsString;
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        sql.Add('select * from YS_Money_KC where 1<>1');
        Open;
      end;
      with ADOQueryCmd do
      begin
        Append;
        FieldByName('CRID').Value := StrToInt(CRID);
        FieldByName('FactoryNo').Value := Trim(Order_Main.fieldbyname('Worker').AsString);
        FieldByName('FactoryName').Value := Trim(Order_Main.fieldbyname('Worker').AsString);
        FieldByName('ZdyStr1').Value := '应付付';
        Post;
      end;
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from YS_Money_CR where YFID=''' + Trim(FMainId) + '''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if Trim(FMainId) = '' then
      begin
        Append;
        FieldByName('ChkStatusP').Value := '未审核';
        FieldByName('Filler').Value := Trim(DName);
      end
      else
      begin
        Edit;
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('EditTime').Value := SGetServerDateTime(ADOQueryTemp);
      end;
      FieldByName('YFID').Value := Trim(maxId);
      FieldByName('CRID').Value := StrToInt(CRID);

      FieldByName('CRType').Value := '应付款登记';
      FieldByName('CRFlag').Value := '应付付';
      FieldByName('QtyFlag').Value := 1;
      FieldByName('FactoryNo').Value := Trim(Order_Main.fieldbyname('Worker').AsString);
      FieldByName('FactoryName').Value := Trim(Order_Main.fieldbyname('Worker').AsString);
//      RTSetsavedata(ADOQueryCmd, 'YS_Money_CR', Panel1, 2);
//      if Trim(FormatDateTime('yyyy-MM', CRTime.Date)) = Trim(FormatDateTime('yyyy-MM', SKDate.Date)) then
//      begin
//        FieldByName('CRTime').Value := SKDate.Date;
//      end
//      else
//      begin
//      FieldByName('CRTime').Value := formatdateTIme('yyyy-MM-dd', StrToDate(FDate));
      FieldByName('CRTime').Value := formatdateTIme('yyyy-MM-dd', FDate);
//      end;
      FieldByName('P_CodeName').Value := '工资';
      FieldByName('ChkstatusP').Value := '未审核';
      FieldByName('JSMoney').Value := Trim(Order_Main.fieldbyname('YFHZMoney').AsString);
      FieldByName('Money').Value := Trim(Order_Main.fieldbyname('YFHZMoney').AsString);
      Post;
    end;
    FMainId := Trim(maxId);
    ADOQueryCmd.Connection.CommitTrans;
//    Application.MessageBox('操作成功!', '提示', 0);
//    Result := True;
  except
//    Result := True;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!', '提示', 0);
  end;
end;

procedure TfrmWorkerPayListB.FormDestroy(Sender: TObject);
begin
  //frmWorkerPayListB:=nil;
end;

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

procedure TfrmWorkerPayListB.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid(Copy(Self.Caption, 1, Length(Self.Caption) - 1), Tv1, '财务');
end;

procedure TfrmWorkerPayListB.InitGrid();
begin

  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add(' select A.* ');
      sql.Add(',WPDateMonth=Convert(varchar(7),WPDate,120)');
      SQL.Add(' from Worker_Pay A where ');
      SQL.Add(' Convert(varchar(7),A.WPDate,120)>=''' + Trim(FormatDateTime('yyyy-MM', BegDate.Date)) + '''');
      SQL.Add(' and Convert(varchar(7),A.WPDate,120)<=''' + Trim(FormatDateTime('yyyy-MM', EndDate.Date)) + '''');
      sql.Add(' and isnull(DataType,'''')=''B'' ');
      sql.Add(' order by WPDateMonth,XHInt');
//      ShowMessage(SQL.text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;

end;

procedure TfrmWorkerPayListB.InitForm();
begin
  CSDate.Date := StrToDate(FormatDateTime('yyyy-MM', SGetServerDateMBeg(ADOQueryTemp) - 1) + '-01');
  EndDate.Date := CSDate.Date;
  BegDate.Date := CSDate.Date;
  ReadCxGrid(Copy(Self.Caption, 1, Length(Self.Caption) - 1), Tv1, '财务');
  //InitGrid();
  if Self.Caption = '固定工资查询*' then
  begin
    TBAdd.Visible := False;
    TBEdit.Visible := False;
    TBDel.Visible := False;
    TBExport.Visible := False;
    TBUpdate.Visible := False;
    v1SSel.Visible := False;
    v1SSel.Hidden := True;
  end;
end;

procedure TfrmWorkerPayListB.TBEditClick(Sender: TObject);
var
  CSDateStr, maxno, FWPID: string;
begin
  if Order_Main.IsEmpty then
    Exit;
  CSDateStr := Trim(FormatDateTime('yyyy-MM', CSDate.Date));
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Worker_Pay where Convert(varchar(7),WPDate,120)=''' + Trim(FormatDateTime('yyyy-MM', CSDate.Date)) + '''');
    SQL.Add(' and isnull(DataType,'''')=''B'' ');
    Open;
  end;
  if ADOQueryTemp.IsEmpty then
  begin
    Application.MessageBox(PChar(CSDateStr + '月份的工资不存在，请点击新增!'), '提示', 0);
    Exit;
  end;

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Worker_Pay where WPDate=''' + Trim(Order_Main.fieldbyname('WPDate').AsString) + '''');
    sql.Add(' and isnull(ChkStatus,'''')<>'''' ');
    SQL.Add(' and isnull(DataType,'''')=''B'' ');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    Application.MessageBox('已审核通过不能编辑!', '提示', 0);
    Exit;
  end;

  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YS_Money_CR where FactoryNo=''' + Trim(Order_Main.fieldbyname('Worker').AsString) + '''');
    sql.Add(' and convert(varchar(7),CRTime,120)=''' + Trim(Order_Main.fieldbyname('WPDateMonth').AsString) + ''' ');
//    SQL.Add(' and isnull(DataType,'''')=''B'' ');
    Open;
  end;
  if ADOQuery1.IsEmpty = False then
  begin
    Application.MessageBox('已生成应付不能删除!', '提示', 0);
    Exit;
  end;

  if Application.MessageBox(PChar('确定要修改' + CSDateStr + '月份的工资吗？'), '提示', 32 + 4) <> IDYES then
    Exit;

  try
    frmWorkPayInPutB := TfrmWorkPayInPutB.Create(Application);
    with frmWorkPayInPutB do
    begin
      FDate := Trim(FormatDateTime('yyyy-MM', CSDate.DateTime));
     // FPayType:=Trim(Self.PayType10.Text);
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmWorkPayInPutB.Free;
  end;
end;

procedure TfrmWorkerPayListB.TBDelClick(Sender: TObject);
begin

  if Order_Main.IsEmpty then
    Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Worker_Pay where WPDate=''' + Trim(Order_Main.fieldbyname('WPDate').AsString) + '''');
    sql.Add(' and isnull(ChkStatus,'''')<>'''' ');
    SQL.Add(' and isnull(DataType,'''')=''B'' ');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    Application.MessageBox('已审核通过不能删除!', '提示', 0);
    Exit;
  end;

  if Order_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  if DelData() then
  begin
    //Order_Main.Delete;
  end;
end;

function TfrmWorkerPayListB.DelData(): Boolean;
begin
  try
    Result := false;
    ADOQueryCmd.Connection.BeginTrans;
    Order_Main.DisableControls;
    with Order_Main do
    begin
      First;
      while Locate('SSel', True, []) do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('delete Worker_Pay  where WPId=''' + Trim(Order_Main.fieldbyname('WPId').AsString) + '''');
          ExecSQL;
        end;
        Order_Main.delete;
      end;
    end;
    Order_Main.EnableControls;

    ADOQueryCmd.Connection.CommitTrans;
    Result := True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result := False;
    Application.MessageBox('数据删除异常！', '提示', 0);
  end;
end;

procedure TfrmWorkerPayListB.TBExportClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  TcxGridToExcel('固定工资列表', cxGrid1);
end;

procedure TfrmWorkerPayListB.TBRafreshClick(Sender: TObject);
begin
  SelOKNo(Order_Main, False);
  InitGrid();
end;

procedure TfrmWorkerPayListB.FormShow(Sender: TObject);
begin
  InitForm();
end;

procedure TfrmWorkerPayListB.cxPageControl1Change(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmWorkerPayListB.FormCreate(Sender: TObject);
begin
  canshu1 := Trim(DParameters1);
  canshu2 := Trim(DParameters2);
end;

procedure TfrmWorkerPayListB.InitGridWsql(fsj: string);
begin

end;

function TfrmWorkerPayListB.SaveData(): Boolean;
var
  maxno, FWPID, XS, FFFInt: string;
  SYDate: TDateTime;
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select SYDate=dateadd(month,-1,:FFDate) ');
    Parameters.ParamByName('FFDate').Value := CSDate.DateTime;
    Open;
  end;
  SYDate := ADOQueryTemp.fieldbyname('SYDate').AsDateTime;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select *  ');
    sql.Add(' from Worker_Pay A ');
    sql.Add(' where Convert(varchar(7),A.WPDate,120)=''' + Trim(FormatDateTime('yyyy-MM', CSDate.DateTime)) + ''' ');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    FFFInt := '有';
  end
  else
  begin
    FFFInt := '无';
  end;
  try
    Result := False;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select A.DPID,A.Dept,A.BankNo,A.ShenFenNo,A.BasePay,A.YGName,B.XHInt,B.SheBaoFee,B.BaoMiMoney,B.GeShuiFee,A.LiZhiFlag,A.LiZhiDate  ');
      sql.Add(' left join Worker_Pay B on A.ShenFenNo=B.IDNO and ');
      if Trim(FFFInt) = '有' then
        SQL.Add(' Convert(varchar(7),B.WPDate,120)=''' + Trim(FormatDateTime('yyyy-MM', CSDate.DateTime)) + '''')
      else
        SQL.Add(' Convert(varchar(7),B.WPDate,120)=''' + Trim(FormatDateTime('yyyy-MM', SYDate)) + '''');
      //SQL.Add(' where isnull(A.PayType,'''')='''+Trim(PayType10.Text)+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_Worker);
    SInitCDSData20(ADOQueryTemp, CDS_Worker);
    ADOQueryCmd.Connection.BeginTrans;
    //保存主表
    CDS_Worker.DisableControls;
    with CDS_Worker do
    begin
      First;
      while not Eof do
      begin
        if CDS_Worker.FieldByName('LiZhiFlag').AsBoolean = True then
        begin
          if Trim(FormatDateTime('yyyy-MM', CSDate.DateTime)) > Trim(FormatDateTime('yyyy-MM', CDS_Worker.FieldByName('LiZhiDate').AsDateTime)) then
          begin
            Next;
            Continue;
          end;
        end;
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from Worker_Pay where ');
          SQL.Add(' Convert(varchar(7),WPDate,120)=''' + Trim(FormatDateTime('yyyy-MM', CSDate.DateTime)) + '''');
          SQL.Add(' and IDNO=''' + Trim(CDS_Worker.fieldbyname('ShenFenNo').AsString) + '''');
          Open;
        end;
        FWPID := Trim(ADOQueryTemp.fieldbyname('WPID').AsString);
        if Trim(FWPID) = '' then
        begin
          if GetLSNo(ADOQueryCmd, maxno, 'WP', 'Worker_Pay', 4, 1) = False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取工资子流水号失败!', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          maxno := Trim(FWPID);
        end;

        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          SQL.Add('select * from Worker_Pay where WPId=''' + Trim(maxno) + '''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(FWPID) = '' then
          begin
            Append;
            FieldByName('Filler').Value := Trim(DName);
          end
          else
          begin
            Edit;
            FieldByName('Editer').Value := Trim(DName);
            FieldByName('EditTime').Value := SGetServerDateTime(ADOQueryTemp);
          end;
          FieldByName('WPId').Value := Trim(maxno);
          FieldByName('WPDate').Value := Trim(FormatDateTime('yyyy-MM', CSDate.Date)) + '-01';
          FieldByName('Worker').Value := Trim(CDS_Worker.fieldbyname('YGName').AsString);
          FieldByName('BankNo').Value := Trim(CDS_Worker.fieldbyname('BankNo').AsString);
          FieldByName('IDNO').Value := Trim(CDS_Worker.fieldbyname('ShenFenNo').AsString);
          FieldByName('BasePay').Value := CDS_Worker.fieldbyname('BasePay').Value;
          FieldByName('BaoMiMoney').Value := CDS_Worker.fieldbyname('BaoMiMoney').Value;
          FieldByName('XHInt').Value := CDS_Worker.fieldbyname('XHInt').Value;
          FieldByName('SheBaoFee').Value := CDS_Worker.fieldbyname('SheBaoFee').Value;
          FieldByName('GeShuiFee').Value := CDS_Worker.fieldbyname('GeShuiFee').Value;
          FieldByName('DPID').Value := Trim(CDS_Worker.fieldbyname('DPID').AsString);
          FieldByName('Dept').Value := Trim(CDS_Worker.fieldbyname('Dept').AsString);
          //FieldByName('PayType').Value:=Trim(PayType10.Text);
          Post;
        end;
        Next;
      end;
    end;
    CDS_Worker.EnableControls;
    //更新不是固定工资的员工的 ”应发合计”
   { with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update Worker_Pay Set YFHZMoney=(select YFMoney from  Worker_Pay_YF A ') ;
      sql.Add('                         where A.WYDate=Worker_Pay.WPDate and A.Worker=Worker_Pay.Worker)');
      sql.Add(' where exists(select * from Worker_Pay_YF B where B.WYDate=Worker_Pay.WPDate and B.Worker=Worker_Pay.Worker)');
      sql.Add(' and WPDate='''+Trim(Trim(FormatDateTime('yyyy-MM',CSDate.Date))+'-01')+'''');
      ExecSQL;
    end;   }
    //更新  应发合计金额
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('UPdate Worker_Pay Set YFHZMoney=');
      sql.Add('isnull(BasePay,0)+isnull(BaoMiMoney,0)+isnull(JiaBanMoney,0)+isnull(YFOtherMoney,0)');
      sql.Add(' where WPDate=''' + Trim(Trim(FormatDateTime('yyyy-MM', CSDate.Date)) + '-01') + '''');
      ExecSQL;
    end;
    //更新  应扣汇总金额
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('UPdate Worker_Pay Set YKHZFee=');
      sql.Add('isnull(SheBaoFee,0)+isnull(QueQinFee,0)+isnull(GeShuiFee,0)+isnull(YKOtherFee,0)');
      sql.Add(' where WPDate=''' + Trim(Trim(FormatDateTime('yyyy-MM', CSDate.Date)) + '-01') + '''');
      ExecSQL;
    end;
    //更新 实发金额
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('UPdate Worker_Pay Set SFMoney=');
      sql.Add('isnull(YFHZMoney,0)+isnull(YKHZFee,0)');
      sql.Add(' where WPDate=''' + Trim(Trim(FormatDateTime('yyyy-MM', CSDate.Date)) + '-01') + '''');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end;
end;

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

procedure TfrmWorkerPayListB.TBAddClick(Sender: TObject);
var
  CSDateStr, FWPID, maxno, CSDateStrQ: string;
begin
  CSDateStr := Trim(FormatDateTime('yyyy-MM', CSDate.Date));
  CSDateStrQ := FormatDateTime('yyyy-MM', StrToDate(CSDateStr + '-01') - 1) + '-01';
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Worker_Pay where Convert(varchar(7),WPDate,120)=''' + Trim(FormatDateTime('yyyy-MM', CSDate.Date)) + '''');
    SQL.Add(' and isnull(DataType,'''')=''B'' ');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    Application.MessageBox(PChar(CSDateStr + '月份的工资已存在，请点击修改!'), '提示', 0);
    Exit;
  end;
  if Application.MessageBox(PChar('确定要制做' + CSDateStr + '月份的工资吗？'), '提示', 32 + 4) <> IDYES then
    Exit;
  Panel3.Visible := True;
  Panel3.Refresh;
  try
    ADOQueryCmd.Connection.BeginTrans;

    //插入固定人的工资
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * ');
      sql.Add(' into #Temp1');
      sql.Add(' from Worker_Pay A where A.WPDate=''' + Trim(CSDateStrQ) + '''');
      SQL.Add(' and isnull(DataType,'''')=''B'' ');
      sql.Add(' and not exists');
      sql.Add(' (select * from Worker_Pay B where B.WPDate=''' + Trim(CSDateStr + '-01') + '''');
      sql.Add('  and B.Worker=A.Worker and B.DataType=A.DataType)');
      sql.Add(' Update #Temp1 Set ');
      sql.Add(' WPId=substring(WPId,1,12)+''' + Trim(CSDateStr) + '''');
      sql.Add(' ,WPDate=''' + Trim(CSDateStr + '-01') + '''');
      sql.Add(' Update #Temp1 Set FillTime=getdate(),Filler=''' + Trim(DName) + '''');
      sql.Add(' insert into Worker_Pay ');
      sql.Add(' select * from #Temp1');
      sql.Add(' Drop Table #Temp1  ');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Panel3.Visible := False;
    try
      frmWorkPayInPutB := TfrmWorkPayInPutB.Create(Application);
      with frmWorkPayInPutB do
      begin
        FDate := Trim(FormatDateTime('yyyy-MM', CSDate.DateTime));
       // FPayType:=Trim(Self.PayType10.Text);
        if ShowModal = 1 then
        begin

        end;
      end;
    finally
      frmWorkPayInPutB.Free;
    end;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end;

end;

procedure TfrmWorkerPayListB.ToolButton4Click(Sender: TObject);
var
  fPrintFile: string;
begin
  if Order_Main.IsEmpty then
    Exit;
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\员工工资表.rmf';
  with ADOQueryTemp do
  begin
    Filtered := False;
    Close;
    sql.Clear;
    sql.Add(' select A.*,WPDateMonth=Convert(varchar(7),WPDate,120) from Worker_Pay A');
    sql.Add(' where Convert(varchar(7),WPDate,120)=''' + Trim(Order_Main.fieldbyname('WPDateMonth').AsString) + '''');
    sql.Add(' and isnull(DataType,'''')=''B'' ');
    sql.Add(' order by XHInt');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_PRT);
  SInitCDSData20(ADOQueryTemp, CDS_PRT);
  RMXLSExport1 := TRMXLSExport.Create(RMXLSExport1);
  if FileExists(fPrintFile) then
  begin
    RMVariables['DName'] := Trim(DName);
    RM1.ShowPrintDialog := True;
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + '员工工资表'), '提示', 0);
  end;

end;

procedure TfrmWorkerPayListB.WorkerChange(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 TfrmWorkerPayListB.N1Click(Sender: TObject);
begin
  SelOKNo(Order_Main, True);
end;

procedure TfrmWorkerPayListB.N2Click(Sender: TObject);
begin
  SelOKNo(Order_Main, False);
end;

procedure TfrmWorkerPayListB.TBUpdateClick(Sender: TObject);
var
  CSDateStr, FWPID, maxno, CSDateStrQ: string;
begin
  CSDateStr := Trim(FormatDateTime('yyyy-MM', CSDate.Date));
  CSDateStrQ := FormatDateTime('yyyy-MM', StrToDate(CSDateStr + '-01') - 1) + '-01';
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Worker_Pay where Convert(varchar(7),WPDate,120)=''' + Trim(FormatDateTime('yyyy-MM', CSDate.Date)) + '''');
    SQL.Add(' and isnull(DataType,'''')=''B'' ');
    Open;
  end;
  if ADOQueryTemp.IsEmpty then
  begin
    Application.MessageBox(PChar(CSDateStr + '月份的工资不存在，不能更新!'), '提示', 0);
    Exit;
  end;
  if Application.MessageBox(PChar('确定要更新' + CSDateStr + '月份的工资吗？'), '提示', 32 + 4) <> IDYES then
    Exit;
  Panel3.Caption := '正在更新数据，请稍后。。。';
  Panel3.Visible := True;
  Panel3.Refresh;
  try

    ADOQueryCmd.Connection.BeginTrans;
    //插入固定人的工资
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * ');
      sql.Add(' into #Temp1');
      sql.Add(' from Worker_Pay A where A.WPDate=''' + Trim(CSDateStrQ) + '''');
      SQL.Add(' and isnull(DataType,'''')=''B'' ');
      sql.Add(' and not exists');
      sql.Add(' (select * from Worker_Pay B where B.WPDate=''' + Trim(CSDateStr + '-01') + '''');
      sql.Add('  and B.Worker=A.Worker and B.DataType=A.DataType)');
      sql.Add(' Update #Temp1 Set ');
      sql.Add(' WPId=substring(WPId,1,12)+''' + Trim(CSDateStr) + '''');
      sql.Add(' ,WPDate=''' + Trim(CSDateStr + '-01') + '''');
      sql.Add(' Update #Temp1 Set FillTime=getdate(),Filler=''' + Trim(DName) + '''');
      sql.Add(' insert into Worker_Pay ');
      sql.Add(' select * from #Temp1');
      sql.Add(' Drop Table #Temp1  ');
      ExecSQL;
    end;
    //更新月工资
    ADOQueryCmd.Connection.CommitTrans;
    Panel3.Visible := False;
    Application.MessageBox('更新成功！', '提示', 0);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('更新失败！', '提示', 0);
  end;

end;

procedure TfrmWorkerPayListB.ToolButton1Click(Sender: TObject);
var
  fPrintFile: string;
begin
  if Order_Main.IsEmpty then
    Exit;
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\个人月份工资.rmf';
  with ADOQueryTemp do
  begin
    Filtered := False;
    Close;
    sql.Clear;
    sql.Add(' select A.*,WPDateMonth=Convert(varchar(7),WPDate,120) from Worker_Pay A');
    sql.Add(' where Worker=''' + Trim(Order_Main.fieldbyname('Worker').AsString) + '''');
    sql.Add(' and Convert(varchar(4),WPDate,120)=''' + Trim(FormatDateTime('yyyy', Order_Main.fieldbyname('WPDate').AsDateTime)) + '''');
    sql.Add(' and isnull(DataType,'''')=''B'' ');
    sql.Add(' order by Convert(varchar(7),WPDate,120)');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_PRT);
  SInitCDSData20(ADOQueryTemp, CDS_PRT);
  RMXLSExport1 := TRMXLSExport.Create(RMXLSExport1);
  if FileExists(fPrintFile) then
  begin
    RMVariables['DName'] := Trim(DName);
    RM1.ShowPrintDialog := True;
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + '个人月份工资'), '提示', 0);
  end;

end;

procedure TfrmWorkerPayListB.ToolButton2Click(Sender: TObject);
var                                                                    
  maxId, CRID, FMainId, X: string;
  FDate: TDateTime;
begin
  if Order_Main.IsEmpty then
    Exit;
  if Order_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  try
    Order_Main.DisableControls;
//    ADOQueryCmd.Connection.BeginTrans;
    with Order_Main do
    begin
      First;
      while not Eof do
      begin
        if Order_Main.FieldByName('SSel').AsBoolean = True then
        begin
          if SaveGZData() then
          begin
            Application.MessageBox('保存成功!', '提示', 0);
            ModalResult := 1;
          end;
        end;
        Next;
      end;
    end;
//    ADOQueryCmd.Connection.CommitTrans;
    Order_Main.EnableControls;
    application.MessageBox('数据保存成功！', '提示信息');
//    Panel9.Visible := false;
//    TBRafresh.Click;
  except
//    ADOQueryCmd.Connection.RollbackTrans;
    Order_Main.EnableControls;
    application.MessageBox('数据保存失败！', '提示信息', 0);
  end;

end;

end.

