unit U_TatClothStkListPH;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, cxGridCustomTableView, cxGridTableView,
  cxGridBandedTableView, cxGridDBBandedTableView, cxGridLevel, cxClasses,
  cxControls, cxGridCustomView, cxGridDBTableView, cxGrid, StdCtrls, ComCtrls,
  ExtCtrls, ToolWin, cxGridCustomPopupMenu, cxGridPopupMenu, ADODB, DBClient,
  cxDropDownEdit, cxCheckBox, RM_Common, RM_Class, RM_e_Xls, RM_Dataset,
  RM_System, RM_GridReport, Menus, cxCalendar, MovePanel, Math, Clipbrd,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, cxTextEdit, dxSkinsCore,
  dxSkinsDefaultPainters, dxDateRanges, dxBarBuiltInMenu, U_BaseList, cxPC,
  System.StrUtils, dxScrollbarAnnotations;

type
  TfrmTatClothStkListPH = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DS_1: TDataSource;
    GPM_1: TcxGridPopupMenu;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_1: TClientDataSet;
    begtime: TDateTimePicker;
    endTime: TDateTimePicker;
    IsJYTime: TCheckBox;
    ToolButton2: TToolButton;
    ToolButton4: TToolButton;
    Panel6: TPanel;
    Label31: TLabel;
    LBCPAP: TLabel;
    BTLP: TButton;
    BTNP: TButton;
    TCBNOR: TComboBox;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column22: TcxGridDBColumn;
    v1Column21: TcxGridDBColumn;
    v1Column32: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column23: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column27: TcxGridDBColumn;
    v1Column28: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column42: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column34: TcxGridDBColumn;
    v1Column46: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Label3: TLabel;
    Label7: TLabel;
    Label10: TLabel;
    Label5: TLabel;
    lbl1: TLabel;
    Label20: TLabel;
    Label23: TLabel;
    OrderNo: TEdit;
    conNO: TEdit;
    STKID: TEdit;
    C_Name: TEdit;
    C_Color: TEdit;
    jyg: TEdit;
    C_Pattern: TEdit;
    C_Code: TEdit;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    Tv1Column12: TcxGridDBColumn;
    PM_1: TPopupMenu;
    PM11: TMenuItem;
    N1: TMenuItem;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column13: TcxGridDBColumn;
    Label1: TLabel;
    Label4: TLabel;
    ToCoName: TEdit;
    ckno: TEdit;
    Label6: TLabel;
    BegDate2: TDateTimePicker;
    EndDate2: TDateTimePicker;
    CheckBox1: TCheckBox;
    Tv1Column14: TcxGridDBColumn;
    ToolButton1: TToolButton;
    ToolButton3: TToolButton;
    ToolButton5: TToolButton;
    ToolButton6: TToolButton;
    Tv1Column15: TcxGridDBColumn;
    Tv1Column16: TcxGridDBColumn;
    Tv1GiftReason: TcxGridDBColumn;
    Tv1issp: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoMChange(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure orderNoKeyPress(Sender: TObject; var Key: Char);
    procedure BTNPClick(Sender: TObject);
    procedure BTLPClick(Sender: TObject);
    procedure TCBNORChange(Sender: TObject);
    procedure JYTypeChange(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure cxPageControl1Change(Sender: TObject);
    procedure CDS_1BeforeOpen(DataSet: TDataSet);
    procedure Tv1DataControllerFilterBeforeChange(Sender: TcxDBDataFilterCriteria; ADataSet: TDataSet; const AFilterText: string);
    procedure Tv1DataControllerSummaryFooterSummaryItemsSummary(ASender: TcxDataSummaryItems; Arguments: TcxSummaryEventArguments; var OutArguments: TcxSummaryEventOutArguments);
    procedure Tv1TcxGridDBDataControllerTcxDataSummaryFooterSummaryItems6GetText(Sender: TcxDataSummaryItem; const AValue: Variant; AIsFooter: Boolean; var AText: string);
    procedure ToolButton4Click(Sender: TObject);
    procedure PM11Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
  private
    CurrentPage, RecordsNumber: Integer;
    lstCust: TStringList;
    procedure InitGrid();
    procedure SetStatus();
    { Private declarations }
  public
    canshu1: string;
    canshu2: string;
    { Public declarations }
  end;

implementation

uses
  U_DataLink, U_RTFun, U_LabelPrint;

{$R *.dfm}

procedure TfrmTatClothStkListPH.SetStatus();
begin

end;

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

procedure TfrmTatClothStkListPH.FormCreate(Sender: TObject);
begin
  inherited;
  lstCust := TStringList.Create;
  EndDate.DateTime := SGetServerDateTime(ADOQueryTemp);
  BegDate.DateTime := EndDate.DateTime - 20;

  EndDate2.DateTime := SGetServerDateTime(ADOQueryTemp);
  BegDate2.DateTime := EndDate2.DateTime - 20;
end;

procedure TfrmTatClothStkListPH.InitGrid();
var
  WSql: string;
begin

  WSql := SGetFilters(Panel1, 1, 2);
  if Trim(WSql) <> '' then
    WSql := ' and ' + WSql;
  if IsJYTime.Checked then
  begin
    WSql := WSql + ' and IOTime>=' + quotedstr(FormatDateTime('yyyy-MM-dd', BegDate.DateTime));
    WSql := WSql + ' and IOTime<' + quotedstr(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1));
  end;
  if CheckBox1.Checked then
  begin
    WSql := WSql + ' and ckfilltime>=' + quotedstr(FormatDateTime('yyyy-MM-dd', BegDate.DateTime));
    WSql := WSql + ' and ckfilltime<' + quotedstr(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1));
  end;

  try
    ADOQueryMain.DisableControls;

    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add(' select top ' + inttostr(RecordsNumber) + ' * ');
      sql.Add(' from  ');
      sql.Add('(select  rownumber=cast((row_number() over(order by AA.IOTime desc)) as int)');
      sql.Add(',COUNT(1) OVER() AS TotalCount');
      sql.Add(',* from V_Tat_Cloth_Stk   AA where 1=1 ' + WSql);
      if canshu2 = '备货' then
      begin
        sql.Add(' and isnull(IsStocking,0)=1');
      end;
      sql.Add(') temp_row');
      sql.Add(' where  rownumber> ' + inttostr(((CurrentPage - 1) * RecordsNumber)));
      sql.Add(' order by rownumber');
//            showmessage(sql.text);
      Open;
    end;
    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);
    LBCPAP.Caption := inttostr(CurrentPage) + '/' + inttostr(ceil(CDS_1.FieldByName('TotalCount').AsInteger / RecordsNumber));
  finally
    ADOQueryMain.EnableControls;
    TV1.DataController.Filter.Clear;
  end;

end;

procedure TfrmTatClothStkListPH.TBRafreshClick(Sender: TObject);
begin
  CurrentPage := 1;
  InitGrid();
end;

procedure TfrmTatClothStkListPH.CDS_1BeforeOpen(DataSet: TDataSet);
begin

  lstCust.Clear;
end;

procedure TfrmTatClothStkListPH.ConNoMChange(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  end;
end;

procedure TfrmTatClothStkListPH.cxPageControl1Change(Sender: TObject);
begin
  CurrentPage := 1;
  SetStatus();
  InitGrid();
end;

procedure TfrmTatClothStkListPH.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmTatClothStkListPH.FormShow(Sender: TObject);
begin
  inherited;

  ReadCxGrid(trim(self.Caption) + 'Tv1', Tv1, '成品配货列表');
  CurrentPage := 1;
  RecordsNumber := 1000;
  canshu1 := Trim(Self.fParameters1);
  canshu2 := Trim(Self.fParameters2);
  SetStatus();
  v1Column4.Visible := false;
  v1Column4.Hidden := true;
  InitGrid();
end;

procedure TfrmTatClothStkListPH.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    exit;
  TcxGridToExcel(Self.Caption, cxGrid1);
end;

procedure TfrmTatClothStkListPH.orderNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    CurrentPage := 1;
    InitGrid();
  end;
end;

procedure TfrmTatClothStkListPH.PM11Click(Sender: TObject);
begin
  SelOKNo(CDS_1, false);
end;

procedure TfrmTatClothStkListPH.Tv1DataControllerFilterBeforeChange(Sender: TcxDBDataFilterCriteria; ADataSet: TDataSet; const AFilterText: string);
begin
  lstCust.Clear;
end;

procedure TfrmTatClothStkListPH.Tv1DataControllerSummaryFooterSummaryItemsSummary(ASender: TcxDataSummaryItems; Arguments: TcxSummaryEventArguments; var OutArguments: TcxSummaryEventOutArguments);
var
  BaoID: string;
begin
//  if TcxGridDBTableSummaryItem(Arguments.SummaryItem).Column = VC_BaoID then
//  begin
//    BaoID := vartostr(ASender.DataController.Values[Arguments.RecordIndex, VC_BaoID.Index]);
//    if BaoID = '' then
//      Exit;
//    if BaoID = null then
//      Exit;
//    if lstCust.IndexOf(BaoID) = -1 then
//    begin
//      lstCust.Add(BaoID);
//    end;
//  end;
end;

procedure TfrmTatClothStkListPH.Tv1TcxGridDBDataControllerTcxDataSummaryFooterSummaryItems6GetText(Sender: TcxDataSummaryItem; const AValue: Variant; AIsFooter: Boolean; var AText: string);
begin
  if lstCust <> nil then
    AText := IntToStr(lstCust.Count);
end;

procedure TfrmTatClothStkListPH.BTNPClick(Sender: TObject);
begin
  if CurrentPage < CDS_1.FieldByName('TotalCount').AsInteger / RecordsNumber then
    CurrentPage := CurrentPage + 1;
  InitGrid();
end;

procedure TfrmTatClothStkListPH.BTLPClick(Sender: TObject);
begin
  if CurrentPage > 1 then
    CurrentPage := CurrentPage - 1;
  InitGrid();
end;

procedure TfrmTatClothStkListPH.TCBNORChange(Sender: TObject);
begin
  RecordsNumber := StrToInt(TCBNOR.Text);
  CurrentPage := 1;
  InitGrid();
end;

procedure TfrmTatClothStkListPH.JYTypeChange(Sender: TObject);
begin
  CurrentPage := 1;
  InitGrid();
end;

procedure TfrmTatClothStkListPH.N1Click(Sender: TObject);
begin

  SelOKNo(CDS_1, true);
end;

procedure TfrmTatClothStkListPH.ToolButton1Click(Sender: TObject);
var
  MIOIDS, MStkIds, MOutNos: string;
begin
  if CDS_1.IsEmpty then
    Exit;
  if not CDS_1.Locate('SSel', True, []) then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;

  MIOIDS := '';
  MStkIds := '';
  MOutNos := '';
  CDS_1.DisableControls;
  with CDS_1 do
  begin
    First;
    while not Eof do
    begin
      if CDS_1.fieldbyname('SSel').AsBoolean then
      begin
        MIOIDS := MIOIDS + (Trim(CDS_1.fieldbyname('IOID').AsString)) + ',';
        MStkIds := MStkIds + (Trim(CDS_1.fieldbyname('STKID').AsString)) + ',';
        MOutNos := MOutNos + (Trim(CDS_1.fieldbyname('ckno').AsString)) + ',';
      end;
      Next;
    end;
  end;
  MIOIDS := leftBstr(trim(MIOIDS), length(MIOIDS) - 1);
  MStkIds := leftBstr(trim(MStkIds), length(MStkIds) - 1);
  MOutNos := leftBstr(trim(MOutNos), length(MOutNos) - 1);
  CDS_1.Locate('SSel', True, []);
  CDS_1.EnableControls;

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

  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      Sql.Clear;

      sql.Add('update  Tat_Cloth_Stock SET IsStocking=''0''  ');
//        sql.Add('where iono=' + quotedstr(trim(Order_Main.fieldbyname('DRMID').AsString)));
      sql.add('where IsStocking=''1''');
      sql.Add('and  EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString]( ' + QuotedStr(MStkIds));
      SQL.Add(','','')  X where X.RTValue=Tat_Cloth_Stock.StkId)     ');

      sql.Add('update  Tat_Cloth_Inspect SET CIIOFlag=''已入库''     ');
//        sql.Add('where iono=' + quotedstr(trim(Order_Main.fieldbyname('DRMID').AsString)));
      sql.Add('WHERE   EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString]( ' + QuotedStr(MStkIds));
      SQL.Add(','','')  X where X.RTValue=Tat_Cloth_Inspect.ciid)     ');

      sql.Add('delete   Tat_Cloth_IO      ');
      sql.Add(' WHERE   EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString]( ' + QuotedStr(MStkIds));
      SQL.Add(','','')  X where X.RTValue=Tat_Cloth_IO.stkid)     ');

      sql.add('and ioflag=''待出库''');
      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(trim(self.Caption)));
      sql.Add(',' + quotedstr(trim('撤销配货')));
      sql.Add(',' + quotedstr(trim(MStkIds)));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      ExecSQL;
    end;

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

procedure TfrmTatClothStkListPH.ToolButton2Click(Sender: TObject);
var
  WSql: string;
begin
  if CDS_1.IsEmpty then
    Exit;
  if not CDS_1.Locate('SSel', True, []) then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  WSql := '';
  CDS_1.DisableControls;
  with CDS_1 do
  begin
    First;
    while not Eof do
    begin
      if CDS_1.fieldbyname('SSel').AsBoolean then
      begin
        if WSql <> '' then
        begin
          WSql := WSql + ',' + (Trim(CDS_1.fieldbyname('CIID').AsString));
        end
        else
        begin
          WSql := (Trim(CDS_1.fieldbyname('CIID').AsString));
        end;
      end;
      Next;
    end;
  end;
  CDS_1.Locate('SSel', True, []);
  CDS_1.EnableControls;
//  ShowMessage(WSql);
  try
    frmLabelPrint := TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin
      FLMType := 'TradeClothStk';
      FFiltration1 := WSql;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmLabelPrint.Free;
  end;
end;

procedure TfrmTatClothStkListPH.ToolButton3Click(Sender: TObject);
var
  MIOIDS, MStkIds, MOutNos: string;
  mFHDH, sQZ: string;
begin
  if CDS_1.IsEmpty then
    Exit;
  if not CDS_1.Locate('SSel', True, []) then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;

  MIOIDS := '';
  MStkIds := '';
  MOutNos := '';
  CDS_1.DisableControls;
  with CDS_1 do
  begin
    First;
    while not Eof do
    begin
      if CDS_1.fieldbyname('SSel').AsBoolean then
      begin
        MIOIDS := MIOIDS + (Trim(CDS_1.fieldbyname('IOID').AsString)) + ',';
        MStkIds := MStkIds + (Trim(CDS_1.fieldbyname('STKID').AsString)) + ',';
        MOutNos := MOutNos + (Trim(CDS_1.fieldbyname('ckno').AsString)) + ',';
      end;
      Next;
    end;
  end;
  MIOIDS := leftBstr(trim(MIOIDS), length(MIOIDS) - 1);
  MStkIds := leftBstr(trim(MStkIds), length(MStkIds) - 1);
  MOutNos := leftBstr(trim(MOutNos), length(MOutNos) - 1);
  CDS_1.Locate('SSel', True, []);
  CDS_1.EnableControls;

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

  try
    ADOQueryCmd.Connection.BeginTrans;

    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
    //SQL同时查询FHDH和QZ前缀
        SQL.Add(' exec Update_Tat_Cloth_IO_MaxFHDH   @CompanyName = ' + quotedstr(trim(CDS_1.fieldbyname('CompanyName').AsString)));
        Open;
      end;

      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
    //SQL同时查询FHDH和QZ前缀
        SQL.Add(' select FHDH=FHDH+1,QZ from tat_cloth_io_FHDH where CompanyName = ' + quotedstr(trim(CDS_1.fieldbyname('CompanyName').AsString)));
        Open;
      end;

      mFHDH := ADOQueryCmd.FieldByName('FHDH').AsString;
      sQZ := ADOQueryCmd.FieldByName('QZ').AsString; //从数据库QZ字段拿前缀

  //前缀 + 年份后两位 + 6位流水号
      mFHDH := sQZ + FormatDateTime('yy', Date) + Format('%.6d', [(StrToIntDef(mFHDH, 0) + 1000000) mod 1000000]);

      with ADOQueryCmd do
      begin
        Close;
        Sql.Clear;
        sql.Add(' update tat_cloth_io_FHDH SET FHDH = FHDH+1,ioFHDH=''' + mFHDH + ''' ');
        SQL.Add(' where CompanyName = ' + quotedstr(trim(CDS_1.fieldbyname('CompanyName').AsString)));
        EXECSQL;
      end;
    end;

    with ADOQueryCmd do
    begin
      Close;
      Sql.Clear;
      sql.Add('update  Tat_Cloth_IO SET FHDH = ''' + mFHDH + ''' ');
      sql.Add(' WHERE   EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString]( ' + QuotedStr(MStkIds));
      SQL.Add(','','')  X where X.RTValue=Tat_Cloth_IO.stkid) ');
      sql.add('and ioflag=''待出库''');
      EXECSQL;
    end;

    with ADOQueryCmd do
    begin
      Close;
      Sql.Clear;

      sql.Add('update  Tat_Cloth_Stock SET StkExists=0     ');
//        sql.Add('where iono=' + quotedstr(trim(Order_Main.fieldbyname('DRMID').AsString)));
      sql.add('where IsStocking=''1''');
      sql.Add('and  EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString]( ' + QuotedStr(MStkIds));
      SQL.Add(','','')  X where X.RTValue=Tat_Cloth_Stock.StkId)     ');

      sql.Add('update  Tat_Cloth_Inspect SET CIIOFlag=''已出库''     ');
//        sql.Add('where iono=' + quotedstr(trim(Order_Main.fieldbyname('DRMID').AsString)));
      sql.Add('WHERE   EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString]( ' + QuotedStr(MStkIds));
      SQL.Add(','','')  X where X.RTValue=Tat_Cloth_Inspect.ciid)     ');

      sql.Add('update  Tat_Cloth_IO SET ioflag=''出库'',iotime=getdate()    ');
      sql.Add(' WHERE   EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString]( ' + QuotedStr(MStkIds));
      SQL.Add(','','')  X where X.RTValue=Tat_Cloth_IO.stkid)     ');

      sql.add('and ioflag=''待出库''');
      EXECSQL;
    end;

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

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

end;

procedure TfrmTatClothStkListPH.ToolButton4Click(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption) + 'Tv1', Tv1, '成品配货列表');
end;

procedure TfrmTatClothStkListPH.ToolButton5Click(Sender: TObject);
var
  MIOIDS, MStkIds, MOutNos: string;
  mGiftReason: string;
begin
  if CDS_1.IsEmpty then
    Exit;
  if not CDS_1.Locate('SSel', True, []) then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;

  MIOIDS := '';
  MStkIds := '';
  MOutNos := '';
  CDS_1.DisableControls;
  with CDS_1 do
  begin
    First;
    while not Eof do
    begin
      if CDS_1.fieldbyname('SSel').AsBoolean then
      begin
        MIOIDS := MIOIDS + (Trim(CDS_1.fieldbyname('IOID').AsString)) + ',';
        MStkIds := MStkIds + (Trim(CDS_1.fieldbyname('STKID').AsString)) + ',';
        MOutNos := MOutNos + (Trim(CDS_1.fieldbyname('ckno').AsString)) + ',';
      end;
      Next;
    end;
  end;
  MIOIDS := leftBstr(trim(MIOIDS), length(MIOIDS) - 1);
  MStkIds := leftBstr(trim(MStkIds), length(MStkIds) - 1);
  MOutNos := leftBstr(trim(MOutNos), length(MOutNos) - 1);
  CDS_1.Locate('SSel', True, []);
  CDS_1.EnableControls;

//  if Application.MessageBox('确定要执行操作吗？', '提示', 32 + 4) <> IdyeS then
//    Exit;
     // 新增：弹出输入框让用户填写赠送原因
  mGiftReason := '';
  if not InputQuery('赠送原因', '请输入赠送原因：', mGiftReason) then
    Exit; // 用户点击了取消

  if Trim(mGiftReason) = '' then
  begin
    Application.MessageBox('请输入赠送原因！', '提示', 0);
    Exit;
  end;

  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      Sql.Clear;

      sql.Add('update  Tat_Cloth_Stock SET ISZS=''赠送'' , GiftReason = ' + QuotedStr(Trim(mGiftReason)));
//        sql.Add('where iono=' + quotedstr(trim(Order_Main.fieldbyname('DRMID').AsString)));
      sql.add('where IsStocking=''1''');
      sql.Add('and  EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString]( ' + QuotedStr(MStkIds));
      SQL.Add(','','')  X where X.RTValue=Tat_Cloth_Stock.StkId)     ');
//      showMessage(sql.Text);
      EXECSQL;
    end;

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

procedure TfrmTatClothStkListPH.ToolButton6Click(Sender: TObject);
var
  MIOIDS, MStkIds, MOutNos: string;
begin
  if CDS_1.IsEmpty then
    Exit;
  if not CDS_1.Locate('SSel', True, []) then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;

  MIOIDS := '';
  MStkIds := '';
  MOutNos := '';
  CDS_1.DisableControls;
  with CDS_1 do
  begin
    First;
    while not Eof do
    begin
      if CDS_1.fieldbyname('SSel').AsBoolean then
      begin
        MIOIDS := MIOIDS + (Trim(CDS_1.fieldbyname('IOID').AsString)) + ',';
        MStkIds := MStkIds + (Trim(CDS_1.fieldbyname('STKID').AsString)) + ',';
        MOutNos := MOutNos + (Trim(CDS_1.fieldbyname('ckno').AsString)) + ',';
      end;
      Next;
    end;
  end;
  MIOIDS := leftBstr(trim(MIOIDS), length(MIOIDS) - 1);
  MStkIds := leftBstr(trim(MStkIds), length(MStkIds) - 1);
  MOutNos := leftBstr(trim(MOutNos), length(MOutNos) - 1);
  CDS_1.Locate('SSel', True, []);
  CDS_1.EnableControls;

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

  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      Sql.Clear;

      sql.Add('update  Tat_Cloth_Stock SET ISZS=NULL ,GiftReason = null ');
//        sql.Add('where iono=' + quotedstr(trim(Order_Main.fieldbyname('DRMID').AsString)));
      sql.add('where IsStocking=''1''');
      sql.Add('and  EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString]( ' + QuotedStr(MStkIds));
      SQL.Add(','','')  X where X.RTValue=Tat_Cloth_Stock.StkId)     ');

      EXECSQL;
    end;

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

end.

