unit U_NameDY;

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, Buttons;

type
  TfrmNameDY = 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;
    Panel1: TPanel;
    RMXLSExport2: TRMXLSExport;
    v2Column8: TcxGridDBColumn;
    Label1: TLabel;
    ADOQueryPrt: TADOQuery;
    DS_Sub: TDataSource;
    CDS_Sub: TClientDataSet;
    ADOQuerySub: TADOQuery;
    Tv2Column1: TcxGridDBColumn;
    YGName: TComboBox;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Tv2Column2: TcxGridDBColumn;
    ToolButton1: TToolButton;
    ToolButton3: TToolButton;
    Panel2: TPanel;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    SpeedButton3: TSpeedButton;
    SpeedButton4: TSpeedButton;
    SpeedButton5: TSpeedButton;
    SpeedButton6: TSpeedButton;
    SpeedButton7: TSpeedButton;
    SpeedButton8: TSpeedButton;
    SpeedButton9: TSpeedButton;
    SpeedButton10: TSpeedButton;
    SpeedButton11: TSpeedButton;
    SpeedButton12: TSpeedButton;
    SpeedButton13: TSpeedButton;
    SpeedButton14: TSpeedButton;
    SpeedButton15: TSpeedButton;
    Image2: TImage;
    Label2: TLabel;
    FS: TComboBox;
    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 CustomerChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure YGNameChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure Image2Click(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
  private
    { Private declarations }
    procedure PrintReport(FZDYNo: string);
    procedure InitGrid();
    procedure initGX();
  public
    fFlag: integer;
    { Public declarations }
    RKFlag, FCYID, fmanage: string;
  end;

var
  frmNameDY: TfrmNameDY;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp, U_GYSList;

{$R *.dfm}

procedure TfrmNameDY.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 top 1 * from  OA_YG_DangAn where YGName=''' + 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.DefaultCopies := strtointdef(trim(FS.Text), 5);
    RM1.PrintReport;
  //  RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile + '!'), '提示', 0);
    Exit;
  end;
end;

procedure TfrmNameDY.InitGrid();
begin
  YGName.SetFocus;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select * from OA_YG_DangAn where FHType =''绣花工'' and YGType =''正式'' ');
      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(YGName) AS name from OA_YG_DangAn where FHType =''绣花工'' and YGType =''正式'' ');
    Open;
  end;
  YGName.Items.Clear; //清空
  YGName.Items.Add(Trim(''));
  while not ADOQueryCmd.eof do
  begin
    YGName.Items.Add(ADOQueryCmd.fieldbyname('name').AsString);
    ADOQueryCmd.next;
  end;
end;

procedure TfrmNameDY.FormDestroy(Sender: TObject);
begin
  frmNameDY := nil;
end;

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

procedure TfrmNameDY.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(Trim(Self.Caption), Tv2, '员工管理');

  Close;
end;

procedure TfrmNameDY.FormShow(Sender: TObject);
begin
  ReadCxGrid(Trim(Self.Caption), Tv2, '员工管理');
  InitGrid();
end;

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

procedure TfrmNameDY.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 TfrmNameDY.cxTabControl1Change(Sender: TObject);
begin
  InitGrid;
end;

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

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

procedure TfrmNameDY.YGNameChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmNameDY.ToolButton1Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    Exit;

  if CDS_HZ.Locate('ssel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;

  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_HZ.DisableControls;
    with CDS_HZ do
    begin
      First;
      while CDS_HZ.Locate('SSel', True, []) do
      begin
        if CDS_HZ.FieldByName('SSel').AsBoolean = True then
        begin

          PrintReport(Trim(CDS_HZ.fieldbyname('YGID').AsString));

          CDS_HZ.Edit;
          CDS_HZ.FieldByName('SSel').AsBoolean := false;
        end;
      end;
    end;
    CDS_HZ.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!', '提示', 0);
  end;
end;

procedure TfrmNameDY.ToolButton3Click(Sender: TObject);
begin
  Panel2.Visible := True;
  initGX();
end;

procedure TfrmNameDY.initGX();
var
  BB: array[0..100] of string;
  i, j: Integer;
begin

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select YGName from OA_YG_DangAn where FHType =''绣花工'' and YGType =''正式''  ');
    Open;
  end;
  if ADOQueryTemp.IsEmpty then
  begin
    Application.MessageBox('没有定义数据！', '提示', 0);
    Exit;
  end;
  with ADOQueryTemp do
  begin
    First;
    i := 0;
    while not Eof do
    begin
      BB[i] := Trim(fieldbyname('YGName').AsString);
      i := i + 1;
      Next;
    end;
  end;
  i := i - 1;
  if i > 14 then
  begin
    i := 14;
  end;
  for j := 0 to i do
  begin
    with Panel2 do
    begin
      TSpeedButton(Controls[j]).Visible := True;
      TSpeedButton(Controls[j]).Caption := BB[j];
    end;
  end;
end;

procedure TfrmNameDY.Image2Click(Sender: TObject);
begin
  Panel2.Visible := False;
end;

procedure TfrmNameDY.SpeedButton1Click(Sender: TObject);
begin
  PrintReport(Trim(TSpeedButton(Sender).Caption));
end;

end.

