unit U_TatDRList2;

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

type
  TfrmTatDRList2 = class(TfrmBaseInput)
    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;
    C_width: TEdit;
    label11: TLabel;
    Panel2: TPanel;
    CDS_2: TClientDataSet;
    DS_2: TDataSource;
    N2: TMenuItem;
    N3: TMenuItem;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    TV1ssel: TcxGridDBColumn;
    TV1DRMID: TcxGridDBColumn;
    TV1Filltime: TcxGridDBColumn;
    TV1DlyDate: TcxGridDBColumn;
    TV1Saleser: TcxGridDBColumn;
    TV1ToCoName: TcxGridDBColumn;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: 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;
    TV1Contacts: TcxGridDBColumn;
    TV1Telephone: TcxGridDBColumn;
    TV1isIncludeTax: TcxGridDBColumn;
    TV1note: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    TV1Grade: 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 C_NameKeyPress(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
    fC_Name: string;
    fC_Width: string;
    fC_GramWeight: string;
    fstatus : string;
//    ffiller : string;
    fcanshu1 : string;
    fEndDateTime, fBegDateTime: TDateTime;
    { Public declarations }
  end;

var
  frmTatDRList2: TfrmTatDRList2;

implementation

uses
  U_DataLink, U_RTFun, U_LabelPrint;

{$R *.dfm}

procedure TfrmTatDRList2.SetStatus();
begin

end;

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

procedure TfrmTatDRList2.FormCreate(Sender: TObject);
begin
  inherited;
  lstCust := TStringList.Create;

end;

procedure TfrmTatDRList2.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;


  with ADOQueryCMD do
  begin

    Close;
    sql.Clear;
//    sql.Add('select A.* ');
//    sql.Add(' from Tat_Cloth_DR_Sub A   where A.DRMID in (select rtvalue from F_Tool_SplitString(''' + SelCDSKey(Order_Main, ['DRMID'])[0]  + ''' , '',''))');
    sql.Add(' select B.*,A.ToCoName,A.Filler,A.Filltime,A.DlyDate,C.Saleser ');
    SQL.Add(' ,Telephone=(select top 1 Telephone from Bs_Company_contact X where X.CoID=A.ToCoID )');
    SQL.Add(' ,Contacts=(select top 1 Contacts from Bs_Company_contact X where X.CoID=A.ToCoID ) ');
    SQL.Add(' from Tat_Cloth_DR_Sub B   ');
    sql.Add(' left join Tat_Cloth_DR_Main A on A.DRMID = B.DRMID ');
    sql.Add(' left join BS_Company C on A.ToCoID = C.COID ');

//    SQL.Add(' group by B.C_Code,B.C_Name,C.Width,C.C_GramWeight ');
    SQL.Add(' where 1 = 1');
    SQL.Add(' and C_Name=' + QuotedStr(Trim(fC_Name)));
    SQL.Add(' and C_GramWeight=' + QuotedStr(Trim(fC_GramWeight)));
    SQL.Add(' and C_Width=' + QuotedStr(Trim(fC_Width)));
    if fstatus <> '999' then
    begin
       SQL.Add(' and A.status=' + QuotedStr(Trim(fstatus)));
    end
    else
    begin
       SQL.Add(' and isnull(A.status,''0'')<>''-2'' ');
    end;
    if Trim(fcanshu1) = '录入' then
    begin
      sql.Add(' and isnull(A.Filler,'''')=''' + Trim(DName) + '''');
    end;



//     if DRMID.Text <> '' then
//     begin
//        SQL.Add(' and A.DRMID like ' + QuotedStr('%' + Trim(DRMID.text) + '%'));
//     end;
//    if ToCoName.Text <> '' then
//    begin
//      SQL.Add(' and A.ToCoName like ' + QuotedStr('%' + Trim(ToCoName.text) + '%'));
//    end;
//    if shdanwei.Text <> '' then
//    begin
//      SQL.Add(' and A.shdanwei like ' + QuotedStr('%' + Trim(shdanwei.text) + '%'));
//    end;
//
//    if CheckBox1.Checked then
//    begin
//      sql.Add(' and A.Filltime>=''' + FormatDateTime('yyyy-MM-dd', begdate.DateTime) + ''' ');
//      sql.Add(' and A.Filltime<''' + FormatDateTime('yyyy-MM-dd', Enddate.DateTime + 1) + ''' ');
//    end;
//    case cxTabControl1.TabIndex of
//      0:
//        begin
//          SQL.Add(' and isnull(A.status,''0'')=''0'' ');
//        end;
//      1:
//        begin
//          SQL.Add(' and isnull(A.status,''0'')=''9'' ');
//        end;
//      2:
//        begin
//          SQL.Add(' and isnull(A.status,''0'')=''10'' ');
//        end;
//      3:
//        begin
//          SQL.Add(' and isnull(A.status,''0'')=''-2'' ');
//        end;
//      4:
//        begin
//          SQL.Add(' and isnull(A.status,''0'')<>''-2'' ');
//        end;
//    end;
    SQL.Add(WSql);
    sql.Add(' order by  A.DRMID , A.Filltime desc ');


//    ShowMessage(SQL.Text);
    Open;
  end;
  SCreateCDS(ADOQueryCMD, CDS_1);
  SInitCDSData(ADOQueryCMD, CDS_1);
end;

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

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

procedure TfrmTatDRList2.CDS_1BeforeOpen(DataSet: TDataSet);
begin

  lstCust.Clear;
end;

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

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

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

procedure TfrmTatDRList2.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmTatDRList2.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid(trim(self.Caption) + 'Tv1', Tv1, '发货申请2');
  CurrentPage := 1;
  RecordsNumber := 500;
  EndDate.DateTime := fEndDateTime;
  BegDate.DateTime := fBegDateTime;

  SetStatus();
  InitGrid();
end;

procedure TfrmTatDRList2.TBExportClick(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    exit;
  with CDS_1 do
  begin
    Filtered := false;
    Filter := ' ssel = 1';
    Filtered := true;
  end;
  TcxGridToExcel(Self.Caption, cxGrid1);
  InitGrid();
end;

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

procedure TfrmTatDRList2.PM11Click(Sender: TObject);
begin
  SelOKNo(CDS_2, false);
end;

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

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

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

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

procedure TfrmTatDRList2.TCBNORChange(Sender: TObject);
begin

  CurrentPage := 1;
  InitGrid();
end;

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

procedure TfrmTatDRList2.N1Click(Sender: TObject);
begin

  SelOKNo(CDS_2, true);
end;

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

end;

procedure TfrmTatDRList2.N3Click(Sender: TObject);
begin

  CopyColumnSummaryToClipboard(TV1.Controller.FocusedColumn);
end;

procedure TfrmTatDRList2.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 TfrmTatDRList2.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 TfrmTatDRList2.ToolButton4Click(Sender: TObject);
begin

  WriteCxGrid(trim(self.Caption) + 'Tv1', Tv1, '发货申请2');
end;

end.

