# Create Print (Tryck) Product API (TSHIRTORDER-1653)

Creates a **print product** ("Tryck", `productKind = "tryck"`): a print (logo, text…) that is linked to normal
products (T-shirts, hoodies…) and is added automatically to an order when one of those products is ordered.
A print product can carry up to 4 prints, each on its own placement (Bröst vänster, Rygg…) with its own print file.

It is the same endpoint as [Create Product](create-product-api.md), with `productKind: "tryck"` and three extra
fields: `connectedProductIds`, `tryckPrints` and the forced defaults below.

| | |
|---|---|
| **Method / URL** | `POST /api/v1/products/` |
| **Auth** | `Authorization: Bearer <access token>` (32 chars, `[0-9a-z]`) |
| **Content-Type** | `application/json`, or `multipart/form-data` when uploading print / mockup files |
| **Controller** | `ProductController::add()` → `ProductService::add()` |

> Request data is the merge of JSON body, form body and query string.
> With multipart, send booleans as `"true"` / `"false"`.

## Before you start: placements

Every print needs a **placement id**. Placements are a shared list (not per channel) managed with
`/api/v1/tryck-placements` — see [Prints on products](prints-on-products-api.md#placements-api). Get the ids with:

```http
GET /api/v1/tryck-placements/?isActive=true
Authorization: Bearer <token>
```

## Authentication

Same as [Create Product](create-product-api.md#authentication): bad / expired token or a token flagged
`lockForOnlyIntegration` → `401 {"message": "Invalid token authorization"}`.

## Request body

### Required

| Field | Type | Notes |
|---|---|---|
| `productKind` | string | Must be exactly `"tryck"`. Omitted → a normal product is created (no auto-detection) |
| `name` | string | NotNull |
| `sku` | string | NotNull. Not checked for uniqueness |

### Usual fields for a print product

| Field | Type | Notes |
|---|---|---|
| `price` | float | Price of the print per shirt (ex. tax). `0` is allowed |
| `purchasePriceExcTax` | float | |
| `taxCode` | int | Percent, e.g. `25`. Defaults to `tax_default * 100` |
| `channelIds` / `channelId` | int[] / int | Channels the print product belongs to |
| `active` | bool | |
| `description`, `color` | string | `color` is free text for a print product |
| `brandId`, `categories`, `typeId` | | Same as [Create Product](create-product-api.md#classification) |
| `productionId`, `productions` | | The single print of the older Tryck flow (TSHIRTORDER-1628). Still accepted; use `tryckPrints` for new integrations |
| `removeFromGuestPortal` | bool | Defaults to **`true`** for a print product when omitted |

### `connectedProductIds` — which products get this print

| Field | Type | Notes |
|---|---|---|
| `connectedProductIds` | int[] | Ids of the normal products this print belongs to. When one of them is ordered, this print product is added to the order |

- Multipart: send as `connectedProductIds[]=123&connectedProductIds[]=456`. A JSON **string** is not decoded here and is ignored.
- Unknown / deleted ids are **silently skipped**. The kind of the target product is not checked — send only normal product ids.
- Ignored when `productKind` is not `"tryck"`.

### `tryckPrints` — the prints (max 4)

Array of prints, stored in the order sent.

| Field per print | Type | Required | Notes |
|---|---|---|---|
| `placementProductId` | int | ✅ | Id from `GET /tryck-placements/`. Must exist and be **active**. Each placement only **once** per print product |
| `printFile` | file \| string | ✅ | A file upload (multipart), or the path of an already stored file |
| `mockupFile` | file \| string | | Same as `printFile` |
| `name` | string | | Empty → `Tryck 1`, `Tryck 2`… (by position) |
| `note` | string | | Placement note ("Placeringsanteckning"), e.g. `8 cm under kragen` |
| `sortOrder` | int | | Defaults to the position in the array (1, 2, …) |

- `tryckPrints` can be omitted on create (print product without prints) and added later with
  [update](prints-on-products-api.md#add--change-prints-on-a-print-product).
- `tryckPrints` may be sent as a JSON string — then `printFile` / `mockupFile` must be existing paths.
- Uploaded files are stored under `products/tryck-prints/<date>-<random>/<original name>` (max ~100 MB each).
- Ignored when `productKind` is not `"tryck"`.

### Forced by the server

For a print product these values are always stored, whatever the payload says:

| Field | Value |
|---|---|
| `showOnEcom` | `false` |
| `manageStock`, `outStock`, `wooSyncStock` | `false` |
| `addOnProduct` | `false` |
| `noKickback` | `true` |
| `procentage`, `fixSum` | `0` |

So a print product is never shown on ecom, never out of stock, never has stock movements and never gives kickback.
`subProducts` (sizes) are not used for a print product.

## Example (multipart, with file upload)

```http
POST /api/v1/products/
Authorization: Bearer 0123456789abcdef0123456789abcdef
Content-Type: multipart/form-data; boundary=...

productKind=tryck
name=Logo ICA
sku=TRYCK-ICA-01
price=49
taxCode=25
channelIds[]=12
connectedProductIds[]=5001
connectedProductIds[]=5002
tryckPrints[0][placementProductId]=812
tryckPrints[0][printFile]=<file logo-front.pdf>
tryckPrints[0][mockupFile]=<file mockup-front.png>
tryckPrints[0][note]=8 cm under kragen
tryckPrints[1][placementProductId]=815
tryckPrints[1][name]=Rygg stor
tryckPrints[1][printFile]=<file logo-back.pdf>
```

## Example (JSON, files already stored)

```json
{
  "productKind": "tryck",
  "name": "Logo ICA",
  "sku": "TRYCK-ICA-01",
  "price": 49,
  "taxCode": 25,
  "channelIds": [12],
  "connectedProductIds": [5001, 5002],
  "tryckPrints": [
    { "placementProductId": 812, "printFile": "products/tryck-prints/20261016-a1b2c3d4e5f6/logo-front.pdf", "note": "8 cm under kragen" },
    { "placementProductId": 815, "printFile": "products/tryck-prints/20261016-a1b2c3d4e5f6/logo-back.pdf" }
  ]
}
```

## Response

### 200 OK

Returns the product summary, same shape as [Create Product](create-product-api.md#200-ok) with
`"productKind": "tryck"`. **`connectedProducts` and `tryckPrints` are not in this response** — call
`GET /api/v1/products/{id}` to read them back (shape in [Prints on products](prints-on-products-api.md#read-prints-of-a-print-product)).

### Errors

| Status | Body | Cause |
|---|---|---|
| 400 | `{"name": "..."}` / `{"sku": "..."}` | Missing `name` or `sku` |
| 400 | `{"productKind": "Invalid product kind"}` | `productKind` not `normal` / `tryck` (`tryck_placement` cannot be created here) |
| 400 | `{"tryckPrints": {"max": "A Tryck product can have at most 4 prints"}}` | More than 4 prints |
| 400 | `{"tryckPrints": {"<index>": "Placement not found"}}` | Missing / unknown / deleted `placementProductId` |
| 400 | `{"tryckPrints": {"<index>": "Placement is not active"}}` | Placement exists but `isActive = false` |
| 400 | `{"tryckPrints": {"<index>": "Each placement can only be used once"}}` | Same placement twice |
| 400 | `{"tryckPrints": {"<index>": "Missing print file"}}` | No `printFile` |
| 400 | `{"tryckPrints": {"<index>": "Upload file failed: ..."}}` | File upload failed |
| 401 | `{"message": "Invalid token authorization"}` | Bad token |
| 500 | `{"message": "<exception message>"}` | Unexpected error |

`<index>` is the 0-based position in `tryckPrints`. Several prints can fail at once.

## Gotchas

- On a `tryckPrints` validation error **nothing is saved**, but files uploaded in that request are already on disk
  (orphan files). Fix the payload and upload again, or send the paths if you know them.
- `connectedProductIds` / unknown ids are silently skipped — compare `GET /products/{id}` with what you sent.
- The parent `sku` is not unique-checked.
- A print product is never returned by the ecom; it also never appears as an add-on.
- Placements themselves are products of kind `tryck_placement`; they never appear in `GET /products` lists and
  cannot be created / edited through `/products` — use `/tryck-placements`.

## Related files

| File | Purpose |
|---|---|
| `src/Application/ApiBundle/Controller/ProductController.php` | `add()` |
| `src/Service/ProductService.php` | `add()`, `applyTryckDefaults()`, `prepareTryckPrints()`, `validateTryckPrints()`, `updateTryckPrints()`, `updateTryckConnections()` |
| `src/Entity/Product.php` | `productKind` constants |
| `src/Entity/ProductTryckConnection.php` | Print product ↔ normal product link |
| `src/Entity/ProductTryckPrint.php` | Prints of a print product (`MAX_PRINTS_PER_TRYCK = 4`) |
| `docs/spec/13-tryck-products.md` | All Tryck rules |
