Skip to content

Instantly share code, notes, and snippets.

@weiserman
Created August 22, 2026 09:31
Show Gist options
  • Select an option

  • Save weiserman/c0ce96fcd66416476d8bf5f41cc026c5 to your computer and use it in GitHub Desktop.

Select an option

Save weiserman/c0ce96fcd66416476d8bf5f41cc026c5 to your computer and use it in GitHub Desktop.
Equipment Scan
CLASS zcl_equip_scan DEFINITION PUBLIC.
PUBLIC SECTION.
INTERFACES z2ui5_if_app.
TYPES:
BEGIN OF ty_s_issue,
issue_id TYPE string,
problem_text TYPE string,
severity TYPE string,
created_on TYPE string,
created_by TYPE string,
photo_label TYPE string,
END OF ty_s_issue.
TYPES:
BEGIN OF ty_s_combo,
key TYPE string,
text TYPE string,
END OF ty_s_combo.
DATA mv_scan_input TYPE string.
DATA mv_page TYPE string.
DATA mv_found TYPE abap_bool.
DATA mv_equnr TYPE string.
DATA mv_descr TYPE string.
DATA mv_category TYPE string.
DATA mv_location TYPE string.
DATA mv_maker TYPE string.
DATA mv_status TYPE string.
DATA mv_problem TYPE string.
DATA mv_severity TYPE string.
DATA mv_file TYPE string.
DATA mv_filepath TYPE string.
DATA mv_photo TYPE string.
DATA mt_severity TYPE STANDARD TABLE OF ty_s_combo WITH EMPTY KEY.
DATA mt_issue TYPE STANDARD TABLE OF ty_s_issue WITH EMPTY KEY.
PROTECTED SECTION.
DATA client TYPE REF TO z2ui5_if_client.
METHODS seed_if_empty.
METHODS lookup_equipment.
METHODS load_issues.
METHODS log_problem.
METHODS go_back.
METHODS view_display.
METHODS view_scan.
METHODS view_detail.
PRIVATE SECTION.
ENDCLASS.
CLASS zcl_equip_scan IMPLEMENTATION.
METHOD z2ui5_if_app~main.
me->client = client.
IF client->check_on_init( ).
seed_if_empty( ).
mv_page = `SCAN`.
mv_severity = `MEDIUM`.
mt_severity = VALUE #(
( key = `LOW` text = `Low` )
( key = `MEDIUM` text = `Medium` )
( key = `HIGH` text = `High` ) ).
view_display( ).
ELSEIF client->check_on_navigated( ).
view_display( ).
ELSEIF client->check_on_event( ).
CASE client->get_event( ).
WHEN `LOOKUP`.
" seed_if_empty( ) used to run here: a SELECT SINGLE on every lookup to
" check demo data that only matters at init, where it is still called.
lookup_equipment( ).
view_display( ).
WHEN `SCANNED`.
" The camera hands the barcode straight to the lookup, so a scan lands
" on the detail page in one action with no second tap on Look up.
mv_scan_input = client->get_event_arg( 1 ).
lookup_equipment( ).
view_display( ).
WHEN `PHOTO`.
IF mv_file IS NOT INITIAL.
mv_photo = mv_file.
CLEAR: mv_file, mv_filepath.
ENDIF.
view_display( ).
WHEN `PHOTO_CLEAR`.
CLEAR: mv_photo, mv_file, mv_filepath.
view_display( ).
WHEN `LOG`.
log_problem( ).
view_display( ).
WHEN `BACK`.
go_back( ).
view_display( ).
ENDCASE.
ENDIF.
ENDMETHOD.
METHOD seed_if_empty.
SELECT SINGLE equipment_id
FROM zequip_hdr
WHERE equipment_id = `6009223606314`
INTO @DATA(lv_ready).
IF sy-subrc = 0.
RETURN.
ENDIF.
DELETE FROM zequip_hdr.
COMMIT WORK AND WAIT.
INSERT zequip_hdr FROM @( VALUE #(
equipment_id = `6009223606314` category = `Fridge`
description = `Walk-in dairy fridge`
location = `Store 12 Sea Point` manufacturer = `Carrier` status = `ACTIVE`
latitude = `-33.9210000` longitude = `18.3820000` ) ).
INSERT zequip_hdr FROM @( VALUE #(
equipment_id = `6009223606321` category = `Fridge`
description = `Open-front produce chiller`
location = `Store 12 Sea Point` manufacturer = `Hussmann` status = `ACTIVE`
latitude = `-33.9212000` longitude = `18.3824000` ) ).
INSERT zequip_hdr FROM @( VALUE #(
equipment_id = `6009223606338` category = `Fridge`
description = `Frozen aisle cabinet`
location = `Store 8 Canal Walk` manufacturer = `Carrier` status = `ACTIVE`
latitude = `-33.8928000` longitude = `18.5110000` ) ).
INSERT zequip_hdr FROM @( VALUE #(
equipment_id = `6009223606345` category = `Bakery`
description = `Deck bakery oven`
location = `In-store bakery Claremont` manufacturer = `Wiesheu` status = `ACTIVE`
latitude = `-33.9805000` longitude = `18.4658000` ) ).
INSERT zequip_hdr FROM @( VALUE #(
equipment_id = `6009223606352` category = `Bakery`
description = `Spiral dough mixer`
location = `In-store bakery Claremont` manufacturer = `Hobart` status = `STANDBY`
latitude = `-33.9806000` longitude = `18.4660000` ) ).
COMMIT WORK AND WAIT.
ENDMETHOD.
METHOD lookup_equipment.
CLEAR: mv_found, mv_equnr, mv_descr, mv_category, mv_location, mv_maker, mv_status, mt_issue.
DATA(lv_id) = condense( mv_scan_input ).
IF lv_id IS INITIAL.
client->message_box_display( text = `Scan a barcode`
type = `warning` ).
RETURN.
ENDIF.
SELECT SINGLE equipment_id, description, category, location, manufacturer, status
FROM zequip_hdr
WHERE equipment_id = @lv_id
INTO ( @DATA(lv_eq), @DATA(lv_descr), @DATA(lv_cat), @DATA(lv_loc),
@DATA(lv_maker), @DATA(lv_stat) ).
IF sy-subrc <> 0.
client->message_box_display( text = |No equipment found for { lv_id }|
type = `error` ).
RETURN.
ENDIF.
mv_found = abap_true.
mv_equnr = lv_eq.
mv_descr = lv_descr.
mv_category = lv_cat.
mv_location = lv_loc.
mv_maker = lv_maker.
mv_status = lv_stat.
load_issues( ).
mv_page = `DETAIL`.
ENDMETHOD.
METHOD go_back.
mv_page = `SCAN`.
CLEAR: mv_scan_input, mv_found, mv_equnr, mv_descr, mv_category,
mv_location, mv_maker, mv_status, mv_problem, mv_photo,
mv_file, mv_filepath, mt_issue.
ENDMETHOD.
METHOD load_issues.
CLEAR mt_issue.
SELECT issue_id, problem_text, severity, created_on, created_by, has_photo
FROM zequip_issue
WHERE equipment_id = @mv_equnr
ORDER BY issue_id DESCENDING
INTO TABLE @DATA(lt_db)
UP TO 20 ROWS.
LOOP AT lt_db INTO DATA(ls_db).
APPEND VALUE #(
issue_id = |{ ls_db-issue_id ALPHA = OUT }|
problem_text = ls_db-problem_text
severity = ls_db-severity
created_on = |{ ls_db-created_on DATE = USER }|
created_by = ls_db-created_by
photo_label = COND #( WHEN ls_db-has_photo = abap_true THEN `Photo` ELSE `` )
) TO mt_issue.
ENDLOOP.
ENDMETHOD.
METHOD log_problem.
IF mv_found = abap_false.
client->message_box_display( text = `Scan equipment first`
type = `warning` ).
RETURN.
ENDIF.
IF mv_problem IS INITIAL.
client->message_box_display( text = `Enter a problem description`
type = `warning` ).
RETURN.
ENDIF.
SELECT MAX( issue_id )
FROM zequip_issue
INTO @DATA(lv_max).
DATA(ls_issue) = VALUE zequip_issue(
issue_id = lv_max + 1
equipment_id = mv_equnr
problem_text = mv_problem
severity = mv_severity
created_on = cl_abap_context_info=>get_system_date( )
created_at = cl_abap_context_info=>get_system_time( )
created_by = cl_abap_context_info=>get_user_technical_name( )
photo = mv_photo
has_photo = xsdbool( mv_photo IS NOT INITIAL ) ).
INSERT zequip_issue FROM @ls_issue.
IF sy-subrc <> 0.
client->message_box_display( text = `Could not save the problem`
type = `error` ).
RETURN.
ENDIF.
COMMIT WORK AND WAIT.
CLEAR: mv_problem, mv_photo, mv_file, mv_filepath.
load_issues( ).
client->message_box_display( text = `Problem logged`
type = `success` ).
ENDMETHOD.
METHOD view_display.
IF mv_page = `DETAIL` AND mv_found = abap_true.
view_detail( ).
ELSE.
view_scan( ).
ENDIF.
ENDMETHOD.
METHOD view_scan.
DATA(view) = z2ui5_cl_ui5_view_builder=>factory(
)->ele( n = `View` ns = `mvc`
)->a( n = `displayBlock` v = `true`
)->a( n = `height` v = `100%`
)->a( n = `xmlns` v = `sap.m`
)->a( n = `xmlns:mvc` v = `sap.ui.core.mvc`
" sap.ndc is SAPUI5-only, which is fine here - the bootstrap is ui5.sap.com.
)->a( n = `xmlns:ndc` v = `sap.ndc` ).
DATA(lo_page) = view->ele( `Shell`
)->ele( `Page`
)->a( n = `title` v = `Equipment log`
)->a( n = `showNavButton` b = client->check_app_prev_stack( )
)->a( n = `navButtonPress` v = client->_event_nav_app_leave( ) ).
DATA(lo_box) = lo_page->ele( `VBox`
)->a( n = `class` v = `sapUiSmallMargin` ).
" Scan sits ON the field, not under it: one row, field flexes, camera button
" where the (dead) value-help icon used to be. That is the shape a phone user
" expects, and it keeps the two ways of filling the same field together.
DATA(lo_row) = lo_box->ele( `HBox`
)->a( n = `width` v = `100%`
)->a( n = `alignItems` v = `Center` ).
" A hardware scanner (Zebra keyboard wedge) types into this field and sends
" Enter, which is what submit handles.
DATA(lo_input) = lo_row->ele( `Input`
)->a( n = `id` v = `eanInput`
)->a( n = `value` v = client->_bind( mv_scan_input )
)->a( n = `placeholder` v = `Scan equipment barcode`
)->a( n = `submit` v = client->_event( `LOOKUP` ) ).
" growFactor + baseSize 0 so the field takes the row and the button stays square.
lo_input->ele( `layoutData`
)->tag( `FlexItemData`
)->a( n = `growFactor` v = `1`
)->a( n = `baseSize` v = `0%` ).
" Opens the camera in the frontend and only calls the backend once it has a
" barcode, so scanning costs no round trip until there is something to look up.
lo_row->tag( n = `BarcodeScannerButton` ns = `ndc`
)->a( n = `width` v = `3rem`
)->a( n = `class` v = `sapUiTinyMarginBegin`
)->a( n = `dialogTitle` v = `Scan equipment barcode`
)->a( n = `scanSuccess` v = client->_event( val = `SCANNED`
t_arg = VALUE #( ( `${$parameters>/text}` ) ) ) ).
lo_box->tag( `Button`
)->a( n = `text` v = `Look up`
)->a( n = `type` v = `Emphasized`
)->a( n = `width` v = `100%`
)->a( n = `class` v = `sapUiSmallMarginTop`
)->a( n = `press` v = client->_event( `LOOKUP` ) ).
client->view_display( view->stringify( ) ).
client->follow_up_action( val = client->cs_event-set_focus
t_arg = VALUE #( ( `eanInput` ) ) ).
ENDMETHOD.
METHOD view_detail.
DATA(view) = z2ui5_cl_ui5_view_builder=>factory(
)->ele( n = `View` ns = `mvc`
)->a( n = `displayBlock` v = `true`
)->a( n = `height` v = `100%`
)->a( n = `xmlns` v = `sap.m`
)->a( n = `xmlns:mvc` v = `sap.ui.core.mvc`
)->a( n = `xmlns:core` v = `sap.ui.core`
)->a( n = `xmlns:z2ui5` v = `z2ui5.cc` ).
DATA(lo_page) = view->ele( `Shell`
)->ele( `Page`
)->a( n = `title` v = `Log a problem`
)->a( n = `showNavButton` b = abap_true
)->a( n = `navButtonPress` v = client->_event( `BACK` ) ).
lo_page->ele( `ObjectHeader`
)->a( n = `title` v = client->_bind( mv_descr )
)->a( n = `number` v = client->_bind( mv_equnr )
)->a( n = `intro` v = client->_bind( mv_location )
)->a( n = `responsive` b = abap_true
)->a( n = `backgroundDesign` v = `Translucent`
)->a( n = `condensed` b = abap_true
)->tag( `ObjectAttribute`
)->a( n = `title` v = `Type`
)->a( n = `text` v = client->_bind( mv_category )
)->tag( `ObjectAttribute`
)->a( n = `title` v = `Manufacturer`
)->a( n = `text` v = client->_bind( mv_maker )
)->ele( `statuses`
)->tag( `ObjectStatus`
)->a( n = `title` v = `Status`
)->a( n = `text` v = client->_bind( mv_status )
)->a( n = `state` v = `Information` ).
DATA(lo_box) = lo_page->ele( `VBox`
)->a( n = `class` v = `sapUiSmallMargin` ).
lo_box->tag( `Label`
)->a( n = `text` v = `Severity` ).
lo_box->ele( `ComboBox`
)->a( n = `selectedKey` v = client->_bind( mv_severity )
)->a( n = `width` v = `100%`
)->a( n = `items` v = |\{path:'{ client->_bind( val = mt_severity path = abap_true ) }'\}|
)->tag( n = `Item` ns = `core`
)->a( n = `key` v = `{KEY}`
)->a( n = `text` v = `{TEXT}` ).
lo_box->tag( `Label`
)->a( n = `text` v = `Problem`
)->a( n = `class` v = `sapUiTinyMarginTop` ).
lo_box->tag( `TextArea`
)->a( n = `value` v = client->_bind( mv_problem )
)->a( n = `rows` v = `3`
)->a( n = `width` v = `100%`
)->a( n = `growing` b = abap_true
)->a( n = `placeholder` v = `What is wrong` ).
lo_box->tag( `Label`
)->a( n = `text` v = `Photo`
)->a( n = `class` v = `sapUiTinyMarginTop` ).
lo_box->tag( n = `FileUploader` ns = `z2ui5`
)->a( n = `value` v = client->_bind( mv_file )
)->a( n = `path` v = client->_bind( mv_filepath )
)->a( n = `upload` v = client->_event( `PHOTO` )
)->a( n = `checkDirectUpload` b = abap_true
)->a( n = `fileType` v = `jpg,jpeg,png`
)->a( n = `icon` v = `sap-icon://camera`
)->a( n = `buttonText` v = `Add photo` ).
IF mv_photo IS NOT INITIAL.
lo_box->tag( `Image`
)->a( n = `src` v = client->_bind( mv_photo )
)->a( n = `width` v = `100%`
)->a( n = `height` v = `160px`
)->a( n = `class` v = `sapUiTinyMarginTop` ).
lo_box->tag( `Button`
)->a( n = `text` v = `Remove photo`
)->a( n = `type` v = `Transparent`
)->a( n = `press` v = client->_event( `PHOTO_CLEAR` ) ).
ENDIF.
lo_box->tag( `Button`
)->a( n = `text` v = `Log problem`
)->a( n = `type` v = `Emphasized`
)->a( n = `width` v = `100%`
)->a( n = `class` v = `sapUiTinyMarginTop`
)->a( n = `press` v = client->_event( `LOG` ) ).
lo_page->ele( `List`
)->a( n = `headerText` v = `Recent logs`
)->a( n = `items` v = client->_bind( mt_issue )
)->ele( `ObjectListItem`
)->a( n = `title` v = `{PROBLEM_TEXT}`
)->a( n = `number` v = `{SEVERITY}`
)->a( n = `intro` v = `{CREATED_ON}`
)->tag( `ObjectAttribute`
)->a( n = `text` v = `{CREATED_BY}`
)->tag( `ObjectAttribute`
)->a( n = `text` v = `{PHOTO_LABEL}` ).
client->view_display( view->stringify( ) ).
ENDMETHOD.
ENDCLASS.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment