## Prerequisites

First run this block to configure your server.

Python

```
import os
from dotenv import load_dotenv
import mindbridgeapi as mbapi

load_dotenv(override=True)
token = os.getenv("MINDBRIDGE_API_TOKEN")
url = os.getenv("MINDBRIDGE_URL")
server = mbapi.Server(url=url, token=token)
user = server.users.get_current()
```

## Creating a General Ledger Analysis

To setup an Analysis we first need an Organization and an Engagement. If no `library_id` is included in the `EngagementItem` the MindBridge SDK will create one using the `MindBridge for-profit (Mac v2) Library`. This Library includes the General ledger, Accounts payable and Accounts receivable analysis types by default.

Python

```
# get-or-create our organization
organization_name = "Chapter 3 - Creating an Analysis"
try:
    organization = next(server.organizations.get({"name": organization_name}))
    print(f"Organization '{organization_name}' already exists.")
except StopIteration:
    new_organization_item = mbapi.OrganizationItem(
        name=organization_name,
        external_client_code="My Client ID",  # Optional
        manager_user_ids=[user.id],  # Optional
    )
    organization_item = mbapi.OrganizationItem(name=organization_name)
    organization = server.organizations.create(organization_item)
    print(f"Organization '{organization_name}' created.")

engagement_name = "Engagement - MindBridge for-profit Library"
try:
    new_engagement_item = mbapi.EngagementItem(
        organization_id=organization.id,
        name=engagement_name,
        engagement_lead_id=user.id,
    )
    engagement = server.engagements.create(new_engagement_item)
    print(f"Created the new engagement {engagement.name}")
except mbapi.exceptions.ValidationError:
    engagement = next(
        server.engagements.get(
            {"organizationId": organization.id, "name": engagement_name}
        )
    )
    print(
        f"Fetched the engagement '{engagement.name}' from the organization with id "
        f"{organization.id}"
    )
```

Now that the Engagement “Engagement - MindBridge for-profit Library” is created, we can create an analysis within it.

Python

```
from datetime import date

new_gl_analysis_item = mbapi.AnalysisItem(
    name="GL Analysis",
    engagement_id=engagement.id,
    currency_code="CAD",
    analysis_periods=[\
        mbapi.AnalysisPeriod(end_date=date(2024, 12, 31), start_date=date(2024, 1, 1))\\
    ],
    analysis_type_id=mbapi.AnalysisTypeItem.GENERAL_LEDGER,
)

analysis = server.analyses.create(new_gl_analysis_item)

print(
    f"https://{os.getenv('MINDBRIDGE_URL')}/app/organization/{organization.id}/"
    f"engagement/{engagement.id}/analysis-list"
)
```

At the above link, you should have an analysis ready for you to upload data.

## Creating a Custom Analysis

To create a Custom Analysis we first need a Library with our desired Analysis Type. Then we’ll create an Engagement that uses that Library.In this example we will setup a TRA Vendor Analysis by creating an Analysis Type using the `TRA Vendor Template` provided by MindBridge.First create a TRA Vendor Analysis Type within the Analysis Designer:

- Go to the Analysis Designer
- Find the Analysis Type named “TRA Vendor Template”
- Duplicate the TRA Vendor Template and set the name to “SDK TRA Vendor”
- Click “Save”
- Go back to the Analysis Designer page
- Select “Publish” on the “SDK TRA Vendor”

Then, create a Library with that Analysis Type

- Go to the Libraries page
- Select “Create Library”
- Set the name to “SDK TRA Vendor Library”
- Set the base library to “MindBridge for-profit (MAC v.2)”
- Add “SDK TRA Vendor” to the Analysis Types
- Set the Account grouping to Mac v.2
- Select “Create Library” You should now have a Library called “SDK TRA Vendor Library” which contains the Analysis Type “SDK TRA Vendor”

Now we can create an Engagement that uses this library by providing the `library_id` in the `EngagementItem`.

Python

```
all_libraries = server.libraries.get()
vendor_library = next(
    library for library in all_libraries if library.name == "SDK TRA Vendor Library"
)

organization_name = "Chapter 3 - Creating an Analysis"
try:
    organization = next(server.organizations.get({"name": organization_name}))
    print(f"Organization '{organization_name}' already exists.")
except StopIteration:
    new_organization_item = mbapi.OrganizationItem(
        name=organization_name,
        external_client_code="My Client ID",  # Optional
        manager_user_ids=[user.id],  # Optional
    )
    organization_item = mbapi.OrganizationItem(name=organization_name)
    organization = server.organizations.create(organization_item)
    print(f"Organization '{organization_name}' created.")

engagement_name = "Engagement - SDK TRA Vendor Library"
try:
    new_engagement_item = mbapi.EngagementItem(
        organization_id=organization.id,
        name=engagement_name,
        engagement_lead_id=user.id,
        library_id=vendor_library.id,
    )
    engagement = server.engagements.create(new_engagement_item)
    print(f"Created the new engagement '{engagement.name}'")
except mbapi.exceptions.ValidationError:
    engagement = next(
        server.engagements.get(
            {"organizationId": organization.id, "name": engagement_name}
        )
    )
    print(
        f"Fetched the engagement '{engagement.name}' from the organization with id "
        f"{organization.id}"
    )
```

Now that the Engagement “Engagement - SDK TRA Vendor Library” is created, we can create an Analysis within it. Note that we need to fetch the Analysis Type from the Engagement

Python

```
from datetime import date

all_libraries = server.libraries.get()
vendor_library = next(
    library for library in all_libraries if library.name == "SDK TRA Vendor Library"
)
available_analysis_types = list(vendor_library.analysis_types)

print(f"These are the Analysis Types available in the library '{vendor_library.name}'")
for at in available_analysis_types:
    print(at.name)

tra_vendor_analysis_type = next(
    analysis_type
    for analysis_type in available_analysis_types
    if analysis_type.name == "SDK TRA Vendor"
)

new_gl_analysis_item = mbapi.AnalysisItem(
    name="GL Analysis",
    engagement_id=engagement.id,
    currency_code="CAD",
    analysis_periods=[\
        mbapi.AnalysisPeriod(end_date=date(2024, 12, 31), start_date=date(2024, 1, 1))\
    ],
    analysis_type_id=tra_vendor_analysis_type.id,
)

analysis = server.analyses.create(new_gl_analysis_item)

print(
    f"https://{os.getenv('MINDBRIDGE_URL')}/app/organization/{organization.id}/"
    f"engagement/{engagement.id}/analysis-list"
)
```
