unit U_TradeTotallClothStkSel;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, cxGridCustomTableView, cxGridTableView,
  cxGridBandedTableView, cxGridDBBandedTableView, cxGridLevel, cxClasses,
  cxControls, cxGridCustomView, cxGridDBTableView, cxGrid, StdCtrls, ComCtrls,
  ExtCtrls, ToolWin, cxGridCustomPopupMenu, cxGridPopupMenu, ADODB, DBClient,
  cxDropDownEdit, cxCheckBox, RM_Common, RM_Class, RM_e_Xls, RM_Dataset,
  RM_System, RM_GridReport, Menus, cxCalendar, cxButtonEdit, cxTextEdit,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxDateRanges,
  dxBarBuiltInMenu, U_BaseHelp, System.ImageList, Vcl.ImgList,
  dxScrollbarAnnotations, cxContainer, dxSkinsCore, dxSkinsDefaultPainters, cxPC;

type
  TfrmTradeTotallClothStkSel = class(TfrmBaseHelp)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADO_1: TADOQuery;
    ADOQueryTemp: TADOQuery;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    Label3: TLabel;
    Label4: TLabel;
    C_name: TcxTextEdit;
    FromCoName: TcxTextEdit;
    GPM_1: TcxGridPopupMenu;
    CDS_1: TClientDataSet;
    DS_1: TDataSource;
    cxPageControl1: TcxPageControl;
    cxTabSheet1: TcxTabSheet;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn15: TcxGridDBColumn;
    cxGridDBColumn16: TcxGridDBColumn;
    cxGridDBColumn24: TcxGridDBColumn;
    cxGridDBColumn26: TcxGridDBColumn;
    cxGridDBColumn28: TcxGridDBColumn;
    cxGridDBColumn29: TcxGridDBColumn;
    cxGridDBColumn17: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxTabSheet2: TcxTabSheet;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    Tv1Column33: TcxGridDBColumn;
    Tv1Column13: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    Tv1Column19: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column21: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column12: TcxGridDBColumn;
    Tv1Column27: TcxGridDBColumn;
    Tv1Column15: TcxGridDBColumn;
    Tv1Column16: TcxGridDBColumn;
    Tv1Column17: TcxGridDBColumn;
    Tv1Column18: TcxGridDBColumn;
    Tv1Column22: TcxGridDBColumn;
    Tv1Column25: TcxGridDBColumn;
    Tv1Column28: TcxGridDBColumn;
    Tv1Column29: TcxGridDBColumn;
    Tv1Column30: TcxGridDBColumn;
    Tv1Column31: TcxGridDBColumn;
    Tv1Column32: TcxGridDBColumn;
    Tv1Column36: TcxGridDBColumn;
    Tv1Column39: TcxGridDBColumn;
    Tv1Column40: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    GPM_2: TcxGridPopupMenu;
    CDS_2: TClientDataSet;
    DS_2: TDataSource;
    ADO_2: TADOQuery;
    Label5: TLabel;
    Label6: TLabel;
    C_Color: TcxTextEdit;
    C_ColorNo: TcxTextEdit;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure ToolButton2Click(Sender: TObject);
    procedure FromCoNameKeyPress(Sender: TObject; var Key: Char);
    procedure TV2DblClick(Sender: TObject);
    procedure cxPageControl1Change(Sender: TObject);
  private
    procedure InitGrid();
    procedure InitGrid1();
    procedure InitGrid2();
    { Private declarations }
  public
    FstkName, FC_Degree, FStkIdS: string;
    { Public declarations }
  end;

var
  frmTradeTotallClothStkSel: TfrmTradeTotallClothStkSel;

implementation

uses
  U_DataLink, U_RTFun;

{$R *.dfm}

procedure TfrmTradeTotallClothStkSel.FormDestroy(Sender: TObject);
begin
  inherited;
  frmTradeTotallClothStkSel := nil;
end;

procedure TfrmTradeTotallClothStkSel.cxPageControl1Change(Sender: TObject);
begin
  case cxPageControl1.ActivePageIndex of
    1:
      begin
        InitGrid1();
      end;
  end;
end;

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

procedure TfrmTradeTotallClothStkSel.InitGrid();
begin
  case cxPageControl1.ActivePageIndex of
    0:
      begin
        InitGrid2();
      end;
    1:
      begin

        InitGrid1();
      end;
  end;

end;

procedure TfrmTradeTotallClothStkSel.InitGrid1();
var
  SqlStr, WSqlStr: string;
begin

  SqlStr := ' select A.*,StkPieceint=CEILING(A.StkPiece) ';
  SqlStr := SqlStr + ' from BS_Cloth_IO A    ';
  SqlStr := SqlStr + ' where A.IOFlag=''入库'' and (isnull(StkQty,0)>0 or isnull(StkPiece,0)>0) ';
  SqlStr := SqlStr + ' and isnull(stkName,'''')=''' + Trim(FstkName) + '''';

  if Trim(FStkIdS) <> '' then
    SqlStr := SqlStr + '   and not EXISTS (select SP.RTValue from [dbo].[F_Tool_SplitString](' + Quotedstr(FStkIdS) + ','','') SP where SP.RTValue=A.BCIOID ) ';

  if FC_Degree <> '' then
    SqlStr := SqlStr + ' and isnull(C_Degree,'''')=''' + Trim(FC_Degree) + '''';

  WSqlStr := CommonFiltersByTv(Tv2, '{"RemovalNull":true,"Fields":"C_Code|C_Name|C_Color|C_ColorNo|C_Pattern|C_Degree|QtyUnit"}');
  if TRIM(WSqlStr) <> '' then
    SqlStr := SqlStr + ' and ' + WSqlStr;

  SqlStr := SqlStr + ' order by IOTime desc';

  InitCDSData(ADO_1, CDS_1, Tv1, SqlStr, SGetFilters(Panel1, 1, 2), 'BCIOID');

end;

procedure TfrmTradeTotallClothStkSel.InitGrid2();
var
  SqlStr, WSqlStr: string;
begin

  SqlStr := ' select A.C_Code,C_Name,C_Color,C_ColorNo,C_Degree,C_Pattern,QtyUnit,StkPieceint=sum(CEILING(A.StkPiece)),StkQty=sum(StkQty),QtyUnit ';
  SqlStr := SqlStr + ' from BS_Cloth_IO A    ';
  SqlStr := SqlStr + ' where A.IOFlag=''入库'' and (isnull(StkQty,0)>0 or isnull(StkPiece,0)>0) ';
  SqlStr := SqlStr + ' and isnull(stkName,'''')=''' + Trim(FstkName) + '''';

  if Trim(FStkIdS) <> '' then
    SqlStr := SqlStr + '   and not EXISTS (select SP.RTValue from [dbo].[F_Tool_SplitString](' + Quotedstr(FStkIdS) + ','','') SP where SP.RTValue=A.BCIOID ) ';

  if FC_Degree <> '' then
    SqlStr := SqlStr + ' and isnull(C_Degree,'''')=''' + Trim(FC_Degree) + '''';

  SqlStr := SqlStr + ' group by C_Code,C_Name,C_Color,C_ColorNo,C_Degree,C_Pattern,QtyUnit';

//  ShowMessage(SqlStr);
  InitCDSData(ADO_2, CDS_2, Tv2, SqlStr, SGetFilters(Panel1, 1, 2), 'C_Code');

end;

procedure TfrmTradeTotallClothStkSel.TBRafreshClick(Sender: TObject);
begin
  InitGrid2();
end;

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

procedure TfrmTradeTotallClothStkSel.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid(trim(self.Caption), Tv1, '贸易汇总仓库');
  InitGrid2();
end;

procedure TfrmTradeTotallClothStkSel.FromCoNameKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    TBFind.Click;
  end;
end;

procedure TfrmTradeTotallClothStkSel.TBFindClick(Sender: TObject);
begin
  case cxPageControl1.ActivePageIndex of
    0:
      begin
        CDSDataFilter(ADO_2, CDS_2, Tv2, '{"FilterStr":"' + CommonFiltersByContainer(Panel1, '  {"EquTag": 1,"LikeTag": 2,"Fields": "C_name|C_Color|C_ColorNo"}') + '"  }');
      end;
    1:
      begin
        CDSDataFilter(ADO_1, CDS_1, Tv1, '{"FilterStr":"' + CommonFiltersByContainer(Panel1, '  {"EquTag": 1,"LikeTag": 2,"Fields": ""}') + '"  }');
      end;
  end;
end;

procedure TfrmTradeTotallClothStkSel.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_1, True);
end;

procedure TfrmTradeTotallClothStkSel.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_1, False);
end;

procedure TfrmTradeTotallClothStkSel.ToolButton1Click(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    Exit;
  if CDS_1.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  ModalResult := 1;
end;

procedure TfrmTradeTotallClothStkSel.ToolButton2Click(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption), Tv1, '贸易汇总仓库');
end;

procedure TfrmTradeTotallClothStkSel.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  ModalResult := 1;
end;

procedure TfrmTradeTotallClothStkSel.TV2DblClick(Sender: TObject);
begin
  cxPageControl1.ActivePageIndex := 1;

end;

end.

