# Calculating a 3D Load Plan

## Objective

Pack units into trailers, containers or your own load spaces from a single request. Nothing has to exist in Transportial, and nothing is stored.

## Before you start

Create an API access integration that holds the `calculate:loadPlan` permission. Send its key and secret with every call.

## Scenario

12 EUR pallets for the first stop and 8 non-stackable drums for the second, in a mega trailer.

## Step 1: Describe the load

Save this as `request.json`. All sizes are whole millimetres, and all weights are grams.

```json
{
  "equipment": [{ "code": "EU_TRAILER_MEGA" }],
  "items": [
    { "id": "eur-pallets", "quantity": 12, "lengthMm": 1200, "widthMm": 800, "heightMm": 1500, "weightG": 450000, "unloadStop": 1 },
    { "id": "drums", "quantity": 8, "lengthMm": 600, "widthMm": 600, "heightMm": 900, "weightG": 200000, "stackable": false, "unloadStop": 2 }
  ],
  "options": { "timeBudgetMs": 500 }
}
```

`GET /load-plan/equipment` lists the catalogue codes. To use your own load space, send an `id` and `lengthMm`, `widthMm` and `heightMm` instead of a `code`. To use one code twice, give each entry its own `id`.

## Step 2: Calculate

```bash
curl -X POST https://api.transportial.com/api/load-plan/calculate \
  -H "x-api-key: $TRANSPORTIAL_KEY" \
  -H "x-api-secret: $TRANSPORTIAL_SECRET" \
  -H "Content-Type: application/json" \
  -d @request.json
```

## Step 3: Read the result

```json
{
  "success": true,
  "result": {
    "status": "FITS",
    "equipment": [
      {
        "equipmentId": "EU_TRAILER_MEGA",
        "weightG": 7000000,
        "volumePercent": 19.64,
        "floorPercent": 25.62,
        "loadingMeters": 3.6,
        "axleLoads": [
          { "support": "FRONT", "loadG": 8651283, "maxG": 12000000 },
          { "support": "REAR", "loadG": 5348717, "maxG": 24000000 }
        ],
        "placements": [
          { "itemId": "drums", "unit": 0, "equipmentId": "EU_TRAILER_MEGA", "xMm": 0, "yMm": 0, "zMm": 0, "lengthMm": 600, "widthMm": 600, "heightMm": 900, "tipped": false, "sequence": 0, "loadStop": 0, "unloadStop": 2 }
        ]
      }
    ],
    "unplaced": []
  }
}
```

The response above is trimmed to the first of 20 placements. Coordinates are the unit's minimum corner: `xMm` from the front wall towards the door, `yMm` from the left wall, `zMm` up from the floor.

| Status | Meaning |
|  --- | --- |
| `FITS` | Everything fits within every limit. |
| `TIGHT` | Everything fits, but a limit is above `tightThresholdPercent`. |
| `DOES_NOT_FIT` | At least one unit is listed in `unplaced`, with its reason. |
| `UNVERIFIED` | Nothing could be checked, because goods or load space lack dimensions. |


## Compartments

A multi-temperature reefer has a frozen front and a chilled rear, separated by a bulkhead. Give the load space its `compartments`, counted from the front wall, and give each goods line its temperature and foodstuff status:

```json
{
  "equipment": [
    {
      "id": "reefer-2",
      "lengthMm": 13300,
      "widthMm": 2460,
      "heightMm": 2600,
      "payloadG": 24000000,
      "temperature": { "minC": -25, "maxC": 12 },
      "compartments": [
        {
          "id": "front", "name": "Frozen", "sequenceNr": 1, "lengthMm": 7300,
          "capacity": { "weightKg": 14000 },
          "temperature": { "minC": -25, "maxC": -18 },
          "foodGrade": true
        },
        { "id": "rear", "name": "Chilled", "sequenceNr": 2, "temperature": { "minC": 2, "maxC": 6 }, "foodGrade": true }
      ]
    }
  ],
  "items": [
    {
      "id": "yoghurt", "quantity": 10, "lengthMm": 1200, "widthMm": 800, "heightMm": 1600, "weightG": 600000,
      "foodstuff": true, "requiredTemperature": { "minC": 2, "maxC": 6 }, "unloadStop": 1
    },
    {
      "id": "ice-cream", "quantity": 14, "lengthMm": 1200, "widthMm": 800, "heightMm": 1600, "weightG": 600000,
      "foodstuff": true, "requiredTemperature": { "minC": -25, "maxC": -18 }, "unloadStop": 2
    }
  ]
}
```

On the load space:

| Field | Meaning |
|  --- | --- |
| `compartments[].id`, `name`, `sequenceNr` | The compartment, and its position from the front wall, starting at 1. |
| `compartments[].lengthMm` | Its length. Empty shares what the other compartments leave; the last one reaches the doors. The stated lengths must fit in the load space. |
| `compartments[].capacity.weightKg` | The compartment's payload, when lower than the load space's `payloadG`. |
| `compartments[].temperature` | The range it holds. Empty takes the load space's `temperature`. |
| `compartments[].adrAllowed`, `adrClasses`, `foodGrade` | Whether dangerous goods may go in and which classes; `foodGrade: true` takes no dangerous goods, `false` takes no foodstuffs. |


On the goods, `requiredTemperature`, `adrClasses` and `foodstuff` decide which compartment a unit may go into. Limited and excepted quantities are left out of `adrClasses`.

Each compartment is packed as its own space behind its bulkhead: a bulkhead is a wall that no unit crosses, and a unit only goes into a compartment that matches it. Axle loads span the whole load space, so they are not checked for a load space with compartments.

In the result, every placement says where it went in `placements[].compartmentId` (`null` in a load space without compartments). Each entry in `equipment` also lists its `compartments`, front to rear:

| Field | Meaning |
|  --- | --- |
| `id`, `name`, `sequenceNr` | The compartment. |
| `xStartMm`, `lengthMm` | Where it starts, in mm from the front wall, and how long it is: where the bulkheads stand. |
| `temperature`, `foodGrade`, `adrAllowed` | What it holds and accepts. |
| `weightG` | Weight loaded in it, at the busiest moment. |


## Options

| Option | Default | Meaning |
|  --- | --- | --- |
| `timeBudgetMs` | 1000 | Search time, at most 5000. With 0 you get the best greedy plan. |
| `minSupportPercent` | 75 | Part of a unit's base that must be supported. |
| `respectUnloadOrder` | true | Nothing for a later stop may block an earlier drop. |
| `respectAxleLoads` | true | Check axle loads when the load space has an axle model. |
| `tightThresholdPercent` | 90 | A limit used above this makes the plan `TIGHT`. |


## Limits

- At most 20 load spaces and 2,000 units.
- Compartment lengths are fixed: there is no movable bulkhead.
- Successful calculations are metered on your plan. Past its free allowance, a `402` asks you to agree to the price: repeat the call with `"hasAgreedToPrice": true`.
- `429` with `Retry-After` means all calculation slots are busy.