B/C Recorder

来源:互联网 发布:oracle认证考试 java 编辑:程序博客网 时间:2024/05/01 13:41

unit UnitMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,inifiles, StdCtrls, ComCtrls, ExtCtrls, Spin, Gauges;

type
  TFormMain = class(TForm)
    StatusBar1: TStatusBar;
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    lbledtWO: TLabeledEdit;
    lbledtName: TLabeledEdit;
    lbledtSpec: TLabeledEdit;
    btnSave: TButton;
    edtBC: TEdit;
    lbledtPlanQty: TLabeledEdit;
    chkinfoLock: TCheckBox;
    lblCounter: TLabel;
    Gauge1: TGauge;
    lbledtScan: TLabeledEdit;
    seBoxQty: TSpinEdit;
    Label2: TLabel;
    lblHint: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure btnSaveClick(Sender: TObject);
    procedure edtBCKeyPress(Sender: TObject; var Key: Char);
    procedure chkinfoLockClick(Sender: TObject);
    procedure seBoxQtyChange(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);

  private
    { Private declarations }
  public
    procedure CountLines;
    function ifexists(s:string;bc:string):Boolean;
    { Public declarations }
  end;

var
  FormMain: TFormMain;
  Counter : Integer ; // 计数器

implementation

{$R *.dfm}

procedure TFormMain.FormCreate(Sender: TObject);
var
  Ini : TIniFile;
begin

  //Application.Title:='TCL产品条码记录器';
  Application.Title:=' TCL B/C Recorder';

  Counter := 0 ;
  Gauge1.MaxValue := seBoxQty.Value;
  // 读取配置数据

  Ini := TIniFile.Create( ChangeFileExt( Application.ExeName, '.INI' ) );
  try
    Caption := Caption +' - [ ' +Ini.ReadString( 'TCL', 'USER', 'SMT')+' ]';
    lbledtWO.Text := Ini.ReadString( 'TCL', 'NO', '');
    lbledtName.Text := Ini.ReadString( 'TCL', 'Name', '');
    lbledtSpec.Text := Ini.ReadString( 'TCL', 'Spec', '');
    lbledtPlanQty.Text := Ini.ReadString( 'TCL', 'PlanQty', '0');
    seBoxQty.Value := Ini.ReadInteger('TCL', 'BoxQty', 35);
  finally
    Ini.Free;
  end;

  // 建立存放 B/C 信息的资料夹.
  if not DirectoryExists('C:/TCL') then
   if not CreateDir('C:/TCL') then
     ShowMessage('Can''t Create Directory ''C:/TCL''.');


   lblCounter.Caption := IntToStr(Counter) + '/' + IntToStr(seBoxQty.Value);
end;

procedure TFormMain.btnSaveClick(Sender: TObject);
var
  Ini : TIniFile;
begin
  Application.Title:=' TCL B/C Recorder';
  // 保存配置数据
  Ini := TIniFile.Create( ChangeFileExt( Application.ExeName, '.INI' ) );
  try
    Ini.WriteString( 'TCL', 'USER', 'SMT');
    Ini.WriteString( 'TCL', 'NO', lbledtWO.Text);
    Ini.WriteString( 'TCL', 'Name', lbledtName.Text);
    Ini.WriteString( 'TCL', 'Spec', lbledtSpec.Text);
    Ini.WriteString( 'TCL', 'PlanQty',lbledtPlanQty.Text);
  finally
    Ini.Free;
  end;
end;

procedure TFormMain.edtBCKeyPress(Sender: TObject; var Key: Char);
var
  s: string;
  BCFile :TextFile;
begin
  lblHint.Caption := '';
  if Key <> #13 then Exit;
  if edtBC.Text='' then Exit;
  edtBC.SelectAll;
  // 先遍历记录,检查是否已经扫描
  s := 'C:/TCL/'+ lbledtWO.Text +'.TXT';
  if not ifexists(s,edtBC.Text) then
  begin
    lblHint.Caption := 'The B/C : ' + edtBC.Text + ' is exists !';
    Exit; // 条码若存在,则不作处理
  end;
  AssignFile(BCFile, s);
  if not FileExists(s) then
    Rewrite(BCFile)   // 档案不存在,则创建新档
  else 
    Append(BCFile);   // 档案若存在,则追加
  Writeln(BCFile,FormatDateTime('yyyy-mm-dd hh:mm:ss',Now)
         +chr(9)+edtBC.Text);
  CloseFile(BCFile);

  CountLines;// 统计行数

  Inc(Counter); // 计数器 +1
  Gauge1.Progress := Counter;
  lblCounter.Caption := IntToStr(Counter) + '/' + IntToStr(seBoxQty.Value);

  // 如果满箱
  if  Counter = seBoxQty.Value then
  begin
    ShowMessage('Box Full.');
    Counter := 0;
    lblCounter.Caption := IntToStr(Counter) + '/' + IntToStr(seBoxQty.Value);
  end;

end;

procedure TFormMain.chkinfoLockClick(Sender: TObject);
begin
  lbledtWO.ReadOnly := chkinfoLock.Checked;
  lbledtName.ReadOnly := chkinfoLock.Checked;
  lbledtSpec.ReadOnly := chkinfoLock.Checked;
  lbledtPlanQty.ReadOnly := chkinfoLock.Checked;
end;

procedure TFormMain.seBoxQtyChange(Sender: TObject);
begin                                      
  if  (seBoxQty.Text='') or (seBoxQty.Value = 0 ) then
    Exit; // 若在手工输入数字的过程中(空或者为0),则不执行下面的处理
  Gauge1.MaxValue := seBoxQty.Value;
  lblCounter.Caption := IntToStr(Counter) + '/' + IntToStr(seBoxQty.Value);
end;

procedure TFormMain.FormClose(Sender: TObject; var Action: TCloseAction);
var
//  s: string;
  WOFile :TextFile;
begin
  btnSave.Click; // 退出时保存设置
  AssignFile(WOFile, 'C:/TCL/WorkOrder.TXT');
  if not FileExists('C:/TCL/WorkOrder.TXT') then
    Rewrite(WOFile)   // 档案不存在,则创建新档
  else
    Append(WOFile);   // 档案若存在,则追加
   
  Writeln(Wofile,'【'+ FormatDateTime('yyyy-mm-dd hh:mm:ss',Now)+'】');
  Writeln(Wofile, 'WorkOrder    = ' + lbledtWO.Text);
  Writeln(Wofile, 'Name         = ' + lbledtName.Text);
  Writeln(Wofile, 'Spec         = ' + lbledtSpec.Text);
  Writeln(Wofile, 'PlanQty      = ' + lbledtPlanQty.Text);
  Writeln(Wofile,  'Scanned Qty = '+ lbledtScan.Text);
  Writeln(Wofile,  '');
  CloseFile(WOFile);
end;

procedure TFormMain.CountLines;
var
  s: string;
  WOFile :TextFile;
  i : Integer;
begin
   s := 'C:/TCL/'+ lbledtWO.Text +'.TXT';
  AssignFile(WOFile, s);
  if FileExists(s) then
  begin
    Reset(WOFile);
    i := 0;
    while not Eof(WOFile) do
    begin
      Readln(Wofile);
      Inc(i);
    end;
    CloseFile(WOFile);
    lbledtScan.Text := IntToStr(i);  // 行数
  end;
end;

// 判断此 B/C 是否已经扫描
function TFormMain.ifexists(s:string;bc:string):Boolean;
var
  WOFile :TextFile;
  ss: string;
begin
  Result := True;
  AssignFile(WOFile, s);
  if FileExists(s) then
  begin
    Reset(WOFile);
    while not Eof(WOFile) do
    begin
      Readln(Wofile,ss);
      if trim(Copy(ss,POS(chr(9),ss)+1,255)) = Trim(bc) then
      begin
        Result := False;
        Break;
      end;
    end;
    CloseFile(WOFile);
  end;

end;
end.