# Reports Overview

Predefined reports available in the system. Use report identifiers to list, execute, and retrieve data via the REST API.

## Sales Reports

Reports based on `sales_order` table. Cover order metrics (count, revenue, qty, discounts, shipping, tax, refunds, gross margin) across different dimensions.

| Identifier | Dimension | Description |
|---|---|---|
| `order_overview` | Day | Daily sales overview with all key order metrics |
| `order_plain` | Order (increment_id) | Individual order listing with customer, products, payment method |
| `order_hour` | Hour of day | Sales broken down by hour |
| `order_day` | Day of week | Sales broken down by day of week |
| `order_coupon` | Coupon code | Sales grouped by coupon code |
| `order_pricerule` | Cart price rule | Sales grouped by applied cart price rules |
| `order_customer` | Customer email | Sales per customer |
| `order_customergroup` | Customer group | Sales grouped by customer group (General, Wholesale, etc.) |
| `order_geo` | Country | Sales grouped by shipping/billing country |
| `order_newvsreturningcustomer` | Day | New vs returning customer orders and revenue over time |
| `order_paymenttype` | Payment method | Sales grouped by payment method |
| `order_taxrate` | Tax code | Sales grouped by tax rate |

## Catalog Reports

Reports connecting products and categories with sales data.

| Identifier | Dimension | Description |
|---|---|---|
| `catalog_product` | Product SKU | Product performance: orders, qty sold, tax, discounts, refunds, gross margin, revenue |
| `catalog_category` | Category | Sales by category: orders, qty sold, tax, discounts, refunds, revenue |
| `catalog_attribute` | Product attribute (e.g., status) | Sales by product attribute values |
| `catalog_attributeset` | Attribute set | Sales grouped by product attribute set |

## Customer Reports

| Identifier | Dimension | Description |
|---|---|---|
| `customers_customers` | Customer | Customer listing with name, email, registration date, group, order count, products |

## Cart Reports

Reports based on `quote` table. Only active (non-converted) carts.

| Identifier | Dimension | Description |
|---|---|---|
| `cart_overview` | Day | Active carts overview: count, subtotal, grand total over time |
| `cart_abandoned` | Cart | Individual abandoned carts with customer, date, qty, products |
| `cart_product` | Product SKU | Products in active carts: cart count, qty |

## Listing Reports

Use `GET /V1/reports/reports` to retrieve all available reports (both predefined and custom). The response contains full report configuration which can be very large. Always use field filtering to retrieve only necessary data.

### Workflow

1. List reports with minimal fields:

```json
{
  "endpoint": "/V1/reports/reports",
  "method": "GET",
  "query": {
    "searchCriteria[pageSize]": "100",
    "fields": "items[identifier,name,table],total_count"
  }
}
```

2. To get full configuration of a specific report, use the get-by-identifier endpoint:

```json
{
  "endpoint": "/V1/reports/report/order_overview",
  "method": "GET"
}
```

### Pitfalls

- Listing reports without `fields` filter returns full configuration (columns, filters, dimensions, etc.) for every report — this fills context quickly and provides little value for browsing
- Predefined reports have named identifiers (e.g., `order_overview`), custom reports have numeric identifiers (e.g., `7`)

## Executing Reports

Use `POST /V1/reports/report/:identifier/execute` to run a report and retrieve its data. The report uses its saved configuration by default — you only need to pass parameters you want to override.

### Workflow

1. Execute a report with default settings:

```json
{
  "endpoint": "/V1/reports/report/order_overview/execute",
  "method": "POST",
  "body": {}
}
```

2. Execute with a time range and pagination:

```json
{
  "endpoint": "/V1/reports/report/order_overview/execute",
  "method": "POST",
  "body": {
    "timeRange": "last30Days",
    "pageSize": 50,
    "currentPage": 1
  }
}
```

3. Execute with custom date range:

```json
{
  "endpoint": "/V1/reports/report/order_overview/execute",
  "method": "POST",
  "body": {
    "dateFrom": "2025-01-01",
    "dateTo": "2025-12-31"
  }
}
```

4. Execute with filters:

```json
{
  "endpoint": "/V1/reports/report/order_plain/execute",
  "method": "POST",
  "body": {
    "filters": [
      {"column": "sales_order|status", "conditionType": "eq", "value": "complete"}
    ],
    "timeRange": "year"
  }
}
```

5. The response contains `columns`, `items`, `totals`, `size`, and `request` (the resolved request metadata). When only report data is needed, filter out the request metadata:

```json
{
  "query": {
    "fields": "columns,items,totals,size"
  }
}
```

### Pitfalls

- The `request` field in the response echoes back the full resolved request configuration — filter it out with `fields` when you only need the data
- `timeRange` accepts presets: `lifetime`, `today`, `yesterday`, `week`, `month`, `quarter`, `year`, `last7Days`, `last30Days`
- When both `dateFrom`/`dateTo` and `timeRange` are provided, the custom dates take precedence

**IMPORTANT — Output Filtering:** Always use the `fields` query parameter to exclude the `request` object and raw `data` arrays from report responses. The `request` object contains the generated SQL query and internal metadata that is large and not useful for presenting results. Use `"fields": "columns,size,totals[formatted_data],items[formatted_data]"` as the default. Keys use snake_case (e.g., `formatted_data`, not `formattedData`). Omit `fields` only when debugging report issues (e.g., inspecting the generated SQL query or raw values).

## Report Not Listed Here

If the user asks for a report that doesn't match any identifier above:

1. **Explore existing reports** — list all reports via `GET /V1/reports/reports` with `fields=items[identifier,name,table,columns,dimensions],total_count`. Check if any existing report (predefined or custom) already covers the request or can be executed with column/dimension/filter overrides
2. **If no matching report exists** — inform the user and ask if they want to create a new custom report. See the **Report Builder** doc in `Mirasvit_ReportBuilder` for creating and updating custom reports. Do not create reports without user confirmation
3. **Use dbreader as last resort** — only for complex queries not covered by the report engine (multi-level aggregations, custom joins)
