unit U_ShaKCList;

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;

type
  TfrmShaKCList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    CDS_Main: TClientDataSet;
    ToolButton1: TToolButton;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column14: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    ToolButton2: TToolButton;
    v1Column10: TcxGridDBColumn;
    Panel1: TPanel;
    Label3: TLabel;
    Label4: TLabel;
    Label8: TLabel;
    Label2: TLabel;
    Y_CodeName: TEdit;
    FactoryName: TEdit;
    Y_Spec: TEdit;
    BatchNo: TEdit;
    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 N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure SXBatchNOChange(Sender: TObject);
    procedure FactoryNameChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure ToolButton2Click(Sender: TObject);
  private
    procedure InitGrid();
    { Private declarations }
  public
    FDYFlag: string;
    { Public declarations }
  end;

var
  frmShaKCList: TfrmShaKCList;

implementation

uses
  U_DataLink, U_RTFun;

{$R *.dfm}

procedure TfrmShaKCList.FormDestroy(Sender: TObject);
begin
  frmShaKCList := nil;
end;

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

procedure TfrmShaKCList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add('select FactoryName,Y_CodeName,Y_Spec,BatchNO,sum(Qty) Qty from (    ');
      sql.Add('select  FactoryName,Y_CodeName,Y_Spec,BatchNO,Qty from CK_Yarn_CR   ');
      sql.Add('union all                                                           ');
      sql.Add(' select  B.FactoryName,B.Y_CodeName,B.Y_Spec,B.BatchNO,-JYNW      ');
      sql.Add(' from CK_YT_CR B  where CRFlag=''入库''   ');
      sql.Add(') AA group by FactoryName,Y_CodeName,Y_Spec,BatchNO');
      Open;
      //ShowMessage(SQL.Text);
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;

end;

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

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

procedure TfrmShaKCList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('纱线库存列表123', Tv1, '纱线仓库');
  Close;
end;

procedure TfrmShaKCList.FormShow(Sender: TObject);
var
  fsj: string;
begin
  ReadCxGrid('纱线库存列表123', Tv1, '纱线仓库');
  if FDYFlag = '99' then
  begin
    InitGrid();
    TBFind.Click;
  end;
//  fsj := 'select distinct(Crtype) name from CK_Yarn_CR where ckname=''纱线'' and crflag=''入库''  ';
//  SInitComBoxBySql(ADOQueryCmd, CRType, False, fsj);
  //InitGrid();
end;

procedure TfrmShaKCList.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  end;
end;

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

procedure TfrmShaKCList.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, True);
end;

procedure TfrmShaKCList.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, False);
end;

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

procedure TfrmShaKCList.FactoryNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmShaKCList.ToolButton1Click(Sender: TObject);
begin
  ModalResult := 1;
end;

procedure TfrmShaKCList.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
 { try
    frmShaOutList:=TfrmShaOutList.Create(Application);
    with frmShaOutList do
    begin
      FFY_ID:=Trim(Self.CDS_Main.fieldbyname('Y_ID').AsString);
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmShaOutList.Free;
  end;}
end;

procedure TfrmShaKCList.ToolButton2Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    exit;
  TcxGridToExcel('纱线库存列表', cxGrid2);
end;

end.

