unit U_BXList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxGraphics, cxCustomData, cxStyles, cxTL, cxMaskEdit, DB, ADODB,
  cxInplaceContainer, cxDBTL, cxControls, cxTLData, ComCtrls, ToolWin, StdCtrls,
  cxFilter, cxData, cxDataStorage, cxEdit, cxDBData, DBClient,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, ExtCtrls,
  cxSplitter, cxGridLevel, cxClasses, cxGridCustomView, cxGrid,
  cxGridCustomPopupMenu, cxGridPopupMenu, RM_Dataset, RM_System, RM_Common,
  RM_Class, RM_GridReport, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdFTP, ShellAPI, IniFiles, cxCheckBox, cxCalendar, cxButtonEdit,
  cxTextEdit, cxDBLookupComboBox, cxContainer, cxDropDownEdit, cxPC, Menus,
  TeEngine, Series, TeeProcs, Chart, DbChart, GanttCh, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator, dxCore, ActiveX, dxBarBuiltInMenu, U_SLT,
  jpeg;

type
  TfrmBXList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ToolButton2: TToolButton;
    ADOQueryMain: TADOQuery;
    cxGridPopupMenu2: TcxGridPopupMenu;
    ClientDataSet1: TClientDataSet;
    DataSource1: TDataSource;
    ToolButton1: TToolButton;
    Panel2: TPanel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1XH: TcxGridDBColumn;
    v1FXH: TcxGridDBColumn;
    v2Column7: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    v2Column15: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Panel10: TPanel;
    Label5: TLabel;
    Label6: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    cxTabControl1: TcxTabControl;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Label3: TLabel;
    Label4: TLabel;
    Label1: TLabel;
    Label2: TLabel;
    DeptName: TEdit;
    FeeType: TEdit;
    FeeName: TEdit;
    Filler: TEdit;
    Label7: TLabel;
    DFDW: TEdit;
    Label8: TLabel;
    FPBH: TEdit;
    Tv1Column7: TcxGridDBColumn;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    GroupBox1: TGroupBox;
    ScrollBox1: TScrollBox;
    adoqueryPicture: TADOQuery;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure C_CodeNameChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure DeptNameChange(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
  private

    { Private declarations }
    procedure InitGrid();
    procedure InitImage();
  public
    canshu1: string;
  end;

var
  //frmBXList: TfrmBXList;
  Mach: array of TfrmSlt;

implementation

uses
  U_DataLink, U_RTFun;

{$R *.dfm}

procedure TfrmBXList.InitGrid();
begin
  Panel10.Visible := True;
  Panel10.Refresh;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add('select A.* ');
      SQL.Add(',WChker=[dbo].[F_Get_Order_SubStr](A.HMID,''BXWChk'') from Bx_HxMain A ');
      sql.Add(' where Filltime>=:begdate and FillTime<:enddate');
      if Trim(canshu1) = '查询' then
      begin
       // SQL.Add(' and ((isnull(sfgk,'''')=''公开'') or (isnull(Filler,'''')=''' + Trim(DName) + '''))');
        SQL.Add(' and ((isnull(sfgk,'''')=''公开'') or (isnull(Filler,'''')=''' + Trim(DName) + ''') OR (isnull(chkerHZ,'''') like ''%' + Trim(DName) + '%'') )');
      end;
      if cxTabControl1.TabIndex = 0 then
      begin
        sql.Add('and isnull(ChkStatus,'''')=''已提交'' ');
      end
      else if cxTabControl1.TabIndex = 1 then
      begin
        sql.Add('and isnull(ChkStatus,'''')=''审核中'' ');
      end
      else if cxTabControl1.TabIndex = 2 then
      begin
        sql.Add('and isnull(ChkStatus,'''')=''已审核'' ');
      end
      else if cxTabControl1.TabIndex = 3 then
      begin
        sql.Add('and isnull(ChkStatus,'''')=''已审核'' and ZFFlag=1  ');
      end
      else if cxTabControl1.TabIndex = 4 then
      begin
        sql.Add('and isnull(ChkStatus,'''') in (''已审核'',''已提交'',''审核中'') ');
      end;

      Parameters.ParamByName('begdate').Value := Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date));
      Parameters.ParamByName('enddate').Value := Trim(FormatDateTime('yyyy-MM-dd', enddate.Date + 1));
      Open;
    end;
    SCreateCDS20(ADOQueryMain, ClientDataSet1);
    SInitCDSData20(ADOQueryMain, ClientDataSet1);

  finally
    ADOQueryMain.EnableControls;
  end;
  Panel10.Visible := False;
end;

procedure TfrmBXList.FormDestroy(Sender: TObject);
begin
  //frmBXList := nil;
end;

procedure TfrmBXList.FormClose(Sender: TObject; var Action: TCloseAction);
var
  i, j: integer;
begin
  j := length(Mach);
  if j > 0 then
  begin
    for i := 0 to j - 1 do
    begin
      Mach[i].free;
    end;
  end;
  SetLength(Mach, 0);
  Action := caFree;
end;

procedure TfrmBXList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('Tv1', Tv1, Self.Caption);
  Close;
end;

procedure TfrmBXList.FormShow(Sender: TObject);
begin
  ReadCxGrid('Tv1', Tv1, Self.Caption);

end;

procedure TfrmBXList.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmBXList.ToolButton2Click(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel2, 1, 2));
    SCreateCDS20(ADOQueryMain, ClientDataSet1);
    SInitCDSData20(ADOQueryMain, ClientDataSet1);
  end;
end;

procedure TfrmBXList.C_CodeNameChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmBXList.FormCreate(Sender: TObject);
var
  FYear: string;
  FMonth: string;
  FDate: TDate;
begin
  {FDate:=SGetServerDate10(ADOQueryTemp);
  FYear:=FormatDateTime('yyyy',FDate);
  FMonth:=FormatDateTime('MM',FDate);
  if StrToInt(FMonth)<2 then
  begin
    BegDate.Date:=StrToDate(IntToStr(strtoint(FYear)-1)+'-02-01');
    //EndDate.Date:=StrToDate(FYear+'-01-31');
  end else
  begin
    BegDate.Date:=StrToDate(FYear+'-02-01');
    //EndDate.Date:=StrToDate(IntToStr(strtoint(FYear)+1)+'-01-31');
  end;  }
  EndDate.Date := SGetServerDate(ADOQueryTemp);
  BegDate.Date := EndDate.Date - 30;
end;

procedure TfrmBXList.cxTabControl1Change(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmBXList.ToolButton1Click(Sender: TObject);
begin
  if ClientDataSet1.IsEmpty then
    Exit;
  if ClientDataSet1.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  ClientDataSet1.DisableControls;
  with ClientDataSet1 do
  begin
    First;
    while Locate('SSel', False, []) do
    begin
      Delete;
    end;
  end;
  ClientDataSet1.EnableControls;
  TcxGridToExcel('费用报销列表', cxGrid1);
  TBRafresh.Click;
  ToolButton2.Click;
end;

procedure TfrmBXList.DeptNameChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmBXList.ToolButton3Click(Sender: TObject);
begin
  if ClientDataSet1.IsEmpty then
    Exit;
  if cxTabControl1.TabIndex <> 2 then
    Exit;
  if ClientDataSet1.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要进行数据操作？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    ClientDataSet1.DisableControls;
    with ClientDataSet1 do
    begin
      First;
      while ClientDataSet1.Locate('SSel', True, []) do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add(' update Bx_HxMain set ZFFlag=1,ZFTime=getdate(),ZFPerson=''' + Trim(DName) + '''');
          sql.Add(' where HMID=''' + Trim(ClientDataSet1.fieldbyname('HMID').AsString) + ''' ');
          ExecSQL;
        end;
        ClientDataSet1.Delete;
      end;
    end;
    ClientDataSet1.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ClientDataSet1.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!', '提示', 0);
  end;
end;

procedure TfrmBXList.ToolButton4Click(Sender: TObject);
begin
  if ClientDataSet1.IsEmpty then
    Exit;
  if cxTabControl1.TabIndex <> 3 then
    Exit;
  if ClientDataSet1.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要进行数据撤销操作？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    ClientDataSet1.DisableControls;
    with ClientDataSet1 do
    begin
      First;
      while ClientDataSet1.Locate('SSel', True, []) do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add(' update Bx_HxMain set ZFFlag=0,ZFTime=getdate(),ZFPerson=''' + Trim(DName) + '''');
          sql.Add(' where HMID=''' + Trim(ClientDataSet1.fieldbyname('HMID').AsString) + ''' ');
          ExecSQL;
        end;
        ClientDataSet1.Delete;
      end;
    end;
    ClientDataSet1.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ClientDataSet1.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!', '提示', 0);
  end;
end;

procedure TfrmBXList.N1Click(Sender: TObject);
begin
  SelOKNoFilter(Tv1, True);
end;

procedure TfrmBXList.N2Click(Sender: TObject);
begin
  SelOKNoFilter(Tv1, False);
end;

procedure TfrmBXList.InitImage();
var
  i, j: integer;
  jpg: TJpegImage;
  myStream: TADOBlobStream;
begin
  j := length(Mach);
  if j > 0 then
  begin
    for i := 0 to j - 1 do
    begin
      Mach[i].free;
    end;
  end;
  SetLength(Mach, 0);
  if ClientDataSet1.IsEmpty then
    exit;
  try
    with adoqueryPicture do
    begin
      close;
      sql.Clear;
      sql.Add(' select A.TFID,A.WBID,A.FilesOther,A.FileName,A.url from TP_File A ');
      sql.add('where  A.WBID=' + quotedstr(trim(ClientDataSet1.fieldbyname('HMID').AsString)));
      sql.Add(' and A.TFType=''报销登记'' ');
      open;
    end;
    j := adoqueryPicture.RecordCount;
    if j < 1 then
      exit;
    adoqueryPicture.DisableControls;
    adoqueryPicture.First;
    SetLength(Mach, j);
    jpg := TJpegImage.Create();
    for i := 0 to j - 1 do
    begin
      if triM(adoqueryPicture.fieldbyname('FilesOther').AsString) <> '' then
      begin
        myStream := tadoblobstream.Create(tblobfield(adoqueryPicture.fieldbyname('FilesOther')), bmread);
        jpg.LoadFromStream(myStream);
        Mach[i] := TfrmSlt.Create(Self);
        Mach[i].Name := trim(adoqueryPicture.fieldbyname('TFID').AsString);
        Mach[i].Parent := ScrollBox1;
        Mach[i].Left := 0 + i * 165;
        Mach[i].Init(adoqueryPicture.fieldbyname('TFID').AsString, adoqueryPicture.fieldbyname('FileName').AsString, adoqueryPicture.fieldbyname('url').AsString, jpg);
      end;
      adoqueryPicture.Next;
    end;
    adoqueryPicture.EnableControls;
  finally
    jpg.free;
    application.ProcessMessages;
  end;
end;

procedure TfrmBXList.Tv1CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  InitImage();
end;

end.

