unit U_ClothScheduleList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, ToolWin, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, ADODB,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel, cxClasses, cxControls,
  cxGridCustomView, cxGridCustomTableView, cxGridTableView, cxGridDBTableView,
  cxGrid, DBClient, cxCheckBox, cxCalendar, cxSplitter, RM_Dataset, RM_System,
  RM_Common, RM_Class, RM_GridReport, RM_e_Xls, Menus, cxPC, cxButtonEdit,
  cxTextEdit, cxDropDownEdit, cxLookAndFeels, cxLookAndFeelPainters, 
   cxNavigator, dxDateRanges, dxBarBuiltInMenu,
  U_BaseList, RM_e_Graphic, RM_e_Jpeg, RM_E_llPDF, Vcl.Clipbrd, 
  dxScrollbarAnnotations, FrameDateSel, cxContainer,  
      
     
     
     
    
    
    
    
    
    
     
      
     
   
    
   cxProgressBar;

type
  TfrmClothScheduleList = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    GPM_1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DS_1: TDataSource;
    Panel1: TPanel;
    Label4: TLabel;
    Label9: TLabel;
    GC_Name: TcxTextEdit;
    MasterOrderNo: TcxTextEdit;
    CDS_1: TClientDataSet;
    Label3: TLabel;
    FtyName: TcxTextEdit;
    pm1: TPopupMenu;
    ADOQueryImage: TADOQuery;
    ToolButton7: TToolButton;
    N3: TMenuItem;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    VC_SCSCode: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    N5: TMenuItem;
    N6: TMenuItem;
    frmFrameDateSel1: TfrmFrameDateSel;
    TV1Column1: TcxGridDBColumn;
    TV1Column2: TcxGridDBColumn;
    TV1Column3: TcxGridDBColumn;
    TV1Column7: TcxGridDBColumn;
    TV1Column8: TcxGridDBColumn;
    ToolButton1: TToolButton;
    ToolBar2: TToolBar;
    ToolButton2: TToolButton;
    ToolButton4: TToolButton;
    DS_2: TDataSource;
    CDS_2: TClientDataSet;
    GPM_2: TcxGridPopupMenu;
    ADO_2: TADOQuery;
    TV1Column4: TcxGridDBColumn;
    TV1Column5: TcxGridDBColumn;
    TV1Column6: TcxGridDBColumn;
    TV1Column9: TcxGridDBColumn;
    TV1Column12: TcxGridDBColumn;
    ToolButton5: TToolButton;
    ToolButton6: TToolButton;
    cxPageControl1: TcxPageControl;
    cxTabSheet2: TcxTabSheet;
    cxTabSheet3: TcxTabSheet;
    cxTabSheet1: TcxTabSheet;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    TV2Column1: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    cxGridDBColumn13: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    TV2Column2: TcxGridDBColumn;
    ADO_3: TADOQuery;
    GPM_3: TcxGridPopupMenu;
    CDS_3: TClientDataSet;
    DS_3: TDataSource;
    ADO_4: TADOQuery;
    GPM_4: TcxGridPopupMenu;
    CDS_4: TClientDataSet;
    DS_4: TDataSource;
    cxGrid3: TcxGrid;
    TV3: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    cxGrid4: TcxGrid;
    TV4: TcxGridDBTableView;
    cxGridDBColumn17: TcxGridDBColumn;
    cxGridDBColumn18: TcxGridDBColumn;
    cxGridDBColumn19: TcxGridDBColumn;
    cxGridDBColumn20: TcxGridDBColumn;
    cxGridDBColumn21: TcxGridDBColumn;
    cxGridDBColumn22: TcxGridDBColumn;
    cxGridDBColumn23: TcxGridDBColumn;
    cxGridDBColumn24: TcxGridDBColumn;
    cxGridDBColumn25: TcxGridDBColumn;
    cxGridDBColumn26: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    TV3Column1: TcxGridDBColumn;
    TV3Column2: TcxGridDBColumn;
    TV4Column1: TcxGridDBColumn;
    TV4Column2: TcxGridDBColumn;
    TV1Column10: TcxGridDBColumn;
    TV1Column11: TcxGridDBColumn;
    TV1Column13: TcxGridDBColumn;
    TV1Column14: TcxGridDBColumn;
    ToolButton3: TToolButton;
    cxTabSheet4: TcxTabSheet;
    cxGrid5: TcxGrid;
    TV5: TcxGridDBTableView;
    cxGridDBColumn15: TcxGridDBColumn;
    cxGridDBColumn16: TcxGridDBColumn;
    cxGridDBColumn27: TcxGridDBColumn;
    cxGridDBColumn28: TcxGridDBColumn;
    cxGridDBColumn29: TcxGridDBColumn;
    cxGridDBColumn30: TcxGridDBColumn;
    cxGridDBColumn31: TcxGridDBColumn;
    cxGridDBColumn32: TcxGridDBColumn;
    cxGridDBColumn33: TcxGridDBColumn;
    cxGridDBColumn34: TcxGridDBColumn;
    cxGridDBColumn35: TcxGridDBColumn;
    cxGridDBColumn36: TcxGridDBColumn;
    cxGridLevel4: TcxGridLevel;
    TV5Column1: TcxGridDBColumn;
    TV5Column2: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    ADO_5: TADOQuery;
    GPM_5: TcxGridPopupMenu;
    CDS_5: TClientDataSet;
    DS_5: TDataSource;
    TV1Column15: TcxGridDBColumn;
    ToolButton8: TToolButton;
    Label1: TLabel;
    CraftCode: TcxTextEdit;
    TV5Column3: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure BuyNameChange(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure N5Click(Sender: TObject);
    procedure N6Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure TV1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure ToolButton4Click(Sender: TObject);
    procedure cxPageControl1Change(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure SellNamePropertiesChange(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
  private
    fFlileFlag: string;
    procedure InitGrid();
    procedure SetStatus();
    procedure InitImage();
    procedure InitGridIO();
    procedure DelGrid2IO();
    procedure DelGrid3IO();
    procedure DelGrid4IO();
    { Private declarations }
  public

    { Public declarations }
  end;

implementation

uses
  U_DataLink, U_RTFun, U_LabelPrint, U_TradeClothTransferInput,
  U_TradeClothProcessInput, U_TradeClothCompleteInput,
  U_TradeClothProcessOutInput, U_TradeClothCostPrice;

{$R *.dfm}

procedure TfrmClothScheduleList.DelGrid2IO();
var
  MBCIOID: string;
begin
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  MBCIOID := SelCDSKey(CDS_2, ['FromBCIOID'])[0];
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('exec P_Fin_Flow_Judge ');
    Sql.Add(' @FFIDS=' + quotedstr(MBCIOID));
    Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
    Sql.Add(',@DName=' + quotedstr(Trim(DName)));
    Open;
  end;

  if ADOQueryTemp.FieldByName('intReturn').AsInteger = -1 then
  begin
    Application.MessageBox(PChar(ADOQueryTemp.fieldbyname('ShowMsg').AsString), '提示', 0);
    exit;
  end;

  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(MBCIOID));
      sql.Add(',''删除''   ');
      sql.Add(',' + quotedstr(DName));
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(MBCIOID));
      sql.Add(')  ');

      Sql.Add('exec P_BS_Cloth_Out_Del ');
      Sql.Add(' @BCIOIDS=' + quotedstr(MBCIOID));
      Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
      Sql.Add(',@DName=' + quotedstr(Trim(DName)));
//      ShowMessage(sql.Text);
      Open;
    end;
    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
      raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
    ADOQueryCmd.Connection.CommitTrans;
    InitGridIO();
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    InitGridIO();
  end;

end;

procedure TfrmClothScheduleList.DelGrid3IO();
var
  MBCIOID: string;
begin
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  case cxPageControl1.ActivePageIndex of

    1:
      begin
        MBCIOID := SelCDSKey(CDS_3, ['BCIOID'])[0];
      end;

    3:
      begin
        MBCIOID := SelCDSKey(CDS_5, ['BCIOID'])[0];
      end;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('exec P_Fin_Flow_Judge ');
    Sql.Add(' @FFIDS=' + quotedstr(MBCIOID));
    Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
    Sql.Add(',@DName=' + quotedstr(Trim(DName)));
    Open;
  end;

  if ADOQueryTemp.FieldByName('intReturn').AsInteger = -1 then
  begin
    Application.MessageBox(PChar(ADOQueryTemp.fieldbyname('ShowMsg').AsString), '提示', 0);
    exit;
  end;

  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(MBCIOID));
      sql.Add(',''删除''   ');
      sql.Add(',' + quotedstr(DName));
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(MBCIOID));
      sql.Add(')  ');

      Sql.Add('exec P_BS_Cloth_Out_Del ');
      Sql.Add(' @BCIOIDS=' + quotedstr(MBCIOID));
      Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
      Sql.Add(',@DName=' + quotedstr(Trim(DName)));
//      ShowMessage(sql.Text);
      Open;
    end;
    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
      raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
    ADOQueryCmd.Connection.CommitTrans;
    InitGridIO();
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    InitGridIO();
  end;

end;

procedure TfrmClothScheduleList.DelGrid4IO();
var
  MBCIOID: string;
begin
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  MBCIOID := SelCDSKey(CDS_4, ['BCIOID'])[0];
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('exec P_Fin_Flow_Judge ');
    Sql.Add(' @FFIDS=' + quotedstr(MBCIOID));
    Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
    Sql.Add(',@DName=' + quotedstr(Trim(DName)));
    Open;
  end;

  if ADOQueryTemp.FieldByName('intReturn').AsInteger = -1 then
  begin
    Application.MessageBox(PChar(ADOQueryTemp.fieldbyname('ShowMsg').AsString), '提示', 0);
    exit;
  end;

  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(MBCIOID));
      sql.Add(',''删除''   ');
      sql.Add(',' + quotedstr(DName));
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(MBCIOID));
      sql.Add(')  ');

      Sql.Add('exec P_BS_Cloth_in_Del ');
      Sql.Add(' @BCIOIDS=' + quotedstr(MBCIOID));
      Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
      Sql.Add(',@DName=' + quotedstr(Trim(DName)));
//      ShowMessage(sql.Text);
      Open;
    end;
    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
      raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
    ADOQueryCmd.Connection.CommitTrans;
    InitGridIO();
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    InitGridIO();
  end;

end;

procedure TfrmClothScheduleList.InitGridIO();
var
  SqlStr: string;
begin
  case cxPageControl1.ActivePageIndex of
    0:
      begin
        SqlStr := ' select A.*';
        SqlStr := SqlStr + ' from BS_Cloth_IO A    ';
        SqlStr := SqlStr + 'where A.STKName = ''贸易布匹'' and A.IOFlag = ''入库'' and A.IOType = ''调拨入库''  ';
        SqlStr := SqlStr + 'and A.FromMasterOrdNo =  ' + quotedstr(Trim(CDS_1.fieldbyname('MasterOrderNo').AsString));
        InitCDSData(ADO_2, CDS_2, Tv2, SqlStr, '', '');
      end;
    1:
      begin
        SqlStr := ' select A.*';
        SqlStr := SqlStr + ' from BS_Cloth_IO A    ';
        SqlStr := SqlStr + 'where A.STKName = ''贸易布匹'' and A.IOFlag = ''出库'' and A.IOType = ''投产出库''  ';
        SqlStr := SqlStr + 'and A.ToMasterOrdNo =  ' + quotedstr(Trim(CDS_1.fieldbyname('MasterOrderNo').AsString));
        InitCDSData(ADO_3, CDS_3, Tv3, SqlStr, '', '');
      end;
    2:
      begin
        SqlStr := ' select A.*';
        SqlStr := SqlStr + ' from BS_Cloth_IO A    ';
        SqlStr := SqlStr + 'where A.STKName = ''贸易布匹'' and A.IOFlag = ''入库'' and A.IOType = ''加工完成''  ';
        SqlStr := SqlStr + 'and A.FromMasterOrdNo =  ' + quotedstr(Trim(CDS_1.fieldbyname('MasterOrderNo').AsString));
        InitCDSData(ADO_4, CDS_4, Tv4, SqlStr, '', '');
      end;
    3:
      begin
        SqlStr := ' select A.*';
        SqlStr := SqlStr + ' from BS_Cloth_IO A    ';
        SqlStr := SqlStr + 'where A.STKName = ''贸易布匹'' and A.IOFlag = ''出库'' and A.IOType <> ''投产出库''  ';
        SqlStr := SqlStr + 'and A.FromMasterOrdNo =  ' + quotedstr(Trim(CDS_1.fieldbyname('MasterOrderNo').AsString));
        InitCDSData(ADO_5, CDS_5, Tv5, SqlStr, '', '');
      end;
  end;
end;

procedure TfrmClothScheduleList.InitImage();
begin

  ADOQueryImage.Close;
  if CDS_1.IsEmpty then
    exit;
  with ADOQueryImage do
  begin
    close;
    sql.Clear;
    sql.Add('select * from TP_File A');
    sql.Add('where WBID=' + quotedstr(trim(CDS_1.fieldbyname('SubId').AsString)));
    sql.Add('and TFType=' + quotedstr(trim('HX')));
    open;
  end;
end;

procedure TfrmClothScheduleList.N1Click(Sender: TObject);
begin
  SelOKNoFiler(Tv1, True); //全选
end;

procedure TfrmClothScheduleList.N2Click(Sender: TObject);
begin
  SelOKNoFiler(Tv1, False); //全弃
end;

procedure TfrmClothScheduleList.N3Click(Sender: TObject);
begin
  Clipboard.SetTextBuf(PChar(Trim(CDS_1.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));
end;

procedure TfrmClothScheduleList.N5Click(Sender: TObject);
begin
  SelOKNo(CDS_1, True);
end;

procedure TfrmClothScheduleList.N6Click(Sender: TObject);
begin
  SelOKNo(CDS_1, False);

end;

procedure TfrmClothScheduleList.SellNamePropertiesChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmClothScheduleList.SetStatus();
begin

end;

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

procedure TfrmClothScheduleList.FormCreate(Sender: TObject);
begin
  inherited;
  frmFrameDateSel1.EndDate.Date := SGetServerDate(ADOQueryTemp);
  frmFrameDateSel1.BegDate.Date := frmFrameDateSel1.EndDate.Date - 90;

end;

procedure TfrmClothScheduleList.TBCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmClothScheduleList.InitGrid();
var
  SqlStr: string;
begin

  SqlStr := ' exec P_Trade_Plan_MasterSchedule ';

  SqlStr := SqlStr + '  @BegDate=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', frmFrameDateSel1.BegDate.Date)));
  SqlStr := SqlStr + ' ,@enddate=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', frmFrameDateSel1.enddate.Date + 1)));

  InitCDSData(ADOQueryMain, CDS_1, Tv1, SqlStr, SGetFilters(Panel1, 1, 2), 'MasterOrderNo');
end;

procedure TfrmClothScheduleList.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS(ADOQueryMain, CDS_1);
  SInitCDSData(ADOQueryMain, CDS_1);
end;

procedure TfrmClothScheduleList.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmClothScheduleList.FormShow(Sender: TObject);
begin
  inherited;
  fFlileFlag := UserDataFlag + 'HX';
  readCxGrid(trim(Self.Caption) + 'tv1', Tv1, '销售合同管理');
  readCxGrid(trim(Self.Caption) + 'tv2', Tv2, '销售合同管理');
  readCxGrid(trim(Self.Caption) + 'tv3', Tv3, '销售合同管理');
  readCxGrid(trim(Self.Caption) + 'tv4', Tv4, '销售合同管理');
  readCxGrid(trim(Self.Caption) + 'tv5', Tv5, '销售合同管理');
  SetStatus();
  InitGrid();
end;

procedure TfrmClothScheduleList.CheckBox1Click(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmClothScheduleList.CheckBox2Click(Sender: TObject);
begin
  TBRafresh.Click;
end;

procedure TfrmClothScheduleList.BuyNameChange(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS(ADOQueryMain, CDS_1);
  SInitCDSData(ADOQueryMain, CDS_1);
end;

procedure TfrmClothScheduleList.cxPageControl1Change(Sender: TObject);
begin
  InitGridIO();
end;

procedure TfrmClothScheduleList.cxTabControl1Change(Sender: TObject);
begin
  SetStatus();
  InitGrid();
end;

procedure TfrmClothScheduleList.ToolButton1Click(Sender: TObject);
var
  Maxno: string;
begin

  ToolBar1.SetFocus;
  if CDS_1.IsEmpty then
    Exit;
  if Application.MessageBox('确定要保存回仓数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;

  try
    ADOQueryCmd.Connection.BeginTrans;

    CDS_1.DisableControls;
    with CDS_1 do
    begin
      First;
      while not eof do
      begin
        if CDS_1.fieldbyname('BCHCQty').AsFloat > 0 then
        begin

          if not GetLSNo(ADOQueryCmd, Maxno, 'YR', 'BS_Yarn_IO', 3, 1) then
            raise Exception.Create('取入库编号失败！');

          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(Trim(CDS_1.fieldbyname('BYIOID').AsString)));
            sql.Add(',''删除''   ');
            sql.Add(',' + quotedstr(DName));
            sql.Add(',' + quotedstr(trim(self.Caption)));
            sql.Add(',' + quotedstr(Trim(CDS_1.fieldbyname('BYIOID').AsString)));
            sql.Add(')  ');

            Sql.Add('exec P_BS_Yarn_OutReturn ');

            Sql.Add(' @BYIOID=' + quotedstr(CDS_1.fieldbyname('BYIOID').AsString));
            Sql.Add(',@Piece=' + CDS_1.fieldbyname('BCHCPiece').AsString);
            Sql.Add(',@Qty=' + CDS_1.fieldbyname('BCHCQty').AsString);
            Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
            Sql.Add(',@DName=' + quotedstr(Trim(DName)));
            Sql.Add(',@Maxno=' + quotedstr(Trim(Maxno)));
            Open;
          end;
          if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
            raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));

        end;
        Next;
      end;
    end;
    CDS_1.EnableControls;

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

end;

procedure TfrmClothScheduleList.ToolButton2Click(Sender: TObject);
begin
  try
    frmTradeClothTransferInput := TfrmTradeClothTransferInput.Create(Application);
    with frmTradeClothTransferInput do
    begin
      GC_Name.Text := Trim(Self.CDS_1.fieldbyname('GC_Name').AsString);
      FMasterOrderNo := Trim(Self.CDS_1.fieldbyname('MasterOrderNo').AsString);
      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmTradeClothTransferInput.Free;
  end;
end;

procedure TfrmClothScheduleList.ToolButton3Click(Sender: TObject);
begin
  try
    frmTradeClothProcessOutInput := TfrmTradeClothProcessOutInput.Create(Application);
    with frmTradeClothProcessOutInput do
    begin
      FMasterOrderNo := Trim(Self.CDS_1.fieldbyname('MasterOrderNo').AsString);
      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmTradeClothProcessOutInput.Free;
  end;
end;

procedure TfrmClothScheduleList.ToolButton4Click(Sender: TObject);
begin
  case cxPageControl1.ActivePageIndex of
    0:
      begin
        DelGrid2IO();
      end;
    1:
      begin
        DelGrid3IO();
      end;
    2:
      begin
        DelGrid4IO();
      end;
    3:
      begin
        DelGrid3IO();
      end;
  end;
end;

procedure TfrmClothScheduleList.ToolButton5Click(Sender: TObject);
begin
  try
    frmTradeClothProcessInput := TfrmTradeClothProcessInput.Create(Application);
    with frmTradeClothProcessInput do
    begin
      FMasterOrderNo := Trim(Self.CDS_1.fieldbyname('MasterOrderNo').AsString);
      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmTradeClothProcessInput.Free;
  end;
end;

procedure TfrmClothScheduleList.ToolButton6Click(Sender: TObject);
begin
  try
    frmTradeClothCompleteInput := TfrmTradeClothCompleteInput.Create(Application);
    with frmTradeClothCompleteInput do
    begin
      FMasterOrderNo := Trim(CDS_1.fieldbyname('MasterOrderNo').AsString);
      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmTradeClothCompleteInput.Free;
  end;
end;

procedure TfrmClothScheduleList.ToolButton7Click(Sender: TObject);
begin
  writeCxGrid(trim(Self.Caption) + 'tv1', Tv1, '销售合同管理');
  writeCxGrid(trim(Self.Caption) + 'tv2', Tv2, '销售合同管理');
  writeCxGrid(trim(Self.Caption) + 'tv3', Tv3, '销售合同管理');
  writeCxGrid(trim(Self.Caption) + 'tv4', Tv4, '销售合同管理');
  writeCxGrid(trim(Self.Caption) + 'tv5', Tv5, '销售合同管理');
end;

procedure TfrmClothScheduleList.ToolButton8Click(Sender: TObject);
begin

  try
    frmTradeClothCostPrice := TfrmTradeClothCostPrice.Create(Application);
    with frmTradeClothCostPrice do
    begin
      FMasterOrderNo := Trim(self.CDS_1.fieldbyname('MasterOrderNo').AsString);
      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmTradeClothCostPrice.Free;
  end;
end;

procedure TfrmClothScheduleList.TV1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  InitGridIO();
end;

end.

