unit U_SMCK;

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,
  cxLookAndFeels, cxLookAndFeelPainters,  
  cxNavigator, dxDateRanges, dxBarBuiltInMenu, U_BaseList, 
      
    
     
     
     
    
    
    
    
    
     
     
     
    
    
     dxScrollbarAnnotations,
  cxDropDownEdit, StrUtils, cxContainer, cxProgressBar;

type
  TfrmSMCK = class(TfrmBaseList)
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    DS_MainSel: TDataSource;
    CDS_MainSel: TClientDataSet;
    GPM_1: TcxGridPopupMenu;
    Panel2: TPanel;
    Label5: TLabel;
    Label4: TLabel;
    Label9: TLabel;
    edtScan: TEdit;
    CRTime: TDateTimePicker;
    ToCoName: TEdit;
    Button2: TButton;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    Label12: TLabel;
    CDS_Main: TClientDataSet;
    CRType: TEdit;
    Button1: TButton;
    RMXLSExport1: TRMXLSExport;
    RMDBMX: TRMDBDataSet;
    RM1: TRMGridReport;
    CDS_JuanPRT: TClientDataSet;
    Label10: TLabel;
    edtIsSMTH: TEdit;
    btn1: TButton;
    ds1: TClientDataSet;
    ds2: TDataSource;
    Panel1: TPanel;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    VC_MJXH: TcxGridDBColumn;
    v3Column2: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    OutNo: TEdit;
    GPM_2: TcxGridPopupMenu;
    Tv3Column2: TcxGridDBColumn;
    Tv3Column3: TcxGridDBColumn;
    Tv3Column1: TcxGridDBColumn;
    Tv3Column4: TcxGridDBColumn;
    Button3: TButton;
    Button4: TButton;
    Tv3Column5: TcxGridDBColumn;
    Tv3Column6: TcxGridDBColumn;
    Tv3Column7: TcxGridDBColumn;
    Tv3Column8: TcxGridDBColumn;
    Tv3Column9: TcxGridDBColumn;
    Tv3Column10: TcxGridDBColumn;
    Tv3Column11: TcxGridDBColumn;
    Button5: TButton;
    ToCoNo: TEdit;
    cxGrid2: TcxGrid;
    Tv4: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    Label1: TLabel;
    OrderNo: TEdit;
    Label2: TLabel;
    Tv4Column1: TcxGridDBColumn;
    Tv3Column12: TcxGridDBColumn;
    Tv3Column13: TcxGridDBColumn;
    PM_1: TPopupMenu;
    PM11: TMenuItem;
    N1: TMenuItem;
    N2: TMenuItem;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure edtScanKeyPress(Sender: TObject; var Key: Char);
    procedure Button2Click(Sender: TObject);
    procedure edtIsSMTHClick(Sender: TObject);
    procedure btn1Click(Sender: TObject);
    procedure TV4MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure PM11Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
  private
    { Private declarations }
    FCKNO, FSMStr, JXFlag, CZZT, fcuowu, FOutNo: string;
    procedure InitCKGrid();
    procedure InitSQGrid();
    procedure PlayWav(MWav: string);
    function JudgeSQD(): Boolean;
    function SaveCKJTM(MStkIds: string): Boolean;//保存出库
    procedure SaveCKJTMCX();   //撤销出库
  public
    { Public declarations }
  end;

var
  frmSMCK: TfrmSMCK;

implementation

uses
  U_DataLink, U_RTFun, MMSystem, U_CompanySel, U_KCList_Sel, U_YarnDRList_Sel;

procedure TfrmSMCK.PlayWav(MWav: string);
begin
  if FileExists(PChar(ExtractFilePath(Application.ExeName) + 'wav\' + MWav + '.wav')) then
    PlaySound(PChar(ExtractFilePath(Application.ExeName) + 'wav\' + MWav + '.wav'), 0, SND_ASYNC);
end;

procedure TfrmSMCK.PM11Click(Sender: TObject);
begin
  SelOKNo(CDS_MainSel, true);
end;

procedure TfrmSMCK.SaveCKJTMCX();
begin

  try
    if Trim(OutNo.Text) = '' then
      raise Exception.Create('出库单号不能为空!');

    if CDS_MainSel.IsEmpty then
      raise Exception.Create('无可撤销的数据!');
    try
      ADOQueryCmd.Connection.BeginTrans;
      with ADOQueryCmd do
      begin
        Close;
        Sql.Clear;
        sql.Add('insert into Finance_Need_Up(UType,UDataId) values(''贸易成品出库'',' + quotedstr(OutNo.Text) + ')  ');

        Sql.Add('exec P_Knit_Cloth_ReOut ');
        Sql.Add(' @StkIds=' + quotedstr(trim(FSMStr)));
        Sql.Add(',@IOIDS=' + quotedstr(trim('')));
        Open;
      end;
      if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
      begin
        raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
      end;
      ADOQueryCmd.Connection.CommitTrans;
      PlayWav('正确');

    except
      ADOQueryCmd.Connection.RollbackTrans;
      raise Exception.Create(PChar(Exception(ExceptObject).Message));
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_Knit_Cloth_DR @OutNo=' + Quotedstr(Trim(OutNo.Text)));
      Open;
    end;
    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
    begin
      raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
    end;
  except

    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
//    PlayWav('发货错误');
  end;

end;

procedure TfrmSMCK.TV4MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  inherited;

  InitCKGrid();
end;

function TfrmSMCK.SaveCKJTM(MStkIds: string): Boolean;
begin
  try
    if Trim(OutNo.Text) = '' then
    begin
      PlayWav('出库单号不能为空');
      raise Exception.Create('出库单号不能为空!');
    end;

    if Trim(ToCoName.Text) = '' then
    begin
      PlayWav('收货单位不能为空');
      raise Exception.Create('收货单位不能为空!');
    end;

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add(' select * from  Knit_Plan_Card    ');
      sql.Add(' where PCID=''' + Trim(MStkIds) + '''');
      Open;
    end;
    if ADOQueryTemp.IsEmpty then
    begin
      edtScan.text := '';
      PlayWav('布票号不存在');
      raise Exception.Create('布票号不存在');
    end;

    if Trim(ADOQueryTemp.fieldbyname('status').AsString) = '-1' then
    begin
      edtScan.text := '';
      PlayWav('布票已删除');
      raise Exception.Create('布票已删除');
    end;

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add(' select A.* from  Knit_Cloth_Inspect A where A.CIIOFlag <>''未入库''   ');
      sql.Add(' and  A.PCID=''' + Trim(MStkIds) + '''');
//      showmessage(sql.text);
      Open;
    end;

    if ADOQueryTemp.IsEmpty then
    begin
      edtScan.text := '';
      PlayWav('未入库');
      edtScan.SetFocus;
      Exit;
    end;

    if Trim(ADOQueryTemp.fieldbyname('CIIOFlag').AsString) = '已出库' then
    begin
      edtScan.text := '';
      PlayWav('已出库');
      edtScan.SetFocus;
      Exit;
    end;

    MStkIds := Trim(ADOQueryTemp.fieldbyname('CIID').AsString);

    try
      ADOQueryCmd.Connection.BeginTrans;

      with ADOQueryCmd do
      begin
        Close;
        Sql.Clear;
        sql.Add('insert into Finance_Need_Up(UType,UDataId) values(''坯布扫描出库'',' + quotedstr(OutNo.Text) + ')  ');

        Sql.Add('exec P_Knit_Cloth_Out ');
        Sql.Add(' @StkIds=' + quotedstr(trim(MStkIds)));
        Sql.Add(',@IOTime=' + Quotedstr(FormatDateTime('yyyy-MM-dd', CRTime.DateTime)));
        Sql.Add(',@IOType=' + Quotedstr(crtype.Text));
        Sql.Add(',@IONO=' + Quotedstr(OutNo.Text));
        Sql.Add(',@FillID=' + Quotedstr(DCode));
        Sql.Add(',@Filler=' + Quotedstr(DName));
        Sql.Add(',@ToCoName=' + Quotedstr(ToCoName.text));
        Open;
      end;
      if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
      begin
        edtScan.text := '';
        raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
      end;
      ADOQueryCmd.Connection.CommitTrans;

    except
      ADOQueryCmd.Connection.RollbackTrans;
      raise Exception.Create(PChar(Exception(ExceptObject).Message));
    end;

   { with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_Knit_Cloth_DR @OutNos=' + Quotedstr(Trim(OutNo.Text)));
      open;
    end;
    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
    begin
      raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
    end;  }

    PlayWav('正确');
    Result := True;
  except
    Result := False;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
//    PlayWav('发货错误');
  end;

end;

{$R *.dfm}

function TfrmSMCK.JudgeSQD(): Boolean;
begin
  Result := false;
  try
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from Tat_Cloth_DR_Main A inner join Tat_Cloth_DR_Sub B on A.DRMID=B.DRMID where A.DRMID=''' + Trim(FSMStr) + '''');
      Open;
    end;
    if ADOQueryTemp.IsEmpty then
      raise Exception.Create('申请单不存在暂停发货!');

    if Trim(OutNo.Text) <> '' then
    begin
      FCKNO := Trim(OutNo.Text);
    end;
    OutNo.Text := Trim(FSMStr);

    if FSMStr <> FCKNO then
    begin
      InitCKGrid();
      InitSQGrid();
    end;

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

procedure TfrmSMCK.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_MainSel, false);
end;

procedure TfrmSMCK.N2Click(Sender: TObject);
var
  MBYIOIDS: string;
begin
  if CDS_MainSel.IsEmpty then
    Exit;
  if not CDS_MainSel.Locate('SSel', True, []) then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  MBYIOIDS := '';

  CDS_MainSel.DisableControls;
  with CDS_MainSel do
  begin
    First;
    while not Eof do
    begin
      if CDS_MainSel.fieldbyname('SSel').AsBoolean then
      begin
        MBYIOIDS := MBYIOIDS + (Trim(CDS_MainSel.fieldbyname('BYIOID2').AsString)) + ',';
      end;
      Next;
    end;
  end;
  MBYIOIDS := leftBstr(trim(MBYIOIDS), length(MBYIOIDS) - 1);

  CDS_MainSel.Locate('SSel', True, []);
  CDS_MainSel.EnableControls;

  if Application.MessageBox('确定要执行操作吗？', '提示', 32 + 4) <> IDYES then
    Exit;

  try
    ADOQueryCmd.Connection.BeginTrans;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('insert into Finance_Need_Up(UType,UDataId,UOperation,UOperator,UModule,UDetails)       ');
      sql.Add('values(''纱线出库''    ');
      sql.Add(',' + quotedstr(MBYIOIDS));
      sql.Add(',''删除''   ');
      sql.Add(',' + quotedstr(DName));
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(MBYIOIDS));
      sql.Add(')  ');
      execsql;
    end;

    with ADOQueryCmd do
    begin
      Close;
      Sql.Clear;
      Sql.Add('exec P_BS_Yarn_Out_Del ');
      Sql.Add(' @BYIOIDS=' + quotedstr(MBYIOIDS));
      Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
      Sql.Add(',@DName=' + quotedstr(Trim(DName)));
//      ShowMessage(sql.Text);
      Open;
    end;

    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
    begin
      raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_Yarn_DR @OutNo=' + Quotedstr(Trim(OutNo.text)));
      Open;
    end;
    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
    begin
      raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
    end;

    InitSQGrid();
    InitCKGrid();
    ADOQueryCmd.Connection.CommitTrans;

  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

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

procedure TfrmSMCK.FormDestroy(Sender: TObject);
begin
  inherited;
  frmSMCK := nil;
end;

procedure TfrmSMCK.FormShow(Sender: TObject);
begin
  inherited;
  edtScan.SetFocus;
  ReadCxGrid('扫描出库Tv44', Tv4, '发货申请');
  ReadCxGrid('扫描出库Tv33', Tv3, '发货申请');
  CRTime.DateTime := SGetServerDate(ADOQueryTemp);

end;

procedure TfrmSMCK.edtScanKeyPress(Sender: TObject; var Key: Char);
var
  maxno, SMFlag, KYCKFlag, FMainId, ff: string;
  FFQty: Integer;
begin

  if Key = #13 then
  begin
    FSMStr := TRIM(edtScan.Text);
    if Copy(FSMStr, 1, 2) = 'SC' then
    begin
      if not JudgeSQD() then
        exit;
    end
    else
    begin
      if Trim(edtIsSMTH.Text) = '' then
      begin

        if SaveCKJTM(FSMStr) then
        begin
          PlayWav('正确');
        end
        else
          exit;

      end
      else
      begin
        SaveCKJTMCX();
      end;

//      InitSQGrid();
      InitCKGrid();
    end;

    edtScan.Text := '';

  end;

end;

procedure TfrmSMCK.Button2Click(Sender: TObject);
begin
  WriteCxGrid('扫描出库Tv44', Tv4, '发货申请');
  WriteCxGrid('扫描出库Tv33', Tv3, '发货申请');
  Close;

end;

procedure TfrmSMCK.Button3Click(Sender: TObject);
var
  MKey: Char;
begin
  MKey := #13;
  try
    frmYarnDRList_Sel := TfrmYarnDRList_Sel.Create(self);
    with frmYarnDRList_Sel do
    begin
      if ShowModal = 1 then
      begin
        edtScan.Text := Trim(Order_Main.fieldbyname('DRMID').AsString);
        edtScanKeyPress(edtScan, MKey);
      end;
    end;
  finally
    frmYarnDRList_Sel.Free;
  end;
end;

procedure TfrmSMCK.Button4Click(Sender: TObject);
begin

  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '供应商';
      if ShowModal = 1 then
      begin
        self.ToCoName.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoAbbrName').AsString);
        self.ToCoNo.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoCode').AsString);
      end;
    end;

  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmSMCK.Button5Click(Sender: TObject);
var
  MKey: Char;
  FBYIOID, FBanID, FMXID, FStkID: string;
  FQty: Integer;
begin
  MKey := #13;

  if Trim(OutNo.Text) = '' then
  begin
    exit;
  end;

  try
    frmKCList_Sel := TfrmKCList_Sel.Create(Application);
    with frmKCList_Sel do
    begin
      FFOutNo := OutNo.text;
      if ShowModal = 1 then
      begin
        FQty := FFQty;
        FStkID := FFStkID;

//        with ADOQueryTemp do
//        begin
//          Close;
//          sql.Clear;
//          Sql.Add(' Select Min(isnull(Qty,0)-DlyQTy) from Tat_Cloth_DR_Sub A  where EXISTS(select * from Bs_Yarn_IO X where X.BYIOID=''' + trim(FStkID) + '''');
//          SQL.Add(' and isnull(X.Y_Code,'''') = isnull(A.Y_Code,'''')   ');
//          SQL.Add(' and isnull(X.Yarn_Name,'''') = isnull(A.Yarn_Name,'''') ');
//          SQL.Add(' and isnull(X.Y_name,'''') = isnull(A.Y_name,'''')       ');
//          SQL.Add(' and isnull(X.Y_Spec,'''') = isnull(A.Y_Spec,'''')       ');
//          SQL.Add(' and isnull(X.Denier,0) = isnull(A.Denier,0)             ');
//          SQL.Add(' and isnull(X.Z_Number,0) = isnull(A.Z_Number,0)         ');
//          SQL.Add(' and isnull(X.F_Number,0) = isnull(A.F_Number,0)         ');
//          SQL.Add(' and isnull(X.Y_Type,'''') = isnull(A.Y_Type,''''))      ');
////            ShowMessage(SQL.text);
//          Open;
//        end;
//        if ADOQueryTemp.IsEmpty then
//        begin
//          raise Exception.Create('未申请该品种，无法发货');
//        end
//        else
//        begin
//
//        end;

        if frmKCList_Sel.cxPageControl1.ActivePageIndex = 0 then
        begin
          FBYIOID := FFBYIOID;

          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            Sql.Add(' update BS_Yarn_IO_MX set IOFlag= ''已出库'',CKLX=''按单'',OutPutId= REPLACE(MXID,''Bao'',''YC''),HZCKNO=''' + Trim(OutNo.text) + '''');
            Sql.Add(' from BS_Yarn_IO_MX A');
            sql.Add(' where IOFlag= ''已入库'' and EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + quotedstr(FBYIOID) + ','','') X where A.BYIOID=X.RTValue )  ');
            if (CRType.text = '整经出库') or (CRType.text = '织造出库') then
            begin
              Sql.Add(' update BS_Yarn_IO_MX set IOFlag2= ''已入库''');
              Sql.Add(' from BS_Yarn_IO_MX A');
              sql.Add(' where IOFlag= ''已出库'' and EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + quotedstr(FBYIOID) + ','','') X where A.BYIOID=X.RTValue )  ');
            end;

//            ShowMessage(SQL.text);
            ExecSQL;
          end;

        end
        else if frmKCList_Sel.cxPageControl1.ActivePageIndex = 1 then
        begin
          FBanID := FFBanID;

          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            Sql.Add(' update BS_Yarn_IO_MX set IOFlag= ''已出库'',CKLX=''按板'',OutPutId= REPLACE(MXID,''Bao'',''YC''),HZCKNO=''' + Trim(OutNo.text) + '''');
            Sql.Add(' from BS_Yarn_IO_MX A');
            sql.Add(' where IOFlag= ''已入库'' and EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + quotedstr(FBanID) + ','','') X where A.BanID=X.RTValue )  ');

            if (CRType.text = '整经出库') or (CRType.text = '织造出库') then
            begin
              Sql.Add(' update BS_Yarn_IO_MX set IOFlag2= ''已入库''');
              Sql.Add(' from BS_Yarn_IO_MX A');
              sql.Add(' where IOFlag= ''已出库'' and EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + quotedstr(FBanID) + ','','') X where A.BanID=X.RTValue )  ');
            end;
//            ShowMessage(SQL.text);
            ExecSQL;
          end;
        end
        else if frmKCList_Sel.cxPageControl1.ActivePageIndex = 2 then
        begin
          FMXID := FFMXID;

          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            Sql.Add(' update BS_Yarn_IO_MX set IOFlag= ''已出库'',CKLX=''按包'',OutPutId= REPLACE(MXID,''Bao'',''YC''),HZCKNO=''' + Trim(OutNo.text) + '''');
            Sql.Add(' from BS_Yarn_IO_MX A');
            sql.Add(' where IOFlag= ''已入库'' and EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + quotedstr(FMXID) + ','','') X where A.MXID=X.RTValue )  ');

            if (CRType.text = '整经出库') or (CRType.text = '织造出库') then
            begin
              Sql.Add(' update BS_Yarn_IO_MX set IOFlag2= ''已入库''');
              Sql.Add(' from BS_Yarn_IO_MX A');
              sql.Add(' where IOFlag= ''已出库'' and EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + quotedstr(FMXID) + ','','') X where A.MXID=X.RTValue )  ');
            end;

//            ShowMessage(SQL.text);
            ExecSQL;
          end;
        end;

      end;

      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        SQL.Add('exec P_Yarn_CK ');
        sql.Add(' @OutNo=' + quotedstr(Trim(OutNo.text)));
        sql.Add(',@FillID=' + quotedstr(Trim(DCode)));
        sql.Add(',@Filler=' + quotedstr(Trim(DName)));
        sql.Add(',@ToCoNo=' + quotedstr(Trim(ToCoNo.text)));
        sql.Add(',@ToCoName=' + quotedstr(Trim(ToCoName.text)));
//            ShowMessage(SQL.text);
        ExecSQL;
      end;

      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('exec P_Yarn_DR @OutNo=' + Quotedstr(Trim(OutNo.Text)));
        Open;
      end;
      if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
      begin
        raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
      end;
      InitSQGrid();
      InitCKGrid();
    end;
  finally
    frmKCList_Sel.Free;
  end;
end;

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

procedure TfrmSMCK.btn1Click(Sender: TObject);
var
  MKey: Char;
begin
  MKey := #13;
  CZZT := '选择库存';
  fcuowu := '1';
//  try
//    frmCKProductCPKCListSel := TfrmCKProductCPKCListSel.Create(Application);
//    with frmCKProductCPKCListSel do
//    begin
////      CustomerNoName.text := trim(Self.KHName.text);
//      if ShowModal = 1 then
//      begin
//        with frmCKProductCPKCListSel.CDS_Main do
//        begin
//          First;
//          while not Eof do
//          begin
//            if Fieldbyname('ssel').AsBoolean then
//            begin
//
//              Self.edtScan.Text := Trim(Fieldbyname('JYID').AsString);
//              edtScanKeyPress(Self.edtScan, MKey);
//              if fcuowu = '错误' then
//              begin
//                Break;
//              end;
//              Sleep(200);
//            end;
//            Next;
//          end;
//        end;
//      end;
//    end;
//  finally
//    frmCKProductCPKCListSel.Free;
//  end;
  InitCKGrid();
end;

procedure TfrmSMCK.InitSQGrid();
begin
  if OutNo.Text <> '' then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('SELECT A.OrderNo,* FROM Tat_Cloth_DR_Sub B');
      sql.Add('inner join Tat_Cloth_DR_Main  A ON A.DRMID=B.DRMID ');
      sql.Add('WHERE B.DRMID=''' + Trim(OutNo.Text) + '''');
//      ShowMessage(SQL.text);
      Open;
    end;
    SCreateCDS(ADOQueryTemp, DS1);
    SInitCDSData(ADOQueryTemp, DS1);

    crtype.Text := Trim(ADOQueryTemp.FieldByName('OutType').AsString);
    ToCoName.Text := Trim(ADOQueryTemp.FieldByName('ToCoName').AsString);
    OrderNo.Text := Trim(ADOQueryTemp.FieldByName('OrderNo').AsString);
  end;
end;

procedure TfrmSMCK.InitCKGrid();
begin
  if OutNo.Text <> '' then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add(' select A.*,B.*,BYIOID2=B.BYIOID ');
      sql.Add(' from BS_Yarn_IO_MX A INNER JOIN BS_Yarn_IO B on A.MXID =B.FromMXID  ');
      sql.Add(' where B.BYIONO=''' + Trim(OutNo.Text) + '''');
      sql.Add(' order by A.BYIOID,A.BanNo,A.BaoNO');
//      ShowMessage(sql.text);
      Open;
    end;
    SCreateCDS(ADOQueryTemp, CDS_MainSel);
    SInitCDSData(ADOQueryTemp, CDS_MainSel);
  end;

end;

end.

