# Charges

Charge session records enriched with charge point, team, and operator data. 
Contains detailed information about charging sessions including timing, 
energy consumption, pricing, and operational state.

**SQL:** `charges`

## Schema

| Column Name                                   | Data Type     | Description                                                                      |
|:----------------------------------------------|:--------------|:---------------------------------------------------------------------------------|
| **Primary ID**                                |               |                                                                                  |
| id                                            | INTEGER       | Primary key                                                                      |
| **Other IDs**                                 |               |                                                                                  |
| charge_point_id                               | INTEGER       | ID of the charge point where the charging session occurred                       |
| charge_point_site_id                          | INTEGER       | ID of the site where the charge point is located                                 |
| charge_point_auth_token_id                    | INTEGER       | ID of the authentication token used to start the charge                          |
| charge_point_brand_id                         | INTEGER       | ID of the charge point hardware brand/manufacturer                               |
| charge_point_model_id                         | INTEGER       | ID of the specific charge point hardware model                                   |
| charge_point_operator_id                      | INTEGER       | ID of the charge point operator managing the charge point                        |
| charge_point_team_id                          | INTEGER       | ID of the team responsible for operating the charge point                        |
| user_id                                       | INTEGER       | ID of the user who initiated the charging session                                |
| paying_team_id                                | INTEGER       | ID of the team responsible for paying for the charge                             |
| paying_team_name                              | VARCHAR       | Name of the team responsible for paying for the charge. For roaming charges, this identifies the eMSP (e.g. Chargemap, Izivia, Total Energies) |
| paying_operator_name                          | VARCHAR       | Name of the operator (parent organisation) of the paying team. For roaming charges, this identifies the eMSP organisation |
| sponsor_team_id                               | INTEGER       | ID of the team sponsoring/subsidizing the charge, if applicable                  |
| application_id                                | INTEGER       | ID of the application used to initiate the charge                                |
| vehicle_id                                    | INTEGER       | ID of the vehicle being charged                                                  |
| external_id                                   | VARCHAR       | External reference ID for the charging session                                   |
| price_group_id                                | INTEGER       | ID of the pricing group applied to the charge                                    |
| cost_price_group_id                           | INTEGER       | ID of the cost pricing group applied                                             |
| promotion_code_id                             | INTEGER       | ID of any promotion code applied to the charge                                   |
| smart_charge_id                               | INTEGER       | ID for smart charging session if applicable                                      |
| **Timestamps**                                |               |                                                                                  |
| created_at                                    | TIMESTAMP_NTZ | Timestamp when the charge record was created in the system                       |
| updated_at                                    | TIMESTAMP_NTZ | Timestamp when the charge record was last updated                                |
| starting_at                                   | TIMESTAMP_NTZ | Timestamp when the charging session was initiated                                |
| charging_at                                   | TIMESTAMP_NTZ | Timestamp when actual power transfer began                                       |
| cable_plugged_in_at                           | TIMESTAMP_NTZ | Timestamp when the charging cable was physically connected                       |
| stopped_at                                    | TIMESTAMP_NTZ | Timestamp when the charging session was stopped                                  |
| released_at                                   | TIMESTAMP_NTZ | Timestamp when the charge point was released after charging                      |
| failed_at                                     | TIMESTAMP_NTZ | Timestamp when the charging session failed, if applicable                        |
| completed_at                                  | TIMESTAMP_NTZ | Timestamp when the charging session was fully completed                          |
| fully_charged_at                              | TIMESTAMP_NTZ | Timestamp when the vehicle reached full charge                                   |
| charge_point_deleted_at                       | TIMESTAMP_NTZ | Timestamp when the associated charge point was deleted, if applicable            |
| paying_team_deleted_at                        | TIMESTAMP_NTZ | Timestamp when the paying team was deleted, if applicable                        |
| **Filters**                                   |               |                                                                                  |
| is_roaming_charge                             | BOOLEAN       | Indicates if this is a roaming charging session                                  |
| **Attributes**                                |               |                                                                                  |
| connector_position                            | INTEGER       | Physical position number of the connector on the charge point                    |
| charge_point_identifier                       | VARCHAR       | External identifier of the charge point, often used in communication protocols   |
| start_source                                  | VARCHAR       | Method or system used to initiate the charging session                           |
| user_gateway                                  | VARCHAR       | Interface or platform through which the user initiated the charge                |
| team_type                                     | VARCHAR       | Classification of the team (e.g. professional, private, personal)                |
| charge_type                                   | VARCHAR       | Classification of the charge session type                                        |
| gateway                                       | VARCHAR       | Payment or authentication gateway used for the session                           |
| application_name                              | VARCHAR       | Name of the application used to initiate the charge                              |
| state                                         | VARCHAR       | Current state of the charging session                                            |
| failed_reason                                 | VARCHAR       | Reason for failure if the charging session failed                                |
| failure_code                                  | VARCHAR       | Specific error code if the charging session failed                               |
| stopped_reason                                | VARCHAR       | Reason why the charging session was stopped                                      |
| note                                          | VARCHAR       | Additional notes or comments about the charging session                          |
| start_meter_wh                                | NUMBER        | Energy meter reading at start of charge in watt-hours                            |
| end_meter_wh                                  | NUMBER        | Energy meter reading at end of charge in watt-hours                              |
| estimated_wh                                  | NUMBER        | Estimated energy consumption for the charge in watt-hours                        |
| watt_hours                                    | NUMBER        | Total energy delivered during the charging session in watt-hours                 |
| meter_accuracy                                | NUMBER        | Accuracy rating of the energy meter measurements                                 |
| meter_validated                               | BOOLEAN       | Indicates if the meter readings have been validated                              |
| soc                                           | NUMBER        | Current state of charge of the vehicle battery (percentage)                      |
| soc_source                                    | VARCHAR       | Source of the state of charge information                                        |
| soc_start                                     | NUMBER        | State of charge at the start of charging session                                 |
| price                                         | NUMBER        | Price charged for the charging session                                           |
| price_limit                                   | NUMBER        | Maximum price limit set for the charging session                                 |
| cost                                          | NUMBER        | Actual cost incurred for the charging session                                    |
| average_price_per_kwh                         | NUMBER        | Average price per kWh for the charging session, calculated as price / kWh rounded to 4 decimal places |
| payment_method                                | VARCHAR       | Method used for payment processing                                               |
| avg_co2_kwh                                   | NUMBER        | Average CO2 emissions (in kg) per kWh for the energy delivered                   |
| co2                                           | NUMBER        | CO2 emissions (in kg) produced for the given charge                              |
| avg_renewable                                 | NUMBER        | Percentage of renewable energy in the power mix                                  |
| charge_auth_identifier                        | VARCHAR       | Identifier used for charge authorization                                         |
| charge_rating_id                              | VARCHAR       | ID of the driver's rating of this charge. Null when the charge has not been rated. Only the most recent rating ( MAX(charge_rating_id)) is shown |
| charge_rating                                 | INTEGER       | Driver's rating of the charging session (e.g. 1-5). Null when the charge has not been rated |
| charge_rating_comment                         | VARCHAR       | Driver's free-text comment accompanying the rating. Null when the charge has not been rated, no comment was given, or the rating author's account is deleted (PII rule) |
| roaming_provider                              | VARCHAR       | Provider handling roaming services if applicable                                 |
| **Categorizations**                           |               |                                                                                  |
| type                                          | VARCHAR       | Classification of the charging session type ('sponsored', 'operator', 'external') |
| operator_role                                 | VARCHAR       | Role of the operator in relation to the charge (owner/payer/both)                 |
| **Operator matching filters**                 |               |                                                                                   |
| paying_team_is_main_operator_match            | BOOLEAN       | Indicates if paying team matches the main operator                                |
| paying_team_is_parent_operator_match          | BOOLEAN       | Indicates if paying team matches the parent operator                              |
| charge_point_is_main_operator_match           | BOOLEAN       | Indicates if charge point matches the main operator                               |
| charge_point_is_parent_operator_match         | BOOLEAN       | Indicates if charge point matches the parent operator                             |
| charge_point_is_sponsor_operator_match        | BOOLEAN       | Indicates if charge point matches the sponsor operator                            |
| charge_point_is_sponsor_parent_operator_match | BOOLEAN       | Indicates if charge point matches the sponsor parent operator                     |
| charge_point_legacy_alignment_flag            | BOOLEAN       | An alignment flag from the charge_points model                                    |

## Data inclusion criteria
- either the `charge_point_id` is among ids of the operator's `charge_points`
- or the `paying_team_id` is among the id's of the operator's `teams'`
```sql
select charges.*
from charges as charges
left join charge_points as cp
    on charges.charge_point_id = cp.id
left join teams as teams
    on charges.paying_team_id = teams.id
where
    -- Filter by operatorRole: 'owner' filters by charge_point_id, 'payer' filters by paying_team_id
    -- Include charges where:
    --   1. The charge point is owned by the partner (operatorRole='owner')
    --   2. OR the charge is paid by a team belonging to the partner (operatorRole='payer')
    cp.id is not null
    or teams.id is not null

```

## Data flow
| Upstream tables         | Downstream tables                                 |
|:------------------------|:--------------------------------------------------|
| `charge_points` | `charge_configs`                          |
| `teams`         | `charge_pricings`                         |
|                         | `charge_pricing_breakdowns`               |
|                         | `charge_pricing_breakdown_specifications` |

## Alignment
### Monta API
- Aligns with Monta API [get/charges](https://developer.monta.com/api/v1/charges/get) endpoint 
- This endpoint has a mandatory `operatorRole` param, so the SQL table has to be subset to match the API
- use `operator_role` column along with operator_match columns to align the API with the SQL table 
- `operatorRole` = 'payer' → ```sql operator_role in ('payer', 'both') and paying_team_is_main_operator_match=true```
- `operatorRole` = 'owner' → ```sql operator_role in ('owner', 'both') and  (charge_point_is_main_operator_match=true or charge_point_is_parent_operator_match=true)```
