unit U_SQKCHZSelList2;

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
  TfrmSQKCHZSelList2 = 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;
    v1Column6: TcxGridDBColumn;
    MovePanel2: TMovePanel;
    v1Column7: TcxGridDBColumn;
    ToolButton1: TToolButton;
    Panel1: TPanel;
    Label3: TLabel;
    Label12: TLabel;
    P_CodeName: TEdit;
    P_Code: TEdit;
    v1Column13: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: 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
  frmSQKCHZSelList2: TfrmSQKCHZSelList2;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp;

{$R *.dfm}

procedure TfrmSQKCHZSelList2.FormDestroy(Sender: TObject);
begin
  frmSQKCHZSelList2 := nil;
end;

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

procedure TfrmSQKCHZSelList2.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  a.p_code,a.p_codename');
      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(',sum(case when JYType=''次品'' then 1 else 0 end )cpps');
      SQL.Add(' ,sum(case when JYType=''正品'' then 1 else 0 end )Zpps');
      SQL.Add(',sum(case when JYType=''次品'' then A.JSLen else 0 end )cpqty');
      SQL.Add(',sum(case when JYType=''正品'' then A.JSLen else 0 end )Zpqty');
      SQL.Add('  from CK_Cloth_CR A');
      SQL.Add(' inner join CK_Cloth_KC B on A.JYID=B.JYID ');
      sql.Add('where B.Valid=''Y'' and A.CRFlag=''入库''');
      sql.Add('and 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(')');

      SQL.Add(' group by  p_code,p_codename,A.BatchNo,A.WXPH');

      sql.Add(' ORDER BY a.p_code');
      
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);

  finally
    ADOQueryMain.EnableControls;
  end;
end;

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

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

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

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

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

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

procedure TfrmSQKCHZSelList2.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.

