Creates a resource. The set of required fields depends on the calculation type.

```
v1/resources/add.php
```

## Parameters

| Field | Type | Required | Description |
|---|---|---|---|
| name | string | Yes | Name |
| type | string | Yes | Calculation type: "rate", "percent" or "amortized" |
| dynamic | int | No | 1: the price of a fixed rate or the percentage of a percentage resource is specified in the product components. Default 0. Not used for depreciation |
| unit | string | For rate and amortized | Unit of measure, a code from [Units of Measure](https://developers.controlata.com/hc/api-docs/articles/api-units-get-list). Not needed for a rate with dynamic 1 |
| price | float | For rate | Price per unit. Not needed for a rate with dynamic 1 |
| base | string | For percent | Calculation basis: "cost" or "price" |
| percent | float | For percent | Percentage. Not needed with dynamic 1 |
| initial_cost | float | For amortized | Initial cost |
| useful_life | float | For amortized | Useful life, in unit units |
| notes | string | No | Notes |
| categories | array | No | Categories: objects with id or name |

A category can be passed by name: if there is no such category, it is created. The IDs of existing categories are returned by [v1/categories/get_list](https://developers.controlata.com/hc/api-docs/articles/api-categories-get-list).

## Resource types

The calculation type determines which fields are needed when creating a resource.

| Type | Description | Required fields |
|---|---|---|
| "rate", Fixed rate | Labor, electricity, contract manufacturing | unit, price |
| "rate" with dynamic 1, Price specified in the product components | The amount is set in each product's components | None |
| "percent", Percentage | Overhead, commissions | base, percent |
| "percent" with dynamic 1, Percentage specified in the product components | The percentage is set in each product's components | base |
| "amortized", Depreciation | Equipment, vehicles, tools | unit, initial_cost, useful_life |

The calculation basis (base) of a percentage resource:

* "cost": the product cost without percentage resources;
* "price": the sale price from the product card.

## Units of measure

A resource can use any unit, including the time and energy units available to resources only: "h", "min", "kWh". See the full list in [Units of Measure](https://developers.controlata.com/hc/api-docs/articles/api-units-get-list).

## Example requests

A fixed rate: a carpenter's hourly rate.

```
{
    "name": "Carpenter's work",
    "type": "rate",
    "unit": "h",
    "price": 600,
    "categories": [{ "id": 31 }]
}
```

A percentage of cost, the same for all products:

```
{
    "name": "Overhead",
    "type": "percent",
    "base": "cost",
    "percent": 10
}
```

A marketplace commission whose percentage is set in each product's components:

```
{
    "name": "Marketplace commission",
    "type": "percent",
    "dynamic": 1,
    "base": "price"
}
```

Equipment depreciation:

```
{
    "name": "Milling machine",
    "type": "amortized",
    "unit": "h",
    "initial_cost": 600000,
    "useful_life": 2000
}
```

## Example response

```
{
    "success": true,
    "resource_id": 57
}
```

## Errors

| Error | Reason |
|---|---|
| No name in input | A required field is missing (instead of name, the field's name: type or a field required for the type) |
| Name must not be empty | Empty name |
| Name must be a string | Name passed not as a string, e.g. as an array |
| Invalid type value. Must be one of: rate, percent, amortized | Invalid type |
| Unit not set | An empty unit of measure was passed |
| Unit not found | Unknown unit of measure |
| Invalid price value. Must be a number, 0 or greater | Price is not a number or below 0 (same for initial_cost) |
| Invalid base value. Must be one of: cost, price | Invalid calculation basis |
| Percent must be greater than 0 and not exceed 100 | Percentage is 0, below 0 or above 100 |
| Useful life must be greater than 0 | Useful life is 0 or less |
| Category 12 not found | Category not found |
