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;

type
  TfrmWorkDeptFeeChkList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    CDS_PRT: TClientDataSet;
    ToolButton3: TToolButton;
    cxGridPopupMenu2: TcxGridPopupMenu;
    cxTabControl1: TcxTabControl;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column2: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    Panel1: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    v1Column7: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    ToolButton2: TToolButton;
    v1Column1: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    v1MSMoney: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxSplitter1: TcxSplitter;
    cxGridPopupMenu3: TcxGridPopupMenu;
    DataSource2: TDataSource;
    ClientDataSet2: TClientDataSet;
    v2Column1: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    Label6: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Worker: TEdit;
    WFType: TEdit;
    BXNO: TEdit;
    Label5: TLabel;
    WFTypeSub: TEdit;
    Label7: TLabel;
    Dept: TEdit;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    ToolButton1: TToolButton;
    v1Column14: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure cxPageControl1Change(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure WorkerChange(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure BXNOKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton1Click(Sender: TObject);
    procedure v1Column1PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1Column13PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
  private
    FInt,PFInt:Integer;
    canshu1,canshu2:string;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    procedure InitGridWsql(fsj:string);
    function  SaveData():Boolean;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmWorkDeptFeeChkList: TfrmWorkDeptFeeChkList;

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

{$R *.dfm}

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('费用报销列表审核',Tv1,'费用管理');
  WriteCxGrid('费用报销列表审核MX',Tv2,'费用管理');
end;

procedure TfrmWorkDeptFeeChkList.InitGrid();

begin

  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add(' select A.* ');
      SQL.Add(',BXMoneyHZ=(select Sum(BXMoney) from Worker_Fee_Sub B where B.WFID=A.WFID)');
      SQL.Add(' from Worker_Fee_Main A where 1=1');
      if cxTabControl1.TabIndex=0 then
      begin
        sql.Add(' and isnull(A.ChkStatus,'''')='''' ');
      end else
      if cxTabControl1.TabIndex=1 then
      begin
        sql.Add(' and isnull(A.ChkStatus,'''')<>'''' ');
      end;
      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;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  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.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmWorkDeptFeeChkList.FormShow(Sender: TObject);
begin
  ReadCxGrid('费用报销列表审核',Tv1,'费用管理');
  ReadCxGrid('费用报销列表审核MX',Tv2,'费用管理');
  InitForm();
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.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
  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
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate Worker_Fee_Main Set ChkStatus=''审核通过'',ChkTIme=getdate(),Chker='''+Trim(DName)+'''');
          sql.Add(',ChkNote='''+Trim(Order_Main.fieldbyname('ChkNote').AsString)+'''');
          sql.Add(',WFType='''+Trim(Order_Main.fieldbyname('WFType').AsString)+'''');
          sql.Add(',WFTypeSub='''+Trim(Order_Main.fieldbyname('WFTypeSub').AsString)+'''');
          sql.Add(' where WFID='''+Trim(Order_Main.fieldbyname('WFID').AsString)+'''');
          ExecSQL;
        end;
        Order_Main.Delete;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Order_Main.EnableControls;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作失败!','提示',0);
  end;
end;

procedure TfrmWorkDeptFeeChkList.ToolButton5Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  if cxTabControl1.TabIndex<>1 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
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from Worker_Fee_Sub A');
          sql.Add(' where WFID='''+Trim(Order_Main.fieldbyname('WFID').AsString)+'''');
          sql.Add(' and isnull(BXMoney,0)<>0');
          Open;
        end;
        if ADOQueryTemp.IsEmpty=False then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Order_Main.EnableControls;
          Application.MessageBox('已有报销数据不能撤销审核!','提示',0);
          Exit;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate Worker_Fee_Main Set ChkStatus=Null,ChkTIme=Null,Chker=Null,ChkNote=Null ');
          sql.Add(' where WFID='''+Trim(Order_Main.fieldbyname('WFID').AsString)+'''');
          ExecSQL;
        end;
        Order_Main.Delete;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Order_Main.EnableControls;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作失败!','提示',0);
  end;
end;

procedure TfrmWorkDeptFeeChkList.Tv1MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Worker_Fee_Sub where WFID='''+Trim(Order_Main.fieldbyname('WFID').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet2);
  SInitCDSData20(ADOQueryTemp,ClientDataSet2);
end;

procedure TfrmWorkDeptFeeChkList.BXNOKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key=#13 then
  begin
    if Trim(BXNO.Text)<>'' then
    begin
      if Length(BXNO.Text)<3 then Exit;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.Add(' select A.* ');
        SQL.Add(',BXMoneyHZ=(select Sum(BXMoney) from Worker_Fee_Sub B where B.WFID=A.WFID)');
        SQL.Add(',DBXMoneyHZ=(select Sum(isnull(Money,0)-isnull(BXMoney,0)) from Worker_Fee_Sub B where B.WFID=A.WFID)');
        SQL.Add(' from Worker_Fee_Main A where 1=1');
        if cxTabControl1.TabIndex=0 then
        begin
          sql.Add(' and isnull(A.ChkStatus,'''')='''' ');
        end else
        if cxTabControl1.TabIndex=1 then
        begin
          sql.Add(' and isnull(A.ChkStatus,'''')<>'''' ');
        end;
        sql.Add(' and BXNO like '''+'%'+Trim(BXNO.Text)+'%'+'''');
        Open;
      end;
      SCreateCDS20(ADOQueryMain,Order_Main);
      SInitCDSData20(ADOQueryMain,Order_Main);
    end;
  end;
end;

procedure TfrmWorkDeptFeeChkList.ToolButton1Click(Sender: TObject);
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;
  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 Worker_Fee_Main Set EditTime=getdate(),Editer='''+Trim(DName)+'''');
          sql.Add(',WFType='''+Trim(Order_Main.fieldbyname('WFType').AsString)+'''');
          sql.Add(',WFTypeSub='''+Trim(Order_Main.fieldbyname('WFTypeSub').AsString)+'''');
          sql.Add(' where WFID='''+Trim(Order_Main.fieldbyname('WFID').AsString)+'''');
          ExecSQL;
        end;
        Edit;
        FieldByName('SSel').Value:=False;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Order_Main.EnableControls;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存异常!','提示',0);
  end;
end;

procedure TfrmWorkDeptFeeChkList.v1Column1PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='WFType';
      flagname:='大类';
      MainType:=Trim(Self.Order_Main.fieldbyname('Dept').AsString);
      if ShowModal=1 then
      begin
        with Self.Order_Main do
        begin
          Edit;
          FieldByName('WFType').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmWorkDeptFeeChkList.v1Column13PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='WFTypeSub';
      flagname:='小类';
      MainType:=Trim(Self.Order_Main.fieldbyname('WFType').AsString);
      if ShowModal=1 then
      begin
        with Self.Order_Main do
        begin
          Edit;
          FieldByName('WFTypeSub').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

end.
