diff --git a/.gitignore b/.gitignore index 08f1343..ce4c9a6 100644 --- a/.gitignore +++ b/.gitignore @@ -32,3 +32,6 @@ osuuspuutarha-*.tar npm-debug.log /assets/node_modules/ +# VS Code +settings.json + diff --git a/lib/osuuspuutarha/orders/parser.ex b/lib/osuuspuutarha/orders/parser.ex new file mode 100644 index 0000000..f2ddb18 --- /dev/null +++ b/lib/osuuspuutarha/orders/parser.ex @@ -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 diff --git a/lib/osuuspuutarha_web/controllers/exports/order_controller.ex b/lib/osuuspuutarha_web/controllers/exports/order_controller.ex new file mode 100644 index 0000000..5833865 --- /dev/null +++ b/lib/osuuspuutarha_web/controllers/exports/order_controller.ex @@ -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 diff --git a/lib/osuuspuutarha_web/live/order_live/index.html.heex b/lib/osuuspuutarha_web/live/order_live/index.html.heex index 817bad0..e954dbb 100644 --- a/lib/osuuspuutarha_web/live/order_live/index.html.heex +++ b/lib/osuuspuutarha_web/live/order_live/index.html.heex @@ -15,6 +15,8 @@ <% end %> +<%= link "Lataa Excel-taulukkona", to: Routes.exports_order_path(@socket, :index), class: "btn btn-default" %> + diff --git a/lib/osuuspuutarha_web/router.ex b/lib/osuuspuutarha_web/router.ex index d269417..b772f92 100644 --- a/lib/osuuspuutarha_web/router.ex +++ b/lib/osuuspuutarha_web/router.ex @@ -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 diff --git a/lib/osuuspuutarha_web/views/exports/order_view.ex b/lib/osuuspuutarha_web/views/exports/order_view.ex new file mode 100644 index 0000000..f3f2a23 --- /dev/null +++ b/lib/osuuspuutarha_web/views/exports/order_view.ex @@ -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 diff --git a/mix.exs b/mix.exs index 3a9774e..3857824 100644 --- a/mix.exs +++ b/mix.exs @@ -47,7 +47,8 @@ defmodule Osuuspuutarha.MixProject do {:telemetry_poller, "~> 1.0"}, {:gettext, "~> 0.18"}, {:jason, "~> 1.2"}, - {:plug_cowboy, "~> 2.5"} + {:plug_cowboy, "~> 2.5"}, + {:elixlsx, "~> 0.5.1"} ] end diff --git a/mix.lock b/mix.lock index c3abaa0..24d9c0a 100644 --- a/mix.lock +++ b/mix.lock @@ -8,6 +8,7 @@ "decimal": {:hex, :decimal, "2.0.0", "a78296e617b0f5dd4c6caf57c714431347912ffb1d0842e998e9792b5642d697", [:mix], [], "hexpm", "34666e9c55dea81013e77d9d87370fe6cb6291d1ef32f46a1600230b1d44f577"}, "ecto": {:hex, :ecto, "3.9.4", "3ee68e25dbe0c36f980f1ba5dd41ee0d3eb0873bccae8aeaf1a2647242bffa35", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "de5f988c142a3aa4ec18b85a4ec34a2390b65b24f02385c1144252ff6ff8ee75"}, "ecto_sql": {:hex, :ecto_sql, "3.9.2", "34227501abe92dba10d9c3495ab6770e75e79b836d114c41108a4bf2ce200ad5", [:mix], [{:db_connection, "~> 2.4.1 or ~> 2.5", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.9.2", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.6.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.16.0 or ~> 1.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.1 or ~> 2.2", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "1eb5eeb4358fdbcd42eac11c1fbd87e3affd7904e639d77903c1358b2abd3f70"}, + "elixlsx": {:hex, :elixlsx, "0.5.1", "3b4129c7059c7bd2ed4e2ff5f6c485410ab125058bee2e9de6e0eb8417176803", [:mix], [], "hexpm", "25d778d43ea1ae86df8dd20170842d324086c0cab40503eb484a3779a6e2254c"}, "esbuild": {:hex, :esbuild, "0.6.1", "a774bfa7b4512a1211bf15880b462be12a4c48ed753a170c68c63b2c95888150", [:mix], [{:castore, ">= 0.0.0", [hex: :castore, repo: "hexpm", optional: false]}], "hexpm", "569f7409fb5a932211573fc20e2a930a0d5cf3377c5b4f6506c651b1783a1678"}, "expo": {:hex, :expo, "0.4.0", "bbe4bf455e2eb2ebd2f1e7d83530ce50fb9990eb88fc47855c515bfdf1c6626f", [:mix], [], "hexpm", "a8ed1683ec8b7c7fa53fd7a41b2c6935f539168a6bb0616d7fd6b58a36f3abf2"}, "file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},