# Gross Profit Summary

Dashboard card that shows Gross Profit for the current period, compares it to the previous period, and returns a 6-period trend line — mirroring the QuickBooks Online Business Overview GP card.

---

## Endpoint

```
GET /api/v1/reports/gross-profit-summary
```

**Auth:** `Bearer` token (Sanctum)  
**Roles:** Any authenticated company user

---

## Query Parameters

| Parameter | Type   | Required | Default | Description                                |
|-----------|--------|----------|---------|--------------------------------------------|
| `period`  | string | No       | `month` | Aggregation window: `month`, `quarter`, `year`, `week`, `today` |

---

## Response `200 OK`

```json
{
  "data": {
    "period": "month",
    "current_start": "2026-06-01",
    "current_end": "2026-06-30",
    "previous_start": "2026-05-01",
    "previous_end": "2026-05-31",
    "gross_profit": "30000.0000",
    "previous_gross_profit": "27000.0000",
    "change_amount": "3000.0000",
    "change_percent": 11.11,
    "trend": [
      { "period": "2026-01", "start_date": "2026-01-01", "end_date": "2026-01-31", "gross_profit": "22000.0000" },
      { "period": "2026-02", "start_date": "2026-02-01", "end_date": "2026-02-28", "gross_profit": "24500.0000" },
      { "period": "2026-03", "start_date": "2026-03-01", "end_date": "2026-03-31", "gross_profit": "26000.0000" },
      { "period": "2026-04", "start_date": "2026-04-01", "end_date": "2026-04-30", "gross_profit": "25000.0000" },
      { "period": "2026-05", "start_date": "2026-05-01", "end_date": "2026-05-31", "gross_profit": "27000.0000" },
      { "period": "2026-06", "start_date": "2026-06-01", "end_date": "2026-06-30", "gross_profit": "30000.0000" }
    ]
  }
}
```

### Fields

| Field                    | Type            | Description                                                      |
|--------------------------|-----------------|------------------------------------------------------------------|
| `period`                 | string          | Echo of the requested period granularity                         |
| `current_start`          | date            | Start of the current period                                      |
| `current_end`            | date            | End of the current period                                        |
| `previous_start`         | date            | Start of the previous period                                     |
| `previous_end`           | date            | End of the previous period                                       |
| `gross_profit`           | decimal(4)      | GP for the current period                                        |
| `previous_gross_profit`  | decimal(4)      | GP for the previous period                                       |
| `change_amount`          | decimal(4)      | `gross_profit − previous_gross_profit`                           |
| `change_percent`         | float \| null   | `(change_amount / abs(previous)) × 100`; `null` if previous = 0 |
| `trend`                  | array           | Last 6 periods, oldest → newest                                  |
| `trend[].period`         | string          | Label: `YYYY-MM`, `YYYY-WNN`, `YYYY-QN`, or `YYYY`              |
| `trend[].start_date`     | date            | Period start                                                     |
| `trend[].end_date`       | date            | Period end                                                       |
| `trend[].gross_profit`   | decimal(4)      | GP for that trend point                                          |

---

## Calculation

```
Gross Profit = Total Income − Cost of Goods Sold (COGS)
```

- **Income** — journal entry lines on accounts with `account_type = INCOME`; net = `credit_sum − debit_sum`
- **COGS** — journal entry lines on accounts with `account_sub_type = COGS`; net = `debit_sum − credit_sum`
- When **negative stock** is enabled, additional COGS from bill true-up posts on the **bill receive date**, which can shift gross profit for that period after invoices were already posted. See [invoices.md](./invoices.md) and [bills.md](./bills.md).
- Only **posted** journal entries (linked to posted transactions) are included
- All figures are scoped to the authenticated user's `company_id`

---

## Period Labels

| `period` | Label format  | Example      | Current window        | Previous window |
|----------|---------------|--------------|-----------------------|-----------------|
| `today`  | `YYYY-MM-DD`  | `2026-06-20` | Today only            | Yesterday only  |
| `month`  | `YYYY-MM`     | `2026-06`    | Full calendar month   | Prior month     |
| `quarter`| `YYYY-QN`     | `2026-Q2`    | Full calendar quarter | Prior quarter   |
| `year`   | `YYYY`        | `2026`       | Full calendar year    | Prior year      |
| `week`   | `YYYY-WNN`    | `2026-W25`   | Full calendar week    | Prior week      |

For `period=today`, the trend returns the last **6 days** (oldest → newest), each point being a single day.

---

## Example — single-day card

```
GET /api/v1/reports/gross-profit-summary?period=today
```

```json
{
  "data": {
    "period": "today",
    "current_start": "2026-06-20",
    "current_end": "2026-06-20",
    "previous_start": "2026-06-19",
    "previous_end": "2026-06-19",
    "gross_profit": "1200.0000",
    "previous_gross_profit": "950.0000",
    "change_amount": "250.0000",
    "change_percent": 26.32,
    "trend": [
      { "period": "2026-06-15", "start_date": "2026-06-15", "end_date": "2026-06-15", "gross_profit": "800.0000" },
      { "period": "2026-06-16", "start_date": "2026-06-16", "end_date": "2026-06-16", "gross_profit": "900.0000" },
      { "period": "2026-06-17", "start_date": "2026-06-17", "end_date": "2026-06-17", "gross_profit": "1100.0000" },
      { "period": "2026-06-18", "start_date": "2026-06-18", "end_date": "2026-06-18", "gross_profit": "1000.0000" },
      { "period": "2026-06-19", "start_date": "2026-06-19", "end_date": "2026-06-19", "gross_profit": "950.0000" },
      { "period": "2026-06-20", "start_date": "2026-06-20", "end_date": "2026-06-20", "gross_profit": "1200.0000" }
    ]
  }
}
```

---

## Error Responses

### `422 Unprocessable Entity` — invalid period

```json
{
  "message": "The selected period is invalid.",
  "errors": {
    "period": ["The selected period is invalid."]
  }
}
```

### `401 Unauthorized`

```json
{ "message": "Unauthenticated." }
```
