# Calculate Remain Order Unit

In the Order Management App, Back Office users allocate order quantities across multiple suppliers.\
To ensure accuracy, the system must **automatically calculate the remaining order quantity** after each supplier’s allocation.

This section explains how to implement **automatic calculation of the Remain Order Unit** using **Java customization** and integrate it with the **Back Office Order Request Item screen**.

***

### Objective

By completing this section, you will learn how to:

* Calculate remaining order quantity dynamically
* Access parent and sibling module data in Java customization
* Implement calculation logic for one‑to‑many records
* Integrate custom Java logic using Module Action in App Designer

***

### Step 1: Create Java Calculation Class

From the **ONEWEB workspace**, create a new Java class in **MasterWeb** with the following details.

**Class Name**

```
com.manual.doc.order.java.OrderRequestItemBackOfficeUpdateMode
```

**Java Code**

```
package com.manual.doc.order.java;

import java.util.HashMap;
import java.util.Vector;

import org.apache.log4j.Logger;

import com.master.form.MasterFormHandler;
import com.master.util.ProcessAction;
import com.master.util.ProcessHelper;

public class OrderRequestItemBackOfficeUpdateMode extends ProcessHelper implements ProcessAction {

    private static Logger logger =
            Logger.getLogger(OrderRequestItemBackOfficeUpdateMode.class);

    @Override
    public void modifyInsertMany(
            String moduleID,
            HashMap hStoreHashMap,
            String processMode,
            int row) {

        logger.debug("hStoreHashMap=" + hStoreHashMap);
        logger.debug("row=" + row);

        // Get Order Item form data (parent)
        MasterFormHandler orderRequestFormData =
                (MasterFormHandler) request.getSession()
                        .getAttribute("MD9443222701_session");

        HashMap orderRequestFormHash =
                orderRequestFormData.getStoreHashMap();

        logger.debug("orderRequestFormHash=" + orderRequestFormHash);

        HashMap orderRequestHash =
                (HashMap) orderRequestFormHash.get("WF_ORDER_REQ_ITEM");

        String orderUnit =
                (String) orderRequestHash.get("UNIT");

        logger.debug("orderUnit=" + orderUnit);

        int orderUnitAmt = Integer.parseInt(orderUnit);

        // Get Supplier module data
        MasterFormHandler supplierFormData =
                (MasterFormHandler) request.getSession()
                        .getAttribute(moduleID + "_session");

        Vector supplierFormList =
                supplierFormData.getStoreActionList();

        // Subtract selected units from other supplier rows
        if (supplierFormList != null && supplierFormList.size() > 0) {

                for (int i = 0; i < supplierFormList.size(); i++) {

                        if (i != row) {

                                HashMap supplierFormHash =
                                        (HashMap) supplierFormList.get(i);

                                HashMap updateSupplierHash =
                                        (HashMap) supplierFormHash.get("UPDATE");

                                HashMap supplierHash =
                                        (HashMap) updateSupplierHash
                                                .get("WF_ORDER_REQ_ITEM_SUPPLIER");

                                String unitSelected =
                                        (String) supplierHash.get("UNIT_BY_BACK_OFFICE");

                                logger.debug("unitSelected=" + unitSelected);

                                if (unitSelected != null &&
                                        !"".equals(unitSelected)) {

                                        orderUnitAmt -=
                                                Integer.parseInt(unitSelected);
                                }
                        }
                }
        }

        logger.debug("orderUnitAmt after calculate=" + orderUnitAmt);

        // Set remaining order unit
        HashMap supplierHash =
                (HashMap) hStoreHashMap
                        .get("WF_ORDER_REQ_ITEM_SUPPLIER");

        supplierHash.put(
                "REMAIN_ORDER_UNIT",
                String.valueOf(orderUnitAmt)
        );

        super.modifyInsertMany(
                moduleID, hStoreHashMap, processMode, row);
    }
}

```

This logic calculates the **remaining order quantity** by subtracting previously allocated units from the total order unit.

***

### Step 2: Update Module Session ID

In the code above, replace:

```
MD9443222701_session
```

with the **actual Module ID** of the **Order Item** module in entity:

```
Doc Order Request Item - Back Office
```

#### How to Find the Module ID

1. Open entity **Doc Order Request Item - Back Office** in App Designer
2. Click **Edit** on module **Order Item**
3. Locate the **Module ID** field
4. Replace the hard‑coded value in Java code with:

```
<ModuleID>_session
```

This ensures the calculation uses the correct parent module.

***

### Step 3: Deploy Java Class

1. From the **ONEWEB workspace**, export `EafMasterEar.ear`
2. Deploy the updated EAR file to the server

The class

```
com.manual.doc.order.java.OrderRequestItemBackOfficeUpdateMode
```

will now be available on the server.

***

### Step 4: Configure Module Action in App Designer

1. Open **App Designer**
2. Edit entity:

   ```
   Doc Order Request Item - Back Office
   ```
3. Under the **Supplier** module (Supplier List tab):
   * From **Tools > Action**, drag **Module Action**
4. The action name appears (default is SEARCH)
5. Click the **pencil icon** to edit

In **Action Field Configuration**:

* **Process Name**: `UPDATE`
* **Class Action**:

  ```
  com.manual.doc.order.java.OrderRequestItemBackOfficeUpdateMode
  ```
* Click **OK**

***

### Step 5: Save the Entity

Click **Save this Entity** to apply the configuration.

***

### Step 6: Test the Calculation

1. Log in to **FrontWeb** as a **Back Office user**
2. Go to **To Do List** and claim a job
3. Open **Order Item**
4. Click **Edit Supplier**
5. Enter values for **Order Unit per Supplier**

#### Expected Result

* The system automatically calculates **Remain Order Unit**
* Remaining quantity updates correctly based on allocations across suppliers

***

### Summary

In this section, you have:

* Implemented dynamic calculation of remaining order quantity
* Accessed and processed one‑to‑many data in Java customization
* Integrated calculation logic using Module Action
* Ensured accurate allocation of order quantities at Back Office stage

This customization is essential for **multi‑supplier order handling** and demonstrates how **App Designer Customize** can support complex business calculations in the **Order Management App (Web)**.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.onewebstack.com/tutorials-examples/order-management-app-web/app-designer-customize/calculate-remain-order-unit.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
