Updates a purchase. Only the fields you pass are changed, the rest stay as they were.

```
v1/purchases/edit.php
```

## Parameters

| Field | Type | Required | Description |
|---|---|---|---|
| purchase_id | int | Yes | Purchase ID |
| supplier_id | int | No | Supplier ID |
| supplier_name | string | No | Supplier name and the supplier_ fields |
| materials | array | No | Material lines. Replace the current lines as a whole |
| products | array | No | Product lines. Replace the current lines as a whole |
| date_placed | string | No | Purchase order date |
| date_received | string | No | Received date. An empty string clears it |
| materials_storage_id | int | No | Materials storage location |
| products_storage_id | int | No | Products storage location |
| delivery_price | float | No | Delivery cost |
| discount | float | No | Discount |
| notes | string | No | Notes |

The status and the payment status are not changed by this method; use [v1/purchases/update_status](https://developers.controlata.com/hc/api-docs/articles/api-purchases-update-status) and [v1/purchases/update_payment](https://developers.controlata.com/hc/api-docs/articles/api-purchases-update-payment).

## Purchase lines

Lines are passed as the materials and products arrays. If at least one of the arrays is passed, the lines are replaced as a whole, and the array that is not passed is treated as empty. If neither is passed, the lines are unchanged.

| Field | Type | Required | Description |
|---|---|---|---|
| id | int | id or sku | Material or product ID |
| sku | string | id or sku | SKU. A product is also matched by its alternative SKUs |
| amount | float | Yes | Quantity |
| total | float | Yes | Line total (not the price per unit) |

The lines from the [v1/purchases/get_entry](https://developers.controlata.com/hc/api-docs/articles/api-purchases-get-entry) response can be sent back unchanged.

## Example request

Change the delivery cost without passing lines:

```
{
    "purchase_id": 29645,
    "delivery_price": 3000
}
```

The line totals stay 26000 and 4000, while the cost becomes 28600 and 4400. Material prices update to the new cost.

Replace the purchase's lines:

```
{
    "purchase_id": 29645,
    "materials": [
        {
            "sku": "M010",
            "amount": 0.25,
            "total": 32500
        }
    ]
}
```

## Example response

```
{
    "success": true
}
```

## Errors

| Error | Reason |
|---|---|
| No purchase_id in input | purchase_id is missing |
| Purchase not found or access denied | Purchase not found or deleted |
| The purchase total is 0, so line totals cannot be restored. Pass materials and products explicitly | The purchase total is 0 and no lines were passed |
| Storage ID is not set | An empty storage location was passed |
| Storage ID not found | Storage location not found or deleted |
| Date is not a valid date in format YYYY-MM-DD | Invalid date. An empty date_placed is also rejected |
| Invalid delivery_price value. Must be a number, 0 or greater | Delivery cost is not a number or below 0 |
| Invalid discount value. Must be a number, 0 or greater | Discount is not a number or below 0 |
| Materials is not an array | materials is not an array (same for products) |
| No products or materials in input | Empty materials and products were passed |
| Material with SKU "M010" not found | The item was not found or is deleted |
| Supplier not found or access denied | The supplier_id supplier was not found or is deleted |
| Supplier name must not be empty | Empty supplier_name |

Other line errors are the same as in [v1/purchases/add](https://developers.controlata.com/hc/api-docs/articles/api-purchases-add).
