unit U_TatInspSHHZ;

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, cxPC, System.StrUtils,
  dxScrollbarAnnotations, U_BaseList, System.ImageList, Vcl.ImgList, U_BaseInput,
  dxSkinWXI;

type
  TfrmTatInspSHHZ = 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;
    C_Name: TEdit;
    PM_1: TPopupMenu;
    PM11: TMenuItem;
    N1: TMenuItem;
    C_GramWeight: TEdit;
    label12: TLabel;
    PCId: TEdit;
    label11: TLabel;
    Panel2: TPanel;
    N2: TMenuItem;
    N3: TMenuItem;
    PM_2: TPopupMenu;
    GPM_2: TcxGridPopupMenu;
    CDS_2: TClientDataSet;
    DS_2: TDataSource;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column22: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1issp: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1SH_TD1: TcxGridDBColumn;
    Tv1SH_ZW2: TcxGridDBColumn;
    Tv1SH_LTY4: TcxGridDBColumn;
    Tv1SH_DJ6: TcxGridDBColumn;
    Tv1SH_HD7: TcxGridDBColumn;
    Tv1SH_DS8: TcxGridDBColumn;
    Tv1SH_HB9: TcxGridDBColumn;
    Tv1SH_SCZ10: TcxGridDBColumn;
    Tv1SH_BB11: TcxGridDBColumn;
    Tv1SH_ZW12: TcxGridDBColumn;
    Tv1SH_HY13: TcxGridDBColumn;
    Tv1SH_DPB14: TcxGridDBColumn;
    Tv1SH_GJ15: TcxGridDBColumn;
    Tv1SH_YS16: TcxGridDBColumn;
    Tv1SH_KZ17: TcxGridDBColumn;
    Tv1SH_SC18: TcxGridDBColumn;
    Tv1SH_XY19: TcxGridDBColumn;
    Tv1SH_LC1: TcxGridDBColumn;
    Tv1SH_PD21: TcxGridDBColumn;
    Tv1SH_QT1: TcxGridDBColumn;
    Tv1SH_JT1: TcxGridDBColumn;
    Tv1SH_MFBZ1: TcxGridDBColumn;
    Tv1SH_YB1: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    N4: TMenuItem;
    N5: TMenuItem;
    N6: TMenuItem;
    N7: TMenuItem;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    Tv1PCId: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    cxGridDBColumn13: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridDBColumn15: TcxGridDBColumn;
    cxGridDBColumn16: TcxGridDBColumn;
    cxGridDBColumn17: TcxGridDBColumn;
    cxGridDBColumn18: TcxGridDBColumn;
    cxGridDBColumn19: TcxGridDBColumn;
    cxGridDBColumn20: TcxGridDBColumn;
    cxGridDBColumn21: TcxGridDBColumn;
    cxGridDBColumn22: TcxGridDBColumn;
    cxGridDBColumn23: TcxGridDBColumn;
    cxGridDBColumn25: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    TV2SH_BK22: TcxGridDBColumn;
    TV2SH_QT26: TcxGridDBColumn;
    Tv1SH_BK22: TcxGridDBColumn;
    Tv1SH_ZB1: TcxGridDBColumn;
    Tv1SH_DW1: TcxGridDBColumn;
    Tv1SH_ZZ2: TcxGridDBColumn;
    Tv1SH_YD1: TcxGridDBColumn;
    TV2SH_DW1: TcxGridDBColumn;
    TV2SH_JT1: TcxGridDBColumn;
    TV2SH_ZB1: TcxGridDBColumn;
    TV2SH_ZZ2: TcxGridDBColumn;
    TV2SH_YD1: TcxGridDBColumn;
    TV2SH_MFBZ1: TcxGridDBColumn;
    TV2SH_YB1: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure IsJYTimeClick(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 C_NameKeyPress(Sender: TObject; var Key: Char);
    procedure N2Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure N4Click(Sender: TObject);
    procedure N5Click(Sender: TObject);
    procedure N6Click(Sender: TObject);
    procedure N7Click(Sender: TObject);
  private
    CurrentPage, RecordsNumber: Integer;
    lstCust: TStringList;
    procedure InitGrid();
    procedure InitGrid2();
    procedure SetStatus();
    procedure CopyColumnSummaryToClipboard(AView: TcxGridDBTableView; AColumn: TcxGridColumn);

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

implementation

uses
  U_DataLink, U_RTFun, U_LabelPrint, U_TatInspSHHZ2;

{$R *.dfm}

procedure TfrmTatInspSHHZ.SetStatus();
begin

end;

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

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

procedure TfrmTatInspSHHZ.InitGrid();
var
  WSql: 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));
  end;

  try
    ADOQueryMain.DisableControls;

    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add(' ExEC P_Tat_Cloth_SHHZ ');
      sql.Add(' @wsql = ' + quotedStr(WSql));
//    showmessage(sql.text);
      Open;
    end;
    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);

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

end;

procedure TfrmTatInspSHHZ.InitGrid2();
var
  WSql: string;
  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));
  end;

  try
    ADOQueryMain.DisableControls;

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

      sql.Add(' ExEC P_Tat_Cloth_SHHZ2 ');
      sql.Add(' @wsql = ' + quotedStr(WSql));
      sql.Add(' ,@wsql1 = ' + quotedStr(WSql1));

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

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

end;

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

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

procedure TfrmTatInspSHHZ.CDS_1BeforeOpen(DataSet: TDataSet);
begin

  lstCust.Clear;
end;

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

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

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

  Close;
end;

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

  ReadCxGrid(trim(self.Caption) + 'Tv1', Tv1, '损耗汇总列表');
  ReadCxGrid(trim(self.Caption) + 'Tv2', Tv2, '损耗汇总列表');
  CurrentPage := 1;
  RecordsNumber := 500;

  SetStatus();
  InitGrid();
  InitGrid2();
end;

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

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

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

procedure TfrmTatInspSHHZ.Tv1DblClick(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    Exit;
  try
    frmTatInspSHHZ2 := TfrmTatInspSHHZ2.Create(Application);
    with frmTatInspSHHZ2 do
    begin

      fEndDateTime := self.EndDate.DateTime;
      fBegDateTime := Self.BegDate.DateTime;
//
//
      fC_Name := Self.CDS_1.FieldByName('C_Name').AsString;
      fissp := Self.CDS_1.FieldByName('issp').AsString;
      fC_GramWeight := Self.CDS_1.FieldByName('C_GramWeight').AsString;
//      fpcid := Self.CDS_1.FieldByName('pcid').AsString;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmTatInspSHHZ2.Free;
  end;
end;

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

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

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

procedure TfrmTatInspSHHZ.TCBNORChange(Sender: TObject);
begin

  CurrentPage := 1;
  InitGrid();
end;

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

procedure TfrmTatInspSHHZ.N1Click(Sender: TObject);
begin

  SelOKNo(CDS_1, true);
end;

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

end;

procedure TfrmTatInspSHHZ.N3Click(Sender: TObject);
begin
  CopyColumnSummaryToClipboard(Tv1, TV1.Controller.FocusedColumn);

end;

procedure TfrmTatInspSHHZ.N4Click(Sender: TObject);
begin

  SelOKNo(CDS_2, true);
end;

procedure TfrmTatInspSHHZ.N5Click(Sender: TObject);
begin

  SelOKNo(CDS_2, false);
end;

procedure TfrmTatInspSHHZ.N6Click(Sender: TObject);
begin
  Clipboard.SetTextBuf(PChar(Trim(CDS_2.fieldbyname(TV2.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));

end;

procedure TfrmTatInspSHHZ.N7Click(Sender: TObject);
begin
  CopyColumnSummaryToClipboard(Tv2, TV2.Controller.FocusedColumn);

end;

procedure TfrmTatInspSHHZ.CopyColumnSummaryToClipboard(AView: TcxGridDBTableView; AColumn: TcxGridColumn);
var
  SummaryIndex: Integer;
  SummaryText: string;
begin
  // 查找指定列的汇总项
  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 TfrmTatInspSHHZ.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 TfrmTatInspSHHZ.ToolButton4Click(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption) + 'Tv1', Tv1, '损耗汇总列表');
  WriteCxGrid(trim(self.Caption) + 'Tv2', Tv2, '损耗汇总列表');
end;

end.

