Order information exporting in Excel-format

This commit is contained in:
2023-05-28 02:08:26 +03:00
parent 57da4eae55
commit 0f49dc2bf8
8 changed files with 155 additions and 1 deletions

View File

@@ -0,0 +1,74 @@
defmodule Osuuspuutarha.Orders.Parser do
def parse_boolean(flag) do
if flag == true do
"Kyllä"
else
"Ei"
end
end
@spec parse_order_type(:community | :elo | :everyother | :full) :: <<_::64, _::_*8>>
def parse_order_type(:full) do
"Joka viikko"
end
def parse_order_type(:everyother) do
"Joka toinen viikko"
end
def parse_order_type(:elo) do
"Elotilaus"
end
def parse_order_type(:community) do
"Osuuskuntatoiminta"
end
@spec parse_location(
:askainen
| :kirjakahvila
| :koroinen
| :livonsaari
| :naantali
| :ocean
| :raisio
| :viherlassila
) :: <<_::48, _::_*16>>
def parse_location(:koroinen) do
"Koroinen"
end
def parse_location(:ocean) do
"Ocean Spirit"
end
def parse_location(:raisio) do
"Raisio"
end
def parse_location(:naantali) do
"Naantali"
end
def parse_location(:viherlassila) do
"Viherlassila"
end
def parse_location(:kirjakahvila) do
"Kirjakahvila"
end
def parse_location(:askainen) do
"Askainen"
end
def parse_location(:livonsaari) do
"Livonsaari"
end
@spec parse_date(atom | %{:day => any, :month => any, :year => any, optional(any) => any}) ::
<<_::16, _::_*8>>
def parse_date(date) do
"#{date.day}.#{date.month}.#{date.year}"
end
end

View File

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

View File

@@ -15,6 +15,8 @@
</.modal>
<% end %>
<%= link "Lataa Excel-taulukkona", to: Routes.exports_order_path(@socket, :index), class: "btn btn-default" %>
<table>
<thead>
<tr>

View File

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

View 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