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;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    CDS_Main: TClientDataSet;
    Label5: TLabel;
    MLOrderNo: TEdit;
    MovePanel2: TMovePanel;
    ToolButton1: TToolButton;
    Label1: TLabel;
    MLCodeName: TEdit;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column1: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    v1Column2: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    lblKHName: TLabel;
    KHName: TEdit;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: 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 MLCodeNameChange(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure ToolButton1Click(Sender: TObject);
  private
    procedure InitGrid();
    { Private declarations }
  public
    { Public declarations }
    FCode, FClothType, FSeBie, FZZType: string;
    FKHNo, FTT: string;
  end;

var
  frmSQKCHZSelList: TfrmSQKCHZSelList;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp, U_JYMXInfo;

{$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();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add('select SUM(A.KGQty) KGQty,SUM(A.Qty) Qty,SUM(A.NetKGQty) NetKGQty,count(A.MJID) JQty   ');
      sql.Add(' ,A.MainId,A.SubId,A.CPType,B.MLOrderNo,B.KHName,B.MLCodeName,B.OrdDate,C.ConSubId  ');
      sql.Add(' ,C.MLColor,C.MLColorNo,C.MLHX,C.MLPrice,B.BZType,B.KHNo,C.FeeName,C.FeeMoney');
      sql.Add(',CRType=(select TOP 1 X.ConType from ML_ConMain X where X.MLConID=B.ConMainId)');
      sql.Add(',SYRName=(select TOP 1 X.SYRName from ML_ConMain X where X.MLConID=B.ConMainId)');
      sql.Add(' from  CK_MLCP_CR A inner join CK_MLCP_KC D on A.MJID=D.MJID ');
      sql.Add(' inner join ML_OrderMainNew B on A.MainId=B.NewMLID  ');
      sql.Add('  inner join ML_OrderSubidNew C on A.SubId=C.NewSubid  ');
      sql.Add('where A.CRFlag=''入库'' and D.KCValid=''Y''  ');
      sql.Add(' group by  B.ConMainId,MainId,A.SubId,C.ConSubId,A.CPType,B.MLOrderNo,B.KHName,B.MLCodeName,B.OrdDate  ');
      sql.Add(' ,C.MLColor,C.MLColorNo,C.MLHX,C.MLPrice,B.BZType,B.KHNo,C.FeeName,C.FeeMoney');
      Open;
//      ShowMessage(SQL.Text);
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmSQKCHZSelList.TBRafreshClick(Sender: TObject);
begin
  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('发货申请专用', Tv1, '坯布仓库');
  Close;
end;

procedure TfrmSQKCHZSelList.FormShow(Sender: TObject);
begin
  ReadCxGrid('发货申请专用', Tv1, '坯布仓库');
  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.MLCodeNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmSQKCHZSelList.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  try
    frmJYMXInfo := TfrmJYMXInfo.Create(Application);
    with frmJYMXInfo do
    begin
      FFMainId := Trim(self.CDS_Main.fieldbyname('MainId').AsString);
      FFClothType := Trim(self.CDS_Main.fieldbyname('ClothType').AsString);
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmJYMXInfo.Free;
  end;
end;

procedure TfrmSQKCHZSelList.ToolButton1Click(Sender: TObject);
var
  FFKHName, FFTT: string;
begin
  with CDS_Main do
  begin
    First;
    FFKHName := '';
    FFTT := '';
    while not Eof do
    begin
      if CDS_Main.fieldbyname('SSel').AsBoolean = True then
      begin

        if (Trim(FFTT) = '') and (Trim(CDS_Main.fieldbyname('SYRName').AsString) <> '') then
        begin
          FFTT := Trim(CDS_Main.fieldbyname('SYRName').AsString);
        end;

        if Trim(FFKHName) = '' then
        begin
          FFKHName := Trim(CDS_Main.fieldbyname('KHName').AsString);
        end;


        //同时选的生产单不能包含多个抬头
        if (Trim(CDS_Main.fieldbyname('SYRName').AsString) <> FFTT) and (Trim(CDS_Main.fieldbyname('SYRName').AsString) <> '') then
        begin
          Application.MessageBox('不能选择多个抬头!', '提示', 0);
          exit;
        end;

        //新选的抬头不能和已经选的抬头不同
        if (Trim(CDS_Main.fieldbyname('SYRName').AsString) <> FTT) and (FTT <> '') and (Trim(CDS_Main.fieldbyname('SYRName').AsString) <> '') then
        begin
          Application.MessageBox('不能选择其它抬头!', '提示', 0);
          exit;
        end;


        //同时选的生产单不能包含多个客户
        if (Trim(CDS_Main.fieldbyname('KHName').AsString) <> FFKHName) then
        begin
          Application.MessageBox('不能选择多个客户!', '提示', 0);
          exit;
        end;

        //新选的生产单不能和已经选的客户不同
        if (Trim(CDS_Main.fieldbyname('KHNo').AsString) <> FKHNo) and (FKHNo <> '') then
        begin
          Application.MessageBox('不能选择其它客户!', '提示', 0);
          exit;
        end;

      end;

      Next;
    end;
  end;
  ModalResult := 1;
end;

end.

