diff --git a/lib/simpleshop_theme/orders.ex b/lib/simpleshop_theme/orders.ex index e4fcbcc..855a335 100644 --- a/lib/simpleshop_theme/orders.ex +++ b/lib/simpleshop_theme/orders.ex @@ -10,6 +10,41 @@ defmodule SimpleshopTheme.Orders do alias SimpleshopTheme.Repo alias SimpleshopTheme.Orders.{Order, OrderItem} + @doc """ + Lists orders, optionally filtered by payment status. + + ## Options + + * `:status` - filter by payment_status ("paid", "pending", "failed", "refunded") + Pass nil or "all" to return all orders. + + Returns orders sorted by newest first, with items preloaded. + """ + def list_orders(opts \\ []) do + status = opts[:status] + + Order + |> maybe_filter_status(status) + |> order_by([o], desc: o.inserted_at) + |> preload(:items) + |> Repo.all() + end + + defp maybe_filter_status(query, nil), do: query + defp maybe_filter_status(query, "all"), do: query + defp maybe_filter_status(query, status), do: where(query, [o], o.payment_status == ^status) + + @doc """ + Returns a map of payment_status => count for all orders. + """ + def count_orders_by_status do + Order + |> group_by(:payment_status) + |> select([o], {o.payment_status, count(o.id)}) + |> Repo.all() + |> Map.new() + end + @doc """ Creates an order with line items from hydrated cart data. diff --git a/lib/simpleshop_theme_web/components/layouts/root.html.heex b/lib/simpleshop_theme_web/components/layouts/root.html.heex index a8bdedf..e5040ed 100644 --- a/lib/simpleshop_theme_web/components/layouts/root.html.heex +++ b/lib/simpleshop_theme_web/components/layouts/root.html.heex @@ -45,6 +45,9 @@
{@order.stripe_payment_intent_id}
+
+ <:item title="Currency">{String.upcase(@order.currency)}
+
+ No shipping address provided
+ <% end %> +| Product | +Variant | +Qty | +Unit price | +Total | +
|---|---|---|---|---|
| {item.product_name} | +{item.variant_title} | +{item.quantity} | +{Cart.format_price(item.unit_price)} | +{Cart.format_price(item.unit_price * item.quantity)} | +
| Subtotal | +{Cart.format_price(@order.subtotal)} | +|||
| Total | +{Cart.format_price(@order.total)} | +|||
No orders yet
+Orders will appear here once customers check out.
+