unit U_DyeClothStkList;

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
  TfrmDyeClothStkList = 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;
    ToolButton2: TToolButton;
    ToolButton4: TToolButton;
    Panel6: TPanel;
    Label31: TLabel;
    LBCPAP: TLabel;
    BTLP: TButton;
    BTNP: TButton;
    TCBNOR: TComboBox;
    Label3: TLabel;
    Label7: TLabel;
    Label10: TLabel;
    Label5: TLabel;
    lbl1: TLabel;
    Label23: TLabel;
    OrderNo: TEdit;
    STKID: TEdit;
    C_Name: TEdit;
    C_Color: TEdit;
    PCId: TEdit;
    C_Code: TEdit;
    PM_1: TPopupMenu;
    PM11: TMenuItem;
    N1: TMenuItem;
    cxPageControl1: TcxPageControl;
    cxTabSheet1: TcxTabSheet;
    cxTabSheet2: TcxTabSheet;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column22: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    cxGridDBColumn13: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    GPM2: TcxGridPopupMenu;
    CDS_2: TClientDataSet;
    DS_2: TDataSource;
    Tv2Column1: TcxGridDBColumn;
    N2: TMenuItem;
    Tv2finishDescription: TcxGridDBColumn;
    Tv1Column1: 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 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 Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView;
      APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
    procedure N2Click(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);

  private
    CurrentPage, RecordsNumber: Integer;
    lstCust: TStringList;
    procedure InitGrid();
    procedure SetStatus();
    procedure InitSubGrid();
    { Private declarations }
  public
    canshu1, FWorkshop: string;
    { Public declarations }
  end;

implementation

uses
  U_DataLink, U_RTFun, U_LabelPrint;

{$R *.dfm}

procedure TfrmDyeClothStkList.SetStatus();
begin

end;

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

procedure TfrmDyeClothStkList.FormCreate(Sender: TObject);
begin
  inherited;
  lstCust := TStringList.Create;
  EndDate.DateTime := SGetServerDateTime(ADOQueryTemp);
  BegDate.DateTime := EndDate.DateTime - 20;
  FWorkshop := trim(Self.fParameters3);
end;

procedure TfrmDyeClothStkList.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = VK_ESCAPE then  // VK_ESCAPE 是 ESC 键的常量
  begin
    Key := 0;  // 清除按键，防止继续传递
    Close;     // 或者 ModalResult := mrCancel;
  end;
end;

procedure TfrmDyeClothStkList.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 IOTime>=' + quotedstr(FormatDateTime('yyyy-MM-dd', BegDate.DateTime));
    WSql := WSql + ' and IOTime<' + quotedstr(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1));
  end;

  try
    ADOQueryMain.DisableControls;

    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(',* from V_Dye_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');
//            showmessage(sql.text);
      Open;
    end;
    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);
    LBCPAP.Caption := inttostr(CurrentPage) + '/' + inttostr(ceil(CDS_1.FieldByName('TotalCount').AsInteger / RecordsNumber));
//    InitSubGrid();
  finally
    ADOQueryMain.EnableControls;
    TV1.DataController.Filter.Clear;
  end;

end;


procedure TfrmDyeClothStkList.InitSubGrid();
begin

  try
    CDS_2.DisableControls;

    with ADOQueryTemp do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add(' select A.* from V_Dye_Cloth_IO A where  A.IONo = ' + QuotedStr(Trim(CDS_1.FieldByName('IONO').AsString) ) );
//            showmessage(sql.text);
      Open;
    end;
    SCreateCDS(ADOQueryTemp, CDS_2);
    SInitCDSData(ADOQueryTemp, CDS_2);

  finally
    CDS_2.EnableControls;
  end;

end;

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

procedure TfrmDyeClothStkList.CDS_1BeforeOpen(DataSet: TDataSet);
begin

  lstCust.Clear;
end;

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



procedure TfrmDyeClothStkList.TBCloseClick(Sender: TObject);
begin

  Close;
end;

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

  ReadCxGrid(trim(self.Caption) + 'Tv1', Tv1, '成品库存列表');
  ReadCxGrid(trim(self.Caption) + 'Tv2', Tv2, '成品库存列表');
  CurrentPage := 1;
  RecordsNumber := 500;
  canshu1 := Trim(Self.fParameters1);
  InitGrid();

  SetStatus();
  v1Column4.Visible := false;
  v1Column4.Hidden := true;
  KeyPreview := True;  // 确保表单可以预览按键
end;

procedure TfrmDyeClothStkList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    exit;
  TcxGridToExcel(Self.Caption, cxGrid1);
end;

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

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

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



procedure TfrmDyeClothStkList.Tv1FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  InitSubGrid();
end;

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

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

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

procedure TfrmDyeClothStkList.TCBNORChange(Sender: TObject);
begin
  RecordsNumber := StrToInt(TCBNOR.Text);
  CurrentPage := 1;
  InitGrid();
end;

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

procedure TfrmDyeClothStkList.N1Click(Sender: TObject);
begin

  SelOKNo(CDS_1, true);
end;

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

procedure TfrmDyeClothStkList.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 := 'DyeClothStk';
      FFiltration1 := WSql;
      if ShowModal = 1 then
      begin

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

procedure TfrmDyeClothStkList.ToolButton4Click(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption) + 'Tv1', Tv1, '成品库存列表');
  WriteCxGrid(trim(self.Caption) + 'Tv2', Tv1, '成品库存列表');
end;

end.

