unit U_WorkDeptFeeChkList;

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 = 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;
    CDS_PRT: TClientDataSet;
    ToolButton3: TToolButton;
    cxGridPopupMenu2: TcxGridPopupMenu;
    cxTabControl1: TcxTabControl;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column5: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    v1Column8: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    ToolButton6: TToolButton;
    v1Column10: TcxGridDBColumn;
    ToolButton7: TToolButton;
    CDS_Chker: TClientDataSet;
    v1WFType: TcxGridDBColumn;
    v1ItemName: TcxGridDBColumn;
    v1Person4: TcxGridDBColumn;
    cxSplitter1: TcxSplitter;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    order_Sub: TClientDataSet;
    DataSource2: TDataSource;
    v1Person5: TcxGridDBColumn;
    ToolButton8: TToolButton;
    Order_Print: TClientDataSet;
    v1ChkNote: TcxGridDBColumn;
    Panel3: TPanel;
    Image1: TImage;
    IdFTP1: TIdFTP;
    ODPat: TOpenDialog;
    SaveDialog1: TSaveDialog;
    Panel1: TPanel;
    Label1: TLabel;
    Label6: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label8: TLabel;
    Label5: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Worker: TEdit;
    Dept: TEdit;
    WFType: TEdit;
    ItemName: TEdit;
    Label2: TLabel;
    EditNote: TMemo;
    v1EditSQTime: TcxGridDBColumn;
    v1EditNote: TcxGridDBColumn;
    v1BZUnit: TcxGridDBColumn;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    v1WFNO: TcxGridDBColumn;
    TSC: TToolButton;
    Label9: TLabel;
    Person4: TEdit;
    Label7: TLabel;
    Person5: TEdit;
    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 ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure v1Column8PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure ToolButton8Click(Sender: TObject);
    procedure TSCClick(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();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmWorkDeptFeeChkList: TfrmWorkDeptFeeChkList;

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

{$R *.dfm}

procedure TfrmWorkDeptFeeChkList.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.FormDestroy(Sender: TObject);
begin
  frmWorkDeptFeeChkList:=nil;
end;

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

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

procedure TfrmWorkDeptFeeChkList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add(' select A.*,OA.Chker DChker, ');
      sql.Add(' BZUnit=(select Top 1 B.PriceUnit from Worker_Fee_Sub B where B.WFID=A.WFID) ');
      SQL.Add(' from Worker_Fee_Main A ');
      sql.Add(' inner join OA_Chk OA on A.WFID=OA.MainId and isnull(OA.OAType,'''')=''费用报销'' ');
      sql.Add(' where A.Valid=''Y'' and OA.Chker='''+Trim(DName)+'''');
      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 cxTabControl1.TabIndex=0 then
      begin
        sql.Add(' and ((isnull(OA.ChkStatus,'''')='''' and (isnull(A.HZStatus,'''')=''已送审'' or isnull(A.HZStatus,'''')=''审核中''))');
        sql.add(' or isnull(OA.ChkStatus,'''')=''单据修改'')');
      end else
      if cxTabControl1.TabIndex=1 then
      begin
        sql.Add(' and isnull(OA.ChkStatus,'''')=''审核通过'' ');
      end  else
      if cxTabControl1.TabIndex=2 then
      begin
        sql.Add(' and isnull(OA.ChkStatus,'''')=''审核不通过'' ');
      end;
      sql.add(' order by A.HZStatus');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from OA_Chk ');
      sql.Add(' where MainId='''+Trim(Order_Main.fieldbyname('WFID').AsString)+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryTemp,Order_Sub);
    SInitCDSData20(ADOQueryTemp,Order_Sub);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

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

function TfrmWorkDeptFeeChkList.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.TBExportClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  TcxGridToExcel('费用报销单',cxGrid1);
end;

procedure TfrmWorkDeptFeeChkList.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
  label5.Caption:='0';
end;

procedure TfrmWorkDeptFeeChkList.FormShow(Sender: TObject);
begin
  ReadCxGrid('费用报销列表审核11',Tv1,'费用管理');
  InitForm();
  InitGrid();
end;

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

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

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

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

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

procedure TfrmWorkDeptFeeChkList.ToolButton4Click(Sender: TObject);
begin
  ToolBar1.SetFocus;
  if Order_Main.IsEmpty then Exit;
  if cxTabControl1.TabIndex<>0 then Exit;
  if Order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要执行此操作吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    Order_Main.DisableControls;
    with Order_Main do
    begin
      while Locate('SSel',True,[])=True do
      begin
        if trim(canshu1)<>'高权限' then
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('UPdate OA_Chk Set ChkStatus=''审核通过'',ChkTIme=getdate() ');
            sql.Add(',ChkNote='''+Trim(Order_Main.fieldbyname('ChkNote').AsString)+'''');
            sql.Add(' where MainId='''+Trim(Order_Main.fieldbyname('WFID').AsString)+'''');
            sql.Add(' and isnull(OAType,'''')=''费用报销'' and isnull(Chker,'''')='''+Trim(DName)+'''');
            ExecSQL;
          end;
        end
        else
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('UPdate OA_Chk Set ChkStatus=''审核通过'',ChkTIme=getdate() ');
            sql.Add(',ChkNote='''+Trim(Order_Main.fieldbyname('ChkNote').AsString)+'''');
            sql.Add(' where MainId='''+Trim(Order_Main.fieldbyname('WFID').AsString)+'''');
            ExecSQL;
          end;
        end;
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from OA_Chk ');
          sql.Add(' where MainId='''+Trim(Order_Main.fieldbyname('WFID').AsString)+'''');
          sql.Add(' and isnull(OAType,'''')=''费用报销'' and isnull(ChkStatus,'''')<>''审核通过'' ');
          Open;
        end;
        if ADOQueryTemp.IsEmpty=False then
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('UPdate Worker_Fee_Main Set ChkStatus=''审核中'',ChkTIme=getdate(),Chker='''+Trim(DName)+'''');
            sql.Add(',HZStatus=''审核中'',HZStatusTime=getdate(),ChkNote='''+Trim(Order_Main.fieldbyname('ChkNote').AsString)+'''');
            sql.Add(' where WFID='''+Trim(Order_Main.fieldbyname('WFID').AsString)+'''');
            ExecSQL;
          end;
        end else
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('UPdate Worker_Fee_Main Set ChkStatus=''审核通过'',ChkTIme=getdate(),Chker='''+Trim(DName)+'''');
            sql.Add(',HZStatus=''审核通过'',HZStatusTime=getdate(),ChkNote='''+Trim(Order_Main.fieldbyname('ChkNote').AsString)+'''');
            sql.Add(' where WFID='''+Trim(Order_Main.fieldbyname('WFID').AsString)+'''');
            ExecSQL;
          end;
        end;
        Order_Main.Delete;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Order_Main.EnableControls;
    label5.Caption:='0';
    initgrid();
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作失败!','提示',0);
  end;
end;

procedure TfrmWorkDeptFeeChkList.ToolButton5Click(Sender: TObject);
var
  FCKID,FChkStatus,FChker,FChkTime,FChkNote:String;
begin
  ToolBar1.SetFocus;
  if Order_Main.IsEmpty then Exit;
  if cxTabControl1.TabIndex=0 then Exit;
  if cxTabControl1.TabIndex=3 then Exit;
  if Order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  if Order_Main.Locate('SSel;CWChkStatus',VarArrayOf([True,'已付款']),[loPartialKey])=True then
  begin
    Application.MessageBox('已付款不能审核','提示',0);
    Exit;
  end;
  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 OA_Chk Set ChkStatus=Null,ChkTIme=Null ,ChkNote='''' ');
          sql.Add(' where MainId='''+Trim(Order_Main.fieldbyname('WFID').AsString)+'''');
          sql.Add(' and isnull(OAType,'''')=''费用报销'' and isnull(Chker,'''')='''+Trim(DName)+'''');
          ExecSQL;
          end;
          with ADOQueryTemp do
          begin
          Close;
          sql.Clear;
          sql.Add('select Top 1 * from OA_Chk');
          sql.Add(' where MainId='''+Trim(Order_Main.fieldbyname('WFID').AsString)+'''');
          sql.Add(' and isnull(ChkStatus,'''')<>'''' ');
          sql.Add(' order by ChkTime desc');
          Open;
          end;
          FCKID:=Trim(ADOQueryTemp.fieldbyname('CKID').AsString);
          FChkStatus:=Trim(ADOQueryTemp.fieldbyname('ChkStatus').AsString);
          FChker:=Trim(ADOQueryTemp.fieldbyname('Chker').AsString);
          FChkNote:=Trim(ADOQueryTemp.fieldbyname('ChkNote').AsString);
          FChkTime:=Trim(ADOQueryTemp.fieldbyname('ChkTime').AsString);
          with ADOQueryCmd do
          begin
          Close;
          sql.Clear;
          sql.Add(' UPdate Worker_Fee_Main Set  ');
          sql.Add(' Chker='''+Trim(FChker)+'''');
          sql.Add(',ChkTIme='''+Trim(FChkTIme)+'''');
          sql.Add(',HZStatusTime='''+Trim(FChkTIme)+'''');
          sql.Add(',ChkNote='''+Trim(FChkNote)+'''');
          sql.Add(',ChkStatus=''审核中'' ' );
          sql.Add(',HZStatus=''审核中'' ' );
          sql.Add(' where WFID='''+Trim(Order_Main.fieldbyname('WFID').AsString)+'''');
          ExecSQL;
          end;
        end;
        next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Order_Main.EnableControls;
    label5.Caption:='0';
    initgrid();
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作失败!','提示',0);
  end;
end;

procedure TfrmWorkDeptFeeChkList.v1Column8PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue:Boolean;
  FQty:Double;
  FXS:Integer;
begin
  if Trim(label5.Caption)='' then
    label5.Caption:='0';
  FQty:=StrToFloat(label5.Caption);
  if Order_Main.IsEmpty then Exit;
  mvalue:=TcxCheckBox(Sender).EditingValue;
  with Order_Main do
  begin
    Edit;
    FieldByName('SSel').Value:=mvalue;
    Post;
  end;
  if mvalue=True then
  begin
    FXS:=1;
  end else
  begin
    FXS:=-1;
  end;
  if Trim(Order_Main.fieldbyname('HZMoney').AsString)<>'' then
    FQty:=FQty+Order_Main.fieldbyname('HZMoney').Value*FXS;
  label5.Caption:=FloatToStr(FQty);

end;

procedure TfrmWorkDeptFeeChkList.ToolButton6Click(Sender: TObject);
begin
  ToolBar1.SetFocus;
  if Order_Main.IsEmpty then Exit;
  if Order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;

  if Order_Main.Locate('SSel;CWChkStatus',VarArrayOf([True,'已付款']),[loPartialKey])=True then
  begin
    Application.MessageBox('已付款不能审核','提示',0);
    Exit;
  end;
  {if Order_Main.Locate('SSel;ChkNote',VarArrayOf([True,'']),[loPartialKey])=True then
  begin
    Application.MessageBox('审核备注不能为空!','提示',0);
    Exit;
  end;}
  if Application.MessageBox('确定要执行此操作吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    Order_Main.DisableControls;
    with Order_Main do
    begin
      while Locate('SSel',True,[])=True do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate OA_Chk Set ChkStatus=''审核不通过'',ChkTIme=getdate()');
          sql.Add(',ChkNote='''+Trim(Order_Main.fieldbyname('ChkNote').AsString)+'''');
          sql.Add(' where MainId='''+Trim(Order_Main.fieldbyname('WFID').AsString)+'''');
          sql.Add(' and isnull(OAType,'''')=''费用报销'' and isnull(Chker,'''')='''+Trim(DName)+'''');
          ExecSQL;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate Worker_Fee_Main Set ChkStatus=''审核不通过'',ChkTIme=getdate(),Chker='''+Trim(DName)+'''');
          sql.Add(',HZStatus=''审核不通过'',HZStatusTime=getdate(),ChkNote='''+Trim(Order_Main.fieldbyname('ChkNote').AsString)+'''');
          sql.Add(' where WFID='''+Trim(Order_Main.fieldbyname('WFID').AsString)+'''');
          ExecSQL;
        end;
        Order_Main.Delete;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Order_Main.EnableControls;
    label5.Caption:='0';
    initgrid();
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作失败!','提示',0);
  end;
end;

procedure TfrmWorkDeptFeeChkList.ToolButton7Click(Sender: TObject);
var FCount: Integer;
FCKID,maxno: String;
begin
  if Order_Main.IsEmpty then exit;
  if cxTabControl1.TabIndex<>0 then exit;
  if Order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  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 Order_Main.Locate('SSel',True,[]) do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate OA_Chk Set ChkStatus=''审核通过'',ChkTIme=getdate() ');
          sql.Add(',ChkNote='''+Trim(Order_Main.fieldbyname('ChkNote').AsString)+'''');
          sql.Add(' where MainId='''+Trim(Order_Main.fieldbyname('WFID').AsString)+'''');
          sql.Add(' and isnull(OAType,'''')=''费用报销'' and isnull(Chker,'''')='''+Trim(DName)+'''');
          ExecSQL;
        end;
        with CDS_Chker do
        begin
          CDS_Chker.First;
          while not CDS_Chker.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);
            ADOQueryTemp.Close;
            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);
                Post;
              end;
            end;
            CDS_Chker.Next;
          end;
        end;
        Order_Main.Delete;
      end;
    end;
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    Exit;
  except
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!','提示',0);
  end;
end;

procedure TfrmWorkDeptFeeChkList.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 ');
    sql.Add(' where MainId='''+Trim(Order_Main.fieldbyname('WFID').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,Order_Sub);
  SInitCDSData20(ADOQueryTemp,Order_Sub);
  EditNote.Text:=trim(Order_Main.fieldbyname('EditNote').AsString);
  InitImage();
end;

procedure TfrmWorkDeptFeeChkList.ToolButton8Click(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);
    if Order_Main.FieldByName('HZStatus').AsString<>'审核通过' then
    begin
      RM1.PreviewButtons:=[pbZoom, pbFind, pbPageSetup,pbExit];
      RM1.ShowReport;
    end
    else
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox('没有找到报表','提示',0);
    exit;
  end;
end;

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

end.
