unit U_PurchasePlanSel;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, ToolWin, ComCtrls, U_BaseHelp, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxGridLevel, cxClasses, cxControls,
  cxGridCustomView, cxGrid, DBClient, ADODB, ImgList, StdCtrls, ExtCtrls,
  cxTextEdit, cxGridCustomPopupMenu, cxGridPopupMenu, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator, dxDateRanges, dxBarBuiltInMenu,
  System.ImageList, U_BaseInput, cxButtonEdit, cxDropDownEdit,
  dxScrollbarAnnotations, dxSkinsCore, dxSkinsDefaultPainters, cxCheckBox, cxPC;

type
  TfrmPurchasePlanSel = class(TfrmBaseHelp)
    ToolBar1: TToolBar;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ToolButton1: TToolButton;
    ADOConnection1: TADOConnection;
    Panel1: TPanel;
    Label1: TLabel;
    CoName: TEdit;
    GPM_1: TcxGridPopupMenu;
    ImageList1: TImageList;
    ThreeColorBase: TcxStyleRepository;
    SHuangSe: TcxStyle;
    SkyBlue: TcxStyle;
    Default: TcxStyle;
    QHuangSe: TcxStyle;
    Red: TcxStyle;
    FontBlue: TcxStyle;
    TextSHuangSe: TcxStyle;
    FonePurple: TcxStyle;
    FoneClMaroon: TcxStyle;
    FoneRed: TcxStyle;
    RowColor: TcxStyle;
    handBlack: TcxStyle;
    cxBlue: TcxStyle;
    SHuangSeCu: TcxStyle;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    v1SPName: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    v1SPSpec: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column15: TcxGridDBColumn;
    Tv1Column16: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column17: TcxGridDBColumn;
    Tv1Column18: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    Tv1Column12: TcxGridDBColumn;
    Tv1Column13: TcxGridDBColumn;
    Tv1Column14: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Tv1Column19: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure TV1DblClick(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure CoNameChange(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
  private
    procedure InitGrid();
    { Private declarations }
  public
    FSTKName: string;
    { Public declarations }
  end;

var
  frmPurchasePlanSel: TfrmPurchasePlanSel;

implementation

uses
  U_DataLink, U_RTFun;

{$R *.dfm}

procedure TfrmPurchasePlanSel.FormCreate(Sender: TObject);
begin
  inherited;
  try
    with ADOConnection1 do
    begin
      Connected := false;
      ConnectionString := DConString;
      Connected := true;
    end;
    ADOQueryBaseCmd.Connection := ADOConnection1;
    ADOQueryBaseTemp.Connection := ADOConnection1;
  except
    application.MessageBox('网络连接失败！', '提示信息');
  end;
end;

procedure TfrmPurchasePlanSel.CoNameChange(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);
  end;

end;

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

procedure TfrmPurchasePlanSel.FormClose(Sender: TObject; var Action: TCloseAction);
begin

  CoName.SetFocus;

  Action := cahide;
end;

procedure TfrmPurchasePlanSel.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      sql.Add(' select A.*,B.* ');
      sql.Add(' from Pur_YarnPlan_Main A');
      sql.Add(' right join Pur_YarnPlan_sub B on A.PurMId=B.PurMId');
      sql.Add(' where  (C_Degree = ''机物料'') or (C_Degree = ''钢筘'') ');
//      sql.Add(' where isnull(STKNAME,'''')=''' + Trim(FSTKName) + '''');
      case cxTabControl1.TabIndex of
        0:
          begin
            sql.Add(' and not EXISTS (select X.FromPurSId from BS_Product_IO X where X.FromPurSId=B.PurSId)  ');
          end;
        1:
          begin
            sql.Add(' and  EXISTS (select X.FromPurSId from BS_Product_IO X where X.FromPurSId=B.PurSId)  ');
          end;

      end;
      Open;
    end;

    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmPurchasePlanSel.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid(self.Caption, TV1, '采购单信息选择');
  InitGrid();
end;

procedure TfrmPurchasePlanSel.TBCloseClick(Sender: TObject);
begin
  inherited;
  Close;
end;

procedure TfrmPurchasePlanSel.TBSaveClick(Sender: TObject);
begin
  WriteCxGrid(self.Caption, TV1, '采购单信息选择');
end;

procedure TfrmPurchasePlanSel.ToolButton1Click(Sender: TObject);
begin
  CoName.SetFocus;
  ModalResult := 1;
end;

procedure TfrmPurchasePlanSel.TV1DblClick(Sender: TObject);
begin
  ModalResult := 1;
end;

procedure TfrmPurchasePlanSel.FormDestroy(Sender: TObject);
begin
  inherited;
  frmPurchasePlanSel := nil;
end;

end.

