Wire Odoo invoices to billing view (Fixes #13)

This commit is contained in:
Timothée Floure 2021-01-20 18:17:16 +01:00
parent 808765ef5a
commit f762f5c030
Signed by: tfloure
GPG Key ID: 4502C902C00A1E12
3 changed files with 79 additions and 4 deletions

View File

@ -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

View File

@ -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

View File

@ -38,3 +38,28 @@
<%= submit "Change address" %>
</div>
<% end %>
<h2>Invoices</h2>
<table>
<thead>
<tr>
<th>#</th>
<th>Created On</th>
<th>Due on</th>
<th>Amount</th>
<th>State</th>
</tr>
</thead>
<tbody>
<%= for invoice <- @invoices do %>
<tr>
<td><%= invoice.display_name %></td>
<td><%= invoice.invoice_date %></td>
<td><%= invoice.invoice_date_due %></td>
<td><%= invoice.amount_total %></td>
<td><%= invoice.state %></td>
</tr>
<% end %>
</tbody>
</table>