unit U_OrderSelDB;

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, BtnEdit;

type
  TfrmOrderSelDB = 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;
    cxGridDBColumn2: 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;
    v2Column8: TcxGridDBColumn;
    v2Column9: TcxGridDBColumn;
    PopupMenu2: TPopupMenu;
    MenuItem1: TMenuItem;
    MenuItem2: TMenuItem;
    Label1: TLabel;
    PRTHX: TEdit;
    v2Column10: TcxGridDBColumn;
    scbznote: TBtnEditA;
    Label4: TLabel;
    Button4: TButton;
    v2Column11: 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 OrderNoChange(Sender: TObject);
    procedure Tv2CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure MenuItem1Click(Sender: TObject);
    procedure MenuItem2Click(Sender: TObject);
    procedure scbznoteBtnClick(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  private
    { Private declarations }

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

var
  frmOrderSelDB: TfrmOrderSelDB;

implementation
uses
  U_DataLink,U_Fun , U_ZDYHelp;

{$R *.dfm}

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

procedure TfrmOrderSelDB.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(fieldbyname('scbznote').AsString)='' then
        begin
          Application.MessageBox('包装要不能为空！','提示',0);
          CDS_OrderSel.EnableControls;
          Exit;
        end;
        if Trim(KHName)='' then
        begin
          KHName:=Trim(fieldbyname('CustomerNo').AsString);
        end else
        begin
          if Trim(fieldbyname('CustomerNo').AsString)<>KHName then
          begin
            Application.MessageBox('不能选择不同客户！','提示',0);
            CDS_OrderSel.EnableControls;
            Exit;
          end;
        end;
      end;
      Next;
    end;
  end;
  CDS_OrderSel.EnableControls;
  ModalResult:=1;
end;

procedure TfrmOrderSelDB.FormDestroy(Sender: TObject);
begin
  frmOrderSelDB:=nil;
end;

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

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

procedure TfrmOrderSelDB.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('exec P_View_OrderDB ');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_OrderSel);
    SInitCDSData20(ADOQueryMain,CDS_OrderSel);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

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

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

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

procedure TfrmOrderSelDB.MenuItem1Click(Sender: TObject);
begin
 if CDS_OrderSel.IsEmpty then Exit;
  CDS_OrderSel.DisableControls;
  with CDS_OrderSel do
  begin
    First;
    while not Eof do
    begin
      Edit;
      FieldByName('SSel').Value:=True;
      Post;
      Next;
    end;
  end;
  CDS_OrderSel.EnableControls;
end;

procedure TfrmOrderSelDB.MenuItem2Click(Sender: TObject);
begin
  if CDS_OrderSel.IsEmpty then Exit;
  CDS_OrderSel.DisableControls;
  with CDS_OrderSel do
  begin
    First;
    while not Eof do
    begin
      Edit;
      FieldByName('SSel').Value:=False;
      Post;
      Next;
    end;
  end;
  CDS_OrderSel.EnableControls;
end;

procedure TfrmOrderSelDB.scbznoteBtnClick(Sender: TObject);
begin

  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='scbznote';
      flagname:='包装要求';
      if ShowModal=1 then
      begin
        scbznote.Text:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmOrderSelDB.Button4Click(Sender: TObject);
begin
  if Trim(scbznote.Text)='' then
  begin
    Application.MessageBox('包装要求不能为空!','提示',0);
    Exit;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate JYOrder_Main ');
    sql.Add(' Set scbznote='''+Trim(scbznote.Text)+'''');
    SQL.Add(' where MainId='''+Trim(CDS_OrderSel.fieldbyname('MainId').AsString)+'''');
    ExecSQL;
  end;
  Button1.Click;
end;

end.
