unit U_OrderListSel;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, ToolWin, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, ADODB,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel, cxClasses,
  cxControls, cxGridCustomView, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGrid, DBClient, cxCheckBox, cxCalendar, cxSplitter,
  RM_Dataset, RM_System, RM_Common, RM_Class, RM_GridReport, RM_e_Xls,
  Menus, cxButtonEdit, cxDropDownEdit, cxPC, Buttons;

type
  TfrmOrderListSel = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Order_Main: TClientDataSet;
    Panel1: TPanel;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport2: TRMXLSExport;
    CDS_PRT: TClientDataSet;
    Label2: TLabel;
    MLConNo: TEdit;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DataSource2: TDataSource;
    PB_MX: TClientDataSet;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyle2: TcxStyle;
    Label6: TLabel;
    MLOrderNo: TEdit;
    Cxgrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1OrdDate: TcxGridDBColumn;
    v1MLOrderNo: TcxGridDBColumn;
    v1MLConNo: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1MLCodeName: TcxGridDBColumn;
    v1KHName: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    Cxgrid1Level1: TcxGridLevel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label1: TLabel;
    Label3: TLabel;
    MLCodeName: TEdit;
    Label4: TLabel;
    KHNameHZ: TEdit;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure CustomerNoNameChange(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ZJStatusChange(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure MLOrderNoChange(Sender: TObject);
  private
    canshu1:string;
    DQdate:TDateTime;
    procedure InitGrid();
    procedure InitForm();
    { Private declarations }
  public
    FFInt,FCloth:Integer;

    { Public declarations }
  end;

var
  frmOrderListSel: TfrmOrderListSel;

implementation
uses
  U_DataLink,U_RTFun;

{$R *.dfm}

procedure TfrmOrderListSel.FormDestroy(Sender: TObject);
begin
  frmOrderListSel:=nil;
end;

procedure TfrmOrderListSel.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action:=caFree;
end;

procedure TfrmOrderListSel.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('指示单选择',Tv1,'贸易生产管理');
  Close;
end;

procedure TfrmOrderListSel.InitGrid();
var
  fwhere, Pwhere: string;
begin
  ToolBar1.SetFocus;
  Pwhere := SGetFilters(Panel1, 1, 2);
  fwhere := ' where OrdDate>=' + quotedstr(FormatDateTime('yyyy-MM-dd', BegDate.DateTime));
  fwhere := fwhere + ' and OrdDate<=' + quotedstr(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1));
  if trim(Pwhere) <> '' then
    fwhere := fwhere + ' and ' + trim(Pwhere);

  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add('SELECT AA.* FROM(');
      sql.add('select A.*,KM.KHNameJC,KHNameHZ=isnull(A.KHNO,'''')+isnull(KM.KHName,'''')+isnull(KM.KHNameJC,'''')');
      sql.Add('+dbo.getpinyin(isnull(KM.KHName,''''))+dbo.getpinyin(isnull(KM.KHNameJC,''''))');
      SQL.Add(',ZQty=(select Sum(B.Qty) from ML_OrderSubidNew B where B.NewMLID=A.NewMLID) ');

      sql.Add(',KHConNo=(select TOP 1 X.KHConNo from ML_ConMain X where X.MLConID=A.ConMainId)');
      sql.Add(' from ML_OrderMainNew A ');
      sql.Add(' left join KH_Main KM on KM.KHNO=A.KHNO ');
      sql.Add(')AA');
      sql.Add(fwhere);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;


procedure TfrmOrderListSel.InitForm();
begin
  BegDate.Date:=SGetServerDateMBeg(ADOQueryTemp);
  EndDate.Date:=SGetServerDate(ADOQueryTemp);
  ReadCxGrid('指示单选择',Tv1,'贸易生产管理');
  InitGrid();
end;

procedure TfrmOrderListSel.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active=False then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;



procedure TfrmOrderListSel.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmOrderListSel.FormShow(Sender: TObject);
begin
  InitForm();
end;

procedure TfrmOrderListSel.CheckBox1Click(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmOrderListSel.CheckBox2Click(Sender: TObject);
begin
  TBRafresh.Click;
end;

procedure TfrmOrderListSel.CustomerNoNameChange(Sender: TObject);
begin
  if ADOQueryMain.Active=False then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;

procedure TfrmOrderListSel.cxTabControl1Change(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmOrderListSel.ZJStatusChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmOrderListSel.Tv1DblClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  ModalResult:=1;
end;

procedure TfrmOrderListSel.MLOrderNoChange(Sender: TObject);
begin
  TBFind.Click;
end;

end.
