Order information exporting in Excel-format
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
defmodule OsuuspuutarhaWeb.Exports.OrderController do
|
||||
use OsuuspuutarhaWeb, :controller
|
||||
|
||||
alias Osuuspuutarha.Orders
|
||||
|
||||
def index(conn, _params) do
|
||||
orders = Orders.list_orders()
|
||||
|
||||
conn
|
||||
|> put_resp_content_type("text/xlsx")
|
||||
|> put_resp_header("content-disposition", "attachment; filename=\"tilaukset.xlxs\"")
|
||||
|> render("tilaukset.xlsx", %{orders: orders})
|
||||
end
|
||||
end
|
||||
@@ -15,6 +15,8 @@
|
||||
</.modal>
|
||||
<% end %>
|
||||
|
||||
<%= link "Lataa Excel-taulukkona", to: Routes.exports_order_path(@socket, :index), class: "btn btn-default" %>
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
|
||||
@@ -45,6 +45,11 @@ defmodule OsuuspuutarhaWeb.Router do
|
||||
live "/tilaukset/:id/nayta/muokkaa", OrderLive.Show, :edit
|
||||
end
|
||||
|
||||
scope "/lataukset", as: :exports, alias: OsuuspuutarhaWeb.Exports do
|
||||
pipe_through :admin_browser
|
||||
resources "/tilaukset", OrderController, only: [:index]
|
||||
end
|
||||
|
||||
# Other scopes may use custom stacks.
|
||||
# scope "/api", OsuuspuutarhaWeb do
|
||||
# pipe_through :api
|
||||
|
||||
54
lib/osuuspuutarha_web/views/exports/order_view.ex
Normal file
54
lib/osuuspuutarha_web/views/exports/order_view.ex
Normal file
@@ -0,0 +1,54 @@
|
||||
defmodule OsuuspuutarhaWeb.Exports.OrderView do
|
||||
use OsuuspuutarhaWeb, :view
|
||||
|
||||
alias Elixlsx.{Workbook, Sheet}
|
||||
alias Osuuspuutarha.Orders.Parser
|
||||
|
||||
@header [
|
||||
"Etunimi",
|
||||
"Sukunimi",
|
||||
"Katuosoite",
|
||||
"Paikkakunta",
|
||||
"Postinumero",
|
||||
"Sähköposti",
|
||||
"Puhelin",
|
||||
"Jäsen",
|
||||
"Tilaustyyppi",
|
||||
"Jakopaikka",
|
||||
"Parilliset viikot",
|
||||
"Jaettu lasku",
|
||||
"Lisätty",
|
||||
"Päivitetty"
|
||||
]
|
||||
|
||||
def render("tilaukset.xlsx", %{orders: orders}) do
|
||||
report_generator(orders)
|
||||
|> Elixlsx.write_to_memory("tilaukset.xlsx")
|
||||
|> elem(1)
|
||||
|> elem(1)
|
||||
end
|
||||
|
||||
def report_generator(orders) do
|
||||
rows = orders |> Enum.map(&row(&1))
|
||||
%Workbook{sheets: [%Sheet{name: "Orders", rows: [@header] ++ rows}]}
|
||||
end
|
||||
|
||||
def row(order) do
|
||||
[
|
||||
order.fname,
|
||||
order.lname,
|
||||
order.address,
|
||||
order.city,
|
||||
order.pcode,
|
||||
order.email,
|
||||
order.phone,
|
||||
Parser.parse_boolean(order.is_member),
|
||||
Parser.parse_order_type(order.order_type),
|
||||
Parser.parse_location(order.location),
|
||||
Parser.parse_boolean(order.even_weeks),
|
||||
Parser.parse_boolean(order.split_invoice),
|
||||
Parser.parse_date(order.inserted_at),
|
||||
Parser.parse_date(order.updated_at)
|
||||
]
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user