unit bpck_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
	Tbpck_frmplan_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;
		DBGrid1: TDBGrid;
		Label4: TLabel;
		EdtSh: TEdit;
		Label5: TLabel;
		EdtHh: TEdit;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
		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);
	private
		{ Private declarations }
	public
		{ Public declarations }
	end;

var
	bpck_frmplan_find: Tbpck_frmplan_find;

implementation

uses U_adodbmd;

{$R *.dfm}

procedure Tbpck_frmplan_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 Tbpck_frmplan_find.ToolButton4Click(Sender: TObject);
begin
	ModalResult := mrCancel;
end;
(******************************************************************************)
(* ²ιΡ―                                                                       *)
(******************************************************************************)
procedure Tbpck_frmplan_find.ToolButton9Click(Sender: TObject);
var
	strSQL: String;
begin
{
	strSQL := 'SELECT A.PlanNo, A.P_Code, A.P_ChnName, A.P_SeqNo, A.P_Colstd, A.P_Color, '
//	         + 'Mf = RTRIM(STR(ROUND(P_Breadth/BE.UnitFact))) + ''/'' + LTRIM(STR(ROUND(P_UpBreadth/BE.UnitFact))) AS mf, '
					 + 'A.P_YHNo, B.CustomNo, C.ShortName, '
           + 'ChnName = ISNULL(D.ChnName, A.P_Color), '
					 + 'P_Num=ROUND(A.P_Num/UE.UnitFact, 2), B.Unit, UnitName=UE.UnitName, '
					 + 'P_Breadth=ROUND(A.P_Breadth/BE.UnitFact, 0), P_UpBreadth=ROUND(A.P_UpBreadth/BE.UnitFact, 0), '
					 + 'A.B_Unit, B_UnitName=BE.UnitName, '
					 + 'P_Gram=ROUND(A.P_Gram/GE.UnitFact, 0), A.G_Unit, G_UnitName=GE.UnitName, '
					 + 'F.P_Fhs, G.P_Kcs '
					 + 'FROM MV_ProductInfo_All A JOIN MV_PlanInfo_All B ON (B.PlanNo=A.PlanNo AND B.Type=A.Type)'
					 + 'LEFT JOIN BC_Customer C ON C.CustomNo=B.CustomNo '
					 + 'LEFT JOIN MC_Color D ON (D.ColorId=A.P_Color AND D.Valid=''Y'') '
					 + 'LEFT JOIN XC_Unit BE ON BE.UnitCode=A.B_Unit '
					 + 'LEFT JOIN XC_Unit GE ON GE.UnitCode=A.G_Unit '
					 + 'LEFT JOIN XC_Unit UE ON UE.UnitCode=B.Unit '
					 + 'LEFT JOIN (SELECT SUM(ISNULL(UQty, 0)) AS P_Fhs, PlanNo, P_Code, P_SeqNo FROM WD_InOut_MX '
					 + 'JOIN WD_InOut ON (WD_InOut.DepotNo=WD_InOut_MX.DepotNo AND WD_InOut.DepotType=0) '
					 + 'GROUP BY PlanNo, P_Code, P_SeqNo) F ON (F.PlanNo=A.PlanNo AND F.P_Code=A.P_Code AND F.P_SeqNo=A.P_SeqNo) '
					 + 'LEFT JOIN (SELECT SUM(ISNULL(UQty, 0)) AS P_Kcs, PlanNo, P_code, P_SeqNo '
					 + 'FROM WD_Clothing_MX GROUP BY PlanNo, P_Code, P_SeqNo) G ON (G.PlanNo=A.PlanNo AND G.P_Code=A.P_Code AND G.P_SeqNo=A.P_SeqNo) '
					 + 'WHERE A.P_Valid=''Y'' ';
}
	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,'#13
					+ '  A.P_Num, B.Unit,'#13
					+ '  A.P_Breadth, A.P_UpBreadth, A.B_Unit,'#13
					+ '  A.P_Gram, A.G_Unit,'#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
//					+ '  F.P_Fhs, G.P_Kcs'#13
					+ 'FROM MV_ProductInfo_All A'#13
          + '  JOIN MV_PlanInfo_All B ON (B.PlanNo=A.PlanNo)'#13
{
					+ '  LEFT JOIN (SELECT SUM(ISNULL(UQty, 0)) AS P_Fhs, PlanNo, P_Code, P_SeqNo FROM WD_InOut_MX '
					+ 'JOIN WD_InOut ON (WD_InOut.DepotNo=WD_InOut_MX.DepotNo AND WD_InOut.DepotType=0) '
					+ 'GROUP BY PlanNo, P_Code, P_SeqNo) F ON (F.PlanNo=A.PlanNo AND F.P_Code=A.P_Code AND F.P_SeqNo=A.P_SeqNo) '
					+ 'LEFT JOIN (SELECT SUM(ISNULL(UQty, 0)) AS P_Kcs, PlanNo, P_code, P_SeqNo '
					+ 'FROM WD_Clothing_MX GROUP BY PlanNo, P_Code, P_SeqNo) G ON (G.PlanNo=A.PlanNo AND G.P_Code=A.P_Code AND G.P_SeqNo=A.P_SeqNo) '
}
					+ '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 Tbpck_frmplan_find.DBGrid1DblClick(Sender: TObject);
begin
	ToolButton6Click(Sender);
end;

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

	ModalResult := mrOk;
end;

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

end.
