unit U_WorkDeptFeeChkList_CX;

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, IdBaseComponent, IdComponent,
  IdTCPConnection, IdTCPClient, IdFTP,jpeg;

type
  TfrmWorkDeptFeeChkList_CX = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    Order_Sub: TClientDataSet;
    ToolButton3: TToolButton;
    cxGridPopupMenu2: TcxGridPopupMenu;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column5: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Panel1: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    v1Column7: TcxGridDBColumn;
    v1Note: TcxGridDBColumn;
    Worker: TEdit;
    Label6: TLabel;
    v1Column17: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    Label3: TLabel;
    Dept: TEdit;
    ToolButton2: TToolButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    TFKCX: TToolButton;
    v1Column10: TcxGridDBColumn;
    CDS_Chker: TClientDataSet;
    Label9: TLabel;
    ItemName: TEdit;
    v1WFType: TcxGridDBColumn;
    v1ItemName: TcxGridDBColumn;
    v1Person4: TcxGridDBColumn;
    v1ZhaiYao: TcxGridDBColumn;
    v1Person1: TcxGridDBColumn;
    v1YuJieMoney: TcxGridDBColumn;
    DataSource2: TDataSource;
    TFK: TToolButton;
    cxTabControl1: TcxTabControl;
    v1ChkNote: TcxGridDBColumn;
    cxSplitter1: TcxSplitter;
    TDSFK: TToolButton;
    v1SumFKMoney: TcxGridDBColumn;
    TSel: TToolButton;
    TPrint: TToolButton;
    CDS_PRT: TClientDataSet;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    V2SumBXHZMoney: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    v1CWChktime: TcxGridDBColumn;
    V2SDefNote: TcxGridDBColumn;
    Label4: TLabel;
    Person4: TEdit;
    Label5: TLabel;
    Label7: TLabel;
    ADOQueryHZ: TADOQuery;
    IdFTP1: TIdFTP;
    ODPat: TOpenDialog;
    SaveDialog1: TSaveDialog;
    Panel3: TPanel;
    Image1: TImage;
    TFKNO: TToolButton;
    v1Huilv: TcxGridDBColumn;
    v1HZBBMoney: TcxGridDBColumn;
    v1BZUnit: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure cxPageControl1Change(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure WorkerChange(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure TFKCXClick(Sender: TObject);
    procedure TFKClick(Sender: TObject);
    procedure v1Column8PropertiesEditValueChanged(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure TSelClick(Sender: TObject);
    procedure TDSFKClick(Sender: TObject);
    procedure v1SselPropertiesEditValueChanged(Sender: TObject);
    procedure TPrintClick(Sender: TObject);
    procedure v1ChkNotePropertiesEditValueChanged(Sender: TObject);
    procedure TFKNOClick(Sender: TObject);
    procedure v1HuilvPropertiesEditValueChanged(Sender: TObject);
  private
    FInt,PFInt:Integer;
    canshu1,canshu2:string;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    procedure InitGridWsql(fsj:string);
    function  SaveData():Boolean;
    procedure InitImage();
    procedure FYBXMoney();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmWorkDeptFeeChkList_CX: TfrmWorkDeptFeeChkList_CX;

implementation
uses
  U_DataLink,U_RTFun,U_ModuleNote,U_WorkerDeptFeeInPut, U_ZDYHelpSel;

{$R *.dfm}

procedure TfrmWorkDeptFeeChkList_CX.FYBXMoney();
var maxno,maxSubNo:string;
begin
  if trim(order_Main.FieldByName('ItemName').AsString)='用款申请单' then
  begin
    with ADOQueryMain do
    begin
      close;
      sql.Clear;
      sql.add('select * from Worker_Fee_Main A ');
      sql.add('inner join Worker_Fee_Sub B on B.WFID=A.WFID ');
      sql.add('where A.WFID='''+Order_Main.fieldbyname('WFID').AsString+''' ');
      sql.add('and A.ItemName=''用款申请单'' ');
      open;
    end;
    if GetLSNo(ADOQueryMain,maxno,'WF','Worker_Fee_Main',3,1)=False then
    begin
      Application.MessageBox('取费用报销主流水号失败!','提示',0);
      Exit;
    end;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from Worker_Fee_Main where WFId='''+Trim(maxno)+'''');
      Open;
    end;
    with ADOQueryMain do
    begin
      Append;
      FieldByName('WFId').Value:=Trim(maxno);
      RTSetsavedata(ADOQueryMain,'Worker_Fee_Main',Panel1,0);
      FieldByName('HZStatus').Value:='待送审';
      FieldByName('Filler').Value:=Trim(DName);
      FieldByName('FillCode').Value:=Trim(DCode);
      FieldByName('FillerCode').Value:=Trim(DCode);
      Post;
    end;
    ///保存子表
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        if Trim(Order_Sub.fieldbyname('WSId').AsString)='' then
        begin
          if GetLSNo(ADOQueryMain,maxSubNo,'WS','Worker_Fee_Sub',4,1)=False then
          begin
            Application.MessageBox('生成费用报销子表流水号异常！','提示',0);
            exit;
          end;
        end else
        begin
          maxSubNo:=Trim(Order_Sub.fieldbyname('WSId').AsString);
        end;
        with ADOQueryMain do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from Worker_Fee_Sub where ');
          sql.Add('  WSId='''+Trim(maxSubNo)+'''');
          Open;
        end;
        with ADOQueryMain do
        begin
          if Trim(Order_Sub.fieldbyname('WSId').AsString)='' then
            Append
          else
            Edit;
          FieldByName('WFID').Value:=Trim(maxno);
          FieldByName('WSId').Value:=Trim(maxSubNo);
          RTSetSaveDataCDS(ADOQueryMain,Tv1,Order_Sub,'Worker_Fee_Sub',0);
          Post;
        end;
        Order_Sub.Edit;
        Order_Sub.FieldByName('WSId').Value:=Trim(maxSubNo);
        Next;
      end;
    end;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      sql.Add('Update Worker_Fee_Main Set HZMoney=');
      sql.Add(' (select isnull(Sum(isnull(Money,0)),0) from Worker_Fee_Sub A where A.WFID=Worker_Fee_Main.WFID)');
      sql.Add(' where WFID='''+Trim(maxno)+'''');
      ExecSQL;
    end;
  end;
end;

procedure TfrmWorkDeptFeeChkList_CX.InitImage();
var
  jpg:TJpegImage;
  myStream:TADOBlobStream;
begin
  if Order_Main.IsEmpty then Exit;
  if Trim(Order_Main.fieldbyname('WFID').AsString)='' then Exit;
  //if cxPageControl1.ActivePageIndex=6 then
  begin
    Image1.Picture.Assign(nil);
    try
      with ADOQueryTemp do
      begin
        close;
        sql.Clear;
        sql.Add(' select * from TP_File A where A.WBID='''+Trim(Order_Main.fieldbyname('WFID').AsString)+'''');
        sql.Add(' and TFType=''凭证'' ');
        open;
        if RecordCount>0 then
        begin
          if trim(ADOQueryTemp.fieldbyname('FilesOther').AsString)<>'' then
          begin
            myStream:=tadoblobstream.Create(tblobfield(ADOQueryTemp.fieldbyname('FilesOther')),bmread);
            if myStream=nil then exit;
            jpg:=TJPEGImage.Create;
            jpg.LoadFromStream(myStream);
            Image1.Picture.Assign(jpg);
          end;
        end;
      end;
    finally
      jpg.Free;
      myStream.Free;
    end;
  end;
end;

procedure TfrmWorkDeptFeeChkList_CX.FormDestroy(Sender: TObject);
begin
  frmWorkDeptFeeChkList_CX:=nil;
end;

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

procedure TfrmWorkDeptFeeChkList_CX.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('费用报销查询',Tv1,'费用管理');
end;

procedure TfrmWorkDeptFeeChkList_CX.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add(' select isnull(A.Huilv,1) HuiLV,A.*,');
      sql.add(' HZBBMoney=(A.HZMoney*isnull(A.Huilv,1)),');
      sql.Add(' BZUnit=(select Top 1 B.PriceUnit from Worker_Fee_Sub B where B.WFID=A.WFID), ');
      sql.add(' SumFKMoney=(select isnull(Sum(B.FKMoney),0) from Worker_Fee_FK B where B.BXID=A.WFID)');
      SQL.Add(' from Worker_Fee_Main A ');
      sql.Add(' where A.HZStatus=''审核通过''');
      if cxTabControl1.TabIndex<>0 then
      begin
        sql.Add(' and  A.WFDate>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.Date))+'''');
        sql.Add(' and A.WFDate<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.Date+1))+'''');
      end;
      if trim(DParameters1)<>'' then
      begin
        sql.add('and A.WFType='''+trim(DParameters1)+''' ');
      end;
      {if trim(DParameters1)='对私业务' then
      begin
        sql.add('and A.ItemName=''费用报销单''');
      end;}
      if cxTabControl1.TabIndex=0 then
      begin
        sql.Add(' and isnull(A.CWChkStatus,'''')='''' ');
      end else
      if cxTabControl1.TabIndex=1 then
      begin
        sql.Add(' and isnull(A.CWChkStatus,'''')=''已付款'' ');
      end;
      if cxTabControl1.TabIndex=2 then
      begin
        sql.Add(' and isnull(A.CWChkStatus,'''')=''作废'' ');
      end;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
    if trim(DParameters1)='对私业务' then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('select AA.*,SDefFloat=cast(0 as decimal(18,2)),SumBXHZMoney=isnull(AA.SumYUJieMoney,0)-isnull(AA.SumFKMoney,0) from ');
        sql.add('(select A.ItemName,A.Worker,A.WFType,A.Dept,Sum(A.YUJieMoney) SumYUJieMoney,');
        {sql.add(' SumYUJieMoney=(select Sum(C.YuJieMoney) from Worker_Fee_Main C where C.Worker=A.Worker and C.ItemName=A.ItemName ');
        sql.add(' and C.ItemName<>''费用报销单'' ),');}
        sql.add(' SumFKMoney=(select Sum(B.FKMoney) from Worker_Fee_FK B where B.FKName=A.Worker and B.LJID=A.ItemName)');
        SQL.Add(' from Worker_Fee_Main A ');
        SQL.Add(' where A.ItemName<>''费用报销单'' ');
        sql.add(' and A.WFType=''对私业务''');
        sql.Add(' and A.Worker='''+trim(Order_Main.fieldbyname('Worker').AsString)+'''');
        sql.add(' and A.CWChkStatus=''已付款''');
        sql.add(' group by A.ItemName,A.Worker,A.WFType,A.Dept) AA');
        Open;
      end;
      SCreateCDS20(ADOQueryCmd,Order_Sub);
      SInitCDSData20(ADOQueryCmd,Order_Sub);
      with ADOQueryHZ do
      begin
        close;
        sql.Clear;
        sql.Add('select SumJYMoney=isnull(AA.XJMoney,0)-isnull(AA.SumFKMoney,0) From (');
        sql.add('select XJMoney=(select sum(A.LYMoney) from Worker_Fee_LK A Where A.ChkStatus=''1''),');
        sql.add('SumFKMoney=Sum(B.YuJieMoney) from Worker_Fee_Main B ');
        sql.add('where B.CWChkStatus=''已付款'' and B.WFType=''对私业务'' and isnull(B.ZuoFeiFlag,'''')='''') AA');
        open;
      end;
      label7.Caption:=ADOQueryHZ.fieldbyname('SumJYMoney').AsString;
    end;
  finally;
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmWorkDeptFeeChkList_CX.InitForm();
begin
  endDate.Date:=SGetServerDate(ADOQueryTemp);
  BegDate.Date:=EndDate.Date-7;
  cxTabControl1.TabIndex:=0;
  if trim(DParameters1)='' then
  begin
    TFK.Visible:=False;
    TFKCX.Visible:=False;
    TDSFK.Visible:=False;
    cxGrid2.Visible:=False;
    TFKNo.Visible:=False;
  end
  else
  begin
    if trim(DParameters1)='对私业务' then
    begin
      cxSplitter1.Visible:=True;
      cxGrid2.Visible:=True;
      TDSFK.Visible:=True;
      Tfk.Visible:=False;
      v1SumFKMoney.Visible:=True;
      cxGrid1.Align:=alTop;
      Label5.Visible:=True;
      label7.Visible:=True;
    end
    else
    begin
      cxSplitter1.Visible:=False;
      cxGrid2.Visible:=False;
      TDSFK.Visible:=False;
      Tfk.Visible:=True;
      v1SumFKMoney.Visible:=False;
      Label5.Visible:=False;
      label7.Visible:=False;
    end;
    TFKCX.Visible:=True;
  end;
  InitGrid();
end;

function TfrmWorkDeptFeeChkList_CX.DelData():Boolean;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete Worker_Fee_FenTan  where WFId='''+Trim(Order_Main.fieldbyname('WFId').AsString)+'''');
      sql.Add('delete Worker_Fee_Main  where WFId='''+Trim(Order_Main.fieldbyname('WFId').AsString)+'''');
      sql.Add('delete Worker_Fee_Sub  where WFId='''+Trim(Order_Main.fieldbyname('WFId').AsString)+'''');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=False;
    Application.MessageBox('数据删除异常！','提示',0);
  end;
end;

procedure TfrmWorkDeptFeeChkList_CX.TBExportClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  TcxGridToExcel('费用报销单',cxGrid1);
end;

procedure TfrmWorkDeptFeeChkList_CX.TBRafreshClick(Sender: TObject);
begin
  ToolBar1.SetFocus;
  InitGrid();
end;

procedure TfrmWorkDeptFeeChkList_CX.FormShow(Sender: TObject);
begin
  ReadCxGrid('费用报销查询',Tv1,'费用管理');
  InitForm();
end;

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

procedure TfrmWorkDeptFeeChkList_CX.ToolButton3Click(Sender: TObject);
begin
  try
    frmModuleNote:=TfrmModuleNote.Create(Application);
    with frmModuleNote do
    begin
      flag:='费用报销单';
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmModuleNote.Free;
  end;
end;

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

procedure TfrmWorkDeptFeeChkList_CX.InitGridWsql(fsj:string);
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select A.*,FS.*,HJMoney=FS.YunFee+FS.MaTouFee+FS.YangBanFee+FS.OtherFee ');
      SQL.Add(' from JYOrderKP_Fee A inner join JYOrderKP_Fee_Sub FS on A.FMID=FS.FMID');
      sql.Add(' where 1=1');
      if Trim(canshu1)<>'高权限' then
      begin
        sql.Add(' and A.Filler='''+Trim(DName)+'''');
      end;
      sql.Add(fsj);
      sql.Add(' order by A.FPNo');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;
function TfrmWorkDeptFeeChkList_CX.SaveData():Boolean;
var
  maxno,maxSubNo:String;
begin
  try
    Result:=False;
    ADOQueryCmd.Connection.BeginTrans;
    //保存主表
    if Trim(Order_Main.fieldbyname('MRID').AsString)='' then
    begin
      if GetLSNo(ADOQueryCmd,maxno,'MR','Module_Prompt_Result',4,1)=False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取工作子流水号失败!','提示',0);
        Exit;
      end;
    end else
    begin
      maxno:=Trim(Order_Main.fieldbyname('MRID').AsString);
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from Module_Prompt_Result where MRId='''+Trim(Order_Main.fieldbyname('MRID').AsString)+'''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if Trim(Order_Main.fieldbyname('MRID').AsString)='' then
      begin
        Append;
      end
      else begin
        Edit;
      end;
      FieldByName('MPId').Value:=Trim(Order_Main.fieldbyname('MPId').AsString);
      FieldByName('MRId').Value:=Trim(maxno);
      FieldByName('MRStatus').Value:='已处理';
      FieldByName('MRDate').Value:=SGetServerDateTime(ADOQueryTemp);
      if Trim(Order_Main.fieldbyname('MRId').AsString)='' then
      begin
        FieldByName('Filler').Value:=Trim(DName);
      end else
      begin
        FieldByName('Editer').Value:=Trim(DName);
        FieldByName('EditTime').Value:=SGetServerDateTime(ADOQueryTemp);
      end;
      Post;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;

procedure TfrmWorkDeptFeeChkList_CX.cxTabControl1Change(Sender: TObject);
begin
  if cxTabControl1.TabIndex<>0 then
  begin
    v1YuJieMoney.Options.Editing:=False;
    v1ChkNote.Options.Editing:=False;
    V2SDefNote.options.Editing:=False;
  end
  else
  begin
    v1YuJieMoney.Options.Editing:=True;
    v1ChkNote.Options.Editing:=True;
    V2SDefNote.options.Editing:=True;
  end;
  InitGrid();
end;

procedure TfrmWorkDeptFeeChkList_CX.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 TfrmWorkDeptFeeChkList_CX.ToolButton2Click(Sender: TObject);
begin
  if ADOQueryMain.Active=False then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
  if trim(DParameters1)='对私业务' then
  begin
    SDofilter(ADOQueryCmd,SGetFilters(Panel1,1,2));
    SCreateCDS20(ADOQueryCmd,Order_Sub);
    SInitCDSData20(ADOQueryCmd,Order_Sub);
  end;
end;

procedure TfrmWorkDeptFeeChkList_CX.TFKCXClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  if (cxTabControl1.TabIndex<>1) and (cxTabControl1.TabIndex<>2) then Exit;
  {if Order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;}
  if Application.MessageBox('确定要执行此操作吗？','提示',32+4)<>IDYES then Exit;
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.add('update Worker_Fee_Main set CWChkStatus='''',CWChker='''',CWChktime=NULL ');
    sql.add(',YuJieMoney=Null,ChkNote='''' ');
    sql.add('where WFID='''+Order_Main.fieldbyname('WFID').AsString+'''');
    execsql;
  end;
  if trim(DParameters1)='对私业务' then
  begin
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.add('delete from Worker_Fee_FK ');
      sql.add('where BXID='''+Order_Main.fieldbyname('WFID').AsString+'''');
      execsql;
    end;
  end;
  initGrid();
end;

procedure TfrmWorkDeptFeeChkList_CX.TFKClick(Sender: TObject);
begin
  ToolBar1.SetFocus;
  if Order_Main.IsEmpty then Exit;
  if cxTabControl1.TabIndex<>0 then Exit;
  if Application.MessageBox('确定要执行此操作吗？','提示',32+4)<>IDYES then Exit;
  with Order_Main do
  begin
    {if fieldbyname('YuJieMoney').AsFloat<>fieldbyname('HZMoney').AsFloat then
    begin
      if Order_Main.fieldbyname('ChkNote').AsString='' then
      begin
        application.MessageBox('报销申请金额与付款金额不相同时,付款备注不能为空','提示');
        exit;
      end;
    end;}
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.add('update Worker_Fee_Main set CWChkStatus=''已付款'',CWChker='''+trim(DName)+''',CWChktime=getDate() ');
      if trim(Order_Main.fieldbyname('YujieMoney').AsString)='' then
      sql.add(',YuJieMoney='''+trim(Order_Main.fieldbyname('HZMoney').asstring)+'''')
      else
      sql.add(',YuJieMoney='''+trim(Order_Main.fieldbyname('YuJieMoney').AsString)+'''');
      sql.add(',ChkNote='''+trim(Order_Main.fieldbyname('ChkNote').AsString)+''' ');
      sql.add('where WFID='''+Order_Main.fieldbyname('WFID').AsString+'''');
      execsql;
    end;
  end;
  TBRafresh.Click;
end;

procedure TfrmWorkDeptFeeChkList_CX.v1Column8PropertiesEditValueChanged(
  Sender: TObject);
var FMoney,FWFID: string;
begin
  FMoney:='0';
  FWFID:=Trim(Order_Main.fieldbyname('WFID').AsString);
  with Order_Main do
  begin
    first;
    DisableControls;
    while not eof do
    begin
      if FieldByName('Ssel').AsBoolean=True then
      begin
        FMoney:=floattoStr(strtofloat(FMoney)+FieldByName('HZMoney').AsFloat);
      end;

      next;
    end;
    EnableControls;
  end;
  order_main.Locate('WFID',FWFID,[]);
  label2.Caption:=FMoney;
end;

procedure TfrmWorkDeptFeeChkList_CX.Tv1CellDblClick(
  Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  if trim(DParameters1)='对私业务' then
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select AA.*,SumBXHZMoney=isnull(AA.SumYUJieMoney,0)-isnull(AA.SumFKMoney,0) from ');
      sql.add('(select A.ItemName,A.Worker,A.WFType,A.Dept,Sum(A.YUJieMoney) SumYUJieMoney,');
      sql.add(' SumFKMoney=(select Sum(B.FKMoney) from Worker_Fee_FK B where B.FKName=A.Worker and B.LJID=A.ItemName)');
      SQL.Add(' from Worker_Fee_Main A ');
      SQL.Add(' where A.ItemName<>''费用报销单'' ');
      sql.add(' and A.WFType=''对私业务''');
      sql.Add(' and A.Worker='''+trim(Order_Main.fieldbyname('Worker').AsString)+'''');
      sql.add(' and A.CWChkStatus=''已付款''');
      sql.add(' group by A.ItemName,A.Worker,A.WFType,A.Dept) AA');
      Open;
    end;
    SCreateCDS20(ADOQueryCmd,Order_Sub);
    SInitCDSData20(ADOQueryCmd,Order_Sub);
  end;
  InitImage();
  {try
    frmWorkerDeptFeeInPut:=TfrmWorkerDeptFeeInPut.Create(Application);
    with frmWorkerDeptFeeInPut do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('WFID').AsString);
      TBSave.Visible:=False;
      ToolBar2.Visible:=False;
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmWorkerDeptFeeInPut.Free;
  end; }
end;

procedure TfrmWorkDeptFeeChkList_CX.TSelClick(Sender: TObject);
begin
  try
    frmWorkerDeptFeeInPut:=TfrmWorkerDeptFeeInPut.Create(Application);
    with frmWorkerDeptFeeInPut do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('WFID').AsString);
      TBSave.Visible:=False;
      ToolBar2.Visible:=False;
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmWorkerDeptFeeInPut.Free;
  end;
end;

procedure TfrmWorkDeptFeeChkList_CX.TDSFKClick(Sender: TObject);
var maxno: string;
begin
  ToolBar1.SetFocus;
  if Order_Main.IsEmpty then Exit;
  if cxTabControl1.TabIndex<>0 then Exit;
  if Application.MessageBox('确定要执行此操作吗？','提示',32+4)<>IDYES then Exit;
  with Order_Sub do
  begin
    first;
    while not eof do
    begin
      if fieldbyname('SDefNote').Asstring<>'' then
      begin
        if fieldbyname('SDefNote').Asfloat>fieldbyname('SumBXHZMoney').AsFloat then
        begin
          application.MessageBox('核销金额不能大于借款或用款金额','提示');
          exit;
        end;
        with ADOQueryMain do
        begin
          close;
          sql.Clear;
          sql.Add('select * from Worker_Fee_FK where BXID='''+trim(Order_Main.fieldbyname('WFId').AsString)+'''');
          open;
        end;
        if ADOQueryMain.fieldbyname('BXID').Asstring='' then
        begin
          if GetLSNo(ADOQueryCmd,maxno,'HX','Worker_Fee_FK',4,1)=False then
          begin
            Application.MessageBox('取最大号失败!','提示',0);
            Exit;
          end;
        end;
        if ADOQueryMain.IsEmpty then
        begin
          ADOQueryMain.append;
          ADOQueryMain.fieldbyname('FKID').asstring:=trim(Maxno);
          ADOQueryMain.fieldbyname('BXID').asstring:=trim(Order_Main.fieldbyname('WFId').AsString);
          ADOQueryMain.fieldbyname('FKName').asstring:=trim(Order_Main.fieldbyname('Worker').AsString);
          ADOQueryMain.fieldbyname('FKDate').value:=SGetServerDate(ADOQueryCmd);
          ADOQueryMain.fieldbyname('FKMoney').Value:=Order_Sub.fieldbyname('SDefNote').AsFloat;
          ADOQueryMain.fieldbyname('LJID').Value:=Order_Sub.fieldbyname('ItemName').AsString;
          ADOQueryMain.post;
        end;
      end;
      next;
    end;
  end;
  with ADOQueryMain do
  begin
    close;
    sql.Clear;
    sql.add('update Worker_Fee_Main set CWChkStatus=''已付款'',CWChker='''+trim(DName)+''',CWChktime=getDate() ');
    if Order_Main.FieldByName('YuJieMoney').AsString<>'' then
    begin
      sql.add(',YuJieMoney='''+trim(Order_Main.fieldbyname('YuJieMoney').AsString)+'''');
    end
    else
    begin
      sql.add(',YuJieMoney=''0''');
    end;
    sql.add(',ChkNote='''+trim(Order_Main.fieldbyname('ChkNote').AsString)+''' ');
    sql.add('where WFID='''+Order_Main.fieldbyname('WFID').AsString+'''');
    execsql;
  end;
  //FYBXMoney();
  TBRafresh.Click;
end;

procedure TfrmWorkDeptFeeChkList_CX.v1SselPropertiesEditValueChanged(
  Sender: TObject);
var FMoney,FWFID: string;
begin
  FMoney:='0';
  FWFID:=Trim(Order_Main.fieldbyname('WFID').AsString);
  with Order_Main do
  begin
    first;
    DisableControls;
    while not eof do
    begin
      if FieldByName('Ssel').AsBoolean=True then
      begin
        FMoney:=floattoStr(strtofloat(FMoney)+FieldByName('HZMoney').AsFloat);
      end;

      next;
    end;
    EnableControls;
  end;
  order_main.Locate('WFID',FWFID,[]);
  //label5.Caption:=FMoney;
end;

procedure TfrmWorkDeptFeeChkList_CX.TPrintClick(Sender: TObject);
var FPrintFile,FYCLName,FpriceUnit,FZhaiYao: string;
begin
  if Order_Main.IsEmpty then exit;
  FYCLName:='';
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add(' select A.* ');
    SQL.Add(' from Worker_Fee_Main A ');
    sql.add(' where WFID='''+trim(Order_Main.fieldbyname('WFID').asstring)+''' ');
    open;
  end;
  SCreateCDS20(ADOQueryCmd,CDS_PRT);
  SInitCDSData20(ADOQueryCmd,CDS_PRT);
  With ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add(' select A.* ');
    SQL.Add(' from Worker_Fee_Sub A ');
    sql.add(' where WFID='''+trim(Order_Main.fieldbyname('WFID').asstring)+''' ');
    open;
  end;
  with ADOQueryTemp do
  begin
    first;
    while not eof do
    begin
      if trim(FYCLName)='' then
      FYCLName:=trim(fieldbyname('YCLName').AsString)
      else
      FYCLName:=FYCLName+','+trim(fieldbyname('YCLName').AsString);
      next;
    end;
    FpriceUnit:=trim(fieldbyname('priceUnit').AsString);
    FZhaiYao:=trim(fieldbyname('ZhaiYao').AsString);
  end;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\费用报销单.rmf';
  if FileExists(fPrintFile) then
  begin
    RMVariables['YCLName']:=trim(FYCLName);
    RMVariables['priceUnit']:=trim(FpriceUnit);
    RMVariables['ZhaiYao']:=trim(FZhaiYao);
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox('没有找到报表','提示',0);
    exit;
  end;
end;

procedure TfrmWorkDeptFeeChkList_CX.v1ChkNotePropertiesEditValueChanged(
  Sender: TObject);
var mvalue: string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  with ADOQueryMain do
  begin
    close;
    sql.Clear;
    sql.add('update Worker_Fee_Main set  ');
    sql.add('ChkNote='''+trim(Mvalue)+''' ');
    sql.add('where WFID='''+Order_Main.fieldbyname('WFID').AsString+'''');
    execsql;
  end;
end;

procedure TfrmWorkDeptFeeChkList_CX.TFKNOClick(Sender: TObject);
begin
  if cxTabControl1.TabIndex<>0 then exit;
  if application.MessageBox('确定要作废吗','提示',1)=2 then exit;
  if Order_Main.IsEmpty then Exit;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('Update Worker_Fee_Main Set ');
    sql.Add(' CWChkStatus=''作废'',ZuoFeiFlag=''1''');
    sql.Add(' where WFID='''+Order_Main.fieldbyname('WFID').AsString+'''');
    ExecSQL;
  end;
  initgrid();
end;

procedure TfrmWorkDeptFeeChkList_CX.v1HuilvPropertiesEditValueChanged(
  Sender: TObject);
var mvalue: string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  if mvalue='' then
  mvalue:='0';
  with Order_Main do
  begin
    edit;
    fieldbyname('Huilv').Value:=trim(mvalue);
    fieldbyname('HZBBMoney').Value:=strtofloat(mvalue)*fieldbyname('HZMoney').AsFloat;
  end;
  with ADOQueryHZ do
  begin
    close;
    sql.Clear;
    sql.Add('update Worker_Fee_Main set Huilv='''+trim(order_Main.fieldbyname('Huilv').AsString)+''' ');
    sql.add('where WFID='''+trim(order_Main.fieldbyname('WFID').AsString)+'''');
    execsql;
  end;
end;

end.
