unit U_WorkDeptFeeListLKSH;

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;

type
  TfrmWorkDeptFeeListLKSH = 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;
    cxGrid1Level1: TcxGridLevel;
    Panel1: TPanel;
    Label1: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    v1WFDate: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    Worker: TEdit;
    Label6: TLabel;
    v1Column17: TcxGridDBColumn;
    ToolButton1: TToolButton;
    Label3: TLabel;
    WFNO: TEdit;
    Label4: TLabel;
    WFType: TEdit;
    ToolButton2: TToolButton;
    TSH: TToolButton;
    TCX: 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;
    ItemName: TEdit;
    Label7: TLabel;
    Person4: TEdit;
    Label8: TLabel;
    Person5: TEdit;
    Label2: TLabel;
    v1HZMoney: TcxGridDBColumn;
    TPrint: TToolButton;
    IdFTP1: TIdFTP;
    ODPat: TOpenDialog;
    SaveDialog1: TSaveDialog;
    Label9: TLabel;
    EditNote: TMemo;
    v1EditSQTime: TcxGridDBColumn;
    v1EditNote: TcxGridDBColumn;
    v1BZUnit: TcxGridDBColumn;
    v1WFNO: TcxGridDBColumn;
    v1WFType: TcxGridDBColumn;
    v1ZhaiYao: TcxGridDBColumn;
    v1ChkStatus: TcxGridDBColumn;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    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 TSHClick(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure TCXClick(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);
  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
  frmWorkDeptFeeListLKSH: TfrmWorkDeptFeeListLKSH;

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

{$R *.dfm}

procedure TfrmWorkDeptFeeListLKSH.InitImage();
begin

end;

procedure TfrmWorkDeptFeeListLKSH.FormDestroy(Sender: TObject);
begin
  frmWorkDeptFeeListLKSH:=nil;
end;

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

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

procedure TfrmWorkDeptFeeListLKSH.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add(' select A.*,B.*, ');
      sql.Add(' BZUnit=(select Top 1 C.PriceUnit from Worker_Fee_LKSub C where C.WFID=A.WFID), ');
      sql.add(' ZhaiYao=(select Top 1 C.ZhaiYao from Worker_Fee_LKSub C where C.WFID=A.WFID)');
      SQL.Add(' from Worker_Fee_LKMain A ');
      sql.add(' inner join OA_Chk B on B.Mainid=A.WFID and B.OAType=''费用报销''');
      sql.Add(' where B.Chker='''+trim(DName)+''' and A.ItemName<>''费用报销单''');
      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(B.Chkstatus,'''')='''' or isnull(B.Chkstatus,'''')=''单据修改'')');
        sql.add(' and isnull(A.HZStatus,'''')<>''审核不通过''');   //已删除审核人
      end;
      if cxTabControl1.TabIndex=1 then
      begin
        sql.Add(' and isnull(B.Chkstatus,'''')=''审核通过'' ');
      end;
      if cxTabControl1.TabIndex=2 then
      begin
        sql.Add(' and isnull(A.HZStatus,'''')=''审核通过'' ');
      end;
      if cxTabControl1.TabIndex=3 then
      begin
        sql.Add(' and isnull(A.HZStatus,'''')=''审核不通过'' ');
      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 TfrmWorkDeptFeeListLKSH.InitForm();
begin
  endDate.Date:=SGetServerDate(ADOQueryTemp);
  BegDate.Date:=EndDate.Date-7;
  cxTabControl1.TabIndex:=0;
  TSH.Visible:=False;
  if cxtabControl1.TabIndex=0 then
  begin
    TSH.Visible:=true;
  end;
  if cxtabControl1.TabIndex=1 then
  begin
  end;
  InitGrid();
end;

function TfrmWorkDeptFeeListLKSH.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_LKMain  where WFId='''+Trim(Order_Main.fieldbyname('WFId').AsString)+'''');
          sql.Add('delete Worker_Fee_LKSub  where WFId='''+Trim(Order_Main.fieldbyname('WFId').AsString)+'''');
          ExecSQL;
        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 TfrmWorkDeptFeeListLKSH.TBExportClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  TcxGridToExcel('费用报销单',cxGrid1);
end;

procedure TfrmWorkDeptFeeListLKSH.TBRafreshClick(Sender: TObject);
begin
  ToolBar1.SetFocus;
  InitGrid();
  label2.Caption:='0';
end;

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

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

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

procedure TfrmWorkDeptFeeListLKSH.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 TfrmWorkDeptFeeListLKSH.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 TfrmWorkDeptFeeListLKSH.cxTabControl1Change(Sender: TObject);
begin
  TSH.Visible:=False;
  if cxtabControl1.TabIndex=0 then
  begin
    TSH.Visible:=true;
  end;
  if cxtabControl1.TabIndex=1 then
  begin

  end;
  if cxtabControl1.TabIndex=3 then
  begin
    TSH.Visible:=true;
  end;
  InitGrid();
end;

procedure TfrmWorkDeptFeeListLKSH.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 TfrmWorkDeptFeeListLKSH.ToolButton1Click(Sender: TObject);
begin
  try
    frmWorkerDeptFeeInPutDJ:=TfrmWorkerDeptFeeInPutDJ.Create(Application);
    with frmWorkerDeptFeeInPutDJ 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
    frmWorkerDeptFeeInPutDJ.Free;
  end;
end;

procedure TfrmWorkDeptFeeListLKSH.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 TfrmWorkDeptFeeListLKSH.TSHClick(Sender: TObject);
var
  maxno,FCKID:string;
  FCount:Integer;
begin
  if Order_Main.IsEmpty then exit;
  if (cxtabControl1.TabIndex<>0) and (cxtabControl1.TabIndex<>3) then exit;
  toolbar1.SetFocus;
  if order_Main.Locate('Ssel',true,[])=False then
  begin
    application.MessageBox('没有选择数据','提示');
    exit;
  end;
  try
    with Order_Main do
    begin
      first;
      while not eof do
      begin
        if fieldbyname('Ssel').AsBoolean=True then
        begin
          if trim(Canshu1)='高权限' then
          begin
            with ADOQueryCmd do
            begin
              Close;
              SQL.Clear;
              sql.Add('Update OA_Chk Set ');
              sql.Add('ChkTime=getdate(),Chkstatus=''审核通过'' ');
              sql.Add('where Mainid='''+trim(Order_Main.fieldbyname('WFID').AsString)+''' ');
              sql.add('and OAType=''费用报销''');
              ExecSQL;
            end;
          end
          else
          begin
            with ADOQueryCmd do
            begin
              Close;
              SQL.Clear;
              sql.Add('Update OA_Chk Set ');
              sql.Add(' ChkTime=getdate(),Chkstatus=''审核通过'' ');
              sql.Add(' where CKID='''+trim(Order_Main.fieldbyname('CKID').AsString)+''' ');
              ExecSQL;
            end;
          end;
          if cxtabControl1.TabIndex=3 then
          begin
            with ADOQueryMain do
            begin
              close;
              sql.Clear;
              sql.add('update Worker_Fee_LKMain set ');
              sql.add('HZStatus=''已送审'' ');
              sql.Add(' where WFID='''+trim(Order_Main.fieldbyname('WFID').AsString)+'''');
              execsql;
            end;
          end;
          with ADOQueryCmd do
          begin
            close;
            sql.clear;
            sql.add('select A.* from OA_Chk A ');
            sql.add('where A.Mainid='''+trim(Order_Main.fieldbyname('WFID').AsString)+''' ');
            sql.add('and isnull(A.Chkstatus,'''')<>''审核通过'' ');
            sql.add('and isnull(A.OAType,'''')=''费用报销''');
            open;
          end;
          if ADOQueryCmd.IsEmpty then
          begin
            with ADOQueryMain do
            begin
              close;
              sql.Clear;
              sql.add('update Worker_Fee_LKMain set ');
              sql.add('HZStatus=''审核通过'' ');
              sql.Add(' where WFID='''+trim(Order_Main.fieldbyname('WFID').AsString)+'''');
              execsql;
            end;
          end;
        end;
        next;
      end;
    end;
    application.MessageBox('审核成功','提示');
    initGrid();
  except;
    application.MessageBox('审核失败','提示');
    exit;
  end;
end;

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

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

procedure TfrmWorkDeptFeeListLKSH.TCXClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  toolbar1.SetFocus;
  if trim(Order_Main.fieldbyname('CWChkStatus').AsString)='已付款' then
  begin
    application.MessageBox('已付款不能审核','提示');
    exit;
  end;
  if Application.MessageBox('确定要执行撤销操作吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('Update Worker_Fee_LKMain Set HZStatus=''审核不通过''');
      sql.Add(' where WFID='''+trim(Order_Main.fieldbyname('WFID').AsString)+'''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('update OA_Chk set ChkStatus=''审核不通过'' where CKID='''+Trim(Order_Main.fieldbyname('CKID').AsString)+'''');
      sql.Add(' and OAType=''费用报销'' ');
      ExecSQL;
    end;
    initGrid();
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!','提示',0);
  end;
end;

procedure TfrmWorkDeptFeeListLKSH.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 MainId='''+Trim(Order_Main.fieldbyname('WFId').AsString)+'''');
    sql.Add(' and OAType=''费用报销'' ');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_Chk);
  SInitCDSData20(ADOQueryTemp,CDS_Chk);
  EditNote.Text:=trim(Order_Main.fieldbyname('EditNote').AsString);
end;

procedure TfrmWorkDeptFeeListLKSH.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 TfrmWorkDeptFeeListLKSH.Tv1DblClick(Sender: TObject);
begin
  ToolButton1.Click;
end;

procedure TfrmWorkDeptFeeListLKSH.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;

end.
