diff --git a/assets/js/charts.js b/assets/js/charts.js index 00b12cd..9f17df1 100644 --- a/assets/js/charts.js +++ b/assets/js/charts.js @@ -3,12 +3,14 @@ import $ from "jquery"; let delta = 'M'; let graph_type = "sum_invoices"; -let time = 7; +let time = 365; let is_count = true; let chart = null; +let prev_chart = null; +let pie_chart = null; function lineChart(label_values, result){ - if(chart != null){ + if (chart != null) { chart.destroy(); } @@ -27,10 +29,14 @@ function lineChart(label_values, result){ } function barChart(label_values, data){ + if (prev_chart != null) { + prev_chart.destroy(); + } + $("#progress").hide(); $("#prevision").show(); - new Chart(document.getElementById("lineChartPrevisions"), { + prev_chart = new Chart(document.getElementById("lineChartPrevisions"), { type: 'bar', data: { labels: label_values, @@ -48,9 +54,13 @@ function barChart(label_values, data){ } function pieChart(costs, earnings) { + if (pie_chart != null) { + pie_chart.destroy(); + } + $("#pieChartError").hide(); - new Chart(document.getElementById("pieChart"), { + pie_chart = new Chart(document.getElementById("pieChart"), { type: 'doughnut', responsive: true, mantainAspectRatio: false, @@ -160,7 +170,7 @@ function graphicTotals(data) { gains = data.gains_values[index]; } }); - + pieChart(costs, gains); } diff --git a/lib/infin/companies.ex b/lib/infin/companies.ex index 4fcccbb..66e9cea 100644 --- a/lib/infin/companies.ex +++ b/lib/infin/companies.ex @@ -55,7 +55,10 @@ defmodule Infin.Companies do """ def get_company_by_nif!(nif), do: Repo.get_by!(Company, nif: nif) - def get_company_by_nif(nif), do: Repo.get_by(Company, nif: nif) + def get_company_by_nif(nif) do + query = from(c in Company, where: c.nif == ^nif, preload: :users) + Repo.one(query) + end @doc """ Creates a company. diff --git a/lib/infin_web/controllers/budget_controller.ex b/lib/infin_web/controllers/budget_controller.ex index a61d342..b06aba5 100644 --- a/lib/infin_web/controllers/budget_controller.ex +++ b/lib/infin_web/controllers/budget_controller.ex @@ -47,6 +47,12 @@ defmodule InfinWeb.BudgetController do budget_params = Map.replace!(budget_params, "value", value) + if is_nil(budget_params["category_id"]) do + conn + |> put_flash(:error, "Please associate a category to the budget.") + |> redirect(to: Routes.budget_path(conn, :new)) + end + budget_params = Map.replace!( budget_params, diff --git a/lib/infin_web/controllers/user_registration_controller.ex b/lib/infin_web/controllers/user_registration_controller.ex index c108b04..0640dfe 100644 --- a/lib/infin_web/controllers/user_registration_controller.ex +++ b/lib/infin_web/controllers/user_registration_controller.ex @@ -4,6 +4,7 @@ defmodule InfinWeb.UserRegistrationController do alias Infin.Accounts alias Infin.Accounts.User alias InfinWeb.UserAuth + alias Infin.Companies def new(conn, _params) do changeset = Accounts.change_user_registration(%User{}) @@ -11,6 +12,25 @@ defmodule InfinWeb.UserRegistrationController do end def create(conn, %{"user" => user_params}) do + company = Companies.get_company_by_nif(user_params["company"]["nif"]) + if (not is_nil(company)) and company.users == [] do + user_params = Map.put(user_params, "company_id", company.id) + case Accounts.add_user(user_params) do + {:ok, user} -> + {:ok, _} = + Accounts.deliver_user_confirmation_instructions( + user, + &Routes.user_confirmation_url(conn, :confirm, &1) + ) + + conn + |> put_flash(:info, "User created successfully.") + |> UserAuth.log_in_user(user) + + {:error, %Ecto.Changeset{} = changeset} -> + render(conn, "new.html", changeset: changeset) + end + end case Accounts.register_user(user_params) do {:ok, user} -> {:ok, _} = diff --git a/lib/infin_web/live/dashboard/graph.html.leex b/lib/infin_web/live/dashboard/graph.html.leex index ef6cafe..7b5043b 100644 --- a/lib/infin_web/live/dashboard/graph.html.leex +++ b/lib/infin_web/live/dashboard/graph.html.leex @@ -39,7 +39,7 @@
-

Year Costs and earnings

+

Year Costs and Earnings


No data available