unit U_ProductListHelp2;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, cxGraphics,
  cxControls, cxLookAndFeels, cxLookAndFeelPainters, cxStyles, cxCustomData,
  cxFilter, cxData, cxDataStorage, cxEdit, cxNavigator, dxDateRanges,
  dxScrollbarAnnotations, Data.DB, cxDBData, cxCalendar, cxGridLevel,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxClasses,
  cxGridCustomView, cxGrid, Vcl.StdCtrls, Vcl.ComCtrls, Vcl.ExtCtrls,
  Vcl.ToolWin, Data.Win.ADODB, U_BaseHelp, U_BaseList, System.ImageList,
  Vcl.ImgList, Datasnap.DBClient, dxBarBuiltInMenu, cxGridCustomPopupMenu,
  cxGridPopupMenu, dxLayoutContainer, dxLayoutControl, cxContainer, dxCore,
  cxDateUtils, dxLayoutcxEditAdapters, cxMaskEdit, cxDropDownEdit, cxTextEdit,
  cxLabel;

type
  TfrmProductListHelp2 = class(TfrmBaseHelp)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    V2Subid: TcxGridDBColumn;
    V2Column10: TcxGridDBColumn;
    V2CustomerNoName: TcxGridDBColumn;
    V2Column14: TcxGridDBColumn;
    V2C_Name: TcxGridDBColumn;
    V2C_Spec: TcxGridDBColumn;
    V2C_Color: TcxGridDBColumn;
    V2C_ColorNo: TcxGridDBColumn;
    V2C_Pattern: TcxGridDBColumn;
    V2OrdSNote: TcxGridDBColumn;
    V2packNote: TcxGridDBColumn;
    V2OrdQty: TcxGridDBColumn;
    V2OrderUnit: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    DataSource1: TDataSource;
    ADOQueryMain: TADOQuery;
    Order_Main: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    TselOk: TToolButton;
    ToolButton1: TToolButton;
    TV2Column1: TcxGridDBColumn;
    TV2Column2: TcxGridDBColumn;
    TV2Column3: TcxGridDBColumn;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    Tv1Column1: TcxGridDBColumn;
    Order_Sub: TClientDataSet;
    DataSource2: TDataSource;
    Panel1: TPanel;
    C_Name: TcxTextEdit;
    OrderNo: TcxTextEdit;
    custName: TcxComboBox;
    cxLabel1: TcxLabel;
    cxLabel2: TcxLabel;
    cxLabel3: TcxLabel;
    C_Color: TcxTextEdit;
    cxLabel4: TcxLabel;
    procedure TBCloseClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TselOkClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure custNamePropertiesChange(Sender: TObject);
    procedure TV2DblClick(Sender: TObject);
    procedure C_ColorPropertiesChange(Sender: TObject);
    procedure C_NamePropertiesChange(Sender: TObject);
    procedure OrderNoPropertiesChange(Sender: TObject);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
  private
    procedure InitForm();
    procedure InitGrid();
    procedure InitSubGrid();
  public
    fType: string;
    FCustName: string;
    FC_Color: string;
    FOrderNo: string;
  end;

var
  frmProductListHelp2: TfrmProductListHelp2;

implementation

uses
  U_DataLink, U_globalVar, U_RTFun, U_FormLayOutDesign;
{$R *.dfm}

procedure TfrmProductListHelp2.custNamePropertiesChange(Sender: TObject);
begin
  inherited;
  with Order_Main do
  begin
    Filtered := false;
    Filter := SGetFilters(panel1, 1, 2);
    Filtered := true;
  end;
end;

procedure TfrmProductListHelp2.TV2DblClick(Sender: TObject);
begin
  inherited;
  TselOk.Click;
end;

procedure TfrmProductListHelp2.C_ColorPropertiesChange(Sender: TObject);
begin
  inherited;

  with Order_Sub do
  begin
    Filtered := false;
    Filter := SGetFilters(panel1, 3, 4);
    Filtered := true;
  end;
end;

procedure TfrmProductListHelp2.C_NamePropertiesChange(Sender: TObject);
begin
  inherited;

  with Order_Main do
  begin
    Filtered := false;
    Filter := SGetFilters(panel1, 1, 2);
    Filtered := true;
  end;

end;

procedure TfrmProductListHelp2.FormCreate(Sender: TObject);
begin
  inherited;
//  cxGrid2.Align := alClient;
//  BegDate.Date := SGetServerDateTime(ADOQueryBaseTemp) - 15;
//  endDate.Date := SGetServerDateTime(ADOQueryBaseTemp)
end;

procedure TfrmProductListHelp2.FormShow(Sender: TObject);
begin
  inherited;
  InitForm();
  SInitTcxComBoxBySql(ADOQueryBaseCmd, custName, true, 'select distinct name = custName from trade_plan_main');
  CustName.Text := FCustName;

  if Trim(FOrderNo) <> '' then
  begin
    Order_Main.Locate('OrderNo', FOrderNo, []);
    InitSubGrid();
  end;

end;

procedure TfrmProductListHelp2.InitForm();
begin
  InitGrid();
  ReadCxGrid(self.Caption + tv2.Name, Tv2, gDllFileCaption);
  ReadCxGrid(self.Caption + tv1.Name, Tv1, gDllFileCaption);
end;

procedure TfrmProductListHelp2.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(panel1, 1, 2));
  SCreateCDS(ADOQueryMain, Order_Main);
  SInitCDSData(ADOQueryMain, Order_Main);
end;

procedure TfrmProductListHelp2.TBRafreshClick(Sender: TObject);
begin
  OrderNo.SetFocus;
  InitGrid();
end;

procedure TfrmProductListHelp2.ToolButton1Click(Sender: TObject);
begin
  WriteCxGrid(trim(Self.Caption) + tv2.Name, Tv2, gDllFileCaption);
  WriteCxGrid(trim(Self.Caption) + tv1.Name, Tv1, gDllFileCaption);
//  if gIsCanDesign then
//  begin
//    saveLayOut(application, dxLayoutControl_query, ADOQueryBaseCmd, PWideChar(fDllFileName + '|' + Self.Name + '|' + dxLayoutControl_query.Name + '.ini'));
//  end;

end;

procedure TfrmProductListHelp2.TselOkClick(Sender: TObject);
begin
  inherited;
  if Order_Sub.IsEmpty then
    exit;

  ModalResult := 1;
end;

procedure TfrmProductListHelp2.Tv1CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  inherited;
  InitSubGrid();
end;

procedure TfrmProductListHelp2.TBCloseClick(Sender: TObject);
begin
  close;
end;

procedure TfrmProductListHelp2.InitGrid();
var
  mBegDate: string;
  mendDate: string;
begin
//  if BegDate.Text <> '' then
//    mBegDate := FormatDateTime('yyyy-MM-dd', BegDate.Date);
//  if enddate.Text <> '' then
//    mendDate := FormatDateTime('yyyy-MM-dd', enddate.Date + 1);
  try
    Order_Main.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
//      sql.Add('select A.*    ');
//    //  sql.Add(',SumPiece=(select Sum(X.Piece) from BS_Cloth_IO X where X.IOFlag=''Èë¿â'' and X.fromSubID=A.SubID and isnull(X.STKNAME,'''')=' + Quotedstr(Trim(FSTKNAME)) + ')');
//      //sql.Add(',SumQty=(select Sum(X.Qty) from BS_Cloth_IO X where X.IOFlag=''Èë¿â'' and X.fromSubID=A.SubID and isnull(X.STKNAME,'''')=' + Quotedstr(Trim(FSTKNAME)) + ')');
//      sql.Add('from V_Trade_Plan_Fty A ');
//      sql.add('where Status=''9'' ');
//      if mBegDate<>'' then
//        sql.Add('and filltime>='+QuotedStr(mBegDate));
//      if mendDate<>'' then
//        sql.Add('and filltime<'+QuotedStTr(mendDate));
      sql.Add('select OrderNo, OrdDate, CustName, C_Name from Trade_Plan_Main A  ');
      sql.Add('join  Trade_Plan_Sub B   ');
      sql.Add('on A.MainId = B.MainId   ');
      sql.Add('where A.status =  ''9'' ');
      sql.Add('GROUP BY OrderNo, OrdDate, CustName, C_Name ');
      Open;
    end;
    SDofilter(ADOQueryMain, SGetFilters(panel1, 1, 2));
    SCreateCDS(ADOQueryMain, Order_Main);
    SInitCDSData(ADOQueryMain, Order_Main);
  finally
    Order_Main.EnableControls;
  end;
end;

procedure TfrmProductListHelp2.InitSubGrid();
begin
  try
    Order_Sub.DisableControls;
    with ADOQueryBaseTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select A.*    ');
    //  sql.Add(',SumPiece=(select Sum(X.Piece) from BS_Cloth_IO X where X.IOFlag=''Èë¿â'' and X.fromSubID=A.SubID and isnull(X.STKNAME,'''')=' + Quotedstr(Trim(FSTKNAME)) + ')');
      //sql.Add(',SumQty=(select Sum(X.Qty) from BS_Cloth_IO X where X.IOFlag=''Èë¿â'' and X.fromSubID=A.SubID and isnull(X.STKNAME,'''')=' + Quotedstr(Trim(FSTKNAME)) + ')');
      sql.Add('from V_Trade_Plan_Fty A ');
      sql.add('where Status=''9'' ');
      SQL.Add(' and OrderNo=' + QuotedStr(Trim(Order_Main.FieldByName('OrderNo').AsString)));
      Open;
    end;
    SDofilter(ADOQueryBaseTemp, SGetFilters(panel1, 3, 4));
    SCreateCDS(ADOQueryBaseTemp, Order_Sub);
    SInitCDSData(ADOQueryBaseTemp, Order_Sub);
  finally
    Order_Sub.EnableControls;
  end;
end;

procedure TfrmProductListHelp2.OrderNoPropertiesChange(Sender: TObject);
begin
  inherited;

  with Order_Main do
  begin
    Filtered := false;
    Filter := SGetFilters(panel1, 1, 2);
    Filtered := true;
  end;

end;

end.

