BAPI / RFC with Delphi(系列之八)--TBAPIControl使用BUS2012建立PO(Delphi源代码)

 1、新建一个Form,并在form上添加下列控件
 

Component Function
SAPLogonControl1 SAP ActiveX-Component to logon to the system
SAPBAPIControl1 SAP ActiveX-Component to connect to BAPI
Button1 Button to start the procedure
Button2 Button to logon
Panel1-3 Elements to display messages

2、源代码如下(BUS2012建立PO) 
 

unit best;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, OleCtrls, SAPBAPIControlLib_TLB, ExtCtrls, Grids,
SAPLogonCtrl_TLB;

type
TForm1 = class(TForm)
  SAPBAPIControl1: TSAPBAPIControl;
  Button1: TButton;
  Panel1: TPanel;
  Panel2: TPanel;
  Panel3: TPanel;
  Button2: TButton;
  SAPLogonControl1: TSAPLogonControl;
  Edit1: TEdit;
  Edit2: TEdit;
  Label1: TLabel;
  Label2: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;

var
Form1: TForm1;
Connection,Mat,Header,Ret,Schedul,Item : Variant;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin

  (* select BusinessObject *)
  Mat:= SAPBapiControl1.GetSAPObject('BUS2012');

  (* define structures *)
  Header := SAPBapiCcontrol1.dimAs (Mat,'CreateFromData','PoHeader');
  Schedul:= SAPBapiCcontrol1.dimAs (Mat,'CreateFromData','PoItemSchedules');
  Item   := SAPBapiCcontrol1.dimAs (Mat,'CreateFromData','PoItems');
  Ret    := SAPBapiCcontrol1.dimAs (Mat,'CreateFromData','Return');

  (* purchaseorder header data's *)
  Header.value ('DOC_TYPE') := 'NB';
  Header.value ('DOC_CAT')  := 'F';
  Header.value ('PURCH_ORG'):= '10';
  Header.value ('PUR_GROUP'):= '10';
  Header.value ('VENDOR')   := '0010000999';

  (* data for position 00010 *)
  Item.Rows.Add;
  Item.Value (1,'PO_ITEM')   := '00010';
  Item.Value (1,'PUR_MAT')   := '000000000000000017';
  Item.Value (1,'STORE_LOC') := '100';
  Item.Value (1,'PLANT')     := '1000';
  Item.Value (1,'NET_PRICE') := '10,00';

  (* schedules for position 00010 *)
  Schedul.Rows.Add;
  Schedul.Value (1,'PO_ITEM')    := '00010';
  Schedul.Value (1,'DEL_DATCAT') := '1';
  Schedul.Value (1,'DELIV_DATE') := '20.09.2000';
  Schedul.Value (1,'QUANTITY')   := '10';

  (* data for position 00020 *)
  Item.Rows.Add;
  Item.value (2,'PO_ITEM')   := '00020';
  Item.value (2,'PUR_MAT')   := '000000000000001161';
  Item.value (2,'STORE_LOC') := '100';
  Item.value (2,'PLANT')     := '1000';
  Item.value (2,'NET_PRICE') := '10,00';

  (* schedules for position 00020 *)
  Schedul.Rows.Add;
  Schedul.Value (2,'PO_ITEM')    := '00020';
  Schedul.Value (2,'DEL_DATCAT') := '1';
  Schedul.Value (2,'DELIV_DATE') := '20.09.2000';
  Schedul.Value (2,'QUANTITY')   := '10';

  (* call the method CreateFromData *)
  Mat.CreateFromData (PoHeader           := Header,
                      SkipItemsWithError := ' ',
                      PoItems            := Item,
                      PoItemSchedules    := Schedul,
                      Return             := Ret);

  (* Errors are saved in the structure Ret *)
  if Ret.RowCount > 0 then
  begin
    Panel1.Caption:= Ret.Value (1,'TYPE');
    Panel2.Caption:= Ret.Value (1,'MESSAGE');
  end

 
(* If the method was calles without errors, *)
  (* display the number of the purchaseorder  *)

  else Panel2.Caption:= Mat.PurchaseOrder;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin

  (* Logon to the system *)
  Connection                    := SAPLogoncontrol1.newConnection;
  Connection.User               := Ansiuppercase(Edit1.text);
  Connection.System             := 'IDS';
  Connection.Client             := '800';
  Connection.ApplicationServer  := 'SAPIDES';
  Connection.SystemNumber       := '00';
  Connection.Password           := Edit2.text;
  Connection.Language           := 'DE' ;
  SAPLogonControl1.Enabled      := false;

  if Connection.LogOn(0,true) = True then
  begin
    ShowMessage('Logon O.K.');
    Button1.Enabled:= true;

   
(* assign the existing connection to the *)
    (* component SAPBapiControl1              *)

    SapBapiControl1.Connection:=Connection;
  end
  else
  begin
    ShowMessage('Error on logon :-(((');
  end;
end;
end.

posted on 2007-03-26 13:35  毛小娃  阅读(118)  评论(0编辑  收藏  举报

导航