unit U_PbxhHelp_Dx;

interface

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

type
  TfrmPbxhHelp_Dx = class(TForm)
    Panel1: TPanel;
    Label1: TLabel;
    Label3: TLabel;
    P_chnName: TEdit;
    ADOQueryHelp: TADOQuery;
    DataSource1: TDataSource;
    Label4: TLabel;
    pbxh: TEdit;
    btFind: TBitBtn;
    BtOk: TBitBtn;
    btCancel: TBitBtn;
    DBGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    dbGrid1Level1: TcxGridLevel;
    v1customName: TcxGridDBColumn;
    v1P_chnName: TcxGridDBColumn;
    v1inNum: TcxGridDBColumn;
    v1rollNum: TcxGridDBColumn;
    v1Quantity: TcxGridDBColumn;
    v1unitName: TcxGridDBColumn;
    Label2: TLabel;
    ListNo: TEdit;
    v1ListNo: TcxGridDBColumn;
    v1P_code: TcxGridDBColumn;
    Label5: TLabel;
    P_code: TEdit;
    Label6: TLabel;
    ADOQueryTmp: TADOQuery;
    BtnEditC1: TBtnEditC;
    v1rollSpec: TcxGridDBColumn;
    v1filltime: TcxGridDBColumn;
    v1P_spec: TcxGridDBColumn;
    Edt_cust: TEdit;
    CheckBox1: TCheckBox;
    ADOConnection1: TADOConnection;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyle2: TcxStyle;
    cxStyle_gridRow: TcxStyle;
    cxStyle_gridFoot: TcxStyle;
    cxStyle_gridHead: TcxStyle;
    cxStyle_gridGroupBox: TcxStyle;
    cxStyle_yellow: TcxStyle;
    cxStyle_Red: TcxStyle;
    cxStyle_focus: TcxStyle;
    cxStyle_fontBlue: TcxStyle;
    cxStyle_fontclPurple: TcxStyle;
    cxStyle_fontBlack: TcxStyle;
    cxStyle_fontclFuchsia: TcxStyle;
    cxStyle_fontclOlive: TcxStyle;
    GroupBox1: TGroupBox;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    tv2inNum: TcxGridDBColumn;
    tv2rollNum: TcxGridDBColumn;
    tv2Quantity: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    DS_sel: TDataSource;
    CDS_sel: TClientDataSet;
    tv2unitName: TcxGridDBColumn;
    v1packs: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    procedure FormCreate(Sender: TObject);
    procedure btFindClick(Sender: TObject);
    procedure btCancelClick(Sender: TObject);
    procedure BtOkClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure BtnEditC1BtnUpClick(Sender: TObject);
    procedure BtnEditC1BtnDnClick(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure Tv2DblClick(Sender: TObject);
  private
    procedure DoQuery();
    procedure DoFilter();
    procedure AddRowData();
  public
    fSelType:integer;  //0:单选；1：多选
    fStoreCode:string;  //仓库名称
    fStoretType:string; //仓库类型
    fcustomNo:string;
  end;

var
  frmPbxhHelp_Dx: TfrmPbxhHelp_Dx;

implementation
uses
  U_formPas, U_XcCodeHelp,U_global;
{$R *.dfm}
///////////////////////////////////////////////
  //函数功能：数据查询
///////////////////////////////////////////////
procedure TfrmPbxhHelp_Dx.DoQuery();
begin
  try
    with ADOQueryHelp do
    begin
      DisableControls ;
      filtered:=false;
      close;
      sql.clear;
      sql.Add('exec P_get_PbXh_Help');
      sql.Add(QuotedStr(fStoreCode));
      sql.Add(','+quotedstr(trim(Edt_cust.Text)));
      sql.Add(','+quotedstr(trim(P_code.Text)));
      sql.Add(','+quotedStr(fStoretType));
      Open;
      CreateCDS(ADOQueryHelp,CDS_Sel);
    end;
  finally
     ADOQueryHelp.EnableControls;
  end;
end;
procedure TfrmPbxhHelp_Dx.FormCreate(Sender: TObject);
begin
  dbgrid1.Align :=alClient;
  with ADOConnection1 do
  begin
    Connected:=false;
    ConnectionString:=gConString;
    Connected:=true;
  end;
  fStoreCode:='JRPC';
end;

procedure TfrmPbxhHelp_Dx.btFindClick(Sender: TObject);
begin
  DoQuery();
  DoFilter();
end;

procedure TfrmPbxhHelp_Dx.btCancelClick(Sender: TObject);
begin
  ModalResult:=-1;
end;

procedure TfrmPbxhHelp_Dx.BtOkClick(Sender: TObject);
begin
  if ADOQueryhelp.IsEmpty then
    ModalResult:=-1
  else
  begin
    if fSelType=0 then
      ModalResult:=1
    else if not CDS_sel.IsEmpty then
      ModalResult:=1
    else
      ModalResult:=0;
  end;
end;

procedure TfrmPbxhHelp_Dx.FormShow(Sender: TObject);
begin
  if fSelType=0 then
    groupBox1.Visible :=false;
  //Edt_cust.text :=fCustomNo;
  DoQuery();
  DoFilter();
end;
////////////////////////////////////////////////////
  //函数：过滤数据
////////////////////////////////////////////////////
procedure TfrmPbxhHelp_Dx.DoFilter();
var
  filterStr:string;
begin
  filterStr:='';
  if trim(Edt_cust.text)<>'' then  //'and ( customNo like ''%'+trim(Edt_cust.text)+'%'''+
     filterStr:='and customName like ''%'+trim(Edt_cust.text)+'%''';
  if trim(ListNo.text)<>'' then
     filterStr:=filterStr+' and ListNo like '+quotedStr('%'+trim(ListNo.text)+'%');

  if trim(P_code.text)<>'' then
     filterStr:=filterStr+' and P_code like '+quotedStr('%'+trim(P_code.text)+'%');

  if trim(P_chnName.text)<>'' then
     filterStr:=filterStr+' and P_chnName like '+quotedStr('%'+trim(P_chnName.text)+'%');

  if trim(pbxh.text)<>'' then
     filterStr:=filterStr+' and inNum like '+quotedStr('%'+trim(pbxh.text)+'%');
  if trim(BtnEditC1.text)<>'' then
     filterStr:=filterStr+' and rollSpec like '+quotedStr('%'+trim(BtnEditC1.text)+'%');

  if CheckBox1.Checked then
     filterStr:=filterStr+' and (quantity>0.0 )';      //or rollNum>0 or packs>0
  try
    ADOQueryhelp.DisableControls ;
    if trim(filterStr)='' then
    begin
      ADOQueryhelp.Filtered:=false;
      ADOQueryhelp.EnableControls;
      exit;
    end;
    filterStr:=trim(RightBStr(filterStr,length(filterStr)-4));
    with ADOQueryhelp do
    begin
      filtered:=false;
      filter:=filterStr;
      filtered:=true;
    end;
  finally
    ADOQueryhelp.EnableControls;
  end;
end;

procedure TfrmPbxhHelp_Dx.Tv1DblClick(Sender: TObject);
begin
  if fSelType=0 then
    BtOk.Click
  else
    AddRowData();
end;

procedure TfrmPbxhHelp_Dx.BtnEditC1BtnUpClick(Sender: TObject);
begin
  frmXcCodeHelp:=TfrmXcCodeHelp.create(self);
  with frmXcCodeHelp do
  begin
    pflag:='PBJGLX';
    if showModal=1 then
    begin
      //BtnEditC1.TxtCode :=trim(ADOQueryHelp.fieldByName('code').AsString);
      BtnEditC1.Text :=fSelResult;//trim(ADOQueryHelp.fieldByName('Name').AsString);
    end;
    free;
  end;
end;

procedure TfrmPbxhHelp_Dx.BtnEditC1BtnDnClick(Sender: TObject);
begin
  BtnEditC1.TxtCode :='';
  BtnEditC1.Text :='';
end;

procedure TfrmPbxhHelp_Dx.CheckBox1Click(Sender: TObject);
begin
  dofilter();
end;
////////////////////////////////////////////////////////
  //
////////////////////////////////////////////////////////
procedure TfrmPbxhHelp_Dx.AddRowData();
begin
  with CDS_sel do
  begin
    if locate('inNum',trim(ADOQueryHelp.fieldByName('InNum').AsString),[]) then
    begin
      application.MessageBox(pchar('坯布序号'+trim(ADOQueryHelp.fieldByName('InNum').AsString)
                                    +'已存在!'),'提示信息',0);
      exit;
    end;
    Append;
    fieldByName('customName').Value :=trim(ADOQueryHelp.fieldByName('customName').AsString) ;
    fieldByName('ListNo').Value :=trim(ADOQueryHelp.fieldByName('ListNo').AsString) ;
    fieldByName('InNum').Value :=trim(ADOQueryHelp.fieldByName('InNum').AsString) ;
    fieldByName('P_code').Value :=trim(ADOQueryHelp.fieldByName('P_code').AsString) ;
    fieldByName('P_chnName').Value :=trim(ADOQueryHelp.fieldByName('P_chnName').AsString) ;
    fieldByName('P_spec').Value :=trim(ADOQueryHelp.fieldByName('P_spec').AsString) ;
    fieldByName('unitName').Value :=trim(ADOQueryHelp.fieldByName('unitName').AsString) ;
    fieldByName('rollUnit').Value :=trim(ADOQueryHelp.fieldByName('rollUnit').AsString) ;
    fieldByName('rollSpec').Value :=trim(ADOQueryHelp.fieldByName('rollSpec').AsString) ;
    fieldByName('depotPlace').Value :=trim(ADOQueryHelp.fieldByName('depotPlace').AsString) ;
    fieldByName('rollNum').Value :=ADOQueryHelp.fieldByName('rollNum').AsFloat ;
    fieldByName('Quantity').Value :=ADOQueryHelp.fieldByName('Quantity').AsFloat ;
    fieldByName('packs').Value :=ADOQueryHelp.fieldByName('packs').AsFloat ;
    fieldByName('packUnit').Value :=trim(ADOQueryHelp.fieldByName('packUnit').AsString) ;
    Post;
  end;
end;

procedure TfrmPbxhHelp_Dx.Tv2DblClick(Sender: TObject);
begin
  if CDS_Sel.IsEmpty then exit;
  CDS_Sel.Delete ;
end;

end.
