unit U_TradeClothOutEdit;

interface

uses
  Windows, Messages, strUtils, SysUtils, Variants, Classes, Graphics, Controls,
  Forms, Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, StdCtrls, ExtCtrls, ADODB, DBClient,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel, MMSystem,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxClasses,
  cxControls, cxGridCustomView, cxGrid, MovePanel, cxCheckBox, Menus, ComCtrls,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, U_BaseList, dxSkinsCore,
  dxSkinsDefaultPainters, dxDateRanges, dxBarBuiltInMenu, RM_Common, RM_Class,
  RM_GridReport, RM_Dataset, Vcl.ToolWin, dxSkinBasic, dxSkinBlack, dxSkinBlue,
  dxSkinBlueprint, dxSkinCaramel, dxSkinCoffee, dxSkinDarkroom, dxSkinDarkSide,
  dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle, dxSkinFoggy,
  dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary, dxSkinLilian,
  dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMetropolis,
  dxSkinMetropolisDark, dxSkinMoneyTwins, dxSkinOffice2007Black,
  dxSkinOffice2007Blue, dxSkinOffice2007Green, dxSkinOffice2007Pink,
  dxSkinOffice2007Silver, dxSkinOffice2010Black, dxSkinOffice2010Blue,
  dxSkinOffice2010Silver, dxSkinOffice2013DarkGray, dxSkinOffice2013LightGray,
  dxSkinOffice2013White, dxSkinOffice2016Colorful, dxSkinOffice2016Dark,
  dxSkinOffice2019Black, dxSkinOffice2019Colorful, dxSkinOffice2019DarkGray,
  dxSkinOffice2019White, dxSkinPumpkin, dxSkinSeven, dxSkinSevenClassic,
  dxSkinSharp, dxSkinSharpPlus, dxSkinSilver, dxSkinSpringtime, dxSkinStardust,
  dxSkinSummer2008, dxSkinTheAsphaltWorld, dxSkinTheBezier, dxSkinValentine,
  dxSkinVisualStudio2013Blue, dxSkinVisualStudio2013Dark,
  dxSkinVisualStudio2013Light, dxSkinVS2010, dxSkinWhiteprint, dxSkinWXI,
  dxSkinXmas2008Blue, dxScrollbarAnnotations;

type
  TfrmTradeClothOutEdit = class(TfrmBaseList)
    GPM_1: TcxGridPopupMenu;
    CDS_1: TClientDataSet;
    DS_1: TDataSource;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    Panel1: TPanel;
    CDS_2: TClientDataSet;
    DS_2: TDataSource;
    GPM_2: TcxGridPopupMenu;
    DS_3: TDataSource;
    CDS_3: TClientDataSet;
    GPM_3: TcxGridPopupMenu;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v1OrderNo: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    v1PRTMF: TcxGridDBColumn;
    v1PRTKZ: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    Panel2: TPanel;
    Panel4: TPanel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column6: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    ToolBar1: TToolBar;
    ToolButton2: TToolButton;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    v3Column5: TcxGridDBColumn;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    v3MJID: TcxGridDBColumn;
    v3Column3: TcxGridDBColumn;
    Tv3Column1: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    v3Column1: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    Panel3: TPanel;
    Label4: TLabel;
    Label5: TLabel;
    Label11: TLabel;
    CRTime: TDateTimePicker;
    OutNo: TEdit;
    CRType: TComboBox;
    ToolBar3: TToolBar;
    ToolButton3: TToolButton;
    btnGetOutNo: TToolButton;
    ToolButton6: TToolButton;
    ToolButton7: TToolButton;
    ToolButton8: TToolButton;
    edtScan: TEdit;
    Label6: TLabel;
    ToolButton9: TToolButton;
    ToolButton10: TToolButton;
    ToolButton11: TToolButton;
    ToolButton12: TToolButton;
    Tv2Column2: TcxGridDBColumn;
    ToolButton4: TToolButton;
    Tv1Column2: TcxGridDBColumn;
    Tv3Column2: TcxGridDBColumn;
    V1C_PacketNo: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    V3C_PacketNo: TcxGridDBColumn;
    Tv3Column4: TcxGridDBColumn;
    V1C_PacketId: TcxGridDBColumn;
    V3C_PacketId: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Tv2CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure btnGetOutNoClick(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
    procedure edtScanKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton9Click(Sender: TObject);
    procedure ToolButton10Click(Sender: TObject);
    procedure ToolButton11Click(Sender: TObject);
    procedure ToolButton12Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure CDS_1BeforeOpen(DataSet: TDataSet);
    procedure CDS_3BeforeOpen(DataSet: TDataSet);
    procedure Tv1DataControllerFilterBeforeChange(Sender: TcxDBDataFilterCriteria; ADataSet: TDataSet; const AFilterText: string);
    procedure Tv3DataControllerFilterBeforeChange(Sender: TcxDBDataFilterCriteria; ADataSet: TDataSet; const AFilterText: string);
    procedure Tv1DataControllerSummaryFooterSummaryItemsSummary(ASender: TcxDataSummaryItems; Arguments: TcxSummaryEventArguments; var OutArguments: TcxSummaryEventOutArguments);
    procedure Tv3DataControllerSummaryFooterSummaryItemsSummary(ASender: TcxDataSummaryItems; Arguments: TcxSummaryEventArguments; var OutArguments: TcxSummaryEventOutArguments);
    procedure Tv1TcxGridDBDataControllerTcxDataSummaryFooterSummaryItems3GetText(Sender: TcxDataSummaryItem; const AValue: Variant; AIsFooter: Boolean; var AText: string);
    procedure Tv3TcxGridDBDataControllerTcxDataSummaryFooterSummaryItems3GetText(Sender: TcxDataSummaryItem; const AValue: Variant; AIsFooter: Boolean; var AText: string);
  private
    { Private declarations }
    FScan, FSubIds: string;
    lstPacketId1, lstPacketId3: TStringList;
    procedure PlayWav(MWav: string);
    procedure InitGrid1();
    procedure InitGrid2();
    procedure InitGrid3();
    procedure ScanInNo();
    procedure ScanCIID();
  public

    { Public declarations }
  end;

var
  frmTradeClothOutEdit: TfrmTradeClothOutEdit;

implementation

uses
  U_DataLink, U_RTFun, U_TradeClothWaitReleased, U_LabelPrint;

{$R *.dfm}
procedure TfrmTradeClothOutEdit.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 TfrmTradeClothOutEdit.InitGrid2();
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select  A.*,B.* ');
    sql.Add(' from  Trade_Plan_Sub A  ');
    sql.Add(' inner join Trade_Plan_Main B on A.MainId=B.Mainid');
    if TRIm(FSubIds) <> '' then
      sql.add('and A.SubId in (' + FSubIds + ')')
    else
      sql.add('and 1=2');
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_2);
  SInitCDSData(ADOQueryTemp, CDS_2);
end;

procedure TfrmTradeClothOutEdit.InitGrid1();
begin

  with ADOQueryMain do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.IOFlag,A.IONo,A.IOID,B.* from Trade_Cloth_IO A    ');
    sql.Add('inner join Trade_Cloth_Stock B on A.StkId=B.StkId and A.IOCycles=B.StkCycles   ');
    sql.Add('where A.IOFlag=''入库'' and B.StkExists=1 ');
    if TRIm(FSubIds) <> '' then
      sql.add('and SubId in (' + FSubIds + ')')
    else
      sql.add('and 1=2');
    Open;
  end;
  SCreateCDS(ADOQueryMain, CDS_1);
  SInitCDSData(ADOQueryMain, CDS_1);

end;

procedure TfrmTradeClothOutEdit.InitGrid3();
begin

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.IOFlag,A.IONo,A.IOID,B.* from Trade_Cloth_IO A    ');
    sql.Add('inner join Trade_Cloth_Stock B on A.StkId=B.StkId and A.IOCycles=B.StkCycles   ');
    sql.Add('where A.IOFlag=''出库'' and B.StkExists=0 ');
    if TRIm(OutNo.Text) <> '' then
      sql.add('and IONO =' + QuotedStr(TRIm(OutNo.Text)))
    else
      sql.add('and 1=2');
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_3);
  SInitCDSData(ADOQueryTemp, CDS_3);

end;

procedure TfrmTradeClothOutEdit.ScanInNo();
var
  MStkIds, MSubIds: string;
begin
  try
    if Trim(OutNo.Text) = '' then
      raise Exception.Create('出库单号不能为空!');
    if CDS_2.IsEmpty then
      raise Exception.Create('未选单不能扫描出库!');

    if not CDS_1.Locate('IONo', Trim(FScan), []) then
      raise Exception.Create('此入库单不包含在待出库的卷数据中');

    MStkIds := '';
    MSubIds := '';
    with CDS_1 do
    begin
      First;
      while CDS_1.Locate('IONo', Trim(FScan), []) = True do
      begin
        MStkIds := MStkIds + (Trim(CDS_1.FieldByName('StkId').AsString)) + ',';
        MSubIds := MSubIds + (Trim(CDS_1.FieldByName('SubId').AsString)) + ',';
        MoveCDS(CDS_1, CDS_3);
        CDS_1.Delete;
      end;
    end;

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

    try
      ADOQueryCmd.Connection.BeginTrans;

      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('insert into Nap_Need_Up(UType,UDataId,UOperation,UOperator,UModule,UDetails)       ');
        sql.Add('values(''贸易成品出库''    ');
        sql.Add(',' + quotedstr(MSubIds));
        sql.Add(',''出库''   ');
        sql.Add(',' + quotedstr(DName));
        sql.Add(',' + quotedstr(trim(self.Caption)));
        sql.Add(',' + quotedstr(MStkIds));
        sql.Add(')  ');
        execsql;
      end;

      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(OutNo.Text));
        sql.Add(',''出库''   ');
        sql.Add(',' + quotedstr(DName));
        sql.Add(',' + quotedstr(trim(self.Caption)));
        sql.Add(',' + quotedstr(MStkIds));
        sql.Add(')  ');
        execsql;
      end;

      with ADOQueryCmd do
      begin
        Close;
        Sql.Clear;
        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;
      PlayWav('正确');
    except
      ADOQueryCmd.Connection.RollbackTrans;
      raise Exception.Create(PChar(Exception(ExceptObject).Message));
    end;

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

end;

procedure TfrmTradeClothOutEdit.ScanCIID();
var
  MStkIds, MSubIds: string;
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('此条码不包含在待出库的卷数据中');

    try
      ADOQueryCmd.Connection.BeginTrans;
      MStkIds := (Trim(CDS_1.FieldByName('StkId').AsString));
      MSubIds := (Trim(CDS_1.FieldByName('SubId').AsString));
      MoveCDS(CDS_1, CDS_3);
      CDS_1.Delete;

      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('insert into Trade_Need_Up(UType,UDataId,UOperation,UOperator,UModule,UDetails)       ');
        sql.Add('values(''贸易成品出库''    ');
        sql.Add(',' + quotedstr(MSubIds));
        sql.Add(',''出库''   ');
        sql.Add(',' + quotedstr(DName));
        sql.Add(',' + quotedstr(trim(self.Caption)));
        sql.Add(',' + quotedstr(MStkIds));
        sql.Add(')  ');
        execsql;
      end;

      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(OutNo.Text));
        sql.Add(',''出库''   ');
        sql.Add(',' + quotedstr(DName));
        sql.Add(',' + quotedstr(trim(self.Caption)));
        sql.Add(',' + quotedstr(MStkIds));
        sql.Add(')  ');
        execsql;
      end;

      with ADOQueryCmd do
      begin
        Close;
        Sql.Clear;
        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;
      PlayWav('正确');
    except
      ADOQueryCmd.Connection.RollbackTrans;
      raise Exception.Create(PChar(Exception(ExceptObject).Message));
    end;

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

end;

procedure TfrmTradeClothOutEdit.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  inherited;
  FreeAndNil(lstPacketId1);
  FreeAndNil(lstPacketId3);
  Action := caFree;
end;

procedure TfrmTradeClothOutEdit.FormCreate(Sender: TObject);
begin
  inherited;
  lstPacketId1 := TStringList.Create;
  lstPacketId3 := TStringList.Create;
end;

procedure TfrmTradeClothOutEdit.FormDestroy(Sender: TObject);
begin
  inherited;
  frmTradeClothOutEdit := nil;
end;

procedure TfrmTradeClothOutEdit.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid(self.caption + 'Tv1', Tv1, '成品仓库');
  ReadCxGrid(self.caption + 'Tv2', Tv2, '成品仓库');
  ReadCxGrid(self.caption + 'Tv3', Tv3, '成品仓库');
  InitGrid2();
  InitGrid1();
  InitGrid3();
  CRTime.DateTime := SGetServerDate(ADOQueryTemp);
end;

procedure TfrmTradeClothOutEdit.Tv1DataControllerFilterBeforeChange(Sender: TcxDBDataFilterCriteria; ADataSet: TDataSet; const AFilterText: string);
begin
  lstPacketId1.Clear;      //重新打开数据集前清空lstPacketId
end;

procedure TfrmTradeClothOutEdit.Tv1DataControllerSummaryFooterSummaryItemsSummary(ASender: TcxDataSummaryItems; Arguments: TcxSummaryEventArguments; var OutArguments: TcxSummaryEventOutArguments);
var
  BaoID: string;
begin
  //对 VC_PacketId 列的合计进行处理
  if TcxGridDBTableSummaryItem(Arguments.SummaryItem).Column = V1C_PacketId then
  begin
    BaoID := vartostr(ASender.DataController.Values[Arguments.RecordIndex, V1C_PacketId.Index]);
    if BaoID = '' then
      Exit;
    if BaoID = null then
      Exit;
    if lstPacketId1.IndexOf(BaoID) = -1 then
    begin
      lstPacketId1.Add(BaoID);   //记下之前未出现过的包编号
    end;
  end;
end;

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

procedure TfrmTradeClothOutEdit.Tv2CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  if CDS_3.IsEmpty = False then
    Exit;
  if CDS_2.IsEmpty then
    Exit;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  CDS_1.DisableControls;
  with CDS_1 do
  begin
    First;
    while not Eof do
    begin
      if Trim(CDS_1.fieldbyname('SubId').AsString) = Trim(CDS_2.fieldbyname('SubId').AsString) then
      begin
        CDS_1.Delete;
      end
      else
        Next;
    end;
  end;
  CDS_1.EnableControls;
  CDS_2.Delete;
end;

procedure TfrmTradeClothOutEdit.Tv3DataControllerFilterBeforeChange(Sender: TcxDBDataFilterCriteria; ADataSet: TDataSet; const AFilterText: string);
begin
  lstPacketId3.Clear;      //重新打开数据集前清空lstPacketId
end;

procedure TfrmTradeClothOutEdit.Tv3DataControllerSummaryFooterSummaryItemsSummary(ASender: TcxDataSummaryItems; Arguments: TcxSummaryEventArguments; var OutArguments: TcxSummaryEventOutArguments);
var
  BaoID: string;
begin
  //对 VC_PacketId 列的合计进行处理
  if TcxGridDBTableSummaryItem(Arguments.SummaryItem).Column = V3C_PacketId then
  begin
    BaoID := vartostr(ASender.DataController.Values[Arguments.RecordIndex, V3C_PacketId.Index]);
    if BaoID = '' then
      Exit;
    if BaoID = null then
      Exit;
    if lstPacketId3.IndexOf(BaoID) = -1 then
    begin
      lstPacketId3.Add(BaoID);   //记下之前未出现过的包编号
    end;
  end;
end;

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

procedure TfrmTradeClothOutEdit.CDS_1BeforeOpen(DataSet: TDataSet);
begin
  lstPacketId1.Clear;
end;

procedure TfrmTradeClothOutEdit.CDS_3BeforeOpen(DataSet: TDataSet);
begin
  lstPacketId3.Clear;
end;

procedure TfrmTradeClothOutEdit.edtScanKeyPress(Sender: TObject; var Key: Char);
var
  maxno, SMFlag, KYCKFlag, FMainId, ff: string;
  FFQty: Integer;
begin
  if Key = #13 then
  begin
    FScan := TRIM(edtScan.Text);
    edtScan.Text := '';
    if Copy(FScan, 1, 2) = 'RK' then
    begin
      ScanInNo();
    end
    else
    begin
      ScanCIID();
    end;
  end;
end;

procedure TfrmTradeClothOutEdit.ToolButton10Click(Sender: TObject);
begin
  SelOKNo(CDS_1, False);

end;

procedure TfrmTradeClothOutEdit.ToolButton11Click(Sender: TObject);
begin
  SelOKNo(CDS_3, True);

end;

procedure TfrmTradeClothOutEdit.ToolButton12Click(Sender: TObject);
begin
  SelOKNo(CDS_3, False);

end;

procedure TfrmTradeClothOutEdit.ToolButton1Click(Sender: TObject);
var
  MStkIds, MIOIDS, MSubIDS: string;
begin
  if CDS_3.IsEmpty then
    Exit;
  if Application.MessageBox('确定要执行此操作吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  MStkIds := '';
  MIOIDS := '';
  MSubIDS := '';
  with CDS_3 do
  begin
    while CDS_3.Locate('SSel', True, []) do
    begin
      MStkIds := MStkIds + (Trim(CDS_3.FieldByName('StkId').AsString)) + ',';
      MIOIDS := MIOIDS + (Trim(CDS_3.FieldByName('IOID').AsString)) + ',';
      MSubIDS := MSubIDS + (Trim(CDS_3.FieldByName('SubID').AsString)) + ',';
      CDS_3.Delete;
    end;
  end;
  MStkIds := copy(MStkIds, 1, Length(MStkIds) - 1);
  MIOIDS := copy(MIOIDS, 1, Length(MIOIDS) - 1);
  try

    try
      ADOQueryCmd.Connection.BeginTrans;

      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('insert into Trade_Need_Up(UType,UDataId,UOperation,UOperator,UModule,UDetails)       ');
        sql.Add('values(''贸易成品出库''    ');
        sql.Add(',' + quotedstr(MSubIDS));
        sql.Add(',''撤销出库''   ');
        sql.Add(',' + quotedstr(DName));
        sql.Add(',' + quotedstr(trim(self.Caption)));
        sql.Add(',' + quotedstr(MStkIds));
        sql.Add(')  ');
        execsql;
      end;

      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(OutNo.Text));
        sql.Add(',''撤销出库''   ');
        sql.Add(',' + quotedstr(DName));
        sql.Add(',' + quotedstr(trim(self.Caption)));
        sql.Add(',' + quotedstr(MStkIds));
        sql.Add(')  ');
        execsql;
      end;
      with ADOQueryCmd do
      begin
        Close;
        Sql.Clear;
        Sql.Add('exec P_Trade_Cloth_ReOut ');
        Sql.Add(' @StkIds=' + quotedstr(trim(MStkIds)));
        Sql.Add(',@IOIDS=' + quotedstr(trim(MIOIDS)));
        Sql.Add(',@FillId=' + Quotedstr(DCode));
        Sql.Add(',@Filler=' + Quotedstr(DName));
//        ShowMessage(sql.Text);
        Open;
      end;
      if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
      begin
        raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
      end;
      ADOQueryCmd.Connection.CommitTrans;
      PlayWav('正确');
      InitGrid1();
    except
      ADOQueryCmd.Connection.RollbackTrans;
      raise Exception.Create(PChar(Exception(ExceptObject).Message));
    end;
  except
    InitGrid1();
    InitGrid3();
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
//    PlayWav('发货错误');
  end;

end;

procedure TfrmTradeClothOutEdit.ToolButton2Click(Sender: TObject);
var
  MStkIds, MSubIds: string;
begin
  try
    if Trim(OutNo.Text) = '' then
      raise Exception.Create('出库单号不能为空!');
    if CDS_2.IsEmpty then
      raise Exception.Create('未选单不能扫描出库!');

    if not CDS_1.Locate('SSel', True, []) then
      raise Exception.Create('请选择数据');

    MStkIds := '';
    MSubIds := '';
    with CDS_1 do
    begin
      while CDS_1.Locate('SSel', True, []) = True do
      begin
        MStkIds := MStkIds + (Trim(CDS_1.FieldByName('StkId').AsString)) + ',';
        MSubIds := MSubIds + (Trim(CDS_1.FieldByName('SubId').AsString)) + ',';
        CDS_1.Delete;
      end;
    end;
    MStkIds := copy(MStkIds, 1, Length(MStkIds) - 1);

    try
      ADOQueryCmd.Connection.BeginTrans;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('insert into Trade_Need_Up(UType,UDataId,UOperation,UOperator,UModule,UDetails)       ');
        sql.Add('values(''贸易成品出库''    ');
        sql.Add(',' + quotedstr(MSubIds));
        sql.Add(',''出库''   ');
        sql.Add(',' + quotedstr(DName));
        sql.Add(',' + quotedstr(trim(self.Caption)));
        sql.Add(',' + quotedstr(MStkIds));
        sql.Add(')  ');
        execsql;
      end;

      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(OutNo.Text));
        sql.Add(',''出库''   ');
        sql.Add(',' + quotedstr(DName));
        sql.Add(',' + quotedstr(trim(self.Caption)));
        sql.Add(',' + quotedstr(MStkIds));
        sql.Add(')  ');
        execsql;
      end;

      with ADOQueryCmd do
      begin
        Close;
        Sql.Clear;
        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;
      PlayWav('正确');
      InitGrid3();
    except
      ADOQueryCmd.Connection.RollbackTrans;
      raise Exception.Create(PChar(Exception(ExceptObject).Message));
    end;
  except
    InitGrid1();
    InitGrid3();
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
//    PlayWav('发货错误');
  end;

end;

procedure TfrmTradeClothOutEdit.ToolButton3Click(Sender: TObject);
begin
  if Trim(OutNo.Text) = '' then
    Exit;

  try
    frmLabelPrint := TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin
      FLMType := 'DyeClothOut1';
      FFiltration1 := Trim(Self.OutNo.Text);
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmLabelPrint.Free;
  end;

end;

procedure TfrmTradeClothOutEdit.ToolButton4Click(Sender: TObject);
begin
  FSubIds := '';
  InitGrid2();
end;

procedure TfrmTradeClothOutEdit.btnGetOutNoClick(Sender: TObject);
var
  maxNo: string;
begin
  if GetLSNo(ADOQueryCmd, maxNo, 'CK', 'CK_BanCP_CR', 3, 1) = False then
  begin
    Application.MessageBox('取出库单号失败!', '提示', 0);
    Exit;
  end;
  OutNo.Text := Trim(maxNo);
  CRTime.DateTime := SGetServerDate(ADOQueryTemp);
  InitGrid3();
end;

procedure TfrmTradeClothOutEdit.ToolButton6Click(Sender: TObject);
var
  maxno, fsj: string;
begin

  try
    frmTradeClothWaitReleased := TfrmTradeClothWaitReleased.Create(Application);
    with frmTradeClothWaitReleased do
    begin
      if ShowModal = 1 then
      begin
        CDS_OrderSel.DisableControls;
        with CDS_OrderSel do
        begin
          First;
          while not Eof do
          begin
            if FieldByName('SSel').Value = True then
            begin
              if Self.CDS_2.Locate('SubId', Trim(CDS_OrderSel.fieldbyname('SubId').AsString), []) = False then
              begin
                with Self.CDS_2 do
                begin
                  Append;
                  FieldByName('OrderNo').Value := Trim(CDS_OrderSel.fieldbyname('OrderNo').AsString);
                  FieldByName('MainId').Value := Trim(CDS_OrderSel.fieldbyname('MainId').AsString);
                  FieldByName('SubId').Value := Trim(CDS_OrderSel.fieldbyname('SubId').AsString);
                  FieldByName('CustNo').Value := Trim(CDS_OrderSel.fieldbyname('CustNo').AsString);
                  FieldByName('CustName').Value := Trim(CDS_OrderSel.fieldbyname('CustName').AsString);
                  FieldByName('C_Name').Value := Trim(CDS_OrderSel.fieldbyname('C_Name').AsString);
                  FieldByName('OrdPiece').Value := CDS_OrderSel.fieldbyname('OrdPiece').Value;
                  FieldByName('OrdQty').Value := CDS_OrderSel.fieldbyname('OrdQty').Value;
                  FieldByName('OrdUnit').Value := Trim(CDS_OrderSel.fieldbyname('OrdUnit').AsString);
                  FieldByName('C_Color').Value := Trim(CDS_OrderSel.fieldbyname('C_Color').AsString);
                  FieldByName('C_Width').Value := Trim(CDS_OrderSel.fieldbyname('C_Width').AsString);
                  FieldByName('C_GramWeight').Value := Trim(CDS_OrderSel.fieldbyname('C_GramWeight').AsString);
                  FieldByName('C_Pattern').Value := Trim(CDS_OrderSel.fieldbyname('C_Pattern').AsString);
                  Post;
                end;
              end;
            end;
            Next;
          end;
        end;
        CDS_OrderSel.EnableControls;
      end;
    end;
  finally
    frmTradeClothWaitReleased.Free;
  end;

  FSubIds := '';
  CDS_2.DisableControls;
  with CDS_2 do
  begin
    First;
    while not Eof do
    begin
      FSubIds := FSubIds + Quotedstr(Trim(CDS_2.FieldByName('SubId').AsString)) + ',';
      Next;
    end;
  end;
  FSubIds := copy(FSubIds, 1, Length(FSubIds) - 1);
  CDS_2.EnableControls;

  if Trim(OutNo.Text) = '' then
    btnGetOutNo.Click;

  InitGrid1();
  InitGrid3();
end;

procedure TfrmTradeClothOutEdit.ToolButton7Click(Sender: TObject);
begin
  tv1.Controller.EditingController.ShowEdit();
  tv3.Controller.EditingController.ShowEdit();
  Close;

end;

procedure TfrmTradeClothOutEdit.ToolButton8Click(Sender: TObject);
begin
  WriteCxGrid(self.caption + 'Tv2', Tv2, '成品仓库');
  WriteCxGrid(self.caption + 'Tv1', Tv1, '成品仓库');
  WriteCxGrid(self.caption + 'Tv3', Tv3, '成品仓库');
end;

procedure TfrmTradeClothOutEdit.ToolButton9Click(Sender: TObject);
begin
  SelOKNo(CDS_1, True);

end;

end.

