unit U_TatMachCutHZList;

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

type
  TfrmTatMachCutHZList = 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;
    IsJYTime: TCheckBox;
    ToolButton4: TToolButton;
    Label5: TLabel;
    Carno: TEdit;
    PM_1: TPopupMenu;
    PM11: TMenuItem;
    N1: TMenuItem;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column22: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    Tv1Column13: TcxGridDBColumn;
    Tv1Column14: TcxGridDBColumn;
    Tv1Column17: TcxGridDBColumn;
    Tv1Column18: TcxGridDBColumn;
    Tv1Column19: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Tv1fbJS: TcxGridDBColumn;
    Tv1fbqty: TcxGridDBColumn;
    Tv1ZJJS: TcxGridDBColumn;
    Tv1ZJqty: TcxGridDBColumn;
    Tv1Carno: TcxGridDBColumn;
    Tv1qgMeter: TcxGridDBColumn;
    Tv1qgMeterGQ: TcxGridDBColumn;
    Tv1qgMeterAll: TcxGridDBColumn;
    N2: TMenuItem;
    N3: TMenuItem;
    Tv1SHGJ: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    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 CarnoKeyPress(Sender: TObject; var Key: Char);
    procedure N2Click(Sender: TObject);
    procedure N3Click(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;

{$R *.dfm}

procedure TfrmTatMachCutHZList.SetStatus();
begin

end;

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

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

procedure TfrmTatMachCutHZList.InitGrid();
var
  WSql,wsql1: string;
  //   子查询筛选时间
begin

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

  try
    ADOQueryMain.DisableControls;

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

//      sql.Add('select max(C_NAME) as C_NAME, max(C_width) as C_width,max(C_GramWeight) as C_GramWeight,max(pcid) as pcid,A.Carno  ');
//
//      SQL.Add(' ,qgMeter=sum(meter+SHMeter)/ ISNULL(NULLIF(ISNULL((SELECT SUM(C_WidthNum) AS Total_C_WidthNum FROM (  ');
//      SQL.Add(' SELECT distinct X.BCIOID,C_WidthNum FROM Bs_Cloth_IO X left join Tat_Cloth_Inspect Y on X.BCIOID = Y.BCIOID  WHERE Y.Carno = A.Carno and X.C_WidthNum is  not null ' + wsql1 + ' ) T), 0), 0), 1)');
//
//
//      SQL.Add(' ,qgC_WidthAll= ISNULL(NULLIF(ISNULL((SELECT SUM(C_WidthNum) AS Total_C_WidthNum FROM (  ');
//      SQL.Add(' SELECT distinct X.BCIOID,C_WidthNum FROM Bs_Cloth_IO X left join Tat_Cloth_Inspect Y on X.BCIOID = Y.BCIOID  WHERE Y.Carno = A.Carno and X.C_WidthNum is  not null ' + wsql1 + ' ) T), 0), 0), 1)');
//
//      SQL.Add(',SHpf=SUM(CASE WHEN ISNUMERIC(SHMETER) = 1 AND ISNUMERIC(C_WIDTH) = 1  ');
//      SQL.Add('     THEN CAST(SHMETER AS DECIMAL(18,2))/100 * CAST(C_WIDTH AS DECIMAL(18,2)) ');
//      SQL.Add('     ELSE 0 END)');
//      SQL.Add(',SUM(CASE WHEN GRADE=''优等品'' THEN 1 ELSE 0 END )AS YDJS');
//      SQL.Add(',SUM(CASE WHEN GRADE=''优等品'' THEN meter ELSE 0 END )AS Ydqty');
//      SQL.Add(',SUM(CASE WHEN GRADE=''二等品'' THEN 1 ELSE 0 END )AS edJS');
//      SQL.Add(',SUM(CASE WHEN GRADE=''二等品'' THEN meter ELSE 0 END )AS edqty');
//      SQL.Add(',SUM(CASE WHEN GRADE=''小卷'' THEN 1 ELSE 0 END )AS xjJS');
//      SQL.Add(',SUM(CASE WHEN GRADE=''小卷'' THEN meter ELSE 0 END )AS xjqty');
//      SQL.Add(',SUM(CASE WHEN GRADE=''废布'' THEN 1 ELSE 0 END )AS fbJS');
//      SQL.Add(',SUM(CASE WHEN GRADE=''废布'' THEN meter ELSE 0 END )AS fbqty');
//      SQL.Add(',SUM(CASE WHEN GRADE=''分切'' THEN 1 ELSE 0 END )AS fqJS');
//      SQL.Add(',SUM(CASE WHEN GRADE=''分切'' THEN meter ELSE 0 END )AS fqqty');
//      SQL.Add(',SUM(CASE WHEN GRADE=''循环布'' THEN 1 ELSE 0 END )AS xhJS');
//      SQL.Add(',SUM(CASE WHEN GRADE=''循环布'' THEN meter ELSE 0 END )AS xhqty');
//      SQL.Add(',SUM(CASE WHEN GRADE=''测试'' THEN 1 ELSE 0 END )AS csJS');
//      SQL.Add(',SUM(CASE WHEN GRADE=''测试'' THEN meter ELSE 0 END )AS csqty');
////      sql.add(',count(*) as ZJJS,sum(Meter) as ZJqty');
//// ZJJS不包括循环布
//      SQL.Add(', SUM(CASE WHEN GRADE IN (''优等品'',''二等品'',''小卷'',''废布'',''分切'',''测试'') THEN 1 ELSE 0 END) as ZJJS');
//// ZJqty不包括循环布
//      SQL.Add(', SUM(CASE WHEN GRADE IN (''优等品'',''二等品'',''小卷'',''废布'',''分切'',''测试'') THEN Meter ELSE 0 END) as ZJqty');
//      SQL.Add('from  Tat_Cloth_Inspect  A ');
//      SQL.Add('where  Carno <>'''' and Carno is not NULL  ');
//      SQL.Add(WSql);
//      sql.Add(' group by Carno');
      sql.Add(' ExEC P_Select_TatMachCutHZ ');
      sql.Add(' @wsql = ' + quotedStr(wsql));
      sql.Add(' ,@wsql1 = ' + quotedStr(wsql1));

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

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

end;

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

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

procedure TfrmTatMachCutHZList.CDS_1BeforeOpen(DataSet: TDataSet);
begin

  lstCust.Clear;
end;

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

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

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

procedure TfrmTatMachCutHZList.TBCloseClick(Sender: TObject);
begin

  Close;
end;

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

//  ReadCxGrid(trim(self.Caption) + 'Tv1', Tv1, '机台切割汇总');
  CurrentPage := 1;
  RecordsNumber := 500;
  canshu1 := Trim(Self.fParameters1);
  BegDate.DateTime := SGetServerDate(ADOQueryCmd);
  EndDate.DateTime := BegDate.DateTime;

  SetStatus();
  InitGrid();

end;

procedure TfrmTatMachCutHZList.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 TfrmTatMachCutHZList.orderNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    CurrentPage := 1;
    InitGrid();
  end;
end;

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

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

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

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

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

procedure TfrmTatMachCutHZList.TCBNORChange(Sender: TObject);
begin

  CurrentPage := 1;
  InitGrid();
end;

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

procedure TfrmTatMachCutHZList.N1Click(Sender: TObject);
begin

  SelOKNo(CDS_1, true);
end;

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

end;

procedure TfrmTatMachCutHZList.N3Click(Sender: TObject);
begin
   CopyColumnSummaryToClipboard(TV1.Controller.FocusedColumn);
end;


procedure TfrmTatMachCutHZList.CopyColumnSummaryToClipboard(AColumn: TcxGridColumn);
var
  AView: TcxGridDBTableView;
  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 TfrmTatMachCutHZList.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 TfrmTatMachCutHZList.ToolButton4Click(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption) + 'Tv1', Tv1, '机台切割汇总');
end;

end.

