unit U_MLX;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxGraphics, cxCustomData, cxStyles, cxTL, cxMaskEdit, DB, ADODB,
  cxInplaceContainer, cxDBTL, cxControls, cxTLData, ComCtrls, ToolWin, StdCtrls,
  cxFilter, cxData, cxDataStorage, cxEdit, cxDBData, DBClient,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, ExtCtrls,
  cxSplitter, cxGridLevel, cxClasses, cxGridCustomView, cxGrid,
  cxGridCustomPopupMenu, cxGridPopupMenu, RM_Dataset, RM_System, RM_Common,
  RM_Class, RM_GridReport, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdFTP, ShellAPI, IniFiles, cxCheckBox, cxCalendar, cxButtonEdit,
  cxTextEdit, cxPC, cxCheckComboBox, cxDropDownEdit, Menus, RM_e_Xls, TeEngine,
  Series, TeeProcs, Chart, DbChart, cxLookAndFeels, cxLookAndFeelPainters,
  cxNavigator;

type
  TfrmMLX = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ToolButton2: TToolButton;
    ADOQueryMain: TADOQuery;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    Panel1: TPanel;
    RMXLSExport2: TRMXLSExport;
    v2Column8: TcxGridDBColumn;
    Label1: TLabel;
    ADOQueryPrt: TADOQuery;
    cxGrid1: TcxGrid;
    TV3: TcxGridDBTableView;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    DS_Sub: TDataSource;
    CDS_Sub: TClientDataSet;
    ADOQuerySub: TADOQuery;
    TV3Column1: TcxGridDBColumn;
    TV3Column2: TcxGridDBColumn;
    Tv2Column1: TcxGridDBColumn;
    N3: TMenuItem;
    PopupMenu2: TPopupMenu;
    MenuItem1: TMenuItem;
    MenuItem2: TMenuItem;
    N2: TMenuItem;
    GYSJC: TComboBox;
    Tv2Column2: TcxGridDBColumn;
    cxGridPopupMenu1: TcxGridPopupMenu;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure CustomerChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure v2Column8PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure TV3Column1PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
    procedure ToolButton9Click(Sender: TObject);
    procedure Tv2Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure N3Click(Sender: TObject);
    procedure MenuItem1Click(Sender: TObject);
    procedure MenuItem2Click(Sender: TObject);
    procedure GYSJCChange(Sender: TObject);
    procedure Tv2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure Tv2Column2PropertiesEditValueChanged(Sender: TObject);
  private
    { Private declarations }
    procedure PrintReport(FZDYNo: string);
    procedure InitGrid();
    procedure InitSub();
    procedure EditValueT();
    procedure EditValueF();
  public
    fFlag: integer;
    { Public declarations }
    RKFlag, FCYID, fmanage: string;
  end;

var
  frmMLX: TfrmMLX;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp, U_GYSList;

{$R *.dfm}

procedure TfrmMLX.PrintReport(FZDYNo: string);
var
  fPrintFile, FFCYID: string;
  i, j: Integer;
  Txt, fImagePath: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin
  if CDS_HZ.IsEmpty then
    Exit;

  ExportFtErpFile('机台标签.rmf', ADOQueryTemp);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\机台标签.rmf';
  with ADOQueryPrt do
  begin
    close;
    sql.Clear;
    sql.Add(' select * from  BS_Machine where MachId=''' + Trim(FZDYNo) + '''');
    open;
  end;
  try
    Moudle := LoadLibrary('MakeQRBarcode.dll');
    @Makebar := GetProcAddress(Moudle, 'Make');
    @Mixtext := GetProcAddress(Moudle, 'MixText');
    Txt := Trim(FZDYNo);
    fImagePath := ExtractFilePath(Application.ExeName) + 'image\temp.bmp';
    if not DirectoryExists(pchar(ExtractFilePath(Application.ExeName) + 'image')) then
      CreateDirectory(pchar(ExtractFilePath(Application.ExeName) + 'image'), nil);
    if FileExists(fImagePath) then
      DeleteFile(fImagePath);
    Makebar(pchar(Txt), Length(Txt), 3, 3, 0, PChar(fImagePath), 3);
  except
    CDS_HZ.EnableControls;
    application.MessageBox('条形码生成失败！', '提示信息', MB_ICONERROR);
    exit;
  end;
  RMVariables['QRBARCODE'] := fImagePath;
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RM1.PrintReport;
  //  RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile + '!'), '提示', 0);
    Exit;
  end;
end;

procedure TfrmMLX.InitGrid();
begin
  GYSJC.SetFocus;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select * from M_GYS order by XHINT');
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_HZ);
    SInitCDSData20(ADOQueryMain, CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
    ToolButton2.Click;
  end;


  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select distinct(GYSJC) AS name ,XHINT from M_GYS order by XHINT ');
    Open;
  end;
  GYSJC.Items.Clear; //清空
  GYSJC.Items.Add(Trim(''));
  while not ADOQueryCmd.eof do
  begin
    GYSJC.Items.Add(ADOQueryCmd.fieldbyname('name').AsString);
    ADOQueryCmd.next;
  end;
end;

procedure TfrmMLX.InitSub();
begin
  with ADOQuerySub do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select A.* from  M_LX A where GYSID= ' + QuotedStr(CDS_HZ.FieldByName('GYSID').AsString));
    Open;
  end;
  SCreateCDS20(ADOQuerySub, CDS_Sub);
  SInitCDSData20(ADOQuerySub, CDS_Sub);

end;

procedure TfrmMLX.FormDestroy(Sender: TObject);
begin
  frmMLX := nil;
end;

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

procedure TfrmMLX.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(Trim(Self.Caption), Tv2, '棉类别');
  WriteCxGrid(Trim(Self.Caption) + '1', Tv3, '棉类别');
  Close;
end;

procedure TfrmMLX.FormShow(Sender: TObject);
begin
  ReadCxGrid(Trim(Self.Caption), Tv2, '棉类别');
  ReadCxGrid(Trim(Self.Caption) + '1', Tv3, '棉类别');
  InitGrid();
  InitSub();
end;

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

procedure TfrmMLX.ToolButton2Click(Sender: TObject);
var
  sql: string;
begin
  if ADOQueryMain.Active then
  begin
    sql := SGetFilters(Panel1, 1, 2);
    SDofilter(ADOQueryMain, sql);
    SCreateCDS20(ADOQueryMain, CDS_HZ);
    SInitCDSData20(ADOQueryMain, CDS_HZ);
  end;
end;

procedure TfrmMLX.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  SelExportData(Tv2, ADOQueryMain, trim(Self.Caption));
end;

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

procedure TfrmMLX.N1Click(Sender: TObject);
var
  maxno, FGYSNo, FGYSJC: string;
begin

  try
    frmGYSList := TfrmGYSList.Create(Application);
    with frmGYSList do
    begin
      KHType.Text := '棉厂';
      KHType.TxtCode := '棉厂';
      if ShowModal = 1 then
      begin
        FGYSNo := Trim(frmGYSList.Order_Main.fieldbyname('KHNo').AsString);
        FGYSJC := Trim(frmGYSList.Order_Main.fieldbyname('KHNameJC').AsString);
      end;
    end;
  finally
    frmGYSList.Free;
  end;

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select * from M_GYS  where GYSNO=''' + Trim(FGYSNo) + '''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = false then
  begin
    Application.MessageBox('同一客户不能重复添加!', '提示', 0);
    Exit;
  end;

  if GetLSNo(ADOQueryCmd, maxno, 'M', 'M_GYS', 4, 1) = False then
  begin
    Application.MessageBox('取最大号失败！', '提示', 0);
    Exit;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('insert into M_GYS(GYSID,GYSNo,GYSJC) values(' + quotedstr(Trim(maxno)) + ',' + quotedstr(Trim(FGYSNo)) + ',' + quotedstr(Trim(FGYSJC)) + ')');
    sql.Add('insert into M_Tree(CPID,CPName,CPParent,CPLevel) values(' + quotedstr(Trim(maxno)) + ',' + quotedstr(Trim(FGYSJC)) + ',' + quotedstr(Trim('YX001')) + ',' + '2' + ')');
    ExecSQL;
  end;
  InitGrid();
end;

procedure TfrmMLX.N2Click(Sender: TObject);
begin
  TV3.OptionsData.Editing := true;
end;

procedure TfrmMLX.CustomerChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmMLX.FormCreate(Sender: TObject);
begin
  fmanage := Trim(DParameters1);
end;

procedure TfrmMLX.v2Column8PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv3.Controller.FocusedColumn.DataBinding.FilterFieldName);
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_Sub do
    begin
      Edit;
      FieldByName(FFieldName).Value := Trim(mvalue);
      Post;
    end;

//    if Trim(FFieldName) = 'KHMName' then
//    begin
//      with ADOQueryCmd do
//      begin
//        Close;
//        sql.Clear;
//        sql.Add('select * from M_LX ');
//        sql.Add(' where KHMName=' + quotedstr(Trim(mvalue)));
//        sql.Add(' and GYSID=' + quotedstr(Trim(CDS_HZ.FieldByName('GYSID').AsString)));
//        open;
//      end;
//      if ADOQueryCmd.IsEmpty = false then
//      begin
//        ADOQueryCmd.Connection.RollbackTrans;
//        Application.MessageBox('同一客户的棉名称(公司内部)不能重复添加!', '提示', 0);
//        InitSub();
//        Exit;
//      end;
//    end;

//    if Trim(FFieldName) = 'GYSMName' then
//    begin
//      with ADOQueryCmd do
//      begin
//        Close;
//        sql.Clear;
//        sql.Add('select * from M_LX ');
//        sql.Add(' where GYSMName=' + quotedstr(Trim(mvalue)));
//        sql.Add(' and GYSID=' + quotedstr(Trim(CDS_HZ.FieldByName('GYSID').AsString)));
//        open;
//      end;
//      if ADOQueryCmd.IsEmpty = false then
//      begin
//        ADOQueryCmd.Connection.RollbackTrans;
//        Application.MessageBox('同一客户的棉名称(供应单位)不能重复添加!', '提示', 0);
//        InitSub();
//        Exit;
//      end;
//    end;

    if Trim(FFieldName) = 'MNo' then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from M_LX ');
        sql.Add(' where MNo=' + quotedstr(Trim(mvalue)));
        sql.Add(' and GYSID=' + quotedstr(Trim(CDS_HZ.FieldByName('GYSID').AsString)));
        open;
      end;
      if ADOQueryCmd.IsEmpty = false then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('同一客户的棉代码不能重复添加!', '提示', 0);
        InitSub();
        Exit;
      end;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate M_LX ');
      sql.Add(' Set ' + FFieldName + '=''' + Trim(mvalue) + '''');
      sql.Add(' where MLXID=' + quotedstr(CDS_Sub.fieldbyname('MLXID').AsString));
      ExecSQL;
    end;
    if Trim(FFieldName) = 'KHMName' then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('UPdate M_Tree ');
        sql.Add(' Set DefStr1=''' + Trim(mvalue) + '''');
        sql.Add(' where CPID=' + quotedstr(CDS_Sub.fieldbyname('MLXID').AsString));
        ExecSQL;
      end;
    end;

    if Trim(FFieldName) = 'MNo' then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('UPdate M_Tree ');
        sql.Add(' Set CPNo=''' + Trim(mvalue) + '''');
        sql.Add(' where CPID=' + quotedstr(CDS_Sub.fieldbyname('MLXID').AsString));
        ExecSQL;
      end;

     //如果该类型下已经有产品，则改产品的CPNO和BPCode
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from M_ZDY ');
        sql.Add(' where CPID=' + quotedstr(Trim(CDS_Sub.fieldbyname('MLXID').AsString)));
//        ShowMessage(sql.text);
        open;
      end;
      if ADOQueryTemp.IsEmpty = False then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add(' update M_ZDY set CPNo=''' + trim(mvalue) + '''');
          sql.Add(',BPCode=''M''+BPKZ+''' + trim(mvalue) + '''');
          sql.Add(' where CPID=' + quotedstr(Trim(CDS_Sub.fieldbyname('MLXID').AsString)));
//          ShowMessage(sql.text);
          ExecSQL;
        end;
      end;
    end;

    if Trim(FFieldName) = 'GYSMName' then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('UPdate M_Tree ');
        sql.Add(' Set CPName=''' + Trim(mvalue) + '''');
        sql.Add(' where CPID=' + quotedstr(CDS_Sub.fieldbyname('MLXID').AsString));
        ExecSQL;
      end;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    tv3.Controller.EditingController.ShowEdit();
  except
    tv3.Controller.EditingController.ShowEdit();
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end;

  InitSub();
end;

procedure TfrmMLX.ToolButton3Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    Exit;
  if CDS_HZ.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('请先选择数据！', '提示', 0);
    Exit;
  end;

  with CDS_HZ do
  begin
    while CDS_HZ.Locate('SSel', True, []) = True do
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('update BS_Machine set EditFlag=0 where MachId=' + QuotedStr(CDS_HZ.FieldByName('MachId').AsString));
        ExecSQL;
      end;
      CDS_HZ.Edit;
      CDS_HZ.FieldByName('SSel').Value := False;
      CDS_HZ.Post;
      PrintReport(Trim(CDS_HZ.fieldbyname('MachId').AsString));
      Next;
    end;

  end;

end;

procedure TfrmMLX.ToolButton4Click(Sender: TObject);
var
  maxId: string;
begin
  GYSJC.SetFocus;
  if GetLSNo(ADOQueryCmd, maxId, 'M', 'BS_Machine', 4, 1) = False then
  begin
    Application.MessageBox('取最大号失败！', '提示', 0);
    Exit;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('insert into BS_Machine(MachId,Filler) values(' + quotedstr(Trim(maxId)) + ',' + quotedstr(Trim(dname)) + ')');
    ExecSQL;
  end;
  InitGrid();
end;

procedure TfrmMLX.ToolButton5Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    Exit;

  if CDS_HZ.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('请先选择数据！', '提示', 0);
    Exit;
  end;

  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;

  with CDS_HZ do
  begin
    while not Eof do
    begin
      if CDS_HZ.Locate('SSel', True, []) = True then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('delete BS_Machine where MachId=' + QuotedStr(CDS_HZ.FieldByName('MachId').AsString));
          ExecSQL;
        end;
      end;
      CDS_HZ.Edit;
      CDS_HZ.FieldByName('SSel').Value := False;
      CDS_HZ.Post;
      Next;
    end;
  end;
  InitGrid();
end;

procedure TfrmMLX.ToolButton6Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    Exit;
end;

procedure TfrmMLX.EditValueT();
var
  i: Integer;
begin
  for i := 0 to tv2.ColumnCount - 1 do
  begin
    if uppercase(tv2.Columns[i].DataBinding.FieldName) <> 'SSEL' then
      tv2.Columns[i].Options.Editing := True;
  end;
end;

procedure TfrmMLX.EditValueF();
var
  i: Integer;
begin
  for i := 0 to tv2.ColumnCount - 1 do
  begin
    if uppercase(tv2.Columns[i].DataBinding.FieldName) <> 'SSEL' then
      tv2.Columns[i].Options.Editing := false;
  end;
end;

procedure TfrmMLX.ToolButton7Click(Sender: TObject);
var
  maxId: string;
begin
  GYSJC.SetFocus;
  if GetLSNo(ADOQueryCmd, maxId, 'BY', 'MachineBY', 4, 1) = False then
  begin
    Application.MessageBox('取最大号失败！', '提示', 0);
    Exit;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('insert into MachineBY(BYId,Filler,filltime,MachType) values(' + quotedstr(Trim(maxId)) + ',' + quotedstr(Trim(dname)) + ',getdate()' + ',' + quotedstr(CDS_HZ.fieldbyname('ZDYName').AsString) + ')');
    ExecSQL;
  end;
  InitSub();
end;

procedure TfrmMLX.TV3Column1PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv3.Controller.FocusedColumn.DataBinding.FilterFieldName);
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_Sub do
    begin
      Edit;
      FieldByName(FFieldName).Value := Trim(mvalue);
      Post;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate MachineBY ');
      sql.Add(' Set ' + FFieldName + '=' + QuotedStr(Trim(mvalue)) + '');
      sql.Add(' , Editer=''' + Trim(DName) + '''');
      sql.Add(' , Edittime=getdate()');
      sql.Add(' where BYId=' + quotedstr(CDS_Sub.fieldbyname('BYId').AsString));
      ExecSQL;
    end;

    if trim(FFieldName) = 'BYZQ' then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('UPdate MachineBY ');
        sql.Add(' Set BYDate =LastTime + BYZQ');
        sql.Add(' where BYId=' + quotedstr(CDS_Sub.fieldbyname('BYId').AsString));
        ExecSQL;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    tv3.Controller.EditingController.ShowEdit();
  except
    tv3.Controller.EditingController.ShowEdit();
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end;
  InitSub();
end;

procedure TfrmMLX.ToolButton8Click(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then
    Exit;

  if CDS_Sub.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('请先选择数据！', '提示', 0);
    Exit;
  end;

  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;

  with CDS_Sub do
  begin
    while not Eof do
    begin
      if CDS_Sub.Locate('SSel', True, []) = True then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('delete MachineBY where BYId=' + QuotedStr(CDS_Sub.FieldByName('BYId').AsString));
          ExecSQL;
        end;
      end;
      CDS_Sub.Edit;
      CDS_Sub.FieldByName('SSel').Value := False;
      CDS_Sub.Post;
      Next;
    end;
  end;
  InitSub();
end;

procedure TfrmMLX.ToolButton9Click(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'JQLX';
      flagname := '机台类型';
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmZDYHelp.free;
  end;
end;

procedure TfrmMLX.Tv2Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
var
  FGYSNo, FGYSJC: string;
begin

  try
    frmGYSList := TfrmGYSList.Create(Application);
    with frmGYSList do
    begin
      KHType.Text := '棉厂';
      KHType.TxtCode := '棉厂';
      if ShowModal = 1 then
      begin
        FGYSNo := Trim(frmGYSList.Order_Main.fieldbyname('KHNo').AsString);
        FGYSJC := Trim(frmGYSList.Order_Main.fieldbyname('KHNameJC').AsString);
      end;
    end;
  finally
    frmGYSList.Free;
  end;

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate M_GYS ');
    sql.Add(' Set GYSNO=''' + Trim(FGYSNo) + '''');
    sql.Add(' , GYSJC=''' + Trim(FGYSJC) + '''');
    sql.Add(' where GYSID=' + quotedstr(CDS_HZ.fieldbyname('GYSID').AsString));

    sql.Add('UPdate M_Tree ');
    sql.Add(' Set CPName=''' + Trim(FGYSJC) + '''');
    sql.Add(' where CPID=' + quotedstr(CDS_HZ.fieldbyname('GYSID').AsString));
    ExecSQL;
  end;

  InitGrid();
end;

procedure TfrmMLX.N3Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    Exit;

  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete M_GYS where GYSID=' + QuotedStr(CDS_HZ.FieldByName('GYSID').AsString));
    sql.Add('delete M_Tree where CPID=' + QuotedStr(CDS_HZ.FieldByName('GYSID').AsString));
    ExecSQL;
  end;

  InitGrid();
end;

procedure TfrmMLX.MenuItem1Click(Sender: TObject);
var
  maxId: string;
begin
  GYSJC.SetFocus;
  if GetLSNo(ADOQueryCmd, maxId, 'MLX', 'M_LX', 4, 1) = False then
  begin
    Application.MessageBox('取最大号失败！', '提示', 0);
    Exit;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('insert into M_LX(MLXID,GYSID) values(' + quotedstr(Trim(maxId)) + ',' + quotedstr(Trim(CDS_HZ.FieldByName('GYSID').AsString)) + ')');
    sql.Add('insert into M_Tree(CPID,CPParent,CPLevel) values(' + quotedstr(Trim(maxId)) + ',' + quotedstr(Trim(CDS_HZ.FieldByName('GYSID').AsString)) + ',' + '3' + ')');
    ExecSQL;
  end;
  InitSub();
  TV3.OptionsData.Editing := true;
end;

procedure TfrmMLX.MenuItem2Click(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then
    Exit;

  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete M_LX where MLXID=' + QuotedStr(CDS_Sub.FieldByName('MLXID').AsString));
    sql.Add('delete M_Tree where CPID=' + QuotedStr(CDS_Sub.FieldByName('MLXID').AsString));
    ExecSQL;
  end;

  InitSub();
  TV3.OptionsData.Editing := false;
end;

procedure TfrmMLX.GYSJCChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmMLX.Tv2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  InitSub();
  TV3.OptionsData.Editing := False;
end;

procedure TfrmMLX.Tv2Column2PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_HZ do
    begin
      Edit;
      FieldByName(FFieldName).Value := Trim(mvalue);
      Post;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate M_GYS ');
      sql.Add(' Set XHINT=' + Trim(mvalue) + '');
      sql.Add(' where GYSID=' + quotedstr(CDS_HZ.fieldbyname('GYSID').AsString));

      sql.Add('UPdate M_Tree');
      sql.Add(' Set XHINT=' + Trim(mvalue) + '');
      sql.Add(' where CPID=' + quotedstr(CDS_HZ.fieldbyname('GYSID').AsString));
      ExecSQL;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    Tv2.Controller.EditingController.ShowEdit();
  except
    Tv2.Controller.EditingController.ShowEdit();
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end;

end;

end.

