From f762f5c030f240da321e9fe7fdbcd3d56391aad7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Floure?= Date: Wed, 20 Jan 2021 18:17:16 +0100 Subject: [PATCH] Wire Odoo invoices to billing view (Fixes #13) --- lib/recycledcloud/billing/invoice.ex | 36 +++++++++++++++++++ .../controllers/billing_controller.ex | 22 +++++++++--- .../templates/billing/index.html.eex | 25 +++++++++++++ 3 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 lib/recycledcloud/billing/invoice.ex diff --git a/lib/recycledcloud/billing/invoice.ex b/lib/recycledcloud/billing/invoice.ex new file mode 100644 index 0000000..b783e7a --- /dev/null +++ b/lib/recycledcloud/billing/invoice.ex @@ -0,0 +1,36 @@ +defmodule RecycledCloud.Billing.Invoice do + alias RecycledCloud.Billing.{Partner, Invoice} + alias RecycledCloud.Odoo + + use Ecto.Schema + + embedded_schema do + field :display_name, :string + field :invoice_date, :date + field :invoice_date_due, :date + field :amount_total, :float + field :access_url, :string + field :state, :string + end + + def get_all_for(%Partner{} = partner) do + fields = ["id", "display_name", "invoice_date", "invoice_date_due", + "amount_total", "access_url", "state"] + result = Odoo.query([ + "account.move", + "search_read", + [[["commercial_partner_id", "=", partner.id]]], + %{"fields" => fields} + ]) + + structify = fn raw -> + args = raw |> Enum.map(fn {k, v} -> {String.to_atom(k), v} end) + struct(Invoice, args) + end + + case result do + {:ok, entries} -> entries |> Enum.map(structify) + _ -> nil + end + end +end diff --git a/lib/recycledcloud_web/controllers/billing_controller.ex b/lib/recycledcloud_web/controllers/billing_controller.ex index 69e7405..4cfe757 100644 --- a/lib/recycledcloud_web/controllers/billing_controller.ex +++ b/lib/recycledcloud_web/controllers/billing_controller.ex @@ -1,7 +1,7 @@ defmodule RecycledCloudWeb.BillingController do use RecycledCloudWeb, :controller - alias RecycledCloud.Billing.Partner + alias RecycledCloud.Billing.{Partner, Invoice} alias RecycledCloud.Repo def current_partner(conn) do @@ -12,8 +12,15 @@ defmodule RecycledCloudWeb.BillingController do def index(conn, _params) do partner = conn |> current_partner changeset = partner |> Partner.changeset(%{}) + invoices = partner |> Invoice.get_all_for - render(conn, "index.html", partner: partner, partner_changeset: changeset) + render( + conn, + "index.html", + partner: partner, + partner_changeset: changeset, + invoices: invoices + ) end def update(conn, %{"partner" => changes} = params) do @@ -26,8 +33,15 @@ defmodule RecycledCloudWeb.BillingController do |> put_flash(:info, "Billing Partner has been updated") |> redirect(to: Routes.billing_path(conn, :index)) {:error, changeset} -> - render(conn, "index.html", partner: partner, partner_changeset: - changeset) + + invoices = partner |> Invoice.get_all_for + render( + conn, + "index.html", + partner: partner, + partner_changeset: changeset, + invoices: invoices + ) end end end diff --git a/lib/recycledcloud_web/templates/billing/index.html.eex b/lib/recycledcloud_web/templates/billing/index.html.eex index 8fd9e9c..96a1f07 100644 --- a/lib/recycledcloud_web/templates/billing/index.html.eex +++ b/lib/recycledcloud_web/templates/billing/index.html.eex @@ -38,3 +38,28 @@ <%= submit "Change address" %> <% end %> + +

Invoices

+ + + + + + + + + + + + + <%= for invoice <- @invoices do %> + + + + + + + + <% end %> + +
#Created OnDue onAmountState
<%= invoice.display_name %><%= invoice.invoice_date %><%= invoice.invoice_date_due %><%= invoice.amount_total %><%= invoice.state %>