# Prints on Products API (TSHIRTORDER-1653)

How to put prints on products: manage the placement list, link a print product to normal products, add / change
/ remove its prints, and what happens in an order. To create the print product itself see
[Create Print Product](create-print-product-api.md).

```
Placement (Bröst vänster, Rygg…)      ← /api/v1/tryck-placements, shared list with price
      ▲ placementProductId
Print product (productKind "tryck")   ← /api/v1/products, max 4 prints, each = placement + print file
      │ connectedProductIds
      ▼
Normal products (T-shirt, hoodie…)    ← when ordered, the print product + one line per print are added
```

All endpoints: base `/api/v1`, `Authorization: Bearer <access token>`; bad token or a token flagged
`lockForOnlyIntegration` → `401 {"message": "Invalid token authorization"}`.

## Placements API

The shared list of print placements and their price per shirt. Not per channel.

| Action | Request | Controller → Service |
|---|---|---|
| List | `GET /tryck-placements/` | `TryckPlacementController::list()` → `TryckPlacementService::list()` |
| Detail | `GET /tryck-placements/{id}` | `::detail()` → `::find()` |
| Create | `POST /tryck-placements/` | `::add()` → `::add()` |
| Update | `POST /tryck-placements/{id}` | `::update()` → `::update()` |
| Delete | `DELETE /tryck-placements/{id}` | `::delete()` → `::delete()` |

### Fields (create / update)

| Field | Type | Notes |
|---|---|---|
| `name` | string | Required, not blank |
| `sku` | string | Required, not blank, trimmed, max 255. Unique among all (not deleted) products, case-insensitive. Can be changed on update |
| `price` | float | `>= 0`. Default `0` (shown as "Ingår" / included) |
| `sortOrder` | int | List order |
| `isActive` | bool | Default `true`. Inactive placements cannot be picked for **new** prints |
| `taxCode` | int | Percent. Default `tax_default * 100` |

Update accepts any subset. A new price / sku only applies to order lines created after the change.

List filter: `?isActive=true` / `false`. Sorted by `sortOrder`, then `id`.

### Item returned

```json
{ "id": 812, "name": "Rygg", "sku": "RYGG", "price": 40, "taxCode": 25, "sortOrder": 4, "isActive": true }
```

### Errors

| Status | Body | Cause |
|---|---|---|
| 400 | `{"name": "This value should not be blank."}` | Empty name |
| 400 | `{"sku": "This value should not be blank."}` | Empty sku |
| 400 | `{"sku": "Sku already existed"}` | Another product / size / placement already has this sku |
| 400 | `{"sku": "Sku is too long (max 255 characters)"}` | More than 255 characters |
| 400 | `{"price": "Price must be 0 or more"}` | Negative / non-numeric price |
| 400 | `{"message": "Placement is used by Tryck products", "tryckProducts": [{"id", "name", "sku"}]}` | Delete while a print product still has a print on it — remove those prints first |
| 404 | `{"message": "Placement not found"}` | Unknown / deleted id |
| 500 | `{"message": "<exception message>"}` | Unexpected error |

Delete is a soft delete. Placements cannot be edited or deleted through `/products`.

## Link a print product to products

`POST /api/v1/products/{id}` (update) on the print product, field `connectedProductIds` (int[]):

- **Full replace**: send the complete list every time. `[]` removes all links.
- Key absent → links unchanged (e.g. when only the price changes).
- Unknown / deleted ids are silently skipped. Ignored on a non-Tryck product.

```json
POST /api/v1/products/6001
{ "connectedProductIds": [5001, 5002, 5003] }
```

## Add / change prints on a print product

`POST /api/v1/products/{id}` (update) on the print product, field `tryckPrints`:

| Value | Effect |
|---|---|
| key absent | Prints unchanged |
| `[]` (or `""` in multipart) | Remove all prints |
| array | **Replaces all prints**, in the order sent |

Fields per print, limits and errors are the same as on create — see
[Create Print Product → `tryckPrints`](create-print-product-api.md#tryckprints--the-prints-max-4).

To **keep** a print file, send back the stored path from `GET /products/{id}` (`tryckPrints[i].printFile`)
instead of a file. A placement that was deactivated after it was picked can still be kept on that print product.

```
POST /api/v1/products/6001
Content-Type: multipart/form-data

tryckPrints[0][placementProductId]=812
tryckPrints[0][printFile]=products/tryck-prints/20261016-a1b2c3d4e5f6/logo-front.pdf   <- keep existing file
tryckPrints[0][note]=8 cm under kragen
tryckPrints[1][placementProductId]=815
tryckPrints[1][printFile]=<file new-back.pdf>                                           <- new file
```

## Read prints of a print product

`GET /api/v1/products/{id}` — only for `productKind = "tryck"`:

```json
{
  "id": 6001,
  "productKind": "tryck",
  "connectedProductIds": [5001, 5002],
  "connectedProducts": [
    { "id": 5001, "name": "Premium T-shirt", "sku": "PT-001" },
    { "id": 5002, "name": "Hoodie Black", "sku": "HOOD-BLK" }
  ],
  "tryckPrints": [
    {
      "id": 3,
      "placementProductId": 812,
      "placementName": "Rygg",
      "placementPrice": 40,
      "placementDeleted": false,
      "name": "Tryck 1",
      "printFile": "products/tryck-prints/20261016-a1b2c3d4e5f6/logo-back.pdf",
      "mockupFile": null,
      "note": "8 cm under kragen",
      "sortOrder": 1
    }
  ]
}
```

Copying a print product ("copy to new") also copies its prints.

## What happens in an order

Nothing extra to send when creating / updating an order: send only the normal product lines, the backend adds
the print lines on every save.

```
Premium T-shirt S        x3   (sent by the client)
  Logo ICA (Tryck)       x3   productKind "tryck",           tryckParentOrderProductId = shirt line
    Bröst vänster        x3   productKind "tryck_placement", tryckParentOrderProductId = Tryck line
    Rygg                 x3   productKind "tryck_placement", tryckParentOrderProductId = Tryck line
```

- Quantity of the print line and its placement lines = quantity of the shirt line.
- One placement line per print, also when the placement price is `0`.
- Placement lines carry `tryckPrintFile`, `tryckMockupFile`; the note is in `productionComment`, the placement
  name in `productionPlacements`.
- Auto-added print / placement lines sent back in `items` are ignored (not editable); removing the shirt line
  removes them.
- `totalQuantity` of the order counts only the shirts.
- Print / placement lines never change stock and never carry kickback.
- Changing the prints of a print product updates existing orders the next time they are saved (price of an
  existing line stays as it was).

## Related files

| File | Purpose |
|---|---|
| `src/Application/ApiBundle/Resources/config/route/tryck_placement.yaml` | Placement routes (prefix `/tryck-placements`) |
| `src/Application/ApiBundle/Controller/TryckPlacementController.php` | Placement endpoints |
| `src/Service/TryckPlacementService.php` | Placement CRUD (a placement = `Product` with `productKind = tryck_placement`) |
| `src/Service/ProductService.php` | `update()`, `generateItemDetail()`, `getTryckPrintItems()`, `updateTryckConnections()`, `updateTryckPrints()` |
| `src/Service/OrderService.php` | `addTryckPlacementOrderProducts()` — print lines in orders |
| `src/Entity/ProductTryckConnection.php`, `src/Entity/ProductTryckPrint.php` | Link / print tables |
| `docs/spec/13-tryck-products.md` | All Tryck rules |
