unit SampleLP;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ComObj, StdCtrls, Grids, ComCtrls;

type
  TMainForm = class(TForm)
    List: TStringGrid;
    ReadData: TButton;
    WriteData: TButton;
    Enable: TCheckBox;
    Cache: TCheckBox;
    AddRow: TButton;
    DeleteRow: TButton;
    PageProperties: TButton;
    Exit: TButton;
    Label1: TLabel;
    Label2: TLabel;
    FirstRecord: TComboBox;
    LastRecord: TComboBox;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure AddRowClick(Sender: TObject);
    procedure DeleteRowClick(Sender: TObject);
    procedure PagePropertiesClick(Sender: TObject);
    procedure ExitClick(Sender: TObject);
    procedure ReadDataClick(Sender: TObject);
    procedure EnableClick(Sender: TObject);
    procedure CacheClick(Sender: TObject);
    procedure WriteDataClick(Sender: TObject);
    procedure FirstRecordChange(Sender: TObject);
    procedure LastRecordChange(Sender: TObject);
  private
    LP: OleVariant;
    procedure UpdateForm;
  public

  end;

var
  MainForm: TMainForm;

implementation

{$R *.DFM}

procedure TMainForm.FormCreate(Sender: TObject);
var
   i: integer;
begin
   //   
   LP := CreateOleObject('AddIn.LP45');

   //  
   List.ColCount := 10;
   List.RowCount := 1;
   List.Cells[0, 0] := 'PLU';
   List.Cells[1, 0] := '';
   List.Cells[2, 0] := ' ';
   List.Cells[3, 0] := '';
   List.Cells[4, 0] := ' 2';
   List.Cells[5, 0] := ' ';
   List.Cells[6, 0] := ' ';
   List.Cells[7, 0] := ' ';
   List.Cells[8, 0] := ' ';
   List.Cells[9, 0] := ' ';

   //     
   FirstRecord.Items.Clear;
   LastRecord.Items.Clear;
   for i:=1 to 1000 do
   begin
        FirstRecord.Items.Add (IntToStr(i));
        LastRecord.Items.Add (IntToStr(i));
   end;
   UpdateForm;
end;

procedure TMainForm.FormDestroy(Sender: TObject);
begin
   //   
   LP := Unassigned;
end;

procedure TMainForm.UpdateForm;
begin
   //  
   Enable.Checked := LP.DeviceEnabled;
   Cache.Checked := LP.CacheReport;
   FirstRecord.ItemIndex := LP.FirstRecord - 1;
   LastRecord.ItemIndex := LP.LastRecord - 1;
end;

procedure TMainForm.AddRowClick(Sender: TObject);
var
  i: Integer;
  row: Integer;
begin
   //   
   List.RowCount := List.RowCount + 1;
   row := List.RowCount;

   //  
   for i:=0 to 9 do
     List.Cells [i,row-1] := '1';

   //   
   if (List.RowCount > 1) then List.FixedRows := 1;
end;

procedure TMainForm.DeleteRowClick(Sender: TObject);
begin
   //   
   if (List.RowCount > 1) then
   begin
      List.RowCount := List.RowCount - 1;
   end;
end;

procedure TMainForm.PagePropertiesClick(Sender: TObject);
begin
   //   
   LP.ShowProperties;
   UpdateForm;
end;

procedure TMainForm.ExitClick(Sender: TObject);
begin
   //  
   Close;
end;

procedure TMainForm.ReadDataClick(Sender: TObject);
var
  i: Integer;
  RowIndex: Integer;
begin
  RowIndex := 1;
  try
     //   
     LP.BeginReport;

     for i:=LP.FirstRecord to LP.LastRecord do
     begin
        //     
        LP.GetRecord;
        List.RowCount := RowIndex + 1;
        //  
        List.Cells[0,RowIndex] := IntToStr(LP.PLU);
        List.Cells[1,RowIndex] := CurrToStr(LP.Price);
        List.Cells[2,RowIndex] := IntToStr(LP.ICode);
        List.Cells[3,RowIndex] := LP.Name;
        List.Cells[4,RowIndex] := LP.Name2;
        List.Cells[5,RowIndex] := IntToStr(LP.MsgNo);
        List.Cells[6,RowIndex] := IntToStr(LP.Life);
        List.Cells[7,RowIndex] := FloatToStr(LP.Tare);
        List.Cells[8,RowIndex] := IntToStr(LP.GCode);
        List.Cells[9,RowIndex] := IntToStr(LP.WareType);

        RowIndex:= RowIndex + 1;
     end;

     //   
     LP.EndReport;

     //   
     List.FixedRows  := 1;

   except
     On E: Exception do
     begin
        //   ,     
        // -2001 =    ( )
        if ((LP.ResultCode <> -2001) And
            (LP.ResultCode <> 0)) then raise;
     end;
  end;

  UpdateForm;
end;

procedure TMainForm.EnableClick(Sender: TObject);
begin
   // / 
   LP.DeviceEnabled := Enable.Checked;
   UpdateForm;
end;

procedure TMainForm.CacheClick(Sender: TObject);
begin
   // / 
   LP.CacheReport := Cache.Checked;
   UpdateForm;
end;

procedure TMainForm.WriteDataClick(Sender: TObject);
var
  i: Integer;
begin
  try
     //   
     LP.BeginAdd;

     for i:=1 to List.RowCount-1 do
     begin
        //  
        LP.PLU := StrToInt (List.Cells[0,i]);
        LP.Price := StrToCurr (List.Cells[1,i]);
        LP.ICode := StrToInt (List.Cells[2,i]);
        LP.Name := List.Cells[3,i];
        LP.Name2 := List.Cells[4,i];
        LP.MsgNo := StrToInt (List.Cells[5,i]);
        LP.Life := StrToInt (List.Cells[6,i]);
        LP.Tare := StrToFloat (List.Cells[7,i]);
        LP.GCode := StrToInt (List.Cells[8,i]);
        LP.WareType := StrToInt (List.Cells[9,i]);

        //     
        LP.SetRecord;
     end;

     //   
     LP.EndAdd;

   except
     On E: Exception do
     begin
        //   ,     
        if (LP.ResultCode <> 0) then raise;
     end;
  end;

  UpdateForm;
end;
procedure TMainForm.FirstRecordChange(Sender: TObject);
begin
     //     
     LP.FirstRecord := FirstRecord.ItemIndex + 1;
     UpdateForm;
end;

procedure TMainForm.LastRecordChange(Sender: TObject);
begin
     //     
     LP.LastRecord := LastRecord.ItemIndex + 1;
     UpdateForm;
end;

end.
