unit U_CPSMRk;

interface

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

type
  TfrmCPSMRk = class(TForm)
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    Panel1: TPanel;
    Button2: TButton;
    Button3: TButton;
    DS_RKJL: TDataSource;
    CDS_RKJL: TClientDataSet;
    PM_RKJL: TPopupMenu;
    MenuItem1: TMenuItem;
    MenuItem2: TMenuItem;
    CRTime: TDateTimePicker;
    Label4: TLabel;
    Button6: TButton;
    RKOrdID: TEdit;
    Label5: TLabel;
    RKOrdPS: TEdit;
    Button7: TButton;
    RM1: TRMGridReport;
    ADOQueryPrint: TADOQuery;
    RMDB_Main: TRMDBDataSet;
    Label6: TLabel;
    edtScan: TEdit;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    cxGridDBColumn13: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure MenuItem1Click(Sender: TObject);
    procedure MenuItem2Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure edtScanKeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
    FBaoId: string;
    procedure InitGrid();
    function CheckData(): Boolean;
  public
    { Public declarations }
  end;

var
  frmCPSMRk: TfrmCPSMRk;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp;

{$R *.dfm}
function TfrmCPSMRk.CheckData(): Boolean;
var
  mvalue: Double;
begin
  try
    if Trim(RKOrdID.Text) = '' then
      raise Exception.Create('入库单号不能为空！');
    if Trim(edtScan.Text) = '' then
      raise Exception.Create('请扫描！');
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select count(*) JLS from CK_MLCP_CR where MJID=' + QuotedStr(Trim(edtScan.Text)));
      sql.Add('and CRFlag in (''待入库'' ,''待出库'') ');
      Open;
    end;
    if ADOQueryTemp.fieldbyname('JLS').AsInteger = 1 then
      raise Exception.Create('已入库暂停发货！');
    if ADOQueryTemp.fieldbyname('JLS').AsInteger <> 2 then
      raise Exception.Create('已出库暂停发货！');
    Result := True;
  except
    Result := false;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

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

procedure TfrmCPSMRk.FormDestroy(Sender: TObject);
begin
  frmCPSMRk := nil;
end;

procedure TfrmCPSMRk.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add(' select A.*  ');
      sql.Add(' ,B.MLOrderNo,B.KHName,B.MLCodeName,B.OrdDate  ');
      sql.Add(' ,C.MLColor,C.MLColorNo,C.MLHX');
      sql.Add('  from CK_MLCP_CR A  inner join ML_OrderMainNew B on A.MainId=B.NewMLID inner join ML_OrderSubidNew C on A.SubId=C.NewSubid ');
      sql.Add(' where  A.CRFlag=''入库''  and A.RKOrdNo=' + quotedstr(RKOrdID.Text));
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_RKJL);
    SCreateCDS20(ADOQueryMain, CDS_RKJL);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmCPSMRk.FormShow(Sender: TObject);
begin
  ReadCxGrid('成品扫描入库', Tv1, '成品仓库');
  InitGrid();
  CRTime.DateTime := SGetServerDate(ADOQueryTemp);
end;

procedure TfrmCPSMRk.Button2Click(Sender: TObject);
begin
  WriteCxGrid('成品扫描入库', Tv1, '成品仓库');
  Close;
end;

procedure TfrmCPSMRk.MenuItem1Click(Sender: TObject);
begin
  SelOKNo(CDS_RKJL, True);
end;

procedure TfrmCPSMRk.MenuItem2Click(Sender: TObject);
begin
  SelOKNo(CDS_RKJL, False);
end;

procedure TfrmCPSMRk.Button6Click(Sender: TObject);
var
  maxno: string;
begin
  if GetLSNo(ADOQueryCmd, maxno, '9', 'CK_BanCP_CR', 4, 1) = False then
  begin
    Application.MessageBox('取入库单号失败!', '提示', 0);
    Exit;
  end;
  RKOrdID.Text := Trim(maxno);
end;

procedure TfrmCPSMRk.edtScanKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    if not CheckData() then
      exit;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' exec P_Do_MLJYRK ');
      sql.Add(' @MJID=' + QuotedStr(Trim(edtScan.Text)));
      sql.Add(' ,@Rkordno=' + QuotedStr(Trim(RKOrdID.Text)));
      sql.Add(' ,@Name=' + QuotedStr(DName));
      ExecSQL;
    end;
    edtScan.Text := '';
    InitGrid();
  end;
end;

end.

