Skip to content

Commit

Permalink
Allow negative cost_per_unit for geofences (#968)
Browse files Browse the repository at this point in the history
* Allow negative cost_per_unit for geofences

* Test cost calculation for negative cost

Co-authored-by: Adrian Kumpf <[email protected]>
  • Loading branch information
ayonix and adriankumpf authored Oct 4, 2020
1 parent 6b19a3b commit c0c7cd5
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
1 change: 0 additions & 1 deletion lib/teslamate/locations/geo_fence.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/teslamate_web/live/geofence_live/form.html.leex
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
</p>
<p class="control is-expanded">
<%= 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" %>
</p>
<%= if @show_errors do %>
Expand Down
2 changes: 0 additions & 2 deletions test/teslamate/locations/geofences_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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"]
}

Expand Down
27 changes: 27 additions & 0 deletions test/teslamate/log/log_charging_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit c0c7cd5

Please sign in to comment.