unit U_OrderSel;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, StdCtrls, ADODB, DBClient, ComCtrls,
  ExtCtrls, cxGridLevel, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxClasses, cxControls, cxGridCustomView, cxGrid,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxCheckBox, Menus;

type
  TfrmOrderSel = class(TForm)
    cxGrid1: TcxGrid;
    Tv2: TcxGridDBTableView;
    v1OrderNo: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    v1PRTMF: TcxGridDBColumn;
    v1PRTKZ: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Panel1: TPanel;
    Label2: TLabel;
    OrderNoM: TEdit;
    Label3: TLabel;
    PRTColor: TEdit;
    CDS_OrderSel: TClientDataSet;
    DataSource1: TDataSource;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    v2Column2: TcxGridDBColumn;
    Label8: TLabel;
    CustomerNoName: TEdit;
    v2Column3: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    v2Column1: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyleRepository2: TcxStyleRepository;
    cxStyle2: TcxStyle;
    cxStyleRepository3: TcxStyleRepository;
    cxStyle3: TcxStyle;
    v2Column7: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button2Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure OrderNoMChange(Sender: TObject);
    procedure Tv2CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
  public
    { Public declarations }
  end;

var
  frmOrderSel: TfrmOrderSel;

implementation
uses
  U_DataLink,U_Fun ;

{$R *.dfm}

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

procedure TfrmOrderSel.Button2Click(Sender: TObject);
var
  KHName:String;
begin
  if CDS_OrderSel.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据！','提示',0);
    Exit;
  end;
  CDS_OrderSel.DisableControls;
  KHName:='';
  with CDS_OrderSel do
  begin
    First;
    while not Eof do
    begin
      if FieldByName('SSel').AsBoolean=True then
      begin
        if Trim(KHName)='' then
        begin
          KHName:=Trim(fieldbyname('CustomerNo').AsString);
        end else
        begin
          if Trim(fieldbyname('CustomerNo').AsString)<>KHName then
          begin
            CDS_OrderSel.EnableControls;
            Application.MessageBox('不能选择不同客户！','提示',0);
            Exit;
          end;
        end;
      end;
      Next;
    end;
  end;
  CDS_OrderSel.EnableControls;
  ModalResult:=1;
end;

procedure TfrmOrderSel.FormDestroy(Sender: TObject);
begin
  frmOrderSel:=nil;
end;

procedure TfrmOrderSel.Button3Click(Sender: TObject);
begin
  ModalResult:=-1;
  WriteCxGrid('订单选择',Tv2,'成品仓库');
  Close;
end;

procedure TfrmOrderSel.FormShow(Sender: TObject);
begin
  ReadCxGrid('订单选择',Tv2,'成品仓库');
  InitGrid();
end;

procedure TfrmOrderSel.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select A.OrderNo OrderNoM,A.OrderNo,A.CustomerNoName,A.CustomerNo,A.MPRTCodeName,A.MPRTMF,A.MPRTKZ,B.*');
      sql.Add(',ZPPS=(select Count(*) from CK_BanCP_CR CR Inner join CK_BanCP_KC KC on CR.CRID=KC.CRID   ');
      sql.Add(' where CR.SubId=B.SubId and CR.CRType=''检验入库'' and (KC.KCQty>0 or KCKGQty>0) and CPType=''正品'') ');
      sql.Add(',ZPQty=(select sum(KCQty) from CK_BanCP_CR CR Inner join CK_BanCP_KC KC on CR.CRID=KC.CRID   ');
      sql.Add(' where CR.SubId=B.SubId and CR.CRType=''检验入库'' and (KC.KCQty>0 or KCKGQty>0) and CPType=''正品'') ');
      sql.Add(',CPPS=(select Count(*) from CK_BanCP_CR CR Inner join CK_BanCP_KC KC on CR.CRID=KC.CRID   ');
      sql.Add(' where CR.SubId=B.SubId and CR.CRType=''检验入库'' and (KC.KCQty>0 or KCKGQty>0) and CPType=''次品'') ');
      sql.Add(',CPQty=(select sum(KCQty) from CK_BanCP_CR CR Inner join CK_BanCP_KC KC on CR.CRID=KC.CRID   ');
      sql.Add(' where CR.SubId=B.SubId and CR.CRType=''检验入库'' and (KC.KCQty>0 or KCKGQty>0) and CPType=''次品'') ');
      sql.Add(',LYPS=(select Count(*) from CK_BanCP_CR CR Inner join CK_BanCP_KC KC on CR.CRID=KC.CRID   ');
      sql.Add(' where CR.SubId=B.SubId and CR.CRType=''检验入库'' and (KC.KCQty>0 or KCKGQty>0) and CPType=''留样'') ');
      sql.Add(',LYQty=(select sum(KCQty) from CK_BanCP_CR CR Inner join CK_BanCP_KC KC on CR.CRID=KC.CRID   ');
      sql.Add(' where CR.SubId=B.SubId and CR.CRType=''检验入库'' and (KC.KCQty>0 or KCKGQty>0) and CPType=''留样'') ');
      sql.Add(' from JYOrder_Main A inner join JYOrder_Sub B on A.MainId=B.Mainid');
      sql.Add(' where exists(select * from CK_BanCP_CR AA inner join CK_BanCP_KC BB on AA.CRID=BB.CRID ');
      sql.Add('              where AA.MainId=A.MainId and AA.SubId=B.SubId and AA.CRType=''检验入库'' and (BB.KCQty>0 or BB.KCKGQty>0))');

      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_OrderSel);
    SInitCDSData20(ADOQueryMain,CDS_OrderSel);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmOrderSel.Button1Click(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmOrderSel.OrderNoMChange(Sender: TObject);
begin
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,CDS_OrderSel);
  SInitCDSData20(ADOQueryMain,CDS_OrderSel);
end;

procedure TfrmOrderSel.Tv2CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  ModalResult:=1;
end;

procedure TfrmOrderSel.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_OrderSel,True);
end;

procedure TfrmOrderSel.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_OrderSel,False);
end;

end.
