unit U_TatClothStkHZList3;

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;

type
  TfrmTatClothStkHZList3 = 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;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column22: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1issp: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    Tv1Column13: TcxGridDBColumn;
    Tv1Column14: TcxGridDBColumn;
    Tv1Column17: TcxGridDBColumn;
    Tv1Column18: 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;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    cxGridDBColumn13: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridDBColumn15: TcxGridDBColumn;
    cxGridDBColumn16: TcxGridDBColumn;
    cxGridDBColumn17: TcxGridDBColumn;
    cxGridDBColumn18: TcxGridDBColumn;
    cxGridDBColumn19: TcxGridDBColumn;
    cxGridDBColumn20: TcxGridDBColumn;
    cxGridDBColumn21: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    CDS_2: TClientDataSet;
    DS_2: TDataSource;
    GPM_2: TcxGridPopupMenu;
    N2: TMenuItem;
    PM_2: TPopupMenu;
    N3: TMenuItem;
    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 Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView;
      APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
    procedure N2Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
  private
    CurrentPage, RecordsNumber: Integer;
    lstCust: TStringList;
    procedure InitGrid();
    procedure SetStatus();
    procedure InitGrid2();
    { Private declarations }
  public
    canshu1: string;
    { Public declarations }
  end;

implementation

uses
  U_DataLink, U_RTFun, U_LabelPrint;

{$R *.dfm}

procedure TfrmTatClothStkHZList3.SetStatus();
begin

end;

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

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

procedure TfrmTatClothStkHZList3.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 A.C_NAME ,A.C_GramWeight');
//      SQL.Add(',pf=SUM(A.meter/100*CAST(A.C_WIDTH AS DECIMAL(18,2)))');
      SQL.Add(',SUM(CASE WHEN A.GRADE=''优等品'' THEN 1 ELSE 0 END )AS YDJS');
      SQL.Add(',SUM(CASE WHEN A.GRADE=''优等品'' THEN A.meter ELSE 0 END )AS Ydqty');
//      SQL.Add(',SUM(CASE WHEN A.GRADE=''优等品'' THEN A.meter/100*CAST(A.C_WIDTH AS DECIMAL(18,2)) ELSE 0 END )AS YdPF');
      SQL.Add(',SUM(CASE WHEN A.GRADE=''二等品'' THEN 1 ELSE 0 END )AS edJS');
      SQL.Add(',SUM(CASE WHEN A.GRADE=''二等品'' THEN A.meter ELSE 0 END )AS edqty');
//      SQL.Add(',SUM(CASE WHEN A.GRADE=''二等品'' THEN A.meter/100*CAST(A.C_WIDTH AS DECIMAL(18,2)) ELSE 0 END )AS edPF');
      SQL.Add(',SUM(CASE WHEN A.GRADE=''小卷'' THEN 1 ELSE 0 END )AS xjJS');
      SQL.Add(',SUM(CASE WHEN A.GRADE=''小卷'' THEN A.meter ELSE 0 END )AS xjqty');
//      SQL.Add(',SUM(CASE WHEN A.GRADE=''小卷'' THEN A.meter/100*CAST(A.C_WIDTH AS DECIMAL(18,2)) ELSE 0 END )AS xjPF');
      SQL.Add(',SUM(CASE WHEN A.GRADE=''废布'' THEN 1 ELSE 0 END )AS fbJS');
      SQL.Add(',SUM(CASE WHEN A.GRADE=''废布'' THEN A.meter ELSE 0 END )AS fbqty');
//      SQL.Add(',SUM(CASE WHEN GRADE=''废布'' THEN meter/100*CAST(C_WIDTH AS DECIMAL(18,2)) ELSE 0 END )AS fbPF');
      SQL.Add(',SUM(CASE WHEN A.GRADE=''分切'' THEN 1 ELSE 0 END )AS fqJS');
      SQL.Add(',SUM(CASE WHEN A.GRADE=''分切'' THEN A.meter ELSE 0 END )AS fqqty');
//      SQL.Add(',SUM(CASE WHEN A.GRADE=''分切'' THEN A.meter/100*CAST(A.C_WIDTH AS DECIMAL(18,2)) ELSE 0 END )AS fqPF');
      SQL.Add(',SUM(CASE WHEN A.GRADE=''循环布'' THEN 1 ELSE 0 END )AS xhJS');
      SQL.Add(',SUM(CASE WHEN A.GRADE=''循环布'' THEN A.meter ELSE 0 END )AS xhqty');
      SQL.Add(',SUM(CASE WHEN A.GRADE=''测试'' THEN 1 ELSE 0 END )AS csJS');
      SQL.Add(',SUM(CASE WHEN A.GRADE=''测试'' THEN A.meter ELSE 0 END )AS csqty');
//      SQL.Add(',SUM(CASE WHEN A.GRADE=''测试'' THEN A.meter/100*CAST(A.C_WIDTH AS DECIMAL(18,2)) ELSE 0 END )AS csPF');
      sql.Add(', issp=isnull(A.issp,'''')  ');

      SQL.Add('from   V_Tat_Cloth_stk A  where  1=1  ' + WSql);

      sql.Add(' group by A.C_NAME,A.C_GramWeight,isnull(A.issp,'''') ');
      SQL.Add(' order by A.C_NAME,A.C_GramWeight,isnull(A.issp,'''') ');

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

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

end;

procedure TfrmTatClothStkHZList3.InitGrid2();
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 A.C_NAME, A.C_width,A.C_GramWeight ');
      SQL.Add(',pf=SUM(A.meter/100*CAST(A.C_WIDTH AS DECIMAL(18,2)))');
      SQL.Add(',SUM(CASE WHEN A.GRADE=''优等品'' THEN 1 ELSE 0 END )AS YDJS');
      SQL.Add(',SUM(CASE WHEN A.GRADE=''优等品'' THEN A.meter ELSE 0 END )AS Ydqty');
      SQL.Add(',SUM(CASE WHEN A.GRADE=''优等品'' THEN A.meter/100*CAST(A.C_WIDTH AS DECIMAL(18,2)) ELSE 0 END )AS YdPF');
      SQL.Add(',SUM(CASE WHEN A.GRADE=''二等品'' THEN 1 ELSE 0 END )AS edJS');
      SQL.Add(',SUM(CASE WHEN A.GRADE=''二等品'' THEN A.meter ELSE 0 END )AS edqty');
      SQL.Add(',SUM(CASE WHEN A.GRADE=''二等品'' THEN A.meter/100*CAST(A.C_WIDTH AS DECIMAL(18,2)) ELSE 0 END )AS edPF');
      SQL.Add(',SUM(CASE WHEN A.GRADE=''小卷'' THEN 1 ELSE 0 END )AS xjJS');
      SQL.Add(',SUM(CASE WHEN A.GRADE=''小卷'' THEN A.meter ELSE 0 END )AS xjqty');
      SQL.Add(',SUM(CASE WHEN A.GRADE=''小卷'' THEN A.meter/100*CAST(A.C_WIDTH AS DECIMAL(18,2)) ELSE 0 END )AS xjPF');
      SQL.Add(',SUM(CASE WHEN A.GRADE=''废布'' THEN 1 ELSE 0 END )AS fbJS');
      SQL.Add(',SUM(CASE WHEN A.GRADE=''废布'' THEN A.meter ELSE 0 END )AS fbqty');
//      SQL.Add(',SUM(CASE WHEN GRADE=''废布'' THEN meter/100*CAST(C_WIDTH AS DECIMAL(18,2)) ELSE 0 END )AS fbPF');
      SQL.Add(',SUM(CASE WHEN A.GRADE=''分切'' THEN 1 ELSE 0 END )AS fqJS');
      SQL.Add(',SUM(CASE WHEN A.GRADE=''分切'' THEN A.meter ELSE 0 END )AS fqqty');
      SQL.Add(',SUM(CASE WHEN A.GRADE=''分切'' THEN A.meter/100*CAST(A.C_WIDTH AS DECIMAL(18,2)) ELSE 0 END )AS fqPF');
      SQL.Add(',SUM(CASE WHEN A.GRADE=''循环布'' THEN 1 ELSE 0 END )AS xhJS');
      SQL.Add(',SUM(CASE WHEN A.GRADE=''循环布'' THEN A.meter ELSE 0 END )AS xhqty');
      SQL.Add(',SUM(CASE WHEN A.GRADE=''测试'' THEN 1 ELSE 0 END )AS csJS');
      SQL.Add(',SUM(CASE WHEN A.GRADE=''测试'' THEN A.meter ELSE 0 END )AS csqty');
      SQL.Add(',SUM(CASE WHEN A.GRADE=''测试'' THEN A.meter/100*CAST(A.C_WIDTH AS DECIMAL(18,2)) ELSE 0 END )AS csPF');
      sql.Add(', issp=isnull(A.issp,'''')  ');

      SQL.Add('from   V_Tat_Cloth_stk A  where  1=1 and C_NAME=' +QuotedStr(Trim(CDS_1.FieldByName('C_NAME').AsString)) );
      SQL.Add( WSql);
      sql.Add(' group by A.C_NAME, A.C_width,A.C_GramWeight,isnull(A.issp,'''') ');
      SQL.Add(' order by A.C_NAME, A.C_width,A.C_GramWeight');

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

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

end;

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

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

procedure TfrmTatClothStkHZList3.CDS_1BeforeOpen(DataSet: TDataSet);
begin

  lstCust.Clear;
end;

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

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

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

procedure TfrmTatClothStkHZList3.TBCloseClick(Sender: TObject);
begin

  Close;
end;

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

  ReadCxGrid(trim(self.Caption) + 'Tv1', Tv1, '成品库存汇总');
  ReadCxGrid(trim(self.Caption) + 'Tv2', Tv2, '成品库存汇总');
  CurrentPage := 1;
  RecordsNumber := 500;


  SetStatus();
  InitGrid();
end;

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

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

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

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

procedure TfrmTatClothStkHZList3.Tv1FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  InitGrid2();

end;

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

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

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

procedure TfrmTatClothStkHZList3.TCBNORChange(Sender: TObject);
begin

  CurrentPage := 1;
  InitGrid();
end;

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

procedure TfrmTatClothStkHZList3.N1Click(Sender: TObject);
begin

  SelOKNo(CDS_1, true);
end;

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

end;

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


end;

procedure TfrmTatClothStkHZList3.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 TfrmTatClothStkHZList3.ToolButton4Click(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption) + 'Tv1', Tv1, '成品库存汇总');
  WriteCxGrid(trim(self.Caption) + 'Tv2', Tv2, '成品库存汇总');
end;

end.

