Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Need help on logic for summing quantities!

former_member565051
Participant
0 Kudos
552

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

4 REPLIES 4

VXLozano
Active Contributor
382

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.

former_member565051
Participant
0 Kudos
382

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.




VXLozano
Active Contributor
0 Kudos
382

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:

  • - control table with index, qty_sum, documents sub-table
  • - table with "non used batches" (created as a copy of the documents table, and when a doc is used, delete it from here)
  • - loop in the main documents (sorted by qty descending)
  • - check the document is in the "non used" table
  • - add a new line to the control table and delete it from the non used one (add the doc number to the documents sub-table, and put the qty to the qty_sum field).
  • - loop again for documents with qty < your_max - the outer loop qty
  • - add a new line to the control table's documents-subtable, adding its qty to the qty_sum field

Repeat this until your qty_sum is your_max.

Sandra_Rossi
Active Contributor
0 Kudos
382

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)