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, cxCalendar, MMSystem;

type
  TfrmCPSMRk = class(TForm)
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    Panel1: TPanel;
    Button2: TButton;
    DS_RKJL: TDataSource;
    CDS_RKJL: TClientDataSet;
    PM_RKJL: TPopupMenu;
    MenuItem1: TMenuItem;
    MenuItem2: TMenuItem;
    Button6: TButton;
    RKOrdID: TEdit;
    Label5: TLabel;
    RM1: TRMGridReport;
    ADOQueryPrint: TADOQuery;
    RMDB_Main: TRMDBDataSet;
    Label6: TLabel;
    edtScan: TEdit;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column4: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column21: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    v1Column22: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Button1: TButton;
    ComboBox1: TComboBox;
    CDS_Label: TClientDataSet;
    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);
    procedure RKOrdIDKeyPress(Sender: TObject; var Key: Char);
    procedure Button1Click(Sender: TObject);
  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_BanCP_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);
    PlaySound(PChar('wav\' + Exception(ExceptObject).Message + '.wav'), 0, SND_FILENAME or SND_ASYNC);
  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

    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      sql.Add(' select A.*  ');
      sql.Add(' ,B.orderNo,B.MPRTCodeName,B.MPRTMF,B.MPRTKZ,B.conNo,B.KHconNo ');
      sql.Add(' ,C.PRTColor,C.PRTEColor');
      sql.Add(' from CK_BanCP_CR A  inner join JYOrder_Main B on A.MainId=B.MainId inner join JYOrder_Sub C on A.SubId=C.SubId ');
      sql.Add(' where  A.CRFlag=''入库'' and A.RKOrdID=' + quotedstr(RKOrdID.Text));

      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_RKJL);

    SInitCDSData20(ADOQueryMain, CDS_RKJL);
  finally

  end;
end;

procedure TfrmCPSMRk.FormShow(Sender: TObject);
var
  fsj: string;
begin
  fsj := 'select distinct(LMName) name from Lbael_Map where LMType=''入库单码单''  ';
  SInitComBoxBySql(ADOQueryCmd, ComboBox1, true, fsj);

  with ADOQueryTemp do
  begin
    Filtered := False;
    Close;
    sql.Clear;
    Sql.Add('select * from Lbael_Map where LMType=''入库单码单''');

    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_Label);
  SInitCDSData20(ADOQueryTemp, CDS_Label);
  ReadCxGrid('成品扫描入库', Tv1, '成品仓库');

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, 'RK', '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
    begin
      edtScan.Text := '';
      exit;
    end;

    try
      ADOQueryCmd.Connection.BeginTrans;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('UPDATE CK_BanCP_CR Set  ');
        sql.add('RKOrdID=' + Quotedstr(Trim(RKOrdID.Text)));
        sql.Add('where CK_BanCP_CR.MJID=' + Quotedstr(Trim(edtScan.Text)));
        sql.Add('and CK_BanCP_CR.CRFlag in (''待入库'',''待出库'')  ');

        sql.Add('UPDATE CK_BanCP_CR Set  ');
        sql.add(' CRTime=getdate(),CRType=''检验入库'',CRFlag=''入库''  ');
        sql.Add(' where CK_BanCP_CR.MJID=' + Quotedstr(Trim(edtScan.Text)));
        sql.Add('and CK_BanCP_CR.CRFlag=''待入库''  ');

        sql.Add('UPDATE WFB_MJJY Set  ');
        sql.add('RKOrdID=' + Quotedstr(Trim(RKOrdID.Text)));
        sql.add(',MJStr2=''已入库'' ');
        sql.Add('where WFB_MJJY.MJID=' + Quotedstr(Trim(edtScan.Text)));
        SQL.Add('and WFB_MJJY.MJStr2=''未入库'' ');
        ExecSQL;
      end;

      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select  count(distinct MainId) DDSL from CK_BanCP_CR A   ');
        sql.Add(' where A.RKOrdID= ' + quotedstr(trim(RKOrdID.Text)));
        Open;
      end;
      if ADOQueryTemp.FieldByName('DDSL').AsInteger > 1 then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('同一入库单不能入库不能多个订单!', '提示', 0);
        Exit;
      end;

      ADOQueryCmd.Connection.CommitTrans;

    except
      ADOQueryCmd.Connection.RollbackTrans;

      Application.MessageBox('撤销失败!', '提示', 0);
    end;
    edtScan.Text := '';
    InitGrid();
  end;
end;

procedure TfrmCPSMRk.RKOrdIDKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
    InitGrid();
end;

procedure TfrmCPSMRk.Button1Click(Sender: TObject);
var
  fPrintFile10, FMainID, FKHName, FFPNo: string;
  fPrintFile: string;
  Txt, fImagePath: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin
  if Trim(RKOrdID.Text) = '' then
    Exit;
  ExportFtErpFile(trim(ComboBox1.Text) + '.rmf', ADOQueryTemp);

  if CDS_Label.Locate('LMName', trim(ComboBox1.Text), []) then
  begin
    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.add('exec ' + trim(CDS_Label.fieldbyname('LMSql').AsString) + ' @RKOrdNo=' + quotedstr(Trim(RKOrdID.Text)));
      Open;
    end;
  end;
  try
    Moudle := LoadLibrary('MakeQRBarcode.dll');
    @Makebar := GetProcAddress(Moudle, 'Make');
    @Mixtext := GetProcAddress(Moudle, 'MixText');
    Txt := Trim(RKOrdID.Text);
    fImagePath := ExtractFilePath(Application.ExeName) + 'image\temp.bmp';
    if not DirectoryExists(pchar(ExtractFilePath(Application.ExeName) + 'image')) then
      CreateDirectory(pchar(ExtractFilePath(Application.ExeName) + 'image'), nil);
    if FileExists(fImagePath) then
      DeleteFile(fImagePath);
    Makebar(pchar(Txt), Length(Txt), 3, 3, 0, PChar(fImagePath), 3);
  except
    application.MessageBox('条形码生成失败！', '提示信息', MB_ICONERROR);
    exit;
  end;
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(ComboBox1.Text) + '.rmf';
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RMVariables['QRBARCODE'] := fImagePath;
    RMVariables['ZDR'] := trim(DName);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找到' + fPrintFile), '提示', 0);
  end;
end;

end.

