unit U_MaterialManyKCSelHelp;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, ADODB, StdCtrls, Grids, DBGrids, ComCtrls, ToolWin, ImgList,
  DBClient, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, cxDBData, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxControls, cxGridCustomView, cxClasses, cxGridLevel,
  cxGrid, cxTextEdit;

type
  TfrmMaterialManyKCSelHelp = class(TForm)
    DataSource1: TDataSource;
    ADOQuery1: TADOQuery;
    ImageList24: TImageList;
    ToolBar1: TToolBar;
    Tok: TToolButton;
    Tclose: TToolButton;
    GroupBox1: TGroupBox;
    edttj: TEdit;
    Label1: TLabel;
    CDS_sel: TClientDataSet;
    DataSourceSel: TDataSource;
    ADOQuery1MNumber: TStringField;
    ADOQuery1Mname: TWideStringField;
    ADOQuery1Model: TWideStringField;
    ADOQuery1CaiZhi: TWideStringField;
    ADOQuery1PurSortCode: TStringField;
    ADOQuery1InvSortcode: TStringField;
    ADOQuery1PrimaryUnit: TStringField;
    ADOQuery1PrimaryUnitName: TWideStringField;
    ADOQuery1PurUnit: TStringField;
    ADOQuery1PurUnitName: TWideStringField;
    ADOQuery1NowStorage: TBCDField;
    ADOQuery1HighStorage: TBCDField;
    ADOQuery1LowStorage: TBCDField;
    ADOQuery1SafeQuan: TBCDField;
    ADOQuery1Isbzqgl: TBooleanField;
    ADOQuery1Ispcgl: TBooleanField;
    CDS_selMNumber: TStringField;
    CDS_selMname: TWideStringField;
    CDS_selModel: TWideStringField;
    CDS_selCaiZhi: TWideStringField;
    CDS_selPurSortCode: TStringField;
    CDS_selInvSortcode: TStringField;
    CDS_selPrimaryUnit: TStringField;
    CDS_selPrimaryUnitName: TWideStringField;
    CDS_selPurUnit: TStringField;
    CDS_selPurUnitName: TWideStringField;
    CDS_selNowStorage: TBCDField;
    CDS_selHighStorage: TBCDField;
    CDS_selLowStorage: TBCDField;
    CDS_selSafeQuan: TBCDField;
    CDS_selIsbzqgl: TBooleanField;
    CDS_selIspcgl: TBooleanField;
    Label2: TLabel;
    Edit1: TEdit;
    ADOQuery1cess: TFloatField;
    CDS_selcess: TFloatField;
    GroupBox2: TGroupBox;
    dbGrid1: TcxGrid;
    dbGrid1Level1: TcxGridLevel;
    Tv1: TcxGridDBTableView;
    Tv1MNumber1: TcxGridDBColumn;
    Tv1Mname1: TcxGridDBColumn;
    Tv1Model1: TcxGridDBColumn;
    Tv1CaiZhi1: TcxGridDBColumn;
    Tv1PrimaryUnitName1: TcxGridDBColumn;
    DBGrid2: TcxGrid;
    DBGrid2Level1: TcxGridLevel;
    TV2: TcxGridDBTableView;
    TV2MNumber1: TcxGridDBColumn;
    TV2Mname1: TcxGridDBColumn;
    TV2Model1: TcxGridDBColumn;
    TV2CaiZhi1: TcxGridDBColumn;
    TV2PrimaryUnitName1: TcxGridDBColumn;
    TV2endquan1: TcxGridDBColumn;
    ADOConnection1: TADOConnection;
    v1KCQuantity: TcxGridDBColumn;
    ADOQuery1KCQuantity: TFloatField;
    CDS_selKCQuantity: TFloatField;
    procedure FormShow(Sender: TObject);
    procedure DBGrid1TitleClick(Column: TColumn);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure edttjChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure TcloseClick(Sender: TObject);
    procedure TokClick(Sender: TObject);
    procedure edttjKeyPress(Sender: TObject; var Key: Char);
    procedure Edit1Change(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure TV2DblClick(Sender: TObject);
  private
    SortField,SortOrder,SortCaption:string;
    SortID:integer;
    procedure AddSelRow();
  public
    str_dylx:string;
    FStoreCode:string;
  end;

var
  frmMaterialManyKCSelHelp: TfrmMaterialManyKCSelHelp;

implementation
uses
  U_global;

{$R *.dfm}

procedure TfrmMaterialManyKCSelHelp.FormShow(Sender: TObject);
begin
  with adoquery1 do
  begin
    close;
    sql.Clear;
    sql.Add('select AA.*,KCQuantity=isnull(KCQuantity1,0)+isnull(KCQuantity0,0) from ( ');
    sql.Add('select A.* ,');
    sql.Add('KCQuantity1=(select Sum(Case When C.InOutFlag=''0'' then isnull(FactIssueQuan,0)*(-1) when C.InOutFlag=''1'' then isnull(FactReceiptQuan,0) end)');
    sql.Add('  from Gy_InoutSub B inner join Gy_InoutMain C on B.InoutMainId=C.InoutMainId');
    sql.Add(' where B.MNumber=A.MNumber  ),');
    sql.Add(' KCQuantity0=(select Sum(Quan)');
    sql.Add('  from kf_StartSub BB inner join kf_StartMain CC on BB.StartMainId=CC.StartMainId');
    sql.Add(' where BB.MNumber=A.MNumber )');
    sql.Add(' from Gy_material A ');
    sql.Add('where StopFlag=''0'' ');  //--StopFlag=''0''
    if trim(FStoreCode)<>'' then
      sql.Add('and A.whcode='''+trim(FStoreCode)+'''');
    sql.Add(') AA'); //order by Mnumber
    open;
  end;
  edttj.SetFocus ;
end;

procedure TfrmMaterialManyKCSelHelp.DBGrid1TitleClick(Column: TColumn);
begin
  if  ADOQuery1.isEmpty then Exit;
end;

procedure TfrmMaterialManyKCSelHelp.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action:=caFree;
end;
/////////////////////////////////////////////////
  //条件过滤
/////////////////////////////////////////////////
procedure TfrmMaterialManyKCSelHelp.edttjChange(Sender: TObject);
begin
  try
      ADOQuery1.DisableControls;
      if trim(edttj.Text)<>'' then
      begin
        with ADOQuery1 do
        begin
          filtered:=false;
          filter:='mNumber like '+quotedStr('%'+trim(edttj.Text)+'%')+
                  ' or mName like '+quotedStr('%'+trim(edttj.Text)+'%');
          filtered:=true;
        end;
      end
      else
      begin
        with ADOQuery1 do
        begin
          filtered:=false;
        end;

      end;
  finally
      ADOQuery1.EnableControls;
  end;

end;

procedure TfrmMaterialManyKCSelHelp.FormCreate(Sender: TObject);
begin
  with ADOConnection1 do
  begin
    Connected:=False;
    ConnectionString:=gConString;
    Connected:=True;
  end;
  //dbgrid1.Align :=alClient;
  groupBox2.Align :=alClient;
  CDS_Sel.Close;
  CDS_Sel.CreateDataSet;
end;
/////////////////////////////////////////////////////
  //函数功能：增加选择的记录
////////////////////////////////////////////////////
procedure TfrmMaterialManyKCSelHelp.AddSelRow();
var
  i:integer;
begin
  with CDS_Sel do
  begin
    CDS_Sel.append;
    for i:=0 to ADOQuery1.FieldCount-1 do
    begin
      fields[i].Value:=ADOQuery1.Fields[i].Value;
    end;
    CDS_Sel.Post;
  end;

end;
procedure TfrmMaterialManyKCSelHelp.TcloseClick(Sender: TObject);
begin
  close;
end;

procedure TfrmMaterialManyKCSelHelp.TokClick(Sender: TObject);
begin
  if adoquery1.IsEmpty then
    ModalResult:=-1
  else
    modalResult:=1;
end;

procedure TfrmMaterialManyKCSelHelp.edttjKeyPress(Sender: TObject;
  var Key: Char);
begin
  if key=#13 then
  begin
    with ADOQuery1 do
    begin
      ////////
      if RecordCount>=1 then
      begin
        AddSelRow();
      end;
      edttj.SelectAll ;
      EnableControls;
    end;
  end;
end;

procedure TfrmMaterialManyKCSelHelp.Edit1Change(Sender: TObject);
begin
  try
      ADOQuery1.DisableControls;
      if trim(edit1.Text)<>'' then
      begin
        with ADOQuery1 do
        begin
          filtered:=false;
          filter:='model like '+quotedStr('%'+trim(edit1.Text)+'%');
          filtered:=true;
        end;
      end
      else
      begin
        with ADOQuery1 do
        begin
          filtered:=false;
        end;

      end;
  finally
      ADOQuery1.EnableControls;
  end;
end;

procedure TfrmMaterialManyKCSelHelp.Tv1DblClick(Sender: TObject);
begin
   AddSelRow();
end;

procedure TfrmMaterialManyKCSelHelp.TV2DblClick(Sender: TObject);
begin
  if CDS_sel.IsEmpty then exit;
  Cds_sel.Delete ;
end;

end.
