unit U_SQSMCK;

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, RM_Dataset, RM_System,
  RM_Common, RM_Class, RM_GridReport, RM_e_Xls;

type
  TfrmSQSMCK = class(TForm)
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    DS_MainSel: TDataSource;
    CDS_MainSel: TClientDataSet;
    cxGridPopupMenu4: TcxGridPopupMenu;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    v3Column1: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    v3Column2: TcxGridDBColumn;
    Panel2: TPanel;
    Label10: TLabel;
    Label11: TLabel;
    Label5: TLabel;
    Label4: TLabel;
    Label9: TLabel;
    CKPSQty: TEdit;
    CKOrdNo: TEdit;
    BaoID: TEdit;
    CRTime: TDateTimePicker;
    KHName: TEdit;
    Button2: TButton;
    Label6: TLabel;
    PrtCode1: TEdit;
    Label8: TLabel;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    MJStr3: TEdit;
    Label2: TLabel;
    Label3: TLabel;
    Edit1: TEdit;
    Label12: TLabel;
    CDS_Main: TClientDataSet;
    CRType: TEdit;
    Button1: TButton;
    Label1: TLabel;
    v3Column4: TcxGridDBColumn;
    Label7: TLabel;
    Button3: TButton;
    ds1: TClientDataSet;
    batchno: TComboBox;
    Edit2: TEdit;
    Label13: TLabel;
    Label33: TLabel;
    Edit6: TEdit;
    CPNAME: TComboBox;
    CARNO: TComboBox;
    Tv3Column1: TcxGridDBColumn;
    Label14: TLabel;
    CPPRICE: TEdit;
    KHPM: TBtnEditA;
    Label15: TLabel;
    Button6: TButton;
    ComboBox1: TComboBox;
    RM1: TRMGridReport;
    CDS_JuanPRT: TClientDataSet;
    RMDBMX: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    Label16: TLabel;
    pici: TComboBox;
    Label18: TLabel;
    taitou: TEdit;
    Label17: TLabel;
    edtIsSMTH: TEdit;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    Tv3Column2: TcxGridDBColumn;
    N2: TMenuItem;
    N3: TMenuItem;
    N4: TMenuItem;
    Tv3Column3: TcxGridDBColumn;
    Panel1: TPanel;
    Button4: TButton;
    Button5: TButton;
    Edit3: TEdit;
    Label19: TLabel;
    Tv3Column4: TcxGridDBColumn;
    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);
//    function YSData(Order_Main10: TClientDataSet): Boolean;
    procedure Button3Click(Sender: TObject);
    procedure Edit2KeyPress(Sender: TObject; var Key: Char);
    procedure Edit6Click(Sender: TObject);
    procedure CPNAMEChange(Sender: TObject);
    procedure KHPMBtnClick(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure cxGridDBColumn1PropertiesEditValueChanged(Sender: TObject);
    procedure v3Column1PropertiesEditValueChanged(Sender: TObject);
    procedure edtIsSMTHClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure N4Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  private
    { Private declarations }
    canshu1, FCKOrdNo, FSMStr, JXFlag: string;
    FSUBID: string;
    procedure TMPanDuan();
    procedure INITTJ();
    procedure SaveCKDataJTMCX();
    function JudgeJTMCX(): Boolean;
  public
    { Public declarations }
  end;

var
  frmSQSMCK: TfrmSQSMCK;

implementation

uses
  U_DataLink, U_Fun, MMSystem, U_FHSQListSel, U_PBKCListSel, U_ZDYHelp;

{$R *.dfm}

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

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

      sql.Add(' UPdate CK_PBCP_KC Set KCValid=''Y'' ');
      sql.Add(' where  CK_PBCP_KC.MJID=' + Quotedstr(Trim(BaoID.Text)));
      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(BaoID.Text)));
      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(',KHTAITOU=NULL');
      SQL.Add(',pici=NULL');
      SQL.Add(',ckno=NULL');
      SQL.Add(',CPPRICE=NULL');
      SQL.Add(',KHPM=NULL');
      SQL.Add('WHERE MJID=' + quotedstr(trim(BaoID.Text)));
      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(BaoID.Text) + ' 出库单号：' + trim(CKOrdNo.Text)));
      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 TfrmSQSMCK.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  NAME IN (SELECT P_NAME FROM FHSQ_Sub WHERE FSID= ' + QuotedStr(Trim(CKOrdNo.Text)) + '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;
  SInitComBoxBySql(ADOQueryCmd, CARNO, False, FSJ2);

  if batchno.Text <> '' then
  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)) + '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;

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

procedure TfrmSQSMCK.FormDestroy(Sender: TObject);
begin
  frmSQSMCK := nil;
end;

procedure TfrmSQSMCK.FormShow(Sender: TObject);
begin
  BaoID.SetFocus;
  ReadCxGrid('扫描出库有申请', Tv3, '坯布仓库');
  CRTime.DateTime := SGetServerDate(ADOQueryTemp);
  canshu1 := Trim(DParameters1);

end;

procedure TfrmSQSMCK.TMPanDuan();
begin
  //判断扫描的是发货申请单，还是布匹条码 ，判断是否已经扫描过发货单 ，或者更改了发货单
  JXFlag := '';
  FSMStr := Trim(BaoID.Text);
  if Copy(FSMStr, 1, 2) = 'CK' then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from FHSQ_Main where FSID=''' + Trim(FSMStr) + '''');
      Open;
    end;
    if ADOQueryTemp.IsEmpty then
    begin
      JXFlag := '999';
      PlaySound('wav/申请单不存在暂停发货.wav', 0, SND_FILENAME or SND_ASYNC);
      Exit;
    end;

    if Trim(CKOrdNo.Text) <> '' then
    begin
      FCKOrdNo := Trim(CKOrdNo.Text);
    end;
    CKOrdNo.Text := Trim(FSMStr);
    if FSMStr <> FCKOrdNo then
    begin
      Edit1.Text := '';
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select B.* ');
        sql.Add(' from CK_PBCP_CR A');
        sql.Add(' inner join WFB_MJJY_PB B on A.MJID=B.MJID');
        sql.Add(' where A.CKOrdNO=''' + Trim(CKOrdNo.Text) + '''');
//        if CPNAME.Text <> '' then
//        begin
//          SQL.Add('and newname=' + QuotedStr(Trim(CPNAME.Text)));
//        end;

        Open;
      end;
      SCreateCDS20(ADOQueryTemp, CDS_MainSel);
      SInitCDSData20(ADOQueryTemp, CDS_MainSel);
//      SCSHDataNew(ADOQueryTemp, Panel2, 2);
//      if (CDS_MainSel.IsEmpty = False) and (Tv3.DataController.Summary.FooterSummaryValues[2] <> NULL) then
//        Edit1.Text := IntToStr(StrToInt(CKPSQty.TEXT) - Tv3.DataController.Summary.FooterSummaryValues[2])
//      else
//        Edit1.Text := CKPSQty.TEXT;
    end;
    BaoID.Text := '';
    JXFlag := '888';
  end
  else
  begin
    if Trim(CKOrdNo.Text) = '' then
    begin
      JXFlag := '999';
      PlaySound('wav\未扫描申请单暂停发货.wav', 0, SND_FILENAME or SND_ASYNC);
      Exit;
    end;
    if Trim(CPNAME.Text) = '' then
    begin
      JXFlag := '999';
      PlaySound('wav\请先选择产品.wav', 0, SND_FILENAME or SND_ASYNC);
      //请先选择产品
      Exit;
    end;
    if trim(Edit6.Text) = '' then
    begin
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from WFB_MJJY_PB A  inner join Cloth_Sub B on A.SubId=B.SubId ');
        sql.Add('where exists (select * from  FHSQ_sub Y   where Y.P_name=B.C_Codename and Y.FSId=' + quotedstr(Trim(CKOrdNo.Text)) + ')');
        sql.Add('and  A.MJID=' + quotedstr(Trim(FSMStr)));
        Open;
      end;
      if ADOQueryTemp.IsEmpty then
      begin
        JXFlag := '999';
        PlaySound('wav\条码错误暂停发货.wav', 0, SND_FILENAME or SND_ASYNC);
        Exit;
      end
      else
      begin
        if Trim(CPNAME.Text) <> '' then
        begin
          if Trim(ADOQueryTemp.fieldbyname('C_Codename').AsString) <> Trim(CPNAME.Text) then
          begin
            JXFlag := '999';
            PlaySound('wav\产品不同暂停发货.wav', 0, SND_FILENAME or SND_ASYNC);
            Exit;
          end;
        end;

        if Trim(CarNo.Text) <> '' then
        begin
          if Trim(ADOQueryTemp.fieldbyname('carno').AsString) <> Trim(carno.Text) then
          begin
            JXFlag := '999';
            PlaySound('wav\机台不同暂停发货.wav', 0, SND_FILENAME or SND_ASYNC);
            Exit;
          end;
        end;
        if Trim(batchno.Text) <> '' then
        begin
          if Trim(ADOQueryTemp.fieldbyname('apbatchno').AsString) <> Trim(batchno.Text) then
          begin
            JXFlag := '999';
            PlaySound('wav\批号不同暂停发货.wav', 0, SND_FILENAME or SND_ASYNC);
            Exit;
          end;
        end;
      end;
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select A.CKOrdNo from CK_PBCP_CR A  inner join Cloth_Sub B on A.SubId=B.SubId ');
        sql.Add('where exists (select * from   FHSQ_SUB Y where Y.P_name=B.C_Codename and Y.FSId=' + quotedstr(Trim(CKOrdNo.Text)) + ')');
        sql.Add(' and  A.MJID=' + quotedstr(Trim(FSMStr)));
        sql.Add(' and  A.CRFlag=''出库'' ');
        Open;
      end;
      if ADOQueryTemp.IsEmpty = False then
      begin
        if Trim(ADOQueryTemp.fieldbyname('CKOrdNo').AsString) = Trim(CKOrdNo.Text) then
        begin
          JXFlag := '999';
          PlaySound('wav\已扫描.wav', 0, SND_FILENAME or SND_ASYNC);
          Exit;
        end
        else
        begin
          JXFlag := '999';
          PlaySound('wav\已出库暂停发货.wav', 0, SND_FILENAME or SND_ASYNC);
          Exit;
        end;
      end;
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select A.CKOrdNo from CK_PBCP_CR A  inner join Cloth_Sub B on A.SubId=B.SubId ');
        sql.Add('where exists (select * from   FHSQ_SUB  Y where Y.P_name=B.C_Codename and Y.FSId=' + quotedstr(Trim(CKOrdNo.Text)) + ')');
        sql.Add(' and  A.MJID=' + quotedstr(Trim(FSMStr)));
        sql.Add(' and  A.CRFlag=''入库'' ');
        Open;
      end;
      if ADOQueryTemp.IsEmpty then
      begin
        JXFlag := '999';
        PlaySound('wav\未入库暂停发货.wav', 0, SND_FILENAME or SND_ASYNC);
        Exit;
      end;
    end
    else
    begin
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from WFB_MJJY_PB a ');
        sql.Add('where  A.MJID=' + quotedstr(Trim(FSMStr)));

        Open;
      end;
      if ADOQueryTemp.IsEmpty then
      begin
        JXFlag := '999';
        PlaySound('wav\未入库暂停发货.wav', 0, SND_FILENAME or SND_ASYNC);
        Exit;
      end;
    end;

    if Trim(Edit1.Text) <> '' then
    begin
//      if StrToInt(Edit1.Text) >= StrToInt(CKPSQty.Text) then
//      begin
//        JXFlag := '999';
//        PlaySound('wav\超出发货上限暂停发货.wav', 0, SND_FILENAME or SND_ASYNC);
//        Exit;
//      end;

      if StrToInt(Edit1.Text) <= 0 then
      begin
        JXFlag := '999';
        PlaySound('wav\超出发货上限暂停发货.wav', 0, SND_FILENAME or SND_ASYNC);
        Exit;
      end;
    end;

  end;
  //判断扫描的是发货申请单，还是布匹条码 ，判断是否已经扫描过发货单 ，或者更改了发货单    FHDate
end;

procedure TfrmSQSMCK.BaoIDKeyPress(Sender: TObject; var Key: Char);
var
  maxno, SMFlag, KYCKFlag, FMainId: string;
  FFQty: Integer;
begin
  if Key = #13 then
  begin
    if edtIsSMTH.Text = '' then
    begin
      TMPanDuan();
      if Trim(JXFlag) = '999' then
      begin
        BaoID.Text := '';
        Exit;
      end;
      if Trim(JXFlag) = '888' then
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select A.*,MJStr3=A.SeBie,KHName=CustomerNoName,CKPSQty=A.SQPSHZ,FHPSHZ2=ISNULL(FHPSHZ,0) ');
          Sql.Add(' ,prtcode1=STUFF(( SELECT  '','' +convert(VARCHAR, P_CODE) FROM  FHSQ_Sub X where A.FSID=X.FSID  FOR XML PATH('''')), 1, 1, '''')  ');
          Sql.Add(' ,prtcodeNAME1=STUFF(( SELECT  '','' +convert(VARCHAR, P_NAME) FROM  FHSQ_Sub X where A.FSID=X.FSID  FOR XML PATH('''')), 1, 1, '''')  ');

          sql.Add(' from FHSQ_Main A');

          sql.Add(' where FSId=''' + Trim(CKOrdNo.Text) + ''' ');
//        ShowMessage(SQL.Text);
          Open;
        end;
        SCSHDataNew(ADOQueryTemp, Panel2, 2);
        taitou.Text := Trim(ADOQueryTemp.fieldbyname('shouhuotaitou').asstring);
        INITTJ();
//      SQBatchNoHZ.text := Trim(ADOQueryTemp.fieldbyname('FHBatchNo').AsString);
        Edit1.Text := IntToStr(StrToInt(CKPSQty.TEXT) - ADOQueryTemp.fieldbyname('FHPSHZ2').VALUE);
        Exit;
      end;

      with ADOQueryTemp do
      begin
        close;
        sql.Clear;
        sql.Add(' select A.*,B.CRID from WFB_MJJY_PB A');
        sql.Add(' inner join CK_PBCP_CR B on A.MJID=B.MJID and B.CRFlag=''入库'' ');
        sql.Add('  where A. MJID=''' + Trim(BaoID.Text) + '''');
        Open;
      end;
      SCreateCDS20(ADOQueryTemp, CDS_Main);
      SInitCDSData20(ADOQueryTemp, CDS_Main);
      try

        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_PBCp_KC where MJID=''' + Trim(BaoID.Text) + '''');
          sql.Add(' and KCKGQty>0');
          Open;
        end;
        if ADOQueryTemp.IsEmpty then
        begin
          KYCKFlag := '不能出库';
        end
        else
        begin
          KYCKFlag := '能出库';
        end;
        if Trim(KYCKFlag) = '能出库' then
        begin
          ADOQueryCmd.Connection.BeginTrans;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('exec P_Do_PBCK @CRFlag=''直接出库'', @CKOrdNo=' + quotedstr(trim(CKOrdNo.Text)) + ', @MJID=' + quotedstr(trim(CDS_Main.fieldbyname('MJID').asstring)) + ', @DName=' + quotedstr(trim(DName)));
            ExecSQL;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('UPDATE WFB_MJJY_PB SET NEWNAME=' + QuotedStr(Trim(CPNAME.Text)));
            if Trim(batchno.Text) <> '' then
            begin
              SQL.Add(',NEWBATCHNO=' + QuotedStr(Trim(batchno.Text)));
            end
            else
            begin
              SQL.Add(',NEWBATCHNO=APBATCHNO');
            end;
            if Trim(CARNO.Text) <> '' then
            begin
              SQL.Add(',NEWCARNO=' + QuotedStr(Trim(CARNO.Text)));
            end
            else
            begin
              SQL.Add(',NEWCARNO=CARNO');
            end;
            sql.Add(',FHKONGJIA=0.2');
            SQL.Add(',ckno=' + QuotedStr(Trim(CKOrdNo.Text)));
            if Trim(CPPRICE.Text) <> '' then
            begin
              SQL.Add(',CPPRICE=' + (Trim(CPPRICE.Text)));
            end;
            if Trim(KHPM.Text) <> '' then
            begin
              SQL.Add(',KHPM=' + QuotedStr(Trim(KHPM.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;
            SQL.Add('WHERE MJID=' + quotedstr(trim(CDS_Main.fieldbyname('MJID').asstring)));
//          ShowMessage(SQL.Text);
            ExecSQL;
          end;

          FMainId := Trim(CDS_Main.fieldbyname('MainID').AsString);

          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('exec P_Do_FHSQ ''' + Trim(CKOrdNo.Text) + '''');
            ExecSQL;
          end;

          ADOQueryCmd.Connection.CommitTrans;
          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);
//        with CDS_MainSel do
//        begin
//          Append;
//          FieldByName('Mainid').Value := Self.CDS_Main.fieldbyname('Mainid').Value;
//          FieldByName('SubID').Value := Self.CDS_Main.fieldbyname('SubID').Value;
//          FieldByName('MJMaoZ').Value := Self.CDS_Main.fieldbyname('MJMaoZ').Value;
//          FieldByName('MJId').Value := Self.CDS_Main.fieldbyname('MJId').Value;
//          FieldByName('APXH').Value := Self.CDS_Main.fieldbyname('APXH').Value;
//          FieldByName('MJStr3').Value := Self.CDS_Main.fieldbyname('MJStr3').Value;
//          FieldByName('CarNo').Value := Self.CDS_Main.fieldbyname('CarNo').Value;
//          FieldByName('APBatchNo').Value := Self.CDS_Main.fieldbyname('APBatchNo').Value;
//          FieldByName('NAME').Value := Self.CDS_Main.fieldbyname('NAME').Value;
//          Post;
//        end;
          if CDS_MainSel.IsEmpty = False then
          begin
            Edit1.Text := IntToStr(StrToInt(CKPSQty.TEXT) - Tv3.DataController.Summary.FooterSummaryValues[2]);
          end
          else
          begin
            Edit1.Text := CKPSQty.Text;
          end;

//        with ADOQueryTemp do
//        begin
//          Close;
//          sql.Clear;
//          sql.Add(' select COUNT(*) fhjs from CK_PBCP_CR where CKOrdNo= ''' + Trim(CKOrdNo.Text) + '''');
//          Open;
//        end;
//
//        if StrToInt(Edit1.Text) <> ADOQueryTemp.FieldByName('fhjs').AsInteger then
//        begin
//          PlaySound('wav\数据保存失败暂停发货.wav', 0, SND_FILENAME or SND_ASYNC);
//          Application.MessageBox('出库异常,请重新选择申请单！', '提示', 0);
//        end;
          BaoID.Text := '';
          PlaySound('wav\正确.wav', 0, SND_FILENAME or SND_ASYNC);
          Exit;
        end;
      except
        BaoID.Text := '';
        PlaySound('数据保存失败暂停发货.wav', 0, SND_FILENAME or SND_ASYNC);
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('出库异常！', '提示', 0);
      end;
    end
    else
    begin
      if JudgeJTMCX() then  //检测卷条码
        SaveCKDataJTMCX();

      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);
      if CDS_MainSel.IsEmpty = False then
      begin
        Edit1.Text := IntToStr(StrToInt(CKPSQty.TEXT) - Tv3.DataController.Summary.FooterSummaryValues[2]);
      end
      else
      begin
        Edit1.Text := CKPSQty.Text;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('exec P_Do_FHSQ ''' + Trim(CKOrdNo.Text) + '''');
        ExecSQL;
      end;
      BaoID.Text := '';
    end;

  end;
end;

function TfrmSQSMCK.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(BaoID.Text));
      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;

procedure TfrmSQSMCK.Button2Click(Sender: TObject);
begin

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

procedure TfrmSQSMCK.Button1Click(Sender: TObject);
var
  MKey: Char;
begin
  MKey := #13;
  try
    frmFHSQListSel := TfrmFHSQListSel.Create(self);
    with frmFHSQListSel do
    begin
      if ShowModal = 1 then
      begin
        BaoID.Text := Trim(Order_Main.fieldbyname('FSID').AsString);
        BaoIDKeyPress(BaoID, MKey);
      end;
    end;
  finally
    frmFHSQListSel.Free;
  end;
end;

procedure TfrmSQSMCK.Button3Click(Sender: TObject);
var
  MKey: Char;
begin
  MKey := #13;
  try
    frmPBKCListSel := TfrmPBKCListSel.Create(Application);
    with frmPBKCListSel do
    begin
      FCKOrdNo := trim(CKOrdNo.text);

      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;
end;

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

  if Key = #13 then
  begin
    if Edit2.Text = '' then
    begin
      Application.MessageBox('数量不能为空！', '提示', 0);
      Exit;
    end;
    if CPNAME.Text = '' then
    begin
      Exit;
    end;
    if FSUBID = '' then
    begin
      Exit;
    end;
    if StrToFloatDef(Edit1.Text, 0) <= 0 then
    begin
      PlaySound('wav\超出发货上限暂停发货.wav', 0, SND_FILENAME or SND_ASYNC);
      Exit;
    end;
    with ADOQueryTemp do
    begin
      close;
      sql.Clear;
      sql.Add('exec P_SC_NEWMJID @FSID=' + QuotedStr(Trim(FSUBID)));
      sql.Add(',@MJMAOZ=' + (Trim(Edit2.Text)));
      sql.Add(',@filler=' + QuotedStr(Trim(DName)));
      sql.Add(',@CARNO=' + QuotedStr(Trim(CARNO.Text)));
      sql.Add(',@BATCHNO=' + QuotedStr(Trim(BATCHNO.Text)));
      Open;
    end;
    BaoID.Text := Trim(ADOQueryTemp.Fieldbyname('bhno').AsString);
    BaoIDKeyPress(BaoID, MKey);
    Sleep(200);
    Edit2.Text := '';
  end;
end;

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

procedure TfrmSQSMCK.CPNAMEChange(Sender: TObject);
var
  FSJ2, FSJ3: string;
begin
  Application.MessageBox('若需要区分机台号或批号，请选择！', '提示', 0);

  with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from fhsq_SUB WHERE FSID=' + QuotedStr(Trim(CKOrdNo.Text)));
    SQL.Add('AND P_NAME=' + QuotedStr(Trim(CPNAME.Text)));
//    ShowMessage(SQL.Text);
    Open;
  end;
  if ADOQueryTemp.IsEmpty = FALSE then
  begin
    FSUBID := ADOQueryTemp.FIELDBYNAME('SSID').AsString;

  end;

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

  SInitComBoxBySql(ADOQueryCmd, CARNO, False, FSJ2);

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

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select *,SYPS=(SQPS-ISNULL(FHPS,0)) ');
//    sql.Add(',YCKPS=(SELLECT COUNT(MJID) FROM CK_PBCP_CR B INNER JOIN WFB_MJJY_PB C ON B.MJID=C.MJID WHERE C.NEWNAME=A.P_NAME AND B.CKORDERNO=C.FSID)');
    sql.Add(' from fhsq_sub c ');

    sql.Add(' where fsid=''' + Trim(CKOrdNo.Text) + '''');
    if CPNAME.Text <> '' then
    begin
      SQL.Add('and p_name=' + QuotedStr(Trim(CPNAME.Text)));
    end;

    Open;
  end;

  Edit1.Text := ADOQueryTemp.fieldbyname('SYPS').asstring;
  CKPSQty.Text := ADOQueryTemp.fieldbyname('sqps').asstring;
  CPPRICE.Text := ADOQueryTemp.fieldbyname('sqprice').asstring;
  if ADOQueryTemp.FIELDBYNAME('FHKHPM').AsString <> '' then
  begin
    SELF.KHPM.Text := ADOQueryTemp.FIELDBYNAME('FHKHPM').AsString;
  end
  else
  begin
    KHPM.Text := CPNAME.Text;
  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)));
    Open;
  end;

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

end;

procedure TfrmSQSMCK.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 TfrmSQSMCK.Button6Click(Sender: TObject);
var
  fPrintFile: string;
begin

  if CKOrdNo.Text = '' then
    EXIT;
  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_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) = '无抬头分机台') 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 shouhuotaitou,KHNAME2=ISNULL(KHNAME,CustomerNoName),b.CRTime,c.note from WFB_MJJY_PB a  inner join CK_PBCP_CR b on a.mjid=b.mjid    ');
      sql.Add(' left join fhsq_main c on b.CKOrdNo=c.fsid  where CKOrdNo=' + quotedstr(Trim(CKOrdNo.Text)));
      Open;
    end;

    if Trim(ADOQueryTemp.fieldbyname('shouhuotaitou').AsString) = '' then
    begin
      RMVariables['shouhuotaitou'] := Trim(ADOQueryTemp.fieldbyname('khname2').AsString);
    end
    else
    begin
      RMVariables['shouhuotaitou'] := Trim(ADOQueryTemp.fieldbyname('shouhuotaitou').AsString);
    end;
    RM1.PreviewButtons := [pbZoom, pbLoad, pbSave, pbPrint, pbFind, pbPageSetup, pbExit, pbSaveToXLS, pbExport, pbNavigator];
//    RMVariables['FHTaiTou'] := Trim(CDS_Main.fieldbyname('FHTT').AsString);
    RMVariables['MJDATE'] := Trim(ADOQueryTemp.fieldbyname('CRTime').AsString);

    RMVariables['Znote'] := Trim(ADOQueryTemp.fieldbyname('note').AsString);
    RMVariables['FILLER'] := Trim(DNAME);

    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
  end;
end;

procedure TfrmSQSMCK.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 TfrmSQSMCK.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 TfrmSQSMCK.edtIsSMTHClick(Sender: TObject);
begin
//  if Trim(edtIsSMTH.Text) = '' then
//  begin
//    edtIsSMTH.Text := '√';
//  end
//  else
//  begin
//    edtIsSMTH.Text := '';
//  end;

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

procedure TfrmSQSMCK.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(',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(CKOrdNo.Text) + ' 条码：' + 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(CKOrdNo.Text) + '''');
            ExecSQL;
          end;
        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);
    if CDS_MainSel.IsEmpty = False then
    begin
      Edit1.Text := IntToStr(StrToInt(CKPSQty.TEXT) - Tv3.DataController.Summary.FooterSummaryValues[2]);
    end
    else
    begin
      Edit1.Text := CKPSQty.Text;
    end;
    CDS_MainSel.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;

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

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

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

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

procedure TfrmSQSMCK.N2Click(Sender: TObject);
begin
  Panel1.Visible := True;
end;

procedure TfrmSQSMCK.Button5Click(Sender: TObject);
begin
  Panel1.Visible := False;
end;

procedure TfrmSQSMCK.Button4Click(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=' + Edit3.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;

end.

