# Summary Price per Order Item

In the Order Management App, each **Order Request Item** may have multiple **Supplier records**.\
The system must automatically **sum the Total Price of all suppliers** and display the value in the **Summary Price** field of the Order Request Item.

This section explains how to calculate and update Summary Price by combining:

* **Ajax‑based Java logic (server side)**, and
* **Custom JavaScript (client side)**

The calculation is triggered dynamically whenever supplier data is saved.

***

### Requirement

The system must calculate:

```
Summary Price = Sum of TOTAL_PRICE from all Supplier records
```

and update the **Summary Price** field of the corresponding Order Request Item automatically.

***

### Step 1: Create Ajax Java Class (Server Side)

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

#### Class Name

```
com.manual.doc.order.ajax.CalculateOrderRequestItemSummaryPrice
```

#### Java Code

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

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

import javax.servlet.http.HttpServletRequest;

import org.jboss.logging.Logger;
import org.json.JSONObject;

import com.master.form.MasterFormHandler;
import com.master.util.EAFManualUtil;
import com.master.util.ManualInterface;

public class CalculateOrderRequestItemSummaryPrice implements ManualInterface {

    static Logger logger =
            Logger.getLogger(CalculateOrderRequestItemSummaryPrice.class);

    HttpServletRequest request;

    @Override
    public String processManual() {
        try {

            MasterFormHandler formData =
                (MasterFormHandler) request.getSession()
                    .getAttribute("MD8321225761_session");

            Vector vForm = formData.getStoreActionList();
            logger.debug("##### vForm ##### " + vForm);

            double sumPrice = 0;

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

                HashMap hData =
                    EAFManualUtil.getDataHashMapFromSession(
                        "MD8321225761", request, i);

                String totalPrice =
                    (String) hData.get("TOTAL_PRICE");

                if (totalPrice != null && !"".equals(totalPrice)) {
                    sumPrice += Double.parseDouble(totalPrice);
                }
            }

            JSONObject json = new JSONObject();
            json.put("SUM_PRICE", sumPrice);

            return json.toString();

        } catch (Exception e) {
            e.printStackTrace();
            return "error|" + e.getMessage();
        }
    }

    @Override
    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }
}
```

This class:

* Reads all Supplier records from session
* Sums `TOTAL_PRICE` values
* Returns the result as JSON (`SUM_PRICE`)

***

#### Step 1.1: Replace Module Session ID

Replace:

```
MD8321225761_session
```

with the **actual Module ID** of module **Supplier** in entity:

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

How to find Module ID:

1. Open entity **Doc Order Request Item - Back Office**
2. Click **Edit** on module **Supplier**
3. Copy the value from **Module ID**
4. Replace hard‑coded value with:

```
<ModuleID>_session
```

***

### Step 2: Add JavaScript for Ajax Call (Client Side)

Open the following JavaScript file in the ONEWEB workspace:

```
/MasterWeb/WebContent/manual/js/doc/order/
order_request_item_back_office_update_mode.js
```

Add the following function.

#### JavaScript Code

```
function MD8321225761manualJS() {

    var uri =
        "/MasterWeb/ManualServlet?className=" +
        "com.manual.doc.order.ajax.CalculateOrderRequestItemSummaryPrice";

    jQuery.ajax({
        type: "POST",
        url: uri,
        async: false,
        success: function (data) {

            var obj = JSON.parse(data);

            $('#MD9443222701_SUMMARY_PRICE_InputField ' +
              '[name="SUMMARY_PRICE"]')
              .val(moneyFormat(obj.SUM_PRICE));
        }
    });
}
```

***

#### Step 2.1: Replace Function Name

Replace:

```
MD8321225761manualJS
```

with the **Supplier module ID** found in Step 1.1.

***

#### Step 2.2–2.3: Update Summary Price Field ID

1. Open entity **Doc Order Request Item - Back Office**
2. Edit module **Order Item**
3. Find **Module ID** of Order Item module
4. Replace:

```
MD9443222701
```

with the **actual Order Item Module ID** in the JavaScript selector.

This ensures the script updates the correct **Summary Price** field.

***

### Step 3: Deploy Updates

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

Both of the following will be updated:

* Java class

  ```
  com.manual.doc.order.ajax.CalculateOrderRequestItemSummaryPrice
  ```
* JavaScript file

  ```
  order_request_item_back_office_update_mode.js
  ```

***

### Step 4: Test the Configuration

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

#### Expected Result

* The system automatically calculates Total Price from all suppliers
* **Summary Price** field of Order Request Item is updated immediately

***

### Summary

In this section, you have:

* Implemented server‑side Ajax logic to summarize supplier prices
* Retrieved one‑to‑many data from session
* Implemented client‑side JavaScript to update UI fields dynamically
* Integrated Ajax calculation with Back Office workflow

This customization ensures **accurate real‑time price aggregation** and demonstrates how **Ajax‑based Customization** can enhance complex data processing 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/oneweb-platform-th/tutorials-examples/order-management-app-web/app-designer-customize/summary-price-per-order-item.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.
