Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow negative cost_per_unit for geofences #968

Merged
merged 2 commits into from
Oct 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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