unit U_CollectList;

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, cxPC, cxDropDownEdit, cxLookAndFeels, cxLookAndFeelPainters,
  cxNavigator, dxBarBuiltInMenu,  
  dxDateRanges, U_BaseList, Vcl.Menus, Vcl.Clipbrd, cxContainer, dxCore,
  cxDateUtils, dxSkinsCore, dxSkinsDefaultPainters, dxScrollbarAnnotations,
  cxProgressBar, dxSkinWXI;

type
  TfrmCollectList = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    btnDel: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ADOQueryMain: TADOQuery;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    Label3: TLabel;
    OppCoName: TcxTextEdit;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    Tv1FFTime: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    GPM_1: TcxGridPopupMenu;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    btnAdd: TToolButton;
    Tv1OppCoName: TcxGridDBColumn;
    F_Name: TcxTextEdit;
    Label4: TLabel;
    Label6: TLabel;
    P_Code: TcxTextEdit;
    cxTabControl1: TcxTabControl;
    Label10: TLabel;
    ConNo: TcxTextEdit;
    Label11: TLabel;
    BuyConNo: TcxTextEdit;
    Tv1OurCoName: TcxGridDBColumn;
    btnChk: TToolButton;
    btnReChk: TToolButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    ToolButton4: TToolButton;
    Label2: TLabel;
    OppParentCoName: TcxTextEdit;
    ToolButton5: TToolButton;
    N2: TMenuItem;
    btnEdit: TToolButton;
    Tv1OurBankName: TcxGridDBColumn;
    Tv1Amount: TcxGridDBColumn;
    Tv1Currency: TcxGridDBColumn;
    Tv1SSel: TcxGridDBColumn;
    ToolButton7: TToolButton;
    ToolButton8: TToolButton;
    ToolButton9: TToolButton;
    N3: TMenuItem;
    Tv1Note: TcxGridDBColumn;
    ToolButton1: TToolButton;
    Tv1FJFlag: TcxGridDBColumn;
    Label1: TLabel;
    begdate: TcxDateEdit;
    Enddate: TcxDateEdit;
    CheckBox1: TcxCheckBox;
    TBInvoice: TToolButton;
    Tv1ourShuiNo: TcxGridDBColumn;
    Tv1oppShuiNo: TcxGridDBColumn;
    Tv1OurBankCardNo: TcxGridDBColumn;
    Tv1IVNo: TcxGridDBColumn;
    TBReceivable: TToolButton;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure btnDelClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure cxGridDBColumn5PropertiesEditValueChanged(Sender: TObject);
    procedure btnAddClick(Sender: TObject);
    procedure OppCoNameKeyPress(Sender: TObject; var Key: Char);
    procedure cxTabControl1Change(Sender: TObject);
    procedure btnChkClick(Sender: TObject);
    procedure btnReChkClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure btnEditClick(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
    procedure ToolButton9Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure TBInvoiceClick(Sender: TObject);
    procedure TBReceivableClick(Sender: TObject);
  private
    procedure InitGrid();
    procedure SetStatus();
  public
    FAuthority: string;
  end;

implementation

uses
  U_DataLink, U_RTFun, U_CollectInput2, U_CollectInput, U_ZDYHelp, U_CollectWriteOff,
  U_CollectWriteOffList, U_AttachmentUpload, U_CollectImport,U_InvoiceOutSel,U_GetableSel;

{$R *.dfm}
procedure TfrmCollectList.SetStatus();
begin

  btnChk.Enabled := false;     //审核
  btnReChk.Enabled := false;     //撤销
  btnChk.Visible := false;     //审核
  btnReChk.Visible := false;     //撤销

  btnAdd.Enabled := false;     //新增
  btnEdit.Enabled := false;     //修改
  btnDel.Enabled := false;           //删除
//  TBInvoice.Enabled := false;

//  if Trim(FAuthority) = '管理' then
  begin
    btnAdd.Enabled := true;
    case cxTabControl1.TabIndex of
      0:
        begin
          btnEdit.Enabled := true;
          btnDel.Enabled := true;
//          btnChk.Enabled := true;
//          TBInvoice.Enabled := true;
        end;
      1:
        begin
//          btnReChk.Enabled := true;
        end;
      2:
        begin

        end;
    end;
  end;
end;

procedure TfrmCollectList.InitGrid();
var
  WSql: string;
begin
  WSql := SGetFilters(Panel1, 1, 2);
  if Trim(WSql) <> '' then
    WSql := ' and ' + WSql
  else
    CheckBox1.Checked := True;

  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      Filtered := False;
      sql.Add(' select A.*  ');
      sql.Add(',FJFlag=CAST((CASE WHEN (SELECT COUNT(X.FileName) FROM FJ_File X where X.TFType=''SD'' and X.WBID=A.FFID)>0 THEN 1 ELSE 0 END) AS BIT)');
      sql.Add(' from Finance_Flow A ');
      sql.Add(' where A.FFType=''收款登记''  ');
      case cxTabControl1.TabIndex of
        0:
          begin
            sql.Add(' and isnull(status,''0'')=''0'' ');
          end;
        1:
          begin
            sql.Add(' and isnull(status,''0'')=''9'' ');
          end;
      end;
      if CheckBox1.Checked then
      begin
        sql.Add(' and A.FFTime>=''' + FormatDateTime('yyyy-MM-dd', begdate.Date) + ''' ');
        sql.Add(' and FFTime<''' + FormatDateTime('yyyy-MM-dd', Enddate.Date + 1) + ''' ');
      end;
      sql.Add(WSql);
      Open;
    end;
    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);
  finally
    ADOQueryMain.EnableControls;
    TV1.DataController.Filter.Clear;
  end;
end;

procedure TfrmCollectList.N1Click(Sender: TObject);
begin
  SelOKNo(cds_1, true);
end;

procedure TfrmCollectList.N2Click(Sender: TObject);
begin
  Clipboard.SetTextBuf(PChar(Trim(CDS_1.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));
end;

procedure TfrmCollectList.N3Click(Sender: TObject);
begin
  SelOKNo(cds_1, false);

end;

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

procedure TfrmCollectList.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmCollectList.TBInvoiceClick(Sender: TObject);
var
  FFFIDList: TStringList;
  IVID, IVNo: string;
  i: Integer;
  LogMsg: string;
begin
  inherited;

  if CDS_1.IsEmpty then Exit;
  if not CDS_1.Locate('SSel', True, []) then
  begin
    Application.MessageBox('请先勾选需要关联发票的收款记录！', '提示', MB_OK + MB_ICONINFORMATION);
    Exit;
  end;

  try
    frmInvoiceOutSel := TfrmInvoiceOutSel.Create(Application);
    frmInvoiceOutSel.cxTabControl1.TabIndex := 0;
    if frmInvoiceOutSel.ShowModal <> mrOk then
      Exit;
    if frmInvoiceOutSel.CDS_Invoice.IsEmpty then
    begin
      Application.MessageBox('未选择任何发票！', '提示', MB_OK + MB_ICONWARNING);
      Exit;
    end;
    IVID := frmInvoiceOutSel.CDS_Invoice.FieldByName('IVID').AsString;
    IVNo := frmInvoiceOutSel.CDS_Invoice.FieldByName('IVNo').AsString;
    if Trim(IVID) = '' then
    begin
      Application.MessageBox('发票数据异常，IVID 为空！', '提示', MB_OK + MB_ICONERROR);
      Exit;
    end;
  finally
    frmInvoiceOutSel.Free;
  end;

  FFFIDList := TStringList.Create;
  try
    CDS_1.DisableControls;
    CDS_1.First;
    while not CDS_1.Eof do
    begin
      if CDS_1.FieldByName('SSel').AsBoolean then
        FFFIDList.Add(CDS_1.FieldByName('FFID').AsString);
      CDS_1.Next;
    end;

    if FFFIDList.Count = 0 then
    begin
      Application.MessageBox('没有勾选任何收款记录！', '提示', MB_OK + MB_ICONINFORMATION);
      Exit;
    end;

    ADOQueryCmd.Connection.BeginTrans;
    try
      for i := 0 to FFFIDList.Count - 1 do
      begin
        ADOQueryCmd.Close;
        ADOQueryCmd.SQL.Text := 'UPDATE Finance_Flow SET IVID = :IVID, IVNo = :IVNo WHERE FFID = :FFID';
        ADOQueryCmd.Parameters.ParamByName('IVID').Value := IVID;
        ADOQueryCmd.Parameters.ParamByName('IVNo').Value := IVNo;
        ADOQueryCmd.Parameters.ParamByName('FFID').Value := FFFIDList[i];
        ADOQueryCmd.ExecSQL;
      end;

      CDS_1.First;
      while not CDS_1.Eof do
      begin
        if CDS_1.FieldByName('SSel').AsBoolean then
        begin
          CDS_1.Edit;
          CDS_1.FieldByName('IVID').Value := IVID;
          CDS_1.FieldByName('IVNo').Value := IVNo;
          CDS_1.Post;
        end;
        CDS_1.Next;
      end;

      ADOQueryCmd.Connection.CommitTrans;

      LogMsg := Format(
        '批量关联发票成功，发票号：%s，IVID：%s，影响记录数：%d，FFIDs：%s',
        [IVNo, IVID, FFFIDList.Count, FFFIDList.CommaText]
      );
      ADOQueryCmd.Close;
      ADOQueryCmd.SQL.Text :=
        'INSERT INTO SY_Syslog (Operor, OperTime, Model, Acction, OPEvent, Result) ' +
        'VALUES (:Operor, GETDATE(), :Model, :Acction, :OPEvent, :Result)';
      ADOQueryCmd.Parameters.ParamByName('Operor').Value := DName + '(' + DCode + ')';
      ADOQueryCmd.Parameters.ParamByName('Model').Value   := '收款登记';
      ADOQueryCmd.Parameters.ParamByName('Acction').Value := '发票关联';
      ADOQueryCmd.Parameters.ParamByName('OPEvent').Value := LogMsg;
      ADOQueryCmd.Parameters.ParamByName('Result').Value  := '成功';
      ADOQueryCmd.ExecSQL;

      Application.MessageBox('发票关联成功！', '提示', MB_OK + MB_ICONINFORMATION);
      InitGrid;
    except
      on E: Exception do
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        LogMsg := Format(
          '批量关联发票失败，发票号：%s，IVID：%s，影响记录数：%d，FFIDs：%s，原因：%s',
          [IVNo, IVID, FFFIDList.Count, FFFIDList.CommaText, E.Message]
        );
        try
          ADOQueryCmd.Close;
          ADOQueryCmd.SQL.Text :=
            'INSERT INTO SY_Syslog (Operor, OperTime, Model, Acction, OPEvent, Result) ' +
            'VALUES (:Operor, GETDATE(), :Model, :Acction, :OPEvent, :Result)';
          ADOQueryCmd.Parameters.ParamByName('Operor').Value := DName + '(' + DCode + ')';
          ADOQueryCmd.Parameters.ParamByName('Model').Value   := '收款登记';
          ADOQueryCmd.Parameters.ParamByName('Acction').Value := '发票关联';
          ADOQueryCmd.Parameters.ParamByName('OPEvent').Value := LogMsg;
          ADOQueryCmd.Parameters.ParamByName('Result').Value  := '失败';
          ADOQueryCmd.ExecSQL;
        except
        end;
        Application.MessageBox(PChar('发票关联失败：' + E.Message), '错误', MB_OK + MB_ICONERROR);
        InitGrid;
      end;
    end;
  finally
    FFFIDList.Free;
    CDS_1.EnableControls;
  end;
end;

procedure TfrmCollectList.btnDelClick(Sender: TObject);
var
  FFFIDS: string;
begin
  if CDS_1.IsEmpty then
    exit;

  if not CDS_1.Locate('SSel', True, []) then
  begin
    application.MessageBox('请选择数据', '提示');
    Exit;
  end;

  with CDS_1 do
  begin
    DisableControls;
    while CDS_1.Locate('SSel', True, []) do
    begin
      FFFIDS := FFFIDS + Trim(CDS_1.fieldbyname('FFID').AsString) + ',';
      CDS_1.Delete;
    end;
    EnableControls;
  end;

  try
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' exec P_Fin_Flow_Del ');
      sql.Add(' @FFIDS=' + QuotedStr(FFFIDS));
      Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
      Sql.Add(',@DName=' + quotedstr(Trim(DName)));
      open;
    end;
    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
      raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
    application.MessageBox('删除成功', '提示');
  except
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    InitGrid();
  end;

end;

procedure TfrmCollectList.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid(trim(Self.Caption), Tv1, '财务管理');
  Enddate.Date := SGetServerDate(ADOQueryTemp);
  begdate.Date := Enddate.Date - 30;
  SetStatus();
  InitGrid();
end;

procedure TfrmCollectList.TBRafreshClick(Sender: TObject);
begin
  toolbar1.SetFocus;

  InitGrid();

end;

procedure TfrmCollectList.TBReceivableClick(Sender: TObject);
var
  FFFIDList: TStringList;
  ReceivableIDs, ReceivableNos: string;
  i: Integer;
  LogMsg: string;
begin
  inherited;

  if CDS_1.IsEmpty then Exit;
  if not CDS_1.Locate('SSel', True, []) then
  begin
    Application.MessageBox('请先勾选需要关联应收款的收款记录！', '提示', MB_OK + MB_ICONINFORMATION);
    Exit;
  end;

  frmGetableSel := TfrmGetableSel.Create(Application);
  try
    frmGetableSel.cxTabControl1.TabIndex := 0;
    if frmGetableSel.ShowModal <> mrOk then
      Exit;
    if frmGetableSel.FFFIDs = '' then
    begin
      Application.MessageBox('未选择任何应收款！', '提示', MB_OK + MB_ICONWARNING);
      Exit;
    end;
    ReceivableIDs := frmGetableSel.FFFIDs;
    ReceivableNos := frmGetableSel.FFFIDs;
  finally
    frmGetableSel.Free;
  end;

  FFFIDList := TStringList.Create;
  try
    CDS_1.DisableControls;
    CDS_1.First;
    while not CDS_1.Eof do
    begin
      if CDS_1.FieldByName('SSel').AsBoolean then
        FFFIDList.Add(CDS_1.FieldByName('FFID').AsString);
      CDS_1.Next;
    end;

    if FFFIDList.Count = 0 then
    begin
      Application.MessageBox('没有勾选任何收款记录！', '提示', MB_OK + MB_ICONINFORMATION);
      Exit;
    end;

    ADOQueryCmd.Connection.BeginTrans;
    try
      for i := 0 to FFFIDList.Count - 1 do
      begin
        ADOQueryCmd.Close;
        ADOQueryCmd.SQL.Text := 'UPDATE Finance_Flow SET IVID = :IVID, IVNo = :IVNo WHERE FFID = :FFID';
        ADOQueryCmd.Parameters.ParamByName('IVID').Value := ReceivableIDs;
        ADOQueryCmd.Parameters.ParamByName('IVNo').Value := ReceivableNos;
        ADOQueryCmd.Parameters.ParamByName('FFID').Value := FFFIDList[i];
        ADOQueryCmd.ExecSQL;
      end;

      CDS_1.First;
      while not CDS_1.Eof do
      begin
        if CDS_1.FieldByName('SSel').AsBoolean then
        begin
          CDS_1.Edit;
          CDS_1.FieldByName('IVID').Value := ReceivableIDs;
          CDS_1.FieldByName('IVNo').Value := ReceivableNos;
          CDS_1.Post;
        end;
        CDS_1.Next;
      end;

      ADOQueryCmd.Connection.CommitTrans;

      LogMsg := Format('批量关联应收款成功，应收款IDs：%s，影响收款记录数：%d',
        [ReceivableIDs, FFFIDList.Count]);
      ADOQueryCmd.Close;
      ADOQueryCmd.SQL.Text := 'INSERT INTO SY_Syslog (Operor, OperTime, Model, Acction, OPEvent, Result) ' +
                              'VALUES (:Operor, GETDATE(), :Model, :Acction, :OPEvent, :Result)';
      ADOQueryCmd.Parameters.ParamByName('Operor').Value := DName + '(' + DCode + ')';
      ADOQueryCmd.Parameters.ParamByName('Model').Value   := '收款登记';
      ADOQueryCmd.Parameters.ParamByName('Acction').Value := '应收款关联';
      ADOQueryCmd.Parameters.ParamByName('OPEvent').Value := LogMsg;
      ADOQueryCmd.Parameters.ParamByName('Result').Value  := '成功';
      ADOQueryCmd.ExecSQL;

      Application.MessageBox('应收款关联成功！', '提示', MB_OK + MB_ICONINFORMATION);
      InitGrid;
    except
      on E: Exception do
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        LogMsg := Format('批量关联应收款失败，应收款IDs：%s，影响记录数：%d，原因：%s',
          [ReceivableIDs, FFFIDList.Count, E.Message]);
        try
          ADOQueryCmd.Close;
          ADOQueryCmd.SQL.Text := 'INSERT INTO SY_Syslog (Operor, OperTime, Model, Acction, OPEvent, Result) ' +
                                  'VALUES (:Operor, GETDATE(), :Model, :Acction, :OPEvent, :Result)';
          ADOQueryCmd.Parameters.ParamByName('Operor').Value := DName + '(' + DCode + ')';
          ADOQueryCmd.Parameters.ParamByName('Model').Value   := '收款登记';
          ADOQueryCmd.Parameters.ParamByName('Acction').Value := '应收款关联';
          ADOQueryCmd.Parameters.ParamByName('OPEvent').Value := LogMsg;
          ADOQueryCmd.Parameters.ParamByName('Result').Value  := '失败';
          ADOQueryCmd.ExecSQL;
        except
        end;
        Application.MessageBox(PChar('关联失败：' + E.Message), '错误', MB_OK + MB_ICONERROR);
        InitGrid;
      end;
    end;
  finally
    FFFIDList.Free;
    CDS_1.EnableControls;
  end;
end;

procedure TfrmCollectList.cxGridDBColumn5PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  FPS, FQty, FPrice, FQTFee, FMoney, FZKMoney: Double;
begin
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  mvalue := TcxTextEdit(Sender).EditingText;
  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;

  with CDS_1 do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    Post;
    FPS := CDS_1.fieldbyname('PS').AsFloat;
    FQty := CDS_1.fieldbyname('Qty').AsFloat;
    FQTFee := CDS_1.fieldbyname('QTFee').AsFloat;
    FPrice := CDS_1.fieldbyname('Price').AsFloat;
    FMoney := CDS_1.fieldbyname('Money').AsFloat;

    FZKMoney := CDS_1.fieldbyname('ZKMoney').AsFloat;
  end;

  if Trim(FFieldName) <> 'Money' then
  begin
    FMoney := FQty * FPrice + FQTFee - FZKMoney;
  end;
  with CDS_1 do
  begin
    Edit;
    FieldByName('Money').Value := FMoney;
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('update Finance_Flow set ' + FFieldName + '=' + Trim(mvalue));
    if Trim(FFieldName) <> 'Money' then
      sql.Add(' ,Money=' + FloatToStr(FMoney));
    sql.Add('where FFID=' + quotedstr(Trim(CDS_1.fieldbyname('FFID').AsString)));
    ExecSQL;
  end;

  Tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmCollectList.btnChkClick(Sender: TObject);
var
  FFFIDS: string;
begin
  if CDS_1.IsEmpty then
    exit;

  if not CDS_1.Locate('SSel', True, []) then
  begin
    application.MessageBox('请选择数据', '提示');
    Exit;
  end;

  with CDS_1 do
  begin
    DisableControls;
    while CDS_1.Locate('SSel', True, []) do
    begin
      FFFIDS := FFFIDS + Trim(CDS_1.fieldbyname('FFID').AsString) + ',';
      CDS_1.Delete;
    end;
    EnableControls;
  end;

  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' exec P_Fin_Flow_SetStatus ');
      sql.Add(' @FFIDS=' + QuotedStr(FFFIDS));
      sql.Add(',@Operation=''审核'' ');
      Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
      Sql.Add(',@DName=' + quotedstr(Trim(DName)));
      open;
    end;
    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
      raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' exec P_Fin_In_Bank_Flow ');
      sql.Add(' @FFIDS=' + QuotedStr(FFFIDS));
      Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
      Sql.Add(',@DName=' + quotedstr(Trim(DName)));
      open;
    end;
    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
      raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
    ADOQueryCmd.Connection.CommitTrans;
    application.MessageBox('审核成功', '提示');
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    InitGrid();
  end;

end;

procedure TfrmCollectList.btnReChkClick(Sender: TObject);
var
  FFFIDS: string;
begin
  if CDS_1.IsEmpty then
    exit;

  if not CDS_1.Locate('SSel', True, []) then
  begin
    application.MessageBox('请选择数据', '提示');
    Exit;
  end;

  with CDS_1 do
  begin
    DisableControls;
    while CDS_1.Locate('SSel', True, []) do
    begin
      FFFIDS := FFFIDS + Trim(CDS_1.fieldbyname('FFID').AsString) + ',';
      CDS_1.Delete;
    end;
    EnableControls;
  end;

  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' exec P_Fin_Flow_SetStatus ');
      sql.Add(' @FFIDS=' + QuotedStr(FFFIDS));
      sql.Add(',@Operation=''撤销审核'' ');
      Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
      Sql.Add(',@DName=' + quotedstr(Trim(DName)));
      open;
    end;
    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
      raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' exec P_Fin_In_Bank_Flow ');
      sql.Add(' @FFIDS=' + QuotedStr(FFFIDS));
      Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
      Sql.Add(',@DName=' + quotedstr(Trim(DName)));
      open;
    end;
    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
      raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
    ADOQueryCmd.Connection.CommitTrans;

    application.MessageBox('撤销审核成功', '提示');
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    InitGrid();
  end;
end;

procedure TfrmCollectList.btnAddClick(Sender: TObject);
begin

  try
    frmCollectInput2 := TfrmCollectInput2.Create(Application);
    with frmCollectInput2 do
    begin
      FFFID := '';
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmCollectInput2.Free;
  end;
end;

procedure TfrmCollectList.ToolButton1Click(Sender: TObject);
begin
  try
    frmCollectImport := TfrmCollectImport.Create(Application);
    with frmCollectImport do
    begin

      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmCollectImport.Free;
  end;

end;

procedure TfrmCollectList.ToolButton4Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  TcxGridToExcel(self.Caption, cxgrid1);
end;

procedure TfrmCollectList.ToolButton5Click(Sender: TObject);
begin
  WriteCxGrid(trim(Self.Caption), Tv1, '财务管理');
end;

procedure TfrmCollectList.btnEditClick(Sender: TObject);
begin
  try
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' exec P_Fin_Flow_Judge ');
      sql.Add(' @FFIDS=' + QuotedStr(Trim(SELF.CDS_1.fieldbyname('FFID').AsString)));
      Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
      Sql.Add(',@DName=' + quotedstr(Trim(DName)));
      open;
    end;
    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
      raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
  except
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    Exit;
  end;

  try
    frmCollectInput2 := TfrmCollectInput2.Create(Application);
    with frmCollectInput2 do
    begin
      FFFID := Trim(SELF.CDS_1.fieldbyname('FFID').AsString);
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmCollectInput2.Free;
  end;
end;

procedure TfrmCollectList.ToolButton7Click(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    exit;

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select * from Finance_Flow');
    sql.Add(' where FFID=''' + Trim(CDS_1.fieldbyname('FFID').AsString) + '''');
    SQL.Add(' and status=''9''  ');
    Open;
  end;
  if ADOQueryTemp.IsEmpty then
  begin
    Application.MessageBox('未审核数据，不能操作!', '提示', 0);
    Exit;
  end;

  frmCollectWriteOff := TfrmCollectWriteOff.create(self);
  with frmCollectWriteOff do
  begin
    FFFID := CDS_1.fieldbyname('FFID').AsString;
    FOppCoNo := CDS_1.fieldbyname('OppCoNo').AsString;
    FOurCoName := CDS_1.fieldbyname('OurCoName').AsString;
    if showmodal = 1 then
    begin

    end;
    free;
  end;

end;

procedure TfrmCollectList.ToolButton8Click(Sender: TObject);
begin
  frmCollectWriteOffList := TfrmCollectWriteOffList.create(self);
  with frmCollectWriteOffList do
  begin
    FromId.text := Self.CDS_1.fieldbyname('FFID').AsString;
    if showmodal = 1 then
    begin

    end;
    free;
  end;

end;

procedure TfrmCollectList.ToolButton9Click(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    Exit;
  try
    frmAttachmentUpload := TfrmAttachmentUpload.Create(Application);
    with frmAttachmentUpload do
    begin
      if Trim(Self.CDS_1.fieldbyname('filler').AsString) = dname then
        FEditAuthority := True;
      fkeyNO := Trim(Self.CDS_1.fieldbyname('FFID').AsString);
      fType := 'SD';
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmAttachmentUpload.Free;
  end;

end;

procedure TfrmCollectList.OppCoNameKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    InitGrid();
  end;
end;

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

end.

