unit U_PBSDCK;

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, ShellAPI, cxTextEdit,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, cxContainer, cxMaskEdit,
  cxButtonEdit, dxSkinsCore, dxSkinsDefaultPainters, dxSkinscxPCPainter;

type
  TfrmPBSDCK = class(TForm)
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    DS_MainSel: TDataSource;
    CDS_MainSel: TClientDataSet;
    cxGridPopupMenu4: TcxGridPopupMenu;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    v3Column3: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Panel2: TPanel;
    Label5: TLabel;
    Label4: TLabel;
    CKOrdNo: TEdit;
    BaoID: TEdit;
    CRTime: TDateTimePicker;
    Button2: TButton;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    Label12: TLabel;
    CDS_Main: TClientDataSet;
    v3Column2: TcxGridDBColumn;
    Button3: TButton;
    CKType: TComboBox;
    Label1: TLabel;
    edtIsSMTH: TEdit;
    Label2: TLabel;
    Button4: TButton;
    CDS_DCK: TClientDataSet;
    MovePanel2: TMovePanel;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure BaoIDKeyPress(Sender: TObject; var Key: Char);
    procedure Button2Click(Sender: TObject);

    procedure Button3Click(Sender: TObject);
    procedure CKTypeChange(Sender: TObject);
    procedure edtIsSMTHClick(Sender: TObject);
    procedure Button4Click(Sender: TObject);

  private
    { Private declarations }
    FSMID, FCKOrdNo, FRKOrdID, FMJID, CZZT: string;
    function JudgeSQD(): Boolean;
    function JudgeRKD(): Boolean;
    function JudgeJTM(): Boolean;
    function JudgeRKDCX(): Boolean;
    function JudgeJTMCX(): Boolean;
    procedure SaveCKData();
    procedure SaveCKDataJTM();
    procedure SaveCKDataCX();
    procedure SaveCKDataJTMCX();
    procedure InitCKGrid();
  public
    { Public declarations }
  end;

var
  frmPBSDCK: TfrmPBSDCK;

implementation

uses
  U_DataLink, U_Fun, MMSystem, U_ZdyAttachment, U_ZdyAttachGYS,
  U_CKProductBCPKCList, U_ZDYHelp, U_PBKCListSel;
{$R *.dfm}

var
  frmCKProductBCPKCListPBSel: TfrmCKProductBCPKCListPB;

function TfrmPBSDCK.JudgeSQD(): Boolean;
begin
  Result := false;
  try

    if Trim(CKOrdNo.Text) = '' then
      raise Exception.Create('未扫描申请单暂停发货!');

    Result := True;
  except
    Result := false;
    MovePanel2.Visible := False;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
//      PlaySound('未扫描申请单暂停发货.wav', 0, SND_FILENAME or SND_ASYNC);
  end;
end;

function TfrmPBSDCK.JudgeRKD(): Boolean;
begin
  try

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select   * from WFB_MJJY A   ');
      sql.Add('where  RKOrdID=' + quotedstr(trim(FRKOrdID)));
      Open;
    end;

    if ADOQueryTemp.IsEmpty then
      raise Exception.Create('条码错误暂停发货!');

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('SELECT RKOrdID,CKOrdNo FROM CK_BanCP_CR where CRFlag=''出库'' and RKOrdID=' + quotedstr(trim(FRKOrdID)));
      Open;
    end;
    if not ADOQueryTemp.IsEmpty then
      raise Exception.Create('已扫描!');

    Result := True;
  except
    Result := false;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

function TfrmPBSDCK.JudgeJTM(): Boolean;
//未检验的布票直接生成出入库数据
begin
  try

//    with ADOQueryTemp do
//    begin
//      Close;
//      sql.Clear;
//      sql.Add('select apid from JYCon_Sub_AnPai  where apid=' + quotedstr(trim(FMJID)));
//      Open;
//    end;
//
//    if ADOQueryTemp.IsEmpty then
//    begin
//      raise Exception.Create('条码错误暂停发货');
//    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('SELECT * FROM CK_PBCP_CR where CRFlag=''入库'' and MJID=' + quotedstr(trim(FMJID)));
      Open;
    end;
    if ADOQueryTemp.IsEmpty = true then
    begin

      raise Exception.Create('该条码未入库');

    end;

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('SELECT CKOrdNo FROM CK_PBCP_CR where CRFlag=''出库'' and MJID=' + quotedstr(trim(FMJID)));
      Open;
    end;
    if ADOQueryTemp.IsEmpty = False then
    begin
      if Trim(ADOQueryTemp.fieldbyname('CKOrdNo').AsString) = Trim(CKOrdNo.Text) then
      begin
        raise Exception.Create('已扫描');
      end
      else
      begin
        raise Exception.Create('已出库暂停发货');
      end;
    end;

    Result := True;
  except
    Result := false;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
//    PlaySound('wav\'+Exception(ExceptObject).Message+'.wav', 0, SND_FILENAME or SND_ASYNC);
  end;
end;

function TfrmPBSDCK.JudgeJTMCX(): Boolean;
begin
  try
    if Trim(CKOrdNo.Text) = '' then
      raise Exception.Create('出库单号不能为空!');

//    if CDS_MainSel.IsEmpty then
//      raise Exception.Create('无可撤销的数据!');

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('SELECT CKOrdNo FROM CK_PBCP_CR where CRFlag=''出库'' and MJID=' + quotedstr(FMJID));
      Open;
    end;

    if ADOQueryTemp.IsEmpty then
      raise Exception.Create('无可撤销的数据!');

    Result := True;
  except
    Result := false;
    MovePanel2.Visible := False;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

function TfrmPBSDCK.JudgeRKDCX(): Boolean;
begin
  try
    if Trim(CKOrdNo.Text) = '' then
      raise Exception.Create('出库单号不能为空!');

    if CDS_MainSel.IsEmpty then
      raise Exception.Create('无可撤销的数据!');

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('SELECT RKOrdID,CKOrdNo FROM CK_BanCP_CR where CRFlag=''出库'' and CKOrdNo=' + quotedstr(FCKOrdNo));
      Open;
    end;

    if ADOQueryTemp.IsEmpty then
      raise Exception.Create('无可撤销的数据!');

    Result := True;
  except
    Result := false;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

procedure TfrmPBSDCK.SaveCKData();
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPDATE CK_BanCP_CKList Set CKDate=getdate() ');

      sql.Add(',CKType= ' + Quotedstr(CKType.Text));
      sql.Add('where CKOrdNo=' + Quotedstr(FCKOrdNo));
      ExecSQL;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPDATE CK_BanCP_CR Set  ');
      sql.add(' CRTime=getdate(),CRType=''扫描出库'',CRFlag=''出库''  ');
      sql.add(',CKOrdNo=' + Quotedstr(FCKOrdNo));
      sql.add(',RKOrdID=' + Quotedstr(FRKOrdID));
      sql.Add(' where exists(select MJID from CK_BanCP_CR X where CK_BanCP_CR.MJID=X.MJID and X.CRFlag=''入库'' and X.RKOrdID=' + Quotedstr(FRKOrdID) + ')  ');
      sql.Add('and CK_BanCP_CR.CRFlag=''待出库''  ');
      ExecSQL;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPDATE CK_BanCP_KC Set  ');
      sql.add(' KCValid=''N''  ');
      sql.Add(' where exists(select MJID from CK_BanCP_CR X where CK_BanCP_KC.MJID=X.MJID and X.CRFlag=''入库'' and X.RKOrdID=' + Quotedstr(FRKOrdID) + ')  ');
      ExecSQL;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPDATE WFB_MJJY Set  MJStr2=''已出库''  ');
      sql.Add(' where exists (select MJID from CK_BanCP_CR X where WFB_MJJY.MJID=X.MJID and X.CRFlag=''入库'' and X.RKOrdID=' + Quotedstr(FRKOrdID) + ')  ');
      SQL.Add(' and WFB_MJJY.MJStr2=''已入库'' ');
      ExecSQL;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    PlaySound('wav\正确.wav', 0, SND_FILENAME or SND_ASYNC);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('出库失败！', '提示信息', 0);
  end;
end;

procedure TfrmPBSDCK.SaveCKDataJTM();
begin
  try
    ADOQueryCmd.Connection.BeginTrans;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from CK_PBCP_CR where mjid=' + Quotedstr(Trim(FMJID)));
      Open;
    end;
    if ADOQueryCmd.IsEmpty = True then
    begin

    end
    else
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('UPDATE CK_PBCP_CR Set  ');
        sql.add(' CRTime=getdate()  ');
        sql.add(',CRType=' + quotedstr(Trim(CKType.Text)));

        sql.add(',CRFlag=''出库''  ');
        sql.add(',CKOrdNo=' + Quotedstr(FCKOrdNo));
        sql.Add(' where CK_PBCP_CR.MJID=' + Quotedstr(Trim(FMJID)));
        sql.Add('and CK_PBCP_CR.CRFlag=''待出库''  ');

        sql.Add('UPDATE CK_PBCP_KC Set  ');
        sql.add(' KCValid=''N'',KCKgQty=0,KCQty=0  ');
        sql.Add(' where CK_PBCP_KC.MJID=' + Quotedstr(Trim(FMJID)));

        sql.Add(' update WFB_MJJY_PB set MJStr4=''已出库'' where MJID=' + Quotedstr(Trim(FMJID)));

        ExecSQL;
//        ShowMessage(sql.Text);
      end;

    end;
    PlaySound('wav\正确.wav', 0, SND_FILENAME or SND_ASYNC);
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    MovePanel2.Visible := False;
    MovePanel2.Visible := False;
    application.MessageBox('出库失败！', '提示信息', 0);
  end;
end;

procedure TfrmPBSDCK.SaveCKDataCX();
begin
  try
    ADOQueryCmd.Connection.BeginTrans;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update CK_BanCP_CR Set CRFlag=''待出库'',CRType='''',CRTime=NULL,CRNote='''',CKOrdNo='''' ');
      sql.Add(' where exists(select MJID from CK_BanCP_CR X where CK_BanCP_CR.MJID=X.MJID and X.CRFlag=''入库'' and X.RKOrdID=' + Quotedstr(FRKOrdID) + ')  ');
      sql.Add(' and CRFlag=''出库''');

      sql.Add(' UPdate CK_BanCP_KC Set KCValid=''Y'' ');
      sql.Add(' where exists(select MJID from CK_BanCP_CR X where CK_BanCP_KC.MJID=X.MJID and X.CRFlag=''入库'' and X.RKOrdID=' + Quotedstr(FRKOrdID) + ')  ');
      sql.Add(' and KCValid=''N'' ');

      sql.Add(' Update WFB_MJJY Set MJStr2=''已入库'' ');
      sql.Add(' where exists (select MJID from CK_BanCP_CR X where WFB_MJJY.MJID=X.MJID and X.CRFlag=''入库'' and X.RKOrdID=' + Quotedstr(FRKOrdID) + ')  ');
      SQL.Add(' and WFB_MJJY.MJStr2=''已出库'' ');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
      sql.Add(' ' + quotedstr(trim(DName)));
      sql.Add(',getdate() ');
      sql.Add(',' + quotedstr(''));
      sql.Add(',' + quotedstr(trim('出库扫描撤销')));
      sql.Add(',' + quotedstr('入库单号：' + trim(FRKOrdID) + ' 出库单号：' + trim(FCKOrdNo)));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    PlaySound('wav\正确.wav', 0, SND_FILENAME or SND_ASYNC);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('撤销出库失败！', '提示信息', 0);
  end;
end;

procedure TfrmPBSDCK.SaveCKDataJTMCX();
begin
  try
    ADOQueryCmd.Connection.BeginTrans;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;

      sql.Add(' UPdate CK_PBCP_KC Set KCValid=''Y'' ');
      sql.Add(' where  CK_PBCP_KC.MJID=' + Quotedstr(Trim(FMJID)));
      sql.Add(' and KCValid=''N'' ');

      sql.Add('UPDATE CK_PBCP_CR Set  ');
      sql.add(' CRTime=NULL  ');
      sql.add(',CRType=NULL');
      sql.add(',KHName=NULL');
      sql.add(',KHTT=NULL');
      sql.add(',RC=NULL');
      sql.add(',FHTT=NULL');
      sql.add(',CRFlag=''待出库''  ');
      sql.add(',CKOrdNo=NULL');
      sql.Add(' where CK_PBCP_CR.MJID=' + Quotedstr(Trim(FMJID)));
      sql.Add('and CK_PBCP_CR.CRFlag=''出库''  ');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
      sql.Add(' ' + quotedstr(trim(DName)));
      sql.Add(',getdate() ');
      sql.Add(',' + quotedstr(''));
      sql.Add(',' + quotedstr(trim('出库扫描撤销')));
      sql.Add(',' + quotedstr('卷条码：' + trim(FMJID) + ' 出库单号：' + trim(FCKOrdNo)));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;

  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('撤销出库失败！', '提示信息', 0);
  end;
end;

procedure TfrmPBSDCK.InitCKGrid();
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select AA.*, C.C_CodeName,C.C_Code,C.C_Color ');
    sql.Add('from  (select A.MainId,A.SubId,count(SubId) ps ');
    sql.Add('from CK_PBCP_CR A where A.CRFlag=''出库'' and CKOrdNo=' + quotedstr(FCKOrdNo));
    sql.Add('group by  A.MainId,A.SubId  ) AA  inner join Cloth_Main B on AA.MainId=B.MainId');
    sql.Add('  inner join Cloth_Sub C on AA.SubId=C.SubId');
//    ShowMessage(SQL.Text);
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_MainSel);
  SInitCDSData20(ADOQueryTemp, CDS_MainSel);

end;

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

procedure TfrmPBSDCK.FormDestroy(Sender: TObject);
begin
  frmPBSDCK := nil;
end;

procedure TfrmPBSDCK.FormShow(Sender: TObject);
begin
  BaoID.SetFocus;
  ReadCxGrid('扫描出库', Tv3, '坯布仓库');
  CRTime.DateTime := SGetServerDate(ADOQueryTemp);
end;

procedure TfrmPBSDCK.BaoIDKeyPress(Sender: TObject; var Key: Char);
begin

  if Key = #13 then
  begin

    MovePanel2.Visible := True;
    MovePanel2.Refresh;
    FSMID := TRIM(BaoID.Text);
    BaoID.Text := '';
    FRKOrdID := '';
    FMJID := '';
    FCKOrdNo := Trim(CKOrdNo.Text);
    if edtIsSMTH.Text = '' then
    begin
      if JudgeSQD() = False then
        exit;

      FMJID := FSMID;

      if JudgeJTM() then  //检测卷条码
        SaveCKDataJTM();
    end
    else
    begin
      FMJID := FSMID;
      if JudgeJTMCX() then  //检测卷条码
        SaveCKDataJTMCX();
    end;
    MovePanel2.Visible := False;

//    if CZZT <> '选择库存' then
    InitCKGrid();
  end;

end;

procedure TfrmPBSDCK.Button2Click(Sender: TObject);
begin
  Close;
  WriteCxGrid('扫描出库', Tv3, '坯布仓库');
end;

procedure TfrmPBSDCK.Button3Click(Sender: TObject);
begin
  if GetLSNo(ADOQueryTemp, FCKOrdNo, 'BF', 'CK_PBCP_CR', 4, 1) = False then
  begin
    Application.MessageBox('生成流水号异常！', '提示', 0);
    exit;
  end;
  CKOrdNo.TEXT := FCKOrdNo;
  BaoID.SetFocus;
//  with ADOQueryCmd do
//  begin
//    Close;
//    sql.Clear;
//    sql.Add('insert into  CK_BanCP_CKList(CKOrdNo) ');
//    sql.add('values(' + quotedstr(FCKOrdNo) + ')  ');
//    ExecSQL;
//  end;
end;

procedure TfrmPBSDCK.CKTypeChange(Sender: TObject);
begin
//  KHName.Text := '';
  BaoID.SetFocus;
end;

procedure TfrmPBSDCK.edtIsSMTHClick(Sender: TObject);
begin
  BaoID.SetFocus;
  if edtIsSMTH.Text = '√' then
  begin
    edtIsSMTH.Text := '';
  end
  else
  begin
    if Application.MessageBox('确定要扫描退货吗？', '提示', 32 + 4) = IDYES then
      edtIsSMTH.Text := '√';
  end;
end;

procedure TfrmPBSDCK.Button4Click(Sender: TObject);
var
  MKey: Char;
begin
  MKey := #13;
  CZZT := '选择库存';
  if CKOrdNo.Text = '' then
  begin
    Application.MessageBox(' 请先取号！', '提示', 0);
    exit;
  end;
  try
    frmPBKCListSel := TfrmPBKCListSel.Create(Application);
    with frmPBKCListSel do
    begin

      if ShowModal = 1 then
      begin
        with frmPBKCListSel.CDS_Main do
        begin
          First;
          while not Eof do
          begin
            if Fieldbyname('ssel').AsBoolean then
            begin
              BaoID.Text := Trim(Fieldbyname('MJID').AsString);
              BaoIDKeyPress(BaoID, MKey);
              Sleep(200);
            end;
            Next;
          end;
        end;
      end;
    end;
  finally
    frmPBKCListSel.Free;

  end;
  InitCKGrid();
  BaoID.SetFocus;
end;

end.

