Skip to content

Commit

Permalink
Fix: Various Issues with Payments / Discounts on Payments
Browse files Browse the repository at this point in the history
  • Loading branch information
dnelyk committed Apr 13, 2023
1 parent ddf3c37 commit 8e09e76
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 74 deletions.
68 changes: 17 additions & 51 deletions client/electric_cl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ if Config.ElectricVehicleCharging then
RegisterNetEvent('cdn-electric:client:OpenContextMenu', function(total, fuelamounttotal, purchasetype)
lib.registerContext({
id = 'electricconfirmationmenu',
title = Lang:t("menu_purchase_station_header_1")..total..Lang:t("menu_purchase_station_header_2"),
title = Lang:t("menu_purchase_station_header_1")..math.ceil(total)..Lang:t("menu_purchase_station_header_2"),
options = {
{
title = Lang:t("menu_purchase_station_confirm_header"),
Expand Down Expand Up @@ -77,36 +77,6 @@ if Config.ElectricVehicleCharging then
end)
end

RegisterNetEvent('cdn-electric:client:OpenContextMenu', function(total, fuelamounttotal, purchasetype)
lib.registerContext({
id = 'electricconfirmationmenu',
title = Lang:t("menu_purchase_station_header_1")..total..Lang:t("menu_purchase_station_header_2"),
options = {
{
title = Lang:t("menu_purchase_station_confirm_header"),
description = Lang:t("menu_electric_accept"),
icon = "fas fa-check-circle",
arrow = false, -- puts arrow to the right
event = 'cdn-fuel:client:electric:ChargeVehicle',
args = {
fuelamounttotal = fuelamounttotal,
purchasetype = purchasetype,
}
},
{
title = Lang:t("menu_header_close"),
description = Lang:t("menu_refuel_cancel"),
icon = "fas fa-times-circle",
arrow = false, -- puts arrow to the right
onSelect = function()
lib.hideContext()
end,
},
},
})
lib.showContext('electricconfirmationmenu')
end)

RegisterNetEvent('cdn-fuel:client:electric:FinalMenu', function(purchasetype)
local money = nil
if purchasetype == "bank" then money = QBCore.Functions.GetPlayerData().money['bank'] elseif purchasetype == 'cash' then money = QBCore.Functions.GetPlayerData().money['cash'] end
Expand Down Expand Up @@ -150,7 +120,7 @@ if Config.ElectricVehicleCharging then
end
else
discount = discount / 100
FuelPrice = FuelPrice - math.ceil(FuelPrice*discount)
FuelPrice = FuelPrice - (FuelPrice*discount)

if Config.FuelDebug then
print("Your discount for Emergency Services is set @ "..discount.."%. Setting new price to: $"..FuelPrice)
Expand All @@ -169,8 +139,8 @@ if Config.ElectricVehicleCharging then
if curfuel < 10 then finalfuel = string.sub(curfuel, 1, 1) else finalfuel = string.sub(curfuel, 1, 2) end
local maxfuel = (100 - finalfuel - 1)
local wholetankcost = (FuelPrice * maxfuel)
local wholetankcostwithtax = math.ceil(FuelPrice * maxfuel + GlobalTax(wholetankcost))

local wholetankcostwithtax = math.ceil((wholetankcost) + GlobalTax(wholetankcost))
if Config.FuelDebug then print("Attempting to open Input with the total: $"..wholetankcostwithtax.." at $"..FuelPrice.." / L".." Maximum Fuel Amount: "..maxfuel) end
if Config.Ox.Input then
Electricity = lib.inputDialog('Electric Charger', {
{ type = "input", label = 'Electric Price',
Expand Down Expand Up @@ -199,15 +169,13 @@ if Config.ElectricVehicleCharging then
QBCore.Functions.Notify(Lang:t("tank_already_full"), "error")
else
if GlobalTax(ElectricityAmount * FuelPrice) + (ElectricityAmount * FuelPrice) <= money then
local totalcost = (ElectricityAmount * FuelPrice)
TriggerServerEvent('cdn-fuel:server:electric:OpenMenu', totalcost, IsInGasStation(), false, purchasetype, FuelPrice)
TriggerServerEvent('cdn-fuel:server:electric:OpenMenu', ElectricityAmount, IsInGasStation(), false, purchasetype, FuelPrice)
else
QBCore.Functions.Notify(Lang:t("not_enough_money"), 'error', 7500)
end
end
end
else

Electricity = exports['qb-input']:ShowInput({
header = "Select the Amount of Fuel<br>Current Price: $" ..
FuelPrice .. " / KWh <br> Current Charge: " .. finalfuel .. " KWh <br> Full Charge Cost: $" ..
Expand Down Expand Up @@ -351,9 +319,9 @@ if Config.ElectricVehicleCharging then
end)

RegisterNetEvent('cdn-fuel:client:electric:ChargeVehicle', function(data)
if Config.FuelDebug then print("Charging Vehicle") end
if Config.FuelDebug then print("Charging Vehicle") end
if not Config.RenewedPhonePayment then
purchasetypeq = data.purchasetype
purchasetype = data.purchasetype
elseif data.purchasetype == "cash" then
purchasetype = "cash"
else
Expand All @@ -371,7 +339,7 @@ if Config.ElectricVehicleCharging then
amount = tonumber(amount)
if amount < 1 then return end
if amount < 10 then fuelamount = string.sub(amount, 1, 1) else fuelamount = string.sub(amount, 1, 2) end

local FuelPrice = (Config.ElectricChargingPrice * 1)
local vehicle = QBCore.Functions.GetClosestVehicle()

-- Police Discount Math --
Expand Down Expand Up @@ -411,7 +379,7 @@ if Config.ElectricVehicleCharging then
end
else
discount = discount / 100
FuelPrice = FuelPrice - math.ceil(FuelPrice*discount)
FuelPrice = FuelPrice - (FuelPrice*discount)

if Config.FuelDebug then
print("Your discount for Emergency Services is set @ "..discount.."%. Setting new price to: $"..FuelPrice)
Expand All @@ -425,7 +393,7 @@ if Config.ElectricVehicleCharging then
end
end

local refillCost = (amount * FuelPrice)
local refillCost = (fuelamount * FuelPrice) + GlobalTax(fuelamount*FuelPrice)
local vehicle = QBCore.Functions.GetClosestVehicle()
local ped = PlayerPedId()
local time = amount * Config.RefuelTime
Expand All @@ -452,10 +420,10 @@ if Config.ElectricVehicleCharging then
Refuelamount = Refuelamount + 1
if Cancelledrefuel then
local finalrefuelamount = math.floor(Refuelamount)
local refillCost = (finalrefuelamount * FuelPrice)
local refillCost = (finalrefuelamount * FuelPrice) + GlobalTax(finalrefuelamount * FuelPrice)
if Config.RenewedPhonePayment and purchasetype == "bank" then
local remainingamount = (amount - Refuelamount)
MoneyToGiveBack = (GlobalTax(remainingamount) + (remainingamount * FuelPrice))
MoneyToGiveBack = (GlobalTax(remainingamount * FuelPrice) + (remainingamount * FuelPrice))
TriggerServerEvent("cdn-fuel:server:phone:givebackmoney", MoneyToGiveBack)
else
TriggerServerEvent('cdn-fuel:server:PayForFuel', refillCost, purchasetype, FuelPrice)
Expand Down Expand Up @@ -491,15 +459,13 @@ if Config.ElectricVehicleCharging then
}) then
refueling = false
if purchasetype == "cash" then
TriggerServerEvent('cdn-fuel:server:PayForFuel', refillCost, purchasetype, FuelPrice)
TriggerServerEvent('cdn-fuel:server:PayForFuel', refillCost, purchasetype, FuelPrice, true)
elseif purchasetype == "bank" then
if Config.NPWD then
local tax = GlobalTax(refillCost)
local totalPayment = math.ceil(refillCost + tax)
exports["npwd"]:createNotification({ -- You can change this export to your own notification
notisId = "npwd:electricityPaidFor",
appId = "BANK",
content = "You have paid $"..totalPayment.." for electric at $"..FuelPrice.." per KWh + tax",
content = "You have paid $"..refillCost.." for electric at $"..FuelPrice.." per KWh + tax",
secondaryTitle = "New Transaction",
keepOpen = false,
duration = 15000,
Expand Down Expand Up @@ -536,7 +502,7 @@ if Config.ElectricVehicleCharging then
disableCombat = true,
}, {}, {}, {}, function()
refueling = false
if not Config.RenewedPhonePayment or purchasetype == 'cash' then TriggerServerEvent('cdn-fuel:server:PayForFuel', refillCost, purchasetype, FuelPrice) end
if not Config.RenewedPhonePayment or purchasetype == 'cash' then TriggerServerEvent('cdn-fuel:server:PayForFuel', refillCost, purchasetype, FuelPrice, true) end
local curfuel = GetFuel(vehicle)
local finalfuel = (curfuel + fuelamount)
if finalfuel > 99 and finalfuel < 100 then
Expand Down Expand Up @@ -629,7 +595,7 @@ if Config.ElectricVehicleCharging then
local purchasetype = "bank"
local fuelamounttotal = tonumber(RefuelPossibleAmount)
if Config.FuelDebug then print("Attempting to charge vehicle.") end
TriggerEvent('cdn-fuel:client:electric:ChargeVehicle', purchasetype, fuelamounttotal)
TriggerEvent('cdn-fuel:client:electric:ChargeVehicle', purchasetype, fuelamounttotal)
else
QBCore.Functions.Notify(Lang:t("electric_more_than_zero"), 'error', 7500)
end
Expand Down Expand Up @@ -682,7 +648,7 @@ if Config.ElectricVehicleCharging then
end
else
discount = discount / 100
FuelPrice = FuelPrice - math.ceil(FuelPrice*discount)
FuelPrice = FuelPrice - (FuelPrice*discount)

if Config.FuelDebug then
print("Your discount for Emergency Services is set @ "..discount.."%. Setting new price to: $"..FuelPrice)
Expand Down
52 changes: 32 additions & 20 deletions client/fuel_cl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ if Config.RenewedPhonePayment then
if Config.FuelDebug then
print(FuelPrice, FuelPrice*discount)
end
FuelPrice = FuelPrice - FuelPrice*discount
FuelPrice = FuelPrice - (FuelPrice*discount)

if Config.FuelDebug then
print("Your discount for Emergency Services is set @ "..discount.."%. Setting new price to: $"..FuelPrice)
Expand Down Expand Up @@ -371,7 +371,7 @@ if Config.Ox.Menu then
if Config.FuelDebug then print("OpenContextMenu for OX sent from server.") end
lib.registerContext({
id = 'cdnconfirmationmenu',
title = Lang:t("menu_purchase_station_header_1")..total..Lang:t("menu_purchase_station_header_2"),
title = Lang:t("menu_purchase_station_header_1")..math.ceil(total)..Lang:t("menu_purchase_station_header_2"),
options = {
{
title = Lang:t("menu_purchase_station_confirm_header"),
Expand Down Expand Up @@ -628,11 +628,12 @@ RegisterNetEvent('cdn-fuel:client:FinalMenu', function(purchasetype)
end
if shouldRecieveDiscount then
local discount = Config.EmergencyServicesDiscount['discount']
if discount > 100 then
discount = 100
else
if discount > 100 then
discount = 100
else
if discount <= 0 then discount = 0 end
end
if Config.FuelDebug then print("Before we apply the discount the FuelPrice is: $"..FuelPrice) end
if discount ~= 0 then
if discount == 100 then
FuelPrice = 0
Expand All @@ -641,7 +642,10 @@ RegisterNetEvent('cdn-fuel:client:FinalMenu', function(purchasetype)
end
else
discount = discount / 100
FuelPrice = FuelPrice - math.ceil(FuelPrice*discount)
if Config.FuelDebug then
print("Math( Current Fuel Price: "..FuelPrice.. " - " ..FuelPrice * discount.. "<<-- FuelPrice * Discount)")
end
FuelPrice = (FuelPrice) - (FuelPrice*discount)
if Config.FuelDebug then
print("Your discount for Emergency Services is set @ "..discount.."%. Setting new price to: $"..FuelPrice)
end
Expand Down Expand Up @@ -701,7 +705,7 @@ RegisterNetEvent('cdn-fuel:client:FinalMenu', function(purchasetype)
QBCore.Functions.Notify(Lang:t("tank_cannot_fit"), "error")
else
if GlobalTax(fuelAmount * FuelPrice) + (fuelAmount * FuelPrice) <= money then
TriggerServerEvent('cdn-fuel:server:OpenMenu', fuel.amount, inGasStation, false, purchasetype, tonumber(FuelPrice))
TriggerServerEvent('cdn-fuel:server:OpenMenu', fuelAmount, inGasStation, false, purchasetype, tonumber(FuelPrice))
else
QBCore.Functions.Notify(Lang:t("not_enough_money"), 'error', 7500)
end
Expand Down Expand Up @@ -768,6 +772,9 @@ RegisterNetEvent('cdn-fuel:client:FinalMenu', function(purchasetype)
QBCore.Functions.Notify(Lang:t("tank_cannot_fit"), "error")
else
if GlobalTax(fuel.amount * FuelPrice) + (fuel.amount * FuelPrice) <= money then
if Config.FuelDebug then
print("Player is getting "..fuel.amount.."L of Fuel @ "..FuelPrice..'/L, Total Cost: '..GlobalTax(fuel.amount * FuelPrice) + (fuel.amount * FuelPrice))
end
TriggerServerEvent('cdn-fuel:server:OpenMenu', fuel.amount, inGasStation, false, purchasetype, tonumber(FuelPrice))
else
QBCore.Functions.Notify(Lang:t("not_enough_money"), 'error', 7500)
Expand Down Expand Up @@ -941,20 +948,25 @@ RegisterNetEvent('cdn-fuel:client:RefuelVehicle', function(data)
end
if shouldRecieveDiscount then
local discount = Config.EmergencyServicesDiscount['discount']
if discount > 100 then
discount = 100
else
if discount > 100 then
discount = 100
else
if discount <= 0 then discount = 0 end
end
if Config.FuelDebug then print("Before we apply the discount the FuelPrice is: $"..FuelPrice) end
if discount ~= 0 then
if discount == 100 then
FuelPrice = 0
if Config.FuelDebug then
print("Your discount for Emergency Services is set @ "..discount.."% so fuel is free!")
print("Your discount for Emergency Services is set @ | "..discount.."% | so fuel is free!")
end
else
discount = discount / 100
FuelPrice = FuelPrice - math.ceil(FuelPrice*discount)
if Config.FuelDebug then
print("Math( Current Fuel Price: "..FuelPrice.. " - " ..FuelPrice * discount.. "<<-- FuelPrice * Discount)")
end

FuelPrice = FuelPrice - (FuelPrice*discount)

if Config.FuelDebug then
print("Your discount for Emergency Services is set @ "..discount.."%. Setting new price to: $"..FuelPrice)
Expand All @@ -967,15 +979,15 @@ RegisterNetEvent('cdn-fuel:client:RefuelVehicle', function(data)
end
end
end
local refillCost = (amount * FuelPrice)
local refillCost = (amount * FuelPrice) + GlobalTax(amount * FuelPrice)
local ped = PlayerPedId()
local time = amount * Config.RefuelTime
if amount < 10 then time = 10 * Config.RefuelTime end
local vehicleCoords = GetEntityCoords(vehicle)
if inGasStation then
if IsPlayerNearVehicle() then
RequestAnimDict(Config.RefuelAnimationDictionary)
while not HasAnimDictLoaded('timetable@gardener@filling_can') do Wait(100) end
while not HasAnimDictLoaded(Config.RefuelAnimationDictionary) do Wait(100) end
if GetIsVehicleEngineRunning(vehicle) and Config.VehicleBlowUp then
local Chance = math.random(1, 100)
if Chance <= Config.BlowUpChance then
Expand All @@ -1002,7 +1014,7 @@ RegisterNetEvent('cdn-fuel:client:RefuelVehicle', function(data)
Refuelamount = Refuelamount + 1
if Cancelledrefuel then
local finalrefuelamount = math.floor(Refuelamount)
local refillCost = (finalrefuelamount * FuelPrice)
local refillCost = (finalrefuelamount * FuelPrice) + GlobalTax(finalrefuelamount * FuelPrice)
if Config.RenewedPhonePayment and purchasetype == "bank" then
local remainingamount = (amount - Refuelamount)
MoneyToGiveBack = (GlobalTax(remainingamount * RefuelCancelledFuelCost) + (remainingamount * RefuelCancelledFuelCost))
Expand Down Expand Up @@ -1051,12 +1063,10 @@ RegisterNetEvent('cdn-fuel:client:RefuelVehicle', function(data)
TriggerServerEvent('cdn-fuel:server:PayForFuel', refillCost, purchasetype, FuelPrice)
elseif purchasetype == "bank" then
if Config.NPWD then
local tax = GlobalTax(refillCost)
local totalPayment = math.ceil(refillCost + tax)
exports["npwd"]:createNotification({ -- You can change this export to your own notification
notisId = "npwd:fuelPaidFor",
appId = "BANK",
content = "You have paid $"..totalPayment.." for fuel at $"..FuelPrice.." Per Liter + tax",
content = "You have paid $"..refillCost.." for fuel at $"..FuelPrice.." Per Liter + tax",
secondaryTitle = "New Transaction",
keepOpen = false,
duration = 15000,
Expand Down Expand Up @@ -1542,7 +1552,8 @@ RegisterNetEvent('cdn-fuel:jerrycan:refueljerrycan', function(data)
else
if Config.FuelDebug then print("Config.PlayerOwnedGasStationsEnabled == false or Config.UnlimitedFuel == true, this means reserves will not be changed.") end
end
TriggerServerEvent('cdn-fuel:server:PayForFuel', tonumber(refuelAmount) * FuelPrice, "cash", FuelPrice)
local total = (tonumber(refuelAmount) * FuelPrice) + GlobalTax(tonumber(refuelAmount) * FuelPrice)
TriggerServerEvent('cdn-fuel:server:PayForFuel', total, "cash", FuelPrice)
else
SetEntityVisible(fuelnozzle, true, 0)
DeleteObject(JerrycanProp)
Expand Down Expand Up @@ -1599,7 +1610,8 @@ RegisterNetEvent('cdn-fuel:jerrycan:refueljerrycan', function(data)
if Config.FuelDebug then print("Config.PlayerOwnedGasStationsEnabled == false or Config.UnlimitedFuel == true, this means reserves will not be changed.") end
end
end
TriggerServerEvent('cdn-fuel:server:PayForFuel', tonumber(refuel.amount) * FuelPrice, "cash", FuelPrice)
local total = (tonumber(refuel.amount) * FuelPrice) + GlobalTax(tonumber(refuel.amount) * FuelPrice)
TriggerServerEvent('cdn-fuel:server:PayForFuel', total, "cash", FuelPrice)
end, function() -- Play When Cancel
SetEntityVisible(fuelnozzle, true, 0)
DeleteObject(JerrycanProp)
Expand Down
3 changes: 3 additions & 0 deletions client/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ function LoadAnimDict(dict)
end

function GlobalTax(value)
if Config.GlobalTax < 0.1 then
return 0
end
local tax = (value / 100 * Config.GlobalTax)
return tax
end
Expand Down
4 changes: 2 additions & 2 deletions server/electric_sv.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ RegisterNetEvent("cdn-fuel:server:electric:OpenMenu", function(amount, inGasStat
TriggerClientEvent("cdn-fuel:client:electric:phone:PayForFuel", src, amount)
else
if Config.Ox.Menu then
TriggerClientEvent('cdn-electric:client:OpenContextMenu', src, total, amount, purchasetype)
TriggerClientEvent('cdn-electric:client:OpenContextMenu', src, math.ceil(total), amount, purchasetype)
else
TriggerClientEvent('qb-menu:client:openMenu', src, {
{
Expand All @@ -35,7 +35,7 @@ RegisterNetEvent("cdn-fuel:server:electric:OpenMenu", function(amount, inGasStat
header = "",
icon = "fas fa-info-circle",
isMenuHeader = true,
txt = Lang:t("menu_purchase_station_header_1")..total..Lang:t("menu_purchase_station_header_2"),
txt = Lang:t("menu_purchase_station_header_1")..math.ceil(total)..Lang:t("menu_purchase_station_header_2"),
},
{
header = Lang:t("menu_purchase_station_confirm_header"),
Expand Down
Loading

0 comments on commit 8e09e76

Please sign in to comment.