# Integrate Calculator Service

## **Overview**

#### **Calculation**

The calculation class contains the calculation logic for the birth certificate registration charges. This can vary from city to city. Based on the application submitted, the calculator class will calculate the tax/charges and call the billing service to generate the demand.

{% hint style="info" %}
**What is a demand?**

A demand is the official communication sent by a government authority to a citizen requesting them to pay for a service. A demand leads to a bill. When a bill is paid, a receipt is generated. A demand can be modified prior to bill generation.
{% endhint %}

## Steps

For our guide, we are going to create a Calculation Service that will call the calculator to generate a demand. Follow the steps below -

1. Create a class under `service` folder by the name of `CalculationService`
2. Annotate this class with @Service annotation and add the following logic within it -

```java
package digit.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import digit.config.BTRConfiguration;
import digit.repository.ServiceRequestRepository;
import digit.web.models.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CalculationService {

    @Autowired
    private BTRConfiguration btrConfiguration;

    @Autowired
    private ObjectMapper mapper;

    @Autowired
    private ServiceRequestRepository serviceRequestRepository;

    public CalculationRes getCalculation(BirthRegistrationRequest request){

        List<CalculationCriteria> calculationCriteriaList = new ArrayList<>();
        for(BirthRegistrationApplication application : request.getBirthRegistrationApplications()) {
            CalculationCriteria calculationCriteria = CalculationCriteria.builder()
                    .birthregistrationapplication(application)
                    .tenantId(application.getTenantId())
                    .applicationNumber(application.getApplicationNumber())
                    .build();
            calculationCriteriaList.add(calculationCriteria);
        }

        CalculationReq calculationReq = CalculationReq.builder()
                .requestInfo(request.getRequestInfo())
                .calculationCriteria(calculationCriteriaList)
                .build();

        StringBuilder url = new StringBuilder().append(btrConfiguration.getBtrCalculatorHost())
                .append(btrConfiguration.getBtrCalculatorCalculateEndpoint());

        Object response = serviceRequestRepository.fetchResult(url, calculationReq);
        CalculationRes calculationRes = mapper.convertValue(response, CalculationRes.class);

        return calculationRes;
    }
}

```


---

# 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.digit.org/platform/guides/developer-guide/backend-developer-guide/section-4-integrate-billing-and-payment/integrate-calculator-service.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.
