unit U_YarnStkTotalList;

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, cxButtonEdit, cxTextEdit,
  cxLookAndFeels, cxLookAndFeelPainters, dxSkinsCore, dxSkinsDefaultPainters,
  cxNavigator, dxDateRanges, dxBarBuiltInMenu, U_BaseList, cxPC;

type
  TfrmYarnTotalStkList = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    CDS_Main: TClientDataSet;
    CDS_HZ: TClientDataSet;
    PopupMenu1: TPopupMenu;
    ToolButton1: TToolButton;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    Column1: TcxGridDBColumn;
    Column2: TcxGridDBColumn;
    Column7: TcxGridDBColumn;
    Column6: TcxGridDBColumn;
    Column3: TcxGridDBColumn;
    Column13: TcxGridDBColumn;
    Column15: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    Label3: TLabel;
    Label4: TLabel;
    Label2: TLabel;
    Y_name: TEdit;
    FromCoName: TEdit;
    BatchNo: TEdit;
    Column4: TcxGridDBColumn;
    ToolButton2: TToolButton;
    Column11: TcxGridDBColumn;
    Column5: TcxGridDBColumn;
    Column8: TcxGridDBColumn;
    Column9: TcxGridDBColumn;
    Column10: TcxGridDBColumn;
    Column16: TcxGridDBColumn;
    Column17: TcxGridDBColumn;
    Column14: TcxGridDBColumn;
    Column12: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    ToolButton3: TToolButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoMChange(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure MPRTCodeNameChange(Sender: TObject);
    procedure SXBatchNOChange(Sender: TObject);
    procedure FromCoNameChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure FormCreate(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
  private
    canshu1, FStkName, canshu3: string;
    procedure InitGrid();
    procedure SetStatus();

    { Private declarations }
  public

    { Public declarations }
  end;

var
  frmYarnTotalStkList: TfrmYarnTotalStkList;

implementation

uses
  U_DataLink, U_RTFun;

{$R *.dfm}

procedure TfrmYarnTotalStkList.SetStatus();
var
  i: Integer;
begin

  for i := 0 to tv1.ColumnCount - 1 do
  begin
    tv1.Columns[i].Visible := true;
  end;

  case cxTabControl1.TabIndex of
    0:
      begin
        Column4.Visible := False;
        Column5.Visible := False;
        Column6.Visible := False;
        Column7.Visible := False;
        Column8.Visible := False;
        Column9.Visible := False;
        Column10.Visible := False;
        Column11.Visible := False;
        Column12.Visible := False;

        Column14.Visible := False;
        Column15.Visible := False;
        Column16.Visible := False;
        Column17.Visible := False;

        BatchNo.text := '';
        FromCoName.text := '';
        Y_name.text := '';

      end;
    1:
      begin

      end;
  end;
end;

procedure TfrmYarnTotalStkList.FormDestroy(Sender: TObject);
begin
  inherited;
  frmYarnTotalStkList := nil;
end;

procedure TfrmYarnTotalStkList.cxTabControl1Change(Sender: TObject);
begin
  SetStatus();
  InitGrid();
end;

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

procedure TfrmYarnTotalStkList.FormCreate(Sender: TObject);
begin
  inherited;
  canshu1 := Trim(self.fParameters1);
  FStkName := Trim(self.fParameters2);
  canshu3 := Trim(self.fParameters3);
end;

procedure TfrmYarnTotalStkList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;

      case cxTabControl1.TabIndex of
        0:
          begin
            sql.Add(' select FromCoName,Y_Name,BatchNo,sum(StkQty) AS StkQty ');
            sql.Add(' from BS_Yarn_IO A    ');
            SQL.Add(' where A.IOFlag=''入库'' and  isnull(StkQty,0)>0   ');
            sql.Add(' and isnull(stkName,'''')=''' + Trim(FstkName) + '''');
            sql.Add(' GROUP BY FromCoName,Y_Name,BatchNo ');
          end;
        1:
          begin
            sql.Add(' select * ');
            sql.Add(' from BS_Yarn_IO A    ');
            SQL.Add(' where A.IOFlag=''入库'' and  isnull(StkQty,0)>0   ');
            sql.Add(' and isnull(stkName,'''')=''' + Trim(FstkName) + '''');
            sql.Add(' and isnull(FromCoName,'''') like ''%' + Trim(FromCoName.text) + '%''');
            sql.Add(' and isnull(Y_Name,'''') like''%' + Trim(Y_Name.text) + '%''');
            sql.Add(' and isnull(BatchNo,'''') like''%' + Trim(BatchNo.text) + '%''');
          end;

      end;

//      ShowMessage(sql.text);
      Open;
    end;
    SCreateCDS(ADOQueryMain, CDS_Main);
    SInitCDSData(ADOQueryMain, CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmYarnTotalStkList.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

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

procedure TfrmYarnTotalStkList.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmYarnTotalStkList.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid(trim(Self.Caption), Tv1, '染色坯布仓库');
  SetStatus();
  InitGrid();
end;

procedure TfrmYarnTotalStkList.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS(ADOQueryMain, CDS_Main);
    SInitCDSData(ADOQueryMain, CDS_Main);
  end;
end;

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

procedure TfrmYarnTotalStkList.SXBatchNOChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmYarnTotalStkList.FromCoNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmYarnTotalStkList.ToolButton1Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  ModalResult := 1;
end;

procedure TfrmYarnTotalStkList.ToolButton2Click(Sender: TObject);
begin
  WriteCxGrid(trim(Self.Caption), Tv1, '染色坯布仓库');
end;

procedure TfrmYarnTotalStkList.ToolButton3Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    exit;
  TcxGridToExcel('纱线库存', cxGrid2);
end;

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

procedure TfrmYarnTotalStkList.Tv1DblClick(Sender: TObject);
var
  Tabint: integer;
begin
  Tabint := cxTabControl1.TabIndex;
  case Tabint of
    0:
      begin
        FromCoName.text := Trim(CDS_Main.fieldbyname('FromCoName').AsString);
        FromCoName.text := Trim(CDS_Main.fieldbyname('FromCoName').AsString);
        Y_Name.text := Trim(CDS_Main.fieldbyname('Y_Name').AsString);
        cxTabControl1.TabIndex := 1;
      end;
  end;
end;

end.

