unit U_TatClothStkList;

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, dxDateRanges,
  dxBarBuiltInMenu, U_BaseList, cxPC, System.StrUtils, cxContainer, dxCore,
  cxDateUtils, cxMaskEdit, dxScrollbarAnnotations, cxProgressBar, dxSkinsCore,
  dxSkinsDefaultPainters, dxSkinOffice2013White, dxSkinSharpPlus, dxSkinWXI;

type
  TfrmTatClothStkList = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DS_1: TDataSource;
    GPM_1: TcxGridPopupMenu;
    CDS_1: TClientDataSet;
    ToolButton2: TToolButton;
    ToolButton4: TToolButton;
    PM_1: TPopupMenu;
    PM11: TMenuItem;
    N1: TMenuItem;
    cxPageControl1: TcxPageControl;
    cxTabSheet1: TcxTabSheet;
    cxTabSheet2: TcxTabSheet;
    Panel6: TPanel;
    Label31: TLabel;
    LBCPAP: TLabel;
    BTLP: TButton;
    BTNP: TButton;
    TCBNOR: TComboBox;
    Pnl_F1: TPanel;
    Label3: TLabel;
    Label7: TLabel;
    Label10: TLabel;
    Label5: TLabel;
    lbl1: TLabel;
    Label20: TLabel;
    Label23: TLabel;
    begtime: TDateTimePicker;
    endTime: TDateTimePicker;
    OrderNo: TcxTextEdit;
    conNO: TcxTextEdit;
    STKID: TcxTextEdit;
    C_Name: TcxTextEdit;
    C_Color: TcxTextEdit;
    BCIOID: TcxTextEdit;
    C_Pattern: TcxTextEdit;
    C_Code: TcxTextEdit;
    Pnl_F2: TPanel;
    Label4: TLabel;
    Label6: TLabel;
    Label9: TLabel;
    Label13: TLabel;
    DateTimePicker3: TDateTimePicker;
    DateTimePicker4: TDateTimePicker;
    Edit1: TcxTextEdit;
    Edit2: TcxTextEdit;
    Edit4: TcxTextEdit;
    Edit5: TcxTextEdit;
    Edit8: TcxTextEdit;
    GPM_2: TcxGridPopupMenu;
    CDS_2: TClientDataSet;
    DS_2: TDataSource;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridDBColumn15: TcxGridDBColumn;
    cxGridDBColumn16: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    TV2Column3: TcxGridDBColumn;
    Label18: TLabel;
    Label19: TLabel;
    Edit9: TcxTextEdit;
    Edit10: TcxTextEdit;
    Label8: TLabel;
    Label14: TLabel;
    Edit3: TcxTextEdit;
    Edit11: TcxTextEdit;
    PM_2: TPopupMenu;
    MenuItem1: TMenuItem;
    N2: TMenuItem;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column22: TcxGridDBColumn;
    v1Column21: TcxGridDBColumn;
    v1Column32: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column23: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column42: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Label1: TLabel;
    BegDate2: TcxDateEdit;
    EndDate2: TcxDateEdit;
    chk_F2: TcxCheckBox;
    Label2: TLabel;
    chk_F1: TcxCheckBox;
    BegDate: TcxDateEdit;
    EndDate: TcxDateEdit;
    TV2Column1: TcxGridDBColumn;
    TV2PacketNoNum: TcxGridDBColumn;
    Tv1PacketNo: TcxGridDBColumn;
    TV2ConNo: TcxGridDBColumn;
    Tv1PCID: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBRafreshClick(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 ToolButton4Click(Sender: TObject);
    procedure PM11Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure TV2CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure MenuItem1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure TV2CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure Tv1TcxGridDBDataControllerTcxDataSummaryFooterSummaryItems6GetText(Sender: TcxDataSummaryItem; const AValue: Variant; AIsFooter: Boolean; var AText: string);
  private
    CurrentPage, RecordsNumber: Integer;
    lstPacketId: TStringList;
    procedure InitGrid(WSql: string);
    procedure InitPage();
    procedure SetStatus();
    { Private declarations }
  public
    canshu1, FWorkshop: string;
    { Public declarations }
  end;

implementation

uses
  U_DataLink, U_RTFun, U_LabelPrint;

{$R *.dfm}
procedure TfrmTatClothStkList.InitPage();
begin
  CurrentPage := 1;
  case cxPageControl1.ActivePageIndex of
    0:
      begin
        InitGrid(SGetHintFilters(Pnl_F2, 1, 2));
      end;
    1:
      begin
        InitGrid(SGetHintFilters(Pnl_F1, 1, 2));
      end;
  end;
end;

procedure TfrmTatClothStkList.SetStatus();
begin
//     if canshu1 then

end;

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

procedure TfrmTatClothStkList.FormCreate(Sender: TObject);
begin
  inherited;
  lstPacketId := TStringList.Create;
  EndDate.Date := SGetServerDateTime(ADOQueryTemp);
  BegDate.Date := EndDate.Date - 20;
  BegDate2.Date := BegDate.Date;
  EndDate2.Date := EndDate.Date;
  FWorkshop := trim(Self.fParameters3);
end;

procedure TfrmTatClothStkList.InitGrid(WSql: string);
begin

  if Trim(WSql) <> '' then
    WSql := ' and ' + WSql;

  case cxPageControl1.ActivePageIndex of
    0:
      begin
        if chk_F2.Checked then
        begin
          WSql := WSql + ' and IOTime>=' + quotedstr(FormatDateTime('yyyy-MM-dd', BegDate2.Date));
          WSql := WSql + ' and IOTime<' + quotedstr(FormatDateTime('yyyy-MM-dd', enddate2.Date + 1));
        end;
        with ADOQueryMain do
        begin
          Filtered := False;
          Close;
          sql.Clear;
          sql.Add(' select ConNo,OrderNo,C_Code,C_Name,Grade,CustName,C_Width,C_GramWeight,BatchNo,C_Color');
          sql.Add(' ,COUNT(PieceNo) SumPieceNo,sum(GrossWeight) SumGrossWeight,sum(Tare) SumTare,sum(NetWeight) SumNetWeight');
          sql.Add(' ,Sum(Meter) SumMeter,Sum(Yardage) SumYardage');
          sql.Add(' ,(SELECT COUNT(DISTINCT PacketNo) FROM Tat_Cloth_Stock X inner join Tat_Cloth_io Y on X.stkID=Y.stkID and X.StkCycles=Y.IOCycles where X.OrderNo = A.OrderNo and  X.StkExists=1 and Y.IOFlag=''入库'' ' +  WSql+ ') as PacketNoNum');
          sql.Add(' from V_Tat_Cloth_Stk A where 1=1 ');

          sql.Add(WSql);
          sql.Add(' GROUP BY ConNo,OrderNo,C_Code,C_Name,Grade,CustName,C_Width,C_GramWeight,BatchNo,C_Color');
//          showmessage(sql.text);
          Open;
        end;
        SCreateCDS(ADOQueryMain, CDS_2);
        SInitCDSData(ADOQueryMain, CDS_2);
        TV2.DataController.Filter.Clear;

      end;
    1:
      begin
        if chk_F1.Checked then
        begin
          WSql := WSql + ' and IOTime>=' + quotedstr(FormatDateTime('yyyy-MM-dd', BegDate.Date));
          WSql := WSql + ' and IOTime<' + quotedstr(FormatDateTime('yyyy-MM-dd', enddate.Date + 1));
        end;
        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(', kl= DATEDIFF(dd,IOTime, GETDATE()),* from V_Tat_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));
        TV1.DataController.Filter.Clear;
      end;
  end;

end;

procedure TfrmTatClothStkList.TBRafreshClick(Sender: TObject);
begin
  InitPage();
end;

procedure TfrmTatClothStkList.CDS_1BeforeOpen(DataSet: TDataSet);
begin
  lstPacketId.Clear;
end;

procedure TfrmTatClothStkList.cxPageControl1Change(Sender: TObject);
begin
  SetStatus();
end;

procedure TfrmTatClothStkList.TBCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmTatClothStkList.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);
  SetStatus();
  InitPage();
end;

procedure TfrmTatClothStkList.TBExportClick(Sender: TObject);
begin
  case cxPageControl1.ActivePageIndex of
    0: // 汇总页
      begin
        if CDS_2.IsEmpty then Exit;
        TcxGridToExcel(Self.Caption + '_汇总', cxGrid2);
      end;
    1: // 明细页
      begin
        if CDS_1.IsEmpty then Exit;
        TcxGridToExcel(Self.Caption + '_明细', cxGrid1);
      end;
  end;
end;

procedure TfrmTatClothStkList.orderNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    InitPage();
  end;
end;

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

procedure TfrmTatClothStkList.Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
  if AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('kl').Index] > 90 then
    ACanvas.Brush.Color := $B3DEF5;
end;

procedure TfrmTatClothStkList.Tv1DataControllerFilterBeforeChange(Sender: TcxDBDataFilterCriteria; ADataSet: TDataSet; const AFilterText: string);
begin
  lstPacketId.Clear;      //重新打开数据集前清空lstPacketId
end;

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

procedure TfrmTatClothStkList.TV2CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  BegDate.Date := BegDate2.Date;
  EndDate.Date := EndDate2.Date;
  chk_F2.Checked := chk_F1.Checked;
  cxPageControl1.ActivePageIndex := 1;
  SetStatus();
  InitGrid(SGetCDSFilters(Tv2, CDS_2));
end;

procedure TfrmTatClothStkList.TV2CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
//  if AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('kl').Index] > 90 then
//    ACanvas.Brush.Color := $B3DEF5;
end;

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

procedure TfrmTatClothStkList.BTLPClick(Sender: TObject);
begin
  if CurrentPage > 1 then
    CurrentPage := CurrentPage - 1;
  InitGrid(SGetHintFilters(Pnl_F1, 1, 2));
end;

procedure TfrmTatClothStkList.TCBNORChange(Sender: TObject);
begin
  RecordsNumber := StrToInt(TCBNOR.Text);
  CurrentPage := 1;
  InitGrid(SGetHintFilters(Pnl_F1, 1, 2));
end;

procedure TfrmTatClothStkList.JYTypeChange(Sender: TObject);
begin
  CurrentPage := 1;
  InitPage();
end;

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

end;

procedure TfrmTatClothStkList.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_1, true);
end;

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

procedure TfrmTatClothStkList.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;
  ShowMessage(WSql);
  CDS_1.Locate('SSel', True, []);
  CDS_1.EnableControls;
  try
    frmLabelPrint := TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin
      FLMType := 'TatClothStk';
      FFiltration1 := WSql;
      if ShowModal = 1 then
      begin

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

procedure TfrmTatClothStkList.ToolButton4Click(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption) + 'Tv1', Tv1, '梭织坯布仓库');
  WriteCxGrid(trim(self.Caption) + 'Tv2', Tv2, '梭织坯布仓库');
end;

end.

