unit U_TatClothCRStkHZList;

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, dxScrollbarAnnotations;

type
  TfrmTatClothCRStkHZList = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DS_1: TDataSource;
    GPM_1: TcxGridPopupMenu;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_1: TClientDataSet;
    begtime: TDateTimePicker;
    endTime: TDateTimePicker;
    ToolButton4: TToolButton;
    Label5: TLabel;
    C_Name: TEdit;
    PM_1: TPopupMenu;
    PM11: TMenuItem;
    N1: TMenuItem;
    C_Width: TEdit;
    label11: TLabel;
    Panel2: TPanel;
    N2: TMenuItem;
    N3: TMenuItem;
    Label1: TLabel;
    dayDate: TDateTimePicker;
    Label3: TLabel;
    cxGrid1: TcxGrid;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1DBBandedTableView1: TcxGridDBBandedTableView;
    Tv1: TcxGridDBBandedTableView;
    Tv1Column1: TcxGridDBBandedColumn;
    Tv1Column3: TcxGridDBBandedColumn;
    Tv1Column4: TcxGridDBBandedColumn;
    Tv1Column5: TcxGridDBBandedColumn;
    Tv1Column7: TcxGridDBBandedColumn;
    Tv1Column8: TcxGridDBBandedColumn;
    Tv1Column9: TcxGridDBBandedColumn;
    Tv1Column10: TcxGridDBBandedColumn;
    Tv1Column11: TcxGridDBBandedColumn;
    Tv1Column12: TcxGridDBBandedColumn;
    Tv1Column13: TcxGridDBBandedColumn;
    Tv1Column14: TcxGridDBBandedColumn;
    Tv1Column15: TcxGridDBBandedColumn;
    Tv1Column16: TcxGridDBBandedColumn;
    Tv1Column17: TcxGridDBBandedColumn;
    Tv1Column6: TcxGridDBBandedColumn;
    Tv1Column2: TcxGridDBBandedColumn;
    Tv1CIssp: TcxGridDBBandedColumn;
    cb_GradeYD: TCheckBox;
    cb_GradeED: TCheckBox;
    cb_GradeXJ: TCheckBox;
    cb_GradeFQ: TCheckBox;
    cb_GradeCS: TCheckBox;
    Tv1Grade: TcxGridDBBandedColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoMChange(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBExportClick(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 ToolButton2Click(Sender: TObject);
    procedure cxPageControl1Change(Sender: TObject);
    procedure CDS_1BeforeOpen(DataSet: TDataSet);
    procedure Tv1DataControllerFilterBeforeChange(Sender: TcxDBDataFilterCriteria; ADataSet: TDataSet; const AFilterText: string);

    procedure Tv1TcxGridDBDataControllerTcxDataSummaryFooterSummaryItems6GetText(Sender: TcxDataSummaryItem; const AValue: Variant; AIsFooter: Boolean; var AText: string);
    procedure ToolButton4Click(Sender: TObject);
    procedure PM11Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure IsJYTimeClick(Sender: TObject);
    procedure C_NameKeyPress(Sender: TObject; var Key: Char);

    procedure N2Click(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure cb_GradeYDClick(Sender: TObject);
  private
    CurrentPage, RecordsNumber: Integer;
    lstCust: TStringList;
    procedure InitGrid();
    procedure SetStatus();
    procedure CopyColumnSummaryToClipboard(AColumn: TcxGridColumn);

    { Private declarations }
  public
    canshu1: string;
    { Public declarations }
  end;

implementation

uses
  U_DataLink, U_RTFun, U_LabelPrint, U_TatClothStkHZList2;

{$R *.dfm}

procedure TfrmTatClothCRStkHZList.SetStatus();
begin

end;

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

procedure TfrmTatClothCRStkHZList.FormCreate(Sender: TObject);
begin
  inherited;
  lstCust := TStringList.Create;
  EndDate.DateTime := SGetServerDateTime(ADOQueryTemp);
  BegDate.DateTime := EndDate.DateTime - 20;
  DayDate.Date := SGetServerDate(ADOQueryTemp);
end;

procedure TfrmTatClothCRStkHZList.InitGrid();
var
  WSql: string;
  wsql_Grade: string;
begin

  WSql := SGetFilters(Panel1, 1, 2);
  if Trim(WSql) <> '' then
    WSql := ' and ' + WSql;
//  if IsJYTime.Checked then
//  begin
//    WSql := WSql + ' and IOTime>=' + quotedstr(FormatDateTime('yyyy-MM-dd', BegDate.DateTime));
//    WSql := WSql + ' and IOTime<' + quotedstr(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1));
//  end;

  if cb_GradeYD.Checked then
  begin
    if wsql_Grade = '' then
      wsql_Grade := '优等品'
    else
      wsql_Grade := wsql_Grade + ',优等品';
  end;

  if cb_GradeED.Checked then
  begin
    if wsql_Grade = '' then
      wsql_Grade := '二等品'
    else
      wsql_Grade := wsql_Grade + ',二等品';
  end;

  if cb_GradeXJ.Checked then
  begin
    if wsql_Grade = '' then
      wsql_Grade := '小卷'
    else
      wsql_Grade := wsql_Grade + ',小卷';
  end;

  if cb_GradeFQ.Checked then
  begin
    if wsql_Grade = '' then
      wsql_Grade := '分切'
    else
      wsql_Grade := wsql_Grade + ',分切';
  end;

  if cb_GradeCS.Checked then
  begin
    if wsql_Grade = '' then
      wsql_Grade := '测试'
    else
      wsql_Grade := wsql_Grade + ',测试';
  end;

  try
    ADOQueryMain.DisableControls;

    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;

      sql.Add(' exec P_Tat_Cloth_PSC ');
      SQL.Add(' @Begdate=' + QuotedStr(Trim(FormatDateTime('yyyy-mm-dd', BegDate.Date))));
      SQL.Add(' ,@Enddate=' + QuotedStr(Trim(FormatDateTime('yyyy-mm-dd', EndDate.Date))));
      SQL.Add(' ,@dayDate=' + QuotedStr(Trim(FormatDateTime('yyyy-mm-dd', dayDate.Date))));
      SQL.Add(' ,@WSql=' + QuotedStr(Trim(WSql)));
      if wsql_Grade <> '' then
      begin
  // 直接将构建好的字符串作为参数值
        sql.Add(',@wsql_Grade=' + QuotedStr(wsql_Grade));
      end;

//      showmessage(sql.text);
      Open;
    end;
    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);

  finally
    ADOQueryMain.EnableControls;
    TV1.DataController.Filter.Clear;
  end;

end;

procedure TfrmTatClothCRStkHZList.IsJYTimeClick(Sender: TObject);
begin
  inherited;
  InitGrid();
end;

procedure TfrmTatClothCRStkHZList.TBRafreshClick(Sender: TObject);
begin
  CurrentPage := 1;
  InitGrid();
end;

procedure TfrmTatClothCRStkHZList.CDS_1BeforeOpen(DataSet: TDataSet);
begin

  lstCust.Clear;
end;

procedure TfrmTatClothCRStkHZList.cb_GradeYDClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmTatClothCRStkHZList.ConNoMChange(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  end;
end;

procedure TfrmTatClothCRStkHZList.cxPageControl1Change(Sender: TObject);
begin
  CurrentPage := 1;
  SetStatus();
  InitGrid();
end;

procedure TfrmTatClothCRStkHZList.C_NameKeyPress(Sender: TObject; var Key: Char);
begin
  inherited;
  if Key = #13 then
  begin
    InitGrid();
  end;
end;

procedure TfrmTatClothCRStkHZList.TBCloseClick(Sender: TObject);
begin

  Close;
end;

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

  ReadCxBandedGrid(trim(self.Caption) + 'Tv1', Tv1, '成品出入库存汇总列表1');

//   Tv1Column2.SortOrder := soNone;

  CurrentPage := 1;
  RecordsNumber := 500;
  canshu1 := Trim(Self.fParameters1);

  SetStatus();
  InitGrid();
end;

procedure TfrmTatClothCRStkHZList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    exit;

  with CDS_1 do
  begin
    Filtered := false;
    Filter := ' ssel = 1';
    Filtered := true;
  end;
  TcxGridToExcel(Self.Caption, cxGrid1);
  InitGrid();

end;

procedure TfrmTatClothCRStkHZList.orderNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    CurrentPage := 1;
    InitGrid();
  end;
end;

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

procedure TfrmTatClothCRStkHZList.Tv1DataControllerFilterBeforeChange(Sender: TcxDBDataFilterCriteria; ADataSet: TDataSet; const AFilterText: string);
begin
  lstCust.Clear;
end;

procedure TfrmTatClothCRStkHZList.Tv1DblClick(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    Exit;
  try
    frmTatClothStkHZList2 := TfrmTatClothStkHZList2.Create(Application);
    with frmTatClothStkHZList2 do
    begin

      fC_Name := Self.CDS_1.FieldByName('C_Name').AsString;
      fissp := Self.CDS_1.FieldByName('issp').AsString;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmTatClothStkHZList2.Free;
  end;
end;

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

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

procedure TfrmTatClothCRStkHZList.BTLPClick(Sender: TObject);
begin
  if CurrentPage > 1 then
    CurrentPage := CurrentPage - 1;
  InitGrid();
end;

procedure TfrmTatClothCRStkHZList.TCBNORChange(Sender: TObject);
begin

  CurrentPage := 1;
  InitGrid();
end;

procedure TfrmTatClothCRStkHZList.JYTypeChange(Sender: TObject);
begin
  CurrentPage := 1;
  InitGrid();
end;

procedure TfrmTatClothCRStkHZList.N1Click(Sender: TObject);
begin

  SelOKNo(CDS_1, true);
end;

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

end;

procedure TfrmTatClothCRStkHZList.N3Click(Sender: TObject);
begin

  CopyColumnSummaryToClipboard(TV1.Controller.FocusedColumn);
end;

procedure TfrmTatClothCRStkHZList.CopyColumnSummaryToClipboard(AColumn: TcxGridColumn);
var
  AView: TcxGridBandedTableView;
  SummaryIndex: Integer;
  SummaryText: string;
begin
  AView := Tv1;

  // 查找指定列的汇总项
  SummaryIndex := AView.DataController.Summary.FooterSummaryItems.IndexOfItemLink(AColumn);
  if SummaryIndex <> -1 then
  begin
    SummaryText := AView.DataController.Summary.FooterSummaryTexts[SummaryIndex];
    Clipboard.AsText := SummaryText;
  end
  else
    ShowMessage('未找到该列的汇总数据');
end;

procedure TfrmTatClothCRStkHZList.ToolButton2Click(Sender: TObject);
var
  WSql: string;
begin
  if CDS_1.IsEmpty then
    Exit;
  if not CDS_1.Locate('SSel', True, []) then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  WSql := '';
  CDS_1.DisableControls;
  with CDS_1 do
  begin
    First;
    while not Eof do
    begin
      if CDS_1.fieldbyname('SSel').AsBoolean then
      begin
        if WSql <> '' then
        begin
          WSql := WSql + ',' + QuotedStr(Trim(CDS_1.fieldbyname('CIID').AsString));
        end
        else
        begin
          WSql := QuotedStr(Trim(CDS_1.fieldbyname('CIID').AsString));
        end;
      end;
      Next;
    end;
  end;
  CDS_1.Locate('SSel', True, []);
  CDS_1.EnableControls;
  try
    frmLabelPrint := TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin
      FLMType := 'TradeClothStk';
      FFiltration1 := WSql;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmLabelPrint.Free;
  end;
end;

procedure TfrmTatClothCRStkHZList.ToolButton4Click(Sender: TObject);
begin
  WriteCxBandedGrid(trim(self.Caption) + 'Tv1', Tv1, '成品出入库存汇总列表1');

end;

end.

