Skip to main content

Columns

ColumnDescriptionAPI Field Name
manual_discount_history_idPrimary key and manual discount history event unique identifier.
is_deletedIndicates whether the manual discount history record is deleted.
event_datetimeThe timestamp when the history event occurred.event_datetime
action_kindThe type of action that produced this history entry (e.g. CREATE, UPDATE, DELETE).action_kind
acting_user_idForeign key to the user table. The identifier of the user who performed the action.acting_user_id
manual_discount_idForeign key to the promotion_manual_discounts table. The identifier of the manual discount this history entry is tracking.manual_discount_id
invoice_idForeign key to the invoice & invoice_enriched table. The identifier of the invoice the manual discount was applied to, if applied to a whole invoice.invoice_id
invoice_item_idForeign key to the invoice_item table. The identifier of the invoice item the manual discount was applied to, if applied to a specific item.invoice_item_id
percent_valueThe percentage value of the manual discount at the time of this event, if applicable.percent_value
dollar_valueThe dollar value of the manual discount at the time of this event, if applicable. Snowflake displays dollars, API returns cents.cents_value
reasonThe reason for applying the manual discount at the time of this event.reason
last_updatedThe timestamp when the manual discount history record was last updated in the source table. This is the most recent timestamp from all related tables.
vetcove_corporate_idThe unique identifier of the corporate group associated with this record.

Relationships

SQL Definition

/*
    This model provides a business-ready, denormalized view of manual discount history data
    pre-joined for easy consumption, mirroring the invoice manual discount history API.

    Filtering logic mirrors the API: history is only returned for discounts where
    the promotion manual discount is not deleted.
*/

"
    )
}}

with manual_discount_history as (
    select * from {{ ref('promotion_manual_discount_history') }}
)

, current_discounts as (
    select * from {{ ref('promotion_manual_discounts') }}
    where
        is_deleted = false
)

select
    manual_discount_history.id as manual_discount_history_id
    , manual_discount_history.is_deleted
    , manual_discount_history.event_datetime
    , manual_discount_history.action_kind
    , manual_discount_history.acting_user_id
    , manual_discount_history.promotion_manual_discount_id as manual_discount_id
    -- Invoice ID populates when promotion is applied to a whole invoice (otherwise 0)
    , manual_discount_history.invoice_id
    -- Invoice Item ID populates when promotion is applied to a specific invoice item (otherwise 0)
    , manual_discount_history.invoice_item_id
    , manual_discount_history.percent_value
    , manual_discount_history.dollar_value
    , manual_discount_history.reason
    , greatest(
        coalesce(manual_discount_history.last_updated, '1900-01-01')
        , coalesce(current_discounts.last_updated, '1900-01-01')
    ) as last_updated
    , manual_discount_history.vetcove_corporate_id
from manual_discount_history
inner join current_discounts
    on manual_discount_history.promotion_manual_discount_id = current_discounts.id