unit U_plan_find;

interface

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

type
	TfrmPlan_find = class(TForm)
		pubquery: TADOQuery;
		ADOQuery1: TADOQuery;
		DataSource1: TDataSource;
		ImageList1: TImageList;
		ToolBar1: TToolBar;
		ToolButton9: TToolButton;
		ToolButton5: TToolButton;
		ToolButton6: TToolButton;
		ToolButton8: TToolButton;
		ToolButton4: TToolButton;
		GroupBox1: TGroupBox;
		Label1: TLabel;
		DtpRq1: TDateTimePicker;
		Label2: TLabel;
		DtpRq2: TDateTimePicker;
		Label3: TLabel;
		EdtJhdh: TEdit;
		Label7: TLabel;
		EdtKh: TEdit;
		Label8: TLabel;
		EdtPm: TEdit;
		Label4: TLabel;
		EdtSh: TEdit;
		Label5: TLabel;
		EdtHh: TEdit;
    cxGrid1: TcxGrid;
    cxGrid1Level1: TcxGridLevel;
    Tv1: TcxGridDBTableView;
    Tv1PlanNo1: TcxGridDBColumn;
    Tv1BusineesName1: TcxGridDBColumn;
    Tv1CustomName1: TcxGridDBColumn;
    Tv1P_ChnName1: TcxGridDBColumn;
    Tv1P_Spec1: TcxGridDBColumn;
    Tv1P_YHNo1: TcxGridDBColumn;
    Tv1P_ColStd1: TcxGridDBColumn;
    Tv1ColorName1: TcxGridDBColumn;
    Tv1p_num1: TcxGridDBColumn;
    Tv1p_fhs1: TcxGridDBColumn;
    Tv1p_kcs1: TcxGridDBColumn;
    Tv1UnitName1: TcxGridDBColumn;
    Tv1filltime1: TcxGridDBColumn;
    Tv1P_Breadth1: TcxGridDBColumn;
    Tv1P_UpBreadth1: TcxGridDBColumn;
    Tv1B_UnitName1: TcxGridDBColumn;
    Tv1P_Gram1: TcxGridDBColumn;
    Tv1G_UnitName1: TcxGridDBColumn;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyle2: TcxStyle;
    v1P_BaseChnName: TcxGridDBColumn;
    v1P_Price: TcxGridDBColumn;
    v1Cost2: TcxGridDBColumn;
    v1Quantity2: TcxGridDBColumn;
    v1RRequest: TcxGridDBColumn;
		procedure FormShow(Sender: TObject);
		procedure ToolButton4Click(Sender: TObject);
		procedure ToolButton9Click(Sender: TObject);
		procedure DBGrid1DblClick(Sender: TObject);
		procedure ToolButton6Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
	private
		{ Private declarations }
	public
    DFlag:string;
		{ Public declarations }
	end;

var
	frmPlan_find: TfrmPlan_find;

implementation

uses U_adodbmd,U_global;

{$R *.dfm}

procedure TfrmPlan_find.FormShow(Sender: TObject);
var
	aDate: TDateTime;
begin
	pubquery.Close;
	pubquery.SQL.Clear;
	pubquery.SQL.Add('SELECT GETDATE()');
	pubquery.Open;

	aDate := pubquery.Fields[0].AsDateTime;

	DtpRq1.Date := aDate - 7;
	DtpRq2.Date := aDate;

	ToolButton9Click(Sender);
end;

procedure TfrmPlan_find.ToolButton4Click(Sender: TObject);
begin
	ModalResult := mrCancel;
end;
(******************************************************************************)
(* ²ιΡ―                                                                       *)
(******************************************************************************)
procedure TfrmPlan_find.ToolButton9Click(Sender: TObject);
var
	strSQL: String;
begin

	strSQL := 'SELECT A.PlanNo, A.P_SeqNo, A.P_Code, A.P_ChnName, A.P_Spec, A.P_Colstd, A.P_Color, A.ColorName,'#13
   			  + '  A.P_YHNo, B.CustomNo, B.CustomName, B.Businees, B.BusineesName,A.P_BaseChnName,'#13
					+ '  A.P_Num, B.Unit,C.Cost2,C.Quantity2,A.P_Price,'#13
					+ '  A.P_Breadth, A.P_UpBreadth, A.B_Unit,'#13
					+ '  A.P_Gram, A.G_Unit,B.RRequest,'#13
          + '  UnitName = ISNULL((SELECT TOP 1 UnitName FROM XC_Unit WHERE UnitCode = B.Unit), B.Unit),'#13
          + '  B_UnitName = (SELECT TOP 1 UnitName FROM XC_Unit WHERE UnitCode = A.B_Unit),'#13
          + '  G_UnitName = (SELECT TOP 1 UnitName FROM XC_Unit WHERE UnitCode = A.G_Unit)'#13
					+ 'FROM MV_ProductInfo_All A'#13
          + '  JOIN MV_PlanInfo_All B ON (B.PlanNo=A.PlanNo)'#13;
  strSQL:=strSQL+ ' LEFT JOIN MD_Zdy C ON (C.Code1=A.PlanNo and C.Code3=A.P_SeqNo and C.ZFlag='''+Trim(DFlag)+''')'#13;
  strSQL:=strSQL+'WHERE A.P_Valid=''Y'' '#13;

	if DtpRq1.Checked then
	begin
		strSQL := strSQL + ' AND B.FillTime>=' + QuotedStr(FormatDateTime('yyyy-MM-dd', DtpRq1.Date)) + #13;
	end;

	if DtpRq2.Checked then
	begin
		strSQL := strSQL + ' AND B.FillTime<' + QuotedStr(FormatDateTime('yyyy-MM-dd', DtpRq2.Date + 1)) + #13;
	end;

	if Trim(EdtJhdh.Text) <> '' then
	begin
		strSQL := strSQL + ' AND A.PlanNo LIKE ' + QuotedStr('%' + Trim(EdtJhdh.Text) + '%') + #13;
	end;

	if Trim(EdtSh.Text) <> '' then
	begin
		strSQL := strSQL + ' AND A.P_Colstd LIKE ' + QuotedStr(Trim(EdtSh.Text) + '%') + #13;
	end;

	if Trim(EdtKh.Text) <> '' then
	begin
		strSQL := strSQL + ' AND B.CustomName LIKE ' + QuotedStr('%' + Trim(EdtKh.Text) + '%') + #13;
	end;

	if Trim(EdtPm.Text) <> '' then
	begin
		strSQL := strSQL + ' AND A.P_ChnName LIKE ' + QuotedStr(Trim(EdtPm.Text) + '%') + #13;
	end;

	if Trim(EdtHh.text) <> '' then
	begin
		strSQL := strSQL + ' AND A.P_YHNo LIKE ' + QuotedStr('%' + Trim(EdtHh.Text) + '%') + #13;
	end;

	with ADOQuery1 do
	begin
		Close;
		SQL.Clear;
		SQL.Add(strSQL);
		Open;
	end;
end;

procedure TfrmPlan_find.DBGrid1DblClick(Sender: TObject);
begin
	ToolButton6Click(Sender);
end;

procedure TfrmPlan_find.ToolButton6Click(Sender: TObject);
begin
	if ADOQuery1.IsEmpty then Exit;

	ModalResult := mrOk;
end;

procedure TfrmPlan_find.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
	Action := caHide;
end;

procedure TfrmPlan_find.FormCreate(Sender: TObject);
begin
  cxGrid1.Align:=alClient;
end;

procedure TfrmPlan_find.Tv1CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  ToolButton6.Click;
end;

end.
