unit U_SQKCHZSelList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, cxGridCustomTableView, cxGridTableView,
  cxGridBandedTableView, cxGridDBBandedTableView, cxGridLevel, cxClasses,
  cxControls, cxGridCustomView, cxGridDBTableView, cxGrid, StdCtrls, ComCtrls,
  ExtCtrls, ToolWin, cxGridCustomPopupMenu, cxGridPopupMenu, ADODB, DBClient,
  cxDropDownEdit, MovePanel, cxButtonEdit, cxCheckBox, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator;

type
  TfrmSQKCHZSelList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    CDS_Main: TClientDataSet;
    Tv1: TcxGridDBTableView;
    cxGrid2Level1: TcxGridLevel;
    cxGrid2: TcxGrid;
    v2Column1: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    MovePanel2: TMovePanel;
    v1Column7: TcxGridDBColumn;
    ToolButton1: TToolButton;
    v1Column12: TcxGridDBColumn;
    Panel1: TPanel;
    Label3: TLabel;
    Label5: TLabel;
    Label12: TLabel;
    Label14: TLabel;
    P_CodeName: TEdit;
    CONNO: TEdit;
    P_Code: TEdit;
    KHName: TEdit;
    v1Column13: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    Label11: TLabel;
    JYType: TComboBox;
    v1Column14: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoMChange(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure C_CodeNameChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure CONNOChange(Sender: TObject);
  private
    procedure InitGrid();
    { Private declarations }
  public
    { Public declarations }
    FCode, FClothType, FSeBie, FZZType, FKHName: string;
    fconno: string;
  end;

var
  frmSQKCHZSelList: TfrmSQKCHZSelList;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp;

{$R *.dfm}

procedure TfrmSQKCHZSelList.FormDestroy(Sender: TObject);
begin
  frmSQKCHZSelList := nil;
end;

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

procedure TfrmSQKCHZSelList.InitGrid();
var
  fwhere1, Pwhere, fwhere2: string;
begin
  Pwhere := SGetFilters(Panel1, 1, 2);
  if trim(Pwhere) <> '' then
  begin
    fwhere1 := fwhere1 + ' and ' + trim(Pwhere);
    fwhere2 := fwhere2 + ' where ' + trim(Pwhere);
  end;

  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      SQL.Add('select AA.*,BB.*,CC.* from ( ');
      SQL.Add(' select A.MainId,A.SubId,A.CKNo,A.CRType,A.JYType');
      SQL.Add(' ,COUNT(A.JYID) PiQty ,SUM(A.JYGW) JYGW,SUM(A.JYNW) JYNW,SUM(A.JYTare) JYTare,SUM(A.JSLen) JYLen,A.BatchNo,A.WXPH');
      SQL.Add('  from CK_Cloth_CR A');
      SQL.Add(' inner join CK_Cloth_KC B on A.JYID=B.JYID where B.Valid=''Y'' and A.CRFlag=''入库''');
      SQL.Add(' group by A.MainId,A.SubId,A.CKNo,A.CRType,A.JYType,A.BatchNo,A.WXPH');
      SQL.Add(' ) AA inner join PD_Plan_Main BB on AA.Mainid=BB.MainId');
      SQL.Add(' inner join PD_Plan_Sub CC on AA.SubId=CC.SubId ');
      sql.Add('where p_code in(select p_code from Contract_sub E INNER JOIN Contract_MAIN F ON E.CONMAINID=F.CONMAINID WHERE CONNO=' + QuotedStr(Trim(fconno)));
      SQL.Add(')');
      if FKHName <> '' then
      begin
        SQL.Add(' and BB.KHName=' + QuotedStr(FKHName));
        sql.Add(fwhere1);
      end
      else
        sql.Add(fwhere1);
      sql.Add(' ORDER BY BB.QDDate');
//      ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);

  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmSQKCHZSelList.TBRafreshClick(Sender: TObject);
begin
  Panel1.SetFocus;
  MovePanel2.Visible := True;
  MovePanel2.Refresh;
  InitGrid();
  MovePanel2.Visible := False;
end;

procedure TfrmSQKCHZSelList.ConNoMChange(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  end;
end;

procedure TfrmSQKCHZSelList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('发货申请专用1', Tv1, '坯布仓库1');
  Close;
end;

procedure TfrmSQKCHZSelList.FormShow(Sender: TObject);
begin
  ReadCxGrid('发货申请专用1', Tv1, '坯布仓库1');
  InitGrid();
end;

procedure TfrmSQKCHZSelList.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  end;
end;

procedure TfrmSQKCHZSelList.C_CodeNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmSQKCHZSelList.ToolButton1Click(Sender: TObject);
begin
  if CDS_Main.Locate('SSel', True, []) = False then
    Exit;
  ModalResult := 1;
end;

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

end.

