From c0c7cd5b770ef744e9b209f7911c80f5f8ba450a Mon Sep 17 00:00:00 2001 From: Adrian Date: Sun, 4 Oct 2020 11:27:36 +0200 Subject: [PATCH] Allow negative cost_per_unit for geofences (#968) * Allow negative cost_per_unit for geofences * Test cost calculation for negative cost Co-authored-by: Adrian Kumpf --- lib/teslamate/locations/geo_fence.ex | 1 - .../live/geofence_live/form.html.leex | 2 +- test/teslamate/locations/geofences_test.exs | 2 -- test/teslamate/log/log_charging_test.exs | 27 +++++++++++++++++++ 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/lib/teslamate/locations/geo_fence.ex b/lib/teslamate/locations/geo_fence.ex index 720693d5b9..14be6cd054 100644 --- a/lib/teslamate/locations/geo_fence.ex +++ b/lib/teslamate/locations/geo_fence.ex @@ -34,7 +34,6 @@ defmodule TeslaMate.Locations.GeoFence do ]) |> validate_required([:name, :latitude, :longitude, :radius]) |> validate_number(:radius, greater_than: 0, less_than: 5000) - |> validate_number(:cost_per_unit, greater_than_or_equal_to: 0) |> validate_number(:session_fee, greater_than_or_equal_to: 0) end end diff --git a/lib/teslamate_web/live/geofence_live/form.html.leex b/lib/teslamate_web/live/geofence_live/form.html.leex index 6a5032287d..3bab09d729 100644 --- a/lib/teslamate_web/live/geofence_live/form.html.leex +++ b/lib/teslamate_web/live/geofence_live/form.html.leex @@ -68,7 +68,7 @@

<%= text_input f, :cost_per_unit, class: "input", - type: :number, inputmode: :decimal, min: 0.0, step: 0.0001, + type: :number, inputmode: :decimal, step: 0.0001, placeholder: "0.00" %>

<%= if @show_errors do %> diff --git a/test/teslamate/locations/geofences_test.exs b/test/teslamate/locations/geofences_test.exs index 145cc8c018..5170090b4e 100644 --- a/test/teslamate/locations/geofences_test.exs +++ b/test/teslamate/locations/geofences_test.exs @@ -29,7 +29,6 @@ defmodule TeslaMate.LocationsGeofencesTest do longitude: nil, radius: nil, billing_type: :per_hour, - cost_per_unit: -0.01, session_fee: -0.01 } @@ -67,7 +66,6 @@ defmodule TeslaMate.LocationsGeofencesTest do name: ["can't be blank"], radius: ["can't be blank"], billing_type: ["is invalid"], - cost_per_unit: ["must be greater than or equal to 0"], session_fee: ["must be greater than or equal to 0"] } diff --git a/test/teslamate/log/log_charging_test.exs b/test/teslamate/log/log_charging_test.exs index 40eb027aec..ca3b00bdd3 100644 --- a/test/teslamate/log/log_charging_test.exs +++ b/test/teslamate/log/log_charging_test.exs @@ -543,6 +543,33 @@ defmodule TeslaMate.LogChargingTest do assert cproc.cost == Decimal.new("0.00") end + test "cost per unit can be negative" do + car = car_fixture() + + assert %GeoFence{id: id} = + geofence_fixture(%{ + latitude: 50.1121, + longitude: 11.597, + radius: 50, + cost_per_unit: -0.15, + session_fee: 0.0 + }) + + assert {:ok, cproc} = + log_charging_process(charges_fixture(:phases_nil), + car: car, + attrs: %{ + date: DateTime.utc_now(), + latitude: 50.112198, + longitude: 11.597669 + } + ) + + assert cproc.charge_energy_added == Decimal.from_float(12.77) + assert cproc.charge_energy_used == Decimal.from_float(12.46) + assert cproc.cost == Decimal.new("-1.92") + end + test "sets charge cost to zero if free supercharging is enabled" do alias TeslaMate.Settings