unit U_YarnMX;

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,
  cxLookAndFeels, cxLookAndFeelPainters,  
  cxNavigator, dxDateRanges, dxBarBuiltInMenu, U_BaseList, 
  dxScrollbarAnnotations, cxContainer,   
      
    
     
     
    
    
    
    
    
    
     
      
     
   
    
   cxMaskEdit, U_Basehelp, System.ImageList, Vcl.ImgList;

type
  TfrmYarnMX = class(TfrmBasehelp)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    CDS_Main: TClientDataSet;
    CDS_HZ: TClientDataSet;
    PopupMenu1: TPopupMenu;
    ToolButton1: TToolButton;
    Label4: TLabel;
    BanNo2: TcxTextEdit;
    ToolButton2: TToolButton;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Label6: TLabel;
    MXID: TcxTextEdit;
    TV1Column1: TcxGridDBColumn;
    TV1Column2: TcxGridDBColumn;
    TV1Column3: TcxGridDBColumn;
    TV1Column4: TcxGridDBColumn;
    ToolButton3: TToolButton;
    PopupMenu2: TPopupMenu;
    MenuItem1: TMenuItem;
    MenuItem2: TMenuItem;
    Panel2: TPanel;
    ToolButton4: TToolButton;
    Label1: TLabel;
    XGBanNo: TcxTextEdit;
    Button1: TButton;
    Button2: TButton;
    TV1Column5: TcxGridDBColumn;
    Label2: TLabel;
    BanID: TcxTextEdit;
    ToolButton5: TToolButton;
    TV1Column6: TcxGridDBColumn;
    TV1Column7: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoMChange(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure MPRTCodeNameChange(Sender: TObject);
    procedure SXBatchNOChange(Sender: TObject);
    procedure FromCoNameChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure ToolButton2Click(Sender: TObject);
    procedure FromCoNamePropertiesChange(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure MenuItem1Click(Sender: TObject);
    procedure MenuItem2Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
  private
    canshu1, FStkName, canshu3: string;
    procedure InitGrid();

    { Private declarations }
  public
    FBCId, FMXID: string;
    { Public declarations }
  end;

var
  frmYarnMX: TfrmYarnMX;

implementation

uses
  U_DataLink, U_RTFun, U_LabelPrint;

{$R *.dfm}

procedure TfrmYarnMX.FormDestroy(Sender: TObject);
begin
  inherited;
  frmYarnMX := nil;
end;

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

procedure TfrmYarnMX.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add(' select A.*,BanNo2=cast(BanNO AS Varchar) ');
      sql.Add(' from BS_Yarn_IO_MX A    ');
      sql.Add(' where BYIOID=''' + Trim(FBCId) + '''');
//      ShowMessage(sql.text);
      Open;
    end;
    SCreateCDS(ADOQueryMain, CDS_Main);
    SInitCDSData(ADOQueryMain, CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

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

procedure TfrmYarnMX.Button1Click(Sender: TObject);
begin

  with ADOQueryCmd do
  begin
    Close;
    Sql.Clear;
    Sql.Add('update BS_Yarn_IO_MX set BanNO=' + trim(XGBanNo.text) + '');
    Sql.Add(', BanID=(select top 1 BanID from BS_Yarn_IO_MX where BaoNO=' + trim(XGBanNo.text) + '');
    sql.Add(' ) where EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + quotedstr(FMXID) + ','','') X where BS_Yarn_IO_MX.MXID=X.RTValue )  ');
//    ShowMessage(sql.text);
    ExecSQL;
  end;

  FMXID := '';
  XGBanNo.text := '';
  cxGrid1.Enabled := true;
  Panel2.Visible := false;

  InitGrid();
end;

procedure TfrmYarnMX.Button2Click(Sender: TObject);
begin
  FMXID := '';
  XGBanNo.text := '';
  cxGrid1.Enabled := true;
  Panel2.Visible := false;
end;

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

procedure TfrmYarnMX.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmYarnMX.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid(trim(Self.Caption) + '2', Tv1, '染色坯布仓库');
  InitGrid();
end;

procedure TfrmYarnMX.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 TfrmYarnMX.MenuItem1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, True);
end;

procedure TfrmYarnMX.MenuItem2Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, false);
end;

procedure TfrmYarnMX.MPRTCodeNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmYarnMX.SXBatchNOChange(Sender: TObject);
begin
  TBFind.Click;
end;

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

procedure TfrmYarnMX.FromCoNamePropertiesChange(Sender: TObject);
begin
  TBFind.Click;
end;

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

procedure TfrmYarnMX.ToolButton2Click(Sender: TObject);
begin
  WriteCxGrid(trim(Self.Caption) + '2', Tv1, '染色坯布仓库');
end;

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

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

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

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

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

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

procedure TfrmYarnMX.ToolButton4Click(Sender: TObject);
var
  MMXID: string;
begin
  MMXID := SelCDSKey(CDS_Main, ['MXID'])[0];

  if MMXID = '' then
    Exit;

  FMXID := MMXID;
  cxGrid1.Enabled := false;
  Panel2.Visible := true;
end;

procedure TfrmYarnMX.ToolButton5Click(Sender: TObject);
var
  MMXID: string;
begin
  MMXID := SelCDSKey(CDS_Main, ['MXID'])[0];

  if MMXID = '' then
    Exit;

  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      Sql.Clear;
      Sql.Add('delete from BS_Yarn_IO_MX ');
      sql.Add('where IOFlag =''已入库'' and EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + quotedstr(MMXID) + ','','') X where BS_Yarn_IO_MX.MXID=X.RTValue )  ');
      ExecSQL;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
//      sql.Add('Update BS_Yarn_IO Set StkPiece=(select Sum(isnull(Piece,0)*IOQtyFlag) from BS_Yarn_IO A where A.STKID=BS_Yarn_IO.BYIOID and A.STKName =''纱线'')');
//      sql.Add(',STKQty=(select Sum(isnull(Qty,0)*IOQtyFlag) from BS_Yarn_IO A where A.STKID=BS_Yarn_IO.BYIOID and A.STKName =''纱线'')');
//      sql.Add(',Piece=(select count(*) from BS_Yarn_IO_MX A where A.BYIOID=BS_Yarn_IO.BYIOID )');
//      sql.Add('where BYIOID=''' + Trim(FBCId) + '''');

      sql.Add('Update BS_Yarn_IO Set StkPiece=(select count(*) from BS_Yarn_IO_MX X where X.BYIOID=A.BYIOID and X.IOFlag =''已入库'' and isnull(X.BaoQty,0) > 0 )');
      sql.Add(',STKQty=(select Sum(BaoQty) from BS_Yarn_IO_MX X where X.BYIOID=A.BYIOID and X.IOFlag =''已入库'' and isnull(X.BaoQty,0) > 0 )');
      sql.Add(',Piece=(select count(*) from BS_Yarn_IO_MX A where A.BYIOID=BS_Yarn_IO.BYIOID )');
      sql.Add('from BS_Yarn_IO A');
      sql.Add('where BYIOID=''' + Trim(FBCId) + '''');
      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(MMXID)));
      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 TfrmYarnMX.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  ModalResult := 1;
end;

end.

