unit U_TradeClothStkListSel;

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, MovePanel, Math, Clipbrd,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, cxTextEdit, dxSkinsCore,
  dxSkinsDefaultPainters, dxDateRanges, dxBarBuiltInMenu, U_BaseList, cxPC,
  System.StrUtils, cxContainer, dxCore, cxDateUtils, cxMaskEdit, dxSkinWXI,
  dxScrollbarAnnotations, U_BaseHelp, System.ImageList, Vcl.ImgList;

type
  TfrmTradeClothStkListSel = class(TfrmBaseHelp)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DS_1: TDataSource;
    GPM_1: TcxGridPopupMenu;
    CDS_1: TClientDataSet;
    ToolButton2: TToolButton;
    ToolButton4: TToolButton;
    PM_1: TPopupMenu;
    PM11: TMenuItem;
    N1: TMenuItem;
    N2: TMenuItem;
    Pnl_F1: TPanel;
    Label3: TLabel;
    Label7: TLabel;
    Label10: TLabel;
    Label5: TLabel;
    lbl1: TLabel;
    Label20: TLabel;
    Label23: TLabel;
    Label8: TLabel;
    Label14: TLabel;
    begtime: TDateTimePicker;
    endTime: TDateTimePicker;
    OrderNo: TcxTextEdit;
    conNO: TcxTextEdit;
    STKID: TcxTextEdit;
    C_Name: TcxTextEdit;
    C_Color: TcxTextEdit;
    BCIOID: TcxTextEdit;
    C_Pattern: TcxTextEdit;
    C_Code: TcxTextEdit;
    Edit3: TcxTextEdit;
    Edit11: TcxTextEdit;
    Panel6: TPanel;
    Label31: TLabel;
    LBCPAP: TLabel;
    BTLP: TButton;
    BTNP: TButton;
    TCBNOR: TComboBox;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column22: TcxGridDBColumn;
    v1Column21: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    v1Column32: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column23: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column27: TcxGridDBColumn;
    v1Column28: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    Tv1Column12: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column42: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column34: TcxGridDBColumn;
    v1Column40: TcxGridDBColumn;
    v1Column46: TcxGridDBColumn;
    VC_BaoNo: TcxGridDBColumn;
    VC_PacketId: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column13: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure orderNoKeyPress(Sender: TObject; var Key: Char);
    procedure BTNPClick(Sender: TObject);
    procedure BTLPClick(Sender: TObject);
    procedure TCBNORChange(Sender: TObject);
    procedure JYTypeChange(Sender: TObject);
    procedure cxPageControl1Change(Sender: TObject);
    procedure CDS_1BeforeOpen(DataSet: TDataSet);
    procedure Tv1DataControllerFilterBeforeChange(Sender: TcxDBDataFilterCriteria; ADataSet: TDataSet; const AFilterText: string);
    procedure ToolButton4Click(Sender: TObject);
    procedure PM11Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure Tv1DataControllerSummaryFooterSummaryItemsSummary(ASender: TcxDataSummaryItems; Arguments: TcxSummaryEventArguments; var OutArguments: TcxSummaryEventOutArguments);
    procedure Tv1TcxGridDBDataControllerTcxDataSummaryFooterSummaryItems6GetText(Sender: TcxDataSummaryItem; const AValue: Variant; AIsFooter: Boolean; var AText: string);
    procedure ToolButton2Click(Sender: TObject);
  private
    CurrentPage, RecordsNumber: Integer;
    lstPacketId: TStringList;
    procedure InitGrid(WSql: string);
    procedure InitPage();
    procedure SetStatus();
    { Private declarations }
  public
    canshu1, FWorkshop: string;
    { Public declarations }
  end;

var
  frmTradeClothStkListSel: TfrmTradeClothStkListSel;

implementation

uses
  U_DataLink, U_RTFun;

{$R *.dfm}
procedure TfrmTradeClothStkListSel.InitPage();
begin
  CurrentPage := 1;
  InitGrid(SGetHintFilters(Pnl_F1, 1, 2));
end;

procedure TfrmTradeClothStkListSel.SetStatus();
begin
//     if canshu1 then

end;

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

procedure TfrmTradeClothStkListSel.FormCreate(Sender: TObject);
begin
  inherited;
  lstPacketId := TStringList.Create;
end;

procedure TfrmTradeClothStkListSel.InitGrid(WSql: string);
begin

  if Trim(WSql) <> '' then
    WSql := ' and ' + WSql;

  with ADOQueryMain do
  begin
    Filtered := False;
    Close;
    sql.Clear;
    sql.Add(' select top ' + inttostr(RecordsNumber) + ' * ');
    sql.Add(' from  ');
    sql.Add('(select  rownumber=cast((row_number() over(order by AA.IOTime desc)) as int)');
    sql.Add(',COUNT(1) OVER() AS TotalCount');
    sql.Add(', kl= DATEDIFF(dd,IOTime, GETDATE()),* from V_Trade_Cloth_Stk   AA where 1=1 ');
    if trim(FWorkshop) <> '' then
      sql.Add(' and Workshop=''' + trim(FWorkshop) + ''' ');
    sql.Add('and 1=1 ' + WSql);
    sql.Add(') temp_row');
    sql.Add(' where  rownumber> ' + inttostr(((CurrentPage - 1) * RecordsNumber)));
    sql.Add(' order by rownumber');
    Open;
  end;
  SCreateCDS(ADOQueryMain, CDS_1);
  SInitCDSData(ADOQueryMain, CDS_1);
  LBCPAP.Caption := inttostr(CurrentPage) + '/' + inttostr(ceil(CDS_1.FieldByName('TotalCount').AsInteger / RecordsNumber));
  TV1.DataController.Filter.Clear;

end;

procedure TfrmTradeClothStkListSel.TBRafreshClick(Sender: TObject);
begin
  InitPage();
end;

procedure TfrmTradeClothStkListSel.CDS_1BeforeOpen(DataSet: TDataSet);
begin
  lstPacketId.Clear;
end;

procedure TfrmTradeClothStkListSel.cxPageControl1Change(Sender: TObject);
begin
  SetStatus();
end;

procedure TfrmTradeClothStkListSel.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmTradeClothStkListSel.FormShow(Sender: TObject);
begin
  inherited;

  ReadCxGrid(trim(self.Caption) + 'Tv1', Tv1, '贸易成品仓库');
  CurrentPage := 1;
  RecordsNumber := 500;
  SetStatus();
  InitPage();
end;

procedure TfrmTradeClothStkListSel.orderNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    InitPage();
  end;
end;

procedure TfrmTradeClothStkListSel.PM11Click(Sender: TObject);
begin
  SelOKNo(CDS_1, false);
end;

procedure TfrmTradeClothStkListSel.Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
  if AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('kl').Index] > 90 then
    ACanvas.Brush.Color := $B3DEF5;
end;

procedure TfrmTradeClothStkListSel.Tv1DataControllerFilterBeforeChange(Sender: TcxDBDataFilterCriteria; ADataSet: TDataSet; const AFilterText: string);
begin
  lstPacketId.Clear;      //重新打开数据集前清空lstPacketId
end;

procedure TfrmTradeClothStkListSel.Tv1DataControllerSummaryFooterSummaryItemsSummary(ASender: TcxDataSummaryItems; Arguments: TcxSummaryEventArguments; var OutArguments: TcxSummaryEventOutArguments);
var
  BaoID: string;
begin
  //对 VC_PacketId 列的合计进行处理
  if TcxGridDBTableSummaryItem(Arguments.SummaryItem).Column = VC_PacketId then
  begin
    BaoID := vartostr(ASender.DataController.Values[Arguments.RecordIndex, VC_PacketId.Index]);
    if BaoID = '' then
      Exit;
    if BaoID = null then
      Exit;
    if lstPacketId.IndexOf(BaoID) = -1 then
    begin
      lstPacketId.Add(BaoID);   //记下之前未出现过的包编号
    end;
  end;
end;

procedure TfrmTradeClothStkListSel.Tv1TcxGridDBDataControllerTcxDataSummaryFooterSummaryItems6GetText(Sender: TcxDataSummaryItem; const AValue: Variant; AIsFooter: Boolean; var AText: string);
begin
  if lstPacketId <> nil then
    AText := IntToStr(lstPacketId.Count);
end;

procedure TfrmTradeClothStkListSel.BTNPClick(Sender: TObject);
begin
  if CurrentPage < CDS_1.FieldByName('TotalCount').AsInteger / RecordsNumber then
    CurrentPage := CurrentPage + 1;
  InitGrid(SGetHintFilters(Pnl_F1, 1, 2));
end;

procedure TfrmTradeClothStkListSel.BTLPClick(Sender: TObject);
begin
  if CurrentPage > 1 then
    CurrentPage := CurrentPage - 1;
  InitGrid(SGetHintFilters(Pnl_F1, 1, 2));
end;

procedure TfrmTradeClothStkListSel.TCBNORChange(Sender: TObject);
begin
  RecordsNumber := StrToInt(TCBNOR.Text);
  CurrentPage := 1;
  InitGrid(SGetHintFilters(Pnl_F1, 1, 2));
end;

procedure TfrmTradeClothStkListSel.JYTypeChange(Sender: TObject);
begin
  CurrentPage := 1;
  InitPage();
end;

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

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

procedure TfrmTradeClothStkListSel.ToolButton2Click(Sender: TObject);
begin

  ModalResult := 1;
end;

procedure TfrmTradeClothStkListSel.ToolButton4Click(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption) + 'Tv1', Tv1, '贸易成品仓库');
end;

end.

