From 34f6d44b983473da9058b75f4a71b9b2c8480511 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Veikko=20Lintuj=C3=A4rvi?= Date: Sat, 18 May 2024 23:00:40 +0300 Subject: [PATCH] Excel export functionality for yields --- assets/css/live/yield.css | 13 +++++++ lib/osuuspuutarha/harvest/parser.ex | 14 ++++++++ .../controllers/exports/yield_controller.ex | 14 ++++++++ .../live/yield_live/index.html.heex | 4 ++- lib/osuuspuutarha_web/router.ex | 1 + .../views/exports/order_view.ex | 2 +- .../views/exports/yield_view.ex | 34 +++++++++++++++++++ 7 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 lib/osuuspuutarha_web/controllers/exports/yield_controller.ex create mode 100644 lib/osuuspuutarha_web/views/exports/yield_view.ex diff --git a/assets/css/live/yield.css b/assets/css/live/yield.css index 6a12396..0951858 100644 --- a/assets/css/live/yield.css +++ b/assets/css/live/yield.css @@ -7,3 +7,16 @@ .date-picker select { max-width: 8rem; } + +.new-yield-link a { + padding: 1em; +} + +.new-yield-link { + margin: 1.5em; + padding-top: 5px; + font-size: large; + font-weight: bold; + background-color: #e5e5ea; + border-radius: 5%; +} diff --git a/lib/osuuspuutarha/harvest/parser.ex b/lib/osuuspuutarha/harvest/parser.ex index bfa2bec..2e97955 100644 --- a/lib/osuuspuutarha/harvest/parser.ex +++ b/lib/osuuspuutarha/harvest/parser.ex @@ -114,4 +114,18 @@ defmodule Osuuspuutarha.Harvest.Parser do def parse_plant(:corn) do "Corn" 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 + + def parse_unit(:kg) do + "kg" + end + + def parse_unit(:kpl) do + "kpl" + end end diff --git a/lib/osuuspuutarha_web/controllers/exports/yield_controller.ex b/lib/osuuspuutarha_web/controllers/exports/yield_controller.ex new file mode 100644 index 0000000..39b3e66 --- /dev/null +++ b/lib/osuuspuutarha_web/controllers/exports/yield_controller.ex @@ -0,0 +1,14 @@ +defmodule OsuuspuutarhaWeb.Exports.YieldController do + use OsuuspuutarhaWeb, :controller + + alias Osuuspuutarha.Harvest + + def index(conn, _params) do + yields = Harvest.list_yields() + + conn + |> put_resp_content_type("text/xlsx") + |> put_resp_header("content-disposition", "attachment; filename=\"korjuut.xlsx\"") + |> render("korjuut.xlsx", %{yields: yields}) + end +end diff --git a/lib/osuuspuutarha_web/live/yield_live/index.html.heex b/lib/osuuspuutarha_web/live/yield_live/index.html.heex index 8639830..e5e8596 100644 --- a/lib/osuuspuutarha_web/live/yield_live/index.html.heex +++ b/lib/osuuspuutarha_web/live/yield_live/index.html.heex @@ -13,7 +13,9 @@ <% end %> -<%= live_patch "New Yield", to: Routes.yield_index_path(@socket, :new) %> +<%= live_patch "New Yield", to: Routes.yield_index_path(@socket, :new) %> + +<%= link "Download as Excel", to: Routes.exports_yield_path(@socket, :index) %> diff --git a/lib/osuuspuutarha_web/router.ex b/lib/osuuspuutarha_web/router.ex index 60da8f4..9be1b58 100644 --- a/lib/osuuspuutarha_web/router.ex +++ b/lib/osuuspuutarha_web/router.ex @@ -58,6 +58,7 @@ defmodule OsuuspuutarhaWeb.Router do scope "/lataukset", as: :exports, alias: OsuuspuutarhaWeb.Exports do pipe_through :admin_browser resources "/tilaukset", OrderController, only: [:index] + resources "/korjuut", YieldController, only: [:index] end # Other scopes may use custom stacks. diff --git a/lib/osuuspuutarha_web/views/exports/order_view.ex b/lib/osuuspuutarha_web/views/exports/order_view.ex index 86b801e..6708bc7 100644 --- a/lib/osuuspuutarha_web/views/exports/order_view.ex +++ b/lib/osuuspuutarha_web/views/exports/order_view.ex @@ -31,7 +31,7 @@ defmodule OsuuspuutarhaWeb.Exports.OrderView do def report_generator(orders) do rows = orders |> Enum.map(&row(&1)) - %Workbook{sheets: [%Sheet{name: "Orders", rows: [@header] ++ rows}]} + %Workbook{sheets: [%Sheet{name: "Tilaukset", rows: [@header] ++ rows}]} end def row(order) do diff --git a/lib/osuuspuutarha_web/views/exports/yield_view.ex b/lib/osuuspuutarha_web/views/exports/yield_view.ex new file mode 100644 index 0000000..22ecbdb --- /dev/null +++ b/lib/osuuspuutarha_web/views/exports/yield_view.ex @@ -0,0 +1,34 @@ +defmodule OsuuspuutarhaWeb.Exports.YieldView do + use OsuuspuutarhaWeb, :view + + alias Elixlsx.{Workbook, Sheet} + alias Osuuspuutarha.Harvest.Parser + + @header [ + "Määrä", + "Päivämäärä", + "Kasvi", + "Yksikkö" + ] + + def render("korjuut.xlsx", %{yields: yields}) do + report_generator(yields) + |> Elixlsx.write_to_memory("korjuut.xlsx") + |> elem(1) + |> elem(1) + end + + def report_generator(yields) do + rows = yields |> Enum.map(&row(&1)) + %Workbook{sheets: [%Sheet{name: "Korjuut", rows: [@header] ++ rows}]} + end + + def row(yield) do + [ + Decimal.to_string(yield.amount), + Parser.parse_date(yield.date), + Parser.parse_plant(yield.plant), + Parser.parse_unit(yield.unit) + ] + end +end