89 lines
2.9 KiB
ABAP
89 lines
2.9 KiB
ABAP
CLASS {{p}}roast_merger DEFINITION PUBLIC FINAL CREATE PUBLIC.
|
|
PUBLIC SECTION.
|
|
TYPES:
|
|
ty_batch_id TYPE c LENGTH 10,
|
|
ty_plant TYPE c LENGTH 4,
|
|
ty_lot_id TYPE c LENGTH 8,
|
|
ty_weight TYPE p LENGTH 7 DECIMALS 2,
|
|
BEGIN OF ty_batch,
|
|
batch_id TYPE ty_batch_id,
|
|
plant TYPE ty_plant,
|
|
lot_id TYPE ty_lot_id,
|
|
roast_date TYPE d,
|
|
roasted_kg TYPE ty_weight,
|
|
END OF ty_batch,
|
|
tt_batches TYPE STANDARD TABLE OF ty_batch WITH EMPTY KEY,
|
|
BEGIN OF ty_lot_total,
|
|
lot_id TYPE ty_lot_id,
|
|
batch_count TYPE i,
|
|
total_kg TYPE ty_weight,
|
|
last_date TYPE d,
|
|
END OF ty_lot_total,
|
|
tt_lot_totals TYPE STANDARD TABLE OF ty_lot_total WITH EMPTY KEY.
|
|
|
|
METHODS merge
|
|
IMPORTING it_plant_a TYPE tt_batches
|
|
it_plant_b TYPE tt_batches
|
|
RETURNING VALUE(rt_totals) TYPE tt_lot_totals.
|
|
|
|
PRIVATE SECTION.
|
|
TYPES tt_unique TYPE HASHED TABLE OF ty_batch WITH UNIQUE KEY batch_id.
|
|
|
|
METHODS add_batches
|
|
IMPORTING it_batches TYPE tt_batches
|
|
CHANGING ct_unique TYPE tt_unique.
|
|
|
|
METHODS build_totals
|
|
IMPORTING it_unique TYPE tt_unique
|
|
RETURNING VALUE(rt_totals) TYPE tt_lot_totals.
|
|
ENDCLASS.
|
|
|
|
|
|
CLASS {{p}}roast_merger IMPLEMENTATION.
|
|
METHOD merge.
|
|
DATA lt_unique TYPE tt_unique.
|
|
add_batches( EXPORTING it_batches = it_plant_a
|
|
CHANGING ct_unique = lt_unique ).
|
|
add_batches( EXPORTING it_batches = it_plant_b
|
|
CHANGING ct_unique = lt_unique ).
|
|
rt_totals = build_totals( lt_unique ).
|
|
ENDMETHOD.
|
|
|
|
METHOD add_batches.
|
|
LOOP AT it_batches INTO DATA(ls_batch).
|
|
IF ls_batch-batch_id IS INITIAL OR ls_batch-roasted_kg <= 0.
|
|
CONTINUE.
|
|
ENDIF.
|
|
READ TABLE ct_unique WITH TABLE KEY batch_id = ls_batch-batch_id
|
|
ASSIGNING FIELD-SYMBOL(<ls_kept>).
|
|
IF sy-subrc <> 0.
|
|
INSERT ls_batch INTO TABLE ct_unique.
|
|
ELSEIF ls_batch-roasted_kg > <ls_kept>-roasted_kg.
|
|
<ls_kept> = ls_batch.
|
|
ENDIF.
|
|
ENDLOOP.
|
|
ENDMETHOD.
|
|
|
|
METHOD build_totals.
|
|
DATA lt_totals TYPE SORTED TABLE OF ty_lot_total WITH UNIQUE KEY lot_id.
|
|
LOOP AT it_unique INTO DATA(ls_batch).
|
|
READ TABLE lt_totals WITH TABLE KEY lot_id = ls_batch-lot_id
|
|
ASSIGNING FIELD-SYMBOL(<ls_total>).
|
|
IF sy-subrc <> 0.
|
|
INSERT VALUE #( lot_id = ls_batch-lot_id
|
|
batch_count = 1
|
|
total_kg = ls_batch-roasted_kg
|
|
last_date = ls_batch-roast_date )
|
|
INTO TABLE lt_totals.
|
|
CONTINUE.
|
|
ENDIF.
|
|
<ls_total>-batch_count = <ls_total>-batch_count + 1.
|
|
<ls_total>-total_kg = <ls_total>-total_kg + ls_batch-roasted_kg.
|
|
IF ls_batch-roast_date > <ls_total>-last_date.
|
|
<ls_total>-last_date = ls_batch-roast_date.
|
|
ENDIF.
|
|
ENDLOOP.
|
|
rt_totals = lt_totals.
|
|
ENDMETHOD.
|
|
ENDCLASS.
|