unit U_TradeClothDROutEdit;

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, dxSkinsCore, dxSkinsDefaultPainters,
  cxNavigator, dxDateRanges, dxBarBuiltInMenu, U_BaseList, dxSkinWXI,
  dxScrollbarAnnotations;

type
  TfrmTradeClothDROutEdit = 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;
    cv3Column3: TcxGridDBColumn;
    VC_MJXH: TcxGridDBColumn;
    v3Column1: TcxGridDBColumn;
    v3Column2: TcxGridDBColumn;
    cv3Column4: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxGrid1: TcxGrid;
    TV4: TcxGridDBTableView;
    cCXCONNO: TcxGridDBColumn;
    cV4Column1: TcxGridDBColumn;
    cCXCOLOR: TcxGridDBColumn;
    VC_SQQTY: TcxGridDBColumn;
    cCXFHPS: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    TV4Column1: TcxGridDBColumn;
    Tv3Column1: TcxGridDBColumn;
    TV4Column2: TcxGridDBColumn;
    TV4Column3: TcxGridDBColumn;
    OutNo: TEdit;
    Tv3Column2: TcxGridDBColumn;
    Tv3Column3: TcxGridDBColumn;
    GPM_2: TcxGridPopupMenu;
    TV4Column5: TcxGridDBColumn;
    Tv3Column4: TcxGridDBColumn;
    ADOQuery1: TADOQuery;
    ADOQuery2: TADOQuery;
    Tv3Column5: TcxGridDBColumn;
    Tv3Column6: TcxGridDBColumn;
    TV4Column4: TcxGridDBColumn;
    TV4Column6: TcxGridDBColumn;
    TV4Column7: TcxGridDBColumn;
    DRSID: TEdit;
    Tv3Column7: TcxGridDBColumn;
    Tv3Column8: TcxGridDBColumn;
    Tv3Column9: TcxGridDBColumn;
    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 Button1Click(Sender: TObject);
    procedure edtIsSMTHClick(Sender: TObject);
    procedure btn1Click(Sender: TObject);
    procedure TV4MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  private
    { Private declarations }
    FCKNO, FSMStr, JXFlag, CZZT, fcuowu, FCRType, FZDMX, FSIDStr: string;
    procedure InitCKGrid();
    procedure InitSQGrid();
    procedure PlayWav(MWav: string);
    function JudgeSQD(): Boolean;
    function SaveCKJTM(MStkIds: string): Boolean; //保存出库
    function SaveSelCKJTM(MStkIds: string): Boolean;
    procedure SaveCKJTMCX();   //撤销出库
    procedure PDCF(MStkIds: string);
  public
    { Public declarations }
  end;

var
  frmTradeClothDROutEdit: TfrmTradeClothDROutEdit;

implementation

uses
  U_DataLink, U_RTFun, MMSystem, U_TradeDRSel, U_TradeKCListSel;

procedure TfrmTradeClothDROutEdit.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 TfrmTradeClothDROutEdit.PDCF(MStkIds: string);
begin
  with ADOQuery1 do
  begin
    Close;
    Sql.Clear;
    sql.Add('select C_Code,SUM(ISNULL(Piece, 0)) Piece,SUM(ISNULL(Qty, 0)) Qty,QtyUnit');
    SQL.Add('from Trade_Cloth_DR_Sub ');
    SQL.Add('where DRMid=' + quotedstr(trim(OutNo.Text)));
    Sql.Add('GROUP BY C_Code,QtyUnit');
    Open;
  end;

  with ADOQueryTemp do
  begin
    Close;
    Sql.Clear;
    sql.Add('select * from Trade_Cloth_Stock ');
    sql.Add('where StkId in ()');
    sql.Add('and C_Code');
    Open;
  end;

end;

procedure TfrmTradeClothDROutEdit.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_Trade_Cloth_ReOut ');
        Sql.Add(' @StkIds=' + quotedstr(trim(FSMStr)));
        Sql.Add(',@IOIDS=' + quotedstr(trim('')));
        Sql.Add(',@FillId=' + Quotedstr(DCode));
        Sql.Add(',@Filler=' + Quotedstr(DName));
        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_Trade_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 TfrmTradeClothDROutEdit.TV4MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  inherited;

  InitCKGrid();
end;

function TfrmTradeClothDROutEdit.SaveSelCKJTM(MStkIds: string): Boolean;
var
  FUnit: string;
//   FQty :Integer;
begin
  try
    if Trim(OutNo.Text) = '' 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) + ')  ');

        if crtype.Text = '加工出库' then
        begin
          Sql.Add('exec P_Trade_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));
        end
        else if crtype.Text = '转单出库' then
        begin
          Sql.Add('exec P_Trade_Cloth_SelOut ');
          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(',@DRSid=' + Quotedstr(DRSid.text));
        end;

        Open;
      end;
      if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
      begin
        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_Trade_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;

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

end;

function TfrmTradeClothDROutEdit.SaveCKJTM(MStkIds: string): Boolean;
var
  FUnit: string;
//   FQty :Integer;
begin
  try
    if Trim(OutNo.Text) = '' then
      raise Exception.Create('出库单号不能为空!');
//    if CDS_2.IsEmpty then
//      raise Exception.Create('未选单不能扫描出库!');
//
//    if not CDS_1.Locate('StkId', Trim(FScan), []) then
//      raise Exception.Create('此条码不包含在待出库的卷数据中');

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_Trade_Code_Judge');
      Sql.Add(' @StkIds=' + quotedstr(trim(MStkIds)));
      Sql.Add(',@IONO=' + Quotedstr(OutNo.Text));
      Open;
    end;
    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
    begin
//      raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
      application.MessageBox(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)), '提示信息', 0);
    end;

    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_Trade_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));
        Open;
      end;
      if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
      begin
        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 ADOQuery1 do
    begin
      Close;
      sql.Clear;
      sql.Add('select C_Code,ISNULL(QtyUnit, '''') Unit,SUM(ISNULL(Piece, 0)) Piece,SUM(ISNULL(Qty, 0)) Qty');
      sql.Add('from Trade_Cloth_DR_Sub where DRMID=' + Quotedstr(Trim(OutNo.Text)));
      sql.Add('GROUP BY C_Code,QtyUnit');
      Open;
    end;
    FUnit := ADOQuery1.FieldByName('Unit').AsString;

    //查询已扫描出库数
    with ADOQuery2 do
    begin
      Close;
      sql.Clear;
      sql.Add('select B.C_Code,SUM(A.NetWeight) NetWeight,SUM(A.Meter) Meter,SUM(A.Yardage) Yardage,COUNT(A.StkID) Piece');
      sql.Add('from Trade_Cloth_IO A  left join Trade_Cloth_Stock B on B.StkId=A.StkId where A.IONO=' + Quotedstr(Trim(OutNo.Text)));
      sql.Add('GROUP BY B.C_Code');
//      ShowMessage(Sql.Text);
      Open;
    end;

    if ADOQuery1.fieldbyname('Piece').AsFloat > 0 then
    begin
      if ADOQuery1.fieldbyname('Piece').AsFloat < ADOQuery2.fieldbyname('Piece').AsFloat then
      begin
        application.MessageBox('发货匹数大于扫描匹数！', '提示信息', 0);
      end;

    end;

    if ADOQuery1.Fieldbyname('Qty').AsFloat > 0 then
    begin
      if FUnit = 'M' then
      begin
        if ADOQuery1.FieldByName('Qty').AsFloat < ADOQuery2.FieldByName('Meter').AsFloat then
        begin
          application.MessageBox('超过申请发货数量！', '提示信息', 0);
        end;
      end
      else if FUnit = 'Y' then
      begin
        if ADOQuery1.FieldByName('Qty').AsFloat < ADOQuery2.FieldByName('Yardage').AsFloat then
        begin
          application.MessageBox('超过申请发货数量！', '提示信息', 0);
        end;
      end
      else if FUnit = 'Kg' then
      begin
        if ADOQuery1.FieldByName('Qty').AsFloat < ADOQuery2.FieldByName('NetWeight').AsFloat then
        begin
          application.MessageBox('超过申请发货数量！', '提示信息', 0);
        end;
      end;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_Trade_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;

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

end;

{$R *.dfm}

function TfrmTradeClothDROutEdit.JudgeSQD(): Boolean;
begin
  Result := false;
  try
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from Trade_Cloth_DR_Main A inner join Trade_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 FCRType = '转单出库' then
    begin
      if FZDMX <> FSidStr then
      begin
        InitCKGrid();
        InitSQGrid();
      end;
      if Trim(DRSid.Text) <> '' then
      begin
        FZDMX := Trim(DRSid.Text);
      end;
    end
    else
    begin
      if FSMStr <> FCKNO then
      begin
        InitCKGrid();
        InitSQGrid();
      end;
    end;

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

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

procedure TfrmTradeClothDROutEdit.FormDestroy(Sender: TObject);
begin
  inherited;
  frmTradeClothDROutEdit := nil;
end;

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

procedure TfrmTradeClothDROutEdit.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);
    FSIDStr := TRIM(DRSid.Text);
    if Copy(FSMStr, 1, 2) = 'DM' then
    begin
      if not JudgeSQD() then
        exit;
    end
    else
    begin
      if Trim(edtIsSMTH.Text) = '' then
      begin
        if Copy(FSMStr, 1, 2) = 'BI' then
        begin
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add(' select StkIdS=CAST((select distinct lTRIM(rTRIM(X.StkId)) + '','' ');
            sql.Add('  from Trade_Cloth_Stock X where X.PacketId=' + quotedstr(FSMStr) + ' for xml path('''') ) AS NVARCHAR(MAX))     ');
            Open;
          end;
          if SaveCKJTM(ADOQueryTemp.FieldByName('StkIdS').AsString) then
          begin
            PlayWav('正确');
          end
          else
            exit;
        end
        else
        begin
          if SaveCKJTM(FSMStr) then
          begin
            PlayWav('正确');
          end
          else
            exit;
        end;
      end
      else
      begin
        SaveCKJTMCX();
      end;

      InitSQGrid();
      InitCKGrid();
    end;

    edtScan.Text := '';

  end;

end;

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

end;

procedure TfrmTradeClothDROutEdit.Button1Click(Sender: TObject);
var
  MKey: Char;
begin
  MKey := #13;
  try
    frmTradeDRSel := TfrmTradeDRSel.Create(self);
    with frmTradeDRSel do
    begin
      if ShowModal = 1 then
      begin
        edtScan.Text := Trim(Order_Main.fieldbyname('DRMID').AsString);
        DRSID.Text := Trim(Order_Sub.fieldbyname('DRSID').AsString);
//        CRType.text := Trim(Order_Main.fieldbyname('OutType').AsString);
        FCRType := Trim(Order_Main.fieldbyname('OutType').AsString);
//        ShowMessage(edtScan.text);
        edtScanKeyPress(edtScan, MKey);
      end;
    end;
  finally
    frmTradeDRSel.Free;
  end;
end;

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

procedure TfrmTradeClothDROutEdit.btn1Click(Sender: TObject);
var
  MKey: Char;
  MStkIds: string;
begin
  MKey := #13;
//  CZZT := '选择库存';
  MStkIds := '';
//  fcuowu := '1';
  try
    frmTradeKCListSel := TfrmTradeKCListSel.Create(Application);
    with frmTradeKCListSel do
    begin
//      CustomerNoName.text := trim(Self.KHName.text);
      if ShowModal = 1 then
      begin
        with frmTradeKCListSel.CDS_1 do
        begin
          First;
          while CDS_1.Locate('SSel', True, []) = True do
          begin
            MStkIds := MStkIds + (Trim(CDS_1.FieldByName('StkId').AsString)) + ',';
            CDS_1.Delete;
          end;

        end;
        MStkIds := copy(MStkIds, 1, Length(MStkIds) - 1);

        if SaveSelCKJTM(MStkIds) then
        begin
          PlayWav('正确');
        end
        else
          exit;

      end;
    end;
  finally
    frmTradeKCListSel.Free;
  end;
  InitCKGrid();
end;

procedure TfrmTradeClothDROutEdit.InitSQGrid();
begin
  if FCRType = '加工出库' then
  begin
    if OutNo.Text <> '' then
    begin
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('SELECT * FROM Trade_Cloth_DR_Sub B');
        sql.Add('inner join Trade_Cloth_DR_Main  A ON A.DRMID=B.DRMID ');
        sql.Add('WHERE B.DRMID=''' + Trim(OutNo.Text) + '''');
        Open;
      end;
      SCreateCDS(ADOQueryTemp, DS1);
      SInitCDSData(ADOQueryTemp, DS1);
      crtype.Text := Trim(ADOQueryTemp.FieldByName('OutType').AsString);
      ToCoName.Text := Trim(ADOQueryTemp.FieldByName('ToCoName').AsString);
    end;
  end
  else if FCRType = '转单出库' then
  begin
    if OutNo.Text <> '' then
    begin
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('SELECT * FROM Trade_Cloth_DR_Sub B');
        sql.Add('inner join Trade_Cloth_DR_Main  A ON A.DRMID=B.DRMID ');
        sql.Add('WHERE B.DRSID=''' + Trim(DRSID.Text) + '''');
        Open;
      end;
      SCreateCDS(ADOQueryTemp, DS1);
      SInitCDSData(ADOQueryTemp, DS1);
      crtype.Text := Trim(ADOQueryTemp.FieldByName('OutType').AsString);
      ToCoName.Text := Trim(ADOQueryTemp.FieldByName('ToCoName').AsString);
    end;
  end;

end;

procedure TfrmTradeClothDROutEdit.InitCKGrid();
begin
  if OutNo.Text <> '' then
  begin
    if FCRType = '加工出库' then
    begin
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * ');
        sql.Add(' from Trade_Cloth_Stock A inner join Trade_Cloth_IO B on A.STKID=B.StkId ');
        sql.Add(' where B.IONO=''' + Trim(OutNo.Text) + '''');
        Open;
      end;
      SCreateCDS(ADOQueryTemp, CDS_MainSel);
      SInitCDSData(ADOQueryTemp, CDS_MainSel);
    end
    else if FCRType = '转单出库' then
    begin
      with ADOQuery1 do
      begin
        Close;
        sql.Clear;
        sql.Add('select * ');
        sql.Add(' from Trade_Cloth_Stock_ZD A inner join Trade_Cloth_IO B on A.STKID=B.StkId ');
//        sql.Add('left join Trade_Plan_Sub C on C.SubID=A.Subid');
        sql.Add(' where A.DRSid=''' + Trim(DRSid.Text) + '''');
        sql.Add(' and B.IONO=''' + Trim(OutNo.Text) + '''');
//        showmessage(sql.text);
        Open;
      end;
      SCreateCDS(ADOQuery1, CDS_MainSel);
      SInitCDSData(ADOQuery1, CDS_MainSel);
    end;
  end;

end;

end.

