unit U_ReceivableList;

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, dxSkinsCore, dxSkinsDefaultPainters,
  dxDateRanges, U_BaseList, Vcl.Menus, Vcl.Clipbrd, dxSkinWXI,
  dxScrollbarAnnotations, cxCalc;

type
  TfrmReceivableList = 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: TEdit;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v2CRTime: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    GPM_1: TcxGridPopupMenu;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    btnAdd: TToolButton;
    v2FactoryName: TcxGridDBColumn;
    F_Name: TEdit;
    Label4: TLabel;
    CheckBox1: TCheckBox;
    Label6: TLabel;
    F_Code: TEdit;
    v2OrderNo: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    Label10: TLabel;
    ConNo: TEdit;
    Label11: TLabel;
    BuyConNo: TEdit;
    v2Column2: TcxGridDBColumn;
    btnChk: TToolButton;
    btnReChk: TToolButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    ToolButton4: TToolButton;
    Label2: TLabel;
    OppParentCoName: TEdit;
    ToolButton5: TToolButton;
    N2: TMenuItem;
    btnEdit: TToolButton;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    Tv1Column12: TcxGridDBColumn;
    Tv1Column13: TcxGridDBColumn;
    Tv1Column14: TcxGridDBColumn;
    Tv1Column15: TcxGridDBColumn;
    Tv1Column16: TcxGridDBColumn;
    Tv1Column17: TcxGridDBColumn;
    Tv1Column18: TcxGridDBColumn;
    Tv1Column19: TcxGridDBColumn;
    Tv1Column20: TcxGridDBColumn;
    Tv1Column21: TcxGridDBColumn;
    Tv1Column22: TcxGridDBColumn;
    Tv1Column23: TcxGridDBColumn;
    N3: TMenuItem;
    Tv1Column24: TcxGridDBColumn;
    Tv1Column25: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    ToolButton1: TToolButton;
    Tv1Column2: TcxGridDBColumn;
    Label1: TLabel;
    OrderNo: TEdit;
    Tv1Column26: TcxGridDBColumn;
    Tv1Column27: TcxGridDBColumn;
    Tv1Column28: TcxGridDBColumn;
    Tv1Column29: TcxGridDBColumn;
    Tv1Column30: TcxGridDBColumn;
    ToolButton2: TToolButton;
    Tv1Column31: TcxGridDBColumn;
    Tv1Column32: TcxGridDBColumn;
    Tv1Column33: TcxGridDBColumn;
    ToolButton3: TToolButton;
    Tv1Column34: TcxGridDBColumn;
    FFID: TEdit;
    Label5: TLabel;
    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 N3Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
  private
    procedure InitGrid();
    procedure SetStatus();
  public
    FAuthority: string;
    MFFIDS: string;
  end;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_ReceivableInput, U_ReceivableEdit,
  U_CollectWriteOffList;

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

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

  btnAdd.Enabled := false;     //新增
  btnEdit.Enabled := false;     //修改
  btnDel.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;
        end;
      1:
        begin
          btnReChk.Enabled := true;
        end;
      2:
        begin

        end;
    end;
  end;
end;

procedure TfrmReceivableList.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 CONVERT(varchar,A.SailingSchedule,23) SailingSchedule,A.*,Saleser=isnull((select Saleser from BS_Company X where X.CoName=A.OppCoName), (SELECT Saleser FROM Trade_Plan_Main Y WHERE Y.OrderNo = A.OrderNo)) ');
      //sql.Add(' select CONVERT(varchar,A.SailingSchedule,23) SailingSchedule,A.*,Saleser=(select Saleser from BS_Company X where X.CoID=A.OppCoName)');
      sql.Add(' select CONVERT(varchar,A.SailingSchedule,23) SailingSchedule,A.*,');
      sql.Add(' Saleser=(select TOP 1 Saleser from BS_Company BC where BC.CoName=A.OppCoName OR BC.CoAbbrName=A.OppCoName)');
      sql.Add(' ,JHSXF=ISNULL((select SUM(FWO1.WoSXFAmount) from Finance_WriteOff FWO1 where FWO1.ToId=A.FFID ), 0)');
      sql.Add(' ,SSAmount=ISNULL((select SUM(FWO2.WoAmount)-SUM(FWO2.WoSXFAmount) from Finance_WriteOff FWO2 where FWO2.ToId=A.FFID ), 0)');
      sql.Add(' ,SSCNYAmount=ISNULL((select SUM(FWO3.WoAmount*FWO3.WoJHExchangeRate)-SUM(FWO3.WoSXFAmount*FWO3.WoJHExchangeRate) from Finance_WriteOff FWO3 where FWO3.ToId=A.FFID ), 0)');
      sql.Add(' from Finance_Flow A where A.FFType=''应收款登记''');
//      SQL.Add('SELECT ');
//      SQL.Add('    CONVERT(varchar, A.SailingSchedule, 23) AS SailingSchedule,');
//      SQL.Add('    A.*,');
//      //SQL.Add('    A.OrderNo,');
//      SQL.Add('    Saleser = COALESCE(');
//      SQL.Add('        (SELECT TOP 1 Saleser FROM BS_Company X WHERE X.CoName = A.OppCoName OR X.CoAbbrName = A.OppCoName),');
//      SQL.Add('        CASE ');
//      SQL.Add('            WHEN A.OrderNo IS NOT NULL ');
//      SQL.Add('            THEN (SELECT TOP 1 Saleser FROM Trade_Plan_Main Y WHERE Y.OrderNo = A.OrderNo)');
//      SQL.Add('            ELSE NULL');
//      SQL.Add('        END');
//      SQL.Add('    ),');
//      SQL.Add('    JHSXF = ISNULL((SELECT SUM(X.WoSXFAmount) FROM Finance_WriteOff X WHERE X.ToId = A.FFID), 0),');
//      SQL.Add('    SSAmount = ISNULL((SELECT SUM(X.WoAmount) - SUM(X.WoSXFAmount) FROM Finance_WriteOff X WHERE X.ToId = A.FFID), 0),');
//      SQL.Add('    SSCNYAmount = ISNULL((SELECT SUM(X.WoAmount * X.WoJHExchangeRate) - SUM(X.WoSXFAmount * X.WoJHExchangeRate) FROM Finance_WriteOff X WHERE X.ToId = A.FFID), 0)');
//      SQL.Add('FROM ');
//      SQL.Add('    Finance_Flow A ');
//      SQL.Add('WHERE ');
//      SQL.Add('    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.DateTime) + ''' ');
        sql.Add(' and FFTime<''' + FormatDateTime('yyyy-MM-dd', Enddate.DateTime + 1) + ''' ');
      end;
      sql.Add(WSql);
//      showmessage(sql.text);
      Open;
    end;
    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);
  finally
    ADOQueryMain.EnableControls;
    TV1.DataController.Filter.Clear;
  end;

  if MFFIDS <> '' then
    CDS_1.Locate('FFID', MFFIDS, []);
  MFFIDS := '';
end;

procedure TfrmReceivableList.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_1, true)
end;

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

procedure TfrmReceivableList.N3Click(Sender: TObject);
begin
  SelOKNo(CDS_1, false)

end;

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

procedure TfrmReceivableList.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmReceivableList.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 ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from Trade_Plan_Main where OrderNo = ' + QuotedStr(CDS_1.FieldByName('OrderNo').AsString) + ' and status in(''10'', ''12'') ');
    open;
  end;

  if not ADOQueryTemp.IsEmpty 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 TfrmReceivableList.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid(trim(Self.Caption), Tv1, '财务管理');
  Enddate.DateTime := SGetServerDate(ADOQueryTemp);
  begdate.DateTime := Enddate.DateTime - 91;
  SetStatus();
  InitGrid();
end;

procedure TfrmReceivableList.TBRafreshClick(Sender: TObject);
begin
  begdate.SetFocus;
  with ADOQueryCmd do
  begin
    Close;
    Sql.Clear;
    Sql.Add('exec P_Fin_Generate_BQ ');
    Open;
  end;
  if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
  begin
    application.MessageBox(pchar(ADOQueryCmd.FieldByName('ShowMsg').AsString), '提示信息');
    Exit;
  end;
  InitGrid();

end;

procedure TfrmReceivableList.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 TfrmReceivableList.btnChkClick(Sender: TObject);
var
  FFFIDS: string;
begin
  if CDS_1.IsEmpty then
    exit;

//  if not VarIsNull(CDS_1.FieldByName('Price').Value) then
//  begin
//    application.MessageBox('单价不能为空', '提示');
//    Exit;
//  end;

  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_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)));
    application.MessageBox('审核成功', '提示');
  except
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    InitGrid();
  end;

end;

procedure TfrmReceivableList.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
      with ADOQueryTemp do
      begin
        Close;
        SQL.Clear;
        SQL.Add(' select * from Trade_Plan_Main where OrderNo = ' + QuotedStr(CDS_1.FieldByName('OrderNo').AsString) + ' and status in(''10'', ''12'') ');
        open;
      end;

      if ADOQueryTemp.IsEmpty = false then
      begin
        application.MessageBox(PChar('计划单' + CDS_1.FieldByName('OrderNo').AsString + '已完成或已清算，无法操作'), '提示');
        Exit;
      end;
      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_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)));
    application.MessageBox('撤销审核成功', '提示');
  except
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    InitGrid();
  end;
end;

procedure TfrmReceivableList.btnAddClick(Sender: TObject);
begin

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

procedure TfrmReceivableList.ToolButton1Click(Sender: TObject);
var
  MFFIDS: string;
begin
  if CDS_1.IsEmpty then
    exit;

  if not CDS_1.Locate('SSel', True, []) then
  begin
    application.MessageBox('请选择数据', '提示');
    Exit;
  end;
//
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from Trade_Plan_Main where OrderNo = ' + QuotedStr(CDS_1.FieldByName('OrderNo').AsString) + ' and status in(''10'', ''12'') ');
    open;
  end;

  if not ADOQueryTemp.IsEmpty then
  begin
    application.MessageBox('计划单已完成或已清算，单价无法修改', '提示');
    Exit;
  end;

  MFFIDS := '';
  with CDS_1 do
  begin
    DisableControls;
    while CDS_1.Locate('SSel', True, []) do
    begin

      MFFIDS := MFFIDS + ',' + 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_Judge ');
      sql.Add(' @FFIDS=' + QuotedStr(MFFIDS));
      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
    frmReceivableEdit := TfrmReceivableEdit.Create(Application);
    with frmReceivableEdit do
    begin
      FFFIDS := MFFIDS;
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmReceivableEdit.Free;
  end;
end;

procedure TfrmReceivableList.ToolButton2Click(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
    frmReceivableInput := TfrmReceivableInput.Create(Application);
    with frmReceivableInput do
    begin
      FFFID := Trim(SELF.CDS_1.fieldbyname('FFID').AsString);
      PState := '复制';
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmReceivableInput.Free;
  end;

end;

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

    end;
    free;
  end;
end;

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

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

procedure TfrmReceivableList.btnEditClick(Sender: TObject);
begin
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from Trade_Plan_Main where OrderNo = ' + QuotedStr(CDS_1.FieldByName('OrderNo').AsString) + ' and status in(''10'', ''12'') ');
    open;
  end;

  if not ADOQueryTemp.IsEmpty then
  begin
    application.MessageBox('计划单已完成或已清算，无法修改', '提示');
    Exit;
  end;

  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
    frmReceivableInput := TfrmReceivableInput.Create(Application);
    with frmReceivableInput do
    begin
      FFFID := Trim(SELF.CDS_1.fieldbyname('FFID').AsString);
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmReceivableInput.Free;
  end;
end;

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

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

end.

