unit U_PBSMCK;

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, RM_Dataset, RM_System, RM_Common, RM_Class, RM_GridReport,
  RM_e_Xls, dxSkinsCore, dxSkinsDefaultPainters, dxSkinscxPCPainter,
  dxSkinBlack, dxSkinBlue, dxSkinBlueprint, dxSkinCaramel, dxSkinCoffee,
  dxSkinDarkRoom, dxSkinDarkSide, dxSkinDevExpressDarkStyle,
  dxSkinDevExpressStyle, dxSkinFoggy, dxSkinGlassOceans,
  dxSkinHighContrast, dxSkiniMaginary, dxSkinLilian, dxSkinLiquidSky,
  dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMetropolis,
  dxSkinMetropolisDark, dxSkinMoneyTwins, dxSkinOffice2007Black,
  dxSkinOffice2007Blue, dxSkinOffice2007Green, dxSkinOffice2007Pink,
  dxSkinOffice2007Silver, dxSkinOffice2010Black, dxSkinOffice2010Blue,
  dxSkinOffice2010Silver, dxSkinOffice2013DarkGray,
  dxSkinOffice2013LightGray, dxSkinOffice2013White, dxSkinPumpkin,
  dxSkinSeven, dxSkinSevenClassic, dxSkinSharp, dxSkinSharpPlus,
  dxSkinSilver, dxSkinSpringTime, dxSkinStardust, dxSkinSummer2008,
  dxSkinTheAsphaltWorld, dxSkinValentine, dxSkinVS2010, dxSkinWhiteprint,
  dxSkinXmas2008Blue;

type
  TfrmPBSMCK = class(TForm)
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    DS_MainSel: TDataSource;
    CDS_MainSel: TClientDataSet;
    cxGridPopupMenu4: TcxGridPopupMenu;
    Panel2: TPanel;
    Label5: TLabel;
    Label4: TLabel;
    Label9: TLabel;
    CKOrdNo: TEdit;
    BaoID: TEdit;
    CRTime: TDateTimePicker;
    Button2: TButton;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    Label12: TLabel;
    CDS_Main: TClientDataSet;
    Button1: TButton;
    Button3: TButton;
    CKType: TComboBox;
    edtIsSMTH: TEdit;
    Label2: TLabel;
    Button4: TButton;
    CDS_DCK: TClientDataSet;
    MovePanel2: TMovePanel;
    KHNAME: TcxButtonEdit;
    Label8: TLabel;
    Label3: TLabel;
    Label7: TLabel;
    batchno: TComboBox;
    CARNO: TComboBox;
    Label14: TLabel;
    CPPRICE: TEdit;
    KHPM: TBtnEditA;
    Label15: TLabel;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    Tv3Column1: TcxGridDBColumn;
    cxGridDBColumn1: TcxGridDBColumn;
    v3Column4: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    v3Column1: TcxGridDBColumn;
    v3Column2: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Label33: TLabel;
    Edit6: TEdit;
    Label13: TLabel;
    Edit3: TEdit;
    Button6: TButton;
    ComboBox1: TComboBox;
    RM1: TRMGridReport;
    CDS_JuanPRT: TClientDataSet;
    RMDBMX: TRMDBDataSet;
    Label1: TLabel;
    taitou: TEdit;
    Edit2: TEdit;
    Edit4: TEdit;
    RMXLSExport1: TRMXLSExport;
    cpname: TcxButtonEdit;
    Label16: TLabel;
    pici: TComboBox;
    Label6: TLabel;
    Label10: TLabel;
    Tv3Column2: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    Tv3Column3: TcxGridDBColumn;
    N2: TMenuItem;
    N3: TMenuItem;
    N4: TMenuItem;
    Panel1: TPanel;
    Label19: TLabel;
    Button5: TButton;
    Button7: TButton;
    Edit1: TEdit;
    Tv3Column4: TcxGridDBColumn;
    Label11: TLabel;
    Edit5: TEdit;
    Label17: TLabel;
    Edit7: TEdit;
    Label18: TLabel;
    Edit8: TEdit;
    Tv3Column5: TcxGridDBColumn;
    Label20: TLabel;
    fsid: TEdit;
    SSID: TEdit;
    Button8: TButton;
    Panel3: TPanel;
    Button9: TButton;
    Button10: TButton;
    Button11: TButton;
    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 Button1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure CKTypeChange(Sender: TObject);
    procedure edtIsSMTHClick(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure KHNAMEPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Button5Click(Sender: TObject);
    procedure KHPMBtnClick(Sender: TObject);
    procedure Edit6Click(Sender: TObject);
    procedure Edit3KeyPress(Sender: TObject; var Key: Char);
    procedure Button6Click(Sender: TObject);
    procedure Edit2Change(Sender: TObject);
    procedure Edit2KeyPress(Sender: TObject; var Key: Char);
    procedure cxGridDBColumn1PropertiesEditValueChanged(Sender: TObject);
    procedure v3Column1PropertiesEditValueChanged(Sender: TObject);
    procedure cpnamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure N1Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure N4Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure Edit5Click(Sender: TObject);
    procedure Edit7Click(Sender: TObject);
    procedure Edit8Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure Button9Click(Sender: TObject);
    procedure Button10Click(Sender: TObject);
    procedure Button11Click(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();
    procedure INITTJ();
  public
    { Public declarations }
    canshu1: string;
  end;

var
  frmPBSMCK: TfrmPBSMCK;

implementation

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

var
  frmCKProductBCPKCListPBSel: TfrmCKProductBCPKCListPB;

procedure TfrmPBSMCK.INITTJ();
var
  FSJ, FSJ2, fsj3: string;
begin
//  if CKOrdNo.Text = '' then
//  begin
//    Exit;
//  end;
//  FSJ := 'select name=P_NAME  from FHSQ_Sub where FSID=' + QuotedStr(Trim(CKOrdNo.Text));
//  SInitComBoxBySql(ADOQueryCmd, CPNAME, False, FSJ);

  if cpname.Text <> '' then
  begin
    FSJ2 := 'select DISTINCT  name=CARNO  from WFB_MJJY_PB where  mjstr4=''未出库'' and NAME = ' + QuotedStr(Trim(CPNAME.Text));
  end
  else
  begin
//    FSJ2 := 'select DISTINCT  name=CARNO  from WFB_MJJY_PB where  NAME IN (SELECT P_NAME FROM FHSQ_Sub WHERE FSID= ' + QuotedStr(Trim(CKOrdNo.Text)) + ')';
  end;
//  ShowMessage(fsj2);
  SInitComBoxBySql(ADOQueryCmd, CARNO, False, FSJ2);

  if cpname.Text <> '' then
  begin
    fsj3 := 'select DISTINCT  name=APBatchNo  from WFB_MJJY_PB where mjstr4=''未出库'' and   NAME = ' + QuotedStr(Trim(CPNAME.Text));
  end
  else
  begin
//    fsj3 := 'select DISTINCT  name=APBatchNo  from WFB_MJJY_PB where  NAME IN (SELECT P_NAME FROM FHSQ_Sub WHERE FSID= ' + QuotedStr(Trim(CKOrdNo.Text)) + ')';
  end;
  SInitComBoxBySql(ADOQueryCmd, batchno, False, fsj3);
//  ShowMessage(FSJ2);

end;

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

    if Trim(KHName.Text) = '' then
      raise Exception.Create('请选择客户!');

    if Trim(CKOrdNo.Text) = '' then
      raise Exception.Create('请先取号!');
    if Trim(cpname.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 TfrmPBSMCK.JudgeRKD(): Boolean;
begin
  try
    if CKType.Text = '回修出库' then
    begin
      if Trim(KHName.Text) = '' then
        raise Exception.Create('请选择收货单位!');
    end;

    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 TfrmPBSMCK.JudgeJTM(): Boolean;
//未检验的布票直接生成出入库数据
begin
  try
    if CKType.Text <> '报废出库' then
    begin
      if Trim(KHName.Text) = '' then
        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('该条码未入库');
      PlaySound('wav/未入库暂停发货.wav', 0, SND_FILENAME or SND_ASYNC);
      Result := false;
      Exit;
    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
        PlaySound('wav/已扫描.wav', 0, SND_FILENAME or SND_ASYNC);
        Result := false;
        Exit;
      end
      else
      begin
//        raise Exception.Create('已出库暂停发货');
        PlaySound('wav/已出库暂停发货.wav', 0, SND_FILENAME or SND_ASYNC);
        Result := false;
        Exit;
      end;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('SELECT * FROM WFB_MJJY_PB where   MJID=' + quotedstr(trim(FMJID)));
      Open;
    end;
    if trim(Edit6.Text) = '' then
    begin
      if Trim(CPNAME.Text) <> '' then
      begin
        if Trim(ADOQueryTemp.fieldbyname('NAME').AsString) <> Trim(CPNAME.Text) then
        begin
          PlaySound('wav\产品不同暂停发货.wav', 0, SND_FILENAME or SND_ASYNC);
          Result := false;
          Exit;
        end;
      end;

      if Trim(CarNo.Text) <> '' then
      begin
        if Trim(ADOQueryTemp.fieldbyname('carno').AsString) <> Trim(carno.Text) then
        begin

          PlaySound('wav\机台不同暂停发货.wav', 0, SND_FILENAME or SND_ASYNC);
          Result := false;
          Exit;
        end;
      end;
      if Trim(batchno.Text) <> '' then
      begin
        if Trim(ADOQueryTemp.fieldbyname('apbatchno').AsString) <> Trim(batchno.Text) then
        begin

          PlaySound('wav\批号不同暂停发货.wav', 0, SND_FILENAME or SND_ASYNC);
          Result := false;
          Exit;
        end;
      end;
    end;
    if Trim(Edit2.Text) <> '' then
    begin
      if StrToInt(Edit4.Text) <= 0 then
      begin

        PlaySound('wav\超出发货上限暂停发货.wav', 0, SND_FILENAME or SND_ASYNC);
        Result := false;
        Exit;
      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 TfrmPBSMCK.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 TfrmPBSMCK.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 TfrmPBSMCK.SaveCKData();
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPDATE CK_BanCP_CKList Set CKDate=getdate() ');
      sql.Add(',KHNmae= ' + Quotedstr(KHName.Text));
      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 TfrmPBSMCK.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(',KHName=' + quotedstr(Trim(KHName.Text)));

        sql.add(',filler=' + quotedstr(Trim(DName)));
        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)));

        sql.Add('UPDATE WFB_MJJY_PB SET NEWNAME=' + QuotedStr(Trim(CPNAME.Text)));
        if batchno.Text <> '' then
        begin
          SQL.Add(',NEWBATCHNO=' + QuotedStr(Trim(batchno.Text)));
        end
        else
        begin
          SQL.Add(',NEWBATCHNO=APBATCHNO');
        end;
        if CARNO.Text <> '' then
        begin
          SQL.Add(',NEWCARNO=' + QuotedStr(Trim(CARNO.Text)));
        end
        else
        begin
          SQL.Add(',NEWCARNO=CARNO');
        end;

        SQL.Add(',ckno=' + QuotedStr(Trim(CKOrdNo.Text)));
        if CPPRICE.Text <> '' then
        begin
          SQL.Add(',CPPRICE=' + (Trim(CPPRICE.Text)));
        end;
        if KHPM.Text <> '' then
        begin
          SQL.Add(',KHPM=' + QuotedStr(Trim(KHPM.Text)));
        end;
        if fsid.Text <> '' then
        begin
          SQL.Add(',FHMAINID=' + QuotedStr(Trim(fsid.Text)));
        end;
        if SSID.Text <> '' then
        begin
          SQL.Add(',FHSUBID=' + QuotedStr(Trim(SSID.Text)));
        end;

        if Trim(taitou.Text) <> '' then
        begin
          SQL.Add(',KHTAITOU=' + QuotedStr(Trim(taitou.Text)));
        end;
        if Trim(pici.Text) <> '' then
        begin
          SQL.Add(',pici=' + QuotedStr(Trim(pici.Text)));
        end;
//        if Trim(EDIT5.Text) <> '' then
//        begin
//          SQL.Add(',FHKUWEI=''库位0'',fhkongjia=0  ');
//        end;
//        if Trim(EDIT7.Text) <> '' then
//        begin
//          SQL.Add(',FHKUWEI=''库位1'',fhkongjia=0.1  ');
//        end;
//        if Trim(EDIT8.Text) <> '' then
//        begin
//          SQL.Add(',FHKUWEI=''库位2'',fhkongjia=0.2  ');
//        end;

        SQL.Add('WHERE MJID=' + quotedstr(trim(FMJID)));

        ExecSQL;
      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 TfrmPBSMCK.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 TfrmPBSMCK.SaveCKDataJTMCX();
begin
  try
    ADOQueryCmd.Connection.BeginTrans;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_Do_PBCK @CRFlag=''未出库'', @CKOrdNo='''', @MJID=' + quotedstr(trim(FMJID)));

      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=''出库''  ');

      sql.Add('UPDATE WFB_MJJY_PB SET NEWNAME=NULL');
      SQL.Add(',NEWBATCHNO=NULL');
      SQL.Add(',NEWCARNO=NULL');
      SQL.Add(',FHKONGJIA=NULL');
      SQL.Add(',FHMAINID=NULL');
      SQL.Add(',FHSUBID=NULL');
      SQL.Add(',FHkuwei=NULL');
      SQL.Add(',pici=NULL');
      SQL.Add(',KHTAITOU=NULL');
      SQL.Add(',ckno=NULL');
      SQL.Add(',CPPRICE=NULL');
      SQL.Add(',KHPM=NULL');
      SQL.Add('WHERE MJID=' + quotedstr(trim(FMJID)));
      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 TfrmPBSMCK.InitCKGrid();
begin
//  with ADOQueryTemp do
//  begin
//    Close;
//    sql.Clear;
//    SQL.Add('SELECT NAME,SUM(KGQTY)KGQTY,NEWCARNO,NEWBATCHNO,count(*)ps    from CK_PBCP_CR a  inner join WFB_MJJY_PB B on A.MJID=B.MJID');
//    SQL.Add('WHERE CKORDNO=' + QuotedStr(Trim(CKOrdNo.Text)));
//    SQL.Add('GROUP BY NAME,NEWCARNO,NEWBATCHNO');
////    ShowMessage(SQL.Text);
//    Open;
//  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * FROM   CK_PBCP_CR a inner join WFB_MJJY_PB B on A.MJID=B.MJID  ');
    SQL.Add('WHERE CKOrdNO=' + QuotedStr(Trim(CKOrdNo.Text)));
    SQL.Add('and ISNULL(NEWNAME,'''')=' + QuotedStr(Trim(CPNAME.Text)));
    sql.Add('order by a.crtime desc');

    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_MainSel);
  SInitCDSData20(ADOQueryTemp, CDS_MainSel);

end;

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

procedure TfrmPBSMCK.FormDestroy(Sender: TObject);
begin
  frmPBSMCK := nil;
end;

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

procedure TfrmPBSMCK.BaoIDKeyPress(Sender: TObject; var Key: Char);
var
  MKey: Char;
begin
  MKey := #13;

  if Key = #13 then
  begin
    if (Edit5.Text = '') and (Edit7.Text = '') and (Edit8.Text = '') then
    begin

      Application.MessageBox('请先选择库位！', '提示', 0);
      PlaySound('wav/请先选择库位.wav', 0, SND_FILENAME or SND_ASYNC);
      exit;

    end;

    if Edit6.Text <> '' then
    begin
      if cpname.Text = '' then
      begin
        Application.MessageBox('强制发货请先选择产品！', '提示', 0);
        exit;
      end;
    end;
    if edit4.Text <> '' then
    begin
      if edtIsSMTH.Text = '' then
      begin
        if StrToFloatDef(edit4.Text, 0) <= 0 then
        begin
          Application.MessageBox('已达发货上限，禁止新增！', '提示', 0);
          Exit;
        end;
      end;

    end;
    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;
    if fsid.Text <> '' then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('exec P_Do_FHSQ_SSID ''' + Trim(fsid.Text) + '''');
        ExecSQL;
      end;
    end;

    MovePanel2.Visible := False;

    if Edit2.Text <> '' then
    begin
      Edit2KeyPress(EDIT2, MKey);
    end;
//    if CZZT <> '选择库存' then
    InitCKGrid();
  end;

end;

procedure TfrmPBSMCK.Button2Click(Sender: TObject);
begin

  WriteCxGrid('扫描出库', Tv3, '坯布仓库');
  Close;
end;

procedure TfrmPBSMCK.Button1Click(Sender: TObject);
begin
  if CKType.Text = '销售出库' then
  begin
    try
      frmZdyAttachment := TfrmZdyAttachment.Create(Application);
      with frmZdyAttachment do
      begin

        if ShowModal = 1 then
        begin
          KHName.Text := Trim(CDS_HZ.fieldbyname('CoName').AsString);

        end;
      end;
    finally
      frmZdyAttachment.Free;
    end;
  end
  else
  begin
    try
      frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
      with frmZdyAttachGYS do
      begin

        if ShowModal = 1 then
        begin
          KHName.Text := Trim(CDS_HZ.fieldbyname('CoName').AsString);

        end;
      end;
    finally
      frmZdyAttachGYS.Free;
    end;
  end;
  BaoID.SetFocus;
end;

procedure TfrmPBSMCK.Button3Click(Sender: TObject);
begin
  if CKOrdNo.Text <> '' then
  begin
    if Application.MessageBox('确定更换出库单号？', '提示', 32 + 4) <> IDYES then
      Exit;
  end;
  if GetLSNo(ADOQueryTemp, FCKOrdNo, 'CK', '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 TfrmPBSMCK.CKTypeChange(Sender: TObject);
begin
//  KHName.Text := '';
  BaoID.SetFocus;
end;

procedure TfrmPBSMCK.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 TfrmPBSMCK.Button4Click(Sender: TObject);
var
  MKey: Char;
  SqlMJID: string;
  FGS: Integer;
begin
  MKey := #13;
  CZZT := '选择库存';
  SqlMJID := '';
  FGS := 0;
  if JudgeSQD() = False then
    exit;
  if (Edit5.Text = '') and (Edit7.Text = '') and (Edit8.Text = '') then
  begin

    Application.MessageBox('请先选择库位！', '提示', 0);
    PlaySound('wav/请先选择库位.wav', 0, SND_FILENAME or SND_ASYNC);
    exit;

  end;

  if Edit6.Text <> '' then
  begin
    if cpname.Text = '' then
    begin
      Application.MessageBox('强制发货请先选择产品！', '提示', 0);
      exit;
    end;
  end;
  if edit4.Text <> '' then
  begin
    if edtIsSMTH.Text = '' then
    begin
      if StrToFloatDef(edit4.Text, 0) <= 0 then
      begin
        Application.MessageBox('已达发货上限，禁止新增！', '提示', 0);
        Exit;
      end;
    end;

  end;
  try
    frmPBKCListSel := TfrmPBKCListSel.Create(Application);
    with frmPBKCListSel do
    begin
      C_CodeName.Text := cpname.Text;
      jhchejian.ItemIndex := jhchejian.Items.IndexOf(trim(canshu1));
      if ShowModal = 1 then
      begin
        with frmPBKCListSel.CDS_Main do
        begin
          First;
          while not Eof do
          begin
            if Fieldbyname('ssel').AsBoolean then
            begin

              if Trim(SqlMJID) = '' then
                SqlMJID := QuotedStr(Trim(fieldbyname('MJID').AsString))
              else
                SqlMJID := SqlMJID + ',' + QuotedStr(Trim(fieldbyname('MJID').AsString));

              FGS := FGS + 1;
//              BaoID.Text := Trim(Fieldbyname('MJID').AsString);
//              BaoIDKeyPress(BaoID, MKey);
//              Sleep(200);
            end;
            Next;
          end;
          if Trim(SqlMJID) = '' then
          begin
            Exit;
          end;
          SqlMJID := 'MJID IN (' + SqlMJID + ')';

          if trim(Edit6.Text) = '' then
          begin
            if Trim(CPNAME.Text) <> '' then
            begin
              with ADOQueryTemp do
              begin
                Close;
                sql.Clear;
                sql.Add('SELECT * FROM WFB_MJJY_PB where  ' + SqlMJID);
                sql.Add('and isnull(name,'''')<>' + QuotedStr(CPNAME.Text));
                Open;
              end;
              if ADOQueryTemp.IsEmpty = FALSE then
              begin
                PlaySound('wav\产品不同暂停发货.wav', 0, SND_FILENAME or SND_ASYNC);

                Exit;
              end;
            end;

            if Trim(CarNo.Text) <> '' then
            begin
              with ADOQueryTemp do
              begin
                Close;
                sql.Clear;
                sql.Add('SELECT * FROM WFB_MJJY_PB where  ' + SqlMJID);
                sql.Add('and isnull(carno,'''')<>' + QuotedStr(carno.Text));
                Open;
              end;
              if ADOQueryTemp.IsEmpty = FALSE then
              begin
                PlaySound('wav\机台不同暂停发货.wav', 0, SND_FILENAME or SND_ASYNC);

                Exit;
              end;

            end;
            if Trim(batchno.Text) <> '' then
            begin
              with ADOQueryTemp do
              begin
                Close;
                sql.Clear;
                sql.Add('SELECT * FROM WFB_MJJY_PB where  ' + SqlMJID);
                sql.Add('and isnull(apbatchno,'''')<>' + QuotedStr(batchno.Text));
                Open;
              end;
              if ADOQueryTemp.IsEmpty = False then
              begin
                PlaySound('wav\批号不同暂停发货.wav', 0, SND_FILENAME or SND_ASYNC);

                Exit;
              end;

            end;
          end;
          if Trim(Edit2.Text) <> '' then
          begin
            if (StrToInt(Edit4.Text) - FGS) < 0 then
            begin

              PlaySound('wav\超出发货上限暂停发货.wav', 0, SND_FILENAME or SND_ASYNC);

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

            sql.add(',filler=' + quotedstr(Trim(DName)));
            sql.add(',CRFlag=''出库''  ');
            sql.add(',CKOrdNo=' + Quotedstr(SELF.CKOrdNo.Text));
            sql.Add('where  ' + SqlMJID);
            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  ' + SqlMJID);

            sql.Add('UPDATE WFB_MJJY_PB SET MJStr4=''已出库'',NEWNAME=' + QuotedStr(Trim(Self.CPNAME.Text)));
            if Self.batchno.Text <> '' then
            begin
              SQL.Add(',NEWBATCHNO=' + QuotedStr(Trim(Self.batchno.Text)));
            end
            else
            begin
              SQL.Add(',NEWBATCHNO=APBATCHNO');
            end;
            if SELF.CARNO.Text <> '' then
            begin
              SQL.Add(',NEWCARNO=' + QuotedStr(Trim(SELF.CARNO.Text)));
            end
            else
            begin
              SQL.Add(',NEWCARNO=CARNO');
            end;

            SQL.Add(',ckno=' + QuotedStr(Trim(Self.CKOrdNo.Text)));
            if SELF.CPPRICE.Text <> '' then
            begin
              SQL.Add(',CPPRICE=' + (Trim(SELF.CPPRICE.Text)));
            end;
            if SELF.KHPM.Text <> '' then
            begin
              SQL.Add(',KHPM=' + QuotedStr(Trim(SELF.KHPM.Text)));
            end;
            if Self.fsid.Text <> '' then
            begin
              SQL.Add(',FHMAINID=' + QuotedStr(Trim(SELF.fsid.Text)));
            end;
            if SSID.Text <> '' then
            begin
              SQL.Add(',FHSUBID=' + QuotedStr(Trim(SSID.Text)));
            end;

            if Trim(SELF.taitou.Text) <> '' then
            begin
              SQL.Add(',KHTAITOU=' + QuotedStr(Trim(SELF.taitou.Text)));
            end;
            if Trim(SELF.pici.Text) <> '' then
            begin
              SQL.Add(',pici=' + QuotedStr(Trim(Self.pici.Text)));
            end;
//            if Trim(EDIT5.Text) <> '' then
//            begin
//              SQL.Add(',FHKUWEI=''库位0'',fhkongjia=0  ');
//            end;
//            if Trim(EDIT7.Text) <> '' then
//            begin
//              SQL.Add(',FHKUWEI=''库位1'',fhkongjia=0.1  ');
//            end;
//            if Trim(EDIT8.Text) <> '' then
//            begin
//              SQL.Add(',FHKUWEI=''库位2'',fhkongjia=0.2  ');
//            end;

            SQL.Add('WHERE ' + SqlMJID);

            ExecSQL;
          end;
          PlaySound('wav\正确.wav', 0, SND_FILENAME or SND_ASYNC);
        end;
      end;
    end;
  finally
    frmPBKCListSel.Free;

  end;
  if fsid.Text <> '' then
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_Do_FHSQ_SSID ''' + Trim(fsid.Text) + '''');
      ExecSQL;
    end;
  end;

  if Edit2.Text <> '' then
  begin
    Edit2KeyPress(EDIT2, MKey);
  end;
//    if CZZT <> '选择库存' then

  InitCKGrid();
  BaoID.SetFocus;
end;

procedure TfrmPBSMCK.KHNAMEPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZdyAttachment := TfrmZdyAttachment.Create(Application);
    with frmZdyAttachment do
    begin
      if ShowModal = 1 then
      begin
        KHNAME.Text := Trim(CDS_HZ.fieldbyname('CoName').AsString);
        taitou.Text := Trim(CDS_HZ.fieldbyname('CoName').AsString);
      end;
    end;
  finally
    frmZdyAttachment.Free;
  end;
//  if KHNAME.Text <> '' then
//  begin
//    if Application.MessageBox('是否要选择发货申请？', '提示', 32 + 4) <> IDYES then
//    begin
//
//    end
//    else
//    begin
//      try
//        frmFHSQListSel := TfrmFHSQListSel.Create(self);
//        with frmFHSQListSel do
//        begin
//          CustomerNoName.Text := Self.KHNAME.Text;
//          CustomerNoName.Enabled := FALSE;
//          if ShowModal = 1 then
//          begin
//            FSID.Text := Trim(Order_Main.fieldbyname('FSID').AsString);
//            SSID.Text := Trim(Order_Main.fieldbyname('SSID').AsString);
//            taitou.Text := Trim(Order_Main.fieldbyname('shouhuotaitou').AsString);
//            KHPM.Text := Trim(Order_Main.fieldbyname('FHKHPM').AsString);
//            CPNAME.Text := Trim(Order_Main.fieldbyname('P_NAME').AsString);
//            CPPRICE.Text := Trim(Order_Main.fieldbyname('SQPRICE').AsString);
//            EDIT2.Text := Trim(Order_Main.fieldbyname('SQPS').AsString);
//            EDIT4.Text := Trim(Order_Main.fieldbyname('WFPS').AsString);
//
////            EDIT4.Text := Trim(Order_Main.fieldbyname('SQPS').AsString);
//          end;
//        end;
//      finally
//        frmFHSQListSel.Free;
//      end;
//    end;
//
//  end;
end;

procedure TfrmPBSMCK.Button5Click(Sender: TObject);
begin
  if CDS_MainSel.IsEmpty then
    Exit;
  if CDS_MainSel.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据', '提示', 0);
    Exit;
  end;

  if Application.MessageBox('确定要空加数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;

  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_MainSel.DisableControls;
    with CDS_MainSel do
    begin
      First;
      while CDS_MainSel.Locate('SSel', True, []) = True do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('update  WFB_MJJY_PB  SET FHKONGJIA=' + Edit1.text);
          SQL.Add(' where MJID=''' + Trim(CDS_MainSel.fieldbyname('MJID').AsString) + '''');

          ExecSQL;
        end;

        CDS_MainSel.Delete;

      end;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * FROM   CK_PBCP_CR a inner join WFB_MJJY_PB B on A.MJID=B.MJID  ');
      SQL.Add('WHERE CKOrdNO=' + QuotedStr(Trim(CKOrdNo.Text)));
      SQL.Add('and NEWNAME=' + QuotedStr(Trim(CPNAME.Text)));
      sql.Add('order by a.crtime desc');
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_MainSel);
    SInitCDSData20(ADOQueryTemp, CDS_MainSel);

    CDS_MainSel.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;

    Application.MessageBox('空加成功！', '提示', 0);
    Panel1.Visible := False;
  except

    CDS_MainSel.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('空加失败！', '提示', 0);
  end;
end;

procedure TfrmPBSMCK.KHPMBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'KHPM';
      flagname := '客户品名';
      if ShowModal = 1 then
      begin
        Self.KHPM.Text := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmPBSMCK.Edit6Click(Sender: TObject);
begin
  if Trim(Edit6.Text) = '' then
  begin
    Edit6.Text := '√';
  end
  else
  begin
    Edit6.Text := '';
  end;
end;

procedure TfrmPBSMCK.Edit3KeyPress(Sender: TObject; var Key: Char);
var
  MKey: Char;
begin
  MKey := #13;
  if CKOrdNo.Text = '' then
  begin
    Application.MessageBox('请先取号！', '提示', 0);
    Exit;
  end;
  if KHNAME.Text = '' then
  begin
    Application.MessageBox('请先选择客户！', '提示', 0);
    Exit;
  end;
  if (Edit5.Text = '') and (Edit7.Text = '') and (Edit8.Text = '') then
  begin

    Application.MessageBox('请先选择库位！', '提示', 0);
    PlaySound('wav/请先选择库位.wav', 0, SND_FILENAME or SND_ASYNC);
    exit;

  end;

  if Edit6.Text <> '' then
  begin
    if cpname.Text = '' then
    begin
      Application.MessageBox('强制发货请先选择产品！', '提示', 0);
      exit;
    end;
  end;
//  if (Edit5.Text = '') and (Edit7.Text = '') and (Edit8.Text = '') then
//  begin
//
//    Application.MessageBox('请先选择库位！', '提示', 0);
//    PlaySound('wav/请先选择库位.wav', 0, SND_FILENAME or SND_ASYNC);
//    exit;
//
//  end;
  if Key = #13 then
  begin
    if (edit4.Text = '') and (Edit2.Text <> '') then
    begin
      Edit2KeyPress(EDIT2, MKey);
    end;
  end;
  if edit4.Text <> '' then
  begin
    if StrToFloatDef(edit4.Text, 0) <= 0 then
    begin
      Application.MessageBox('已达发货上限，禁止新增！', '提示', 0);
      Exit;
    end;

  end;
  if Key = #13 then
  begin
    if CPNAME.Text = '' then
    begin
      Exit;
    end;

    if Edit3.Text = '' then
    begin
      Application.MessageBox('数量不能为空！', '提示', 0);
      Exit;
    end;
    with ADOQueryTemp do
    begin
      close;
      sql.Clear;
      sql.Add('exec P_SC_NEWMJID_WSQ @ckordno=' + QuotedStr(Trim(CKOrdNo.Text)));
      sql.Add(',@cpname=' + QuotedStr(Trim(cpname.Text)));
      sql.Add(',@khname=' + QuotedStr(Trim(khname.Text)));
      sql.Add(',@MJMAOZ=' + (Trim(Edit3.Text)));
      sql.Add(',@filler=' + QuotedStr(Trim(DName)));
      sql.Add(',@CARNO=' + QuotedStr(Trim(CARNO.Text)));
      sql.Add(',@BATCHNO=' + QuotedStr(Trim(BATCHNO.Text)));
      sql.Add(',@chejian=' + QuotedStr(Trim(canshu1)));
      Open;
    end;
    BaoID.Text := Trim(ADOQueryTemp.Fieldbyname('bhno').AsString);
    BaoIDKeyPress(BaoID, MKey);
    Sleep(200);
    Edit3.Text := '';
  end;
end;

procedure TfrmPBSMCK.Button6Click(Sender: TObject);
var
  fPrintFile, fshtt: string;
begin

  if CKOrdNo.Text = '' then
    EXIT;
  if (Edit5.Text = '') and (Edit7.Text = '') and (Edit8.Text = '') then
  begin

    Application.MessageBox('请先选择库位！', '提示', 0);
    PlaySound('wav/请先选择库位.wav', 0, SND_FILENAME or SND_ASYNC);
    exit;

  end;
  if Application.MessageBox('是否要更新库位？', '提示', 32 + 4) = IDYES then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_UPDATE_KONGJIA @ckno=' + quotedstr(Trim(CKOrdNo.Text)));

      if Trim(EDIT5.Text) <> '' then
      begin
        SQL.Add(',@FHKUWEI=''库位0'',@kongjia=0  ');
      end;
      if Trim(EDIT7.Text) <> '' then
      begin
        SQL.Add(',@FHKUWEI=''库位1'',@kongjia=0.1  ');
      end;
      if Trim(EDIT8.Text) <> '' then
      begin
        SQL.Add(',@FHKUWEI=''库位2'',@kongjia=0.2  ');
      end;
      ExecSQL;
    end;
  end;

  ExportFtErpFile(trim(ComboBox1.Text) + '.rmf', ADOQueryTemp);
  if (trim(ComboBox1.Text) = '按品名码单') or (trim(ComboBox1.Text) = '无抬头按品名码单') then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMD_PM @CKOrdNo=' + quotedstr(Trim(CKOrdNo.Text)));

      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_JuanPRT);
    SInitCDSData20(ADOQueryTemp, CDS_JuanPRT);
  end;
  if (trim(ComboBox1.Text) = '普通码单') or (trim(ComboBox1.Text) = '无抬头普通码单') or (trim(ComboBox1.Text) = '普通码单(向豪纺织)') then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMD1 @CKOrdNo=' + quotedstr(Trim(CKOrdNo.Text)));

      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_JuanPRT);
    SInitCDSData20(ADOQueryTemp, CDS_JuanPRT);
  end;
  if (trim(ComboBox1.Text) = '有抬头分机台') or (trim(ComboBox1.Text) = '无抬头分机台') or (trim(ComboBox1.Text) = '有抬头分机台(向豪纺织)') then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMD2 @CKOrdNo=' + quotedstr(Trim(CKOrdNo.Text)));

      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_JuanPRT);
    SInitCDSData20(ADOQueryTemp, CDS_JuanPRT);
  end;

  if (trim(ComboBox1.Text) = '有抬头分批号') or (trim(ComboBox1.Text) = '无抬头分批号') then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMD5 @CKOrdNo=' + quotedstr(Trim(CKOrdNo.Text)));

      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_JuanPRT);
    SInitCDSData20(ADOQueryTemp, CDS_JuanPRT);
  end;

  if (trim(ComboBox1.Text) = '出库单') then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMD6 @CKOrdNo=' + quotedstr(Trim(CKOrdNo.Text)));

      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_JuanPRT);
    SInitCDSData20(ADOQueryTemp, CDS_JuanPRT);
  end;
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(ComboBox1.Text) + '.rmf';
  if FileExists(fPrintFile) then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('select top 1 ISNULL(KHTAITOU,KHNAME)KHNAME,khname as khname2,CRTIME from WFB_MJJY_PB a  inner join CK_PBCP_CR b on a.mjid=b.mjid   where CKOrdNo=' + quotedstr(Trim(CKOrdNo.Text)));
      Open;
    end;
    RM1.PreviewButtons := [pbZoom, pbLoad, pbSave, pbPrint, pbFind, pbPageSetup, pbExit, pbSaveToXLS, pbExport, pbNavigator];
    fshtt := Trim(ADOQueryTemp.fieldbyname('khname2').AsString);

    RMVariables['shouhuotaitou'] := Trim(ADOQueryTemp.fieldbyname('khname').AsString);
    RMVariables['MJDATE'] := Trim(ADOQueryTemp.fieldbyname('CRTime').AsString);

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add(' SELECT TOP 1 * FROM Company  WHERE CONAME=' + quotedstr(Trim(fshtt)));
      SQL.Add('AND VALID=''Y'' and cotype=''客户''');
      Open;
    end;
    RMVariables['SFHS'] := Trim(ADOQueryTemp.fieldbyname('SFHS').AsString);

//    RMVariables['FHTaiTou'] := Trim(CDS_Main.fieldbyname('FHTT').AsString);

    RMVariables['Znote'] := '';
    RMVariables['FILLER'] := Trim(DNAME);

    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * FROM   CK_PBCP_CR a inner join WFB_MJJY_PB B on A.MJID=B.MJID  ');
    SQL.Add('WHERE CKOrdNO=' + QuotedStr(Trim(CKOrdNo.Text)));
    SQL.Add('and NEWNAME=' + QuotedStr(Trim(CPNAME.Text)));
    sql.Add('order by a.crtime desc');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_MainSel);
  SInitCDSData20(ADOQueryTemp, CDS_MainSel);

end;

procedure TfrmPBSMCK.Edit2Change(Sender: TObject);
var
  f1, f2: double;
begin
  f1 := StrToFloatDef(Edit2.Text, 0);

end;

procedure TfrmPBSMCK.Edit2KeyPress(Sender: TObject; var Key: Char);
var
  f1, f2: double;
begin
  if Key = #13 then
  begin
    f1 := StrToFloatDef(Edit2.Text, 0);
    if fsid.Text = '' then
    begin
      if CKOrdNo.Text <> '' then
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.add('select count(*)js from CK_PBCP_CR a inner join WFB_MJJY_PB b on a.mjid=b.mjid  where CKOrdNo=' + quotedstr(Trim(CKOrdNo.Text)));
          sql.Add('and khpm=' + quotedstr(Trim(KHPM.Text)));
          Open;
        end;
      end
      else
      begin
        Edit4.Text := FloatToStr(f1);
      end;

    end
    else
    begin
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.add('select count(*)js from WFB_MJJY_PB a   where fhsubid=' + quotedstr(Trim(ssid.Text)));
        Open;
      end;
    end;
    if ADOQueryTemp.IsEmpty = false then
    begin
      Edit4.Text := FloatToStr(f1 - ADOQueryTemp.fieldbyname('js').AsInteger);
    end;

  end;
end;

procedure TfrmPBSMCK.cxGridDBColumn1PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin

  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv3.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_MainSel do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('update  WFB_MJJY_PB set newcarno=' + QuotedStr(Trim(mvalue)));
    sql.Add('where mjid=' + QuotedStr(Trim(CDS_MainSel.fieldbyname('mjid').AsString)));
    ExecSQL;
  end;
  tv3.Controller.EditingController.ShowEdit();
end;

procedure TfrmPBSMCK.v3Column1PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin

  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv3.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_MainSel do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('update  WFB_MJJY_PB set mjmaoz=' + (Trim(mvalue)));
    sql.Add('where mjid=' + QuotedStr(Trim(CDS_MainSel.fieldbyname('mjid').AsString)));
    sql.Add('update  CK_PBCP_CR set kgqty=' + (Trim(mvalue)));
    sql.Add('where mjid=' + QuotedStr(Trim(CDS_MainSel.fieldbyname('mjid').AsString)));

    sql.Add('exec P_UP_YCLQTY @mainid=' + QuotedStr(Trim(CDS_MainSel.fieldbyname('mainid').AsString)));
    ExecSQL;
  end;
  tv3.Controller.EditingController.ShowEdit();
end;

procedure TfrmPBSMCK.cpnamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  Panel3.Visible := True;
//  if fsid.text = '' then
//  begin
//    try
//      frmCPManageFMSel := TfrmCPManageFMSel.Create(Application);
//      with frmCPManageFMSel do
//      begin
//        if ShowModal = 1 then
//        begin
//
//          cpname.Text := frmCPManageFMSel.CDS_Main.fieldbyname('CYName').AsString;
//          KHPM.Text := frmCPManageFMSel.CDS_Main.fieldbyname('CYName').AsString;
//        end;
//      end;
//    finally
//      frmCPManageFMSel.Free;
//    end;
//    if cpname.Text <> '' then
//      INITTJ();
//  end
//  else
//  begin
//    try
//      frmFHSQListSel := TfrmFHSQListSel.Create(self);
//      with frmFHSQListSel do
//      begin
//        CustomerNoName.Text := Self.KHNAME.Text;
//        CustomerNoName.Enabled := FALSE;
//        if ShowModal = 1 then
//        begin
//          FSID.Text := Trim(Order_Main.fieldbyname('FSID').AsString);
//          SSID.Text := Trim(Order_Main.fieldbyname('SSID').AsString);
//          taitou.Text := Trim(Order_Main.fieldbyname('shouhuotaitou').AsString);
//          KHPM.Text := Trim(Order_Main.fieldbyname('FHKHPM').AsString);
//          CPNAME.Text := Trim(Order_Main.fieldbyname('P_NAME').AsString);
//          CPPRICE.Text := Trim(Order_Main.fieldbyname('SQPRICE').AsString);
//          EDIT2.Text := Trim(Order_Main.fieldbyname('SQPS').AsString);
//          EDIT4.Text := Trim(Order_Main.fieldbyname('WFPS').AsString);
//
////            EDIT4.Text := Trim(Order_Main.fieldbyname('SQPS').AsString);
//        end;
//      end;
//    finally
//      frmFHSQListSel.Free;
//    end;
//  end;

end;

procedure TfrmPBSMCK.N1Click(Sender: TObject);
var
  f1: Double;
begin
  if CDS_MainSel.IsEmpty then
    Exit;
  if CDS_MainSel.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据', '提示', 0);
    Exit;
  end;

  if Application.MessageBox('确定要撤销数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;

  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_MainSel.DisableControls;
    with CDS_MainSel do
    begin
      First;
      while CDS_MainSel.Locate('SSel', True, []) = True do
      begin
        if pos('ZD', Trim(CDS_MainSel.fieldbyname('MJID').asstring)) > 0 then
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('delete WFB_MJJY_PB_DelLog where MJID=''' + Trim(CDS_MainSel.fieldbyname('MJID').AsString) + '''');
            SQL.Add('insert into WFB_MJJY_PB_DelLog ');
            sql.Add('select * from WFB_MJJY_PB where MJID=''' + Trim(CDS_MainSel.fieldbyname('MJID').AsString) + '''');
            ExecSQL;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update WFB_MJJY_PB_DelLog Set Note=Isnull(Note,'''')+''→删除人:''+''' + Trim(DName) + '''');
            sql.Add('+'' ''+Convert(Varchar(20),getdate(),120)');
            sql.Add(' where MJID=''' + Trim(CDS_MainSel.fieldbyname('MJID').AsString) + '''');
            ExecSQL;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('delete WFB_MJJY_PB where MJID=''' + Trim(CDS_MainSel.fieldbyname('MJID').AsString) + '''');
            sql.Add('delete WFB_MJJY_CD_PB where MJID=''' + Trim(CDS_MainSel.fieldbyname('MJID').AsString) + '''');
            sql.Add('delete CK_PBCP_CR where MJID=''' + Trim(CDS_MainSel.fieldbyname('MJID').AsString) + '''');
            sql.Add('delete CK_PBCP_kc where MJID=''' + Trim(CDS_MainSel.fieldbyname('MJID').AsString) + '''');
            ExecSQL;
          end;

          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('exec P_UP_YCLQTY  @mainid=' + quotedstr(trim(Trim(CDS_MainSel.fieldbyname('mainid').AsString))));
            ExecSQL;
          end;
        end
        else
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('exec P_Do_PBCK @CRFlag=''未出库'', @CKOrdNo='''', @MJID=' + quotedstr(trim(CDS_MainSel.fieldbyname('MJID').asstring)));
            sql.Add('UPDATE WFB_MJJY_PB SET NEWNAME=NULL');
            SQL.Add(',NEWBATCHNO=NULL');
            SQL.Add(',NEWCARNO=NULL');
            SQL.Add(',ckno=NULL');
            SQL.Add(',FHKONGJIA=NULL');
            SQL.Add(',FHKuwei=NULL');
            SQL.Add(',FHMAINID=NULL');
            SQL.Add(',FHSUBID=NULL');
            SQL.Add(',CPPRICE=NULL');
            SQL.Add(',pici=NULL');
            SQL.Add(',KHPM=NULL');
            SQL.Add(',KHTAITOU=NULL');
            SQL.Add('WHERE MJID=' + quotedstr(trim(CDS_MainSEL.fieldbyname('MJID').asstring)));
            sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
            sql.Add(' ' + quotedstr(trim(DName)));
            sql.Add(',getdate() ');
            sql.Add(',' + quotedstr(trim(self.Caption)));
            sql.Add(',' + quotedstr(trim('成品出库撤销')));
            sql.Add(',' + quotedstr(trim('出库单号：' + trim(CDS_MainSEL.FieldByName('CKOrdNo').AsString) + ' 条码：' + trim(CDS_MainSEL.FieldByName('MJID').AsString))));
            sql.Add(',' + quotedstr(trim('成功')));
            sql.Add(')');
            ExecSQL;
          end;

//          with ADOQueryCmd do
//          begin
//            Close;
//            sql.Clear;
//            sql.Add('exec P_Do_FHSQ ''' + Trim(CDS_MainSel.fieldbyname('CKOrdNo').AsString) + '''');
//            ExecSQL;
//          end;
        end;
        if fsid.Text <> '' then
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('exec P_Do_FHSQ_SSID ''' + Trim(fsid.Text) + '''');
            ExecSQL;
          end;
        end;
        if Edit2.Text <> '' then
        begin
          f1 := StrToFloatDef(Edit2.Text, 0);
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.add('select count(*)js from CK_PBCP_CR a   where CKOrdNo=' + quotedstr(Trim(CKOrdNo.Text)));
            Open;
          end;
          Edit4.Text := FloatToStr(f1 - ADOQueryTemp.fieldbyname('js').AsInteger);
        end;

        CDS_MainSel.Delete;

      end;
    end;
    CDS_MainSel.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;

    Application.MessageBox('作废成功！', '提示', 0);
  except

    CDS_MainSel.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('作废失败！', '提示', 0);
  end;

end;

procedure TfrmPBSMCK.Button7Click(Sender: TObject);
begin
  Panel1.Visible := False;
end;

procedure TfrmPBSMCK.N3Click(Sender: TObject);
begin
  SelOKNo(CDS_MainSel, True);
end;

procedure TfrmPBSMCK.N4Click(Sender: TObject);
begin
  SelOKNo(CDS_MainSel, false);
end;

procedure TfrmPBSMCK.N2Click(Sender: TObject);
begin
  panel1.Visible := true;
end;

procedure TfrmPBSMCK.Edit5Click(Sender: TObject);
begin

  if edit5.Text = '√' then
  begin
    edit5.Text := '';
  end
  else
  begin
    edit5.Text := '√';
    edit7.Text := '';
    edit8.Text := '';
  end;
end;

procedure TfrmPBSMCK.Edit7Click(Sender: TObject);
begin
  if edit7.Text = '√' then
  begin
    edit7.Text := '';
  end
  else
  begin
    edit7.Text := '√';
    edit5.Text := '';
    edit8.Text := '';
  end;
end;

procedure TfrmPBSMCK.Edit8Click(Sender: TObject);
begin
  if edit8.Text = '√' then
  begin
    edit8.Text := '';
  end
  else
  begin
    edit8.Text := '√';
    edit5.Text := '';
    edit7.Text := '';
  end;
end;

procedure TfrmPBSMCK.Button8Click(Sender: TObject);
begin
  fsid.Text := '';
  ssid.Text := '';
  Edit2.Text := '0';
  Edit4.Text := '0';
  CPPRICE.Text := '0';
  edit5.Text := '';
  edit7.Text := '';
  edit8.Text := '';
end;

procedure TfrmPBSMCK.Button9Click(Sender: TObject);
var
  fxz: string;
begin
  if khname.Text = '' then
  begin
    Application.MessageBox('请先选择客户！', '提示', 0);
    exit;
  end;
  try
    frmFHSQListSel := TfrmFHSQListSel.Create(self);
    with frmFHSQListSel do
    begin
      CustomerNoName.Text := Self.KHNAME.Text;
      CustomerNoName.Enabled := FALSE;
      if ShowModal = 1 then
      begin
        FSID.Text := Trim(Order_Main.fieldbyname('FSID').AsString);
        SSID.Text := Trim(Order_Main.fieldbyname('SSID').AsString);
        taitou.Text := Trim(Order_Main.fieldbyname('shouhuotaitou').AsString);
        KHPM.Text := Trim(Order_Main.fieldbyname('FHKHPM').AsString);
//        CPNAME.Text := Trim(Order_Main.fieldbyname('P_NAME').AsString);
        CPPRICE.Text := Trim(Order_Main.fieldbyname('SQPRICE').AsString);
        EDIT2.Text := Trim(Order_Main.fieldbyname('SQPS').AsString);
        EDIT4.Text := Trim(Order_Main.fieldbyname('WFPS').AsString);
        Panel3.Visible := False;
        KHPM.Enabled := False;
        fxz := '1';
      end;
    end;
  finally
    frmFHSQListSel.Free;
  end;
  if fxz = '1' then
  begin
    try
      frmCPManageFMSel := TfrmCPManageFMSel.Create(Application);
      with frmCPManageFMSel do
      begin
        if ShowModal = 1 then
        begin
//          KHPM.Enabled := True;
          cpname.Text := frmCPManageFMSel.CDS_Main.fieldbyname('CYName').AsString;
//          KHPM.Text := frmCPManageFMSel.CDS_Main.fieldbyname('CYName').AsString;

//          Self.Panel3.Visible := False;
        end;
      end;
    finally
      frmCPManageFMSel.Free;
    end;
    if cpname.Text <> '' then
      INITTJ();
  end;
end;

procedure TfrmPBSMCK.Button10Click(Sender: TObject);
begin
  try
    frmCPManageFMSel := TfrmCPManageFMSel.Create(Application);
    with frmCPManageFMSel do
    begin
      if ShowModal = 1 then
      begin
        KHPM.Enabled := True;
        cpname.Text := frmCPManageFMSel.CDS_Main.fieldbyname('CYName').AsString;
        KHPM.Text := frmCPManageFMSel.CDS_Main.fieldbyname('CYName').AsString;
        FSID.Text := '';
        SSID.Text := '';
        CPPRICE.Text := '';
        Edit2.Text := '0';
        Edit4.Text := '0';
        Self.Panel3.Visible := False;
      end;
    end;
  finally
    frmCPManageFMSel.Free;
  end;
  if cpname.Text <> '' then
    INITTJ();
end;

procedure TfrmPBSMCK.Button11Click(Sender: TObject);
begin
  panel3.Visible := False;
end;

end.

