Purchase orders Closure.
The bapi ‘BAPI_PO_CHANGE’ used to close
the Purchase Order. The input fields are BEDAT, BSART and WERKS, Provide Radio
button to close completely Open Purchase Orders and Partially Open Purchase
Orders.
- Provide the Selection Screen with Purchasing
Document Date, Purchasing Document Type and Plant and Provide button to close
completely Open Purchase Orders and Partially Open Purchase Orders.
- Fetch the Header Data for Open Purchase Order into
an Internal Table by selecting All Header details from EKKO table by Passing
BSTYP = ‘F’ , Purchasing Document Date(BEDAT) = Selection Screen Date ,
BUKRS(Company Code) = ‘XXX’ and Purchasing Document Type (BSART) = Selection
Screen Purchasing Document Type.
- For the header data, fetch the relevant item details
into an internal table from EKPO table by passing LOEKZ = ‘ ’, ELIKZ = ‘ ’ and
Plant (WERKS) = Selection Screen Plant.
- Loop the item data and read the header data, to move
all the header and item details into a single internal table.
- For the header and item details, fetch the relevant
Schedule line details into an internal table from EKET Table.
- For the schedule line details, fetch the relevant History
per Purchasing Document into an internal table from EKBE Table.
- If Complete Open Purchase Order (Radio button1
PSNO). Then Delete EKBE where BEWTP EQ ‘Q’. Loop the EKET internal table and
Read the EKBE internal table with key EBELN and EBELP. If EKBE is an initial
move the EKET values to EKET1 and append EKET1 into an internal table.
- If EKET1 is not initial, then Select EBELN, EBELP,
KNTTP, PSTYP, MATNR, TXZ01 from EKPO table into an EKPO internal table for all
entries in EKKO table where EBELN EQ EKKO-EBELN and LOEKZ equal to ‘L’ and
WERKS equal to selection parameter P_WERKS.
- Loop the EKPO internal table, move the values to a
message log internal table, by moving message text ‘Deletion indicator already
set for the item’.
- Loop the EKET1 internal table, move PO number
(EBELN), PO item (EBELP) and deletion indicator ‘L’ and append the BAPI
internal table. In the same move Update field Value ‘X’ and append the BAPI
update internal table.
- Call the BAPI_CHANGE Function module and pass the
internal table and pass the BAPI_TRANSACTION_COMMIT.
- Get the Return values and move it into a message log
internal table and append it. Use GUI_DOWNLOAD and download the internal table.
- Else if Partial Open Purchase Order (Radio Button2
SONO).
- Delete the values in the History per Purchasing
Document internal table by BEWTP (PO history category) EQ ‘Q’.
- Fetch the Service Entry Sheet Header Data, for all History
per Purchasing Document values from ESSR Table by passing Entry sheet number (LBLNI)
from ESSR = Document No. of a Reference Document(LFBNR) from EKBE table.
- Loop the Schedule line internal table and read the History
per Purchasing Document.
- By passing certain condition move the Schedule line
details, History per Purchasing Document details into a single internal table.
- State the condition EKBE-BEWTP EQ ‘E’ , then loop
the History per Purchasing Document internal table using key field of PO number
and item number.
- Create an internal table with PO number, PO item, Number
of Material Document (BELNR), Item in Material Document (BUZEI).
- Read that newly created internal table for BELNR,
With the Key of EBELN, EBELP, BELNR, and BUZEI.
- Check for the BELNR Equal is initial, Check for the
Condition where Movement type (BWART) EQ ‘101’ and PO number and PO item number
EQ to History per Purchasing Document PO number and PO item number.
- Else if check for the movement type ‘102’ and ‘122’
and EBELN and EBELP, if BWART (Movement type) = 122 add the Quantity for the
new internal for Schedule line = new internal for Schedule line + History per
Purchasing Document quantity (MENGE), and move the same for quantity1 as well
else move the Quantity for the new internal for Schedule line = new internal
for Schedule line + History per Purchasing Document quantity (MENGE).Check for
the Quantity for movement type 103 is not initial, for movement type 122 clear
the Quantity and Set a flag = ‘Y’, else if movement type 101 initial Clear
quantity1 and Set a flag = ‘X’.
- Else if check for the movement type ‘103’ and EBELN
and EBELP, Quantity 103 = quantity 103 + EKBE-BAMNG.
- Else if check for the movement type ‘104’ , ‘122’ and
‘124’ and EBELN and EBELP, if BWART (Movement type) = 104 or 124 , add
quantity1 = quantity1 + EKBE-BAMNG Else add the Quantity for the new internal
for Schedule line = new internal for Schedule line + History per Purchasing
Document quantity (MENGE), and move the same for quantity1 as well. Check for
the Quantity for movement type 103 is not initial for movement type 122 clear
the Quantity and Set a flag = ‘Y’, else if movement type 101 initial Clear
quantity1 and Set a flag = ‘X’ else set flag = ‘Y’.
- Else if check for the movement type ‘101’ and EBELN
and EBELP is not initial, if flag = ‘Y’, finalquantity103 = quantity103 –
quantity1 and final value = menge value - finalquantity103 and quantity103 =
quantity103 + quantity1. Else if flag = ‘X’ then finalquantity101 = quantity101
– quantity and final value = menge value - finalquantity101 and quantity101 =
quantity101 + quantity. If final value not equal to ‘0’ , move EBELN, EBELP,
EINDT, SLFDT, LPEIN, ETENR, if flag = ‘X’ Move finalquantity101 to menge
else Move finalquantity103 to menge and
WEMNG and MENGE Value and append into EKET2 internal table and quantity101 =
quantity101 + EKBE-MENGE and Set flag = ‘X’.
- Else if check for the movement type ‘102’, ‘122’ and
EBELN and EBELP is not initial, if flag = ‘Y’, finalquantity103 = quantity103 –
quantity1 and final value = menge value - finalquantity103 and quantity103 =
quantity103 + quantity1. Else if flag = ‘X’ then finalquantity101 = quantity101
– quantity and final value = menge value - finalquantity101 and quantity101 =
quantity101 + quantity. If final value not equal to ‘0’, move EBELN, EBELP,
EINDT, SLFDT, LPEIN, ETENR, if flag = ‘X’ Move finalquantity101 to menge
else Move finalquantity103 to menge and
WEMNG and MENGE Value and append into EKET2 internal table and if movement type
= 122, quantity = quantity + EKBE-MENGE and quantity = quantity + EKBE-MENGE ,
check quantity103 is not initial Set flag = ‘Y’ and clear quantity else
quantity = quantity + EKBE-MENGE , check quantity101 is not initial Set flag =
‘X’ and clear quantity1 else set flag = ‘X’.
- Else if check for the movement type ‘103’ and EBELN
and EBELP is not initial, if flag = ‘Y’, finalquantity103 = quantity103 –
quantity1 and final value = menge value - finalquantity103 and quantity103 =
quantity103 + quantity1. Else if flag = ‘X’ then finalquantity101 = quantity101
– quantity and final value = menge value - finalquantity101 and quantity101 =
quantity101 + quantity. If final value not equal to ‘0’ , move EBELN, EBELP,
EINDT, SLFDT, LPEIN, ETENR, if flag = ‘X’ Move finalquantity101 to menge
else Move finalquantity103 to menge and
WEMNG and MENGE Value and append into EKET2 internal table and quantity103 =
quantity103 + EKBE-MENGE and Set flag = ‘Y’.
- Else if check for the movement type ‘104’, ‘122’,
‘124’ and EBELN and EBELP is not initial, if flag = ‘Y’, finalquantity103 =
quantity103 – quantity1 and final value = menge value - finalquantity103 and
quantity103 = quantity103 + quantity1. Else if flag = ‘X’ then finalquantity101
= quantity101 – quantity and final value = menge value - finalquantity101 and
quantity101 = quantity101 + quantity. If final value not equal to ‘0’, move EBELN,
EBELP, EINDT, SLFDT, LPEIN, ETENR, if flag = ‘X’ Move finalquantity101 to menge
else Move finalquantity103 to menge and
WEMNG and MENGE Value and append into EKET2 internal table and if movement type
= ‘124’ or ‘104’, quantity1 = quantity + EKBE-MENGE, check quantity103 is not
initial Set flag = ‘Y’ and clear quantity else quantity1 = quantity1 +
EKBE-MENGE and quantity = quantity + EKBE-MENGE, check quantity101 is not
initial Set flag = ‘X’ and clear quantity1 else set flag = ‘Y’.
- Else if check for the movement type ‘101’, if flag =
‘Y’, finalquantity103 = quantity103 – quantity1 and final value = menge value -
finalquantity103 and quantity103 = quantity103 + quantity1. Else if flag = ‘X’
then finalquantity101 = quantity101 – quantity and final value = menge value -
finalquantity101 and quantity101 = quantity101 + quantity. If final value not
equal to ‘0’ , move EBELN, EBELP, EINDT, SLFDT, LPEIN, ETENR, if flag = ‘X’
Move finalquantity101 to menge else Move
finalquantity103 to menge and WEMNG and MENGE Value and append into EKET2
internal table and quantity101 = quantity101 + EKBE-MENGE and Set flag = ‘X’.
- Else if check for the movement type ‘102’, ‘122’, if
flag = ‘Y’, finalquantity103 = quantity103 – quantity1 and final value = menge
value - finalquantity103 and quantity103 = quantity103 + quantity1. Else if
flag = ‘X’ then finalquantity101 = quantity101 – quantity and final value =
menge value - finalquantity101 and quantity101 = quantity101 + quantity. If
final value not equal to ‘0’, move EBELN, EBELP, EINDT, SLFDT, LPEIN, ETENR, if
flag = ‘X’ Move finalquantity101 to menge else
Move finalquantity103 to menge and WEMNG and MENGE Value and append into
EKET2 internal table and if movement type = 122, quantity = quantity +
EKBE-MENGE and quantity = quantity + EKBE-MENGE , check quantity103 is not
initial Set flag = ‘Y’ and clear quantity else quantity = quantity + EKBE-MENGE
, check quantity101 is not initial Set flag = ‘X’ and clear quantity1 else set
flag = ‘X’.
- Else if check for the movement type ‘103’, if flag =
‘Y’, finalquantity103 = quantity103 – quantity1 and final value = menge value -
finalquantity103 and quantity103 = quantity103 + quantity1. Else if flag = ‘X’
then finalquantity101 = quantity101 – quantity and final value = menge value - finalquantity101
and quantity101 = quantity101 + quantity. If final value not equal to ‘0’ ,
move EBELN, EBELP, EINDT, SLFDT, LPEIN, ETENR, if flag = ‘X’ Move
finalquantity101 to menge else Move
finalquantity103 to menge and WEMNG and MENGE Value and append into EKET2
internal table and quantity103 = quantity103 + EKBE-MENGE and Set flag = ‘Y’.
- Else if check for the movement type ‘104’, ‘122’,
‘124’, if flag = ‘Y’, finalquantity103 = quantity103 – quantity1 and final
value = menge value - finalquantity103 and quantity103 = quantity103 +
quantity1. Else if flag = ‘X’ then finalquantity101 = quantity101 – quantity
and final value = menge value - finalquantity101 and quantity101 = quantity101
+ quantity. If final value not equal to ‘0’, move EBELN, EBELP, EINDT, SLFDT,
LPEIN, ETENR, if flag = ‘X’ Move finalquantity101 to menge else Move finalquantity103 to menge and WEMNG and
MENGE Value and append into EKET2 internal table and if movement type = ‘124’
or ‘104’, quantity1 = quantity + EKBE-MENGE, check quantity103 is not initial
Set flag = ‘Y’ and clear quantity else quantity1 = quantity1 + EKBE-MENGE and
quantity = quantity + EKBE-MENGE, check quantity101 is not initial Set flag =
‘X’ and clear quantity1 else set flag = ‘Y’.
- Move all the values to BELNR Internal Table and
append it and calculate menge value = menge value + EKET-MENGE and end if
Statement and end the Loop Statement.
- Else if Check for EKBE-BEWTP = ‘D’, Read the ESSR
Internal Table with key LBLNI = EKBE-LFBNR and LOKEZ = ‘X’. Check the if SY-SUBRC
EQ ‘0’ move all value from EKET to EKET1 and Append it, else read the EKBE1
Internal Table With Key LFBNR = EKBE-LFBNR BEWTP = ‘E’ BWART = ‘102’ . If EKBE1
is not an initial Move the values from EKET to EKET1 and append the EKET1
Internal Table.
- Else Move the values from EKET to EKET1 and append
the EKET1 Internal Table and end if statement.
- Else Move the values from EKET to EKET1 and append
the EKET1 Internal Table and end if Statement and end the Loop Statement.
- If quantity103 is not initial, finalquantity103 =
Quantity103 – Quantity1 and final value = Menge Value – Finalquantity103 and
Set flag = ‘Y’. Else if quantity101 is not initial, finalquantity101 =
Quantity101 – Quantity and final value = Menge Value – Finalquantity101 and Set
flag = ‘X’. If final value not equal to ‘0’, move EBELN, EBELP, EINDT, SLFDT,
LPEIN, ETENR, if flag = ‘X’ Move finalquantity101 to EKET2-menge else Move finalquantity103 to EKET2-menge and
WEMNG and MENGE Value and append into EKET2 internal table and end if statement.
- Loop the EKET internal table, if EBELN = EKET-EBELN
and EBELP = EKET-EBELP, Move Menge Value = Menge value + EKET-MENGE and if
EBELN and EBELP is initial Move Menge Value = Menge value + EKET-MENGE and Move
EBELN, EBELP, Menge Value to BELNR and append the internal table and move the
values of EKET-EBELN to EBELN and EKET-EBELP to EBELP and end the loop
Statement.
- If menge value is not initial move EBELN, EBELP,
MENGE Value to BELNR internal table and append it and end if Statement.
- Again Loop the EKET internal table read the EKBE
internal Table with key BEWTP = ‘E’ EBELN EBELP, if EKBE is not initial Read
EKET2 internal table if SY-SUBRC = ‘0’ Read the BELNR internal table, Calculate
Menge Value = EKET2-MENGE – BELNR-MENGE.
If EKET2-MENGE = ‘0’, Move the EKBE and EKET Values to EKET1 and Append
the EKET2 internal table and move EBELN = EKBE-EBELN and EBELP = EKBE-EBELP.
- Else if EKET-MENGE > ‘0’ and EKET-EBELN = EBELN
and EKET-EBELP = EBELP and EKET2-MENGE = ‘0’, Move all the Values from EKBE,
EKET to EKET1 and Calculate MENGE1 = EKET-MENGE - EKET2-MENGE and EKET1-MENGE =
MENGE1, append the EKET1 internal table and assign EKET2-MENGE = ‘0’ Modify the
EKET2 internal table by transporting menge where EBELN and EBELP equal to EKBE
and move EBELN = EKBE-EBELN and EBELP = EKBE-EBELP.
- Else if EKET-MENGE > ‘0’ and EKET-MENGE >
EKET2-MENGE or EKET-MENGE = EKET2-MENGE, Move all the Values from EKBE and EKET
to EKET1 and Calculate MENGE1 = EKET-MENGE - EKET2-MENGE and EKET1-MENGE =
MENGE1, append the EKET1 internal table and assign EKET2-MENGE = ‘0’ Modify the
EKET2 internal table by transporting menge where EBELN and EBELP equal to EKBE
and move EBELN = EKBE-EBELN and EBELP = EKBE-EBELP.
- Else if EKET-MENGE > ‘0’ and EKET-MENGE <
EKET2-MENGE and EKET2-MENGE = ‘0’, Move all the Values from EKBE and EKET to
EKET1 and EKET1-MENGE = ‘0’ and Calculate MENGE1 = EKET-MENGE - EKET2-MENGE and
append the EKET1 internal table and assign EKET2-MENGE = MENGE1 Modify the
EKET2 internal table by transporting menge where EBELN and EBELP equal to EKBE
and move EBELN = EKBE-EBELN and EBELP = EKBE-EBELP.
- Else if EKET-MENGE > ‘0’ and EKET-MENGE <
EKET2-MENGE and EKET2-MENGE not equal to ‘0’, Move all the Values from EKBE and
EKET to EKET1 and EKET1-MENGE = ‘0’ and Calculate MENGE1 = EKET-MENGE -
EKET2-MENGE and append the EKET1 internal table and assign EKET2-MENGE = MENGE1
Modify the EKET2 internal table by transporting menge where EBELN and EBELP
equal to EKBE and move EBELN = EKBE-EBELN and EBELP = EKBE-EBELP.
- Else if EKET-MENGE > ‘0’ and EKET-MENGE <
EKET2-MENGE and EKET2-MENGE equal to ‘0’ and EKET-EBELN = EBELN and EKET-EBELP
= EBELP, Move all the Values from EKBE and EKET to EKET1 and append the EKET1
internal table and move EBELN = EKBE-EBELN and EBELP = EKBE-EBELP.
- End if statement and end if statement and end the
loop statement.
- Sort the EKET1 internal table by EBELN EBELP.
- Delete adjacent duplicates EKET1 internal table by
comparing EBELN EBELP ETENR.
- Loop the EKET1 internal table, move PO number
(EBELN), PO item (EBELP) and deletion indicator ‘L’ and Move Schedule line
(ETENR) and Move Quantity (MENGE) and append the BAPI internal table. In the
same move Update field Value ‘X’ and append the BAPI update internal table.
- Call the BAPI_CHANGE Function module and pass the internal
table and pass the BAPI_TRANSACTION_COMMIT.
- Get the Return values and move it into a message log
internal table and append it. Use GUI_DOWNLOAD and download the internal table.