2021-01-20 17:28:48 +01:00
|
|
|
defmodule RecycledCloudWeb.BillingController do
|
|
|
|
use RecycledCloudWeb, :controller
|
|
|
|
|
2021-01-20 18:17:16 +01:00
|
|
|
alias RecycledCloud.Billing.{Partner, Invoice}
|
2021-01-20 17:28:48 +01:00
|
|
|
alias RecycledCloud.Repo
|
|
|
|
|
|
|
|
def current_partner(conn) do
|
|
|
|
user = conn.assigns.current_user
|
|
|
|
Partner.get_or_create(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def index(conn, _params) do
|
|
|
|
partner = conn |> current_partner
|
|
|
|
changeset = partner |> Partner.changeset(%{})
|
2021-01-20 18:17:16 +01:00
|
|
|
invoices = partner |> Invoice.get_all_for
|
2021-01-20 17:28:48 +01:00
|
|
|
|
2021-01-20 18:17:16 +01:00
|
|
|
render(
|
|
|
|
conn,
|
|
|
|
"index.html",
|
|
|
|
partner: partner,
|
|
|
|
partner_changeset: changeset,
|
|
|
|
invoices: invoices
|
|
|
|
)
|
2021-01-20 17:28:48 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def update(conn, %{"partner" => changes} = params) do
|
|
|
|
partner = conn |> current_partner
|
|
|
|
changeset = partner |> Partner.changeset(changes)
|
|
|
|
|
|
|
|
case Partner.apply(changeset) do
|
|
|
|
{:ok, _} ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:info, "Billing Partner has been updated")
|
|
|
|
|> redirect(to: Routes.billing_path(conn, :index))
|
|
|
|
{:error, changeset} ->
|
2021-01-20 18:17:16 +01:00
|
|
|
|
|
|
|
invoices = partner |> Invoice.get_all_for
|
|
|
|
render(
|
|
|
|
conn,
|
|
|
|
"index.html",
|
|
|
|
partner: partner,
|
|
|
|
partner_changeset: changeset,
|
|
|
|
invoices: invoices
|
|
|
|
)
|
2021-01-20 17:28:48 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|