# Inventory Adjustments API

Inventory adjustments allow you to manually correct the quantity on hand for inventory items
outside of normal sales and purchase flows — e.g., shrinkage, found stock, physical count discrepancies.

Each adjustment automatically:

- Updates FIFO cost layers (consumes layers for decreases, creates new layers for increases).
- Posts a balanced GL journal entry using the chosen adjustment account.

---

## Base URL

```
/api/v1/inventory-adjustments
```

All endpoints require `Authorization: Bearer {token}` and the `company` middleware (company-scoped).

---

## Role Permissions

| Action | ADMIN | ACCOUNTANT | VIEWER |
|--------|-------|------------|--------|
| List   | ✓     | ✓          | ✓      |
| Show   | ✓     | ✓          | ✓      |
| Create | ✓     | ✓          | ✗      |
| Void   | ✓     | ✗          | ✗      |

---

## Endpoints

### `GET /inventory-adjustments`

List all adjustments for the authenticated company, newest first.

#### Query Parameters

| Parameter    | Type   | Default | Description                            |
|--------------|--------|---------|----------------------------------------|
| `start_date` | date   | —       | Filter adjustments on or after date    |
| `end_date`   | date   | —       | Filter adjustments on or before date   |
| `status`     | string | —       | Comma-separated: `open`, `voided` (default: both) |
| `per_page`   | int    | 25      | Results per page (max 100)             |

#### Example Response

```json
{
  "data": [
    {
      "id": 1,
      "adjustment_number": "IADJ-0001",
      "status": "OPEN",
      "adjustment_date": "2026-06-13",
      "adjustment_account": {
        "id": 8,
        "name": "Inventory Shrinkage"
      },
      "memo": "Monthly physical count",
      "lines": [
        {
          "id": 1,
          "item": { "id": 3, "sku": "WIDGET-A", "name": "Widget A" },
          "qty_before": 100.0,
          "qty_after": 97.0,
          "change_in_qty": -3.0,
          "value_change": -150.0,
          "movement_type": "adjustment_out"
        }
      ],
      "created_at": "2026-06-13T14:30:00.000000Z"
    }
  ],
  "meta": { "current_page": 1, "per_page": 25, "total": 1 },
  "links": { "first": "...", "last": "...", "prev": null, "next": null }
}
```

---

### `POST /inventory-adjustments`

Create a new inventory adjustment.

#### Request Body

```json
{
  "adjustment_date": "2026-06-13",
  "adjustment_account_id": 8,
  "memo": "Monthly physical count",
  "lines": [
    {
      "item_id": 3,
      "change_in_qty": -3
    },
    {
      "item_id": 5,
      "new_qty": 42
    }
  ]
}
```

#### Field Notes

| Field                    | Required | Description                                                                                   |
|--------------------------|----------|-----------------------------------------------------------------------------------------------|
| `adjustment_date`        | Yes      | Date of the physical count or discovery                                                       |
| `adjustment_account_id`  | Yes      | Account to debit/credit the value change against (e.g., Inventory Shrinkage, Inventory Over) |
| `memo`                   | No       | Optional note attached to the adjustment and its journal entry                                |
| `lines[].item_id`        | Yes      | Must be an `INVENTORY` type item in the same company                                          |
| `lines[].change_in_qty`  | One of   | Delta: negative = decrease, positive = increase. Cannot be zero.                             |
| `lines[].new_qty`        | One of   | Absolute target quantity. Service computes the delta from current `qty_on_hand`.              |

Provide **exactly one** of `change_in_qty` or `new_qty` per line.

#### GL Impact

| Scenario     | Debit                    | Credit                   |
|--------------|--------------------------|--------------------------|
| Decrease     | Adjustment Account       | Inventory Asset          |
| Increase     | Inventory Asset          | Adjustment Account       |

- **Decrease cost basis:** FIFO — oldest layers consumed first, layer unit costs used.
- **Increase cost basis:** Weighted average of remaining FIFO layers (or 0.00 if no stock exists).

#### Example Response (201 Created)

```json
{
  "data": {
    "id": 1,
    "adjustment_number": "IADJ-0001",
    "status": "OPEN",
    "adjustment_date": "2026-06-13",
    "adjustment_account": { "id": 8, "name": "Inventory Shrinkage" },
    "memo": "Monthly physical count",
    "lines": [
      {
        "id": 1,
        "item": { "id": 3, "sku": "WIDGET-A", "name": "Widget A" },
        "qty_before": 100.0,
        "qty_after": 97.0,
        "change_in_qty": -3.0,
        "value_change": -150.0,
        "movement_type": "adjustment_out"
      }
    ],
    "created_at": "2026-06-13T14:30:00.000000Z"
  }
}
```

---

### `GET /inventory-adjustments/{id}`

Retrieve a single adjustment with all lines.

---

### `POST /inventory-adjustments/{id}/void`

Void an open adjustment. **ADMIN only.** Reverses FIFO layer changes and the auto-generated journal entry. The adjustment record is retained with `status: VOIDED` for audit.

#### Request Body (optional)

```json
{
  "void_reason": "Counted wrong item"
}
```

#### Example Response `200 OK`

```json
{
  "data": {
    "id": 1,
    "adjustment_number": "IADJ-0001",
    "status": "VOIDED",
    "adjustment_date": "2026-06-13",
    "adjustment_account": { "id": 8, "name": "Inventory Shrinkage" },
    "memo": "Monthly physical count",
    "voided_at": "2026-06-14T09:00:00.000000Z",
    "voided_by": { "id": 2, "name": "Admin User" },
    "void_reason": "Counted wrong item",
    "lines": [ "..." ],
    "available_actions": ["view_journal"]
  }
}
```

#### Void rules

| Line type | FIFO on void |
|-----------|--------------|
| Decrease (`change_in_qty < 0`) | Restores consumed layers from `inventory_adjustment_line_layers` |
| Increase (`change_in_qty > 0`) | Deletes the adjustment-created cost layer if fully intact |

Void fails with `422` if stock from an increase line has already been partially consumed (e.g. by a subsequent sale or decrease adjustment).

---

## Inventory Transaction Hub Integration

Adjustments automatically appear in `GET /inventory-transactions` under `movement_type`:

- **`ADJUSTMENT_IN`** — increase adjustments (`change_in_qty > 0`)
- **`ADJUSTMENT_OUT`** — decrease adjustments (`change_in_qty < 0`)

Filter by type explicitly: `?type=adjustment_in,adjustment_out`

Voided adjustments are **excluded** from the hub so running balances stay correct. They remain visible via `GET /inventory-adjustments?status=voided`.

---

## Error Responses

| Status | Scenario                                                   |
|--------|------------------------------------------------------------|
| 401    | Missing or invalid token                                   |
| 403    | Role is VIEWER (create only) or non-ADMIN (void only)      |
| 404    | Adjustment belongs to a different company                  |
| 422    | Validation failure (missing line fields, zero qty, etc.)   |
| 422    | Insufficient FIFO stock for a decrease adjustment          |
| 422    | Void blocked: increase layer partially consumed, or not OPEN |
