2021 Jul 19 11:06 AM
Hello Experts,
I need a help to proceed with logic where i need to sum the quantities in internal table based on the required quantity.
User is sending the required quantity from Idoc and based on that we have to create Material document using BAPI.
My internal table contains various deliveries (LIKP-VBELN) and ORMNG (quantities) and i need to get only those quantities whose sum is equal to the requested quantities from Idoc.
Eg. if requested quantity is 7, and I have records in internal table with quantities (ORMNG = 4, 5, 2, 2, 2, 2) then how can i select only those deliveries from Internal table whose quantities sum = 7 (5 + 2) ?
(Attaching Internal table screenshot for reference).
Kindly help me with the logic to achieve above requirement.
Thanks & Regards,
Ajay
2021 Jul 19 11:30 AM
This requirement sounds weird. Too weird to be real or useful. It's nonsense: why will you "join" different sales documents based on some value sum?
I'd ask the functional (or the user) why do they need it, because I'm pretty sure there's something wrong there.
I assume you will need to loop at the table for each entry lower than 7, and then seek for its compatibles (rows with ormng 7- your row-ormng)... recursively, because maybe you cannot find 4+3 but you can find 4+2+1.
Once you have used a row, be sure to delete it from the initial table (I'd make a copy of that table to work for).
I cannot think about a short way to give you some pseudo-code, and I have not much time to spare here. Good luck.
2021 Jul 19 12:07 PM
Hey vicen.lozano ,
Thanks for stopping by,
i know the requirement is bit tedious, but what functional consultant want is to get the deliveries equal to the required quantity and creating their material docs (if totals deliveries are captured as 3, then there will be 3 mat docs.)
I have used below logic to get the required set of quantities, but it is failing when there are more than 3 rows required.
Eg. Required quantity = 8 ==> (4 + 2 + 2)
DATA(lv_tabix3) = 0.
LOOP AT lt_batch INTO DATA(lwa_batch).
lv_tabix3 = sy-tabix.
IF lwa_batch-ormng <> lv_idoc_qty.
lv_tabix3 = lv_tabix3 + 1.
LOOP AT lt_batch INTO DATA(lwa_batch_2) FROM lv_tabix3.
DATA(lv_qty) = lwa_batch_2-ormng + lwa_batch-ormng.
IF lv_qty = lv_idoc_qty.
lwa_batch_item-vbeln = lwa_batch_2-vbeln.
lwa_batch_item-qty = lwa_batch_2-ormng.
lwa_batch_item-matnr = lwa_batch_2-matnr.
lwa_batch_item-batch = lwa_batch_2-charg.
APPEND lwa_batch_item TO lt_batch_item.
lwa_batch_item-vbeln = lwa_batch-vbeln.
lwa_batch_item-qty = lwa_batch-ormng.
lwa_batch_item-matnr = lwa_batch-matnr.
lwa_batch_item-batch = lwa_batch-charg.
APPEND lwa_batch_item TO lt_batch_item.
EXIT.
ELSE.
CONTINUE.
ENDIF.
ENDLOOP.
ENDIF.
ENDLOOP.
2021 Jul 19 2:19 PM
I'm sorry, but it's still a ridiculous requirement. Why the deliveries must be sum a specific quantity? There's something wrong there, on the requirements side or in the real life workflow.
I'm sorry again, but I'm trying to find a short way to explain how I'd do it, but it takes me too much time, and I cannot develop for you. Hints:
Repeat this until your qty_sum is your_max.
2021 Jul 19 3:54 PM
If the "required quantity" is 7, try to find the closest-lower-or-equal "delivery quantity" (could be 7, or 6, etc.) If you find one, then repeat again as if "required quantity" = what remains. If in the end you have remaining <> 0, then you have to search again with only the deliveries that you already tried.
(the algorithm is highly simplified here, you will end in a very complex one ; I recommend to get advice from your consultant)