From 6fe83def643a979140291d7ff604956ed7d7abca Mon Sep 17 00:00:00 2001 From: bitpredator <67551273+bitpredator@users.noreply.github.com> Date: Mon, 10 Jun 2024 18:14:22 +0200 Subject: [PATCH] convert: esx_billing > bpt_billing --- .../[bpt_addons]/bpt_billing/server/main.lua | 135 ++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 server-data/resources/[bpt_addons]/bpt_billing/server/main.lua diff --git a/server-data/resources/[bpt_addons]/bpt_billing/server/main.lua b/server-data/resources/[bpt_addons]/bpt_billing/server/main.lua new file mode 100644 index 000000000..3458cc6c1 --- /dev/null +++ b/server-data/resources/[bpt_addons]/bpt_billing/server/main.lua @@ -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)