# Supplier and Plant Weather Risk Insights

This consumption data product sample demonstrates how to combine zero-copy
[SAP Business Data Cloud (SAP BDC)](https://docs.cloud.google.com/cortex/docs/source-system-integration/sap-bdc.md.txt)
procurement and inbound delivery shares with **Google WeatherNext 3** ensemble
weather forecasts subscribed through
[BigQuery sharing](https://docs.cloud.google.com/cortex/docs/source-system-integration/analytics-hub.md.txt).
It requires no commercial shipping-telemetry subscription and serves both as a
standalone facility-to-facility weather risk accelerator and as the base module
for
[Supplier Inbound Delivery Weather Risk Insights](https://docs.cloud.google.com/cortex/docs/solution-samples/supplier-inbound-delivery-weather-risk-insights.md.txt).

With this sample, you can:

- Track open inbound delivery spend and Value at Risk across suppliers, plants, and critical material types.
- Geocode physical dispatch origins (using a multi-step SAP origin waterfall) and receiving plants against a pre-packaged city geospatial catalog.
- Overlay 72-hour, 0.1-degree WeatherNext 3 ensemble forecasts within a configurable spatial radius (25 km by default) to project P50 and P90 arrival dates and flag weather-induced schedule variance.

## Reference architecture

A high-level reference architecture provides an overview of the source-specific
data products required to feed this use case.

[![Supplier and Plant Weather Risk Insights consumption data product sample reference architecture for Cortex Framework](https://docs.cloud.google.com/static/cortex/docs/images/cortex7_solution_samples_bdc.png)](https://docs.cloud.google.com/static/cortex/docs/images/cortex7_solution_samples_bdc.png)
**Figure 1**. Supplier and Plant Weather Risk Insights consumption data product sample reference architecture for Cortex Framework.

## Published data assets

Deploying `cortex_samples.sap_bdc.products.supplier_plant_weather_risk_insights`
creates the following BigQuery tables and views in your target dataset:

| Data asset | Materialization | Primary key grain | Description |
| `inbound_delivery_insights` | `Table` | `delivery_document`, `delivery_document_item` | Conformed inbound delivery item spine resolving physical dispatch origins using a multi-step SAP waterfall (manual PO header override address `ADRNR`, goods supplier `LLIEF`, supplying plant `RESWK`, and vendor master `LIFNR`), prorated net order value, historical On-Time In-Full (OTIF) flags, and supplier-to-plant lead-time and volatility benchmarks. |
| `supplier_plant_weather_risk_insights` | `View` | `delivery_document`, `delivery_document_item` | Consumption view overlaying 72-hour WeatherNext 3 precipitation, wind, temperature, and ensemble spread forecasts on both dispatch origins and receiving SAP plants to classify weather exposure tiers and project adjusted arrival dates. |
| `world_city_directory` | `Table` | `city_id` | Reference table of global and US cities with standardized WGS84 coordinates and precomputed BigQuery `GEOGRAPHY` points (`city_geom`) for spatial joins. |
|---|---|---|---|

## Required external catalog sources

Before enabling this sample, configure the following external catalog sources:

- **SAP Business Data Cloud (`lakehouse_delta_share`)** : Import shares exposing Purchase Order (`purchaseorder`), Supplier (`supplier`), Plant (`plant`), Address (`address`), Product (`product`), Company (`company`), Inbound Delivery Header (`delivery_header`), and Inbound Delivery Item (`delivery_item`). For setup instructions, see [SAP Business Data Cloud integration](https://docs.cloud.google.com/cortex/docs/source-system-integration/sap-bdc.md.txt).
- **Google WeatherNext 3 (`analytics_hub`)** : Subscribe to the Google WeatherNext 3 listing in BigQuery sharing and include the forecast table (for example, `weathernext_3_0_0`) in `inlineTableSettings.tables` of an `analytics_hub` catalog module. For setup instructions, see [BigQuery sharing integration](https://docs.cloud.google.com/cortex/docs/source-system-integration/analytics-hub.md.txt).

## Enable the data product in `config/config.yaml`

Register your SAP BDC and Google WeatherNext catalogs under
`data.modules.catalogs`, then enable
`cortex_samples.sap_bdc.products.supplier_plant_weather_risk_insights` under
`data.modules.products`:

```yaml
data:
  namespaces:
    - name: cortex
    - name: cortex_samples

  modules:
    catalogs:
      # SAP Business Data Cloud Delta Sharing catalog.
      - id: sap_bdc_catalog
        type: lakehouse_delta_share
        bindsNamespaces: [sap_bdc]
        connectionSettings:
          catalogId: sap_bdc_catalog
          projectId: SAP_BDC_CATALOG_PROJECT_ID
          location: US
          shares:
            - shareId: PURCHASE_ORDER_SHARE_ID
            - shareId: SUPPLIER_SHARE_ID
            - shareId: PLANT_SHARE_ID
            - shareId: ADDRESS_SHARE_ID
            - shareId: PRODUCT_SHARE_ID
            - shareId: COMPANY_SHARE_ID
            - shareId: DELIVERY_HEADER_SHARE_ID
            - shareId: DELIVERY_ITEM_SHARE_ID

      # Google WeatherNext 3 linked dataset subscribed from BigQuery sharing.
      - id: ah_weathernext_catalog
        type: analytics_hub
        bindsNamespaces: [cortex.external.weathernext]
        connectionSettings:
          projectId: LINKED_DATASET_PROJECT_ID
          datasetId: WEATHERNEXT_DATASET_ID
          location: US
        inlineTableSettings:
          tables:
            - weathernext_3_0_0

    products:
      # Enabled Supplier & Plant Weather Risk Insights sample.
      - moduleId: supplier_plant_weather_risk_insights
        enabled: true
        modulePath: cortex_samples.sap_bdc.products.supplier_plant_weather_risk_insights
        dataTargetId: samples_target
        dependencyBindings:
          sapBdcPurchaseOrder: sap_bdc_catalog.purchaseorder
          sapBdcSupplier: sap_bdc_catalog.supplier
          sapBdcPlant: sap_bdc_catalog.plant
          sapBdcAddress: sap_bdc_catalog.address
          sapBdcProduct: sap_bdc_catalog.product
          sapBdcCompany: sap_bdc_catalog.company
          sapBdcDeliveryHeader: sap_bdc_catalog.delivery_header
          sapBdcDeliveryItem: sap_bdc_catalog.delivery_item
          weatherNext: ah_weathernext_catalog.weathernext_3_0_0
        inlineTableSettings:
          supplier_plant_weather_risk_insights:
            filters:
              critical_material_types: ["ROH", "RAW", "HALB"]
              weather_thresholds:
                forecast_horizon_hours: 72
                spatial_radius_meters: 25000
```

Replace the following:

- `SAP_BDC_CATALOG_PROJECT_ID`: the Google Cloud project ID hosting your BigLake Delta Sharing catalog, for example: `my-sap-bdc-project`.
- `PURCHASE_ORDER_SHARE_ID`: physical Delta Sharing share ID imported from SAP BDC for Purchase Order, for example: `purchaseorder`.
- `SUPPLIER_SHARE_ID`: physical Delta Sharing share ID imported from SAP BDC for Supplier, for example: `supplier`.
- `PLANT_SHARE_ID`: physical Delta Sharing share ID imported from SAP BDC for Plant, for example: `plant`.
- `ADDRESS_SHARE_ID`: physical Delta Sharing share ID imported from SAP BDC for Address, for example: `address`.
- `PRODUCT_SHARE_ID`: physical Delta Sharing share ID imported from SAP BDC for Product, for example: `product`.
- `COMPANY_SHARE_ID`: physical Delta Sharing share ID imported from SAP BDC for Company, for example: `company`.
- `DELIVERY_HEADER_SHARE_ID`: physical Delta Sharing share ID imported from SAP BDC for Inbound Delivery Header, for example: `delivery_header`.
- `DELIVERY_ITEM_SHARE_ID`: physical Delta Sharing share ID imported from SAP BDC for Inbound Delivery Item, for example: `delivery_item`.
- `LINKED_DATASET_PROJECT_ID`: the Google Cloud project ID hosting the subscribed WeatherNext linked dataset, for example: `my-cortex-project`.
- `WEATHERNEXT_DATASET_ID`: the BigQuery dataset ID of your subscribed WeatherNext 3 listing, for example: `google_weathernext_3`.

You can further customize SAP client and delivery filters (`sap_client`,
`inbound_delivery_categories_vbtyp`, and `ocean_incoterms` on
`inbound_delivery_insights`), critical material types
(`critical_material_types`), schedule variance buffers
(`schedule_variance_thresholds`), and severe weather cutoffs
(`weather_thresholds`) under each asset's `filters` block in
`inlineTableSettings` or a `tableSettings` file. For the complete list of
tunable thresholds and defaults, see
`src/data_modules/cortex_samples/sap_bdc/products/supplier_plant_weather_risk_insights/table_settings.default.yaml`.

Build and deploy the configured sample:

```bash
export CORTEX_CONFIG_FILE=config/config.yaml
uv run cortex-build-and-deploy --config $CORTEX_CONFIG_FILE
```

## Entity-relationship diagram

You can view the Entity-Relationship (ER) diagram in the module's `README.md` or
generate it from your compiled workspace using
[`uv run cortex-metadata erd`](https://docs.cloud.google.com/cortex/docs/uv-run-cortex-metadata.md.txt#erd):

```bash
export CORTEX_CONFIG_FILE=config/config.yaml
uv run cortex-metadata erd \
  --config $CORTEX_CONFIG_FILE \
  --module-id supplier_plant_weather_risk_insights
```

## Source code

Explore the SQL models, `manifest.yaml`, and annotations in the repository at
[`src/data_modules/cortex_samples/sap_bdc/products/supplier_plant_weather_risk_insights`](https://github.com/GoogleCloudPlatform/cortex-framework/tree/main/src/data_modules/cortex_samples/sap_bdc/products/supplier_plant_weather_risk_insights).

## Enable AI agents

After materializing `inbound_delivery_insights` and
`supplier_plant_weather_risk_insights` in BigQuery, you can
provision a natural-language data agent with
[`uv run cortex-create-ca-agents`](https://docs.cloud.google.com/cortex/docs/conversational-analytics.md.txt) or
build a custom data agent directly in BigQuery Studio
([Create data agents](https://docs.cloud.google.com/bigquery/docs/create-data-agents.md.txt)) to ask questions about
weather-exposed deliveries, delayed suppliers, and plant spend at risk.

## What's next

- [Solution samples overview](https://docs.cloud.google.com/cortex/docs/solution-samples/overview.md.txt)
- [Supplier Inbound Delivery Weather Risk Insights](https://docs.cloud.google.com/cortex/docs/solution-samples/supplier-inbound-delivery-weather-risk-insights.md.txt)
- [SAP Business Data Cloud integration](https://docs.cloud.google.com/cortex/docs/source-system-integration/sap-bdc.md.txt)
- [BigQuery sharing integration](https://docs.cloud.google.com/cortex/docs/source-system-integration/analytics-hub.md.txt)