unit U_MachineBaoYang;

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
  TfrmMachineBaoYang = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ToolButton2: TToolButton;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    Panel1: TPanel;
    MachNo: TEdit;
    RMXLSExport2: TRMXLSExport;
    v2Column8: TcxGridDBColumn;
    Label1: TLabel;
    ToolButton3: TToolButton;
    ADOQueryPrt: TADOQuery;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    ToolButton6: TToolButton;
    ToolButton7: TToolButton;
    cxGrid1: TcxGrid;
    TV3: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    DS_Sub: TDataSource;
    CDS_Sub: TClientDataSet;
    ADOQuerySub: TADOQuery;
    TV3Column1: TcxGridDBColumn;
    ToolButton8: TToolButton;
    TV3Column2: TcxGridDBColumn;
    ToolButton9: TToolButton;
    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 Tv2MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure TV3Column1PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
    procedure TV3Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton9Click(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
  frmMachineBaoYang: TfrmMachineBaoYang;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp, U_BMSel;

{$R *.dfm}

procedure TfrmMachineBaoYang.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 TfrmMachineBaoYang.InitGrid();
begin
  MachNo.SetFocus;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select * from KH_ZDY where Type=''JQLX'' ');
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_HZ);
    SInitCDSData20(ADOQueryMain, CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
    ToolButton2.Click;
  end;
end;

procedure TfrmMachineBaoYang.InitSub();
begin
  with ADOQuerySub do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select A.* from  MachineBY A where MachType= ' + QuotedStr(CDS_HZ.FieldByName('ZDYName').AsString));
//    ShowMessage(sql.text);
    Open;
  end;
  SCreateCDS20(ADOQuerySub, CDS_Sub);
  SInitCDSData20(ADOQuerySub, CDS_Sub);
end;

procedure TfrmMachineBaoYang.FormDestroy(Sender: TObject);
begin
  frmMachineBaoYang := nil;
end;

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

procedure TfrmMachineBaoYang.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(Trim(Self.Caption) + '1', Tv2, '机台管理');
  WriteCxGrid(Trim(Self.Caption) + '2', Tv3, '机台管理');
  Close;
end;

procedure TfrmMachineBaoYang.FormShow(Sender: TObject);
begin
  ReadCxGrid(Trim(Self.Caption) + '1', Tv2, '机台管理');
  ReadCxGrid(Trim(Self.Caption) + '2', Tv3, '机台管理');
//  Enddate.DateTime:=SGetServerDate(ADOQueryTemp);
//  begdate.DateTime:=Enddate.DateTime-30;
  InitGrid();
  InitSub();
end;

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

procedure TfrmMachineBaoYang.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 TfrmMachineBaoYang.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  SelExportData(Tv2, ADOQueryMain, trim(Self.Caption));
end;

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

procedure TfrmMachineBaoYang.N1Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    exit;
  SelOKNo(CDS_HZ, True);
end;

procedure TfrmMachineBaoYang.N2Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    exit;
  SelOKNo(CDS_HZ, False);
end;

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

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

procedure TfrmMachineBaoYang.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;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate MachineBY ');
      sql.Add(' Set ' + FFieldName + '=''' + Trim(mvalue) + '''');
      sql.Add(' , Editer=''' + Trim(DName) + '''');
      sql.Add(' , Edittime=getdate()');
      sql.Add(' where BYId=' + quotedstr(CDS_Sub.fieldbyname('BYId').AsString));
    //  ShowMessage(sql.text);
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    tv3.Controller.EditingController.ShowEdit();
  except
    tv3.Controller.EditingController.ShowEdit();
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end
end;

procedure TfrmMachineBaoYang.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 TfrmMachineBaoYang.ToolButton4Click(Sender: TObject);
var
  maxId: string;
begin
  MachNo.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 TfrmMachineBaoYang.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 TfrmMachineBaoYang.ToolButton6Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    Exit;
end;

procedure TfrmMachineBaoYang.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 TfrmMachineBaoYang.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 TfrmMachineBaoYang.ToolButton7Click(Sender: TObject);
var
  maxId: string;
begin
  MachNo.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) + ')');
   // ShowMessage(sql.text);
    ExecSQL;
  end;
  InitSub();
end;

procedure TfrmMachineBaoYang.Tv2MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  InitSub();
end;

procedure TfrmMachineBaoYang.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));
//      ShowMessage(sql.text);
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    tv3.Controller.EditingController.ShowEdit();
  except
    tv3.Controller.EditingController.ShowEdit();
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end
end;

procedure TfrmMachineBaoYang.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 TfrmMachineBaoYang.TV3Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  frmBMSel := TfrmBMSel.Create(Self);
  with frmBMSel do
  begin
    if ShowModal = 1 then
    begin
      CDS_Sub.Edit;
      CDS_Sub.FieldByName('ZRBM').AsString := ClientDataSet1.fieldbyname('Udept').AsString;
      CDS_Sub.Post;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('UPdate MachineBY ');
        sql.Add(' Set ZRBM=''' + CDS_Sub.FieldByName('ZRBM').AsString + '''');
        sql.Add(' , Editer=''' + Trim(DName) + '''');
        sql.Add(' , Edittime=getdate()');
        sql.Add(' where BYId=' + quotedstr(CDS_Sub.fieldbyname('BYId').AsString));
//        ShowMessage(sql.text);
        ExecSQL;
      end;
    end;
  end;
end;

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

end.

