unit U_TaxSel;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, ToolWin, ComCtrls, U_BaseHelp, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxGridLevel, cxClasses, cxControls,
  cxGridCustomView, cxGrid, DBClient, ADODB, ImgList, StdCtrls, ExtCtrls,
  cxTextEdit, cxGridCustomPopupMenu, cxGridPopupMenu, cxLookAndFeels,
  cxLookAndFeelPainters, dxSkinsCore, dxSkinsDefaultPainters, cxNavigator,
  dxDateRanges, dxBarBuiltInMenu, System.ImageList, U_BaseInput, cxButtonEdit,
  cxDropDownEdit, dxSkinWXI, dxScrollbarAnnotations, Vcl.Menus, Vcl.Clipbrd,
  cxCalc;

type
  TfrmTaxSel = class(TfrmBaseHelp)
    ToolBar1: TToolBar;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ToolButton1: TToolButton;
    ADOConnection1: TADOConnection;
    Panel1: TPanel;
    Label1: TLabel;
    OrderNo: TEdit;
    GPM_1: TcxGridPopupMenu;
    ImageList1: TImageList;
    ThreeColorBase: TcxStyleRepository;
    SHuangSe: TcxStyle;
    SkyBlue: TcxStyle;
    Default: TcxStyle;
    QHuangSe: TcxStyle;
    Red: TcxStyle;
    FontBlue: TcxStyle;
    TextSHuangSe: TcxStyle;
    FonePurple: TcxStyle;
    FoneClMaroon: TcxStyle;
    FoneRed: TcxStyle;
    RowColor: TcxStyle;
    handBlack: TcxStyle;
    cxBlue: TcxStyle;
    SHuangSeCu: TcxStyle;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    BuyConNo: TEdit;
    Label2: TLabel;
    Tv1Column7: TcxGridDBColumn;
    PM_1: TPopupMenu;
    MenuItem1: TMenuItem;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    Tv1Column12: TcxGridDBColumn;
    Tv1Column13: TcxGridDBColumn;
    Tv1Column14: TcxGridDBColumn;
    CustName: TEdit;
    客户: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure TV1DblClick(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure OrderNoChange(Sender: TObject);
    procedure N1Click(Sender: TObject);
  private
    procedure InitGrid();
    { Private declarations }
  public
    FSTKName: string;
    { Public declarations }
  end;

var
  frmTaxSel: TfrmTaxSel;

implementation

uses
  U_DataLink, U_RTFun;

{$R *.dfm}

procedure TfrmTaxSel.FormCreate(Sender: TObject);
begin
  inherited;
  try
    with ADOConnection1 do
    begin
      Connected := false;
      ConnectionString := DConString;
      Connected := true;
    end;
    ADOQueryBaseCmd.Connection := ADOConnection1;
    ADOQueryBaseTemp.Connection := ADOConnection1;
  except
    application.MessageBox('网络连接失败！', '提示信息');
  end;
end;

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

end;

procedure TfrmTaxSel.FormClose(Sender: TObject; var Action: TCloseAction);
begin

//  OrderNo.SetFocus;

  Action := cahide;
end;

procedure TfrmTaxSel.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
//      sql.Add('SELECT ');
//      sql.Add('    OrderNo,OppCoName,BuyConNo,F_Name,FFTime,');
//      sql.Add('    SUM(CASE WHEN Currency = ''CNY'' THEN Amount ELSE 0 END) AS TotalCNY,');
//      sql.Add('    SUM(CASE WHEN Currency = ''USD'' THEN Amount ELSE 0 END) AS TotalUSD,');
//
//      SQL.add('( SELECT STUFF((SELECT '','' + CAST(H.WoJhExchangeRate AS VARCHAR) FROM V_HXHL H WHERE H.OrderNo = F.OrderNo FOR XML PATH('''')), 1, 1, '''') ) as HXHV');
//      sql.Add('FROM ');
//      sql.Add('    Finance_Flow F');
//      sql.Add('where FFFlag = ''应收收'' ');
//      sql.Add('GROUP BY ');
//      sql.Add('    OrderNo,OppCoName,BuyConNo,F_Name,FFTime');
//      sql.Add('ORDER BY ');
//      sql.Add('    OrderNo');
//      ShowMessage(SQL.text);
      sql.Add('select * from V_Tax');
      Open;
    end;

    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

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

//procedure TfrmTaxSel.InitGrid();
//begin
//  try
//    ADOQueryMain.DisableControls;
//    with ADOQueryMain do
//    begin
//      Close;
//      sql.Clear;
//      sql.Add('SELECT ');
//      sql.Add('    OrderNo,');
//      sql.Add('    OppCoName,');
//      sql.Add('    SUM(CASE WHEN Currency = ''CNY'' THEN Amount ELSE 0 END) AS TotalCNY,');
//      sql.Add('    SUM(CASE WHEN Currency = ''USD'' THEN Amount ELSE 0 END) AS TotalUSD');
//      sql.Add('FROM ');
//      sql.Add('    Finance_Flow');
//      sql.Add('WHERE 1=1');
//      // 如果需要添加其他筛选条件，可以在这里添加
//      // if Trim(FSTKName) <> '' then
//      //   sql.Add('AND SomeField = ''' + Trim(FSTKName) + '''');
//      sql.Add('GROUP BY ');
//      sql.Add('    OrderNo, OppCoName');  // 按OrderNo和OppCoName分组
//      sql.Add('ORDER BY ');
//
//      sql.Add('    OrderNo, OppCoName');   // 先按OrderNo排序，再按OppCoName排序
//      ShowMessage(sql.text)  ;
//      Open;
//    end;
//
//    SCreateCDS(ADOQueryMain, CDS_1);
//    SInitCDSData(ADOQueryMain, CDS_1);
//  finally
//    ADOQueryMain.EnableControls;
//  end;
//end;

procedure TfrmTaxSel.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid('物料类型' + Trim(FSTKName), TV1, '通用窗体');
  InitGrid();
end;

procedure TfrmTaxSel.TBCloseClick(Sender: TObject);
begin
  inherited;
  Close;
end;

procedure TfrmTaxSel.TBSaveClick(Sender: TObject);
begin
  WriteCxGrid('物料类型' + Trim(FSTKName), TV1, '通用窗体');
end;

procedure TfrmTaxSel.ToolButton1Click(Sender: TObject);
begin
//    CoName.SetFocus;
  ModalResult := 1;
end;

procedure TfrmTaxSel.TV1DblClick(Sender: TObject);
begin
  ModalResult := 1;
end;

procedure TfrmTaxSel.FormDestroy(Sender: TObject);
begin
  inherited;
  frmTaxSel := nil;
end;

end.

