unit U_JBKCSelHelp;

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
  TfrmJBKCSelHelp = 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;
    ADOQuery1P_Code: TStringField;
    ADOQuery1P_Chnname: TWideStringField;
    ADOQuery1RollUnit: TStringField;
    ADOQuery1UnitName: TWideStringField;
    CDS_seP_Code: TStringField;
    CDS_selP_Chnname: TWideStringField;
    CDS_selRollUnit: TStringField;
    CDS_selUnitName: TWideStringField;
    GroupBox2: TGroupBox;
    dbGrid1: TcxGrid;
    dbGrid1Level1: TcxGridLevel;
    Tv1: TcxGridDBTableView;
    Tv1MNumber1: TcxGridDBColumn;
    Tv1Mname1: TcxGridDBColumn;
    Tv1CaiZhi1: TcxGridDBColumn;
    Tv1PrimaryUnitName1: TcxGridDBColumn;
    DBGrid2: TcxGrid;
    DBGrid2Level1: TcxGridLevel;
    TV2: TcxGridDBTableView;
    TV2MNumber1: TcxGridDBColumn;
    TV2Mname1: 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 Tv1DblClick(Sender: TObject);
    procedure TV2DblClick(Sender: TObject);
  private
    SortField,SortOrder,SortCaption:string;
    SortID:integer;
    procedure AddSelRow();
  public
    str_dylx:string;
    FPType:string;
  end;

var
  frmJBKCSelHelp: TfrmJBKCSelHelp;

implementation
uses
  U_global;

{$R *.dfm}

procedure TfrmJBKCSelHelp.FormShow(Sender: TObject);
begin
  with adoquery1 do
  begin
    close;
    sql.Clear;
    sql.Add('select A.P_Code,A.P_ChnName,CC.RollUnit,UnitName=(select UnitName from XC_Unit XX where XX.UnitCode=CC.RollUnit),CC.KCQuantity ');
    sql.Add('from MC_CatLog A left join ');
    sql.Add('(select BB.P_Code,BB.RollUnit ,');
    sql.Add('KCQuantity=(select Sum(Case When Convert(int,B.DepotType)%2=0 then isnull(Quantity,0)*(-1) ');
    SQL.Add('             when Convert(int,B.DepotType)%2=1 then isnull(Quantity,0) end)');
    sql.Add('             from WC_Clothinout B inner join WC_Clothinout_info C on B.DepotNo=C.DepotNo');
    sql.Add('             where C.P_Code=BB.P_Code and C.RollUnit=BB.RollUnit Group by C.P_Code,C.RollUnit) ');
    sql.Add(' from WC_Clothinout_info BB group by BB.P_Code,BB.RollUnit) CC on A.P_Code=CC.P_Code');
    sql.Add('where A.P_Code like '''+Trim(FPType)+'%'' order by CC.KCQuantity,A.P_ChnName ');  //--StopFlag=''0''
    open;
  end;
  edttj.SetFocus ;
end;

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

procedure TfrmJBKCSelHelp.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action:=caFree;
end;
/////////////////////////////////////////////////
  //条件过滤
/////////////////////////////////////////////////
procedure TfrmJBKCSelHelp.edttjChange(Sender: TObject);
begin
  try
      ADOQuery1.DisableControls;
      if trim(edttj.Text)<>'' then
      begin
        with ADOQuery1 do
        begin
          filtered:=false;
          filter:='P_Code like '+quotedStr('%'+trim(edttj.Text)+'%')+
                  ' or P_ChnName 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 TfrmJBKCSelHelp.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 TfrmJBKCSelHelp.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 TfrmJBKCSelHelp.TcloseClick(Sender: TObject);
begin
  close;
end;

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

procedure TfrmJBKCSelHelp.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 TfrmJBKCSelHelp.Tv1DblClick(Sender: TObject);
begin
   AddSelRow();
end;

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

end.
