unit U_BMQXManage;

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,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, U_BaseList, dxDateRanges,
  dxBarBuiltInMenu, RM_BarCode, dxScrollbarAnnotations, dxSkinsCore, dxSkinWXI,
  dxSkinsDefaultPainters;

type
  TfrmBMQXManage = class(TfrmBaseList)
    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;
    N2: TMenuItem;
    Panel1: TPanel;
    Udept: TEdit;
    RMXLSExport2: TRMXLSExport;
    Label1: TLabel;
    v2Column12: TcxGridDBColumn;
    ADOQueryPrt: TADOQuery;
    Panel2: TPanel;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    ToolBar2: TToolBar;
    ToolButton6: TToolButton;
    ToolButton7: TToolButton;
    CDS_1: TClientDataSet;
    DS_1: TDataSource;
    RMBarCodeObject1: TRMBarCodeObject;
    Tv2Column1: TcxGridDBColumn;
    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 cxTabControl1Change(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure CustomerChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure Tv2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
  private
    { Private declarations }
    procedure PrintReport(FZDYNo: string);
    procedure InitGrid();
    procedure InitBDJT();
  public
    fFlag: integer;
    { Public declarations }
    RKFlag, FCYID, fmanage: string;
  end;

var
  frmBMQXManage: TfrmBMQXManage;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_QXSel;

{$R *.dfm}
procedure TfrmBMQXManage.InitBDJT();
begin

  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select A.* from  User_BGQX A  ');
    sql.Add(' where UDept=' + quotedstr(trim(Self.CDS_HZ.fieldbyname('UDept').AsString)));
    sql.Add(' AND UserName=' + quotedstr(trim(Self.CDS_HZ.fieldbyname('UserName').AsString)));
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_1);
  SInitCDSData(ADOQueryTemp, CDS_1);

end;

procedure TfrmBMQXManage.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 TfrmBMQXManage.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select * from sy_user  ');
      Open;
    end;
    SCreateCDS(ADOQueryMain, CDS_HZ);
    SInitCDSData(ADOQueryMain, CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
    ToolButton2.Click;
  end;
end;

procedure TfrmBMQXManage.FormDestroy(Sender: TObject);
begin
  inherited;
  frmBMQXManage := nil;
end;

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

procedure TfrmBMQXManage.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(Trim(Self.Caption), Tv2, '机台管理');
  Close;
end;

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

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

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

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

procedure TfrmBMQXManage.N1Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    exit;
  with CDS_HZ do
  begin
    DisableControls;
    first;
    while not eof do
    begin
      edit;
      fieldbyname('ssel').Value := true;
      post;
      next;
    end;
    First;
    EnableControls;
  end;
end;

procedure TfrmBMQXManage.N2Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    exit;
  with CDS_HZ do
  begin
    DisableControls;
    first;
    while not eof do
    begin
      edit;
      fieldbyname('ssel').Value := false;
      post;
      next;
    end;
    First;
    EnableControls;
  end;
end;

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

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

procedure TfrmBMQXManage.ToolButton6Click(Sender: TObject);
var
  maxno: string;
begin

  try
    frmQXSel := TfrmQXSel.create(self);
    with frmQXSel do
    begin
      if showmodal = 1 then
      begin
        CDS_HZ.First;
        while not CDS_HZ.eof do
        begin
          if CDS_HZ.fieldbyname('SSEL').AsBoolean then
          begin
            if GetLSNo(Self.ADOQueryTemp, maxno, 'QX', 'User_BGQX', 3, 1) = False then
            begin
              Application.MessageBox('取最大号失败!', '提示', 0);
              Exit;
            end;
            with Self.ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              sql.Add(' insert into User_BGQX(QXID,UDEPT,GX,UserName) ');
              sql.Add(' values(' + quotedstr(trim(maxno)));
              sql.Add(',' + quotedstr(trim(Self.CDS_HZ.fieldbyname('UDEPT').AsString)));
              sql.Add(',' + quotedstr(trim(CDS_HZ.fieldbyname('PSType').AsString)));
              sql.Add(',' + quotedstr(trim(Self.CDS_HZ.fieldbyname('UserName').AsString)));
              sql.Add(') ');
              ExecSQL;
            end;
          end;

          CDS_HZ.Next;
        end;
      end;
    end;
  finally
    frmQXSel.Free;
    InitBDJT();
  end;
end;

procedure TfrmBMQXManage.ToolButton7Click(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    Exit;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete User_BGQX where QXID=' + QuotedStr(CDS_1.FieldByName('QXID').AsString));
    ExecSQL;
  end;
  CDS_1.Delete;
end;

procedure TfrmBMQXManage.Tv2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  InitBDJT();
end;

end.

