unit U_YarnInList2;

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, cxButtonEdit, cxTextEdit, cxPC,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxDateRanges,
  dxBarBuiltInMenu, U_BaseList, dxScrollbarAnnotations, cxContainer, dxCore,
  cxDateUtils, cxMaskEdit, FrameDateSel, U_InputBoxSingleNumber, dxSkinsCore,
  dxSkinsDefaultPainters, cxProgressBar;

type
  TfrmYarnInList2 = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DS_1: TDataSource;
    GPM_1: TcxGridPopupMenu;
    CDS_Main: TClientDataSet;
    TBDel: TToolButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    Label3: TLabel;
    Label4: TLabel;
    Label8: TLabel;
    Label12: TLabel;
    Y_Name: TcxTextEdit;
    FromCoName: TcxTextEdit;
    Label10: TLabel;
    BCPH: TcxTextEdit;
    ToolButton1: TToolButton;
    Label6: TLabel;
    BYIOID: TcxTextEdit;
    ToolButton2: TToolButton;
    Label5: TLabel;
    StkCoName: TcxTextEdit;
    Frame11: TfrmFrameDateSel;
    IOType: TcxComboBox;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    GYSPH: TcxTextEdit;
    Label1: TLabel;
    ToolButton5: TToolButton;
    Label2: TLabel;
    JGPH: TcxTextEdit;
    Y_Spec: TcxComboBox;
    Panel2: TPanel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    Tv1Column9: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column12: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column14: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Panel4: TPanel;
    cxGrid2: TcxGrid;
    TvMX: TcxGridDBTableView;
    vMXColumn2: TcxGridDBColumn;
    vMXColumn1: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    ToolBar2: TToolBar;
    ToolButton10: TToolButton;
    ToolButton11: TToolButton;
    BanNO: TEdit;
    CDS_MX: TClientDataSet;
    DS_MX: TDataSource;
    ToolButton6: TToolButton;
    PopupMenu2: TPopupMenu;
    MenuItem1: TMenuItem;
    MenuItem2: TMenuItem;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    Label7: TLabel;
    Label9: TLabel;
    Y_Code: TcxTextEdit;
    Yarn_Name: TcxTextEdit;
    Tv1Column11: TcxGridDBColumn;
    Tv1Column13: TcxGridDBColumn;
    TvMXColumn2: TcxGridDBColumn;
    ToolButton7: TToolButton;
    ToolButton8: TToolButton;
    TvMXColumn1: TcxGridDBColumn;
    Tv1Column15: TcxGridDBColumn;
    Tv1Column16: TcxGridDBColumn;
    Tv1Column17: TcxGridDBColumn;
    Tv1Column18: 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 TBFindClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure BYIOIDChange(Sender: TObject);
    procedure Y_specChange(Sender: TObject);
    procedure FromCoNameChange(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure BYIOIDPropertiesChange(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton10Click(Sender: TObject);
    procedure ToolButton11Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure cxGridDBColumn8PropertiesEditValueChanged(Sender: TObject);
    procedure MenuItem1Click(Sender: TObject);
    procedure MenuItem2Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
  private
    canshu1, FStkName, canshu3: string;
    procedure InitGrid();
    procedure setStatus();
    { Private declarations }
  public
    SXPrice: Double;
    { Public declarations }
  end;

implementation

uses
  U_DataLink, U_RTFun, U_YarnInEdit, U_LabelPrint, U_YarnOutReturnList,
  U_YarnOutReturnList1, U_YarnMX;

{$R *.dfm}
procedure TfrmYarnInList2.setStatus();
begin

  if canshu1 = '管理' then
  begin
    Tv1Column16.Visible := true;
    Tv1Column17.Visible := true;
    Tv1Column18.Visible := true;

    Tv1Column16.Hidden := False;
    Tv1Column17.Hidden := False;
    Tv1Column18.Hidden := False;

  end
  else
  begin
    Tv1Column16.Visible := False;
    Tv1Column17.Visible := False;
    Tv1Column18.Visible := False;

    Tv1Column16.Hidden := true;
    Tv1Column17.Hidden := true;
    Tv1Column18.Hidden := true;
  end;

end;

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

procedure TfrmYarnInList2.FormCreate(Sender: TObject);
begin
  inherited;
  Frame11.EndDate.Date := SGetServerDate(ADOQueryTemp);
  Frame11.BegDate.Date := Frame11.EndDate.Date - 30;

  canshu1 := Trim(self.fParameters1);
  FStkName := Trim(self.fParameters2);
  canshu3 := Trim(self.fParameters3);
end;

procedure TfrmYarnInList2.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add(' select A.* ');
      sql.Add(' from BS_Yarn_IO A');
      sql.add(' where A.IOTime>=''' + Trim(FormatDateTime('yyyy-MM-dd', Frame11.BegDate.Date)) + '''');
      sql.Add(' and A.IOTime<''' + Trim(FormatDateTime('yyyy-MM-dd', Frame11.enddate.Date + 1)) + '''');
      sql.Add(' and isnull(StkName,'''')=''' + Trim(FStkName) + '''');
      SQL.Add(' and IOFlag=''入库''  ');
      SQL.Add(' order by IOTime desc');
      Open;
    end;
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS(ADOQueryMain, CDS_Main);
    SInitCDSData(ADOQueryMain, CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmYarnInList2.MenuItem1Click(Sender: TObject);
begin
  SelOKNo(CDS_MX, True);
end;

procedure TfrmYarnInList2.MenuItem2Click(Sender: TObject);
begin
  SelOKNo(CDS_MX, False);
end;

procedure TfrmYarnInList2.TBRafreshClick(Sender: TObject);
begin
  ToolBar1.SetFocus;
  InitGrid();
end;

procedure TfrmYarnInList2.ToolButton10Click(Sender: TObject);
var
  i, y: Integer;
  MaxNo, MMXID, MBYIOID: string;
  str, str2: string;
begin

  if StrToIntdef(BanNo.text, 0) = 0 then
  begin
    Application.MessageBox('请正确输入板号', '提示', 0);
    Exit;
  end;

  if InputQuery('输入包数', '输入包数', str) then
  begin
    MBYIOID := Trim(CDS_MAIN.fieldbyname('BYIOID').AsString);

    if GetLSNo(ADOQueryTemp, MaxNo, 'Bao', 'BS_Yarn_IO_MX', 3, 1) = False then
    begin
      Application.MessageBox('取最大号失败!', '提示', 0);
      Exit;
    end;
    y := CDS_MX.RecordCount;
    try
      ADOQueryCmd.Connection.BeginTrans;
      for i := 1 to strtointdef(str, 1) do
      begin

        y := y + 1;
        MMXID := Trim(MaxNo) + Trim(inttostr(i));
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('insert into  BS_Yarn_IO_MX (BYIOID,MXID,BaoQty,BanNO,BaoNO)');
          sql.Add('values(' + quotedstr(MBYIOID));
          sql.Add(',' + quotedstr(Trim(MMXID)));
          sql.Add(',0');
          sql.Add(',' + trim(BanNo.text));
          sql.Add(',' + quotedstr(INTTOSTR(y)));
          sql.Add(')');
//          ShowMessage(sql.text);
          ExecSQL;
        end;
      end;
      ADOQueryCmd.Connection.CommitTrans;
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        if CDS_Main.IsEmpty = false then
          sql.Add('select * from BS_Yarn_IO_MX where BYIOID=''' + Trim(CDS_Main.fieldbyname('BYIOID').AsString) + '''')
        else
          sql.Add('select * from BS_Yarn_IO_MX where 1=2');
        Open;
      end;
      SCreateCDS(ADOQueryTemp, CDS_MX);
      SInitCDSData(ADOQueryTemp, CDS_MX);
    except
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('添加失败!', '提示', 0);
    end;

  end;

end;

procedure TfrmYarnInList2.ToolButton11Click(Sender: TObject);
var
  MMXID: string;
begin
  if CDS_MX.IsEmpty then
    Exit;
  if CDS_MX.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;

  MMXID := '';
  CDS_MX.DisableControls;
  with CDS_MX do
  begin
    First;
    while CDS_MX.Locate('SSel', True, []) do
    begin

      MMXID := MMXID + ',' + (Trim(CDS_MX.fieldbyname('MXID').AsString));

      Edit;
      FieldByName('SSel').Value := False;
      Post;
    end;
  end;
  CDS_MX.EnableControls;

  if CDS_MX.ISEMPTY = FALSE then
  begin
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('delete BS_Yarn_IO_MX  where EXISTS (select SP.RTValue from [dbo].[F_Tool_SplitString](' + quotedstr(Trim(MMXID)));
      sql.Add(','','') SP where SP.RTValue=BS_Yarn_IO_MX.MXID )');
      ExecSQL;
    end;
    CDS_MX.Delete;
  end;

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    if CDS_Main.IsEmpty = false then
      sql.Add('select * from BS_Yarn_IO_MX where BYIOID=''' + Trim(CDS_Main.fieldbyname('BYIOID').AsString) + '''')
    else
      sql.Add('select * from BS_Yarn_IO_MX where 1=2');
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_MX);
  SInitCDSData(ADOQueryTemp, CDS_MX);
end;

procedure TfrmYarnInList2.ToolButton1Click(Sender: TObject);
begin
  WriteCxGrid(Self.Caption + '1', Tv1, '纱线仓库');
end;

procedure TfrmYarnInList2.ToolButton2Click(Sender: TObject);
var
  MBYIOID: string;
begin
  if CDS_Main.IsEmpty then
    Exit;
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;

  MBYIOID := '';
  CDS_Main.DisableControls;
  with CDS_Main do
  begin
    First;
    while CDS_Main.Locate('SSel', True, []) do
    begin

      MBYIOID := MBYIOID + ',' + (Trim(CDS_Main.fieldbyname('BYIOID').AsString));

      Edit;
      FieldByName('SSel').Value := False;
      Post;
    end;
  end;
  CDS_Main.EnableControls;

  try
    frmLabelPrint := TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin
      FLMType := 'BSYarnInPrt1';
      FFiltration1 := MBYIOID;
      if ShowModal = 1 then
      begin
//        Self.InitGrid();
      end;
    end;
  finally
    frmLabelPrint.Free;
  end;
end;

procedure TfrmYarnInList2.ToolButton3Click(Sender: TObject);
var
  MBYIOIDs: string;
begin
  MBYIOIDs := SelCDSKey(CDS_Main, ['BYIOID'])[0];
  if MBYIOIDs = '' then
    Exit;

  try
    frmInputBoxSingleNumber := TfrmInputBoxSingleNumber.Create(Application);
    with frmInputBoxSingleNumber do
    begin
      if ShowModal = 1 then
      begin
        if StrToFloatDef(frmInputBoxSingleNumber.Price.Text, 0) = 0 then
          frmInputBoxSingleNumber.Price.Text := '0';

        try
          ADOQueryCmd.Connection.BeginTrans;
          with ADOQueryCmd do
          begin
            Close;
            Sql.Clear;
            Sql.Add('update BS_Yarn_IO set TestDenier=' + frmInputBoxSingleNumber.Price.Text);
            sql.Add('where EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + quotedstr(MBYIOIDs) + ','','') X where BS_Yarn_IO.BYIOID=X.RTValue )  ');
            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('D数调整')));
            sql.Add(',' + quotedstr(trim(MBYIOIDs) + '   D数：' + frmInputBoxSingleNumber.Price.Text));
            sql.Add(',' + quotedstr(trim('成功')));
            sql.Add(')');
            ExecSQL;
          end;
          ADOQueryCmd.Connection.CommitTrans;

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

procedure TfrmYarnInList2.ToolButton4Click(Sender: TObject);
begin
  try
    frmYarnOutReturnList := TfrmYarnOutReturnList.Create(Application);
    with frmYarnOutReturnList do
    begin
      FStkName := Self.FStkName;
      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmYarnOutReturnList.Free;
  end;
end;

procedure TfrmYarnInList2.ToolButton5Click(Sender: TObject);
begin
  try
    frmYarnOutReturnList1 := TfrmYarnOutReturnList1.Create(Application);
    with frmYarnOutReturnList1 do
    begin
      FStkName := Self.FStkName;
      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmYarnOutReturnList1.Free;
  end;

end;

procedure TfrmYarnInList2.ToolButton6Click(Sender: TObject);
var
  MBanID: string;
begin
  if CDS_MX.IsEmpty then
    Exit;
  if CDS_MX.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;

  MBanID := '';
  CDS_MX.DisableControls;
  with CDS_MX do
  begin
    First;
    while CDS_MX.Locate('SSel', True, []) do
    begin

      MBanID := MBanID + ',' + (Trim(CDS_MX.fieldbyname('BanID').AsString));

      Edit;
      FieldByName('SSel').Value := False;
      Post;
    end;
  end;
  CDS_MX.EnableControls;

  try
    frmLabelPrint := TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin
      FLMType := 'BanMX';
      FFiltration1 := MBanID;
      if ShowModal = 1 then
      begin

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

procedure TfrmYarnInList2.ToolButton7Click(Sender: TObject);
var
  MBanID: string;
begin
  if CDS_MX.IsEmpty then
    Exit;
  if CDS_MX.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;

  MBanID := '';
  CDS_MX.DisableControls;
  with CDS_MX do
  begin
    First;
    while CDS_MX.Locate('SSel', True, []) do
    begin

      MBanID := MBanID + ',' + (Trim(CDS_MX.fieldbyname('BanID').AsString));

      Edit;
      FieldByName('SSel').Value := False;
      Post;
    end;
  end;
  CDS_MX.EnableControls;

  try
    frmLabelPrint := TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin
      FLMType := 'BaoMX2';
      FFiltration1 := MBanID;
      if ShowModal = 1 then
      begin

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

procedure TfrmYarnInList2.ToolButton8Click(Sender: TObject);
begin
  try
    frmYarnMX := TfrmYarnMX.Create(Application);
    with frmYarnMX do
    begin
      FBCId := Trim(self.CDS_Main.fieldbyname('BYIOID').AsString);
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmYarnMX.Free;
  end;
end;

procedure TfrmYarnInList2.Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    if CDS_Main.IsEmpty = false then
    begin
      sql.Add(' select BYIOID,BanID,BanNo,COUNT(*) AS GS  from BS_Yarn_IO_MX where BYIOID=''' + Trim(CDS_Main.fieldbyname('BYIOID').AsString) + '''');
      sql.Add(' GROUP BY BYIOID,BanID,BanNo ');
    end
    else
      sql.Add('select * from BS_Yarn_IO_MX where 1=2');
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_MX);
  SInitCDSData(ADOQueryTemp, CDS_MX);
end;

procedure TfrmYarnInList2.BYIOIDPropertiesChange(Sender: TObject);
begin
  TBFind.Click;
end;

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

procedure TfrmYarnInList2.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmYarnInList2.FormShow(Sender: TObject);
var
  mSql: string;
begin
  inherited;
  mSql := 'select distinct name=A.IOType from BS_Yarn_IO A where IOFlag=''入库'' and isnull(STKNAME,'''')=''' + Trim(FSTKNAME) + ''' ';
  SInitTcxComBoxBySql(ADOQueryTemp, IOType, false, mSql);
  ReadCxGrid(Self.Caption + '1', Tv1, '纱线仓库');
  setStatus();
  InitGrid();
end;

procedure TfrmYarnInList2.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    exit;
  TcxGridToExcel(FStkName + '入库列表', cxGrid1);
end;

procedure TfrmYarnInList2.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS(ADOQueryMain, CDS_Main);
    SInitCDSData(ADOQueryMain, CDS_Main);
  end;
end;

procedure TfrmYarnInList2.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, True);
end;

procedure TfrmYarnInList2.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, False);
end;

procedure TfrmYarnInList2.TBDelClick(Sender: TObject);
var
  MBYIOID: string;
begin
  if CDS_Main.IsEmpty then
    Exit;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  MBYIOID := '';
  CDS_Main.DisableControls;
  with CDS_Main do
  begin
    First;
    while CDS_Main.Locate('SSel', True, []) do
    begin

      MBYIOID := MBYIOID + ',' + Trim(CDS_Main.fieldbyname('BYIOID').AsString);
      CDS_Main.Delete;
    end;
  end;
  CDS_Main.EnableControls;

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('exec P_Fin_Flow_Judge ');
    Sql.Add(' @FFIDS=' + quotedstr(MBYIOID));
    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(MBYIOID));
      sql.Add(',''删除''   ');
      sql.Add(',' + quotedstr(DName));
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(MBYIOID));
      sql.Add(')  ');

      Sql.Add('exec P_BS_Yarn_In_Del ');
      Sql.Add(' @BYIOIDS=' + quotedstr(MBYIOID));
      Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
      Sql.Add(',@DName=' + quotedstr(Trim(DName)));
      Open;
    end;
    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
      raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
    ADOQueryCmd.Connection.CommitTrans;
    InitGrid();
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;

end;

procedure TfrmYarnInList2.TBAddClick(Sender: TObject);
begin
  try
    frmYarnInEdit := TfrmYarnInEdit.Create(Application);
    with frmYarnInEdit do
    begin
      FBCId := '';
      frmYarnInEdit.canshu3 := Trim(Self.canshu3);
      FStkName := Self.FStkName;

      canshu1 := Self.canshu1;
      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmYarnInEdit.Free;
  end;
end;

procedure TfrmYarnInList2.TBEditClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('exec P_Fin_Flow_Judge ');
    Sql.Add(' @FFIDS=' + quotedstr(Trim(CDS_Main.fieldbyname('BYIOID').AsString)));
    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;
  with ADOQueryTemp do
  begin
    Close;
    Sql.Clear;
    Sql.Add('exec P_BS_Yarn_In_Edit ');
    Sql.Add(' @BYIOIDS=' + quotedstr(Trim(CDS_Main.fieldbyname('BYIOID').AsString)));
    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
    frmYarnInEdit := TfrmYarnInEdit.Create(Application);
    with frmYarnInEdit do
    begin
      FBCId := Trim(CDS_Main.fieldbyname('BYIOID').AsString);
      TBDel.Visible := False;
      TBAdd.Visible := False;
      FStkName := Self.FStkName;

      canshu1 := Self.canshu1;
      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmYarnInEdit.Free;
  end;
end;

procedure TfrmYarnInList2.BYIOIDChange(Sender: TObject);
begin
  if Length(Trim(BYIOID.Text)) < 4 then
  begin
    if Trim(BYIOID.Text) <> '' then
      Exit;
  end;
  TBFind.Click;
end;

procedure TfrmYarnInList2.Y_specChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmYarnInList2.FromCoNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmYarnInList2.cxGridDBColumn8PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin

  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(TvMX.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_MX do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;
  end;

  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('  update BS_Yarn_IO_MX set ' + FFieldName + ' = ' + mvalue);
    sql.Add('  where MXID=' + quotedstr(Trim(CDS_MX.FieldByName('MXID').AsString)));
    ExecSQL;
  end;
  TvMX.Controller.EditingController.ShowEdit();
end;

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

end.

