Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/bitpredator/empiretown into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
bitpredator committed Jun 10, 2024
2 parents 3bb154f + 6fe83de commit d8a72f0
Showing 1 changed file with 135 additions and 0 deletions.
135 changes: 135 additions & 0 deletions server-data/resources/[bpt_addons]/bpt_billing/server/main.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
RegisterNetEvent("bpt_billing:sendBill", function(playerId, sharedAccountName, label, amount)
local xPlayer = ESX.GetPlayerFromId(source)
local xTarget = ESX.GetPlayerFromId(playerId)
amount = ESX.Math.Round(amount)

if amount > 0 and xTarget then
if string.match(sharedAccountName, "society_") then
local jobName = string.gsub(sharedAccountName, "society_", "")
if xPlayer.job.name ~= jobName then
print(("[^2ERROR^7] Player ^5%s^7 Attempted to Send bill from a society (^5%s^7), but does not have the correct Job - Possibly Cheats"):format(xPlayer.source, sharedAccountName))
return
end
TriggerEvent("bpt_addonaccount:getSharedAccount", sharedAccountName, function(account)
if account then
MySQL.insert("INSERT INTO billing (identifier, sender, target_type, target, label, amount) VALUES (?, ?, ?, ?, ?, ?)", { xTarget.identifier, xPlayer.identifier, "society", sharedAccountName, label, amount }, function(rowsChanged)
xTarget.showNotification(TranslateCap("received_invoice"))
end)
else
print(("[^2ERROR^7] Player ^5%s^7 Attempted to Send bill from invalid society - ^5%s^7"):format(xPlayer.source, sharedAccountName))
end
end)
else
MySQL.insert("INSERT INTO billing (identifier, sender, target_type, target, label, amount) VALUES (?, ?, ?, ?, ?, ?)", { xTarget.identifier, xPlayer.identifier, "player", xPlayer.identifier, label, amount }, function(rowsChanged)
xTarget.showNotification(TranslateCap("received_invoice"))
end)
end
end
end)

ESX.RegisterServerCallback("bpt_billing:getBills", function(source, cb)
local xPlayer = ESX.GetPlayerFromId(source)

MySQL.query("SELECT amount, id, label FROM billing WHERE identifier = ?", { xPlayer.identifier }, function(result)
cb(result)
end)
end)

ESX.RegisterServerCallback("bpt_billing:getTargetBills", function(source, cb, target)
local xPlayer = ESX.GetPlayerFromId(target)

if xPlayer then
MySQL.query("SELECT amount, id, label FROM billing WHERE identifier = ?", { xPlayer.identifier }, function(result)
cb(result)
end)
else
cb({})
end
end)

ESX.RegisterServerCallback("bpt_billing:payBill", function(source, cb, billId)
local xPlayer = ESX.GetPlayerFromId(source)

MySQL.single("SELECT sender, target_type, target, amount FROM billing WHERE id = ?", { billId }, function(result)
if result then
local amount = result.amount
local xTarget = ESX.GetPlayerFromIdentifier(result.sender)

if result.target_type == "player" then
if xTarget then
if xPlayer.getMoney() >= amount then
MySQL.update("DELETE FROM billing WHERE id = ?", { billId }, function(rowsChanged)
if rowsChanged == 1 then
xPlayer.removeMoney(amount, "Bill Paid")
xTarget.addMoney(amount, "Paid bill")

xPlayer.showNotification(TranslateCap("paid_invoice", ESX.Math.GroupDigits(amount)))
xTarget.showNotification(TranslateCap("received_payment", ESX.Math.GroupDigits(amount)))
end

cb()
end)
elseif xPlayer.getAccount("bank").money >= amount then
MySQL.update("DELETE FROM billing WHERE id = ?", { billId }, function(rowsChanged)
if rowsChanged == 1 then
xPlayer.removeAccountMoney("bank", amount, "Bill Paid")
xTarget.addAccountMoney("bank", amount, "Paid bill")

xPlayer.showNotification(TranslateCap("paid_invoice", ESX.Math.GroupDigits(amount)))
xTarget.showNotification(TranslateCap("received_payment", ESX.Math.GroupDigits(amount)))
end

cb()
end)
else
xTarget.showNotification(TranslateCap("target_no_money"))
xPlayer.showNotification(TranslateCap("no_money"))
cb()
end
else
xPlayer.showNotification(TranslateCap("player_not_online"))
cb()
end
else
TriggerEvent("bpt_addonaccount:getSharedAccount", result.target, function(account)
if xPlayer.getMoney() >= amount then
MySQL.update("DELETE FROM billing WHERE id = ?", { billId }, function(rowsChanged)
if rowsChanged == 1 then
xPlayer.removeMoney(amount, "Bill Paid")
account.addMoney(amount)

xPlayer.showNotification(TranslateCap("paid_invoice", ESX.Math.GroupDigits(amount)))
if xTarget then
xTarget.showNotification(TranslateCap("received_payment", ESX.Math.GroupDigits(amount)))
end
end

cb()
end)
elseif xPlayer.getAccount("bank").money >= amount then
MySQL.update("DELETE FROM billing WHERE id = ?", { billId }, function(rowsChanged)
if rowsChanged == 1 then
xPlayer.removeAccountMoney("bank", amount, "Bill Paid")
account.addMoney(amount)
xPlayer.showNotification(TranslateCap("paid_invoice", ESX.Math.GroupDigits(amount)))

if xTarget then
xTarget.showNotification(TranslateCap("received_payment", ESX.Math.GroupDigits(amount)))
end
end

cb()
end)
else
if xTarget then
xTarget.showNotification(TranslateCap("target_no_money"))
end

xPlayer.showNotification(TranslateCap("no_money"))
cb()
end
end)
end
end
end)
end)

0 comments on commit d8a72f0

Please sign in to comment.