unit U_WorkDeptFeeListBXDJ;

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

type
  TfrmWorkDeptFeeListBXDJ = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBAdd: 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;
    cxGridPopupMenu2: TcxGridPopupMenu;
    cxTabControl1: TcxTabControl;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column5: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Panel1: TPanel;
    Label1: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    v1WFDate: TcxGridDBColumn;
    v1FSDate: TcxGridDBColumn;
    Worker: TEdit;
    Label6: TLabel;
    v1Column17: TcxGridDBColumn;
    ToolButton1: TToolButton;
    Label3: TLabel;
    WFNO: TEdit;
    Label4: TLabel;
    WFType: TEdit;
    ToolButton2: TToolButton;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    v1Column8: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N2: TMenuItem;
    N1: TMenuItem;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxSplitter1: TcxSplitter;
    CDS_Chker: TClientDataSet;
    CDS_Chk: TClientDataSet;
    DS_Chk: TDataSource;
    v1ItemName: TcxGridDBColumn;
    Label5: TLabel;
    ItemNameBX: TEdit;
    Label7: TLabel;
    PZType: TEdit;
    Label8: TLabel;
    INVNo: TEdit;
    TPrint: TToolButton;
    IdFTP1: TIdFTP;
    ODPat: TOpenDialog;
    SaveDialog1: TSaveDialog;
    v1WFNO: TcxGridDBColumn;
    v1WFType: TcxGridDBColumn;
    ADOQuery1: TADOQuery;
    TSC: TToolButton;
    Panel3: TPanel;
    Image1: TImage;
    ADOQuery2: TADOQuery;
    v1BZDate: TcxGridDBColumn;
    v1PerSon2: TcxGridDBColumn;
    v1Person3: TcxGridDBColumn;
    v1Person4: TcxGridDBColumn;
    v1Person1: TcxGridDBColumn;
    ADOQuery3: TADOQuery;
    TZF: TToolButton;
    TCXZF: TToolButton;
    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 TBAddClick(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 ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure v1Column8PropertiesEditValueChanged(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure TPrintClick(Sender: TObject);
    procedure TEditSQClick(Sender: TObject);
    procedure TSCClick(Sender: TObject);
    procedure Image1Click(Sender: TObject);
    procedure ReadINIFile();
    procedure TZFClick(Sender: TObject);
    procedure TCXZFClick(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 setstatus();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmWorkDeptFeeListBXDJ: TfrmWorkDeptFeeListBXDJ;

implementation
uses
  U_DataLink,U_RTFun,U_ModuleNote, U_ZDYHelpSel,
  U_ZDYHelp,U_FileUp_PZ,U_WorkerDeptFeeInPut_BXDJ;

{$R *.dfm}
procedure TfrmWorkDeptFeeListBXDJ.setstatus();
begin
  TBEdit.Visible:=False;
  TBDel.Visible:=False;
  ToolButton4.Visible:=False;
  ToolButton5.Visible:=False;
  //TEditSQ.Visible:=False;
  TZF.Visible:=False;
  TCXZF.Visible:=False;
  if cxtabcontrol1.TabIndex=0 then
  begin

  end;
  if cxtabcontrol1.TabIndex=1 then
  begin
    TBEdit.Visible:=True;
    TBDel.Visible:=True;
    ToolButton4.Visible:=true;
    TZF.Visible:=true;
  end;
  if cxtabcontrol1.TabIndex=2 then
  begin
    ToolButton5.Visible:=true;
    //TEditSQ.Visible:=true;
  end;
  if cxtabcontrol1.TabIndex=3 then
  begin
    TCXZF.Visible:=true;
  end;
end;

procedure TfrmWorkDeptFeeListBXDJ.FormDestroy(Sender: TObject);
begin
  frmWorkDeptFeeListBXDJ:=nil;
end;

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

procedure TfrmWorkDeptFeeListBXDJ.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('费用报销列表2',Tv1,'费用管理');
end;

procedure TfrmWorkDeptFeeListBXDJ.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add(' select distinct A.* ');
      SQL.Add(' from Worker_Fee_LKMain A ');
      sql.add(' inner join Worker_Fee_LKSub B on B.WFId=A.WFID');
      sql.Add(' where 1=1');
      if (cxTabControl1.TabIndex<>0) and (cxTabControl1.TabIndex<>1) 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 cxTabControl1.TabIndex=0 then
      begin
        sql.add(' and ItemName<>''费用报销单'' and isnull(A.CWChkStatus,'''')=''已付款'' and isnull(B.ZuoFeiFlag,'''')<>''1''');
      end;
      if cxTabControl1.TabIndex=1 then
      begin
        sql.add(' and ItemName=''费用报销单'' ');
        sql.Add(' and (isnull(B.MStatus,'''')=''待送审'' or isnull(B.MStatus,'''')=''审核不通过'') ');
      end;
      if cxTabControl1.TabIndex=2 then
      begin
        sql.add(' and ItemName=''费用报销单'' ');
        sql.Add(' and isnull(B.MStatus,'''')<>''待送审'' and isnull(B.MStatus,'''')<>''审核不通过''');
      end;
      if cxTabControl1.TabIndex=3 then
      begin
        sql.add(' and ItemName=''费用报销单'' ');
        sql.Add(' and isnull(B.MStatus,'''')=''作废''');
      end;
      if cxTabControl1.TabIndex=4 then
      begin
        sql.add(' and ItemName=''费用报销单'' ');
      end;
      if Trim(canshu1)<>'高权限' then
      begin
        sql.Add(' and isnull(A.FillCode,'''')='''+Trim(DCode)+'''');
      end;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from OA_Chk where 1=2');
      Open;
    end;
    SCreateCDS20(ADOQueryTemp,CDS_Chk);
    SInitCDSData20(ADOQueryTemp,CDS_Chk);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmWorkDeptFeeListBXDJ.InitForm();
begin
  endDate.Date:=SGetServerDate(ADOQueryTemp);
  BegDate.Date:=EndDate.Date-7;
  //cxTabControl1.TabIndex:=0;
  setstatus();
  InitGrid();
end;

procedure TfrmWorkDeptFeeListBXDJ.TBEditClick(Sender: TObject);
begin
  if cxTabControl1.TabIndex<>1 then Exit;
  if Order_Main.IsEmpty then Exit;
  if Trim(DCode)<>Trim(Order_Main.fieldbyname('FillCode').AsString) then
  begin
    Application.MessageBox('不能操作他人的信息!','提示',0);
    Exit;
  end;
  frmWorkerDeptFeeInPut_BXDJ:=TfrmWorkerDeptFeeInPut_BXDJ.Create(Application);
  with frmWorkerDeptFeeInPut_BXDJ do
  begin
    PState:=1;
    FMainId:=trim(Order_Main.fieldbyname('WFID').AsString);
    if ShowModal=1 then
    begin
      initGrid();
    end;
  end;
  frmWorkerDeptFeeInPut_BXDJ.Free;
end;

procedure TfrmWorkDeptFeeListBXDJ.TBDelClick(Sender: TObject);
begin
  if cxTabControl1.TabIndex<>1 then Exit;
  if Order_Main.IsEmpty then Exit;
  if Trim(DCode)<>Trim(Order_Main.fieldbyname('FillCode').AsString) then
  begin
    Application.MessageBox('不能操作他人的信息!','提示',0);
    Exit;
  end;
  if Order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Worker_Fee_LKSub where WFId='''+Trim(Order_Main.fieldbyname('WFId').AsString)+'''');
    open;
  end;
  if ADOQueryCmd.RecordCount=1 then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from Worker_Fee_LKMain where WFId='''+Trim(Order_Main.fieldbyname('BXWFId').AsString)+'''');
      open;
    end;
    if ADOQueryTemp.IsEmpty then
    begin
      application.MessageBox('没有领款信息,请作废','提示');
      exit;
    end;
  end; 
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  if DelData() then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from OA_Chk where 1=2');
      Open;
    end;
    SCreateCDS20(ADOQueryTemp,CDS_Chk);
    SInitCDSData20(ADOQueryTemp,CDS_Chk);
  end;
end;

function TfrmWorkDeptFeeListBXDJ.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_Fee_LKSub where WFID='''+Trim(Order_Main.fieldbyname('WFID').AsString)+'''');
          ExecSQL;
        end;
        with ADOQuery1 do
        begin
          close;
          sql.clear;
          sql.Add('Update Worker_Fee_LKSub set ZuoFeiFlag='''' where WFID='''+Trim(Order_Main.fieldbyname('BXWFId').AsString)+'''');
          ExecSQL;
        end;
        with ADOQuery1 do
        begin
          close;
          sql.clear;
          sql.Add('select * from Worker_Fee_LKSub where WFID='''+Trim(Order_Main.fieldbyname('WFId').AsString)+'''');
          open;
        end;
        if ADOQuery1.IsEmpty then
        begin
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('delete Worker_Fee_LKMain where WFId='''+Trim(Order_Main.fieldbyname('WFId').AsString)+'''');
            ExecSQL;
          end;
        end;
        Delete;
      end;
    end;
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=False;
    Application.MessageBox('数据删除异常！','提示',0);
  end;
end;

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

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

procedure TfrmWorkDeptFeeListBXDJ.TBAddClick(Sender: TObject);
var Maxno,MaxSubno:string;
begin
  if cxTabControl1.TabIndex=0 then
  begin
    if Order_Main.IsEmpty then exit;
    if Order_Main.Locate('SSel',True,[])=False then
    begin
      Application.MessageBox('没有选择数据!','提示',0);
      Exit;
    end;
    if application.MessageBox('是否执行此操作','提示',1)=2 then exit;
    with Order_Main do
    begin
      first;
      while not eof do
      begin
        if fieldbyname('Ssel').AsBoolean=true then
        begin
          if Trim(DCode)<>Trim(Order_Main.fieldbyname('FillCode').AsString) then
          begin
            Application.MessageBox('不能操作他人的信息!','提示',0);
            Exit;
          end;
          with ADOQueryTemp do
          begin
            close;
            sql.Clear;
            sql.Add('select * from Worker_Fee_LKMain where BXWFID='''+trim(Order_Main.fieldbyname('WFID').AsString)+'''');
            open;
          end;
          if ADOQueryTemp.isempty then
          begin
            if GetLSNo(ADOQuery2,maxno,'BX','Worker_Fee_LKMain',4,0)=False then
            begin
              Application.MessageBox('取费用报销主流水号失败!','提示',0);
              Exit;
            end;
            with ADOQuery2 do
            begin
              close;
              sql.Clear;
              sql.Add('select * from Worker_Fee_LKMain where 1=2');
              open;
            end;
            with ADOQuery2 do
            begin
              append;
              fieldbyname('WFID').AsString:=Trim(Maxno);
              fieldbyname('WFNO').AsString:=trim(Order_Main.fieldbyname('WFNO').AsString);
              fieldbyname('Worker').AsString:=trim(Order_Main.fieldbyname('Worker').AsString);
              fieldbyname('Dept').AsString:=trim(Order_Main.fieldbyname('Dept').AsString);
              fieldbyname('Note').AsString:=trim(Order_Main.fieldbyname('Note').AsString);
              fieldbyname('WFType').AsString:=trim(Order_Main.fieldbyname('WFType').AsString);
              fieldbyname('BXWFID').AsString:=trim(Order_Main.fieldbyname('WFID').AsString);
              fieldbyname('HZMoney').Value:=Order_Main.fieldbyname('HZMoney').asfloat;
              fieldbyname('ItemName').AsString:='费用报销单';
              if Order_Main.fieldbyname('WFType').AsString='对公业务' then
              fieldbyname('ItemNameBX').AsString:='公司费用单'
              else
              fieldbyname('ItemNameBX').AsString:='费用报销单';
              fieldbyname('WFDate').Value:=SGetServerDateTime(ADOQuery1);
              FieldByName('Filler').Value:=Trim(DName);
              FieldByName('FillCode').Value:=Trim(DCode);
              FieldByName('FillerCode').Value:=Trim(DCode);
              fieldbyname('filltime').Value:=SGetServerDateTime(ADOQuery1);
              //fieldbyname('FSDate').Value:=Order_Main.fieldbyname('FSDate').AsDateTime;
              //fieldbyname('BZDate').Value:=Order_Main.fieldbyname('BZDate').AsDateTime;
              fieldbyname('Person2').Value:=trim(Order_Main.fieldbyname('Person2').AsString);
              fieldbyname('Person3').Value:=trim(Order_Main.fieldbyname('Person3').AsString);
              fieldbyname('Person1').Value:=trim(Order_Main.fieldbyname('Person1').AsString);
              fieldbyname('Person4').Value:=trim(Order_Main.fieldbyname('Person4').AsString);
              post;
            end;
          end
          else
          maxno:=trim(ADOQueryTemp.fieldbyname('WFID').AsString);
          if GetLSNo(ADOQuery2,maxSubNo,'BS','Worker_Fee_LKSub',4,1)=False then
          begin
            Application.MessageBox('生成费用报销子表流水号异常！','提示',0);
            exit;
          end;
          with ADOQuery2 do
          begin
            close;
            sql.Clear;
            sql.Add('select * from Worker_Fee_LKSub where 1=2');
            open;
          end;
          with ADOQuery2 do
          begin
            Append;
            fieldbyname('WFID').AsString:=Trim(Maxno);
            fieldbyname('WSID').AsString:=trim(MaxSubno);
            fieldbyname('BXWSID').AsString:=trim(Order_Main.fieldbyname('WSID').AsString);
            fieldbyname('ZhaiYao').AsString:=trim(Order_Main.fieldbyname('ZhaiYao').AsString);
            fieldbyname('YCLName').AsString:=trim(Order_Main.fieldbyname('YCLName').AsString);
            fieldbyname('YCLQty').Value:=Order_Main.fieldbyname('YCLQty').AsFloat;
            fieldbyname('YCLUnit').AsString:=trim(Order_Main.fieldbyname('YCLUnit').AsString);
            fieldbyname('YCLPrice').Value:=Order_Main.fieldbyname('YCLPrice').AsFloat;
            fieldbyname('Money').Value:=Order_Main.fieldbyname('Money').AsFloat;
            fieldbyname('priceUnit').AsString:=trim(Order_Main.fieldbyname('priceUnit').AsString);
            fieldbyname('MStatus').AsString:='待送审';
            post;
          end;
          with ADOQuery2 do
          begin
            close;
            sql.Clear;
            sql.Add('update Worker_Fee_LKSub set ZuoFeiFlag=''1'' where WSID='''+trim(Order_Main.fieldbyname('WSID').AsString)+'''');
            execsql;
          end;
        end;
        next;
      end;
    end;
    initGrid();
  end
  else
  begin
    frmWorkerDeptFeeInPut_BXDJ:=TfrmWorkerDeptFeeInPut_BXDJ.Create(Application);
    with frmWorkerDeptFeeInPut_BXDJ do
    begin
      PState:=0;
      FMainId:='';
      if ShowModal=1 then
      begin
        initGrid();
      end;
    end;
    frmWorkerDeptFeeInPut_BXDJ.Free;
  end;
end;

procedure TfrmWorkDeptFeeListBXDJ.FormShow(Sender: TObject);
begin
  ReadCxGrid('费用报销列表2',Tv1,'费用管理');
  InitForm();
end;

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

procedure TfrmWorkDeptFeeListBXDJ.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 TfrmWorkDeptFeeListBXDJ.FormCreate(Sender: TObject);
begin
  canshu1:=Trim(DParameters1);
  canshu2:=Trim(DParameters2);
end;

procedure TfrmWorkDeptFeeListBXDJ.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 TfrmWorkDeptFeeListBXDJ.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 TfrmWorkDeptFeeListBXDJ.cxTabControl1Change(Sender: TObject);
begin
  setstatus();
  InitGrid();
end;

procedure TfrmWorkDeptFeeListBXDJ.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 TfrmWorkDeptFeeListBXDJ.ToolButton1Click(Sender: TObject);
begin
  try
    frmWorkerDeptFeeInPut_BXDJ:=TfrmWorkerDeptFeeInPut_BXDJ.Create(Application);
    with frmWorkerDeptFeeInPut_BXDJ do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('WFID').AsString);
      TBSave.Visible:=False;
      ToolBar2.Visible:=False;
      if ShowModal=1 then
      begin
        initGrid();
      end;
    end;
  finally
    frmWorkerDeptFeeInPut_BXDJ.Free;
  end;
end;

procedure TfrmWorkDeptFeeListBXDJ.ToolButton2Click(Sender: TObject);
begin
  if ADOQueryMain.Active=False then Exit;
  ToolBar1.SetFocus;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;

procedure TfrmWorkDeptFeeListBXDJ.ToolButton4Click(Sender: TObject);
var
  maxno,FCKID:string;
  FCount:Integer;
  FWFId:String;
begin
  if Order_Main.IsEmpty then Exit;
  ToolBar1.SetFocus;
  if cxTabControl1.TabIndex<>1 then Exit;
  if Order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  FWFId:=Trim(Order_Main.fieldbyname('WFID').AsString);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select Chker=Cast('''' as varchar(20))');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_Chker);
  SInitCDSData20(ADOQueryTemp,CDS_Chker);
  while CDS_Chker.IsEmpty=False do
  begin
    CDS_Chker.Delete;
  end;
  try
    frmZDYHelpSel:=TfrmZDYHelpSel.Create(Application);
    with frmZDYHelpSel do
    begin
      flag:='OAChker';
      flagname:='OA审核人';
      MainType:=Trim(DName);
      if ShowModal=1 then
      begin
        with ClientDataSet1 do
        begin
          First;
          while not Eof do
          begin
            if FieldByName('SSEl').AsBoolean=True then
            begin
              CDS_Chker.Append;
              CDS_Chker.FieldByName('Chker').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
              CDS_Chker.Post;
            end;
            Next;
          end;
        end;
        if CDS_Chker.IsEmpty then FCount:=9
      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;
  if FCount=9 then
  begin
    if CDS_Chker.IsEmpty then
    begin
      Application.MessageBox('没有选择审核人!','提示',0);
      Exit;
    end;
  end;
  if CDS_Chker.IsEmpty then Exit;
  CDS_Chker.DisableControls;
  with CDS_Chker do
  begin
    First;
    while not eof do
    begin
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from SY_User where UserName='''+Trim(CDS_Chker.fieldbyname('Chker').AsString)+'''');
        Open;
      end;
      if ADOQueryTemp.IsEmpty then
      begin
        CDS_Chker.EnableControls;
        Application.MessageBox('审核人定义错误!','提示',0);
        Exit;
      end;
      Next;
    end;
  end;
  CDS_Chker.EnableControls;

  if Application.MessageBox('确定要执行此操作吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    Order_Main.DisableControls;
    with Order_Main do
    begin
      First;
      while not eof do
      begin
        if FieldByName('Ssel').AsBoolean=true then
        begin
          with ADOQueryCmd do
          begin
            Close;
            SQL.Clear;
            sql.Add('Update Worker_Fee_LKSub Set ');
            sql.Add(' MStatus=''已送审''');
            sql.Add(' where WFID='''+Order_Main.fieldbyname('WFID').AsString+'''');
            ExecSQL;
          end;
          with CDS_Chker do
          begin
            First;
            while not Eof do
            begin
              with ADOQueryTemp do
              begin
                Close;
                sql.Clear;
                sql.Add('select * from OA_Chk where MainId='''+Trim(order_Main.FieldByName('WFId').AsString)+'''');
                sql.Add(' and isnull(OAType,'''')=''费用报销'' ');
                sql.Add(' and isnull(Chker,'''')='''+Trim(CDS_Chker.fieldbyname('Chker').AsString)+'''');
                Open;
              end;
              FCKID:=Trim(ADOQueryTemp.fieldbyname('CKID').AsString);
              if Trim(FCKID)='' then
              begin
                if GetLSNo(ADOQueryCmd,maxno,'CK','OA_Chk',4,1)=False then
                begin
                  ADOQueryCmd.Connection.RollbackTrans;
                  Application.MessageBox('取最大号失败!','提示',0);
                  Exit;
                end;
                with ADOQueryCmd do
                begin
                  Close;
                  sql.Clear;
                  sql.Add('select * from OA_Chk where 1=2');
                  Open;
                end;
                with ADOQueryCmd do
                begin
                  Append;
                  FieldByName('CKID').Value:=Trim(maxno);
                  FieldByName('MainID').Value:=Trim(order_Main.FieldByName('WFId').AsString);
                  FieldByName('OAType').Value:='费用报销';
                  FieldByName('Chker').Value:=Trim(CDS_Chker.fieldbyname('Chker').AsString);
                  FieldByName('ChkStatus').Value:='';
                  FieldByName('ChkTime').value:=NULL;
                  Post;
                end;
              end;
              next;
            end;
          end;
        end;
        next;
      end;
    end;
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    initgrid();
  except
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!','提示',0);
  end;
end;

procedure TfrmWorkDeptFeeListBXDJ.N2Click(Sender: TObject);
begin
  SelOKNo(Order_Main,True);  
end;

procedure TfrmWorkDeptFeeListBXDJ.N1Click(Sender: TObject);
begin
  SelOKNo(Order_Main,False);  
end;

procedure TfrmWorkDeptFeeListBXDJ.ToolButton5Click(Sender: TObject);
var FWFID:string;
begin
  if Order_Main.IsEmpty then Exit;
  toolBar1.SetFocus;
  if cxTabControl1.TabIndex<>2 then exit;
  if Order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  FWFID:=Trim(Order_Main.fieldbyname('WFID').AsString);
  Order_Main.DisableControls;
  with Order_Main do
  begin
    First;
    while not Eof do
    begin
      if FieldByName('Ssel').AsBoolean=true then
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add(' select * from Worker_Fee_LKSub where WFID='''+Trim(Order_Main.fieldbyname('WFID').AsString)+'''');
          sql.Add(' and isnull(MStatus,'''')=''已送审''');
          Open;
        end;
        if ADOQueryTemp.IsEmpty then
        begin
          Order_Main.EnableControls;
          Application.MessageBox('已审核，不能撤销!','提示',0);
          Exit;
        end;
      end;
      Next;
    end;
  end;

  Order_Main.EnableControls;
  if Application.MessageBox('确定要执行撤销操作吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    Order_Main.DisableControls;
    with Order_Main do
    begin
      First;
      while not Eof do
      begin
        if FieldByName('Ssel').AsBoolean=true then
        begin
          with ADOQueryCmd do
          begin
            Close;
            SQL.Clear;
            sql.Add('Update Worker_Fee_LKSub Set ');
            sql.Add(' MStatus=''待送审''');
            sql.Add(' where WFID='''+trim(Order_Main.fieldbyname('WFID').AsString)+'''');
            ExecSQL;
          end;
          with ADOQueryCmd do
          begin
            Close;
            SQL.Clear;
            sql.Add('delete from OA_Chk ');
            sql.Add(' where Mainid='''+trim(Order_Main.fieldbyname('WFID').AsString)+'''');
            sql.add(' and OAType=''费用报销''');
            ExecSQL;
          end;
          Order_Main.Delete;
        end
        else
        Next;
      end;
    end;
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from OA_Chk where 1=2');
      Open;
    end;
    SCreateCDS20(ADOQueryTemp,CDS_Chk);
    SInitCDSData20(ADOQueryTemp,CDS_Chk);
    initGrid();
  except
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!','提示',0);
  end;
end;

procedure TfrmWorkDeptFeeListBXDJ.Tv1CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from OA_Chk where ');
    sql.add(' MainId='''+Trim(Order_Main.fieldbyname('WFId').AsString)+'''');
    sql.Add(' and OAType=''费用报销'' ');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_Chk);
  SInitCDSData20(ADOQueryTemp,CDS_Chk);
  InitImage();
end;

procedure TfrmWorkDeptFeeListBXDJ.InitImage();
var
  jpg:TJpegImage;
  myStream:TADOBlobStream;
begin
  if Order_Main.IsEmpty then Exit;
  if Trim(Order_Main.fieldbyname('WFNO').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('WFNO').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 TfrmWorkDeptFeeListBXDJ.v1Column8PropertiesEditValueChanged(
  Sender: TObject);
var FMoney,FWSID: string;
begin
  FMoney:='0';
  FWSID:=Trim(Order_Main.fieldbyname('WSId').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('Money').AsFloat);
      end;

      next;
    end;
    EnableControls;
  end;
  order_main.Locate('WSID',FWSID,[]);
end;

procedure TfrmWorkDeptFeeListBXDJ.Tv1DblClick(Sender: TObject);
begin
  ToolButton1.Click;
end;

procedure TfrmWorkDeptFeeListBXDJ.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_LKMain 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_LKSub 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.PreviewButtons:=[pbZoom, pbFind, pbPageSetup,pbExit];
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox('没有找到报表','提示',0);
    exit;
  end;
end;

procedure TfrmWorkDeptFeeListBXDJ.TEditSQClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then exit;
  if cxTabControl1.TabIndex<2 then exit;
  frmZDYHelpSel:=TfrmZDYHelpSel.Create(self);
  with frmZDYHelpSel do
  begin
    flag:='OAChker';
    flagname:='OA审核人';
    MainType:=Trim(DName);
    if showModal=1 then
    begin
      with ClientDataSet1 do
      begin
        first;
        while not eof do
        begin
          if fieldbyname('Ssel').AsBoolean=true then
          begin
            if CDS_Chk.Locate('Chker',Trim(fieldbyname('ZdyName').AsString),[])=False then
            begin
              application.MessageBox('没有该申请单的审核人','提示');
              exit;
            end;
            with ADOQueryTemp do
            begin
              Close;
              sql.Clear;
              sql.Add('Update OA_Chk Set ChkStatus=''单据修改'',chknote='''',ChkTime=getdate()');
              sql.Add(' where Mainid='''+Trim(Order_Main.fieldbyname('WSID').AsString)+'''');
              sql.Add(' and Chker='''+Trim(ClientDataSet1.fieldbyname('ZdyName').AsString)+'''');
              sql.Add(' and isnull(OAType,'''')=''费用报销'' ');
              ExecSQL;
            end;
          end;
          next;
        end;
      end;
    end;
    free;
  end;
end;

procedure TfrmWorkDeptFeeListBXDJ.TSCClick(Sender: TObject);
begin
  try
    frmFileUpPZ:=TfrmFileUpPZ.Create(Application);
    with frmFileUpPZ do
    begin
      Code.Text:=Trim(Self.Order_Main.fieldbyname('WFNO').AsString);
      CYID:=Trim(Self.Order_Main.fieldbyname('WFNO').AsString);
      if ShowModal=1 then
      begin
        Self.InitGrid();
        Self.Order_Main.Locate('WFNO',CYID,[]);
      end;
    end;
  finally
    frmFileUpPZ.Free;
  end;
end;

procedure TfrmWorkDeptFeeListBXDJ.Image1Click(Sender: TObject);
var
  fHandle:THandle;
  FInt:Integer;
  FFName,FPath:String;
begin
  with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from XD_File where CYID='''+Trim(Order_Main.fieldbyname('WFNO').AsString)+'''');
    Open;
    if IsEmpty then
    begin
      Application.MessageBox('凭证图片未上传！','提示',0);
      Exit;
    end;
  end;
  try
    ReadINIFile();
    server:=ReadINIFileStr('SYSTEMSET.INI','SERVER','服务器地址','127.0.0.1');
    if Length(server)<6 then
    begin
      server:='127.0.0.1';
    end;
    IdFTP1.Host :=server;//PicSvr;
    IdFTP1.Username := 'three';
    IdFTP1.Password := '641010';
    IdFTP1.Connect();
  except
    IdFTP1.Quit;
    Application.MessageBox('无法连接到文件服务器，请检查！', '提示', MB_ICONWARNING);
    Exit;
  end;
  FPath:='D:\Right1209\';
  if not DirectoryExists(ExtractFileDir(FPath)) then
    CreateDir(ExtractFileDir(FPath));
  FFName:=Trim(ADOQueryTemp.fieldbyname('FileName').AsString);
  FFName:=FPath+FFName;
  if FileExists(FFName) then
  begin
    FInt:=1;
  end;
  if FInt<>1 then
    IdFTP1.Get('PZ\'+Trim(ADOQueryTemp.fieldbyname('FileName').AsString),
                   FPath+Trim(ADOQueryTemp.fieldbyname('FileName').AsString));
  if IdFTP1.Connected then IdFTP1.Quit;
  ShellExecute(Handle, 'open',PChar(FPath+Trim(ADOQueryTemp.fieldbyname('FileName').AsString)),'', '', SW_SHOWNORMAL);

end;

procedure TfrmWorkDeptFeeListBXDJ.ReadINIFile();
var
  programIni:Tinifile;  //配置文件名
  FileName:string;
begin
  FileName:=ExtractFilePath(Paramstr(0))+'SYSTEMSET.INI';
  programIni:=Tinifile.create(FileName);
  server:=programIni.ReadString('SERVER','服务器地址','127.0.0.1');
  programIni.Free;
end;

procedure TfrmWorkDeptFeeListBXDJ.TZFClick(Sender: TObject);
begin
  if order_Main.IsEmpty then exit;
  if Trim(DCode)<>Trim(Order_Main.fieldbyname('FillCode').AsString) then
  begin
    Application.MessageBox('不能操作他人的信息!','提示',0);
    Exit;
  end;
  if Order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据','提示');
    exit;
  end;
  if Application.MessageBox('确定作废数据吗？','提示',32+4)<>IDYES then Exit;
  with Order_Main do
  begin
    first;
    while not eof do
    begin
      if FieldByName('Ssel').AsBoolean=true then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from Worker_Fee_LKSub where WFId='''+Trim(Order_Main.fieldbyname('WFId').AsString)+'''');
          open;
        end;
        if ADOQueryCmd.RecordCount=1 then
        begin
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from Worker_Fee_LKMain where WFId='''+Trim(Order_Main.fieldbyname('BXWFId').AsString)+'''');
            open;
          end;
          if ADOQueryTemp.IsEmpty=False then
          begin
            application.MessageBox('有领款信息,请删除','提示');
            exit;
          end;
        end;
        with ADOQuery1 do
        begin
          close;
          sql.clear;
          sql.Add('Update Worker_Fee_LKSub set MStatus=''作废'' where WFID='''+Trim(Order_Main.fieldbyname('WFID').AsString)+'''');
          ExecSQL;
        end;
        Delete;
      end
      else
      next;
    end;
  end;
end;

procedure TfrmWorkDeptFeeListBXDJ.TCXZFClick(Sender: TObject);
begin
  if order_Main.IsEmpty then exit;
  {if Trim(DCode)<>Trim(Order_Main.fieldbyname('FillCode').AsString) then
  begin
    Application.MessageBox('不能操作他人的信息!','提示',0);
    Exit;
  end;}
  if Order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据','提示');
    exit;
  end;
  if Application.MessageBox('确定撤销作废数据吗？','提示',32+4)<>IDYES then Exit;
  with Order_Main do
  begin
    first;
    while not eof do
    begin
      if FieldByName('Ssel').AsBoolean=true then
      begin
        with ADOQuery1 do
        begin
          close;
          sql.clear;
          sql.Add('Update Worker_Fee_LKSub set MStatus=''待送审'' where WFID='''+Trim(Order_Main.fieldbyname('WFID').AsString)+'''');
          ExecSQL;
        end;
        Order_Main.Delete;
      end
      else
      next;
    end;
  end;
end;

end.
