unit U_OrderSelRK;

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

type
  TfrmOrderSelRK = class(TForm)
    Panel1: TPanel;
    Label2: TLabel;
    OrderNo: TEdit;
    Label3: TLabel;
    PRTColor: TEdit;
    CDS_OrderSel: TClientDataSet;
    DataSource1: TDataSource;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label8: TLabel;
    CustomerNoName: TEdit;
    cxGrid1: TcxGrid;
    Tv2: TcxGridDBTableView;
    v2Column3: TcxGridDBColumn;
    v1OrderNo: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v1PRTCodeName: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    v1PRTMF: TcxGridDBColumn;
    v1PRTKZ: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyleRepository2: TcxStyleRepository;
    cxStyle2: TcxStyle;
    cxStyleRepository3: TcxStyleRepository;
    cxStyle3: TcxStyle;
    v2Column7: TcxGridDBColumn;
    PRTCodeName: TEdit;
    Label1: TLabel;
    v2Column8: TcxGridDBColumn;
    v2Column9: TcxGridDBColumn;
    v2Column10: TcxGridDBColumn;
    CheckBox1: TCheckBox;
    Tv2Column1: TcxGridDBColumn;
    Label4: TLabel;
    prtkuanno: TEdit;
    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 OrderNoChange(Sender: TObject);
    procedure Tv2CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure CheckBox1Click(Sender: TObject);
  private
    { Private declarations }

    procedure InitGrid();
  public
    { Public declarations }
  end;

var
  frmOrderSelRK: TfrmOrderSelRK;

implementation

uses
  U_DataLink, U_Fun;

{$R *.dfm}

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

procedure TfrmOrderSelRK.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
            Application.MessageBox('不能选择不同客户！', '提示', 0);
            Exit;
          end;
        end;
      end;
      Next;
    end;
  end;
  CDS_OrderSel.EnableControls;
  ModalResult := 1;
end;

procedure TfrmOrderSelRK.FormDestroy(Sender: TObject);
begin
  frmOrderSelRK := nil;
end;

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

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

procedure TfrmOrderSelRK.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add('select A.OrderNo,A.CustomerNoName,A.CustomerNo,B.PRTCodeName,A.MainId,B.SubId,B.PRTColor,B.SOrddefstr1,B.OrderUnit,B.PRTOrderQty');
      sql.Add(',B.PRTMF,B.PRTKZ,B.PRTHX,B.prtkuanno');
      sql.Add(',ZPPS=(select Count(MJID) from WFB_MJJY WY ');
      sql.Add(' where WY.subid=B.subid and isnull(WY.MJStr2,'''')=''未入库'' ) ');
      sql.Add(',ZPQty=(select sum(MJLen) from WFB_MJJY WY    ');
      sql.Add(' where WY.subid=B.subid and isnull(WY.MJStr2,'''')=''未入库''  ) ');
      sql.Add(',ZPKGQty=(select sum(MJMaoZ) from WFB_MJJY WY    ');
      sql.Add(' where WY.subid=B.subid and isnull(WY.MJStr2,'''')=''未入库''  ) ');
      sql.Add(',CPPS=(select Count(MJID) from WFB_MJJY WY    ');
      sql.Add(' where WY.subid=B.subid and isnull(WY.MJStr2,'''')=''未入库'' and WY.MJType=''次品'') ');
      sql.Add(',CPQty=(select sum(MJLen) from WFB_MJJY WY ');
      sql.Add(' where WY.subid=B.subid and isnull(WY.MJStr2,'''')=''未入库'' and WY.MJType=''次品'') ');
      sql.Add(',CPKGQty=(select sum(MJMaoZ) from WFB_MJJY WY ');
      sql.Add(' where WY.subid=B.subid and isnull(WY.MJStr2,'''')=''未入库'' and WY.MJType=''次品'') ');
      sql.Add(' from JYOrder_Main A inner join JYOrder_Sub B on A.MainId=B.Mainid');
     // sql.add(' inner join WFB_MJJY C on C.SubID=B.Subid and C.Mainid=B.Mainid ');
      sql.Add(' where exists(select MJID from WFB_MJJY AA ');
      sql.Add('              where AA.MainId=A.MainId and AA.Subid=B.Subid and isnull(AA.MJStr2,'''')=''未入库'' ) ');
      sql.Add('group by A.OrderNo,A.CustomerNoName,A.CustomerNo,B.PRTCodeName,A.MainId,B.SubId,B.PRTColor,B.SOrddefstr1,B.OrderUnit,B.PRTOrderQty');
      sql.Add(',B.PRTMF,B.PRTKZ,B.PRTHX,B.prtkuanno');
      sql.Add('order by cast(dbo.Get_ShuZi(B.SOrddefstr1) as bigint)');
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_OrderSel);
    SInitCDSData20(ADOQueryMain, CDS_OrderSel);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

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

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

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

procedure TfrmOrderSelRK.CheckBox1Click(Sender: TObject);
begin
  SelOKNo(CDS_OrderSel, CheckBox1.Checked);
end;

end.

