unit U_TaxList;

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, dxSkinBasic, dxSkinBlack, dxSkinBlue, dxSkinBlueprint,
  dxSkinCaramel, dxSkinCoffee, dxSkinDarkroom, dxSkinDarkSide,
  dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle, dxSkinFoggy,
  dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary, dxSkinLilian,
  dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMetropolis,
  dxSkinMetropolisDark, dxSkinMoneyTwins, dxSkinOffice2007Black,
  dxSkinOffice2007Blue, dxSkinOffice2007Green, dxSkinOffice2007Pink,
  dxSkinOffice2007Silver, dxSkinOffice2010Black, dxSkinOffice2010Blue,
  dxSkinOffice2010Silver, dxSkinOffice2013DarkGray, dxSkinOffice2013LightGray,
  dxSkinOffice2013White, dxSkinOffice2016Colorful, dxSkinOffice2016Dark,
  dxSkinOffice2019Black, dxSkinOffice2019Colorful, dxSkinOffice2019DarkGray,
  dxSkinOffice2019White, dxSkinPumpkin, dxSkinSeven, dxSkinSevenClassic,
  dxSkinSharp, dxSkinSharpPlus, dxSkinSilver, dxSkinSpringtime, dxSkinStardust,
  dxSkinSummer2008, dxSkinTheAsphaltWorld, dxSkinTheBezier, dxSkinValentine,
  dxSkinVisualStudio2013Blue, dxSkinVisualStudio2013Dark,
  dxSkinVisualStudio2013Light, dxSkinVS2010, dxSkinWhiteprint,
  dxSkinXmas2008Blue;

type
  TfrmTaxList = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    btnDel: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ADOQueryMain: TADOQuery;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    GPM_1: TcxGridPopupMenu;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    btnAdd: TToolButton;
    btnChk: TToolButton;
    btnReChk: TToolButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    N2: TMenuItem;
    btnEdit: TToolButton;
    ToolButton9: TToolButton;
    N3: TMenuItem;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    ToolButton6: TToolButton;
    ToolButton7: TToolButton;
    CheckBox1: TCheckBox;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    Tv1Column22: TcxGridDBColumn;
    Tv1Column12: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    Tv1Column13: TcxGridDBColumn;
    Tv1Column14: TcxGridDBColumn;
    Tv1Column15: TcxGridDBColumn;
    Tv1Column16: TcxGridDBColumn;
    Tv1Column17: TcxGridDBColumn;
    Tv1Column18: TcxGridDBColumn;
    Tv1Column19: TcxGridDBColumn;
    Tv1Column20: TcxGridDBColumn;
    Tv1Column21: TcxGridDBColumn;
    Tv1Column23: TcxGridDBColumn;
    Tv1Column24: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Panel2: TPanel;
    SetDate1: TDateTimePicker;
    Panel3: TPanel;
    Button4: TButton;
    Button1: TButton;
    Panel4: TPanel;
    SetDate2: TDateTimePicker;
    Panel5: TPanel;
    Button3: TButton;
    Button2: TButton;
    Label11: TLabel;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    COrderNo: TEdit;
    CBusiness_unit: TEdit;
    CCustomer: TEdit;
    Tv1Column25: TcxGridDBColumn;
    BuyConNo: TEdit;
    Label4: TLabel;
    Tv1Column26: TcxGridDBColumn;
    ToolButton8: TToolButton;
    Tv1Column27: TcxGridDBColumn;
    ToolButton10: TToolButton;
    ToolButton11: TToolButton;
    Tv1Column28: TcxGridDBColumn;
    Tv1Column29: TcxGridDBColumn;
    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 ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure btnEditClick(Sender: TObject);
    procedure ToolButton9Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure ToolButton8Click(Sender: TObject);
    procedure ToolButton10Click(Sender: TObject);
    procedure ToolButton11Click(Sender: TObject);
  private
    procedure InitGrid();
    procedure SetStatus();
  public
    FAuthority: string;
  end;

implementation

uses
  U_DataLink, U_RTFun, U_TaxInput, U_ZDYHelp, U_AttachmentUpload, U_FinFileUp;

{$R *.dfm}
procedure TfrmTaxList.SetStatus();
begin
  ToolButton3.Visible := false;
end;

procedure TfrmTaxList.InitGrid();
var
  WSql: string;
begin
//  else
//    CheckBox1.Checked := True;

  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      Filtered := False;
//      sql.Add(' select IsTP=cast((case when (select COUNT(T.WBID) from TP_File T where T.WBID=A.FFID)=0 then 0 else 1 end) as bit )  ');
//      sql.ADD(',A.*,B.account,B.OpenBank from Finance_Flow A');
//      sql.Add('  Left join BS_Company_Bank B on A.OppBankID=B.CBID  ');
//      sql.Add(' where (A.FFType=''付款登记'' Or ISNULL(A.RegType,'''')=''收款'')');

      SQL.add('select *, OrdDate = (select OrdDate from Trade_Plan_Main x where x.OrderNo = a.COrderNo) ');
      SQL.Add(' ,BuyConNo = (select BuyConNo from V_Trade_Plan_Single_2 X where COrderNo = X.OrderNo )');
      SQL.Add(' ,status = (select status from Trade_plan_main X where COrderNo = X.OrderNo )');
      SQL.Add(' from V_Costing_Table a ');
      SQL.add('where 1=1');
      if CheckBox1.Checked then
      begin
        sql.Add(' and CZDDate>=''' + FormatDateTime('yyyy-MM-dd', begdate.DateTime) + ''' ');
        sql.Add(' and CZDDate<''' + FormatDateTime('yyyy-MM-dd', Enddate.DateTime + 1) + ''' ');
      end;
//      showmessage(sql.text);
      Open;
    end;
    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);
  finally
    ADOQueryMain.EnableControls;
    TV1.DataController.Filter.Clear;
  end;
end;

procedure TfrmTaxList.N1Click(Sender: TObject);
begin
  SelOKNo(cds_1, True);

end;

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

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

end;

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

procedure TfrmTaxList.TBCloseClick(Sender: TObject);
begin

  Close;
end;

//procedure TfrmTaxList.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('CostMID').AsString) + ',';
//      CDS_1.Delete;
//    end;
//    EnableControls;
//  end;
//
//  try
//    ADOQueryCmd.Connection.BeginTrans;
//    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)));
//    ADOQueryCmd.Connection.CommitTrans;
//    application.MessageBox('删除成功', '提示');
//  except
//    ADOQueryCmd.Connection.RollbackTrans;
//    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
//    InitGrid();
//  end;
//
//end;

procedure TfrmTaxList.btnDelClick(Sender: TObject);
var
  FFFIDS: string;
  SQL: string;
  bSuccess: Boolean;
  IDs: TStringList;
  i: Integer;
begin
  if CDS_1.IsEmpty then
    Exit;

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

  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;

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

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

  with CDS_1 do
  begin
    DisableControls;
    try
      while Locate('SSel', True, []) do
      begin
        FFFIDS := FFFIDS + QuotedStr(Trim(FieldByName('CostMID').AsString)) + ',';
        Delete;
      end;
    finally
      EnableControls;
    end;
  end;
  if (FFFIDS <> '') and (FFFIDS[Length(FFFIDS)] = ',') then
    Delete(FFFIDS, Length(FFFIDS), 1);

  if FFFIDS = '' then
    Exit;
  bSuccess := False;
  try
    ADOQueryCmd.Connection.BeginTrans;
    try
      SQL := 'DELETE FROM Costing_Table WHERE CostMID IN (' + FFFIDS + ')';
      ADOQueryCmd.SQL.Text := SQL;
      ADOQueryCmd.ExecSQL;

      ADOQueryCmd.Connection.CommitTrans;
      bSuccess := True;
      InitGrid();
    except
      ADOQueryCmd.Connection.RollbackTrans;
      raise;
    end;
  except
    on E: Exception do
    begin
      Application.MessageBox(PChar('删除失败: ' + E.Message), '提示信息', MB_ICONERROR);
      InitGrid();
    end;
  end;

  if bSuccess then
    Application.MessageBox('删除成功', '提示', MB_OK);
end;

procedure TfrmTaxList.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid(trim(Self.Caption), Tv1, '财务管理');
  Enddate.DateTime := SGetServerDate(ADOQueryTemp);
  SetDate1.DateTime := SGetServerDate(ADOQueryTemp);
  SetDate2.DateTime := SGetServerDate(ADOQueryTemp);
  //begdate.DateTime := Enddate.DateTime - 91;
  EndDate.DateTime := SGetServerDate(ADOQueryTemp);
  BegDate.DateTime := SGetServerDate(ADOQueryTemp) - 91;
  FAuthority := self.fParameters1;
  SetStatus();
  InitGrid();
end;

procedure TfrmTaxList.TBRafreshClick(Sender: TObject);
begin
  begdate.SetFocus;

  InitGrid();

end;

procedure TfrmTaxList.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 TfrmTaxList.btnChkClick(Sender: TObject);
//begin
//  Panel2.Left := (self.width - Panel2.width) div 2;
//  Panel2.Visible := True;
//end;
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('COrderNo').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
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      SQL.Add(' exec P_Fin_Flow_SetStatus_1 ');
      SQL.Add(' @FFIDS=' + QuotedStr(FFFIDS));
      SQL.Add(',@Operation=''审核'' ');
      SQL.Add(',@SetDate=''' + FormatDateTime('yyyy-MM-dd', Setdate1.DateTime) + ''' ');
      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('审核成功', '提示');
    Panel2.Visible := False;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    InitGrid();
//    Panel2.Visible := False;
  end;

end;

procedure TfrmTaxList.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_1 ');
      SQL.Add(' @FFIDS=' + QuotedStr(FFFIDS));
      SQL.Add(',@Operation=''撤销审核'' ');
      SQL.Add(',@SetDate=''' + FormatDateTime('yyyy-MM-dd', Setdate1.DateTime) + ''' ');
      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 TfrmTaxList.Button1Click(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_1 ');
      SQL.Add(' @FFIDS=' + QuotedStr(FFFIDS));
      SQL.Add(',@Operation=''审核'' ');
      SQL.Add(',@SetDate=''' + FormatDateTime('yyyy-MM-dd', Setdate1.DateTime) + ''' ');
      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('审核成功', '提示');
    Panel2.Visible := False;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    InitGrid();
    Panel2.Visible := False;
  end;

end;

procedure TfrmTaxList.Button2Click(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_1 ');
      SQL.Add(' @FFIDS=' + QuotedStr(FFFIDS));
      SQL.Add(',@Operation=''付款'' ');
      SQL.Add(',@SetDate=''' + FormatDateTime('yyyy-MM-dd', Setdate2.DateTime) + ''' ');
      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('付款成功', '提示');
    Panel4.Visible := False;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    InitGrid();
    Panel4.Visible := False;
  end;

end;

procedure TfrmTaxList.Button3Click(Sender: TObject);
begin
//  inherited;
  Panel4.Visible := False;
end;

procedure TfrmTaxList.Button4Click(Sender: TObject);
begin
  Panel2.Visible := False;

end;

procedure TfrmTaxList.btnAddClick(Sender: TObject);
begin

  try
    frmTaxInput := TfrmTaxInput.Create(Application);
    with frmTaxInput do
    begin
      PState := '新增';
      FFFID := '';
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmTaxInput.Free;
  end;
end;

procedure TfrmTaxList.ToolButton10Click(Sender: TObject);
begin
  inherited;

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

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

  with ADOQueryCmd do
  begin
    close;
    SQL.Clear;
    SQL.add(' update Costing_Table set HYStatus = 1 where CostMid = ' + quotedstr(CDS_1.FieldByName('CostMid').AsString));
    ExecSQL;
  end;
  InitGrid();
end;

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

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

  with ADOQueryCmd do
  begin
    close;
    SQL.Clear;
    SQL.add(' update Costing_Table set HYStatus = 0 where CostMid = ' + quotedstr(CDS_1.FieldByName('CostMid').AsString));
    ExecSQL;
  end;
  InitGrid();
end;

procedure TfrmTaxList.ToolButton1Click(Sender: TObject);
begin
  Panel4.Left := (self.width - Panel4.width) div 2;
  Panel4.Visible := True;

end;

procedure TfrmTaxList.ToolButton2Click(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_1 ');
//      sql.Add(' @FFIDS=' + QuotedStr(FFFIDS));
//      sql.Add(',@Operation=''撤销付款'' ');
//      Sql.Add(',@SetDate=''' + FormatDateTime('yyyy-MM-dd', Setdate2.DateTime) + ''' ');
//      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 TfrmTaxList.ToolButton3Click(Sender: TObject);
begin
  try
    frmTaxInput := TfrmTaxInput.Create(Application);
    with frmTaxInput do
    begin
      FFFID := Trim(SELF.CDS_1.fieldbyname('CostMID').AsString);
      PState := '复制';
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmTaxInput.Free;
  end;

end;

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

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

procedure TfrmTaxList.ToolButton6Click(Sender: TObject);
begin
  try
    frmFinFileUp := TfrmFinFileUp.Create(Application);
    with frmFinFileUp do
    begin
//      Code.Text := Trim(Self.CDS_1.fieldbyname('CoCode').AsString);
      FBCIID := Trim(Self.CDS_1.fieldbyname('FFID').AsString);
      if ShowModal = 1 then
      begin
        Self.InitGrid();
//        Self.CDS_1.Locate('BCIID', BCIID, []);
      end;
    end;
  finally
    frmFinFileUp.Free;
  end;

end;

procedure TfrmTaxList.ToolButton7Click(Sender: TObject);
begin
  try
    frmTaxInput := TfrmTaxInput.Create(Application);
    with frmTaxInput do
    begin
      FFFID := Trim(SELF.CDS_1.fieldbyname('CostMID').AsString);
      ToolButton3.Visible := false;
      ToolButton2.Visible := false;
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmTaxInput.Free;
  end;

end;

procedure TfrmTaxList.ToolButton8Click(Sender: TObject);
begin
  inherited;
  if CDS_1.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);
  end;
end;

procedure TfrmTaxList.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;

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

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

  try
    frmTaxInput := TfrmTaxInput.Create(Application);
    with frmTaxInput do
    begin
      FFFID := Trim(SELF.CDS_1.fieldbyname('CostMID').AsString);
      PState := '修改';
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmTaxInput.Free;
  end;
end;

procedure TfrmTaxList.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 TfrmTaxList.Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  mColumn: TcxGridDBColumn;
  mvalue: Variant;
begin
  if AViewInfo.GridRecord.Values[tv1.GetColumnByFieldName('RegType').Index] = '收款' then
  begin
    ACanvas.Brush.Color := $00FF8080;
  end;

  if AViewInfo.GridRecord.Values[tv1.GetColumnByFieldName('status').Index] = '12' then
  begin
    ACanvas.Brush.Color := clMoneyGreen;
  end;

  mColumn := Tv1.GetColumnByFieldName('HYStatus');
  if mColumn = nil then
    exit;
  mvalue := AViewInfo.GridRecord.Values[mColumn.index];
  if mvalue = true then
  begin
    ACanvas.Font.Color := clBlue;
//    Exit;
  end;

end;

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

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

end.

