unit U_PlanProuductList;

interface

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

type
  TfrmPlanProuductList = class(TForm)
    dbGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    Tv1plantype: TcxGridDBColumn;
    Tv1PlanNo: TcxGridDBColumn;
    Tv1exigence: TcxGridDBColumn;
    Tv1customName: TcxGridDBColumn;
    Tv1pbName: TcxGridDBColumn;
    v1PbSpec: TcxGridDBColumn;
    Tv1pnum: TcxGridDBColumn;
    Tv1Businees: TcxGridDBColumn;
    Tv1filltime: TcxGridDBColumn;
    Tv1Deliver: TcxGridDBColumn;
    Tv1statusName: TcxGridDBColumn;
    v1dhChk: TcxGridDBColumn;
    dbGrid1Level1: TcxGridLevel;
    ToolBar1: TToolBar;
    Tok: TToolButton;
    BtnCancel: TToolButton;
    ADOQueryHelp: TADOQuery;
    DataSource1: TDataSource;
    GroupBox1: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    Label4: TLabel;
    ComboBox1: TComboBox;
    begDate: TDateTimePicker;
    endDate: TDateTimePicker;
    edtPlan: TEdit;
    cxTabControl1: TcxTabControl;
    Tfind: TToolButton;
    Panel1: TPanel;
    v1pbWeight: TcxGridDBColumn;
    v1PbBreadth: TcxGridDBColumn;
    v1MachZsName: TcxGridDBColumn;
    v1jlUnitName: TcxGridDBColumn;
    edt_Name: TBtnEditA;
    Label6: TLabel;
    edt_cust: TBtnEditA;
    procedure BtnCancelClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure TokClick(Sender: TObject);
    procedure TfindClick(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure edt_NameBtnClick(Sender: TObject);
    procedure edt_custBtnClick(Sender: TObject);
  private
    procedure DoQuery();
    procedure DoFilter();
    procedure CreateGrid();
  public
    { Public declarations }
  end;

var
  frmPlanProuductList: TfrmPlanProuductList;

implementation
uses
  U_adodbmd,U_global,U_cxGridSet, U_ProductHelp, U_custHelp;
{$R *.dfm}

procedure TfrmPlanProuductList.BtnCancelClick(Sender: TObject);
begin
  if trim(self.Caption)='' then
      WriteCxGrid(trim(self.Name),tv1,'纱线成品仓库')
  else
      WriteCxGrid(trim(self.Caption),tv1,'纱线成品仓库');
  close;
end;

procedure TfrmPlanProuductList.FormCreate(Sender: TObject);
begin
  dbgrid1.Align :=alClient;
  begDate.DateTime :=Date()-30;
  endDate.DateTime :=date();
end;
//////////////////////////////////////////////////
  //函数：提交数据
//////////////////////////////////////////////////
procedure TfrmPlanProuductList.DoQuery();
var
  mAction:string;
  mwhere:string;
  mBegDate,mEndDate:string;
  mIdx:integer;
begin
  mBegDate:=formatDateTime('yyyy-MM-dd',begdate.Date);
  mEndDate:=formatDateTime('yyyy-MM-dd',enddate.Date+1);
  //业务员
  if gIsYwy then
    mwhere:=mwhere+' and BusnFill='''+trim(gUserName)+'''';
  midx:= cxTabControl1.TabIndex;
  mAction:=trim(cxTabControl1.Tabs[midx].Caption) ;
  try
    panel1.Visible :=true;
    panel1.Refresh ;
    ADOQueryHelp.DisableControls ;
    with ADOQueryHelp do
    begin
      close;
      filtered:=false;
      sql.Clear ;
      sql.Add('EXEC P_Get_SXCP_PlanList');
      sql.Add(intTostr(ComboBox1.ItemIndex));
      sql.Add(','+quotedStr(mAction));
      sql.Add(','+quotedStr(mBegDate));
      sql.Add(','+quotedStr(mEndDate));
      sql.Add(','+quotedStr(mwhere)); 
      Open;
    end;
  finally
    panel1.Visible :=false;
    ADOQueryHelp.EnableControls ;
  end;
end;
////////////////////////////////////////////////////
  //函数：过滤数据
////////////////////////////////////////////////////
procedure TfrmPlanProuductList.DoFilter();
var
  filterStr:string;
begin
  filterStr:='';
  //计划单号
  if trim(EdtPlan.Text) <>'' then
  begin
    filterStr:=' and planNo like '+quotedStr('%'+trim(EdtPlan.Text)+'%');
  end;
  //名称
  if trim(edt_Name.Text)<>'' then
  begin
    filterStr:=filterStr+' and P_chnName like '+quotedStr('%'+trim(edt_Name.Text)+'%');
  end;
  //客户
  if trim(Edt_cust.Text)<>'' then
    filterStr:=filterStr+' and customName like '+quotedStr('%'+trim(Edt_cust.Text)+'%');

  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 TfrmPlanProuductList.TokClick(Sender: TObject);
begin
  if ADOQueryHelp.IsEmpty then exit;
  ModalResult:=1;  
end;

procedure TfrmPlanProuductList.TfindClick(Sender: TObject);
begin
  DoQuery();
  DoFilter();
end;

procedure TfrmPlanProuductList.Tv1DblClick(Sender: TObject);
begin
  Tok.Click ;
end;

procedure TfrmPlanProuductList.cxTabControl1Change(Sender: TObject);
begin
  Tfind.Click ;
end;
/////////////////////////////////////////////////
  //函数功能：创建表格
//////////////////////////////////////////////////
procedure TfrmPlanProuductList.CreateGrid();
begin
  if trim(self.Caption)='' then
    ReadCxGrid(trim(self.Name),tv1,'纱线成品仓库')
  else
    ReadCxGrid(trim(self.Caption),tv1,'纱线成品仓库');

end;
procedure TfrmPlanProuductList.FormShow(Sender: TObject);
begin
  CreateGrid();
end;

procedure TfrmPlanProuductList.edt_NameBtnClick(Sender: TObject);
begin
  frmProductHelp:=TfrmProductHelp.create(self);
  with frmProductHelp do
  begin
    fDipFlag:=2;
    if showModal=1 then
    begin
      edt_Name.Text :=trim(ADOQueryHelp.fieldByName('P_chnName').AsString) ;
    end;
    free;
  end;
end;

procedure TfrmPlanProuductList.edt_custBtnClick(Sender: TObject);
begin
  frmCustHelp:=TfrmCustHelp.create(self);
  with frmCustHelp do
  begin
    if showModal=1 then
    begin
      edt_cust.TxtCode :=trim(ADOQueryHelp.fieldByName('customNo').AsString) ;
      edt_cust.Text :=trim(ADOQueryHelp.fieldByName('shortName').AsString) ;
    end;
    free;
  end;
end;

end.
