REPORT FOR INITIAL LOAD OF STOCKS

REPORT ZMCH0049 LINE-SIZE 170.
*
* Initial load of stocks.
*-------------------------
TABLES : MARA,
MARC,
MARD,
MCHB,
MCHA,
MSLB,
T9F1T.

DATA :BEGIN OF A1,
PREMB(12),
PROC(2),
SISO(4),
MAGA(4),
CLOT(12),
NISU(1),
NIAF(1),
AFFE(5) TYPE P,
GRAD(5) TYPE P,
SANA(5) TYPE P,
FAMI(3),
ENTI(4),
DADE(6),
PLAC(7),
POID(3) TYPE P,
FAFF(1),
FGRA(1),
FSOR(1),
TARE(3) TYPE P DECIMALS 2,
GRA1(3),
NUCO(5),
NUEX(2),
FILLER(60),
END OF A1.

DATA : BEGIN OF K1-RECORD.
INCLUDE STRUCTURE BMSEG.
DATA : END OF K1-RECORD.

DATA : BEGIN OF TAB_APIN OCCURS 10000,
PREMB(12),
MATNR LIKE MARD-MATNR,
WERKS LIKE MARD-WERKS,
LGORT LIKE MARD-LGORT,
CHARG LIKE MCHA-CHARG,
LICHA LIKE MCHA-LICHA, "apin translation
LICHS LIKE MCHA-LICHA, "sap data
GRAD(5) TYPE P, "apin gradé
SANA(5) TYPE P, "apin sous analyse
SUBC(5) TYPE P, "apin subcontracting
LABST(5) TYPE P, "sap unresticted use
INSME(5) TYPE P, "sap inspection
LBLAB(5) TYPE P, "sap subcontracting
FLAG(1) TYPE C,
NISU(1) TYPE C,
VENDOR LIKE LFA1-LIFNR,
END OF TAB_APIN.

DATA: BEGIN OF TBDCDATA OCCURS 200. "Dynpro table
INCLUDE STRUCTURE BDCDATA.
DATA: END OF TBDCDATA.

DATA: W1-MATNR LIKE MARD-MATNR,
W1-WERKS LIKE MARD-WERKS,
WS-PREMB(12),
W1-LGORT LIKE MARD-LGORT,
W1-CHARG LIKE MCHA-CHARG,
W1-LICHA LIKE MCHA-LICHA,
W-LICHA LIKE MCHA-LICHA,
W1-BWART LIKE BMSEG-BWART,
W1-ERFMG(5) TYPE P,
TOT-MVT(5) TYPE P.
DATA: W1-CTRREC(5) TYPE P VALUE 0,
CTR_TRX TYPE I VALUE 0,
CTR_LOTS TYPE I,
WTRAV(20),
WKEY(14),
W-VENDOR LIKE LFA1-LIFNR,
SW_ERROR(1),
CTR_OK TYPE I VALUE 0,
CTR_KO TYPE I VALUE 0,
SW_PRINT_ULINE(1) VALUE 'N'.
PARAMETERS:
P_FILE_M(40) DEFAULT 'APSAO.DAMVTS.EXTRACT.IL3', "APIN UNLOAD
P_NODE_M(8) DEFAULT 'BXLESSY1',
P_FILE_U(40) DEFAULT 'il_mvts.bin' LOWER CASE, "unix file created
P_FILE_2(40) DEFAULT '/usr/sap/trans02/userdata/il_mvts.bin'
LOWER CASE, "unix file to treat
P_FILE_3(40) DEFAULT '/usr/sap/trans02/userdata/il_mvts.out'
LOWER CASE, "unix file mvts
P_CODE_U(8) DEFAULT '3', "security code
PARM_DEL DEFAULT ' ' AS CHECKBOX, "delete mvs file
PARM_TRF DEFAULT 'X' AS CHECKBOX, "transfert file ?
PARM_GEN DEFAULT 'X' AS CHECKBOX, "generate mvts ?
P_MAXREC TYPE I DEFAULT 99999,
P_REASAP(1) DEFAULT ' ' .
SELECT-OPTIONS : MATNR FOR W1-MATNR,
CHARG FOR W1-CHARG,
PROC FOR A1-PROC,
WERKS FOR MARC-WERKS,
LGORT FOR W1-LGORT.

START-OF-SELECTION.

IF PARM_GEN = 'X'.
PERFORM INITIALISATION_SBDC.
ENDIF.
CLEAR TAB_APIN.
IF PARM_TRF = 'X'.
PERFORM TRANSFER_FILE.
ENDIF.
IF SY-SUBRC NE 0. EXIT. ENDIF.
PERFORM OPEN_FILE.
IF SY-SUBRC NE 0.
WRITE : / 'open file not ok'.
EXIT.
ENDIF.
DO.
READ DATASET P_FILE_2 INTO A1.
IF SY-SUBRC NE 0.
EXIT.
ENDIF.
TRANSLATE A1 FROM CODE PAGE '0100' TO CODE PAGE '1100'.
IF PROC.
MOVE A1-PREMB TO WKEY.
REPLACE 'GG' WITH '__' INTO WKEY.
MOVE 'YY' TO WKEY+12.
SELECT * FROM MARA WHERE WRKST = WKEY.
IF SY-SUBRC = 0.
PERFORM TRANSLATION_APIN_SAP.
IF MATNR AND CHARG AND LGORT AND SW_ERROR = SPACE.

PERFORM LOAD_TABLE_WITH_APIN_DATA.
ENDIF.
ENDIF.
ENDSELECT.
IF SY-SUBRC <> 0.
IF A1-PREMB CS 'GG'.
MOVE A1-PREMB TO WKEY.
ELSE.
REPLACE ' ' WITH '__' INTO A1-PREMB.
MOVE A1-PREMB TO WKEY.
ENDIF.
MOVE 'YY' TO WKEY+12.
SELECT * FROM MARA WHERE WRKST = WKEY.
IF SY-SUBRC = 0.
PERFORM TRANSLATION_APIN_SAP.
IF MATNR AND CHARG AND LGORT AND SW_ERROR = SPACE.
PERFORM LOAD_TABLE_WITH_APIN_DATA.
ENDIF.
ENDIF.
ENDSELECT.
ENDIF.
ENDIF.
ENDDO.
FORMAT INTENSIFIED OFF.
WRITE : / 'APIN MVTS read ', W1-CTRREC.
SORT TAB_APIN BY MATNR WERKS LGORT CHARG.
IF P_REASAP = 'N'.
PERFORM LOAD_TABLE_WITH_SAP_DATA.
ENDIF.
* SORT TAB_APIN BY MATNR WERKS LGORT CHARG.
SORT TAB_APIN BY MATNR WERKS CHARG LGORT.
LOOP AT TAB_APIN.
AT NEW PREMB.
IF SW_PRINT_ULINE = 'Y'.
ULINE.
SW_PRINT_ULINE = 'N'.
ENDIF.
ENDAT.
IF TAB_APIN-GRAD <> TAB_APIN-LABST OR
TAB_APIN-SANA <> TAB_APIN-INSME OR
TAB_APIN-SUBC <> TAB_APIN-LBLAB OR
TAB_APIN-LICHA <> TAB_APIN-LICHS.
TOT-MVT = TAB_APIN-GRAD + TAB_APIN-SANA + TAB_APIN-SUBC
- TAB_APIN-LABST - TAB_APIN-INSME - TAB_APIN-LBLAB.
MOVE TAB_APIN-LICHA TO W-LICHA.
IF TAB_APIN-FLAG = 'I'.
MOVE SPACE TO W-LICHA.
ENDIF.
IF TOT-MVT <> 0.
MOVE '?' TO TAB_APIN-FLAG.
ENDIF.
WRITE:/ TAB_APIN-MATNR+12(6),
TAB_APIN-PREMB,
TAB_APIN-WERKS,
TAB_APIN-LGORT,
TAB_APIN-CHARG,
W-LICHA(5),
TAB_APIN-LICHS(5),
TAB_APIN-GRAD,TAB_APIN-SANA,TAB_APIN-SUBC,
TAB_APIN-LABST DECIMALS 0,
TAB_APIN-INSME DECIMALS 0,
TAB_APIN-LBLAB DECIMALS 0,
TAB_APIN-FLAG.
CTR_KO = CTR_KO + 1.
SW_PRINT_ULINE = 'Y'.
ELSE.
CTR_OK = CTR_OK + 1.
ENDIF.

PERFORM GENERATION_MOUVEMENTS.
ADD 1 TO CTR_TRX.
ENDLOOP.

PERFORM CLOSE_FILE.
ULINE.
WRITE : / 'Nbre Transactions',CTR_TRX.
WRITE : 'Ok batches ', CTR_OK,' Differences ',CTR_KO.
IF PARM_GEN = 'X'.
PERFORM CLOSE_SBDC.
ENDIF.
*---------------------------------------------------------------------*
* FORM TRANSLATION *
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
FORM TRANSLATION_APIN_SAP.
MOVE SPACE TO SW_ERROR.
MOVE: SPACE TO W1-CHARG,
SPACE TO W1-LGORT,
SPACE TO W1-LICHA,
SPACE TO W1-MATNR,
SPACE TO W1-WERKS.

CALL FUNCTION 'Z_PRODUCT_DETERMINATION'
EXPORTING
IN_APINENTI = A1-ENTI
IN_APINLOCA = A1-PLAC
IN_APINLOTN = A1-CLOT
IN_APINMAGA = A1-MAGA
IN_APINPROD = A1-PREMB
IN_APINSITE = A1-SISO
IN_CHARG = ' '
IN_DIREC = 'SAP '
IN_LGORT = ' '
IN_LICHA = ' '
IN_MATNR = ' '
IN_WERKS = ' '
IN_LIFNR = ' '
IN_EXACT = 'X'
IMPORTING
* OUT_APINENTI =
* OUT_APINLOCA =
* OUT_APINLOTN =
* OUT_APINMAGA =
* OUT_APINPROD =
* OUT_APINSITE =
OUT_CHARG = W1-CHARG
OUT_LGORT = W1-LGORT
OUT_LICHA = W1-LICHA
OUT_MATNR = W1-MATNR
OUT_WERKS = W1-WERKS
OUT_LIFNR = W-VENDOR
EXCEPTIONS
APIN_PRODUCT_CODE_NOT_FOUND = 01
INVALID_DIRECTION = 02
PLANT_STOR_LOC_NOT_FOUND = 03
SAP_BATCH_NUMBER_NOT_FOUND = 04
SAP_MATERIAL_CODE_MANDATORY = 05
SAP_MATERIAL_CODE_NOT_FOUND = 06
SITE_MAGASIN_NOT_FOUND = 07
PRODUCT_LINE_NOT_FOUND = 08.
IF SY-SUBRC <> 0.
MOVE 'E' TO SW_ERROR.
WRITE : / 'Z_PRODUCT_DETERMINATION Error ',SY-SUBRC,'for',
A1-SISO,A1-MAGA,A1-PREMB,A1-CLOT.
ENDIF.
ENDFORM.

*---------------------------------------------------------------------*
* FORM GENERATION_MOUVEMENTS *
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
FORM GENERATION_MOUVEMENTS.
* generation mvts 561/562
IF TAB_APIN-GRAD <> TAB_APIN-LABST.
MOVE SPACE TO W-VENDOR.
W1-ERFMG = TAB_APIN-GRAD - TAB_APIN-LABST.
IF W1-ERFMG > 0.
MOVE '561' TO W1-BWART. "mvt : GR to unrestriced use
WRITE: '(',W1-BWART , W1-ERFMG,')'.
ENDIF.
IF W1-ERFMG < 0.
W1-ERFMG = W1-ERFMG * -1.
MOVE '562' TO W1-BWART. "mvt : gr to unrestriced use rev
WRITE: '(',W1-BWART , W1-ERFMG,')'.
ENDIF.
IF PARM_GEN = 'X'.
PERFORM GENERATE_MVTS.
ENDIF.
ENDIF.

* generation mvts 563/564
IF TAB_APIN-SANA <> TAB_APIN-INSME.
MOVE SPACE TO W-VENDOR.
W1-ERFMG = TAB_APIN-SANA - TAB_APIN-INSME.
IF W1-ERFMG > 0.
MOVE '563' TO W1-BWART. "mvt : GR to qual inspect
WRITE: '(',W1-BWART , W1-ERFMG,')'.
ENDIF.
IF W1-ERFMG < 0.
W1-ERFMG = W1-ERFMG * -1.
MOVE '564' TO W1-BWART. "mvt : GR to qual inspect rev
WRITE: '(',W1-BWART , W1-ERFMG,')'.
ENDIF.
IF PARM_GEN = 'X'.
PERFORM GENERATE_MVTS.
ENDIF.
ENDIF.
* generation mvts 561/562 type O
IF TAB_APIN-SUBC <> TAB_APIN-LBLAB.
MOVE TAB_APIN-VENDOR TO W-VENDOR.
W1-ERFMG = TAB_APIN-SUBC - TAB_APIN-LBLAB.
IF W1-ERFMG > 0.
MOVE '561' TO W1-BWART. "mvt : GR to unrestriced use
WRITE: '(',W1-BWART,'O', W1-ERFMG,TAB_APIN-VENDOR,')'.
ENDIF.
IF W1-ERFMG < 0.
W1-ERFMG = W1-ERFMG * -1.
MOVE '562' TO W1-BWART. "mvt : gr to unrestriced use rev
WRITE: '(',W1-BWART,'O', W1-ERFMG,TAB_APIN-VENDOR,')'.
ENDIF.
IF PARM_GEN = 'X'.
PERFORM GENERATE_MVTS.
ENDIF.
ENDIF.
* generation info vendor batch.
IF TAB_APIN-LICHA <> TAB_APIN-LICHS.
WRITE '(Ven.b)'.
IF PARM_GEN = 'X'.
PERFORM GENERATE_SBDC.
PERFORM GENERATE_SBDC_TRANSACTION.
ENDIF.
ENDIF.
ENDFORM.
*---------------------------------------------------------------------*
* FORM LOAD_TABLE_WITH_APIN_DATA *
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
FORM LOAD_TABLE_WITH_APIN_DATA.
CLEAR TAB_APIN.
MOVE A1-PREMB TO TAB_APIN-PREMB.
REPLACE 'GG' WITH '__' INTO TAB_APIN-PREMB.
MOVE W1-MATNR TO TAB_APIN-MATNR.
MOVE W1-WERKS TO TAB_APIN-WERKS.
MOVE W1-LGORT TO TAB_APIN-LGORT.
MOVE W1-CHARG TO TAB_APIN-CHARG.
MOVE W1-LICHA TO TAB_APIN-LICHA.
* REPLACE 'GG' WITH ' ' INTO TAB_APIN-LICHA. "dt0103
MOVE A1-NISU TO TAB_APIN-NISU.
* Determination subcontracting.
MOVE SPACE TO W-VENDOR.
MOVE A1-SISO TO WTRAV.
MOVE A1-MAGA TO WTRAV+4.
SELECT * FROM T9F1T
WHERE BUKRS = '0100'
AND ID = 'VENDOR'
AND FKT = WTRAV.
MOVE T9F1T-ARG TO W-VENDOR.
ENDSELECT.
IF W-VENDOR = SPACE. " pas de subcontracting
IF A1-NISU = 'M'.
MOVE SPACE TO TAB_APIN-LICHA.
MOVE SPACE TO TAB_APIN-CHARG.
MOVE A1-GRAD TO TAB_APIN-GRAD.
MOVE A1-SANA TO TAB_APIN-SANA.
ENDIF.
IF A1-NISU = 'L'.
MOVE A1-GRAD TO TAB_APIN-GRAD.
MOVE A1-SANA TO TAB_APIN-SANA.
ENDIF.
IF A1-NISU = 'E'.
IF A1-FGRA = 'Y' AND A1-FSOR = ' '.
MOVE A1-POID TO TAB_APIN-GRAD.
MOVE 0 TO TAB_APIN-SANA.
ENDIF.
IF A1-FGRA = 'N' AND A1-FSOR = ' '.
MOVE 0 TO TAB_APIN-GRAD.
MOVE A1-POID TO TAB_APIN-SANA.
ENDIF.
IF A1-FGRA <> 'Y' AND A1-FGRA <> 'N'.
WRITE : / 'error flag gradé', A1-FGRA,A1-PREMB,A1-CLOT,
A1-ENTI.
ENDIF.
IF A1-FSOR = 'Y'.
MOVE 0 TO TAB_APIN-GRAD.
MOVE 0 TO TAB_APIN-SANA.
ENDIF.
ENDIF.
ELSE.
MOVE W-VENDOR TO TAB_APIN-VENDOR. " si subcontracting
MOVE A1-MAGA TO TAB_APIN-LGORT.
IF A1-NISU = 'M'.
MOVE SPACE TO TAB_APIN-CHARG.
MOVE A1-GRAD TO TAB_APIN-SUBC.
ADD A1-SANA TO TAB_APIN-SUBC.
ENDIF.
IF A1-NISU = 'L'.
MOVE A1-GRAD TO TAB_APIN-SUBC.
ADD A1-SANA TO TAB_APIN-SUBC.
ENDIF.
IF A1-NISU = 'E'.
IF A1-FGRA = 'Y' AND A1-FSOR = ' '.
MOVE A1-POID TO TAB_APIN-SUBC.
ENDIF.
IF A1-FGRA = 'N' AND A1-FSOR = ' '.
MOVE A1-POID TO TAB_APIN-SUBC.
ENDIF.
IF A1-FGRA <> 'Y' AND A1-FGRA <> 'N'.
WRITE : / 'error flag gradé', A1-FGRA,A1-PREMB,A1-CLOT,
A1-ENTI.
ENDIF.
IF A1-FSOR = 'Y'.
MOVE 0 TO TAB_APIN-GRAD.
MOVE 0 TO TAB_APIN-SANA.
ENDIF.
ENDIF.
ENDIF.
IF TAB_APIN-GRAD < 0 OR TAB_APIN-SANA < 0.
WRITE : / 'error stock < 0',A1-PREMB,A1-SISO,A1-MAGA,A1-CLOT.
ELSE.
IF TAB_APIN-GRAD <> 0 OR TAB_APIN-SANA <> 0 OR TAB_APIN-SUBC <> 0.
APPEND TAB_APIN.
ENDIF.
ENDIF.
ENDFORM.
*---------------------------------------------------------------------*
* FORM LOAD_TABLE_WITH_SAP_DATAS *
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
FORM LOAD_TABLE_WITH_SAP_DATA.

SELECT * FROM MARA.
MOVE MARA-MATNR TO W1-MATNR.
IF MATNR.
IF MARA-WRKST+12(2) = 'YY'.
SELECT * FROM MARC
WHERE MATNR = MARA-MATNR.
IF WERKS.
IF MARC-XCHPF = 'X'.
PERFORM SAP_EXTRACT_LOTS.
PERFORM EXTRACT_SAP_SUBCONTRACTING.
ELSE.
PERFORM SAP_EXTRACT_STOR_LOC.
PERFORM EXTRACT_SAP_SUBCONTRACTING.
ENDIF.
ENDIF.
ENDSELECT.
ENDIF.
ENDIF.
ENDSELECT.
ENDFORM.
*---------------------------------------------------------------------*
* FORM SAP_EXTRACT_LOTS *
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
FORM SAP_EXTRACT_LOTS.
SELECT * FROM MARD
WHERE MATNR = MARA-MATNR
AND WERKS = MARC-WERKS.
SELECT * FROM MCHB
WHERE MATNR = MARA-MATNR
AND WERKS = MARC-WERKS
AND LGORT = MARD-LGORT.
MOVE MCHB-CHARG TO W1-CHARG.
IF CHARG AND LGORT.
SELECT SINGLE * FROM MCHA
WHERE MATNR = MARA-MATNR
AND WERKS = MARC-WERKS
AND CHARG = MCHB-CHARG.
* IF MCHA-LICHA = 'GG'. "dt0103
* MOVE SPACE TO MCHA-LICHA. "dt0103
* ENDIF. "dt0103
IF MARC-WERKS = '7120'.
IF MCHB-CHARG+7(1) = 'T' OR MCHB-CHARG+7(1) = 'R'
OR MCHB-CHARG+7(2) = 'CO' OR MCHB-CHARG+7(2) = 'SC'.
MOVE SPACE TO MCHA-LICHA.
ENDIF. "dt
ENDIF. "dt
CLEAR TAB_APIN.

MOVE MARA-WRKST TO TAB_APIN-PREMB.
REPLACE 'GG' WITH '__' INTO TAB_APIN-PREMB.
MOVE MARA-MATNR TO TAB_APIN-MATNR.
MOVE MARC-WERKS TO TAB_APIN-WERKS.
MOVE MARD-LGORT TO TAB_APIN-LGORT.
MOVE MCHB-CHARG TO TAB_APIN-CHARG.
MOVE SPACE TO TAB_APIN-LICHA.
MOVE SPACE TO TAB_APIN-LICHS.
READ TABLE TAB_APIN.
IF SY-SUBRC = 0.
IF TAB_APIN-NISU = 'M'.
WRITE: / TAB_APIN-MATNR,TAB_APIN-WERKS, 'Gestion par lots?'.
ENDIF.
MOVE MCHA-LICHA TO TAB_APIN-LICHS.
ADD MCHB-CLABS TO TAB_APIN-LABST.
ADD MCHB-CINSM TO TAB_APIN-INSME.
MODIFY TAB_APIN INDEX SY-TABIX.
ELSE.
MOVE MCHA-LICHA TO TAB_APIN-LICHA.
MOVE MCHA-LICHA TO TAB_APIN-LICHS.
MOVE MCHB-CLABS TO TAB_APIN-LABST.
MOVE MCHB-CINSM TO TAB_APIN-INSME.
MOVE 'I' TO TAB_APIN-FLAG.
INSERT TAB_APIN INDEX 1.
ENDIF.
ENDIF.
ENDSELECT.
ENDSELECT.
ENDFORM.
*---------------------------------------------------------------------*
* FORM EXTRACT_SAP_SUBCONTRACTING *
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
FORM EXTRACT_SAP_SUBCONTRACTING.
SELECT * FROM MSLB
WHERE MATNR = MARA-MATNR
AND WERKS = MARC-WERKS.
MOVE MSLB-CHARG TO W1-CHARG.
MOVE MSLB-LIFNR TO WTRAV.
MOVE MARC-WERKS TO WTRAV+11.
IF CHARG.
SELECT SINGLE * FROM T9F1T
WHERE BUKRS = '0100'
AND ID = 'VENDOR'
AND ARG = WTRAV.
IF SY-SUBRC <> 0.
MOVE '????????' TO T9F1T-FKT.
ENDIF.
SELECT SINGLE * FROM MCHA
WHERE MATNR = MARA-MATNR
AND WERKS = MARC-WERKS
AND CHARG = MSLB-CHARG.
CLEAR TAB_APIN.
MOVE MARA-WRKST TO TAB_APIN-PREMB.
MOVE MARA-MATNR TO TAB_APIN-MATNR.
MOVE MARC-WERKS TO TAB_APIN-WERKS.
MOVE T9F1T-FKT+4(4) TO TAB_APIN-LGORT.
MOVE MSLB-CHARG TO TAB_APIN-CHARG.
MOVE SPACE TO TAB_APIN-LICHA.
MOVE SPACE TO TAB_APIN-LICHS.
MOVE MSLB-LIFNR TO TAB_APIN-VENDOR.
READ TABLE TAB_APIN.
IF SY-SUBRC = 0.
IF TAB_APIN-NISU = 'M'.
WRITE: / TAB_APIN-MATNR,TAB_APIN-WERKS, 'Gestion par lots?'.
ENDIF.
MOVE MCHA-LICHA TO TAB_APIN-LICHS.
* ADD MCHB-CLABS TO TAB_APIN-LABST.
ADD MSLB-LBLAB TO TAB_APIN-LBLAB.
MODIFY TAB_APIN INDEX SY-TABIX.
ELSE.
MOVE MCHA-LICHA TO TAB_APIN-LICHA.
MOVE MCHA-LICHA TO TAB_APIN-LICHS.
* MOVE MCHB-CLABS TO TAB_APIN-LABST.
MOVE MSLB-LBLAB TO TAB_APIN-LBLAB.
MOVE 'I' TO TAB_APIN-FLAG.
INSERT TAB_APIN INDEX 1.
ENDIF.
ENDIF.
ENDSELECT.
ENDFORM.
*---------------------------------------------------------------------*
* FORM SAP_EXTRACT_STOR_LOC. *
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
FORM SAP_EXTRACT_STOR_LOC.
SELECT * FROM MARD
WHERE MATNR = MARA-MATNR
AND WERKS = MARC-WERKS.
CLEAR TAB_APIN.
MOVE MARA-WRKST TO TAB_APIN-PREMB.
MOVE MARA-MATNR TO TAB_APIN-MATNR.
MOVE MARC-WERKS TO TAB_APIN-WERKS.
MOVE MARD-LGORT TO TAB_APIN-LGORT.
MOVE SPACE TO TAB_APIN-CHARG.
MOVE SPACE TO TAB_APIN-LICHA.
READ TABLE TAB_APIN.

IF TAB_APIN-NISU = 'E' OR TAB_APIN-NISU = 'L'.
WRITE: / TAB_APIN-MATNR,TAB_APIN-WERKS, 'Gestion par maga?'.
ENDIF.
MOVE MARD-LABST TO TAB_APIN-LABST.
MOVE MARD-INSME TO TAB_APIN-INSME.
IF SY-SUBRC = 0.
MODIFY TAB_APIN INDEX SY-TABIX.
ELSE.
MOVE 'I' TO TAB_APIN-FLAG.
INSERT TAB_APIN INDEX 1.
ENDIF.

ENDSELECT.
ENDFORM.
*---------------------------------------------------------------------*
* FORM GENERATE_MVTS *
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
FORM GENERATE_MVTS.
MOVE SPACE TO K1-RECORD.
MOVE '0100APMVTIL1' TO K1-RECORD-MAPPE.
MOVE 'MB1A' TO K1-RECORD-TCODE.
MOVE SY-DATUM TO K1-RECORD-BLDAT.
MOVE SY-DATUM TO K1-RECORD-BUDAT.
* MOVE '/' TO K1-RECORD-XBLNR.
MOVE 'Initial Load' TO K1-RECORD-BKTXT.
* MOVE '/' TO K1-RECORD-FRBNR.
* MOVE '/' TO K1-RECORD-WEVER.
* MOVE '/' TO K1-RECORD-LDEST.
MOVE W1-BWART TO K1-RECORD-BWART.
MOVE TAB_APIN-MATNR+12(6) TO K1-RECORD-MATNR.
MOVE TAB_APIN-WERKS TO K1-RECORD-WERKS.
IF W-VENDOR = SPACE.
MOVE TAB_APIN-LGORT TO K1-RECORD-LGORT.
ELSE.
MOVE SPACE TO K1-RECORD-LGORT.
ENDIF.
MOVE TAB_APIN-CHARG TO K1-RECORD-CHARG.
* MOVE '/' TO K1-RECORD-INSMK.
* MOVE '/' TO K1-RECORD-ZUSCH.
* MOVE '/' TO K1-RECORD-SOBKZ.
IF W-VENDOR <> SPACE.
MOVE W-VENDOR TO K1-RECORD-LIFNR.
MOVE 'O' TO K1-RECORD-SOBKZ.
ENDIF.
* MOVE '/' TO K1-RECORD-LIFNR.
* MOVE '/' TO K1-RECORD-KUNNR.
* MOVE '/' TO K1-RECORD-KDAUF.
* MOVE '/' TO K1-RECORD-KDPOS.
* MOVE '/' TO K1-RECORD-KDEIN.
* MOVE '/' TO K1-RECORD-PLPLA.
UNPACK W1-ERFMG TO K1-RECORD-ERFMG.
MOVE 'KG' TO K1-RECORD-ERFME.
* MOVE '/' TO K1-RECORD-BPMNG.
* MOVE '/' TO K1-RECORD-BPRME.
* MOVE '/' TO K1-RECORD-EBELN.
* MOVE '/' TO K1-RECORD-EBELP.
* MOVE '/' TO K1-RECORD-ELIKZ.
* MOVE '/' TO K1-RECORD-SGTXT.
* MOVE '/' TO K1-RECORD-EQUNR.
* MOVE '/' TO K1-RECORD-WEMPF.
* MOVE '/' TO K1-RECORD-ABLAD.
* MOVE '/' TO K1-RECORD-KOKRS.
* MOVE '/' TO K1-RECORD-PARGB.
* MOVE '/' TO K1-RECORD-PARBU.
* MOVE 'PE01' TO K1-RECORD-KOSTL.
* MOVE '/' TO K1-RECORD-PROJN.
* MOVE '/' TO K1-RECORD-AUFNR.
* MOVE '/' TO K1-RECORD-ANLN1.
* MOVE '/' TO K1-RECORD-ANLN2.
* MOVE '/' TO K1-RECORD-RSNUM.
* MOVE '/' TO K1-RECORD-RSPOS.
* MOVE '/' TO K1-RECORD-KZEAR.
* MOVE '/' TO K1-RECORD-UMMAT.
* MOVE '/' TO K1-RECORD-UMWRK.
* MOVE '/' TO K1-RECORD-UMLGO.
* MOVE '/' TO K1-RECORD-UMCHA.
* MOVE '/' TO K1-RECORD-UMZUS.
* MOVE '/' TO K1-RECORD-UMBAR.
* MOVE '/' TO K1-RECORD-UMSOK.
* MOVE '/' TO K1-RECORD-WEANZ.
* MOVE '/' TO K1-RECORD-GRUND.
* MOVE '/' TO K1-RECORD-KONTO.
MOVE ' ' TO K1-RECORD-EXWRT.
* MOVE '/' TO K1-RECORD-XZGVH.
* MOVE '/' TO K1-RECORD-LGTYP.
* MOVE '/' TO K1-RECORD-LGPLA.
* MOVE '/' TO K1-RECORD-GSBER.
* MOVE '/' TO K1-RECORD-KSTRG.
* MOVE '/' TO K1-RECORD-PAOBJNR.
* MOVE '/' TO K1-RECORD-PRCTR.
* MOVE '/' TO K1-RECORD-PS_PSP_PNR.
* MOVE '/' TO K1-RECORD-NPLNR.
* MOVE '/' TO K1-RECORD-AUFPL.
* MOVE '/' TO K1-RECORD-APLZL.
* MOVE '/' TO K1-RECORD-AUFPS.
* MOVE '/' TO K1-RECORD-VPTNR.
* MOVE '/' TO K1-RECORD-FIPOS.
TRANSFER K1-RECORD TO P_FILE_3.
ENDFORM.
*---------------------------------------------------------------------*
* FORM TRANSFER_FILE *
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
FORM TRANSFER_FILE.
* Check existence of a file on IBM mainframe
WRITE:/ 'Step1 : Check MVS file existence'.

CALL FUNCTION 'ZFILE_EXTMVS'
EXPORTING
INPUT_FILE = P_FILE_M
MVS_NODE = P_NODE_M
TEST_MODE = SPACE
EXCEPTIONS
FILE_NOT_FOUND = 01
INVALID_NAME_MVS = 02
NOT_AUTHORISED = 03
OTHER_PROBLEM = 04.

CASE SY-SUBRC.
WHEN 1.
WRITE:/ 'Step1 : File MVS not found : ', P_FILE_M.
WHEN 2.
WRITE:/ 'Step1 : Invalid MVS file name : ', P_FILE_M.
WHEN 3.
WRITE:/ 'Step1 : User not authorized to check file ', P_FILE_M.
WHEN OTHERS.
ENDCASE.

WRITE:/ 'Step1 : rc = ', SY-SUBRC.
CHECK SY-SUBRC = 0.

* Transfer file from IBM mainframe to RISC 6000 mainframe
WRITE:/ 'Step2 : Transfer MVS file to UNIX'.

CALL FUNCTION 'ZTR_MVS_UNIX'
EXPORTING
INPUT_FILE = P_FILE_M
MVS_NODE = P_NODE_M
OUTPUT_FILE = P_FILE_U
SECURITY_CODE = P_CODE_U
TEST_MODE = SPACE
EXCEPTIONS
BAD_SECURITY_CODE = 01
FILE_NOT_FOUND_MVS = 02
FILE_NOT_FOUND_UNIX = 03
INVALID_NAME_MVS = 04
NOT_AUTHORISED = 05
OTHER_PROBLEM = 06.

CASE SY-SUBRC.
WHEN 1.
WRITE:/ 'Step2 : Bad security code : ', P_CODE_U.
WHEN 2.
WRITE:/ 'Step2 : File MVS not found : ', P_FILE_M.
WHEN 3.
WRITE:/ 'Step2 : File UNIX not found ', P_FILE_U.
WHEN 4.
WRITE:/ 'Step2 : Invalid MVS file name : ', P_FILE_M.
WHEN 5.
WRITE:/ 'Step2 : User not authorized to copy file ', P_FILE_M.
WHEN OTHERS.
ENDCASE.

WRITE:/ 'Step2 : rc = ', SY-SUBRC.
CHECK SY-SUBRC = 0.

* Delete IBM file
IF PARM_DEL = 'X'.
WRITE:/ 'Step3 : Delete MVS file'.

CALL FUNCTION 'ZFILE_DELMVS'
EXPORTING
INPUT_FILE = P_FILE_M
MVS_NODE = P_NODE_M
TEST_MODE = SPACE
EXCEPTIONS
FILE_NOT_FOUND = 01
INVALID_NAME_MVS = 02
NOT_AUTHORISED = 03
OTHER_PROBLEM = 04.

CASE SY-SUBRC.
WHEN 1.
WRITE:/ 'Step3 : File MVS not found : ', P_FILE_M.
WHEN 2.
WRITE:/ 'Step3 : Invalid MVS file name : ', P_FILE_M.
WHEN 3.
WRITE:/ 'Step3 : User not authorized to delete file ', P_FILE_M.
WHEN OTHERS.
ENDCASE.

WRITE:/ 'Step3 : rc = ', SY-SUBRC.
CHECK SY-SUBRC = 0.
ENDIF.
ENDFORM.

*---------------------------------------------------------------------*
* FORM OPEN_FILE *
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
FORM OPEN_FILE.

OPEN DATASET P_FILE_2 FOR INPUT IN BINARY MODE.
IF SY-SUBRC NE 0.
WRITE:/ 'Step4 : rc = ', SY-SUBRC.
WRITE:/ 'Unable to open dataset : ', P_FILE_2.
ENDIF.
OPEN DATASET P_FILE_3 FOR OUTPUT IN TEXT MODE.

ENDFORM.

*---------------------------------------------------------------------*
* FORM CLOSE_FILE *
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
FORM CLOSE_FILE.

CLOSE DATASET P_FILE_2.
CLOSE DATASET P_FILE_3.

ENDFORM.

*---------------------------------------------------------------------*
* FORM INITIALISATION_SBDC *
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
FORM INITIALISATION_SBDC.

CALL FUNCTION 'BDC_OPEN_GROUP'
EXPORTING
CLIENT = SY-MANDT
GROUP = '0100APMVTIL2'
KEEP = ' '
USER = SY-UNAME
EXCEPTIONS
CLIENT_INVALID = 01
DESTINATION_INVALID = 02
GROUP_INVALID = 03
HOLDDATE_INVALID = 04
INTERNAL_ERROR = 05
QUEUE_ERROR = 06
RUNNING = 07
USER_INVALID = 08.

ENDFORM.

*---------------------------------------------------------------------*
* FORM GENERATE_SBDC_TRANSACTION *
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
FORM GENERATE_SBDC_TRANSACTION.
*----------------------------------------------------------------------
MOVE SPACE TO K1-RECORD.
MOVE '0100APMVTIL1' TO K1-RECORD-MAPPE.
MOVE 'MSC1' TO K1-RECORD-TCODE.
MOVE TAB_APIN-MATNR TO K1-RECORD-MATNR.
MOVE TAB_APIN-CHARG TO K1-RECORD-CHARG.
MOVE TAB_APIN-WERKS TO K1-RECORD-WERKS.
MOVE TAB_APIN-LICHA TO K1-RECORD-BKTXT.
TRANSFER K1-RECORD TO P_FILE_3.
*----------------------------------------------------------------------
*
* CALL FUNCTION 'BDC_INSERT'
* EXPORTING
* TCODE = 'ZSC2'
* TABLES
* DYNPROTAB = TBDCDATA
* EXCEPTIONS
* INTERNAL_ERROR = 01
* NOT_OPEN = 02
* QUEUE_ERROR = 03
* TCODE_INVALID = 04.
*
ENDFORM.

*---------------------------------------------------------------------*
* FORM GENERATE_SBDC *
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
FORM GENERATE_SBDC.
REFRESH TBDCDATA.
CLEAR TBDCDATA.
TBDCDATA-PROGRAM = 'SAPMM03S'.
TBDCDATA-DYNPRO = '0105'.
TBDCDATA-DYNBEGIN = 'X'.
APPEND TBDCDATA.

CLEAR TBDCDATA.
TBDCDATA-FNAM = 'RM03S-MATNR'.
TBDCDATA-FVAL = TAB_APIN-MATNR+12(6).
APPEND TBDCDATA.

CLEAR TBDCDATA.
TBDCDATA-FNAM = 'RM03S-WERKS'.
TBDCDATA-FVAL = TAB_APIN-WERKS.
APPEND TBDCDATA.

CLEAR TBDCDATA.
TBDCDATA-FNAM = 'RM03S-CHARG'.
TBDCDATA-FVAL = TAB_APIN-CHARG.
APPEND TBDCDATA.



CLEAR TBDCDATA.
TBDCDATA-PROGRAM = 'SAPMM03S'.
TBDCDATA-DYNPRO = '0200'.
TBDCDATA-DYNBEGIN = 'X'.
APPEND TBDCDATA.

CLEAR TBDCDATA.
TBDCDATA-FNAM = 'MCHA-LICHA'.
TBDCDATA-FVAL = TAB_APIN-LICHA.
APPEND TBDCDATA.

CLEAR TBDCDATA.
TBDCDATA-FNAM = 'BDC_OKCODE'.
TBDCDATA-FVAL = 'BU'.
APPEND TBDCDATA.

ENDFORM.
*---------------------------------------------------------------------*
* FORM CLOSE_SBDC *
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
FORM CLOSE_SBDC.

CALL FUNCTION 'BDC_CLOSE_GROUP'
EXCEPTIONS
NOT_OPEN = 01
QUEUE_ERROR = 02.
ENDFORM.