-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbilling.ex
208 lines (152 loc) · 4.46 KB
/
billing.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
defmodule Fly.Billing do
@moduledoc """
The Billing context.
This file was generated using:
`mix phx.gen.context Billing Invoice invoices`
"""
import Ecto.Query, warn: false
alias Fly.Repo
alias Fly.Billing.Invoice
alias Fly.Billing.InvoiceItem
alias Fly.Organizations.Organization
defmodule BillingError do
defexception message: "billing error"
end
@doc """
Returns the list of invoices.
## Examples
iex> list_invoices()
[%Invoice{}, ...]
"""
def list_invoices(opts \\ []) do
preload = Keyword.get(opts, :preload, [:invoice_items])
from(Invoice, preload: ^preload)
|> Repo.all()
end
@doc """
Gets a single invoice.
Raises `Ecto.NoResultsError` if the Invoice does not exist.
## Examples
iex> get_invoice!(123)
%Invoice{}
iex> get_invoice!(456)
** (Ecto.NoResultsError)
"""
def get_invoice!(id, opts \\ []) do
preload = Keyword.get(opts, :preload, [])
from(i in Invoice, preload: ^preload)
|> Repo.get!(id)
end
@doc """
Gets a single invoice.
## Examples
iex> get_invoice!(123)
{:ok, %Invoice{}}
iex> get_invoice!(456)
{:error, %Ecto.NoResultsError{}}
"""
def get_invoice(id, opts \\ []) do
preload = Keyword.get(opts, :preload, [])
from(i in Invoice, preload: ^preload)
|> Repo.get(id)
|> case do
nil -> {:error, %Ecto.NoResultsError{message: "Not found"}}
invoice -> {:ok, invoice}
end
end
@doc """
Gets a single invoice by stripe invoice ID
## Examples
iex> find_invoice_by_stripe_id(123)
{:ok, %Invoice{}}
iex> find_invoice_by_stripe_id(456)
{:error, %Ecto.NoResultsError{}}
"""
def find_invoice_by_stripe_id(stripe_invoice_id, opts \\ []) do
preload = Keyword.get(opts, :preload, [])
from(i in Invoice, preload: ^preload)
|> where([i], i.stripe_id == ^stripe_invoice_id)
|> Repo.one()
|> case do
nil -> {:error, %Ecto.NoResultsError{message: "Invoice not found"}}
invoice -> {:ok, invoice}
end
end
@doc """
Creates a invoice.
## Examples
iex> create_invoice(%{field: value})
{:ok, %Invoice{}}
iex> create_invoice(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_invoice(%Organization{} = organization, attrs \\ %{}) do
%Invoice{}
|> Invoice.organization_changeset(organization, attrs)
|> Repo.insert()
end
@doc """
Updates a invoice.
## Examples
iex> update_invoice(invoice, %{field: new_value})
{:ok, %Invoice{}}
iex> update_invoice(invoice, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_invoice(%Invoice{} = invoice, attrs) do
invoice
|> Invoice.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a invoice.
## Examples
iex> delete_invoice(invoice)
{:ok, %Invoice{}}
iex> delete_invoice(invoice)
{:error, %Ecto.Changeset{}}
"""
def delete_invoice(%Invoice{} = invoice) do
Repo.delete(invoice)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking invoice changes.
## Examples
iex> change_invoice(invoice)
%Ecto.Changeset{data: %Invoice{}}
"""
def change_invoice(%Invoice{} = invoice, attrs \\ %{}) do
Invoice.changeset(invoice, attrs)
end
@doc """
Add Invoice Item to Invoice
"""
def create_invoice_item(%Invoice{} = invoice, attrs) do
InvoiceItem.invoice_changeset(invoice, %InvoiceItem{}, attrs)
|> Repo.insert()
end
@doc """
Returns the current (draft) invoice from the database.
This needs improvement, because currently the assumption is that simply
the latest invoice is the current one, but we'll need a status `field` and check
for current interval match to support multiple invoice case-scenarios.
## Examples
iex> get_current_invoice(%Oganization{id: 1})
%Invoice{}
"""
def get_current_invoice(org, opts \\ [])
def get_current_invoice(%Organization{id: org_id}, opts) do
get_current_invoice(org_id, opts)
end
def get_current_invoice(org_id, opts) do
preload = Keyword.get(opts, :preload, [:invoice_items])
from(inv in Invoice)
|> join(:left, [inv], org in assoc(inv, :organization))
# This is annoying, ordering by database record ID, but needed in absence of utc_datetime_usec field.
|> order_by([inv, org], desc: inv.inserted_at, desc: inv.id)
|> where([inv, org], org.id == ^org_id)
|> limit(1)
|> preload(^preload)
|> Repo.one()
end
end