【轉載】使用 BAPI_BUPA_CREATE_FROM_DATA 建立BP

hubaichun發表於2018-10-24

 

*&---------------------------------------------------------------------* 
*& Report ZBR_L_BP_ORGANIZATION 
*&---------------------------------------------------------------------* 
*& DESIGNED BY: Wilson Xavier - 19Aug2005 
*& CODED BY : Wilson Xavier - 19Aug2005 
*& LOCATION : XXXXXXXX 
*&---------------------------------------------------------------------* 
*& USE: For initial load in CRM Brazil of BP type Organization from 
*& legacy systems and user data 
*&---------------------------------------------------------------------* 
*& COMMENTS: Program to create BP Organization 
*& from input file type Excel 
*& using BAPI_BUPA_CREATE_FROM_DATA 
*&---------------------------------------------------------------------* 
*& MODIFICATION LOG: 
*& VSN DATE FULL NAME - Change Request Number 
*& v000 Initial Version - 
*&---------------------------------------------------------------------* 
REPORT ZBR_L_BP_ORGANIZATION MESSAGE-ID ZBR_CRM LINE-SIZE 130. 

* Tables 
TABLES: BUT000, ADDR1_DATA, SZA11_0100, BUPA_CRM101. 

* Constants 
CONSTANTS CC_TYPE_CPF LIKE DFKKBPTAXNUM-TAXTYPE VALUE 'BR2'. 
CONSTANTS CC_TYPE_CNPJ LIKE DFKKBPTAXNUM-TAXTYPE VALUE 'BR1'. 

* Variables and Structures 
DATA FILE_IN LIKE RLGRAP-FILENAME. 
DATA LS_SALES TYPE BAPIBUS1006_SALES_AREA. 
DATA BAPI_BPTYP TYPE BAPIBUS1006_HEAD. 
DATA BAPI_ADDRESS TYPE BAPIBUS1006_ADDRESS. 
DATA BAPI_DATA TYPE BAPIBUS1006_CENTRAL. 
DATA BAPI_DATA_ORG TYPE BAPIBUS1006_CENTRAL_ORGAN. 
DATA BAPI_ADTEL TYPE BAPIADTEL OCCURS 10 WITH HEADER LINE. 
DATA BAPI_ADFAX TYPE BAPIADFAX OCCURS 10 WITH HEADER LINE. 
DATA BAPI_ADSMTP TYPE BAPIADSMTP OCCURS 10 WITH HEADER LINE. 
DATA BAPI_RET TYPE BAPIRET2 OCCURS 10 WITH HEADER LINE. 
DATA BAPI_GUID TYPE BUT020-GUID. 
DATA GV_ROLE LIKE BAPIBUS1006_HEAD-PARTNERROLE. 
DATA BAPI_SALES_CLASS TYPE BAPIBUS1006040_SALESCLASS. 
DATA BAPI_TAX TYPE BAPIBUS1006TAX. 
DATA GV_PARTNER TYPE BUT000-PARTNER. 
DATA GV_ERROR(1). 

* Record layout for PC file 
DATA: BEGIN OF ITAB OCCURS 0, 
GROUP LIKE BUT000-BU_GROUP, 
NAME1 LIKE BUT000-NAME_ORG1, 
NAME2 LIKE BUT000-NAME_ORG2, 
NAME3 LIKE BUT000-NAME_ORG3, 
NAME4 LIKE BUT000-NAME_ORG4, 
SEARCHTERM1 LIKE BUT000-BU_SORT1, 
SEARCHTERM2 LIKE BUT000-BU_SORT2, 
STREET LIKE ADDR1_DATA-STREET, 
HOUSE_NUM LIKE ADDR1_DATA-HOUSE_NUM1, 
POSTL_COD1 LIKE ADDR1_DATA-POST_CODE1, 
CITY LIKE ADDR1_DATA-CITY1, 
COUNTRY LIKE ADDR1_DATA-COUNTRY, 
REGION LIKE ADDR1_DATA-REGION, 
LANGU LIKE ADDR1_DATA-LANGU, 
TEL_NUMBER LIKE SZA11_0100-TEL_NUMBER, 
TEL_EXTENS LIKE SZA11_0100-TEL_EXTENS, 
MOB_NUMBER LIKE SZA11_0100-MOB_NUMBER, 
FAX_NUMBER LIKE SZA11_0100-FAX_NUMBER, 
FAX_EXTENS LIKE SZA11_0100-FAX_EXTENS, 
SMTP_ADDR LIKE SZA11_0100-SMTP_ADDR, 
BPEXT LIKE BUT000-BPEXT, 
CPF LIKE DFKKBPTAXNUM-TAXNUM, 
CNPJ LIKE DFKKBPTAXNUM-TAXNUM, 
SALES_ORG LIKE BUPA_CRM101-SALES_ORG, 
CHANNEL LIKE BUPA_CRM101-CHANNEL, 
DIVISION LIKE BUPA_CRM101-DIVISION, 
PROSPECT(1), 
OTHER_ORG(1), 
CUSTOMER(1), 
CONSUMER(1), 
COMPETITOR(1), 
PROPERTY(1), 
DEPARTMENT(1), 
PRESTADOR(1), 
END OF ITAB. 

* Cells in Flat Structure for Flexible Excel Upload 
DATA EXCEL_TAB LIKE YEXCELSTRUC OCCURS 0 WITH HEADER LINE. 

* Parameters selection 
SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001. 
PARAMETERS: P_FILEIN LIKE RLGRAP-FILENAME OBLIGATORY. 
SELECTION-SCREEN SKIP 1. 
SELECTION-SCREEN END OF BLOCK B1. 

* Events 
AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILEIN. 

CALL FUNCTION 'KD_GET_FILENAME_ON_F4' 
EXPORTING 
STATIC = 'X' 
CHANGING 
FILE_NAME = P_FILEIN. 

* Program Init 
START-OF-SELECTION. 

* Upload Data from EXCEL 
PERFORM F_UPLOAD_EXCEL. 

* Create BP 
IF NOT ITAB[] IS INITIAL. 
CALL FUNCTION 'MESSAGES_INITIALIZE'. 
PERFORM F_CREATE_BP. 
ENDIF. 

END-OF-SELECTION. 

* Display log 
PERFORM F_SHOW_MESSAGES. 

*&---------------------------------------------------------------------* 
*& Form rollback_data 
*&---------------------------------------------------------------------* 
* text 
*----------------------------------------------------------------------* 
* --> p1 text 
* <-- p2 text 
*----------------------------------------------------------------------* 
FORM ROLLBACK_DATA . 

CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'. 
CALL FUNCTION 'BUP_MEMORY_ADDRESS_INIT'. 
CALL FUNCTION 'BUP_MEMORY_CENTRAL_INIT'. 

ENDFORM. "" rollback_data 

*&---------------------------------------------------------------------* 
*& Form f_upload_excel 
*&---------------------------------------------------------------------* 
* text 
*----------------------------------------------------------------------* 
* --> p1 text 
* <-- p2 text 
*----------------------------------------------------------------------* 
FORM F_UPLOAD_EXCEL . 

* Upload Data from EXCEL 
CALL FUNCTION 'Y_UPLOAD_EXCEL_BY_OLE' 
EXPORTING 
INFILE = P_FILEIN 
MAX_COL = '256' 
MAX_ROW = '65000' 
TABLES 
EXCEL_TAB = EXCEL_TAB 
EXCEPTIONS 
INCONSISTENT_PARAMETERS = 1 
ERROR_CREATE_OBJECT = 2 
ERROR_CALL_METHOD = 3 
ERROR_GET_PROPERTY = 4 
ERROR_OPENING_FILE = 5 
OTHERS = 6. 

* Excel Upload Error 
IF NOT SY-SUBRC IS INITIAL. 
MESSAGE I019. 
EXIT. 
ENDIF. 

* Move Data Upload into the ITAB file 
CLEAR ITAB. REFRESH ITAB. 
SORT EXCEL_TAB. 
LOOP AT EXCEL_TAB. 
AT NEW ZROW. CLEAR ITAB. ENDAT. 
CASE EXCEL_TAB-ZCOL. 
WHEN 1. ITAB-GROUP = EXCEL_TAB-ZVALUE. 
WHEN 2. ITAB-NAME1 = EXCEL_TAB-ZVALUE. 
WHEN 3. ITAB-NAME2 = EXCEL_TAB-ZVALUE. 
WHEN 4. ITAB-NAME3 = EXCEL_TAB-ZVALUE. 
WHEN 5. ITAB-NAME4 = EXCEL_TAB-ZVALUE. 
WHEN 6. ITAB-SEARCHTERM1 = EXCEL_TAB-ZVALUE. 
WHEN 7. ITAB-SEARCHTERM2 = EXCEL_TAB-ZVALUE. 
WHEN 8. ITAB-STREET = EXCEL_TAB-ZVALUE. 
WHEN 9. ITAB-HOUSE_NUM = EXCEL_TAB-ZVALUE. 
WHEN 10. ITAB-POSTL_COD1 = EXCEL_TAB-ZVALUE. 
WHEN 11. ITAB-CITY = EXCEL_TAB-ZVALUE. 
WHEN 12. ITAB-COUNTRY = EXCEL_TAB-ZVALUE. 
WHEN 13. ITAB-REGION = EXCEL_TAB-ZVALUE. 
WHEN 14. ITAB-LANGU = EXCEL_TAB-ZVALUE. 
WHEN 15. ITAB-TEL_NUMBER = EXCEL_TAB-ZVALUE. 
WHEN 16. ITAB-TEL_EXTENS = EXCEL_TAB-ZVALUE. 
WHEN 17. ITAB-MOB_NUMBER = EXCEL_TAB-ZVALUE. 
WHEN 18. ITAB-FAX_NUMBER = EXCEL_TAB-ZVALUE. 
WHEN 19. ITAB-FAX_EXTENS = EXCEL_TAB-ZVALUE. 
WHEN 20. ITAB-SMTP_ADDR = EXCEL_TAB-ZVALUE. 
WHEN 21. ITAB-BPEXT = EXCEL_TAB-ZVALUE. 
WHEN 22. ITAB-CPF = EXCEL_TAB-ZVALUE. 
WHEN 23. ITAB-CNPJ = EXCEL_TAB-ZVALUE. 
WHEN 24. ITAB-SALES_ORG = EXCEL_TAB-ZVALUE. 
WHEN 25. ITAB-CHANNEL = EXCEL_TAB-ZVALUE. 
WHEN 26. ITAB-DIVISION = EXCEL_TAB-ZVALUE. 
WHEN 27. ITAB-PROSPECT = EXCEL_TAB-ZVALUE. 
WHEN 28. ITAB-OTHER_ORG = EXCEL_TAB-ZVALUE. 
WHEN 29. ITAB-CUSTOMER = EXCEL_TAB-ZVALUE. 
WHEN 30. ITAB-CONSUMER = EXCEL_TAB-ZVALUE. 
WHEN 31. ITAB-COMPETITOR = EXCEL_TAB-ZVALUE. 
WHEN 32. ITAB-PROPERTY = EXCEL_TAB-ZVALUE. 
WHEN 33. ITAB-DEPARTMENT = EXCEL_TAB-ZVALUE. 
WHEN 34. ITAB-PRESTADOR = EXCEL_TAB-ZVALUE. 
ENDCASE. 
AT END OF ZROW. 
APPEND ITAB. 
ENDAT. 
ENDLOOP. 

ENDFORM. "" f_upload_excel 

*&---------------------------------------------------------------------* 
*& Form f_create_bp 
*&---------------------------------------------------------------------* 
* text 
*----------------------------------------------------------------------* 
* --> p1 text 
* <-- p2 text 
*----------------------------------------------------------------------* 
FORM F_CREATE_BP . 

LOOP AT ITAB. 

* Header of colums in Excel Table 
IF SY-TABIX = 1 OR SY-TABIX = 2. 
CONTINUE. 
ENDIF. 

* Validation of the CNPJ 
IF NOT ITAB-CNPJ IS INITIAL. 
CLEAR GV_PARTNER. 
SELECT PARTNER TAXNUM 
INTO (GV_PARTNER,BAPI_TAX-TAXNUMBER) 
FROM DFKKBPTAXNUM UP TO 1 ROWS 
WHERE TAXTYPE = CC_TYPE_CNPJ 
AND TAXNUM = ITAB-CNPJ. 
ENDSELECT. 

IF SY-SUBRC = 0. 
CALL FUNCTION 'MESSAGE_STORE' 
EXPORTING 
ARBGB = 'ZBR_CRM' 
MSGTY = 'E' 
MSGV1 = ITAB-CNPJ 
MSGV2 = GV_PARTNER 
TXTNR = '001' 
ZEILE = ITAB-BPEXT 
EXCEPTIONS 
MESSAGE_TYPE_NOT_VALID = 1 
NOT_ACTIVE = 2 
OTHERS = 3. 
CONTINUE. 
ENDIF. 
ENDIF. 

* Validation of the CPF 
IF NOT ITAB-CPF IS INITIAL. 
CLEAR GV_PARTNER. 
SELECT PARTNER TAXNUM 
INTO (GV_PARTNER, BAPI_TAX-TAXNUMBER) 
FROM DFKKBPTAXNUM UP TO 1 ROWS 
WHERE TAXTYPE = CC_TYPE_CPF 
AND TAXNUM = ITAB-CPF. 
ENDSELECT. 
IF SY-SUBRC = 0. 
CALL FUNCTION 'MESSAGE_STORE' 
EXPORTING 
ARBGB = 'ZBR_CRM' 
MSGTY = 'E' 
MSGV1 = ITAB-CPF 
MSGV2 = GV_PARTNER 
TXTNR = '001' 
ZEILE = ITAB-BPEXT 
EXCEPTIONS 
MESSAGE_TYPE_NOT_VALID = 1 
NOT_ACTIVE = 2 
OTHERS = 3. 
CONTINUE. 
ENDIF. 
ENDIF. 

* BP Type 
CLEAR BAPI_BPTYP. 
BAPI_BPTYP-PARTN_CAT = '2'. ""Organization 
BAPI_BPTYP-PARTN_GRP = ITAB-GROUP. ""Grouping 

* Central Data 
CLEAR BAPI_DATA. 
BAPI_DATA-SEARCHTERM1 = ITAB-SEARCHTERM1. 
BAPI_DATA-SEARCHTERM2 = ITAB-SEARCHTERM2. 
BAPI_DATA-PARTNEREXTERNAL = ITAB-BPEXT. ""External ID 

* Organization Data 
CLEAR BAPI_DATA_ORG. 
BAPI_DATA_ORG-NAME1 = ITAB-NAME1. 
BAPI_DATA_ORG-NAME2 = ITAB-NAME2. 
BAPI_DATA_ORG-NAME3 = ITAB-NAME3. 
BAPI_DATA_ORG-NAME4 = ITAB-NAME4. 

* Address Data 
CLEAR BAPI_ADDRESS. 
BAPI_ADDRESS-STREET = ITAB-STREET. 
BAPI_ADDRESS-HOUSE_NO = ITAB-HOUSE_NUM. 
BAPI_ADDRESS-CITY = ITAB-CITY. 
BAPI_ADDRESS-POSTL_COD1 = ITAB-POSTL_COD1. 
BAPI_ADDRESS-COUNTRY = ITAB-COUNTRY. 
BAPI_ADDRESS-REGION = ITAB-REGION. 
BAPI_ADDRESS-LANGU = ITAB-LANGU. 

* Telephone Data 
CLEAR BAPI_ADTEL. REFRESH BAPI_ADTEL. 
IF NOT ITAB-TEL_NUMBER IS INITIAL OR 
NOT ITAB-TEL_EXTENS IS INITIAL. 
BAPI_ADTEL-TELEPHONE = ITAB-TEL_NUMBER. 
BAPI_ADTEL-EXTENSION = ITAB-TEL_EXTENS. 
BAPI_ADTEL-R_3_USER = '1'. 
BAPI_ADTEL-CONSNUMBER = 1. 
BAPI_ADTEL-STD_NO = 'X'. 
APPEND BAPI_ADTEL. 
ENDIF. 

* Mobile Phone 
CLEAR: BAPI_ADTEL. 
IF NOT ITAB-MOB_NUMBER IS INITIAL. 
BAPI_ADTEL-TELEPHONE = ITAB-MOB_NUMBER. 
BAPI_ADTEL-R_3_USER = '3'. 
BAPI_ADTEL-CONSNUMBER = 2. 
APPEND BAPI_ADTEL. 
ENDIF. 

* Fax Data 
CLEAR BAPI_ADFAX. REFRESH BAPI_ADFAX. 
IF NOT ITAB-FAX_NUMBER IS INITIAL OR 
NOT ITAB-FAX_EXTENS IS INITIAL. 
BAPI_ADFAX-FAX = ITAB-FAX_NUMBER. 
BAPI_ADFAX-EXTENSION = ITAB-FAX_EXTENS. 
BAPI_ADFAX-CONSNUMBER = 1. 
BAPI_ADFAX-STD_NO = 'X'. 
APPEND BAPI_ADFAX. 
ENDIF. 

* Email Data 
CLEAR BAPI_ADSMTP. REFRESH BAPI_ADSMTP. 
IF NOT ITAB-SMTP_ADDR IS INITIAL. 
BAPI_ADSMTP-E_MAIL = ITAB-SMTP_ADDR. 
BAPI_ADSMTP-CONSNUMBER = 1. 
BAPI_ADSMTP-STD_NO = 'X'. 
APPEND BAPI_ADSMTP. 
ENDIF. 

* Save central data 
CLEAR BUT000. 
CLEAR BAPI_RET. REFRESH BAPI_RET. 
CALL FUNCTION 'BAPI_BUPA_CREATE_FROM_DATA' 
EXPORTING 
PARTNERCATEGORY = BAPI_BPTYP-PARTN_CAT 
PARTNERGROUP = BAPI_BPTYP-PARTN_GRP 
CENTRALDATA = BAPI_DATA 
CENTRALDATAORGANIZATION = BAPI_DATA_ORG 
ADDRESSDATA = BAPI_ADDRESS 
IMPORTING 
BUSINESSPARTNER = BUT000-PARTNER 
TABLES 
TELEFONDATA = BAPI_ADTEL 
FAXDATA = BAPI_ADFAX 
E_MAILDATA = BAPI_ADSMTP 
RETURN = BAPI_RET. 

* Verify return 
CLEAR GV_ERROR. 
LOOP AT BAPI_RET. 
IF BAPI_RET-TYPE = 'E'. 
PERFORM ROLLBACK_DATA. 
CLEAR: BUT000-PARTNER. 
GV_ERROR = 'X'. 
ENDIF. 
* Store message 
CALL FUNCTION 'MESSAGE_STORE' 
EXPORTING 
ARBGB = BAPI_RET-ID 
MSGTY = BAPI_RET-TYPE 
MSGV1 = BAPI_RET-MESSAGE_V1 
MSGV2 = BAPI_RET-MESSAGE_V2 
MSGV3 = BAPI_RET-MESSAGE_V3 
MSGV4 = BAPI_RET-MESSAGE_V4 
TXTNR = BAPI_RET-NUMBER 
ZEILE = ITAB-BPEXT 
EXCEPTIONS 
MESSAGE_TYPE_NOT_VALID = 1 
NOT_ACTIVE = 2 
OTHERS = 3. 
ENDLOOP. 
* Error goto next record 
IF NOT GV_ERROR IS INITIAL. 
CONTINUE. 
ENDIF. 

* Gets GUID for business partner 
CLEAR BAPI_RET. REFRESH BAPI_RET. 
CALL FUNCTION 'BAPI_BUPA_GET_NUMBERS' 
EXPORTING 
BUSINESSPARTNER = BUT000-PARTNER 
IMPORTING 
BUSINESSPARTNERGUIDOUT = BAPI_GUID 
TABLES 
RETURN = BAPI_RET. 

* Verify return 
CLEAR GV_ERROR. 
LOOP AT BAPI_RET. 
IF BAPI_RET-TYPE = 'E'. 
PERFORM ROLLBACK_DATA. 
CLEAR: BUT000-PARTNER. 
GV_ERROR = 'X'. 
ENDIF. 
* Store message 
CALL FUNCTION 'MESSAGE_STORE' 
EXPORTING 
ARBGB = BAPI_RET-ID 
MSGTY = BAPI_RET-TYPE 
MSGV1 = BAPI_RET-MESSAGE_V1 
MSGV2 = BAPI_RET-MESSAGE_V2 
MSGV3 = BAPI_RET-MESSAGE_V3 
MSGV4 = BAPI_RET-MESSAGE_V4 
TXTNR = BAPI_RET-NUMBER 
ZEILE = ITAB-BPEXT 
EXCEPTIONS 
MESSAGE_TYPE_NOT_VALID = 1 
NOT_ACTIVE = 2 
OTHERS = 3. 
ENDLOOP. 
* Error goto next record 
IF NOT GV_ERROR IS INITIAL. 
CONTINUE. 
ENDIF. 

* Natural Person 
IF NOT ITAB-CPF IS INITIAL. 
CLEAR BAPI_RET. REFRESH BAPI_RET. 
BUT000-NATPERS = 'X'. 
CALL FUNCTION 'BAPI_BUPA_TAX_COMMON_CHANGE' 
EXPORTING 
BUSINESSPARTNER = BUT000-PARTNER 
TAXCOMMON = BUT000-NATPERS 
TABLES 
RETURN = BAPI_RET. 

* Verify return 
CLEAR GV_ERROR. 
LOOP AT BAPI_RET. 
IF BAPI_RET-TYPE = 'E'. 
PERFORM ROLLBACK_DATA. 
CLEAR: BUT000-PARTNER. 
GV_ERROR = 'X'. 
ENDIF. 
* Store message 
CALL FUNCTION 'MESSAGE_STORE' 
EXPORTING 
ARBGB = BAPI_RET-ID 
MSGTY = BAPI_RET-TYPE 
MSGV1 = BAPI_RET-MESSAGE_V1 
MSGV2 = BAPI_RET-MESSAGE_V2 
MSGV3 = BAPI_RET-MESSAGE_V3 
MSGV4 = BAPI_RET-MESSAGE_V4 
TXTNR = BAPI_RET-NUMBER 
ZEILE = ITAB-BPEXT 
EXCEPTIONS 
MESSAGE_TYPE_NOT_VALID = 1 
NOT_ACTIVE = 2 
OTHERS = 3. 
ENDLOOP. 
* Error goto next record 
IF NOT GV_ERROR IS INITIAL. 
CONTINUE. 
ENDIF. 
ENDIF. 

* Tax Number CNPJ 
IF NOT ITAB-CNPJ IS INITIAL. 
CLEAR BAPI_TAX. 
BAPI_TAX-TAXNUMBER = ITAB-CNPJ. 
BAPI_TAX-TAXTYPE = CC_TYPE_CNPJ. 
CALL FUNCTION 'BAPI_BUPA_TAX_ADD' 
EXPORTING 
BUSINESSPARTNER = BUT000-PARTNER 
TAXTYPE = BAPI_TAX-TAXTYPE 
TAXNUMBER = BAPI_TAX-TAXNUMBER 
TABLES 
RETURN = BAPI_RET. 

* Verify return 
CLEAR GV_ERROR. 
LOOP AT BAPI_RET. 
IF BAPI_RET-TYPE = 'E'. 
PERFORM ROLLBACK_DATA. 
CLEAR: BUT000-PARTNER. 
GV_ERROR = 'X'. 
ENDIF. 
* Store message 
CALL FUNCTION 'MESSAGE_STORE' 
EXPORTING 
ARBGB = BAPI_RET-ID 
MSGTY = BAPI_RET-TYPE 
MSGV1 = BAPI_RET-MESSAGE_V1 
MSGV2 = BAPI_RET-MESSAGE_V2 
MSGV3 = BAPI_RET-MESSAGE_V3 
MSGV4 = BAPI_RET-MESSAGE_V4 
TXTNR = BAPI_RET-NUMBER 
ZEILE = ITAB-BPEXT 
EXCEPTIONS 
MESSAGE_TYPE_NOT_VALID = 1 
NOT_ACTIVE = 2 
OTHERS = 3. 
ENDLOOP. 
* Error goto next record 
IF NOT GV_ERROR IS INITIAL. 
CONTINUE. 
ENDIF. 
ENDIF. 

* Tax Number CPF 
IF NOT ITAB-CPF IS INITIAL. 
CLEAR BAPI_TAX. 
BAPI_TAX-TAXNUMBER = ITAB-CPF. 
BAPI_TAX-TAXTYPE = CC_TYPE_CPF. 
CALL FUNCTION 'BAPI_BUPA_TAX_ADD' 
EXPORTING 
BUSINESSPARTNER = BUT000-PARTNER 
TAXTYPE = BAPI_TAX-TAXTYPE 
TAXNUMBER = BAPI_TAX-TAXNUMBER 
TABLES 
RETURN = BAPI_RET. 

* Verify return 
CLEAR GV_ERROR. 
LOOP AT BAPI_RET. 
IF BAPI_RET-TYPE = 'E'. 
PERFORM ROLLBACK_DATA. 
CLEAR: BUT000-PARTNER. 
GV_ERROR = 'X'. 
ENDIF. 
* Store message 
CALL FUNCTION 'MESSAGE_STORE' 
EXPORTING 
ARBGB = BAPI_RET-ID 
MSGTY = BAPI_RET-TYPE 
MSGV1 = BAPI_RET-MESSAGE_V1 
MSGV2 = BAPI_RET-MESSAGE_V2 
MSGV3 = BAPI_RET-MESSAGE_V3 
MSGV4 = BAPI_RET-MESSAGE_V4 
TXTNR = BAPI_RET-NUMBER 
ZEILE = ITAB-BPEXT 
EXCEPTIONS 
MESSAGE_TYPE_NOT_VALID = 1 
NOT_ACTIVE = 2 
OTHERS = 3. 
ENDLOOP. 
* Error goto next record 
IF NOT GV_ERROR IS INITIAL. 
CONTINUE. 
ENDIF. 
ENDIF. 

* Roles 
* Prospect 
IF NOT ITAB-PROSPECT IS INITIAL. 
CLEAR GV_ROLE. 
MOVE 'BUP002' TO GV_ROLE. 
PERFORM CREATE_ROLE USING GV_ROLE. 
* Error goto next record 
IF NOT GV_ERROR IS INITIAL. 
CONTINUE. 
ENDIF. 
ENDIF. 

* Other Organizaton 
* (Outro parceiro de negocio) 
IF NOT ITAB-OTHER_ORG IS INITIAL. 
CLEAR GV_ROLE. 
MOVE 'ZBR001' TO GV_ROLE. 
PERFORM CREATE_ROLE USING GV_ROLE.. 
* Error goto next record 
IF NOT GV_ERROR IS INITIAL. 
CONTINUE. 
ENDIF. 
ENDIF. 

* Customer 
* (Emissor da ordem, Pagador, Recebedor da fatura, 
* Recebedor da Mercadoria) 
IF NOT ITAB-CUSTOMER IS INITIAL. 
CLEAR GV_ROLE. 
MOVE 'CRM001' TO GV_ROLE. 
PERFORM CREATE_ROLE USING GV_ROLE.. 
* Error goto next record 
IF NOT GV_ERROR IS INITIAL. 
CONTINUE. 
ENDIF. 
ENDIF. 

* Consumer 
IF NOT ITAB-CONSUMER IS INITIAL. 
CLEAR GV_ROLE. 
MOVE 'CRM006' TO GV_ROLE. 
PERFORM CREATE_ROLE USING GV_ROLE. 
* Error goto next record 
IF NOT GV_ERROR IS INITIAL. 
CONTINUE. 
ENDIF. 
ENDIF. 

* Concorrente 

* Propriedade 

* Departamento 

* Prestador de Serviço 

* Define Sales Data 
CLEAR BAPI_RET. REFRESH BAPI_RET. 
CLEAR LS_SALES. 
LS_SALES-SALES_ORGANIZATION = ITAB-SALES_ORG. 
LS_SALES-DISTRIBUTION_CHANNEL = ITAB-CHANNEL. 
LS_SALES-DIVISION = ITAB-DIVISION. 
CALL FUNCTION 'BAPI_BUPA_FRG0010_ADD' 
EXPORTING 
BUSINESSPARTNER = BUT000-PARTNER 
SALES_AREA = LS_SALES 
TABLES 
RETURN = BAPI_RET. 

* Verify return 
CLEAR GV_ERROR. 
LOOP AT BAPI_RET. 
IF BAPI_RET-TYPE = 'E'. 
PERFORM ROLLBACK_DATA. 
CLEAR: BUT000-PARTNER. 
GV_ERROR = 'X'. 
ENDIF. 
* Store message 
CALL FUNCTION 'MESSAGE_STORE' 
EXPORTING 
ARBGB = BAPI_RET-ID 
MSGTY = BAPI_RET-TYPE 
MSGV1 = BAPI_RET-MESSAGE_V1 
MSGV2 = BAPI_RET-MESSAGE_V2 
MSGV3 = BAPI_RET-MESSAGE_V3 
MSGV4 = BAPI_RET-MESSAGE_V4 
TXTNR = BAPI_RET-NUMBER 
ZEILE = ITAB-BPEXT 
EXCEPTIONS 
MESSAGE_TYPE_NOT_VALID = 1 
NOT_ACTIVE = 2 
OTHERS = 3. 
ENDLOOP. 
* Error goto next record 
IF NOT GV_ERROR IS INITIAL. 
CONTINUE. 
ENDIF. 

* Business Partner Classification Sales 
CLEAR BAPI_RET. REFRESH BAPI_RET. 
CLEAR BAPI_SALES_CLASS. 

BAPI_SALES_CLASS-IS_PROSPECT = ITAB-PROSPECT. 
BAPI_SALES_CLASS-IS_CUSTOMER = ITAB-CUSTOMER. 
BAPI_SALES_CLASS-IS_CONSUMER = ITAB-CONSUMER. 
BAPI_SALES_CLASS-IS_COMPETITOR = ITAB-COMPETITOR. 

IF NOT BAPI_SALES_CLASS IS INITIAL. 
CALL FUNCTION 'BAPI_BUPA_FRG0040_CREATE' 
EXPORTING 
BUSINESSPARTNER = BUT000-PARTNER 
DATA = BAPI_SALES_CLASS 
TABLES 
RETURN = BAPI_RET. 
ENDIF. 

* Verify return 
CLEAR GV_ERROR. 
LOOP AT BAPI_RET. 
IF BAPI_RET-TYPE = 'E'. 
PERFORM ROLLBACK_DATA. 
CLEAR: BUT000-PARTNER. 
GV_ERROR = 'X'. 
ENDIF. 
* Store message 
CALL FUNCTION 'MESSAGE_STORE' 
EXPORTING 
ARBGB = BAPI_RET-ID 
MSGTY = BAPI_RET-TYPE 
MSGV1 = BAPI_RET-MESSAGE_V1 
MSGV2 = BAPI_RET-MESSAGE_V2 
MSGV3 = BAPI_RET-MESSAGE_V3 
MSGV4 = BAPI_RET-MESSAGE_V4 
TXTNR = BAPI_RET-NUMBER 
ZEILE = ITAB-BPEXT 
EXCEPTIONS 
MESSAGE_TYPE_NOT_VALID = 1 
NOT_ACTIVE = 2 
OTHERS = 3. 
ENDLOOP. 
* Error goto next record 
IF NOT GV_ERROR IS INITIAL. 
CONTINUE. 
ENDIF. 

* Execute external Commit 
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' 
EXPORTING 
WAIT = 'X'. 
CALL FUNCTION 'BUP_MEMORY_ADDRESS_INIT'. 
CALL FUNCTION 'BUP_MEMORY_CENTRAL_INIT'. 

* Sucess Message 
CALL FUNCTION 'MESSAGE_STORE' 
EXPORTING 
ARBGB = 'R1' 
MSGTY = 'S' 
MSGV1 = BUT000-PARTNER 
MSGV2 = BAPI_RET-MESSAGE_V2 
MSGV3 = BAPI_RET-MESSAGE_V3 
MSGV4 = BAPI_RET-MESSAGE_V4 
TXTNR = '425' 
ZEILE = ITAB-BPEXT 
EXCEPTIONS 
MESSAGE_TYPE_NOT_VALID = 1 
NOT_ACTIVE = 2 
OTHERS = 3. 

ENDLOOP. 

ENDFORM. "" f_create_bp 

*&---------------------------------------------------------------------* 
*& Form create_role 
*&---------------------------------------------------------------------* 
* text 
*----------------------------------------------------------------------* 
* --> p1 text 
* <-- p2 text 
*----------------------------------------------------------------------* 
FORM CREATE_ROLE USING P_GV_ROLE. 

* Create Role 
CLEAR BAPI_RET. REFRESH BAPI_RET. 
CALL FUNCTION 'BAPI_BUPA_ROLE_ADD' 
EXPORTING 
BUSINESSPARTNER = BUT000-PARTNER 
BUSINESSPARTNERROLE = P_GV_ROLE 
TABLES 
RETURN = BAPI_RET. 

* Verify return 
CLEAR GV_ERROR. 
LOOP AT BAPI_RET. 
IF BAPI_RET-TYPE = 'E'. 
PERFORM ROLLBACK_DATA. 
CLEAR: BUT000-PARTNER. 
GV_ERROR = 'X'. 
ENDIF. 
* Store message 
CALL FUNCTION 'MESSAGE_STORE' 
EXPORTING 
ARBGB = BAPI_RET-ID 
MSGTY = BAPI_RET-TYPE 
MSGV1 = BAPI_RET-MESSAGE_V1 
MSGV2 = BAPI_RET-MESSAGE_V2 
MSGV3 = BAPI_RET-MESSAGE_V3 
MSGV4 = BAPI_RET-MESSAGE_V4 
TXTNR = BAPI_RET-NUMBER 
ZEILE = ITAB-BPEXT 
EXCEPTIONS 
MESSAGE_TYPE_NOT_VALID = 1 
NOT_ACTIVE = 2 
OTHERS = 3. 
ENDLOOP. 
* Error goto next record 
IF NOT GV_ERROR IS INITIAL. 
EXIT. 
ENDIF. 

ENDFORM. "" create_role 

*---------------------------------------------------------------------* 
* Form f_show_messages 
* Show or print error messages 
*----------------------------------------------------------------------* 
FORM F_SHOW_MESSAGES. 

* Init 
DATA LI_FLISTE TYPE TABLE OF SMESG. 
DATA LW_FLISTE TYPE SMESG. 
DATA LV_COLOR(1). 

* Results 
CLEAR: LI_FLISTE. 
CALL FUNCTION 'MESSAGES_GIVE' 
TABLES 
T_MESG = LI_FLISTE. 

DESCRIBE TABLE LI_FLISTE. 
IF NOT SY-TFILL IS INITIAL. 

FORMAT COLOR 1 INTENSIFIED. 
WRITE: /01 SY-VLINE, 03 TEXT-003, 130 SY-VLINE. 
ULINE. 

WRITE: /01 SY-VLINE, 03 TEXT-006, 20 SY-VLINE, 
22 TEXT-004, 32 SY-VLINE, 34 TEXT-005, 130 SY-VLINE. 
ULINE. 

CLEAR LV_COLOR. 
LOOP AT LI_FLISTE INTO LW_FLISTE. 

IF LV_COLOR IS INITIAL. 
FORMAT COLOR COL_NORMAL INTENSIFIED OFF. 
ELSE. 
FORMAT COLOR COL_NORMAL INTENSIFIED ON. 
ENDIF. 

WRITE: /01 SY-VLINE, 
03 LW_FLISTE-ZEILE, 
20 SY-VLINE, 
22 LW_FLISTE-MSGTY, 
32 SY-VLINE, 
34 LW_FLISTE-TEXT, 
130 SY-VLINE. 

IF LV_COLOR IS INITIAL. 
LV_COLOR = 'X'. 
ELSE. 
CLEAR LV_COLOR. 
ENDIF. 
ENDLOOP. 
ULINE. 
ENDIF. 

ENDFORM. ""f_show_messages 

相關文章