unit U_SQSMCKXH;

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

type
  TfrmSQSMCKXH = class(TForm)
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    DS_MainSel: TDataSource;
    CDS_MainSel: TClientDataSet;
    cxGridPopupMenu4: TcxGridPopupMenu;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGridDBColumn4: TcxGridDBColumn;
    v3Column1: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    v3Column2: TcxGridDBColumn;
    Panel2: TPanel;
    Label5: TLabel;
    Label4: TLabel;
    Label9: TLabel;
    CKPSQty: TEdit;
    CKOrdNo: TEdit;
    BaoID: TEdit;
    CRTime: TDateTimePicker;
    KHName: TEdit;
    Button2: TButton;
    Label6: TLabel;
    PrtCode: TEdit;
    Label8: TLabel;
    PrtCodeName: TEdit;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    Label2: TLabel;
    Label3: TLabel;
    Edit1: TEdit;
    Label12: TLabel;
    CDS_Main: TClientDataSet;
    CRType: TEdit;
    Button1: TButton;
    Label1: TLabel;
    BatchNo: TEdit;
    ComboBox1: TComboBox;
    Button3: TButton;
    RMXLSExport1: TRMXLSExport;
    RMDBMX: TRMDBDataSet;
    RM1: TRMGridReport;
    CDS_JuanPRT: TClientDataSet;
    Label10: TLabel;
    edtIsSMTH: TEdit;
    cv3Column3: TcxGridDBColumn;
    btn1: TButton;
    lbl2: TLabel;
    cxGrid1: TcxGrid;
    TV4: TcxGridDBTableView;
    cCXCONNO: TcxGridDBColumn;
    cCXCOLOR: TcxGridDBColumn;
    cCXSQPS: TcxGridDBColumn;
    cCXFHPS: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    ds1: TClientDataSet;
    ds2: TDataSource;
    cv3Column4: TcxGridDBColumn;
    cV4Column1: 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 edtIsSMTHClick(Sender: TObject);
    procedure btn1Click(Sender: TObject);
  private
    { Private declarations }
    FCKOrdNo, FSMStr, JXFlag, CZZT, fcuowu: string;
    procedure TMPanDuan();
    procedure InitCKGrid();
  public
    { Public declarations }
  end;

var
  frmSQSMCKXH: TfrmSQSMCKXH;

implementation

uses
  U_DataLink, U_Fun, U_RTFun, MMSystem, U_FHSQListSel, U_CKProductBCPKCListSel;

{$R *.dfm}

function TfrmSQSMCKXH.YSData(Order_Main10: TClientDataSet): Boolean;
var
  CRID, YFID, PriceUnit, OrderUnit, FComTaiTou, FromSubID: string;
  fprice, huilv: double;
begin
//  Result:=False;
//  fprice:=0;
//  huilv:=0;
//  PriceUnit:='RMB';
//  with Order_Main10 do
//  begin
//    begin
//      with ADOQueryTemp do
//      begin
//        Close;
//        SQL.Clear;
//        sql.Add('select *,fPrice=dbo.F_Get_Order_Price(MainID,subID) from JYOrder_Sub Where subID='''+Trim(Order_Main10.fieldbyname('subID').AsString)+'''');
//        Open;
//      end;
//      IF not ADOQueryTemp.IsEmpty then
//      begin
//        PriceUnit:=Trim(ADOQueryTemp.fieldbyname('PriceUnit').AsString);
//        OrderUnit:=Trim(ADOQueryTemp.fieldbyname('OrderUnit').AsString);
//        fPrice:= ADOQueryTemp.fieldbyname('fPrice').AsFloat;
//        huilv:= ADOQueryTemp.fieldbyname('huilv').AsFloat;
//      end;
//      IF huilv=0 then huilv:=1;
//      CRID:='0';
//      with ADOQueryTemp do
//      begin
//        Close;
//        sql.Clear;
//        sql.Add(' select * from YF_Money_CR ');
//        sql.Add(' where mainID='''+Trim(Order_Main10.fieldbyname('mainID').AsString)+'''');
//        sql.Add(' and subID='''+Trim(Order_Main10.fieldbyname('subID').AsString)+'''');
//        sql.Add(' and CRTime='''+FormatDateTime('yyyy-MM-dd',CRTime.Date)+'''') ;
//        sql.Add(' and YFName=''销售金额'' ');
//        Open;
//      end;
//      if ADOQueryTemp.IsEmpty then
//      begin
//        if GetLSNo(ADOQueryCmd,YFID,'CS','YF_Money_CR',3,1)=False then
//        begin
//          Application.MessageBox('取成品应收最大号失败!','提示',0);
//          Exit;
//        end;
//        with ADOQueryCmd do
//        begin
//          Close;
//          sql.Clear;
//          sql.Add('select * from YF_Money_CR where 1<>1');
//          Open;
//        end;
//        with ADOQueryCmd do
//        begin
//          Append;
//          FieldByName('YFID').Value:=Trim(YFID);
//          FieldByName('YFTypeId').Value:=Trim(Order_Main10.fieldbyname('mainID').AsString);
//          FieldByName('CRID').Value:=StrToInt(CRID);
//          FieldByName('Filler').Value:=Trim(DName);
//          FieldByName('CRType').Value:='应收款登记';
//          FieldByName('CRFlag').Value:='应收收';
//          FieldByName('QtyFlag').Value:=1;
//          FieldByName('FactoryName').Value:=Trim(TocustName.Text);
//          FieldByName('CRTime').Value:=Trim(FormatDateTime('yyyy-MM-dd',CRTime.Date));
//          FieldByName('YFType').Value:='自动生成';
//          FieldByName('Price').Value:=fprice;
//          FieldByName('HuiLv').Value:=huilv;
//          FieldByName('BZType').Value:=Trim(PriceUnit);
//          FieldByName('QtyUnit').Value:=Trim(OrderUnit);
//          FieldByName('ComTaiTou').Value:=Trim(FComTaiTou);
//          FieldByName('YFName').Value:='销售金额';
//          FieldByName('MainId').Value:=Trim(Order_Main10.fieldbyname('Mainid').AsString);
//          FieldByName('subID').Value:=Trim(Order_Main10.fieldbyname('subID').AsString);
//          FieldByName('OrderNO').Value:=Trim(Order_Main10.fieldbyname('OrderNO').AsString);
//          FieldByName('P_CodeName').Value:=Trim(Order_Main10.fieldbyname('MprtCodeName').AsString);
//          FieldByName('P_Spec').Value:=Trim(Order_Main10.fieldbyname('MprtSpec').AsString);
//          FieldByName('P_color').Value:=Trim(Order_Main10.fieldbyname('PRTColor').AsString);
//          FieldByName('status').Value:='0';
//          Post;
//        end;
//      end
//      else
//      begin
//        YFID:=Trim(ADOQueryTemp.fieldbyname('YFID').AsString);
//      end;
//      IF uppercase(OrderUnit)='KG' then
//      begin
//        with ADOQueryCmd do
//        begin
//          Close;
//          sql.Clear;
//          sql.Add('update YF_Money_CR Set Qty=(select isnull(Sum(B.MJQty4),0) from CK_BanCP_CR A,WFB_MJJY B where A.MJID=B.MJID and  A.MainID=YF_Money_CR.MainID and A.subID=YF_Money_CR.subID and A.CRtime=YF_Money_CR.CRTime and A.CRType=''销售出库'') ');
//          sql.Add(',PS=(select isnull(count(A.mainID),0) from CK_BanCP_CR A where A.maiNID=YF_Money_CR.maiNID and A.subID=YF_Money_CR.subID and A.CRtime=YF_Money_CR.CRTime and A.CRType=''销售出库'')  ');
//          sql.Add(' where YFID='''+Trim(YFID)+'''');
//          ExecSQL;
//        end;
//      end
//      else
//      IF (uppercase(OrderUnit)='Y') and (uppercase(Trim(Order_Main10.fieldbyname('QtyUnit').AsString))='M')  then
//      begin
//        with ADOQueryCmd do
//        begin
//          Close;
//          sql.Clear;
//          sql.Add('update YF_Money_CR Set Qty=(select cast(isnull(Sum(A.Qty),0)/0.9144 as decimal(18,1)) from CK_BanCP_CR A where A.MainID=YF_Money_CR.MainID and A.subID=YF_Money_CR.subID and A.CRtime=YF_Money_CR.CRTime and A.CRType=''销售出库'') ');
//          sql.Add(',PS=(select isnull(count(A.mainID),0) from CK_BanCP_CR A where A.maiNID=YF_Money_CR.maiNID and A.subID=YF_Money_CR.subID and A.CRtime=YF_Money_CR.CRTime and A.CRType=''销售出库'')  ');
//          sql.Add(' where YFID='''+Trim(YFID)+'''');
//          ExecSQL;
//        end;
//      end
//      else
//      IF (uppercase(OrderUnit)='M') and (uppercase(Trim(Order_Main10.fieldbyname('QtyUnit').AsString))='Y')  then
//      begin
//        with ADOQueryCmd do
//        begin
//          Close;
//          sql.Clear;
//          sql.Add('update YF_Money_CR Set Qty=(select cast(isnull(Sum(A.Qty),0)*0.9144 as decimal(18,1)) from CK_BanCP_CR A where A.MainID=YF_Money_CR.MainID and A.subID=YF_Money_CR.subID and A.CRtime=YF_Money_CR.CRTime and A.CRType=''销售出库'') ');
//          sql.Add(',PS=(select isnull(count(A.mainID),0) from CK_BanCP_CR A where A.maiNID=YF_Money_CR.maiNID and A.subID=YF_Money_CR.subID and A.CRtime=YF_Money_CR.CRTime and A.CRType=''销售出库'')  ');
//          sql.Add(' where YFID='''+Trim(YFID)+'''');
//          ExecSQL;
//        end;
//      end
//      else
//      begin
//        with ADOQueryCmd do
//        begin
//          Close;
//          sql.Clear;
//          sql.Add('update YF_Money_CR Set Qty=(select isnull(Sum(A.Qty),0) from CK_BanCP_CR A where A.MainID=YF_Money_CR.MainID and A.subID=YF_Money_CR.subID and A.CRtime=YF_Money_CR.CRTime and A.CRType=''销售出库'') ');
//          sql.Add(',PS=(select isnull(count(A.mainID),0) from CK_BanCP_CR A where A.maiNID=YF_Money_CR.maiNID and A.subID=YF_Money_CR.subID and A.CRtime=YF_Money_CR.CRTime and A.CRType=''销售出库'')  ');
//          sql.Add(' where YFID='''+Trim(YFID)+'''');
//          ExecSQL;
//        end;
//      end;
//      with ADOQueryCmd do
//      begin
//        Close;
//        sql.Clear;
//        sql.Add('Update YF_Money_CR Set Money=Price*Qty,BBMoney=Price*Qty*HuiLv');
//        sql.Add(' where YFID='''+Trim(YFID)+'''');
//        ExecSQL;
//      end;
//      with ADOQueryTemp do
//      begin
//        Close;
//        sql.Clear;
//        sql.Add('select * from YF_Money_CR where YFId='''+Trim(YFID)+'''');
//        Open;
//      end;
//      if ADOQueryTemp.FieldByName('Qty').Value=0 then
//      begin
//        with ADOQueryCmd do
//        begin
//          Close;
//          sql.Clear;
//          sql.Add('delete from YF_Money_CR where YFID='''+Trim(YFID)+'''');
//          ExecSQL;
//        end;
//      end;
//      //Next;
//    end;
//  end;
//  Result:=True;
end;

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

procedure TfrmSQSMCKXH.FormDestroy(Sender: TObject);
begin
  frmSQSMCKXH := nil;
end;

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

procedure TfrmSQSMCKXH.TMPanDuan();
begin
  //判断扫描的是发货申请单，还是布匹条码 ，判断是否已经扫描过发货单 ，或者更改了发货单
  JXFlag := '';

  if Copy(FSMStr, 1, 2) = 'SM' then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from FHSQ_Main A inner join FHSQ_Sub B on A.FSID=B.FSID where A.FSID=''' + Trim(FSMStr) + '''');
      Open;
    end;
    if ADOQueryTemp.IsEmpty then
    begin
      JXFlag := '999';
      PlaySound('申请单不存在暂停发货.wav', 0, SND_FILENAME or SND_ASYNC);
      Exit;
    end;
    with ADOQueryTemp do
    begin
      close;
      sql.Clear;
      sql.Add('select * from FHSQ_Main');
      sql.Add('where FSID=' + quotedstr(trim(FSMStr)));
      Open;
    end;
    if Trim(ADOQueryTemp.fieldbyname('YSID').AsString) <> '' 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
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select B.*');
        sql.Add(' from CK_BanCP_CR A');
        sql.Add(' inner join WFB_MJJY B on A.MJID=B.MJID');
        sql.Add(' where A.CKOrdNO=''' + Trim(CKOrdNo.Text) + '''');
        Open;
      end;
      SCreateCDS20(ADOQueryTemp, CDS_MainSel);
      SInitCDSData20(ADOQueryTemp, CDS_MainSel);
      if CDS_MainSel.IsEmpty = False then
        Edit1.Text := floatToStr(Tv3.DataController.Summary.FooterSummaryValues[3])
//        Edit1.Text := floatToStr(Format('%.2f', Tv3.DataController.Summary.FooterSummaryValues[3]))
      else
        Edit1.Text := '0';
    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;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from WFB_MJJY A   ');
      sql.Add('where exists (select Y.SubId from  FHSQ_Sub Y   where Y.SubId=A.SubId and Y.FSId=' + quotedstr(Trim(CKOrdNo.Text)) + ')');
      sql.Add('and  A.MJID=' + quotedstr(Trim(FSMStr)));
//      ShowMessage(SQL.Text);

      Open;
    end;
    if ADOQueryTemp.IsEmpty then
    begin
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select A.APID from WFB_MJJY A   ');
        sql.Add('where  A.MJID=' + quotedstr(Trim(FSMStr)));
//        ShowMessage(SQL.Text);
        Open;
      end;
      if ADOQueryTemp.IsEmpty then
      begin
        JXFlag := '999';
        PlaySound('wav\未检验暂停发货.wav', 0, SND_FILENAME or SND_ASYNC);
        Exit;
      end
      else
      begin
        JXFlag := '999';
        Application.MessageBox('请检查指示单是否正确!', '提示', 0);
        PlaySound('wav\条码错误暂停发货.wav', 0, SND_FILENAME or SND_ASYNC);
        Exit;
      end;
    end;
//    else
//    begin
////      if Trim(BatchNo.Text) <> '' then
////      begin
////        if FormatTitle(Trim(ADOQueryTemp.fieldbyname('APBatchNo').AsString)) <> FormatTitle(Trim(BatchNo.Text)) then
////        begin
////          JXFlag := '999';
////          PlaySound('批号不同暂停发货.wav', 0, SND_FILENAME or SND_ASYNC);
////          Exit;
////        end;
////      end;
////      if ((ADOQueryTemp.fieldbyname('MJMaoZ').AsFloat < 5) or (ADOQueryTemp.fieldbyname('MJMaoZ').AsFloat > 50)) then
////      begin
////        JXFlag := '999';
////        PlaySound('重量异常暂停发货.wav', 0, SND_FILENAME or SND_ASYNC);
////        Application.MessageBox('重量异常暂停发货!', '提示', 0);
////        Exit;
////      end;
//
//    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;

      sql.Add('select A.CKOrdNo from CK_BanCP_CR A   ');
      sql.Add('where exists (select Y.SubId from  FHSQ_Sub Y where Y.SubId=A.SubId and Y.FSId=' + quotedstr(Trim(CKOrdNo.Text)) + ')');
      sql.Add(' and  A.MJID=' + quotedstr(Trim(FSMStr)));
      sql.Add(' and  A.CRFlag=''出库'' ');
      Open;
    end;
    if not ADOQueryTemp.IsEmpty 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_BanCP_CR A   ');
      sql.Add('where exists (select Y.SubId from  FHSQ_Sub Y where Y.SubId=A.SubId 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;
    if Trim(Edit1.Text) <> '' then
    begin
      if StrToFloat(Edit1.Text) >= StrToFloat(CKPSQty.Text) then
      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 * from FHSQ_Sub A  where isnull(SQQty,0)+isnull(fdfw,0)<isnull(FHQty,0)   ');
//
//      sql.Add(' and  A.FSID=' + quotedstr(Trim(FSMStr)));
//
//      Open;
//    end;
//    if ADOQueryTemp.IsEmpty = False then
//    begin
//      JXFlag := '999';
//      PlaySound('wav\超出发货上限暂停发货.wav', 0, SND_FILENAME or SND_ASYNC);
//      Exit;
//    end;

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

procedure TfrmSQSMCKXH.BaoIDKeyPress(Sender: TObject; var Key: Char);
var
  maxno, SMFlag, KYCKFlag, FMainId, ff: string;
  FFQty: Integer;
begin
  if Key = #13 then
  begin
    FSMStr := Trim(BaoID.Text);

    if edtIsSMTH.Text = '' then
    begin

      TMPanDuan();
      if Trim(JXFlag) = '999' then
      begin
        BaoID.Text := '';
        fcuowu := '错误';
        Exit;
      end;
      if Trim(JXFlag) = '888' then
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select A.* ,KHName=CustomerNoName,CKPSQty=A.SQQTYHZ ');    //0720
          sql.Add(' from FHSQ_Main A');

          sql.Add(' where FSId=''' + Trim(CKOrdNo.Text) + ''' ');
          Open;
        end;

        SCSHDataNew(ADOQueryTemp, Panel2, 2);
        InitCKGrid();

        Exit;
      end;

      with ADOQueryTemp do
      begin
        close;
        sql.Clear;
        sql.Add(' select A.*,B.CRID ,C.PRTColor  ');
        sql.Add(' from WFB_MJJY A');
        sql.Add(' inner join CK_BanCP_CR B on A.MJID=B.MJID and B.CRFlag=''入库'' ');
        SQL.Add('     inner join  JYOrder_Sub C ON A.SUBID=C.SubId');
        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_BanCP_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('UPDATE CK_BanCP_CR Set  ');
            sql.add(' CRTime=getdate(),CRType=''扫描出库'',CRFlag=''出库''  ');
            sql.add(',CKOrdNo=' + quotedstr(Trim(CKOrdNo.Text)));
            sql.add(',filler=' + quotedstr(DName));
            sql.Add(' where MJID=' + quotedstr(Trim(FSMStr)));
            sql.Add('and CK_BanCP_CR.CRFlag=''待出库''  ');
//            ShowMessage(SQL.Text);
            ExecSQL;
          end;
          //更新kcvalid状态
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('UPDATE CK_BanCP_KC Set  ');
            sql.add(' KCValid=''N''  ');
            sql.Add(' where MJID=' + quotedstr(Trim(FSMStr)));
            ExecSQL;
          end;
          //更新出入库状态
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('UPDATE WFB_MJJY Set  MJStr2=''已出库''  ');
            sql.Add(' where MJID=' + quotedstr(Trim(FSMStr)));
            SQL.Add(' and WFB_MJJY.MJStr2=''已入库'' ');
            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;
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;

            sql.Add(' select * from FHSQ_Sub A  where isnull(SQQty,0)*(1+0.01*isnull(fdfw,0))<isnull(FHQty,0)   ');

            sql.Add(' and  A.FSID=' + quotedstr(Trim(FSMStr)));

            Open;
          end;
          if ADOQueryTemp.IsEmpty = False then
          begin
            BaoID.Text := '';
            PlaySound('wav\超出发货上限暂停发货.wav', 0, SND_FILENAME or SND_ASYNC);
            ADOQueryCmd.Connection.RollbackTrans;

            Exit;
          end;
          ADOQueryCmd.Connection.CommitTrans;
          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('MJXH').Value := Self.CDS_Main.fieldbyname('MJXH').Value;
            FieldByName('mjstr4').Value := Self.CDS_Main.fieldbyname('mjstr4').Value;
            FieldByName('prtcolor').Value := Self.CDS_Main.fieldbyname('prtcolor').Value;
//            FieldByName('CarNo').Value := Self.CDS_Main.fieldbyname('CarNo').Value;
//            FieldByName('APBatchNo').Value := Self.CDS_Main.fieldbyname('APBatchNo').Value;
            Post;
          end;

          BaoID.Text := '';
          PlaySound('正确.wav', 0, SND_FILENAME or SND_ASYNC);
        end;
      except
        BaoID.Text := '';
        PlaySound('数据保存失败暂停发货.wav', 0, SND_FILENAME or SND_ASYNC);
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('出库异常！', '提示', 0);
      end;
    end
    else
    begin
      if not CDS_MainSel.Locate('MJID', FSMStr, []) then
        Exit;
      try

        ADOQueryCmd.Connection.BeginTrans;

        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPDATE CK_BanCP_CR Set  ');
          sql.add(' CRTime=null,CRType=null,CRFlag=''待出库''  ');
          sql.add(',CKOrdNo=''''');
          sql.add(',filler=''''');
          sql.Add(' where MJID=' + quotedstr(Trim(FSMStr)));
          sql.Add('and CK_BanCP_CR.CRFlag=''出库''  ');
//          ShowMessage(SQL.Text);
          ExecSQL;
        end;

          //更新出入库状态
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPDATE WFB_MJJY Set  MJStr2=''已入库''  ');
          sql.Add(' where MJID=' + quotedstr(Trim(FSMStr)));
          SQL.Add(' and WFB_MJJY.MJStr2=''已出库'' ');

          sql.Add(' UPdate CK_BanCP_KC Set KCValid=''Y'' ');
          sql.Add(' where  CK_BanCP_KC.MJID=' + Quotedstr(Trim(FSMStr)));
          sql.Add(' and KCValid=''N'' ');
          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(FSMStr) + ' 出库单号：' + trim(CKOrdNo.Text)));
          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;
        ADOQueryCmd.Connection.CommitTrans;
      except
        ADOQueryCmd.Connection.RollbackTrans;
        application.MessageBox('撤销出库失败！', '提示信息', 0);
      end;
      CDS_MainSel.Locate('MJID', FSMStr, []);
      CDS_MainSel.Delete;
      BaoID.Text := '';
      PlaySound('正确.wav', 0, SND_FILENAME or SND_ASYNC);
    end;
  end;
    
    //判断发货匹数                    CDS_MainSel
//  if CDS_MainSel.IsEmpty = False then
//    Edit1.Text := IntToStr(Tv3.DataController.Summary.FooterSummaryValues[2])
//  else
//    Edit1.Text := '0';
  if CDS_MainSel.IsEmpty = False then
    Edit1.Text := floatToStr(Tv3.DataController.Summary.FooterSummaryValues[3])
  else
    Edit1.Text := '0';

//  with ADOQueryTemp do
//  begin
//    Close;
//    sql.Clear;
//    sql.Add(' select COUNT(*) fhjs from CK_BanCP_CR where CKOrdNo= ''' + Trim(CKOrdNo.Text) + '''');
//    Open;
//  end;
//  ff := Format('%.2f', [StrToFloat(Edit1.Text)]);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select sum(KGQTY) FMJMaoZ from CK_BanCP_CR where CKOrdNo= ''' + Trim(CKOrdNo.Text) + '''');
    Open;
  end;

//  if StrToFloat(Edit1.Text) <> ADOQueryTemp.FieldByName('FMJMaoZ').AsFloat then  //asfloat
  if Tv3.DataController.Summary.FooterSummaryValues[3] <> ADOQueryTemp.FieldByName('FMJMaoZ').AsFloat then
  begin
    PlaySound('数据保存失败暂停发货.wav', 0, SND_FILENAME or SND_ASYNC);
    Application.MessageBox('出库异常,请重新选择申请单！', '提示', 0);
  end;

end;

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

procedure TfrmSQSMCKXH.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 TfrmSQSMCKXH.Button3Click(Sender: TObject);
var
  fPrintFile: string;
begin
  if Trim(CKOrdNo.Text) = '' then
    EXIT;
  ExportFtErpFile(trim(ComboBox1.Text) + '.rmf', ADOQueryTemp);
  if ComboBox1.Text = '公斤码单' then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_RKMDGangNo2 ' + 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_RKMDGangNo2 ' + quotedstr(Trim(CKOrdNo.Text)));
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_JuanPRT);
    SInitCDSData20(ADOQueryTemp, CDS_JuanPRT);
  end;
  if ComboBox1.Text = '公斤与换算码单' then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_RKMDGangNo5 ' + 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
//    RMVariables['OrdNo'] := Trim(CDS_Main.fieldbyname('OrdNo').AsString)；
    RM1.DefaultCopies := 2;
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
  end;
end;

procedure TfrmSQSMCKXH.edtIsSMTHClick(Sender: TObject);
begin
  if Trim(edtIsSMTH.Text) = '' then
    edtIsSMTH.Text := '√'
  else
    edtIsSMTH.Text := '';
end;

procedure TfrmSQSMCKXH.btn1Click(Sender: TObject);
var
  MKey: Char;
begin
  MKey := #13;
  CZZT := '选择库存';
  fcuowu := '1';
  try
    frmCKProductBCPKCListSel := TfrmCKProductBCPKCListSel.Create(Application);
    with frmCKProductBCPKCListSel do
    begin
//      CustomerNoName.text := trim(Self.KHName.text);
      if ShowModal = 1 then
      begin
        with frmCKProductBCPKCListSel.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);
              if fcuowu = '错误' then
              begin
                Break;
              end;
              Sleep(200);
            end;
            Next;
          end;
        end;
      end;
    end;
  finally
    frmCKProductBCPKCListSel.Free;
  end;
  InitCKGrid();
end;

procedure TfrmSQSMCKXH.InitCKGrid();
begin
  if CKOrdNo.Text <> '' then
  begin
    with ADOQueryTemp do
    begin

      Close;
      sql.Clear;
      sql.Add('select A.MAINID,A.SUBID, B.MJMaoZ,B.MJStr4,B.MJXH,B.MJID,C.PRTColor');
      sql.Add(' from CK_BanCP_CR A');
      sql.Add(' inner join WFB_MJJY B on A.MJID=B.MJID');
      SQL.Add(' inner join JYOrder_Sub C ON B.SUBID=C.SubId');
      sql.Add(' where A.CKOrdNO=''' + Trim(CKOrdNo.Text) + '''');
//      ShowMessage(SQL.Text);
      Open;

    end;
    SCreateCDS20(ADOQueryTemp, CDS_MainSel);
    SInitCDSData20(ADOQueryTemp, CDS_MainSel);
    with ADOQueryTemp do
    begin

      Close;
      sql.Clear;
      sql.Add('SELECT b.*,A.ConNo FROM FHSQ_Sub B');
      sql.Add('inner join FHSQ_Main  A ON A.FSId=B.FSId ');
      sql.Add('WHERE B.FSId=''' + Trim(CKOrdNo.Text) + '''');


//      ShowMessage(SQL.Text);
      Open;

    end;
    SCreateCDS20(ADOQueryTemp, DS1);
    SInitCDSData20(ADOQueryTemp, DS1);
  end;

end;

end.

