unit U_WorkDeptFeeListHZCX;

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
  TfrmWorkDeptFeeListHZCX = 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;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column5: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Panel1: TPanel;
    Label1: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    v1WFDate: TcxGridDBColumn;
    Worker: TEdit;
    Label6: TLabel;
    v1Column17: TcxGridDBColumn;
    ToolButton1: TToolButton;
    Label3: TLabel;
    Dept: TEdit;
    ToolButton2: TToolButton;
    PopupMenu1: TPopupMenu;
    N2: TMenuItem;
    N1: TMenuItem;
    CDS_Chker: TClientDataSet;
    CDS_Chk: TClientDataSet;
    DS_Chk: TDataSource;
    Label2: TLabel;
    v1Money: TcxGridDBColumn;
    TPrint: TToolButton;
    IdFTP1: TIdFTP;
    ODPat: TOpenDialog;
    SaveDialog1: TSaveDialog;
    v1WFNO: TcxGridDBColumn;
    v1ZhaiYao: TcxGridDBColumn;
    ADOQuery1: TADOQuery;
    v1BXMoney: TcxGridDBColumn;
    v1HKMoney: 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 WorkerChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure N1Click(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();
    procedure setstatus();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmWorkDeptFeeListHZCX: TfrmWorkDeptFeeListHZCX;

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

{$R *.dfm}
procedure TfrmWorkDeptFeeListHZCX.setstatus();
begin
 
end;

procedure TfrmWorkDeptFeeListHZCX.InitImage();
begin
  
end;

procedure TfrmWorkDeptFeeListHZCX.FormDestroy(Sender: TObject);
begin
  frmWorkDeptFeeListHZCX:=nil;
end;

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

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

procedure TfrmWorkDeptFeeListHZCX.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add(' select A.*, ');
      sql.add(' BXMoney=(select Sum(C.BXMoney) from Worker_Fee_LKMain B inner join Worker_Fee_LKSub C on C.WFID=B.WFID where B.BXWFID=A.WFID and C.MStatus=''审核通过'' and C.FKStatus=''已付款''),');
      sql.add(' HKMoney=(select Sum(C.HKMoney) from Worker_Fee_LKMain B inner join Worker_Fee_LKSub C on C.WFID=B.WFID where B.BXWFID=A.WFID and C.MStatus=''审核通过'' and C.FKStatus=''已付款'')');
      SQL.Add(' from Worker_Fee_LKMain A ');
      sql.Add(' where A.ItemName=''领款申请单'' and isnull(A.CWChkStatus,'''')=''已付款'' and 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 where 1=2');
      Open;
    end;
    SCreateCDS20(ADOQueryTemp,CDS_Chk);
    SInitCDSData20(ADOQueryTemp,CDS_Chk);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

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

function TfrmWorkDeptFeeListHZCX.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 WSId='''+Trim(Order_Main.fieldbyname('WSId').AsString)+'''');
          ExecSQL;
        end;
        with ADOQuery1 do
        begin
          close;
          sql.clear;
          sql.Add('Update Worker_Fee_LKSub set ZuoFeiFlag='''' where WSID='''+Trim(Order_Main.fieldbyname('BXWSId').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 TfrmWorkDeptFeeListHZCX.TBExportClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  TcxGridToExcel('费用报销单',cxGrid1);
end;

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

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

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

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

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

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

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

procedure TfrmWorkDeptFeeListHZCX.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.
