# `Accrue.Billing.Query`
[🔗](https://github.com/szTheory/accrue/blob/accrue-v1.0.0/lib/accrue/billing/query.ex#L1)

Composable `Ecto.Query` fragments mirroring the
`Accrue.Billing.Subscription` predicates.

Every predicate in `Accrue.Billing.Subscription` has a matching query
fragment here so you can filter subscriptions in the database with the
same semantics as the in-memory predicates. Prefer these fragments over
direct `.status` comparisons in `where` clauses — the predicates on
`Accrue.Billing.Subscription` are the correct way to check subscription
state, as direct comparisons miss edge cases like `cancel_at_period_end`
and `ended_at` that the predicates cover.

All functions accept an optional queryable (default
`Accrue.Billing.Subscription`) and compose via `|>`:

    import Ecto.Query

    from(s in Subscription, where: s.customer_id == ^id)
    |> Accrue.Billing.Query.active()
    |> Repo.all()

# `active`

```elixir
@spec active(Ecto.Queryable.t()) :: Ecto.Query.t()
```

Subscriptions counted as active (includes `:trialing`).

# `canceled`

```elixir
@spec canceled(Ecto.Queryable.t()) :: Ecto.Query.t()
```

Subscriptions that are terminated (`:canceled`, `:incomplete_expired`, or any ended_at).

# `canceling`

```elixir
@spec canceling(Ecto.Queryable.t()) :: Ecto.Query.t()
```

Subscriptions that are `:active` with `cancel_at_period_end` set and a
period end still in the future — i.e. the cancel hasn't landed yet.

# `dunning_sweep_candidates`

```elixir
@spec dunning_sweep_candidates(pos_integer(), Ecto.Queryable.t()) :: Ecto.Query.t()
```

Subscriptions eligible for a dunning sweep tick: strictly
`:past_due`, with `past_due_since` older than the grace window, and
with no prior `dunning_sweep_attempted_at` stamp.

# `past_due`

```elixir
@spec past_due(Ecto.Queryable.t()) :: Ecto.Query.t()
```

Subscriptions that are past due or unpaid (dunning territory).

# `paused`

```elixir
@spec paused(Ecto.Queryable.t()) :: Ecto.Query.t()
```

Subscriptions that are paused (legacy `:paused` status or non-nil `pause_collection`).

# `trialing`

```elixir
@spec trialing(Ecto.Queryable.t()) :: Ecto.Query.t()
```

Subscriptions currently in trial.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
