unit U_JGCKListSel;

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, cxTextEdit,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator;

type
  TfrmJJGCKListSel = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Order_Main: TClientDataSet;
    Panel1: TPanel;
    v1Column1: TcxGridDBColumn;
    Label34: TLabel;
    v1Column7: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    KHNameHZ: TEdit;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    v1Column17: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    Label2: TLabel;
    MYName: TEdit;
    v1Column3: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    Label1: TLabel;
    Note: TEdit;
    v1JCDanWeiJC: TcxGridDBColumn;
    v1filler: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    Label5: TLabel;
    MYTypeFlag: TComboBox;
    v1Column11: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    Label7: TLabel;
    KHNameHZZ: TEdit;
    v1Column13: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Label8: TLabel;
    PBBatchNo: TEdit;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Label9: TLabel;
    MYColor: TEdit;
    Tv1Column6: TcxGridDBColumn;
    Label3: TLabel;
    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 cxTabControl1Change(Sender: TObject);
    procedure MYNameChange(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
  private
    procedure InitGrid();
    procedure InitForm();
    { Private declarations }
  public

    { Public declarations }
  end;

var
  frmJJGCKListSel: TfrmJJGCKListSel;

implementation
uses
  U_DataLink,U_RTFun;

{$R *.dfm}

procedure TfrmJJGCKListSel.FormDestroy(Sender: TObject);
begin
  frmJJGCKListSel:=nil;
end;

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

procedure TfrmJJGCKListSel.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('加工码单Sel',Tv1,'贸易生产管理');
  Close;
end;

procedure TfrmJJGCKListSel.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select AA.* ');
      sql.Add(',KHNameHZ=dbo.getpinyin(isnull(AA.KHNameJC,''''))+AA.GYSNo+isnull(AA.KHNameJC,'''')');
      sql.Add('from(');
      sql.Add(' select A.* ');
      sql.Add(',KHNameJC=(select Top 1 B.KHNameJC from KH_Main B ');
      sql.Add(' where isnull(B.KHFlag,'''')=''GYS'' and isnull(B.Valid,'''')=''Y'' and B.KHNO=A.GYSNo)');
      sql.Add(',KHNameJCDH=(select Top 1 B.KHNameJC from KH_Main B ');
      sql.Add(' where isnull(B.KHFlag,'''')=''GYS'' and isnull(B.Valid,'''')=''Y'' and B.KHNO=A.ToGYSNo)');
      sql.Add(',KHNameHZZ=dbo.getpinyin(isnull(KM.KHNameJC,''''))+B.KHNO+isnull(KM.KHNameJC,'''')');
      sql.Add(',KHNameJCZ=KM.KHNameJC,B.MLOrderNo,KHNOKH=B.KHNO');
      sql.Add(' from CK_MYSC_CR A  ');
      sql.Add(' left join ML_OrderMainNew B on A.MainId=B.NewMLID');
      sql.Add(' left join KH_Main KM on KM.KHNO=B.KHNO ');
      sql.Add(' where 1=1 ');
      sql.Add(' and A.CRTime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.Date))+'''');
      sql.Add(' and A.CRTime<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.Date+1))+'''');
      sql.Add(' and isnull(A.CRType,'''')=''加工码单'' ');
      SQL.Add(' and isnull(A.ToGYSNo,'''')=''GY0091'' ');
      sql.Add(')AA');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmJJGCKListSel.InitForm();
var
  fsj:string;
begin
  BegDate.Date:=StrToDate(formatdatetime('yyyy-MM',SGetServerDate(ADOQueryTemp))+'-01');
  EndDate.Date:=SGetServerDate(ADOQueryTemp);
  ReadCxGrid('加工码单Sel',Tv1,'贸易生产管理');
  fsj:='select Name=ZDYName,Code=Null from KH_Zdy where Type=''JGGongXu'' ';
  SInitComBoxBySql(ADOQueryTemp,MYTypeFlag,False,fsj);
  InitGrid();
end;

procedure TfrmJJGCKListSel.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 TfrmJJGCKListSel.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
  TBFind.Click;
end;



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

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

procedure TfrmJJGCKListSel.MYNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

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

end.
