36 lines
914 B
Elixir
36 lines
914 B
Elixir
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
|