unit U_MaterialHelp;

interface

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

type
  TfrmMaterialHelp = class(TForm)
    DataSource1: TDataSource;
    ADOQueryHelp: TADOQuery;
    ToolBar1: TToolBar;
    Tok: TToolButton;
    ToolButton3: TToolButton;
    ImageList24: TImageList;
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    Label1: TLabel;
    edttj: TEdit;
    DataSourceSel: TDataSource;
    CDS_sel: TClientDataSet;
    Label2: TLabel;
    Edit1: TEdit;
    cxGrid1: TcxGrid;
    cxGrid1Level1: TcxGridLevel;
    tv1: TcxGridDBTableView;
    tv1mnumber1: TcxGridDBColumn;
    tv1mname1: TcxGridDBColumn;
    tv1model1: TcxGridDBColumn;
    tv1mareaname1: TcxGridDBColumn;
    tv1batchnum1: TcxGridDBColumn;
    tv1factoryname1: TcxGridDBColumn;
    tv1caizhi1: TcxGridDBColumn;
    tv1primaryunitname1: TcxGridDBColumn;
    tv1endquan1: TcxGridDBColumn;
    cxGrid2: TcxGrid;
    cxGrid2Level1: TcxGridLevel;
    Tv2: TcxGridDBTableView;
    Tv2mnumber1: TcxGridDBColumn;
    Tv2mname1: TcxGridDBColumn;
    Tv2model1: TcxGridDBColumn;
    Tv2mareaname1: TcxGridDBColumn;
    Tv2batchnum1: TcxGridDBColumn;
    Tv2factoryname1: TcxGridDBColumn;
    Tv2caizhi1: TcxGridDBColumn;
    Tv2primaryunitname1: TcxGridDBColumn;
    Tv2endquan1: TcxGridDBColumn;
    cxGridPopupMenu1: TcxGridPopupMenu;
    cxGridPopupMenu2: TcxGridPopupMenu;
    cxSplitter1: TcxSplitter;
    tv1fzEndQuan: TcxGridDBColumn;
    tv1purUnitName: TcxGridDBColumn;
    procedure FormShow(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure edttjChange(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure TokClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure DBGrid2DblClick(Sender: TObject);
    procedure edttjKeyPress(Sender: TObject; var Key: Char);
    procedure Edit1Change(Sender: TObject);
    procedure tv1DblClick(Sender: TObject);
  private
    procedure AddSelRow();
    procedure DoQuery();
    procedure CreateGrid();
  public
    fDisType:integer; //0:控制显示；1：显示全部现存量
    fInOutFlag:integer;
    FStoreCode:string;
    gisyxfck:Boolean;
    fsj:string;
  end;

var
  frmMaterialHelp: TfrmMaterialHelp;

implementation
uses
  U_adodbmd,U_formPas,U_iniParam,U_cxGridSet;
{$R *.dfm}

procedure TfrmMaterialHelp.FormShow(Sender: TObject);
begin
  DoQuery();
  edttj.SetFocus ;
end;

procedure TfrmMaterialHelp.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action:=caFree;
end;
/////////////////////////////////////////////////
  //条件过滤
/////////////////////////////////////////////////
procedure TfrmMaterialHelp.edttjChange(Sender: TObject);
begin
  try
      adoqueryHelp.DisableControls;
      if trim(edttj.Text)<>'' then
      begin
        with adoqueryHelp 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 adoqueryHelp do
        begin
          filtered:=false;
        end;

      end;
  finally
      adoqueryHelp.EnableControls;
  end;

end;

procedure TfrmMaterialHelp.ToolButton3Click(Sender: TObject);
begin
{  if trim(self.Caption)='' then
      WriteCxGrid(trim(self.Name),tv1,'纱线仓库')
  else
      WriteCxGrid(trim(self.Caption),tv1,'纱线仓库');
  if trim(self.Caption)='' then
      WriteCxGrid(trim(self.Name)+'2',tv2,'纱线仓库')
  else
      WriteCxGrid(trim(self.Caption)+'2',tv2,'纱线仓库');   }
  close;
end;

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

procedure TfrmMaterialHelp.FormCreate(Sender: TObject);
begin
  groupBox1.Align :=alClient;
  groupBox1.Visible:=false;
  cxSplitter1.Visible:= false;
  cxgrid1.Align :=alClient;
  CDS_Sel.Close;
  CreateGrid();
end;
/////////////////////////////////////////////////////
  //函数功能：增加选择的记录
////////////////////////////////////////////////////
procedure TfrmMaterialHelp.AddSelRow();
var
  i:integer;
begin
  
  with CDS_Sel do
  begin
    CDS_Sel.append;
    for i:=0 to ADOQueryHelp.FieldCount-1 do
    begin
      fields[i].Value:=ADOQueryHelp.Fields[i].Value;
    end;
    CDS_Sel.Post;
  end;

end;

procedure TfrmMaterialHelp.DBGrid2DblClick(Sender: TObject);
begin
  if CDS_Sel.IsEmpty then exit;
  CDS_sel.Delete;
end;

procedure TfrmMaterialHelp.edttjKeyPress(Sender: TObject; var Key: Char);
begin
  if key=#13 then
  begin
    with ADOQueryHelp do
    begin
      ////////
      if RecordCount>=1 then
      begin
        AddSelRow();
      end;
      edttj.SelectAll ;
      EnableControls;
    end;
  end;
end;
//////////////////////////////////////////////////////
  //
//////////////////////////////////////////////////////
procedure TfrmMaterialHelp.DoQuery();
begin
  with adoqueryHelp do
  begin
    close;
    sql.Clear;
    //出库
    if fInOutFlag=0 then
    begin
      sql.Add('select B.*,A.endQuan,A.fzendQuan,');
      sql.Add('A.BatchNum,A.marea,A.factory,A.factoryName,A.kcPrice,A.Price,');
      sql.Add('mareaname=(select MareaName from kf_marea where marea=A.marea)');
      sql.Add('from SX_nowquan A');
      sql.Add('inner join SX_Material B ON A.mNumber=B.mNumber');
      if trim(FStoreCode)<>'' then
        sql.Add('where A.whcode='''+FStoreCode+'''');
       //是否允许负出库
      if fDisType=0 then
      begin
        if not gisyxfck and (FStoreCode<>'') then
           sql.Add('and A.endQuan>0')
        else if not gisyxfck and (FStoreCode='') then
           sql.Add('where A.endQuan>0');
       end;
      sql.Add('order by B.whcode,B.Mnumber');
    end
    else
    begin
      sql.Add('select * from sx_Material A ');
      //if trim(FStoreCode)<>'' then
      //  sql.Add('where A.whcode='''+FStoreCode+'''');
      sql.Add('order by A.whcode,A.Mnumber');
    end;
    open;
  end;
  CreateCDS(adoqueryHelp,cds_sel);
end;

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

      end;
  finally
      adoqueryHelp.EnableControls;
  end;
end;
/////////////////////////////////////////////////
  //函数功能：创建表格
//////////////////////////////////////////////////
procedure TfrmMaterialHelp.CreateGrid();
begin
{  if trim(self.Caption)='' then
    ReadCxGrid(trim(self.Name),tv1,'纱线仓库')
  else
    ReadCxGrid(trim(self.Caption),tv1,'纱线仓库');
  if trim(self.Caption)='' then
    ReadCxGrid(trim(self.Name)+'2',tv2,'纱线仓库')
  else
    ReadCxGrid(trim(self.Caption)+'2',tv2,'纱线仓库');   }
end;
procedure TfrmMaterialHelp.tv1DblClick(Sender: TObject);
begin
//  AddSelRow();
tok.Click;
end;

end.
