unit U_TradeClothTotalStkList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Vcl.Printers, 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, cxButtonEdit, cxTextEdit,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxDateRanges,
  dxBarBuiltInMenu, U_BaseList, dxScrollbarAnnotations, cxContainer,
  cxProgressBar, cxButtons, cxMaskEdit, dxSkinsCore, dxSkinsDefaultPainters,
  cxPC, System.Math, Clipbrd;

type
  TfrmTradeClothTotalStkList = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADO_2: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DS_1: TDataSource;
    GPM_1: TcxGridPopupMenu;
    CDS_1: TClientDataSet;
    PopupMenu1: TPopupMenu;
    Label3: TLabel;
    Label4: TLabel;
    Label2: TLabel;
    C_name: TcxTextEdit;
    FromCoName: TcxTextEdit;
    C_Color: TcxTextEdit;
    ToolButton2: TToolButton;
    Label6: TLabel;
    C_ColorNo: TcxTextEdit;
    N1: TMenuItem;
    N2: TMenuItem;
    ToolButton3: TToolButton;
    cxPageControl1: TcxPageControl;
    cxTabSheet1: TcxTabSheet;
    cxTabSheet2: TcxTabSheet;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    Tv1Column33: TcxGridDBColumn;
    Tv1Column13: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    Tv1Column19: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column21: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column12: TcxGridDBColumn;
    Tv1Column27: TcxGridDBColumn;
    Tv1Column15: TcxGridDBColumn;
    Tv1Column16: TcxGridDBColumn;
    Tv1Column17: TcxGridDBColumn;
    Tv1Column18: TcxGridDBColumn;
    Tv1Column22: TcxGridDBColumn;
    Tv1Column25: TcxGridDBColumn;
    Tv1Column28: TcxGridDBColumn;
    Tv1Column30: TcxGridDBColumn;
    Tv1Column31: TcxGridDBColumn;
    Tv1Column36: TcxGridDBColumn;
    Tv1Column39: TcxGridDBColumn;
    Tv1Column40: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridDBColumn15: TcxGridDBColumn;
    cxGridDBColumn16: TcxGridDBColumn;
    cxGridDBColumn17: TcxGridDBColumn;
    cxGridDBColumn24: TcxGridDBColumn;
    cxGridDBColumn26: TcxGridDBColumn;
    cxGridDBColumn28: TcxGridDBColumn;
    cxGridDBColumn29: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    GPM_2: TcxGridPopupMenu;
    CDS_2: TClientDataSet;
    DS_2: TDataSource;
    ADO_1: TADOQuery;
    Label1: TLabel;
    StkPosition: TcxTextEdit;
    TV2Column1: TcxGridDBColumn;
    Panel6: TPanel;
    Label31: TLabel;
    LBCPAP: TLabel;
    BTLP: TButton;
    BTNP: TButton;
    TCBNOR: TcxComboBox;
    N3: TMenuItem;
    Label5: TLabel;
    ftypcid: TcxTextEdit;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure MPRTCodeNameChange(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure ToolButton2Click(Sender: TObject);
    procedure FromCoNamePropertiesChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure FromCoNameKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton3Click(Sender: TObject);
    procedure TV2DblClick(Sender: TObject);
    procedure cxPageControl1Change(Sender: TObject);
    procedure N3Click(Sender: TObject);
  private
    CurrentPage, RecordsNumber: Integer;
    canshu1, FSTKName, canshu3, canshu4: string;
//    procedure InitGrid();
    procedure InitGrid(MWSql: string);
    procedure InitGrid1();
    procedure InitGrid2();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmTradeClothTotalStkList: TfrmTradeClothTotalStkList;

implementation

uses
  U_DataLink, U_RTFun, U_PrintLabFun, U_dynamicsDll, U_printPdf,
  U_TradeClothSplit, U_LabelPrint;

{$R *.dfm}

procedure TfrmTradeClothTotalStkList.FormDestroy(Sender: TObject);
begin
  inherited;
  frmTradeClothTotalStkList := nil;
end;

procedure TfrmTradeClothTotalStkList.cxPageControl1Change(Sender: TObject);
begin
  if cxPageControl1.ActivePageIndex = 1 then
  begin
    ftypcid.tag := 2;
  end
  else
  begin
    ftypcid.tag := 0;
  end;
  case cxPageControl1.ActivePageIndex of
    1:
      begin

        InitGrid1();
      end;
  end;

end;

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

procedure TfrmTradeClothTotalStkList.FormCreate(Sender: TObject);
begin
  inherited;
  canshu1 := Trim(self.fParameters1);
  FSTKName := Trim(self.fParameters2);
  canshu3 := Trim(self.fParameters3);
  canshu4 := Trim(self.fParameters4);
end;

//procedure TfrmTradeClothTotalStkList.InitGrid();
//begin
//  case cxPageControl1.ActivePageIndex of
//    0:
//      begin
//        InitGrid2();
//      end;
//    1:
//      begin
//
//        InitGrid1();
//      end;
//  end;
//
//end;
procedure TfrmTradeClothTotalStkList.InitGrid(MWSql: string);
var
  WSql, WDateSql: string;
  SqlStr: string;
begin

  WDateSql := ' and isnull(STKNAME,'''')=''' + Trim(FSTKNAME) + '''';
  if Trim(canshu4) <> '' then
    WDateSql := WDateSql + ' and isnull(StkCoName,'''')=''' + Trim(canshu4) + '''';

  case cxPageControl1.ActivePageIndex of
    0:
      begin
//        WSql := CommonFiltersByContainer(dxLayoutControl_query, '  {"EquTag": 1,"LikeTag": 2,"Fields": "OrderNo|FtyPCId|FtyPCId|C_Color|C_Code|C_Name"}');

        if trim(WSql) <> '' then
        begin
          WSql := WDateSql + ' and ' + trim(WSql);
        end
        else
          WSql := WDateSql;

        SqlStr := ' select A.C_Code,C_Name,C_Color,C_ColorNo,C_Degree,C_Pattern,QtyUnit,StkPieceint=sum(CEILING(A.StkPiece)),StkQty=sum(StkQty),QtyUnit,StkPosition ';
        SqlStr := SqlStr + ' from BS_Cloth_IO A    ';
        SqlStr := SqlStr + ' where A.IOFlag=''入库'' and (isnull(StkQty,0)>0 or isnull(StkPiece,0)>0) ';
        SqlStr := SqlStr + ' and isnull(stkName,'''')=''' + Trim(FstkName) + '''';

        if Trim(canshu4) <> '' then
          SqlStr := SqlStr + ' and isnull(StkCoName,'''')=''' + Trim(canshu4) + '''';

        SqlStr := SqlStr + ' group by C_Code,C_Name,C_Color,C_ColorNo,C_Degree,C_Pattern,QtyUnit,StkPosition';

//  ShowMessage(SqlStr);
        InitCDSData(ADO_2, CDS_2, Tv2, SqlStr, SGetFilters(Panel1, 1, 2), 'C_Code');

      end;

    1:
      begin
        if MWSql = '' then
          WSql := CommonFiltersByContainer(panel1, ' {"EquTag": 1,"LikeTag": 2,"Fields": ""}')
        else
          WSql := MWSql;

        if trim(WSql) <> '' then
        begin
          WSql := WDateSql + ' and ' + trim(WSql);
        end
        else
          WSql := WDateSql;

        SqlStr := 'exec P_Trade_Totalstk_Get_Page ';
        SqlStr := SqlStr + ' @pageIndex=' + inttostr(CurrentPage);
        SqlStr := SqlStr + ' ,@pageSize=' + inttostr(RecordsNumber);
        SqlStr := SqlStr + ' ,@criteria= ' + quotedstr(WSql);

        InitCDSData(ADO_1, CDS_1, Tv1, SqlStr, SGetFilters(Panel1, 1, 2), 'C_Code');
        LBCPAP.Caption := inttostr(CurrentPage) + '/' + inttostr(ceil(CDS_1.FieldByName('TotalCount').AsInteger / RecordsNumber));
      end;
  end;

end;

procedure TfrmTradeClothTotalStkList.InitGrid2();
var
  SqlStr: string;
begin

  SqlStr := ' select A.C_Code,C_Name,C_Color,C_ColorNo,C_Degree,C_Pattern,QtyUnit,StkPieceint=sum(CEILING(A.StkPiece)),StkQty=sum(StkQty),QtyUnit,StkPosition ';
  SqlStr := SqlStr + ' from BS_Cloth_IO A    ';
  SqlStr := SqlStr + ' where A.IOFlag=''入库'' and (isnull(StkQty,0)>0 or isnull(StkPiece,0)>0) ';
  SqlStr := SqlStr + ' and isnull(stkName,'''')=''' + Trim(FstkName) + '''';

  if Trim(canshu4) <> '' then
    SqlStr := SqlStr + ' and isnull(StkCoName,'''')=''' + Trim(canshu4) + '''';

  SqlStr := SqlStr + ' group by C_Code,C_Name,C_Color,C_ColorNo,C_Degree,C_Pattern,QtyUnit,StkPosition';

//  ShowMessage(SqlStr);
  InitCDSData(ADO_2, CDS_2, Tv2, SqlStr, SGetFilters(Panel1, 1, 2), 'C_Code');

end;

procedure TfrmTradeClothTotalStkList.InitGrid1();
var
  SqlStr, WSqlStr: string;
begin

  SqlStr := ' select A.*,StkPieceint=CEILING(A.StkPiece) ';
  SqlStr := SqlStr + ' from BS_Cloth_IO A    ';
  SqlStr := SqlStr + ' where A.IOFlag=''入库'' and (isnull(StkQty,0)>0 or isnull(StkPiece,0)>0) ';
  SqlStr := SqlStr + ' and isnull(stkName,'''')=''' + Trim(FstkName) + '''';
  if ftypcid.Text <> '' then
  begin
    SqlStr := SqlStr + ' and isnull(ftypcid,'''')=''' + Trim(ftypcid.Text) + '''';
  end;
  if Trim(canshu4) <> '' then
    SqlStr := SqlStr + ' and isnull(StkCoName,'''')=''' + Trim(canshu4) + '''';
  WSqlStr := CommonFiltersByTv(Tv2, '{"RemovalNull":true,"Fields":"C_Code|C_Name|C_Color|C_ColorNo|C_Pattern|C_Degree|QtyUnit"}');

  if TRIM(WSqlStr) <> '' then
    SqlStr := SqlStr + ' and ' + WSqlStr;
  SqlStr := SqlStr + ' order by IOTime desc';

  InitCDSData(ADO_1, CDS_1, Tv1, SqlStr, SGetFilters(Panel1, 1, 2), 'BCIOID');

end;

procedure TfrmTradeClothTotalStkList.TBRafreshClick(Sender: TObject);
begin
  InitGrid('');
//  InitGrid2();
end;

procedure TfrmTradeClothTotalStkList.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmTradeClothTotalStkList.FormShow(Sender: TObject);
begin
  inherited;
  CurrentPage := 1;
  RecordsNumber := 500;
  ReadCxGrid(trim(Self.Caption) + 'Tv1', Tv1, '贸易汇总仓库');
  ReadCxGrid(trim(Self.Caption) + 'Tv2', Tv2, '贸易汇总仓库');
  canshu4 := Trim(self.fParameters4);
  InitGrid2();
end;

procedure TfrmTradeClothTotalStkList.FromCoNameKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
    TBFind.Click;
end;

procedure TfrmTradeClothTotalStkList.FromCoNamePropertiesChange(Sender: TObject);
begin
  if Length(Trim(TcxTextEdit(Sender).Text)) < 4 then
    Exit;
  TBFind.Click;
end;

procedure TfrmTradeClothTotalStkList.TBFindClick(Sender: TObject);
begin
  case cxPageControl1.ActivePageIndex of
    0:
      begin
        CDSDataFilter(ADO_2, CDS_2, Tv2, '{"FilterStr":"' + CommonFiltersByContainer(Panel1, '  {"EquTag": 1,"LikeTag": 2,"Fields": "C_name|C_Color|C_ColorNo|StkPosition|FromCoName"}') + '"  }');
      end;
    1:
      begin
        CDSDataFilter(ADO_1, CDS_1, Tv1, '{"FilterStr":"' + CommonFiltersByContainer(Panel1, '  {"EquTag": 1,"LikeTag": 2,"Fields": ""}') + '"  }');
      end;
  end;

end;

procedure TfrmTradeClothTotalStkList.MPRTCodeNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmTradeClothTotalStkList.N1Click(Sender: TObject);
begin
  inherited;
  SelOKNo(CDS_1, True);
end;

procedure TfrmTradeClothTotalStkList.N2Click(Sender: TObject);
begin
  inherited;
  SelOKNo(CDS_1, False);
end;

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

end;

procedure TfrmTradeClothTotalStkList.ToolButton2Click(Sender: TObject);
begin
  WriteCxGrid(trim(Self.Caption) + 'Tv1', Tv1, '贸易汇总仓库');
  WriteCxGrid(trim(Self.Caption) + 'Tv2', Tv2, '贸易汇总仓库');
end;

procedure TfrmTradeClothTotalStkList.ToolButton3Click(Sender: TObject);
begin
  if CDS_1.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  try
    frmLabelPrint := TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin
      FLMType := 'TradeLabVolume1';
      FFiltration1 := Trim(SelCDSKey(self.CDS_1, ['BCIOID'])[0]);
      if ShowModal = 1 then
      begin

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

procedure TfrmTradeClothTotalStkList.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  ModalResult := 1;
end;

procedure TfrmTradeClothTotalStkList.TV2DblClick(Sender: TObject);
begin
  cxPageControl1.ActivePageIndex := 1;

end;

end.

